-
-
Notifications
You must be signed in to change notification settings - Fork 34.1k
inspector: initial support websocket inspection #59404
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
Merged
nodejs-github-bot
merged 4 commits into
nodejs:main
from
islandryu:feat/websocketInspect
Aug 19, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -206,6 +206,39 @@ function onClientResponseFinish({ request }) { | |
| }); | ||
| } | ||
|
|
||
| // TODO: Move Network.webSocketCreated to the actual creation time of the WebSocket. | ||
| // undici:websocket:open fires when the connection is established, but this results | ||
| // in an inaccurate stack trace. | ||
| function onWebSocketOpen({ websocket }) { | ||
| websocket[kInspectorRequestId] = getNextRequestId(); | ||
| const url = websocket.url.toString(); | ||
| Network.webSocketCreated({ | ||
| requestId: websocket[kInspectorRequestId], | ||
| url, | ||
| }); | ||
| // TODO: Use handshake response data from undici diagnostics when available. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be helpful to link to nodejs/undici#4396 indicating when the handshake data will be available.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the review. I’ve added it. |
||
| // https://github.com/nodejs/undici/pull/4396 | ||
| Network.webSocketHandshakeResponseReceived({ | ||
| requestId: websocket[kInspectorRequestId], | ||
| timestamp: getMonotonicTime(), | ||
| response: { | ||
| status: 101, | ||
| statusText: 'Switching Protocols', | ||
| headers: {}, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| function onWebSocketClose({ websocket }) { | ||
| if (typeof websocket[kInspectorRequestId] !== 'string') { | ||
| return; | ||
| } | ||
| Network.webSocketClosed({ | ||
| requestId: websocket[kInspectorRequestId], | ||
| timestamp: getMonotonicTime(), | ||
| }); | ||
| } | ||
|
|
||
| function enable() { | ||
| dc.subscribe('undici:request:create', onClientRequestStart); | ||
| dc.subscribe('undici:request:error', onClientRequestError); | ||
|
|
@@ -214,6 +247,8 @@ function enable() { | |
| dc.subscribe('undici:request:bodyChunkSent', onClientRequestBodyChunkSent); | ||
| dc.subscribe('undici:request:bodySent', onClientRequestBodySent); | ||
| dc.subscribe('undici:request:bodyChunkReceived', onClientRequestBodyChunkReceived); | ||
| dc.subscribe('undici:websocket:open', onWebSocketOpen); | ||
| dc.subscribe('undici:websocket:close', onWebSocketClose); | ||
| } | ||
|
|
||
| function disable() { | ||
|
|
@@ -224,6 +259,8 @@ function disable() { | |
| dc.unsubscribe('undici:request:bodyChunkSent', onClientRequestBodyChunkSent); | ||
| dc.unsubscribe('undici:request:bodySent', onClientRequestBodySent); | ||
| dc.unsubscribe('undici:request:bodyChunkReceived', onClientRequestBodyChunkReceived); | ||
| dc.unsubscribe('undici:websocket:open', onWebSocketOpen); | ||
| dc.unsubscribe('undici:websocket:close', onWebSocketClose); | ||
| } | ||
|
|
||
| module.exports = { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| 'use strict'; | ||
| const common = require('./index'); | ||
| if (!common.hasCrypto) | ||
| common.skip('missing crypto'); | ||
| const http = require('http'); | ||
| const crypto = require('crypto'); | ||
|
|
||
| class WebSocketServer { | ||
| constructor({ | ||
| port = 0, | ||
| }) { | ||
| this.port = port; | ||
| this.server = http.createServer(); | ||
| this.clients = new Set(); | ||
|
|
||
| this.server.on('upgrade', this.handleUpgrade.bind(this)); | ||
| } | ||
|
|
||
| start() { | ||
| return new Promise((resolve) => { | ||
| this.server.listen(this.port, () => { | ||
| this.port = this.server.address().port; | ||
| resolve(); | ||
| }); | ||
| }).catch((err) => { | ||
| console.error('Failed to start WebSocket server:', err); | ||
| }); | ||
| } | ||
|
|
||
| handleUpgrade(req, socket, head) { | ||
| const key = req.headers['sec-websocket-key']; | ||
| const acceptKey = this.generateAcceptValue(key); | ||
| const responseHeaders = [ | ||
| 'HTTP/1.1 101 Switching Protocols', | ||
| 'Upgrade: websocket', | ||
| 'Connection: Upgrade', | ||
| `Sec-WebSocket-Accept: ${acceptKey}`, | ||
| ]; | ||
|
|
||
| socket.write(responseHeaders.join('\r\n') + '\r\n\r\n'); | ||
| this.clients.add(socket); | ||
|
|
||
| socket.on('data', (buffer) => { | ||
| const opcode = buffer[0] & 0x0f; | ||
|
|
||
| if (opcode === 0x8) { | ||
| socket.end(); | ||
| this.clients.delete(socket); | ||
| return; | ||
| } | ||
|
|
||
| socket.write(this.encodeMessage('Hello from server!')); | ||
| }); | ||
|
|
||
| socket.on('close', () => { | ||
| this.clients.delete(socket); | ||
| }); | ||
|
|
||
| socket.on('error', (err) => { | ||
| console.error('Socket error:', err); | ||
| this.clients.delete(socket); | ||
| }); | ||
| } | ||
|
|
||
| generateAcceptValue(secWebSocketKey) { | ||
| return crypto | ||
| .createHash('sha1') | ||
| .update(secWebSocketKey + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11', 'binary') | ||
| .digest('base64'); | ||
| } | ||
|
|
||
| decodeMessage(buffer) { | ||
| const secondByte = buffer[1]; | ||
| const length = secondByte & 127; | ||
| const maskStart = 2; | ||
| const dataStart = maskStart + 4; | ||
| const masks = buffer.slice(maskStart, dataStart); | ||
| const data = buffer.slice(dataStart, dataStart + length); | ||
| const result = Buffer.alloc(length); | ||
|
|
||
| for (let i = 0; i < length; i++) { | ||
| result[i] = data[i] ^ masks[i % 4]; | ||
| } | ||
|
|
||
| return result.toString(); | ||
| } | ||
|
|
||
| encodeMessage(message) { | ||
| const msgBuffer = Buffer.from(message); | ||
| const length = msgBuffer.length; | ||
| const frame = [0x81]; | ||
|
|
||
| if (length < 126) { | ||
| frame.push(length); | ||
| } else if (length < 65536) { | ||
| frame.push(126, (length >> 8) & 0xff, length & 0xff); | ||
| } else { | ||
| throw new Error('Message too long'); | ||
| } | ||
|
|
||
| return Buffer.concat([Buffer.from(frame), msgBuffer]); | ||
| } | ||
| } | ||
|
|
||
| module.exports = WebSocketServer; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a TODO to indicate that
Network.webSocketCreatedshould be moved to an earlier time point when theWebSocketis created. Currently,undici:websocket:openis fired when theWebSocketconnection is established, which means that the timing is later than expected, and theinitiatorstacktrace is inaccurate.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’ve added the TODO.