Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(GUI): don't split robot input by new lines inside properties #1008

Merged
merged 1 commit into from
Jan 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/shared/child-writer/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

'use strict';

const _ = require('lodash');

/**
* @summary Get the explicit boolean form of an argument
* @function
Expand Down Expand Up @@ -85,3 +87,29 @@ exports.getCLIWriterArguments = (options) => {

return argv;
};

/**
* @summary Split stringified object lines
* @function
* @public
*
* @description
* This function takes special care to not consider new lines
* inside the object properties.
*
* @param {String} lines - lines
* @returns {String[]} split lines
*
* @example
* const result = utils.splitObjectLines('{"foo":"bar"}\n{"hello":"Hello\nWorld"}');
* console.log(result);
*
* > [ '{"foo":"bar"}', '{"hello":"Hello\nWorld"}' ]
*/
exports.splitObjectLines = (lines) => {
return _.chain(lines)
.split(/((?:[^\n"']|"[^"]*"|'[^']*')+)/)
.map(_.trim)
.reject(_.isEmpty)
.value();
};
11 changes: 3 additions & 8 deletions lib/shared/child-writer/writer-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const _ = require('lodash');
const os = require('os');
const path = require('path');
const sudoPrompt = Bluebird.promisifyAll(require('sudo-prompt'));
const utils = require('./utils');
const EXIT_CODES = require('../exit-codes');
const packageJSON = require('../../../package.json');

Expand Down Expand Up @@ -162,14 +163,8 @@ return isElevated().then((elevated) => {
// 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/)));
// As a solution, we only consider the last message.
const object = _.last(utils.splitObjectLines(data.toString()));
ipc.of[process.env.IPC_SERVER_ID].emit('message', object);

};
Expand Down
81 changes: 81 additions & 0 deletions tests/shared/child-writer/utils.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2016 resin.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const m = require('mochainon');
const utils = require('../../../lib/shared/child-writer/utils');

describe('Shared: ChildWriter Utils', function() {

describe('.splitObjectLines()', function() {

it('should split multiple object lines', function() {
const input = '{"id":"foo"}\n{"id":"bar"}\n{"id":"baz"}';
m.chai.expect(utils.splitObjectLines(input)).to.deep.equal([
'{"id":"foo"}',
'{"id":"bar"}',
'{"id":"baz"}'
]);
});

it('should ignore spaces in between', function() {
const input = '{"id":"foo"} \n {"id":"bar"}\n {"id":"baz"}';
m.chai.expect(utils.splitObjectLines(input)).to.deep.equal([
'{"id":"foo"}',
'{"id":"bar"}',
'{"id":"baz"}'
]);
});

it('should ignore multiple new lines', function() {
const input = '{"id":"foo"}\n\n\n\n{"id":"bar"}\n\n{"id":"baz"}';
m.chai.expect(utils.splitObjectLines(input)).to.deep.equal([
'{"id":"foo"}',
'{"id":"bar"}',
'{"id":"baz"}'
]);
});

it('should ignore new lines inside properties', function() {
const input = '{"id":"foo\nbar"}\n{"id":"\nhello\n"}';
m.chai.expect(utils.splitObjectLines(input)).to.deep.equal([
'{"id":"foo\nbar"}',
'{"id":"\nhello\n"}'
]);
});

it('should handle carriage returns', function() {
const input = '{"id":"foo"}\r\n{"id":"bar"}\r\n{"id":"baz"}';
m.chai.expect(utils.splitObjectLines(input)).to.deep.equal([
'{"id":"foo"}',
'{"id":"bar"}',
'{"id":"baz"}'
]);
});

it('should ignore multiple carriage returns', function() {
const input = '{"id":"foo"}\r\n\r\n{"id":"bar"}\r\n\r\n\r\n{"id":"baz"}';
m.chai.expect(utils.splitObjectLines(input)).to.deep.equal([
'{"id":"foo"}',
'{"id":"bar"}',
'{"id":"baz"}'
]);
});

});

});