Skip to content

Commit ebe6b15

Browse files
authored
chore: add jsdoc for lib/web/websocket/util.js (#3563)
1 parent 7f0f6c9 commit ebe6b15

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

lib/web/websocket/util.js

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ function isClosed (readyState) {
5050
* @param {EventTarget} target
5151
* @param {(...args: ConstructorParameters<typeof Event>) => Event} eventFactory
5252
* @param {EventInit | undefined} eventInitDict
53+
* @returns {void}
5354
*/
5455
function fireEvent (e, target, eventFactory = (type, init) => new Event(type, init), eventInitDict = {}) {
5556
// 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
7273
* @param {import('./websocket').Handler} handler
7374
* @param {number} type Opcode
7475
* @param {Buffer} data application data
76+
* @returns {void}
7577
*/
7678
function websocketMessageReceived (handler, type, data) {
7779
handler.onMessage(type, data)
7880
}
7981

82+
/**
83+
* @param {Buffer} buffer
84+
* @returns {ArrayBuffer}
85+
*/
8086
function toArrayBuffer (buffer) {
8187
if (buffer.byteLength === buffer.buffer.byteLength) {
8288
return buffer.buffer
@@ -89,6 +95,7 @@ function toArrayBuffer (buffer) {
8995
* @see https://datatracker.ietf.org/doc/html/rfc2616
9096
* @see https://bugs.chromium.org/p/chromium/issues/detail?id=398407
9197
* @param {string} protocol
98+
* @returns {boolean}
9299
*/
93100
function isValidSubprotocol (protocol) {
94101
// If present, this value indicates one
@@ -135,6 +142,7 @@ function isValidSubprotocol (protocol) {
135142
/**
136143
* @see https://datatracker.ietf.org/doc/html/rfc6455#section-7-4
137144
* @param {number} code
145+
* @returns {boolean}
138146
*/
139147
function isValidStatusCode (code) {
140148
if (code >= 1000 && code < 1015) {
@@ -152,6 +160,7 @@ function isValidStatusCode (code) {
152160
* @param {import('./websocket').Handler} handler
153161
* @param {number} code
154162
* @param {string|undefined} reason
163+
* @returns {void}
155164
*/
156165
function failWebsocketConnection (handler, code, reason) {
157166
handler.onFail(code, reason)
@@ -160,6 +169,7 @@ function failWebsocketConnection (handler, code, reason) {
160169
/**
161170
* @see https://datatracker.ietf.org/doc/html/rfc6455#section-5.5
162171
* @param {number} opcode
172+
* @returns {boolean}
163173
*/
164174
function isControlFrame (opcode) {
165175
return (
@@ -169,14 +179,27 @@ function isControlFrame (opcode) {
169179
)
170180
}
171181

182+
/**
183+
* @param {number} opcode
184+
* @returns {boolean}
185+
*/
172186
function isContinuationFrame (opcode) {
173187
return opcode === opcodes.CONTINUATION
174188
}
175189

190+
/**
191+
* @param {number} opcode
192+
* @returns {boolean}
193+
*/
176194
function isTextBinaryFrame (opcode) {
177195
return opcode === opcodes.TEXT || opcode === opcodes.BINARY
178196
}
179197

198+
/**
199+
*
200+
* @param {number} opcode
201+
* @returns {boolean}
202+
*/
180203
function isValidOpcode (opcode) {
181204
return isTextBinaryFrame(opcode) || isContinuationFrame(opcode) || isControlFrame(opcode)
182205
}
@@ -210,6 +233,7 @@ function parseExtensions (extensions) {
210233
* @see https://www.rfc-editor.org/rfc/rfc7692#section-7.1.2.2
211234
* @description "client-max-window-bits = 1*DIGIT"
212235
* @param {string} value
236+
* @returns {boolean}
213237
*/
214238
function isValidClientWindowBits (value) {
215239
for (let i = 0; i < value.length; i++) {
@@ -223,22 +247,22 @@ function isValidClientWindowBits (value) {
223247
return true
224248
}
225249

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-
230250
/**
231251
* Converts a Buffer to utf-8, even on platforms without icu.
232-
* @param {Buffer} buffer
252+
* @type {(buffer: Buffer) => string}
233253
*/
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) {
237260
if (isUtf8(buffer)) {
238261
return buffer.toString('utf-8')
239262
}
240263
throw new TypeError('Invalid utf-8 received.')
241264
}
265+
})()
242266

243267
module.exports = {
244268
isConnecting,

0 commit comments

Comments
 (0)