support onread in net (#10753)

Co-authored-by: Georgijs Vilums <=>
Co-authored-by: gvilums <gvilums@users.noreply.github.com>
Co-authored-by: Jarred Sumner <jarred@jarredsumner.com>
This commit is contained in:
Georgijs
2024-05-04 20:35:28 -07:00
committed by GitHub
parent 9ba7d420d5
commit 7bfcc2c9e3
2 changed files with 57 additions and 6 deletions

View File

@@ -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,
@@ -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());
@@ -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);
@@ -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;
@@ -554,7 +575,7 @@ const Socket = (function (InternalSocket) {
const result = socket.upgradeTLS({
data: this,
tls,
socket: Socket.#Handlers,
socket: this.#handlers,
});
if (result) {
@@ -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);
@@ -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);

View File

@@ -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);
}),
);
});
}
});