Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 96f93cc

Browse files
mcollinatargos
authored andcommittedMay 3, 2023
http: remove internal error in assignSocket
Change ServerResponse.assignSocket to not throw an internal error, but an error with its own code. Signed-off-by: Matteo Collina <hello@matteocollina.com> PR-URL: #47723 Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
1 parent 30af5ce commit 96f93cc

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed
 

‎doc/api/errors.md

+7
Original file line numberDiff line numberDiff line change
@@ -1438,6 +1438,12 @@ Status code was outside the regular status code range (100-999).
14381438

14391439
The client has not sent the entire request within the allowed time.
14401440

1441+
<a id="ERR_HTTP_SOCKET_ASSIGNED"></a>
1442+
1443+
### `ERR_HTTP_SOCKET_ASSIGNED`
1444+
1445+
The given [`ServerResponse`][] was already assigned a socket.
1446+
14411447
<a id="ERR_HTTP_SOCKET_ENCODING"></a>
14421448

14431449
### `ERR_HTTP_SOCKET_ENCODING`
@@ -3590,6 +3596,7 @@ The native call from `process.cpuUsage` could not be processed.
35903596
[`Object.getPrototypeOf`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getPrototypeOf
35913597
[`Object.setPrototypeOf`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf
35923598
[`REPL`]: repl.md
3599+
[`ServerResponse`]: http.md#class-httpserverresponse
35933600
[`Writable`]: stream.md#class-streamwritable
35943601
[`child_process`]: child_process.md
35953602
[`cipher.getAuthTag()`]: crypto.md#ciphergetauthtag

‎lib/_http_server.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ const {
7575
ERR_HTTP_HEADERS_SENT,
7676
ERR_HTTP_INVALID_STATUS_CODE,
7777
ERR_HTTP_SOCKET_ENCODING,
78+
ERR_HTTP_SOCKET_ASSIGNED,
7879
ERR_INVALID_ARG_VALUE,
7980
ERR_INVALID_CHAR,
8081
} = codes;
@@ -276,7 +277,9 @@ function onServerResponseClose() {
276277
}
277278

278279
ServerResponse.prototype.assignSocket = function assignSocket(socket) {
279-
assert(!socket._httpMessage);
280+
if (socket._httpMessage) {
281+
throw new ERR_HTTP_SOCKET_ASSIGNED();
282+
}
280283
socket._httpMessage = this;
281284
socket.on('close', onServerResponseClose);
282285
this.socket = socket;

‎lib/internal/errors.js

+2
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,8 @@ E('ERR_HTTP_INVALID_HEADER_VALUE',
11671167
'Invalid value "%s" for header "%s"', TypeError);
11681168
E('ERR_HTTP_INVALID_STATUS_CODE', 'Invalid status code: %s', RangeError);
11691169
E('ERR_HTTP_REQUEST_TIMEOUT', 'Request timeout', Error);
1170+
E('ERR_HTTP_SOCKET_ASSIGNED',
1171+
'ServerResponse has an already assigned socket', Error);
11701172
E('ERR_HTTP_SOCKET_ENCODING',
11711173
'Changing the socket encoding is not allowed per RFC7230 Section 3.', Error);
11721174
E('ERR_HTTP_TRAILER_INVALID',

‎test/parallel/test-http-server-response-standalone.js

+6
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,10 @@ const ws = new Writable({
3131

3232
res.assignSocket(ws);
3333

34+
assert.throws(function() {
35+
res.assignSocket(ws);
36+
}, {
37+
code: 'ERR_HTTP_SOCKET_ASSIGNED'
38+
});
39+
3440
res.end('hello world');

0 commit comments

Comments
 (0)
Please sign in to comment.