Skip to content

Commit

Permalink
revert
Browse files Browse the repository at this point in the history
  • Loading branch information
cirospaciari committed Nov 2, 2024
1 parent 6e44861 commit 5236d97
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 44 deletions.
29 changes: 7 additions & 22 deletions src/bun.js/api/bun/socket.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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..];
Expand All @@ -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();
}
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions src/bun.js/api/sockets.classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ function generate(ssl) {
alpnProtocol: {
getter: "getALPNProtocol",
},
bytesWritten: {
getter: "getBytesWritten",
},
write: {
fn: "write",
length: 3,
Expand Down Expand Up @@ -166,9 +169,7 @@ function generate(ssl) {
bytesWritten: {
getter: "getBytesWritten",
},
bufferedQueueSize: {
getter: "getBufferedQueueSize",
},

setServername: {
fn: "setServername",
length: 1,
Expand Down
26 changes: 7 additions & 19 deletions src/js/node/net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 5236d97

Please sign in to comment.