diff --git a/package.json b/package.json index b999244..9a61fe4 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "dependencies": { "relative-url": "^1.0.2", "safe-buffer": "^5.1.1", - "ws": "^1.1.0" + "ws": "^7.0.0" }, "devDependencies": { "mapleTree": "^0.5.1", diff --git a/ready.js b/ready.js index afb86b4..a19edcc 100644 --- a/ready.js +++ b/ready.js @@ -1,3 +1,4 @@ +'use strict' module.exports = function(socket, callback) { var remove = socket && (socket.removeEventListener || socket.removeListener); diff --git a/server.js b/server.js index 123b10b..d2cb35a 100644 --- a/server.js +++ b/server.js @@ -1,6 +1,6 @@ +'use strict' var ws = require('./') var WebSocket = require('ws') -var url = require('url') var http = require('http') var https = require('https') @@ -25,7 +25,7 @@ module.exports = !WebSocket.Server ? null : function (opts, onConnection) { }) } - var server = opts.server || + server = opts.server || (opts.key && opts.cert ? https.createServer(opts) : http.createServer()) var wsServer = new WebSocket.Server({ @@ -38,9 +38,10 @@ module.exports = !WebSocket.Server ? null : function (opts, onConnection) { proxy(server, 'request') proxy(server, 'close') - wsServer.on('connection', function (socket) { + wsServer.on('connection', function (socket, req) { + socket.upgradeReq = req // mix: kinda gross hack to preserve the API of duplex.js, but might confuse users... var stream = ws(socket) - stream.remoteAddress = socket.upgradeReq.socket.remoteAddress + stream.remoteAddress = req.connection.remoteAddress emitter.emit('connection', stream) }) @@ -60,8 +61,3 @@ module.exports = !WebSocket.Server ? null : function (opts, onConnection) { emitter.address = server.address.bind(server) return emitter } - - - - - diff --git a/sink.js b/sink.js index 349dfbe..3b15464 100644 --- a/sink.js +++ b/sink.js @@ -1,3 +1,4 @@ +'use strict' var ready = require('./ready'); /** @@ -40,7 +41,7 @@ module.exports = function(socket, opts) { // socket ready? ready(socket, function(end) { if (end) { - return read(end, function () {}); + return read(end === true ? true : end.error, function () {}); } socket.send(data); nextTick(function() { diff --git a/source.js b/source.js index 887bd6e..b8fa2d6 100644 --- a/source.js +++ b/source.js @@ -1,3 +1,4 @@ +'use strict' /** ### `source(socket)` @@ -46,10 +47,10 @@ module.exports = function(socket, cb) { ended = evt; if(!started) { started = true - cb && cb(evt) + cb && cb(evt.error) } if (receiver) { - receiver(ended) + receiver(ended.error) } }); diff --git a/test/error.js b/test/error.js index d402165..fea0aae 100644 --- a/test/error.js +++ b/test/error.js @@ -1,6 +1,5 @@ var test = require('tape'); var WebSocket = require('ws'); -var endpoint = require('./helpers/wsurl') + '/read'; var pull = require('pull-stream'); var ws = require('../'); @@ -28,7 +27,6 @@ test('test error', function (t) { _err = err }) ) - }) //connect to a server that does not exist, and check that it errors. @@ -44,8 +42,14 @@ test('test error', function (t) { }) - test('close', function (t) { server.close() t.end() }) + + + + + + + diff --git a/test/server.js b/test/server.js index b6361a5..34b5135 100644 --- a/test/server.js +++ b/test/server.js @@ -3,8 +3,8 @@ var mapleTree = require('mapleTree'); var port = process.env.ZUUL_PORT || process.env.PORT || 3000; module.exports = function () { -var router = new mapleTree.RouteTree(); -var wss = new WebSocketServer({ port: port }); + var router = new mapleTree.RouteTree(); + var wss = new WebSocketServer({ port: port }); router.define('/read', function(ws) { var values = ['a', 'b', 'c', 'd']; @@ -27,8 +27,8 @@ var wss = new WebSocketServer({ port: port }); }); }); - wss.on('connection', function(ws) { - var match = router.match(ws.upgradeReq.url); + wss.on('connection', function(ws, req) { + var match = router.match(req.url); if (match && typeof match.fn == 'function') { match.fn(ws); }