Skip to content

Commit 1c783c2

Browse files
committedJan 5, 2018
[major] Rename the 'headers' event to 'upgrade'
Refs: #1082 (comment)
1 parent 9bbc978 commit 1c783c2

File tree

4 files changed

+19
-20
lines changed

4 files changed

+19
-20
lines changed
 

‎doc/ws.md

+8-9
Original file line numberDiff line numberDiff line change
@@ -237,15 +237,6 @@ human-readable string explaining why the connection has been closed.
237237
Emitted when an error occurs. Errors from the underlying `net.Socket` are
238238
forwarded here.
239239

240-
### Event: 'headers'
241-
242-
- `headers` {Object}
243-
- `response` {http.IncomingMessage}
244-
245-
Emitted when response headers are received from the server as part of the
246-
handshake. This allows you to read headers from the server, for example
247-
'set-cookie' headers.
248-
249240
### Event: 'message'
250241

251242
- `data` {String|Buffer|ArrayBuffer|Buffer[]}
@@ -278,6 +269,14 @@ response. This event gives the ability to read the response in order to extract
278269
useful information. If the server sends an invalid response and there isn't a
279270
listener for this event, an error is emitted.
280271

272+
### Event: 'upgrade'
273+
274+
- `response` {http.IncomingMessage}
275+
276+
Emitted when response headers are received from the server as part of the
277+
handshake. This allows you to read headers from the server, for example
278+
'set-cookie' headers.
279+
281280
### websocket.addEventListener(type, listener)
282281

283282
- `type` {String} A string representing the event type to listen for.

‎lib/websocket.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -679,10 +679,10 @@ function initAsClient (address, protocols, options) {
679679
});
680680

681681
this._req.on('upgrade', (res, socket, head) => {
682-
this.emit('headers', res.headers, res);
682+
this.emit('upgrade', res);
683683

684684
//
685-
// The user may have closed the connection from a listener of the `headers`
685+
// The user may have closed the connection from a listener of the `upgrade`
686686
// event.
687687
//
688688
if (this.readyState !== WebSocket.CONNECTING) return;

‎test/websocket-server.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -713,9 +713,9 @@ describe('WebSocketServer', function () {
713713
const port = wss._server.address().port;
714714
const ws = new WebSocket(`ws://localhost:${port}`);
715715

716-
ws.on('headers', (headers) => {
716+
ws.on('upgrade', (res) => {
717717
assert.strictEqual(
718-
headers['sec-websocket-extensions'],
718+
res.headers['sec-websocket-extensions'],
719719
'permessage-deflate; client_max_window_bits=8'
720720
);
721721

‎test/websocket.test.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -402,12 +402,12 @@ describe('WebSocket', function () {
402402
});
403403
});
404404

405-
it("emits a 'headers' event", function (done) {
405+
it("emits an 'upgrade' event", function (done) {
406406
const wss = new WebSocket.Server({ port: 0 }, () => {
407407
const port = wss._server.address().port;
408408
const ws = new WebSocket(`ws://localhost:${port}`);
409-
ws.on('headers', (headers, res) => {
410-
assert.strictEqual(headers, res.headers);
409+
ws.on('upgrade', (res) => {
410+
assert.ok(res instanceof http.IncomingMessage);
411411
wss.close(done);
412412
});
413413
});
@@ -1206,7 +1206,7 @@ describe('WebSocket', function () {
12061206
});
12071207
});
12081208

1209-
it('can be called from a listener of the headers event', function (done) {
1209+
it("can be called from a listener of the 'upgrade' event", function (done) {
12101210
const wss = new WebSocket.Server({ port: 0 }, () => {
12111211
const port = wss._server.address().port;
12121212
const ws = new WebSocket(`ws://localhost:${port}`);
@@ -1220,7 +1220,7 @@ describe('WebSocket', function () {
12201220
);
12211221
ws.on('close', () => wss.close(done));
12221222
});
1223-
ws.on('headers', () => ws.close());
1223+
ws.on('upgrade', () => ws.close());
12241224
});
12251225
});
12261226

@@ -1479,7 +1479,7 @@ describe('WebSocket', function () {
14791479
});
14801480
});
14811481

1482-
it('can be called from a listener of the headers event', function (done) {
1482+
it("can be called from a listener of the 'upgrade' event", function (done) {
14831483
const wss = new WebSocket.Server({ port: 0 }, () => {
14841484
const port = wss._server.address().port;
14851485
const ws = new WebSocket(`ws://localhost:${port}`);
@@ -1493,7 +1493,7 @@ describe('WebSocket', function () {
14931493
);
14941494
ws.on('close', () => wss.close(done));
14951495
});
1496-
ws.on('headers', () => ws.terminate());
1496+
ws.on('upgrade', () => ws.terminate());
14971497
});
14981498
});
14991499

0 commit comments

Comments
 (0)
Please sign in to comment.