From 6ceacd87198a614e83319fab073158a1ef6af195 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20Kooi?= Date: Tue, 14 Jan 2020 16:48:04 +0100 Subject: [PATCH 1/2] emit 'error' event for invalid json instead of crashing the process immediately. this will allow clinic doctor to print a helpful error message when the trace event output is unexpected. usually that means that multiple node.js processes are writing interleaved trace event data. --- index.js | 13 +++++++++---- test.js | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index c823718..13beff7 100644 --- a/index.js +++ b/index.js @@ -37,11 +37,16 @@ class Parser extends Transform { parse.pointer++ this.push(msg) } catch (err) { - const rem = '[' + str.slice(parse.pointer, last + 2) + ']' - for (const msg of JSON.parse(rem)) { - this.push(msg) + try { + const rem = '[' + str.slice(parse.pointer, last + 2) + ']' + for (const msg of JSON.parse(rem)) { + this.push(msg) + } + parse.pointer = last + 3 + } catch (err) { + cb(err) + return } - parse.pointer = last + 3 } } this._buffer = str.slice(parse.pointer) diff --git a/test.js b/test.js index 789a282..18405a5 100644 --- a/test.js +++ b/test.js @@ -89,3 +89,26 @@ tape('chunked', function (t) { t.end() }) }) + +tape('invalid json', function (t) { + t.plan(2) + const parse = parser() + const expected = [sample()] + const s = '{"traceEvents":[' + JSON.stringify(expected[0]) + ', {"x":"x"]}},]}' + + for (var i = 0; i < s.length; i++) { + parse.write(s.slice(i, i + 1)) + } + parse.end() + + parse + .on('data', function (data) { + t.same(data, expected.shift()) + }) + .on('error', function (error) { + t.ok(error) + }) + .on('end', function () { + t.fail() + }) +}) From 315102ddc1b951ca8d598cc9f774b4068e934619 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20Kooi?= Date: Tue, 14 Jan 2020 16:50:01 +0100 Subject: [PATCH 2/2] Disable package-lock.json --- .npmrc | 1 + 1 file changed, 1 insertion(+) create mode 100644 .npmrc diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false