diff --git a/src/bun.js/api/bun/socket.zig b/src/bun.js/api/bun/socket.zig index 39330e3a452047..7cc80571567d4d 100644 --- a/src/bun.js/api/bun/socket.zig +++ b/src/bun.js/api/bun/socket.zig @@ -1474,11 +1474,6 @@ fn NewSocket(comptime ssl: bool) type { if (vm.isShuttingDown()) { return; } - - this.internalFlush(); - // is not writable if we have buffered data - if (this.buffered_data_for_node_net.len > 0) return; - vm.eventLoop().enter(); defer vm.eventLoop().exit(); @@ -2368,10 +2363,15 @@ fn NewSocket(comptime ssl: bool) type { }; } - fn internalFlush(this: *This) JSValue { + pub fn flush( + this: *This, + _: *JSC.JSGlobalObject, + _: *JSC.CallFrame, + ) JSValue { + JSC.markBinding(@src()); 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,15 +2385,6 @@ 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(); } @@ -2715,12 +2706,6 @@ fn NewSocket(comptime ssl: bool) type { ) JSValue { return JSC.JSValue.jsNumber(this.bytes_written + this.buffered_data_for_node_net.len); } - pub fn getBufferedQueueSize( - this: *This, - _: *JSC.JSGlobalObject, - ) JSValue { - return JSC.JSValue.jsNumber(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 d50aabf7ef8025..ee3e60e1dfe12f 100644 --- a/src/bun.js/api/sockets.classes.ts +++ b/src/bun.js/api/sockets.classes.ts @@ -83,6 +83,9 @@ function generate(ssl) { alpnProtocol: { getter: "getALPNProtocol", }, + bytesWritten: { + getter: "getBytesWritten", + }, write: { fn: "write", length: 3, @@ -166,9 +169,7 @@ function generate(ssl) { bytesWritten: { getter: "getBytesWritten", }, - bufferedQueueSize: { - getter: "getBufferedQueueSize", - }, + setServername: { fn: "setServername", length: 1, diff --git a/src/js/node/net.ts b/src/js/node/net.ts index 26b83ae81ce43d..63003ae68431dc 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 (!writeChunk || socket.$write(writeChunk || "", self._pendingEncoding || "utf8")) { + if (socket.$write(writeChunk || "", "utf8")) { self._pendingData = self.#writeCallback = null; callback(null); } else { @@ -856,28 +856,16 @@ const Socket = (function (InternalSocket) { if (!socket) { // detached but connected? wait for the socket to be attached this.#writeCallback = callback; - this._pendingEncoding = encoding; - this._pendingData = chunk; + this._pendingEncoding = "buffer"; + this._pendingData = Buffer.from(chunk, encoding); return; } - const writeResult = socket.$write(chunk, encoding); + const success = socket?.$write(chunk, encoding); this[kBytesWritten] = socket.bytesWritten; - switch (writeResult) { - case -1: - // dropped - this.#writeCallback = callback; - this._pendingEncoding = encoding; - this._pendingData = chunk; - break; - default: - // written or buffered by the socket - if (socket.bufferedQueueSize === 0) { - callback(); - return; - } - } - if (this.#writeCallback) { + if (success) { + callback(); + } else if (this.#writeCallback) { callback(new Error("overlapping _write()")); } else { this.#writeCallback = callback;