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(price-service-sdk): make price-service-sdk portable #2242

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

cprussin
Copy link
Collaborator

@cprussin cprussin commented Jan 10, 2025

The price-service-sdk package previously used Buffers in it's interface in many places. However, Buffer is a node-specific interface which does not exist in browsers.

Many browsers ship a polyfill, however I've discovered that the polyfill is not actually fully compatible with the Node module. In particular, while the Node module supports aliases such as readUint8 for readUInt8 (note the difference in capitalization), the polyfill does not.

As a result, attempting to utilize price-service-sdk in the browser was either going to cause errors because the Buffer polyfill isn't present, or it would cause errors because of the use of aliased function names.

There were three options to fix this issue:

  1. Switch everything to using unaliased function names. This would work portably, but only assuming that the Buffer polyfill is present.

  2. Switch everything to using Uint8Array, which is a more generic typed array and largely supersedes the need to use the Buffer class at all (and which is a superclass of Buffer). This would work, however it would be a breaking change.

  3. Switch everything to using a generic interface which accepts anything which extends Uint8Array (including Buffer), and use Uint8Array methods exclusively inside price-service-sdk. This requires a few gross typescript hacks to type check correctly and avoid anything being a breaking change, but it makes the code the most portable without requiring any polyfills or requiring a major version bump.

This commit implements option 3.

Copy link

vercel bot commented Jan 10, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
api-reference ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 10, 2025 11:23pm
proposals ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 10, 2025 11:23pm
staking ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 10, 2025 11:23pm
2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
component-library ⬜️ Ignored (Inspect) Visit Preview Jan 10, 2025 11:23pm
insights ⬜️ Ignored (Inspect) Visit Preview Jan 10, 2025 11:23pm

Copy link
Contributor

@tejasbadadare tejasbadadare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm! version bump?

price_service/sdk/js/src/AccumulatorUpdateData.ts Outdated Show resolved Hide resolved
The price-service-sdk package previously used `Buffers` in it's interface in
many places.  However, `Buffer` is a node-specific interface which does not
exist in browsers.

Many browsers ship [a polyfill](https://github.com/feross/buffer), however I've
discovered that the polyfill is not actually fully compatible with the Node
module.  In particular, while the Node module [supports aliases such as
`readUint8` for `readUInt8`](https://nodejs.org/api/buffer.html#bufreaduint8offset) (note the
difference in capitalization), [the polyfill does
not](https://github.com/feross/buffer/blob/master/index.d.ts).

As a result, attempting to utilize `price-service-sdk` in the browser was either
going to cause errors because the Buffer sdk isn't present, or it would cause
errors because of the use of aliased function names.

There were three options to fix this issue:

1. Switch everything to using unaliased function names.  This would work
portably, but only assuming that the Buffer polyfill is present.

2. Switch everything to using
[`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array),
which is a more generic [typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray)
and largely supersedes the need to use the `Buffer` class at all.  This would
work, however it would be a breaking change.

3. Switch everything to using a generic interface which accepts anything which
extends `Uint8Array`.  This requires a few gross typescript hacks to type check
correctly and avoid anything being a breaking change, but it makes the code the
most portable without requiring any major version or any consumer code changes.

This commit implements option 3.
@cprussin cprussin force-pushed the cprussin/make-price-service-sdk-browser-compatible branch from 8c0f107 to 7727d3c Compare January 10, 2025 23:18
@cprussin
Copy link
Collaborator Author

thanks @tejasbadadare , I updated the version

Copy link
Contributor

@guibescos guibescos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to pass the tests, but it's a bit complicated imo tbh

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 this pull request may close these issues.

3 participants