diff --git a/cli/tests/unit_node/http_test.ts b/cli/tests/unit_node/http_test.ts index 4732781f8b..d54368003d 100644 --- a/cli/tests/unit_node/http_test.ts +++ b/cli/tests/unit_node/http_test.ts @@ -196,11 +196,14 @@ Deno.test("[node/http] request default protocol", async () => { // @ts-ignore IncomingMessageForClient // deno-lint-ignore no-explicit-any let clientRes: any; + // deno-lint-ignore no-explicit-any + let clientReq: any; server.listen(() => { - const req = http.request( + clientReq = http.request( // deno-lint-ignore no-explicit-any { host: "localhost", port: (server.address() as any).port }, (res) => { + assert(res.socket instanceof EventEmitter); assertEquals(res.complete, false); res.on("data", () => {}); res.on("end", () => { @@ -211,13 +214,14 @@ Deno.test("[node/http] request default protocol", async () => { promise2.resolve(); }, ); - req.end(); + clientReq.end(); }); server.on("close", () => { promise.resolve(); }); await promise; await promise2; + assert(clientReq.socket instanceof EventEmitter); assertEquals(clientRes!.complete, true); }); diff --git a/ext/node/polyfills/http.ts b/ext/node/polyfills/http.ts index ff6dede3f4..0529ccca59 100644 --- a/ext/node/polyfills/http.ts +++ b/ext/node/polyfills/http.ts @@ -270,6 +270,9 @@ const kError = Symbol("kError"); const kUniqueHeaders = Symbol("kUniqueHeaders"); +class FakeSocket extends EventEmitter { +} + /** ClientRequest represents the http(s) request from the client */ class ClientRequest extends OutgoingMessage { defaultProtocol = "http:"; @@ -544,6 +547,7 @@ class ClientRequest extends OutgoingMessage { this.onSocket(createConnection(optsWithoutSignal)); } }*/ + this.onSocket(new FakeSocket()); const url = this._createUrlStrFromOptions(); @@ -573,41 +577,12 @@ class ClientRequest extends OutgoingMessage { return undefined; } - onSocket(socket, err) { - if (this.destroyed || err) { - this.destroyed = true; - - // deno-lint-ignore no-inner-declarations - function _destroy(req, err) { - if (!req.aborted && !err) { - err = connResetException("socket hang up"); - } - if (err) { - req.emit("error", err); - } - req._closed = true; - req.emit("close"); - } - - if (socket) { - if (!err && this.agent && !socket.destroyed) { - socket.emit("free"); - } else { - finished(socket.destroy(err || this[kError]), (er) => { - if (er?.code === "ERR_STREAM_PREMATURE_CLOSE") { - er = null; - } - _destroy(this, er || err); - }); - return; - } - } - - _destroy(this, err || this[kError]); - } else { - //tickOnSocket(this, socket); - //this._flush(); - } + // TODO(bartlomieju): handle error + onSocket(socket, _err) { + nextTick(() => { + this.socket = socket; + this.emit("socket", socket); + }); } // deno-lint-ignore no-explicit-any