forked from dignifiedquire/pull-length-prefixed
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alan Shaw
committed
May 4, 2019
1 parent
7090f5d
commit d5f6d03
Showing
5 changed files
with
123 additions
and
100 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,35 @@ | ||
'use strict' | ||
|
||
const Buffer = require('safe-buffer').Buffer | ||
const Varint = require('varint') | ||
const BufferList = require('bl') | ||
|
||
module.exports = encode | ||
|
||
const poolSize = 10 * 1024 | ||
|
||
function encode (opts) { | ||
opts = Object.assign({ | ||
fixed: false | ||
}, opts || {}) | ||
|
||
// Only needed for varint | ||
const varint = require('varint') | ||
let pool = opts.fixed ? null : createPool() | ||
let used = 0 | ||
|
||
let ended = false | ||
let first = true | ||
|
||
return (read) => (end, cb) => { | ||
if (end) ended = end | ||
if (ended) return cb(ended) | ||
|
||
read(null, (end, data) => { | ||
if (end) ended = end | ||
if (ended && !first) { | ||
return cb(ended) | ||
} | ||
|
||
first = false | ||
const MIN_POOL_SIZE = 147 // Varint.encode(Number.MAX_VALUE).length | ||
const DEFAULT_POOL_SIZE = 10 * 1024 | ||
|
||
if (!ended && !Buffer.isBuffer(data)) { | ||
ended = new Error('data must be a buffer') | ||
return cb(ended) | ||
} | ||
function encode (options) { | ||
options = options || {} | ||
options.poolSize = Math.max(options.poolSize || DEFAULT_POOL_SIZE, MIN_POOL_SIZE) | ||
|
||
const dataLength = ended ? 0 : data.length | ||
return source => (async function * () { | ||
let pool = Buffer.alloc(options.poolSize) | ||
let poolOffset = 0 | ||
|
||
let encodedLength | ||
if (opts.fixed) { | ||
encodedLength = Buffer.alloc(4) | ||
encodedLength.writeInt32BE(dataLength, 0) // writes exactly 4 bytes | ||
} else { | ||
varint.encode(dataLength, pool, used) | ||
used += varint.encode.bytes | ||
encodedLength = pool.slice(used - varint.encode.bytes, used) | ||
for await (const chunk of source) { | ||
Varint.encode(chunk.length, pool, poolOffset) | ||
poolOffset += Varint.encode.bytes | ||
const encodedLength = pool.slice(poolOffset - Varint.encode.bytes, poolOffset) | ||
|
||
if (pool.length - used < 100) { | ||
pool = createPool() | ||
used = 0 | ||
} | ||
if (pool.length - poolOffset < MIN_POOL_SIZE) { | ||
pool = Buffer.alloc(options.poolSize) | ||
poolOffset = 0 | ||
} | ||
|
||
if (ended) { | ||
return cb(null, encodedLength) | ||
} | ||
|
||
cb(null, Buffer.concat([ | ||
encodedLength, | ||
data | ||
], (opts.fixed ? 4 : varint.encode.bytes) + dataLength)) | ||
}) | ||
} | ||
yield new BufferList().append(encodedLength).append(chunk) | ||
// yield Buffer.concat([encodedLength, chunk]) | ||
} | ||
})() | ||
} | ||
|
||
function createPool () { | ||
return Buffer.alloc(poolSize) | ||
} | ||
module.exports = encode | ||
module.exports.MIN_POOL_SIZE = MIN_POOL_SIZE | ||
module.exports.DEFAULT_POOL_SIZE = DEFAULT_POOL_SIZE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const pipe = require('it-pipe') | ||
const { expect } = require('chai') | ||
const randomInt = require('random-int') | ||
const randomBytes = require('random-bytes') | ||
const { map, collect } = require('streaming-iterables') | ||
const Varint = require('varint') | ||
|
||
const lp = require('../') | ||
const { MIN_POOL_SIZE } = lp.encode | ||
|
||
const times = (n, fn) => Array.from(Array(n), fn) | ||
const someBytes = n => randomBytes(randomInt(1, n || 32)) | ||
|
||
describe('encode', () => { | ||
it('should encode length as prefix', async () => { | ||
const input = await Promise.all(times(randomInt(1, 10), someBytes)) | ||
const output = await pipe(input, lp.encode(), map(c => c.slice()), collect) | ||
output.forEach((o, i) => { | ||
const length = Varint.decode(o) | ||
expect(length).to.equal(input[i].length) | ||
expect(o.slice(Varint.decode.bytes)).to.deep.equal(input[i]) | ||
}) | ||
}) | ||
|
||
it('should encode zero length as prefix', async () => { | ||
const input = [Buffer.alloc(0)] | ||
const output = await pipe(input, lp.encode(), map(c => c.slice()), collect) | ||
output.forEach((o, i) => { | ||
const length = Varint.decode(o) | ||
expect(length).to.equal(input[i].length) | ||
expect(o.slice(Varint.decode.bytes)).to.deep.equal(input[i]) | ||
}) | ||
}) | ||
|
||
it('should re-allocate buffer pool when empty', async () => { | ||
const input = await Promise.all(times(MIN_POOL_SIZE * 2, someBytes)) | ||
const output = await pipe( | ||
input, | ||
lp.encode({ poolSize: MIN_POOL_SIZE * 1.5 }), | ||
map(c => c.slice()), | ||
collect | ||
) | ||
output.forEach((o, i) => { | ||
const length = Varint.decode(o) | ||
expect(length).to.equal(input[i].length) | ||
expect(o.slice(Varint.decode.bytes)).to.deep.equal(input[i]) | ||
}) | ||
}) | ||
}) |