From 13d5e5ef0fe6ec63a3a0f1c105904d79007bee85 Mon Sep 17 00:00:00 2001 From: zb Date: Wed, 19 Jul 2017 11:43:09 +0200 Subject: [PATCH] add parsefail event --- README.md | 29 ++++++++++++++++++++++++++++- lib/json-stream.js | 9 +++++++-- test/json-stream-test.js | 21 +++++++++++++++++++++ 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d1e62dd..f32ae1c 100644 --- a/README.md +++ b/README.md @@ -26,4 +26,31 @@ Will output: { hello: 'world' } ``` -If invalid JSON gets written, it's silently ignored. +If invalid JSON gets written, it's ignored. + +You can listen to 'parsefail' event on the stream to get the lines that failed to parse. + +```js +var JSONStream = require('json-stream'); + +var stream = JSONStream(); + +stream.on('data', function (chunk) { + console.dir(chunk); +}); +stream.on('parsefail', function (chunk) { + console.dir('!!'+chunk); +}); +stream.write('{"a":'); +stream.write('42}\n'); +stream.write('{"hel'); +stream.write('lo": "world"}\n'); +stream.write('not json\n'); +``` + +Will output: +``` +{ a: 42 } +{ hello: 'world' } +!!not json +``` diff --git a/lib/json-stream.js b/lib/json-stream.js index 269d0a1..97c5bc4 100644 --- a/lib/json-stream.js +++ b/lib/json-stream.js @@ -24,10 +24,15 @@ JSONStream.prototype._transform = function (data, encoding, callback) { while (++ptr <= data.length) { if (data[ptr] === 10 || ptr === data.length) { var line; + var slice = data.slice(start, ptr) try { - line = JSON.parse(data.slice(start, ptr)); + line = JSON.parse(slice); + } + catch (ex) { + if (data[ptr] === 10){ + this.emit('parsefail', slice) + } } - catch (ex) { } if (line) { this.push(line); line = null; diff --git a/test/json-stream-test.js b/test/json-stream-test.js index 48cd2c5..9546678 100644 --- a/test/json-stream-test.js +++ b/test/json-stream-test.js @@ -29,6 +29,22 @@ function expect(stream, events) { }); } +function expectSideEffect(stream, evnetName, events) { + var lines = [], endCalled = false; + stream.on(evnetName, function (line) { + if (line) { + lines.push(line.toString()); + } + }); + stream.on('end', function () { + endCalled = true; + }); + process.on('exit', function () { + assert.deepEqual(lines, events); + assert(endCalled); + }); +} + var stream = JSONStream(); expect(stream, [ { a: 42 } ]); write(stream, '{"a": 42}\n'); @@ -37,6 +53,11 @@ stream = JSONStream(); expect(stream, [ { a: 42 } ]); write(stream, '{"a":', '42}\n'); +stream = JSONStream(); +expect(stream, [ { a: 42 } ]); +expectSideEffect(stream, 'parsefail', [ 'blah' ]); +write(stream, '{"a":', '42}\n','blah\n'); + stream = JSONStream(); expect(stream, [ { a: 42, b: 1337 } ]); write(stream, '{"a":', '42', ',"b": 1337', '}\n');