Skip to content

feat(websocket): expand diagnostics channel payloads#4888

Open
islandryu wants to merge 3 commits intonodejs:mainfrom
islandryu:expandWebsocketDiagnostics
Open

feat(websocket): expand diagnostics channel payloads#4888
islandryu wants to merge 3 commits intonodejs:mainfrom
islandryu:expandWebsocketDiagnostics

Conversation

@islandryu
Copy link
Member

This relates to...

Fixes: #4673

Rationale

I would like to extend the WebSocket events in order to send CDP data.

Changes

Introduces undici:websocket:created, undici:websocket:handshakeRequest, frameSent, frameReceived, and frameError coverage.

Features

Bug Fixes

N/A

Breaking Changes and Deprecations

Status

@codecov-commenter
Copy link

codecov-commenter commented Mar 13, 2026

Codecov Report

❌ Patch coverage is 68.87417% with 47 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.79%. Comparing base (0d7ec33) to head (aa50095).
⚠️ Report is 7 commits behind head on main.

Files with missing lines Patch % Lines
lib/core/diagnostics.js 13.51% 32 Missing ⚠️
lib/web/websocket/sender.js 86.53% 7 Missing ⚠️
lib/web/websocket/stream/websocketstream.js 0.00% 4 Missing ⚠️
lib/web/websocket/websocket.js 71.42% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4888      +/-   ##
==========================================
- Coverage   92.89%   92.79%   -0.11%     
==========================================
  Files         112      112              
  Lines       35665    35774     +109     
==========================================
+ Hits        33131    33195      +64     
- Misses       2534     2579      +45     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@metcoder95 metcoder95 requested a review from KhafraDev March 13, 2026 11:21
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm


for (let i = 0; i < payloadData.length; ++i) {
payloadData[i] = frame[payloadOffset + i] ^ maskKey[i & 3]
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

diagnostics channels are meant to be low overhead. This is the opposite of that

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to use the data before masking.

Copy link
Member

@KhafraDev KhafraDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the changes. Diagnostics channels should be unobtrusive.

this.#socket.uncork()
} else {
// direct writing
publishFrame(this.#websocket, hint === sendHints.text ? opcodes.TEXT : opcodes.BINARY, true, toBuffer(item, hint))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the toBuffer will make WebSocket slower for the majority of people not using diagnostics channels

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the toBuffer call after the hasSubscribers check.

const node = {
promise: item.arrayBuffer().then((ab) => {
node.promise = null
publishFrame(this.#websocket, opcodes.BINARY, true, new Uint8Array(ab))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

@islandryu islandryu requested review from KhafraDev and mcollina March 20, 2026 02:46
@islandryu
Copy link
Member Author

@mcollina @KhafraDev
Could you take a look?

const rsv3 = buffer[0] & 0x10

if (!isValidOpcode(opcode)) {
this.publishFrameError(new Error('Invalid opcode received'))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't there a cleaner way to handle this? Repeating these same lines before every failWebsocketConnection call seems like it could lead to maintenance issues.

Comment on lines -60 to +63
channels.socketError.publish(err)
channels.socketError.publish({
error: err,
websocket: undefined
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the reason for this change? Wouldn't this be considered a breaking change?

this.#socket.uncork()
} else {
// direct writing
publishFrame(this.#websocket, hint === sendHints.text ? opcodes.TEXT : opcodes.BINARY, true, item, hint)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
publishFrame(this.#websocket, hint === sendHints.text ? opcodes.TEXT : opcodes.BINARY, true, item, hint)
publishFrame(this.#websocket, opcodes.BINARY, true, item, hint)

unreachable branch

Comment on lines +75 to +76
publishFrame(this.#websocket, opcodes.BINARY, true, ab, hint)
node.diagnosticInfo.published = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is it published when resolved? Wouldn't that be called before it's actually sent? Also, wouldn't that mess up the order?

channels.frameSent.publish({
websocket,
opcode,
mask,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is client-side only, masked is always true. Is this really necessary?

channels.frameReceived.publish({
websocket: this.#handler.websocket,
opcode: this.#info.opcode,
mask: this.#info.masked,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is client-side only, masked is always false. Is this really necessary?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add WebSocket lifecycle, frame, and frame-error diagnostic events for DevTools Protocol support

5 participants