Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

removed cleartext piping hack + refctoring + test

  • Loading branch information...
commit d64f69deff7736b4d7aafc57ac882911e4676909 1 parent 8c35f04
@kilianc kilianc authored
View
2  Makefile
@@ -1,7 +1,7 @@
REPORTER = spec
test:
- @NODE_ENV=test ./node_modules/.bin/mocha test-mocha/*.test.js $(OPT) --colors --reporter $(REPORTER)
+ @NODE_ENV=test ./node_modules/.bin/mocha test/*.test.js $(OPT) --colors --reporter $(REPORTER)
test-bail:
$(MAKE) test OPT=--bail
View
31 examples/bla.js
@@ -1,18 +1,15 @@
-
-var nssocket = require('../lib/nssocket');
-
-var sockets = [];
-var server = nssocket.createServer(function (socket) {
-
- sockets.push(socket);
-
- socket.data('Connecting', function (data) {
- console.log("There are now", sockets.length);
-
- for(var i=0, l=sockets.length; i<l; i++) {
- sockets[i].send('Broadcasting', data);
+var nssocket = require('../')
+
+var sockets = []
+nssocket.createServer(function (socket) {
+ sockets.push(socket)
+ socket.ondata('connecting', function (data) {
+ console.log('There are now', sockets.length)
+ sockets.forEach(function (s) {
+ if (socket !== socket) {
+ s.send('broadcasting', data)
}
- console.dir(data);
- });
-
-}).listen(4949);
+ })
+ console.dir(data)
+ })
+}).listen(4949)
View
16 examples/foo.js
@@ -1,10 +1,8 @@
-var nssocket = require('../lib/nssocket');
-var outbound = new nssocket.NsSocket();
-
-outbound.data('Broadcasting', function (data) {
- console.log(data)
-});
-
-outbound.connect(4949);
+var nssocket = require('../')
+var outbound = nssocket()
-outbound.send('Connecting', { "random": Math.random() });
+outbound.on('data::broadcasting', function (data) {
+ console.log(data)
+}).connect(4949, function () {
+ outbound.send('connecting', { 'random' : Math.random() })
+})
View
31 examples/reconnect.js
@@ -1,29 +1,12 @@
var net = require('net'),
- nssocket = require('../lib/nssocket');
+ nssocket = require('../')
net.createServer(function (socket) {
- //
- // Close the underlying socket after `1000ms`
- //
setTimeout(function () {
- socket.destroy();
- }, 1000);
-}).listen(8345);
+ socket.destroy()
+ }, 1000)
+}).listen(8345)
-//
-// Create an NsSocket instance with reconnect enabled
-//
-var socket = new nssocket.NsSocket({
- reconnect: true,
- type: 'tcp4',
-});
-
-socket.on('start', function () {
- //
- // The socket will emit this event periodically
- // as it attempts to reconnect
- //
- console.dir('start');
-});
-
-socket.connect(8345);
+nssocket({ reconnect: true }).on('start', function () {
+ console.log('start')
+}).connect(8345)
View
30 examples/simple-protocol.js
@@ -1,10 +1,10 @@
-var nssocket = require('../lib/nssocket');
+var nssocket = require('../')
//
// define a simple message protocol as [<type>, <id>] and create some messages that use it.
//
-var message1 = ['message', 'one'];
-var message2 = ['message', 'two'];
+var message1 = ['message', 'one']
+var message2 = ['message', 'two']
//
// Create an `nssocket` TCP server and tell the server to listen on port `6785`.
@@ -15,37 +15,37 @@ var server = nssocket.createServer(function (socket) {
// Here `socket` will be an instance of `nssocket.NsSocket`.
// When there is a connection, send `message1` to the socket.
//
- socket.send(message1);
+ socket.send(message1)
//
// listen for `message2` from the connecting socket.
//
- socket.data(message2, function (data) {
+ socket.ondata(message2, function (data) {
//
// If this callback is called, we know that the socket
// speaks our language, we will likely be provided with
// a payload. In this case `{ "foo": "bar" }`.
//
- console.dir(data);
+ console.log(data)
})
-}).listen(6785);
+}).listen(6785)
//
// Create a new `nssocket` instance and then connect to the server in 1000 miliseconds.
//
setTimeout(function() {
- var outbound = new nssocket.NsSocket();
+ var outbound = nssocket()
//
- //
//
- outbound.data(message1, function () {
- outbound.send(message2, { "foo": "bar" });
- });
+ //
+ outbound.ondata(message1, function () {
+ outbound.send(message2, { "foo": "bar" })
+ })
+
+ outbound.connect(6785)
- outbound.connect(6785);
-
-}, 1000);
+}, 1000)
View
27 examples/verbose-protocol.js
@@ -1,4 +1,4 @@
- var nssocket = require('../lib/nssocket');
+ var nssocket = require('../')
//
// Create an `nssocket` TCP server
@@ -8,30 +8,27 @@
// Here `socket` will be an instance of `nssocket.NsSocket`.
//
- socket.send(['drink', 'rum']);
- socket.send(['drink', 'vodka']);
+ socket.send(['drink', 'rum'])
+ socket.send(['drink', 'vodka'])
// socket.data(['iam', 'here'], function (data) {
// //
- // // Good! The socket speaks our language
+ // // Good! The socket speaks our language
// // (i.e. simple 'you::there', 'iam::here' protocol)
// //
// // { iam: true, indeedHere: true }
// //
- // console.dir(data);
- // });
- });
+ // console.dir(data)
+ // })
+ })
//
// Tell the server to listen on port `6785` and then connect to it
// using another NsSocket instance.
//
- server.listen(6785);
+ server.listen(6785)
- var outbound = new nssocket.NsSocket();
- outbound.data(['drink', '*'], function () {
- console.log('I can mix a', this.event[2], 'drink');
- //outbound.send(['iam', 'here'], { iam: true, indeedHere: true });
- });
-
- outbound.connect(6785);
+ nssocket().ondata(['drink', '*'], function () {
+ console.log('I can mix a', this.event[2], 'drink')
+ //outbound.send(['iam', 'here'], { iam: true, indeedHere: true })
+ }).connect(6785)
View
2  index.js
@@ -1,3 +1,3 @@
-module.exports = process.env.NSSSOCKET_COV
+module.exports = process.env.NSSOCKET_COV
? require('./lib-cov/nssocket')
: require('./lib/nssocket')
View
529 lib-cov/nssocket.js
@@ -0,0 +1,529 @@
+/* automatically generated by JSCoverage - do not edit */
+if (typeof _$jscoverage === 'undefined') _$jscoverage = {};
+if (! _$jscoverage['nssocket.js']) {
+ _$jscoverage['nssocket.js'] = [];
+ _$jscoverage['nssocket.js'][1] = 0;
+ _$jscoverage['nssocket.js'][8] = 0;
+ _$jscoverage['nssocket.js'][9] = 0;
+ _$jscoverage['nssocket.js'][10] = 0;
+ _$jscoverage['nssocket.js'][11] = 0;
+ _$jscoverage['nssocket.js'][12] = 0;
+ _$jscoverage['nssocket.js'][15] = 0;
+ _$jscoverage['nssocket.js'][16] = 0;
+ _$jscoverage['nssocket.js'][18] = 0;
+ _$jscoverage['nssocket.js'][19] = 0;
+ _$jscoverage['nssocket.js'][20] = 0;
+ _$jscoverage['nssocket.js'][23] = 0;
+ _$jscoverage['nssocket.js'][29] = 0;
+ _$jscoverage['nssocket.js'][30] = 0;
+ _$jscoverage['nssocket.js'][31] = 0;
+ _$jscoverage['nssocket.js'][33] = 0;
+ _$jscoverage['nssocket.js'][34] = 0;
+ _$jscoverage['nssocket.js'][35] = 0;
+ _$jscoverage['nssocket.js'][38] = 0;
+ _$jscoverage['nssocket.js'][39] = 0;
+ _$jscoverage['nssocket.js'][42] = 0;
+ _$jscoverage['nssocket.js'][43] = 0;
+ _$jscoverage['nssocket.js'][44] = 0;
+ _$jscoverage['nssocket.js'][51] = 0;
+ _$jscoverage['nssocket.js'][57] = 0;
+ _$jscoverage['nssocket.js'][58] = 0;
+ _$jscoverage['nssocket.js'][59] = 0;
+ _$jscoverage['nssocket.js'][60] = 0;
+ _$jscoverage['nssocket.js'][61] = 0;
+ _$jscoverage['nssocket.js'][65] = 0;
+ _$jscoverage['nssocket.js'][67] = 0;
+ _$jscoverage['nssocket.js'][68] = 0;
+ _$jscoverage['nssocket.js'][69] = 0;
+ _$jscoverage['nssocket.js'][71] = 0;
+ _$jscoverage['nssocket.js'][72] = 0;
+ _$jscoverage['nssocket.js'][73] = 0;
+ _$jscoverage['nssocket.js'][74] = 0;
+ _$jscoverage['nssocket.js'][76] = 0;
+ _$jscoverage['nssocket.js'][78] = 0;
+ _$jscoverage['nssocket.js'][80] = 0;
+ _$jscoverage['nssocket.js'][83] = 0;
+ _$jscoverage['nssocket.js'][84] = 0;
+ _$jscoverage['nssocket.js'][87] = 0;
+ _$jscoverage['nssocket.js'][89] = 0;
+ _$jscoverage['nssocket.js'][90] = 0;
+ _$jscoverage['nssocket.js'][93] = 0;
+ _$jscoverage['nssocket.js'][94] = 0;
+ _$jscoverage['nssocket.js'][95] = 0;
+ _$jscoverage['nssocket.js'][96] = 0;
+ _$jscoverage['nssocket.js'][99] = 0;
+ _$jscoverage['nssocket.js'][102] = 0;
+ _$jscoverage['nssocket.js'][103] = 0;
+ _$jscoverage['nssocket.js'][105] = 0;
+ _$jscoverage['nssocket.js'][106] = 0;
+ _$jscoverage['nssocket.js'][109] = 0;
+ _$jscoverage['nssocket.js'][111] = 0;
+ _$jscoverage['nssocket.js'][112] = 0;
+ _$jscoverage['nssocket.js'][114] = 0;
+ _$jscoverage['nssocket.js'][115] = 0;
+ _$jscoverage['nssocket.js'][118] = 0;
+ _$jscoverage['nssocket.js'][119] = 0;
+ _$jscoverage['nssocket.js'][121] = 0;
+ _$jscoverage['nssocket.js'][124] = 0;
+ _$jscoverage['nssocket.js'][125] = 0;
+ _$jscoverage['nssocket.js'][126] = 0;
+ _$jscoverage['nssocket.js'][128] = 0;
+ _$jscoverage['nssocket.js'][131] = 0;
+ _$jscoverage['nssocket.js'][132] = 0;
+ _$jscoverage['nssocket.js'][135] = 0;
+ _$jscoverage['nssocket.js'][136] = 0;
+ _$jscoverage['nssocket.js'][137] = 0;
+ _$jscoverage['nssocket.js'][139] = 0;
+ _$jscoverage['nssocket.js'][142] = 0;
+ _$jscoverage['nssocket.js'][143] = 0;
+ _$jscoverage['nssocket.js'][144] = 0;
+ _$jscoverage['nssocket.js'][147] = 0;
+ _$jscoverage['nssocket.js'][148] = 0;
+ _$jscoverage['nssocket.js'][150] = 0;
+ _$jscoverage['nssocket.js'][151] = 0;
+ _$jscoverage['nssocket.js'][152] = 0;
+ _$jscoverage['nssocket.js'][155] = 0;
+ _$jscoverage['nssocket.js'][158] = 0;
+ _$jscoverage['nssocket.js'][159] = 0;
+ _$jscoverage['nssocket.js'][160] = 0;
+ _$jscoverage['nssocket.js'][162] = 0;
+ _$jscoverage['nssocket.js'][163] = 0;
+ _$jscoverage['nssocket.js'][165] = 0;
+ _$jscoverage['nssocket.js'][166] = 0;
+ _$jscoverage['nssocket.js'][169] = 0;
+ _$jscoverage['nssocket.js'][172] = 0;
+ _$jscoverage['nssocket.js'][173] = 0;
+ _$jscoverage['nssocket.js'][175] = 0;
+ _$jscoverage['nssocket.js'][176] = 0;
+ _$jscoverage['nssocket.js'][177] = 0;
+ _$jscoverage['nssocket.js'][180] = 0;
+ _$jscoverage['nssocket.js'][181] = 0;
+ _$jscoverage['nssocket.js'][183] = 0;
+ _$jscoverage['nssocket.js'][184] = 0;
+ _$jscoverage['nssocket.js'][185] = 0;
+ _$jscoverage['nssocket.js'][188] = 0;
+ _$jscoverage['nssocket.js'][191] = 0;
+ _$jscoverage['nssocket.js'][192] = 0;
+ _$jscoverage['nssocket.js'][194] = 0;
+ _$jscoverage['nssocket.js'][195] = 0;
+ _$jscoverage['nssocket.js'][197] = 0;
+ _$jscoverage['nssocket.js'][200] = 0;
+ _$jscoverage['nssocket.js'][201] = 0;
+ _$jscoverage['nssocket.js'][204] = 0;
+ _$jscoverage['nssocket.js'][206] = 0;
+ _$jscoverage['nssocket.js'][207] = 0;
+ _$jscoverage['nssocket.js'][208] = 0;
+ _$jscoverage['nssocket.js'][209] = 0;
+ _$jscoverage['nssocket.js'][211] = 0;
+ _$jscoverage['nssocket.js'][212] = 0;
+ _$jscoverage['nssocket.js'][214] = 0;
+ _$jscoverage['nssocket.js'][217] = 0;
+ _$jscoverage['nssocket.js'][218] = 0;
+ _$jscoverage['nssocket.js'][219] = 0;
+ _$jscoverage['nssocket.js'][222] = 0;
+ _$jscoverage['nssocket.js'][223] = 0;
+ _$jscoverage['nssocket.js'][228] = 0;
+ _$jscoverage['nssocket.js'][229] = 0;
+ _$jscoverage['nssocket.js'][230] = 0;
+ _$jscoverage['nssocket.js'][231] = 0;
+ _$jscoverage['nssocket.js'][232] = 0;
+ _$jscoverage['nssocket.js'][233] = 0;
+ _$jscoverage['nssocket.js'][234] = 0;
+ _$jscoverage['nssocket.js'][236] = 0;
+ _$jscoverage['nssocket.js'][240] = 0;
+ _$jscoverage['nssocket.js'][241] = 0;
+ _$jscoverage['nssocket.js'][242] = 0;
+ _$jscoverage['nssocket.js'][243] = 0;
+ _$jscoverage['nssocket.js'][245] = 0;
+ _$jscoverage['nssocket.js'][246] = 0;
+ _$jscoverage['nssocket.js'][247] = 0;
+ _$jscoverage['nssocket.js'][248] = 0;
+ _$jscoverage['nssocket.js'][250] = 0;
+ _$jscoverage['nssocket.js'][251] = 0;
+ _$jscoverage['nssocket.js'][253] = 0;
+ _$jscoverage['nssocket.js'][255] = 0;
+ _$jscoverage['nssocket.js'][256] = 0;
+ _$jscoverage['nssocket.js'][259] = 0;
+ _$jscoverage['nssocket.js'][262] = 0;
+ _$jscoverage['nssocket.js'][265] = 0;
+ _$jscoverage['nssocket.js'][266] = 0;
+ _$jscoverage['nssocket.js'][267] = 0;
+ _$jscoverage['nssocket.js'][269] = 0;
+ _$jscoverage['nssocket.js'][270] = 0;
+ _$jscoverage['nssocket.js'][272] = 0;
+ _$jscoverage['nssocket.js'][275] = 0;
+ _$jscoverage['nssocket.js'][278] = 0;
+ _$jscoverage['nssocket.js'][279] = 0;
+ _$jscoverage['nssocket.js'][282] = 0;
+ _$jscoverage['nssocket.js'][283] = 0;
+ _$jscoverage['nssocket.js'][284] = 0;
+}
+_$jscoverage['nssocket.js'][1]++;
+var net = require("net"), tls = require("tls"), util = require("util"), assert = require("chai").assert, EventEmitter2 = require("eventemitter2").EventEmitter2, bufferjoiner = require("bufferjoiner");
+_$jscoverage['nssocket.js'][8]++;
+module.exports = NsSocket;
+_$jscoverage['nssocket.js'][9]++;
+module.exports.createServer = (function createServer(options, connectionListener) {
+ _$jscoverage['nssocket.js'][10]++;
+ if (! connectionListener && typeof options === "function") {
+ _$jscoverage['nssocket.js'][11]++;
+ connectionListener = options;
+ _$jscoverage['nssocket.js'][12]++;
+ options = {};
+ }
+ _$jscoverage['nssocket.js'][15]++;
+ options.type = options.type || "tcp4";
+ _$jscoverage['nssocket.js'][16]++;
+ options.delimiter = options.delimiter || "::";
+ _$jscoverage['nssocket.js'][18]++;
+ function onConnection(socket) {
+ _$jscoverage['nssocket.js'][19]++;
+ options.reconnect = false;
+ _$jscoverage['nssocket.js'][20]++;
+ connectionListener(new NsSocket(options, socket));
+}
+ _$jscoverage['nssocket.js'][23]++;
+ return options.type === "tls"? tls.createServer(options, onConnection): net.createServer(options, onConnection);
+});
+_$jscoverage['nssocket.js'][29]++;
+module.exports.data = module.exports.ondata;
+_$jscoverage['nssocket.js'][30]++;
+module.exports.unData = module.exports.offdata;
+_$jscoverage['nssocket.js'][31]++;
+module.exports.dataOnce = module.exports.oncedata;
+_$jscoverage['nssocket.js'][33]++;
+function NsSocket(options, socket) {
+ _$jscoverage['nssocket.js'][34]++;
+ if (! (this instanceof NsSocket)) {
+ _$jscoverage['nssocket.js'][35]++;
+ return new NsSocket(options, socket);
+ }
+ _$jscoverage['nssocket.js'][38]++;
+ if (! options) {
+ _$jscoverage['nssocket.js'][39]++;
+ options = socket || {};
+ }
+ _$jscoverage['nssocket.js'][42]++;
+ this.connected = false;
+ _$jscoverage['nssocket.js'][43]++;
+ this.type = options.type || "tcp4";
+ _$jscoverage['nssocket.js'][44]++;
+ this.retry = options.reconnect? {retries: 0, max: options.maxRetries || 10, interval: options.retryInterval || 5000, wait: options.retryInterval || 5000}: false;
+ _$jscoverage['nssocket.js'][51]++;
+ EventEmitter2.call(this, {delimiter: options.delimiter || "::", wildcard: true, maxListeners: options.maxListeners || 10});
+ _$jscoverage['nssocket.js'][57]++;
+ if (socket) {
+ _$jscoverage['nssocket.js'][58]++;
+ this.stream = socket;
+ _$jscoverage['nssocket.js'][59]++;
+ this.socket = this.stream instanceof net.Socket? this.stream: this.stream.socket;
+ _$jscoverage['nssocket.js'][60]++;
+ this.connected = this.socket.writable && this.socket.readable || false;
+ _$jscoverage['nssocket.js'][61]++;
+ configureEvents(this);
+ }
+}
+_$jscoverage['nssocket.js'][65]++;
+util.inherits(NsSocket, EventEmitter2);
+_$jscoverage['nssocket.js'][67]++;
+NsSocket.prototype.connect = (function connect(port) {
+ _$jscoverage['nssocket.js'][68]++;
+ if (! this.socket) {
+ _$jscoverage['nssocket.js'][69]++;
+ var module = this.type === "tls"? tls: net;
+ _$jscoverage['nssocket.js'][71]++;
+ this.stream = module.connect.apply(null, arguments);
+ _$jscoverage['nssocket.js'][72]++;
+ this.socket = this.stream instanceof net.Socket? this.stream: this.stream.socket;
+ _$jscoverage['nssocket.js'][73]++;
+ this.connected = this.socket.writable && this.socket.readable || false;
+ _$jscoverage['nssocket.js'][74]++;
+ this.connectionArgs = arguments;
+ _$jscoverage['nssocket.js'][76]++;
+ configureEvents(this);
+ }
+ else {
+ _$jscoverage['nssocket.js'][78]++;
+ this.socket.connect.apply(this.socket, arguments);
+ }
+ _$jscoverage['nssocket.js'][80]++;
+ return this;
+});
+_$jscoverage['nssocket.js'][83]++;
+NsSocket.prototype.write = (function write(buff) {
+ _$jscoverage['nssocket.js'][84]++;
+ this.stream.write(buff);
+});
+_$jscoverage['nssocket.js'][87]++;
+NsSocket.prototype.send = (function send(event, data, callback) {
+ _$jscoverage['nssocket.js'][89]++;
+ if (! this.socket || ! this.connected) {
+ _$jscoverage['nssocket.js'][90]++;
+ return this.emit("error", new Error("NsSocket: sending on a bad socket"));
+ }
+ _$jscoverage['nssocket.js'][93]++;
+ var dataType = typeof data;
+ _$jscoverage['nssocket.js'][94]++;
+ if (dataType === "undefined" || dataType === "function") {
+ _$jscoverage['nssocket.js'][95]++;
+ callback = data;
+ _$jscoverage['nssocket.js'][96]++;
+ data = null;
+ }
+ _$jscoverage['nssocket.js'][99]++;
+ this.stream.write(this.createMessage(event, data), callback);
+});
+_$jscoverage['nssocket.js'][102]++;
+NsSocket.prototype.createMessage = (function createMessage(event, data) {
+ _$jscoverage['nssocket.js'][103]++;
+ var header = new Buffer(9);
+ _$jscoverage['nssocket.js'][105]++;
+ if (typeof event === "string") {
+ _$jscoverage['nssocket.js'][106]++;
+ event = event.split(this.delimiter);
+ }
+ _$jscoverage['nssocket.js'][109]++;
+ event = Buffer(JSON.stringify(event));
+ _$jscoverage['nssocket.js'][111]++;
+ if (Buffer.isBuffer(data)) {
+ _$jscoverage['nssocket.js'][112]++;
+ header.writeInt8(1, 8);
+ }
+ else {
+ _$jscoverage['nssocket.js'][114]++;
+ data = Buffer(JSON.stringify(data));
+ _$jscoverage['nssocket.js'][115]++;
+ header.writeInt8(0, 8);
+ }
+ _$jscoverage['nssocket.js'][118]++;
+ header.writeUInt32BE(event.length, 0);
+ _$jscoverage['nssocket.js'][119]++;
+ header.writeUInt32BE(data.length, 4);
+ _$jscoverage['nssocket.js'][121]++;
+ return Buffer.concat([header, event, data], 9 + event.length + data.length);
+});
+_$jscoverage['nssocket.js'][124]++;
+NsSocket.prototype.ondata = (function (event, listener) {
+ _$jscoverage['nssocket.js'][125]++;
+ if (typeof event === "string") {
+ _$jscoverage['nssocket.js'][126]++;
+ event = event.split(this.delimiter);
+ }
+ _$jscoverage['nssocket.js'][128]++;
+ return this.on(["data"].concat(event), listener);
+});
+_$jscoverage['nssocket.js'][131]++;
+NsSocket.prototype.offdata = (function (event, listener) {
+ _$jscoverage['nssocket.js'][132]++;
+ return this.off(["data"].concat(event), listener);
+});
+_$jscoverage['nssocket.js'][135]++;
+NsSocket.prototype.oncedata = (function (event, listener) {
+ _$jscoverage['nssocket.js'][136]++;
+ if (typeof event === "string") {
+ _$jscoverage['nssocket.js'][137]++;
+ event = event.split(this.delimiter);
+ }
+ _$jscoverage['nssocket.js'][139]++;
+ return this.once(["data"].concat(event), listener);
+});
+_$jscoverage['nssocket.js'][142]++;
+NsSocket.prototype.setIdle = (function setIdle(timeout) {
+ _$jscoverage['nssocket.js'][143]++;
+ this.socket.setTimeout(timeout);
+ _$jscoverage['nssocket.js'][144]++;
+ this.timeout = timeout;
+});
+_$jscoverage['nssocket.js'][147]++;
+NsSocket.prototype.destroy = (function destroy() {
+ _$jscoverage['nssocket.js'][148]++;
+ this.removeAllListeners();
+ _$jscoverage['nssocket.js'][150]++;
+ try {
+ _$jscoverage['nssocket.js'][151]++;
+ this.socket.end();
+ _$jscoverage['nssocket.js'][152]++;
+ this.socket.destroy();
+ }
+ catch (err) {
+ }
+ _$jscoverage['nssocket.js'][155]++;
+ this.emit("destroy");
+});
+_$jscoverage['nssocket.js'][158]++;
+NsSocket.prototype.end = (function end() {
+ _$jscoverage['nssocket.js'][159]++;
+ var hadErr;
+ _$jscoverage['nssocket.js'][160]++;
+ this.connected = false;
+ _$jscoverage['nssocket.js'][162]++;
+ try {
+ _$jscoverage['nssocket.js'][163]++;
+ this.socket.end();
+ }
+ catch (err) {
+ _$jscoverage['nssocket.js'][165]++;
+ hadErr = true;
+ _$jscoverage['nssocket.js'][166]++;
+ this.emit("error", err);
+ }
+ _$jscoverage['nssocket.js'][169]++;
+ this.emit("close", hadErr);
+});
+_$jscoverage['nssocket.js'][172]++;
+NsSocket.prototype.reconnect = (function reconnect() {
+ _$jscoverage['nssocket.js'][173]++;
+ var self = this;
+ _$jscoverage['nssocket.js'][175]++;
+ function doReconnect() {
+ _$jscoverage['nssocket.js'][176]++;
+ self.retry.waiting = true;
+ _$jscoverage['nssocket.js'][177]++;
+ self.socket && self.socket.destroy();
+ _$jscoverage['nssocket.js'][180]++;
+ assert.isFalse(self.connected, "before actually reconnect connected must be false");
+ _$jscoverage['nssocket.js'][181]++;
+ assert.isUndefined(self.socket, "before actually reconnect socket must be destroied");
+ _$jscoverage['nssocket.js'][183]++;
+ self.once("start", (function () {
+ _$jscoverage['nssocket.js'][184]++;
+ self.retry.waiting = false;
+ _$jscoverage['nssocket.js'][185]++;
+ self.retry.retries = 0;
+}));
+ _$jscoverage['nssocket.js'][188]++;
+ self.connect.apply(self, self.connectionArgs);
+}
+ _$jscoverage['nssocket.js'][191]++;
+ function tryReconnect() {
+ _$jscoverage['nssocket.js'][192]++;
+ self.retry.retries++;
+ _$jscoverage['nssocket.js'][194]++;
+ if (self.retry.retries >= self.retry.max) {
+ _$jscoverage['nssocket.js'][195]++;
+ return self.emit("error", new Error("Did not reconnect after maximum retries: " + self.retry.max));
+ }
+ _$jscoverage['nssocket.js'][197]++;
+ doReconnect();
+}
+ _$jscoverage['nssocket.js'][200]++;
+ this.retry.wait = this.retry.interval * Math.pow(10, this.retry.retries);
+ _$jscoverage['nssocket.js'][201]++;
+ setTimeout(tryReconnect, this.retry.wait);
+});
+_$jscoverage['nssocket.js'][204]++;
+function configureEvents(self) {
+ _$jscoverage['nssocket.js'][206]++;
+ var eventLength = -1;
+ _$jscoverage['nssocket.js'][207]++;
+ var messageLength = -1;
+ _$jscoverage['nssocket.js'][208]++;
+ var messagetype = 0;
+ _$jscoverage['nssocket.js'][209]++;
+ var bufferJoiner = bufferjoiner();
+ _$jscoverage['nssocket.js'][211]++;
+ if (self.type === "tls") {
+ _$jscoverage['nssocket.js'][212]++;
+ self.stream.on("secureConnect", onStart);
+ }
+ else {
+ _$jscoverage['nssocket.js'][214]++;
+ self.socket.on("connect", onStart);
+ }
+ _$jscoverage['nssocket.js'][217]++;
+ function onStart() {
+ _$jscoverage['nssocket.js'][218]++;
+ self.connected = true;
+ _$jscoverage['nssocket.js'][219]++;
+ self.emit("start");
+}
+ _$jscoverage['nssocket.js'][222]++;
+ self.stream.on("data", (function onData(chunk) {
+ _$jscoverage['nssocket.js'][223]++;
+ ~ messageLength? fetchBody(chunk): fetchHeader(chunk);
+}));
+ _$jscoverage['nssocket.js'][228]++;
+ function fetchHeader(chunk) {
+ _$jscoverage['nssocket.js'][229]++;
+ if (bufferJoiner.length + chunk.length >= 9) {
+ _$jscoverage['nssocket.js'][230]++;
+ var header = bufferJoiner.add(chunk).join();
+ _$jscoverage['nssocket.js'][231]++;
+ eventLength = header.readUInt32BE(0);
+ _$jscoverage['nssocket.js'][232]++;
+ messageLength = header.readUInt32BE(4);
+ _$jscoverage['nssocket.js'][233]++;
+ messagetype = header.readInt8(8);
+ _$jscoverage['nssocket.js'][234]++;
+ fetchBody(chunk.slice(9));
+ }
+ else {
+ _$jscoverage['nssocket.js'][236]++;
+ bufferJoiner.add(chunk);
+ }
+}
+ _$jscoverage['nssocket.js'][240]++;
+ function fetchBody(chunk) {
+ _$jscoverage['nssocket.js'][241]++;
+ var raw, event, data;
+ _$jscoverage['nssocket.js'][242]++;
+ var chunkLength = chunk.length;
+ _$jscoverage['nssocket.js'][243]++;
+ var bytesLeft = (eventLength + messageLength) - bufferJoiner.length;
+ _$jscoverage['nssocket.js'][245]++;
+ if (chunkLength >= bytesLeft) {
+ _$jscoverage['nssocket.js'][246]++;
+ raw = bufferJoiner.add(chunk.slice(0, bytesLeft)).join();
+ _$jscoverage['nssocket.js'][247]++;
+ event = JSON.parse(raw.slice(0, eventLength));
+ _$jscoverage['nssocket.js'][248]++;
+ data = messagetype? raw.slice(eventLength): JSON.parse(raw.slice(eventLength).toString());
+ _$jscoverage['nssocket.js'][250]++;
+ eventLength = -1;
+ _$jscoverage['nssocket.js'][251]++;
+ messageLength = -1;
+ _$jscoverage['nssocket.js'][253]++;
+ self.emit(["data"].concat(event), data);
+ _$jscoverage['nssocket.js'][255]++;
+ if (chunkLength - bytesLeft) {
+ _$jscoverage['nssocket.js'][256]++;
+ fetchHeader(chunk.slice(bytesLeft));
+ }
+ _$jscoverage['nssocket.js'][259]++;
+ return;
+ }
+ _$jscoverage['nssocket.js'][262]++;
+ bufferJoiner.add(chunk);
+}
+ _$jscoverage['nssocket.js'][265]++;
+ self.socket.on("close", (function onClose(hadError) {
+ _$jscoverage['nssocket.js'][266]++;
+ self.socket = undefined;
+ _$jscoverage['nssocket.js'][267]++;
+ self.connected = false;
+ _$jscoverage['nssocket.js'][269]++;
+ if (hadError) {
+ _$jscoverage['nssocket.js'][270]++;
+ self.emit("close", hadError, arguments[1]);
+ }
+ else {
+ _$jscoverage['nssocket.js'][272]++;
+ self.emit("close");
+ }
+ _$jscoverage['nssocket.js'][275]++;
+ self.retry && self.reconnect();
+}));
+ _$jscoverage['nssocket.js'][278]++;
+ self.socket.on("error", (function (err) {
+ _$jscoverage['nssocket.js'][279]++;
+ ! self.retry && self.emit("error", err || new Error("An Unknown Error occured"));
+}));
+ _$jscoverage['nssocket.js'][282]++;
+ self.socket.on("timeout", (function onIdle() {
+ _$jscoverage['nssocket.js'][283]++;
+ self.emit("idle");
+ _$jscoverage['nssocket.js'][284]++;
+ self.timeout && self.socket.setTimeout(this.timeout);
+}));
+}
+_$jscoverage['nssocket.js'].source = ["var net = require('net'),"," tls = require('tls'),"," util = require('util'),"," assert = require('chai').assert,"," EventEmitter2 = require('eventemitter2').EventEmitter2,"," bufferjoiner = require('bufferjoiner')","","module.exports = NsSocket","module.exports.createServer = function createServer(options, connectionListener) {"," if (!connectionListener &amp;&amp; typeof options === 'function') {"," connectionListener = options"," options = {}"," }",""," options.type = options.type || 'tcp4'"," options.delimiter = options.delimiter || '::'",""," function onConnection (socket) {"," options.reconnect = false"," connectionListener(new NsSocket(options, socket))"," }",""," return options.type === 'tls'"," ? tls.createServer(options, onConnection)"," : net.createServer(options, onConnection)","}","","// retro compatibility fixes","module.exports.data = module.exports.ondata","module.exports.unData = module.exports.offdata","module.exports.dataOnce = module.exports.oncedata","","function NsSocket(options, socket) {"," if (!(this instanceof NsSocket)) {"," return new NsSocket(options, socket)"," }",""," if (!options) {"," options = socket || {}"," }",""," this.connected = false"," this.type = options.type || 'tcp4'"," this.retry = options.reconnect ? {"," retries: 0,"," max: options.maxRetries || 10,"," interval: options.retryInterval || 5000,"," wait: options.retryInterval || 5000"," } : false",""," EventEmitter2.call(this, {"," delimiter: options.delimiter || '::',"," wildcard: true,"," maxListeners: options.maxListeners || 10"," })",""," if (socket) {"," this.stream = socket"," this.socket = this.stream instanceof net.Socket ? this.stream : this.stream.socket"," this.connected = this.socket.writable &amp;&amp; this.socket.readable || false"," configureEvents(this)"," }","}","","util.inherits(NsSocket, EventEmitter2)","","NsSocket.prototype.connect = function connect(port) {"," if (!this.socket) {"," var module = this.type === 'tls' ? tls : net",""," this.stream = module.connect.apply(null, arguments)"," this.socket = this.stream instanceof net.Socket ? this.stream : this.stream.socket"," this.connected = this.socket.writable &amp;&amp; this.socket.readable || false"," this.connectionArgs = arguments",""," configureEvents(this)"," } else {"," this.socket.connect.apply(this.socket, arguments)"," }"," return this","}","","NsSocket.prototype.write = function write(buff) {"," this.stream.write(buff)","}","","NsSocket.prototype.send = function send(event, data, callback) {"," // if we aren't connected/socketed, then error"," if (!this.socket || !this.connected) {"," return this.emit('error', new Error('NsSocket: sending on a bad socket'))"," }",""," var dataType = typeof data"," if (dataType === 'undefined' || dataType === 'function') {"," callback = data"," data = null"," }",""," this.stream.write(this.createMessage(event, data), callback)","}","","NsSocket.prototype.createMessage = function createMessage(event, data) {"," var header = new Buffer(9)",""," if (typeof event === 'string') {"," event = event.split(this.delimiter)"," }",""," event = Buffer(JSON.stringify(event))",""," if (Buffer.isBuffer(data)) {"," header.writeInt8(1, 8)"," } else {"," data = Buffer(JSON.stringify(data))"," header.writeInt8(0, 8)"," }",""," header.writeUInt32BE(event.length, 0)"," header.writeUInt32BE(data.length, 4)",""," return Buffer.concat([header, event, data], 9 + event.length + data.length)","}","","NsSocket.prototype.ondata = function (event, listener) {"," if (typeof event === 'string') {"," event = event.split(this.delimiter)"," }"," return this.on(['data'].concat(event), listener)","}","","NsSocket.prototype.offdata = function (event, listener) {"," return this.off(['data'].concat(event), listener)","}","","NsSocket.prototype.oncedata = function (event, listener) {"," if (typeof event === 'string') {"," event = event.split(this.delimiter)"," }"," return this.once(['data'].concat(event), listener)","}","","NsSocket.prototype.setIdle = function setIdle(timeout) {"," this.socket.setTimeout(timeout)"," this.timeout = timeout","}","","NsSocket.prototype.destroy = function destroy() {"," this.removeAllListeners()",""," try {"," this.socket.end()"," this.socket.destroy()"," } catch (err) {}",""," this.emit('destroy')","}","","NsSocket.prototype.end = function end() {"," var hadErr"," this.connected = false",""," try {"," this.socket.end()"," } catch (err) {"," hadErr = true"," this.emit('error', err)"," }",""," this.emit('close', hadErr)","}","","NsSocket.prototype.reconnect = function reconnect() {"," var self = this",""," function doReconnect() {"," self.retry.waiting = true"," self.socket &amp;&amp; self.socket.destroy()",""," // here for debugging reasons"," assert.isFalse(self.connected, 'before actually reconnect connected must be false')"," assert.isUndefined(self.socket, 'before actually reconnect socket must be destroied')",""," self.once('start', function () {"," self.retry.waiting = false"," self.retry.retries = 0"," })",""," self.connect.apply(self, self.connectionArgs)"," }",""," function tryReconnect() {"," self.retry.retries ++",""," if (self.retry.retries &gt;= self.retry.max) {"," return self.emit('error', new Error('Did not reconnect after maximum retries: ' + self.retry.max))"," }"," doReconnect()"," }",""," this.retry.wait = this.retry.interval * Math.pow(10, this.retry.retries)"," setTimeout(tryReconnect, this.retry.wait)","}","","function configureEvents(self) {"," // parsing holders"," var eventLength = -1"," var messageLength = -1"," var messagetype = 0"," var bufferJoiner = bufferjoiner()",""," if (self.type === 'tls') {"," self.stream.on('secureConnect', onStart)"," } else {"," self.socket.on('connect', onStart)"," }",""," function onStart() {"," self.connected = true"," self.emit('start')"," }",""," self.stream.on('data', function onData(chunk) {"," ~messageLength"," ? fetchBody(chunk)"," : fetchHeader(chunk)"," })",""," function fetchHeader(chunk) {"," if (bufferJoiner.length + chunk.length &gt;= 9) {"," var header = bufferJoiner.add(chunk).join()"," eventLength = header.readUInt32BE(0)"," messageLength = header.readUInt32BE(4)"," messagetype = header.readInt8(8)"," fetchBody(chunk.slice(9))"," } else {"," bufferJoiner.add(chunk)"," }"," }",""," function fetchBody(chunk) {"," var raw, event, data"," var chunkLength = chunk.length"," var bytesLeft = (eventLength + messageLength) - bufferJoiner.length",""," if (chunkLength &gt;= bytesLeft) {"," raw = bufferJoiner.add(chunk.slice(0, bytesLeft)).join()"," event = JSON.parse(raw.slice(0, eventLength))"," data = messagetype ? raw.slice(eventLength) : JSON.parse(raw.slice(eventLength).toString())",""," eventLength = -1"," messageLength = -1",""," self.emit(['data'].concat(event), data)",""," if (chunkLength - bytesLeft) {"," fetchHeader(chunk.slice(bytesLeft))"," }",""," return"," }",""," bufferJoiner.add(chunk)"," }",""," self.socket.on('close', function onClose(hadError) {"," self.socket = undefined"," self.connected = false",""," if (hadError) {"," self.emit('close', hadError, arguments[1])"," } else {"," self.emit('close')"," }",""," self.retry &amp;&amp; self.reconnect()"," })",""," self.socket.on('error', function (err) {"," !self.retry &amp;&amp; self.emit('error', err || new Error('An Unknown Error occured'))"," })",""," self.socket.on('timeout', function onIdle() {"," self.emit('idle')"," self.timeout &amp;&amp; self.socket.setTimeout(this.timeout)"," })","}"];
View
105 lib/common.js
@@ -1,105 +0,0 @@
-/*
- * common.js
- *
- * (C) 2011, Nodejitsu Inc.
- */
-
-var fs = require('fs'),
- tls = require('tls'),
- net = require('net'),
- crypto = require('crypto');
-
-exports.createSocket = function (options) {
- options = options || {};
- options.type = options.type || 'tcp4';
-
- return options.type === 'tls'
- ? exports.createTlsSocket(options)
- : new net.Socket(options);
-};
-
-//
-// ### function createTlsSocket (options)
-// #### @options {Object} Tls options like in tls.js
-// #### Should behave like tls.connect, except it just creates the socket like net.Socket
-// #### Also has a function called 'connect' that will allow` it to connect to a remote host
-// this is a rip of tls.js's connect
-//
-exports.createTlsSocket = function(options) {
- var self = this;
-
- //
- // Setup the TLS connection over the existing TCP connection:
- //
- // 1. Create a new instance of `net.Socket`.
- // 2. Create a new set of credentials with `options`.
- // 3. Create the TLS pair
- // 4. Pipe the TLS pair to the TCP socket
- //
- var socket = new net.Stream({ type: 'tcp4' });
-
- function setupTlsPipe () {
- var sslcontext = crypto.createCredentials(options),
- pair = tls.createSecurePair(sslcontext, false),
- cleartext = pipe(pair, socket);
-
- pair.on('secure', function() {
- var verifyError = pair.ssl.verifyError();
-
- if (verifyError) {
- cleartext.authorized = false;
- cleartext.authorizationError = verifyError;
- }
- else {
- cleartext.authorized = true;
- }
- });
-
- //
- // Setup the cleartext stream to have a `.connect()` method
- // which passes through to the underlying TCP socket.
- //
- socket.cleartext = cleartext;
- cleartext._controlReleased = true;
- }
-
- socket.on('connect', setupTlsPipe);
-
- return socket;
-};
-
-//
-// helper function for createTlsSocket
-//
-function pipe(pair, socket) {
- pair.encrypted.pipe(socket);
- socket.pipe(pair.encrypted);
-
- pair.fd = socket.fd;
- var cleartext = pair.cleartext;
- cleartext.socket = socket;
- cleartext.encrypted = pair.encrypted;
- cleartext.authorized = false;
-
- function onerror(e) {
- if (cleartext._controlReleased) {
- cleartext.emit('error', e);
- }
- }
-
- function onclose() {
- socket.removeListener('error', onerror);
- socket.removeListener('close', onclose);
- socket.removeListener('timeout', ontimeout);
- }
-
- function ontimeout() {
- cleartext.emit('timeout');
- }
-
- socket.on('error', onerror);
- socket.on('close', onclose);
- socket.on('timeout', ontimeout);
-
- return cleartext;
-}
View
687 lib/nssocket.js
@@ -1,553 +1,286 @@
-/*
- * nssocket.js - Wraps a TLS/TCP socket to emit namespace events also auto-buffers.
- *
- * (C) 2011, Nodejitsu Inc.
- *
- */
-
var net = require('net'),
tls = require('tls'),
util = require('util'),
- events2 = require('eventemitter2'),
- BufferJoiner = require('bufferjoiner'),
- common = require('./common');
-
-//
-// ### function NsSocket (socket, options)
-// #### @socket {Object} TCP or TLS 'socket' either from a 'connect' 'new' or from a server
-// #### @options {Object} Options for this NsSocket
-// NameSpace Socket, NsSocket, is a thin wrapper above TLS/TCP.
-// It provides automatic buffering and name space based data emits.
-//
-var NsSocket = exports.NsSocket = function (socket, options) {
+ assert = require('chai').assert,
+ EventEmitter2 = require('eventemitter2').EventEmitter2,
+ bufferjoiner = require('bufferjoiner')
+
+module.exports = NsSocket
+module.exports.createServer = function createServer(options, connectionListener) {
+ if (!connectionListener && typeof options === 'function') {
+ connectionListener = options
+ options = {}
+ }
+
+ options.type = options.type || 'tcp4'
+ options.delimiter = options.delimiter || '::'
+
+ function onConnection (socket) {
+ options.reconnect = false
+ connectionListener(new NsSocket(options, socket))
+ }
+
+ return options.type === 'tls'
+ ? tls.createServer(options, onConnection)
+ : net.createServer(options, onConnection)
+}
+
+// retro compatibility fixes
+module.exports.data = module.exports.ondata
+module.exports.unData = module.exports.offdata
+module.exports.dataOnce = module.exports.oncedata
+
+function NsSocket(options, socket) {
if (!(this instanceof NsSocket)) {
- return new NsSocket(socket, options);
+ return new NsSocket(options, socket)
}
- //
- // If there is no Socket instnace to wrap,
- // create one.
- //
if (!options) {
- options = socket;
- socket = common.createSocket(options);
+ options = socket || {}
}
- //
- // Options should be
- //
- // {
- // type : 'tcp' or 'tls',
- // delimiter : '::', delimiter that separates between segments
- // msgLength : 3 //number of segments in a complete message
- // }
- //
- options = options || {};
-
- var self = this,
- startName;
-
- //
- // Setup underlying socket state.
- //
- this.socket = socket;
- this.connected = options.connected || socket.writable && socket.readable || false;
-
- //
- // Setup reconnect options.
- //
- this._reconnect = options.reconnect || false;
- this.retry = {
- retries: 0,
- max: options.maxRetries || 10,
+ this.connected = false
+ this.type = options.type || 'tcp4'
+ this.retry = options.reconnect ? {
+ retries: 0,
+ max: options.maxRetries || 10,
interval: options.retryInterval || 5000,
- wait: options.retryInterval || 5000
- };
-
- //
- // Setup default instance variables.
- //
- this._options = options;
- this._type = options.type || 'tcp4',
- this._delimiter = options.delimiter || '::';
-
- events2.EventEmitter2.call(this, {
- delimiter: this._delimiter,
+ wait: options.retryInterval || 5000
+ } : false
+
+ EventEmitter2.call(this, {
+ delimiter: options.delimiter || '::',
wildcard: true,
maxListeners: options.maxListeners || 10
- });
-
- // Initializing parsing holders
- this._eventLength = -1;
- this._messageLength = -1;
- this._messagetype = 0;
- this._bufferJoiner = new BufferJoiner();
-
- this._setup();
-};
-
-//
-// Inherit from `events2.EventEmitter2`.
-//
-util.inherits(NsSocket, events2.EventEmitter2);
-
-//
-// ### function createServer (options, connectionListener)
-// #### @options {Object} **Optional**
-// Creates a new TCP/TLS server which wraps every incoming connection
-// in an instance of `NsSocket`.
-//
-exports.createServer = function createServer(options, connectionListener) {
- if (!connectionListener && typeof options === 'function') {
- connectionListener = options;
- options = {};
+ })
+
+ if (socket) {
+ this.stream = socket
+ this.socket = this.stream instanceof net.Socket ? this.stream : this.stream.socket
+ this.connected = this.socket.writable && this.socket.readable || false
+ configureEvents(this)
}
+}
- options.type = options.type || 'tcp4';
- options.delimiter = options.delimiter || '::';
+util.inherits(NsSocket, EventEmitter2)
- function onConnection (socket) {
+NsSocket.prototype.connect = function connect(port) {
+ if (!this.socket) {
+ var module = this.type === 'tls' ? tls : net
- //
- // Incoming socket connections cannot reconnect
- // by definition.
- //
- options.reconnect = false;
- connectionListener(new NsSocket(socket, options));
+ this.stream = module.connect.apply(null, arguments)
+ this.socket = this.stream instanceof net.Socket ? this.stream : this.stream.socket
+ this.connected = this.socket.writable && this.socket.readable || false
+ this.connectionArgs = arguments
+
+ configureEvents(this)
+ } else {
+ this.socket.connect.apply(this.socket, arguments)
}
+ return this
+}
+
+NsSocket.prototype.write = function write(buff) {
+ this.stream.write(buff)
+}
- return options.type === 'tls'
- ? tls.createServer(options, onConnection)
- : net.createServer(options, onConnection);
-};
-
-// exports.createConnection = exports.connect = function connect(options, port, host, callback) {
-// return new NsSocket(options).connect(port, port, host, callback);
-// };
-
-//
-// ### function send (data, callback)
-// #### @event {Array|string} The array (or string) that holds the event name
-// #### @data {Literal|Object|Buffer} The data to be sent with the event.
-// #### @callback {Function} the callback function when send is done sending
-// The send function follows write/send rules for TCP/TLS/UDP
-// in that the callback is called when sending is complete, not when delivered
-//
NsSocket.prototype.send = function send(event, data, callback) {
// if we aren't connected/socketed, then error
if (!this.socket || !this.connected) {
- return this.emit('error', new Error('NsSocket: sending on a bad socket'));
- }
-
- // rebinds
- if (typeof event === 'string') {
- event = event.split(this._delimiter);
+ return this.emit('error', new Error('NsSocket: sending on a bad socket'))
}
var dataType = typeof data
if (dataType === 'undefined' || dataType === 'function') {
- callback = data;
- data = null;
+ callback = data
+ data = null
}
- var message = this.createMessage(event, data);
+ this.stream.write(this.createMessage(event, data), callback)
+}
- // now actually write to the socket
- if (this.socket.cleartext) {
- this.socket.cleartext.write(message, callback);
- } else {
- this.socket.write(message, callback);
- }
-};
+NsSocket.prototype.createMessage = function createMessage(event, data) {
+ var header = new Buffer(9)
-//
-// ### function _createMessage (event, data)
-// #### @event {Array|string} The array (or string) that holds the event name
-// #### @data {Literal|Object|Buffer} The data to be sent with the event.
-// Returns a packaged binary message ready to be written to the socket.
-//
-NsSocket.prototype.createMessage = function _createMessage(event, data) {
- var header = new Buffer(9);
+ if (typeof event === 'string') {
+ event = event.split(this.delimiter)
+ }
- event = Buffer(JSON.stringify(event));
+ event = Buffer(JSON.stringify(event))
if (Buffer.isBuffer(data)) {
- header.writeInt8(1, 8);
+ header.writeInt8(1, 8)
} else {
- data = Buffer(JSON.stringify(data));
- header.writeInt8(0, 8);
+ data = Buffer(JSON.stringify(data))
+ header.writeInt8(0, 8)
}
- header.writeUInt32BE(event.length, 0);
- header.writeUInt32BE(data.length, 4);
+ header.writeUInt32BE(event.length, 0)
+ header.writeUInt32BE(data.length, 4)
- return Buffer.concat([header, event, data], 9 + event.length + data.length);
+ return Buffer.concat([header, event, data], 9 + event.length + data.length)
}
-//
-// ### function data (event, callback)
-// #### @event {Array|string} Namespaced `data` event to listen to.
-// #### @callback {function} Continuation to call when the event is raised.
-// Shorthand function for listening to `['data', '*']` events.
-//
-NsSocket.prototype.data = function (event, callback) {
+NsSocket.prototype.ondata = function (event, listener) {
if (typeof event === 'string') {
- event = event.split(this._delimiter);
+ event = event.split(this.delimiter)
}
+ return this.on(['data'].concat(event), listener)
+}
- this.on(['data'].concat(event), callback);
-};
-
-NsSocket.prototype.undata = function (event, listener) {
- this.off(['data'].concat(event), listener);
-};
+NsSocket.prototype.offdata = function (event, listener) {
+ return this.off(['data'].concat(event), listener)
+}
-//
-// ### function data (event, callback)
-// #### @event {Array|string} Namespaced `data` event to listen to once.
-// #### @callback {function} Continuation to call when the event is raised.
-// Shorthand function for listening to `['data', '*']` events once.
-//
-NsSocket.prototype.dataOnce = function (event, callback) {
+NsSocket.prototype.oncedata = function (event, listener) {
if (typeof event === 'string') {
- event = event.split(this._delimiter);
+ event = event.split(this.delimiter)
}
+ return this.once(['data'].concat(event), listener)
+}
+
+NsSocket.prototype.setIdle = function setIdle(timeout) {
+ this.socket.setTimeout(timeout)
+ this.timeout = timeout
+}
- this.once(['data'].concat(event), callback);
-};
-
-//
-// ### function setIdle (time, callback)
-// #### @time {Integer} how often to emit idle
-// Set the idle/timeout timer
-//
-NsSocket.prototype.setIdle = function setIdle(time) {
- this.socket.setTimeout(time);
- this._timeout = time;
-};
-
-//
-// ### function destroy (void)
-// #### forcibly destroys this nsSocket, unregister socket, remove all callbacks
-//
NsSocket.prototype.destroy = function destroy() {
- // this should be forcibly remove EVERY listener
- this.removeAllListeners();
+ this.removeAllListeners()
- if (this.socket) {
- try {
- this.socket.end(); // send FIN
- this.socket.destroy(); // make sure fd's are gone
- }
- catch (ex) {
- // do nothing on errors
- }
- }
+ try {
+ this.socket.end()
+ this.socket.destroy()
+ } catch (err) {}
- // clear buffer
- // this.data = '';
- this.emit('destroy');
-};
+ this.emit('destroy')
+}
-//
-// ### function end (void)
-// #### closes the underlying socket, recommend you call destroy after
-//
NsSocket.prototype.end = function end() {
- var hadErr;
- this.connected = false;
-
- if (this.socket) {
- try {
- this.socket.end();
- }
- catch (ex) {
- this.emit('error', ex);
- hadErr = true;
- return;
- }
-
- this.socket = null;
+ var hadErr
+ this.connected = false
+
+ try {
+ this.socket.end()
+ } catch (err) {
+ hadErr = true
+ this.emit('error', err)
}
- return this.emit('close', hadErr || undefined);
-};
-
-//
-// ### function connect (port[, host, callback])
-// A passthrough to the underlying socket's connect function
-//
-NsSocket.prototype.connect = function connect(/*port, host, callback*/) {
- var args = Array.prototype.slice.call(arguments),
- self = this,
- callback,
- host,
- port;
-
- args.forEach(function handle(arg) {
- var type = typeof arg;
- switch (type) {
- case 'number':
- port = arg;
- break;
- case 'string':
- host = arg;
- break;
- case 'function':
- callback = arg;
- break;
- default:
- self.emit('error', new Error('bad argument to connect'));
- break;
- }
- });
-
- host = host || '127.0.0.1';
- this.port = port || this.port;
- this.host = host || this.host;
- args = this.port ? [this.port, this.host] : [this.host];
-
- if (callback) {
- args.push(callback);
- }
+ this.emit('close', hadErr)
+}
- if (['tcp4', 'tls'].indexOf(this._type) === -1) {
- return this.emit('error', new Error('Unknown Socket Type'));
- }
+NsSocket.prototype.reconnect = function reconnect() {
+ var self = this
- var errHandlers = self.listeners('error');
+ function doReconnect() {
+ self.retry.waiting = true
+ self.socket && self.socket.destroy()
- if (errHandlers.length > 0) {
- //
- // copy the last error from nssocker onto the error event.
- //
- self.socket._events.error = errHandlers[errHandlers.length-1];
- }
+ // here for debugging reasons
+ assert.isFalse(self.connected, 'before actually reconnect connected must be false')
+ assert.isUndefined(self.socket, 'before actually reconnect socket must be destroied')
- this.connected = true;
- this.socket.connect.apply(this.socket, args);
- return this;
-};
-
-//
-// ### function reconnect ()
-// Attempts to reconnect the current socket on `close` or `error`.
-// This instance will attempt to reconnect until `this.retry.max` is reached,
-// with an interval increasing by powers of 10.
-//
-NsSocket.prototype.reconnect = function reconnect() {
- var self = this;
+ self.once('start', function () {
+ self.retry.waiting = false
+ self.retry.retries = 0
+ })
- //
- // Helper function containing the core reconnect logic
- //
- function doReconnect() {
- //
- // Cleanup and recreate the socket associated
- // with this instance.
- //
- self.retry.waiting = true;
- self.socket && self.socket.destroy();
- self.socket = common.createSocket(self._options);
-
- //
- // Cleanup reconnect logic once the socket connects
- //
- self.socket.once('connect', function () {
- self.retry.waiting = false;
- self.retry.retries = 0;
- });
-
- //
- // Attempt to reconnect the socket
- //
- self._setup();
- self.connect();
+ self.connect.apply(self, self.connectionArgs)
}
- //
- // Helper function which attempts to retry if
- // it is less than the maximum
- //
function tryReconnect() {
- self.retry.retries++;
+ self.retry.retries ++
+
if (self.retry.retries >= self.retry.max) {
- return self.emit('error', new Error('Did not reconnect after maximum retries: ' + self.retry.max));
+ return self.emit('error', new Error('Did not reconnect after maximum retries: ' + self.retry.max))
}
-
- doReconnect();
+ doReconnect()
}
- this.retry.wait = this.retry.interval * Math.pow(10, this.retry.retries);
- setTimeout(tryReconnect, this.retry.wait);
-};
-
-//
-// ### @private function _setup ()
-// Sets up the underlying socket associate with this instance.
-//
-NsSocket.prototype._setup = function () {
- var self = this,
- startName;
- //
- // Because of how the code node.js `tls` module works, we have
- // to separate some bindings. The main difference is on
- // connection, some socket activities.
- //
- if (this._type === 'tcp4') {
- startName = 'connect';
-
- this.socket.on('data', this._onData.bind(this));
- // this.socket.on('data', );
-
- // create a stub for the setKeepAlive functionality
- this.setKeepAlive = function () {
- self.socket.setKeepAlive.apply(self.socket, arguments);
- };
+ this.retry.wait = this.retry.interval * Math.pow(10, this.retry.retries)
+ setTimeout(tryReconnect, this.retry.wait)
+}
+
+function configureEvents(self) {
+ // parsing holders
+ var eventLength = -1
+ var messageLength = -1
+ var messagetype = 0
+ var bufferJoiner = bufferjoiner()
+
+ if (self.type === 'tls') {
+ self.stream.on('secureConnect', onStart)
+ } else {
+ self.socket.on('connect', onStart)
}
- else if (this._type === 'tls') {
- startName = 'secureConnection';
- this.socket.once('connect', function () {
- console.log('secureConnection')
- self.socket.cleartext.on('data', self._onData.bind(self));
- });
-
- // create a stub for the setKeepAlive functionality
- this.setKeepAlive = function () {
- self.socket.socket.setKeepAlive.apply(self.socket.socket, arguments);
- };
+
+ function onStart() {
+ self.connected = true
+ self.emit('start')
}
- else {
- // bad arguments, so throw an error
- this.emit('error', new Error('Bad Option Argument [type]'));
- return null;
+
+ self.stream.on('data', function onData(chunk) {
+ ~messageLength
+ ? fetchBody(chunk)
+ : fetchHeader(chunk)
+ })
+
+ function fetchHeader(chunk) {
+ if (bufferJoiner.length + chunk.length >= 9) {
+ var header = bufferJoiner.add(chunk).join()
+ eventLength = header.readUInt32BE(0)
+ messageLength = header.readUInt32BE(4)
+ messagetype = header.readInt8(8)
+ fetchBody(chunk.slice(9))
+ } else {
+ bufferJoiner.add(chunk)
+ }
}
- // make sure we listen to the underlying socket
- this.socket.on(startName, this._onStart.bind(this));
- this.socket.on('close', this._onClose.bind(this));
+ function fetchBody(chunk) {
+ var raw, event, data
+ var chunkLength = chunk.length
+ var bytesLeft = (eventLength + messageLength) - bufferJoiner.length
- if (this.socket.socket) {
- //
- // otherwise we get a error passed from net.js
- // they need to backport the fix from v5 to v4
- //
- this.socket.socket.on('error', this._onError.bind(this));
- }
+ if (chunkLength >= bytesLeft) {
+ raw = bufferJoiner.add(chunk.slice(0, bytesLeft)).join()
+ event = JSON.parse(raw.slice(0, eventLength))
+ data = messagetype ? raw.slice(eventLength) : JSON.parse(raw.slice(eventLength).toString())
- this.socket.on('error', this._onError.bind(this));
- this.socket.on('timeout', this._onIdle.bind(this));
-};
-
-//
-// ### @private function _onStart ()
-// Emits a start event when the underlying socket finish connecting
-// might be used to do other activities.
-//
-NsSocket.prototype._onStart = function _onStart() {
- this.emit('start');
-};
-
-//
-// ### @private function _onData (message)
-// #### @chunk {Buffer} binary chunk from the data event of the socket
-// Messages are assumed to be formatted properly (if using nssocket to send)
-// We assume messages arrive in order.
-//
-NsSocket.prototype._onData = function _onData(chunk) {
- ~this._messageLength
- ? this._fetchBody(chunk)
- : this._fetchHeader(chunk);
-};
-
-//
-// ### @private function _fetchHeader (chunk)
-// #### @chunk {Buffer} binary chunk from the data event of the socket
-// Buffers and parse the header of the message
-//
-NsSocket.prototype._fetchHeader = function _fetchHeader(chunk) {
- if (this._bufferJoiner.length + chunk.length >= 9) {
- var header = this._bufferJoiner.add(chunk).join();
- this._eventLength = header.readUInt32BE(0);
- this._messageLength = header.readUInt32BE(4);
- this._messagetype = header.readInt8(8);
- this._fetchBody(chunk.slice(9));
- } else {
- this._bufferJoiner.add(chunk);
- }
-};
-
-//
-// ### @private function _fetchBody (chunk)
-// #### @chunk {Buffer} binary chunk from the data event of the socket
-// Buffers and parse the body of the message, when ready
-// emits a data::* event
-//
-NsSocket.prototype._fetchBody = function _fetchBody(chunk) {
- var raw, event, data;
- var chunkLength = chunk.length;
- var bytesLeft = (this._eventLength + this._messageLength) - this._bufferJoiner.length;
-
- if (chunkLength >= bytesLeft) {
- raw = this._bufferJoiner.add(chunk.slice(0, bytesLeft)).join();
- event = JSON.parse(raw.slice(0, this._eventLength));
- data = this._messagetype ? raw.slice(this._eventLength) : JSON.parse(raw.slice(this._eventLength).toString());
-
- this._eventLength = -1;
- this._messageLength = -1;
- this.emit(['data'].concat(event), data);
-
- if (chunkLength - bytesLeft) {
- this._fetchHeader(chunk.slice(bytesLeft));
+ eventLength = -1
+ messageLength = -1
+
+ self.emit(['data'].concat(event), data)
+
+ if (chunkLength - bytesLeft) {
+ fetchHeader(chunk.slice(bytesLeft))
+ }
+
+ return
}
- return;
+ bufferJoiner.add(chunk)
}
- this._bufferJoiner.add(chunk);
-};
-
-//
-// ### @private function _onClose (hadError)
-// #### @hadError {Boolean} true if there was an error, which then include the
-// actual error included by the underlying socket
-//
-NsSocket.prototype._onClose = function _onClose(hadError) {
- if (hadError) {
- this.emit('close', hadError, arguments[1]);
- }
- else {
- this.emit('close');
- }
+ self.socket.on('close', function onClose(hadError) {
+ self.socket = undefined
+ self.connected = false
- this.connected = false;
- if (this._reconnect) {
- this.reconnect();
- }
-};
-
-//
-// ### @private function _onError (error)
-// #### @error {Error} emits and error event in place of the socket
-// Error event is raise with an error if there was one
-//
-NsSocket.prototype._onError = function _onError(error) {
- this.connected = false;
-
- if (!this._reconnect) {
- return this.emit('error', error || new Error('An Unknown Error occured'));
- }
+ if (hadError) {
+ self.emit('close', hadError, arguments[1])
+ } else {
+ self.emit('close')
+ }
- this.reconnect();
-};
-
-//
-// ### @private function _onIdle ()
-// #### Emits the idle event (based on timeout)
-//
-NsSocket.prototype._onIdle = function _onIdle() {
- this.emit('idle');
- if (this._timeout) {
- this.socket.setTimeout(this._timeout);
- }
-};
+ self.retry && self.reconnect()
+ })
+
+ self.socket.on('error', function (err) {
+ !self.retry && self.emit('error', err || new Error('An Unknown Error occured'))
+ })
+
+ self.socket.on('timeout', function onIdle() {
+ self.emit('idle')
+ self.timeout && self.socket.setTimeout(this.timeout)
+ })
+}
View
16 test-mocha/fixtures/ryans-cert.pem
@@ -1,16 +0,0 @@
------BEGIN CERTIFICATE-----
-MIICfzCCAegCCQCE5Xuxkur1mjANBgkqhkiG9w0BAQUFADCBgzELMAkGA1UEBhMC
-VVMxCzAJBgNVBAgMAk5ZMQswCQYDVQQHDAJOWTEVMBMGA1UECgwMSW50ZXJuZXQu
-Y29tMRgwFgYDVQQLDA9JbnRlcm5ldCBOaW5qYXMxDjAMBgNVBAMMBU51am9vMRkw
-FwYJKoZIhvcNAQkBFgpudUBqb28uY29tMB4XDTExMTAxMzIxNDgxOVoXDTExMTEx
-MjIxNDgxOVowgYMxCzAJBgNVBAYTAlVTMQswCQYDVQQIDAJOWTELMAkGA1UEBwwC
-TlkxFTATBgNVBAoMDEludGVybmV0LmNvbTEYMBYGA1UECwwPSW50ZXJuZXQgTmlu
-amFzMQ4wDAYDVQQDDAVOdWpvbzEZMBcGCSqGSIb3DQEJARYKbnVAam9vLmNvbTCB
-nzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA2hKAVOtI/mNUpOkuHgBDaIu0EelR
-SdEc5GoqfIeH+59VkJqkIf+lAgRJk7PSJuwGY5Kzq6DwD/RStzk+S/Z+5h/61YQX
-D28SOf9d53+hSn6NpetOa0f9gL+ouc0MkSL9fOW8geD07Yv0N8XpdgiSQG6jp6Wd
-yo2R7xJaoBjRfP8CAwEAATANBgkqhkiG9w0BAQUFAAOBgQC7DFd1pWGVwzGGYAq0
-KkJYCJQoq6NgthHQ206U/3cFlelSG2NqbnjfAxPX1N7waT2FIa2yE/Ax6AMZDw8A
-v3hxwo+c0j5YzCBfFXXbP/8jZtWEUuj5bDa0rplqP1JwDa0JTqxuIdpgVANa8FLE
-NkLSqWOjovXCdekT/LaN84s5aw==
------END CERTIFICATE-----
View
12 test-mocha/fixtures/ryans-csr.pem
@@ -1,12 +0,0 @@
------BEGIN CERTIFICATE REQUEST-----
-MIIBxDCCAS0CAQAwgYMxCzAJBgNVBAYTAlVTMQswCQYDVQQIDAJOWTELMAkGA1UE
-BwwCTlkxFTATBgNVBAoMDEludGVybmV0LmNvbTEYMBYGA1UECwwPSW50ZXJuZXQg
-TmluamFzMQ4wDAYDVQQDDAVOdWpvbzEZMBcGCSqGSIb3DQEJARYKbnVAam9vLmNv
-bTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA2hKAVOtI/mNUpOkuHgBDaIu0
-EelRSdEc5GoqfIeH+59VkJqkIf+lAgRJk7PSJuwGY5Kzq6DwD/RStzk+S/Z+5h/6
-1YQXD28SOf9d53+hSn6NpetOa0f9gL+ouc0MkSL9fOW8geD07Yv0N8XpdgiSQG6j
-p6Wdyo2R7xJaoBjRfP8CAwEAAaAAMA0GCSqGSIb3DQEBBQUAA4GBADdnMH2brmA/
-pYWleVBOd1ZhkkXoTns0Lv0KNWpdjKT6mVxSsiiuKc8MBtWTFseM/kDxRm1VCjPL
-YcNZRtKVLOUn3cwwtYGJm9EMmvn0EzHr6p2IO6Z7JBETOHoJT9zXszbcvUFaRb+9
-7jTr6wsDY/raS3FdCBGp1nK8EVw8QT0r
------END CERTIFICATE REQUEST-----
View
15 test-mocha/fixtures/ryans-key.pem
@@ -1,15 +0,0 @@
------BEGIN RSA PRIVATE KEY-----
-MIICXQIBAAKBgQDaEoBU60j+Y1Sk6S4eAENoi7QR6VFJ0Rzkaip8h4f7n1WQmqQh
-/6UCBEmTs9Im7AZjkrOroPAP9FK3OT5L9n7mH/rVhBcPbxI5/13nf6FKfo2l605r
-R/2Av6i5zQyRIv185byB4PTti/Q3xel2CJJAbqOnpZ3KjZHvElqgGNF8/wIDAQAB
-AoGAX9PnfumRvu/pXzp0oIxfEs7pR0GvDfANcTZSCz0HfYQL9qpt297aJOO7bWOE
-wsPPHux1dcMYGvqzan6GKJ1eL3OzGDWdgVgMGLlp0mZj74QWCJDyBrAKseNJc/vV
-YXqq2nfb44yLVvFzZnJmD59FpDVgEdUw/KtVoA7Qg3MNnlECQQD/ITXaukRiCHMk
-/yJBbcyt7hb/M3gdknr1ophCnwIcoKFrJUUzY0LF5NXbA7OMDgK7Fg63blc8KEEi
-L7gZYdBJAkEA2tDuTElF2awoY791vrfaOhIpMcqTtn/glojhO9XYDt3iAY+o6LW3
-o2FZxt+7jwoQSUxN68lwc+1MXc1IafRzBwJAZPNzJ9VEcbX+OclqeJFFyBzJpLls
-8eagGMn5jYL1hvZYaNkahLbmGP/vTvYr+WMh2X1k3Vgf1IHpI+nV4tU9YQJBANl0
-Mq07UCBO92CRf8kF2uhE7g1eXUdLc/0FkJgvHuU/Wf/lLZ3+IL5L27VI2JMBFEhT
-fUhqSsfaNj8t593sIXcCQQDtWaKRz2QPAjhtM29xVihOBVPppdXdm1jSk25EciJX
-x4lrUXyNTeyQif0Hezp4WaYhVOS8uRGYnKH9eJzXQ5Pv
------END RSA PRIVATE KEY-----
View
21 test-mocha/buffer-tcp.test.js → test/buffer-tcp.test.js
@@ -1,24 +1,20 @@
var net = require('net'),
fs = require ('fs'),
expect = require('chai').expect,
- NsSocket = require('../lib/nssocket').NsSocket
+ nssocket = require('../')
-var trollface = fs.readFileSync('test-mocha/fixtures/trollface.jpg')
+var trollface = fs.readFileSync('test/fixtures/trollface.jpg')
var TCP_PORT = 5467
describe('nssocket/tcp/buffer', function () {
before(function (done) {
var self = this
- this.outbound = new NsSocket({
- type : 'tcp4',
- delimiter: '/'
- })
this.server = net.createServer(function (inbound) {
self.inbound = inbound
done()
})
- this.server.listen(TCP_PORT, function (stream) {
- self.outbound.connect(TCP_PORT)
+ this.server.listen(TCP_PORT, function () {
+ self.outbound = nssocket({ delimiter: '/' }).connect(TCP_PORT)
})
})
after(function () {
@@ -36,20 +32,19 @@ describe('nssocket/tcp/buffer', function () {
function testMulti(n, data) {
return function (done) {
var self = this
- var message = self.outbound.createMessage('test::multi', data)
+ var message = this.outbound.createMessage('test/multi', data)
var buffer = Buffer.concat(arrayOf(message, n), message.length * n)
function onMessage(data) {
n --
expect(data).to.be.eql(data)
if (!n) {
- self.outbound.undata('binary', onMessage)
+ self.outbound.off('data/test/multi', onMessage)
done()
}
}
-
- self.outbound.data('test::multi', onMessage)
- self.inbound.write(buffer)
+ this.outbound.on('data/test/multi', onMessage)
+ this.inbound.write(buffer)
}
}
View
24 test-mocha/buffer-tls.test.js → test/buffer-tls.test.js
@@ -1,25 +1,24 @@
var tls = require('tls'),
fs = require ('fs'),
expect = require('chai').expect,
- NsSocket = require('../lib/nssocket').NsSocket
+ nssocket = require('../')
-var trollface = fs.readFileSync('test-mocha/fixtures/trollface.jpg')
+var trollface = fs.readFileSync('test/fixtures/trollface.jpg')
var TCP_PORT = 5467
describe('nssocket/tls/buffer', function () {
before(function (done) {
var self = this
- this.outbound = new NsSocket({ type : 'tls', delimiter: '/' })
this.server = tls.createServer({
- key: fs.readFileSync('test-mocha/fixtures/ryans-key.pem'),
- cert: fs.readFileSync('test-mocha/fixtures/ryans-cert.pem'),
- ca: fs.readFileSync('test-mocha/fixtures/ryans-csr.pem')
+ key: fs.readFileSync('test/fixtures/ryans-key.pem'),
+ cert: fs.readFileSync('test/fixtures/ryans-cert.pem'),
+ ca: fs.readFileSync('test/fixtures/ryans-csr.pem')
}, function (inbound) {
self.inbound = inbound
done()
})
- this.server.listen(TCP_PORT, function (stream) {
- self.outbound.connect(TCP_PORT)
+ this.server.listen(TCP_PORT, function () {
+ self.outbound = nssocket({ type : 'tls', delimiter: '/' }).connect(TCP_PORT)
})
})
after(function () {
@@ -37,20 +36,19 @@ describe('nssocket/tls/buffer', function () {
function testMulti(n, data) {
return function (done) {
var self = this
- var message = self.outbound.createMessage('test::multi', data)
+ var message = this.outbound.createMessage('test/multi', data)
var buffer = Buffer.concat(arrayOf(message, n), message.length * n)
function onMessage(data) {
n --
expect(data).to.be.eql(data)
if (!n) {
- self.outbound.undata('binary', onMessage)
+ self.outbound.off('data/test/multi', onMessage)
done()
}
}
-
- self.outbound.data('test::multi', onMessage)
- self.inbound.write(buffer)
+ this.outbound.on('data/test/multi', onMessage)
+ this.inbound.write(buffer)
}
}
View
72 test/create-server-test.js
@@ -1,72 +0,0 @@
-/*
- * create-server-test.js : namespace socket unit test for TLS.
- *
- * (C) 2011, Nodejitsu Inc.
- *
- */
-
-var assert = require('assert'),
- fs = require('fs'),
- net = require('net'),
- path = require('path'),
- tls = require('tls'),
- vows = require('vows'),
- nssocket = require('../lib/nssocket');
-
-
-function getBatch() {
- var args = Array.prototype.slice.call(arguments),
- res = {};
-
- return {
- "the createServer() method": {
- topic: function () {
- var outbound = new nssocket.NsSocket(),
- server = nssocket.createServer(this.callback.bind(null, null, outbound));
-
- server.listen.apply(server, args.concat(function () {
- outbound.connect.apply(outbound, args);
- }));
- },
- "should create a full-duplex namespaced socket": {
- topic: function (outbound, inbound) {
- outbound.on(['data', 'here', 'is'], this.callback.bind(outbound, null));
- inbound.send(['here', 'is'], 'something.');
- },
- "should handle namespaced events": function (_, data) {
- assert.isArray(this.event);
- assert.lengthOf(this.event, 3);
- assert.isString(this.event[0]);
- assert.isString(this.event[1]);
- assert.isString(this.event[2]);
- assert.isString(data);
- assert.equal(this.event[0], 'data');
- assert.equal(this.event[1], 'here');
- assert.equal(this.event[2], 'is');
- assert.equal(data, 'something.');
- }
- }
- }
- };
-}
-
-var PORT = 9564,
- HOST = "127.0.0.1",
- PIPE = path.join(__dirname, "fixtures", "nssocket.sock"),
- HOSTNAME = "localhost";
-
-vows.describe('nssocket/create-server').addBatch({
- "When using NsSocket": {
- "with `(PORT)` argument": getBatch(PORT),
- "with `(PORT, HOST)` arguments": getBatch(PORT + 1, HOST),
- "with `(PORT, HOSTNAME)` argument": getBatch(PORT + 2, HOSTNAME),
- "with `(PIPE)` argument": getBatch(PIPE)
- }
-}).addBatch({
- "When tests are finished": {
- "`PIPE` should be removed": function () {
- fs.unlinkSync(PIPE);
- }
- }
-}).export(module);
-
View
8 test-mocha/create-server.test.js → test/create-server.test.js
@@ -14,13 +14,13 @@ var net = require('net'),
var PORT = 9568
var HOST = '127.0.0.1'
-var PIPE = path.join(__dirname, 'fixtures', 'nssocket.sock')
var HOSTNAME = 'localhost'
+var PIPE = path.join(__dirname, 'fixtures', 'nssocket.sock')
describe('nssocket/create-server', function () {
- describe('#createServer()', function () {
+ describe('#listen()', function () {
before(function() {
- try { fs.unlinkSync(PIPE) }
+ try { fs.unlinkSync(FD) }
catch (err) {}
})
afterEach(function (done) {
@@ -40,7 +40,6 @@ function testWith() {
return function (done) {
var self = this
- self.outbound = new nssocket.NsSocket()
self.server = nssocket.createServer(function (inbound) {
self.inbound = inbound
self.outbound.on(['data', 'here', 'is'], function (data) {
@@ -51,6 +50,7 @@ function testWith() {
self.inbound.send(['here', 'is'], 'something.');
})
self.server.listen.apply(self.server, args.concat(function () {
+ self.outbound = nssocket()
self.outbound.connect.apply(self.outbound, args)
}))
}
View
0  test-mocha/fixtures/trollface.jpg → test/fixtures/trollface.jpg
File renamed without changes
View
80 test/tcp-reconnect-test.js
@@ -1,80 +0,0 @@
-/*
- * nssocket-test.js : namespace socket unit test for TCP
- *
- * (C) 2011, Nodejitsu Inc.
- *
- */
-
-var assert = require('assert'),
- fs = require('fs'),
- net = require('net'),
- path = require('path'),
- vows = require('vows'),
- NsSocket = require('../lib/nssocket').NsSocket;
-
-var TCP_PORT = 30105;
-
-var tcpServer = net.createServer(),
- tcpOpt;
-
-tcpOpt = {
- type : 'tcp4',
- delimiter: '.}',
- reconnect: true,
- retryInterval: 1000
-};
-
-tcpServer.listen(TCP_PORT);
-
-vows.describe('nssocket/tcp/reconnect').addBatch({
- "When using NsSocket with TCP": {
- topic: new NsSocket(tcpOpt),
- "the connect() method": {
- topic: function (outbound) {
- var that = this;
- tcpServer.on('connection', this.callback.bind(null, null, outbound));
- outbound.connect(TCP_PORT);
- },
- "should actually connect": function (_, outbound, inbound) {
- assert.instanceOf(outbound, NsSocket);
- assert.instanceOf(inbound, net.Socket);
- },
- "when the server closes": {
- topic: function (outbound, inbound) {
- outbound.once('close', this.callback.bind(this, null, outbound));
- tcpServer.close();
- inbound.destroy();
- },
- "and then restarts": {
- topic: function (outbound) {
- tcpServer = net.createServer();
- tcpServer.listen(TCP_PORT);
- tcpServer.on('connection', this.callback.bind(null, null, outbound));
- },
- "the socket should reconnect correctly": function (_, outbound, inbound) {
- assert.instanceOf(outbound, NsSocket);
- assert.instanceOf(inbound, net.Socket);
- },
- "the on() method": {
- topic: function (outbound, inbound) {
- outbound.on('data.}here.}is', this.callback.bind(outbound, null));
- inbound.write(Buffer('0000000d0000000c005b2268657265222c226973225d22736f6d657468696e672e22', 'hex'));
- },
- "should handle namespaced events": function (_, data) {
- assert.isArray(this.event);
- assert.lengthOf(this.event, 3);
- assert.isString(this.event[0]);
- assert.isString(this.event[1]);
- assert.isString(this.event[2]);
- assert.isString(data);
- assert.equal(this.event[0], 'data');
- assert.equal(this.event[1], 'here');
- assert.equal(this.event[2], 'is');
- assert.equal(data, 'something.');
- }
- }
- }
- }
- }
- }
-}).export(module);
View
22 test-mocha/tcp-reconnect.test.js → test/tcp-reconnect.test.js
@@ -23,13 +23,13 @@ var TCP_OPTIONS = {
describe('nssocket/tcp/reconnect', function () {
after(function (done) {
this.server.close(done)
- this.receiver.end()
- this.sender.end()
+ this.outbound.end()
+ this.inbound.end()
})
describe('client', function () {
it('should connect', function (done) {
var self = this
- self.receiver = new nssocket.NsSocket(TCP_OPTIONS).connect(TCP_PORT)
+ self.outbound = nssocket(TCP_OPTIONS).connect(TCP_PORT)
self.server = nssocket.createServer(TCP_OPTIONS, done.bind(null, null))
setTimeout(function () {
self.server.listen(TCP_PORT)
@@ -37,21 +37,21 @@ describe('nssocket/tcp/reconnect', function () {
})
it('should handle reconnection', function (done) {
var self = this
- self.server.close()
- self.receiver.end()
- self.server = nssocket.createServer(TCP_OPTIONS, function (sender) {
- self.sender = sender
- done()
+ self.server.close(function () {
+ self.server = nssocket.createServer(TCP_OPTIONS, function (inbound) {
+ self.inbound = inbound
+ done()
+ }).listen(TCP_PORT)
})
- self.server.listen(TCP_PORT)
+ self.outbound.end()
})
it('should be able to send after reconnection', function (done) {
- this.receiver.once('data.}here.}is', function (data) {
+ this.outbound.once('data.}here.}is', function (data) {
assert.deepEqual(this.event, ['data', 'here', 'is'])
assert.equal(data, 'something.')
done()
})
- this.sender.send('here.}is', 'something.')
+ this.inbound.send('here.}is', 'something.')
})
})
})
View
100 test/tcp-test.js
@@ -1,100 +0,0 @@
-/*
- * nssocket-test.js : namespace socket unit test for TCP
- *
- * (C) 2011, Nodejitsu Inc.
- *
- */
-
-var assert = require('assert'),
- fs = require('fs'),
- net = require('net'),
- path = require('path'),
- vows = require('vows'),
- NsSocket = require('../lib/nssocket').NsSocket;
-
-var TCP_PORT = 30103;
-
-var tcpServer = net.createServer(),
- tcpOpt;
-
-tcpOpt = {
- type : 'tcp4',
- delimiter: '.}'
-};
-
-tcpServer.listen(TCP_PORT);
-
-vows.describe('nssocket/tcp').addBatch({
- "When using NsSocket with TCP": {
- topic: new NsSocket(tcpOpt),
- "should create a wrapped socket": function (outbound) {
- assert.instanceOf(outbound, NsSocket);
- },
- "should have the proper configuration settings": function (outbound) {
- assert.equal(outbound._type, tcpOpt.type);
- assert.equal(outbound._delimiter, tcpOpt.delimiter);
- },
- "the connect() method": {
- topic: function (outbound) {
- var that = this;
- tcpServer.on('connection', this.callback.bind(null, null, outbound));
- outbound.connect(TCP_PORT);
- },
- "should actually connect": function (_, outbound, inbound) {
- assert.instanceOf(outbound, NsSocket);
- assert.instanceOf(inbound, net.Socket);
- },
- "the on() method": {
- topic: function (outbound, inbound) {
- outbound.on('data.}here.}is', this.callback.bind(outbound, null));
- inbound.write(Buffer('0000000d0000000c005b2268657265222c226973225d22736f6d657468696e672e22', 'hex'));
- },
- "should handle namespaced events": function (_, data) {
- assert.isArray(this.event);
- assert.lengthOf(this.event, 3);
- assert.isString(this.event[0]);
- assert.isString(this.event[1]);
- assert.isString(this.event[2]);
- assert.isString(data);
- assert.equal(this.event[0], 'data');
- assert.equal(this.event[1], 'here');
- assert.equal(this.event[2], 'is');
- assert.equal(data, 'something.');
- },
- "once idle": {
- topic: function (_, outbound, inbound) {
- outbound.once('idle', this.callback.bind(null, null, outbound, inbound));
- outbound.setIdle(100);
- },
- "it should emit `idle`": function (_, outbound, inbound) {
- assert.isNull(_);
- },
- "the send() method": {
- topic: function (outbound, inbound) {
- inbound.on('data', this.callback.bind(null, null, outbound, inbound));
- outbound.send(['hello','world'], Buffer('foo::bar'));
- },
- "we should see it on the other end": function (_, outbound, wraped, data) {
- assert.isObject(data);
- event = JSON.parse(data.slice(9, 26).toString());
- data = data.slice(26).toString();
- assert.lengthOf(event, 2);
- assert.equal(event[0], 'hello');
- assert.equal(event[1], 'world');
- assert.deepEqual(data, 'foo::bar');
- },
- "the end() method": {
- topic: function (outbound, inbound) {
- outbound.on('close', this.callback.bind(null, null, outbound, inbound));
- inbound.end();
- },
- "should close without errors": function (_, _, _, err) {
- assert.isUndefined(err);
- }
- }
- }
- }
- }
- }
- }
-}).export(module);
View
13 test-mocha/tcp.test.js → test/tcp.test.js
@@ -21,16 +21,19 @@ var TCP_OPTIONS = {
}
describe('nssocket/tcp', function () {
+ after(function (done) {
+ this.server.close(done)
+ })
describe('#()', function () {
before(function () {
- this.outbound = new nssocket.NsSocket(TCP_OPTIONS)
+ this.outbound = nssocket(TCP_OPTIONS)
})
it('should create a wrapped socket', function () {
- assert.instanceOf(this.outbound, nssocket.NsSocket)
+ assert.instanceOf(this.outbound, nssocket)
})
it('should have the proper configuration settings', function () {
- assert.equal(this.outbound._type, TCP_OPTIONS.type)
- assert.equal(this.outbound._delimiter, TCP_OPTIONS.delimiter)
+ assert.equal(this.outbound.type, TCP_OPTIONS.type)
+ assert.equal(this.outbound.delimiter, TCP_OPTIONS.delimiter)
})
})
describe('#connect()', function () {
@@ -84,7 +87,7 @@ describe('nssocket/tcp', function () {
assert.equal(data, 'something.')
done()