Skip to content

Commit cbdcf8b

Browse files
committed
Clarify the 'upgrade' socket argument behaviour
1 parent 82f6c1e commit cbdcf8b

File tree

2 files changed

+34
-30
lines changed

2 files changed

+34
-30
lines changed

doc/api/http.md

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ added: v0.1.94
745745
-->
746746

747747
* `response` {http.IncomingMessage}
748-
* `socket` {stream.Duplex}
748+
* `stream` {stream.Duplex}
749749
* `head` {Buffer}
750750

751751
Emitted each time a server responds to a request with an upgrade. If this
@@ -768,13 +768,13 @@ const server = http.createServer((req, res) => {
768768
res.writeHead(200, { 'Content-Type': 'text/plain' });
769769
res.end('okay');
770770
});
771-
server.on('upgrade', (req, socket, head) => {
772-
socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
771+
server.on('upgrade', (req, stream, head) => {
772+
stream.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
773773
'Upgrade: WebSocket\r\n' +
774774
'Connection: Upgrade\r\n' +
775775
'\r\n');
776776

777-
socket.pipe(socket); // echo back
777+
stream.pipe(stream); // echo back
778778
});
779779

780780
// Now that server is running
@@ -793,9 +793,9 @@ server.listen(1337, '127.0.0.1', () => {
793793
const req = http.request(options);
794794
req.end();
795795

796-
req.on('upgrade', (res, socket, upgradeHead) => {
796+
req.on('upgrade', (res, stream, upgradeHead) => {
797797
console.log('got upgraded!');
798-
socket.end();
798+
stream.end();
799799
process.exit(0);
800800
});
801801
});
@@ -809,13 +809,13 @@ const server = http.createServer((req, res) => {
809809
res.writeHead(200, { 'Content-Type': 'text/plain' });
810810
res.end('okay');
811811
});
812-
server.on('upgrade', (req, socket, head) => {
813-
socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
812+
server.on('upgrade', (req, stream, head) => {
813+
stream.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
814814
'Upgrade: WebSocket\r\n' +
815815
'Connection: Upgrade\r\n' +
816816
'\r\n');
817817

818-
socket.pipe(socket); // echo back
818+
stream.pipe(stream); // echo back
819819
});
820820

821821
// Now that server is running
@@ -834,9 +834,9 @@ server.listen(1337, '127.0.0.1', () => {
834834
const req = http.request(options);
835835
req.end();
836836

837-
req.on('upgrade', (res, socket, upgradeHead) => {
837+
req.on('upgrade', (res, stream, upgradeHead) => {
838838
console.log('got upgraded!');
839-
socket.end();
839+
stream.end();
840840
process.exit(0);
841841
});
842842
});
@@ -1677,10 +1677,11 @@ changes:
16771677
- version: REPLACEME
16781678
pr-url: https://github.com/nodejs/node/pull/60016
16791679
description: Request bodies are no longer exposed raw (unparsed) on the
1680-
socket argument. Instead, only the content after the request
1681-
body will be emitted on the socket, and the parsed request
1682-
body data will be emitted from the request, just like in
1683-
the normal server `request` events.
1680+
socket argument. Instead, if a body is received, the stream
1681+
argument will be a duplex that emits socket content only
1682+
after the request body, while the parsed request body data
1683+
will be emitted from the request, just as in normal server
1684+
`'request'` events.
16841685
- version:
16851686
- v24.9.0
16861687
- v22.21.0
@@ -1696,35 +1697,38 @@ changes:
16961697

16971698
* `request` {http.IncomingMessage} Arguments for the HTTP request, as it is in
16981699
the [`'request'`][] event
1699-
* `socket` {stream.Duplex} Network socket between the server and client
1700+
* `stream` {stream.Duplex} The upgraded stream between the server and client
17001701
* `head` {Buffer} The first packet of the upgraded stream (may be empty)
17011702

17021703
Emitted each time a client's HTTP upgrade request is accepted. By default
17031704
all HTTP upgrade requests are ignored (i.e. only regular `'request'` events
17041705
are emitted, sticking with the normal HTTP request/response flow) unless you
17051706
listen to this event, in which case they are all accepted (i.e. the `'upgrade'`
17061707
event is emitted instead, and future communication must handled directly
1707-
through the raw socket). You can control this more precisely by using the
1708+
through the raw stream). You can control this more precisely by using the
17081709
server `shouldUpgradeCallback` option.
17091710

17101711
Listening to this event is optional and clients cannot insist on a protocol
17111712
change.
17121713

1713-
In the uncommon case that the incoming request has a body, this body will be
1714-
parsed as normal separate to the upgrade stream, and the raw socket data will
1715-
only begin after it has completed. To ensure that reading from the socket isn't
1716-
blocked by waiting for the request body to be read, any reads on the socket
1717-
will start the request body flowing automatically. If you want to read the
1718-
request body, ensure that you do so (i.e. you attach `'data'` listeners)
1719-
before starting to read from the upgraded socket.
1720-
17211714
If an upgrade is accepted by `shouldUpgradeCallback` but no event handler
1722-
is registered then the socket is destroyed, resulting in an immediate
1715+
is registered then the socket will be destroyed, resulting in an immediate
17231716
connection closure for the client.
17241717

1725-
This event is guaranteed to be passed an instance of the {net.Socket} class,
1726-
a subclass of {stream.Duplex}, unless the user specifies a socket
1727-
type other than {net.Socket}.
1718+
In the uncommon case that the incoming request has a body, this body will be
1719+
parsed as normal, separate to the upgrade stream, and the raw stream data will
1720+
only begin after it has completed. To ensure that reading from the stream isn't
1721+
blocked by waiting for the request body to be read, any reads on the stream
1722+
will start the request body flowing automatically. If you want to read the
1723+
request body, ensure that you do so (i.e. you attach `'data'` listeners)
1724+
before starting to read from the upgraded stream.
1725+
1726+
The stream argument will typically be the {net.Socket} instance used by the
1727+
request, but in some cases (such as with a request body) it may be a duplex
1728+
stream (though socket properties are exposed for backward compatibility). If
1729+
required, you can access the raw connection underlying the request via
1730+
[`request.socket`][], which is guaranteed to be an instance of {net.Socket}
1731+
unless the user specified another socket type.
17281732

17291733
### `server.close([callback])`
17301734

lib/_http_server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ class UpgradeStream extends Duplex {
990990

991991
// This stream wraps itself in a proxy which forwards all non-stream
992992
// property lookups back to the underlying socket, for backward
993-
// compatibiility.
993+
// compatibility.
994994
// eslint-disable-next-line no-constructor-return
995995
return new Proxy(this, {
996996
__proto__: null,

0 commit comments

Comments
 (0)