Skip to content

Commit 598178a

Browse files
authored
Merge pull request #50 from ssb-js/upgrade-ws
Upgrade pull-ws
2 parents 1b3a31d + e647c6b commit 598178a

File tree

3 files changed

+93
-75
lines changed

3 files changed

+93
-75
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"multicb": "^1.2.2",
1313
"multiserver-scopes": "^1.0.0",
1414
"pull-stream": "^3.6.1",
15-
"pull-ws": "^3.3.0",
15+
"pull-websocket": "^3.4.0",
1616
"secret-handshake": "^1.1.16",
1717
"separator-escape": "0.0.1",
1818
"socks": "^2.2.3",

plugins/ws.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
var WS = require('pull-ws')
1+
var WS = require('pull-websocket')
22
var URL = require('url')
33
var pull = require('pull-stream/pull')
44
var Map = require('pull-stream/throughs/map')
55
var scopes = require('multiserver-scopes')
66
var http = require('http')
7-
var https = require('https')
8-
var fs = require('fs')
7+
var https = require('https')
8+
var fs = require('fs')
99
var debug = require('debug')('multiserver:ws')
1010

1111
function safe_origin (origin, address, port) {
@@ -51,9 +51,7 @@ module.exports = function (opts = {}) {
5151
name: 'ws',
5252
scope: () => scope,
5353
server: function (onConnect, startedCb) {
54-
if (WS.createServer == null) {
55-
return null
56-
}
54+
if (WS.createServer == null) return null
5755

5856
// Maybe weird: this sets a random port each time that `server()` is run
5957
// whereas the net plugin sets the port when the outer function is run.
@@ -68,9 +66,10 @@ module.exports = function (opts = {}) {
6866
opts.cert = fs.readFileSync(opts.cert)
6967

7068
var server = opts.server ||
71-
(opts.key && opts.cert ? https.createServer({ key: opts.key, cert: opts.cert }, opts.handler) : http.createServer(opts.handler))
69+
(opts.key && opts.cert ? https.createServer({ key: opts.key, cert: opts.cert }, opts.handler) : http.createServer(opts.handler))
7270

73-
WS.createServer(Object.assign({}, opts, {server: server}), function (stream) {
71+
const serverOpts = Object.assign({}, opts, {server: server})
72+
let wsServer = WS.createServer(serverOpts, function (stream) {
7473
stream.address = safe_origin(
7574
stream.headers.origin,
7675
stream.remoteAddress,
@@ -90,7 +89,8 @@ module.exports = function (opts = {}) {
9089

9190
return function (cb) {
9291
debug('Closing server on %s:%d', opts.host, opts.port)
93-
server.close(function(err) {
92+
wsServer.close((err) => {
93+
debug('after WS close', err)
9494
if (err) console.error(err)
9595
else debug('No longer listening on %s:%d', opts.host, opts.port)
9696
if (cb) cb(err)
@@ -117,7 +117,7 @@ module.exports = function (opts = {}) {
117117
stream.address = addr
118118

119119
return function () {
120-
stream.close(cb)
120+
stream.close()
121121
}
122122
},
123123
stringify: function (targetScope = 'device') {
@@ -157,7 +157,3 @@ module.exports = function (opts = {}) {
157157
}
158158
}
159159
}
160-
161-
162-
163-

test/plugs.js

Lines changed: 82 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -356,85 +356,107 @@ tape('onion plug', function (t) {
356356
t.end()
357357
})
358358

359-
tape('id of stream from server', function (t) {
360-
check = function (id, cb) {
361-
cb(null, true)
362-
}
363-
var close = combined.server(function (stream) {
364-
var addr = combined.parse(stream.address)
365-
t.ok(addr)
366-
//console.log('address as seen on server', addr)
367-
t.equal(addr[0].name, 'net')
368-
t.deepEqual(addr[1], combined.parse(combined.stringify())[1])
369-
370-
pull(stream.source, stream.sink) //echo
371-
}, function (err) {
372-
if(err) throw err
373-
}, function () {
374-
375-
combined.client(combined.stringify(), function (err, stream) {
376-
if(err) throw err
359+
function testServerId(combined, name, port) {
360+
tape('id of stream from server', function (t) {
361+
check = function (id, cb) {
362+
cb(null, true)
363+
}
364+
var close = combined.server(function (stream) {
365+
console.log('raw address on server:', stream.address)
377366
var addr = combined.parse(stream.address)
378-
t.equal(addr[0].name, 'net')
379-
t.equal(addr[0].port, 4848)
367+
t.ok(addr)
368+
console.log('address as seen on server', addr)
369+
t.equal((addr[0].name || addr[0].protocol).replace(':', ''), name)
380370
t.deepEqual(addr[1], combined.parse(combined.stringify())[1])
381-
stream.source(true, function () {
382-
close(t.end)
371+
372+
pull(stream.source, stream.sink) //echo
373+
}, function (err) {
374+
if(err) throw err
375+
}, function () {
376+
377+
combined.client(combined.stringify(), function (err, stream) {
378+
if(err) throw err
379+
var addr = combined.parse(stream.address)
380+
t.equal((addr[0].name || addr[0].protocol).replace(':', ''), name)
381+
if (addr[0].protocol === 'ws:')
382+
t.equal(+addr[0].port, 4849)
383+
else
384+
t.equal(+addr[0].port, 4848)
385+
t.deepEqual(addr[1], combined.parse(combined.stringify())[1])
386+
stream.source(true, function () {
387+
close(t.end)
388+
})
383389
})
384390
})
385391
})
386-
})
392+
393+
}
394+
395+
testServerId(combined, 'net')
396+
testServerId(combined_ws, 'ws')
397+
398+
399+
387400

388401
function testAbort (name, combined) {
389402
tape(name+', aborted', function (t) {
390403
var close = combined.server(function onConnection() {
391404
throw new Error('should never happen')
392-
}, null, () => {
393-
var abort = combined.client(combined.stringify(), function (err, stream) {
394-
t.ok(err, 'the error is expected')
395-
396-
// NOTE: without the timeout, we try to close the server
397-
// before it actually started listening, which fails and then
398-
// the server keeps runnung, causing the next test to fail with EADDRINUSE
399-
//
400-
// This is messy, combined.server should be a proper async call
401-
setTimeout( function() {
402-
//console.log('Calling close')
403-
close(t.end)
404-
}, 500)
405-
})
405+
})
406406

407-
abort()
407+
var abort = combined.client(combined.stringify(), function (err, stream) {
408+
t.ok(err)
409+
console.error("CLIENT ABORTED", err)
410+
// NOTE: without the timeout, we try to close the server
411+
// before it actually started listening, which fails and then
412+
// the server keeps runnung, causing the next test to fail with EADDRINUSE
413+
//
414+
// This is messy, combined.server should be a proper async call
415+
setTimeout( function() {
416+
console.log('Calling close')
417+
close(t.end)
418+
}, 500)
408419
})
420+
421+
abort()
409422
})
410423
}
411424

412425
testAbort('combined', combined)
413426
testAbort('combined.ws', combined_ws)
414427

415-
tape('error should have client address on it', function (t) {
416-
// return t.end()
417-
check = function (id, cb) {
418-
throw new Error('should never happen')
419-
}
420-
var close = combined.server(function (stream) {
421-
throw new Error('should never happen')
422-
}, function (err) {
423-
t.ok(/^net:/.test(err.address))
424-
t.ok(/~shs:/.test(err.address))
425-
//the shs address won't actually parse, because it doesn't have the key in it
426-
//because the key is not known in a wrong number.
427-
}, function () {
428-
//very unlikely this is the address, which will give a wrong number at the server.
429-
var addr = combined.stringify().replace(/shs:......../, 'shs:XXXXXXXX')
430-
combined.client(addr, function (err, stream) {
431-
//client should see client auth rejected
432-
t.ok(err)
433-
//console.log('Calling close')
434-
close(t.end)
428+
function testErrorAddress(combined, type) {
429+
tape('error should have client address on it:' + type, function (t) {
430+
check = function (id, cb) {
431+
throw new Error('should never happen')
432+
}
433+
var close = combined.server(function (stream) {
434+
throw new Error('should never happen')
435+
}, function (err) {
436+
var addr = err.address
437+
t.ok(err.address.indexOf(type) == 0) //net or ws
438+
t.ok(/\~shs\:/.test(err.address))
439+
//the shs address won't actually parse, because it doesn't have the key in it
440+
//because the key is not known in a wrong number.
441+
}, function () {
442+
//very unlikely this is the address, which will give a wrong number at the server.
443+
var addr = combined.stringify().replace(/shs:......../, 'shs:XXXXXXXX')
444+
combined.client(addr, function (err, stream) {
445+
//client should see client auth rejected
446+
t.ok(err)
447+
close(() => {
448+
if (type === 'ws') // we need to wait for the kill
449+
setTimeout(t.end, 1100)
450+
else
451+
t.end()
452+
})
453+
})
435454
})
436455
})
437-
})
456+
}
457+
458+
testErrorAddress(combined, 'net')
459+
testErrorAddress(combined_ws, 'ws')
438460

439461
tape('multiple public different hosts', function(t) {
440462
var net1 = Net({ host: '127.0.0.1', port: 4854, scope: 'public'})
@@ -593,4 +615,4 @@ tape('ws: external is an array w/ multiple entries & shs transform', function (t
593615
'ws://domain.de:9966~shs:+y42DK+BGzqvU00EWMKiyj4fITskSm+Drxq1Dt2s3Yw=;ws://funtime.net:9966~shs:+y42DK+BGzqvU00EWMKiyj4fITskSm+Drxq1Dt2s3Yw='
594616
)
595617
t.end()
596-
})
618+
})

0 commit comments

Comments
 (0)