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

net-socket-messaging

v0.1.6

Published

Simple messaging protocol for node's net socket

Downloads

1,951

Readme

net-socket-messaging

npm install net-socket-messaging

Implementation of a simple messaging protocol by monkey-patching a net.Socket. The protocol is the simplest I could think of:

  • The head consists of 4 bytes which encodes an unsigned 32 int written in little endian.
  • The body is a string of bytes which encodes a string in utf8 and whose byte length is defined by the head.

Example

const socket = require("net").connect();
// `registerSocket` monkey-patches a net.Socket
// with the `send` method and the `message` event.
require("net-socket-messaging").monkeyPatch(socket);
// Once monkey-patched, the socket should not be
// directly used to read or write data.
const message = "foo";
socket.send(message);
// The `message` event is emitted only if there is a
// least one listener registered.
socket.on("message", (message) => {
  console.log("Received:", message);
});

API

NetSocketMessaging.patch(socket)

  • socket <net.Socket>: the socket to monkey-patch.
  • Returns <undefined>: I'm not a big fan of artificial chain callings, you should assume your side effects!

NetSocketMessaging.getMaxByteLength()

  • Returns <integer>: the maximum length of messages.

socket.send(message)

Send a message through the net.Socket.

  • message <string>: the message to send. Sending a string longer than 256 * 2^20 characters will fire an 'error' event on the socket. There is two motivations for this limit:
    1. It ensures that the body is under 1GiB (as a single utf16 character may be encoded in as much as 4 bytes). There is two reasons why we want to limit the length of the body. First, the maximum number encoded by the head is 2^32. Second, the stream.readable.read(size) does not accept a size argument greater than 2^30.
    2. JS engines impose a limit to the length of strings. Although the ECMAScript 2016 specification states that the maximum length of a string is 2^53 - 1, many engines opted for a smaller limit. A length of 256 * 2^20 is a conservative limit which most JS engines support.
  • Returns <null> | <boolean>: null if the message is too big to be sent and a <boolean> as per the return value of new.Socket.write(buffer).

Event 'message'

Emitted when a message has been fully received.

  • message <string>: the received message. Neither the length of the body nor the length of the message is checked. If the engine supports it, a message as long as 2^32 characters could be provided.