MasterKale / SimpleWebAuthn

WebAuthn, Simplified. A collection of TypeScript-first libraries for simpler WebAuthn integration. Supports modern browsers, Node, Deno, and more.
https://simplewebauthn.dev
MIT License
1.62k stars 137 forks source link

Fix `@simplewebauthn/server` not working in edge environments #455

Closed Maronato closed 1 year ago

Maronato commented 1 year ago

One of @simplewebauthn/server dependencies cbor-x, supports streams, which uses Node's and Deno's stream interface.

Unfortunately, stream is not part of the standard Web APIs that many edge compute runtimes use (e.g. Vercel doesn't support it, and Cloudflare requires a separate node: namespace). Importing it throws errors like these: Screenshot 2023-10-06 at 13 09 30

Since the only object used from cbor-x is Encoder, the fix is pretty simple. Instead of importing the entire package, we only import its encode file, which is officially exported. This avoids loading the stream.js file that uses stream.

I tested it by patching @simplewebauthn/server directly in my node_modules and it worked without issues, but I haven't attempted to build it locally. I don't see a reason for it not building, though.

MasterKale commented 1 year ago

Let's see what CI has to say about this, we'll find out quick if this is works. 🤞

MasterKale commented 1 year ago

CI failure:

error: Uncaught "The following specifiers were indicated to be mapped to a package, but were not found:\n * https://deno.land/x/cbor@v1.5.2/index.js"

You also need to update the path mapping this to an NPM package here:

https://github.com/MasterKale/SimpleWebAuthn/blob/master/packages/server/build_npm.ts#L73

But since we're not importing index.js I don't know hot dnt will handle this 🤔

Maronato commented 1 year ago

Looks like we can use subPath. I'm trying to build it now

Maronato commented 1 year ago

All tests passed with act Screenshot 2023-10-06 at 14 26 54

Maronato commented 1 year ago

Hi @MasterKale! CI should pass now. Let me know if you need anything else :)

MasterKale commented 1 year ago

Thank you for your contribution, @Maronato, this looks good to me :shipit:

MasterKale commented 1 year ago

@Maronato This fix is now available in @simplewebauthn/server@8.3.2