diff --git a/src/bun.js/api/bun/socket.zig b/src/bun.js/api/bun/socket.zig index 7cc80571567d4d..0651256cd5073c 100644 --- a/src/bun.js/api/bun/socket.zig +++ b/src/bun.js/api/bun/socket.zig @@ -1474,6 +1474,12 @@ fn NewSocket(comptime ssl: bool) type { if (vm.isShuttingDown()) { return; } + this.ref(); + defer this.deref(); + this.internalFlush(); + // is not writable if we have buffered data or if we are already detached + if (this.buffered_data_for_node_net.len > 0 or this.socket.isDetached()) return; + vm.eventLoop().enter(); defer vm.eventLoop().exit(); @@ -2363,15 +2369,10 @@ fn NewSocket(comptime ssl: bool) type { }; } - pub fn flush( - this: *This, - _: *JSC.JSGlobalObject, - _: *JSC.CallFrame, - ) JSValue { - JSC.markBinding(@src()); + fn internalFlush(this: *This) void { if (this.buffered_data_for_node_net.len > 0) { const written: usize = @intCast(@max(this.socket.write(this.buffered_data_for_node_net.slice(), false), 0)); - + this.bytes_written += written; if (written > 0) { if (this.buffered_data_for_node_net.len > written) { const remaining = this.buffered_data_for_node_net.slice()[written..]; @@ -2385,6 +2386,15 @@ fn NewSocket(comptime ssl: bool) type { } this.socket.flush(); + } + + pub fn flush( + this: *This, + _: *JSC.JSGlobalObject, + _: *JSC.CallFrame, + ) JSValue { + JSC.markBinding(@src()); + this.internalFlush(); return JSValue.jsUndefined(); } @@ -2706,6 +2716,7 @@ fn NewSocket(comptime ssl: bool) type { ) JSValue { return JSC.JSValue.jsNumber(this.bytes_written + this.buffered_data_for_node_net.len); } + pub fn getALPNProtocol( this: *This, globalObject: *JSC.JSGlobalObject, diff --git a/src/bun.js/api/sockets.classes.ts b/src/bun.js/api/sockets.classes.ts index ee3e60e1dfe12f..a3a06da9d8f271 100644 --- a/src/bun.js/api/sockets.classes.ts +++ b/src/bun.js/api/sockets.classes.ts @@ -83,9 +83,6 @@ function generate(ssl) { alpnProtocol: { getter: "getALPNProtocol", }, - bytesWritten: { - getter: "getBytesWritten", - }, write: { fn: "write", length: 3, @@ -169,7 +166,6 @@ function generate(ssl) { bytesWritten: { getter: "getBytesWritten", }, - setServername: { fn: "setServername", length: 1, diff --git a/src/js/node/net.ts b/src/js/node/net.ts index 63003ae68431dc..fd80b0783bec9c 100644 --- a/src/js/node/net.ts +++ b/src/js/node/net.ts @@ -241,7 +241,7 @@ const Socket = (function (InternalSocket) { if (callback) { const writeChunk = self._pendingData; - if (socket.$write(writeChunk || "", "utf8")) { + if (!writeChunk || socket.$write(writeChunk || "", self._pendingEncoding || "utf8")) { self._pendingData = self.#writeCallback = null; callback(null); } else { @@ -856,12 +856,12 @@ const Socket = (function (InternalSocket) { if (!socket) { // detached but connected? wait for the socket to be attached this.#writeCallback = callback; - this._pendingEncoding = "buffer"; - this._pendingData = Buffer.from(chunk, encoding); + this._pendingEncoding = encoding; + this._pendingData = chunk; return; } - const success = socket?.$write(chunk, encoding); + const success = socket.$write(chunk, encoding); this[kBytesWritten] = socket.bytesWritten; if (success) { callback();