Skip to content

Commit 0be4cff

Browse files
committed
Guard against for stream.read returning null in nodejs
1 parent 1396f9d commit 0be4cff

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/io/load.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,15 @@ jBinary.loadData = promising(function (source, callback) {
2828
if (NODE && is(source, ReadableStream)) {
2929
var buffers = [];
3030
source
31-
.on('readable', function () { buffers.push(this.read()) })
32-
.on('end', function () { callback(null, Buffer.concat(buffers)) })
31+
.on('readable', function () {
32+
var buf = this.read();
33+
if(buf) {
34+
buffers.push(buf);
35+
}
36+
})
37+
.on('end', function () {
38+
callback(null, Buffer.concat(buffers));
39+
})
3340
.on('error', callback)
3441
;
3542
} else

test/test.js

+11
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,17 @@ suite('Loading data', function () {
204204
});
205205
}
206206

207+
if (hasNodeRequire && require('stream').Readable) {
208+
test('from file-based readableStream', function (done) {
209+
var stream = require('fs').createReadStream(localFileName);
210+
jBinary.loadData(stream, function (err, data) {
211+
assert.notOk(err, err);
212+
assert.equal(data.byteLength || data.length, 512);
213+
done();
214+
});
215+
});
216+
}
217+
207218
test('with explicit typeset object', function (done) {
208219
var typeSet = {
209220
IS_CORRECT_TYPESET: true

0 commit comments

Comments
 (0)