forked from dignifiedquire/pull-length-prefixed
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* support for empty streams * return immediately on empty stream * compute data.length only once * added test for zero-length streams * fix: properly handle sync streams Co-Authored-By: jacobheun <jacobheun@gmail.com>
- Loading branch information
1 parent
accb16b
commit 33e3a64
Showing
4 changed files
with
125 additions
and
10 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const Buffer = require('safe-buffer').Buffer | ||
|
||
const pull = require('pull-stream') | ||
const expect = require('chai').expect | ||
const varint = require('varint') | ||
|
||
const lp = require('../src') | ||
|
||
describe('pull-length-prefixed', () => { | ||
it('sync stream', (done) => { | ||
const input = [...Array(500).keys()].map(() => Buffer.from('payload')) | ||
|
||
pull( | ||
pull.values(input), | ||
lp.encode(), | ||
pull.collect((err, encoded) => { | ||
if (err) throw err | ||
|
||
expect( | ||
encoded | ||
).to.be.eql( | ||
input.map(data => { | ||
const len = varint.encode(data.length) | ||
return Buffer.concat([ | ||
Buffer.alloc(len.length, len, 'utf8'), | ||
Buffer.alloc(data.length, data, 'utf8') | ||
]) | ||
})) | ||
|
||
pull( | ||
pull.values(encoded), | ||
lp.decode(), | ||
pull.collect((err, output) => { | ||
if (err) throw err | ||
expect( | ||
input | ||
).to.be.eql( | ||
output | ||
) | ||
done() | ||
}) | ||
) | ||
}) | ||
) | ||
}) | ||
}) |