diff --git a/lib/json-stream.js b/lib/json-stream.js index 269d0a1..71dff50 100644 --- a/lib/json-stream.js +++ b/lib/json-stream.js @@ -31,6 +31,7 @@ JSONStream.prototype._transform = function (data, encoding, callback) { if (line) { this.push(line); line = null; + start = ptr; } if (data[ptr] === 10) start = ++ptr; } diff --git a/test/json-stream-test.js b/test/json-stream-test.js index 48cd2c5..68fd8e5 100644 --- a/test/json-stream-test.js +++ b/test/json-stream-test.js @@ -14,11 +14,8 @@ function write(stream) { function expect(stream, events) { var chunks = [], endCalled = false; - stream.on('readable', function () { - var chunk = stream.read(); - if (chunk) { - chunks.push(chunk); - } + stream.on('data', function (chunk) { + chunks.push(chunk); }); stream.on('end', function () { endCalled = true; @@ -37,6 +34,10 @@ stream = JSONStream(); expect(stream, [ { a: 42 } ]); write(stream, '{"a":', '42}\n'); +stream = JSONStream(); +expect(stream, [ { a: 42 } ]); +write(stream, '{"a":', '42}', '\n'); + stream = JSONStream(); expect(stream, [ { a: 42, b: 1337 } ]); write(stream, '{"a":', '42', ',"b": 1337', '}\n'); diff --git a/test/throw-in-readable-test.js b/test/throw-in-readable-test.js index 378f30a..a01f877 100644 --- a/test/throw-in-readable-test.js +++ b/test/throw-in-readable-test.js @@ -3,10 +3,10 @@ var assert = require('assert'), var stream = JSONStream(); -stream.on('readable', function () { +stream.on('data', function () { throw new Error('This should crash and burn.'); }); assert.throws(function () { stream.write('{"a":"b"}\n'); -}, 'write with a throwing `readable` handler should throw'); +}, 'write with a throwing `data` handler should throw');