From 2959d1211c90969ae75b3432e171c6653f4d2f53 Mon Sep 17 00:00:00 2001 From: Payam Yousefi Date: Tue, 6 Dec 2016 14:52:34 -0800 Subject: [PATCH] Fix for issue where incomplete data from the process was being JSON.parse'd causing error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Concat data as chunks, callback on ‘close’ instead --- index.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index f43b588..fee84be 100644 --- a/index.js +++ b/index.js @@ -29,9 +29,13 @@ R.prototype.call = function(_opts, _callback) { var opts = _.isFunction(_opts) ? {} : _opts; this.options.env.input = JSON.stringify([this.d, this.path, opts]); var child = child_process.spawn("Rscript", this.args, this.options); + var body = ""; child.stderr.on("data", callback); child.stdout.on("data", function(d) { - callback(null, JSON.parse(d)); + body += d; + }); + child.on("close", function(code) { + callback(null, JSON.parse(body)); }); };