@@ -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