holepunchto / b4a

Bridging the gap between buffers and typed arrays
Apache License 2.0
43 stars 6 forks source link

Similar to feross/buffer? #1

Closed arj03 closed 2 years ago

arj03 commented 2 years ago

Hi

I see you are starting to change the whole hypercore stack to use this module. I was wondering if you could expand why one would do that instead of using https://github.com/feross/buffer?

Thanks :)

mafintosh commented 2 years ago

Hi! It's about what the core primitive is. Is it a Node.js buffer or a Uint8Array? We're swithing to always having the Uint8Array be the core binary primitive. Why does the distinction matter you say? Here is a silly example:

await core.append(Buffer.from('hi'))
await core.append(new Uint8Array(...))

When using b4a both of those work, as internally we only assume the input is a uint8. If instead you are relying on the Buffer package everywhere you have to special case the code in append to be something along

append (buf) { ...
  if (!Buffer.isBuffer(buf) && isUint8Array(buf)) buf = Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
}

So instead we do the most supported primitive instead, the uint8array - that's in react-native, browsers, in Node. Now in Node.js there are certain times you'd want to use Buffers over uint8s, that's why we have the alloc methods here that return either a uint8 or a Buffer if available (looking at you allocUnsafe). We just never assume it's a buffer, but always a uint8.

Hope that makes it more clear, otherwise feel free to ask.

arj03 commented 2 years ago

Makes sense, thanks