Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade to ws@7 #26

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions ready.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict'
module.exports = function(socket, callback) {
var remove = socket && (socket.removeEventListener || socket.removeListener);

Expand Down
14 changes: 5 additions & 9 deletions server.js
Original file line number Diff line number Diff line change
@@ -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')

Expand All @@ -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({
Expand All @@ -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...
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok this line gets the tests passing for :

  • this module
  • ssb-server/test/bin.js

This fix would stop us needing a breaking change ... but feels a bit ech? don't know how much duplex.js is getting used directly

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a cleaner option would be to pass the headers directly to duplex (I don't think anything uses it directly, but client.js and server.js need it) or, at least, to pass just the origin header. A WebSocket object created in the browser doesn't even have a headers property, but multiserver sets the address from the address you passed it, in that case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh I also ran this against the multiserver tests and found another place it breaks.
however, in this case, I think it's multiserver that is causing the problem...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you mean we should change the code to something like :

      var opts = {
        header: req.headers,
        remoteAddress: req.connection.remoteAddress
      }
      var stream = ws(socket, opts)   // here ws is duplex.js
      emitter.emit('connection', stream)

var stream = ws(socket)
stream.remoteAddress = socket.upgradeReq.socket.remoteAddress
stream.remoteAddress = req.connection.remoteAddress
emitter.emit('connection', stream)
})

Expand All @@ -60,8 +61,3 @@ module.exports = !WebSocket.Server ? null : function (opts, onConnection) {
emitter.address = server.address.bind(server)
return emitter
}





3 changes: 2 additions & 1 deletion sink.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict'
var ready = require('./ready');

/**
Expand Down Expand Up @@ -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() {
Expand Down
5 changes: 3 additions & 2 deletions source.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict'
/**
### `source(socket)`

Expand Down Expand Up @@ -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)
}
});

Expand Down
10 changes: 7 additions & 3 deletions test/error.js
Original file line number Diff line number Diff line change
@@ -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('../');

Expand Down Expand Up @@ -28,7 +27,6 @@ test('test error', function (t) {
_err = err
})
)

})

//connect to a server that does not exist, and check that it errors.
Expand All @@ -44,8 +42,14 @@ test('test error', function (t) {

})


test('close', function (t) {
server.close()
t.end()
})







8 changes: 4 additions & 4 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand All @@ -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);
}
Expand Down