diff --git a/bin/sandcastle.js b/bin/sandcastle.js old mode 100644 new mode 100755 diff --git a/lib/script.js b/lib/script.js index 8f6ea2e..bff7db0 100644 --- a/lib/script.js +++ b/lib/script.js @@ -1,7 +1,9 @@ var _ = require('underscore'), net = require('net'), events = require('events'), - util = require('util'); + util = require('util'), + BufferStream = require('bufferstream'), + Buffer = require('buffer').Buffer; function Script(opts) { _.extend(this, { @@ -66,25 +68,17 @@ Script.prototype.createClient = function(globals) { }, 500); }); - var data = ''; + + var stream = new BufferStream({size:'flexible'}); + stream.split('\u0000'); + stream.on('split', function(chunk) { + client.end(); + _this.onExit(chunk); + }); + client.on('data', function(chunk) { _this.dataReceived = true; - var chunk = chunk.toString(); - if ( chunk.charCodeAt( chunk.length - 1 ) !== 0) { - data += chunk; - // data is still incomplete - return; - } else { - // append all but the separator - data += chunk.substr( 0, chunk.length - 1 ); - client.end(); - } - - // process parsed data - _this.onExit(data.toString()); - - // reset data for the next data transfer - data = ''; + stream.write(chunk); }); }); diff --git a/package.json b/package.json index d84fc3f..d9fb460 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "url": "git://github.com/bcoe/sandcastle.git" }, "dependencies": { + "bufferstream": ">=0.6.x", "optimist": "0.3.4", "underscore": ">=1.4.2", "micro-test": "1.0.0"