Live data from Hacker News

Goodbye, Node.js Buffer

sindresorhus.com

61–70 of 103 posts

Re: Goodbye, Node.js Buffer

#61
Probably less common, but I write a lot of JS code interfacing with devices over serial connections. This makes Buffer's convenience methods for encoding and decoding data types in both big and little endian ordering really useful to me.

It looks like I could accomplish the same thing using a DataView of an ArrayBuffer, but I don't see enough of a benefit to justify converting everything to this approach.

Re: Goodbye, Node.js Buffer

#62
post #20

Earlier quoted context omitted.

`Uint8Array.prototype.isPrototypeOf` and `instanceof Uint8Array` do not work across realms (frames, Node.js VM, etc). Feel free to copy-paste the function to your own code base if you don't want the dependency: ``` const objectToString = Object.prototype.toString; export function isUint8Array(value) { return value && objectToString.call(value) === '[object Uint8Array]'; } ```

I understand what you're saying, but that's actually in support of my point. This is still extremely trivial code to implement and, from what I can tell, doesn't warrant downloading an NPM package. Have we already forgotten the left-pad fiasco? This isn't meant as a personal attack on anyone, but we really need to frown upon needless dependencies, especially given the growing number of malicious NPM packages.

You're talking to someone who has published well over a thousand packages, many of them tiny.

I suspect your philosophies are irreconcilable.

Re: Goodbye, Node.js Buffer

#63
Having just spent the past few months dealing with the utter carnage caused by updating an old Node project to use modern dependencies, I predict doing this will cause a bloodbath as some libraries update but don't correctly specify semver for the breaking changes, or other libraries that depend on the Buffer usage not bothering to limit their dependencies to a major version unexpectedly breaking when `npm update` is run.

I wonder what unmaintaned libraries will have to be dropped this time as the ecosystem grinds on and causes them to break?

(How did we get to a place where everything is this bad?)

Re: Goodbye, Node.js Buffer

#64

TBH I prefer Buffer as an abstraction. Too bad it didn't make it into native JS standard. I can't think of many use cases in JS land were Uint8Array, Uint16Array, Uint32Array, Int8Array would be absolutely necessary. Seems more useful for perf optimizations. For WebAssembly? Surely a plain array of numbers can be used in most cases. The main use case for Buffer IMO is to convert between different formats like Base64,…

Buffer is a utility. It combines several abstractions that are common in nature into one highly useful class. I quite prefer using it as well.

This is what I feel the node people get right over the ES people, the ES ideology is so abstracted and pushes everything out into small utility classes so you have to create a handful of different objects with odd combinations of methods to get one useful conversion done.

The ES people also seem to have no love of the CLI or for type types of debugging and testing done there. As a result, I almost always choose the node created abstractions over the ES specified ones.

Re: Goodbye, Node.js Buffer

#65
post #54

So the only important difference is that Buffer creates a mutable view at slice(). It could use a better/distinct name for sure, e.g. mutableView(). But do you actually want to always copy a slice that is big enough? This ideological pedantic purism which eliminates practical use cases is in my book just that - impractical pedantic purism. It’s easy to advocate for when your job is shaping landscapes without having t…

> So the only important difference is that Buffer creates a mutable view at slice(). No, it also (from the article): > introduces numerous methods that are not available in other JavaScript environments. Consequently, code leveraging Buffer-specific methods needs polyfilling, preventing many valuable packages from being browser-compatible.

The polyfill argument is good, but it addresses a world where you just push html, css and js files. We live in a different one. It is valid, but more on a wishful thinking side rather than practical. And when you avoid calling it “polyfill”, it simply turns into a useful compatibility/extension library. I find it strange to argue about one dependency but then importing few others, with the only difference in taxonomy.

Re: Goodbye, Node.js Buffer

#66
My absolute go to issue these days with Node is the persistence on doing the same type/interfaces as the browser. The absolute sh* show trying to make a FormData request in Node to work with native Node streams is mind boggling, just because fetch/FormData needs to be EXACTLY like in the browser. Sure, but how about I do not want to put a 100mb file into memory just because I want to use native fetch ....

Re: Goodbye, Node.js Buffer

#67
post #13
post #4

I find buffer useful for its conversion functions to/from different encodings, e.g., `Buffer.from(data, 'hex')` or `Buffer.toString('base64')`. Is there a good way to do this with `Uint8Array`?

You can use ‘atob’ and ‘btoa’ functions for some of that.

Those functions are fundamentally broken: https://developer.mozilla.org/en-US/docs/Glossary/Base64#jav...

See the whole section on converting arbitrary binary data and the complex ways to do it.

Re: Goodbye, Node.js Buffer

#69

> buffers expose private information through global variables, a potential security risk. Does JavaScript's security model let you effectively sandbox scripts running in the same context from each other? If not, then why does this matter?

Consider a web server serving multiple ongoing requests from different users (with separate permissions). Each of them uses a buffer at some point (either a Buffer or a Uint8Array)

If the buffer doesn't have any cross-contamination with global state, there's no way one user could access another's data (because it's behind an object reference that never comes into the scope of the request logic for the other user). But if it did, and a malicious user found some other kind of vulnerability, they could potentially access data across-scopes

Re: Goodbye, Node.js Buffer

#70
post #38

Earlier quoted context omitted.

- Blob: Immutable raw data container with a size and MIME type, not directly readable. - File: Like a Blob, but with additional file-specific properties (e.g., filename). - ArrayBuffer: Fixed-length raw binary data in JavaScript, not directly accessible. - Uint8Array: Interface for reading/writing binary data in ArrayBuffer, showing them as 8-bit unsigned integers. - Buffer: Readable/writable raw binary data containe…

Nit: "fixed-length" is no longer true as of very recently [1]. [1] https://github.com/tc39/proposal-resizablearraybuffer

Now it's bounded-length.
Post reply on HN