3
3
const { webidl } = require ( '../fetch/webidl' )
4
4
const { URLSerializer } = require ( '../fetch/data-url' )
5
5
const { getGlobalOrigin } = require ( '../fetch/global' )
6
- const { staticPropertyDescriptors, states, opcodes, emptyBuffer } = require ( './constants' )
6
+ const { staticPropertyDescriptors, states, sentCloseFrameState , opcodes, emptyBuffer } = require ( './constants' )
7
7
const {
8
8
kWebSocketURL,
9
9
kReadyState,
@@ -13,7 +13,15 @@ const {
13
13
kSentClose,
14
14
kByteParser
15
15
} = require ( './symbols' )
16
- const { isEstablished, isClosing, isValidSubprotocol, failWebsocketConnection, fireEvent } = require ( './util' )
16
+ const {
17
+ isConnecting,
18
+ isEstablished,
19
+ isClosed,
20
+ isClosing,
21
+ isValidSubprotocol,
22
+ failWebsocketConnection,
23
+ fireEvent
24
+ } = require ( './util' )
17
25
const { establishWebSocketConnection } = require ( './connection' )
18
26
const { WebsocketFrameSend } = require ( './frame' )
19
27
const { ByteParser } = require ( './receiver' )
@@ -132,6 +140,8 @@ class WebSocket extends EventTarget {
132
140
// be CONNECTING (0).
133
141
this [ kReadyState ] = WebSocket . CONNECTING
134
142
143
+ this [ kSentClose ] = sentCloseFrameState . NOT_SENT
144
+
135
145
// The extensions attribute must initially return the empty string.
136
146
137
147
// The protocol attribute must initially return the empty string.
@@ -184,7 +194,7 @@ class WebSocket extends EventTarget {
184
194
}
185
195
186
196
// 3. Run the first matching steps from the following list:
187
- if ( this [ kReadyState ] === WebSocket . CLOSING || this [ kReadyState ] === WebSocket . CLOSED ) {
197
+ if ( isClosing ( this ) || isClosed ( this ) ) {
188
198
// If this's ready state is CLOSING (2) or CLOSED (3)
189
199
// Do nothing.
190
200
} else if ( ! isEstablished ( this ) ) {
@@ -193,7 +203,7 @@ class WebSocket extends EventTarget {
193
203
// to CLOSING (2).
194
204
failWebsocketConnection ( this , 'Connection was closed before it was established.' )
195
205
this [ kReadyState ] = WebSocket . CLOSING
196
- } else if ( ! isClosing ( this ) ) {
206
+ } else if ( this [ kSentClose ] === sentCloseFrameState . NOT_SENT ) {
197
207
// If the WebSocket closing handshake has not yet been started
198
208
// Start the WebSocket closing handshake and set this's ready
199
209
// state to CLOSING (2).
@@ -204,6 +214,8 @@ class WebSocket extends EventTarget {
204
214
// - If reason is also present, then reasonBytes must be
205
215
// provided in the Close message after the status code.
206
216
217
+ this [ kSentClose ] = sentCloseFrameState . PROCESSING
218
+
207
219
const frame = new WebsocketFrameSend ( )
208
220
209
221
// If neither code nor reason is present, the WebSocket Close
@@ -230,7 +242,7 @@ class WebSocket extends EventTarget {
230
242
231
243
socket . write ( frame . createFrame ( opcodes . CLOSE ) , ( err ) => {
232
244
if ( ! err ) {
233
- this [ kSentClose ] = true
245
+ this [ kSentClose ] = sentCloseFrameState . SENT
234
246
}
235
247
} )
236
248
@@ -258,7 +270,7 @@ class WebSocket extends EventTarget {
258
270
259
271
// 1. If this's ready state is CONNECTING, then throw an
260
272
// "InvalidStateError" DOMException.
261
- if ( this [ kReadyState ] === WebSocket . CONNECTING ) {
273
+ if ( isConnecting ( this ) ) {
262
274
throw new DOMException ( 'Sent before connected.' , 'InvalidStateError' )
263
275
}
264
276
0 commit comments