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

nodeia

v0.0.1

Published

Simplified networking api for Node.js

Downloads

12

Readme

Nodeia

Nodeia is a simplified networking api for Node. It is based on Bun.serve api and is designed to be easy to use and understand.

Note: Nodeia is still in development and is not yet ready for production use. You can help by testing it and reporting any issues you find.

Installation

npm install nodeia

Current features

  • ✅ HTTP server
  • ✅ WebSocket server
  • ✅ TCP server
  • ✅ TCP client
  • ❌ UDP server (todo)
  • ❌ UDP client (todo)

Usage

Simple HTTP server with WebSocket support

Here is an example of a simple HTTP server that responds with "Hello, world!" to all requests. It also supports WebSocket connections.

By default, Nodeia listens on 0.0.0.0 and port 3000. You can change this by passing the hostname and port options to the serve function. If the port given is already in use, Nodeia will use a random port. You can also manually pass port 0 to let the OS choose a random port.

import Nodeia from 'nodeia';

Nodeia.serve({
  fetch(req, server) {
    // Upgrade to WebSocket if the request is a WebSocket upgrade request
    // do not return a response if the request is upgraded
    if (server.upgrade(req)) return;

    return new Response('Hello, world!', {
      headers: {
        'Content-Type': 'text/plain',
      },
    });
  },
  listening(hostname, port, server) {
    // 👂 We are now listening to the requests
    console.log(`Listening on ${server.url}`);
  },
  websocket: {
    // Called when a WebSocket connection is opened
    open(ws) {
      ws.send('Hello, websocket!');
    },
    // Called when a WebSocket message is received
    message(ws, message) {
      const msg = String(message);
      console.log('WebSocket message:', msg);
      // Close the WebSocket connection if the message is 'close'
      if (msg === 'close') ws.close(1000, 'Goodbye!');
    },
    // Called when a WebSocket connection is closed
    close(ws, code, message) {
      console.log('WebSocket closed');
    },
  },
});

Simple TCP server

Here is an example of a simple TCP server that responds with "Hello, world!" to all connections.

import Nodeia from 'nodeia';

Nodeia.listen({
  port: 8080,
  hostname: 'localhost',
  socket: {
    open(socket) {
      console.log('Socket opened');
      socket.write('Hello, world!\n');
    },
    close(socket) {
      console.log('Socket closed');
    },
    data(socket, data) {
      const msg = String(data);
      console.log('Socket sent a data:', msg);

      if (msg === 'close') socket.end();
    },
    drain(socket) {
      console.log('Socket drained');
    },
    error(socket, err) {
      console.error('Socket error:', err);
    },
  },
});

Simple TCP client

Here is an example of a simple TCP client that connects to a TCP server and sends a message.

import Nodeia from 'nodeia';

Nodeia.connect({
  hostname: 'localhost',
  port: 3000,
  socket: {
    data(socket, data) {
      console.log('Server sent data:', String(data));

      if (String(data) === 'Hello, world!') socket.write('close');
    },
    open(socket) {
      console.log('Connection opened');
    },
    close(socket) {
      console.log('Connection closed');
    },
    drain(socket) {},
    error(socket, error) {},

    // client-specific handlers
    connectError(socket, error) {}, // connection failed
    end(socket) {}, // connection closed by server
    timeout(socket) {}, // connection timed out
  },
});