Fix test-http-client-response-timeout.js

This commit is contained in:
Kai Tamkun
2025-04-04 21:16:51 -07:00
parent e3cfcb41ce
commit 99ec99de5c
3 changed files with 45 additions and 9 deletions

View File

@@ -1304,13 +1304,15 @@ JSC_DEFINE_HOST_FUNCTION(jsHTTPSetTimeout, (JSGlobalObject * globalObject, CallF
if (auto* jsRequest = jsDynamicCast<WebCore::JSRequest*>(requestValue)) {
Request__setTimeout(jsRequest->wrapped(), JSValue::encode(seconds), globalObject);
return JSValue::encode(jsBoolean(true));
}
if (auto* nodeHttpResponse = jsDynamicCast<WebCore::JSNodeHTTPResponse*>(requestValue)) {
NodeHTTPResponse__setTimeout(nodeHttpResponse->wrapped(), JSValue::encode(seconds), globalObject);
return JSValue::encode(jsBoolean(true));
}
return JSValue::encode(jsUndefined());
return JSValue::encode(jsBoolean(false));
}
JSC_DEFINE_HOST_FUNCTION(jsHTTPSetServerIdleTimeout, (JSGlobalObject * globalObject, CallFrame* callFrame))
{

View File

@@ -1545,8 +1545,12 @@ const IncomingMessagePrototype = {
const req = this[kHandle] || this[webRequestOrResponse];
if (req) {
setRequestTimeout(req, Math.ceil(msecs / 1000));
typeof callback === "function" && this.once("timeout", callback);
if (setRequestTimeout(req, Math.ceil(msecs / 1000))) {
typeof callback === "function" && this.once("timeout", callback);
} else {
// Actually a Response object
req.setTimeout?.(msecs, callback);
}
}
return this;
},
@@ -2659,6 +2663,10 @@ function ClientRequest(input, options, cb) {
this.finished = true;
if (this.res && !this.res.complete) {
this.res.emit("end");
}
// If request is destroyed we abort the current response
this[kAbortController]?.abort?.();
this.socket.destroy(err);
@@ -2843,6 +2851,19 @@ function ClientRequest(input, options, cb) {
}));
isNextIncomingMessageHTTPS = prevIsHTTPS;
res.req = this;
let timer;
response.setTimeout = (msecs, callback) => {
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(() => {
if (res.complete) {
return;
}
res.emit("timeout");
callback?.();
}, msecs);
};
process.nextTick(
(self, res) => {
// If the user did not listen for the 'response' event, then they
@@ -2958,9 +2979,8 @@ function ClientRequest(input, options, cb) {
const send = () => {
this.finished = true;
const controller = new AbortController();
this[kAbortController] = controller;
controller.signal.addEventListener("abort", onAbort, { once: true });
this[kAbortController] = new AbortController();
this[kAbortController].signal.addEventListener("abort", onAbort, { once: true });
var body = this[kBodyChunks] && this[kBodyChunks].length > 1 ? new Blob(this[kBodyChunks]) : this[kBodyChunks]?.[0];
@@ -3093,7 +3113,7 @@ function ClientRequest(input, options, cb) {
signal.addEventListener(
"abort",
() => {
this[kAbortController]?.abort?.();
this[kAbortController]?.abort();
},
{ once: true },
);
@@ -3304,7 +3324,7 @@ function ClientRequest(input, options, cb) {
const onTimeout = () => {
this[kTimeoutTimer] = undefined;
this[kAbortController]?.abort?.();
this[kAbortController]?.abort();
this.emit("timeout");
};
@@ -3370,7 +3390,7 @@ const ClientRequestPrototype = {
},
get aborted() {
return this[abortedSymbol] || this[kSignal]?.aborted || !!this[kAbortController]?.signal?.aborted;
return this[abortedSymbol] || this[kSignal]?.aborted || !!this[kAbortController]?.signal.aborted;
},
set aborted(value) {

View File

@@ -0,0 +1,14 @@
'use strict';
const common = require('../common');
const http = require('http');
const server = http.createServer((req, res) => res.flushHeaders());
server.listen(common.mustCall(() => {
const req =
http.get({ port: server.address().port }, common.mustCall((res) => {
res.on('timeout', common.mustCall(() => req.destroy()));
res.setTimeout(1);
server.close();
}));
}));