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

extensor

v1.0.7

Published

Extra funcionalities to socket.io

Downloads

116

Readme

Extensor

Build Status DependenciesSstatus FOSSA Status Known Vulnerabilities Code Coverage

Extend functions to the socket.io.

Currently implemented:

  • Handler authentication;
  • Grant unique connections;
  • Made easy the serialization with schemapack.

Next:

  • Serialization in acknowledge

PR's are welcome.

Contents

Install

npm install extensor

Examples

Binary serialization with schemapack

import { parsers } from "extensor";

// create a schema map
const { parser, schemas } = parsers.schemapack({
  message: {
    // Convert the event name to int
    // it's a method to minimize the packet size
    id: 1,
    schema: {
      content: "string",
      ts: "varuint"
    }
  }
});

// On both, server and client
// initialize socketio with the extensor generated parser
const io = SocketIO({
  parser
});

// You can encode an object, if you need, like this:
const binary = schemas.message.encode({
  content: "hi",
  ts: Math.round(Date.now() / 1000)
});
console.log(schemas.message.decode(binary)); // => { content: "hi": ts: 159798894154 }
  • All emitted "message" events are binary, according with the schema;
  • Here we not use 2 packets like in socket.io-parser binary event.
  • For the events that not in list, JSON parser is used.

Schemapack are the smallest JavaScript object serialization library.

All supported types and more info, you find at schemapack

Authentication

Server

import SocketIO from "socket.io";
import { auth } from "extensor";

const io = SocketIO();

auth.server(io, ({ socket, data }) => {
  if (data.token === 1) return { userId: 123 };

  return false;
});

io.on("connection", async socket => {
  await socket.auth;

  socket.on("msg", msg => console.log(msg));
});

Client

import SocketIOClient from "socket.io-client";
import { auth } from "extensor".

const socket = SocketIOClient();

socket.on("connect", () => {
  await auth.client(socket, { token: 1 });

  console.log(socket.userId); // => 123
});

   

Blocking multiple connections

To use in a cluster, you need an external storage, current have adapters for redis and ioredis modules.

Server

import { unique, adapters } from "extensor";

...

unique(io, {
  storage: adapters.IORedis(new Redis()) // default is stored in a simple local object
});

To catch a multiple attemp on client:

socket.on("error", err => {
  if (err === "multiple attemp") {
    alert("you already connected");
  }
});

 

API

parsers.schemapack( map: Object ): { parser: { Encoder, Decoder }, schemas, idmap }

Create a schemapack parser for both Socket.io server and client.

auth.server( io: SocketIO.Server, handler({ socket, data, done })[, options: Extensor.Options ]): void

Create a server middleware to handle authentication

auth.client( io: SocketIOClient.Socket, data[, callback(error?: string)]): Promise | void

Send credential to server

unique( io: SocketIO.Server [, options: ExtensorOptions ] )

Create a step on the server to force a single connection

License

MIT

FOSSA Status