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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@cjkihl/bun-cors

v2.0.7

Published

Cors middleware for bun

Readme

@cjkihl/bun-cors

A lightweight CORS middleware for Bun server applications. This package provides a simple and efficient way to handle Cross-Origin Resource Sharing (CORS) in your Bun applications.

Installation

bun add @cjkihl/bun-cors

Quick Start

import { cors } from "@cjkihl/bun-cors";

const PORT = process.env.PORT || 4000;

Bun.serve({
  fetch: cors(
    async (req) => {
      return new Response("Hello from Bun!", { status: 200 });
    },
    {
      origin: ["http://localhost:3000", "https://myapp.com"],
      methods: ["GET", "POST", "PUT", "DELETE"],
      credentials: true,
    }
  ),
  port: PORT,
});

API

cors(handler, options?)

Creates a CORS-enabled request handler.

Parameters

  • handler (Function): Your request handler function that takes a Request and returns a Promise<Response>
  • options (CorsOptions, optional): CORS configuration options

Returns

A new request handler function that automatically handles CORS headers and preflight requests.

Configuration Options

origin

Specifies which origins are allowed to access the resource.

  • Type: string | (string | RegExp)[]
  • Default: "*" (allows all origins)
// Allow specific origins
origin: ["http://localhost:3000", "https://myapp.com"]

// Allow all origins (default)
origin: "*"

// Use regex patterns
origin: [/^https:\/\/.*\.myapp\.com$/, "http://localhost:3000"]

methods

Specifies which HTTP methods are allowed.

  • Type: string[]
  • Default: ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"]
methods: ["GET", "POST", "PUT", "DELETE"]

headers

Specifies which headers can be used in the actual request.

  • Type: string[]
  • Default: ["Content-Type", "Authorization"]
headers: ["Content-Type", "Authorization", "X-Custom-Header"]

maxAge

Specifies how long (in seconds) the results of a preflight request can be cached.

  • Type: number
  • Default: 3600 (1 hour)
maxAge: 86400 // 24 hours

credentials

Specifies whether the request can include user credentials like cookies, authorization headers, or TLS client certificates.

  • Type: boolean
  • Default: false
credentials: true

Note: When credentials is true, the origin cannot be "*" and must be a specific origin or array of origins.

Examples

Basic Usage

import { cors } from "@cjkihl/bun-cors";

Bun.serve({
  fetch: cors(async (req) => {
    return new Response("Hello World!", { status: 200 });
  }),
  port: 3000,
});

Custom Configuration

import { cors } from "@cjkihl/bun-cors";

Bun.serve({
  fetch: cors(
    async (req) => {
      const data = { message: "Hello from API!", timestamp: Date.now() };
      return new Response(JSON.stringify(data), {
        headers: { "Content-Type": "application/json" },
        status: 200,
      });
    },
    {
      origin: ["http://localhost:3000", "https://myapp.com"],
      methods: ["GET", "POST"],
      headers: ["Content-Type", "Authorization", "X-API-Key"],
      credentials: true,
      maxAge: 7200, // 2 hours
    }
  ),
  port: 4000,
});

With Authentication

import { cors } from "@cjkihl/bun-cors";

Bun.serve({
  fetch: cors(
    async (req) => {
      // Your authentication logic here
      const authHeader = req.headers.get("Authorization");
      if (!authHeader) {
        return new Response("Unauthorized", { status: 401 });
      }

      return new Response("Authenticated response", { status: 200 });
    },
    {
      origin: "https://myapp.com",
      credentials: true,
      headers: ["Content-Type", "Authorization"],
    }
  ),
  port: 5000,
});

Development vs Production

import { cors } from "@cjkihl/bun-cors";

const isDevelopment = process.env.NODE_ENV === "development";

Bun.serve({
  fetch: cors(
    async (req) => {
      return new Response("API Response", { status: 200 });
    },
    {
      origin: isDevelopment 
        ? ["http://localhost:3000", "http://localhost:3001"]
        : ["https://myapp.com", "https://admin.myapp.com"],
      credentials: true,
      methods: ["GET", "POST", "PUT", "DELETE"],
    }
  ),
  port: 4000,
});

Features

  • Lightweight: Minimal overhead and dependencies
  • TypeScript Support: Full type definitions included
  • Flexible Origin Matching: Support for exact strings and regex patterns
  • Automatic Preflight Handling: Handles OPTIONS requests automatically
  • Credentials Support: Full support for cookies and authentication headers
  • Configurable Headers: Customizable allowed headers and methods
  • Caching Control: Configurable preflight response caching

License

MIT