@bit-js/bun-utils
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

bun-utils

An utilities library for Bun.

WebSocket router

A WebSocket router for Bun.

import { ws } from '@bit-js/bun-utils';

To create a route.

const route = ws.route<{ id: string }>({
    message(ws) {
        ws.send(ws.data.id);
    }
})

Use in a request handler.

function fetch(req: Request) {
    return route.upgrade(req, { 
        data: { id: performance.now() } 
    });
}

Serve the fetch handler (this step creates the server and bind all routes).

const server = ws.serve({
    // Bun.serve options
    server: { fetch },

    // Bun WebSocket options
    ws: { publishToSelf: true }
});

Or with an already created server instance without ws.serve.

ws.bind(server);

Readme

Keywords

none

Package Sidebar

Install

npm i @bit-js/bun-utils

Weekly Downloads

0

Version

1.0.3

License

MIT

Unpacked Size

28.1 kB

Total Files

7

Last publish

Collaborators

  • reve-node