pi-dent-agent 0.2.9 → 0.2.11

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 (39) hide show
  1. package/lib/plugin-state.ts +587 -0
  2. package/lib/plugin-ui.ts +2 -0
  3. package/lib/script-runner.ts +8 -0
  4. package/lib/script-tool.ts +3 -0
  5. package/lib/script-worker.ts +78 -7
  6. package/node_modules/pi-web-access/node_modules/undici/docs/docs/api/CacheStore.md +11 -0
  7. package/node_modules/pi-web-access/node_modules/undici/docs/docs/api/Client.md +1 -1
  8. package/node_modules/pi-web-access/node_modules/undici/docs/docs/api/Connector.md +9 -2
  9. package/node_modules/pi-web-access/node_modules/undici/docs/docs/api/Dispatcher.md +20 -0
  10. package/node_modules/pi-web-access/node_modules/undici/docs/docs/api/Interceptors.md +13 -3
  11. package/node_modules/pi-web-access/node_modules/undici/docs/docs/best-practices/mocking-request.md +1 -1
  12. package/node_modules/pi-web-access/node_modules/undici/lib/api/abort-signal.js +55 -5
  13. package/node_modules/pi-web-access/node_modules/undici/lib/api/api-connect.js +6 -2
  14. package/node_modules/pi-web-access/node_modules/undici/lib/api/api-pipeline.js +7 -2
  15. package/node_modules/pi-web-access/node_modules/undici/lib/api/api-request.js +12 -4
  16. package/node_modules/pi-web-access/node_modules/undici/lib/api/api-stream.js +6 -2
  17. package/node_modules/pi-web-access/node_modules/undici/lib/api/api-upgrade.js +6 -2
  18. package/node_modules/pi-web-access/node_modules/undici/lib/core/request.js +17 -0
  19. package/node_modules/pi-web-access/node_modules/undici/lib/core/symbols.js +3 -1
  20. package/node_modules/pi-web-access/node_modules/undici/lib/dispatcher/client-h1.js +24 -0
  21. package/node_modules/pi-web-access/node_modules/undici/lib/dispatcher/client-h2.js +11 -5
  22. package/node_modules/pi-web-access/node_modules/undici/lib/dispatcher/client.js +109 -2
  23. package/node_modules/pi-web-access/node_modules/undici/lib/dispatcher/dispatcher-base.js +6 -1
  24. package/node_modules/pi-web-access/node_modules/undici/lib/dispatcher/dispatcher.js +22 -1
  25. package/node_modules/pi-web-access/node_modules/undici/lib/dispatcher/pool-base.js +72 -16
  26. package/node_modules/pi-web-access/node_modules/undici/lib/dispatcher/pool.js +22 -9
  27. package/node_modules/pi-web-access/node_modules/undici/lib/handler/cache-handler.js +1 -1
  28. package/node_modules/pi-web-access/node_modules/undici/lib/handler/retry-handler.js +27 -1
  29. package/node_modules/pi-web-access/node_modules/undici/lib/interceptor/cache.js +9 -2
  30. package/node_modules/pi-web-access/node_modules/undici/lib/interceptor/decompress.js +0 -11
  31. package/node_modules/pi-web-access/node_modules/undici/lib/interceptor/deduplicate.js +4 -1
  32. package/node_modules/pi-web-access/node_modules/undici/lib/interceptor/redirect.js +4 -1
  33. package/node_modules/pi-web-access/node_modules/undici/lib/util/cache.js +1 -1
  34. package/node_modules/pi-web-access/node_modules/undici/lib/util/timers.js +1 -1
  35. package/node_modules/pi-web-access/node_modules/undici/lib/web/fetch/index.js +20 -1
  36. package/node_modules/pi-web-access/node_modules/undici/lib/web/fetch/util.js +2 -2
  37. package/node_modules/pi-web-access/node_modules/undici/package.json +3 -3
  38. package/node_modules/pi-web-access/node_modules/undici/types/retry-handler.d.ts +1 -1
  39. package/package.json +1 -1
@@ -55,12 +55,14 @@ const {
55
55
  kHostAuthority,
56
56
  kResume,
57
57
  kHTTP2Options,
58
- kUseMilo
58
+ kUseMilo,
59
+ kQueuedAbort
59
60
  } = require('../core/symbols.js')
60
61
  const connectH1 = require('./client-h1.js')
61
62
  const connectH2 = require('./client-h2.js')
62
63
 
63
64
  const kClosedResolve = Symbol('kClosedResolve')
65
+ const kCancelConnect = Symbol('kCancelConnect')
64
66
 
65
67
  const getDefaultNodeMaxHeaderSize = http &&
66
68
  http.maxHeaderSize &&
@@ -322,6 +324,7 @@ class Client extends DispatcherBase {
322
324
  this[kClosedResolve] = null
323
325
  this[kMaxResponseSize] = maxResponseSize > -1 ? maxResponseSize : -1
324
326
  this[kHTTPContext] = null
327
+ this[kCancelConnect] = null
325
328
  // h2
326
329
  this[kHTTP2Options] = {
327
330
  pingInterval: h2Options?.pingInterval ?? pingInterval ?? 60e3,
@@ -402,6 +405,8 @@ class Client extends DispatcherBase {
402
405
 
403
406
  [kConnect] (cb) {
404
407
  connect(this)
408
+ // Asked for explicitly: keep it even if no request waits for it.
409
+ this[kCancelConnect] = null
405
410
  this.once('connect', cb)
406
411
  }
407
412
 
@@ -419,6 +424,15 @@ class Client extends DispatcherBase {
419
424
  this[kResume](true)
420
425
  }
421
426
 
427
+ if (request.abort === null && !request.aborted) {
428
+ // The request is still waiting for a connection: if its handler listens
429
+ // to a signal, give it a way to take the request out of the queue.
430
+ const onQueued = opts[kQueuedAbort]
431
+ if (onQueued !== undefined) {
432
+ onQueued((reason) => abortPendingRequest(this, request, reason))
433
+ }
434
+ }
435
+
422
436
  if (this[kResuming] && this[kNeedDrain] !== 2 && this[kBusy]) {
423
437
  this[kNeedDrain] = 2
424
438
  }
@@ -469,6 +483,56 @@ class Client extends DispatcherBase {
469
483
  }
470
484
  }
471
485
 
486
+ function failQueuedRequest (client, request, reason) {
487
+ try {
488
+ request.onQueuedAbort(reason)
489
+ assert(request.aborted)
490
+ } catch (err) {
491
+ client.emit('error', err)
492
+ }
493
+ }
494
+
495
+ function abortPendingRequest (client, request, reason) {
496
+ // The request already settled, or it started and its handler owns the abort.
497
+ if (request.aborted || request.abort !== null) {
498
+ return
499
+ }
500
+
501
+ const queue = client[kQueue]
502
+ const idx = queue.indexOf(request, client[kPendingIdx])
503
+ if (idx === -1) {
504
+ return
505
+ }
506
+
507
+ // _resume() may be handing this request to the connection right now, before
508
+ // onRequestStart runs. Leave it there: the handler aborts it once it starts.
509
+ if (idx === client[kPendingIdx] && client[kResuming] === 2) {
510
+ return
511
+ }
512
+
513
+ queue.splice(idx, 1)
514
+ failQueuedRequest(client, request, reason)
515
+
516
+ // The client may no longer be busy: emit 'drain' so a Pool can use it again.
517
+ client[kResume](true)
518
+
519
+ if (client[kCancelConnect] !== null && client[kSize] === 0) {
520
+ // The drain is emitted in a microtask: check once a Pool had the chance to
521
+ // hand this client the requests it queued.
522
+ queueMicrotask(() => cancelUnusedConnect(client))
523
+ }
524
+ }
525
+
526
+ // A connection attempt that no request waits for only holds a connection slot,
527
+ // until it connects or times out. Destroy it, so the next request starts a new
528
+ // one instead of waiting for it.
529
+ function cancelUnusedConnect (client) {
530
+ const cancel = client[kCancelConnect]
531
+ if (cancel !== null && client[kSize] === 0) {
532
+ cancel(new InformationalError('no request waits for this connection'))
533
+ }
534
+ }
535
+
472
536
  function onError (client, err) {
473
537
  if (
474
538
  client[kRunning] === 0 &&
@@ -515,6 +579,9 @@ function connect (client) {
515
579
 
516
580
  client[kConnecting] = true
517
581
 
582
+ let calledBack = false
583
+ let cancelled = false
584
+
518
585
  if (channels.beforeConnect.hasSubscribers) {
519
586
  channels.beforeConnect.publish({
520
587
  connectParams: {
@@ -531,7 +598,7 @@ function connect (client) {
531
598
  }
532
599
 
533
600
  try {
534
- client[kConnector]({
601
+ const connecting = client[kConnector]({
535
602
  host,
536
603
  hostname,
537
604
  protocol,
@@ -539,6 +606,17 @@ function connect (client) {
539
606
  servername: client[kServerName],
540
607
  localAddress: client[kLocalAddress]
541
608
  }, (err, socket) => {
609
+ calledBack = true
610
+
611
+ if (cancelled) {
612
+ if (socket) {
613
+ util.destroy(socket.on('error', noop))
614
+ }
615
+ return
616
+ }
617
+
618
+ client[kCancelConnect] = null
619
+
542
620
  if (err) {
543
621
  handleConnectError(client, err, { host, hostname, protocol, port })
544
622
  client[kResume]()
@@ -590,6 +668,35 @@ function connect (client) {
590
668
  client.emit('connect', client[kUrl], [client])
591
669
  client[kResume]()
592
670
  })
671
+
672
+ // The attempt can only be cancelled through the socket the connector
673
+ // returns. The built-in connector returns it; custom connectors may not.
674
+ if (!calledBack && util.isStream(connecting)) {
675
+ client[kCancelConnect] = (err) => {
676
+ cancelled = true
677
+ client[kCancelConnect] = null
678
+ client[kConnecting] = false
679
+
680
+ // Not a 'connectionError': that would make a Pool retire this client.
681
+ if (channels.connectError.hasSubscribers) {
682
+ channels.connectError.publish({
683
+ connectParams: {
684
+ host,
685
+ hostname,
686
+ protocol,
687
+ port,
688
+ version: client[kHTTPContext]?.version,
689
+ servername: client[kServerName],
690
+ localAddress: client[kLocalAddress]
691
+ },
692
+ connector: client[kConnector],
693
+ error: err
694
+ })
695
+ }
696
+
697
+ util.destroy(connecting.on('error', noop), err)
698
+ }
699
+ }
593
700
  } catch (err) {
594
701
  handleConnectError(client, err, { host, hostname, protocol, port })
595
702
  client[kResume]()
@@ -190,7 +190,12 @@ class DispatcherBase extends Dispatcher {
190
190
 
191
191
  handler.onResponseError(null, err)
192
192
 
193
- return false
193
+ // The request failed synchronously: no work was queued on this
194
+ // dispatcher, no capacity was consumed and no 'drain' event will
195
+ // ever follow. Returning false would make callers (e.g. Pool)
196
+ // mark this dispatcher as busy waiting for a drain that never
197
+ // comes, permanently taking it out of rotation.
198
+ return true
194
199
  }
195
200
  }
196
201
  }
@@ -1,6 +1,7 @@
1
1
  'use strict'
2
2
  const EventEmitter = require('node:events')
3
- const { kOriginless, kUrl } = require('../core/symbols')
3
+ const { InvalidArgumentError } = require('../core/errors')
4
+ const { kOriginless, kUrl, kInterceptorKind } = require('../core/symbols')
4
5
 
5
6
  class Dispatcher extends EventEmitter {
6
7
  dispatch () {
@@ -24,6 +25,8 @@ class Dispatcher extends EventEmitter {
24
25
  : this[kUrl]?.origin
25
26
  let dispatch = this.dispatch.bind(this)
26
27
 
28
+ const orderSensitiveKinds = []
29
+
27
30
  for (const interceptor of interceptors) {
28
31
  if (interceptor == null) {
29
32
  continue
@@ -33,6 +36,10 @@ class Dispatcher extends EventEmitter {
33
36
  throw new TypeError(`invalid interceptor, expected function received ${typeof interceptor}`)
34
37
  }
35
38
 
39
+ if (interceptor[kInterceptorKind] != null) {
40
+ orderSensitiveKinds.push(interceptor[kInterceptorKind])
41
+ }
42
+
36
43
  dispatch = interceptor(dispatch, interceptorOrigin)
37
44
 
38
45
  if (dispatch == null || typeof dispatch !== 'function' || dispatch.length !== 2) {
@@ -40,6 +47,20 @@ class Dispatcher extends EventEmitter {
40
47
  }
41
48
  }
42
49
 
50
+ // Several shipped interceptors are order-sensitive: they cooperate through
51
+ // the redirect history threaded on the request context, and the cache
52
+ // handler only receives it when the redirect interceptor is composed before
53
+ // the cache interceptor. Composing `cache` before `redirect` silently
54
+ // disables the cache's redirect-origin guards (cross-origin cache
55
+ // poisoning, credential-bearing redirect replay), so fail fast instead of
56
+ // storing an attacker-controlled or cross-principal response under a key
57
+ // the application trusts.
58
+ const cacheIndex = orderSensitiveKinds.indexOf('cache')
59
+ const redirectIndex = orderSensitiveKinds.indexOf('redirect')
60
+ if (cacheIndex !== -1 && redirectIndex !== -1 && cacheIndex < redirectIndex) {
61
+ throw new InvalidArgumentError('the cache interceptor must be composed after the redirect interceptor; composing the cache before the redirect disables the cache\'s cross-origin and credential redirect guards (GHSA-75VM-9G8F-99J4, GHSA-MQG3-3V4R-CM32, GHSA-4W7W-5XRC-9H26, GHSA-7XG3-FPHP-28HG)')
62
+ }
63
+
43
64
  const originalDispatch = dispatch
44
65
  const self = this
45
66
  dispatch = function (opts, handler) {
@@ -4,7 +4,8 @@ const { AsyncResource } = require('node:async_hooks')
4
4
  const { PoolStats } = require('../util/stats.js')
5
5
  const DispatcherBase = require('./dispatcher-base')
6
6
  const FixedQueue = require('./fixed-queue')
7
- const { kConnected, kSize, kRunning, kPending, kQueued, kBusy, kFree, kUrl, kClose, kDestroy, kDispatch } = require('../core/symbols')
7
+ const { kConnected, kSize, kRunning, kPending, kQueued, kBusy, kFree, kUrl, kClose, kDestroy, kDispatch, kQueuedAbort } = require('../core/symbols')
8
+ const { createAbortedController } = require('../core/request')
8
9
 
9
10
  const kClients = Symbol('clients')
10
11
  const kNeedDrain = Symbol('needDrain')
@@ -23,6 +24,8 @@ const kAddClient = Symbol('add client')
23
24
  const kRemoveClient = Symbol('remove client')
24
25
  const kRetireClient = Symbol('retire client')
25
26
  const kRetiring = Symbol('retiring clients')
27
+ const kShiftQueue = Symbol('shift queue')
28
+ const kAbortQueued = Symbol('abort queued')
26
29
 
27
30
  // Closes every live client, including clients that have been
28
31
  // taken out of rotation but are still finishing their own
@@ -36,6 +39,8 @@ class QueuedRequest extends AsyncResource {
36
39
  super('UNDICI_POOL_QUEUED_REQUEST')
37
40
  this.opts = opts
38
41
  this.handler = handler
42
+ // Cleared once the request leaves the queue, so a late abort is ignored.
43
+ this.queued = true
39
44
  }
40
45
 
41
46
  dispatch (dispatcher) {
@@ -75,17 +80,15 @@ class PoolBase extends DispatcherBase {
75
80
  return
76
81
  }
77
82
 
78
- const queue = this[kQueue]
79
83
  let needDrain = false
80
84
 
81
85
  this[kOnClientDrain](client)
82
86
 
83
87
  while (!needDrain) {
84
- const item = queue.shift()
88
+ const item = this[kShiftQueue]()
85
89
  if (!item) {
86
90
  break
87
91
  }
88
- this[kQueued]--
89
92
  needDrain = !item.dispatch(client)
90
93
  }
91
94
 
@@ -98,7 +101,7 @@ class PoolBase extends DispatcherBase {
98
101
  // negotiated and hit maxConcurrentStreams. Hand the rest to other
99
102
  // clients, creating new ones up to the connection limit, instead of
100
103
  // leaving them for this client to drain one batch at a time.
101
- if (!queue.isEmpty()) {
104
+ if (this[kQueued] > 0) {
102
105
  return this[kDrainQueue](origin, targets)
103
106
  }
104
107
  }
@@ -108,7 +111,7 @@ class PoolBase extends DispatcherBase {
108
111
  this.emit('drain', origin, [this, ...targets])
109
112
  }
110
113
 
111
- if (this[kClosedResolve] && queue.isEmpty()) {
114
+ if (this[kClosedResolve] && this[kQueued] === 0) {
112
115
  return closeClients(this).then(this[kClosedResolve])
113
116
  }
114
117
  }
@@ -118,23 +121,31 @@ class PoolBase extends DispatcherBase {
118
121
  [kOnClientDrain] () {}
119
122
 
120
123
  [kDrainQueue] (origin, targets) {
121
- const queue = this[kQueue]
122
124
  let hasDispatcher = true
123
125
 
124
- while (!queue.isEmpty()) {
125
- const dispatcher = this[kGetDispatcher]()
126
+ while (this[kQueued] > 0) {
127
+ let dispatcher
128
+ try {
129
+ dispatcher = this[kGetDispatcher]()
130
+ } catch (err) {
131
+ // Selection failed before a dispatcher took ownership of this item.
132
+ const item = this[kShiftQueue]()
133
+ item.runInAsyncScope(item.handler.onResponseError, item.handler, null, err)
134
+ // The error handler may synchronously dispatch and consume capacity.
135
+ hasDispatcher = !this.destroyed && this[kHasDispatcher](false)
136
+ continue
137
+ }
126
138
  if (!dispatcher) {
127
139
  hasDispatcher = false
128
140
  break
129
141
  }
130
142
 
131
- const item = queue.shift()
132
- this[kQueued]--
143
+ const item = this[kShiftQueue]()
133
144
 
134
145
  if (!item.dispatch(dispatcher)) {
135
146
  dispatcher[kNeedDrain] = true
136
147
  this[kOnClientBusy](dispatcher)
137
- hasDispatcher = this[kHasDispatcher]()
148
+ hasDispatcher = this[kHasDispatcher](false)
138
149
  if (!hasDispatcher) {
139
150
  break
140
151
  }
@@ -146,11 +157,48 @@ class PoolBase extends DispatcherBase {
146
157
  this.emit('drain', origin, [this, ...targets])
147
158
  }
148
159
 
149
- if (this[kClosedResolve] && queue.isEmpty()) {
160
+ if (this[kClosedResolve] && this[kQueued] === 0) {
150
161
  return closeClients(this).then(this[kClosedResolve])
151
162
  }
152
163
  }
153
164
 
165
+ // Aborted requests stay in the queue as tombstones (queued === false) and
166
+ // are skipped here; kQueued only counts the live ones.
167
+ [kShiftQueue] () {
168
+ const queue = this[kQueue]
169
+ let item = queue.shift()
170
+ while (item !== null && item.queued === false) {
171
+ item = queue.shift()
172
+ }
173
+
174
+ if (item !== null) {
175
+ this[kQueued]--
176
+ item.queued = false
177
+ }
178
+
179
+ return item
180
+ }
181
+
182
+ // Called through the handler's kQueuedAbort hook when a queued request is
183
+ // aborted.
184
+ [kAbortQueued] (item, reason) {
185
+ if (!item.queued) {
186
+ return
187
+ }
188
+
189
+ const { handler } = item
190
+ item.queued = false
191
+ item.opts = null
192
+ item.handler = null
193
+ this[kQueued]--
194
+
195
+ item.runInAsyncScope(handler.onResponseError, handler, createAbortedController(reason), reason)
196
+
197
+ if (this[kClosedResolve] && this[kQueued] === 0) {
198
+ closeClients(this).then(this[kClosedResolve])
199
+ }
200
+ }
201
+
154
202
  [kOnConnect] = (origin, targets) => {
155
203
  this.emit('connect', origin, [this, ...targets])
156
204
  };
@@ -212,7 +260,7 @@ class PoolBase extends DispatcherBase {
212
260
  }
213
261
 
214
262
  [kClose] () {
215
- if (this[kQueue].isEmpty()) {
263
+ if (this[kQueued] === 0) {
216
264
  return closeClients(this)
217
265
  } else {
218
266
  return new Promise((resolve) => {
@@ -223,7 +271,7 @@ class PoolBase extends DispatcherBase {
223
271
 
224
272
  [kDestroy] (err) {
225
273
  while (true) {
226
- const item = this[kQueue].shift()
274
+ const item = this[kShiftQueue]()
227
275
  if (!item) {
228
276
  break
229
277
  }
@@ -244,9 +292,17 @@ class PoolBase extends DispatcherBase {
244
292
  const dispatcher = this[kGetDispatcher]()
245
293
 
246
294
  if (!dispatcher) {
295
+ const item = new QueuedRequest(opts, handler)
247
296
  this[kNeedDrain] = true
248
- this[kQueue].push(new QueuedRequest(opts, handler))
297
+ this[kQueue].push(item)
249
298
  this[kQueued]++
299
+
300
+ const onQueued = opts[kQueuedAbort]
301
+ if (onQueued !== undefined) {
302
+ // Give the handler a way to remove the request from the queue as soon
303
+ // as it is aborted.
304
+ onQueued((reason) => this[kAbortQueued](item, reason))
305
+ }
250
306
  } else if (!dispatcher.dispatch(opts, handler)) {
251
307
  dispatcher[kNeedDrain] = true
252
308
  this[kOnClientBusy](dispatcher)
@@ -18,7 +18,7 @@ const {
18
18
  InvalidArgumentError
19
19
  } = require('../core/errors')
20
20
  const util = require('../core/util')
21
- const { kConnecting, kHTTPContext, kUrl, kQueued } = require('../core/symbols')
21
+ const { kConnecting, kHTTPContext, kHTTP2Session, kRemoteSettings, kUrl, kQueued } = require('../core/symbols')
22
22
  const buildConnector = require('../core/connect')
23
23
 
24
24
  const kOptions = Symbol('options')
@@ -116,12 +116,25 @@ class Pool extends PoolBase {
116
116
  }
117
117
 
118
118
  if (client === this[kProtocolProbe]) {
119
- // An h2 Client's drain event releases the requests accumulated during
120
- // negotiation onto that Client. If ALPN selected h1, release the probe
121
- // immediately and restore normal Pool fan-out instead.
122
- if (this[kProtocol] !== 'h2') {
123
- this[kProtocolProbe] = null
124
- this[kDrainQueue](origin, targets.slice(1))
119
+ const releaseProbe = () => {
120
+ if (client === this[kProtocolProbe]) {
121
+ this[kProtocolProbe] = null
122
+ // An empty queue must not release backpressure when this client
123
+ // is still saturated and the connection limit prevents fan-out.
124
+ // Check capacity without invoking a factory for unused capacity.
125
+ if (!this.destroyed && (this[kQueued] > 0 || (!this.closed && this[kHasDispatcher](false)))) {
126
+ this[kDrainQueue](origin, targets.slice(1))
127
+ }
128
+ }
129
+ }
130
+ const session = client[kHTTP2Session]
131
+ // A peer's stream limit can keep the probe busy indefinitely. Once
132
+ // SETTINGS arrive, queued work may need another connection without
133
+ // waiting for this client's drain event.
134
+ if (this[kProtocol] !== 'h2' || session[kRemoteSettings]) {
135
+ releaseProbe()
136
+ } else {
137
+ session.once('remoteSettings', releaseProbe)
125
138
  }
126
139
  }
127
140
  })
@@ -200,7 +213,7 @@ class Pool extends PoolBase {
200
213
  }
201
214
  }
202
215
 
203
- [kHasDispatcher] () {
216
+ [kHasDispatcher] (create = true) {
204
217
  const clientTtlOption = this[kOptions].clientTtl
205
218
  for (let i = 0; i < this[kClients].length; i++) {
206
219
  const client = this[kClients][i]
@@ -218,7 +231,7 @@ class Pool extends PoolBase {
218
231
  }
219
232
 
220
233
  if (!this[kConnections] || this[kClients].length < this[kConnections]) {
221
- createClient(this)
234
+ if (create) createClient(this)
222
235
  return true
223
236
  }
224
237
 
@@ -412,7 +412,7 @@ class CacheHandler {
412
412
  deleteCachedValue(this.#store, this.#cacheKey)
413
413
  }
414
414
 
415
- // Don't have anything to tell us this response is cachable and we're not
415
+ // Don't have anything to tell us this response is cacheable and we're not
416
416
  // caching by default
417
417
  return downstreamOnHeaders()
418
418
  }
@@ -1,7 +1,7 @@
1
1
  'use strict'
2
2
  const assert = require('node:assert')
3
3
 
4
- const { kRetryHandlerDefaultRetry } = require('../core/symbols')
4
+ const { kRetryHandlerDefaultRetry, kQueuedAbort } = require('../core/symbols')
5
5
  const { RequestRetryError, RequestAbortedError } = require('../core/errors')
6
6
  const {
7
7
  hasSafeIterator,
@@ -98,6 +98,11 @@ class RetryController {
98
98
  }
99
99
 
100
100
  class RetryHandler {
101
+ // Takes the retry that waits for a connection out of its queue.
102
+ #queuedCancel = null
103
+ // Whether retries are dispatched with a hook that sets #queuedCancel.
104
+ #tracksQueuedRetry = false
105
+
101
106
  constructor (opts, { dispatch, handler }) {
102
107
  const { retryOptions, ...dispatchOpts } = opts
103
108
  const {
@@ -561,6 +566,18 @@ class RetryHandler {
561
566
  }
562
567
  }
563
568
 
569
+ if (!this.#tracksQueuedRetry) {
570
+ // The consumer's controller still targets the earlier attempt, so an
571
+ // abort would not reach a retry that waits for a connection. Keep the
572
+ // function that takes it out of its queue; the caller's hook still runs.
573
+ this.#tracksQueuedRetry = true
574
+ const onQueued = this.opts[kQueuedAbort]
575
+ this.opts[kQueuedAbort] = (cancel) => {
576
+ this.#queuedCancel = cancel
577
+ onQueued?.(cancel)
578
+ }
579
+ }
580
+
564
581
  try {
565
582
  this.retryCountCheckpoint = this.retryCount
566
583
  this.dispatch(this.opts, this)
@@ -622,6 +639,15 @@ class RetryHandler {
622
639
  }
623
640
 
624
641
  #onAbort (reason) {
642
+ // A retry that waits for a connection is taken out of its queue: its
643
+ // dispatcher fails it with an aborted controller, so onResponseError passes
644
+ // the abort on instead of retrying. Once the retry started, this does nothing.
645
+ const cancel = this.#queuedCancel
646
+ if (cancel !== null) {
647
+ this.#queuedCancel = null
648
+ cancel(reason ?? new RequestAbortedError())
649
+ }
650
+
625
651
  // A consumer abort lands on the controller proxy. If the retry policy is
626
652
  // still deciding (typically holding a backoff timer), cancel the wait and
627
653
  // surface the abort immediately instead of letting the request hang until
@@ -18,7 +18,9 @@ const {
18
18
  isEtagUsable
19
19
  } = require('../util/cache.js')
20
20
  const { AbortError } = require('../core/errors.js')
21
+ const { kQueuedAbort } = require('../core/symbols.js')
21
22
  const { parseHttpDate } = require('../util/date.js')
23
+ const { kInterceptorKind } = require('../core/symbols.js')
22
24
 
23
25
  /**
24
26
  * @param {(string | RegExp)[] | undefined} origins
@@ -541,7 +543,10 @@ function handleResult (
541
543
  dispatch(
542
544
  {
543
545
  ...opts,
544
- headers
546
+ headers,
547
+ // The revalidation is not the caller's request: aborting the
548
+ // caller must not cancel it while it waits for a connection.
549
+ [kQueuedAbort]: undefined
545
550
  },
546
551
  new CacheHandler(globalOpts, cacheKey, {
547
552
  // Silent handler that just updates the cache
@@ -665,7 +670,7 @@ module.exports = (opts = {}) => {
665
670
  }
666
671
  }
667
672
 
668
- return (dispatch, interceptorOrigin) => {
673
+ const interceptor = (dispatch, interceptorOrigin) => {
669
674
  return (opts, handler) => {
670
675
  const requestOrigin = getInterceptorOrigin(opts, interceptorOrigin)
671
676
  if (!requestOrigin || arrayIncludes(safeMethodsToNotCache, opts.method)) {
@@ -768,4 +773,6 @@ module.exports = (opts = {}) => {
768
773
  }
769
774
  }
770
775
  }
776
+ interceptor[kInterceptorKind] = 'cache'
777
+ return interceptor
771
778
  }
@@ -105,8 +105,6 @@ function createMaxSizeLimiter (maxSize) {
105
105
  })
106
106
  }
107
107
 
108
- let warningEmitted = /** @type {boolean} */ (false)
109
-
110
108
  /**
111
109
  * @typedef {Object} DecompressHandlerOptions
112
110
  * @property {number[]|Readonly<number[]>} [skipStatusCodes=[204, 304]] - List of status codes to skip decompression for
@@ -550,15 +548,6 @@ class DecompressHandler extends DecoratorHandler {
550
548
  * @returns {Function} - Interceptor function
551
549
  */
552
550
  function createDecompressInterceptor (options = {}) {
553
- // Emit experimental warning only once
554
- if (!warningEmitted) {
555
- process.emitWarning(
556
- 'DecompressInterceptor is experimental and subject to change',
557
- 'ExperimentalWarning'
558
- )
559
- warningEmitted = true
560
- }
561
-
562
551
  return (dispatch) => {
563
552
  return (opts, handler) => {
564
553
  if (opts.method === 'HEAD') {
@@ -4,6 +4,7 @@ const diagnosticsChannel = require('node:diagnostics_channel')
4
4
  const util = require('../core/util')
5
5
  const DeduplicationHandler = require('../handler/deduplication-handler')
6
6
  const { getInterceptorOrigin, normalizeHeaders, makeCacheKey, makeDeduplicationKey } = require('../util/cache.js')
7
+ const { kInterceptorKind } = require('../core/symbols.js')
7
8
 
8
9
  const pendingRequestsChannel = diagnosticsChannel.channel('undici:request:pending-requests')
9
10
 
@@ -57,7 +58,7 @@ module.exports = (opts = {}) => {
57
58
  */
58
59
  const pendingRequests = new Map()
59
60
 
60
- return (dispatch, interceptorOrigin) => {
61
+ const interceptor = (dispatch, interceptorOrigin) => {
61
62
  return (opts, handler) => {
62
63
  const requestOrigin = getInterceptorOrigin(opts, interceptorOrigin)
63
64
  if (!requestOrigin || opts.upgrade || methods.includes(opts.method) === false) {
@@ -115,4 +116,6 @@ module.exports = (opts = {}) => {
115
116
  return dispatch(opts, deduplicationHandler)
116
117
  }
117
118
  }
119
+ interceptor[kInterceptorKind] = 'deduplicate'
120
+ return interceptor
118
121
  }
@@ -1,9 +1,10 @@
1
1
  'use strict'
2
2
 
3
3
  const RedirectHandler = require('../handler/redirect-handler')
4
+ const { kInterceptorKind } = require('../core/symbols.js')
4
5
 
5
6
  function createRedirectInterceptor ({ maxRedirections: defaultMaxRedirections, throwOnMaxRedirect: defaultThrowOnMaxRedirect, stripHeadersOnRedirect: defaultStripHeadersOnRedirect, stripHeadersOnCrossOriginRedirect: defaultStripHeadersOnCrossOriginRedirect } = {}) {
6
- return (dispatch) => {
7
+ const interceptor = (dispatch) => {
7
8
  return function Intercept (opts, handler) {
8
9
  const { maxRedirections = defaultMaxRedirections, throwOnMaxRedirect = defaultThrowOnMaxRedirect, stripHeadersOnRedirect = defaultStripHeadersOnRedirect, stripHeadersOnCrossOriginRedirect = defaultStripHeadersOnCrossOriginRedirect, ...rest } = opts
9
10
 
@@ -16,6 +17,8 @@ function createRedirectInterceptor ({ maxRedirections: defaultMaxRedirections, t
16
17
  return dispatch(dispatchOpts, redirectHandler)
17
18
  }
18
19
  }
20
+ interceptor[kInterceptorKind] = 'redirect'
21
+ return interceptor
19
22
  }
20
23
 
21
24
  module.exports = createRedirectInterceptor
@@ -650,7 +650,7 @@ function isInvalidOrWildcardVaryHeader (varyHeader) {
650
650
 
651
651
  /**
652
652
  * Note: this deviates from the spec a little. Empty etags ("", W/"") are valid,
653
- * however, including them in cached resposnes serves little to no purpose.
653
+ * however, including them in cached responses serves little to no purpose.
654
654
  *
655
655
  * @see https://www.rfc-editor.org/rfc/rfc9110.html#name-etag
656
656
  *
@@ -194,7 +194,7 @@ function refreshTimeout () {
194
194
  // method (e.g. mocked timers in tests).
195
195
  if (fastNowTimeout?.refresh) {
196
196
  fastNowTimeout.refresh()
197
- // fastNowTimeout is not instantiated yet or refresh is not availabe,
197
+ // fastNowTimeout is not instantiated yet or refresh is not available,
198
198
  // create a new Timer.
199
199
  } else {
200
200
  clearTimeout(fastNowTimeout)