How to use nssocket - 10 common examples

To help you get started, we’ve selected a few nssocket examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github snupa / node-queuefy / lib / worker.js View on Github external
worker.prototype.connect = function Connect(callback) {
	var self = this;
	this.connection = new nssocket.NsSocket();
	this.connection.on('error', function(e) {
		self._connected = false;
		self.connection.destroy();
		// Any callbacks expecting result will be called with error
		for(var callback_id in self.CALLBACKS) {
			self.CALLBACKS[callback_id].call(self, e, null);
			delete self.CALLBACKS[callback_id];
		}
		self.emit('disconnect');
		setTimeout(function() {
			self.connect();
		}, 100);
	});
	this.connection.connect(this.config.port, this.config.host, function() {
		self._connected = true;
		Bind.call(self);
github foreversd / forever / lib / forever / worker.js View on Github external
function findAndStart() {
    self._socket = nssocket.createServer(workerProtocol);
    self._socket.on('listening', function () {
      //
      // `listening` listener doesn't take error as the first parameter
      //
      self.emit('start');
      if (callback) {
        callback(null, self._sockFile);
      }
    });

    self._socket.on('error', function (err) {
      if (err.code === 'EADDRINUSE') {
        return findAndStart();
      }
      else if (callback) {
        callback(err);
github foreversd / nssocket / lib-cov / nssocket.js View on Github external
self.retry && self.reconnect();
}));
  _$jscoverage['nssocket.js'][284]++;
  self.socket.on("error", (function (err) {
  _$jscoverage['nssocket.js'][285]++;
  ! self.retry && self.emit("error", err || new Error("An Unknown Error occured"));
}));
  _$jscoverage['nssocket.js'][288]++;
  self.socket.on("timeout", (function onIdle() {
  _$jscoverage['nssocket.js'][289]++;
  self.emit("idle");
  _$jscoverage['nssocket.js'][290]++;
  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.createClient = 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) {","    // inbounds sockets can not reconnect by definition","    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,","    wait: options.retryInterval || 5000,","    timeoutId: undefined","  } : 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 && this.socket.readable || false;","    configureEvents(this);","  }","}","","util.inherits(NsSocket, EventEmitter2);","","NsSocket.prototype.connect = function connect(port) {","  this.retry.timeoutId && clearTimeout(this.retry.timeoutId);","","  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 && 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) {","  // 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'));","  }","  this.stream.write(buff);","  return this;","};","","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);","  return this;","};","","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;","","  this.retry.timeoutId = setTimeout(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));","    }","","    self.retry.waiting = true;","","    // 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);","  }, 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 >= 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 >= 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.destroy();","    self.socket = undefined;","    self.connected = false;","","    if (hadError) {","      self.emit('close', hadError, arguments[1]);","    } else {","      self.emit('close');","    }","","    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);","  });","}"];
github cardstack / cardstack / packages / hub / docker-host / hub-connection.js View on Github external
async function _connect() {
  let socket = new nssocket.NsSocket();
  socket.connect(6785);

  return new Promise(function(resolve, reject) {
    log.trace("Attempting to connect to the hub's heartbeat port");

    async function onClose(err) {
      // When a container is starting up and hasn't yet internally
      // started listening on a port, Docker trolls us by accepting the
      // connection, but then immediately closing the connection.
      // So, if it was closed with no error, just try again.
      if (!err) {
        await timeout(50);
        resolve(_connect());
      }
    }
github foreversd / forever / lib / forever.js View on Github external
function sendAction(proc, next) {
    var socket = new nssocket.NsSocket();

    function onMessage(data) {
      //
      // Cleanup the socket.
      //
      socket.undata([action, 'ok'],    onMessage);
      socket.undata([action, 'error'], onMessage);
      socket.end();

      //
      // Messages are only sent back from error cases. The event
      // calling context is available from `nssocket`.
      //
      var message = data && data.message,
          type    = this.event.slice().pop();
github marcelklehr / smokesignal / lib / node.js View on Github external
Node.prototype.start = function() {
  if(this.listening) return
  
  // Set up the server
  this.server = nssocket.createServer(function (socket) {
    this.logger.debug('Incoming Connection from '+socket.socket.remoteAddress+':'+socket.socket.remotePort)
    
    socket.data(['smoke', 'handshake'], this.onHandshake.bind(this, socket))
    socket.data(['smoke', 'peer request'], this.onUnconnectedPeerRequest.bind(this, socket))
    socket.data(['smoke', 'pong'], this.onPong.bind(this, socket))
    
    socket.on('error', function(er) {
      this.logger.error(er)
      socket.destroy()
    }.bind(this))
    
    socket._smokeTimeout = setTimeout(function() {
      socket.destroy()
    }, 5000)
  }.bind(this))
github cardstack / cardstack / packages / hub / docker-container / ember-connection.js View on Github external
constructor({ orchestrator, heartbeat, ready }) {
    this.orchestrator = orchestrator;

    if (heartbeat) {
      this.stopLater = _.debounce(this._stop.bind(this), HUB_HEARTBEAT_TIMEOUT);
    }

    let that = this;

    this._server = nssocket.createServer(async function(socket) {
      log.info('Connection established from ember-cli');

      // Docker does some weird stuff for containers with published ports
      // before they start actually listening on the port. Long story short,
      // we need a handshake when we first establish a connection.
      socket.data('hand', function() {
        socket.send('shake');
      });

      // Ember-cli may shut us down manually.
      socket.data('shutdown', function() {
        log.info('Received shutdown message from ember-cli');
        orchestrator.stop();
      });

      if (heartbeat) {
github foreversd / nssocket / lib-cov / nssocket.js View on Github external
/* 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'][10] = 0;
  _$jscoverage['nssocket.js'][12] = 0;
  _$jscoverage['nssocket.js'][13] = 0;
  _$jscoverage['nssocket.js'][14] = 0;
  _$jscoverage['nssocket.js'][15] = 0;
  _$jscoverage['nssocket.js'][18] = 0;
  _$jscoverage['nssocket.js'][19] = 0;
  _$jscoverage['nssocket.js'][21] = 0;
  _$jscoverage['nssocket.js'][23] = 0;
  _$jscoverage['nssocket.js'][24] = 0;
  _$jscoverage['nssocket.js'][27] = 0;
  _$jscoverage['nssocket.js'][33] = 0;
  _$jscoverage['nssocket.js'][34] = 0;
github foreversd / nssocket / lib-cov / nssocket.js View on Github external
/* 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'][10] = 0;
  _$jscoverage['nssocket.js'][12] = 0;
  _$jscoverage['nssocket.js'][13] = 0;
  _$jscoverage['nssocket.js'][14] = 0;
  _$jscoverage['nssocket.js'][15] = 0;
  _$jscoverage['nssocket.js'][18] = 0;
  _$jscoverage['nssocket.js'][19] = 0;
  _$jscoverage['nssocket.js'][21] = 0;
  _$jscoverage['nssocket.js'][23] = 0;
  _$jscoverage['nssocket.js'][24] = 0;
  _$jscoverage['nssocket.js'][27] = 0;
  _$jscoverage['nssocket.js'][33] = 0;
  _$jscoverage['nssocket.js'][34] = 0;
  _$jscoverage['nssocket.js'][35] = 0;
github keymetrics / pmx / test / vxx.e2e.mocha.js View on Github external
function listenRev(cb) {
  var listener_server = require('nssocket').createServer(function(_socket) {
  });

  listener_server.listen(4322, '0.0.0.0', function() {
    console.log('Reverse interact online');
    cb();
  });
}

nssocket

An elegant way to define lightweight protocols on-top of TCP/TLS sockets in node.js

MIT
Latest version published 9 years ago

Package Health Score

74 / 100
Full package analysis