-
Notifications
You must be signed in to change notification settings - Fork 97
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
RFC Browser Support & Buffer Module #213
Comments
@viglucci as of me, the Node like Buffer polyfill is the best option
indeed, the configuration of a polyfill is something to be done if not automatically done by build-tool.
I guess this can be compensated with nice documentation. As for me, a standard Buffer API is the best option to decrease the learning curve since standard Node Buffer API already has very nice documentation so we don't have to do anything except document how to configure polyfills for different builds tools. Based on the issue tracker, the other options would cause more challenges rather than benefits since
this requires extra configuration for every client/server setup while the polyfill options can be done once for the whole application and in a single place. |
I am only loosely following this project, but I was wondering why you can't use Uint8Array as that is natively supported by node and browsers? |
@benwiles1 I hadn't considered that and I'm not overly familiar with Uint8Array, but with some really brief searching, it seems like Buffer is a subclass of Uint8Array so it could be promising, and suggests that most of the necessary functionality would be supported. The interesting thing I learned is that the feross/buffer package, which is the most popular Buffer polyfill that I know of actually uses Uint8Array under the hood (according to their docs), so I'm wondering if we should just adopt the feross/buffer package as the Buffer implementation for usage in Node and browsers.
Performance would of course be a consideration, and we would need to do some profiling to see if there is a noticeable overhead to doing so. |
Providing a pleasant experience when using RSocket-js in browser environments is a goal of the work we are doing in #158, and as we approach a preview release, I would like to take a closer look at what changes might be needed, and what options we have.
As demonstrated in #212, we know that the
Buffer
module, which is native to node, but unavailable in Browser environments, is something that will need to be supported in some way (as suspected) as it is required to produce the Binary data involved with sending and receiving payloads.There are a few possible directions to go with this, in no particular order:
A) require consumers to polyfill the
Buffer
module with their build tool of choiceMost frontend build tools (Webpack, etc.) support a mechanism to polyfill Node APIs with alternatives that support browser environments. For example, the webpack
resolve.fallback
configuration can be used to provide an alternative version ofBuffer
that has browser compact.Learn more here.
Pros:
Cons:
B) update APIs to accept browser compatible
Buffer
modules as a dependencyA dependency injection type pattern could be adopted by the public APIs to accept a browser compatible
Buffer
implementation. This pattern would likely involve introducing a similar configuration as thewsCreator
on theWebsocketClientTransport
API. See below.rsocket-js/packages/rsocket-websocket-client/src/WebsocketClientTransport.ts
Line 31 in 285e3b4
Pros:
Cons:
@rsocket/composite-metadata
being one such package.Example:
Changing the
encodeRoutes
API could require adding an additional argument that would provide a browser-compliantBuffer
implementation.Alternatively,
@rsocket/composite-metadata
could be altered to provide a more class-based approach which could provide benefits to dependency injection patterns, but at the potential cost of losing module tree-shaking at build time, due to the loss of individually exported functions. Loss of module tree-shaking may not be a concern though due to the relatively minimal size of the composite-metadata APIs.C) produce browser compatible build artifacts with pre-provided
Buffer
polyfillsCurrently, the project builds for
commonjs
environments, which supports Node as well as build tooling such as Webpack quite well. We could additionally introduce build targets/configurations that would provide a browser-compatibleBuffer
module within scope.rsocket-js/tsconfig.build.json
Line 3 in 285e3b4
Pros:
Cons:
Buffer
polyfill is usedrequire('@rsocket/core/browser')
)I would argue that this approach may be the least appealing approach from the package's maintenance perspective.
The text was updated successfully, but these errors were encountered: