Skip to content
This repository has been archived by the owner on Feb 25, 2021. It is now read-only.

Commit

Permalink
Use bufferstream module to properly split client data stream. Resolve…
Browse files Browse the repository at this point in the history
…s a JSON parsing error caused by separator being sent in the middle of a data chunk.
  • Loading branch information
alexkwolfe committed Dec 30, 2013
1 parent 6c49b67 commit 58e8660
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
Empty file modified bin/sandcastle.js
100644 → 100755
Empty file.
30 changes: 12 additions & 18 deletions lib/script.js
Original file line number Diff line number Diff line change
@@ -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, {
Expand Down Expand Up @@ -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);
});

});
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 58e8660

Please sign in to comment.