Compare commits

...

2 Commits

Author SHA1 Message Date
Jarred Sumner
91e0abfefe Merge branch 'main' into codex/fix-node.js-test-for-timeout-option 2025-05-29 23:36:32 -07:00
Jarred Sumner
60f45dd5c6 fix http Agent timeout 2025-05-29 14:46:39 -07:00
4 changed files with 43 additions and 5 deletions

View File

@@ -110,11 +110,17 @@ var FakeSocket = class Socket extends Duplex {
}
setTimeout(timeout, callback) {
const socketData = this[kInternalSocketData];
if (!socketData) return; // sometimes 'this' is Socket not FakeSocket
this.timeout = timeout;
if (!this.listeners("timeout").includes(this._onTimeout)) {
this.on("timeout", this._onTimeout);
}
const socketData = this[kInternalSocketData];
if (socketData) {
const http_res = socketData[1];
http_res?.req?.setTimeout(timeout, callback);
}
const http_res = socketData[1];
http_res?.req?.setTimeout(timeout, callback);
return this;
}

View File

@@ -26,6 +26,7 @@ interface Agent extends InstanceType<typeof EventEmitter> {
scheduling: string;
maxTotalSockets: any;
totalSocketCount: number;
timeout?: number;
[kfakeSocket]?: any;
createConnection(): any;
@@ -65,6 +66,7 @@ function Agent(options = kEmptyObject) {
this.keepAliveMsecs = options.keepAliveMsecs || 1000;
this.keepAlive = options.keepAlive || false;
if (options.timeout !== undefined) this.timeout = options.timeout;
this.maxSockets = options.maxSockets || Agent.defaultMaxSockets;
this.maxFreeSockets = options.maxFreeSockets || 256;
this.scheduling = options.scheduling || "lifo";

View File

@@ -90,6 +90,9 @@ function ClientRequest(input, options, cb) {
let writeCount = 0;
let resolveNextChunk: ((end: boolean) => void) | undefined = _end => {};
this.timeoutCb = () => {
this.emit("timeout");
};
const pushChunk = chunk => {
this[kBodyChunks].push(chunk);
@@ -566,7 +569,13 @@ function ClientRequest(input, options, cb) {
if (this.destroyed) return;
if (!(this[kEmitState] & (1 << ClientRequestEmitState.socket))) {
this[kEmitState] |= 1 << ClientRequestEmitState.socket;
this.emit("socket", this.socket);
const socket = this.socket;
const agentTimeout = this[kAgent]?.timeout;
if (agentTimeout !== undefined) {
socket.setTimeout(agentTimeout);
socket.on("timeout", this.timeoutCb);
}
this.emit("socket", socket);
}
};

View File

@@ -0,0 +1,21 @@
const { mustCall } = require('../common');
const { strictEqual } = require('assert');
const { Agent, get } = require('http');
// Test that the listener that forwards the `'timeout'` event from the socket to
// the `ClientRequest` instance is added to the socket when the `timeout` option
// of the `Agent` is set.
const request = get({
agent: new Agent({ timeout: 50 }),
lookup: () => {}
});
request.on('socket', mustCall((socket) => {
strictEqual(socket.timeout, 50);
const listeners = socket.listeners('timeout');
strictEqual(listeners.length, 2);
strictEqual(listeners[1], request.timeoutCb);
}));