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

csocket

v1.0.3

Published

Port for C's `sys/socket.h` methods for synchronous usage of sockets as file descriptors.

Downloads

1

Readme

csocket

Port for C's sys/socket.h methods for synchronous usage of sockets as file descriptors.

This package uses a native node module so it is compiled on your machine. For using the module prebuilt for Linux, see csocket-linux.

It is not recommended to make synchronous operations in NodeJS since there is only one thread and blocking it prevents the event loop from going, and any asynchronous operations will not be done. See Switching to normal NodeJS operations to minimize this risk.

Usage

Install using:

npm install --save csocket

Then in NodeJS:

const csocket = require('csocket');

const PORT = 1234;
const TIMEOUT = 5000; // in milliseconds (allows floating point), undefined/null/0 for no timeout

/* TCP Server */
const BIND_HOST = '127.0.0.1'; // use 0.0.0.0 for all interfaces
const BACKLOG = 5;
let listenerFd = csocket.socket();
csocket.bind(listenerFd, BIND_HOST, PORT);
csocket.listen(listenerFd, BACKLOG);
let socketFd = csocket.accept(listenerFd, TIMEOUT);

/* TCP Client */
const HOST = '127.0.0.1';
let socketFd = csocket.socket();
csocket.connect(socketFd, HOST, PORT);

/* Both */
let bytesSent = csocket.send(socketFd, new Buffer([1, 2, 3]), TIMEOUT);
// bytesSent <= 3
let buffer = new Buffer(4096);
let bytesReceived = csocket.read(socketFd, buffer, TIMEOUT);
// bytesReceived <= 3
// buffer == <Buffer 01 02 03 00 00 00 ... >

/*
 * Always close the file descriptor (not from this library, no "close" in sys/socket.h).
 * The kernel might not immediately close the socket when the process exits,
 *   and NodeJS won't close it during garbage collection.
 */
const net = require('net');
net.createServer().listen({ fd: listenerFd }).close();
new net.Socket({ fd: socketFd }).end();

Switching to normal NodeJS operations

At any point you can use the file descriptor with NodeJS libraries that follow the asynchronous way NodeJS is supposed to work with, like fs and net.

const csocket = require('csocket');
const net = require('net');

let socketfd = csocket.socket();

...

/* TCP Server */
let server = net.createServer().listen({ fd: socketFd });

/* TCP Client */
let socket = new net.Socket({ fd: socketFd });

Important note

Once you are done with your synchronous operations (receiving, writing, accepting, etc) you should always create a net.Socket/net.Server and let NodeJS handle the file descriptor's lifecycle.

According to my experience, if you don't pass the file descriptor to NodeJS the socket may not close properly, and may cause a port to stay bound or the process to hang (even if using fs.close).

Only do that after you finish using csocket, otherwise you may race condition with NodeJS over accepts/reads/etc.

Implementation notes

  • Currently csocket.socket() only creates a socket for TCP/IPv4 (AF_INET/SOCK_STREAM). PRs are welcome.
  • Not all sys/socket.h operations are implemented, only the bare minimum needed to communicate. PRs are welcome.
  • Allowing a timeout is actually implemented using the select, I cannot fathom why other I/O implementations don't expose this functionallity, blocking I/O functions are unusuable without it.