高性能的 Node.js Unix socket 库,使用 Rust (Neon) 实现,支持文件描述符传递 (SCM_RIGHTS)。
- 🚀 高性能 - 使用 Rust 实现,基于 poll 事件循环
- 📁 文件描述符传递 - 支持通过 Unix socket 传递文件描述符 (SCM_RIGHTS)
- 🔍 对方pid获取 - 读取连接对方的 pid,在确认身份时非常有用
- 📡 事件驱动 - 基于
data事件接收数据 - 🏗 TypeScript - 完整的类型定义
- 📦 开箱即用 - 无需编译/重编译,自带 Linux 兼容二进制
pnpm add myde-unix-socketimport { UServer, USocket } from "myde-unix-socket";
const server = new UServer();
server.listen("/tmp/my-socket");
server.on("connection", (socket) => {
socket.on("data", (data: Buffer, fds: number[]) => {
console.log("Received:", data.toString());
});
socket.write(Buffer.from("Hello!"));
});
server.resume(); // 开始接受连接import { USocket } from "myde-unix-socket";
const client = new USocket("/tmp/my-socket");
client.on("data", (data: Buffer, fds: number[]) => {
console.log("Received:", data.toString());
});
client.on("connect", () => {
client.write(Buffer.from("Hello Server!"));
});const server = new UServer();
server.listen("/tmp/test");
server.resume();
server.on("connection", (socket) => {
socket.on("data", (data: Buffer, fds: number[]) => {
console.log("Data:", data.toString());
console.log("FDs:", fds);
});
});
const client = new USocket("/tmp/test");
client.write(Buffer.from("message1"));
client.write(Buffer.from("message2"));import * as fs from "fs";
// 服务器
server.on("connection", (socket) => {
socket.on("data", (data, fds) => {
if (fds.length > 0) {
const content = fs.readFileSync(fds[0], "utf-8");
console.log(content);
fs.closeSync(fds[0]);
}
});
});
// 客户端
const fd = fs.openSync("/tmp/file.txt", "r");
client.write({ data: Buffer.from("fd"), fds: [fd] });
fs.closeSync(fd);pnpm install
pnpm run build
pnpm test脚本:
pnpm run build- 完整构建pnpm run build:rust- 构建 Rust 模块pnpm run build:ts- 构建 TypeScriptpnpm test- 运行测试pnpm run test:rust- 运行 Rust 测试
- Rust + nix - Unix socket 实现
- Neon - Rust/Node.js 桥接
- Vite - 打包
- Vitest - 测试
usocket 参考