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

unix-socketpair

v0.1.4

Published

create two connected unix sockets with the socketpair() function from POSIX

Downloads

179

Readme

unix-socketpair

This add-on exposes the real POSIX socketpair() to Node.js applications. socketpair() is unrivaled in simplicity when you want to create connected sockets. It allows very easy creation of a bi-directional data channel for a child process implemented in some other language than Node.js.

The sockets are created with the SOCK_CLOEXEC and SOCK_NONBLOCK flags, so they should work with Node.js without a problem. Note that the socket is dup()'ed into a child process if you pass it to the stdio argument of require('child_process').spawn or friends, so SOCK_CLOEXEC will not cause problems in that scenario.

Note

The add-on is implemented with napi, which is still experimental. You will have to run node with the --napi-modules flags for now if you want to use this module.

API

function socketpair(type)

const socketpair = require('unix-socketpair');
let fds = socketpair(type)

Create a pair of connected sockets.

  • type: socketpair.SOCK_STREAM (default) or socketpair.SOCK_DGRAM
  • returns: an array holding the two file descriptors for the created sockets.
  • throws: if the underlying call to socketpair() fails

The created sockets are already connected, so if you wrap them in a net.Socket, they will not generate a open or connected event.

For more information, see man 2 socketpair.

Warning

Node has no knowledge of raw file descriptors. One of the consequences is that they will not keep the node process running, but there could be other consequences as well.

You should generally wrap one or both of the file descriptors in an object like net.Socket. If you don't wrap a file descriptor, for example to pass it to a child process, then you should close it (in the parent process) as soon as possible (right after the spawn) with fs.close() or fs.closeSync().

Examples

This section shows some examples of how you can use unix-socketpair .

Printing the file descriptors

The simplest thing you could do with the file descriptors is print them. The example below shows how you might do that.

const socketpair = require('unix-socketpair');

const   stream_fds = socketpair(socketpair.SOCK_STREAM); // Make a pair of connected unix streaming sockets.
const datagram_fds = socketpair(socketpair.SOCK_DGRAM);  // Make a pair of connected unix datagram sockets.

console.log('streaming descriptors:', stream_fds);
console.log('datagram descriptors: ', datagram_fds);

Could output the following (the numbers may vary):

streaming descriptors: [3, 4]
datagram descriptors:  [5, 6]

Wrapping in net.Socket

You can create net.Socket instances to use one or both of the created sockets as you normally would in Node.js. At the moment of writing dgram.Socket does not support the creation from file descriptors, and net.Socket only supports streaming sockets.

The example below wraps both sockets in a net.Socket, but you could also wrap one of the two and send the other to a child process.

const socketpair = require('unix-socketpair');
const net        = require('net');

const fds = socketpair(socketpair.SOCK_STREAM);

const sock0 = net.Socket({fd: fds[0], readable: true, writable: true});
const sock1 = net.Socket({fd: fds[1], readable: true, writable: true});

let buffer = Buffer.alloc(0);

sock1.on('data', function (data) {
	buffer = Buffer.concat([buffer, data])
});
sock1.on('close', function() {
	console.log(buffer.toString())
});

sock0.write('Hello World!');
sock0.end();

Should output the following:

Hello World!