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

@yingyeothon/naive-socket

v0.4.0

Published

A naive TCP Socket library to send and receive with the opposite.

Downloads

29

Readme

Naive Socket

This is a very lightweight library to communicate with the opposite using net.Socket.

Rationale

I'm a Serverless programmer and I want to reduce a deployment package size as possible as I can. There are lots of many libraries to do anything in this world but because of this they have the big codebase sometimes. For example, ioredis is the perfect library to use Redis in NodeJS but it is very big to use in Serverless environment.

This library is a very naive wrapper of net.Socket so it doesn't have functions to use. This is proper to use only when there is no need for complex features while communicating with the opposite, for example, the case of only sending an one command to Redis and finished.

Example

Fire and forget

This is a simple example when we don't expect the boundary of message. It would return any message that returns from the server immediately. So please be careful this message can be truncated because this function can finish before a message fully reached.

import NaiveSocket from "@yingyeothon/naive-socket";

const redisNewline = `\r\n`;
const naiveSocket = new NaiveSocket({
  host: "localhost",
  port: 6379
});

export const ping = () =>
  naiveSocket.send({
    message: [`PING`, ``].join(redisNewline)
  });

Using Length

Or, we can wait by the length of expected response when we know the exact length of the message that we want to receive. It helps ensure the message boundary when we communicate multiple commands in this one connection.

export const ping = () =>
  naiveSocket.send({
    message: [`PING`, ``].join(redisNewline),
    fulfill: `+PONG`.length
  });

Using RegExp

Or, in some cases, RegExp is more useful to check the correct message. This is a simple example to communicate with Redis with 2 cases.

  1. Send a message via Redis queue.
  2. Retrieve a gameId by userId.
export const enqueueGameMessage = (gameId: string, message: any) =>
  naiveSocket.send({
    message: [
      `RPUSH "queue/${gameId}" "${JSON.stringify(JSON.stringify(message))}"`,
      ``
    ].join(redisNewline),
    timeoutMillis: 1000
  });

export const loadGameId = (userId: string) =>
  naiveSocket
    .send({
      message: [`GET "gameId/${userId}"`, ``].join(redisNewline),
      // When the pattern of gameId is UUID.
      fulfill: /^(\$[0-9]+\r\n(?:[0-9A-Za-z_\-]+)\r\n|\$-1\r\n)/,
      timeoutMillis: 1000
    })
    .then(response => response.match(/([0-9A-F\-]+)\r\n/)[1] || "");

timeoutMillis will help us to prevent infinitely waiting due to invalid RegExp.

Using TextMatch

If we want to receive more complex response, for example, like the response of SMEMBERS in Redis, we can use TextMatch for this.

import { withMatch } from "@yingyeothon/naive-socket/lib/match";

export const loadMembers = (membersKey: string) =>
  naiveSocket.send({
    message: [`SMEMBERS "${membersKey}"`, ``].join(redisNewline),
    /* This is the pattern of its result.
     * *COUNT\r\n
     * $LENGTH\r\n
     * SOMETHING-VALUE\r\n
     * ...
     */
    fulfill: withMatch(m => {
      let count = +m
        .capture("\r\n") // Read the first line,
        .values()[0]
        .slice(1); // And parse the count from "*COUNT".
      while (count-- > 0) {
        m.capture("\r\n") // Read a length of next line,
          .capture("\r\n"); // Read a value.
      }
      return m;
    }),
    timeoutMillis: 1000
  });

License

MIT