Skip to content

Commit f3cbe48

Browse files
authored
fix websocketstream bugs (#5863)
* fix websocketstream extension getter * fix websocketstream bugs * fix websocketstream bugs * fix websocket bugs * fixup * skip slowest tests * ubuntu needs 30 mins to run :(
1 parent 1d4ea0a commit f3cbe48

11 files changed

Lines changed: 213 additions & 740 deletions

File tree

‎.github/workflows/nodejs.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ permissions:
2424
jobs:
2525
test:
2626
name: Test ${{ inputs.codecov == true && 'and Coverage ' || '' }}with Node.js ${{ inputs.node-version }} on ${{ inputs.runs-on }}
27-
timeout-minutes: ${{ (inputs.runs-on == 'windows-latest' || inputs.runs-on == 'macos-latest') && 30 || 20 }}
27+
timeout-minutes: ${{ 30 }}
2828
runs-on: ${{ inputs.runs-on }}
2929
steps:
3030
- name: Checkout Repository

‎lib/web/websocket/connection.js‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,15 @@ function establishWebSocketConnection (url, protocols, client, handler, options)
159159
// E914-47DA-95CA-C5AB0DC85B11" but ignoring any leading and
160160
// trailing whitespace, the client MUST _Fail the WebSocket
161161
// Connection_.
162-
const secWSAccept = response.headersList.get('Sec-WebSocket-Accept')
163-
const digest = crypto.hash('sha1', keyValue + uid, 'base64')
164-
if (secWSAccept !== digest) {
165-
failWebsocketConnection(handler, 1002, 'Incorrect hash received in Sec-WebSocket-Accept header.')
166-
return
162+
// For H2, implementations "do not do the processing of the Sec-WebSocket-Key and
163+
// Sec-WebSocket-Accept header fields". https://datatracker.ietf.org/doc/html/rfc8441#section-5
164+
if (response.socket.session == null) {
165+
const secWSAccept = response.headersList.get('Sec-WebSocket-Accept')
166+
const digest = crypto.hash('sha1', keyValue + uid, 'base64')
167+
if (secWSAccept !== digest) {
168+
failWebsocketConnection(handler, 1002, 'Incorrect hash received in Sec-WebSocket-Accept header.')
169+
return
170+
}
167171
}
168172

169173
// 5. If the response includes a |Sec-WebSocket-Extensions| header
@@ -206,6 +210,13 @@ function establishWebSocketConnection (url, protocols, client, handler, options)
206210
}
207211
}
208212

213+
// For H2, "Orderly TCP-level closures are represented as END_STREAM flags", so
214+
// end our side of the stream when the server ends its side, as a TCP socket would.
215+
// https://datatracker.ietf.org/doc/html/rfc8441#section-5
216+
if (response.socket.session != null) {
217+
response.socket.allowHalfOpen = false
218+
}
219+
209220
response.socket.on('data', handler.onSocketData)
210221
response.socket.on('close', handler.onSocketClose)
211222
response.socket.on('error', handler.onSocketError)

‎lib/web/websocket/stream/websocketstream.js‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ class WebSocketStream {
6262

6363
this.#handler.socket.destroy()
6464
},
65-
onSocketClose: () => this.#onSocketClose(),
65+
// When the WebSocket connection is closed for a WebSocketStream stream, possibly cleanly, the user agent must
66+
// queue a global task on the WebSocket task source given stream ’s relevant global object to run the following substeps:
67+
onSocketClose: () => queueMicrotask(() => this.#onSocketClose()),
6668
onPing: () => {},
6769
onPong: () => {},
6870

@@ -246,11 +248,15 @@ class WebSocketStream {
246248
const frame = new WebsocketFrameSend(data)
247249

248250
this.#handler.socket.write(frame.createFrame(opcode), () => {
251+
// 6.3. Queue a global task on the WebSocket task source given stream ’s relevant global object to resolve promise with undefined.
249252
promise.resolve(undefined)
250253
})
254+
} else {
255+
// 6.3. Queue a global task on the WebSocket task source given stream ’s relevant global object to resolve promise with undefined.
256+
promise.resolve(undefined)
251257
}
252258

253-
// 6.3. Queue a global task on the WebSocket task source given stream ’s relevant global object to resolve promise with undefined.
259+
// 7. Return promise.
254260
return promise.promise
255261
}
256262

@@ -278,7 +284,7 @@ class WebSocketStream {
278284
// This is done in the opening handshake.
279285

280286
// 3. Let extensions be the extensions in use .
281-
const extensions = parsedExtensions ?? ''
287+
const extensions = response.headersList.get('sec-websocket-extensions') ?? ''
282288

283289
// 4. Let protocol be the subprotocol in use .
284290
const protocol = response.headersList.get('sec-websocket-protocol') ?? ''
@@ -291,6 +297,7 @@ class WebSocketStream {
291297
start: (controller) => {
292298
this.#readableStreamController = controller
293299
},
300+
pull: () => this.#pull(),
294301
cancel: (reason) => this.#cancel(reason)
295302
})
296303

@@ -350,6 +357,9 @@ class WebSocketStream {
350357
this.#readableStreamController.enqueue(chunk)
351358

352359
// 4. Apply backpressure to the WebSocket.
360+
if (this.#readableStreamController.desiredSize <= 0) {
361+
this.#handler.socket.pause()
362+
}
353363
}
354364

355365
/** @type {import('../websocket').Handler['onSocketClose']} */
@@ -383,7 +393,7 @@ class WebSocketStream {
383393
// 1006.
384394
let code = result?.code ?? 1005
385395

386-
if (!this.#handler.closeState.has(sentCloseFrameState.SENT) && !this.#handler.closeState.has(sentCloseFrameState.RECEIVED)) {
396+
if (!this.#handler.closeState.has(sentCloseFrameState.RECEIVED)) {
387397
code = 1006
388398
}
389399

@@ -441,6 +451,11 @@ class WebSocketStream {
441451
closeWebSocketConnection(this.#handler, code, reasonString)
442452
}
443453

454+
// To pull bytes from a WebSocketStream stream , if stream is currently applying backpressure, release backpressure.
455+
#pull () {
456+
this.#handler.socket.resume()
457+
}
458+
444459
// To cancel a WebSocketStream stream given reason , close using reason giving stream and reason .
445460
#cancel (reason) {
446461
this.#closeUsingReason(reason)

0 commit comments

Comments
 (0)