feat: 초기 프로젝트 설정 및 룰.md 파일 추가
This commit is contained in:
19
api.hyungi.net/node_modules/nssocket/LICENSE
generated
vendored
Normal file
19
api.hyungi.net/node_modules/nssocket/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
Copyright (C) 2011 Charlie Robbins, Paolo Fragomeni, & the Contributors.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
143
api.hyungi.net/node_modules/nssocket/README.md
generated
vendored
Normal file
143
api.hyungi.net/node_modules/nssocket/README.md
generated
vendored
Normal file
@@ -0,0 +1,143 @@
|
||||
# Synposis
|
||||
An elegant way to define lightweight protocols on-top of TCP/TLS sockets in node.js
|
||||
|
||||
# Motivation
|
||||
Working within node.js it is very easy to write lightweight network protocols that communicate over TCP or TLS. The definition of such protocols often requires repeated (and tedious) parsing of individual TCP/TLS packets into a message header and some JSON body.
|
||||
|
||||
# Build Status
|
||||
[](http://travis-ci.org/nodejitsu/nssocket)
|
||||
|
||||
# Installation
|
||||
```
|
||||
[sudo] npm install nssocket
|
||||
```
|
||||
|
||||
# How it works
|
||||
|
||||
With `nssocket` this tedious bookkeeping work is done automatically for you in two ways:
|
||||
|
||||
1. Leverages wildcard and namespaced events from [EventEmitter2][0]
|
||||
2. Automatically serializes messages passed to `.send()` and deserializes messages from `data` events.
|
||||
3. Implements default reconnect logic for potentially faulty connections.
|
||||
4. Automatically wraps TCP connections with TLS using [a known workaround][1]
|
||||
|
||||
## Messages
|
||||
Messages in `nssocket` are serialized JSON arrays of the following form:
|
||||
|
||||
``` js
|
||||
["namespace", "to", "event", { "this": "is", "the": "payload" }]
|
||||
```
|
||||
|
||||
Although this is not as optimal as other message formats (pure binary, msgpack) most of your applications are probably IO-bound, and not by the computation time needed for serialization / deserialization. When working with `NsSocket` instances, all events are namespaced under `data` to avoid collision with other events.
|
||||
|
||||
## Simple Example
|
||||
``` js
|
||||
var nssocket = require('nssocket');
|
||||
|
||||
//
|
||||
// Create an `nssocket` TCP server
|
||||
//
|
||||
var server = nssocket.createServer(function (socket) {
|
||||
//
|
||||
// Here `socket` will be an instance of `nssocket.NsSocket`.
|
||||
//
|
||||
socket.send(['you', 'there']);
|
||||
socket.data(['iam', 'here'], function (data) {
|
||||
//
|
||||
// Good! The socket speaks our language
|
||||
// (i.e. simple 'you::there', 'iam::here' protocol)
|
||||
//
|
||||
// { iam: true, indeedHere: true }
|
||||
//
|
||||
console.dir(data);
|
||||
})
|
||||
});
|
||||
|
||||
//
|
||||
// Tell the server to listen on port `6785` and then connect to it
|
||||
// using another NsSocket instance.
|
||||
//
|
||||
server.listen(6785);
|
||||
|
||||
var outbound = new nssocket.NsSocket();
|
||||
outbound.data(['you', 'there'], function () {
|
||||
outbound.send(['iam', 'here'], { iam: true, indeedHere: true });
|
||||
});
|
||||
|
||||
outbound.connect(6785);
|
||||
```
|
||||
|
||||
## Reconnect Example
|
||||
`nssocket` exposes simple options for enabling reconnection of the underlying socket. By default, these options are disabled. Lets look at a simple example:
|
||||
|
||||
``` js
|
||||
var net = require('net'),
|
||||
nssocket = require('nssocket');
|
||||
|
||||
net.createServer(function (socket) {
|
||||
//
|
||||
// Close the underlying socket after `1000ms`
|
||||
//
|
||||
setTimeout(function () {
|
||||
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);
|
||||
```
|
||||
|
||||
# API
|
||||
|
||||
### socket.send(event, data)
|
||||
Writes `data` to the socket with the specified `event`, on the receiving end it will look like: `JSON.stringify([event, data])`.
|
||||
|
||||
### socket.on(event, callback)
|
||||
Equivalent to the underlying `.addListener()` or `.on()` function on the underlying socket except that it will permit all `EventEmitter2` wildcards and namespaces.
|
||||
|
||||
### socket.data(event, callback)
|
||||
Helper function for performing shorthand listeners namespaced under the `data` event. For example:
|
||||
|
||||
``` js
|
||||
//
|
||||
// These two statements are equivalent
|
||||
//
|
||||
someSocket.on(['data', 'some', 'event'], function (data) { });
|
||||
someSocket.data(['some', 'event'], function (data) { });
|
||||
```
|
||||
|
||||
### socket.end()
|
||||
Closes the current socket, emits `close` event, possibly also `error`
|
||||
|
||||
### socket.destroy()
|
||||
Remove all listeners, destroys socket, clears buffer. It is recommended that you use `socket.end()`.
|
||||
|
||||
## Tests
|
||||
All tests are written with [vows][2] and should be run through [npm][3]:
|
||||
|
||||
``` bash
|
||||
$ npm test
|
||||
```
|
||||
|
||||
### Author: [Nodejitsu](http://www.nodejitsu.com)
|
||||
### Contributors: [Paolo Fragomeni](http://github.com/hij1nx), [Charlie Robbins](http://github.com/indexzero), [Jameson Lee](http://github.com/drjackal), [Gene Diaz Jr.](http://github.com/genediazjr)
|
||||
|
||||
[0]: http://github.com/hij1nx/eventemitter2
|
||||
[1]: https://gist.github.com/848444
|
||||
[2]: http://vowsjs.org
|
||||
[3]: http://npmjs.org
|
||||
18
api.hyungi.net/node_modules/nssocket/examples/bla.js
generated
vendored
Normal file
18
api.hyungi.net/node_modules/nssocket/examples/bla.js
generated
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
|
||||
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);
|
||||
}
|
||||
console.dir(data);
|
||||
});
|
||||
|
||||
}).listen(4949);
|
||||
10
api.hyungi.net/node_modules/nssocket/examples/foo.js
generated
vendored
Normal file
10
api.hyungi.net/node_modules/nssocket/examples/foo.js
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
var nssocket = require('../lib/nssocket');
|
||||
var outbound = new nssocket.NsSocket();
|
||||
|
||||
outbound.data('Broadcasting', function (data) {
|
||||
console.log(data)
|
||||
});
|
||||
|
||||
outbound.connect(4949);
|
||||
|
||||
outbound.send('Connecting', { "random": Math.random() });
|
||||
29
api.hyungi.net/node_modules/nssocket/examples/reconnect.js
generated
vendored
Normal file
29
api.hyungi.net/node_modules/nssocket/examples/reconnect.js
generated
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
var net = require('net'),
|
||||
nssocket = require('../lib/nssocket');
|
||||
|
||||
net.createServer(function (socket) {
|
||||
//
|
||||
// Close the underlying socket after `1000ms`
|
||||
//
|
||||
setTimeout(function () {
|
||||
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);
|
||||
51
api.hyungi.net/node_modules/nssocket/examples/simple-protocol.js
generated
vendored
Normal file
51
api.hyungi.net/node_modules/nssocket/examples/simple-protocol.js
generated
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
var nssocket = require('../lib/nssocket');
|
||||
|
||||
//
|
||||
// define a simple message protocol as [<type>, <id>] and create some messages that use it.
|
||||
//
|
||||
var message1 = ['message', 'one'];
|
||||
var message2 = ['message', 'two'];
|
||||
|
||||
//
|
||||
// Create an `nssocket` TCP server and tell the server to listen on port `6785`.
|
||||
//
|
||||
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);
|
||||
|
||||
//
|
||||
// listen for `message2` from the connecting socket.
|
||||
//
|
||||
socket.data(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);
|
||||
})
|
||||
|
||||
}).listen(6785);
|
||||
|
||||
//
|
||||
// Create a new `nssocket` instance and then connect to the server in 1000 miliseconds.
|
||||
//
|
||||
setTimeout(function() {
|
||||
|
||||
var outbound = new nssocket.NsSocket();
|
||||
|
||||
//
|
||||
//
|
||||
//
|
||||
outbound.data(message1, function () {
|
||||
outbound.send(message2, { "foo": "bar" });
|
||||
});
|
||||
|
||||
outbound.connect(6785);
|
||||
|
||||
}, 1000);
|
||||
37
api.hyungi.net/node_modules/nssocket/examples/verbose-protocol.js
generated
vendored
Normal file
37
api.hyungi.net/node_modules/nssocket/examples/verbose-protocol.js
generated
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
var nssocket = require('../lib/nssocket');
|
||||
|
||||
//
|
||||
// Create an `nssocket` TCP server
|
||||
//
|
||||
var server = nssocket.createServer(function (socket) {
|
||||
//
|
||||
// Here `socket` will be an instance of `nssocket.NsSocket`.
|
||||
//
|
||||
|
||||
socket.send(['drink', 'rum']);
|
||||
socket.send(['drink', 'vodka']);
|
||||
|
||||
// socket.data(['iam', 'here'], function (data) {
|
||||
// //
|
||||
// // Good! The socket speaks our language
|
||||
// // (i.e. simple 'you::there', 'iam::here' protocol)
|
||||
// //
|
||||
// // { iam: true, indeedHere: true }
|
||||
// //
|
||||
// console.dir(data);
|
||||
// });
|
||||
});
|
||||
|
||||
//
|
||||
// Tell the server to listen on port `6785` and then connect to it
|
||||
// using another NsSocket instance.
|
||||
//
|
||||
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);
|
||||
105
api.hyungi.net/node_modules/nssocket/lib/common.js
generated
vendored
Normal file
105
api.hyungi.net/node_modules/nssocket/lib/common.js
generated
vendored
Normal file
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* common.js
|
||||
*
|
||||
* (C) 2011, Charlie Robbins, Paolo Fragomeni, & the Contributors.
|
||||
*/
|
||||
|
||||
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;
|
||||
}
|
||||
501
api.hyungi.net/node_modules/nssocket/lib/nssocket.js
generated
vendored
Normal file
501
api.hyungi.net/node_modules/nssocket/lib/nssocket.js
generated
vendored
Normal file
@@ -0,0 +1,501 @@
|
||||
/*
|
||||
* nssocket.js - Wraps a TLS/TCP socket to emit namespace events also auto-buffers.
|
||||
*
|
||||
* (C) 2011, Charlie Robbins, Paolo Fragomeni, & the Contributors.
|
||||
*
|
||||
*/
|
||||
|
||||
var net = require('net'),
|
||||
tls = require('tls'),
|
||||
util = require('util'),
|
||||
events2 = require('eventemitter2'),
|
||||
Lazy = require('lazy'),
|
||||
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) {
|
||||
if (!(this instanceof NsSocket)) {
|
||||
return new NsSocket(socket, options);
|
||||
}
|
||||
|
||||
//
|
||||
// If there is no Socket instnace to wrap,
|
||||
// create one.
|
||||
//
|
||||
if (!options) {
|
||||
options = socket;
|
||||
socket = common.createSocket(options);
|
||||
}
|
||||
|
||||
//
|
||||
// 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,
|
||||
interval: options.retryInterval || 5000,
|
||||
wait: options.retryInterval || 5000
|
||||
};
|
||||
|
||||
//
|
||||
// Setup default instance variables.
|
||||
//
|
||||
this._options = options;
|
||||
this._type = options.type || 'tcp4',
|
||||
this._delimiter = options.delimiter || '::';
|
||||
|
||||
//
|
||||
// Setup encode format.
|
||||
//
|
||||
this._encode = options.encode || JSON.stringify;
|
||||
this._decode = options.decode || JSON.parse;
|
||||
|
||||
events2.EventEmitter2.call(this, {
|
||||
delimiter: this._delimiter,
|
||||
wildcard: true,
|
||||
maxListeners: options.maxListeners || 10
|
||||
});
|
||||
|
||||
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 = {};
|
||||
}
|
||||
|
||||
options.type = options.type || 'tcp4';
|
||||
options.delimiter = options.delimiter || '::';
|
||||
|
||||
function onConnection (socket) {
|
||||
//
|
||||
// Incoming socket connections cannot reconnect
|
||||
// by definition.
|
||||
//
|
||||
options.reconnect = false;
|
||||
connectionListener(new NsSocket(socket, options));
|
||||
}
|
||||
|
||||
return options.type === 'tls'
|
||||
? tls.createServer(options, onConnection)
|
||||
: net.createServer(options, onConnection);
|
||||
};
|
||||
|
||||
//
|
||||
// ### function send (data, callback)
|
||||
// #### @event {Array|string} The array (or string) that holds the event name
|
||||
// #### @data {Literal|Object} 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) {
|
||||
var dataType = typeof data,
|
||||
message;
|
||||
|
||||
// rebinds
|
||||
if (typeof event === 'string') {
|
||||
event = event.split(this._delimiter);
|
||||
}
|
||||
|
||||
if (dataType === 'undefined' || dataType === 'function') {
|
||||
callback = data;
|
||||
data = null;
|
||||
}
|
||||
|
||||
// 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'));
|
||||
}
|
||||
|
||||
message = Buffer(this._encode(event.concat(data)) + '\n');
|
||||
|
||||
if (this.socket.cleartext) {
|
||||
this.socket.cleartext.write(message, callback);
|
||||
}
|
||||
else {
|
||||
// now actually write to the socket
|
||||
this.socket.write(message, callback);
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// ### 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) {
|
||||
if (typeof event === 'string') {
|
||||
event = event.split(this._delimiter);
|
||||
}
|
||||
|
||||
this.on(['data'].concat(event), callback);
|
||||
};
|
||||
|
||||
NsSocket.prototype.undata = function (event, listener) {
|
||||
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) {
|
||||
if (typeof event === 'string') {
|
||||
event = event.split(this._delimiter);
|
||||
}
|
||||
|
||||
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() {
|
||||
if (this.socket) {
|
||||
try {
|
||||
this.socket.end(); // send FIN
|
||||
this.socket.destroy(); // make sure fd's are gone
|
||||
}
|
||||
catch (ex) {
|
||||
// do nothing on errors
|
||||
}
|
||||
}
|
||||
|
||||
// clear buffer
|
||||
this.data = '';
|
||||
this.emit('destroy');
|
||||
|
||||
// this should forcibly remove EVERY listener
|
||||
this.removeAllListeners();
|
||||
};
|
||||
|
||||
//
|
||||
// ### function end (void)
|
||||
// #### closes the underlying socket, recommend you call destroy after
|
||||
//
|
||||
NsSocket.prototype.end = function end() {
|
||||
this.connected = false;
|
||||
|
||||
if (this.socket) {
|
||||
try {
|
||||
this.socket.end();
|
||||
}
|
||||
catch (ex) {
|
||||
this.emit('error', ex);
|
||||
return;
|
||||
}
|
||||
|
||||
this.socket = null;
|
||||
}
|
||||
|
||||
return;
|
||||
};
|
||||
|
||||
//
|
||||
// ### 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;
|
||||
}
|
||||
});
|
||||
|
||||
this.port = port || this.port;
|
||||
this.host = host || this.host;
|
||||
this.host = this.host || '127.0.0.1';
|
||||
args = this.port ? [this.port, this.host] : [this.host];
|
||||
|
||||
if (callback) {
|
||||
args.push(callback);
|
||||
}
|
||||
|
||||
if (['tcp4', 'tls'].indexOf(this._type) === -1) {
|
||||
return this.emit('error', new Error('Unknown Socket Type'));
|
||||
}
|
||||
|
||||
var errHandlers = self.listeners('error');
|
||||
|
||||
if (errHandlers.length > 0) {
|
||||
//
|
||||
// copy the last error from nssocker onto the error event.
|
||||
//
|
||||
self.socket._events.error = errHandlers[errHandlers.length-1];
|
||||
}
|
||||
|
||||
this.connected = true;
|
||||
this.socket.connect.apply(this.socket, args);
|
||||
};
|
||||
|
||||
//
|
||||
// ### 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;
|
||||
|
||||
//
|
||||
// Helper function containing the core reconnect logic
|
||||
//
|
||||
function doReconnect() {
|
||||
//
|
||||
// Cleanup and recreate the socket associated
|
||||
// with this instance.
|
||||
//
|
||||
self.retry.waiting = true;
|
||||
self.socket.removeAllListeners();
|
||||
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();
|
||||
}
|
||||
|
||||
//
|
||||
// Helper function which attempts to retry if
|
||||
// it is less than the maximum
|
||||
//
|
||||
function tryReconnect() {
|
||||
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));
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
function bindData(sock) {
|
||||
Lazy(sock)
|
||||
.lines
|
||||
.map(String)
|
||||
.forEach(self._onData.bind(self));
|
||||
}
|
||||
|
||||
//
|
||||
// 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';
|
||||
|
||||
bindData(this.socket);
|
||||
|
||||
// create a stub for the setKeepAlive functionality
|
||||
this.setKeepAlive = function () {
|
||||
self.socket.setKeepAlive.apply(self.socket, arguments);
|
||||
};
|
||||
}
|
||||
else if (this._type === 'tls') {
|
||||
startName = 'secureConnection';
|
||||
|
||||
if (this.connected) {
|
||||
bindData(self.socket);
|
||||
} else {
|
||||
this.socket.once('connect', function () {
|
||||
bindData(self.socket.cleartext);
|
||||
});
|
||||
}
|
||||
|
||||
// create a stub for the setKeepAlive functionality
|
||||
this.setKeepAlive = function () {
|
||||
self.socket.socket.setKeepAlive.apply(self.socket.socket, arguments);
|
||||
};
|
||||
}
|
||||
else {
|
||||
// bad arguments, so throw an error
|
||||
this.emit('error', new Error('Bad Option Argument [type]'));
|
||||
return null;
|
||||
}
|
||||
|
||||
// make sure we listen to the underlying socket
|
||||
this.socket.on(startName, this._onStart.bind(this));
|
||||
this.socket.on('close', this._onClose.bind(this));
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
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)
|
||||
// #### @message {String} literal message from the data event of the socket
|
||||
// Messages are assumed to be delimited properly (if using nssocket to send)
|
||||
// otherwise the delimiter should exist at the end of every message
|
||||
// We assume messages arrive in order.
|
||||
//
|
||||
NsSocket.prototype._onData = function _onData(message) {
|
||||
var parsed,
|
||||
data;
|
||||
|
||||
try {
|
||||
parsed = this._decode(message);
|
||||
data = parsed.pop();
|
||||
}
|
||||
catch (err) {
|
||||
//
|
||||
// Don't do anything, assume that the message is only partially
|
||||
// received.
|
||||
//
|
||||
}
|
||||
this.emit(['data'].concat(parsed), data);
|
||||
};
|
||||
|
||||
//
|
||||
// ### @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) {
|
||||
this.connected = false;
|
||||
|
||||
if (hadError) {
|
||||
this.emit('close', hadError, arguments[1]);
|
||||
}
|
||||
else {
|
||||
this.emit('close');
|
||||
}
|
||||
|
||||
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'));
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
||||
248
api.hyungi.net/node_modules/nssocket/node_modules/eventemitter2/README.md
generated
vendored
Normal file
248
api.hyungi.net/node_modules/nssocket/node_modules/eventemitter2/README.md
generated
vendored
Normal file
@@ -0,0 +1,248 @@
|
||||
[](https://www.codeship.io/projects/11259)
|
||||
|
||||
# SYNOPSIS
|
||||
|
||||
EventEmitter2 is an implementation of the EventEmitter found in Node.js
|
||||
|
||||
# DESCRIPTION
|
||||
|
||||
### FEATURES
|
||||
- Namespaces/Wildcards.
|
||||
- Times To Listen (TTL), extends the `once` concept with `many`.
|
||||
- Browser environment compatibility.
|
||||
- Demonstrates good performance in benchmarks
|
||||
|
||||
```
|
||||
EventEmitterHeatUp x 3,728,965 ops/sec \302\2610.68% (60 runs sampled)
|
||||
EventEmitter x 2,822,904 ops/sec \302\2610.74% (63 runs sampled)
|
||||
EventEmitter2 x 7,251,227 ops/sec \302\2610.55% (58 runs sampled)
|
||||
EventEmitter2 (wild) x 3,220,268 ops/sec \302\2610.44% (65 runs sampled)
|
||||
Fastest is EventEmitter2
|
||||
```
|
||||
|
||||
### Differences (Non breaking, compatible with existing EventEmitter)
|
||||
|
||||
- The constructor takes a configuration object.
|
||||
|
||||
```javascript
|
||||
var EventEmitter2 = require('eventemitter2').EventEmitter2;
|
||||
var server = new EventEmitter2({
|
||||
|
||||
//
|
||||
// use wildcards.
|
||||
//
|
||||
wildcard: true,
|
||||
|
||||
//
|
||||
// the delimiter used to segment namespaces, defaults to `.`.
|
||||
//
|
||||
delimiter: '::',
|
||||
|
||||
//
|
||||
// if you want to emit the newListener event set to true.
|
||||
//
|
||||
newListener: false,
|
||||
|
||||
//
|
||||
// max listeners that can be assigned to an event, default 10.
|
||||
//
|
||||
maxListeners: 20
|
||||
});
|
||||
```
|
||||
|
||||
- Getting the actual event that fired.
|
||||
|
||||
```javascript
|
||||
server.on('foo.*', function(value1, value2) {
|
||||
console.log(this.event, value1, value2);
|
||||
});
|
||||
```
|
||||
|
||||
- Fire an event N times and then remove it, an extension of the `once` concept.
|
||||
|
||||
```javascript
|
||||
server.many('foo', 4, function() {
|
||||
console.log('hello');
|
||||
});
|
||||
```
|
||||
|
||||
- Pass in a namespaced event as an array rather than a delimited string.
|
||||
|
||||
```javascript
|
||||
server.many(['foo', 'bar', 'bazz'], function() {
|
||||
console.log('hello');
|
||||
});
|
||||
```
|
||||
|
||||
|
||||
# API
|
||||
|
||||
When an `EventEmitter` instance experiences an error, the typical action is
|
||||
to emit an `error` event. Error events are treated as a special case.
|
||||
If there is no listener for it, then the default action is to print a stack
|
||||
trace and exit the program.
|
||||
|
||||
All EventEmitters emit the event `newListener` when new listeners are
|
||||
added.
|
||||
|
||||
|
||||
**Namespaces** with **Wildcards**
|
||||
To use namespaces/wildcards, pass the `wildcard` option into the EventEmitter
|
||||
constructor. When namespaces/wildcards are enabled, events can either be
|
||||
strings (`foo.bar`) separated by a delimiter or arrays (`['foo', 'bar']`). The
|
||||
delimiter is also configurable as a constructor option.
|
||||
|
||||
An event name passed to any event emitter method can contain a wild card (the
|
||||
`*` character). If the event name is a string, a wildcard may appear as `foo.*`.
|
||||
If the event name is an array, the wildcard may appear as `['foo', '*']`.
|
||||
|
||||
If either of the above described events were passed to the `on` method,
|
||||
subsequent emits such as the following would be observed...
|
||||
|
||||
```javascript
|
||||
emitter.emit('foo.bazz');
|
||||
emitter.emit(['foo', 'bar']);
|
||||
```
|
||||
|
||||
|
||||
### emitter.addListener(event, listener)
|
||||
### emitter.on(event, listener)
|
||||
|
||||
Adds a listener to the end of the listeners array for the specified event.
|
||||
|
||||
```javascript
|
||||
server.on('data', function(value1, value2, value3, ...) {
|
||||
console.log('The event was raised!');
|
||||
});
|
||||
```
|
||||
|
||||
```javascript
|
||||
server.on('data', function(value) {
|
||||
console.log('The event was raised!');
|
||||
});
|
||||
```
|
||||
|
||||
### emitter.onAny(listener)
|
||||
|
||||
Adds a listener that will be fired when any event is emitted.
|
||||
|
||||
```javascript
|
||||
server.onAny(function(value) {
|
||||
console.log('All events trigger this.');
|
||||
});
|
||||
```
|
||||
|
||||
### emitter.offAny(listener)
|
||||
|
||||
Removes the listener that will be fired when any event is emitted.
|
||||
|
||||
```javascript
|
||||
server.offAny(function(value) {
|
||||
console.log('The event was raised!');
|
||||
});
|
||||
```
|
||||
|
||||
#### emitter.once(event, listener)
|
||||
|
||||
Adds a **one time** listener for the event. The listener is invoked
|
||||
only the first time the event is fired, after which it is removed.
|
||||
|
||||
```javascript
|
||||
server.once('get', function (value) {
|
||||
console.log('Ah, we have our first value!');
|
||||
});
|
||||
```
|
||||
|
||||
### emitter.many(event, timesToListen, listener)
|
||||
|
||||
Adds a listener that will execute **n times** for the event before being
|
||||
removed. The listener is invoked only the first **n times** the event is
|
||||
fired, after which it is removed.
|
||||
|
||||
```javascript
|
||||
server.many('get', 4, function (value) {
|
||||
console.log('This event will be listened to exactly four times.');
|
||||
});
|
||||
```
|
||||
|
||||
|
||||
### emitter.removeListener(event, listener)
|
||||
### emitter.off(event, listener)
|
||||
|
||||
Remove a listener from the listener array for the specified event.
|
||||
**Caution**: changes array indices in the listener array behind the listener.
|
||||
|
||||
```javascript
|
||||
var callback = function(value) {
|
||||
console.log('someone connected!');
|
||||
};
|
||||
server.on('get', callback);
|
||||
// ...
|
||||
server.removeListener('get', callback);
|
||||
```
|
||||
|
||||
|
||||
### emitter.removeAllListeners([event])
|
||||
|
||||
Removes all listeners, or those of the specified event.
|
||||
|
||||
|
||||
### emitter.setMaxListeners(n)
|
||||
|
||||
By default EventEmitters will print a warning if more than 10 listeners
|
||||
are added to it. This is a useful default which helps finding memory leaks.
|
||||
Obviously not all Emitters should be limited to 10. This function allows
|
||||
that to be increased. Set to zero for unlimited.
|
||||
|
||||
|
||||
### emitter.listeners(event)
|
||||
|
||||
Returns an array of listeners for the specified event. This array can be
|
||||
manipulated, e.g. to remove listeners.
|
||||
|
||||
```javascript
|
||||
server.on('get', function(value) {
|
||||
console.log('someone connected!');
|
||||
});
|
||||
console.log(server.listeners('get')); // [ [Function] ]
|
||||
```
|
||||
|
||||
### emitter.listenersAny()
|
||||
|
||||
Returns an array of listeners that are listening for any event that is
|
||||
specified. This array can be manipulated, e.g. to remove listeners.
|
||||
|
||||
```javascript
|
||||
server.onAny(function(value) {
|
||||
console.log('someone connected!');
|
||||
});
|
||||
console.log(server.listenersAny()[0]); // [ [Function] ]
|
||||
```
|
||||
|
||||
### emitter.emit(event, [arg1], [arg2], [...])
|
||||
|
||||
Execute each of the listeners that may be listening for the specified event
|
||||
name in order with the list of arguments.
|
||||
|
||||
# LICENSE
|
||||
|
||||
(The MIT License)
|
||||
|
||||
Copyright (c) 2011 hij1nx <http://www.twitter.com/hij1nx>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the 'Software'), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is furnished
|
||||
to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
1
api.hyungi.net/node_modules/nssocket/node_modules/eventemitter2/index.js
generated
vendored
Normal file
1
api.hyungi.net/node_modules/nssocket/node_modules/eventemitter2/index.js
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
module.exports = require('./lib/eventemitter2');
|
||||
573
api.hyungi.net/node_modules/nssocket/node_modules/eventemitter2/lib/eventemitter2.js
generated
vendored
Normal file
573
api.hyungi.net/node_modules/nssocket/node_modules/eventemitter2/lib/eventemitter2.js
generated
vendored
Normal file
@@ -0,0 +1,573 @@
|
||||
/*!
|
||||
* EventEmitter2
|
||||
* https://github.com/hij1nx/EventEmitter2
|
||||
*
|
||||
* Copyright (c) 2013 hij1nx
|
||||
* Licensed under the MIT license.
|
||||
*/
|
||||
;!function(undefined) {
|
||||
|
||||
var isArray = Array.isArray ? Array.isArray : function _isArray(obj) {
|
||||
return Object.prototype.toString.call(obj) === "[object Array]";
|
||||
};
|
||||
var defaultMaxListeners = 10;
|
||||
|
||||
function init() {
|
||||
this._events = {};
|
||||
if (this._conf) {
|
||||
configure.call(this, this._conf);
|
||||
}
|
||||
}
|
||||
|
||||
function configure(conf) {
|
||||
if (conf) {
|
||||
|
||||
this._conf = conf;
|
||||
|
||||
conf.delimiter && (this.delimiter = conf.delimiter);
|
||||
conf.maxListeners && (this._events.maxListeners = conf.maxListeners);
|
||||
conf.wildcard && (this.wildcard = conf.wildcard);
|
||||
conf.newListener && (this.newListener = conf.newListener);
|
||||
|
||||
if (this.wildcard) {
|
||||
this.listenerTree = {};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function EventEmitter(conf) {
|
||||
this._events = {};
|
||||
this.newListener = false;
|
||||
configure.call(this, conf);
|
||||
}
|
||||
|
||||
//
|
||||
// Attention, function return type now is array, always !
|
||||
// It has zero elements if no any matches found and one or more
|
||||
// elements (leafs) if there are matches
|
||||
//
|
||||
function searchListenerTree(handlers, type, tree, i) {
|
||||
if (!tree) {
|
||||
return [];
|
||||
}
|
||||
var listeners=[], leaf, len, branch, xTree, xxTree, isolatedBranch, endReached,
|
||||
typeLength = type.length, currentType = type[i], nextType = type[i+1];
|
||||
if (i === typeLength && tree._listeners) {
|
||||
//
|
||||
// If at the end of the event(s) list and the tree has listeners
|
||||
// invoke those listeners.
|
||||
//
|
||||
if (typeof tree._listeners === 'function') {
|
||||
handlers && handlers.push(tree._listeners);
|
||||
return [tree];
|
||||
} else {
|
||||
for (leaf = 0, len = tree._listeners.length; leaf < len; leaf++) {
|
||||
handlers && handlers.push(tree._listeners[leaf]);
|
||||
}
|
||||
return [tree];
|
||||
}
|
||||
}
|
||||
|
||||
if ((currentType === '*' || currentType === '**') || tree[currentType]) {
|
||||
//
|
||||
// If the event emitted is '*' at this part
|
||||
// or there is a concrete match at this patch
|
||||
//
|
||||
if (currentType === '*') {
|
||||
for (branch in tree) {
|
||||
if (branch !== '_listeners' && tree.hasOwnProperty(branch)) {
|
||||
listeners = listeners.concat(searchListenerTree(handlers, type, tree[branch], i+1));
|
||||
}
|
||||
}
|
||||
return listeners;
|
||||
} else if(currentType === '**') {
|
||||
endReached = (i+1 === typeLength || (i+2 === typeLength && nextType === '*'));
|
||||
if(endReached && tree._listeners) {
|
||||
// The next element has a _listeners, add it to the handlers.
|
||||
listeners = listeners.concat(searchListenerTree(handlers, type, tree, typeLength));
|
||||
}
|
||||
|
||||
for (branch in tree) {
|
||||
if (branch !== '_listeners' && tree.hasOwnProperty(branch)) {
|
||||
if(branch === '*' || branch === '**') {
|
||||
if(tree[branch]._listeners && !endReached) {
|
||||
listeners = listeners.concat(searchListenerTree(handlers, type, tree[branch], typeLength));
|
||||
}
|
||||
listeners = listeners.concat(searchListenerTree(handlers, type, tree[branch], i));
|
||||
} else if(branch === nextType) {
|
||||
listeners = listeners.concat(searchListenerTree(handlers, type, tree[branch], i+2));
|
||||
} else {
|
||||
// No match on this one, shift into the tree but not in the type array.
|
||||
listeners = listeners.concat(searchListenerTree(handlers, type, tree[branch], i));
|
||||
}
|
||||
}
|
||||
}
|
||||
return listeners;
|
||||
}
|
||||
|
||||
listeners = listeners.concat(searchListenerTree(handlers, type, tree[currentType], i+1));
|
||||
}
|
||||
|
||||
xTree = tree['*'];
|
||||
if (xTree) {
|
||||
//
|
||||
// If the listener tree will allow any match for this part,
|
||||
// then recursively explore all branches of the tree
|
||||
//
|
||||
searchListenerTree(handlers, type, xTree, i+1);
|
||||
}
|
||||
|
||||
xxTree = tree['**'];
|
||||
if(xxTree) {
|
||||
if(i < typeLength) {
|
||||
if(xxTree._listeners) {
|
||||
// If we have a listener on a '**', it will catch all, so add its handler.
|
||||
searchListenerTree(handlers, type, xxTree, typeLength);
|
||||
}
|
||||
|
||||
// Build arrays of matching next branches and others.
|
||||
for(branch in xxTree) {
|
||||
if(branch !== '_listeners' && xxTree.hasOwnProperty(branch)) {
|
||||
if(branch === nextType) {
|
||||
// We know the next element will match, so jump twice.
|
||||
searchListenerTree(handlers, type, xxTree[branch], i+2);
|
||||
} else if(branch === currentType) {
|
||||
// Current node matches, move into the tree.
|
||||
searchListenerTree(handlers, type, xxTree[branch], i+1);
|
||||
} else {
|
||||
isolatedBranch = {};
|
||||
isolatedBranch[branch] = xxTree[branch];
|
||||
searchListenerTree(handlers, type, { '**': isolatedBranch }, i+1);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if(xxTree._listeners) {
|
||||
// We have reached the end and still on a '**'
|
||||
searchListenerTree(handlers, type, xxTree, typeLength);
|
||||
} else if(xxTree['*'] && xxTree['*']._listeners) {
|
||||
searchListenerTree(handlers, type, xxTree['*'], typeLength);
|
||||
}
|
||||
}
|
||||
|
||||
return listeners;
|
||||
}
|
||||
|
||||
function growListenerTree(type, listener) {
|
||||
|
||||
type = typeof type === 'string' ? type.split(this.delimiter) : type.slice();
|
||||
|
||||
//
|
||||
// Looks for two consecutive '**', if so, don't add the event at all.
|
||||
//
|
||||
for(var i = 0, len = type.length; i+1 < len; i++) {
|
||||
if(type[i] === '**' && type[i+1] === '**') {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
var tree = this.listenerTree;
|
||||
var name = type.shift();
|
||||
|
||||
while (name) {
|
||||
|
||||
if (!tree[name]) {
|
||||
tree[name] = {};
|
||||
}
|
||||
|
||||
tree = tree[name];
|
||||
|
||||
if (type.length === 0) {
|
||||
|
||||
if (!tree._listeners) {
|
||||
tree._listeners = listener;
|
||||
}
|
||||
else if(typeof tree._listeners === 'function') {
|
||||
tree._listeners = [tree._listeners, listener];
|
||||
}
|
||||
else if (isArray(tree._listeners)) {
|
||||
|
||||
tree._listeners.push(listener);
|
||||
|
||||
if (!tree._listeners.warned) {
|
||||
|
||||
var m = defaultMaxListeners;
|
||||
|
||||
if (typeof this._events.maxListeners !== 'undefined') {
|
||||
m = this._events.maxListeners;
|
||||
}
|
||||
|
||||
if (m > 0 && tree._listeners.length > m) {
|
||||
|
||||
tree._listeners.warned = true;
|
||||
console.error('(node) warning: possible EventEmitter memory ' +
|
||||
'leak detected. %d listeners added. ' +
|
||||
'Use emitter.setMaxListeners() to increase limit.',
|
||||
tree._listeners.length);
|
||||
console.trace();
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
name = type.shift();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// By default EventEmitters will print a warning if more than
|
||||
// 10 listeners are added to it. This is a useful default which
|
||||
// helps finding memory leaks.
|
||||
//
|
||||
// Obviously not all Emitters should be limited to 10. This function allows
|
||||
// that to be increased. Set to zero for unlimited.
|
||||
|
||||
EventEmitter.prototype.delimiter = '.';
|
||||
|
||||
EventEmitter.prototype.setMaxListeners = function(n) {
|
||||
this._events || init.call(this);
|
||||
this._events.maxListeners = n;
|
||||
if (!this._conf) this._conf = {};
|
||||
this._conf.maxListeners = n;
|
||||
};
|
||||
|
||||
EventEmitter.prototype.event = '';
|
||||
|
||||
EventEmitter.prototype.once = function(event, fn) {
|
||||
this.many(event, 1, fn);
|
||||
return this;
|
||||
};
|
||||
|
||||
EventEmitter.prototype.many = function(event, ttl, fn) {
|
||||
var self = this;
|
||||
|
||||
if (typeof fn !== 'function') {
|
||||
throw new Error('many only accepts instances of Function');
|
||||
}
|
||||
|
||||
function listener() {
|
||||
if (--ttl === 0) {
|
||||
self.off(event, listener);
|
||||
}
|
||||
fn.apply(this, arguments);
|
||||
}
|
||||
|
||||
listener._origin = fn;
|
||||
|
||||
this.on(event, listener);
|
||||
|
||||
return self;
|
||||
};
|
||||
|
||||
EventEmitter.prototype.emit = function() {
|
||||
|
||||
this._events || init.call(this);
|
||||
|
||||
var type = arguments[0];
|
||||
|
||||
if (type === 'newListener' && !this.newListener) {
|
||||
if (!this._events.newListener) { return false; }
|
||||
}
|
||||
|
||||
// Loop through the *_all* functions and invoke them.
|
||||
if (this._all) {
|
||||
var l = arguments.length;
|
||||
var args = new Array(l - 1);
|
||||
for (var i = 1; i < l; i++) args[i - 1] = arguments[i];
|
||||
for (i = 0, l = this._all.length; i < l; i++) {
|
||||
this.event = type;
|
||||
this._all[i].apply(this, args);
|
||||
}
|
||||
}
|
||||
|
||||
// If there is no 'error' event listener then throw.
|
||||
if (type === 'error') {
|
||||
|
||||
if (!this._all &&
|
||||
!this._events.error &&
|
||||
!(this.wildcard && this.listenerTree.error)) {
|
||||
|
||||
if (arguments[1] instanceof Error) {
|
||||
throw arguments[1]; // Unhandled 'error' event
|
||||
} else {
|
||||
throw new Error("Uncaught, unspecified 'error' event.");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
var handler;
|
||||
|
||||
if(this.wildcard) {
|
||||
handler = [];
|
||||
var ns = typeof type === 'string' ? type.split(this.delimiter) : type.slice();
|
||||
searchListenerTree.call(this, handler, ns, this.listenerTree, 0);
|
||||
}
|
||||
else {
|
||||
handler = this._events[type];
|
||||
}
|
||||
|
||||
if (typeof handler === 'function') {
|
||||
this.event = type;
|
||||
if (arguments.length === 1) {
|
||||
handler.call(this);
|
||||
}
|
||||
else if (arguments.length > 1)
|
||||
switch (arguments.length) {
|
||||
case 2:
|
||||
handler.call(this, arguments[1]);
|
||||
break;
|
||||
case 3:
|
||||
handler.call(this, arguments[1], arguments[2]);
|
||||
break;
|
||||
// slower
|
||||
default:
|
||||
var l = arguments.length;
|
||||
var args = new Array(l - 1);
|
||||
for (var i = 1; i < l; i++) args[i - 1] = arguments[i];
|
||||
handler.apply(this, args);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else if (handler) {
|
||||
var l = arguments.length;
|
||||
var args = new Array(l - 1);
|
||||
for (var i = 1; i < l; i++) args[i - 1] = arguments[i];
|
||||
|
||||
var listeners = handler.slice();
|
||||
for (var i = 0, l = listeners.length; i < l; i++) {
|
||||
this.event = type;
|
||||
listeners[i].apply(this, args);
|
||||
}
|
||||
return (listeners.length > 0) || !!this._all;
|
||||
}
|
||||
else {
|
||||
return !!this._all;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
EventEmitter.prototype.on = function(type, listener) {
|
||||
|
||||
if (typeof type === 'function') {
|
||||
this.onAny(type);
|
||||
return this;
|
||||
}
|
||||
|
||||
if (typeof listener !== 'function') {
|
||||
throw new Error('on only accepts instances of Function');
|
||||
}
|
||||
this._events || init.call(this);
|
||||
|
||||
// To avoid recursion in the case that type == "newListeners"! Before
|
||||
// adding it to the listeners, first emit "newListeners".
|
||||
this.emit('newListener', type, listener);
|
||||
|
||||
if(this.wildcard) {
|
||||
growListenerTree.call(this, type, listener);
|
||||
return this;
|
||||
}
|
||||
|
||||
if (!this._events[type]) {
|
||||
// Optimize the case of one listener. Don't need the extra array object.
|
||||
this._events[type] = listener;
|
||||
}
|
||||
else if(typeof this._events[type] === 'function') {
|
||||
// Adding the second element, need to change to array.
|
||||
this._events[type] = [this._events[type], listener];
|
||||
}
|
||||
else if (isArray(this._events[type])) {
|
||||
// If we've already got an array, just append.
|
||||
this._events[type].push(listener);
|
||||
|
||||
// Check for listener leak
|
||||
if (!this._events[type].warned) {
|
||||
|
||||
var m = defaultMaxListeners;
|
||||
|
||||
if (typeof this._events.maxListeners !== 'undefined') {
|
||||
m = this._events.maxListeners;
|
||||
}
|
||||
|
||||
if (m > 0 && this._events[type].length > m) {
|
||||
|
||||
this._events[type].warned = true;
|
||||
console.error('(node) warning: possible EventEmitter memory ' +
|
||||
'leak detected. %d listeners added. ' +
|
||||
'Use emitter.setMaxListeners() to increase limit.',
|
||||
this._events[type].length);
|
||||
console.trace();
|
||||
}
|
||||
}
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
EventEmitter.prototype.onAny = function(fn) {
|
||||
|
||||
if (typeof fn !== 'function') {
|
||||
throw new Error('onAny only accepts instances of Function');
|
||||
}
|
||||
|
||||
if(!this._all) {
|
||||
this._all = [];
|
||||
}
|
||||
|
||||
// Add the function to the event listener collection.
|
||||
this._all.push(fn);
|
||||
return this;
|
||||
};
|
||||
|
||||
EventEmitter.prototype.addListener = EventEmitter.prototype.on;
|
||||
|
||||
EventEmitter.prototype.off = function(type, listener) {
|
||||
if (typeof listener !== 'function') {
|
||||
throw new Error('removeListener only takes instances of Function');
|
||||
}
|
||||
|
||||
var handlers,leafs=[];
|
||||
|
||||
if(this.wildcard) {
|
||||
var ns = typeof type === 'string' ? type.split(this.delimiter) : type.slice();
|
||||
leafs = searchListenerTree.call(this, null, ns, this.listenerTree, 0);
|
||||
}
|
||||
else {
|
||||
// does not use listeners(), so no side effect of creating _events[type]
|
||||
if (!this._events[type]) return this;
|
||||
handlers = this._events[type];
|
||||
leafs.push({_listeners:handlers});
|
||||
}
|
||||
|
||||
for (var iLeaf=0; iLeaf<leafs.length; iLeaf++) {
|
||||
var leaf = leafs[iLeaf];
|
||||
handlers = leaf._listeners;
|
||||
if (isArray(handlers)) {
|
||||
|
||||
var position = -1;
|
||||
|
||||
for (var i = 0, length = handlers.length; i < length; i++) {
|
||||
if (handlers[i] === listener ||
|
||||
(handlers[i].listener && handlers[i].listener === listener) ||
|
||||
(handlers[i]._origin && handlers[i]._origin === listener)) {
|
||||
position = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (position < 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if(this.wildcard) {
|
||||
leaf._listeners.splice(position, 1);
|
||||
}
|
||||
else {
|
||||
this._events[type].splice(position, 1);
|
||||
}
|
||||
|
||||
if (handlers.length === 0) {
|
||||
if(this.wildcard) {
|
||||
delete leaf._listeners;
|
||||
}
|
||||
else {
|
||||
delete this._events[type];
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
else if (handlers === listener ||
|
||||
(handlers.listener && handlers.listener === listener) ||
|
||||
(handlers._origin && handlers._origin === listener)) {
|
||||
if(this.wildcard) {
|
||||
delete leaf._listeners;
|
||||
}
|
||||
else {
|
||||
delete this._events[type];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return this;
|
||||
};
|
||||
|
||||
EventEmitter.prototype.offAny = function(fn) {
|
||||
var i = 0, l = 0, fns;
|
||||
if (fn && this._all && this._all.length > 0) {
|
||||
fns = this._all;
|
||||
for(i = 0, l = fns.length; i < l; i++) {
|
||||
if(fn === fns[i]) {
|
||||
fns.splice(i, 1);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
this._all = [];
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
EventEmitter.prototype.removeListener = EventEmitter.prototype.off;
|
||||
|
||||
EventEmitter.prototype.removeAllListeners = function(type) {
|
||||
if (arguments.length === 0) {
|
||||
!this._events || init.call(this);
|
||||
return this;
|
||||
}
|
||||
|
||||
if(this.wildcard) {
|
||||
var ns = typeof type === 'string' ? type.split(this.delimiter) : type.slice();
|
||||
var leafs = searchListenerTree.call(this, null, ns, this.listenerTree, 0);
|
||||
|
||||
for (var iLeaf=0; iLeaf<leafs.length; iLeaf++) {
|
||||
var leaf = leafs[iLeaf];
|
||||
leaf._listeners = null;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!this._events[type]) return this;
|
||||
this._events[type] = null;
|
||||
}
|
||||
return this;
|
||||
};
|
||||
|
||||
EventEmitter.prototype.listeners = function(type) {
|
||||
if(this.wildcard) {
|
||||
var handlers = [];
|
||||
var ns = typeof type === 'string' ? type.split(this.delimiter) : type.slice();
|
||||
searchListenerTree.call(this, handlers, ns, this.listenerTree, 0);
|
||||
return handlers;
|
||||
}
|
||||
|
||||
this._events || init.call(this);
|
||||
|
||||
if (!this._events[type]) this._events[type] = [];
|
||||
if (!isArray(this._events[type])) {
|
||||
this._events[type] = [this._events[type]];
|
||||
}
|
||||
return this._events[type];
|
||||
};
|
||||
|
||||
EventEmitter.prototype.listenersAny = function() {
|
||||
|
||||
if(this._all) {
|
||||
return this._all;
|
||||
}
|
||||
else {
|
||||
return [];
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
if (typeof define === 'function' && define.amd) {
|
||||
// AMD. Register as an anonymous module.
|
||||
define(function() {
|
||||
return EventEmitter;
|
||||
});
|
||||
} else if (typeof exports === 'object') {
|
||||
// CommonJS
|
||||
exports.EventEmitter2 = EventEmitter;
|
||||
}
|
||||
else {
|
||||
// Browser global.
|
||||
window.EventEmitter2 = EventEmitter;
|
||||
}
|
||||
}();
|
||||
29
api.hyungi.net/node_modules/nssocket/node_modules/eventemitter2/package.json
generated
vendored
Normal file
29
api.hyungi.net/node_modules/nssocket/node_modules/eventemitter2/package.json
generated
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "eventemitter2",
|
||||
"version": "0.4.14",
|
||||
"description": "A Node.js event emitter implementation with namespaces, wildcards, TTL and browser support.",
|
||||
"keywords": ["event", "events", "emitter", "eventemitter"],
|
||||
"author": "hij1nx <paolo@async.ly> http://twitter.com/hij1nx",
|
||||
"contributors": [
|
||||
"Eric Elliott",
|
||||
"Charlie Robbins <charlie@nodejitsu.com> http://twitter.com/indexzero",
|
||||
"Jameson Lee <jameson@nodejitsu.com> http://twitter.com/Jamesonjlee",
|
||||
"Jeroen van Duffelen <jvduf@nodejitsu.com> http://www.twitter.com/jvduf",
|
||||
"Fedor Indutny <fedor.indutny@gmail.com> http://www.twitter.com/indutny"
|
||||
],
|
||||
"license": "MIT",
|
||||
"repository": "git://github.com/hij1nx/EventEmitter2.git",
|
||||
"devDependencies": {
|
||||
"nodeunit": "*",
|
||||
"benchmark" : ">= 0.2.2"
|
||||
},
|
||||
"main": "./lib/eventemitter2.js",
|
||||
"scripts" : {
|
||||
"test" : "nodeunit test/simple/ && nodeunit test/wildcardEvents/",
|
||||
"benchmark" : "node test/perf/benchmark.js"
|
||||
},
|
||||
"files": [
|
||||
"lib/eventemitter2.js",
|
||||
"index.js"
|
||||
]
|
||||
}
|
||||
29
api.hyungi.net/node_modules/nssocket/package.json
generated
vendored
Normal file
29
api.hyungi.net/node_modules/nssocket/package.json
generated
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"name": "nssocket",
|
||||
"description": "An elegant way to define lightweight protocols on-top of TCP/TLS sockets in node.js",
|
||||
"version": "0.6.0",
|
||||
"author": "Charlie Robbins <charlie.robbins@gmail.com>",
|
||||
"maintainers": [
|
||||
"genediazjr <genediazjr@gmail.com>"
|
||||
],
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "http://github.com/foreverjs/nssocket.git"
|
||||
},
|
||||
"dependencies": {
|
||||
"eventemitter2": "~0.4.14",
|
||||
"lazy": "~1.0.11"
|
||||
},
|
||||
"devDependencies": {
|
||||
"msgpack": "^1.0.2",
|
||||
"vows": "0.8.0"
|
||||
},
|
||||
"main": "./lib/nssocket",
|
||||
"engines": {
|
||||
"node": ">= 0.10.x"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "vows test/*-test.js --spec"
|
||||
},
|
||||
"license": "MIT"
|
||||
}
|
||||
72
api.hyungi.net/node_modules/nssocket/test/create-server-test.js
generated
vendored
Normal file
72
api.hyungi.net/node_modules/nssocket/test/create-server-test.js
generated
vendored
Normal file
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* create-server-test.js : namespace socket unit test for TLS.
|
||||
*
|
||||
* (C) 2011, Charlie Robbins, Paolo Fragomeni, & the Contributors.
|
||||
*
|
||||
*/
|
||||
|
||||
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);
|
||||
|
||||
16
api.hyungi.net/node_modules/nssocket/test/fixtures/ryans-cert.pem
generated
vendored
Normal file
16
api.hyungi.net/node_modules/nssocket/test/fixtures/ryans-cert.pem
generated
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
-----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-----
|
||||
12
api.hyungi.net/node_modules/nssocket/test/fixtures/ryans-csr.pem
generated
vendored
Normal file
12
api.hyungi.net/node_modules/nssocket/test/fixtures/ryans-csr.pem
generated
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
-----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-----
|
||||
15
api.hyungi.net/node_modules/nssocket/test/fixtures/ryans-key.pem
generated
vendored
Normal file
15
api.hyungi.net/node_modules/nssocket/test/fixtures/ryans-key.pem
generated
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
-----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-----
|
||||
106
api.hyungi.net/node_modules/nssocket/test/msgpack-tcp-test.js
generated
vendored
Normal file
106
api.hyungi.net/node_modules/nssocket/test/msgpack-tcp-test.js
generated
vendored
Normal file
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* msgpack-tcp-test.js : namespace socket unit test for Messagepack encoder/decoder.
|
||||
*
|
||||
* (C) 2011, Charlie Robbins, Paolo Fragomeni, & the Contributors.
|
||||
*
|
||||
*/
|
||||
|
||||
var assert = require('assert'),
|
||||
fs = require('fs'),
|
||||
msgpack = require('msgpack'),
|
||||
net = require('net'),
|
||||
path = require('path'),
|
||||
vows = require('vows'),
|
||||
NsSocket = require('../lib/nssocket').NsSocket;
|
||||
|
||||
var TCP_PORT = 30109;
|
||||
|
||||
var tcpServer = net.createServer(),
|
||||
tcpOpt;
|
||||
|
||||
tcpOpt = {
|
||||
type : 'tcp4',
|
||||
delimiter: '.}',
|
||||
encode: function (data) {
|
||||
return msgpack.pack(data).toString('binary');
|
||||
},
|
||||
decode: function (data) {
|
||||
return msgpack.unpack(new Buffer(data.toString('binary'), 'binary'));
|
||||
}
|
||||
};
|
||||
|
||||
tcpServer.listen(TCP_PORT);
|
||||
|
||||
vows.describe('nssocket/msgpack').addBatch({
|
||||
"When using NsSocket with msgpack encoder/decoder": {
|
||||
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(tcpOpt.encode(['here', 'is', 'something.']) + '\n');
|
||||
},
|
||||
"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'], { some: "json", data: 123 });
|
||||
},
|
||||
"we should see it on the other end": function (_, outbound, wraped, data) {
|
||||
assert.isObject(data);
|
||||
arr = tcpOpt.decode(data.toString());
|
||||
assert.lengthOf(arr, 3);
|
||||
assert.equal(arr[0], 'hello');
|
||||
assert.equal(arr[1], 'world');
|
||||
assert.deepEqual(arr[2], { some: "json", data: 123 });
|
||||
},
|
||||
"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);
|
||||
80
api.hyungi.net/node_modules/nssocket/test/tcp-reconnect-test.js
generated
vendored
Normal file
80
api.hyungi.net/node_modules/nssocket/test/tcp-reconnect-test.js
generated
vendored
Normal file
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* nssocket-test.js : namespace socket unit test for TCP
|
||||
*
|
||||
* (C) 2011, Charlie Robbins, Paolo Fragomeni, & the Contributors.
|
||||
*
|
||||
*/
|
||||
|
||||
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(JSON.stringify(['here', 'is', 'something.']) + '\n');
|
||||
},
|
||||
"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);
|
||||
99
api.hyungi.net/node_modules/nssocket/test/tcp-test.js
generated
vendored
Normal file
99
api.hyungi.net/node_modules/nssocket/test/tcp-test.js
generated
vendored
Normal file
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* nssocket-test.js : namespace socket unit test for TCP
|
||||
*
|
||||
* (C) 2011, Charlie Robbins, Paolo Fragomeni, & the Contributors.
|
||||
*
|
||||
*/
|
||||
|
||||
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(JSON.stringify(['here', 'is', 'something.']) + '\n');
|
||||
},
|
||||
"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'], { some: "json", data: 123 });
|
||||
},
|
||||
"we should see it on the other end": function (_, outbound, wraped, data) {
|
||||
assert.isObject(data);
|
||||
arr = JSON.parse(data.toString());
|
||||
assert.lengthOf(arr, 3);
|
||||
assert.equal(arr[0], 'hello');
|
||||
assert.equal(arr[1], 'world');
|
||||
assert.deepEqual(arr[2], { some: "json", data: 123 });
|
||||
},
|
||||
"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);
|
||||
106
api.hyungi.net/node_modules/nssocket/test/tls-test.js
generated
vendored
Normal file
106
api.hyungi.net/node_modules/nssocket/test/tls-test.js
generated
vendored
Normal file
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* nssocket-test.js : namespace socket unit test for TLS.
|
||||
*
|
||||
* (C) 2011, Charlie Robbins, Paolo Fragomeni, & the Contributors.
|
||||
*
|
||||
*/
|
||||
|
||||
var assert = require('assert'),
|
||||
fs = require('fs'),
|
||||
net = require('net'),
|
||||
path = require('path'),
|
||||
tls = require('tls'),
|
||||
vows = require('vows'),
|
||||
NsSocket = require('../lib/nssocket').NsSocket;
|
||||
|
||||
var TLS_PORT = 50305,
|
||||
fixturesDir = path.join(__dirname, 'fixtures');
|
||||
|
||||
var serverOpts = {
|
||||
key: fs.readFileSync(path.join(fixturesDir, 'ryans-key.pem')),
|
||||
cert: fs.readFileSync(path.join(fixturesDir, 'ryans-cert.pem')),
|
||||
ca: fs.readFileSync(path.join(fixturesDir, 'ryans-csr.pem'))
|
||||
};
|
||||
|
||||
var tlsServer = tls.createServer(serverOpts),
|
||||
tlsOpt;
|
||||
|
||||
tlsOpt = {
|
||||
type: 'tls',
|
||||
delimiter: '::'
|
||||
};
|
||||
|
||||
tlsServer.listen(TLS_PORT);
|
||||
|
||||
vows.describe('nssocket/tls').addBatch({
|
||||
"When using NsSocket with TLS": {
|
||||
topic: new NsSocket(tlsOpt),
|
||||
"should create a wrapped socket": function (outbound) {
|
||||
assert.instanceOf(outbound, NsSocket);
|
||||
},
|
||||
"should have the proper configuration settings": function (outbound) {
|
||||
assert.equal(outbound._type, tlsOpt.type);
|
||||
assert.equal(outbound._delimiter, tlsOpt.delimiter);
|
||||
},
|
||||
"the connect() method": {
|
||||
topic: function (outbound) {
|
||||
var that = this;
|
||||
tlsServer.on('secureConnection', this.callback.bind(null, null, outbound));
|
||||
outbound.connect(TLS_PORT);
|
||||
},
|
||||
"should actually connect": function (_, outbound, inbound) {
|
||||
assert.instanceOf(outbound, NsSocket);
|
||||
if (!inbound.authorized) {
|
||||
console.log('Certificate is not authorized: ' + inbound.authorizationError);
|
||||
}
|
||||
},
|
||||
"the on() method": {
|
||||
topic: function (outbound, inbound) {
|
||||
outbound.on(['data', 'here', 'is'], this.callback.bind(outbound, null));
|
||||
inbound.write(JSON.stringify(['here', 'is', 'something']) + '\n');
|
||||
},
|
||||
"should handle namespaced events": function (_, data) {
|
||||
assert.isString(data);
|
||||
assert.isArray(this.event);
|
||||
assert.lengthOf(this.event, 3);
|
||||
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'], { some: "json", data: 123 });
|
||||
},
|
||||
"we should see it on the other end": function (_, outbound, inbound, data) {
|
||||
assert.isObject(data);
|
||||
arr = JSON.parse(data.toString());
|
||||
assert.lengthOf(arr, 3);
|
||||
assert.equal(arr[0], 'hello');
|
||||
assert.equal(arr[1], 'world');
|
||||
assert.deepEqual(arr[2], { some: "json", data: 123 });
|
||||
},
|
||||
"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);
|
||||
Reference in New Issue
Block a user