Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions deps/undici/src/docs/docs/api/EnvHttpProxyAgent.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,27 @@ Uppercase environment variables are also supported: `HTTP_PROXY`, `HTTPS_PROXY`,

Arguments:

* **options** `EnvHttpProxyAgentOptions` (optional) - extends the `Agent` options.
* **options** `EnvHttpProxyAgentOptions` (optional) - extends the `ProxyAgent` options, except `uri`.

Returns: `EnvHttpProxyAgent`

### Parameter: `EnvHttpProxyAgentOptions`

Extends: [`AgentOptions`](Agent.md#parameter-agentoptions)
Extends: [`ProxyAgentOptions`](ProxyAgent.md#parameter-proxyagentoptions), except `uri`

* **httpProxy** `string` (optional) - When set, it will override the `HTTP_PROXY` environment variable.
* **httpsProxy** `string` (optional) - When set, it will override the `HTTPS_PROXY` environment variable.
* **noProxy** `string` (optional) - When set, it will override the `NO_PROXY` environment variable.

The TLS options for the connection to the target server, given as `requestTls`
or as a `connect` object, apply on every route: direct (no proxy configured),
hosts matching `noProxy`, and proxied through an HTTP or HTTPS proxy.
When both are set they are merged key by key, and keys set in `connect` take
precedence over `requestTls` on every route. `proxyTls` applies only to the connection to the
proxy server, and the target TLS options are never applied to it. When `connect`
is a function it is used as-is on direct routes, where `requestTls` is then not
applied, and it is not used on proxied routes.

Examples:

```js
Expand Down
11 changes: 9 additions & 2 deletions deps/undici/src/lib/api/api-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class StreamHandler extends AsyncResource {
this.body = body
this.onInfo = onInfo || null
this.throwOnError = throwOnError || false
// Set once the factory's writable is tracked: from then on its lifecycle
// owns the callback, which must not be invoked from onError.
this.responseStarted = false

if (util.isStream(body)) {
body.on('error', (err) => {
Expand Down Expand Up @@ -134,12 +137,16 @@ class StreamHandler extends AsyncResource {
}

this.callback = null
this.runInAsyncScope(callback, null, err || null, { opaque, trailers })
if (callback) {
this.runInAsyncScope(callback, null, err || null, { opaque, trailers })
}

if (err) {
abort()
}
})

this.responseStarted = true
}

res.on('drain', resume)
Expand Down Expand Up @@ -183,7 +190,7 @@ class StreamHandler extends AsyncResource {
if (res) {
this.res = null
util.destroy(res, err)
} else if (callback) {
} else if (callback && !this.responseStarted) {
this.callback = null
queueMicrotask(() => {
this.runInAsyncScope(callback, null, err, { opaque })
Expand Down
24 changes: 23 additions & 1 deletion deps/undici/src/lib/dispatcher/client-h1.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,32 @@ class Parser {
finish () {
assert(currentParser === null)
assert(this.ptr != null)
assert(!this.paused)

const { llhttp } = this

// The peer closed the connection. If the body parser was paused by
// backpressure we must finish parsing before signalling EOF, otherwise
// llhttp_finish() would crash (it used to assert !paused) or report a
// half-parsed message. Backpressure is advisory here: onData keeps buffering
// delivered bytes into the response stream, so resume across pauses and
// drain whatever is still buffered on the socket. A Content-Length/chunked
// body reaches on_message_complete during execute(); an EOF-delimited body
// stays paused (its length is unknown) and is completed by llhttp_finish().
if (this.paused) {
let data
do {
llhttp.llhttp_resume(this.ptr)
this.paused = false
data = this.socket.read() || EMPTY_BUF
this.execute(data)
} while (this.paused && data.length > 0)

if (this.paused) {
llhttp.llhttp_resume(this.ptr)
this.paused = false
}
}

let ret

try {
Expand Down
24 changes: 21 additions & 3 deletions deps/undici/src/lib/dispatcher/env-http-proxy-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,36 @@ class EnvHttpProxyAgent extends DispatcherBase {

const { httpProxy, httpsProxy, noProxy, ...agentOpts } = opts

this[kNoProxyAgent] = new Agent(agentOpts)
// The environment picks the route (direct, NO_PROXY or proxied), so the
// TLS options for the target must apply on every route, merged the same
// way: field by field, `connect` over `requestTls` over `tls`. The direct
// Agent reads them from `connect` and `tls`, a ProxyAgent from
// `requestTls`. A `connect` function is a custom connector: it is used
// as-is on direct routes and cannot be applied to the target of a proxy.
const { connect, tls, requestTls } = agentOpts
const connectOpts = typeof connect === 'function' ? undefined : connect
const tlsOpts = typeof tls === 'object' ? tls : undefined

this[kNoProxyAgent] = new Agent(
requestTls && typeof connect !== 'function'
? { ...agentOpts, connect: { ...requestTls, ...connect } }
: agentOpts
)

const proxyOpts = connectOpts || tlsOpts
? { ...agentOpts, requestTls: { ...tlsOpts, ...requestTls, ...connectOpts } }
: agentOpts

const HTTP_PROXY = httpProxy ?? process.env.http_proxy ?? process.env.HTTP_PROXY
if (HTTP_PROXY) {
this[kHttpProxyAgent] = new ProxyAgent({ ...agentOpts, uri: HTTP_PROXY })
this[kHttpProxyAgent] = new ProxyAgent({ ...proxyOpts, uri: HTTP_PROXY })
} else {
this[kHttpProxyAgent] = this[kNoProxyAgent]
}

const HTTPS_PROXY = httpsProxy ?? process.env.https_proxy ?? process.env.HTTPS_PROXY
if (HTTPS_PROXY) {
this[kHttpsProxyAgent] = new ProxyAgent({ ...agentOpts, uri: HTTPS_PROXY })
this[kHttpsProxyAgent] = new ProxyAgent({ ...proxyOpts, uri: HTTPS_PROXY })
} else {
this[kHttpsProxyAgent] = this[kHttpProxyAgent]
}
Expand Down
5 changes: 4 additions & 1 deletion deps/undici/src/lib/dispatcher/proxy-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ class ProxyAgent extends DispatcherBase {
: []
this[kRequestTls] = opts.requestTls
this[kProxyTls] = opts.proxyTls
this[kProxyHeaders] = opts.headers || {}
// Copy the caller's headers: proxy-authorization is added below, and it
// must not leak into an object the caller shares with other agents (such
// as the HTTP and HTTPS proxies of an EnvHttpProxyAgent) or requests.
this[kProxyHeaders] = { ...opts.headers }
this[kTunnelProxy] = proxyTunnel

if (opts.auth && opts.token) {
Expand Down
28 changes: 21 additions & 7 deletions deps/undici/src/lib/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const globalDispatcher = Symbol.for('undici.globalDispatcher.1')
const { InvalidArgumentError } = require('./core/errors')
const Agent = require('./dispatcher/agent')

// Fallback storage for when globalThis is not extensible (e.g. frozen)
let fallbackDispatcher

if (getGlobalDispatcher() === undefined) {
setGlobalDispatcher(new Agent())
}
Expand All @@ -14,16 +17,27 @@ function setGlobalDispatcher (agent) {
if (!agent || typeof agent.dispatch !== 'function') {
throw new InvalidArgumentError('Argument agent must implement Agent')
}
Object.defineProperty(globalThis, globalDispatcher, {
value: agent,
writable: true,
enumerable: false,
configurable: false
})

try {
Object.defineProperty(globalThis, globalDispatcher, {
value: agent,
writable: true,
enumerable: false,
configurable: false
})
} catch (err) {
// globalThis is not extensible (e.g. Object.freeze(globalThis))
// Use fallback storage instead
if (err instanceof TypeError) {
fallbackDispatcher = agent
return
}
throw err
}
}

function getGlobalDispatcher () {
return globalThis[globalDispatcher]
return globalThis[globalDispatcher] ?? fallbackDispatcher
}

module.exports = {
Expand Down
11 changes: 10 additions & 1 deletion deps/undici/src/lib/handler/redirect-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,17 @@ class RedirectHandler {
// Remove headers referring to the original URL.
// By default it is Host only, unless it's a 303 (see below), which removes also all Content-* headers.
// https://tools.ietf.org/html/rfc7231#section-6.4
this.opts.headers = cleanRequestHeaders(this.opts.headers, statusCode === 303, this.opts.origin !== origin)
// this.opts.origin may be a URL object or a non-canonical string, while
// origin is always URL.origin, so compare normalized origins.
const crossOrigin = !this.opts.origin || new URL(this.opts.origin).origin !== origin
this.opts.headers = cleanRequestHeaders(this.opts.headers, statusCode === 303, crossOrigin)
this.opts.path = path
if (crossOrigin && this.opts.servername != null) {
// A TLS servername chosen for the original host must not be used to
// verify the certificate of another origin. Let the next request derive
// it from the new host instead.
this.opts.servername = null
}
this.opts.origin = origin
this.opts.maxRedirections = 0
this.opts.query = null
Expand Down
4 changes: 2 additions & 2 deletions deps/undici/src/lib/llhttp/wasm_build_env.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

> undici@6.29.1 prebuild:wasm
> undici@6.29.2 prebuild:wasm
> node build/wasm.js --prebuild

> docker build --platform=linux/x86_64 -t llhttp_wasm_builder -f /home/runner/work/node/node/deps/undici/src/build/Dockerfile /home/runner/work/node/node/deps/undici/src



> undici@6.29.1 build:wasm
> undici@6.29.2 build:wasm
> node build/wasm.js --docker

> docker run --rm -t --platform=linux/x86_64 --user 1001:1001 --mount type=bind,source=/home/runner/work/node/node/deps/undici/src/lib/llhttp,target=/home/node/undici/lib/llhttp llhttp_wasm_builder node build/wasm.js
Expand Down
7 changes: 5 additions & 2 deletions deps/undici/src/lib/web/websocket/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const {
kReceivedClose,
kResponse
} = require('./symbols')
const { fireEvent, failWebsocketConnection, isClosing, isClosed, isEstablished, parseExtensions } = require('./util')
const { fireEvent, failWebsocketConnection, isConnecting, isClosing, isClosed, isEstablished, parseExtensions } = require('./util')
const { channels } = require('../../core/diagnostics')
const { CloseEvent } = require('./events')
const { makeRequest } = require('../fetch/request')
Expand Down Expand Up @@ -107,7 +107,10 @@ function establishWebSocketConnection (url, protocols, client, ws, onEstablish,
// 1. If response is a network error or its status is not 101,
// fail the WebSocket connection.
if (response.type === 'error' || response.status !== 101) {
failWebsocketConnection(ws, 'Received network error or non-101 status code.')
// close() aborts the fetch after it has already failed the connection.
if (isConnecting(ws)) {
failWebsocketConnection(ws, 'Received network error or non-101 status code.')
}
return
}

Expand Down
17 changes: 16 additions & 1 deletion deps/undici/src/lib/web/websocket/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { kReadyState, kController, kResponse, kBinaryType, kWebSocketURL } = require('./symbols')
const { states, opcodes } = require('./constants')
const { ErrorEvent, createFastMessageEvent } = require('./events')
const { ErrorEvent, CloseEvent, createFastMessageEvent } = require('./events')
const { isUtf8 } = require('node:buffer')
const { collectASequenceOfCodePointsFast, removeHTTPWhitespace } = require('../fetch/data-url')

Expand Down Expand Up @@ -202,13 +202,28 @@ function failWebsocketConnection (ws, reason) {
response.socket.destroy()
}

// A connection that was never established has no socket close listener,
// so the connection is closed here instead of in onSocketClose.
const wasConnecting = isConnecting(ws)

if (wasConnecting) {
ws[kReadyState] = states.CLOSED
}

if (reason) {
// TODO: process.nextTick
fireEvent('error', ws, (type, init) => new ErrorEvent(type, init), {
error: new Error(reason),
message: reason
})
}

if (wasConnecting) {
fireEvent('close', ws, (type, init) => new CloseEvent(type, init), {
wasClean: false,
code: 1006
})
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion deps/undici/src/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "undici",
"version": "6.29.1",
"version": "6.29.2",
"description": "An HTTP/1.1 client, written from scratch for Node.js",
"homepage": "https://undici.nodejs.org",
"bugs": {
Expand Down
3 changes: 2 additions & 1 deletion deps/undici/src/types/env-http-proxy-agent.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Agent from './agent'
import ProxyAgent from './proxy-agent'
import Dispatcher from './dispatcher'

export default EnvHttpProxyAgent
Expand All @@ -10,7 +11,7 @@ declare class EnvHttpProxyAgent extends Dispatcher {
}

declare namespace EnvHttpProxyAgent {
export interface Options extends Agent.Options {
export interface Options extends Omit<ProxyAgent.Options, 'uri'> {
/** Overrides the value of the HTTP_PROXY environment variable */
httpProxy?: string;
/** Overrides the value of the HTTPS_PROXY environment variable */
Expand Down
Loading
Loading