Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support onread in net #10753

Merged
merged 11 commits into from
May 5, 2024
33 changes: 27 additions & 6 deletions src/js/node/net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,10 @@ const Socket = (function (InternalSocket) {
pauseOnConnect = false;
#upgraded;
#unrefOnConnected = false;
#handlers = Socket.#Handlers;

constructor(options) {
const { socket, signal, write, read, allowHalfOpen = false, ...opts } = options || {};
const { socket, signal, write, read, allowHalfOpen = false, onread = null, ...opts } = options || {};
super({
...opts,
allowHalfOpen,
Expand All @@ -359,6 +360,26 @@ const Socket = (function (InternalSocket) {
if (socket instanceof Socket) {
this.#socket = socket;
}
if (onread) {
if (typeof onread !== "object") {
throw new TypeError("onread must be an object");
}
if (typeof onread.callback !== "function") {
throw new TypeError("onread.callback must be a function");
}
// when the onread option is specified we use a different handlers object
this.#handlers = {
...Socket.#Handlers,
data({ data: self }, buffer) {
if (!self) return;
try {
onread.callback(buffer.length, buffer);
} catch (e) {
self.emit("error", e);
}
},
};
}

if (signal) {
signal.addEventListener("abort", () => this.destroy());
Expand Down Expand Up @@ -454,7 +475,7 @@ const Socket = (function (InternalSocket) {
bunConnect({
data: this,
fd,
socket: Socket.#Handlers,
socket: this.#handlers,
tls,
}).catch(error => {
this.emit("error", error);
Expand Down Expand Up @@ -529,7 +550,7 @@ const Socket = (function (InternalSocket) {
const result = socket.upgradeTLS({
data: this,
tls,
socket: Socket.#Handlers,
socket: this.#handlers,
});
if (result) {
const [raw, tls] = result;
Expand All @@ -554,7 +575,7 @@ const Socket = (function (InternalSocket) {
const result = socket.upgradeTLS({
data: this,
tls,
socket: Socket.#Handlers,
socket: this.#handlers,
});

if (result) {
Expand All @@ -576,7 +597,7 @@ const Socket = (function (InternalSocket) {
bunConnect({
data: this,
unix: path,
socket: Socket.#Handlers,
socket: this.#handlers,
tls,
}).catch(error => {
this.emit("error", error);
Expand All @@ -588,7 +609,7 @@ const Socket = (function (InternalSocket) {
data: this,
hostname: host || "localhost",
port: port,
socket: Socket.#Handlers,
socket: this.#handlers,
tls,
}).catch(error => {
this.emit("error", error);
Expand Down
30 changes: 30 additions & 0 deletions test/js/node/net/node-net.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,36 @@ describe("net.Socket read", () => {
.on("error", done);
}, socket_domain),
);

it(
"should support onread callback",
runWithServer((server, drain, done) => {
var data = "";
const options = {
host: server.hostname,
port: server.port,
onread: {
buffer: Buffer.alloc(4096),
callback: (size, buf) => {
data += buf.slice(0, size).toString("utf8");
},
},
};
const socket = createConnection(options, () => {
expect(socket).toBeDefined();
expect(socket.connecting).toBe(false);
})
.on("end", () => {
try {
expect(data).toBe(message);
done();
} catch (e) {
done(e);
}
})
.on("error", done);
}),
);
});
}
});
Expand Down