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

@bklarjs/cors

v1.0.1

Published

CORS middleware for the bklar framework.

Downloads

19

Readme

@bklarjs/cors 🌐

NPM Version License: MIT Tests

Official CORS (Cross-Origin Resource Sharing) middleware for the bklar framework.

This package enables your bklar application to handle requests from different origins, which is essential for building modern web applications where the frontend and backend are hosted on separate domains.


✨ Features

  • 🧩 Simple Integration: Enable CORS for your entire application with a single line of code.
  • 🔧 Flexible Configuration: Easily configure allowed origins, methods, headers, and more to fit your security requirements.
  • ⚙️ Automatic Preflight Handling: Automatically handles complex OPTIONS (preflight) requests without any extra effort.
  • 🔒 Sensible Defaults: Comes with permissive defaults for a quick and easy setup during development.
  • 🛡️ Full TypeScript Support: Strongly-typed configuration options for a better development experience.

📦 Installation

This package is designed to work with bklar. You'll need both installed in your project.

bun add bklar @bklarjs/cors

🚀 Usage

The most common way to use this middleware is to apply it globally to your application.

1. Basic Usage (Development)

For local development, you can enable CORS with its default, permissive settings. This will allow requests from any origin.

import { Bklar } from "bklar";
import { cors } from "@bklarjs/cors";

const app = Bklar();

// Apply the CORS middleware globally
app.use(cors());

app.get("/", (ctx) => {
  return ctx.json({ message: "CORS is enabled!" });
});

app.listen(3000);

2. Production Configuration (Secure)

For production, you should always restrict which origins are allowed to access your API.

import { Bklar } from "bklar";
import { cors } from "@bklarjs/cors";

const app = Bklar();

app.use(
  cors({
    // Allow a specific frontend origin
    origin: "https://my-awesome-frontend.com",

    // Allowed HTTP methods
    methods: ["GET", "POST", "PUT", "DELETE"],

    // Allow the frontend to send credentials (e.g., cookies)
    credentials: true,

    // Cache preflight response for 1 day (in seconds)
    maxAge: 86400,
  })
);

app.get("/api/data", (ctx) => {
  return ctx.json({ data: "This is protected by CORS" });
});

app.listen(3000);

⚙️ Configuration Options

The cors() factory accepts an options object:

  • origin: (Required) Configures the Access-Control-Allow-Origin header.
    • true: Allows any origin.
    • string: Allows a single, specific origin (e.g., 'https://example.com').
    • string[]: Allows a list of specific origins.
    • RegExp: Allows origins matching a regular expression.
    • (string | RegExp)[]: Allows a mixed list of specific origins and regular expressions.
  • methods: An optional string or string[] of allowed HTTP methods. Defaults to 'GET,HEAD,PUT,PATCH,POST,DELETE'.
  • allowedHeaders: An optional string or string[] of allowed request headers.
  • exposedHeaders: An optional string or string[] to configure the Access-Control-Expose-Headers header. This allows the client-side code to access custom headers you set in your responses.
  • credentials: A boolean. If true, it sets the Access-Control-Allow-Credentials header. Defaults to false.
  • maxAge: A number that sets the Access-Control-Max-Age header in seconds. This specifies how long the results of a preflight request can be cached.

Example: Multiple Origins

You can easily allow multiple domains, which is useful for staging and production environments.

const allowedOrigins = [
  "https://my-awesome-frontend.com",
  "https://staging.my-awesome-frontend.com",
  /localhost:\d{4}$/, // Allow localhost on any 4-digit port
];

app.use(cors({ origin: allowedOrigins }));

🛠️ How It Works

This middleware inspects incoming requests for an Origin header.

  1. Preflight Requests (OPTIONS): If the request method is OPTIONS, the middleware checks if the origin is allowed. If so, it responds immediately with a 204 No Content status and the appropriate Access-Control-* headers, effectively ending the request cycle.
  2. Actual Requests (e.g., GET, POST): For other requests, the middleware checks the origin and, if allowed, attaches the appropriate Access-Control-Allow-Origin header to the final response sent by your route handler.

🧰 API Reference

This package exports the following:

  • cors(options: CorsOptions): Middleware: The main factory function that creates the CORS middleware.

🤝 Contributing

Contributions are welcome! Please open an issue or submit a Pull Request to the main bklar repository.

📄 License

This project is licensed under the MIT License.