Comparing version 0.6.6 to 0.6.7
145
index.js
@@ -30,3 +30,3 @@ var net = require('net'); | ||
var stream = params.stream; | ||
var client = this.proto.create(); | ||
var client = null; | ||
@@ -40,23 +40,22 @@ if (params.port) { | ||
if (params.reconnect) { | ||
var args = arguments; | ||
stream.on('error', (function (err) { | ||
if (err.code === 'ECONNREFUSED') { | ||
this.emit('refused'); | ||
if (client) client.emit('refused'); | ||
setTimeout((function () { | ||
this.emit('reconnect'); | ||
if (client) client.emit('reconnect'); | ||
dnode.prototype.connect.apply(this, args); | ||
}).bind(this), params.reconnect); | ||
} | ||
else if (client) client.emit('error', err) | ||
else this.emit('error', err) | ||
}).bind(this)); | ||
this.once('end', (function () { | ||
stream.once('end', (function () { | ||
if (!params.reconnect) return; | ||
this.emit('drop'); | ||
client.emit('drop'); | ||
setTimeout((function () { | ||
if (!params.reconnect) return; | ||
this.emit('reconnect'); | ||
client.emit('reconnect'); | ||
dnode.prototype.connect.apply(this, args); | ||
@@ -67,39 +66,40 @@ }).bind(this), params.reconnect); | ||
else { | ||
stream.on('error', this.emit.bind(this, 'error')); | ||
stream.on('error', (function (err) { | ||
if (client) client.emit('error', err) | ||
else this.emit('error', err) | ||
}).bind(this)); | ||
} | ||
stream.on('end', this.emit.bind(this, 'end')); | ||
var args = arguments; | ||
stream.on('connect', (function () { | ||
client = createClient(this.proto, stream); | ||
client.end = function () { | ||
if (params.reconnect) params.reconnect = 0; | ||
stream.end(); | ||
}; | ||
this.stack.forEach(function (middleware) { | ||
middleware.call(client.instance, client.remote, client); | ||
}); | ||
if (params.block) { | ||
client.on('remote', function () { | ||
params.block.call(client.instance, client.remote, client); | ||
}); | ||
} | ||
process.nextTick(function () { | ||
if (client.listeners('error').length === 0) { | ||
// default error handler to keep everything from crashing | ||
client.on('error', function (err) { | ||
console.error(err && err.stack || err); | ||
}) | ||
} | ||
}); | ||
client.start(); | ||
this.emit('connect'); | ||
}).bind(this)); | ||
client.end = function () { | ||
if (params.reconnect) params.reconnect = 0; | ||
stream.end(); | ||
}; | ||
client.destroy = stream.destroy.bind(stream); | ||
client.stream = stream; | ||
this.stack.forEach(function (middleware) { | ||
middleware.call(client.instance, client.remote, client); | ||
}); | ||
client.on('request', function (req) { | ||
stream.write(JSON.stringify(req) + '\n'); | ||
}); | ||
if (params.block) { | ||
client.on('remote', function () { | ||
params.block.call(client.instance, client.remote, client); | ||
}); | ||
} | ||
Lazy(stream).lines | ||
.map(String) | ||
.forEach(client.parse) | ||
; | ||
return this; | ||
@@ -134,10 +134,5 @@ }; | ||
server.on('connection', (function (stream) { | ||
var client = this.proto.create(); | ||
var client = createClient(this.proto, stream); | ||
clients[client.id] = client; | ||
client.stream = stream; | ||
client.end = stream.end.bind(stream); | ||
client.destroy = stream.destroy.bind(stream); | ||
stream.on('end', client.emit.bind(client, 'end')); | ||
this.stack.forEach(function (middleware) { | ||
@@ -147,9 +142,2 @@ middleware.call(client.instance, client.remote, client); | ||
client.on('request', function (req) { | ||
if (stream.writable) { | ||
stream.write(JSON.stringify(req) + '\n'); | ||
} | ||
// silently ignore data sent to non-writable streams | ||
}); | ||
if (params.block) { | ||
@@ -161,7 +149,2 @@ client.on('remote', function () { | ||
Lazy(stream).lines | ||
.map(String) | ||
.forEach(client.parse) | ||
; | ||
client.start(); | ||
@@ -171,8 +154,44 @@ }).bind(this)); | ||
server.on('error', this.emit.bind(this, 'error')); | ||
if (!this.servers) this.servers = []; | ||
this.servers.push(server); | ||
this.server = server; | ||
server.on('close', this.emit.bind(this, 'close')); | ||
return this; | ||
}; | ||
function createClient (proto, stream) { | ||
var client = proto.create(); | ||
process.nextTick(function () { | ||
if (client.listeners('error').length === 0) { | ||
// default error handler to keep everything from crashing | ||
client.on('error', function (err) { | ||
console.error(err && err.stack || err); | ||
}) | ||
} | ||
}); | ||
client.stream = stream; | ||
client.end = stream.end.bind(stream); | ||
client.destroy = stream.destroy.bind(stream); | ||
stream.on('end', client.emit.bind(client, 'end')); | ||
client.on('request', function (req) { | ||
if (stream.writable) { | ||
stream.write(JSON.stringify(req) + '\n'); | ||
} | ||
else { | ||
client.emit('dropped', req); | ||
} | ||
}); | ||
Lazy(stream).lines | ||
.map(String) | ||
.forEach(client.parse) | ||
; | ||
return client; | ||
} | ||
dnode.prototype.end = function () { | ||
@@ -188,11 +207,3 @@ Object.keys(this.proto.sessions) | ||
dnode.prototype.close = function () { | ||
var servers = this.servers || []; | ||
var waiting = servers.length; | ||
servers.forEach((function (server) { | ||
server.once('close', (function () { | ||
waiting --; | ||
if (waiting === 0) this.emit('close'); | ||
}).bind(this)); | ||
server.close(); | ||
}).bind(this)); | ||
this.server.close(); | ||
}; | ||
@@ -199,0 +210,0 @@ |
{ | ||
"name" : "dnode", | ||
"version" : "0.6.6", | ||
"version" : "0.6.7", | ||
"description" : "freestyle RPC like a boss", | ||
@@ -5,0 +5,0 @@ "main" : "./index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
125750
55
2978