Live data from Hacker News

Goodbye, Node.js Buffer

sindresorhus.com

31–40 of 103 posts

Re: Goodbye, Node.js Buffer

#32

I wonder why you would introduce an extra dependency for the base64 example. It seems more trivial than left pad. https://github.com/sindresorhus/uint8array-extras/blob/cbf24...

> // Required as `btoa` and `atob` don't properly support Unicode: https://developer.mozilla.org/en-US/docs/Glossary/Base64#the...

Re: Goodbye, Node.js Buffer

#33
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.

[deleted]

Re: Goodbye, Node.js Buffer

#34
Personally, I won't make the switch until the utility methods are supported natively. I don't think it's worth the extra package overhead to execute `buf.toString('base64')`. The second that lands, I'm all-in on abandoning buffers.

Re: Goodbye, Node.js Buffer

#35

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,…

Typed arrays are essential for web apps that use WebGL and WebGPU. Being able to send this type of data to run computations on the GPU can give you 1000x speed up. You can see it in action on this WebGL fluid simulator[0] by PavelDoGreat. [0] https://github.com/PavelDoGreat/WebGL-Fluid-Simulation

This simulator os awesome.

Re: Goodbye, Node.js Buffer

#36
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]'; } ```

What do you mean by "across realms"? Is that just another way of saying `Uint8Array.prototype.isPrototypeOf` and `instanceof Uint8Array` are not available in all JS environments? I guess what I'm asking is the definition of a "Javascript Realm" in case I'm thinking it's something different.

https://weizmangal.com/2022/10/28/what-is-a-realm-in-js

Examples of this are frames in the browser and the `vm` module in Node.js.

Re: Goodbye, Node.js Buffer

#37
post #36

Earlier quoted context omitted.

What do you mean by "across realms"? Is that just another way of saying `Uint8Array.prototype.isPrototypeOf` and `instanceof Uint8Array` are not available in all JS environments? I guess what I'm asking is the definition of a "Javascript Realm" in case I'm thinking it's something different.

https://weizmangal.com/2022/10/28/what-is-a-realm-in-js Examples of this are frames in the browser and the `vm` module in Node.js.

Ah thanks, I thought "realms" sounded familiar and that helps clear things up a bit. Also Lavamoat and SES look really interesting thanks for the link.

Re: Goodbye, Node.js Buffer

#38

I had to deal with binary data in a project recently and all the options made my head spin. File, Blob, Buffer, ArrayBuffer, Uint8Array, and so on. Was very confused on what to use!

- 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 container in Node.js (subclass of Uint8Array)

Re: Goodbye, Node.js Buffer

#39
>Buffer also comes with additional caveats. For instance, Buffer#slice() creates a mutable segment linked to the original Buffer, while Uint8Array#slice() creates an immutable copy, resulting in possible unpredictable behavior. The problem is not the behavior of the Buffer#slice() method, but the fact that Buffer is a subclass of Uint8Array, but changes the behavior of an inherited method.

But this is an implementation detail, not specified behavior. Changing method behavior in subclasses is a key aspect of inheritance.

Re: Goodbye, Node.js Buffer

#40
post #3

It's good to see that people care about native JS standards. I'm a bit concerned how Bun seems to be adding their own proprietary APIs to their JS runtime, and can't help feeling that it could be intentional vendor lock-in. Since they are venture backed I'm sure they want some ROI from the project eventually and if they start charging money it will be harder to switch away due to the proprietary APIs without refactor…

It's really because we have some wonderful collaboration between vendors now, and they don't want to have to come up with their own APIs. https://wintercg.org/

Cough. Vercel.
Post reply on HN