Skip to content

Commit

Permalink
deps: update undici to 5.12.0
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
nodejs-github-bot authored and danielleadams committed Jan 3, 2023
1 parent a67da59 commit bb6e8b1
Show file tree
Hide file tree
Showing 40 changed files with 9,571 additions and 4,711 deletions.
1 change: 1 addition & 0 deletions deps/undici/src/docs/api/Client.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Returns: `Client`
* **keepAliveTimeout** `number | null` (optional) - Default: `4e3` - The timeout after which a socket without active requests will time out. Monitors time between activity on a connected socket. This value may be overridden by *keep-alive* hints from the server. See [MDN: HTTP - Headers - Keep-Alive directives](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Keep-Alive#directives) for more details. Defaults to 4 seconds.
* **keepAliveTimeoutThreshold** `number | null` (optional) - Default: `1e3` - A number subtracted from server *keep-alive* hints when overriding `keepAliveTimeout` to account for timing inaccuracies caused by e.g. transport latency. Defaults to 1 second.
* **maxHeaderSize** `number | null` (optional) - Default: `16384` - The maximum length of request headers in bytes. Defaults to 16KiB.
* **maxResponseSize** `number | null` (optional) - Default: `-1` - The maximum length of response body in bytes. Set to `-1` to disable.
* **pipelining** `number | null` (optional) - Default: `1` - The amount of concurrent requests to be sent over the single TCP/TLS connection according to [RFC7230](https://tools.ietf.org/html/rfc7230#section-6.3.2). Carefully consider your workload and environment before enabling concurrent requests as pipelining may reduce performance if used incorrectly. Pipelining is sensitive to network stack settings as well as head of line blocking caused by e.g. long running requests. Set to `0` to disable keep-alive connections.
* **connect** `ConnectOptions | Function | null` (optional) - Default: `null`.
* **strictContentLength** `Boolean` (optional) - Default: `true` - Whether to treat request content length mismatches as errors. If true, an error is thrown when the request content-length header doesn't match the length of the request body.
Expand Down
1 change: 1 addition & 0 deletions deps/undici/src/docs/api/Errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { errors } from 'undici'
| `RequestContentLengthMismatchError` | `UND_ERR_REQ_CONTENT_LENGTH_MISMATCH` | request body does not match content-length header |
| `ResponseContentLengthMismatchError` | `UND_ERR_RES_CONTENT_LENGTH_MISMATCH` | response body does not match content-length header |
| `InformationalError` | `UND_ERR_INFO` | expected error with reason |
| `ResponseExceededMaxSizeError` | `UND_ERR_RES_EXCEEDED_MAX_SIZE` | response body exceed the max size allowed |

### `SocketError`

Expand Down
22 changes: 22 additions & 0 deletions deps/undici/src/docs/api/ProxyAgent.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Returns: `ProxyAgent`
Extends: [`AgentOptions`](Agent.md#parameter-agentoptions)

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

Examples:

Expand Down Expand Up @@ -74,6 +76,26 @@ for await (const data of body) {
}
```

#### Example - Basic Proxy Request with authentication

```js
import { setGlobalDispatcher, request, ProxyAgent } from 'undici';

const proxyAgent = new ProxyAgent({
uri: 'my.proxy.server',
token: 'Bearer xxxx'
});
setGlobalDispatcher(proxyAgent);

const { statusCode, body } = await request('http://localhost:3000/foo');

console.log('response received', statusCode); // response received 200

for await (const data of body) {
console.log('data', data.toString('utf8')); // data foo
}
```

### `ProxyAgent.close()`

Closes the proxy agent and waits for registered pools and clients to also close before resolving.
Expand Down
4 changes: 1 addition & 3 deletions deps/undici/src/index-fetch.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
'use strict'

const { getGlobalDispatcher } = require('./lib/global')
const fetchImpl = require('./lib/fetch').fetch

module.exports.fetch = async function fetch (resource) {
const dispatcher = (arguments[1] && arguments[1].dispatcher) || getGlobalDispatcher()
try {
return await fetchImpl.apply(dispatcher, arguments)
return await fetchImpl(...arguments)
} catch (err) {
Error.captureStackTrace(err, this)
throw err
Expand Down
1 change: 1 addition & 0 deletions deps/undici/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { request, pipeline, stream, connect, upgrade } from './types/api'

export * from './types/fetch'
export * from './types/file'
export * from './types/filereader'
export * from './types/formdata'
export * from './types/diagnostics-channel'
export { Interceptable } from './types/mock-interceptor'
Expand Down
5 changes: 3 additions & 2 deletions deps/undici/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ if (nodeMajor > 16 || (nodeMajor === 16 && nodeMinor >= 8)) {
if (!fetchImpl) {
fetchImpl = require('./lib/fetch').fetch
}
const dispatcher = (arguments[1] && arguments[1].dispatcher) || getGlobalDispatcher()

try {
return await fetchImpl.apply(dispatcher, arguments)
return await fetchImpl(...arguments)
} catch (err) {
Error.captureStackTrace(err, this)
throw err
Expand All @@ -111,6 +111,7 @@ if (nodeMajor > 16 || (nodeMajor === 16 && nodeMinor >= 8)) {
module.exports.Request = require('./lib/fetch/request').Request
module.exports.FormData = require('./lib/fetch/formdata').FormData
module.exports.File = require('./lib/fetch/file').File
module.exports.FileReader = require('./lib/fileapi/filereader').FileReader

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

Expand Down
2 changes: 1 addition & 1 deletion deps/undici/src/lib/api/api-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class StreamHandler extends AsyncResource {
}

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

Expand Down
63 changes: 40 additions & 23 deletions deps/undici/src/lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const {
SocketError,
InformationalError,
BodyTimeoutError,
HTTPParserError
HTTPParserError,
ResponseExceededMaxSizeError
} = require('./core/errors')
const buildConnector = require('./core/connect')
const {
Expand Down Expand Up @@ -60,7 +61,9 @@ const {
kClose,
kDestroy,
kDispatch,
kInterceptors
kInterceptors,
kLocalAddress,
kMaxResponseSize
} = require('./core/symbols')

const kClosedResolve = Symbol('kClosedResolve')
Expand Down Expand Up @@ -102,7 +105,9 @@ class Client extends DispatcherBase {
maxCachedSessions,
maxRedirections,
connect,
maxRequestsPerClient
maxRequestsPerClient,
localAddress,
maxResponseSize
} = {}) {
super()

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

if (localAddress != null && (typeof localAddress !== 'string' || net.isIP(localAddress) === 0)) {
throw new InvalidArgumentError('localAddress must be valid string IP address')
}

if (maxResponseSize != null && (!Number.isInteger(maxResponseSize) || maxResponseSize < -1)) {
throw new InvalidArgumentError('maxResponseSize must be a positive number')
}

if (typeof connect !== 'function') {
connect = buildConnector({
...tls,
Expand All @@ -193,6 +206,7 @@ class Client extends DispatcherBase {
this[kKeepAliveTimeoutThreshold] = keepAliveTimeoutThreshold == null ? 1e3 : keepAliveTimeoutThreshold
this[kKeepAliveTimeoutValue] = this[kKeepAliveDefaultTimeout]
this[kServerName] = null
this[kLocalAddress] = localAddress != null ? localAddress : null
this[kResuming] = 0 // 0, idle, 1, scheduled, 2 resuming
this[kNeedDrain] = 0 // 0, idle, 1, scheduled, 2 resuming
this[kHostHeader] = `host: ${this[kUrl].hostname}${this[kUrl].port ? `:${this[kUrl].port}` : ''}\r\n`
Expand All @@ -202,6 +216,7 @@ class Client extends DispatcherBase {
this[kMaxRedirections] = maxRedirections
this[kMaxRequests] = maxRequestsPerClient
this[kClosedResolve] = null
this[kMaxResponseSize] = maxResponseSize > -1 ? maxResponseSize : -1

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

this.keepAlive = ''
this.contentLength = ''
this.maxResponseSize = client[kMaxResponseSize]
}

setTimeout (value, type) {
Expand Down Expand Up @@ -542,19 +558,6 @@ class Parser {
}
}

finish () {
try {
try {
currentParser = this
} finally {
currentParser = null
}
} catch (err) {
/* istanbul ignore next: difficult to make a test case for */
util.destroy(this.socket, err)
}
}

destroy () {
assert(this.ptr != null)
assert(currentParser == null)
Expand Down Expand Up @@ -783,7 +786,7 @@ class Parser {
}

onBody (buf) {
const { client, socket, statusCode } = this
const { client, socket, statusCode, maxResponseSize } = this

if (socket.destroyed) {
return -1
Expand All @@ -802,6 +805,11 @@ class Parser {

assert(statusCode >= 200)

if (maxResponseSize > -1 && this.bytesRead + buf.length > maxResponseSize) {
util.destroy(socket, new ResponseExceededMaxSizeError())
return -1
}

this.bytesRead += buf.length

try {
Expand Down Expand Up @@ -917,7 +925,7 @@ function onSocketError (err) {
// to the user.
if (err.code === 'ECONNRESET' && parser.statusCode && !parser.shouldKeepAlive) {
// We treat all incoming data so for as a valid response.
parser.finish()
parser.onMessageComplete()
return
}

Expand Down Expand Up @@ -951,7 +959,7 @@ function onSocketEnd () {

if (parser.statusCode && !parser.shouldKeepAlive) {
// We treat all incoming data so far as a valid response.
parser.finish()
parser.onMessageComplete()
return
}

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

if (!this[kError] && this[kParser].statusCode && !this[kParser].shouldKeepAlive) {
// We treat all incoming data so far as a valid response.
this[kParser].onMessageComplete()
}

this[kParser].destroy()
this[kParser] = null

Expand Down Expand Up @@ -1020,7 +1033,8 @@ async function connect (client) {
hostname,
protocol,
port,
servername: client[kServerName]
servername: client[kServerName],
localAddress: client[kLocalAddress]
},
connector: client[kConnector]
})
Expand All @@ -1033,7 +1047,8 @@ async function connect (client) {
hostname,
protocol,
port,
servername: client[kServerName]
servername: client[kServerName],
localAddress: client[kLocalAddress]
}, (err, socket) => {
if (err) {
reject(err)
Expand Down Expand Up @@ -1076,7 +1091,8 @@ async function connect (client) {
hostname,
protocol,
port,
servername: client[kServerName]
servername: client[kServerName],
localAddress: client[kLocalAddress]
},
connector: client[kConnector],
socket
Expand All @@ -1093,7 +1109,8 @@ async function connect (client) {
hostname,
protocol,
port,
servername: client[kServerName]
servername: client[kServerName],
localAddress: client[kLocalAddress]
},
connector: client[kConnector],
error: err
Expand Down
4 changes: 3 additions & 1 deletion deps/undici/src/lib/core/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function buildConnector ({ maxCachedSessions, socketPath, timeout, ...opts }) {
timeout = timeout == null ? 10e3 : timeout
maxCachedSessions = maxCachedSessions == null ? 100 : maxCachedSessions

return function connect ({ hostname, host, protocol, port, servername, httpSocket }, callback) {
return function connect ({ hostname, host, protocol, port, servername, localAddress, httpSocket }, callback) {
let socket
if (protocol === 'https:') {
if (!tls) {
Expand All @@ -39,6 +39,7 @@ function buildConnector ({ maxCachedSessions, socketPath, timeout, ...opts }) {
...options,
servername,
session,
localAddress,
socket: httpSocket, // upgrade socket connection
port: port || 443,
host: hostname
Expand Down Expand Up @@ -70,6 +71,7 @@ function buildConnector ({ maxCachedSessions, socketPath, timeout, ...opts }) {
socket = net.connect({
highWaterMark: 64 * 1024, // Same as nodejs fs streams.
...options,
localAddress,
port: port || 80,
host: hostname
})
Expand Down
13 changes: 12 additions & 1 deletion deps/undici/src/lib/core/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,16 @@ class HTTPParserError extends Error {
}
}

class ResponseExceededMaxSizeError extends UndiciError {
constructor (message) {
super(message)
Error.captureStackTrace(this, ResponseExceededMaxSizeError)
this.name = 'ResponseExceededMaxSizeError'
this.message = message || 'Response content exceeded max size'
this.code = 'UND_ERR_RES_EXCEEDED_MAX_SIZE'
}
}

module.exports = {
HTTPParserError,
UndiciError,
Expand All @@ -201,5 +211,6 @@ module.exports = {
SocketError,
NotSupportedError,
ResponseContentLengthMismatchError,
BalancedPoolMissingUpstreamError
BalancedPoolMissingUpstreamError,
ResponseExceededMaxSizeError
}
4 changes: 3 additions & 1 deletion deps/undici/src/lib/core/symbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = {
kHeadersTimeout: Symbol('headers timeout'),
kBodyTimeout: Symbol('body timeout'),
kServerName: Symbol('server name'),
kLocalAddress: Symbol('local address'),
kHost: Symbol('host'),
kNoRef: Symbol('no ref'),
kBodyUsed: Symbol('used'),
Expand Down Expand Up @@ -49,5 +50,6 @@ module.exports = {
kMaxRequests: Symbol('maxRequestsPerClient'),
kProxy: Symbol('proxy agent options'),
kCounter: Symbol('socket request counter'),
kInterceptors: Symbol('dispatch interceptors')
kInterceptors: Symbol('dispatch interceptors'),
kMaxResponseSize: Symbol('max response size')
}
Loading

0 comments on commit bb6e8b1

Please sign in to comment.