node-worker-pool
Advanced tools
Comparing version 2.4.2 to 2.4.3
"use strict"; | ||
function JSONStreamParser(initialStreamOffset) { | ||
function JSONStreamParser() { | ||
this._currentlyWithinQuotedString = false; | ||
this._currentObjectHead = 0; | ||
this._cursorPosition = initialStreamOffset || 0; | ||
this._depth = 0; | ||
this._leftOverStreamText = ''; | ||
this._buffer = ''; | ||
} | ||
JSONStreamParser.prototype.parse=function(streamText) { | ||
var cursor = this._buffer.length; | ||
this._buffer += streamText; | ||
var currChar; | ||
var responses = []; | ||
while (this._cursorPosition < streamText.length) { | ||
currChar = streamText.charAt(this._cursorPosition); | ||
while (cursor < this._buffer.length) { | ||
currChar = this._buffer.charAt(cursor); | ||
if (this._currentlyWithinQuotedString && currChar === '\\') { | ||
// If the current character is escaped, move forward | ||
this._cursorPosition++; | ||
cursor++; | ||
} else if (currChar === '"') { | ||
@@ -24,5 +24,2 @@ // Are we inside a quoted string? | ||
if (currChar === '{') { | ||
if (this._depth === 0) { | ||
this._currentObjectHead = this._cursorPosition; | ||
} | ||
this._depth++; | ||
@@ -32,12 +29,10 @@ } else if (currChar === '}') { | ||
if (this._depth === 0) { | ||
responses.push(JSON.parse( | ||
streamText.substring( | ||
this._currentObjectHead, | ||
this._cursorPosition + 1 | ||
) | ||
)); | ||
responses.push(JSON.parse(this._buffer.substring(0, cursor + 1))); | ||
this._buffer = this._buffer.substring(cursor + 1); | ||
cursor = 0; | ||
continue; | ||
} | ||
} | ||
} | ||
this._cursorPosition++; | ||
cursor++; | ||
} | ||
@@ -47,3 +42,6 @@ return responses; | ||
JSONStreamParser.prototype.getBuffer=function() { | ||
return this._buffer; | ||
} | ||
module.exports = JSONStreamParser; |
@@ -19,3 +19,2 @@ var JSONStreamParser = require('./lib/JSONStreamParser'); | ||
process.stdin.setEncoding('utf8'); | ||
var inputData = ''; | ||
var inputStreamParser = new JSONStreamParser(); | ||
@@ -27,4 +26,3 @@ | ||
process.stdin.on('data', function(data) { | ||
inputData += data; | ||
var rcvdMsg = inputStreamParser.parse(inputData); | ||
var rcvdMsg = inputStreamParser.parse(data); | ||
if (rcvdMsg.length === 1) { | ||
@@ -31,0 +29,0 @@ if (initialized === false) { |
{ | ||
"name": "node-worker-pool", | ||
"version": "2.4.2", | ||
"version": "2.4.3", | ||
"dependencies": { | ||
@@ -5,0 +5,0 @@ "q": "~0.9.7" |
@@ -22,3 +22,2 @@ "use strict"; | ||
this._stderrData = ''; | ||
this._stdoutData = ''; | ||
this._streamParser = new JSONStreamParser(); | ||
@@ -70,6 +69,3 @@ this._workerArgs = workerArgs; | ||
'stderr:\n' + | ||
' ' + this._stderrData.trim() + '\n' + | ||
'\n' + | ||
'stdout:\n' + | ||
' ' + this._stdoutData.trim(); | ||
' ' + this._stderrData.trim() + '\n'; | ||
@@ -102,9 +98,7 @@ if (this._initialized === false) { | ||
this._stdoutData += data; | ||
var responses; | ||
try { | ||
responses = this._streamParser.parse(this._stdoutData); | ||
responses = this._streamParser.parse(data); | ||
} catch (e) { | ||
e = new Error('Unable to parse child response data: ' + this._stdoutData); | ||
e = new Error('Unable to parse child response data: ' + this._streamParser.getBuffer()); | ||
if (this._initialized === false) { | ||
@@ -111,0 +105,0 @@ throw e; |
Sorry, the diff of this file is not supported yet
16003
389