Streaming length prefixed buffers with async iterators
npm install it-length-prefixed
const pipe = require('it-pipe')
const lp = require('it-length-prefixed')
const encoded = []
// encode
await pipe(
[Buffer.from('hello world')],
lp.encode(),
async source => {
for await (const chunk of source) {
encoded.push(chunk.slice()) // (.slice converts BufferList to Buffer)
}
}
)
console.log(encoded)
// => [Buffer <0b 68 65 6c 6c 6f 20 77 6f 72 6c 64>]
const decoded = []
// decode
await pipe(
encoded, // e.g. from above
lp.decode(),
async source => {
for await (const chunk of source) {
decoded.push(chunk.slice()) // (.slice converts BufferList to Buffer)
}
}
)
console.log(decoded)
// => [Buffer <68 65 6c 6c 6f 20 77 6f 72 6c 64>]
opts: Object
, optionalpoolSize: 10 * 1024
: Buffer pool size to allocate up front
All messages will be prefixed with a varint.
Returns a transform that yields BufferList
objects.
opts: Object
, optionalmaxDataLength
: If provided, will not decode messages longer than the size specified, if omitted will use the current default of 4MB.
All messages will be prefixed with a varint.
Returns a transform that yields BufferList
objects.
PRs and issues gladly accepted! Check out the issues.
MIT ยฉ 2016 Friedel Ziegelmayer