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

@openknot-ts/knot-http-bun

v0.0.6-preview

Published

Native Bun HTTP server lifecycle and draining for OpenKnot applications.

Readme

@openknot-ts/knot-http-bun

A native Bun.serve host with Wire lifecycle, environment configuration and graceful draining. Protocols implement its Fetch handler contract; the host has no protocol, health or Node HTTP dependency.

Usage

import { http, httpServer } from "@openknot-ts/knot-http-bun";
import { httpHandler } from "@openknot-ts/knot-http-bun/handler";
import { defineApplication, providePlugs, runApplication, unit } from "@openknot-ts/wire";

const handler = unit({
  provides: httpHandler,
  factory: () => (request) => Response.json({ path: new URL(request.url).pathname }),
});

const app = await runApplication(defineApplication({ modules: [providePlugs([handler]), http] }));
console.log(app.resolve(httpServer).url);
await app.stopped;

API

| Export | Description | | --- | --- | | http | Ready module listing httpServer. | | httpServer | The default host resource. Resolves to HttpServer<Data>: { native, url, listening }. | | bunHttpServer({ handler, requirements?, options? }) | Declares another host with native BunHttpOptions<Data, Route> (TLS, routes, WebSockets, Unix sockets). | | httpConfig | The default host's environment configuration. | | httpHandler, HttpHandler<Data> | Contract for the fallback fetch handler; it receives the native Request and Server<Data>. | | httpMiddleware, HttpMiddleware | Collection contract for (request, server, next) middleware. |

The handler and middleware contracts are also exported from @openknot-ts/knot-http-bun/handler, without the host.

Configuration

| Variable | Default | Meaning | | --- | --- | --- | | PORT | 3000 | TCP port; 0 picks a free port. | | HTTP_HOST | 127.0.0.1 | Bind address. | | HTTP_IDLE_TIMEOUT_SECONDS | Bun default | 0–255; 0 disables the inactivity timeout. | | HTTP_MAX_REQUEST_BODY_SIZE | Bun default | Maximum request body in bytes. |

Precedence: environment, then an options object, then the defaults above. development defaults to false. Combining unix with an explicit PORT or HTTP_HOST is rejected.

Custom hosts

An options object keeps the default environment tuning. An options(dependencies) callback replaces it entirely: httpConfig is not read, and the callback returns native options from the application's own validated config.

const settings = config({
  API_PORT: z.coerce.number().int().min(0).max(65_535).default(8080),
  API_HOST: z.string().default("127.0.0.1"),
});

const server = bunHttpServer({
  handler: httpHandler,
  requirements: { settings },
  options: ({ settings }) => ({ port: settings.API_PORT, hostname: settings.API_HOST, idleTimeout: 0 }),
});

Select it with providePlugs([server]) instead of http. Route and WebSocket data types are inferred from the options:

const socketHandler = unit({
  factory: (): HttpHandler<{ userId: string }> => (request, server) => {
    if (server.upgrade(request, { data: { userId: "example" } })) return;
    return new Response("Upgrade required", { status: 426 });
  },
});

const socketServer = bunHttpServer({
  handler: socketHandler,
  options: {
    websocket: {
      message(socket, message) {
        socket.send(`${socket.data.userId}: ${message}`);
      },
    },
  },
});

Middleware

Units that provide httpMiddleware run in module order and return a response or next()'s result. Middleware wraps the fallback fetch; native routes bypass it. A middleware reached only as a dependency does not join. @openknot-ts/knot-health-bun serves the health probes this way.

Lifecycle

  • A handler or middleware that throws fails only its own request: the host hands the failure to unattendedFailures, which logs it and reports it when a reporter is bound, and answers 500 unless a native error option answers it. A handler answers the failures of the executions it invokes, as trpcHttp and restRouter do: Flow has already logged and reported them, so one it lets through is logged and reported twice.
  • Bun.serve() listens when constructed, inside the resource's initialize, so native is available from then on. Missing handlers and invalid configuration fail before acquisition; bind failures and cancelled startups unwind.
  • url is the bound URL with a trailing slash; build paths with new URL("/path", server.url).
  • app.stop() awaits native server.stop(), which waits for active responses. Open WebSockets are closed with 1001 (going away). Other indefinite streams end in shutdown tasks.
  • Bun's idle timeout still applies to requests and streams; see Bun's server documentation.

Examples and tests

Requires Bun 1.4.4+ next to Node and pnpm.

pnpm build
pnpm --filter @openknot-ts/knot-http-bun test
pnpm --filter @openknot-ts/knot-http-bun example