-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make this work on Cloudflare workers #66
Comments
@dylanjha I think that node based crypto (https://www.npmjs.com/package/jsonwebtoken) may not work as well. |
Just looked through the code, saw Totally get that it was build like that, nowadays with all the different environments, Deno, Bun, V8, Workers etc it's really nice to be less Node dependent 🥰 Anyway, this is not stopping us from switching from CF Stream to Mux 🤓 |
For anyone following along, there are a couple core issues here:
Here's an example in TypeScript for creating a signed token in a way that's compatible with Web Crypto: import * as jose from "jose";
let rs = require("jsrsasign");
const alg = "RS256";
export async function sign(
privateKey: any,
claims: any
): Promise<string> {
const pk = atob(privateKey);
const pcks1 = rs.KEYUTIL.getKey(pk);
const pcks8 = rs.KEYUTIL.getPEM(pcks1, "PKCS8PRV");
// WebCrypto needs a pcks8 with no header, footer, or newlines
// So strip these off in the laziest way
const cleanPcks1 = pcks8
.replace("-----BEGIN PRIVATE KEY-----", "")
.replace("-----END PRIVATE KEY-----", "")
.replace(/(\r\n|\n|\r)/gm, "");
const key = await importPrivateKey(cleanPcks1).catch((e) => {
console.log(`Error loading key: ${e}`);
});
const jwt = await new jose.SignJWT(claims)
.setProtectedHeader({ alg })
.setExpirationTime("1d")
// eslint-disable-next-line
// @ts-ignore
.sign(key);
return jwt;
} |
Is there any plan to support edge runtimes? |
Hi folks, version 8 (beta) is available for use and supports other runtimes. See here for details: #327 |
Version 8 is released with compatibility for alternate JS runtimes: |
The
require('fs')
line crashes in cloudflare workers environment.https://github.com/muxinc/mux-node-sdk/blob/master/src/utils/jwt.js#L6
https://community.cloudflare.com/t/cant-resolve-fs-in-cloudflare-workers/112762
The text was updated successfully, but these errors were encountered: