Comparing version 0.0.6 to 0.0.7
@@ -6,4 +6,6 @@ var Router = require('./lib/router'); | ||
var Protocol = require('./lib/protocol'); | ||
var Utils = require('./lib/utils'); | ||
module.exports = { | ||
utils: Utils, | ||
createRouter: (...opts) => { | ||
@@ -19,2 +21,4 @@ return new Router(...opts); | ||
createClient: (...opts) => { | ||
var arr = [...opts]; | ||
console.log('arr[0]:'+(arr[0] instanceof Ua)); | ||
return new Client(...opts); | ||
@@ -21,0 +25,0 @@ }, |
@@ -7,3 +7,2 @@ var net = require('net'); | ||
function Client(consumer, options) { | ||
var self = this; | ||
var buf = ''; | ||
@@ -18,11 +17,11 @@ | ||
var client = this.client = srv.connect(options, function() { | ||
console.log('Client connect'); | ||
console.log('Client connect:'+typeof consumer, JSON.stringify(options)); | ||
consumer.connect(client); | ||
}) | ||
this.client.on('data', function(data) { | ||
this.client.on('data', data => { | ||
client.pause(); | ||
data = buf + data.toString(); | ||
consumer.input({data}).then(waste => { | ||
consumer.read({data, trunk: client}).then(waste => { | ||
buf = waste || buf; | ||
@@ -33,10 +32,10 @@ client.resume(); | ||
this.client.on('end', function() { | ||
self.client.destroy(); | ||
this.client.on('end', () => { | ||
this.client.destroy(); | ||
}) | ||
this.client.on('error', function(error) { | ||
this.client.on('error', error => { | ||
console.log('client error: '+error); | ||
process.nextTick(function() { | ||
process.nextTick(() => { | ||
process.exit(0); | ||
@@ -49,4 +48,4 @@ }) | ||
Client.prototype.outgoing = function(trans, consumer) { | ||
for (let {id, counter, data} of trans.q) { | ||
Client.prototype.outgoing = function(trans) { | ||
for (let {data} of trans.q) { | ||
this.client.write(data); | ||
@@ -57,2 +56,4 @@ } | ||
Client.prototype.end = function() { | ||
console.log('END'); | ||
if(this.client) { | ||
@@ -59,0 +60,0 @@ this.client.unref(); |
@@ -6,3 +6,2 @@ var net = require('net'); | ||
function Router() { | ||
var self = this; | ||
this.servers = {}; | ||
@@ -98,7 +97,4 @@ this.routes = {}; | ||
Router.prototype.scan = function(messages) { | ||
var self = this; | ||
messages.forEach(msg => { | ||
self.chain(msg).then(() => { | ||
}).catch(err => { | ||
this.chain(msg).catch(err => { | ||
console.log('routing error:'+err); | ||
@@ -112,3 +108,3 @@ }) | ||
while(true) { | ||
var {messages, conn} = yield; | ||
var {messages, trunk} = yield; | ||
this.scan(messages); | ||
@@ -115,0 +111,0 @@ } |
@@ -9,4 +9,2 @@ var path = require('path'); | ||
function Server(consumer, options) { | ||
var self = this; | ||
if(options.tls) { | ||
@@ -19,6 +17,6 @@ var srv = tls; | ||
this.server = srv.createServer(options, function(socket) { | ||
var buf = '', key = utils.randomAscii(12); | ||
var conn = {key, socket}; | ||
var buf = '', id = utils.randomAscii(12); | ||
var trunk = {id, socket, end: () => {socket.destroy()}}; | ||
consumer.$accept(conn).then(result => { | ||
consumer.$accept(trunk).then(result => { | ||
socket.on('data', function(data) { | ||
@@ -28,3 +26,3 @@ socket.pause(); | ||
conn.input({data, conn}).then(waste => { | ||
trunk.read({data, trunk}).then(waste => { | ||
buf = waste || buf; | ||
@@ -40,3 +38,3 @@ socket.resume(); | ||
socket.on('end', function() { | ||
consumer.$decline({key}); | ||
consumer.$decline(trunk); | ||
}) | ||
@@ -43,0 +41,0 @@ }).catch(err => { |
@@ -10,2 +10,3 @@ var util = require('util'); | ||
this.req = new Map(); | ||
this.headers = {}; | ||
@@ -17,4 +18,4 @@ EventEmitter.call(this); | ||
Ua.prototype.setClient = function(name, client) { | ||
this.clients[name] = client; | ||
Ua.prototype.setClient = function(name, trunk) { | ||
this.clients[name] = trunk; | ||
} | ||
@@ -24,3 +25,3 @@ | ||
Ua.prototype.incoming = function(id, msg) { | ||
Ua.prototype.incoming = function({id, msg, trunk}) { | ||
if(id) { | ||
@@ -38,11 +39,11 @@ var trans = this.req.get(id); | ||
} else { | ||
this.emit('request', msg); | ||
this.emit('request', {req: msg, trunk}); | ||
} | ||
} else { | ||
this.emit('notification', msg); | ||
this.emit('notification', {msg, trunk}); | ||
} | ||
} | ||
Ua.prototype.trans = function(client) { | ||
var trans = new Trans(this, this.clients[client]); | ||
Ua.prototype.trans = function(trunk) { | ||
var trans = new Trans(this, this.clients[trunk]); | ||
this.t.set(trans.id, trans); | ||
@@ -52,14 +53,21 @@ return trans; | ||
Ua.prototype.request = function(client, req) { | ||
return this.trans(client).add(t => {t.request(req)}).run(); | ||
Ua.prototype.request = function(trunk, ttl, req) { | ||
if(!req) { | ||
req = ttl; | ||
ttl = undefined; | ||
} | ||
return this.trans(trunk).add(t => {t.request(req)}).run(ttl).then(res => { | ||
return Promise.resolve(res.get(req.headers.id).resp); | ||
}) | ||
} | ||
Ua.prototype.notification = function(client, req) { | ||
return this.trans(client).add(t => {t.notification(req)}).run(); | ||
Ua.prototype.notification = function(trunk, req) { | ||
return this.trans(trunk).add(t => {t.notification(req)}).run(); | ||
} | ||
// transaction | ||
function Trans(ua, client) { | ||
function Trans(ua, trunk) { | ||
this.ua = ua; | ||
this.client = client; | ||
this.client = trunk; | ||
this.id = utils.randomAscii(16); | ||
@@ -69,8 +77,9 @@ this.counter = 0; | ||
this.q = new Set(); | ||
this.chain = []; | ||
this.handlers = []; | ||
this.table = new Map(); | ||
this.ttl = 3000; | ||
} | ||
Trans.prototype.add = function(handler) { | ||
this.chain.push(handler); | ||
this.handlers.push(handler); | ||
return this; | ||
@@ -80,3 +89,5 @@ } | ||
Trans.prototype.request = function(req) { | ||
var {id, counter, data} = this.ua.outgoing(req); | ||
var {id, counter} = this.ua.outgoing(req); | ||
var data = this.ua.write(req); | ||
this.ua.req.set(id, this); | ||
@@ -94,4 +105,8 @@ this.table.set(id, {counter, req, resp: [], data}); | ||
Trans.prototype.destroy = function() { | ||
if(this.timeout) { | ||
clearTimeout(this.timeout); | ||
delete this.timeout; | ||
} | ||
for(let [id, data] of this.table) { | ||
console.log('['+this.id+']: destroy ' + id); | ||
this.ua.req.delete(id); | ||
@@ -123,19 +138,23 @@ } | ||
trans.client.outgoing(trans); | ||
trans.timeout = setTimeout(() => { | ||
trans.destroy(); | ||
if(trans.counter) { | ||
res(trans.table); | ||
} | ||
}, trans.ttl) | ||
}) | ||
} | ||
Trans.prototype.createChain = function(handlers) { | ||
var t = this, p = Promise.resolve(this); | ||
handlers.forEach(item => { | ||
p = p.then(item).then(() => {return t}); | ||
}) | ||
return p; | ||
Trans.prototype.chain = function(ctx, handlers) { | ||
return handlers.reduce((p, cb) => { | ||
return p.then(cb).then(() => {return ctx}) | ||
}, Promise.resolve(this)) | ||
} | ||
Trans.prototype.run = function() { | ||
return this.createChain(this.chain).then(this.end); | ||
Trans.prototype.run = function(ttl) { | ||
this.ttl = ttl ? ttl*1000 : this.ttl; | ||
return this.chain(this, this.handlers).then(this.end) | ||
} | ||
module.exports = Ua; |
{ | ||
"name": "sipware", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "author": { |
AI-detected possible typosquat
Supply chain riskAI has identified this package as a potential typosquat of a more popular package. This suggests that the package may be intentionally mimicking another package's name, description, or other metadata.
Found 1 instance in 1 package
47219
451
6