diff options
Diffstat (limited to 'src/js/node/http.ts')
-rw-r--r-- | src/js/node/http.ts | 49 |
1 files changed, 33 insertions, 16 deletions
diff --git a/src/js/node/http.ts b/src/js/node/http.ts index 8a7cd35ae..77f8c55de 100644 --- a/src/js/node/http.ts +++ b/src/js/node/http.ts @@ -125,21 +125,18 @@ function validateFunction(callable: any, field: string) { type FakeSocket = InstanceType<typeof FakeSocket>; var FakeSocket = class Socket extends Duplex { - [kInternalSocketData]: any; + [kInternalSocketData]!: [import("bun").Server, OutgoingMessage, Request]; bytesRead = 0; bytesWritten = 0; connecting = false; - remoteAddress: string | null = null; - remotePort; timeout = 0; isServer = false; + #address; address() { - return { - address: this.localAddress, - family: this.localFamily, - port: this.localPort, - }; + // Call server.requestIP() without doing any propety getter twice. + var internalData; + return (this.#address ??= (internalData = this[kInternalSocketData])[0].requestIP(internalData[2])); } get bufferSize() { @@ -183,8 +180,31 @@ var FakeSocket = class Socket extends Duplex { ref() {} + get remoteAddress() { + return this.address()?.address; + } + + set remoteAddress(val) { + // initialize the object so that other properties wouldn't be lost + this.address().address = val; + } + + get remotePort() { + return this.address()?.port; + } + + set remotePort(val) { + // initialize the object so that other properties wouldn't be lost + this.address().port = val; + } + get remoteFamily() { - return "IPv4"; + return this.address()?.family; + } + + set remoteFamily(val) { + // initialize the object so that other properties wouldn't be lost + this.address().family = val; } resetAndDestroy() {} @@ -511,14 +531,14 @@ class Server extends EventEmitter { const http_req = new RequestClass(req); const http_res = new ResponseClass({ reply, req: http_req }); + http_req.socket[kInternalSocketData] = [_server, http_res, req]; + http_req.once("error", err => reject(err)); http_res.once("error", err => reject(err)); const upgrade = req.headers.get("upgrade"); if (upgrade) { - const socket = http_req.socket; - socket[kInternalSocketData] = [_server, http_res, req]; - server.emit("upgrade", http_req, socket, kEmptyBuffer); + server.emit("upgrade", http_req, http_req.socket, kEmptyBuffer); } else { server.emit("request", http_req, http_res); } @@ -598,13 +618,11 @@ class IncomingMessage extends Readable { this.#bodyStream = undefined; const socket = new FakeSocket(); - socket.remoteAddress = url.hostname; - socket.remotePort = url.port; if (url.protocol === "https:") socket.encrypted = true; this.#fakeSocket = socket; this.url = url.pathname + url.search; - this.#nodeReq = this.req = nodeReq; + this.req = nodeReq; assignHeaders(this, req); } @@ -619,7 +637,6 @@ class IncomingMessage extends Readable { #req; url; #type; - #nodeReq; _construct(callback) { // TODO: streaming |