diff options
author | 2023-05-12 13:27:17 -0300 | |
---|---|---|
committer | 2023-05-12 09:27:17 -0700 | |
commit | f48f48795b6ee427c13c2249fa824e7eb731953c (patch) | |
tree | 4f1a0b1c4bbcbcfcefd966ed90ba9e0ae3547d70 | |
parent | 8f3b6486020977b0cd12f18da0475306a0dae8fe (diff) | |
download | bun-f48f48795b6ee427c13c2249fa824e7eb731953c.tar.gz bun-f48f48795b6ee427c13c2249fa824e7eb731953c.tar.zst bun-f48f48795b6ee427c13c2249fa824e7eb731953c.zip |
proper stub for sockets (#2868)
-rw-r--r-- | src/bun.js/http.exports.js | 88 |
1 files changed, 80 insertions, 8 deletions
diff --git a/src/bun.js/http.exports.js b/src/bun.js/http.exports.js index e6bff65aa..8176c1260 100644 --- a/src/bun.js/http.exports.js +++ b/src/bun.js/http.exports.js @@ -1,5 +1,5 @@ const { EventEmitter } = import.meta.require("node:events"); -const { Readable, Writable } = import.meta.require("node:stream"); +const { Readable, Writable, Duplex } = import.meta.require("node:stream"); const { URL } = import.meta.require("node:url"); const { newArrayWithSize, String, Object, Array } = import.meta.primordials; @@ -40,16 +40,85 @@ var _globalAgent; var _defaultHTTPSAgent; var kInternalRequest = Symbol("kInternalRequest"); -var FakeSocket = class Socket { - on() { +var FakeSocket = class Socket extends Duplex { + bytesRead = 0; + bytesWritten = 0; + connecting = false; + remoteAddress = null; + localAddress = "127.0.0.1"; + remotePort; + timeout = 0; + + isServer = false; + + address() { + return { + address: this.localAddress, + family: this.localFamily, + port: this.localPort, + }; + } + + get bufferSize() { + return this.writableLength; + } + + connect(port, host, connectListener) { + return this; + } + + _destroy(err, callback) {} + + _final(callback) {} + + get localAddress() { + return "127.0.0.1"; + } + + get localFamily() { + return "IPv4"; + } + + get localPort() { + return 80; + } + + get pending() { + return this.connecting; + } + + _read(size) {} + + get readyState() { + if (this.connecting) return "opening"; + if (this.readable) { + return this.writable ? "open" : "readOnly"; + } else { + return this.writable ? "writeOnly" : "closed"; + } + } + + ref() {} + + get remoteFamily() { + return "IPv4"; + } + + resetAndDestroy() {} + + setKeepAlive(enable = false, initialDelay = 0) {} + + setNoDelay(noDelay = true) { return this; } - off() {} - addListener() { + + setTimeout(timeout, callback) { return this; } - removeListener() {} - removeAllListeners() {} + + unref() {} + + _write(chunk, encoding, callback) {} }; export function createServer(options, callback) { @@ -376,7 +445,10 @@ export class IncomingMessage extends Readable { this.complete = !!this.#noBody; this.#bodyStream = null; - this.#fakeSocket = undefined; + const socket = new FakeSocket(); + socket.remoteAddress = url.hostname; + socket.remotePort = url.port; + this.#fakeSocket = socket; this.url = url.pathname + url.search; this.#nodeReq = nodeReq; |