New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

sipware

Package Overview
Dependencies
Maintainers
1
Versions
263
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sipware - npm Package Compare versions

Comparing version 0.0.17 to 0.1.0

93

lib/client.js

@@ -7,61 +7,62 @@ var net = require('net');

function Client(consumer, options) {
var buf = Buffer.concat([]);
var id = utils.randomAscii(12);
var trunk = {id, options};
class Client extends EventEmitter {
constructor(consumer, options) {
var buf = Buffer.concat([]);
var id = utils.randomAscii(12);
var trunk = {id, options};
super();
if(options.tls) {
var srv = tls;
} else {
var srv = net;
}
if(options.tls) {
var srv = tls;
} else {
var srv = net;
}
this.client = srv.connect(options, () => {
trunk.socket = this.client;
trunk.end = () => {this.client.destroy()};
consumer.connect(trunk);
})
this.client = srv.connect(options, () => {
trunk.socket = this.client;
trunk.end = () => {this.client.destroy()};
consumer.connect(trunk);
})
this.client.on('data', data => {
this.client.pause();
data = Buffer.concat([buf, data], buf.length + data.length);
this.client.on('data', data => {
this.client.pause();
data = Buffer.concat([buf, data], buf.length + data.length);
consumer.read({data, trunk, options}).then(leftover => {
if(leftover && leftover.length) {
buf = leftover;
} else {
buf = Buffer.concat([]);
}
consumer.read({data, trunk, options}).then(leftover => {
if(leftover && leftover.length) {
buf = leftover;
} else {
buf = Buffer.concat([]);
}
this.client.resume();
this.client.resume();
})
})
})
this.client.on('end', (...args) => {
this.emit('end', ...args)
// this.client.destroy();
})
this.client.on('end', (...args) => {
this.emit('end', ...args)
// this.client.destroy();
})
this.client.on('error', error => {
console.log('client error: '+error);
this.emit('error', error)
this.client.on('error', error => {
console.log('client error: '+error);
this.emit('error', error)
process.nextTick(() => {
process.exit(0);
process.nextTick(() => {
process.exit(0);
})
})
})
}
}
util.inherits(Client, EventEmitter);
Client.prototype.outgoing = function(trans) {
for (let data of trans.tx) {
this.client.write(data);
outgoing(trans) {
for (let data of trans.tx) {
this.client.write(data);
}
}
}
Client.prototype.end = function() {
if(this.client) {
this.client.unref();
this.client.end();
end() {
if(this.client) {
this.client.unref();
this.client.end();
}
}

@@ -68,0 +69,0 @@ }

@@ -6,4 +6,71 @@ var util = require('util');

function Protocol(options) {
this.options = options;
class Protocol {
constructor(options) {
this.options = options;
}
createMessage(headers, data) {
if(Object.keys(headers).length) {
if(data instanceof Buffer) {
var msg = data.toString();
} else if('object' === typeof data) {
var msg = JSON.stringify(data);
} else {
var msg = data;
}
msg = msg || '';
headers['content-length'] = Buffer.byteLength(msg, 'utf8');
return createHeaders(headers) + '\n' + msg;
}
}
async peek(buf, offset=0) {
var sep = getSeparator(buf, offset);
if(sep > 0) {
var headers = await parseHeaders(buf.slice(offset, sep));
if(headers) {
return {headers, sep, offset};
}
}
}
// Return offset of next packet or -1
next(info, buf) {
if(info && buf) {
var {headers, sep, offset} = info;
var len = headers['content-length'];
console.log('next:',sep,len,buf.length);
if(buf.length - sep >= len) {
return len + sep;
}
}
return -1;
}
async parse(buf) {
var data = {messages: [], buf};
var offset, info = await this.peek(buf, 0);
while((offset = this.next(info, buf)) >= 0) {
var content = buf.slice(info.sep, offset);
data.buf = buf.slice(offset);
if(content && content.length) {
data.messages.push({headers: info.headers, content});
} else {
data.messages.push({headers: info.headers});
}
info = await this.peek(buf, offset);
}
if(!data.buf || !data.buf.length) {
delete data.buf;
}
return data;
}
}

@@ -29,18 +96,2 @@

Protocol.prototype.createMessage = function(headers, data) {
if(Object.keys(headers).length) {
if(data instanceof Buffer) {
var msg = data.toString();
} else if('object' === typeof data) {
var msg = JSON.stringify(data);
} else {
var msg = data;
}
msg = msg || '';
headers['content-length'] = Buffer.byteLength(msg, 'utf8');
return createHeaders(headers) + '\n' + msg;
}
}
// CRLF

@@ -107,51 +158,2 @@ function getBufSeparator(buf, offset=0) {

//
Protocol.prototype.peek = async function(buf, offset=0) {
var sep = getSeparator(buf, offset);
if(sep > 0) {
var headers = await parseHeaders(buf.slice(offset, sep));
if(headers) {
return {headers, sep, offset};
}
}
}
// Return offset of next packet or -1
Protocol.prototype.next = function(info, buf) {
if(info && buf) {
var {headers, sep, offset} = info;
var len = headers['content-length'];
console.log('next:',sep,len,buf.length);
if(buf.length - sep >= len) {
return len + sep;
}
}
return -1;
}
Protocol.prototype.parse = async function(buf) {
var data = {messages: [], buf};
var offset, info = await this.peek(buf, 0);
while((offset = this.next(info, buf)) >= 0) {
var content = buf.slice(info.sep, offset);
data.buf = buf.slice(offset);
if(content && content.length) {
data.messages.push({headers: info.headers, content});
} else {
data.messages.push({headers: info.headers});
}
info = await this.peek(buf, offset);
}
if(!data.buf || !data.buf.length) {
delete data.buf;
}
return data;
}
module.exports = Protocol;

@@ -158,0 +160,0 @@

@@ -8,115 +8,108 @@ var net = require('net');

function Router() {
this.servers = {};
this.routes = {};
class Router extends EventEmitter {
constructor() {
super();
this.servers = {};
this.routes = {};
this.handlers = [];
this.$accept = [];
this.$decline = [];
this.handlers = [];
this.$accept = [];
this.$decline = [];
this.enqueue = this.queue();
this.enqueue.next();
this.enqueue = this.queue();
this.enqueue.next();
}
EventEmitter.call(this);
}
addServer(name, server) {
console.log('Add server: ' + name);
this.servers[name] = server;
}
util.inherits(Router, EventEmitter);
listen(name, options) {
this.addServer(name, new Server(this, options));
}
Router.prototype.addServer = function(name, server) {
console.log('Add server: ' + name);
this.servers[name] = server;
}
connect(name, opts) {
var ua = new Ua();
ua.setClient(name, new Client(ua, opts));
return ua;
}
Router.prototype.listen = function(name, options) {
this.addServer(name, new Server(this, options));
}
// get route data
set(name, data) { this.routes[name] = data; }
Router.prototype.connect = function(name, opts) {
var ua = new Ua();
ua.setClient(name, new Client(ua, opts));
return ua;
}
// set route data
get(name) { return this.routes[name]; }
// get route data
Router.prototype.set = function(name, data) {
this.routes[name] = data;
}
// del route data
del(name) { delete this.routes[name]; }
// set route data
Router.prototype.get = function(name) {
return this.routes[name];
}
// make routing chain
createChain(handlers) {
return (data, next) => {
var lock = -1;
// del route data
Router.prototype.del = function(name) {
delete this.routes[name];
}
function step(idx) {
if(idx <= lock) {
throw 'next() called multiple times';
}
// make routing chain
Router.prototype.createChain = function(handlers) {
return (data, next) => {
var lock = -1;
lock = idx;
var func = handlers[idx];
function step(idx) {
if(idx <= lock) {
throw 'next() called multiple times';
}
if(idx === handlers.length) {
func = next;
}
lock = idx;
var func = handlers[idx];
if(!func) {
return Promise.resolve();
}
if(idx === handlers.length) {
func = next;
return Promise.resolve(func(data, () => {
return step(++idx);
}))
}
if(!func) {
return Promise.resolve();
}
return Promise.resolve(func(data, () => {
return step(++idx);
}))
return step(0);
}
}
return step(0);
// add new route and make chain
use(handler) {
this.handlers.push(handler);
this.chain = this.createChain(this.handlers);
}
}
// add new route and make chain
Router.prototype.use = function(handler) {
this.handlers.push(handler);
this.chain = this.createChain(this.handlers);
}
// set accept handler
accept(accept) {
this.$accept.push(accept);
this.acceptChain = this.createChain(this.$accept);
}
// set accept handler
Router.prototype.accept = function(accept) {
this.$accept.push(accept);
this.acceptChain = this.createChain(this.$accept);
}
decline(decline) {
this.$decline.push(decline);
this.declineChain = this.createChain(this.$decline);
}
Router.prototype.decline = function(decline) {
this.$decline.push(decline);
this.declineChain = this.createChain(this.$decline);
}
// start routing chain
Router.prototype.scan = function(messages) {
messages.forEach(msg => {
this.chain(msg).catch(err => {
console.log('routing error:'+err);
// start routing chain
scan(messages) {
messages.forEach(msg => {
this.chain(msg).catch(err => {
console.log('routing error:'+err);
})
})
})
}
}
// enqueue messages
Router.prototype.queue = function *() {
while(true) {
var {messages, trunk} = yield;
this.scan(messages);
// enqueue messages
*queue() {
while(true) {
var {messages, trunk} = yield;
this.scan(messages);
}
}
}
Router.prototype.destroy = function() {
for(var srv in this.servers) {
this.servers[srv].destroy();
destroy() {
for(var srv in this.servers) {
this.servers[srv].destroy();
}
}

@@ -123,0 +116,0 @@ }

@@ -8,68 +8,70 @@ var path = require('path');

function Server(consumer, options) {
var socks = this.socks = [];
class Server {
constructor(consumer, options) {
var socks = this.socks = [];
if(options.tls) {
var srv = tls;
} else {
var srv = net;
}
if(options.tls) {
var srv = tls;
} else {
var srv = net;
}
this.server = srv.createServer(options, function(socket) {
var buf = Buffer.concat([]), id = utils.randomAscii(12);
var trunk = {id, socket, options, end: () => {socket.destroy()}};
socks.push(socket);
this.server = srv.createServer(options, function(socket) {
var buf = Buffer.concat([]), id = utils.randomAscii(12);
var trunk = {id, socket, options, end: () => {socket.destroy()}};
socks.push(socket);
consumer.acceptChain(trunk).then(result => {
socket.on('data', function(data) {
socket.pause();
console.log('SOCKET data:'+data.length+', buf:' + Buffer.byteLength(buf));
// data = buf + data.toString();
data = Buffer.concat([buf, data], buf.length + data.length);
consumer.acceptChain(trunk).then(result => {
socket.on('data', function(data) {
socket.pause();
console.log('SOCKET data:'+data.length+', buf:' + Buffer.byteLength(buf));
// data = buf + data.toString();
data = Buffer.concat([buf, data], buf.length + data.length);
trunk.read({data, trunk, options}).then(leftover => {
if(leftover && leftover.length) {
buf = leftover;
} else {
buf = Buffer.concat([]);
}
socket.resume();
trunk.read({data, trunk, options}).then(leftover => {
if(leftover && leftover.length) {
buf = leftover;
} else {
buf = Buffer.concat([]);
}
socket.resume();
})
})
})
socket.on('error', function(err) {
console.log('socket error: '+err);
consumer.declineChain(trunk);
})
socket.on('error', function(err) {
console.log('socket error: '+err);
consumer.declineChain(trunk);
})
socket.on('end', function() {
console.log('socket end');
consumer.declineChain(trunk);
})
socket.on('end', function() {
console.log('socket end');
consumer.declineChain(trunk);
})
socket.on('close', function() {
socks.splice(socks.indexOf(socket), 1);
socket.on('close', function() {
socks.splice(socks.indexOf(socket), 1);
})
}).catch(err => {
socket.destroy();
socket.end();
})
}).catch(err => {
socket.destroy();
socket.end();
})
})
if(options.tls) {
this.server.listen(options.port, options.host);
} else {
this.server.listen(options);
if(options.tls) {
this.server.listen(options.port, options.host);
} else {
this.server.listen(options);
}
}
}
Server.prototype.destroy = function() {
for (var i in this.socks) {
this.socks[i].destroy();
destroy() {
for (var i in this.socks) {
this.socks[i].destroy();
}
this.server.close();
this.server.unref();
}
this.server.close();
this.server.unref();
}
module.exports = Server;
var util = require('util');
var utils = require('./utils');
var crypto = require('crypto');
var EventEmitter = require('events');
const ascii = 'abcdefghijklmnopqrstuwxyz0123456789';
function Ua() {
this.t = new Map();
this.req = new Map();
this.headers = {};
class Ua extends EventEmitter {
constructor() {
super();
this.t = new Map();
this.req = new Map();
this.headers = {};
}
EventEmitter.call(this);
}
setClient(client) {
console.log('SET CLIENT');
this.client = client;
util.inherits(Ua, EventEmitter);
client.on('error', (...args) => {
this.emit('error', client, ...args);
})
Ua.prototype.setClient = function(client) {
console.log('SET CLIENT');
this.client = client;
client.on('end', (...args) => {
this.emit('end', client, ...args);
})
}
client.on('error', (...args) => {
this.emit('error', client, ...args);
})
connect(trunk) {
console.log('UA CONNECT::'+trunk);
trunk.ua = this;
this.trunk = trunk;
}
client.on('end', (...args) => {
this.emit('end', client, ...args);
})
}
rnd(len) {
return utils.randomAscii(len);
}
Ua.prototype.connect = function(trunk) {
console.log('UA CONNECT::'+trunk);
trunk.ua = this;
this.trunk = trunk;
}
incoming({id, msg, trunk}) {
if(id) {
var trans = this.req.get(id);
Ua.prototype.rnd = utils.randomAscii;
if(trans) {
var entry = trans.table.get(id);
Ua.prototype.incoming = function({id, msg, trunk}) {
if(id) {
var trans = this.req.get(id);
if(trans) {
var entry = trans.table.get(id);
if(entry) {
entry.counter--;
entry.resp.push(msg);
trans.response.next(entry);
if(entry) {
entry.counter--;
entry.resp.push(msg);
trans.response.next(entry);
}
} else {
this.emit('request', {req: msg, trunk});
}
} else {
this.emit('request', {req: msg, trunk});
this.emit('notification', {msg, trunk});
}
} else {
this.emit('notification', {msg, trunk});
}
}
Ua.prototype.trans = function() {
var trans = new Trans(this);
this.t.set(trans.id, trans);
return trans;
}
trans() {
var trans = new Trans(this);
this.t.set(trans.id, trans);
return trans;
}
Ua.prototype.request = function(headers, content, ttl) {
var trans = this.trans(this.client);
ttl = ttl || undefined;
request(headers, content, ttl) {
var trans = this.trans(this.client);
ttl = ttl || undefined;
trans.use(async function(t, next) {t.request({headers, content}); await next()})
trans.use(trans.end);
trans.use(async function(t, next) {t.request({headers, content}); await next()})
trans.use(trans.end);
return trans.run(ttl).then(t => {
return Promise.resolve(t.table.get(headers.id).resp);
})
}
return trans.run(ttl).then(t => {
return Promise.resolve(t.table.get(headers.id).resp);
})
}
Ua.prototype.notification = function(headers, content) {
var trans = this.trans(this.client);
trans.use(async function(t, next) {t.notification({headers, content}); await next()});
trans.use(trans.end);
return trans.run();
notification(headers, content) {
var trans = this.trans(this.client);
trans.use(async function(t, next) {t.notification({headers, content}); await next()});
trans.use(trans.end);
return trans.run();
}
}
// transaction
function Trans(ua) {
this.ua = ua;
this.client = ua.client;
this.id = utils.randomAscii(16);
this.counter = 0;
this.ts = Math.floor(new Date().getTime()/1000);
this.tx = new Set();
this.handlers = [];
this.table = new Map();
this.ttl = 3000;
}
class Trans {
constructor(ua) {
this.ua = ua;
this.client = ua.client;
this.id = utils.randomAscii(16);
this.counter = 0;
this.ts = Math.floor(new Date().getTime()/1000);
this.tx = new Set();
this.handlers = [];
this.table = new Map();
this.ttl = 3000;
}
Trans.prototype.use = function(handler) {
this.handlers.push(handler);
}
use(handler) {
this.handlers.push(handler);
}
Trans.prototype.request = function(req) {
var {id, counter} = this.ua.outgoing(req);
var data = this.ua.write(req);
request(req) {
var {id, counter} = this.ua.outgoing(req);
var data = this.ua.write(req);
this.ua.req.set(id, this);
this.table.set(id, {counter, req, resp: [], data});
this.tx.add(data);
this.counter++;
}
Trans.prototype.notification = function(req) {
this.ua.outgoing(req);
this.tx.add(this.ua.write(req));
}
Trans.prototype.destroy = function() {
if(this.timeout) {
clearTimeout(this.timeout);
delete this.timeout;
this.ua.req.set(id, this);
this.table.set(id, {counter, req, resp: [], data});
this.tx.add(data);
this.counter++;
}
for(let [id, data] of this.table) {
this.ua.req.delete(id);
notification(req) {
this.ua.outgoing(req);
this.tx.add(this.ua.write(req));
}
this.ua.t.delete(this.id);
}
destroy() {
if(this.timeout) {
clearTimeout(this.timeout);
delete this.timeout;
}
Trans.prototype.end = async function(trans, next) {
if(!trans.counter) {
trans.client.outgoing(trans);
return Promise.resolve([]);
for(let [id, data] of this.table) {
this.ua.req.delete(id);
}
this.ua.t.delete(this.id);
}
return new Promise((res, rej) => {
function *response() {
var entry;
async end(trans, next) {
if(!trans.counter) {
trans.client.outgoing(trans);
return Promise.resolve([]);
}
while(trans.counter > 0) {
entry = yield;
return new Promise((res, rej) => {
function *response() {
var entry;
if(!entry.counter) {
trans.counter--;
while(trans.counter > 0) {
entry = yield;
if(!entry.counter) {
trans.counter--;
}
}
trans.destroy();
res(trans.table);
}
trans.destroy();
res(trans.table);
}
trans.response = response();
trans.response.next();
trans.client.outgoing(trans);
trans.timeout = setTimeout(() => {
trans.destroy();
trans.response = response();
trans.response.next();
trans.client.outgoing(trans);
trans.timeout = setTimeout(() => {
trans.destroy();
if(trans.counter) {
res(trans.table);
}
}, trans.ttl)
})
}
if(trans.counter) {
res(trans.table);
compose(handlers, data) {
function step(idx) {
var func = handlers[idx];
if(idx === handlers.length) {
func = next;
}
}, trans.ttl)
})
}
Trans.prototype.compose = function(handlers, data) {
function step(idx) {
var func = handlers[idx];
if(!func) {
return Promise.resolve();
}
if(idx === handlers.length) {
func = next;
return Promise.resolve(func(data, () => {
return step(++idx);
}))
}
if(!func) {
return Promise.resolve();
}
return step(0).then(() => (data));
}
return Promise.resolve(func(data, () => {
return step(++idx);
}))
run(ttl) {
this.ttl = ttl ? ttl*1000 : this.ttl;
return this.compose(this.handlers.concat(this.end), this);
}
return step(0).then(() => (data));
}
Trans.prototype.run = function(ttl) {
this.ttl = ttl ? ttl*1000 : this.ttl;
return this.compose(this.handlers.concat(this.end), this);
}
module.exports = Ua;

@@ -59,3 +59,3 @@ {

"optionalDependencies": {},
"version": "0.0.17",
"version": "0.1.00",
"directories": {

@@ -62,0 +62,0 @@ "lib": "lib"

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc