Skip to content

feat(node/http): implement ClientRequest.setTimeout() #18783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 22, 2023
Merged
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
3 changes: 3 additions & 0 deletions cli/tests/node_compat/config.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,9 @@
"test-http-agent-getname.js",
"test-http-client-get-url.js",
"test-http-client-read-in-error.js",
// TODO(lev): ClientRequest.socket is not polyfilled so this test keeps
// failing
//"test-http-client-set-timeout.js",
"test-http-localaddress.js",
"test-http-outgoing-buffer.js",
"test-http-outgoing-internal-headernames-getter.js",
Expand Down
25 changes: 23 additions & 2 deletions ext/node/polyfills/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { urlToHttpOptions } from "ext:deno_node/internal/url.ts";
import { constants, TCP } from "ext:deno_node/internal_binding/tcp_wrap.ts";
import * as denoHttp from "ext:deno_http/01_http.js";
import * as httpRuntime from "ext:runtime/40_http.js";
import { connResetException } from "ext:deno_node/internal/errors.ts";

enum STATUS_CODES {
/** RFC 7231, 6.2.1 */
Expand Down Expand Up @@ -259,16 +260,21 @@ class ClientRequest extends NodeWritable {
method: this.opts.method,
client,
headers: this.opts.headers,
signal: this.opts.signal ?? undefined,
};
const mayResponse = fetch(this._createUrlStrFromOptions(this.opts), opts)
.catch((e) => {
if (e.message.includes("connection closed before message completed")) {
// Node.js seems ignoring this error
} else if (e.message.includes("The signal has been aborted")) {
// Remap this error
this.emit("error", connResetException("socket hang up"));
} else {
this.emit("error", e);
}
return undefined;
});

const res = new IncomingMessageForClient(
await mayResponse,
this._createSocket(),
Expand All @@ -279,6 +285,10 @@ class ClientRequest extends NodeWritable {
client.close();
});
}
if (this.opts.timeout != undefined) {
clearTimeout(this.opts.timeout);
this.opts.timeout = undefined;
}
this.cb?.(res);
}

Expand Down Expand Up @@ -340,8 +350,19 @@ class ClientRequest extends NodeWritable {
}${path}`;
}

setTimeout() {
console.log("not implemented: ClientRequest.setTimeout");
setTimeout(timeout: number, callback?: () => void) {
const controller = new AbortController();
this.opts.signal = controller.signal;

this.opts.timeout = setTimeout(() => {
controller.abort();

this.emit("timeout");

if (callback !== undefined) {
callback();
}
}, timeout);
}
}

Expand Down