Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: empty streams #23

Merged
merged 3 commits into from
May 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ sudo: false
language: node_js
node_js:
- 8
- stable
- 10

# Make sure we have new NPM and yarn
before_install:
Expand Down
4 changes: 0 additions & 4 deletions src/decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,6 @@ function readVarintMessage (reader, maxLength, cb) {
return cb(new Error('size longer than max permitted length of ' + maxLength + '!'))
}

if (msgSize <= 0) {
return cb(true) // eslint-disable-line standard/no-callback-literal
}

readMessage(reader, msgSize, (err, msg) => {
if (err) {
return cb(err)
Expand Down
7 changes: 1 addition & 6 deletions src/encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,14 @@ function encode (opts) {
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
if (ended) return cb(ended)

if (!ended && !Buffer.isBuffer(data)) {
ended = new Error('data must be a buffer')
Expand Down
17 changes: 10 additions & 7 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,25 @@ describe('pull-length-prefixed', () => {
pull.values(),
lp.encode(),
pull.collect((err, encoded) => {
if (err) throw err

expect(
encoded
).to.be.eql([Buffer.alloc(1, 0)])
expect(err).to.eql(null)
expect(encoded).to.eql([])

pull(
pull.values(),
pull.values([
Buffer.alloc(0),
Buffer.from('more data')
]),
lp.encode(),
lp.decode(),
pull.collect((err, decoded) => {
if (err) throw err

expect(
decoded
).to.be.eql([])
).to.be.eql([
Buffer.alloc(0),
Buffer.from('more data')
])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense, we should be able to pull an empty buffer.


done()
})
Expand Down