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

socket-promise

v1.0.1

Published

This package extends default NodeJS sockets adding promises support for connect and receive.

Downloads

6

Readme

Socket Promise

Socket Promise extends NodeJS default Socket class and add's promise support.

API

Client

writeInChunks(data, chunkLength) - Split and write data in chunks

conn(ip, port, [timeout]) - (Promise) Connect to specified ip and port

recv([contains, timeout]) - (Promise) Receive data

SocketPromise.wrapper(socket) - Implements SocketPromise functions in a default socket instance

Server

SocketPromise.createServer() - Create tcp server

accept() - (Promise) Accept new connection

Also including everything in https://nodejs.org/api/net.html

Installation

npm i -S socket-promise

Client Example

const SocketPromise = require('socket-promise');

(async () => {
    const socket = new SocketPromise();
    try {
        const milisec = await socket.conn('123.123.123.123', 1337, 5000);
        console.log(`Connected for ${milisec} miliseconds`);
        socket.write('hello world'); // send
        const response1 = await socket.recv(); // receive
        console.log(`Received ${response1.toString()}`);
        // receive until response contains string 'end_of_message'
        const response2 = await socket.recv('end_of_message');
        console.log(`Received ${response2.toString()}`);
    } catch (err) {
        if (err instanceof SocketPromise.errors.Timeout) {
            console.log('Connect Timeout'); // after 5 seconds
        } else if (err instanceof SocketPromise.errors.Closed) {
            console.log('Connection Closed');
        } else {
            console.log(err);
        }
    }
})();

Server Example

const SocketPromise = require('socket-promise');

(async () => {
    const server = SocketPromise.createServer();
    server.listen(1234, '0.0.0.0');
    let client;
    let counter = 1;
    try {
        while (client = await server.accept()) {
          console.log(`client ${counter}`);
          counter++;
          // do something with the client
        }
      } catch (err) {
        if (err instanceof SocketPromise.errors.Closed) {
            console.log('Connection Closed');
        } else {
            console.log(err);
        }
      }
})();