undici 6.19.7 → 7.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. package/README.md +5 -9
  2. package/docs/docs/api/Agent.md +0 -3
  3. package/docs/docs/api/Client.md +0 -2
  4. package/docs/docs/api/Dispatcher.md +204 -6
  5. package/docs/docs/api/EnvHttpProxyAgent.md +0 -1
  6. package/docs/docs/api/Fetch.md +1 -0
  7. package/docs/docs/api/Pool.md +0 -1
  8. package/docs/docs/api/RetryHandler.md +1 -1
  9. package/index.js +0 -4
  10. package/lib/api/api-connect.js +3 -1
  11. package/lib/api/api-pipeline.js +3 -4
  12. package/lib/api/api-request.js +29 -46
  13. package/lib/api/api-stream.js +36 -49
  14. package/lib/api/api-upgrade.js +5 -3
  15. package/lib/api/readable.js +71 -27
  16. package/lib/core/connect.js +39 -24
  17. package/lib/core/errors.js +17 -4
  18. package/lib/core/request.js +7 -5
  19. package/lib/core/symbols.js +0 -1
  20. package/lib/core/tree.js +6 -0
  21. package/lib/core/util.js +1 -11
  22. package/lib/dispatcher/agent.js +3 -17
  23. package/lib/dispatcher/balanced-pool.js +27 -11
  24. package/lib/dispatcher/client-h1.js +44 -39
  25. package/lib/dispatcher/client.js +3 -27
  26. package/lib/dispatcher/dispatcher-base.js +2 -34
  27. package/lib/dispatcher/dispatcher.js +3 -24
  28. package/lib/dispatcher/pool.js +3 -6
  29. package/lib/dispatcher/proxy-agent.js +3 -6
  30. package/lib/handler/decorator-handler.js +24 -0
  31. package/lib/handler/redirect-handler.js +9 -0
  32. package/lib/handler/retry-handler.js +22 -3
  33. package/lib/interceptor/dump.js +2 -2
  34. package/lib/interceptor/redirect.js +11 -14
  35. package/lib/interceptor/response-error.js +89 -0
  36. package/lib/llhttp/constants.d.ts +97 -0
  37. package/lib/llhttp/constants.js +412 -192
  38. package/lib/llhttp/constants.js.map +1 -0
  39. package/lib/llhttp/llhttp-wasm.js +11 -1
  40. package/lib/llhttp/llhttp_simd-wasm.js +11 -1
  41. package/lib/llhttp/utils.d.ts +2 -0
  42. package/lib/llhttp/utils.js +9 -9
  43. package/lib/llhttp/utils.js.map +1 -0
  44. package/lib/mock/mock-client.js +2 -2
  45. package/lib/mock/mock-pool.js +2 -2
  46. package/lib/mock/mock-symbols.js +1 -0
  47. package/lib/util/timers.js +324 -44
  48. package/lib/web/cookies/index.js +15 -13
  49. package/lib/web/cookies/parse.js +2 -2
  50. package/lib/web/eventsource/eventsource-stream.js +9 -8
  51. package/lib/web/eventsource/eventsource.js +10 -6
  52. package/lib/web/fetch/body.js +31 -11
  53. package/lib/web/fetch/data-url.js +1 -1
  54. package/lib/web/fetch/formdata-parser.js +1 -2
  55. package/lib/web/fetch/formdata.js +28 -37
  56. package/lib/web/fetch/headers.js +1 -1
  57. package/lib/web/fetch/index.js +7 -8
  58. package/lib/web/fetch/request.js +11 -28
  59. package/lib/web/fetch/response.js +13 -41
  60. package/lib/web/fetch/symbols.js +0 -1
  61. package/lib/web/fetch/util.js +3 -12
  62. package/lib/web/fetch/webidl.js +73 -62
  63. package/lib/web/websocket/connection.js +26 -174
  64. package/lib/web/websocket/constants.js +1 -1
  65. package/lib/web/websocket/frame.js +45 -3
  66. package/lib/web/websocket/receiver.js +28 -26
  67. package/lib/web/websocket/sender.js +18 -13
  68. package/lib/web/websocket/util.js +20 -74
  69. package/lib/web/websocket/websocket.js +294 -70
  70. package/package.json +16 -29
  71. package/scripts/strip-comments.js +3 -1
  72. package/types/agent.d.ts +7 -7
  73. package/types/api.d.ts +24 -24
  74. package/types/balanced-pool.d.ts +11 -11
  75. package/types/client.d.ts +11 -12
  76. package/types/diagnostics-channel.d.ts +10 -10
  77. package/types/dispatcher.d.ts +96 -97
  78. package/types/env-http-proxy-agent.d.ts +2 -2
  79. package/types/errors.d.ts +53 -47
  80. package/types/eventsource.d.ts +0 -2
  81. package/types/fetch.d.ts +8 -8
  82. package/types/formdata.d.ts +7 -7
  83. package/types/global-dispatcher.d.ts +4 -4
  84. package/types/global-origin.d.ts +5 -5
  85. package/types/handlers.d.ts +4 -4
  86. package/types/header.d.ts +157 -1
  87. package/types/index.d.ts +42 -46
  88. package/types/interceptors.d.ts +10 -8
  89. package/types/mock-agent.d.ts +18 -18
  90. package/types/mock-client.d.ts +4 -4
  91. package/types/mock-errors.d.ts +3 -3
  92. package/types/mock-interceptor.d.ts +19 -19
  93. package/types/mock-pool.d.ts +4 -4
  94. package/types/patch.d.ts +0 -42
  95. package/types/pool-stats.d.ts +8 -8
  96. package/types/pool.d.ts +12 -12
  97. package/types/proxy-agent.d.ts +4 -4
  98. package/types/readable.d.ts +14 -9
  99. package/types/retry-agent.d.ts +1 -1
  100. package/types/retry-handler.d.ts +8 -8
  101. package/types/util.d.ts +3 -3
  102. package/types/utility.d.ts +7 -0
  103. package/types/webidl.d.ts +22 -4
  104. package/types/websocket.d.ts +1 -3
  105. package/docs/docs/api/DispatchInterceptor.md +0 -60
  106. package/lib/interceptor/redirect-interceptor.js +0 -21
  107. package/lib/web/fetch/file.js +0 -126
  108. package/lib/web/fileapi/encoding.js +0 -290
  109. package/lib/web/fileapi/filereader.js +0 -344
  110. package/lib/web/fileapi/progressevent.js +0 -78
  111. package/lib/web/fileapi/symbols.js +0 -10
  112. package/lib/web/fileapi/util.js +0 -391
  113. package/lib/web/websocket/symbols.js +0 -12
  114. package/types/file.d.ts +0 -39
  115. package/types/filereader.d.ts +0 -54
@@ -1,99 +1,379 @@
1
1
  'use strict'
2
2
 
3
- const TICK_MS = 499
3
+ /**
4
+ * This module offers an optimized timer implementation designed for scenarios
5
+ * where high precision is not critical.
6
+ *
7
+ * The timer achieves faster performance by using a low-resolution approach,
8
+ * with an accuracy target of within 500ms. This makes it particularly useful
9
+ * for timers with delays of 1 second or more, where exact timing is less
10
+ * crucial.
11
+ *
12
+ * It's important to note that Node.js timers are inherently imprecise, as
13
+ * delays can occur due to the event loop being blocked by other operations.
14
+ * Consequently, timers may trigger later than their scheduled time.
15
+ */
4
16
 
5
- let fastNow = Date.now()
17
+ const nativeSetTimeout = global.setTimeout
18
+ const nativeClearTimeout = global.clearTimeout
19
+
20
+ /**
21
+ * The fastNow variable contains the internal fast timer clock value.
22
+ *
23
+ * @type {number}
24
+ */
25
+ let fastNow = 0
26
+
27
+ /**
28
+ * RESOLUTION_MS represents the target resolution time in milliseconds.
29
+ *
30
+ * @type {number}
31
+ * @default 1000
32
+ */
33
+ const RESOLUTION_MS = 1e3
34
+
35
+ /**
36
+ * TICK_MS defines the desired interval in milliseconds between each tick.
37
+ * The target value is set to half the resolution time, minus 1 ms, to account
38
+ * for potential event loop overhead.
39
+ *
40
+ * @type {number}
41
+ * @default 499
42
+ */
43
+ const TICK_MS = (RESOLUTION_MS >> 1) - 1
44
+
45
+ /**
46
+ * fastNowTimeout is a Node.js timer used to manage and process
47
+ * the FastTimers stored in the `fastTimers` array.
48
+ *
49
+ * @type {NodeJS.Timeout}
50
+ */
6
51
  let fastNowTimeout
7
52
 
53
+ /**
54
+ * The kFastTimer symbol is used to identify FastTimer instances.
55
+ *
56
+ * @type {Symbol}
57
+ */
58
+ const kFastTimer = Symbol('kFastTimer')
59
+
60
+ /**
61
+ * The fastTimers array contains all active FastTimers.
62
+ *
63
+ * @type {FastTimer[]}
64
+ */
8
65
  const fastTimers = []
9
66
 
10
- function onTimeout () {
11
- fastNow = Date.now()
67
+ /**
68
+ * These constants represent the various states of a FastTimer.
69
+ */
12
70
 
13
- let len = fastTimers.length
71
+ /**
72
+ * The `NOT_IN_LIST` constant indicates that the FastTimer is not included
73
+ * in the `fastTimers` array. Timers with this status will not be processed
74
+ * during the next tick by the `onTick` function.
75
+ *
76
+ * A FastTimer can be re-added to the `fastTimers` array by invoking the
77
+ * `refresh` method on the FastTimer instance.
78
+ *
79
+ * @type {-2}
80
+ */
81
+ const NOT_IN_LIST = -2
82
+
83
+ /**
84
+ * The `TO_BE_CLEARED` constant indicates that the FastTimer is scheduled
85
+ * for removal from the `fastTimers` array. A FastTimer in this state will
86
+ * be removed in the next tick by the `onTick` function and will no longer
87
+ * be processed.
88
+ *
89
+ * This status is also set when the `clear` method is called on the FastTimer instance.
90
+ *
91
+ * @type {-1}
92
+ */
93
+ const TO_BE_CLEARED = -1
94
+
95
+ /**
96
+ * The `PENDING` constant signifies that the FastTimer is awaiting processing
97
+ * in the next tick by the `onTick` function. Timers with this status will have
98
+ * their `_idleStart` value set and their status updated to `ACTIVE` in the next tick.
99
+ *
100
+ * @type {0}
101
+ */
102
+ const PENDING = 0
103
+
104
+ /**
105
+ * The `ACTIVE` constant indicates that the FastTimer is active and waiting
106
+ * for its timer to expire. During the next tick, the `onTick` function will
107
+ * check if the timer has expired, and if so, it will execute the associated callback.
108
+ *
109
+ * @type {1}
110
+ */
111
+ const ACTIVE = 1
112
+
113
+ /**
114
+ * The onTick function processes the fastTimers array.
115
+ *
116
+ * @returns {void}
117
+ */
118
+ function onTick () {
119
+ /**
120
+ * Increment the fastNow value by the TICK_MS value, despite the actual time
121
+ * that has passed since the last tick. This approach ensures independence
122
+ * from the system clock and delays caused by a blocked event loop.
123
+ *
124
+ * @type {number}
125
+ */
126
+ fastNow += TICK_MS
127
+
128
+ /**
129
+ * The `idx` variable is used to iterate over the `fastTimers` array.
130
+ * Expired timers are removed by replacing them with the last element in the array.
131
+ * Consequently, `idx` is only incremented when the current element is not removed.
132
+ *
133
+ * @type {number}
134
+ */
14
135
  let idx = 0
136
+
137
+ /**
138
+ * The len variable will contain the length of the fastTimers array
139
+ * and will be decremented when a FastTimer should be removed from the
140
+ * fastTimers array.
141
+ *
142
+ * @type {number}
143
+ */
144
+ let len = fastTimers.length
145
+
15
146
  while (idx < len) {
147
+ /**
148
+ * @type {FastTimer}
149
+ */
16
150
  const timer = fastTimers[idx]
17
151
 
18
- if (timer.state === 0) {
19
- timer.state = fastNow + timer.delay - TICK_MS
20
- } else if (timer.state > 0 && fastNow >= timer.state) {
21
- timer.state = -1
22
- timer.callback(timer.opaque)
152
+ // If the timer is in the ACTIVE state and the timer has expired, it will
153
+ // be processed in the next tick.
154
+ if (timer._state === PENDING) {
155
+ // Set the _idleStart value to the fastNow value minus the TICK_MS value
156
+ // to account for the time the timer was in the PENDING state.
157
+ timer._idleStart = fastNow - TICK_MS
158
+ timer._state = ACTIVE
159
+ } else if (
160
+ timer._state === ACTIVE &&
161
+ fastNow >= timer._idleStart + timer._idleTimeout
162
+ ) {
163
+ timer._state = TO_BE_CLEARED
164
+ timer._idleStart = -1
165
+ timer._onTimeout(timer._timerArg)
23
166
  }
24
167
 
25
- if (timer.state === -1) {
26
- timer.state = -2
27
- if (idx !== len - 1) {
28
- fastTimers[idx] = fastTimers.pop()
29
- } else {
30
- fastTimers.pop()
168
+ if (timer._state === TO_BE_CLEARED) {
169
+ timer._state = NOT_IN_LIST
170
+
171
+ // Move the last element to the current index and decrement len if it is
172
+ // not the only element in the array.
173
+ if (--len !== 0) {
174
+ fastTimers[idx] = fastTimers[len]
31
175
  }
32
- len -= 1
33
176
  } else {
34
- idx += 1
177
+ ++idx
35
178
  }
36
179
  }
37
180
 
38
- if (fastTimers.length > 0) {
181
+ // Set the length of the fastTimers array to the new length and thus
182
+ // removing the excess FastTimers elements from the array.
183
+ fastTimers.length = len
184
+
185
+ // If there are still active FastTimers in the array, refresh the Timer.
186
+ // If there are no active FastTimers, the timer will be refreshed again
187
+ // when a new FastTimer is instantiated.
188
+ if (fastTimers.length !== 0) {
39
189
  refreshTimeout()
40
190
  }
41
191
  }
42
192
 
43
193
  function refreshTimeout () {
44
- if (fastNowTimeout?.refresh) {
194
+ // If the fastNowTimeout is already set, refresh it.
195
+ if (fastNowTimeout) {
45
196
  fastNowTimeout.refresh()
197
+ // fastNowTimeout is not instantiated yet, create a new Timer.
46
198
  } else {
47
199
  clearTimeout(fastNowTimeout)
48
- fastNowTimeout = setTimeout(onTimeout, TICK_MS)
200
+ fastNowTimeout = setTimeout(onTick, TICK_MS)
201
+
202
+ // If the Timer has an unref method, call it to allow the process to exit if
203
+ // there are no other active handles.
49
204
  if (fastNowTimeout.unref) {
50
205
  fastNowTimeout.unref()
51
206
  }
52
207
  }
53
208
  }
54
209
 
55
- class Timeout {
56
- constructor (callback, delay, opaque) {
57
- this.callback = callback
58
- this.delay = delay
59
- this.opaque = opaque
210
+ /**
211
+ * The `FastTimer` class is a data structure designed to store and manage
212
+ * timer information.
213
+ */
214
+ class FastTimer {
215
+ [kFastTimer] = true
216
+
217
+ /**
218
+ * The state of the timer, which can be one of the following:
219
+ * - NOT_IN_LIST (-2)
220
+ * - TO_BE_CLEARED (-1)
221
+ * - PENDING (0)
222
+ * - ACTIVE (1)
223
+ *
224
+ * @type {-2|-1|0|1}
225
+ * @private
226
+ */
227
+ _state = NOT_IN_LIST
228
+
229
+ /**
230
+ * The number of milliseconds to wait before calling the callback.
231
+ *
232
+ * @type {number}
233
+ * @private
234
+ */
235
+ _idleTimeout = -1
236
+
237
+ /**
238
+ * The time in milliseconds when the timer was started. This value is used to
239
+ * calculate when the timer should expire.
240
+ *
241
+ * @type {number}
242
+ * @default -1
243
+ * @private
244
+ */
245
+ _idleStart = -1
246
+
247
+ /**
248
+ * The function to be executed when the timer expires.
249
+ * @type {Function}
250
+ * @private
251
+ */
252
+ _onTimeout
253
+
254
+ /**
255
+ * The argument to be passed to the callback when the timer expires.
256
+ *
257
+ * @type {*}
258
+ * @private
259
+ */
260
+ _timerArg
60
261
 
61
- // -2 not in timer list
62
- // -1 in timer list but inactive
63
- // 0 in timer list waiting for time
64
- // > 0 in timer list waiting for time to expire
65
- this.state = -2
262
+ /**
263
+ * @constructor
264
+ * @param {Function} callback A function to be executed after the timer
265
+ * expires.
266
+ * @param {number} delay The time, in milliseconds that the timer should wait
267
+ * before the specified function or code is executed.
268
+ * @param {*} arg
269
+ */
270
+ constructor (callback, delay, arg) {
271
+ this._onTimeout = callback
272
+ this._idleTimeout = delay
273
+ this._timerArg = arg
66
274
 
67
275
  this.refresh()
68
276
  }
69
277
 
278
+ /**
279
+ * Sets the timer's start time to the current time, and reschedules the timer
280
+ * to call its callback at the previously specified duration adjusted to the
281
+ * current time.
282
+ * Using this on a timer that has already called its callback will reactivate
283
+ * the timer.
284
+ *
285
+ * @returns {void}
286
+ */
70
287
  refresh () {
71
- if (this.state === -2) {
288
+ // In the special case that the timer is not in the list of active timers,
289
+ // add it back to the array to be processed in the next tick by the onTick
290
+ // function.
291
+ if (this._state === NOT_IN_LIST) {
72
292
  fastTimers.push(this)
73
- if (!fastNowTimeout || fastTimers.length === 1) {
74
- refreshTimeout()
75
- }
76
293
  }
77
294
 
78
- this.state = 0
295
+ // If the timer is the only active timer, refresh the fastNowTimeout for
296
+ // better resolution.
297
+ if (!fastNowTimeout || fastTimers.length === 1) {
298
+ refreshTimeout()
299
+ }
300
+
301
+ // Setting the state to PENDING will cause the timer to be reset in the
302
+ // next tick by the onTick function.
303
+ this._state = PENDING
79
304
  }
80
305
 
306
+ /**
307
+ * The `clear` method cancels the timer, preventing it from executing.
308
+ *
309
+ * @returns {void}
310
+ * @private
311
+ */
81
312
  clear () {
82
- this.state = -1
313
+ // Set the state to TO_BE_CLEARED to mark the timer for removal in the next
314
+ // tick by the onTick function.
315
+ this._state = TO_BE_CLEARED
316
+
317
+ // Reset the _idleStart value to -1 to indicate that the timer is no longer
318
+ // active.
319
+ this._idleStart = -1
83
320
  }
84
321
  }
85
322
 
323
+ /**
324
+ * This module exports a setTimeout and clearTimeout function that can be
325
+ * used as a drop-in replacement for the native functions.
326
+ */
86
327
  module.exports = {
87
- setTimeout (callback, delay, opaque) {
88
- return delay <= 1e3
89
- ? setTimeout(callback, delay, opaque)
90
- : new Timeout(callback, delay, opaque)
328
+ /**
329
+ * The setTimeout() method sets a timer which executes a function once the
330
+ * timer expires.
331
+ * @param {Function} callback A function to be executed after the timer
332
+ * expires.
333
+ * @param {number} delay The time, in milliseconds that the timer should
334
+ * wait before the specified function or code is executed.
335
+ * @param {*} [arg] An optional argument to be passed to the callback function
336
+ * when the timer expires.
337
+ * @returns {NodeJS.Timeout|FastTimer}
338
+ */
339
+ setTimeout (callback, delay, arg) {
340
+ // If the delay is less than or equal to the RESOLUTION_MS value return a
341
+ // native Node.js Timer instance.
342
+ return delay <= RESOLUTION_MS
343
+ ? nativeSetTimeout(callback, delay, arg)
344
+ : new FastTimer(callback, delay, arg)
91
345
  },
346
+ /**
347
+ * The clearTimeout method cancels an instantiated Timer previously created
348
+ * by calling setTimeout.
349
+ *
350
+ * @param {FastTimer} timeout
351
+ */
92
352
  clearTimeout (timeout) {
93
- if (timeout instanceof Timeout) {
353
+ // If the timeout is a FastTimer, call its own clear method.
354
+ if (timeout[kFastTimer]) {
355
+ /**
356
+ * @type {FastTimer}
357
+ */
94
358
  timeout.clear()
359
+ // Otherwise it is an instance of a native NodeJS.Timeout, so call the
360
+ // Node.js native clearTimeout function.
95
361
  } else {
96
- clearTimeout(timeout)
362
+ nativeClearTimeout(timeout)
97
363
  }
98
- }
364
+ },
365
+ /**
366
+ * The now method returns the value of the internal fast timer clock.
367
+ *
368
+ * @returns {number}
369
+ */
370
+ now () {
371
+ return fastNow
372
+ },
373
+ /**
374
+ * Exporting for testing purposes only.
375
+ * Marking as deprecated to discourage any use outside of testing.
376
+ * @deprecated
377
+ */
378
+ kFastTimer
99
379
  }
@@ -9,14 +9,14 @@ const { Headers } = require('../fetch/headers')
9
9
  * @typedef {Object} Cookie
10
10
  * @property {string} name
11
11
  * @property {string} value
12
- * @property {Date|number|undefined} expires
13
- * @property {number|undefined} maxAge
14
- * @property {string|undefined} domain
15
- * @property {string|undefined} path
16
- * @property {boolean|undefined} secure
17
- * @property {boolean|undefined} httpOnly
18
- * @property {'Strict'|'Lax'|'None'} sameSite
19
- * @property {string[]} unparsed
12
+ * @property {Date|number} [expires]
13
+ * @property {number} [maxAge]
14
+ * @property {string} [domain]
15
+ * @property {string} [path]
16
+ * @property {boolean} [secure]
17
+ * @property {boolean} [httpOnly]
18
+ * @property {'Strict'|'Lax'|'None'} [sameSite]
19
+ * @property {string[]} [unparsed]
20
20
  */
21
21
 
22
22
  /**
@@ -26,9 +26,11 @@ const { Headers } = require('../fetch/headers')
26
26
  function getCookies (headers) {
27
27
  webidl.argumentLengthCheck(arguments, 1, 'getCookies')
28
28
 
29
- webidl.brandCheck(headers, Headers, { strict: false })
29
+ webidl.brandCheckMultiple(headers, [Headers, globalThis.Headers])
30
30
 
31
31
  const cookie = headers.get('cookie')
32
+
33
+ /** @type {Record<string, string>} */
32
34
  const out = {}
33
35
 
34
36
  if (!cookie) {
@@ -51,7 +53,7 @@ function getCookies (headers) {
51
53
  * @returns {void}
52
54
  */
53
55
  function deleteCookie (headers, name, attributes) {
54
- webidl.brandCheck(headers, Headers, { strict: false })
56
+ webidl.brandCheckMultiple(headers, [Headers, globalThis.Headers])
55
57
 
56
58
  const prefix = 'deleteCookie'
57
59
  webidl.argumentLengthCheck(arguments, 2, prefix)
@@ -76,7 +78,7 @@ function deleteCookie (headers, name, attributes) {
76
78
  function getSetCookies (headers) {
77
79
  webidl.argumentLengthCheck(arguments, 1, 'getSetCookies')
78
80
 
79
- webidl.brandCheck(headers, Headers, { strict: false })
81
+ webidl.brandCheckMultiple(headers, [Headers, globalThis.Headers])
80
82
 
81
83
  const cookies = headers.getSetCookie()
82
84
 
@@ -95,14 +97,14 @@ function getSetCookies (headers) {
95
97
  function setCookie (headers, cookie) {
96
98
  webidl.argumentLengthCheck(arguments, 2, 'setCookie')
97
99
 
98
- webidl.brandCheck(headers, Headers, { strict: false })
100
+ webidl.brandCheckMultiple(headers, [Headers, globalThis.Headers])
99
101
 
100
102
  cookie = webidl.converters.Cookie(cookie)
101
103
 
102
104
  const str = stringify(cookie)
103
105
 
104
106
  if (str) {
105
- headers.append('Set-Cookie', str)
107
+ headers.append('set-cookie', str, true)
106
108
  }
107
109
  }
108
110
 
@@ -9,7 +9,7 @@ const assert = require('node:assert')
9
9
  * @description Parses the field-value attributes of a set-cookie header string.
10
10
  * @see https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis#section-5.4
11
11
  * @param {string} header
12
- * @returns if the header is invalid, null will be returned
12
+ * @returns {import('./index').Cookie|null} if the header is invalid, null will be returned
13
13
  */
14
14
  function parseSetCookie (header) {
15
15
  // 1. If the set-cookie-string contains a %x00-08 / %x0A-1F / %x7F
@@ -85,7 +85,7 @@ function parseSetCookie (header) {
85
85
  * Parses the remaining attributes of a set-cookie header
86
86
  * @see https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis#section-5.4
87
87
  * @param {string} unparsedAttributes
88
- * @param {[Object.<string, unknown>]={}} cookieAttributeList
88
+ * @param {Object.<string, unknown>} [cookieAttributeList={}]
89
89
  */
90
90
  function parseUnparsedAttributes (unparsedAttributes, cookieAttributeList = {}) {
91
91
  // 1. If the unparsed-attributes string is empty, skip the rest of
@@ -35,16 +35,16 @@ const SPACE = 0x20
35
35
  /**
36
36
  * @typedef eventSourceSettings
37
37
  * @type {object}
38
- * @property {string} lastEventId The last event ID received from the server.
39
- * @property {string} origin The origin of the event source.
40
- * @property {number} reconnectionTime The reconnection time, in milliseconds.
38
+ * @property {string} [lastEventId] The last event ID received from the server.
39
+ * @property {string} [origin] The origin of the event source.
40
+ * @property {number} [reconnectionTime] The reconnection time, in milliseconds.
41
41
  */
42
42
 
43
43
  class EventSourceStream extends Transform {
44
44
  /**
45
45
  * @type {eventSourceSettings}
46
46
  */
47
- state = null
47
+ state
48
48
 
49
49
  /**
50
50
  * Leading byte-order-mark check.
@@ -63,7 +63,7 @@ class EventSourceStream extends Transform {
63
63
  eventEndCheck = false
64
64
 
65
65
  /**
66
- * @type {Buffer}
66
+ * @type {Buffer|null}
67
67
  */
68
68
  buffer = null
69
69
 
@@ -78,8 +78,9 @@ class EventSourceStream extends Transform {
78
78
 
79
79
  /**
80
80
  * @param {object} options
81
- * @param {eventSourceSettings} options.eventSourceSettings
82
- * @param {Function} [options.push]
81
+ * @param {boolean} [options.readableObjectMode]
82
+ * @param {eventSourceSettings} [options.eventSourceSettings]
83
+ * @param {(chunk: any, encoding?: BufferEncoding | undefined) => boolean} [options.push]
83
84
  */
84
85
  constructor (options = {}) {
85
86
  // Enable object mode as EventSourceStream emits objects of shape
@@ -280,7 +281,7 @@ class EventSourceStream extends Transform {
280
281
 
281
282
  /**
282
283
  * @param {Buffer} line
283
- * @param {EventStreamEvent} event
284
+ * @param {EventSourceStreamEvent} event
284
285
  */
285
286
  parseLine (line, event) {
286
287
  // If the line is empty (a blank line)
@@ -28,7 +28,8 @@ const defaultReconnectionTime = 3000
28
28
 
29
29
  /**
30
30
  * The readyState attribute represents the state of the connection.
31
- * @enum
31
+ * @typedef ReadyState
32
+ * @type {0|1|2}
32
33
  * @readonly
33
34
  * @see https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource-readystate-dev
34
35
  */
@@ -80,9 +81,12 @@ class EventSource extends EventTarget {
80
81
  message: null
81
82
  }
82
83
 
83
- #url = null
84
+ #url
84
85
  #withCredentials = false
85
86
 
87
+ /**
88
+ * @type {ReadyState}
89
+ */
86
90
  #readyState = CONNECTING
87
91
 
88
92
  #request = null
@@ -98,7 +102,7 @@ class EventSource extends EventTarget {
98
102
  /**
99
103
  * Creates a new EventSource object.
100
104
  * @param {string} url
101
- * @param {EventSourceInit} [eventSourceInitDict]
105
+ * @param {EventSourceInit} [eventSourceInitDict={}]
102
106
  * @see https://html.spec.whatwg.org/multipage/server-sent-events.html#the-eventsource-interface
103
107
  */
104
108
  constructor (url, eventSourceInitDict = {}) {
@@ -115,7 +119,7 @@ class EventSource extends EventTarget {
115
119
  })
116
120
  }
117
121
 
118
- url = webidl.converters.USVString(url, prefix, 'url')
122
+ url = webidl.converters.USVString(url)
119
123
  eventSourceInitDict = webidl.converters.EventSourceInitDict(eventSourceInitDict, prefix, 'eventSourceInitDict')
120
124
 
121
125
  this.#dispatcher = eventSourceInitDict.dispatcher
@@ -148,7 +152,7 @@ class EventSource extends EventTarget {
148
152
  // 7. If the value of eventSourceInitDict's withCredentials member is true,
149
153
  // then set corsAttributeState to Use Credentials and set ev's
150
154
  // withCredentials attribute to true.
151
- if (eventSourceInitDict.withCredentials) {
155
+ if (eventSourceInitDict.withCredentials === true) {
152
156
  corsAttributeState = USE_CREDENTIALS
153
157
  this.#withCredentials = true
154
158
  }
@@ -189,7 +193,7 @@ class EventSource extends EventTarget {
189
193
  /**
190
194
  * Returns the state of this EventSource object's connection. It can have the
191
195
  * values described below.
192
- * @returns {0|1|2}
196
+ * @returns {ReadyState}
193
197
  * @readonly
194
198
  */
195
199
  get readyState () {