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

fix: don't reference globalThis.Buffer when env=browser (#967) #999

Merged
merged 1 commit into from
Feb 15, 2024

Conversation

cmd-johnson
Copy link
Contributor

I stumbled over #967 today. The code generated by ts-proto includes references to globalThis.Buffer, even though the env option is set to browser.

While the transpiled code still works in the browser, TypeScript (when using strict mode) complains that error TS7017: Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature..

The fix is relatively simple: update the makeByteUtils function in main.ts and only emit the implementations matching chosen environment:

  • for env=node, the emitted bytesFromBase64 and base64FromBytes functions now exclusively use globalThis.Buffer to de-/encode to and from base64
  • for env=browser, globalThis.btoa / globalThis.atob are used
  • for env=both, the emitted functions work as before: they check for globalThis.Buffer and use either the node or browser implementations accordingly

The generated bytesFromBase64 and base64FromBytes functions now only include code that's required for the specified env:
For env=node, the functions now exclusively use globalThis.Buffer to de-/encode to and from JSON
For env=browser, globalThis.btoa/atob is used
For env=both, the two functions use either the node or browser implementations depending on whether globalThis.Buffer exists
@stephenh
Copy link
Owner

This is great @cmd-johnson , thank you!

@stephenh stephenh merged commit 0d34612 into stephenh:main Feb 15, 2024
6 checks passed
stephenh pushed a commit that referenced this pull request Feb 15, 2024
## [1.167.4](v1.167.3...v1.167.4) (2024-02-15)

### Bug Fixes

* don't reference globalThis.Buffer when env=browser ([#967](#967)) ([#999](#999)) ([0d34612](0d34612))
@stephenh
Copy link
Owner

🎉 This PR is included in version 1.167.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

2 participants