feat(websocket): expand diagnostics channel payloads#4888
feat(websocket): expand diagnostics channel payloads#4888islandryu wants to merge 14 commits intonodejs:mainfrom
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4888 +/- ##
==========================================
- Coverage 92.89% 92.84% -0.06%
==========================================
Files 112 112
Lines 35665 35771 +106
==========================================
+ Hits 33131 33211 +80
- Misses 2534 2560 +26 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
lib/web/websocket/sender.js
Outdated
|
|
||
| for (let i = 0; i < payloadData.length; ++i) { | ||
| payloadData[i] = frame[payloadOffset + i] ^ maskKey[i & 3] | ||
| } |
There was a problem hiding this comment.
diagnostics channels are meant to be low overhead. This is the opposite of that
There was a problem hiding this comment.
Changed to use the data before masking.
KhafraDev
left a comment
There was a problem hiding this comment.
I don't like the changes. Diagnostics channels should be unobtrusive.
lib/web/websocket/sender.js
Outdated
| this.#socket.uncork() | ||
| } else { | ||
| // direct writing | ||
| publishFrame(this.#websocket, hint === sendHints.text ? opcodes.TEXT : opcodes.BINARY, true, toBuffer(item, hint)) |
There was a problem hiding this comment.
the toBuffer will make WebSocket slower for the majority of people not using diagnostics channels
There was a problem hiding this comment.
Moved the toBuffer call after the hasSubscribers check.
lib/web/websocket/sender.js
Outdated
| const node = { | ||
| promise: item.arrayBuffer().then((ab) => { | ||
| node.promise = null | ||
| publishFrame(this.#websocket, opcodes.BINARY, true, new Uint8Array(ab)) |
|
@mcollina @KhafraDev |
lib/web/websocket/receiver.js
Outdated
| const rsv3 = buffer[0] & 0x10 | ||
|
|
||
| if (!isValidOpcode(opcode)) { | ||
| this.publishFrameError(new Error('Invalid opcode received')) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I've made the function common.
| channels.socketError.publish(err) | ||
| channels.socketError.publish({ | ||
| error: err, | ||
| websocket: undefined | ||
| }) |
There was a problem hiding this comment.
What was the reason for this change? Wouldn't this be considered a breaking change?
There was a problem hiding this comment.
As you pointed out, there was no need to pass undefined.
Fixed
As you pointed out, this would be a breaking change, so I’ll exclude it from this PR for now.
However, in places like the following, the error is tied to a specific WebSocket, so we need a way to let the subscriber know which WebSocket the error occurred on. I may send this in a separate PR.
https://github.com/islandryu/undici/blob/main/lib/web/websocket/websocket.js#L78
lib/web/websocket/sender.js
Outdated
| publishFrame(this.#websocket, opcodes.BINARY, true, ab, hint) | ||
| node.diagnosticInfo.published = true |
There was a problem hiding this comment.
Why is it published when resolved? Wouldn't that be called before it's actually sent? Also, wouldn't that mess up the order?
There was a problem hiding this comment.
Exactly, it should be published when socket writes
Fixed.
lib/web/websocket/sender.js
Outdated
| channels.frameSent.publish({ | ||
| websocket, | ||
| opcode, | ||
| mask, |
There was a problem hiding this comment.
Since this is client-side only, masked is always true. Is this really necessary?
There was a problem hiding this comment.
Indeed, it didn’t seem like there are cases where the mask changes in Undici.
Fixed
lib/web/websocket/receiver.js
Outdated
| channels.frameReceived.publish({ | ||
| websocket: this.#handler.websocket, | ||
| opcode: this.#info.opcode, | ||
| mask: this.#info.masked, |
There was a problem hiding this comment.
Since this is client-side only, masked is always false. Is this really necessary?
Co-authored-by: tsctx <91457664+tsctx@users.noreply.github.com>
Co-authored-by: tsctx <91457664+tsctx@users.noreply.github.com>
Co-authored-by: tsctx <91457664+tsctx@users.noreply.github.com>
Co-authored-by: tsctx <91457664+tsctx@users.noreply.github.com>
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