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 🙏

© 2026 – Pkg Stats / Ryan Hefner

ws-switch

v1.2.0

Published

Switches incomming WebSocket-requests

Readme

ws-switch

Simple switch for websocket requests

npm install --save ws-switch

WS-Switch is designed for the ws module but works with an WebSocket-library that supports the connection event.
WS-Switch allows you to create your own server and simplify the switching of new connections.

Usage

const WS = require('ws');
const WSSwitch = require('ws-switch');

let server = new WS.Server({ port: 1234 });
let wsSwitch = new WSSwitch(server);
//You can also ommit the server and pass new ws-objects directly to wsSwitch.switchRequest
//But notice: As ws removed the upgradeReq-attribute you either must commit it to the switchRequest-method
//or place it manually as upgradeReq into the ws-object.


wsSwitch.for('/', (ws) => //Handles requests matching string '/'
{

});

wsSwitch.for(/^\/foo/, (ws) => 	//Handles requests matching regex ^/foo
								//which includes every string starting with /foo
								//even '/foo/bar' and '/foobar'
{
	
});

let handler404 =
{
	handle: function(ws)
	{
		/* Send not-found message */
	}
};

wsSwitch.for({ hostname: 'example.com' }, handler404); //Handles every request for example.com

wsSwitch.for({ hostname: '.+\.example.com', pathname: /^/ }, handler404); //Handles every request for *.example.com

wsSwitch.for(/^/, handler404); //Handles every request

Handlers are processed sequentially, so handler 3 will be called only if handler 1 and 2 don't match

Examples

  • /
    Handler 1
  • /foo
    Handler 2
  • /foo/bar/abc
    Handler 2
  • /foobar/abc
    Handler 2
  • /barfoo
    Handler 3
  • /abc
    Handler 3

Exceptions

If the handler-function returns a Promise, WS-Switch will catch exceptions in that Promise and end the connection.


wsSwitch.on(/^\/foo/, async (ws) =>
{
	let foo = await something();

	throw new Error();
	//WebSocket will be terminated
}

API

new WSSwitch(server)

arguments

  • server: object
    Any object compatible to ws.Server If ommited, you have to pass websockets and their upgradeRequests manually via the switchRequest-method

returns

  • WSSwitch: object

WSSwitch.for(pathname, onConnection)

arguments

  • path: string or RegExp
  • onConnection: function(websocket)

returns

  • this

WSSwitch.for(path, onConnection)

arguments

  • path: object
    • pathname | path: string or RegExp
    • hostname | host: string or RegExp
    • port: int
  • onConnection: function(websocket)

returns

  • this

WSSwitch.addHandler(pathname, onConnection)

Same as WSSwitch.for

WSSwitch.addHandler(path, onConnection)

Same as WSSwitch.for

WSSwitch.switchRequest(websocket, upgradeRequest)

If a WebSocket-Server is given in constructor, this method is called automatically

arguments

  • websocket: WebSocket
  • upgradeRequest: HTTP.IncommingMessage

returns

  • handler: object