undici 6.19.8 → 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.
- package/README.md +5 -9
- package/docs/docs/api/Agent.md +0 -3
- package/docs/docs/api/Client.md +0 -2
- package/docs/docs/api/Dispatcher.md +204 -6
- package/docs/docs/api/EnvHttpProxyAgent.md +0 -1
- package/docs/docs/api/Fetch.md +1 -0
- package/docs/docs/api/Pool.md +0 -1
- package/docs/docs/api/RetryHandler.md +1 -1
- package/index.js +0 -4
- package/lib/api/api-connect.js +3 -1
- package/lib/api/api-pipeline.js +3 -4
- package/lib/api/api-request.js +29 -46
- package/lib/api/api-stream.js +36 -49
- package/lib/api/api-upgrade.js +5 -3
- package/lib/api/readable.js +71 -27
- package/lib/core/connect.js +39 -24
- package/lib/core/errors.js +17 -4
- package/lib/core/request.js +7 -5
- package/lib/core/symbols.js +0 -1
- package/lib/core/tree.js +6 -0
- package/lib/core/util.js +1 -11
- package/lib/dispatcher/agent.js +3 -17
- package/lib/dispatcher/balanced-pool.js +5 -8
- package/lib/dispatcher/client-h1.js +44 -39
- package/lib/dispatcher/client.js +3 -27
- package/lib/dispatcher/dispatcher-base.js +2 -34
- package/lib/dispatcher/dispatcher.js +3 -24
- package/lib/dispatcher/pool.js +3 -6
- package/lib/dispatcher/proxy-agent.js +3 -6
- package/lib/handler/decorator-handler.js +24 -0
- package/lib/handler/redirect-handler.js +9 -0
- package/lib/handler/retry-handler.js +22 -3
- package/lib/interceptor/dump.js +2 -2
- package/lib/interceptor/redirect.js +11 -14
- package/lib/interceptor/response-error.js +89 -0
- package/lib/llhttp/constants.d.ts +97 -0
- package/lib/llhttp/constants.js +412 -192
- package/lib/llhttp/constants.js.map +1 -0
- package/lib/llhttp/llhttp-wasm.js +11 -1
- package/lib/llhttp/llhttp_simd-wasm.js +11 -1
- package/lib/llhttp/utils.d.ts +2 -0
- package/lib/llhttp/utils.js +9 -9
- package/lib/llhttp/utils.js.map +1 -0
- package/lib/mock/mock-client.js +2 -2
- package/lib/mock/mock-pool.js +2 -2
- package/lib/mock/mock-symbols.js +1 -0
- package/lib/util/timers.js +324 -44
- package/lib/web/cookies/index.js +15 -13
- package/lib/web/cookies/parse.js +2 -2
- package/lib/web/eventsource/eventsource-stream.js +9 -8
- package/lib/web/eventsource/eventsource.js +10 -6
- package/lib/web/fetch/body.js +4 -6
- package/lib/web/fetch/data-url.js +1 -1
- package/lib/web/fetch/formdata-parser.js +1 -2
- package/lib/web/fetch/formdata.js +28 -37
- package/lib/web/fetch/headers.js +1 -1
- package/lib/web/fetch/index.js +7 -8
- package/lib/web/fetch/request.js +7 -24
- package/lib/web/fetch/response.js +9 -22
- package/lib/web/fetch/symbols.js +0 -1
- package/lib/web/fetch/util.js +3 -12
- package/lib/web/fetch/webidl.js +73 -62
- package/lib/web/websocket/connection.js +26 -174
- package/lib/web/websocket/constants.js +1 -1
- package/lib/web/websocket/frame.js +45 -3
- package/lib/web/websocket/receiver.js +28 -26
- package/lib/web/websocket/sender.js +18 -13
- package/lib/web/websocket/util.js +20 -74
- package/lib/web/websocket/websocket.js +294 -70
- package/package.json +16 -29
- package/scripts/strip-comments.js +3 -1
- package/types/agent.d.ts +7 -7
- package/types/api.d.ts +24 -24
- package/types/balanced-pool.d.ts +11 -11
- package/types/client.d.ts +11 -12
- package/types/diagnostics-channel.d.ts +10 -10
- package/types/dispatcher.d.ts +96 -97
- package/types/env-http-proxy-agent.d.ts +2 -2
- package/types/errors.d.ts +53 -47
- package/types/eventsource.d.ts +0 -2
- package/types/fetch.d.ts +8 -8
- package/types/formdata.d.ts +7 -7
- package/types/global-dispatcher.d.ts +4 -4
- package/types/global-origin.d.ts +5 -5
- package/types/handlers.d.ts +4 -4
- package/types/header.d.ts +157 -1
- package/types/index.d.ts +42 -46
- package/types/interceptors.d.ts +10 -8
- package/types/mock-agent.d.ts +18 -18
- package/types/mock-client.d.ts +4 -4
- package/types/mock-errors.d.ts +3 -3
- package/types/mock-interceptor.d.ts +19 -19
- package/types/mock-pool.d.ts +4 -4
- package/types/patch.d.ts +0 -42
- package/types/pool-stats.d.ts +8 -8
- package/types/pool.d.ts +12 -12
- package/types/proxy-agent.d.ts +4 -4
- package/types/readable.d.ts +14 -9
- package/types/retry-agent.d.ts +1 -1
- package/types/retry-handler.d.ts +8 -8
- package/types/util.d.ts +3 -3
- package/types/utility.d.ts +7 -0
- package/types/webidl.d.ts +22 -4
- package/types/websocket.d.ts +1 -3
- package/docs/docs/api/DispatchInterceptor.md +0 -60
- package/lib/interceptor/redirect-interceptor.js +0 -21
- package/lib/web/fetch/file.js +0 -126
- package/lib/web/fileapi/encoding.js +0 -290
- package/lib/web/fileapi/filereader.js +0 -344
- package/lib/web/fileapi/progressevent.js +0 -78
- package/lib/web/fileapi/symbols.js +0 -10
- package/lib/web/fileapi/util.js +0 -391
- package/lib/web/websocket/symbols.js +0 -12
- package/types/file.d.ts +0 -39
- package/types/filereader.d.ts +0 -54
package/lib/util/timers.js
CHANGED
|
@@ -1,99 +1,379 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
|
|
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
|
-
|
|
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
|
-
|
|
11
|
-
|
|
67
|
+
/**
|
|
68
|
+
* These constants represent the various states of a FastTimer.
|
|
69
|
+
*/
|
|
12
70
|
|
|
13
|
-
|
|
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
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
timer.
|
|
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.
|
|
26
|
-
timer.
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
|
177
|
+
++idx
|
|
35
178
|
}
|
|
36
179
|
}
|
|
37
180
|
|
|
38
|
-
|
|
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
|
-
|
|
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(
|
|
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
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
}
|
package/lib/web/cookies/index.js
CHANGED
|
@@ -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
|
|
13
|
-
* @property {number
|
|
14
|
-
* @property {string
|
|
15
|
-
* @property {string
|
|
16
|
-
* @property {boolean
|
|
17
|
-
* @property {boolean
|
|
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.
|
|
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.
|
|
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.
|
|
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.
|
|
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('
|
|
107
|
+
headers.append('set-cookie', str, true)
|
|
106
108
|
}
|
|
107
109
|
}
|
|
108
110
|
|
package/lib/web/cookies/parse.js
CHANGED
|
@@ -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 {
|
|
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
|
|
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 {
|
|
82
|
-
* @param {
|
|
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 {
|
|
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
|
-
* @
|
|
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
|
|
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
|
|
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 {
|
|
196
|
+
* @returns {ReadyState}
|
|
193
197
|
* @readonly
|
|
194
198
|
*/
|
|
195
199
|
get readyState () {
|
package/lib/web/fetch/body.js
CHANGED
|
@@ -3,8 +3,6 @@
|
|
|
3
3
|
const util = require('../../core/util')
|
|
4
4
|
const {
|
|
5
5
|
ReadableStreamFrom,
|
|
6
|
-
isBlobLike,
|
|
7
|
-
isReadableStreamLike,
|
|
8
6
|
readableStreamClose,
|
|
9
7
|
createDeferredPromise,
|
|
10
8
|
fullyReadBody,
|
|
@@ -44,7 +42,7 @@ function extractBody (object, keepalive = false) {
|
|
|
44
42
|
// 2. If object is a ReadableStream object, then set stream to object.
|
|
45
43
|
if (object instanceof ReadableStream) {
|
|
46
44
|
stream = object
|
|
47
|
-
} else if (
|
|
45
|
+
} else if (object instanceof Blob) {
|
|
48
46
|
// 3. Otherwise, if object is a Blob object, set stream to the
|
|
49
47
|
// result of running object’s get stream.
|
|
50
48
|
stream = object.stream()
|
|
@@ -67,7 +65,7 @@ function extractBody (object, keepalive = false) {
|
|
|
67
65
|
}
|
|
68
66
|
|
|
69
67
|
// 5. Assert: stream is a ReadableStream object.
|
|
70
|
-
assert(
|
|
68
|
+
assert(stream instanceof ReadableStream)
|
|
71
69
|
|
|
72
70
|
// 6. Let action be null.
|
|
73
71
|
let action = null
|
|
@@ -112,7 +110,7 @@ function extractBody (object, keepalive = false) {
|
|
|
112
110
|
|
|
113
111
|
// Set source to a copy of the bytes held by object.
|
|
114
112
|
source = new Uint8Array(object.buffer.slice(object.byteOffset, object.byteOffset + object.byteLength))
|
|
115
|
-
} else if (
|
|
113
|
+
} else if (object instanceof FormData) {
|
|
116
114
|
const boundary = `----formdata-undici-0${`${Math.floor(Math.random() * 1e11)}`.padStart(11, '0')}`
|
|
117
115
|
const prefix = `--${boundary}\r\nContent-Disposition: form-data`
|
|
118
116
|
|
|
@@ -178,7 +176,7 @@ function extractBody (object, keepalive = false) {
|
|
|
178
176
|
// followed by the multipart/form-data boundary string generated
|
|
179
177
|
// by the multipart/form-data encoding algorithm.
|
|
180
178
|
type = `multipart/form-data; boundary=${boundary}`
|
|
181
|
-
} else if (
|
|
179
|
+
} else if (object instanceof Blob) {
|
|
182
180
|
// Blob
|
|
183
181
|
|
|
184
182
|
// Set source to object.
|
|
@@ -431,7 +431,7 @@ function parseMIMEType (input) {
|
|
|
431
431
|
/** @param {string} data */
|
|
432
432
|
function forgivingBase64 (data) {
|
|
433
433
|
// 1. Remove all ASCII whitespace from data.
|
|
434
|
-
data = data.replace(ASCII_WHITESPACE_REPLACE_REGEX, '')
|
|
434
|
+
data = data.replace(ASCII_WHITESPACE_REPLACE_REGEX, '')
|
|
435
435
|
|
|
436
436
|
let dataLength = data.length
|
|
437
437
|
// 2. If data’s code point length divides by 4 leaving
|