@@ -50,6 +50,7 @@ function isClosed (readyState) {
50
50
* @param {EventTarget } target
51
51
* @param {(...args: ConstructorParameters<typeof Event>) => Event } eventFactory
52
52
* @param {EventInit | undefined } eventInitDict
53
+ * @returns {void }
53
54
*/
54
55
function fireEvent ( e , target , eventFactory = ( type , init ) => new Event ( type , init ) , eventInitDict = { } ) {
55
56
// 1. If eventConstructor is not given, then let eventConstructor be Event.
@@ -72,11 +73,16 @@ function fireEvent (e, target, eventFactory = (type, init) => new Event(type, in
72
73
* @param {import('./websocket').Handler } handler
73
74
* @param {number } type Opcode
74
75
* @param {Buffer } data application data
76
+ * @returns {void }
75
77
*/
76
78
function websocketMessageReceived ( handler , type , data ) {
77
79
handler . onMessage ( type , data )
78
80
}
79
81
82
+ /**
83
+ * @param {Buffer } buffer
84
+ * @returns {ArrayBuffer }
85
+ */
80
86
function toArrayBuffer ( buffer ) {
81
87
if ( buffer . byteLength === buffer . buffer . byteLength ) {
82
88
return buffer . buffer
@@ -89,6 +95,7 @@ function toArrayBuffer (buffer) {
89
95
* @see https://datatracker.ietf.org/doc/html/rfc2616
90
96
* @see https://bugs.chromium.org/p/chromium/issues/detail?id=398407
91
97
* @param {string } protocol
98
+ * @returns {boolean }
92
99
*/
93
100
function isValidSubprotocol ( protocol ) {
94
101
// If present, this value indicates one
@@ -135,6 +142,7 @@ function isValidSubprotocol (protocol) {
135
142
/**
136
143
* @see https://datatracker.ietf.org/doc/html/rfc6455#section-7-4
137
144
* @param {number } code
145
+ * @returns {boolean }
138
146
*/
139
147
function isValidStatusCode ( code ) {
140
148
if ( code >= 1000 && code < 1015 ) {
@@ -152,6 +160,7 @@ function isValidStatusCode (code) {
152
160
* @param {import('./websocket').Handler } handler
153
161
* @param {number } code
154
162
* @param {string|undefined } reason
163
+ * @returns {void }
155
164
*/
156
165
function failWebsocketConnection ( handler , code , reason ) {
157
166
handler . onFail ( code , reason )
@@ -160,6 +169,7 @@ function failWebsocketConnection (handler, code, reason) {
160
169
/**
161
170
* @see https://datatracker.ietf.org/doc/html/rfc6455#section-5.5
162
171
* @param {number } opcode
172
+ * @returns {boolean }
163
173
*/
164
174
function isControlFrame ( opcode ) {
165
175
return (
@@ -169,14 +179,27 @@ function isControlFrame (opcode) {
169
179
)
170
180
}
171
181
182
+ /**
183
+ * @param {number } opcode
184
+ * @returns {boolean }
185
+ */
172
186
function isContinuationFrame ( opcode ) {
173
187
return opcode === opcodes . CONTINUATION
174
188
}
175
189
190
+ /**
191
+ * @param {number } opcode
192
+ * @returns {boolean }
193
+ */
176
194
function isTextBinaryFrame ( opcode ) {
177
195
return opcode === opcodes . TEXT || opcode === opcodes . BINARY
178
196
}
179
197
198
+ /**
199
+ *
200
+ * @param {number } opcode
201
+ * @returns {boolean }
202
+ */
180
203
function isValidOpcode ( opcode ) {
181
204
return isTextBinaryFrame ( opcode ) || isContinuationFrame ( opcode ) || isControlFrame ( opcode )
182
205
}
@@ -210,6 +233,7 @@ function parseExtensions (extensions) {
210
233
* @see https://www.rfc-editor.org/rfc/rfc7692#section-7.1.2.2
211
234
* @description "client-max-window-bits = 1*DIGIT"
212
235
* @param {string } value
236
+ * @returns {boolean }
213
237
*/
214
238
function isValidClientWindowBits ( value ) {
215
239
for ( let i = 0 ; i < value . length ; i ++ ) {
@@ -223,22 +247,22 @@ function isValidClientWindowBits (value) {
223
247
return true
224
248
}
225
249
226
- // https://nodejs.org/api/intl.html#detecting-internationalization-support
227
- const hasIntl = typeof process . versions . icu === 'string'
228
- const fatalDecoder = hasIntl ? new TextDecoder ( 'utf-8' , { fatal : true } ) : undefined
229
-
230
250
/**
231
251
* Converts a Buffer to utf-8, even on platforms without icu.
232
- * @param { Buffer } buffer
252
+ * @type { (buffer: Buffer) => string }
233
253
*/
234
- const utf8Decode = hasIntl
235
- ? fatalDecoder . decode . bind ( fatalDecoder )
236
- : function ( buffer ) {
254
+ const utf8Decode = ( ( ) => {
255
+ if ( typeof process . versions . icu === 'string' ) {
256
+ const fatalDecoder = new TextDecoder ( 'utf-8' , { fatal : true } )
257
+ return fatalDecoder . decode . bind ( fatalDecoder )
258
+ }
259
+ return function ( buffer ) {
237
260
if ( isUtf8 ( buffer ) ) {
238
261
return buffer . toString ( 'utf-8' )
239
262
}
240
263
throw new TypeError ( 'Invalid utf-8 received.' )
241
264
}
265
+ } ) ( )
242
266
243
267
module . exports = {
244
268
isConnecting,
0 commit comments