npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@lmdb/node-unix-socket

v0.2.6

Published

node-unix-socket allows you to use SO_REUSEPORT, SOCK_SEQPACKET, SOCK_DGRAM in Node.js.

Downloads

2

Readme

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