0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-10-29 08:58:01 -04:00

fix(node/http): allow callback in first argument of end call (#19778)

Closes #19762
This commit is contained in:
Leo Kettmeir 2023-07-11 14:49:19 +02:00 committed by GitHub
parent be9e73d340
commit 4cfc54931d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 5 deletions

View file

@ -707,3 +707,31 @@ Deno.test(
await promise;
},
);
Deno.test(
"[node/http] client end with callback",
{ permissions: { net: true } },
async () => {
const promise = deferred();
let body = "";
const request = http.request(
"http://localhost:4545/http_version",
(resp) => {
resp.on("data", (chunk) => {
body += chunk;
});
resp.on("end", () => {
promise.resolve();
});
},
);
request.on("error", promise.reject);
request.end();
await promise;
assertEquals(body, "HTTP/1.1");
},
);

View file

@ -599,6 +599,15 @@ class ClientRequest extends OutgoingMessage {
// deno-lint-ignore no-explicit-any
end(chunk?: any, encoding?: any, cb?: any): this {
if (typeof chunk === "function") {
cb = chunk;
chunk = null;
encoding = null;
} else if (typeof encoding === "function") {
cb = encoding;
encoding = null;
}
this.finished = true;
if (chunk !== undefined && chunk !== null) {
this.write(chunk, encoding);
@ -617,13 +626,13 @@ class ClientRequest extends OutgoingMessage {
}
core.tryClose(this._bodyWriteRid);
}
try {
cb?.();
} catch (_) {
//
}
}
})(),
]);
if (this._timeout) {