diff options
Diffstat (limited to 'ext/node/polyfills/http.ts')
-rw-r--r-- | ext/node/polyfills/http.ts | 51 |
1 files changed, 24 insertions, 27 deletions
diff --git a/ext/node/polyfills/http.ts b/ext/node/polyfills/http.ts index 101d888f8..1670ef0b9 100644 --- a/ext/node/polyfills/http.ts +++ b/ext/node/polyfills/http.ts @@ -48,6 +48,7 @@ import { } from "ext:deno_node/internal/errors.ts"; import { getTimerDuration } from "ext:deno_node/internal/timers.mjs"; import { serve, upgradeHttpRaw } from "ext:deno_http/00_serve.js"; +import { createHttpClient } from "ext:deno_fetch/22_http_client.js"; enum STATUS_CODES { /** RFC 7231, 6.2.1 */ @@ -541,13 +542,14 @@ class ClientRequest extends OutgoingMessage { } } - const client = this._getClient(); + const client = this._getClient() ?? createHttpClient({ http2: false }); + this._client = client; const req = core.ops.op_node_http_request( this.method, url, headers, - client, + client.rid, this.method === "POST" || this.method === "PATCH", ); @@ -652,6 +654,10 @@ class ClientRequest extends OutgoingMessage { this.controller.close(); core.opAsync("op_fetch_send", this._req.requestRid).then((res) => { + if (this._timeout) { + this._timeout.onabort = null; + } + this._client.close(); const incoming = new IncomingMessageForClient(this.socket); // TODO(@crowlKats): @@ -665,7 +671,10 @@ class ClientRequest extends OutgoingMessage { incoming.statusCode = res.status; incoming.statusMessage = res.statusText; - incoming._addHeaderLines(res.headers); + incoming._addHeaderLines( + res.headers, + Object.entries(res.headers).flat().length, + ); incoming._bodyRid = res.responseRid; if (this._req.cancelHandleRid !== null) { @@ -793,31 +802,19 @@ class ClientRequest extends OutgoingMessage { }${path}${search}${hash}`; } - setTimeout(timeout: number, callback?: () => void) { - if (timeout == 0) { - // Node's underlying Socket implementation expects a 0 value to disable the - // existing timeout. - if (this.opts.timeout) { - clearTimeout(this.opts.timeout); - this.opts.timeout = undefined; - this.opts.signal = undefined; - } - - return; + setTimeout(msecs: number, callback?: () => void) { + if (this._ended || this._timeout) { + return this; } - const controller = new AbortController(); - this.opts.signal = controller.signal; - - this.opts.timeout = setTimeout(() => { - controller.abort(); + msecs = getTimerDuration(msecs, "msecs"); + if (callback) this.once("timeout", callback); - this.emit("timeout"); + const timeout = AbortSignal.timeout(msecs); + timeout.onabort = () => this.emit("timeout"); + this._timeout = timeout; - if (callback !== undefined) { - callback(); - } - }, timeout); + return this; } _processHeader(headers, key, value, validate) { @@ -860,7 +857,7 @@ function isCookieField(s) { function isContentDispositionField(s) { return s.length === 19 && - StringPrototypeToLowerCase(s) === "content-disposition"; + s.toLowerCase() === "content-disposition"; } const kHeaders = Symbol("kHeaders"); @@ -1111,7 +1108,7 @@ export class IncomingMessageForClient extends NodeReadable { } _addHeaderLineDistinct(field, value, dest) { - field = StringPrototypeToLowerCase(field); + field = field.toLowerCase(); if (!dest[field]) { dest[field] = [value]; } else { @@ -1256,7 +1253,7 @@ function matchKnownFields(field, lowercased) { if (lowercased) { return "\u0000" + field; } - return matchKnownFields(StringPrototypeToLowerCase(field), true); + return matchKnownFields(field.toLowerCase(), true); } function onError(self, error, cb) { |