deps: update undici to 5.12.0 · nodejs/node@38202d3 · GitHub
Skip to content

Commit 38202d3

Browse files
nodejs-github-botrichardlau
authored andcommitted
deps: update undici to 5.12.0
PR-URL: #45236 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
1 parent 6d736a5 commit 38202d3

40 files changed

Lines changed: 9571 additions & 4711 deletions

deps/undici/src/docs/api/Client.md

Lines changed: 1 addition & 0 deletions

deps/undici/src/docs/api/Errors.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { errors } from 'undici'
1919
| `RequestContentLengthMismatchError` | `UND_ERR_REQ_CONTENT_LENGTH_MISMATCH` | request body does not match content-length header |
2020
| `ResponseContentLengthMismatchError` | `UND_ERR_RES_CONTENT_LENGTH_MISMATCH` | response body does not match content-length header |
2121
| `InformationalError` | `UND_ERR_INFO` | expected error with reason |
22+
| `ResponseExceededMaxSizeError` | `UND_ERR_RES_EXCEEDED_MAX_SIZE` | response body exceed the max size allowed |
2223

2324
### `SocketError`
2425

deps/undici/src/docs/api/ProxyAgent.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Returns: `ProxyAgent`
1717
Extends: [`AgentOptions`](Agent.md#parameter-agentoptions)
1818

1919
* **uri** `string` (required) - It can be passed either by a string or a object containing `uri` as string.
20+
* **token** `string` (optional) - It can be passed by a string of token for authentication.
21+
* **auth** `string` (**deprecated**) - Use token.
2022

2123
Examples:
2224

@@ -74,6 +76,26 @@ for await (const data of body) {
7476
}
7577
```
7678

79+
#### Example - Basic Proxy Request with authentication
80+
81+
```js
82+
import { setGlobalDispatcher, request, ProxyAgent } from 'undici';
83+
84+
const proxyAgent = new ProxyAgent({
85+
uri: 'my.proxy.server',
86+
token: 'Bearer xxxx'
87+
});
88+
setGlobalDispatcher(proxyAgent);
89+
90+
const { statusCode, body } = await request('http://localhost:3000/foo');
91+
92+
console.log('response received', statusCode); // response received 200
93+
94+
for await (const data of body) {
95+
console.log('data', data.toString('utf8')); // data foo
96+
}
97+
```
98+
7799
### `ProxyAgent.close()`
78100

79101
Closes the proxy agent and waits for registered pools and clients to also close before resolving.

deps/undici/src/index-fetch.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
'use strict'
22

3-
const { getGlobalDispatcher } = require('./lib/global')
43
const fetchImpl = require('./lib/fetch').fetch
54

65
module.exports.fetch = async function fetch (resource) {
7-
const dispatcher = (arguments[1] && arguments[1].dispatcher) || getGlobalDispatcher()
86
try {
9-
return await fetchImpl.apply(dispatcher, arguments)
7+
return await fetchImpl(...arguments)
108
} catch (err) {
119
Error.captureStackTrace(err, this)
1210
throw err

deps/undici/src/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { request, pipeline, stream, connect, upgrade } from './types/api'
1818

1919
export * from './types/fetch'
2020
export * from './types/file'
21+
export * from './types/filereader'
2122
export * from './types/formdata'
2223
export * from './types/diagnostics-channel'
2324
export { Interceptable } from './types/mock-interceptor'

deps/undici/src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ if (nodeMajor > 16 || (nodeMajor === 16 && nodeMinor >= 8)) {
9898
if (!fetchImpl) {
9999
fetchImpl = require('./lib/fetch').fetch
100100
}
101-
const dispatcher = (arguments[1] && arguments[1].dispatcher) || getGlobalDispatcher()
101+
102102
try {
103-
return await fetchImpl.apply(dispatcher, arguments)
103+
return await fetchImpl(...arguments)
104104
} catch (err) {
105105
Error.captureStackTrace(err, this)
106106
throw err
@@ -111,6 +111,7 @@ if (nodeMajor > 16 || (nodeMajor === 16 && nodeMinor >= 8)) {
111111
module.exports.Request = require('./lib/fetch/request').Request
112112
module.exports.FormData = require('./lib/fetch/formdata').FormData
113113
module.exports.File = require('./lib/fetch/file').File
114+
module.exports.FileReader = require('./lib/fileapi/filereader').FileReader
114115

115116
const { setGlobalOrigin, getGlobalOrigin } = require('./lib/fetch/global')
116117

deps/undici/src/lib/api/api-stream.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class StreamHandler extends AsyncResource {
106106
}
107107

108108
res.on('drain', resume)
109-
// TODO: Avoid finished. It registers an unecessary amount of listeners.
109+
// TODO: Avoid finished. It registers an unnecessary amount of listeners.
110110
finished(res, { readable: false }, (err) => {
111111
const { callback, res, opaque, trailers, abort } = this
112112

deps/undici/src/lib/client.js

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ const {
1717
SocketError,
1818
InformationalError,
1919
BodyTimeoutError,
20-
HTTPParserError
20+
HTTPParserError,
21+
ResponseExceededMaxSizeError
2122
} = require('./core/errors')
2223
const buildConnector = require('./core/connect')
2324
const {
@@ -60,7 +61,9 @@ const {
6061
kClose,
6162
kDestroy,
6263
kDispatch,
63-
kInterceptors
64+
kInterceptors,
65+
kLocalAddress,
66+
kMaxResponseSize
6467
} = require('./core/symbols')
6568

6669
const kClosedResolve = Symbol('kClosedResolve')
@@ -102,7 +105,9 @@ class Client extends DispatcherBase {
102105
maxCachedSessions,
103106
maxRedirections,
104107
connect,
105-
maxRequestsPerClient
108+
maxRequestsPerClient,
109+
localAddress,
110+
maxResponseSize
106111
} = {}) {
107112
super()
108113

@@ -170,6 +175,14 @@ class Client extends DispatcherBase {
170175
throw new InvalidArgumentError('maxRequestsPerClient must be a positive number')
171176
}
172177

178+
if (localAddress != null && (typeof localAddress !== 'string' || net.isIP(localAddress) === 0)) {
179+
throw new InvalidArgumentError('localAddress must be valid string IP address')
180+
}
181+
182+
if (maxResponseSize != null && (!Number.isInteger(maxResponseSize) || maxResponseSize < -1)) {
183+
throw new InvalidArgumentError('maxResponseSize must be a positive number')
184+
}
185+
173186
if (typeof connect !== 'function') {
174187
connect = buildConnector({
175188
...tls,
@@ -193,6 +206,7 @@ class Client extends DispatcherBase {
193206
this[kKeepAliveTimeoutThreshold] = keepAliveTimeoutThreshold == null ? 1e3 : keepAliveTimeoutThreshold
194207
this[kKeepAliveTimeoutValue] = this[kKeepAliveDefaultTimeout]
195208
this[kServerName] = null
209+
this[kLocalAddress] = localAddress != null ? localAddress : null
196210
this[kResuming] = 0 // 0, idle, 1, scheduled, 2 resuming
197211
this[kNeedDrain] = 0 // 0, idle, 1, scheduled, 2 resuming
198212
this[kHostHeader] = `host: ${this[kUrl].hostname}${this[kUrl].port ? `:${this[kUrl].port}` : ''}\r\n`
@@ -202,6 +216,7 @@ class Client extends DispatcherBase {
202216
this[kMaxRedirections] = maxRedirections
203217
this[kMaxRequests] = maxRequestsPerClient
204218
this[kClosedResolve] = null
219+
this[kMaxResponseSize] = maxResponseSize > -1 ? maxResponseSize : -1
205220

206221
// kQueue is built up of 3 sections separated by
207222
// the kRunningIdx and kPendingIdx indices.
@@ -426,6 +441,7 @@ class Parser {
426441

427442
this.keepAlive = ''
428443
this.contentLength = ''
444+
this.maxResponseSize = client[kMaxResponseSize]
429445
}
430446

431447
setTimeout (value, type) {
@@ -542,19 +558,6 @@ class Parser {
542558
}
543559
}
544560

545-
finish () {
546-
try {
547-
try {
548-
currentParser = this
549-
} finally {
550-
currentParser = null
551-
}
552-
} catch (err) {
553-
/* istanbul ignore next: difficult to make a test case for */
554-
util.destroy(this.socket, err)
555-
}
556-
}
557-
558561
destroy () {
559562
assert(this.ptr != null)
560563
assert(currentParser == null)
@@ -783,7 +786,7 @@ class Parser {
783786
}
784787

785788
onBody (buf) {
786-
const { client, socket, statusCode } = this
789+
const { client, socket, statusCode, maxResponseSize } = this
787790

788791
if (socket.destroyed) {
789792
return -1
@@ -802,6 +805,11 @@ class Parser {
802805

803806
assert(statusCode >= 200)
804807

808+
if (maxResponseSize > -1 && this.bytesRead + buf.length > maxResponseSize) {
809+
util.destroy(socket, new ResponseExceededMaxSizeError())
810+
return -1
811+
}
812+
805813
this.bytesRead += buf.length
806814

807815
try {
@@ -917,7 +925,7 @@ function onSocketError (err) {
917925
// to the user.
918926
if (err.code === 'ECONNRESET' && parser.statusCode && !parser.shouldKeepAlive) {
919927
// We treat all incoming data so for as a valid response.
920-
parser.finish()
928+
parser.onMessageComplete()
921929
return
922930
}
923931

@@ -951,7 +959,7 @@ function onSocketEnd () {
951959

952960
if (parser.statusCode && !parser.shouldKeepAlive) {
953961
// We treat all incoming data so far as a valid response.
954-
parser.finish()
962+
parser.onMessageComplete()
955963
return
956964
}
957965

@@ -961,6 +969,11 @@ function onSocketEnd () {
961969
function onSocketClose () {
962970
const { [kClient]: client } = this
963971

972+
if (!this[kError] && this[kParser].statusCode && !this[kParser].shouldKeepAlive) {
973+
// We treat all incoming data so far as a valid response.
974+
this[kParser].onMessageComplete()
975+
}
976+
964977
this[kParser].destroy()
965978
this[kParser] = null
966979

@@ -1020,7 +1033,8 @@ async function connect (client) {
10201033
hostname,
10211034
protocol,
10221035
port,
1023-
servername: client[kServerName]
1036+
servername: client[kServerName],
1037+
localAddress: client[kLocalAddress]
10241038
},
10251039
connector: client[kConnector]
10261040
})
@@ -1033,7 +1047,8 @@ async function connect (client) {
10331047
hostname,
10341048
protocol,
10351049
port,
1036-
servername: client[kServerName]
1050+
servername: client[kServerName],
1051+
localAddress: client[kLocalAddress]
10371052
}, (err, socket) => {
10381053
if (err) {
10391054
reject(err)
@@ -1076,7 +1091,8 @@ async function connect (client) {
10761091
hostname,
10771092
protocol,
10781093
port,
1079-
servername: client[kServerName]
1094+
servername: client[kServerName],
1095+
localAddress: client[kLocalAddress]
10801096
},
10811097
connector: client[kConnector],
10821098
socket
@@ -1093,7 +1109,8 @@ async function connect (client) {
10931109
hostname,
10941110
protocol,
10951111
port,
1096-
servername: client[kServerName]
1112+
servername: client[kServerName],
1113+
localAddress: client[kLocalAddress]
10971114
},
10981115
connector: client[kConnector],
10991116
error: err

deps/undici/src/lib/core/connect.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function buildConnector ({ maxCachedSessions, socketPath, timeout, ...opts }) {
2121
timeout = timeout == null ? 10e3 : timeout
2222
maxCachedSessions = maxCachedSessions == null ? 100 : maxCachedSessions
2323

24-
return function connect ({ hostname, host, protocol, port, servername, httpSocket }, callback) {
24+
return function connect ({ hostname, host, protocol, port, servername, localAddress, httpSocket }, callback) {
2525
let socket
2626
if (protocol === 'https:') {
2727
if (!tls) {
@@ -39,6 +39,7 @@ function buildConnector ({ maxCachedSessions, socketPath, timeout, ...opts }) {
3939
...options,
4040
servername,
4141
session,
42+
localAddress,
4243
socket: httpSocket, // upgrade socket connection
4344
port: port || 443,
4445
host: hostname
@@ -70,6 +71,7 @@ function buildConnector ({ maxCachedSessions, socketPath, timeout, ...opts }) {
7071
socket = net.connect({
7172
highWaterMark: 64 * 1024, // Same as nodejs fs streams.
7273
...options,
74+
localAddress,
7375
port: port || 80,
7476
host: hostname
7577
})

deps/undici/src/lib/core/errors.js

Lines changed: 12 additions & 1 deletion

0 commit comments

Comments
 (0)