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

@nex-app/bun-server

v1.2.10

Published

A simple server wrapper for the Bun.js webserver to give a more express-like experience.

Downloads

697

Readme

Bun Server

This is a pretty small and lightweight abstraction from the built-in Bun webserver. Currently, it handles routing and websockets (what I need for my own purposes right now).

You can enable CORS headers by passing them into the globalHeaders property of the createServer function.

If you'd like state that's available for any request objects, you can add it to the state property.

The request object:

request: The original request object that was passed into the native Bun server.

params: Contains the following - query: an object containing the key/value pairs of the querystring. - body: either an object or string of the JSON request or an object containing a single property text containing the plain text of the request body - path: an object containing the key/value pairs of the path variables eg. /foo/:bar will return { bar: 'whatever bar was' }`

state: Anything you have set in the state object upon server creation. I believe this is currently mutable so be careful if you are assigning values here.

The response object:

setStatus(status: number): Sets the HTTP status code to respond with

setHeader(k: string, v:string): Sets a header for the response

send(responseTextOrObject: string): Send the response to the client.

The res.send() method will automatically send the response as JSON if you pass an object in.

req.body will try to parse the input as an object if the request header content type is application/json.

Example Usage

import { createServer } from '..';

const app = createServer({
	port: 3222,
	globalHeaders: {
		'Access-Control-Allow-Origin': '*',
	},
	state: {
		authenticate: () => {
			console.log('authenticate!');
		},
		db: () => {
			console.log('db!');
		},
	},
	webSocket: {
		path: '/ws', // localhost:3222/ws from the client
		onUpgrade: (req) => {
			console.log(
				'you can upgrade here, whatever you return will be attached to socket.data'
			);
			return {
				userId: '1234',
			};
		},
		onConnected: (socket) => {
			console.log('A socket was connected');
		},
		onMessage: (socket, message) => {
			// 1234 set in the upgrade function. You can sessionize like this or just emit to select clients rather than everyone on the socket using this
			console.log(socket.data.userId);
			console.log('a message was received', message);
			// echo back:
			socket.send(message);
		},
		onClose: (socket) => {
			console.log('socket connection was closed');
		},
	},
	debug: true,
});

app.get('/hello', (req, res) => {
	const user = req.state.authenticate();
	const db = req.state.db();
	console.log(req.params.query);
	res.setStatus(400);
	res.setHeader('custom', 'custom value');
	return res.send({
		message: 'Hello World',
	});
});

app.get('/hello/:id', (req, res) => {
	return res.send({
		message: 'Hello World with param',
		params: req.params,
	});
});

app.get('/hello/:id/configure/:name', (req, res) => {
	return res.send({
		message: 'Hello World with param',
		params: req.params,
	});
});

app.get('/text', (req, res) => {
	return res.send('text content');
});

app.onError((err) => {
	console.log('error handler', err.message);
	return new Response('error', { status: err.status || 500 });
});

const server = app.start();

console.log('server started', server.url.host);