Unix dgram, seqpacket, etc binding for Node.js.

Overview

node-unix-socket

npm_bandage github_ci_status

node-unix-socket allows you to use some nonblocking unix sockets that are currently not supported by Node.js native modules, including:

  • unix seqpacket(SOCK_SEQPACKET) sockets
  • unix datagram(SOCK_DGRAM) sockets
  • Using SO_REUSEPORT enabled TCP net.Server

node-unix-socket is a napi-rs based Node.js addons and:

  • This lib bases on n-api and is pre-compiled so that it doesn't require compilation environments if yours is pre-built supported.
  • This lib won't introduce any other asynchronous runtimes as it uses libuv inside Node.js.

We use SOCK_SEQPACKET sockets for in our internal APM.

Tested Platforms & Node.js

Platform Node.js DgramSocket Seqpacket
x64 Linux 12 + LTS
x64 Darwin 12 + LTS
aarch64 Darwin 12 + LTS

Installation

npm i node-unix-socket

API Documents

API Documents

Seqpacket Sockets

SOCK_SEQPACKET sockets are like SOCK_STREAM sockets while they keep message boundaries.

Note that SOCK_SEQPACKET sockets don't work on MacOS.

Example

Online Example

const { SeqpacketServer, SeqpacketSocket } = require('node-unix-socket');
const os = require('os');
const path = require('path');
const fs = require('fs');

const bindPath = path.resolve(os.tmpdir(), './my_seqpacket.sock');

try {
  fs.unlinkSync(bindPath);
} catch (e) {}

const server = new SeqpacketServer();
server.listen(bindPath);
server.on('connection', (socket) => {
  socket.on('data', (buf) => {
    console.log('received', buf.toString());
  });
});

const client = new SeqpacketSocket();
client.connect(bindPath, () => {
  const data = ['hello, ', 'w', 'o', 'r', 'l', 'd'];

  for (const str of data) {
    client.write(Buffer.from(str));
  }
  client.end();
});

Dgram Sockets

Example

Online Example

const { DgramSocket } = require('node-unix-socket');
const os = require('os');
const path = require('path');
const fs = require('fs');

const path1 = path.resolve(os.tmpdir(), './my_dgram_1.sock');
const path2 = path.resolve(os.tmpdir(), './my_dgram_2.sock');

try {
  fs.unlinkSync(path1);
  fs.unlinkSync(path2);
} catch (err) {}

const socket1 = new DgramSocket();
const socket2 = new DgramSocket();

socket1.bind(path1);
socket2.bind(path2);

socket2.on('data', (data, remoteAddr) => {
  console.log(`socket2 received: ${data.toString()}`);
  // echo
  socket2.sendTo(data, 0, data.length, remoteAddr);
});

socket1.on('data', (data) => {
  console.log(`socket1 received: ${data.toString()}`);
});

setInterval(() => {
  const buf = Buffer.from('hello');
  socket1.sendTo(buf, 0, buf.length, path2);
}, 1000);

SO_REUSEPORT enabled TCP net.Server

The cluster module share server ports by accepting new connections in the primary process and distributing them to worker processes.

With SO_REUSEPORT, sockets will be distributed by kernel instead, and which should be more performant especially for scenario of having a lot of short-lived connections.

For example, the arrow in the image below shows cpu usage of a PM2 primary process which we found in our environment.

cpu_usage

Note that SO_REUSEPORT might behave much differently across operating systems. See this post for more information.

Example

Online Http Server Example

const { createReuseportFd } = require('node-unix-socket');
const { Server, Socket } = require('net');

const port = 8080;
const host = '0.0.0.0';

// create multple servers listening to a same host, port.
for (let i = 0; i < 2; i += 1) {
  const fd = createReuseportFd(port, host);
  const server = new Server((socket) => {
    socket.on('data', (buf) => {
      console.log(`server ${i} received:`, buf);
      // echo
      socket.write(buf);
    });
  });

  server.listen(
    {
      fd,
    },
    () => {
      console.log(`server ${i} is listening on ${port}`);
    }
  );
}

setInterval(() => {
  const client = new Socket();
  client.on('data', (buf) => {
    console.log('client received:', buf);
    client.destroy();
  });
  client.connect(port, host, () => {
    client.write(Buffer.from('hello'));
  });
}, 1000);

CONTRIBUTING

CONTRIBUTING.md

Development

  1. Setup rust and Node.js.
  2. Modify the code.
  3. npm run build && npm run test

LICENSE

MIT

Comments
  • chore(deps): bump json5 from 2.2.1 to 2.2.3

    chore(deps): bump json5 from 2.2.1 to 2.2.3

    Bumps json5 from 2.2.1 to 2.2.3.

    Release notes

    Sourced from json5's releases.

    v2.2.3

    v2.2.2

    • Fix: Properties with the name __proto__ are added to objects and arrays. (#199) This also fixes a prototype pollution vulnerability reported by Jonathan Gregson! (#295).
    Changelog

    Sourced from json5's changelog.

    v2.2.3 [code, diff]

    v2.2.2 [code, diff]

    • Fix: Properties with the name __proto__ are added to objects and arrays. (#199) This also fixes a prototype pollution vulnerability reported by Jonathan Gregson! (#295).
    Commits
    • c3a7524 2.2.3
    • 94fd06d docs: update CHANGELOG for v2.2.3
    • 3b8cebf docs(security): use GitHub security advisories
    • f0fd9e1 docs: publish a security policy
    • 6a91a05 docs(template): bug -> bug report
    • 14f8cb1 2.2.2
    • 10cc7ca docs: update CHANGELOG for v2.2.2
    • 7774c10 fix: add proto to objects and arrays
    • edde30a Readme: slight tweak to intro
    • 97286f8 Improve example in readme
    • Additional commits viewable in compare view

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself) You can disable automated security fix PRs for this repo from the Security Alerts page.
    dependencies javascript 
    opened by dependabot[bot] 0
  • chore(deps): bump openssl from 0.10.38 to 0.10.48

    chore(deps): bump openssl from 0.10.38 to 0.10.48

    Bumps openssl from 0.10.38 to 0.10.48.

    Release notes

    Sourced from openssl's releases.

    openssl v0.10.48

    What's Changed

    New Contributors

    Full Changelog: https://github.com/sfackler/rust-openssl/compare/openssl-v0.10.47...openssl-v0.10.48

    openssl v0.10.47

    No release notes provided.

    openssl v0.10.46

    No release notes provided.

    openssl v0.10.45

    No release notes provided.

    openssl v0.10.44

    No release notes provided.

    openssl v0.10.43

    No release notes provided.

    openssl v0.10.42

    No release notes provided.

    openssl v0.10.41

    No release notes provided.

    openssl v0.10.40

    No release notes provided.

    openssl v0.10.39

    No release notes provided.

    Commits
    • 4ff734f Release openssl v0.10.48 and openssl-sys v0.9.83 (#1855)
    • 5efceaa Merge pull request #1854 from alex/davids-openssl-of-horrors
    • 6ced4f3 Fix race condition with X509Name creation
    • a752805 Document the horror show
    • 78aa9aa Always provide an X509V3Context in X509Extension::new because OpenSSL require...
    • 332311b Resolve an injection vulnerability in EKU creation
    • 482575b Resolve an injection vulnerability in SAN creation
    • 690eeb2 Merge pull request #1852 from smoelius/master
    • e5b6d97 Improve reliability of some tests
    • 319200a Merge pull request #1851 from alex/libressl-versions
    • Additional commits viewable in compare view

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself) You can disable automated security fix PRs for this repo from the Security Alerts page.
    dependencies rust 
    opened by dependabot[bot] 0
  • chore(deps): bump bumpalo from 3.9.1 to 3.12.0

    chore(deps): bump bumpalo from 3.9.1 to 3.12.0

    Bumps bumpalo from 3.9.1 to 3.12.0.

    Changelog

    Sourced from bumpalo's changelog.

    3.12.0

    Released 2023-01-17.

    Added

    • Added the bumpalo::boxed::Box::bump and bumpalo::collections::String::bump getters to get the underlying Bump that a string or box was allocated into.

    Changed

    • Some uses of Box that MIRI did not previously consider as UB are now reported as UB, and bumpalo's internals have been adjusted to avoid the new UB.

    3.11.1

    Released 2022-10-18.

    Security

    • Fixed a bug where when std::vec::IntoIter was ported to bumpalo::collections::vec::IntoIter, it didn't get its underlying Bump's lifetime threaded through. This meant that rustc was not checking the borrows for bumpalo::collections::IntoIter and this could result in use-after-free bugs.

    3.11.0

    Released 2022-08-17.

    Added

    • Added support for per-Bump allocation limits. These are enforced only in the slow path when allocating new chunks in the Bump, not in the bump allocation hot path, and therefore impose near zero overhead.
    • Added the bumpalo::boxed::Box::into_inner method.

    Changed

    • Updated to Rust 2021 edition.
    • The minimum supported Rust version (MSRV) is now 1.56.0.

    3.10.0

    ... (truncated)

    Commits
    • 50ba1bd Bump to 3.12.0
    • 3dd3650 Merge pull request #190 from mattfbacon/main
    • 37be9a9 Merge branch 'fitzgen:main' into main
    • 3664dbb Add String::bump method
    • 701514f Merge pull request #189 from mattfbacon/main
    • c6507f7 Add Vec::bump method
    • b1e67b7 Merge pull request #188 from saethlin/field-retagging
    • d325e2c Use ManuallyDrop with bumpalo's Box instead of mem::forget
    • c699cd1 Merge pull request #183 from stepancheg/allocated-bytes-no-headers
    • 5805a29 Clarify allocated_bytes does not include headers
    • Additional commits viewable in compare view

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
    • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
    • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
    • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language

    You can disable automated security fix PRs for this repo from the Security Alerts page.

    dependencies rust 
    opened by dependabot[bot] 0
  • chore(deps): bump openssl from 0.10.48 to 0.10.55

    chore(deps): bump openssl from 0.10.48 to 0.10.55

    Bumps openssl from 0.10.48 to 0.10.55.

    Release notes

    Sourced from openssl's releases.

    openssl-v0.10.55

    What's Changed

    New Contributors

    Full Changelog: https://github.com/sfackler/rust-openssl/compare/openssl-v0.10.54...openssl-v0.10.55

    openssl-v0.10.54

    What's Changed

    Full Changelog: https://github.com/sfackler/rust-openssl/compare/openssl-v0.10.53...openssl-v0.10.54

    openssl-v0.10.53

    What's Changed

    New Contributors

    Full Changelog: https://github.com/sfackler/rust-openssl/compare/openssl-v0.10.52...openssl-v0.10.53

    ... (truncated)

    Commits
    • d7dae6f Merge pull request #1970 from alex/bump-for-release
    • 983b9e2 Release openssl v0.10.55 and openssl-sys v0.9.89
    • 28b3925 Merge pull request #1966 from tesuji/tidy-old-msrv
    • f03a2dc Merge pull request #1968 from sfackler/empty-domain-segfault
    • 155b3dc Fix handling of empty host strings
    • 9784356 chore: simplify cfg attributes
    • 8ab3c3f update min-version passed to bindgen
    • 8587ff8 chore: use pre-existing clean APIs instead
    • b1e16e9 clippy: use strip_prefix instead of manually strip
    • fb5ae60 clippy: remove unused allow attributes
    • Additional commits viewable in compare view

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself) You can disable automated security fix PRs for this repo from the Security Alerts page.
    dependencies rust 
    opened by dependabot[bot] 0
  • Typescript is broken

    Typescript is broken

    node_modules/node-unix-socket/js/seqpacket.ts:237:26 - error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'.
      Type 'undefined' is not assignable to type 'number'.
    
    237     this.wrap.write(buf, offset, length, cb);
                                 ~~~~~~
    
    opened by Shonke 2
Releases(v0.2.5)
A super minimal wrapper around unix sockets for IPC on top of tokio.

tokio-unix-ipc This crate implements a minimal abstraction over UNIX domain sockets for the purpose of IPC on top of tokio.

Armin Ronacher 26 Nov 18, 2022
Streaming data over unix sockets, in Rust

Unix-socket based client/server In order to dig into Sōzu channels, I had to dig into the workings of unix sockets. What this repo contains a small so

Emmanuel Bosquet 3 Nov 28, 2022
Examples of interacting with a Polkadot node using Rust

Examples of interacting with a Polkadot node Some examples of using JSON RPC to interact with a Polkadot node, working up to manually building and sub

Parity Technologies 15 Dec 21, 2022
Easy c̵̰͠r̵̛̠ö̴̪s̶̩̒s̵̭̀-t̶̲͝h̶̯̚r̵̺͐e̷̖̽ḁ̴̍d̶̖̔ ȓ̵͙ė̶͎ḟ̴͙e̸̖͛r̶̖͗ë̶̱́ṉ̵̒ĉ̷̥e̷͚̍ s̷̹͌h̷̲̉a̵̭͋r̷̫̊ḭ̵̊n̷̬͂g̵̦̃ f̶̻̊ơ̵̜ṟ̸̈́ R̵̞̋ù̵̺s̷̖̅ţ̸͗!̸̼͋

Rust S̵̓i̸̓n̵̉ I̴n̴f̶e̸r̵n̷a̴l mutability! Howdy, friendly Rust developer! Ever had a value get m̵̯̅ð̶͊v̴̮̾ê̴̼͘d away right under your nose just when

null 294 Dec 23, 2022
Node.js http server framework powered by Hyper native binding.

hnsjs POC project. Install this test package yarn add @hnsjs/core Support matrix node10 node12 node14 node15 Windows x64 ✓ ✓ ✓ ✓ Windows x32 ✓ ✓ ✓ ✓

LongYinan 18 Nov 23, 2022
notify Node.js binding via napi-rs.

@napi-rs/notify notify Node.js binding via napi-rs. Install this package yarn add

LongYinan 9 Jun 6, 2022
swc node binding use wasm

node_swc swc node binding use wasm Build Make sure you have rust wasm-pack installed. $ yarn build # build wasm, node Usage import { parseSync, printS

伊撒尔 23 Sep 8, 2022
Benchmark over Node.js binding frameworks in Rust

Benchmark over Node.js binding frameworks in Rust

LongYinan 7 Dec 28, 2022
lzma-rs binding to Node.js via napi-rs.

@napi-rs/lzma lzma-rs binding to Node.js via napi-rs. ?? Help me to become a full-time open-source developer by sponsoring me on Github Install yarn a

LongYinan 8 Aug 16, 2022
Russh Node.js binding

@napi-rs/ssh ?? Help me to become a full-time open-source developer by sponsoring me on Github Usage import { connect, checkKnownHosts } from '@napi-r

LongYinan 21 Mar 4, 2023
Node.js tar binding

@napi-rs/tar Node.js tar binding https://docs.rs/tar/latest/tar/ Usage export class Entries { [Symbol.iterator](): Iterator<Entry, void, void> } exp

Node-API (N-API) for Rust 5 Dec 18, 2023
A node and runtime configuration for polkadot node.

MANTA NODE This repo is a fresh FRAME-based Substrate node, forked from substrate-developer-hub/substrate-node-templte ?? It links to pallet-manta-dap

Manta Network 14 Apr 25, 2021
A minimal library for building compiled Node.js add-ons in Rust via Node-API

A minimal library for building compiled Node.js add-ons in Rust via Node-API

Node-API (N-API) for Rust 3.1k Dec 29, 2022
Nvm - Node Version Manager - POSIX-compliant bash script to manage multiple active node.js versions

Node Version Manager Table of Contents Intro About Installing and Updating Install & Update Script Additional Notes Troubleshooting on Linux Troublesh

nvm.sh 63.8k Jan 7, 2023
Egui node graph is a featureful, customizable library to create node graph applications using egui

Egui node graph is a featureful, customizable library to create node graph applications using egui. The library takes care of presenting a node graph to your users, and allows customizing many aspects of the interaction, creating the semantics you want for your specific application.

null 367 Jan 8, 2023
Abuse the node.js inspector mechanism in order to force any node.js/electron/v8 based process to execute arbitrary javascript code.

jscythe abuses the node.js inspector mechanism in order to force any node.js/electron/v8 based process to execute arbitrary javascript code, even if t

Simone Margaritelli 301 Jan 4, 2023
Simple node and rust script to achieve an easy to use bridge between rust and node.js

Node-Rust Bridge Simple rust and node.js script to achieve a bridge between them. Only 1 bridge can be initialized per rust program. But node.js can h

Pure 5 Apr 30, 2023
Sample lightning node command-line app built on top of Ldk Node (similar to ldk-sample).

ldk-node-sample Sample lightning node command-line app built on top of Ldk Node (similar to ldk-sample ). Installation git clone https://github.com/op

optout 3 Nov 21, 2023
rga: ripgrep, but also search in PDFs, E-Books, Office documents, zip, tar.gz, etc.

rga: ripgrep, but also search in PDFs, E-Books, Office documents, zip, tar.gz, etc. rga is a line-oriented search tool that allows you to look for a r

null 5.2k Jan 2, 2023
Rust edit distance routines accelerated using SIMD. Supports fast Hamming, Levenshtein, restricted Damerau-Levenshtein, etc. distance calculations and string search.

triple_accel Rust edit distance routines accelerated using SIMD. Supports fast Hamming, Levenshtein, restricted Damerau-Levenshtein, etc. distance cal

Daniel Liu 75 Jan 8, 2023