multiserver
Advanced tools
Comparing version 1.13.7 to 2.0.0
@@ -39,2 +39,16 @@ var assert = require('assert') | ||
function asyncify(f) { | ||
return function(cb) { | ||
if (f.length) return f(cb) | ||
if (cb) { | ||
var result | ||
try{ | ||
result = f() | ||
} catch(err) {return cb(err)} | ||
return cb(null, result) | ||
} | ||
return f() | ||
} | ||
} | ||
module.exports = function (ary, wrap) { | ||
@@ -75,2 +89,5 @@ if(!wrap) wrap = function (e) { return e } | ||
}, | ||
// There should be a callback , called with | ||
// null when the server started to listen. | ||
// (net.server.listen is async for example) | ||
server: function (onConnection, onError) { | ||
@@ -81,3 +98,3 @@ onError = onError || function (err) { | ||
} | ||
return proto.server(function (stream) { | ||
return asyncify(proto.server(function (stream) { | ||
compose( | ||
@@ -91,3 +108,3 @@ wrap(stream), | ||
) | ||
}) | ||
})) | ||
}, | ||
@@ -94,0 +111,0 @@ parse: parse, |
16
index.js
var compose = require('./compose') | ||
var isArray = Array.isArray | ||
var multicb = require('multicb') | ||
@@ -14,3 +15,3 @@ function split(str) { | ||
return { | ||
var _self = { | ||
name: plugs.map(function (e) { return e.name }).join(';'), | ||
@@ -26,3 +27,3 @@ client: function (addr, cb) { | ||
if(plug) plug.client(_addr, cb) | ||
else cb(new Error('could not connect to:'+addr+', only know:'+this.name)) | ||
else cb(new Error('could not connect to:'+addr+', only know:'+_self.name)) | ||
}, | ||
@@ -35,4 +36,10 @@ server: function (onConnect, onError) { | ||
return function () { | ||
closes.forEach(function (close) { close() }) | ||
return function (cb) { | ||
var done | ||
if (cb) done = multicb() | ||
closes.forEach(function (close) { | ||
if (done && close.length) close(done()) | ||
else close() | ||
}) | ||
if (done) done(cb) | ||
} | ||
@@ -60,3 +67,4 @@ }, | ||
} | ||
return _self | ||
} | ||
{ | ||
"name": "multiserver", | ||
"description": "write a server which works over many protocols at once, or connect to the same", | ||
"version": "1.13.7", | ||
"version": "2.0.0", | ||
"homepage": "https://github.com/dominictarr/multiserver", | ||
@@ -11,2 +11,3 @@ "repository": { | ||
"dependencies": { | ||
"multicb": "^1.2.2", | ||
"pull-cat": "~1.1.5", | ||
@@ -18,2 +19,3 @@ "pull-stream": "^3.6.1", | ||
"socks": "2.2.1", | ||
"multiserver-scopes": "^1.0.0", | ||
"stream-to-pull-stream": "^1.7.2" | ||
@@ -20,0 +22,0 @@ }, |
@@ -7,2 +7,3 @@ var net | ||
var toPull = require('stream-to-pull-stream') | ||
var scopes = require('multiserver-scopes') | ||
@@ -22,8 +23,16 @@ function toDuplex (str) { | ||
server: function (onConnection) { | ||
var port = opts.port | ||
var host = opts.host || opts.scope && scopes.host(opts.scope) || 'localhost' | ||
console.log('Listening on ' + host + ':' + port + ' (multiserver net plugin)') | ||
var server = net.createServer(opts, function (stream) { | ||
var addr = stream.address() | ||
onConnection(toDuplex(stream)) | ||
}).listen(opts.port) | ||
return function () { | ||
server.close() | ||
}).listen(port, host) | ||
return function (cb) { | ||
console.log('Closing server on ' + host + ':' + port + ' (multiserver net plugin)') | ||
server.close(function(err) { | ||
if (err) console.error(err) | ||
else console.log('No longer listening on ' + host + ':' + port + ' (multiserver net plugin)') | ||
if (cb) cb(err) | ||
}) | ||
} | ||
@@ -68,5 +77,3 @@ }, | ||
stringify: function (scope) { | ||
var host = opts.external || opts.host || 'localhost' | ||
if (scope === 'private') | ||
host = opts.host || 'localhost' | ||
var host = scope == 'public' && opts.external || opts.host || scope && scopes.host(scope) || 'localhost' | ||
return ['net', host, opts.port].join(':') | ||
@@ -73,0 +80,0 @@ } |
@@ -10,3 +10,3 @@ var SHS = require('secret-handshake') | ||
var keys = SHS.toKeys(opts.keys || opts.seed) | ||
var appKey = isString(opts.appKey) ? new Buffer(opts.appKey, 'base64') : opts.appKey | ||
var appKey = isString(opts.appKey) ? Buffer.from(opts.appKey, 'base64') : opts.appKey | ||
@@ -49,6 +49,6 @@ var server = SHS.createServer( | ||
if(ary.length > 2) { | ||
seed = new Buffer(ary[2], 'base64') | ||
seed = Buffer.from(ary[2], 'base64') | ||
if(seed.length !== 32) return null | ||
} | ||
var key = new Buffer(ary[1], 'base64') | ||
var key = Buffer.from(ary[1], 'base64') | ||
if(key.length !== 32) return null | ||
@@ -55,0 +55,0 @@ |
@@ -5,2 +5,3 @@ var WS = require('pull-ws') | ||
var Map = require('pull-stream/throughs/map') | ||
var scopes = require('multiserver-scopes') | ||
@@ -16,2 +17,3 @@ module.exports = function (opts) { | ||
if(!WS.createServer) return | ||
opts.host = opts.host || opts.scope && scopes.host(opts.scope) || 'localhost' | ||
var server = WS.createServer(opts, function (stream) { | ||
@@ -22,4 +24,14 @@ stream.address = 'ws:'+stream.remoteAddress + (stream.remotePort ? ':'+stream.remotePort : '') | ||
if(!opts.server) server.listen(opts.port) | ||
return server.close.bind(server) | ||
if(!opts.server) { | ||
console.log('Listening on ' + opts.host +':' + opts.port + ' (multiserver ws plugin)') | ||
server.listen(opts.port) | ||
} | ||
return function (cb) { | ||
console.log('Closing server on ' + opts.host +':' + opts.port + ' (multiserver ws plugin)') | ||
server.close(function(err) { | ||
if (err) console.error(err) | ||
else console.log('No longer listening on ' + opts.host +':' + opts.port + ' (multiserver ws plugin)') | ||
if (cb) cb(err) | ||
}) | ||
} | ||
}, | ||
@@ -39,3 +51,3 @@ client: function (addr, cb) { | ||
//ensure stream is a stream of node buffers | ||
stream.source = pull(stream.source, Map(Buffer)) | ||
stream.source = pull(stream.source, Map(Buffer.from.bind(Buffer))) | ||
cb(err, stream) | ||
@@ -42,0 +54,0 @@ } |
@@ -13,5 +13,5 @@ var tape = require('tape') | ||
var cl = require('chloride') | ||
var seed = cl.crypto_hash_sha256(new Buffer('TESTSEED')) | ||
var seed = cl.crypto_hash_sha256(Buffer.from('TESTSEED')) | ||
var keys = cl.crypto_sign_seed_keypair(seed) | ||
var appKey = cl.crypto_hash_sha256(new Buffer('TEST')) | ||
var appKey = cl.crypto_hash_sha256(Buffer.from('TEST')) | ||
@@ -64,3 +64,3 @@ var requested, ts | ||
pull( | ||
pull.values([new Buffer('Hello')]), | ||
pull.values([Buffer.from('Hello')]), | ||
stream, | ||
@@ -86,3 +86,3 @@ pull.collect(function (err, ary) { | ||
pull( | ||
pull.values([new Buffer('Hello')]), | ||
pull.values([Buffer.from('Hello')]), | ||
stream, | ||
@@ -105,3 +105,3 @@ pull.collect(function (err, ary) { | ||
pull( | ||
pull.values([new Buffer('Hello')]), | ||
pull.values([Buffer.from('Hello')]), | ||
stream, | ||
@@ -108,0 +108,0 @@ pull.collect(function (err, ary) { |
var tape = require('tape') | ||
var pull = require('pull-stream') | ||
var Pushable = require('pull-pushable') | ||
var scopes = require('multiserver-scopes') | ||
@@ -13,5 +14,5 @@ var Compose = require('../compose') | ||
var cl = require('chloride') | ||
var seed = cl.crypto_hash_sha256(new Buffer('TESTSEED')) | ||
var seed = cl.crypto_hash_sha256(Buffer.from('TESTSEED')) | ||
var keys = cl.crypto_sign_seed_keypair(seed) | ||
var appKey = cl.crypto_hash_sha256(new Buffer('TEST')) | ||
var appKey = cl.crypto_hash_sha256(Buffer.from('TEST')) | ||
@@ -25,2 +26,3 @@ var requested, ts | ||
//var net = Net({port: 4848, scope: 'device'}) | ||
var net = Net({port: 4848}) | ||
@@ -38,2 +40,5 @@ var ws = Ws({port: 4848}) | ||
// travis currently does not support ipv6, becaue GCE does not. | ||
var has_ipv6 = process.env.TRAVIS === undefined | ||
tape('parse, stringify', function (t) { | ||
@@ -68,3 +73,2 @@ | ||
t.end() | ||
@@ -77,3 +81,3 @@ }) | ||
pull.map(function (data) { | ||
return new Buffer(data.toString().toUpperCase()) | ||
return Buffer.from(data.toString().toUpperCase()) | ||
}), | ||
@@ -90,3 +94,3 @@ stream | ||
pull( | ||
pull.values([new Buffer('hello world')]), | ||
pull.values([Buffer.from('hello world')]), | ||
stream, | ||
@@ -96,4 +100,3 @@ pull.collect(function (err, ary) { | ||
t.equal(Buffer.concat(ary).toString(), 'HELLO WORLD') | ||
t.end() | ||
close() | ||
close(function() {t.end()}) | ||
}) | ||
@@ -104,7 +107,15 @@ ) | ||
if (has_ipv6) | ||
tape('combined, ipv6', function (t) { | ||
var combined = Compose([ | ||
Net({ | ||
port: 4848, | ||
host: '::' | ||
}), | ||
shs | ||
]) | ||
var close = combined.server(echo) | ||
var addr = combined.stringify() | ||
console.log('addr', addr) | ||
var addr = combined.stringify().replace('localhost', '::1') | ||
@@ -115,3 +126,3 @@ combined.client(addr, function (err, stream) { | ||
pull( | ||
pull.values([new Buffer('hello world')]), | ||
pull.values([Buffer.from('hello world')]), | ||
stream, | ||
@@ -121,4 +132,3 @@ pull.collect(function (err, ary) { | ||
t.equal(Buffer.concat(ary).toString(), 'HELLO WORLD') | ||
t.end() | ||
close() | ||
close(function() {t.end()}) | ||
}) | ||
@@ -129,3 +139,23 @@ ) | ||
tape('net: do not listen on all addresses', function (t) { | ||
var combined = Compose([ | ||
Net({ | ||
port: 4848, | ||
host: 'localhost', | ||
external: scopes.host('private') // unroutable IP, but not localhost (e.g. 192.168 ...) | ||
}), | ||
shs | ||
]) | ||
var close = combined.server(echo) | ||
var addr = combined.stringify('public') // returns external | ||
console.log('addr public scope', addr) | ||
combined.client(addr, function (err, stream) { | ||
t.ok(err, 'should only listen on localhost') | ||
close(function() {t.end()}) | ||
}) | ||
}) | ||
tape('ws with combined', function (t) { | ||
@@ -143,3 +173,3 @@ var close = combined_ws.server(function (stream) { | ||
var pushable = Pushable() | ||
pushable.push(new Buffer('hello world')) | ||
pushable.push(Buffer.from('hello world')) | ||
pull( | ||
@@ -153,4 +183,3 @@ pushable, | ||
t.equal(Buffer.concat(ary).toString(), 'HELLO WORLD') | ||
t.end() | ||
close() | ||
close(function() {t.end()}) | ||
}) | ||
@@ -175,3 +204,3 @@ ) | ||
var seed = cl.crypto_hash_sha256(new Buffer('TEST SEED')) | ||
var seed = cl.crypto_hash_sha256(Buffer.from('TEST SEED')) | ||
var bob = cl.crypto_sign_seed_keypair(seed) | ||
@@ -193,5 +222,4 @@ | ||
t.deepEqual(checked, bob.publicKey) | ||
t.end() | ||
stream.source(true, function () {}) | ||
close() | ||
close(function() {t.end()}) | ||
}) | ||
@@ -271,4 +299,3 @@ | ||
stream.source(true, function () { | ||
close() | ||
t.end() | ||
close(function() {t.end()}) | ||
}) | ||
@@ -281,3 +308,3 @@ }) | ||
tape(name+', aborted', function (t) { | ||
var close = combined.server(function () { | ||
var close = combined.server(function onConnection() { | ||
throw new Error('should never happen') | ||
@@ -288,7 +315,16 @@ }) | ||
t.ok(err) | ||
t.end() | ||
close() | ||
// NOTE: without the timeout, we try to close the server | ||
// before it actually started listening, which fails and then | ||
// the server keeps runnung, causing the next test to fail with EADDRINUSE | ||
// | ||
// This is messy, combined.server should be a proper async call | ||
setTimeout( function() { | ||
console.log('Calling close') | ||
close(function() {t.end()}) | ||
}, 500) | ||
}) | ||
abort() | ||
abort() | ||
}) | ||
@@ -313,4 +349,2 @@ } | ||
//because the key is not known in a wrong number. | ||
t.end() | ||
close() | ||
}) | ||
@@ -323,6 +357,7 @@ | ||
t.ok(err) | ||
close() | ||
console.log('Calling close') | ||
close() // in this case, net.server.close(cb) never calls its cb, why? | ||
t.end() | ||
}) | ||
}) | ||
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 3 instances 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
214520
19
6445
9
6
3
+ Addedmulticb@^1.2.2
+ Addedmultiserver-scopes@^1.0.0
+ Addedmulticb@1.2.2(transitive)
+ Addedmultiserver-scopes@1.0.0(transitive)
+ Addednon-private-ip@1.4.4(transitive)