logto-io / js

🤓 Logto JS SDKs.
https://docs.logto.io/quick-starts/
MIT License
67 stars 40 forks source link

bug: @logto/node/edge doesn't work with Cloudflare #573

Closed aaronc closed 11 months ago

aaronc commented 1 year ago

Describe the bug

Deploying an app with Cloudflare pages that use the @logto/node/edge client will fail because Buffer is not defined globally. Buffer is used primarily for Base64 encoding/decoding and could be replaced with a platform independent library.

Expected behavior

An alternative to Buffer that works across all edge runtimes would be used with the @logto/node/edge client.

How to reproduce?

Deploy a project using a @logto/node/edge such as @logto/next to Cloudflare pages.

Context

I'm actually using this with Astro instead of Next by adapting the @logto/next. (I can open a PR at some point to contribute an Astro client if that would be desired).

Buffer is used in both node/edge/index.ts and in @silverhand/essentials urlSafeBase64 (it thinks isNode is true on Cloudflare).

There is a very simple workaround to polyfill Buffer in Cloudflare if node compatibility is enabled:

import {Buffer} from 'node:buffer';
globalThis.Buffer = Buffer;

Ideally, however, Cloudflare users wouldn't have to polyfill or even enable node compatibility.

Screenshots

wangsijie commented 1 year ago

Thanks for the report with details, this will be fixed in the next patch release.

aaronc commented 1 year ago

Thanks @wangsijie 🙏

wangsijie commented 11 months ago

https://github.com/logto-io/js/pull/591