node_modules: update (#344)

Co-authored-by: dawidd6 <9713907+dawidd6@users.noreply.github.com>
This commit is contained in:
Dawid Dziurlaanddawidd6 authored and GitHub committed 2026-10-10 17:27:57 +02:00
1 parent b099186883
commit 6e88c72525
63 files changed
+2863 -1227

No files matched your search

+88 -68
View File
@@ -46,6 +46,8 @@ const errors = __importStar(require("../errors.js"));
// Cap the CONNECT response we buffer before the header terminator, so a proxy that
// never sends \r\n\r\n cannot grow memory unboundedly before the socket times out.
const MAX_RESPONSE_HEADER_BYTES = 64 * 1024;
// URL hostnames keep the brackets around an IPv6 literal, socket options and net.isIPv6 take it without
const unbracket = (host) => typeof host === 'string' && host.startsWith('[') && host.endsWith(']') ? host.slice(1, -1) : host;
function httpProxyClient(proxyUrl, destinationPort, destinationHost, tlsOptions, callback) {
if (typeof tlsOptions === 'function') {
callback = tlsOptions;
@@ -65,6 +67,8 @@ function httpProxyClient(proxyUrl, destinationPort, destinationHost, tlsOptions,
return;
}
const proxy = urllib.parse(proxyUrl);
// the CONNECT request line and the Host header take an IPv6 destination in brackets
const authority = (node_net_1.default.isIPv6(unbracket(destinationHost)) ? '[' + unbracket(destinationHost) + ']' : destinationHost) + ':' + destinationPort;
const connectOptions = {
host: proxy.hostname,
port: Number(proxy.port) ? Number(proxy.port) : proxy.protocol === 'https:' ? 443 : 80
@@ -80,15 +84,27 @@ function httpProxyClient(proxyUrl, destinationPort, destinationHost, tlsOptions,
else {
connect = node_net_1.default.connect.bind(node_net_1.default);
}
// The handshake is bounded as a whole, a proxy that keeps sending a byte now and then can
// not hold the connection open past it
const timeout = Number(tlsOptions.timeout) || httpProxyClient.timeout || 30 * 1000;
let socket;
// Error harness for initial connection. Once connection is established, the responsibility
// to handle errors is passed to whoever uses this socket
// Single settlement path for the handshake: every temporary listener and the timer are
// dropped exactly once. Once the tunnel is up, the responsibility to handle errors is passed
// to whoever uses this socket
let finished = false;
const tempSocketErr = (err) => {
let timer;
const cleanup = () => {
clearTimeout(timer);
socket.removeListener('data', onSocketData);
socket.removeListener('error', fail);
socket.removeListener('close', onEarlyClose);
};
function fail(err) {
if (finished) {
return;
}
finished = true;
cleanup();
try {
socket.destroy();
}
@@ -96,18 +112,72 @@ function httpProxyClient(proxyUrl, destinationPort, destinationHost, tlsOptions,
// ignore
}
done(err);
};
const timeoutErr = () => {
const err = new Error('Proxy socket timed out');
err.code = 'ETIMEDOUT';
tempSocketErr(err);
};
}
function onEarlyClose() {
const err = new Error('Proxy closed the connection before the tunnel was established');
err.code = errors.EPROXY;
fail(err);
}
// The response is collected as chunks and only the bytes that just arrived, together
// with the three before them, are searched for the end of the headers. Appending to a
// string and searching all of it again re-read the whole response on every chunk.
const chunks = [];
let received = 0;
let tail = '';
function onSocketData(chunk) {
if (finished) {
return;
}
const window = tail + chunk.toString('binary');
const windowEnd = window.indexOf('\r\n\r\n');
chunks.push(chunk);
received += chunk.length;
tail = window.slice(-3);
if (windowEnd < 0) {
if (received > MAX_RESPONSE_HEADER_BYTES) {
const err = new Error('Proxy response headers too large');
err.code = errors.EPROXY;
fail(err);
}
return;
}
// Stop reading before anything is put back. A socket that keeps flowing would emit the
// bytes after the headers, a greeting the proxy sent together with its own response,
// before the next owner of the socket has a listener for them
socket.removeListener('data', onSocketData);
socket.pause();
const headerEnd = received - window.length + windowEnd;
const response = Buffer.concat(chunks, received);
if (response.length > headerEnd + 4) {
socket.unshift(response.subarray(headerEnd + 4));
}
// check response code
const match = response.toString('binary', 0, headerEnd).match(/^HTTP\/\d+\.\d+ (\d+)/i);
if (!match || (match[1] || '').charAt(0) !== '2') {
const err = new Error('Invalid response from proxy' + ((match && ': ' + match[1]) || ''));
err.code = errors.EPROXY;
return fail(err);
}
// proxy connection is now established
finished = true;
cleanup();
// A fresh socket starts flowing once something listens for 'data', a paused one would
// not. Keep that behaviour for the next owner of the socket
const resumeOnData = (event) => {
if (event === 'data') {
socket.removeListener('newListener', resumeOnData);
socket.resume();
}
};
socket.on('newListener', resumeOnData);
return done(null, socket);
}
socket = connect(connectOptions, () => {
if (finished) {
return;
}
const reqHeaders = {
Host: destinationHost + ':' + destinationPort,
Host: authority,
Connection: 'close'
};
if (proxy.auth) {
@@ -116,9 +186,7 @@ function httpProxyClient(proxyUrl, destinationPort, destinationHost, tlsOptions,
socket.write(
// HTTP method
'CONNECT ' +
destinationHost +
':' +
destinationPort +
authority +
' HTTP/1.1\r\n' +
// HTTP request headers
Object.keys(reqHeaders)
@@ -126,63 +194,15 @@ function httpProxyClient(proxyUrl, destinationPort, destinationHost, tlsOptions,
.join('\r\n') +
// End request
'\r\n\r\n');
// The response is collected as chunks and only the bytes that just arrived, together
// with the three before them, are searched for the end of the headers. Appending to a
// string and searching all of it again re-read the whole response on every chunk.
const chunks = [];
let received = 0;
let tail = '';
const onSocketData = (chunk) => {
let match;
if (finished) {
return;
}
const window = tail + chunk.toString('binary');
const windowEnd = window.indexOf('\r\n\r\n');
chunks.push(chunk);
received += chunk.length;
tail = window.slice(-3);
if (windowEnd >= 0) {
socket.removeListener('data', onSocketData);
const headerEnd = received - window.length + windowEnd;
const response = Buffer.concat(chunks, received).toString('binary');
const headers = response.substr(0, headerEnd);
const remainder = response.substr(headerEnd + 4);
if (remainder) {
socket.unshift(Buffer.from(remainder, 'binary'));
}
// proxy connection is now established
finished = true;
// check response code
match = headers.match(/^HTTP\/\d+\.\d+ (\d+)/i);
if (!match || (match[1] || '').charAt(0) !== '2') {
try {
socket.destroy();
}
catch (_E) {
// ignore
}
const err = new Error('Invalid response from proxy' + ((match && ': ' + match[1]) || ''));
err.code = errors.EPROXY;
return done(err);
}
socket.removeListener('error', tempSocketErr);
socket.removeListener('timeout', timeoutErr);
socket.setTimeout(0);
return done(null, socket);
}
if (received > MAX_RESPONSE_HEADER_BYTES) {
socket.removeListener('data', onSocketData);
const err = new Error('Proxy response headers too large');
err.code = errors.EPROXY;
return tempSocketErr(err);
}
};
socket.on('data', onSocketData);
});
socket.setTimeout(httpProxyClient.timeout || 30 * 1000);
socket.on('timeout', timeoutErr);
socket.once('error', tempSocketErr);
timer = setTimeout(() => {
const err = new Error('Proxy socket timed out');
err.code = errors.ETIMEDOUT;
fail(err);
}, timeout);
socket.once('error', fail);
socket.once('close', onEarlyClose);
}
exports.default = httpProxyClient;
module.exports = exports.default;