1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-25 00:29:09 -05:00

refactor: further work on node http client (#19327)

Closes https://github.com/denoland/deno/issues/18300
This commit is contained in:
Leo Kettmeir 2023-05-31 20:06:21 +02:00 committed by Bartek Iwańczuk
parent 782db80629
commit a6b0866fb6
No known key found for this signature in database
GPG key ID: 0C6BCDDC3B3AD750
4 changed files with 80 additions and 9 deletions

View file

@ -531,3 +531,68 @@ Deno.test("[node/http] ClientRequest uses HTTP/1.1", async () => {
await def; await def;
assertEquals(body, "HTTP/1.1"); assertEquals(body, "HTTP/1.1");
}); });
Deno.test("[node/http] ClientRequest setTimeout", async () => {
let body = "";
const def = deferred();
const timer = setTimeout(() => def.reject("timed out"), 50000);
const req = http.request("http://localhost:4545/http_version", (resp) => {
resp.on("data", (chunk) => {
body += chunk;
});
resp.on("end", () => {
def.resolve();
});
});
req.setTimeout(120000);
req.once("error", (e) => def.reject(e));
req.end();
await def;
clearTimeout(timer);
assertEquals(body, "HTTP/1.1");
});
Deno.test("[node/http] ClientRequest PATCH", async () => {
let body = "";
const def = deferred();
const req = http.request("http://localhost:4545/echo_server", {
method: "PATCH",
}, (resp) => {
resp.on("data", (chunk) => {
body += chunk;
});
resp.on("end", () => {
def.resolve();
});
});
req.write("hello ");
req.write("world");
req.once("error", (e) => def.reject(e));
req.end();
await def;
assertEquals(body, "hello world");
});
Deno.test("[node/http] ClientRequest PUT", async () => {
let body = "";
const def = deferred();
const req = http.request("http://localhost:4545/echo_server", {
method: "PUT",
}, (resp) => {
resp.on("data", (chunk) => {
body += chunk;
});
resp.on("end", () => {
def.resolve();
});
});
req.write("hello ");
req.write("world");
req.once("error", (e) => def.reject(e));
req.end();
await def;
assertEquals(body, "hello world");
});

View file

@ -50,6 +50,8 @@ import {
import { getTimerDuration } from "ext:deno_node/internal/timers.mjs"; import { getTimerDuration } from "ext:deno_node/internal/timers.mjs";
import { serve, upgradeHttpRaw } from "ext:deno_http/00_serve.js"; import { serve, upgradeHttpRaw } from "ext:deno_http/00_serve.js";
import { createHttpClient } from "ext:deno_fetch/22_http_client.js"; import { createHttpClient } from "ext:deno_fetch/22_http_client.js";
import { timerId } from "ext:deno_web/03_abort_signal.js";
import { clearTimeout as webClearTimeout } from "ext:deno_web/02_timers.js";
enum STATUS_CODES { enum STATUS_CODES {
/** RFC 7231, 6.2.1 */ /** RFC 7231, 6.2.1 */
@ -350,10 +352,7 @@ class ClientRequest extends OutgoingMessage {
this.socketPath = options!.socketPath; this.socketPath = options!.socketPath;
if (options!.timeout !== undefined) { if (options!.timeout !== undefined) {
const msecs = getTimerDuration(options.timeout, "timeout"); this.setTimeout(options.timeout);
const timeout = AbortSignal.timeout(msecs);
timeout.onabort = () => this.emit("timeout");
this._timeout = timeout;
} }
const signal = options!.signal; const signal = options!.signal;
@ -561,7 +560,8 @@ class ClientRequest extends OutgoingMessage {
url, url,
headers, headers,
client.rid, client.rid,
this.method === "POST" || this.method === "PATCH", this.method === "POST" || this.method === "PATCH" ||
this.method === "PUT",
); );
this._bodyWriteRid = this._req.requestBodyRid; this._bodyWriteRid = this._req.requestBodyRid;
} }
@ -637,7 +637,8 @@ class ClientRequest extends OutgoingMessage {
})(), })(),
]); ]);
if (this._timeout) { if (this._timeout) {
this._timeout.onabort = null; this._timeout.removeEventListener("abort", this._timeoutCb);
webClearTimeout(this._timeout[timerId]);
} }
this._client.close(); this._client.close();
const incoming = new IncomingMessageForClient(this.socket); const incoming = new IncomingMessageForClient(this.socket);
@ -752,7 +753,7 @@ class ClientRequest extends OutgoingMessage {
if (msecs === 0) { if (msecs === 0) {
if (this._timeout) { if (this._timeout) {
this.removeAllListeners("timeout"); this.removeAllListeners("timeout");
this._timeout.onabort = () => {}; this._timeout.removeEventListener("abort", this._timeoutCb);
this._timeout = undefined; this._timeout = undefined;
} }
@ -766,7 +767,8 @@ class ClientRequest extends OutgoingMessage {
if (callback) this.once("timeout", callback); if (callback) this.once("timeout", callback);
const timeout = AbortSignal.timeout(msecs); const timeout = AbortSignal.timeout(msecs);
timeout.onabort = () => this.emit("timeout"); this._timeoutCb = () => this.emit("timeout");
timeout.addEventListener("abort", this._timeoutCb);
this._timeout = timeout; this._timeout = timeout;
return this; return this;

View file

@ -203,4 +203,5 @@ export {
newSignal, newSignal,
remove, remove,
signalAbort, signalAbort,
timerId,
}; };

View file

@ -727,7 +727,10 @@ async fn main_server(
req: Request<Body>, req: Request<Body>,
) -> Result<Response<Body>, hyper::http::Error> { ) -> Result<Response<Body>, hyper::http::Error> {
return match (req.method(), req.uri().path()) { return match (req.method(), req.uri().path()) {
(&hyper::Method::POST, "/echo_server") => { (
&hyper::Method::POST | &hyper::Method::PATCH | &hyper::Method::PUT,
"/echo_server",
) => {
let (parts, body) = req.into_parts(); let (parts, body) = req.into_parts();
let mut response = Response::new(body); let mut response = Response::new(body);