Comparing version 0.0.8 to 0.0.9
@@ -21,3 +21,2 @@ var Router = require('./lib/router'); | ||
var arr = [...opts]; | ||
console.log('arr[0]:'+(arr[0] instanceof Ua)); | ||
return new Client(...opts); | ||
@@ -24,0 +23,0 @@ }, |
@@ -16,3 +16,2 @@ var net = require('net'); | ||
var client = this.client = srv.connect(options, function() { | ||
console.log('Client connect:'+typeof consumer, JSON.stringify(options)); | ||
consumer.connect(client); | ||
@@ -47,3 +46,3 @@ }) | ||
Client.prototype.outgoing = function(trans) { | ||
for (let {data} of trans.q) { | ||
for (let data of trans.tx) { | ||
this.client.write(data); | ||
@@ -54,4 +53,2 @@ } | ||
Client.prototype.end = function() { | ||
console.log('END'); | ||
if(this.client) { | ||
@@ -58,0 +55,0 @@ this.client.unref(); |
@@ -19,3 +19,3 @@ var net = require('net'); | ||
Router.prototype.addServer = function(name, server) { | ||
console.log('add server:'+name); | ||
console.log('Add server: ' + name); | ||
this.servers[name] = server; | ||
@@ -22,0 +22,0 @@ } |
@@ -51,2 +51,4 @@ var util = require('util'); | ||
Ua.prototype.request = function(trunk, ttl, req) { | ||
var trans = this.trans(trunk); | ||
if(!req) { | ||
@@ -57,4 +59,7 @@ req = ttl; | ||
return this.trans(trunk).add(t => {t.request(req)}).run(ttl).then(res => { | ||
return Promise.resolve(res.get(req.headers.id).resp); | ||
trans.use(async function(t, next) {t.request(req); await next()}) | ||
trans.use(trans.end); | ||
return trans.run(ttl).then(t => { | ||
return Promise.resolve(t.table.get(req.headers.id).resp); | ||
}) | ||
@@ -64,3 +69,6 @@ } | ||
Ua.prototype.notification = function(trunk, req) { | ||
return this.trans(trunk).add(t => {t.notification(req)}).run(); | ||
var trans = this.trans(trunk); | ||
trans.use(async function(t, next) {t.notification(req); await next()}); | ||
trans.use(trans.end); | ||
return trans.run(); | ||
} | ||
@@ -75,3 +83,3 @@ | ||
this.ts = Math.floor(new Date().getTime()/1000); | ||
this.q = new Set(); | ||
this.tx = new Set(); | ||
this.handlers = []; | ||
@@ -82,5 +90,4 @@ this.table = new Map(); | ||
Trans.prototype.add = function(handler) { | ||
Trans.prototype.use = function(handler) { | ||
this.handlers.push(handler); | ||
return this; | ||
} | ||
@@ -94,3 +101,3 @@ | ||
this.table.set(id, {counter, req, resp: [], data}); | ||
this.q.add({id, counter, data}); | ||
this.tx.add(data); | ||
this.counter++; | ||
@@ -101,4 +108,3 @@ } | ||
this.ua.outgoing(req); | ||
var data = this.ua.write(req); | ||
this.q.add({data}); | ||
this.tx.add(this.ua.write(req)); | ||
} | ||
@@ -119,3 +125,3 @@ | ||
Trans.prototype.end = function(trans) { | ||
Trans.prototype.end = async function(trans, next) { | ||
if(!trans.counter) { | ||
@@ -155,13 +161,27 @@ trans.client.outgoing(trans); | ||
Trans.prototype.chain = function(ctx, handlers) { | ||
return handlers.reduce((p, cb) => { | ||
return p.then(cb).then(() => {return ctx}) | ||
}, Promise.resolve(this)) | ||
} | ||
Trans.prototype.compose = function(handlers, data) { | ||
function step(idx) { | ||
var func = handlers[idx]; | ||
if(idx === handlers.length) { | ||
func = next; | ||
} | ||
if(!func) { | ||
return Promise.resolve(); | ||
} | ||
return Promise.resolve(func(data, () => { | ||
return step(++idx); | ||
})) | ||
} | ||
return step(0).then(() => (data)); | ||
} | ||
Trans.prototype.run = function(ttl) { | ||
this.ttl = ttl ? ttl*1000 : this.ttl; | ||
return this.chain(this, this.handlers).then(this.end) | ||
return this.compose(this.handlers.concat(this.end), this); | ||
} | ||
module.exports = Ua; |
{ | ||
"name": "sipware", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "author": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
47552
468