Skip to content

Commit

Permalink
fix(GUI): multiple JSON objects coming up at once from the IPC client
Browse files Browse the repository at this point in the history
The GUI executes the CLI and pipes every line of output coming from
either `stdout` or `stderr` to the IPC server as "messages."

In some scenarios, the child process data handler might receive multiple
lines of output at the same time. Since the CLI outputs stringified JSON
objects each in a different line, trying to parse two JSON objects
separated by a new line causes the parser to crash.

As a solution, we split the data coming in by new lines (`\r\n` in
Windows), and emit a message for only the last one.

Fixes: #898
Changelog-Entry: Fix "Invalid message" error caused by the IPC client emitting multiple JSON objects as a single message.
Change-Type: patch
Signed-off-by: Juan Cruz Viotti <jviotti@openmailbox.org>
  • Loading branch information
Juan Cruz Viotti committed Jan 5, 2017
1 parent e82e6bd commit c5c672c
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/src/child-writer/writer-proxy.js
Expand Up @@ -157,7 +157,21 @@ return isElevated().then((elevated) => {
child.on('close', resolve);

const emitMessage = (data) => {
ipc.of[process.env.IPC_SERVER_ID].emit('message', data.toString());

// Output from stdout/stderr coming from the CLI might be buffered,
// causing several progress lines to come up at once as single message.
// Trying to parse multiple JSON objects separated by new lines will
// of course make the parser confused, causing errors later on.
//
// As a solution, we split the data coming in from the CLI into
// separate lines, and only emit a "message" event for the last one.
//
// Since each line is terminated by a new line, the last string
// is an empty string, which we don't want to send to the IPC
// server, so we take the last element of every element but the last.
const object = _.last(_.initial(_.split(data.toString(), /\r?\n/)))
ipc.of[process.env.IPC_SERVER_ID].emit('message', object);

};

child.stdout.on('data', emitMessage);
Expand Down

0 comments on commit c5c672c

Please sign in to comment.