Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 64fc625

Browse filesBrowse files
islandryuaduh95
authored andcommitted
inspector: support handshake response for websocket inspection
PR-URL: #60225 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 8c8525c commit 64fc625
Copy full SHA for 64fc625

File tree

Expand file treeCollapse file tree

3 files changed

+17
-8
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+17
-8
lines changed
Open diff view settings
Collapse file

‎lib/internal/inspector/network_undici.js‎

Copy file name to clipboardExpand all lines: lib/internal/inspector/network_undici.js
+2-8Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,23 +209,17 @@ function onClientResponseFinish({ request }) {
209209
// TODO: Move Network.webSocketCreated to the actual creation time of the WebSocket.
210210
// undici:websocket:open fires when the connection is established, but this results
211211
// in an inaccurate stack trace.
212-
function onWebSocketOpen({ websocket }) {
212+
function onWebSocketOpen({ websocket, handshakeResponse }) {
213213
websocket[kInspectorRequestId] = getNextRequestId();
214214
const url = websocket.url.toString();
215215
Network.webSocketCreated({
216216
requestId: websocket[kInspectorRequestId],
217217
url,
218218
});
219-
// TODO: Use handshake response data from undici diagnostics when available.
220-
// https://github.com/nodejs/undici/pull/4396
221219
Network.webSocketHandshakeResponseReceived({
222220
requestId: websocket[kInspectorRequestId],
223221
timestamp: getMonotonicTime(),
224-
response: {
225-
status: 101,
226-
statusText: 'Switching Protocols',
227-
headers: {},
228-
},
222+
response: handshakeResponse,
229223
});
230224
}
231225

Collapse file

‎test/common/websocket-server.js‎

Copy file name to clipboardExpand all lines: test/common/websocket-server.js
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ class WebSocketServer {
99
constructor({
1010
port = 0,
1111
server,
12+
customHandleUpgradeHeaders = [],
1213
}) {
1314
this.port = port;
1415
this.server = server || http.createServer();
1516
this.clients = new Set();
17+
this.customHandleUpgradeHeaders = customHandleUpgradeHeaders;
1618

1719
this.server.on('upgrade', this.handleUpgrade.bind(this));
1820
}
@@ -36,6 +38,7 @@ class WebSocketServer {
3638
'Upgrade: websocket',
3739
'Connection: Upgrade',
3840
`Sec-WebSocket-Accept: ${acceptKey}`,
41+
...this.customHandleUpgradeHeaders,
3942
];
4043

4144
socket.write(responseHeaders.join('\r\n') + '\r\n\r\n');
Collapse file

‎test/parallel/test-inspector-network-websocket.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-inspector-network-websocket.js
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,15 @@ function findFrameInInitiator(regex, initiator) {
2929

3030
async function test() {
3131
await session.post('Network.enable');
32+
33+
const CUSTOM_HEADER_NAME = 'X-Custom-Header';
34+
const CUSTOM_HEADER_VALUE = 'CustomHeaderValue';
35+
3236
const server = new WebSocketServer({
3337
responseError: true,
38+
customHandleUpgradeHeaders: [
39+
`${CUSTOM_HEADER_NAME}: ${CUSTOM_HEADER_VALUE}`,
40+
]
3441
});
3542
await server.start();
3643
const url = `ws://127.0.0.1:${server.port}/`;
@@ -49,6 +56,11 @@ async function test() {
4956
assert.strictEqual(message.params.requestId, requestId);
5057
assert.strictEqual(message.params.response.status, 101);
5158
assert.strictEqual(message.params.response.statusText, 'Switching Protocols');
59+
assert.strictEqual(message.params.response.headers.upgrade, 'websocket');
60+
assert.strictEqual(message.params.response.headers.connection, 'Upgrade');
61+
assert.ok(message.params.response.headers['sec-websocket-accept']);
62+
assert.ok(message.params.response.headers['sec-websocket-accept'].length > 0);
63+
assert.strictEqual(message.params.response.headers[CUSTOM_HEADER_NAME.toLowerCase()], CUSTOM_HEADER_VALUE);
5264
assert.strictEqual(typeof message.params.timestamp, 'number');
5365
socket.close();
5466
}));

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.