From 7c01d735a7290d9171899af3717e954d72c5bbfa Mon Sep 17 00:00:00 2001 From: Anton Medvedev Date: Mon, 25 Feb 2019 01:58:09 +0700 Subject: [PATCH] Fix unexpected usage info on stream end --- index.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 4bc97df..6864cc2 100755 --- a/index.js +++ b/index.js @@ -58,7 +58,9 @@ void function main() { stdin.on('readable', reader.read) stdin.on('end', () => { - handle(reader.value()) + if (!reader.isStream()) { + handle(reader.value()) + } }) }() @@ -135,14 +137,14 @@ function stream() { let len = 0 let depth = 0 let isString = false - let isStream = false - let head = '' + let count = 0 + let head = '' const check = (i) => { if (depth <= 0) { const input = buff.substring(0, len + i + 1) - if (isStream) { + if (count > 0) { if (head !== '') { const json = JSON.parse(head) apply(json) @@ -152,16 +154,19 @@ function stream() { const json = JSON.parse(input) apply(json) } else { - isStream = true head = input } buff = buff.substring(len + i + 1) len = buff.length + count++ } } return { + isStream() { + return count > 1 + }, value() { return head + buff },