Skip to content
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

Buffer.from isn’t supported in all JavaScript runtimes #444

Open
huw opened this issue Aug 16, 2023 · 2 comments
Open

Buffer.from isn’t supported in all JavaScript runtimes #444

huw opened this issue Aug 16, 2023 · 2 comments

Comments

@huw
Copy link
Contributor

huw commented Aug 16, 2023

The new oauth.token method uses Buffer.from under the hood, but Buffer is Node-specific and won’t work in other runtimes.

The recommendations for other runtimes appear to be:

In fact, WinterCG (the standards body governing alternative JS runtime interoperability) mandates btoa on the global scope for compliant runtimes (which is basically any edge runtime that’s not Node or Bun).

The merits of using btoa are mixed. It’s not actually a deprecated API; it’s merely marked as legacy and the recommendation for modern code (especially when using Unicode, which we don’t) is to use something more compatible. If you have a single implementation with known inputs and you know it’s going to create the output correctly, it’s probably fine to use btoa.

I recommend this package uses btoa instead of Buffer.from().toString(“base64”), because it’s more compatible and doesn’t introduce that much of a footgun. The alternative would be to ponyfill buffer, or introduce a separate package just for base64 encoding/decoding; these would add more complexity and downstream dependencies so are probably less favourable.

As always, will gladly PR this.

@Lev-Shapiro
Copy link

Is there any progress with it? It seems during 1 month no one reacted on your issue as well as moved to btoa...

@Lev-Shapiro
Copy link

I've made a pull request to add fix this in case Buffer doesn't exist #461

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants