Skip to content

Commit

Permalink
destroy the connection on "fail" events from the proto but re-emit "e…
Browse files Browse the repository at this point in the history
…rror" events
  • Loading branch information
James Halliday committed Jun 19, 2012
1 parent cf9066f commit 87e9b9e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
37 changes: 24 additions & 13 deletions lib/dnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,44 +60,55 @@ dnode.prototype._start = function () {
else self.emit('data', json.stringify(req) + '\n');
});

proto.on('fail', function (err) {
// errors that the remote end was responsible for
self.emit('fail', err);
self.end();
});

proto.on('error', function (err) {
// errors that the local code was responsible for
self.emit('error', err);
});

proto.start();
};

dnode.prototype.write = function (buf) {
var self = this;
var row;

if (buf && typeof buf === 'object'
&& buf.constructor && buf.constructor.name === 'Buffer') {
// treat like a buffer
for (var i = 0; i < buf.length; i++) {
if (buf[i] === 0x0a) {
try { row = json.parse(this._line) }
catch (err) { return this.end() }
try { row = json.parse(self._line) }
catch (err) { return self.end() }

this.proto.handle(row);
this._line = '';
self.proto.handle(row);
self._line = '';
}
else this._line += String.fromCharCode(buf[i])
else self._line += String.fromCharCode(buf[i])
}
}
else if (buf && typeof buf === 'object') {
// .isBuffer() without the Buffer
// Use this to pipe JSONStream.parse() streams.
this.proto.handle(buf);
return;
// Use self to pipe JSONStream.parse() streams.
self.proto.handle(buf);
}
else {
if (typeof buf !== 'string') buf = String(buf);

for (var i = 0; i < buf.length; i++) {
if (buf.charCodeAt(i) === 0x0a) {
try { row = json.parse(this._line) }
catch (err) { return this.end() }
try { row = json.parse(self._line) }
catch (err) { return self.end() }

this.proto.handle(row);
this._line = '';
self.proto.handle(row);
self._line = '';
}
else this._line += buf.charAt(i)
else self._line += buf.charAt(i)
}
}
};
Expand Down
15 changes: 7 additions & 8 deletions test/error.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var dnode = require('../');
var test = require('tap').test;
var destroy = require('./lib/destroy');

test('errors', function (t) {
t.plan(4);
Expand All @@ -8,6 +9,7 @@ test('errors', function (t) {

var server = dnode(function (remote, conn) {
conn.on('error', function (err) {
console.log('ERR! ' + err);
errors.server.push(err);
});

Expand All @@ -24,12 +26,7 @@ test('errors', function (t) {
};
}).listen(port);

var ts = setTimeout(function () {
t.fail('server never ended');
}, 5000);

server.on('end', function () {
clearTimeout(ts);
server.on('close', function () {
t.deepEqual(errors.server[0], 'string throw');

try { undefined.name }
Expand All @@ -46,6 +43,7 @@ test('errors', function (t) {
server.on('listening', function () {
var client = dnode(function (client, conn) {
conn.on('error', function (err) {
console.log('C error: ' + err);
errors.client.push(err);
});

Expand All @@ -63,13 +61,13 @@ test('errors', function (t) {

setTimeout(function () {
conn.end();
server.end();
server.close();
destroy(server);
}, 500);
});
});
});

/*
test('refused', function (t) {
t.plan(2);
Expand Down Expand Up @@ -110,3 +108,4 @@ test('bad connection string', function(t) {
t.end();
}
});
*/

0 comments on commit 87e9b9e

Please sign in to comment.