Closed achingbrain closed 3 years ago
We could also adjust the examples. Eg.: https://github.com/ipfs/js-ipfs/tree/master/examples/ipfs-101
From
console.log('Added file contents:', Buffer.concat(chunks).toString())
To
console.log('Added file contents:', chunks.toString())
or at least make a note about the usage within Node/Browser.
@Weedshaker when this effort bubbles up to a PR for this repo, yes, the documentation will be updated.
Some notes on transitioning:
Buffer.allocUnsafe
is being used it should be retained for Node.js. There is no alternative for Uint8Array and there is a significant performance drop switching to Uint8Array only. Hot paths in Node.js should continue to use Buffer.allocUnsafe
.Buffer.slice(...)
should become Uint8Array.subarray(...)
. Uint8Array's slice
does a clone instead of referencing the underlying memory, which Buffer's slice
and subarray
does. subarray
can be used for both Buffer and Uint8Array.It's worth noting that .subarray
cannot be used with BufferLists, which occasionally complicates matters. .slice
can, and it'll give you a no-copy view of an underlying buffer, unless the requested range falls over two internal buffers, in which case it's a copy operation.
BufferList has “shallowSlice” which is roughly what subarray does, just returning a new BufferList instead of Uint8array. But I would imagine that what you often want from BufferList is still .slice() because you probably need a complete binary value.
-Mikeal
From: Alex Potsides notifications@github.com Sent: Tuesday, August 11, 2020 7:53:04 AM To: ipfs/js-ipfs js-ipfs@noreply.github.com Cc: Subscribed subscribed@noreply.github.com Subject: Re: [ipfs/js-ipfs] Replace node Buffers with Uint8Arrays (#3220)
It's worth noting that .subarray cannot be used with BufferListhttps://www.npmjs.com/package/bls, which occasionally complicates matters.
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/ipfs/js-ipfs/issues/3220#issuecomment-671994548, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAAAEQ6T3ERS552OHDBPVPLSAFLNBANCNFSM4P2DZHJQ.
Is BufferList
usage to be replaced with Uint8ArrayList
(I don't think it currently exists)?
The push here is really to have our ecosystem modules accept Uint8Array
s in place of Buffer
s and have them work without throwing errors.
For converting BufferList
to Uint8ArrayList
I think you'd have to do it for a module if it makes sense, but it may not - lots of our modules wrap other modules (crypto ones in particular) that only accept Buffer
s/BufferList
s so from a practical point of view it may not be necessary or desirable until/if these wrapped modules can be refactored to accept Uint8Array
s.
This shipped in js-ipfs@0.50.0! We did it!
https://github.com/ipfs/js-datastore-idb seems to have been missed
@HexaField until https://github.com/ipfs/js-stores/issues/198 is resolved, that module shouldn't be used anywhere in the stack, instead we should be using datastore-level - are you seeing problems?
In order to treat browsers as first-class citizens, we should not use modules from node core unless we can absolutely guarantee that the code we are writing will not run in a browser.
The next generation of multiformats & IPLD modules will also be
Uint8Array
s all the way down so embracing more modern formats will mean the upgrade path easier to follow when they are ready for use.Modern JavaScript runtimes have
TypedArray
s such asUint8Array
backed byArrayBuffer
s, as well asDataView
s that wrap anArrayBuffer
and allow you to do bit-twiddly operations like writing little endian 32 bit floats at arbitrary offsets, etc.Node's
Buffer
module pre-dates all of this but since node v3 it extends theUint8Array
class, so we should be able to treatBuffer
s asUint8Array
s internally in our stack, though some dependencies may requireBuffer
s as input until they too can be refactored.PRs to modules
IPFS
IPLD
libp2p
Multiformats
Other