diff --git a/src/bun.js/api/bun/socket.zig b/src/bun.js/api/bun/socket.zig index 4cdcbdac84625..a8c0948d94a36 100644 --- a/src/bun.js/api/bun/socket.zig +++ b/src/bun.js/api/bun/socket.zig @@ -996,7 +996,12 @@ pub const Listener = struct { handlers.vm.allocator.destroy(handlers_ptr); handlers.promise.deinit(); bun.default_allocator.destroy(tls); - exception.* = ZigString.static("Failed to connect").toErrorInstance(globalObject).asObjectRef(); + const err = JSC.SystemError{ + .message = bun.String.static("Failed to connect"), + .syscall = bun.String.static("connect"), + .code = if (port == null) bun.String.static("ENOENT") else bun.String.static("ECONNREFUSED"), + }; + exception.* = err.toErrorInstance(globalObject).asObjectRef(); return .zero; }; tls.poll_ref.ref(handlers.vm); @@ -1022,7 +1027,12 @@ pub const Listener = struct { handlers.vm.allocator.destroy(handlers_ptr); handlers.promise.deinit(); bun.default_allocator.destroy(tcp); - exception.* = ZigString.static("Failed to connect").toErrorInstance(globalObject).asObjectRef(); + const err = JSC.SystemError{ + .message = bun.String.static("Failed to connect"), + .syscall = bun.String.static("connect"), + .code = if (port == null) bun.String.static("ENOENT") else bun.String.static("ECONNREFUSED"), + }; + exception.* = err.toErrorInstance(globalObject).asObjectRef(); return .zero; }; tcp.poll_ref.ref(handlers.vm); @@ -1205,6 +1215,12 @@ fn NewSocket(comptime ssl: bool) type { .errno = errno, .message = bun.String.static("Failed to connect"), .syscall = bun.String.static("connect"), + + // For some reason errno is 0 which causes this to be success. + // Unix socket case wont hit this callback because it instantly errors. + .code = bun.String.static("ECONNREFUSED"), + // .code = bun.String.static(@tagName(bun.sys.getErrno(errno))), + // .code = bun.String.static(@tagName(@as(bun.C.E, @enumFromInt(errno)))), }; if (callback == .zero) { diff --git a/src/js/node/net.js b/src/js/node/net.js index f0873ae22d89a..5283538c3ac77 100644 --- a/src/js/node/net.js +++ b/src/js/node/net.js @@ -66,6 +66,11 @@ const bunSocketServerOptions = Symbol.for("::bunnetserveroptions::"); const bunSocketInternal = Symbol.for("::bunnetsocketinternal::"); const bunTLSConnectOptions = Symbol.for("::buntlsconnectoptions::"); +function endNT(socket, callback, err) { + socket.end(); + callback(err); +} + var SocketClass; const Socket = (function (InternalSocket) { SocketClass = InternalSocket; @@ -446,34 +451,11 @@ const Socket = (function (InternalSocket) { } else if (connectListener) this.on("connect", connectListener); // start using existing connection - if (connection) { - const socket = connection[bunSocketInternal]; - - if (socket) { - this.connecting = true; - this.#upgraded = true; - const result = socket.upgradeTLS({ - data: this, - tls, - socket: Socket.#Handlers, - }); - if (result) { - const [raw, tls] = result; - // replace socket - connection[bunSocketInternal] = raw; - raw.timeout(raw.timeout); - raw.connecting = false; - this[bunSocketInternal] = tls; - } else { - this[bunSocketInternal] = null; - throw new Error("Invalid socket"); - } - } else { - // wait to be connected - connection.once("connect", () => { - const socket = connection[bunSocketInternal]; - if (!socket) return; + try { + if (connection) { + const socket = connection[bunSocketInternal]; + if (socket) { this.connecting = true; this.#upgraded = true; const result = socket.upgradeTLS({ @@ -481,7 +463,6 @@ const Socket = (function (InternalSocket) { tls, socket: Socket.#Handlers, }); - if (result) { const [raw, tls] = result; // replace socket @@ -493,38 +474,66 @@ const Socket = (function (InternalSocket) { this[bunSocketInternal] = null; throw new Error("Invalid socket"); } + } else { + // wait to be connected + connection.once("connect", () => { + const socket = connection[bunSocketInternal]; + if (!socket) return; + + this.connecting = true; + this.#upgraded = true; + const result = socket.upgradeTLS({ + data: this, + tls, + socket: Socket.#Handlers, + }); + + if (result) { + const [raw, tls] = result; + // replace socket + connection[bunSocketInternal] = raw; + raw.timeout(raw.timeout); + raw.connecting = false; + this[bunSocketInternal] = tls; + } else { + this[bunSocketInternal] = null; + throw new Error("Invalid socket"); + } + }); + } + } else if (path) { + // start using unix socket + bunConnect({ + data: this, + unix: path, + socket: Socket.#Handlers, + tls, + }).catch(error => { + this.emit("error", error); + this.emit("close"); + }); + } else { + // default start + bunConnect({ + data: this, + hostname: host || "localhost", + port: port, + socket: Socket.#Handlers, + tls, + }).catch(error => { + this.emit("error", error); + this.emit("close"); }); } - } else if (path) { - // start using unix socket - bunConnect({ - data: this, - unix: path, - socket: Socket.#Handlers, - tls, - }).catch(error => { - this.emit("error", error); - this.emit("close"); - }); - } else { - // default start - bunConnect({ - data: this, - hostname: host || "localhost", - port: port, - socket: Socket.#Handlers, - tls, - }).catch(error => { - this.emit("error", error); - this.emit("close"); - }); + } catch (error) { + process.nextTick(emitErrorAndCloseNextTick, this, error); } return this; } _destroy(err, callback) { - this[bunSocketInternal]?.end(); - callback(err); + const socket = this[bunSocketInternal]; + socket && process.nextTick(endNT, socket, callback, err); } _final(callback) { @@ -864,6 +873,11 @@ function emitErrorNextTick(self, error) { self.emit("error", error); } +function emitErrorAndCloseNextTick(self, error) { + self.emit("error", error); + self.emit("close"); +} + function emitListeningNextTick(self, onListen) { if (typeof onListen === "function") { try { diff --git a/src/js/out/InternalModuleRegistryConstants.h b/src/js/out/InternalModuleRegistryConstants.h index 836f16aca756c..474eddb28a5d6 100644 --- a/src/js/out/InternalModuleRegistryConstants.h +++ b/src/js/out/InternalModuleRegistryConstants.h @@ -106,7 +106,7 @@ static constexpr ASCIILiteral NodeInspectorCode = "(function (){\"use strict\";/ // // -static constexpr ASCIILiteral NodeNetCode = "(function (){\"use strict\";// src/js/out/tmp/node/net.ts\nvar isIPv4 = function(s) {\n return IPv4Reg.test(s);\n}, isIPv6 = function(s) {\n return IPv6Reg.test(s);\n}, isIP = function(s) {\n if (isIPv4(s))\n return 4;\n if (isIPv6(s))\n return 6;\n return 0;\n}, createConnection = function(port, host, connectListener) {\n if (typeof port === \"object\")\n return new Socket(port).connect(port, host, connectListener);\n return new Socket().connect(port, host, connectListener);\n}, emitErrorNextTick = function(self, error) {\n self.emit(\"error\", error);\n}, emitListeningNextTick = function(self, onListen) {\n if (typeof onListen === \"function\")\n try {\n onListen();\n } catch (err) {\n self.emit(\"error\", err);\n }\n self.emit(\"listening\");\n}, createServer = function(options, connectionListener) {\n return new Server(options, connectionListener);\n}, $, { Duplex } = @getInternalField(@internalModuleRegistry, 37) || @createInternalModuleById(37), EventEmitter = @getInternalField(@internalModuleRegistry, 18) || @createInternalModuleById(18);\nvar IPv4Reg = new @RegExp(\"^((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$\");\nvar IPv6Reg = new @RegExp(\"^((\?:(\?:[0-9a-fA-F]{1,4}):){7}(\?:(\?:[0-9a-fA-F]{1,4})|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){6}(\?:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|:(\?:[0-9a-fA-F]{1,4})|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){5}(\?::((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(\?:[0-9a-fA-F]{1,4})){1,2}|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){4}(\?:(:(\?:[0-9a-fA-F]{1,4})){0,1}:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(\?:[0-9a-fA-F]{1,4})){1,3}|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){3}(\?:(:(\?:[0-9a-fA-F]{1,4})){0,2}:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(\?:[0-9a-fA-F]{1,4})){1,4}|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){2}(\?:(:(\?:[0-9a-fA-F]{1,4})){0,3}:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(\?:[0-9a-fA-F]{1,4})){1,5}|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){1}(\?:(:(\?:[0-9a-fA-F]{1,4})){0,4}:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(\?:[0-9a-fA-F]{1,4})){1,6}|:)|(\?::((\?::(\?:[0-9a-fA-F]{1,4})){0,5}:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(\?::(\?:[0-9a-fA-F]{1,4})){1,7}|:)))(%[0-9a-zA-Z-.:]{1,})\?$\"), { connect: bunConnect } = Bun, { setTimeout } = globalThis, bunTlsSymbol = Symbol.for(\"::buntls::\"), bunSocketServerHandlers = Symbol.for(\"::bunsocket_serverhandlers::\"), bunSocketServerConnections = Symbol.for(\"::bunnetserverconnections::\"), bunSocketServerOptions = Symbol.for(\"::bunnetserveroptions::\"), bunSocketInternal = Symbol.for(\"::bunnetsocketinternal::\"), bunTLSConnectOptions = Symbol.for(\"::buntlsconnectoptions::\"), SocketClass, Socket = function(InternalSocket) {\n return SocketClass = InternalSocket, Object.defineProperty(SocketClass.prototype, Symbol.toStringTag, {\n value: \"Socket\",\n enumerable: !1\n }), Object.defineProperty(function Socket(options) {\n return new InternalSocket(options);\n }, Symbol.hasInstance, {\n value(instance) {\n return instance instanceof InternalSocket;\n }\n });\n}(class Socket2 extends Duplex {\n static #Handlers = {\n close: Socket2.#Close,\n data({ data: self }, buffer) {\n self.bytesRead += buffer.length;\n const queue = self.#readQueue;\n if (queue.isEmpty()) {\n if (self.push(buffer))\n return;\n }\n queue.push(buffer);\n },\n drain: Socket2.#Drain,\n end: Socket2.#Close,\n error(socket, error) {\n const self = socket.data, callback = self.#writeCallback;\n if (callback)\n self.#writeCallback = null, callback(error);\n self.emit(\"error\", error);\n },\n open(socket) {\n const self = socket.data;\n socket.timeout(self.timeout), socket.ref(), self[bunSocketInternal] = socket, self.connecting = !1;\n const options = self[bunTLSConnectOptions];\n if (options) {\n const { session } = options;\n if (session)\n self.setSession(session);\n }\n if (!self.#upgraded)\n self.emit(\"connect\", self);\n Socket2.#Drain(socket);\n },\n handshake(socket, success, verifyError) {\n const { data: self } = socket;\n self._securePending = !1, self.secureConnecting = !1, self._secureEstablished = !!success, self.emit(\"secure\", self);\n const { checkServerIdentity } = self[bunTLSConnectOptions];\n if (!verifyError && typeof checkServerIdentity === \"function\" && self.servername) {\n const cert = self.getPeerCertificate(!0);\n verifyError = checkServerIdentity(self.servername, cert);\n }\n if (self._requestCert || self._rejectUnauthorized) {\n if (verifyError) {\n if (self.authorized = !1, self.authorizationError = verifyError.code || verifyError.message, self._rejectUnauthorized) {\n self.destroy(verifyError);\n return;\n }\n }\n } else\n self.authorized = !0;\n self.emit(\"secureConnect\", verifyError);\n },\n timeout(socket) {\n const self = socket.data;\n self.emit(\"timeout\", self);\n },\n binaryType: \"buffer\"\n };\n static #Close(socket) {\n const self = socket.data;\n if (self.#closed)\n return;\n self.#closed = !0, self[bunSocketInternal] = null;\n const queue = self.#readQueue;\n if (queue.isEmpty()) {\n if (self.push(null))\n return;\n }\n queue.push(null);\n }\n static #Drain(socket) {\n const self = socket.data, callback = self.#writeCallback;\n if (callback) {\n const chunk = self.#writeChunk, written = socket.write(chunk);\n if (self.bytesWritten += written, written < chunk.length)\n self.#writeChunk = chunk.slice(written);\n else\n self.#writeCallback = null, self.#writeChunk = null, callback(null);\n }\n }\n static [bunSocketServerHandlers] = {\n data: Socket2.#Handlers.data,\n close(socket) {\n Socket2.#Handlers.close(socket), this.data[bunSocketServerConnections]--;\n },\n end(socket) {\n Socket2.#Handlers.end(socket), this.data[bunSocketServerConnections]--;\n },\n open(socket) {\n const self = this.data, options = self[bunSocketServerOptions], { pauseOnConnect, connectionListener, InternalSocketClass, requestCert, rejectUnauthorized } = options, _socket = new InternalSocketClass({});\n if (_socket.isServer = !0, _socket._requestCert = requestCert, _socket._rejectUnauthorized = rejectUnauthorized, _socket.#attach(this.localPort, socket), self.maxConnections && self[bunSocketServerConnections] >= self.maxConnections) {\n const data = {\n localAddress: _socket.localAddress,\n localPort: _socket.localPort,\n localFamily: _socket.localFamily,\n remoteAddress: _socket.remoteAddress,\n remotePort: _socket.remotePort,\n remoteFamily: _socket.remoteFamily || \"IPv4\"\n };\n socket.end(), self.emit(\"drop\", data);\n return;\n }\n if (!pauseOnConnect)\n _socket.resume();\n if (self[bunSocketServerConnections]++, typeof connectionListener == \"function\")\n if (InternalSocketClass.name === \"TLSSocket\")\n self.once(\"secureConnection\", () => connectionListener(_socket));\n else\n connectionListener(_socket);\n self.emit(\"connection\", _socket);\n },\n handshake(socket, success, verifyError) {\n const { data: self } = socket;\n if (self.emit(\"secure\", self), self._securePending = !1, self.secureConnecting = !1, self._secureEstablished = !!success, self._requestCert || self._rejectUnauthorized) {\n if (verifyError) {\n if (self.authorized = !1, self.authorizationError = verifyError.code || verifyError.message, self._rejectUnauthorized) {\n self.destroy(verifyError);\n return;\n }\n }\n } else\n self.authorized = !0;\n self.emit(\"secureConnection\", verifyError);\n },\n error(socket, error) {\n Socket2.#Handlers.error(socket, error), this.data.emit(\"error\", error);\n },\n timeout: Socket2.#Handlers.timeout,\n connectError: Socket2.#Handlers.connectError,\n drain: Socket2.#Handlers.drain,\n binaryType: \"buffer\"\n };\n bytesRead = 0;\n bytesWritten = 0;\n #closed = !1;\n connecting = !1;\n localAddress = \"127.0.0.1\";\n #readQueue = @createFIFO();\n remotePort;\n [bunSocketInternal] = null;\n [bunTLSConnectOptions] = null;\n timeout = 0;\n #writeCallback;\n #writeChunk;\n #pendingRead;\n isServer = !1;\n _handle;\n _parent;\n _parentWrap;\n #socket;\n #upgraded;\n constructor(options) {\n const { socket, signal, write, read, allowHalfOpen = !1, ...opts } = options || {};\n super({\n ...opts,\n allowHalfOpen,\n readable: !0,\n writable: !0\n });\n if (this._handle = this, this._parent = this, this._parentWrap = this, this.#pendingRead = @undefined, this.#upgraded = !1, socket instanceof Socket2)\n this.#socket = socket;\n signal\?.once(\"abort\", () => this.destroy()), this.once(\"connect\", () => this.emit(\"ready\"));\n }\n address() {\n return {\n address: this.localAddress,\n family: this.localFamily,\n port: this.localPort\n };\n }\n get bufferSize() {\n return this.writableLength;\n }\n #attach(port, socket) {\n if (this.remotePort = port, socket.data = this, socket.timeout(this.timeout), socket.ref(), this[bunSocketInternal] = socket, this.connecting = !1, !this.#upgraded)\n this.emit(\"connect\", this);\n Socket2.#Drain(socket);\n }\n connect(port, host, connectListener) {\n var path, connection = this.#socket, _checkServerIdentity = @undefined;\n if (typeof port === \"string\") {\n if (path = port, port = @undefined, typeof host === \"function\")\n connectListener = host, host = @undefined;\n } else if (typeof host == \"function\") {\n if (typeof port === \"string\")\n path = port, port = @undefined;\n connectListener = host, host = @undefined;\n }\n if (typeof port == \"object\") {\n var {\n port,\n host,\n path,\n socket,\n localAddress,\n localPort,\n family,\n hints,\n lookup,\n noDelay,\n keepAlive,\n keepAliveInitialDelay,\n requestCert,\n rejectUnauthorized,\n pauseOnConnect,\n servername,\n checkServerIdentity,\n session\n } = port;\n if (_checkServerIdentity = checkServerIdentity, this.servername = servername, socket)\n connection = socket;\n }\n if (!pauseOnConnect)\n this.resume();\n this.connecting = !0, this.remotePort = port;\n const bunTLS = this[bunTlsSymbol];\n var tls = @undefined;\n if (typeof bunTLS === \"function\") {\n if (tls = bunTLS.call(this, port, host, !0), this._requestCert = !0, this._rejectUnauthorized = rejectUnauthorized, tls) {\n if (tls.rejectUnauthorized = rejectUnauthorized, tls.requestCert = !0, tls.session = session || tls.session, this.servername = tls.servername, tls.checkServerIdentity = _checkServerIdentity || tls.checkServerIdentity, this[bunTLSConnectOptions] = tls, !connection && tls.socket)\n connection = tls.socket;\n }\n if (connection) {\n if (typeof connection !== \"object\" || !(connection instanceof Socket2) || typeof connection[bunTlsSymbol] === \"function\")\n @throwTypeError(\"socket must be an instance of net.Socket\");\n }\n if (this.authorized = !1, this.secureConnecting = !0, this._secureEstablished = !1, this._securePending = !0, connectListener)\n this.on(\"secureConnect\", connectListener);\n } else if (connectListener)\n this.on(\"connect\", connectListener);\n if (connection) {\n const socket2 = connection[bunSocketInternal];\n if (socket2) {\n this.connecting = !0, this.#upgraded = !0;\n const result = socket2.upgradeTLS({\n data: this,\n tls,\n socket: Socket2.#Handlers\n });\n if (result) {\n const [raw, tls2] = result;\n connection[bunSocketInternal] = raw, raw.timeout(raw.timeout), raw.connecting = !1, this[bunSocketInternal] = tls2;\n } else\n throw this[bunSocketInternal] = null, new Error(\"Invalid socket\");\n } else\n connection.once(\"connect\", () => {\n const socket3 = connection[bunSocketInternal];\n if (!socket3)\n return;\n this.connecting = !0, this.#upgraded = !0;\n const result = socket3.upgradeTLS({\n data: this,\n tls,\n socket: Socket2.#Handlers\n });\n if (result) {\n const [raw, tls2] = result;\n connection[bunSocketInternal] = raw, raw.timeout(raw.timeout), raw.connecting = !1, this[bunSocketInternal] = tls2;\n } else\n throw this[bunSocketInternal] = null, new Error(\"Invalid socket\");\n });\n } else if (path)\n bunConnect({\n data: this,\n unix: path,\n socket: Socket2.#Handlers,\n tls\n }).catch((error) => {\n this.emit(\"error\", error), this.emit(\"close\");\n });\n else\n bunConnect({\n data: this,\n hostname: host || \"localhost\",\n port,\n socket: Socket2.#Handlers,\n tls\n }).catch((error) => {\n this.emit(\"error\", error), this.emit(\"close\");\n });\n return this;\n }\n _destroy(err, callback) {\n this[bunSocketInternal]\?.end(), callback(err);\n }\n _final(callback) {\n this[bunSocketInternal]\?.end(), callback();\n }\n get localAddress() {\n return \"127.0.0.1\";\n }\n get localFamily() {\n return \"IPv4\";\n }\n get localPort() {\n return this[bunSocketInternal]\?.localPort;\n }\n get pending() {\n return this.connecting;\n }\n _read(size) {\n const queue = this.#readQueue;\n let chunk;\n while (chunk = queue.peek()) {\n if (!this.push(chunk))\n return;\n queue.shift();\n }\n }\n get readyState() {\n if (this.connecting)\n return \"opening\";\n if (this.readable)\n return this.writable \? \"open\" : \"readOnly\";\n else\n return this.writable \? \"writeOnly\" : \"closed\";\n }\n ref() {\n this[bunSocketInternal]\?.ref();\n }\n get remoteAddress() {\n return this[bunSocketInternal]\?.remoteAddress;\n }\n get remoteFamily() {\n return \"IPv4\";\n }\n resetAndDestroy() {\n this[bunSocketInternal]\?.end();\n }\n setKeepAlive(enable = !1, initialDelay = 0) {\n return this;\n }\n setNoDelay(noDelay = !0) {\n return this;\n }\n setTimeout(timeout, callback) {\n if (this[bunSocketInternal]\?.timeout(timeout), this.timeout = timeout, callback)\n this.once(\"timeout\", callback);\n return this;\n }\n unref() {\n this[bunSocketInternal]\?.unref();\n }\n _write(chunk, encoding, callback) {\n if (typeof chunk == \"string\" && encoding !== \"ascii\")\n chunk = @Buffer.from(chunk, encoding);\n var written = this[bunSocketInternal]\?.write(chunk);\n if (written == chunk.length)\n callback();\n else if (this.#writeCallback)\n callback(new Error(\"overlapping _write()\"));\n else {\n if (written > 0)\n if (typeof chunk == \"string\")\n chunk = chunk.slice(written);\n else\n chunk = chunk.subarray(written);\n this.#writeCallback = callback, this.#writeChunk = chunk;\n }\n }\n}), connect = createConnection;\n\nclass Server extends EventEmitter {\n #server;\n #listening = !1;\n [bunSocketServerConnections] = 0;\n [bunSocketServerOptions];\n maxConnections = 0;\n constructor(options, connectionListener) {\n super();\n if (typeof options === \"function\")\n connectionListener = options, options = {};\n else if (options == null || typeof options === \"object\")\n options = { ...options };\n else\n throw new Error(\"bun-net-polyfill: invalid arguments\");\n const { maxConnections } = options;\n this.maxConnections = Number.isSafeInteger(maxConnections) && maxConnections > 0 \? maxConnections : 0, options.connectionListener = connectionListener, this[bunSocketServerOptions] = options;\n }\n ref() {\n return this.#server\?.ref(), this;\n }\n unref() {\n return this.#server\?.unref(), this;\n }\n close(callback) {\n if (this.#server) {\n if (this.#server.stop(!0), this.#server = null, this.#listening = !1, this[bunSocketServerConnections] = 0, this.emit(\"close\"), typeof callback === \"function\")\n callback();\n return this;\n }\n if (typeof callback === \"function\") {\n const error = new Error(\"Server is not running\");\n error.code = \"ERR_SERVER_NOT_RUNNING\", callback(error);\n }\n return this;\n }\n address() {\n const server = this.#server;\n if (server) {\n const unix = server.unix;\n if (unix)\n return unix;\n let address = server.hostname;\n const type = isIP(address), port = server.port;\n if (typeof port === \"number\")\n return {\n port,\n address,\n family: type \? `IPv${type}` : @undefined\n };\n if (type)\n return {\n address,\n family: type \? `IPv${type}` : @undefined\n };\n return address;\n }\n return null;\n }\n getConnections(callback) {\n if (typeof callback === \"function\")\n callback(null, this.#server \? this[bunSocketServerConnections] : 0);\n return this;\n }\n listen(port, hostname, onListen) {\n let backlog, path, exclusive = !1;\n if (typeof port === \"string\") {\n if (Number.isSafeInteger(hostname)) {\n if (hostname > 0)\n backlog = hostname;\n } else if (typeof hostname === \"function\")\n onListen = hostname;\n path = port, hostname = @undefined, port = @undefined;\n } else {\n if (typeof hostname === \"function\")\n onListen = hostname, hostname = @undefined;\n if (typeof port === \"function\")\n onListen = port, port = 0;\n else if (typeof port === \"object\") {\n const options = port;\n options.signal\?.addEventListener(\"abort\", () => this.close()), hostname = options.host, exclusive = options.exclusive === !0;\n const path2 = options.path;\n if (port = options.port, !Number.isSafeInteger(port) || port < 0)\n if (path2)\n hostname = path2, port = @undefined;\n else {\n let message = 'The argument \\'options\\' must have the property \"port\" or \"path\"';\n try {\n message = `${message}. Received ${JSON.stringify(options)}`;\n } catch {\n }\n const error = @makeTypeError(message);\n throw error.code = \"ERR_INVALID_ARG_VALUE\", error;\n }\n else if (!Number.isSafeInteger(port) || port < 0)\n port = 0;\n if (typeof port.callback === \"function\")\n onListen = port\?.callback;\n } else if (!Number.isSafeInteger(port) || port < 0)\n port = 0;\n hostname = hostname || \"::\";\n }\n try {\n var tls = @undefined, TLSSocketClass = @undefined;\n const bunTLS = this[bunTlsSymbol], options = this[bunSocketServerOptions];\n if (typeof bunTLS === \"function\")\n [tls, TLSSocketClass] = bunTLS.call(this, port, hostname, !1), options.servername = tls.serverName, options.InternalSocketClass = TLSSocketClass;\n else\n options.InternalSocketClass = SocketClass;\n this.#server = Bun.listen(path \? {\n exclusive,\n unix: path,\n tls,\n socket: SocketClass[bunSocketServerHandlers]\n } : {\n exclusive,\n port,\n hostname,\n tls,\n socket: SocketClass[bunSocketServerHandlers]\n }), this.#server.data = this, this.#listening = !0, setTimeout(emitListeningNextTick, 1, this, onListen);\n } catch (err) {\n this.#listening = !1, setTimeout(emitErrorNextTick, 1, this, err);\n }\n return this;\n }\n}\n$ = {\n createServer,\n Server,\n createConnection,\n connect,\n isIP,\n isIPv4,\n isIPv6,\n Socket,\n [Symbol.for(\"::bunternal::\")]: SocketClass\n};\nreturn $})\n"_s; +static constexpr ASCIILiteral NodeNetCode = "(function (){\"use strict\";// src/js/out/tmp/node/net.ts\nvar isIPv4 = function(s) {\n return IPv4Reg.test(s);\n}, isIPv6 = function(s) {\n return IPv6Reg.test(s);\n}, isIP = function(s) {\n if (isIPv4(s))\n return 4;\n if (isIPv6(s))\n return 6;\n return 0;\n}, endNT = function(socket, callback, err) {\n socket.end(), callback(err);\n}, createConnection = function(port, host, connectListener) {\n if (typeof port === \"object\")\n return new Socket(port).connect(port, host, connectListener);\n return new Socket().connect(port, host, connectListener);\n}, emitErrorNextTick = function(self, error) {\n self.emit(\"error\", error);\n}, emitErrorAndCloseNextTick = function(self, error) {\n self.emit(\"error\", error), self.emit(\"close\");\n}, emitListeningNextTick = function(self, onListen) {\n if (typeof onListen === \"function\")\n try {\n onListen();\n } catch (err) {\n self.emit(\"error\", err);\n }\n self.emit(\"listening\");\n}, createServer = function(options, connectionListener) {\n return new Server(options, connectionListener);\n}, $, { Duplex } = @getInternalField(@internalModuleRegistry, 37) || @createInternalModuleById(37), EventEmitter = @getInternalField(@internalModuleRegistry, 18) || @createInternalModuleById(18);\nvar IPv4Reg = new @RegExp(\"^((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$\");\nvar IPv6Reg = new @RegExp(\"^((\?:(\?:[0-9a-fA-F]{1,4}):){7}(\?:(\?:[0-9a-fA-F]{1,4})|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){6}(\?:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|:(\?:[0-9a-fA-F]{1,4})|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){5}(\?::((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(\?:[0-9a-fA-F]{1,4})){1,2}|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){4}(\?:(:(\?:[0-9a-fA-F]{1,4})){0,1}:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(\?:[0-9a-fA-F]{1,4})){1,3}|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){3}(\?:(:(\?:[0-9a-fA-F]{1,4})){0,2}:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(\?:[0-9a-fA-F]{1,4})){1,4}|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){2}(\?:(:(\?:[0-9a-fA-F]{1,4})){0,3}:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(\?:[0-9a-fA-F]{1,4})){1,5}|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){1}(\?:(:(\?:[0-9a-fA-F]{1,4})){0,4}:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(\?:[0-9a-fA-F]{1,4})){1,6}|:)|(\?::((\?::(\?:[0-9a-fA-F]{1,4})){0,5}:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(\?::(\?:[0-9a-fA-F]{1,4})){1,7}|:)))(%[0-9a-zA-Z-.:]{1,})\?$\"), { connect: bunConnect } = Bun, { setTimeout } = globalThis, bunTlsSymbol = Symbol.for(\"::buntls::\"), bunSocketServerHandlers = Symbol.for(\"::bunsocket_serverhandlers::\"), bunSocketServerConnections = Symbol.for(\"::bunnetserverconnections::\"), bunSocketServerOptions = Symbol.for(\"::bunnetserveroptions::\"), bunSocketInternal = Symbol.for(\"::bunnetsocketinternal::\"), bunTLSConnectOptions = Symbol.for(\"::buntlsconnectoptions::\"), SocketClass, Socket = function(InternalSocket) {\n return SocketClass = InternalSocket, Object.defineProperty(SocketClass.prototype, Symbol.toStringTag, {\n value: \"Socket\",\n enumerable: !1\n }), Object.defineProperty(function Socket(options) {\n return new InternalSocket(options);\n }, Symbol.hasInstance, {\n value(instance) {\n return instance instanceof InternalSocket;\n }\n });\n}(class Socket2 extends Duplex {\n static #Handlers = {\n close: Socket2.#Close,\n data({ data: self }, buffer) {\n self.bytesRead += buffer.length;\n const queue = self.#readQueue;\n if (queue.isEmpty()) {\n if (self.push(buffer))\n return;\n }\n queue.push(buffer);\n },\n drain: Socket2.#Drain,\n end: Socket2.#Close,\n error(socket, error) {\n const self = socket.data, callback = self.#writeCallback;\n if (callback)\n self.#writeCallback = null, callback(error);\n self.emit(\"error\", error);\n },\n open(socket) {\n const self = socket.data;\n socket.timeout(self.timeout), socket.ref(), self[bunSocketInternal] = socket, self.connecting = !1;\n const options = self[bunTLSConnectOptions];\n if (options) {\n const { session } = options;\n if (session)\n self.setSession(session);\n }\n if (!self.#upgraded)\n self.emit(\"connect\", self);\n Socket2.#Drain(socket);\n },\n handshake(socket, success, verifyError) {\n const { data: self } = socket;\n self._securePending = !1, self.secureConnecting = !1, self._secureEstablished = !!success, self.emit(\"secure\", self);\n const { checkServerIdentity } = self[bunTLSConnectOptions];\n if (!verifyError && typeof checkServerIdentity === \"function\" && self.servername) {\n const cert = self.getPeerCertificate(!0);\n verifyError = checkServerIdentity(self.servername, cert);\n }\n if (self._requestCert || self._rejectUnauthorized) {\n if (verifyError) {\n if (self.authorized = !1, self.authorizationError = verifyError.code || verifyError.message, self._rejectUnauthorized) {\n self.destroy(verifyError);\n return;\n }\n }\n } else\n self.authorized = !0;\n self.emit(\"secureConnect\", verifyError);\n },\n timeout(socket) {\n const self = socket.data;\n self.emit(\"timeout\", self);\n },\n binaryType: \"buffer\"\n };\n static #Close(socket) {\n const self = socket.data;\n if (self.#closed)\n return;\n self.#closed = !0, self[bunSocketInternal] = null;\n const queue = self.#readQueue;\n if (queue.isEmpty()) {\n if (self.push(null))\n return;\n }\n queue.push(null);\n }\n static #Drain(socket) {\n const self = socket.data, callback = self.#writeCallback;\n if (callback) {\n const chunk = self.#writeChunk, written = socket.write(chunk);\n if (self.bytesWritten += written, written < chunk.length)\n self.#writeChunk = chunk.slice(written);\n else\n self.#writeCallback = null, self.#writeChunk = null, callback(null);\n }\n }\n static [bunSocketServerHandlers] = {\n data: Socket2.#Handlers.data,\n close(socket) {\n Socket2.#Handlers.close(socket), this.data[bunSocketServerConnections]--;\n },\n end(socket) {\n Socket2.#Handlers.end(socket), this.data[bunSocketServerConnections]--;\n },\n open(socket) {\n const self = this.data, options = self[bunSocketServerOptions], { pauseOnConnect, connectionListener, InternalSocketClass, requestCert, rejectUnauthorized } = options, _socket = new InternalSocketClass({});\n if (_socket.isServer = !0, _socket._requestCert = requestCert, _socket._rejectUnauthorized = rejectUnauthorized, _socket.#attach(this.localPort, socket), self.maxConnections && self[bunSocketServerConnections] >= self.maxConnections) {\n const data = {\n localAddress: _socket.localAddress,\n localPort: _socket.localPort,\n localFamily: _socket.localFamily,\n remoteAddress: _socket.remoteAddress,\n remotePort: _socket.remotePort,\n remoteFamily: _socket.remoteFamily || \"IPv4\"\n };\n socket.end(), self.emit(\"drop\", data);\n return;\n }\n if (!pauseOnConnect)\n _socket.resume();\n if (self[bunSocketServerConnections]++, typeof connectionListener == \"function\")\n if (InternalSocketClass.name === \"TLSSocket\")\n self.once(\"secureConnection\", () => connectionListener(_socket));\n else\n connectionListener(_socket);\n self.emit(\"connection\", _socket);\n },\n handshake(socket, success, verifyError) {\n const { data: self } = socket;\n if (self.emit(\"secure\", self), self._securePending = !1, self.secureConnecting = !1, self._secureEstablished = !!success, self._requestCert || self._rejectUnauthorized) {\n if (verifyError) {\n if (self.authorized = !1, self.authorizationError = verifyError.code || verifyError.message, self._rejectUnauthorized) {\n self.destroy(verifyError);\n return;\n }\n }\n } else\n self.authorized = !0;\n self.emit(\"secureConnection\", verifyError);\n },\n error(socket, error) {\n Socket2.#Handlers.error(socket, error), this.data.emit(\"error\", error);\n },\n timeout: Socket2.#Handlers.timeout,\n connectError: Socket2.#Handlers.connectError,\n drain: Socket2.#Handlers.drain,\n binaryType: \"buffer\"\n };\n bytesRead = 0;\n bytesWritten = 0;\n #closed = !1;\n connecting = !1;\n localAddress = \"127.0.0.1\";\n #readQueue = @createFIFO();\n remotePort;\n [bunSocketInternal] = null;\n [bunTLSConnectOptions] = null;\n timeout = 0;\n #writeCallback;\n #writeChunk;\n #pendingRead;\n isServer = !1;\n _handle;\n _parent;\n _parentWrap;\n #socket;\n #upgraded;\n constructor(options) {\n const { socket, signal, write, read, allowHalfOpen = !1, ...opts } = options || {};\n super({\n ...opts,\n allowHalfOpen,\n readable: !0,\n writable: !0\n });\n if (this._handle = this, this._parent = this, this._parentWrap = this, this.#pendingRead = @undefined, this.#upgraded = !1, socket instanceof Socket2)\n this.#socket = socket;\n signal\?.once(\"abort\", () => this.destroy()), this.once(\"connect\", () => this.emit(\"ready\"));\n }\n address() {\n return {\n address: this.localAddress,\n family: this.localFamily,\n port: this.localPort\n };\n }\n get bufferSize() {\n return this.writableLength;\n }\n #attach(port, socket) {\n if (this.remotePort = port, socket.data = this, socket.timeout(this.timeout), socket.ref(), this[bunSocketInternal] = socket, this.connecting = !1, !this.#upgraded)\n this.emit(\"connect\", this);\n Socket2.#Drain(socket);\n }\n connect(port, host, connectListener) {\n var path, connection = this.#socket, _checkServerIdentity = @undefined;\n if (typeof port === \"string\") {\n if (path = port, port = @undefined, typeof host === \"function\")\n connectListener = host, host = @undefined;\n } else if (typeof host == \"function\") {\n if (typeof port === \"string\")\n path = port, port = @undefined;\n connectListener = host, host = @undefined;\n }\n if (typeof port == \"object\") {\n var {\n port,\n host,\n path,\n socket,\n localAddress,\n localPort,\n family,\n hints,\n lookup,\n noDelay,\n keepAlive,\n keepAliveInitialDelay,\n requestCert,\n rejectUnauthorized,\n pauseOnConnect,\n servername,\n checkServerIdentity,\n session\n } = port;\n if (_checkServerIdentity = checkServerIdentity, this.servername = servername, socket)\n connection = socket;\n }\n if (!pauseOnConnect)\n this.resume();\n this.connecting = !0, this.remotePort = port;\n const bunTLS = this[bunTlsSymbol];\n var tls = @undefined;\n if (typeof bunTLS === \"function\") {\n if (tls = bunTLS.call(this, port, host, !0), this._requestCert = !0, this._rejectUnauthorized = rejectUnauthorized, tls) {\n if (tls.rejectUnauthorized = rejectUnauthorized, tls.requestCert = !0, tls.session = session || tls.session, this.servername = tls.servername, tls.checkServerIdentity = _checkServerIdentity || tls.checkServerIdentity, this[bunTLSConnectOptions] = tls, !connection && tls.socket)\n connection = tls.socket;\n }\n if (connection) {\n if (typeof connection !== \"object\" || !(connection instanceof Socket2) || typeof connection[bunTlsSymbol] === \"function\")\n @throwTypeError(\"socket must be an instance of net.Socket\");\n }\n if (this.authorized = !1, this.secureConnecting = !0, this._secureEstablished = !1, this._securePending = !0, connectListener)\n this.on(\"secureConnect\", connectListener);\n } else if (connectListener)\n this.on(\"connect\", connectListener);\n try {\n if (connection) {\n const socket2 = connection[bunSocketInternal];\n if (socket2) {\n this.connecting = !0, this.#upgraded = !0;\n const result = socket2.upgradeTLS({\n data: this,\n tls,\n socket: Socket2.#Handlers\n });\n if (result) {\n const [raw, tls2] = result;\n connection[bunSocketInternal] = raw, raw.timeout(raw.timeout), raw.connecting = !1, this[bunSocketInternal] = tls2;\n } else\n throw this[bunSocketInternal] = null, new Error(\"Invalid socket\");\n } else\n connection.once(\"connect\", () => {\n const socket3 = connection[bunSocketInternal];\n if (!socket3)\n return;\n this.connecting = !0, this.#upgraded = !0;\n const result = socket3.upgradeTLS({\n data: this,\n tls,\n socket: Socket2.#Handlers\n });\n if (result) {\n const [raw, tls2] = result;\n connection[bunSocketInternal] = raw, raw.timeout(raw.timeout), raw.connecting = !1, this[bunSocketInternal] = tls2;\n } else\n throw this[bunSocketInternal] = null, new Error(\"Invalid socket\");\n });\n } else if (path)\n bunConnect({\n data: this,\n unix: path,\n socket: Socket2.#Handlers,\n tls\n }).catch((error) => {\n this.emit(\"error\", error), this.emit(\"close\");\n });\n else\n bunConnect({\n data: this,\n hostname: host || \"localhost\",\n port,\n socket: Socket2.#Handlers,\n tls\n }).catch((error) => {\n this.emit(\"error\", error), this.emit(\"close\");\n });\n } catch (error) {\n process.nextTick(emitErrorAndCloseNextTick, this, error);\n }\n return this;\n }\n _destroy(err, callback) {\n const socket = this[bunSocketInternal];\n socket && process.nextTick(endNT, socket, callback, err);\n }\n _final(callback) {\n this[bunSocketInternal]\?.end(), callback();\n }\n get localAddress() {\n return \"127.0.0.1\";\n }\n get localFamily() {\n return \"IPv4\";\n }\n get localPort() {\n return this[bunSocketInternal]\?.localPort;\n }\n get pending() {\n return this.connecting;\n }\n _read(size) {\n const queue = this.#readQueue;\n let chunk;\n while (chunk = queue.peek()) {\n if (!this.push(chunk))\n return;\n queue.shift();\n }\n }\n get readyState() {\n if (this.connecting)\n return \"opening\";\n if (this.readable)\n return this.writable \? \"open\" : \"readOnly\";\n else\n return this.writable \? \"writeOnly\" : \"closed\";\n }\n ref() {\n this[bunSocketInternal]\?.ref();\n }\n get remoteAddress() {\n return this[bunSocketInternal]\?.remoteAddress;\n }\n get remoteFamily() {\n return \"IPv4\";\n }\n resetAndDestroy() {\n this[bunSocketInternal]\?.end();\n }\n setKeepAlive(enable = !1, initialDelay = 0) {\n return this;\n }\n setNoDelay(noDelay = !0) {\n return this;\n }\n setTimeout(timeout, callback) {\n if (this[bunSocketInternal]\?.timeout(timeout), this.timeout = timeout, callback)\n this.once(\"timeout\", callback);\n return this;\n }\n unref() {\n this[bunSocketInternal]\?.unref();\n }\n _write(chunk, encoding, callback) {\n if (typeof chunk == \"string\" && encoding !== \"ascii\")\n chunk = @Buffer.from(chunk, encoding);\n var written = this[bunSocketInternal]\?.write(chunk);\n if (written == chunk.length)\n callback();\n else if (this.#writeCallback)\n callback(new Error(\"overlapping _write()\"));\n else {\n if (written > 0)\n if (typeof chunk == \"string\")\n chunk = chunk.slice(written);\n else\n chunk = chunk.subarray(written);\n this.#writeCallback = callback, this.#writeChunk = chunk;\n }\n }\n}), connect = createConnection;\n\nclass Server extends EventEmitter {\n #server;\n #listening = !1;\n [bunSocketServerConnections] = 0;\n [bunSocketServerOptions];\n maxConnections = 0;\n constructor(options, connectionListener) {\n super();\n if (typeof options === \"function\")\n connectionListener = options, options = {};\n else if (options == null || typeof options === \"object\")\n options = { ...options };\n else\n throw new Error(\"bun-net-polyfill: invalid arguments\");\n const { maxConnections } = options;\n this.maxConnections = Number.isSafeInteger(maxConnections) && maxConnections > 0 \? maxConnections : 0, options.connectionListener = connectionListener, this[bunSocketServerOptions] = options;\n }\n ref() {\n return this.#server\?.ref(), this;\n }\n unref() {\n return this.#server\?.unref(), this;\n }\n close(callback) {\n if (this.#server) {\n if (this.#server.stop(!0), this.#server = null, this.#listening = !1, this[bunSocketServerConnections] = 0, this.emit(\"close\"), typeof callback === \"function\")\n callback();\n return this;\n }\n if (typeof callback === \"function\") {\n const error = new Error(\"Server is not running\");\n error.code = \"ERR_SERVER_NOT_RUNNING\", callback(error);\n }\n return this;\n }\n address() {\n const server = this.#server;\n if (server) {\n const unix = server.unix;\n if (unix)\n return unix;\n let address = server.hostname;\n const type = isIP(address), port = server.port;\n if (typeof port === \"number\")\n return {\n port,\n address,\n family: type \? `IPv${type}` : @undefined\n };\n if (type)\n return {\n address,\n family: type \? `IPv${type}` : @undefined\n };\n return address;\n }\n return null;\n }\n getConnections(callback) {\n if (typeof callback === \"function\")\n callback(null, this.#server \? this[bunSocketServerConnections] : 0);\n return this;\n }\n listen(port, hostname, onListen) {\n let backlog, path, exclusive = !1;\n if (typeof port === \"string\") {\n if (Number.isSafeInteger(hostname)) {\n if (hostname > 0)\n backlog = hostname;\n } else if (typeof hostname === \"function\")\n onListen = hostname;\n path = port, hostname = @undefined, port = @undefined;\n } else {\n if (typeof hostname === \"function\")\n onListen = hostname, hostname = @undefined;\n if (typeof port === \"function\")\n onListen = port, port = 0;\n else if (typeof port === \"object\") {\n const options = port;\n options.signal\?.addEventListener(\"abort\", () => this.close()), hostname = options.host, exclusive = options.exclusive === !0;\n const path2 = options.path;\n if (port = options.port, !Number.isSafeInteger(port) || port < 0)\n if (path2)\n hostname = path2, port = @undefined;\n else {\n let message = 'The argument \\'options\\' must have the property \"port\" or \"path\"';\n try {\n message = `${message}. Received ${JSON.stringify(options)}`;\n } catch {\n }\n const error = @makeTypeError(message);\n throw error.code = \"ERR_INVALID_ARG_VALUE\", error;\n }\n else if (!Number.isSafeInteger(port) || port < 0)\n port = 0;\n if (typeof port.callback === \"function\")\n onListen = port\?.callback;\n } else if (!Number.isSafeInteger(port) || port < 0)\n port = 0;\n hostname = hostname || \"::\";\n }\n try {\n var tls = @undefined, TLSSocketClass = @undefined;\n const bunTLS = this[bunTlsSymbol], options = this[bunSocketServerOptions];\n if (typeof bunTLS === \"function\")\n [tls, TLSSocketClass] = bunTLS.call(this, port, hostname, !1), options.servername = tls.serverName, options.InternalSocketClass = TLSSocketClass;\n else\n options.InternalSocketClass = SocketClass;\n this.#server = Bun.listen(path \? {\n exclusive,\n unix: path,\n tls,\n socket: SocketClass[bunSocketServerHandlers]\n } : {\n exclusive,\n port,\n hostname,\n tls,\n socket: SocketClass[bunSocketServerHandlers]\n }), this.#server.data = this, this.#listening = !0, setTimeout(emitListeningNextTick, 1, this, onListen);\n } catch (err) {\n this.#listening = !1, setTimeout(emitErrorNextTick, 1, this, err);\n }\n return this;\n }\n}\n$ = {\n createServer,\n Server,\n createConnection,\n connect,\n isIP,\n isIPv4,\n isIPv6,\n Socket,\n [Symbol.for(\"::bunternal::\")]: SocketClass\n};\nreturn $})\n"_s; // // @@ -347,7 +347,7 @@ static constexpr ASCIILiteral NodeInspectorCode = "(function (){\"use strict\";/ // // -static constexpr ASCIILiteral NodeNetCode = "(function (){\"use strict\";// src/js/out/tmp/node/net.ts\nvar isIPv4 = function(s) {\n return IPv4Reg.test(s);\n}, isIPv6 = function(s) {\n return IPv6Reg.test(s);\n}, isIP = function(s) {\n if (isIPv4(s))\n return 4;\n if (isIPv6(s))\n return 6;\n return 0;\n}, createConnection = function(port, host, connectListener) {\n if (typeof port === \"object\")\n return new Socket(port).connect(port, host, connectListener);\n return new Socket().connect(port, host, connectListener);\n}, emitErrorNextTick = function(self, error) {\n self.emit(\"error\", error);\n}, emitListeningNextTick = function(self, onListen) {\n if (typeof onListen === \"function\")\n try {\n onListen();\n } catch (err) {\n self.emit(\"error\", err);\n }\n self.emit(\"listening\");\n}, createServer = function(options, connectionListener) {\n return new Server(options, connectionListener);\n}, $, { Duplex } = @getInternalField(@internalModuleRegistry, 37) || @createInternalModuleById(37), EventEmitter = @getInternalField(@internalModuleRegistry, 18) || @createInternalModuleById(18);\nvar IPv4Reg = new @RegExp(\"^((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$\");\nvar IPv6Reg = new @RegExp(\"^((\?:(\?:[0-9a-fA-F]{1,4}):){7}(\?:(\?:[0-9a-fA-F]{1,4})|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){6}(\?:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|:(\?:[0-9a-fA-F]{1,4})|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){5}(\?::((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(\?:[0-9a-fA-F]{1,4})){1,2}|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){4}(\?:(:(\?:[0-9a-fA-F]{1,4})){0,1}:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(\?:[0-9a-fA-F]{1,4})){1,3}|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){3}(\?:(:(\?:[0-9a-fA-F]{1,4})){0,2}:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(\?:[0-9a-fA-F]{1,4})){1,4}|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){2}(\?:(:(\?:[0-9a-fA-F]{1,4})){0,3}:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(\?:[0-9a-fA-F]{1,4})){1,5}|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){1}(\?:(:(\?:[0-9a-fA-F]{1,4})){0,4}:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(\?:[0-9a-fA-F]{1,4})){1,6}|:)|(\?::((\?::(\?:[0-9a-fA-F]{1,4})){0,5}:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(\?::(\?:[0-9a-fA-F]{1,4})){1,7}|:)))(%[0-9a-zA-Z-.:]{1,})\?$\"), { connect: bunConnect } = Bun, { setTimeout } = globalThis, bunTlsSymbol = Symbol.for(\"::buntls::\"), bunSocketServerHandlers = Symbol.for(\"::bunsocket_serverhandlers::\"), bunSocketServerConnections = Symbol.for(\"::bunnetserverconnections::\"), bunSocketServerOptions = Symbol.for(\"::bunnetserveroptions::\"), bunSocketInternal = Symbol.for(\"::bunnetsocketinternal::\"), bunTLSConnectOptions = Symbol.for(\"::buntlsconnectoptions::\"), SocketClass, Socket = function(InternalSocket) {\n return SocketClass = InternalSocket, Object.defineProperty(SocketClass.prototype, Symbol.toStringTag, {\n value: \"Socket\",\n enumerable: !1\n }), Object.defineProperty(function Socket(options) {\n return new InternalSocket(options);\n }, Symbol.hasInstance, {\n value(instance) {\n return instance instanceof InternalSocket;\n }\n });\n}(class Socket2 extends Duplex {\n static #Handlers = {\n close: Socket2.#Close,\n data({ data: self }, buffer) {\n self.bytesRead += buffer.length;\n const queue = self.#readQueue;\n if (queue.isEmpty()) {\n if (self.push(buffer))\n return;\n }\n queue.push(buffer);\n },\n drain: Socket2.#Drain,\n end: Socket2.#Close,\n error(socket, error) {\n const self = socket.data, callback = self.#writeCallback;\n if (callback)\n self.#writeCallback = null, callback(error);\n self.emit(\"error\", error);\n },\n open(socket) {\n const self = socket.data;\n socket.timeout(self.timeout), socket.ref(), self[bunSocketInternal] = socket, self.connecting = !1;\n const options = self[bunTLSConnectOptions];\n if (options) {\n const { session } = options;\n if (session)\n self.setSession(session);\n }\n if (!self.#upgraded)\n self.emit(\"connect\", self);\n Socket2.#Drain(socket);\n },\n handshake(socket, success, verifyError) {\n const { data: self } = socket;\n self._securePending = !1, self.secureConnecting = !1, self._secureEstablished = !!success, self.emit(\"secure\", self);\n const { checkServerIdentity } = self[bunTLSConnectOptions];\n if (!verifyError && typeof checkServerIdentity === \"function\" && self.servername) {\n const cert = self.getPeerCertificate(!0);\n verifyError = checkServerIdentity(self.servername, cert);\n }\n if (self._requestCert || self._rejectUnauthorized) {\n if (verifyError) {\n if (self.authorized = !1, self.authorizationError = verifyError.code || verifyError.message, self._rejectUnauthorized) {\n self.destroy(verifyError);\n return;\n }\n }\n } else\n self.authorized = !0;\n self.emit(\"secureConnect\", verifyError);\n },\n timeout(socket) {\n const self = socket.data;\n self.emit(\"timeout\", self);\n },\n binaryType: \"buffer\"\n };\n static #Close(socket) {\n const self = socket.data;\n if (self.#closed)\n return;\n self.#closed = !0, self[bunSocketInternal] = null;\n const queue = self.#readQueue;\n if (queue.isEmpty()) {\n if (self.push(null))\n return;\n }\n queue.push(null);\n }\n static #Drain(socket) {\n const self = socket.data, callback = self.#writeCallback;\n if (callback) {\n const chunk = self.#writeChunk, written = socket.write(chunk);\n if (self.bytesWritten += written, written < chunk.length)\n self.#writeChunk = chunk.slice(written);\n else\n self.#writeCallback = null, self.#writeChunk = null, callback(null);\n }\n }\n static [bunSocketServerHandlers] = {\n data: Socket2.#Handlers.data,\n close(socket) {\n Socket2.#Handlers.close(socket), this.data[bunSocketServerConnections]--;\n },\n end(socket) {\n Socket2.#Handlers.end(socket), this.data[bunSocketServerConnections]--;\n },\n open(socket) {\n const self = this.data, options = self[bunSocketServerOptions], { pauseOnConnect, connectionListener, InternalSocketClass, requestCert, rejectUnauthorized } = options, _socket = new InternalSocketClass({});\n if (_socket.isServer = !0, _socket._requestCert = requestCert, _socket._rejectUnauthorized = rejectUnauthorized, _socket.#attach(this.localPort, socket), self.maxConnections && self[bunSocketServerConnections] >= self.maxConnections) {\n const data = {\n localAddress: _socket.localAddress,\n localPort: _socket.localPort,\n localFamily: _socket.localFamily,\n remoteAddress: _socket.remoteAddress,\n remotePort: _socket.remotePort,\n remoteFamily: _socket.remoteFamily || \"IPv4\"\n };\n socket.end(), self.emit(\"drop\", data);\n return;\n }\n if (!pauseOnConnect)\n _socket.resume();\n if (self[bunSocketServerConnections]++, typeof connectionListener == \"function\")\n if (InternalSocketClass.name === \"TLSSocket\")\n self.once(\"secureConnection\", () => connectionListener(_socket));\n else\n connectionListener(_socket);\n self.emit(\"connection\", _socket);\n },\n handshake(socket, success, verifyError) {\n const { data: self } = socket;\n if (self.emit(\"secure\", self), self._securePending = !1, self.secureConnecting = !1, self._secureEstablished = !!success, self._requestCert || self._rejectUnauthorized) {\n if (verifyError) {\n if (self.authorized = !1, self.authorizationError = verifyError.code || verifyError.message, self._rejectUnauthorized) {\n self.destroy(verifyError);\n return;\n }\n }\n } else\n self.authorized = !0;\n self.emit(\"secureConnection\", verifyError);\n },\n error(socket, error) {\n Socket2.#Handlers.error(socket, error), this.data.emit(\"error\", error);\n },\n timeout: Socket2.#Handlers.timeout,\n connectError: Socket2.#Handlers.connectError,\n drain: Socket2.#Handlers.drain,\n binaryType: \"buffer\"\n };\n bytesRead = 0;\n bytesWritten = 0;\n #closed = !1;\n connecting = !1;\n localAddress = \"127.0.0.1\";\n #readQueue = @createFIFO();\n remotePort;\n [bunSocketInternal] = null;\n [bunTLSConnectOptions] = null;\n timeout = 0;\n #writeCallback;\n #writeChunk;\n #pendingRead;\n isServer = !1;\n _handle;\n _parent;\n _parentWrap;\n #socket;\n #upgraded;\n constructor(options) {\n const { socket, signal, write, read, allowHalfOpen = !1, ...opts } = options || {};\n super({\n ...opts,\n allowHalfOpen,\n readable: !0,\n writable: !0\n });\n if (this._handle = this, this._parent = this, this._parentWrap = this, this.#pendingRead = @undefined, this.#upgraded = !1, socket instanceof Socket2)\n this.#socket = socket;\n signal\?.once(\"abort\", () => this.destroy()), this.once(\"connect\", () => this.emit(\"ready\"));\n }\n address() {\n return {\n address: this.localAddress,\n family: this.localFamily,\n port: this.localPort\n };\n }\n get bufferSize() {\n return this.writableLength;\n }\n #attach(port, socket) {\n if (this.remotePort = port, socket.data = this, socket.timeout(this.timeout), socket.ref(), this[bunSocketInternal] = socket, this.connecting = !1, !this.#upgraded)\n this.emit(\"connect\", this);\n Socket2.#Drain(socket);\n }\n connect(port, host, connectListener) {\n var path, connection = this.#socket, _checkServerIdentity = @undefined;\n if (typeof port === \"string\") {\n if (path = port, port = @undefined, typeof host === \"function\")\n connectListener = host, host = @undefined;\n } else if (typeof host == \"function\") {\n if (typeof port === \"string\")\n path = port, port = @undefined;\n connectListener = host, host = @undefined;\n }\n if (typeof port == \"object\") {\n var {\n port,\n host,\n path,\n socket,\n localAddress,\n localPort,\n family,\n hints,\n lookup,\n noDelay,\n keepAlive,\n keepAliveInitialDelay,\n requestCert,\n rejectUnauthorized,\n pauseOnConnect,\n servername,\n checkServerIdentity,\n session\n } = port;\n if (_checkServerIdentity = checkServerIdentity, this.servername = servername, socket)\n connection = socket;\n }\n if (!pauseOnConnect)\n this.resume();\n this.connecting = !0, this.remotePort = port;\n const bunTLS = this[bunTlsSymbol];\n var tls = @undefined;\n if (typeof bunTLS === \"function\") {\n if (tls = bunTLS.call(this, port, host, !0), this._requestCert = !0, this._rejectUnauthorized = rejectUnauthorized, tls) {\n if (tls.rejectUnauthorized = rejectUnauthorized, tls.requestCert = !0, tls.session = session || tls.session, this.servername = tls.servername, tls.checkServerIdentity = _checkServerIdentity || tls.checkServerIdentity, this[bunTLSConnectOptions] = tls, !connection && tls.socket)\n connection = tls.socket;\n }\n if (connection) {\n if (typeof connection !== \"object\" || !(connection instanceof Socket2) || typeof connection[bunTlsSymbol] === \"function\")\n @throwTypeError(\"socket must be an instance of net.Socket\");\n }\n if (this.authorized = !1, this.secureConnecting = !0, this._secureEstablished = !1, this._securePending = !0, connectListener)\n this.on(\"secureConnect\", connectListener);\n } else if (connectListener)\n this.on(\"connect\", connectListener);\n if (connection) {\n const socket2 = connection[bunSocketInternal];\n if (socket2) {\n this.connecting = !0, this.#upgraded = !0;\n const result = socket2.upgradeTLS({\n data: this,\n tls,\n socket: Socket2.#Handlers\n });\n if (result) {\n const [raw, tls2] = result;\n connection[bunSocketInternal] = raw, raw.timeout(raw.timeout), raw.connecting = !1, this[bunSocketInternal] = tls2;\n } else\n throw this[bunSocketInternal] = null, new Error(\"Invalid socket\");\n } else\n connection.once(\"connect\", () => {\n const socket3 = connection[bunSocketInternal];\n if (!socket3)\n return;\n this.connecting = !0, this.#upgraded = !0;\n const result = socket3.upgradeTLS({\n data: this,\n tls,\n socket: Socket2.#Handlers\n });\n if (result) {\n const [raw, tls2] = result;\n connection[bunSocketInternal] = raw, raw.timeout(raw.timeout), raw.connecting = !1, this[bunSocketInternal] = tls2;\n } else\n throw this[bunSocketInternal] = null, new Error(\"Invalid socket\");\n });\n } else if (path)\n bunConnect({\n data: this,\n unix: path,\n socket: Socket2.#Handlers,\n tls\n }).catch((error) => {\n this.emit(\"error\", error), this.emit(\"close\");\n });\n else\n bunConnect({\n data: this,\n hostname: host || \"localhost\",\n port,\n socket: Socket2.#Handlers,\n tls\n }).catch((error) => {\n this.emit(\"error\", error), this.emit(\"close\");\n });\n return this;\n }\n _destroy(err, callback) {\n this[bunSocketInternal]\?.end(), callback(err);\n }\n _final(callback) {\n this[bunSocketInternal]\?.end(), callback();\n }\n get localAddress() {\n return \"127.0.0.1\";\n }\n get localFamily() {\n return \"IPv4\";\n }\n get localPort() {\n return this[bunSocketInternal]\?.localPort;\n }\n get pending() {\n return this.connecting;\n }\n _read(size) {\n const queue = this.#readQueue;\n let chunk;\n while (chunk = queue.peek()) {\n if (!this.push(chunk))\n return;\n queue.shift();\n }\n }\n get readyState() {\n if (this.connecting)\n return \"opening\";\n if (this.readable)\n return this.writable \? \"open\" : \"readOnly\";\n else\n return this.writable \? \"writeOnly\" : \"closed\";\n }\n ref() {\n this[bunSocketInternal]\?.ref();\n }\n get remoteAddress() {\n return this[bunSocketInternal]\?.remoteAddress;\n }\n get remoteFamily() {\n return \"IPv4\";\n }\n resetAndDestroy() {\n this[bunSocketInternal]\?.end();\n }\n setKeepAlive(enable = !1, initialDelay = 0) {\n return this;\n }\n setNoDelay(noDelay = !0) {\n return this;\n }\n setTimeout(timeout, callback) {\n if (this[bunSocketInternal]\?.timeout(timeout), this.timeout = timeout, callback)\n this.once(\"timeout\", callback);\n return this;\n }\n unref() {\n this[bunSocketInternal]\?.unref();\n }\n _write(chunk, encoding, callback) {\n if (typeof chunk == \"string\" && encoding !== \"ascii\")\n chunk = @Buffer.from(chunk, encoding);\n var written = this[bunSocketInternal]\?.write(chunk);\n if (written == chunk.length)\n callback();\n else if (this.#writeCallback)\n callback(new Error(\"overlapping _write()\"));\n else {\n if (written > 0)\n if (typeof chunk == \"string\")\n chunk = chunk.slice(written);\n else\n chunk = chunk.subarray(written);\n this.#writeCallback = callback, this.#writeChunk = chunk;\n }\n }\n}), connect = createConnection;\n\nclass Server extends EventEmitter {\n #server;\n #listening = !1;\n [bunSocketServerConnections] = 0;\n [bunSocketServerOptions];\n maxConnections = 0;\n constructor(options, connectionListener) {\n super();\n if (typeof options === \"function\")\n connectionListener = options, options = {};\n else if (options == null || typeof options === \"object\")\n options = { ...options };\n else\n throw new Error(\"bun-net-polyfill: invalid arguments\");\n const { maxConnections } = options;\n this.maxConnections = Number.isSafeInteger(maxConnections) && maxConnections > 0 \? maxConnections : 0, options.connectionListener = connectionListener, this[bunSocketServerOptions] = options;\n }\n ref() {\n return this.#server\?.ref(), this;\n }\n unref() {\n return this.#server\?.unref(), this;\n }\n close(callback) {\n if (this.#server) {\n if (this.#server.stop(!0), this.#server = null, this.#listening = !1, this[bunSocketServerConnections] = 0, this.emit(\"close\"), typeof callback === \"function\")\n callback();\n return this;\n }\n if (typeof callback === \"function\") {\n const error = new Error(\"Server is not running\");\n error.code = \"ERR_SERVER_NOT_RUNNING\", callback(error);\n }\n return this;\n }\n address() {\n const server = this.#server;\n if (server) {\n const unix = server.unix;\n if (unix)\n return unix;\n let address = server.hostname;\n const type = isIP(address), port = server.port;\n if (typeof port === \"number\")\n return {\n port,\n address,\n family: type \? `IPv${type}` : @undefined\n };\n if (type)\n return {\n address,\n family: type \? `IPv${type}` : @undefined\n };\n return address;\n }\n return null;\n }\n getConnections(callback) {\n if (typeof callback === \"function\")\n callback(null, this.#server \? this[bunSocketServerConnections] : 0);\n return this;\n }\n listen(port, hostname, onListen) {\n let backlog, path, exclusive = !1;\n if (typeof port === \"string\") {\n if (Number.isSafeInteger(hostname)) {\n if (hostname > 0)\n backlog = hostname;\n } else if (typeof hostname === \"function\")\n onListen = hostname;\n path = port, hostname = @undefined, port = @undefined;\n } else {\n if (typeof hostname === \"function\")\n onListen = hostname, hostname = @undefined;\n if (typeof port === \"function\")\n onListen = port, port = 0;\n else if (typeof port === \"object\") {\n const options = port;\n options.signal\?.addEventListener(\"abort\", () => this.close()), hostname = options.host, exclusive = options.exclusive === !0;\n const path2 = options.path;\n if (port = options.port, !Number.isSafeInteger(port) || port < 0)\n if (path2)\n hostname = path2, port = @undefined;\n else {\n let message = 'The argument \\'options\\' must have the property \"port\" or \"path\"';\n try {\n message = `${message}. Received ${JSON.stringify(options)}`;\n } catch {\n }\n const error = @makeTypeError(message);\n throw error.code = \"ERR_INVALID_ARG_VALUE\", error;\n }\n else if (!Number.isSafeInteger(port) || port < 0)\n port = 0;\n if (typeof port.callback === \"function\")\n onListen = port\?.callback;\n } else if (!Number.isSafeInteger(port) || port < 0)\n port = 0;\n hostname = hostname || \"::\";\n }\n try {\n var tls = @undefined, TLSSocketClass = @undefined;\n const bunTLS = this[bunTlsSymbol], options = this[bunSocketServerOptions];\n if (typeof bunTLS === \"function\")\n [tls, TLSSocketClass] = bunTLS.call(this, port, hostname, !1), options.servername = tls.serverName, options.InternalSocketClass = TLSSocketClass;\n else\n options.InternalSocketClass = SocketClass;\n this.#server = Bun.listen(path \? {\n exclusive,\n unix: path,\n tls,\n socket: SocketClass[bunSocketServerHandlers]\n } : {\n exclusive,\n port,\n hostname,\n tls,\n socket: SocketClass[bunSocketServerHandlers]\n }), this.#server.data = this, this.#listening = !0, setTimeout(emitListeningNextTick, 1, this, onListen);\n } catch (err) {\n this.#listening = !1, setTimeout(emitErrorNextTick, 1, this, err);\n }\n return this;\n }\n}\n$ = {\n createServer,\n Server,\n createConnection,\n connect,\n isIP,\n isIPv4,\n isIPv6,\n Socket,\n [Symbol.for(\"::bunternal::\")]: SocketClass\n};\nreturn $})\n"_s; +static constexpr ASCIILiteral NodeNetCode = "(function (){\"use strict\";// src/js/out/tmp/node/net.ts\nvar isIPv4 = function(s) {\n return IPv4Reg.test(s);\n}, isIPv6 = function(s) {\n return IPv6Reg.test(s);\n}, isIP = function(s) {\n if (isIPv4(s))\n return 4;\n if (isIPv6(s))\n return 6;\n return 0;\n}, endNT = function(socket, callback, err) {\n socket.end(), callback(err);\n}, createConnection = function(port, host, connectListener) {\n if (typeof port === \"object\")\n return new Socket(port).connect(port, host, connectListener);\n return new Socket().connect(port, host, connectListener);\n}, emitErrorNextTick = function(self, error) {\n self.emit(\"error\", error);\n}, emitErrorAndCloseNextTick = function(self, error) {\n self.emit(\"error\", error), self.emit(\"close\");\n}, emitListeningNextTick = function(self, onListen) {\n if (typeof onListen === \"function\")\n try {\n onListen();\n } catch (err) {\n self.emit(\"error\", err);\n }\n self.emit(\"listening\");\n}, createServer = function(options, connectionListener) {\n return new Server(options, connectionListener);\n}, $, { Duplex } = @getInternalField(@internalModuleRegistry, 37) || @createInternalModuleById(37), EventEmitter = @getInternalField(@internalModuleRegistry, 18) || @createInternalModuleById(18);\nvar IPv4Reg = new @RegExp(\"^((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$\");\nvar IPv6Reg = new @RegExp(\"^((\?:(\?:[0-9a-fA-F]{1,4}):){7}(\?:(\?:[0-9a-fA-F]{1,4})|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){6}(\?:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|:(\?:[0-9a-fA-F]{1,4})|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){5}(\?::((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(\?:[0-9a-fA-F]{1,4})){1,2}|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){4}(\?:(:(\?:[0-9a-fA-F]{1,4})){0,1}:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(\?:[0-9a-fA-F]{1,4})){1,3}|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){3}(\?:(:(\?:[0-9a-fA-F]{1,4})){0,2}:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(\?:[0-9a-fA-F]{1,4})){1,4}|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){2}(\?:(:(\?:[0-9a-fA-F]{1,4})){0,3}:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(\?:[0-9a-fA-F]{1,4})){1,5}|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){1}(\?:(:(\?:[0-9a-fA-F]{1,4})){0,4}:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(\?:[0-9a-fA-F]{1,4})){1,6}|:)|(\?::((\?::(\?:[0-9a-fA-F]{1,4})){0,5}:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(\?::(\?:[0-9a-fA-F]{1,4})){1,7}|:)))(%[0-9a-zA-Z-.:]{1,})\?$\"), { connect: bunConnect } = Bun, { setTimeout } = globalThis, bunTlsSymbol = Symbol.for(\"::buntls::\"), bunSocketServerHandlers = Symbol.for(\"::bunsocket_serverhandlers::\"), bunSocketServerConnections = Symbol.for(\"::bunnetserverconnections::\"), bunSocketServerOptions = Symbol.for(\"::bunnetserveroptions::\"), bunSocketInternal = Symbol.for(\"::bunnetsocketinternal::\"), bunTLSConnectOptions = Symbol.for(\"::buntlsconnectoptions::\"), SocketClass, Socket = function(InternalSocket) {\n return SocketClass = InternalSocket, Object.defineProperty(SocketClass.prototype, Symbol.toStringTag, {\n value: \"Socket\",\n enumerable: !1\n }), Object.defineProperty(function Socket(options) {\n return new InternalSocket(options);\n }, Symbol.hasInstance, {\n value(instance) {\n return instance instanceof InternalSocket;\n }\n });\n}(class Socket2 extends Duplex {\n static #Handlers = {\n close: Socket2.#Close,\n data({ data: self }, buffer) {\n self.bytesRead += buffer.length;\n const queue = self.#readQueue;\n if (queue.isEmpty()) {\n if (self.push(buffer))\n return;\n }\n queue.push(buffer);\n },\n drain: Socket2.#Drain,\n end: Socket2.#Close,\n error(socket, error) {\n const self = socket.data, callback = self.#writeCallback;\n if (callback)\n self.#writeCallback = null, callback(error);\n self.emit(\"error\", error);\n },\n open(socket) {\n const self = socket.data;\n socket.timeout(self.timeout), socket.ref(), self[bunSocketInternal] = socket, self.connecting = !1;\n const options = self[bunTLSConnectOptions];\n if (options) {\n const { session } = options;\n if (session)\n self.setSession(session);\n }\n if (!self.#upgraded)\n self.emit(\"connect\", self);\n Socket2.#Drain(socket);\n },\n handshake(socket, success, verifyError) {\n const { data: self } = socket;\n self._securePending = !1, self.secureConnecting = !1, self._secureEstablished = !!success, self.emit(\"secure\", self);\n const { checkServerIdentity } = self[bunTLSConnectOptions];\n if (!verifyError && typeof checkServerIdentity === \"function\" && self.servername) {\n const cert = self.getPeerCertificate(!0);\n verifyError = checkServerIdentity(self.servername, cert);\n }\n if (self._requestCert || self._rejectUnauthorized) {\n if (verifyError) {\n if (self.authorized = !1, self.authorizationError = verifyError.code || verifyError.message, self._rejectUnauthorized) {\n self.destroy(verifyError);\n return;\n }\n }\n } else\n self.authorized = !0;\n self.emit(\"secureConnect\", verifyError);\n },\n timeout(socket) {\n const self = socket.data;\n self.emit(\"timeout\", self);\n },\n binaryType: \"buffer\"\n };\n static #Close(socket) {\n const self = socket.data;\n if (self.#closed)\n return;\n self.#closed = !0, self[bunSocketInternal] = null;\n const queue = self.#readQueue;\n if (queue.isEmpty()) {\n if (self.push(null))\n return;\n }\n queue.push(null);\n }\n static #Drain(socket) {\n const self = socket.data, callback = self.#writeCallback;\n if (callback) {\n const chunk = self.#writeChunk, written = socket.write(chunk);\n if (self.bytesWritten += written, written < chunk.length)\n self.#writeChunk = chunk.slice(written);\n else\n self.#writeCallback = null, self.#writeChunk = null, callback(null);\n }\n }\n static [bunSocketServerHandlers] = {\n data: Socket2.#Handlers.data,\n close(socket) {\n Socket2.#Handlers.close(socket), this.data[bunSocketServerConnections]--;\n },\n end(socket) {\n Socket2.#Handlers.end(socket), this.data[bunSocketServerConnections]--;\n },\n open(socket) {\n const self = this.data, options = self[bunSocketServerOptions], { pauseOnConnect, connectionListener, InternalSocketClass, requestCert, rejectUnauthorized } = options, _socket = new InternalSocketClass({});\n if (_socket.isServer = !0, _socket._requestCert = requestCert, _socket._rejectUnauthorized = rejectUnauthorized, _socket.#attach(this.localPort, socket), self.maxConnections && self[bunSocketServerConnections] >= self.maxConnections) {\n const data = {\n localAddress: _socket.localAddress,\n localPort: _socket.localPort,\n localFamily: _socket.localFamily,\n remoteAddress: _socket.remoteAddress,\n remotePort: _socket.remotePort,\n remoteFamily: _socket.remoteFamily || \"IPv4\"\n };\n socket.end(), self.emit(\"drop\", data);\n return;\n }\n if (!pauseOnConnect)\n _socket.resume();\n if (self[bunSocketServerConnections]++, typeof connectionListener == \"function\")\n if (InternalSocketClass.name === \"TLSSocket\")\n self.once(\"secureConnection\", () => connectionListener(_socket));\n else\n connectionListener(_socket);\n self.emit(\"connection\", _socket);\n },\n handshake(socket, success, verifyError) {\n const { data: self } = socket;\n if (self.emit(\"secure\", self), self._securePending = !1, self.secureConnecting = !1, self._secureEstablished = !!success, self._requestCert || self._rejectUnauthorized) {\n if (verifyError) {\n if (self.authorized = !1, self.authorizationError = verifyError.code || verifyError.message, self._rejectUnauthorized) {\n self.destroy(verifyError);\n return;\n }\n }\n } else\n self.authorized = !0;\n self.emit(\"secureConnection\", verifyError);\n },\n error(socket, error) {\n Socket2.#Handlers.error(socket, error), this.data.emit(\"error\", error);\n },\n timeout: Socket2.#Handlers.timeout,\n connectError: Socket2.#Handlers.connectError,\n drain: Socket2.#Handlers.drain,\n binaryType: \"buffer\"\n };\n bytesRead = 0;\n bytesWritten = 0;\n #closed = !1;\n connecting = !1;\n localAddress = \"127.0.0.1\";\n #readQueue = @createFIFO();\n remotePort;\n [bunSocketInternal] = null;\n [bunTLSConnectOptions] = null;\n timeout = 0;\n #writeCallback;\n #writeChunk;\n #pendingRead;\n isServer = !1;\n _handle;\n _parent;\n _parentWrap;\n #socket;\n #upgraded;\n constructor(options) {\n const { socket, signal, write, read, allowHalfOpen = !1, ...opts } = options || {};\n super({\n ...opts,\n allowHalfOpen,\n readable: !0,\n writable: !0\n });\n if (this._handle = this, this._parent = this, this._parentWrap = this, this.#pendingRead = @undefined, this.#upgraded = !1, socket instanceof Socket2)\n this.#socket = socket;\n signal\?.once(\"abort\", () => this.destroy()), this.once(\"connect\", () => this.emit(\"ready\"));\n }\n address() {\n return {\n address: this.localAddress,\n family: this.localFamily,\n port: this.localPort\n };\n }\n get bufferSize() {\n return this.writableLength;\n }\n #attach(port, socket) {\n if (this.remotePort = port, socket.data = this, socket.timeout(this.timeout), socket.ref(), this[bunSocketInternal] = socket, this.connecting = !1, !this.#upgraded)\n this.emit(\"connect\", this);\n Socket2.#Drain(socket);\n }\n connect(port, host, connectListener) {\n var path, connection = this.#socket, _checkServerIdentity = @undefined;\n if (typeof port === \"string\") {\n if (path = port, port = @undefined, typeof host === \"function\")\n connectListener = host, host = @undefined;\n } else if (typeof host == \"function\") {\n if (typeof port === \"string\")\n path = port, port = @undefined;\n connectListener = host, host = @undefined;\n }\n if (typeof port == \"object\") {\n var {\n port,\n host,\n path,\n socket,\n localAddress,\n localPort,\n family,\n hints,\n lookup,\n noDelay,\n keepAlive,\n keepAliveInitialDelay,\n requestCert,\n rejectUnauthorized,\n pauseOnConnect,\n servername,\n checkServerIdentity,\n session\n } = port;\n if (_checkServerIdentity = checkServerIdentity, this.servername = servername, socket)\n connection = socket;\n }\n if (!pauseOnConnect)\n this.resume();\n this.connecting = !0, this.remotePort = port;\n const bunTLS = this[bunTlsSymbol];\n var tls = @undefined;\n if (typeof bunTLS === \"function\") {\n if (tls = bunTLS.call(this, port, host, !0), this._requestCert = !0, this._rejectUnauthorized = rejectUnauthorized, tls) {\n if (tls.rejectUnauthorized = rejectUnauthorized, tls.requestCert = !0, tls.session = session || tls.session, this.servername = tls.servername, tls.checkServerIdentity = _checkServerIdentity || tls.checkServerIdentity, this[bunTLSConnectOptions] = tls, !connection && tls.socket)\n connection = tls.socket;\n }\n if (connection) {\n if (typeof connection !== \"object\" || !(connection instanceof Socket2) || typeof connection[bunTlsSymbol] === \"function\")\n @throwTypeError(\"socket must be an instance of net.Socket\");\n }\n if (this.authorized = !1, this.secureConnecting = !0, this._secureEstablished = !1, this._securePending = !0, connectListener)\n this.on(\"secureConnect\", connectListener);\n } else if (connectListener)\n this.on(\"connect\", connectListener);\n try {\n if (connection) {\n const socket2 = connection[bunSocketInternal];\n if (socket2) {\n this.connecting = !0, this.#upgraded = !0;\n const result = socket2.upgradeTLS({\n data: this,\n tls,\n socket: Socket2.#Handlers\n });\n if (result) {\n const [raw, tls2] = result;\n connection[bunSocketInternal] = raw, raw.timeout(raw.timeout), raw.connecting = !1, this[bunSocketInternal] = tls2;\n } else\n throw this[bunSocketInternal] = null, new Error(\"Invalid socket\");\n } else\n connection.once(\"connect\", () => {\n const socket3 = connection[bunSocketInternal];\n if (!socket3)\n return;\n this.connecting = !0, this.#upgraded = !0;\n const result = socket3.upgradeTLS({\n data: this,\n tls,\n socket: Socket2.#Handlers\n });\n if (result) {\n const [raw, tls2] = result;\n connection[bunSocketInternal] = raw, raw.timeout(raw.timeout), raw.connecting = !1, this[bunSocketInternal] = tls2;\n } else\n throw this[bunSocketInternal] = null, new Error(\"Invalid socket\");\n });\n } else if (path)\n bunConnect({\n data: this,\n unix: path,\n socket: Socket2.#Handlers,\n tls\n }).catch((error) => {\n this.emit(\"error\", error), this.emit(\"close\");\n });\n else\n bunConnect({\n data: this,\n hostname: host || \"localhost\",\n port,\n socket: Socket2.#Handlers,\n tls\n }).catch((error) => {\n this.emit(\"error\", error), this.emit(\"close\");\n });\n } catch (error) {\n process.nextTick(emitErrorAndCloseNextTick, this, error);\n }\n return this;\n }\n _destroy(err, callback) {\n const socket = this[bunSocketInternal];\n socket && process.nextTick(endNT, socket, callback, err);\n }\n _final(callback) {\n this[bunSocketInternal]\?.end(), callback();\n }\n get localAddress() {\n return \"127.0.0.1\";\n }\n get localFamily() {\n return \"IPv4\";\n }\n get localPort() {\n return this[bunSocketInternal]\?.localPort;\n }\n get pending() {\n return this.connecting;\n }\n _read(size) {\n const queue = this.#readQueue;\n let chunk;\n while (chunk = queue.peek()) {\n if (!this.push(chunk))\n return;\n queue.shift();\n }\n }\n get readyState() {\n if (this.connecting)\n return \"opening\";\n if (this.readable)\n return this.writable \? \"open\" : \"readOnly\";\n else\n return this.writable \? \"writeOnly\" : \"closed\";\n }\n ref() {\n this[bunSocketInternal]\?.ref();\n }\n get remoteAddress() {\n return this[bunSocketInternal]\?.remoteAddress;\n }\n get remoteFamily() {\n return \"IPv4\";\n }\n resetAndDestroy() {\n this[bunSocketInternal]\?.end();\n }\n setKeepAlive(enable = !1, initialDelay = 0) {\n return this;\n }\n setNoDelay(noDelay = !0) {\n return this;\n }\n setTimeout(timeout, callback) {\n if (this[bunSocketInternal]\?.timeout(timeout), this.timeout = timeout, callback)\n this.once(\"timeout\", callback);\n return this;\n }\n unref() {\n this[bunSocketInternal]\?.unref();\n }\n _write(chunk, encoding, callback) {\n if (typeof chunk == \"string\" && encoding !== \"ascii\")\n chunk = @Buffer.from(chunk, encoding);\n var written = this[bunSocketInternal]\?.write(chunk);\n if (written == chunk.length)\n callback();\n else if (this.#writeCallback)\n callback(new Error(\"overlapping _write()\"));\n else {\n if (written > 0)\n if (typeof chunk == \"string\")\n chunk = chunk.slice(written);\n else\n chunk = chunk.subarray(written);\n this.#writeCallback = callback, this.#writeChunk = chunk;\n }\n }\n}), connect = createConnection;\n\nclass Server extends EventEmitter {\n #server;\n #listening = !1;\n [bunSocketServerConnections] = 0;\n [bunSocketServerOptions];\n maxConnections = 0;\n constructor(options, connectionListener) {\n super();\n if (typeof options === \"function\")\n connectionListener = options, options = {};\n else if (options == null || typeof options === \"object\")\n options = { ...options };\n else\n throw new Error(\"bun-net-polyfill: invalid arguments\");\n const { maxConnections } = options;\n this.maxConnections = Number.isSafeInteger(maxConnections) && maxConnections > 0 \? maxConnections : 0, options.connectionListener = connectionListener, this[bunSocketServerOptions] = options;\n }\n ref() {\n return this.#server\?.ref(), this;\n }\n unref() {\n return this.#server\?.unref(), this;\n }\n close(callback) {\n if (this.#server) {\n if (this.#server.stop(!0), this.#server = null, this.#listening = !1, this[bunSocketServerConnections] = 0, this.emit(\"close\"), typeof callback === \"function\")\n callback();\n return this;\n }\n if (typeof callback === \"function\") {\n const error = new Error(\"Server is not running\");\n error.code = \"ERR_SERVER_NOT_RUNNING\", callback(error);\n }\n return this;\n }\n address() {\n const server = this.#server;\n if (server) {\n const unix = server.unix;\n if (unix)\n return unix;\n let address = server.hostname;\n const type = isIP(address), port = server.port;\n if (typeof port === \"number\")\n return {\n port,\n address,\n family: type \? `IPv${type}` : @undefined\n };\n if (type)\n return {\n address,\n family: type \? `IPv${type}` : @undefined\n };\n return address;\n }\n return null;\n }\n getConnections(callback) {\n if (typeof callback === \"function\")\n callback(null, this.#server \? this[bunSocketServerConnections] : 0);\n return this;\n }\n listen(port, hostname, onListen) {\n let backlog, path, exclusive = !1;\n if (typeof port === \"string\") {\n if (Number.isSafeInteger(hostname)) {\n if (hostname > 0)\n backlog = hostname;\n } else if (typeof hostname === \"function\")\n onListen = hostname;\n path = port, hostname = @undefined, port = @undefined;\n } else {\n if (typeof hostname === \"function\")\n onListen = hostname, hostname = @undefined;\n if (typeof port === \"function\")\n onListen = port, port = 0;\n else if (typeof port === \"object\") {\n const options = port;\n options.signal\?.addEventListener(\"abort\", () => this.close()), hostname = options.host, exclusive = options.exclusive === !0;\n const path2 = options.path;\n if (port = options.port, !Number.isSafeInteger(port) || port < 0)\n if (path2)\n hostname = path2, port = @undefined;\n else {\n let message = 'The argument \\'options\\' must have the property \"port\" or \"path\"';\n try {\n message = `${message}. Received ${JSON.stringify(options)}`;\n } catch {\n }\n const error = @makeTypeError(message);\n throw error.code = \"ERR_INVALID_ARG_VALUE\", error;\n }\n else if (!Number.isSafeInteger(port) || port < 0)\n port = 0;\n if (typeof port.callback === \"function\")\n onListen = port\?.callback;\n } else if (!Number.isSafeInteger(port) || port < 0)\n port = 0;\n hostname = hostname || \"::\";\n }\n try {\n var tls = @undefined, TLSSocketClass = @undefined;\n const bunTLS = this[bunTlsSymbol], options = this[bunSocketServerOptions];\n if (typeof bunTLS === \"function\")\n [tls, TLSSocketClass] = bunTLS.call(this, port, hostname, !1), options.servername = tls.serverName, options.InternalSocketClass = TLSSocketClass;\n else\n options.InternalSocketClass = SocketClass;\n this.#server = Bun.listen(path \? {\n exclusive,\n unix: path,\n tls,\n socket: SocketClass[bunSocketServerHandlers]\n } : {\n exclusive,\n port,\n hostname,\n tls,\n socket: SocketClass[bunSocketServerHandlers]\n }), this.#server.data = this, this.#listening = !0, setTimeout(emitListeningNextTick, 1, this, onListen);\n } catch (err) {\n this.#listening = !1, setTimeout(emitErrorNextTick, 1, this, err);\n }\n return this;\n }\n}\n$ = {\n createServer,\n Server,\n createConnection,\n connect,\n isIP,\n isIPv4,\n isIPv6,\n Socket,\n [Symbol.for(\"::bunternal::\")]: SocketClass\n};\nreturn $})\n"_s; // // @@ -589,7 +589,7 @@ static constexpr ASCIILiteral NodeInspectorCode = "(function (){\"use strict\";/ // // -static constexpr ASCIILiteral NodeNetCode = "(function (){\"use strict\";// src/js/out/tmp/node/net.ts\nvar isIPv4 = function(s) {\n return IPv4Reg.test(s);\n}, isIPv6 = function(s) {\n return IPv6Reg.test(s);\n}, isIP = function(s) {\n if (isIPv4(s))\n return 4;\n if (isIPv6(s))\n return 6;\n return 0;\n}, createConnection = function(port, host, connectListener) {\n if (typeof port === \"object\")\n return new Socket(port).connect(port, host, connectListener);\n return new Socket().connect(port, host, connectListener);\n}, emitErrorNextTick = function(self, error) {\n self.emit(\"error\", error);\n}, emitListeningNextTick = function(self, onListen) {\n if (typeof onListen === \"function\")\n try {\n onListen();\n } catch (err) {\n self.emit(\"error\", err);\n }\n self.emit(\"listening\");\n}, createServer = function(options, connectionListener) {\n return new Server(options, connectionListener);\n}, $, { Duplex } = @getInternalField(@internalModuleRegistry, 37) || @createInternalModuleById(37), EventEmitter = @getInternalField(@internalModuleRegistry, 18) || @createInternalModuleById(18);\nvar IPv4Reg = new @RegExp(\"^((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$\");\nvar IPv6Reg = new @RegExp(\"^((\?:(\?:[0-9a-fA-F]{1,4}):){7}(\?:(\?:[0-9a-fA-F]{1,4})|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){6}(\?:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|:(\?:[0-9a-fA-F]{1,4})|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){5}(\?::((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(\?:[0-9a-fA-F]{1,4})){1,2}|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){4}(\?:(:(\?:[0-9a-fA-F]{1,4})){0,1}:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(\?:[0-9a-fA-F]{1,4})){1,3}|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){3}(\?:(:(\?:[0-9a-fA-F]{1,4})){0,2}:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(\?:[0-9a-fA-F]{1,4})){1,4}|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){2}(\?:(:(\?:[0-9a-fA-F]{1,4})){0,3}:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(\?:[0-9a-fA-F]{1,4})){1,5}|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){1}(\?:(:(\?:[0-9a-fA-F]{1,4})){0,4}:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(\?:[0-9a-fA-F]{1,4})){1,6}|:)|(\?::((\?::(\?:[0-9a-fA-F]{1,4})){0,5}:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(\?::(\?:[0-9a-fA-F]{1,4})){1,7}|:)))(%[0-9a-zA-Z-.:]{1,})\?$\"), { connect: bunConnect } = Bun, { setTimeout } = globalThis, bunTlsSymbol = Symbol.for(\"::buntls::\"), bunSocketServerHandlers = Symbol.for(\"::bunsocket_serverhandlers::\"), bunSocketServerConnections = Symbol.for(\"::bunnetserverconnections::\"), bunSocketServerOptions = Symbol.for(\"::bunnetserveroptions::\"), bunSocketInternal = Symbol.for(\"::bunnetsocketinternal::\"), bunTLSConnectOptions = Symbol.for(\"::buntlsconnectoptions::\"), SocketClass, Socket = function(InternalSocket) {\n return SocketClass = InternalSocket, Object.defineProperty(SocketClass.prototype, Symbol.toStringTag, {\n value: \"Socket\",\n enumerable: !1\n }), Object.defineProperty(function Socket(options) {\n return new InternalSocket(options);\n }, Symbol.hasInstance, {\n value(instance) {\n return instance instanceof InternalSocket;\n }\n });\n}(class Socket2 extends Duplex {\n static #Handlers = {\n close: Socket2.#Close,\n data({ data: self }, buffer) {\n self.bytesRead += buffer.length;\n const queue = self.#readQueue;\n if (queue.isEmpty()) {\n if (self.push(buffer))\n return;\n }\n queue.push(buffer);\n },\n drain: Socket2.#Drain,\n end: Socket2.#Close,\n error(socket, error) {\n const self = socket.data, callback = self.#writeCallback;\n if (callback)\n self.#writeCallback = null, callback(error);\n self.emit(\"error\", error);\n },\n open(socket) {\n const self = socket.data;\n socket.timeout(self.timeout), socket.ref(), self[bunSocketInternal] = socket, self.connecting = !1;\n const options = self[bunTLSConnectOptions];\n if (options) {\n const { session } = options;\n if (session)\n self.setSession(session);\n }\n if (!self.#upgraded)\n self.emit(\"connect\", self);\n Socket2.#Drain(socket);\n },\n handshake(socket, success, verifyError) {\n const { data: self } = socket;\n self._securePending = !1, self.secureConnecting = !1, self._secureEstablished = !!success, self.emit(\"secure\", self);\n const { checkServerIdentity } = self[bunTLSConnectOptions];\n if (!verifyError && typeof checkServerIdentity === \"function\" && self.servername) {\n const cert = self.getPeerCertificate(!0);\n verifyError = checkServerIdentity(self.servername, cert);\n }\n if (self._requestCert || self._rejectUnauthorized) {\n if (verifyError) {\n if (self.authorized = !1, self.authorizationError = verifyError.code || verifyError.message, self._rejectUnauthorized) {\n self.destroy(verifyError);\n return;\n }\n }\n } else\n self.authorized = !0;\n self.emit(\"secureConnect\", verifyError);\n },\n timeout(socket) {\n const self = socket.data;\n self.emit(\"timeout\", self);\n },\n binaryType: \"buffer\"\n };\n static #Close(socket) {\n const self = socket.data;\n if (self.#closed)\n return;\n self.#closed = !0, self[bunSocketInternal] = null;\n const queue = self.#readQueue;\n if (queue.isEmpty()) {\n if (self.push(null))\n return;\n }\n queue.push(null);\n }\n static #Drain(socket) {\n const self = socket.data, callback = self.#writeCallback;\n if (callback) {\n const chunk = self.#writeChunk, written = socket.write(chunk);\n if (self.bytesWritten += written, written < chunk.length)\n self.#writeChunk = chunk.slice(written);\n else\n self.#writeCallback = null, self.#writeChunk = null, callback(null);\n }\n }\n static [bunSocketServerHandlers] = {\n data: Socket2.#Handlers.data,\n close(socket) {\n Socket2.#Handlers.close(socket), this.data[bunSocketServerConnections]--;\n },\n end(socket) {\n Socket2.#Handlers.end(socket), this.data[bunSocketServerConnections]--;\n },\n open(socket) {\n const self = this.data, options = self[bunSocketServerOptions], { pauseOnConnect, connectionListener, InternalSocketClass, requestCert, rejectUnauthorized } = options, _socket = new InternalSocketClass({});\n if (_socket.isServer = !0, _socket._requestCert = requestCert, _socket._rejectUnauthorized = rejectUnauthorized, _socket.#attach(this.localPort, socket), self.maxConnections && self[bunSocketServerConnections] >= self.maxConnections) {\n const data = {\n localAddress: _socket.localAddress,\n localPort: _socket.localPort,\n localFamily: _socket.localFamily,\n remoteAddress: _socket.remoteAddress,\n remotePort: _socket.remotePort,\n remoteFamily: _socket.remoteFamily || \"IPv4\"\n };\n socket.end(), self.emit(\"drop\", data);\n return;\n }\n if (!pauseOnConnect)\n _socket.resume();\n if (self[bunSocketServerConnections]++, typeof connectionListener == \"function\")\n if (InternalSocketClass.name === \"TLSSocket\")\n self.once(\"secureConnection\", () => connectionListener(_socket));\n else\n connectionListener(_socket);\n self.emit(\"connection\", _socket);\n },\n handshake(socket, success, verifyError) {\n const { data: self } = socket;\n if (self.emit(\"secure\", self), self._securePending = !1, self.secureConnecting = !1, self._secureEstablished = !!success, self._requestCert || self._rejectUnauthorized) {\n if (verifyError) {\n if (self.authorized = !1, self.authorizationError = verifyError.code || verifyError.message, self._rejectUnauthorized) {\n self.destroy(verifyError);\n return;\n }\n }\n } else\n self.authorized = !0;\n self.emit(\"secureConnection\", verifyError);\n },\n error(socket, error) {\n Socket2.#Handlers.error(socket, error), this.data.emit(\"error\", error);\n },\n timeout: Socket2.#Handlers.timeout,\n connectError: Socket2.#Handlers.connectError,\n drain: Socket2.#Handlers.drain,\n binaryType: \"buffer\"\n };\n bytesRead = 0;\n bytesWritten = 0;\n #closed = !1;\n connecting = !1;\n localAddress = \"127.0.0.1\";\n #readQueue = @createFIFO();\n remotePort;\n [bunSocketInternal] = null;\n [bunTLSConnectOptions] = null;\n timeout = 0;\n #writeCallback;\n #writeChunk;\n #pendingRead;\n isServer = !1;\n _handle;\n _parent;\n _parentWrap;\n #socket;\n #upgraded;\n constructor(options) {\n const { socket, signal, write, read, allowHalfOpen = !1, ...opts } = options || {};\n super({\n ...opts,\n allowHalfOpen,\n readable: !0,\n writable: !0\n });\n if (this._handle = this, this._parent = this, this._parentWrap = this, this.#pendingRead = @undefined, this.#upgraded = !1, socket instanceof Socket2)\n this.#socket = socket;\n signal\?.once(\"abort\", () => this.destroy()), this.once(\"connect\", () => this.emit(\"ready\"));\n }\n address() {\n return {\n address: this.localAddress,\n family: this.localFamily,\n port: this.localPort\n };\n }\n get bufferSize() {\n return this.writableLength;\n }\n #attach(port, socket) {\n if (this.remotePort = port, socket.data = this, socket.timeout(this.timeout), socket.ref(), this[bunSocketInternal] = socket, this.connecting = !1, !this.#upgraded)\n this.emit(\"connect\", this);\n Socket2.#Drain(socket);\n }\n connect(port, host, connectListener) {\n var path, connection = this.#socket, _checkServerIdentity = @undefined;\n if (typeof port === \"string\") {\n if (path = port, port = @undefined, typeof host === \"function\")\n connectListener = host, host = @undefined;\n } else if (typeof host == \"function\") {\n if (typeof port === \"string\")\n path = port, port = @undefined;\n connectListener = host, host = @undefined;\n }\n if (typeof port == \"object\") {\n var {\n port,\n host,\n path,\n socket,\n localAddress,\n localPort,\n family,\n hints,\n lookup,\n noDelay,\n keepAlive,\n keepAliveInitialDelay,\n requestCert,\n rejectUnauthorized,\n pauseOnConnect,\n servername,\n checkServerIdentity,\n session\n } = port;\n if (_checkServerIdentity = checkServerIdentity, this.servername = servername, socket)\n connection = socket;\n }\n if (!pauseOnConnect)\n this.resume();\n this.connecting = !0, this.remotePort = port;\n const bunTLS = this[bunTlsSymbol];\n var tls = @undefined;\n if (typeof bunTLS === \"function\") {\n if (tls = bunTLS.call(this, port, host, !0), this._requestCert = !0, this._rejectUnauthorized = rejectUnauthorized, tls) {\n if (tls.rejectUnauthorized = rejectUnauthorized, tls.requestCert = !0, tls.session = session || tls.session, this.servername = tls.servername, tls.checkServerIdentity = _checkServerIdentity || tls.checkServerIdentity, this[bunTLSConnectOptions] = tls, !connection && tls.socket)\n connection = tls.socket;\n }\n if (connection) {\n if (typeof connection !== \"object\" || !(connection instanceof Socket2) || typeof connection[bunTlsSymbol] === \"function\")\n @throwTypeError(\"socket must be an instance of net.Socket\");\n }\n if (this.authorized = !1, this.secureConnecting = !0, this._secureEstablished = !1, this._securePending = !0, connectListener)\n this.on(\"secureConnect\", connectListener);\n } else if (connectListener)\n this.on(\"connect\", connectListener);\n if (connection) {\n const socket2 = connection[bunSocketInternal];\n if (socket2) {\n this.connecting = !0, this.#upgraded = !0;\n const result = socket2.upgradeTLS({\n data: this,\n tls,\n socket: Socket2.#Handlers\n });\n if (result) {\n const [raw, tls2] = result;\n connection[bunSocketInternal] = raw, raw.timeout(raw.timeout), raw.connecting = !1, this[bunSocketInternal] = tls2;\n } else\n throw this[bunSocketInternal] = null, new Error(\"Invalid socket\");\n } else\n connection.once(\"connect\", () => {\n const socket3 = connection[bunSocketInternal];\n if (!socket3)\n return;\n this.connecting = !0, this.#upgraded = !0;\n const result = socket3.upgradeTLS({\n data: this,\n tls,\n socket: Socket2.#Handlers\n });\n if (result) {\n const [raw, tls2] = result;\n connection[bunSocketInternal] = raw, raw.timeout(raw.timeout), raw.connecting = !1, this[bunSocketInternal] = tls2;\n } else\n throw this[bunSocketInternal] = null, new Error(\"Invalid socket\");\n });\n } else if (path)\n bunConnect({\n data: this,\n unix: path,\n socket: Socket2.#Handlers,\n tls\n }).catch((error) => {\n this.emit(\"error\", error), this.emit(\"close\");\n });\n else\n bunConnect({\n data: this,\n hostname: host || \"localhost\",\n port,\n socket: Socket2.#Handlers,\n tls\n }).catch((error) => {\n this.emit(\"error\", error), this.emit(\"close\");\n });\n return this;\n }\n _destroy(err, callback) {\n this[bunSocketInternal]\?.end(), callback(err);\n }\n _final(callback) {\n this[bunSocketInternal]\?.end(), callback();\n }\n get localAddress() {\n return \"127.0.0.1\";\n }\n get localFamily() {\n return \"IPv4\";\n }\n get localPort() {\n return this[bunSocketInternal]\?.localPort;\n }\n get pending() {\n return this.connecting;\n }\n _read(size) {\n const queue = this.#readQueue;\n let chunk;\n while (chunk = queue.peek()) {\n if (!this.push(chunk))\n return;\n queue.shift();\n }\n }\n get readyState() {\n if (this.connecting)\n return \"opening\";\n if (this.readable)\n return this.writable \? \"open\" : \"readOnly\";\n else\n return this.writable \? \"writeOnly\" : \"closed\";\n }\n ref() {\n this[bunSocketInternal]\?.ref();\n }\n get remoteAddress() {\n return this[bunSocketInternal]\?.remoteAddress;\n }\n get remoteFamily() {\n return \"IPv4\";\n }\n resetAndDestroy() {\n this[bunSocketInternal]\?.end();\n }\n setKeepAlive(enable = !1, initialDelay = 0) {\n return this;\n }\n setNoDelay(noDelay = !0) {\n return this;\n }\n setTimeout(timeout, callback) {\n if (this[bunSocketInternal]\?.timeout(timeout), this.timeout = timeout, callback)\n this.once(\"timeout\", callback);\n return this;\n }\n unref() {\n this[bunSocketInternal]\?.unref();\n }\n _write(chunk, encoding, callback) {\n if (typeof chunk == \"string\" && encoding !== \"ascii\")\n chunk = @Buffer.from(chunk, encoding);\n var written = this[bunSocketInternal]\?.write(chunk);\n if (written == chunk.length)\n callback();\n else if (this.#writeCallback)\n callback(new Error(\"overlapping _write()\"));\n else {\n if (written > 0)\n if (typeof chunk == \"string\")\n chunk = chunk.slice(written);\n else\n chunk = chunk.subarray(written);\n this.#writeCallback = callback, this.#writeChunk = chunk;\n }\n }\n}), connect = createConnection;\n\nclass Server extends EventEmitter {\n #server;\n #listening = !1;\n [bunSocketServerConnections] = 0;\n [bunSocketServerOptions];\n maxConnections = 0;\n constructor(options, connectionListener) {\n super();\n if (typeof options === \"function\")\n connectionListener = options, options = {};\n else if (options == null || typeof options === \"object\")\n options = { ...options };\n else\n throw new Error(\"bun-net-polyfill: invalid arguments\");\n const { maxConnections } = options;\n this.maxConnections = Number.isSafeInteger(maxConnections) && maxConnections > 0 \? maxConnections : 0, options.connectionListener = connectionListener, this[bunSocketServerOptions] = options;\n }\n ref() {\n return this.#server\?.ref(), this;\n }\n unref() {\n return this.#server\?.unref(), this;\n }\n close(callback) {\n if (this.#server) {\n if (this.#server.stop(!0), this.#server = null, this.#listening = !1, this[bunSocketServerConnections] = 0, this.emit(\"close\"), typeof callback === \"function\")\n callback();\n return this;\n }\n if (typeof callback === \"function\") {\n const error = new Error(\"Server is not running\");\n error.code = \"ERR_SERVER_NOT_RUNNING\", callback(error);\n }\n return this;\n }\n address() {\n const server = this.#server;\n if (server) {\n const unix = server.unix;\n if (unix)\n return unix;\n let address = server.hostname;\n const type = isIP(address), port = server.port;\n if (typeof port === \"number\")\n return {\n port,\n address,\n family: type \? `IPv${type}` : @undefined\n };\n if (type)\n return {\n address,\n family: type \? `IPv${type}` : @undefined\n };\n return address;\n }\n return null;\n }\n getConnections(callback) {\n if (typeof callback === \"function\")\n callback(null, this.#server \? this[bunSocketServerConnections] : 0);\n return this;\n }\n listen(port, hostname, onListen) {\n let backlog, path, exclusive = !1;\n if (typeof port === \"string\") {\n if (Number.isSafeInteger(hostname)) {\n if (hostname > 0)\n backlog = hostname;\n } else if (typeof hostname === \"function\")\n onListen = hostname;\n path = port, hostname = @undefined, port = @undefined;\n } else {\n if (typeof hostname === \"function\")\n onListen = hostname, hostname = @undefined;\n if (typeof port === \"function\")\n onListen = port, port = 0;\n else if (typeof port === \"object\") {\n const options = port;\n options.signal\?.addEventListener(\"abort\", () => this.close()), hostname = options.host, exclusive = options.exclusive === !0;\n const path2 = options.path;\n if (port = options.port, !Number.isSafeInteger(port) || port < 0)\n if (path2)\n hostname = path2, port = @undefined;\n else {\n let message = 'The argument \\'options\\' must have the property \"port\" or \"path\"';\n try {\n message = `${message}. Received ${JSON.stringify(options)}`;\n } catch {\n }\n const error = @makeTypeError(message);\n throw error.code = \"ERR_INVALID_ARG_VALUE\", error;\n }\n else if (!Number.isSafeInteger(port) || port < 0)\n port = 0;\n if (typeof port.callback === \"function\")\n onListen = port\?.callback;\n } else if (!Number.isSafeInteger(port) || port < 0)\n port = 0;\n hostname = hostname || \"::\";\n }\n try {\n var tls = @undefined, TLSSocketClass = @undefined;\n const bunTLS = this[bunTlsSymbol], options = this[bunSocketServerOptions];\n if (typeof bunTLS === \"function\")\n [tls, TLSSocketClass] = bunTLS.call(this, port, hostname, !1), options.servername = tls.serverName, options.InternalSocketClass = TLSSocketClass;\n else\n options.InternalSocketClass = SocketClass;\n this.#server = Bun.listen(path \? {\n exclusive,\n unix: path,\n tls,\n socket: SocketClass[bunSocketServerHandlers]\n } : {\n exclusive,\n port,\n hostname,\n tls,\n socket: SocketClass[bunSocketServerHandlers]\n }), this.#server.data = this, this.#listening = !0, setTimeout(emitListeningNextTick, 1, this, onListen);\n } catch (err) {\n this.#listening = !1, setTimeout(emitErrorNextTick, 1, this, err);\n }\n return this;\n }\n}\n$ = {\n createServer,\n Server,\n createConnection,\n connect,\n isIP,\n isIPv4,\n isIPv6,\n Socket,\n [Symbol.for(\"::bunternal::\")]: SocketClass\n};\nreturn $})\n"_s; +static constexpr ASCIILiteral NodeNetCode = "(function (){\"use strict\";// src/js/out/tmp/node/net.ts\nvar isIPv4 = function(s) {\n return IPv4Reg.test(s);\n}, isIPv6 = function(s) {\n return IPv6Reg.test(s);\n}, isIP = function(s) {\n if (isIPv4(s))\n return 4;\n if (isIPv6(s))\n return 6;\n return 0;\n}, endNT = function(socket, callback, err) {\n socket.end(), callback(err);\n}, createConnection = function(port, host, connectListener) {\n if (typeof port === \"object\")\n return new Socket(port).connect(port, host, connectListener);\n return new Socket().connect(port, host, connectListener);\n}, emitErrorNextTick = function(self, error) {\n self.emit(\"error\", error);\n}, emitErrorAndCloseNextTick = function(self, error) {\n self.emit(\"error\", error), self.emit(\"close\");\n}, emitListeningNextTick = function(self, onListen) {\n if (typeof onListen === \"function\")\n try {\n onListen();\n } catch (err) {\n self.emit(\"error\", err);\n }\n self.emit(\"listening\");\n}, createServer = function(options, connectionListener) {\n return new Server(options, connectionListener);\n}, $, { Duplex } = @getInternalField(@internalModuleRegistry, 37) || @createInternalModuleById(37), EventEmitter = @getInternalField(@internalModuleRegistry, 18) || @createInternalModuleById(18);\nvar IPv4Reg = new @RegExp(\"^((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$\");\nvar IPv6Reg = new @RegExp(\"^((\?:(\?:[0-9a-fA-F]{1,4}):){7}(\?:(\?:[0-9a-fA-F]{1,4})|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){6}(\?:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|:(\?:[0-9a-fA-F]{1,4})|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){5}(\?::((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(\?:[0-9a-fA-F]{1,4})){1,2}|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){4}(\?:(:(\?:[0-9a-fA-F]{1,4})){0,1}:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(\?:[0-9a-fA-F]{1,4})){1,3}|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){3}(\?:(:(\?:[0-9a-fA-F]{1,4})){0,2}:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(\?:[0-9a-fA-F]{1,4})){1,4}|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){2}(\?:(:(\?:[0-9a-fA-F]{1,4})){0,3}:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(\?:[0-9a-fA-F]{1,4})){1,5}|:)|(\?:(\?:[0-9a-fA-F]{1,4}):){1}(\?:(:(\?:[0-9a-fA-F]{1,4})){0,4}:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(:(\?:[0-9a-fA-F]{1,4})){1,6}|:)|(\?::((\?::(\?:[0-9a-fA-F]{1,4})){0,5}:((\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(\?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])|(\?::(\?:[0-9a-fA-F]{1,4})){1,7}|:)))(%[0-9a-zA-Z-.:]{1,})\?$\"), { connect: bunConnect } = Bun, { setTimeout } = globalThis, bunTlsSymbol = Symbol.for(\"::buntls::\"), bunSocketServerHandlers = Symbol.for(\"::bunsocket_serverhandlers::\"), bunSocketServerConnections = Symbol.for(\"::bunnetserverconnections::\"), bunSocketServerOptions = Symbol.for(\"::bunnetserveroptions::\"), bunSocketInternal = Symbol.for(\"::bunnetsocketinternal::\"), bunTLSConnectOptions = Symbol.for(\"::buntlsconnectoptions::\"), SocketClass, Socket = function(InternalSocket) {\n return SocketClass = InternalSocket, Object.defineProperty(SocketClass.prototype, Symbol.toStringTag, {\n value: \"Socket\",\n enumerable: !1\n }), Object.defineProperty(function Socket(options) {\n return new InternalSocket(options);\n }, Symbol.hasInstance, {\n value(instance) {\n return instance instanceof InternalSocket;\n }\n });\n}(class Socket2 extends Duplex {\n static #Handlers = {\n close: Socket2.#Close,\n data({ data: self }, buffer) {\n self.bytesRead += buffer.length;\n const queue = self.#readQueue;\n if (queue.isEmpty()) {\n if (self.push(buffer))\n return;\n }\n queue.push(buffer);\n },\n drain: Socket2.#Drain,\n end: Socket2.#Close,\n error(socket, error) {\n const self = socket.data, callback = self.#writeCallback;\n if (callback)\n self.#writeCallback = null, callback(error);\n self.emit(\"error\", error);\n },\n open(socket) {\n const self = socket.data;\n socket.timeout(self.timeout), socket.ref(), self[bunSocketInternal] = socket, self.connecting = !1;\n const options = self[bunTLSConnectOptions];\n if (options) {\n const { session } = options;\n if (session)\n self.setSession(session);\n }\n if (!self.#upgraded)\n self.emit(\"connect\", self);\n Socket2.#Drain(socket);\n },\n handshake(socket, success, verifyError) {\n const { data: self } = socket;\n self._securePending = !1, self.secureConnecting = !1, self._secureEstablished = !!success, self.emit(\"secure\", self);\n const { checkServerIdentity } = self[bunTLSConnectOptions];\n if (!verifyError && typeof checkServerIdentity === \"function\" && self.servername) {\n const cert = self.getPeerCertificate(!0);\n verifyError = checkServerIdentity(self.servername, cert);\n }\n if (self._requestCert || self._rejectUnauthorized) {\n if (verifyError) {\n if (self.authorized = !1, self.authorizationError = verifyError.code || verifyError.message, self._rejectUnauthorized) {\n self.destroy(verifyError);\n return;\n }\n }\n } else\n self.authorized = !0;\n self.emit(\"secureConnect\", verifyError);\n },\n timeout(socket) {\n const self = socket.data;\n self.emit(\"timeout\", self);\n },\n binaryType: \"buffer\"\n };\n static #Close(socket) {\n const self = socket.data;\n if (self.#closed)\n return;\n self.#closed = !0, self[bunSocketInternal] = null;\n const queue = self.#readQueue;\n if (queue.isEmpty()) {\n if (self.push(null))\n return;\n }\n queue.push(null);\n }\n static #Drain(socket) {\n const self = socket.data, callback = self.#writeCallback;\n if (callback) {\n const chunk = self.#writeChunk, written = socket.write(chunk);\n if (self.bytesWritten += written, written < chunk.length)\n self.#writeChunk = chunk.slice(written);\n else\n self.#writeCallback = null, self.#writeChunk = null, callback(null);\n }\n }\n static [bunSocketServerHandlers] = {\n data: Socket2.#Handlers.data,\n close(socket) {\n Socket2.#Handlers.close(socket), this.data[bunSocketServerConnections]--;\n },\n end(socket) {\n Socket2.#Handlers.end(socket), this.data[bunSocketServerConnections]--;\n },\n open(socket) {\n const self = this.data, options = self[bunSocketServerOptions], { pauseOnConnect, connectionListener, InternalSocketClass, requestCert, rejectUnauthorized } = options, _socket = new InternalSocketClass({});\n if (_socket.isServer = !0, _socket._requestCert = requestCert, _socket._rejectUnauthorized = rejectUnauthorized, _socket.#attach(this.localPort, socket), self.maxConnections && self[bunSocketServerConnections] >= self.maxConnections) {\n const data = {\n localAddress: _socket.localAddress,\n localPort: _socket.localPort,\n localFamily: _socket.localFamily,\n remoteAddress: _socket.remoteAddress,\n remotePort: _socket.remotePort,\n remoteFamily: _socket.remoteFamily || \"IPv4\"\n };\n socket.end(), self.emit(\"drop\", data);\n return;\n }\n if (!pauseOnConnect)\n _socket.resume();\n if (self[bunSocketServerConnections]++, typeof connectionListener == \"function\")\n if (InternalSocketClass.name === \"TLSSocket\")\n self.once(\"secureConnection\", () => connectionListener(_socket));\n else\n connectionListener(_socket);\n self.emit(\"connection\", _socket);\n },\n handshake(socket, success, verifyError) {\n const { data: self } = socket;\n if (self.emit(\"secure\", self), self._securePending = !1, self.secureConnecting = !1, self._secureEstablished = !!success, self._requestCert || self._rejectUnauthorized) {\n if (verifyError) {\n if (self.authorized = !1, self.authorizationError = verifyError.code || verifyError.message, self._rejectUnauthorized) {\n self.destroy(verifyError);\n return;\n }\n }\n } else\n self.authorized = !0;\n self.emit(\"secureConnection\", verifyError);\n },\n error(socket, error) {\n Socket2.#Handlers.error(socket, error), this.data.emit(\"error\", error);\n },\n timeout: Socket2.#Handlers.timeout,\n connectError: Socket2.#Handlers.connectError,\n drain: Socket2.#Handlers.drain,\n binaryType: \"buffer\"\n };\n bytesRead = 0;\n bytesWritten = 0;\n #closed = !1;\n connecting = !1;\n localAddress = \"127.0.0.1\";\n #readQueue = @createFIFO();\n remotePort;\n [bunSocketInternal] = null;\n [bunTLSConnectOptions] = null;\n timeout = 0;\n #writeCallback;\n #writeChunk;\n #pendingRead;\n isServer = !1;\n _handle;\n _parent;\n _parentWrap;\n #socket;\n #upgraded;\n constructor(options) {\n const { socket, signal, write, read, allowHalfOpen = !1, ...opts } = options || {};\n super({\n ...opts,\n allowHalfOpen,\n readable: !0,\n writable: !0\n });\n if (this._handle = this, this._parent = this, this._parentWrap = this, this.#pendingRead = @undefined, this.#upgraded = !1, socket instanceof Socket2)\n this.#socket = socket;\n signal\?.once(\"abort\", () => this.destroy()), this.once(\"connect\", () => this.emit(\"ready\"));\n }\n address() {\n return {\n address: this.localAddress,\n family: this.localFamily,\n port: this.localPort\n };\n }\n get bufferSize() {\n return this.writableLength;\n }\n #attach(port, socket) {\n if (this.remotePort = port, socket.data = this, socket.timeout(this.timeout), socket.ref(), this[bunSocketInternal] = socket, this.connecting = !1, !this.#upgraded)\n this.emit(\"connect\", this);\n Socket2.#Drain(socket);\n }\n connect(port, host, connectListener) {\n var path, connection = this.#socket, _checkServerIdentity = @undefined;\n if (typeof port === \"string\") {\n if (path = port, port = @undefined, typeof host === \"function\")\n connectListener = host, host = @undefined;\n } else if (typeof host == \"function\") {\n if (typeof port === \"string\")\n path = port, port = @undefined;\n connectListener = host, host = @undefined;\n }\n if (typeof port == \"object\") {\n var {\n port,\n host,\n path,\n socket,\n localAddress,\n localPort,\n family,\n hints,\n lookup,\n noDelay,\n keepAlive,\n keepAliveInitialDelay,\n requestCert,\n rejectUnauthorized,\n pauseOnConnect,\n servername,\n checkServerIdentity,\n session\n } = port;\n if (_checkServerIdentity = checkServerIdentity, this.servername = servername, socket)\n connection = socket;\n }\n if (!pauseOnConnect)\n this.resume();\n this.connecting = !0, this.remotePort = port;\n const bunTLS = this[bunTlsSymbol];\n var tls = @undefined;\n if (typeof bunTLS === \"function\") {\n if (tls = bunTLS.call(this, port, host, !0), this._requestCert = !0, this._rejectUnauthorized = rejectUnauthorized, tls) {\n if (tls.rejectUnauthorized = rejectUnauthorized, tls.requestCert = !0, tls.session = session || tls.session, this.servername = tls.servername, tls.checkServerIdentity = _checkServerIdentity || tls.checkServerIdentity, this[bunTLSConnectOptions] = tls, !connection && tls.socket)\n connection = tls.socket;\n }\n if (connection) {\n if (typeof connection !== \"object\" || !(connection instanceof Socket2) || typeof connection[bunTlsSymbol] === \"function\")\n @throwTypeError(\"socket must be an instance of net.Socket\");\n }\n if (this.authorized = !1, this.secureConnecting = !0, this._secureEstablished = !1, this._securePending = !0, connectListener)\n this.on(\"secureConnect\", connectListener);\n } else if (connectListener)\n this.on(\"connect\", connectListener);\n try {\n if (connection) {\n const socket2 = connection[bunSocketInternal];\n if (socket2) {\n this.connecting = !0, this.#upgraded = !0;\n const result = socket2.upgradeTLS({\n data: this,\n tls,\n socket: Socket2.#Handlers\n });\n if (result) {\n const [raw, tls2] = result;\n connection[bunSocketInternal] = raw, raw.timeout(raw.timeout), raw.connecting = !1, this[bunSocketInternal] = tls2;\n } else\n throw this[bunSocketInternal] = null, new Error(\"Invalid socket\");\n } else\n connection.once(\"connect\", () => {\n const socket3 = connection[bunSocketInternal];\n if (!socket3)\n return;\n this.connecting = !0, this.#upgraded = !0;\n const result = socket3.upgradeTLS({\n data: this,\n tls,\n socket: Socket2.#Handlers\n });\n if (result) {\n const [raw, tls2] = result;\n connection[bunSocketInternal] = raw, raw.timeout(raw.timeout), raw.connecting = !1, this[bunSocketInternal] = tls2;\n } else\n throw this[bunSocketInternal] = null, new Error(\"Invalid socket\");\n });\n } else if (path)\n bunConnect({\n data: this,\n unix: path,\n socket: Socket2.#Handlers,\n tls\n }).catch((error) => {\n this.emit(\"error\", error), this.emit(\"close\");\n });\n else\n bunConnect({\n data: this,\n hostname: host || \"localhost\",\n port,\n socket: Socket2.#Handlers,\n tls\n }).catch((error) => {\n this.emit(\"error\", error), this.emit(\"close\");\n });\n } catch (error) {\n process.nextTick(emitErrorAndCloseNextTick, this, error);\n }\n return this;\n }\n _destroy(err, callback) {\n const socket = this[bunSocketInternal];\n socket && process.nextTick(endNT, socket, callback, err);\n }\n _final(callback) {\n this[bunSocketInternal]\?.end(), callback();\n }\n get localAddress() {\n return \"127.0.0.1\";\n }\n get localFamily() {\n return \"IPv4\";\n }\n get localPort() {\n return this[bunSocketInternal]\?.localPort;\n }\n get pending() {\n return this.connecting;\n }\n _read(size) {\n const queue = this.#readQueue;\n let chunk;\n while (chunk = queue.peek()) {\n if (!this.push(chunk))\n return;\n queue.shift();\n }\n }\n get readyState() {\n if (this.connecting)\n return \"opening\";\n if (this.readable)\n return this.writable \? \"open\" : \"readOnly\";\n else\n return this.writable \? \"writeOnly\" : \"closed\";\n }\n ref() {\n this[bunSocketInternal]\?.ref();\n }\n get remoteAddress() {\n return this[bunSocketInternal]\?.remoteAddress;\n }\n get remoteFamily() {\n return \"IPv4\";\n }\n resetAndDestroy() {\n this[bunSocketInternal]\?.end();\n }\n setKeepAlive(enable = !1, initialDelay = 0) {\n return this;\n }\n setNoDelay(noDelay = !0) {\n return this;\n }\n setTimeout(timeout, callback) {\n if (this[bunSocketInternal]\?.timeout(timeout), this.timeout = timeout, callback)\n this.once(\"timeout\", callback);\n return this;\n }\n unref() {\n this[bunSocketInternal]\?.unref();\n }\n _write(chunk, encoding, callback) {\n if (typeof chunk == \"string\" && encoding !== \"ascii\")\n chunk = @Buffer.from(chunk, encoding);\n var written = this[bunSocketInternal]\?.write(chunk);\n if (written == chunk.length)\n callback();\n else if (this.#writeCallback)\n callback(new Error(\"overlapping _write()\"));\n else {\n if (written > 0)\n if (typeof chunk == \"string\")\n chunk = chunk.slice(written);\n else\n chunk = chunk.subarray(written);\n this.#writeCallback = callback, this.#writeChunk = chunk;\n }\n }\n}), connect = createConnection;\n\nclass Server extends EventEmitter {\n #server;\n #listening = !1;\n [bunSocketServerConnections] = 0;\n [bunSocketServerOptions];\n maxConnections = 0;\n constructor(options, connectionListener) {\n super();\n if (typeof options === \"function\")\n connectionListener = options, options = {};\n else if (options == null || typeof options === \"object\")\n options = { ...options };\n else\n throw new Error(\"bun-net-polyfill: invalid arguments\");\n const { maxConnections } = options;\n this.maxConnections = Number.isSafeInteger(maxConnections) && maxConnections > 0 \? maxConnections : 0, options.connectionListener = connectionListener, this[bunSocketServerOptions] = options;\n }\n ref() {\n return this.#server\?.ref(), this;\n }\n unref() {\n return this.#server\?.unref(), this;\n }\n close(callback) {\n if (this.#server) {\n if (this.#server.stop(!0), this.#server = null, this.#listening = !1, this[bunSocketServerConnections] = 0, this.emit(\"close\"), typeof callback === \"function\")\n callback();\n return this;\n }\n if (typeof callback === \"function\") {\n const error = new Error(\"Server is not running\");\n error.code = \"ERR_SERVER_NOT_RUNNING\", callback(error);\n }\n return this;\n }\n address() {\n const server = this.#server;\n if (server) {\n const unix = server.unix;\n if (unix)\n return unix;\n let address = server.hostname;\n const type = isIP(address), port = server.port;\n if (typeof port === \"number\")\n return {\n port,\n address,\n family: type \? `IPv${type}` : @undefined\n };\n if (type)\n return {\n address,\n family: type \? `IPv${type}` : @undefined\n };\n return address;\n }\n return null;\n }\n getConnections(callback) {\n if (typeof callback === \"function\")\n callback(null, this.#server \? this[bunSocketServerConnections] : 0);\n return this;\n }\n listen(port, hostname, onListen) {\n let backlog, path, exclusive = !1;\n if (typeof port === \"string\") {\n if (Number.isSafeInteger(hostname)) {\n if (hostname > 0)\n backlog = hostname;\n } else if (typeof hostname === \"function\")\n onListen = hostname;\n path = port, hostname = @undefined, port = @undefined;\n } else {\n if (typeof hostname === \"function\")\n onListen = hostname, hostname = @undefined;\n if (typeof port === \"function\")\n onListen = port, port = 0;\n else if (typeof port === \"object\") {\n const options = port;\n options.signal\?.addEventListener(\"abort\", () => this.close()), hostname = options.host, exclusive = options.exclusive === !0;\n const path2 = options.path;\n if (port = options.port, !Number.isSafeInteger(port) || port < 0)\n if (path2)\n hostname = path2, port = @undefined;\n else {\n let message = 'The argument \\'options\\' must have the property \"port\" or \"path\"';\n try {\n message = `${message}. Received ${JSON.stringify(options)}`;\n } catch {\n }\n const error = @makeTypeError(message);\n throw error.code = \"ERR_INVALID_ARG_VALUE\", error;\n }\n else if (!Number.isSafeInteger(port) || port < 0)\n port = 0;\n if (typeof port.callback === \"function\")\n onListen = port\?.callback;\n } else if (!Number.isSafeInteger(port) || port < 0)\n port = 0;\n hostname = hostname || \"::\";\n }\n try {\n var tls = @undefined, TLSSocketClass = @undefined;\n const bunTLS = this[bunTlsSymbol], options = this[bunSocketServerOptions];\n if (typeof bunTLS === \"function\")\n [tls, TLSSocketClass] = bunTLS.call(this, port, hostname, !1), options.servername = tls.serverName, options.InternalSocketClass = TLSSocketClass;\n else\n options.InternalSocketClass = SocketClass;\n this.#server = Bun.listen(path \? {\n exclusive,\n unix: path,\n tls,\n socket: SocketClass[bunSocketServerHandlers]\n } : {\n exclusive,\n port,\n hostname,\n tls,\n socket: SocketClass[bunSocketServerHandlers]\n }), this.#server.data = this, this.#listening = !0, setTimeout(emitListeningNextTick, 1, this, onListen);\n } catch (err) {\n this.#listening = !1, setTimeout(emitErrorNextTick, 1, this, err);\n }\n return this;\n }\n}\n$ = {\n createServer,\n Server,\n createConnection,\n connect,\n isIP,\n isIPv4,\n isIPv6,\n Socket,\n [Symbol.for(\"::bunternal::\")]: SocketClass\n};\nreturn $})\n"_s; // // diff --git a/test/js/bun/net/socket.test.ts b/test/js/bun/net/socket.test.ts index cf6625dc57594..70173d31a1adf 100644 --- a/test/js/bun/net/socket.test.ts +++ b/test/js/bun/net/socket.test.ts @@ -93,7 +93,7 @@ it("should reject on connection error, calling both connectError() and rejecting expect(socket).toBeDefined(); expect(socket.data).toBe(data); expect(error).toBeDefined(); - expect(error.name).toBe("SystemError"); + expect(error.name).toBe("ECONNREFUSED"); expect(error.message).toBe("Failed to connect"); }, data() { @@ -119,7 +119,7 @@ it("should reject on connection error, calling both connectError() and rejecting () => done(new Error("Promise should reject instead")), err => { expect(err).toBeDefined(); - expect(err.name).toBe("SystemError"); + expect(err.name).toBe("ECONNREFUSED"); expect(err.message).toBe("Failed to connect"); done(); @@ -164,8 +164,9 @@ it("should handle connection error", done => { expect(socket).toBeDefined(); expect(socket.data).toBe(data); expect(error).toBeDefined(); - expect(error.name).toBe("SystemError"); + expect(error.name).toBe("ECONNREFUSED"); expect(error.message).toBe("Failed to connect"); + expect((error as any).code).toBe("ECONNREFUSED"); done(); }, data() { @@ -191,5 +192,5 @@ it("should handle connection error", done => { }); it("should not leak memory when connect() fails again", async () => { - await expectMaxObjectTypeCount(expect, "TCPSocket", 1, 100); + await expectMaxObjectTypeCount(expect, "TCPSocket", 2, 100); }); diff --git a/test/js/node/net/node-net.test.ts b/test/js/node/net/node-net.test.ts index efdff4bc55fce..109391af5fc40 100644 --- a/test/js/node/net/node-net.test.ts +++ b/test/js/node/net/node-net.test.ts @@ -370,8 +370,36 @@ it("should handle connection error", done => { } errored = true; expect(error).toBeDefined(); - expect(error.name).toBe("SystemError"); expect(error.message).toBe("Failed to connect"); + expect((error as any).code).toBe("ECONNREFUSED"); + }); + + socket.on("connect", () => { + done(new Error("Should not have connected")); + }); + + socket.on("close", () => { + expect(errored).toBe(true); + done(); + }); +}); + +it("should handle connection error (unix)", done => { + let errored = false; + + // @ts-ignore + const socket = connect("loser", () => { + done(new Error("Should not have connected")); + }); + + socket.on("error", error => { + if (errored) { + return done(new Error("Should not have errored twice")); + } + errored = true; + expect(error).toBeDefined(); + expect(error.message).toBe("Failed to connect"); + expect((error as any).code).toBe("ENOENT"); }); socket.on("connect", () => {