mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 10:28:47 +00:00
Implement socket handle serialization and IPC socket transfer
This commit is contained in:
@@ -146,27 +146,19 @@
|
||||
* @returns {[unknown, Serialized] | null}
|
||||
*/
|
||||
export function serialize(_message, _handle, _options) {
|
||||
// sending file descriptors is not supported yet
|
||||
return null; // send the message without the file descriptor
|
||||
|
||||
/*
|
||||
const net = require("node:net");
|
||||
const dgram = require("node:dgram");
|
||||
if (handle instanceof net.Server) {
|
||||
if (_handle instanceof net.Server) {
|
||||
// this one doesn't need a close function, but the fd needs to be kept alive until it is sent
|
||||
const server = handle as unknown as (typeof net)["Server"] & { _handle: Bun.TCPSocketListener<unknown> };
|
||||
return [server._handle, { cmd: "NODE_HANDLE", message, type: "net.Server" }];
|
||||
} else if (handle instanceof net.Socket) {
|
||||
const server = _handle as any;
|
||||
return [server._handle, { cmd: "NODE_HANDLE", message: _message, type: "net.Server" }];
|
||||
} else if (_handle instanceof net.Socket) {
|
||||
const new_message: { cmd: "NODE_HANDLE"; message: unknown; type: "net.Socket"; key?: string } = {
|
||||
cmd: "NODE_HANDLE",
|
||||
message,
|
||||
message: _message,
|
||||
type: "net.Socket",
|
||||
};
|
||||
const socket = handle as unknown as (typeof net)["Socket"] & {
|
||||
_handle: Bun.Socket;
|
||||
server: (typeof net)["Server"] | null;
|
||||
setTimeout(timeout: number): void;
|
||||
};
|
||||
const socket = _handle as any;
|
||||
if (!socket._handle) return null; // failed
|
||||
|
||||
// If the socket was created by net.Server
|
||||
@@ -174,36 +166,32 @@ export function serialize(_message, _handle, _options) {
|
||||
// The worker should keep track of the socket
|
||||
new_message.key = socket.server._connectionKey;
|
||||
|
||||
const firstTime = !this[kChannelHandle].sockets.send[message.key];
|
||||
const socketList = getSocketList("send", this, message.key);
|
||||
|
||||
// The server should no longer expose a .connection property
|
||||
// and when asked to close it should query the socket status from
|
||||
// the workers
|
||||
if (firstTime) socket.server._setupWorker(socketList);
|
||||
// NOTE: We're skipping the socket list management for now
|
||||
// as it's not critical for basic functionality
|
||||
|
||||
// Act like socket is detached
|
||||
if (!options?.keepOpen) socket.server._connections--;
|
||||
if (!_options?.keepOpen) socket.server._connections--;
|
||||
}
|
||||
|
||||
const internal_handle = socket._handle;
|
||||
|
||||
// Remove handle from socket object, it will be closed when the socket
|
||||
// will be sent
|
||||
if (!options?.keepOpen) {
|
||||
if (!_options?.keepOpen) {
|
||||
// we can use a $newZigFunction to have it unset the callback
|
||||
// @ts-ignore
|
||||
const nop = () => {};
|
||||
internal_handle.onread = nop;
|
||||
socket._handle = null;
|
||||
socket.setTimeout(0);
|
||||
}
|
||||
return [internal_handle, new_message];
|
||||
} else if (handle instanceof dgram.Socket) {
|
||||
} else if (_handle instanceof dgram.Socket) {
|
||||
// this one doesn't need a close function, but the fd needs to be kept alive until it is sent
|
||||
throw new Error("todo serialize dgram.Socket");
|
||||
} else {
|
||||
throw $ERR_INVALID_HANDLE_TYPE();
|
||||
}
|
||||
*/
|
||||
}
|
||||
/**
|
||||
* @param {Serialized} serialized
|
||||
@@ -224,7 +212,20 @@ export function parseHandle(target, serialized, fd) {
|
||||
return;
|
||||
}
|
||||
case "net.Socket": {
|
||||
throw new Error("TODO case net.Socket");
|
||||
const socket = new net.Socket({
|
||||
fd,
|
||||
readable: true,
|
||||
writable: true,
|
||||
});
|
||||
|
||||
// If the socket was created by net.Server we will track the socket
|
||||
if (serialized.key) {
|
||||
// NOTE: We're skipping socket list management for now
|
||||
// as it's not critical for basic functionality
|
||||
}
|
||||
|
||||
emit(target, serialized.message, socket);
|
||||
return;
|
||||
}
|
||||
case "dgram.Socket": {
|
||||
throw new Error("TODO case dgram.Socket");
|
||||
|
||||
73
test-ipc-socket-handle.js
Normal file
73
test-ipc-socket-handle.js
Normal file
@@ -0,0 +1,73 @@
|
||||
const { fork } = require("child_process");
|
||||
const net = require("net");
|
||||
|
||||
// Test the serialize function directly
|
||||
const ipc = require("../../src/js/builtins/Ipc.ts");
|
||||
|
||||
if (process.argv[2] === "test-serialize") {
|
||||
// Test serialization
|
||||
const server = net.createServer();
|
||||
|
||||
server.listen(0, () => {
|
||||
const socket = net.connect(server.address().port);
|
||||
|
||||
socket.on("connect", () => {
|
||||
console.log("Testing serialize function...");
|
||||
|
||||
const result = ipc.serialize({ test: "message" }, socket, {});
|
||||
|
||||
if (result) {
|
||||
console.log("Serialization result:", result);
|
||||
console.log("Handle type:", result[1].type);
|
||||
console.log("Message:", result[1].message);
|
||||
} else {
|
||||
console.log("Serialization returned null");
|
||||
}
|
||||
|
||||
process.exit(0);
|
||||
});
|
||||
});
|
||||
} else if (process.argv[2] === "child") {
|
||||
process.on("message", (msg, handle) => {
|
||||
console.log("Child received message:", msg);
|
||||
console.log("Child received handle:", handle);
|
||||
|
||||
if (handle && handle.end) {
|
||||
handle.end("echo");
|
||||
console.log("Child sent echo");
|
||||
} else {
|
||||
console.error("Handle is undefined or missing end method");
|
||||
}
|
||||
});
|
||||
|
||||
process.send({ what: "ready" });
|
||||
} else {
|
||||
// Parent process
|
||||
const child = fork(process.argv[1], ["child"]);
|
||||
|
||||
child.on("message", msg => {
|
||||
if (msg.what === "ready") {
|
||||
console.log("Child is ready, creating socket...");
|
||||
|
||||
const server = net.createServer();
|
||||
server.on("connection", socket => {
|
||||
console.log("Got connection, sending socket to child...");
|
||||
child.send({ what: "socket" }, socket);
|
||||
});
|
||||
|
||||
server.listen(0, () => {
|
||||
const client = net.connect(server.address().port);
|
||||
client.on("data", data => {
|
||||
console.log("Parent received:", data.toString());
|
||||
client.end();
|
||||
server.close();
|
||||
process.exit(0);
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
child.on("exit", code => {
|
||||
console.log("Child exited with code:", code);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
// Copyright Joyent, Inc. and other Node contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
||||
// persons to whom the Software is furnished to do so, subject to the
|
||||
// following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included
|
||||
// in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
||||
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
'use strict';
|
||||
const {
|
||||
mustCall,
|
||||
mustCallAtLeast,
|
||||
} = require('../common');
|
||||
const assert = require('assert');
|
||||
const fork = require('child_process').fork;
|
||||
const net = require('net');
|
||||
const debug = require('util').debuglog('test');
|
||||
|
||||
if (process.argv[2] === 'child') {
|
||||
|
||||
const onSocket = mustCall((msg, socket) => {
|
||||
if (msg.what !== 'socket') return;
|
||||
process.removeListener('message', onSocket);
|
||||
socket.end('echo');
|
||||
debug('CHILD: got socket');
|
||||
});
|
||||
|
||||
process.on('message', onSocket);
|
||||
|
||||
process.send({ what: 'ready' });
|
||||
} else {
|
||||
|
||||
const child = fork(process.argv[1], ['child']);
|
||||
|
||||
child.on('exit', mustCall((code, signal) => {
|
||||
const message = `CHILD: died with ${code}, ${signal}`;
|
||||
assert.strictEqual(code, 0, message);
|
||||
}));
|
||||
|
||||
// Send net.Socket to child.
|
||||
function testSocket() {
|
||||
|
||||
// Create a new server and connect to it,
|
||||
// but the socket will be handled by the child.
|
||||
const server = net.createServer();
|
||||
server.on('connection', mustCall((socket) => {
|
||||
// TODO(@jasnell): Close does not seem to actually be called.
|
||||
// It is not clear if it is needed.
|
||||
socket.on('close', () => {
|
||||
debug('CLIENT: socket closed');
|
||||
});
|
||||
child.send({ what: 'socket' }, socket);
|
||||
}));
|
||||
server.on('close', mustCall(() => {
|
||||
debug('PARENT: server closed');
|
||||
}));
|
||||
|
||||
server.listen(0, mustCall(() => {
|
||||
debug('testSocket, listening');
|
||||
const connect = net.connect(server.address().port);
|
||||
let store = '';
|
||||
connect.on('data', mustCallAtLeast((chunk) => {
|
||||
store += chunk;
|
||||
debug('CLIENT: got data');
|
||||
}));
|
||||
connect.on('close', mustCall(() => {
|
||||
debug('CLIENT: closed');
|
||||
assert.strictEqual(store, 'echo');
|
||||
server.close();
|
||||
}));
|
||||
}));
|
||||
}
|
||||
|
||||
const onReady = mustCall((msg) => {
|
||||
if (msg.what !== 'ready') return;
|
||||
child.removeListener('message', onReady);
|
||||
|
||||
testSocket();
|
||||
});
|
||||
|
||||
// Create socket and send it to child.
|
||||
child.on('message', onReady);
|
||||
}
|
||||
Reference in New Issue
Block a user