From 41f0ba217b66c2015496c23307f14a9da52beaf9 Mon Sep 17 00:00:00 2001 From: kumavis Date: Tue, 15 Nov 2016 17:52:18 -0800 Subject: [PATCH 1/7] deps - bump aegir --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2f8d274..0df164c 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "varint": "^4.0.1" }, "devDependencies": { - "aegir": "^6.0.0", + "aegir": "^9.1.2", "chai": "^3.5.0", "pull-block": "^1.0.1", "pull-stream": "^3.4.3" From 8a0750e9c313c0cc3bbd1b5b606777fda3c3853e Mon Sep 17 00:00:00 2001 From: kumavis Date: Tue, 15 Nov 2016 20:29:19 -0800 Subject: [PATCH 2/7] tests - add failing test for decodeFromReader --- test/decode.spec.js | 62 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 test/decode.spec.js diff --git a/test/decode.spec.js b/test/decode.spec.js new file mode 100644 index 0000000..7e4edc1 --- /dev/null +++ b/test/decode.spec.js @@ -0,0 +1,62 @@ +/* eslint-env mocha */ +'use strict' + +const pull = require('pull-stream') +const Reader = require('pull-reader') +const expect = require('chai').expect +const varint = require('varint') +const block = require('pull-block') + +const lp = require('../src') + +describe('pull-length-prefixed decode', () => { + it('decodeFromReader', (done) => { + + const input = [ + new Buffer('haay wuurl!') + ] + + const reader = Reader(1e3) + + // length-prefix encode input + pull( + pull.values(input), + lp.encode(), + reader + ) + + // decode from reader + lp.decodeFromReader(reader, function(err, output){ + if (err) throw err + expect( + output + ).to.be.eql( + input[0] + ) + done() + }) + + }) + + it('decodeFromReader - empty input', (done) => { + + const input = [] + + const reader = Reader(1e3) + + // length-prefix encode input + pull( + pull.values(input), + lp.encode(), + reader + ) + + // decode from reader + lp.decodeFromReader(reader, function(err, output){ + expect(err).to.exist + expect(err).to.be.instanceof(Error) + done() + }) + + }) +}) From 2c4fb5b08620ad348751f8be96099937ca8eb56b Mon Sep 17 00:00:00 2001 From: kumavis Date: Tue, 15 Nov 2016 20:56:15 -0800 Subject: [PATCH 3/7] tests - rename decode -> fromReader --- test/{decode.spec.js => fromReader.spec.js} | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) rename test/{decode.spec.js => fromReader.spec.js} (87%) diff --git a/test/decode.spec.js b/test/fromReader.spec.js similarity index 87% rename from test/decode.spec.js rename to test/fromReader.spec.js index 7e4edc1..7bd3ff8 100644 --- a/test/decode.spec.js +++ b/test/fromReader.spec.js @@ -9,8 +9,8 @@ const block = require('pull-block') const lp = require('../src') -describe('pull-length-prefixed decode', () => { - it('decodeFromReader', (done) => { +describe('pull-length-prefixed decodeFromReader', () => { + it('basic', (done) => { const input = [ new Buffer('haay wuurl!') @@ -38,7 +38,7 @@ describe('pull-length-prefixed decode', () => { }) - it('decodeFromReader - empty input', (done) => { + it('empty input', (done) => { const input = [] @@ -55,6 +55,7 @@ describe('pull-length-prefixed decode', () => { lp.decodeFromReader(reader, function(err, output){ expect(err).to.exist expect(err).to.be.instanceof(Error) + expect(output).to.not.exist done() }) From a833ff89f60d732fa144fbf0b6f7ae0f7435f4c5 Mon Sep 17 00:00:00 2001 From: kumavis Date: Tue, 15 Nov 2016 20:56:57 -0800 Subject: [PATCH 4/7] decodeFromReader - handle unexpected end of stream --- src/decode.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/decode.js b/src/decode.js index 374ef78..63748d2 100644 --- a/src/decode.js +++ b/src/decode.js @@ -20,7 +20,7 @@ function decode (opts) { return (read) => { reader(read) function next () { - decodeFromReader(reader, opts, (err, msg) => { + _decodeFromReader(reader, opts, (err, msg) => { if (err) return p.end(err) p.push(msg) @@ -33,12 +33,23 @@ function decode (opts) { } } +// wrapper to detect sudden pull-stream disconnects function decodeFromReader (reader, opts, cb) { if (typeof opts === 'function') { cb = opts opts = {} } + _decodeFromReader(reader, opts, function onComplete(err, msg){ + if (err) { + if (err === true) return cb(new Error('Unexpected end of input from reader.')) + return cb(err) + } + cb(null, msg) + }) +} + +function _decodeFromReader (reader, opts, cb) { opts = Object.assign({ fixed: false, bytes: 4 From 92d2ced2483d55f7ef79535a5a6a4c5d02b29365 Mon Sep 17 00:00:00 2001 From: kumavis Date: Tue, 15 Nov 2016 21:29:13 -0800 Subject: [PATCH 5/7] decode - add error and test for non-matching prefix --- src/decode.js | 3 +++ test/index.spec.js | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/decode.js b/src/decode.js index 63748d2..df20bc4 100644 --- a/src/decode.js +++ b/src/decode.js @@ -99,6 +99,9 @@ function readVarintMessage (reader, cb) { rawMsgSize = [] + if (msg.length < msgSize) { + return cb (new Error('Message length does not match prefix specified length.')) + } cb(null, msg) }) }) diff --git a/test/index.spec.js b/test/index.spec.js index ab5eb4d..427b67f 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -89,6 +89,28 @@ describe('pull-length-prefixed', () => { ) }) + it('invalid prefix', (done) => { + const input = [ + new Buffer('br34k mai h34rt'), + ] + + pull( + // encode valid input + pull.values(input), + lp.encode(), + // corrupt data + pull.map(data => data.slice(0,-6)), + // attempt decode + lp.decode(), + pull.collect((err, output) => { + expect(err).to.exist + expect(err).to.be.instanceof(Error) + expect(output).to.not.exist + done() + }) + ) + }) + const sizes = [1, 2, 4, 6, 10, 100, 1000] sizes.forEach((size) => { From 64e87c6802ec6d1e795ed9fa408ab5447e997020 Mon Sep 17 00:00:00 2001 From: kumavis Date: Tue, 15 Nov 2016 21:30:17 -0800 Subject: [PATCH 6/7] tests - skip invalid prefix test, pending pull-reader fix --- test/index.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/index.spec.js b/test/index.spec.js index 427b67f..95a1871 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -89,7 +89,7 @@ describe('pull-length-prefixed', () => { ) }) - it('invalid prefix', (done) => { + it.skip('invalid prefix', (done) => { const input = [ new Buffer('br34k mai h34rt'), ] From 4b6ea1dfe6d1306900130dc443b6fa6ecb68cb7a Mon Sep 17 00:00:00 2001 From: kumavis Date: Wed, 16 Nov 2016 11:07:04 -0800 Subject: [PATCH 7/7] lint fixes for the lint god --- src/decode.js | 4 ++-- test/fromReader.spec.js | 14 ++++---------- test/index.spec.js | 4 ++-- 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/src/decode.js b/src/decode.js index df20bc4..1c1fc29 100644 --- a/src/decode.js +++ b/src/decode.js @@ -40,7 +40,7 @@ function decodeFromReader (reader, opts, cb) { opts = {} } - _decodeFromReader(reader, opts, function onComplete(err, msg){ + _decodeFromReader(reader, opts, function onComplete (err, msg) { if (err) { if (err === true) return cb(new Error('Unexpected end of input from reader.')) return cb(err) @@ -100,7 +100,7 @@ function readVarintMessage (reader, cb) { rawMsgSize = [] if (msg.length < msgSize) { - return cb (new Error('Message length does not match prefix specified length.')) + return cb(new Error('Message length does not match prefix specified length.')) } cb(null, msg) }) diff --git a/test/fromReader.spec.js b/test/fromReader.spec.js index 7bd3ff8..534e1e3 100644 --- a/test/fromReader.spec.js +++ b/test/fromReader.spec.js @@ -4,20 +4,17 @@ const pull = require('pull-stream') const Reader = require('pull-reader') const expect = require('chai').expect -const varint = require('varint') -const block = require('pull-block') const lp = require('../src') describe('pull-length-prefixed decodeFromReader', () => { it('basic', (done) => { - const input = [ new Buffer('haay wuurl!') ] const reader = Reader(1e3) - + // length-prefix encode input pull( pull.values(input), @@ -26,7 +23,7 @@ describe('pull-length-prefixed decodeFromReader', () => { ) // decode from reader - lp.decodeFromReader(reader, function(err, output){ + lp.decodeFromReader(reader, function (err, output) { if (err) throw err expect( output @@ -35,15 +32,13 @@ describe('pull-length-prefixed decodeFromReader', () => { ) done() }) - }) it('empty input', (done) => { - const input = [] const reader = Reader(1e3) - + // length-prefix encode input pull( pull.values(input), @@ -52,12 +47,11 @@ describe('pull-length-prefixed decodeFromReader', () => { ) // decode from reader - lp.decodeFromReader(reader, function(err, output){ + lp.decodeFromReader(reader, function (err, output) { expect(err).to.exist expect(err).to.be.instanceof(Error) expect(output).to.not.exist done() }) - }) }) diff --git a/test/index.spec.js b/test/index.spec.js index 95a1871..ccfb17b 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -91,7 +91,7 @@ describe('pull-length-prefixed', () => { it.skip('invalid prefix', (done) => { const input = [ - new Buffer('br34k mai h34rt'), + new Buffer('br34k mai h34rt') ] pull( @@ -99,7 +99,7 @@ describe('pull-length-prefixed', () => { pull.values(input), lp.encode(), // corrupt data - pull.map(data => data.slice(0,-6)), + pull.map(data => data.slice(0, -6)), // attempt decode lp.decode(), pull.collect((err, output) => {