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

posix-read

v0.2.0

Published

Do POSIX read on files and sockets with Node.js

Downloads

4

Readme

posix-read

Do POSIX read on files and sockets with Node.js.

Build Status

Motivation

In Node.js, reading from sockets is performed using uv_read_start() from libuv. This consumes data from the file descriptor whenever it is available, but consumes it all. There is not built-in way to read a fixed amount, let's say n bytes.

This is a problem when you want to read only n bytes and leave the rest in the socket (so that, for instance, another process reads remaining data).

posix-read is a module to perform a POSIX read on a socket. That way, only n bytes are brought up to user-space and the rest remains in kernel-space, ready to be read(2) by any other process.

Usage

Warning

For posix-read to work, your code must prevent libuv to start reading from the socket. That means the socket must have the pauseOnCreate property.

In practice: if you get the socket from a net.Server, this server has to be created with the pauseOnConnect set to true.

Example

const net = require('net');
const posixRead = require('posix-read');

const server = net.createServer({ pauseOnConnect: true }, function (socket) {
    // Just got an incoming connection. Let's read 10 bytes from it (but DO NOT
    // consume more than 10 bytes from the socket).
    posixRead.read(socket, 10, function (err, buffer) {
        if (err && err.endOfFile)
            return process.stderr.write('peer sent less than 10 bytes\n');
        if (err)
            return process.stderr.write(`error: ${err}\n`);

        return process.stdout.write(`10 first bytes: ${buffer}\n`);
    });
}).listen(1234);

Error types

If a problem happens, the Error object passed to the callback has helpful properties:

  • error.badStream === true if the socket is malformed or its file descriptor is not available
  • error.endOfFile === true if the end-of-file was reached before having read all the bytes requested
  • error.systemError === true in case of a system call error (in such a case, error.message should contain more useful information.

License

MIT license