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

@decoraxios/mock-ws

v0.5.0

Published

MSW-powered WebSocket mock package for Decoraxios

Downloads

100

Readme

@decoraxios/mock-ws

@decoraxios/mock-ws adds MSW-powered WebSocket mocking as a standalone package in the Decoraxios workspace.

It supports both a decorator-first declaration style and direct access to the native MSW ws API.

Install

npm install @decoraxios/mock-ws msw

Includes

  • MockWebSocketAPI
  • @WebSocketMock
  • @WebSocketState
  • withWebSocketState
  • withWebSocketMethodConfig
  • withWebSocketMethodGuards
  • @OnConnection
  • @OnClientMessage
  • @OnClientClose
  • @OnServerOpen
  • @OnServerMessage
  • @OnServerError
  • @OnServerClose
  • @WsAck
  • @WsError
  • @WsMessageType
  • @WsGuard
  • @WsJsonGuard
  • @WsJsonMatch
  • @WsJsonPath
  • @WsNamespace
  • @WsContext
  • @WsClient
  • @WsServer
  • @WsEvent
  • @WsData
  • @WsJsonData
  • @WsParams
  • @WsPathParam
  • @WsInfo
  • @WsProtocols
  • @WsState
  • @WsSend
  • @WsSendJson
  • @WsPatchState
  • ws

Decorator example

import {
  MockWebSocketAPI,
  OnClientMessage,
  OnConnection,
  WebSocketState,
  WebSocketMock,
  WsAck,
  WsError,
  WsGuard,
  WsJsonData,
  WsJsonGuard,
  WsJsonMatch,
  WsNamespace,
  WsMessageType,
  WsPatchState,
  WsPathParam,
  WsSendJson,
  WsState,
} from '@decoraxios/mock-ws';

@WebSocketMock('ws://localhost:3300/chat/:roomId')
@WebSocketState(() => ({ counters: { messages: 0 } }))
class ChatSocketMock {
  @OnConnection()
  @WsSendJson()
  welcome(@WsPathParam('roomId') roomId: string, @WsState() state: Record<string, unknown>) {
    state.roomId = roomId;
    return { roomId, type: 'welcome' };
  }

  @OnClientMessage()
  @WsMessageType('ping')
  @WsPatchState()
  recordPing(@WsState('counters.messages') count: number) {
    return { counters: { messages: count + 1 } };
  }

  @OnClientMessage()
  @WsMessageType('stats')
  @WsAck('stats-ok')
  stats(
    @WsJsonData('payload.request.id') requestId: string,
    @WsState('counters.messages') count: number,
    @WsState('roomId') roomId: string,
  ) {
    return { count, requestId, roomId, type: 'stats' };
  }

  @OnClientMessage()
  @WsMessageType('secure-stats')
  @WsGuard(context => Boolean((context.state.session as { authorized?: boolean } | undefined)?.authorized))
  @WsSendJson()
  secureStats(@WsState('counters.messages') count: number) {
    return { count, type: 'secure-stats' };
  }

  @OnClientMessage()
  @WsMessageType('auth')
  @WsJsonGuard(payload => typeof payload === 'object' && payload !== null && payload.token === 'letmein')
  @WsPatchState()
  authorize() {
    return { session: { authorized: true } };
  }

  @OnClientMessage()
  @WsNamespace('catalog')
  @WsJsonMatch({ meta: { phase: 'public' } })
  @WsMessageType('lookup')
  @WsAck('lookup-ok', { correlationPath: 'request.id', correlationKey: 'requestId', payloadKey: 'result' })
  lookup(@WsJsonData('payload.slug') slug: string) {
    return { slug, visibility: 'public' };
  }

  @OnClientMessage()
  @WsMessageType('explode')
  @WsError('stats-error')
  explode() {
    throw new Error('boom');
  }
}

MockWebSocketAPI.register(ChatSocketMock);

await MockWebSocketAPI.on();

const socket = new WebSocket('ws://localhost:3300/chat/alpha');
socket.addEventListener('message', event => {
  console.log(event.data);
});

Parameter decorators make the handler body feel closer to the rest of Decoraxios. You can inject the current client, server, event, raw message data, parsed JSON payload, path params, connection info, and per-connection state instead of manually unpacking a context object every time.

@WebSocketState(...) gives every connection a fresh state object, @WsPatchState() lets a handler merge returned data into that state, and @WsSend() / @WsSendJson() let a handler return the outbound message declaratively.

Handlers on the same socket connection are processed sequentially, so state updates remain deterministic even when the client sends multiple messages quickly.

@WsGuard(...) and @WsJsonGuard(...) let you express routing conditions at the decorator layer, which is useful when multiple handlers share the same event or message type but should react under different state or payload conditions.

@WsAck(...) and @WsError(...) add protocol-style success and failure envelopes on top of the same handler model, so you can express "stats-ok" / "stats-error" style contracts without repeating response boilerplate in every method.

@WsJsonMatch(...), @WsJsonPath(...), and @WsNamespace(...) make payload-based protocol routing read like a DSL instead of a chain of if statements.

Raw MSW example

import { MockWebSocketAPI, ws } from '@decoraxios/mock-ws';

MockWebSocketAPI.registerHandlers(
  ws.link('ws://localhost:3300/chat').addEventListener('connection', ({ client }) => {
    client.send(JSON.stringify({ type: 'welcome' }));
  }),
);

Node environments

MSW WebSocket mocking expects a WHATWG-compatible global WebSocket.

If your Node runtime does not expose globalThis.WebSocket, install or provide a WebSocket polyfill before calling MockWebSocketAPI.on().

Workspace example

Documentation