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

najm-cors

v1.1.1

Published

Optional CORS plugin for Najm framework with global, controller-level, and route-level configuration

Readme

najm-cors

CORS (Cross-Origin Resource Sharing) plugin for Najm framework with support for global, controller-level, and route-level configuration.

Installation

bun add najm-cors

Quick Start

import { Server } from 'najm-core';
import { cors } from 'najm-cors';

new Server()
  .use(cors({ origin: 'https://example.com' }))
  .load(YourController)
  .listen(3000);

Usage

Global CORS Configuration

Apply CORS settings globally to all routes:

import { Server } from 'najm-core';
import { cors } from 'najm-cors';

new Server()
  .use(cors({
    origin: 'https://example.com',
    allowMethods: ['GET', 'POST', 'PUT', 'DELETE'],
    allowHeaders: ['Content-Type', 'Authorization'],
    credentials: true,
    maxAge: 86400
  }))
  .load(Controller)
  .listen(3000);

Default CORS

Enable CORS with default settings:

new Server()
  .use(cors(true))
  .load(Controller)
  .listen(3000);

Controller-Level CORS

Override global settings for an entire controller:

import { Controller, Get } from 'najm-core';
import { Cors } from 'najm-core';

@Controller('/api')
@Cors({ origin: 'https://admin.example.com' })
export class AdminController {
  @Get('/users')
  getUsers() {
    return { users: [] };
  }
}

Route-Level CORS

Override settings for specific routes:

@Controller('/api')
@Cors({ origin: 'https://admin.example.com' })
export class AdminController {
  @Get('/public')
  @Cors({ origin: '*' })
  publicData() {
    return { data: 'public' };
  }

  @Post('/private')
  @Cors({ disabled: true })
  privateData() {
    return { data: 'private' };
  }
}

Configuration Options

interface CorsOptions {
  origin?: string | string[];           // Origin URL(s) allowed ('*' for any)
  allowMethods?: string[];              // Allowed HTTP methods
  allowHeaders?: string[];              // Allowed request headers
  exposeHeaders?: string[];             // Headers exposed to client
  maxAge?: number;                      // Preflight cache time in seconds
  credentials?: boolean;                // Allow credentials
  preflight?: boolean;                  // Handle preflight requests
}

Default Configuration

{
  origin: 'http://localhost:3000',
  allowMethods: ['GET', 'HEAD', 'PUT', 'PATCH', 'POST', 'DELETE'],
  allowHeaders: ['Content-Type', 'Authorization'],
  exposeHeaders: ['Content-Type', 'Authorization'],
  credentials: true,
  preflight: true
}

Priority Resolution

CORS configuration is resolved in this order (highest to lowest priority):

  1. Route-level @Cors() decorator (specific method)
  2. Controller-level @Cors() decorator (all methods)
  3. Global plugin configuration via .use(cors(...))
  4. Default built-in configuration

Examples

Multiple Origins

cors({
  origin: ['https://app1.com', 'https://app2.com'],
  credentials: true
})

Wildcard with Custom Headers

cors({
  origin: '*',
  allowHeaders: ['Content-Type', 'X-Custom-Header'],
  exposeHeaders: ['X-Response-Header']
})

Disable CORS for Public Routes

@Controller('/public')
export class PublicController {
  @Get('/data')
  @Cors({ disabled: true })
  publicData() {
    return { data: 'public' };
  }
}

Architecture

The CORS plugin:

  1. Scans decorators on controllers and routes during the scan phase
  2. Configures global CORS middleware during the configure phase
  3. Registers route-specific CORS middleware during the activate phase
  4. Integrates with RoutesService via the INJECTIONS token for route-level middleware

Security Notes

  • Using wildcard origin (*) with credentials: true is not recommended and will log a warning
  • Always validate and restrict origins in production
  • Be cautious with allowHeaders - only allow necessary headers

License

MIT