undici 4.16.0 → 5.0.0
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/docs/api/MockAgent.md +9 -45
- package/docs/api/MockClient.md +1 -4
- package/docs/api/MockPool.md +1 -5
- package/lib/agent.js +30 -98
- package/lib/client.js +59 -159
- package/lib/core/symbols.js +3 -0
- package/lib/dispatcher-base.js +159 -0
- package/lib/fetch/constants.js +3 -0
- package/lib/fetch/headers.js +4 -3
- package/lib/fetch/index.js +535 -489
- package/lib/fetch/request.js +9 -17
- package/lib/fetch/response.js +79 -30
- package/lib/fetch/util.js +20 -8
- package/lib/mock/mock-interceptor.js +1 -1
- package/lib/mock/mock-utils.js +8 -0
- package/lib/pool-base.js +21 -99
- package/lib/proxy-agent.js +8 -4
- package/package.json +3 -3
- package/types/mock-interceptor.d.ts +2 -2
package/docs/api/MockAgent.md
CHANGED
|
@@ -72,11 +72,7 @@ const mockAgent = new MockAgent()
|
|
|
72
72
|
setGlobalDispatcher(mockAgent)
|
|
73
73
|
|
|
74
74
|
const mockPool = mockAgent.get('http://localhost:3000')
|
|
75
|
-
|
|
76
|
-
mockPool.intercept({
|
|
77
|
-
path: '/foo',
|
|
78
|
-
method: 'GET'
|
|
79
|
-
}).reply(200, 'foo')
|
|
75
|
+
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')
|
|
80
76
|
|
|
81
77
|
const { statusCode, body } = await request('http://localhost:3000/foo')
|
|
82
78
|
|
|
@@ -95,11 +91,7 @@ import { MockAgent, request } from 'undici'
|
|
|
95
91
|
const mockAgent = new MockAgent()
|
|
96
92
|
|
|
97
93
|
const mockPool = mockAgent.get('http://localhost:3000')
|
|
98
|
-
|
|
99
|
-
mockPool.intercept({
|
|
100
|
-
path: '/foo',
|
|
101
|
-
method: 'GET'
|
|
102
|
-
}).reply(200, 'foo')
|
|
94
|
+
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')
|
|
103
95
|
|
|
104
96
|
const {
|
|
105
97
|
statusCode,
|
|
@@ -121,11 +113,7 @@ import { MockAgent, request } from 'undici'
|
|
|
121
113
|
const mockAgent = new MockAgent()
|
|
122
114
|
|
|
123
115
|
const mockPool = mockAgent.get('http://localhost:3000')
|
|
124
|
-
|
|
125
|
-
mockPool.intercept({
|
|
126
|
-
path: '/foo',
|
|
127
|
-
method: 'GET'
|
|
128
|
-
}).reply(200, 'foo')
|
|
116
|
+
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')
|
|
129
117
|
|
|
130
118
|
const {
|
|
131
119
|
statusCode,
|
|
@@ -147,11 +135,7 @@ import { MockAgent, request } from 'undici'
|
|
|
147
135
|
const mockAgent = new MockAgent({ connections: 1 })
|
|
148
136
|
|
|
149
137
|
const mockClient = mockAgent.get('http://localhost:3000')
|
|
150
|
-
|
|
151
|
-
mockClient.intercept({
|
|
152
|
-
path: '/foo',
|
|
153
|
-
method: 'GET'
|
|
154
|
-
}).reply(200, 'foo')
|
|
138
|
+
mockClient.intercept({ path: '/foo' }).reply(200, 'foo')
|
|
155
139
|
|
|
156
140
|
const {
|
|
157
141
|
statusCode,
|
|
@@ -174,16 +158,8 @@ const mockAgent = new MockAgent()
|
|
|
174
158
|
setGlobalDispatcher(mockAgent)
|
|
175
159
|
|
|
176
160
|
const mockPool = mockAgent.get('http://localhost:3000')
|
|
177
|
-
|
|
178
|
-
mockPool.intercept({
|
|
179
|
-
path: '/foo',
|
|
180
|
-
method: 'GET'
|
|
181
|
-
}).reply(200, 'foo')
|
|
182
|
-
|
|
183
|
-
mockPool.intercept({
|
|
184
|
-
path: '/hello',
|
|
185
|
-
method: 'GET'
|
|
186
|
-
}).reply(200, 'hello')
|
|
161
|
+
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')
|
|
162
|
+
mockPool.intercept({ path: '/hello'}).reply(200, 'hello')
|
|
187
163
|
|
|
188
164
|
const result1 = await request('http://localhost:3000/foo')
|
|
189
165
|
|
|
@@ -250,11 +226,7 @@ const mockAgent = new MockAgent()
|
|
|
250
226
|
setGlobalDispatcher(mockAgent)
|
|
251
227
|
|
|
252
228
|
const mockPool = mockAgent.get(new RegExp('http://localhost:3000'))
|
|
253
|
-
|
|
254
|
-
mockPool.intercept({
|
|
255
|
-
path: '/foo',
|
|
256
|
-
method: 'GET',
|
|
257
|
-
}).reply(200, 'foo')
|
|
229
|
+
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')
|
|
258
230
|
|
|
259
231
|
const {
|
|
260
232
|
statusCode,
|
|
@@ -277,11 +249,7 @@ const mockAgent = new MockAgent()
|
|
|
277
249
|
setGlobalDispatcher(mockAgent)
|
|
278
250
|
|
|
279
251
|
const mockPool = mockAgent.get((origin) => origin === 'http://localhost:3000')
|
|
280
|
-
|
|
281
|
-
mockPool.intercept({
|
|
282
|
-
path: '/foo',
|
|
283
|
-
method: 'GET'
|
|
284
|
-
}).reply(200, 'foo')
|
|
252
|
+
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')
|
|
285
253
|
|
|
286
254
|
const {
|
|
287
255
|
statusCode,
|
|
@@ -328,11 +296,7 @@ import { MockAgent } from 'undici'
|
|
|
328
296
|
const mockAgent = new MockAgent()
|
|
329
297
|
|
|
330
298
|
const mockPool = mockAgent.get('http://localhost:3000')
|
|
331
|
-
|
|
332
|
-
mockPool.intercept({
|
|
333
|
-
path: '/foo',
|
|
334
|
-
method: 'GET'
|
|
335
|
-
}).reply(200, 'foo')
|
|
299
|
+
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')
|
|
336
300
|
|
|
337
301
|
const {
|
|
338
302
|
statusCode,
|
package/docs/api/MockClient.md
CHANGED
|
@@ -58,10 +58,7 @@ import { MockAgent } from 'undici'
|
|
|
58
58
|
const mockAgent = new MockAgent({ connections: 1 })
|
|
59
59
|
|
|
60
60
|
const mockClient = mockAgent.get('http://localhost:3000')
|
|
61
|
-
mockClient.intercept({
|
|
62
|
-
path: '/foo',
|
|
63
|
-
method: 'GET',
|
|
64
|
-
}).reply(200, 'foo')
|
|
61
|
+
mockClient.intercept({ path: '/foo' }).reply(200, 'foo')
|
|
65
62
|
|
|
66
63
|
const {
|
|
67
64
|
statusCode,
|
package/docs/api/MockPool.md
CHANGED
|
@@ -95,11 +95,7 @@ setGlobalDispatcher(mockAgent)
|
|
|
95
95
|
|
|
96
96
|
// MockPool
|
|
97
97
|
const mockPool = mockAgent.get('http://localhost:3000')
|
|
98
|
-
|
|
99
|
-
mockPool.intercept({
|
|
100
|
-
path: '/foo',
|
|
101
|
-
method: 'GET',
|
|
102
|
-
}).reply(200, 'foo')
|
|
98
|
+
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')
|
|
103
99
|
|
|
104
100
|
const {
|
|
105
101
|
statusCode,
|
package/lib/agent.js
CHANGED
|
@@ -1,20 +1,14 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
ClientDestroyedError
|
|
7
|
-
} = require('./core/errors')
|
|
8
|
-
const { kClients, kRunning } = require('./core/symbols')
|
|
9
|
-
const Dispatcher = require('./dispatcher')
|
|
3
|
+
const { InvalidArgumentError } = require('./core/errors')
|
|
4
|
+
const { kClients, kRunning, kClose, kDestroy, kDispatch } = require('./core/symbols')
|
|
5
|
+
const DispatcherBase = require('./dispatcher-base')
|
|
10
6
|
const Pool = require('./pool')
|
|
11
7
|
const Client = require('./client')
|
|
12
8
|
const util = require('./core/util')
|
|
13
9
|
const RedirectHandler = require('./handler/redirect')
|
|
14
10
|
const { WeakRef, FinalizationRegistry } = require('./compat/dispatcher-weakref')()
|
|
15
11
|
|
|
16
|
-
const kDestroyed = Symbol('destroyed')
|
|
17
|
-
const kClosed = Symbol('closed')
|
|
18
12
|
const kOnConnect = Symbol('onConnect')
|
|
19
13
|
const kOnDisconnect = Symbol('onDisconnect')
|
|
20
14
|
const kOnConnectionError = Symbol('onConnectionError')
|
|
@@ -30,7 +24,7 @@ function defaultFactory (origin, opts) {
|
|
|
30
24
|
: new Pool(origin, opts)
|
|
31
25
|
}
|
|
32
26
|
|
|
33
|
-
class Agent extends
|
|
27
|
+
class Agent extends DispatcherBase {
|
|
34
28
|
constructor ({ factory = defaultFactory, maxRedirections = 0, connect, ...options } = {}) {
|
|
35
29
|
super()
|
|
36
30
|
|
|
@@ -60,8 +54,6 @@ class Agent extends Dispatcher {
|
|
|
60
54
|
this[kClients].delete(key)
|
|
61
55
|
}
|
|
62
56
|
})
|
|
63
|
-
this[kClosed] = false
|
|
64
|
-
this[kDestroyed] = false
|
|
65
57
|
|
|
66
58
|
const agent = this
|
|
67
59
|
|
|
@@ -94,76 +86,38 @@ class Agent extends Dispatcher {
|
|
|
94
86
|
return ret
|
|
95
87
|
}
|
|
96
88
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
89
|
+
[kDispatch] (opts, handler) {
|
|
90
|
+
let key
|
|
91
|
+
if (opts.origin && (typeof opts.origin === 'string' || opts.origin instanceof URL)) {
|
|
92
|
+
key = String(opts.origin)
|
|
93
|
+
} else {
|
|
94
|
+
throw new InvalidArgumentError('opts.origin must be a non-empty string or URL.')
|
|
100
95
|
}
|
|
101
96
|
|
|
102
|
-
|
|
103
|
-
if (!opts || typeof opts !== 'object') {
|
|
104
|
-
throw new InvalidArgumentError('opts must be an object.')
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
let key
|
|
108
|
-
if (opts.origin && (typeof opts.origin === 'string' || opts.origin instanceof URL)) {
|
|
109
|
-
key = String(opts.origin)
|
|
110
|
-
} else {
|
|
111
|
-
throw new InvalidArgumentError('opts.origin must be a non-empty string or URL.')
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
if (this[kDestroyed]) {
|
|
115
|
-
throw new ClientDestroyedError()
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if (this[kClosed]) {
|
|
119
|
-
throw new ClientClosedError()
|
|
120
|
-
}
|
|
97
|
+
const ref = this[kClients].get(key)
|
|
121
98
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
.on('disconnect', this[kOnDisconnect])
|
|
130
|
-
.on('connectionError', this[kOnConnectionError])
|
|
131
|
-
|
|
132
|
-
this[kClients].set(key, new WeakRef(dispatcher))
|
|
133
|
-
this[kFinalizer].register(dispatcher, key)
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
const { maxRedirections = this[kMaxRedirections] } = opts
|
|
137
|
-
if (maxRedirections != null && maxRedirections !== 0) {
|
|
138
|
-
opts = { ...opts, maxRedirections: 0 } // Stop sub dispatcher from also redirecting.
|
|
139
|
-
handler = new RedirectHandler(this, maxRedirections, opts, handler)
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
return dispatcher.dispatch(opts, handler)
|
|
143
|
-
} catch (err) {
|
|
144
|
-
if (typeof handler.onError !== 'function') {
|
|
145
|
-
throw new InvalidArgumentError('invalid onError method')
|
|
146
|
-
}
|
|
99
|
+
let dispatcher = ref ? ref.deref() : null
|
|
100
|
+
if (!dispatcher) {
|
|
101
|
+
dispatcher = this[kFactory](opts.origin, this[kOptions])
|
|
102
|
+
.on('drain', this[kOnDrain])
|
|
103
|
+
.on('connect', this[kOnConnect])
|
|
104
|
+
.on('disconnect', this[kOnDisconnect])
|
|
105
|
+
.on('connectionError', this[kOnConnectionError])
|
|
147
106
|
|
|
148
|
-
|
|
107
|
+
this[kClients].set(key, new WeakRef(dispatcher))
|
|
108
|
+
this[kFinalizer].register(dispatcher, key)
|
|
149
109
|
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
get closed () {
|
|
153
|
-
return this[kClosed]
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
get destroyed () {
|
|
157
|
-
return this[kDestroyed]
|
|
158
|
-
}
|
|
159
110
|
|
|
160
|
-
|
|
161
|
-
if (
|
|
162
|
-
|
|
111
|
+
const { maxRedirections = this[kMaxRedirections] } = opts
|
|
112
|
+
if (maxRedirections != null && maxRedirections !== 0) {
|
|
113
|
+
opts = { ...opts, maxRedirections: 0 } // Stop sub dispatcher from also redirecting.
|
|
114
|
+
handler = new RedirectHandler(this, maxRedirections, opts, handler)
|
|
163
115
|
}
|
|
164
116
|
|
|
165
|
-
|
|
117
|
+
return dispatcher.dispatch(opts, handler)
|
|
118
|
+
}
|
|
166
119
|
|
|
120
|
+
async [kClose] () {
|
|
167
121
|
const closePromises = []
|
|
168
122
|
for (const ref of this[kClients].values()) {
|
|
169
123
|
const client = ref.deref()
|
|
@@ -173,27 +127,10 @@ class Agent extends Dispatcher {
|
|
|
173
127
|
}
|
|
174
128
|
}
|
|
175
129
|
|
|
176
|
-
|
|
177
|
-
return Promise.all(closePromises)
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
// Should never error.
|
|
181
|
-
Promise.all(closePromises).then(() => process.nextTick(callback))
|
|
130
|
+
await Promise.all(closePromises)
|
|
182
131
|
}
|
|
183
132
|
|
|
184
|
-
|
|
185
|
-
if (typeof err === 'function') {
|
|
186
|
-
callback = err
|
|
187
|
-
err = null
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
if (callback != null && typeof callback !== 'function') {
|
|
191
|
-
throw new InvalidArgumentError('callback must be a function')
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
this[kClosed] = true
|
|
195
|
-
this[kDestroyed] = true
|
|
196
|
-
|
|
133
|
+
async [kDestroy] (err) {
|
|
197
134
|
const destroyPromises = []
|
|
198
135
|
for (const ref of this[kClients].values()) {
|
|
199
136
|
const client = ref.deref()
|
|
@@ -203,12 +140,7 @@ class Agent extends Dispatcher {
|
|
|
203
140
|
}
|
|
204
141
|
}
|
|
205
142
|
|
|
206
|
-
|
|
207
|
-
return Promise.all(destroyPromises)
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
// Should never error.
|
|
211
|
-
Promise.all(destroyPromises).then(() => process.nextTick(callback))
|
|
143
|
+
await Promise.all(destroyPromises)
|
|
212
144
|
}
|
|
213
145
|
}
|
|
214
146
|
|