Skip to content

Commit

Permalink
__writev
Browse files Browse the repository at this point in the history
  • Loading branch information
cirospaciari committed Oct 18, 2024
1 parent 253cc15 commit 902eaf8
Showing 1 changed file with 33 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/js/node/net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ const Socket = (function (InternalSocket) {
if (finalCallback) {
self.#final_callback = null;
finalCallback();
self.data = null;
return;
}
if (!self.#ended) {
Expand All @@ -226,6 +227,7 @@ const Socket = (function (InternalSocket) {
}
queue.push(null);
}
self.data = null;
}

static #Drain(socket) {
Expand All @@ -249,9 +251,11 @@ const Socket = (function (InternalSocket) {
static [bunSocketServerHandlers] = {
data: Socket.#Handlers.data,
close(socket) {
const data = this.data;
if (!data) return;
Socket.#Handlers.close(socket);
this.data.server[bunSocketServerConnections]--;
this.data.server._emitCloseIfDrained();
data.server[bunSocketServerConnections]--;
data.server._emitCloseIfDrained();
},
end(socket) {
Socket.#Handlers.end(socket);
Expand Down Expand Up @@ -338,9 +342,11 @@ const Socket = (function (InternalSocket) {
}
},
error(socket, error) {
const data = this.data;
if (!data) return;
Socket.#Handlers.error(socket, error);
this.data.emit("error", error);
this.data.server.emit("clientError", error, this.data);
data.emit("error", error);
data.server.emit("clientError", error, data);
},
timeout: Socket.#Handlers.timeout,
connectError: Socket.#Handlers.connectError,
Expand Down Expand Up @@ -802,6 +808,29 @@ const Socket = (function (InternalSocket) {
else this.once("finish", this.destroy);
}

//TODO: migrate to native
_writev(data, callback) {
const allBuffers = data.allBuffers;
let chunks;
chunks = data;
if (allBuffers) {
for (let i = 0; i < data.length; i++) {
data[i] = data[i].chunk;
}
} else {
for (let i = 0; i < data.length; i++) {
const { chunk, encoding } = data[i];
if (typeof chunk === "string") {
data[i] = Buffer.from(chunk, encoding);
} else {
data[i] = chunk;
}
}
}
const chunk = Buffer.concat(chunks || []);
return this._write(chunk, "buffer", callback);
}

_write(chunk, encoding, callback) {
if (typeof chunk == "string" && encoding !== "ascii") chunk = Buffer.from(chunk, encoding);
var written = this[bunSocketInternal]?.write(chunk);
Expand Down

0 comments on commit 902eaf8

Please sign in to comment.