Dependencies: use node or Buffer shim with browsers
tshinnic opened this issue · 8 comments
Because I haven't done Nodejs-only coding in a while, this somehow escaped me on first and second doc read-throughs. The lowest-level data access here is using the Nodejs Buffer
APIs, such as readUint32BE()
rather than the other cross-platform possibilities like DataView
and Typed Arrays
.
The readme needs to be updated to mention the use of Buffer APIs, and thus that additional shims like buffer might be needed when targeting browsers. I see mention of Browserify
in package.json
which when used would automatically add the shim, but I haven't used Browserify in ages either.
Yeah browserify provides buffer. What are you using?
Let me show what I first tried, so you can see my brainfart. Since I was thinking in terms of "generic buffers" that both node and browsers have, I started out with:
buffer = new Uint8Array(testdata01)
stream = new DecodeStream(buffer)
result = struct.decode(stream)
Once I researched and realized 'buffer' here really meant 'Buffer' as in Nodejs Buffer, I changed to
stream = new r.DecodeStream(new Buffer(testdata01))
result = struct.decode(stream)
with resulting success. Testing with mocha under node (v7.7.4).
Since I will be needing code to run on both browser/server I will need to arrange for a nodejs Buffer shim for the browser environment. This can be accomplished in many ways. Outside Browserify/Babel/Webpack/etc buffer can be used.
All I am requesting is a short readme note that the underlying data manipulation here is implemented using Nodejs-style Buffer APIs, and so a Buffer shim - like Browserify or buffer provides - must be loaded explicitly on the browser by the developer if not implicitly like Browserify does.
Is it possible to make restructure to use only Uint8Array
part of Buffer (as Buffer itself is subclass of Uint8Array on recent nodes) that is available both on browsers and node.js so no buffer shim would be needed?
Edit: Now I see how restructure is highly dependent to presence of node.js's Buffer so above request possibly is far from reality for now.
Given we now have typed arrays which behave like buffers, and we also have standardized ReadableStream, is it possible now to re-open this issue and port restructure-next
to support browser native APIs here? This would be a nice bundle saving for fontkit
and PDFKit
use cases at Adobe @devongovett
+1 for browser version. Funny, @benjamind it looks like we're probably working on something similar and I notice your comment is only from a few hours ago.
@benjamind yep, sounds good. Some modernization is needed here for sure. Would you be willing to send a PR?
To be honest it might just be easier to create a small abstraction over BufferBackedObject to align the APIs so it could be easily dropped into place.
This small lib used DataView to achieve a similar result. Would likely need some massaging for API consistency, and a stream adapter but probably roughly the direction it needs to go in?
WIP: #48