Skip to content

Repository files navigation

chubbyts-undici-cors

CI Coverage Status Mutation testing badge npm-version

bugs code_smells coverage duplicated_lines_density ncloc sqale_rating alert_status reliability_rating security_rating sqale_index vulnerabilities

Description

A minimal cors middleware for chubbyts-undici-server.

Requirements

Installation

Through NPM as @chubbyts/chubbyts-undici-cors.

npm i @chubbyts/chubbyts-undici-cors@^1.3.0

Usage

import { createCorsMiddleware } from '@chubbyts/chubbyts-undici-cors/dist/middleware';
import {
  createAllowOriginRegex,
  createHeadersNegotiator,
  createMethodNegotiator,
  createOriginNegotiator,
} from '@chubbyts/chubbyts-undici-cors/dist/negotiation';
import { Handler, Response, ServerRequest } from '@chubbyts/chubbyts-undici-server/dist/server';

const corsMiddleware = createCorsMiddleware(
  createOriginNegotiator([createAllowOriginRegex(/^https?\:\/\/localhost(\:\d+)?$/)]),
  createMethodNegotiator(['GET', 'POST', 'PUT', 'DELETE']),
  createHeadersNegotiator(['Content-Type', 'Accept']),
);

const handler: Handler = async (serverRequest: ServerRequest) => {
  return new Response();
};

(async () => {
  const serverRequest = new ServerRequest();
  const response = await corsMiddleware(serverRequest, handler);
})();

Warning: When using createAllowOriginRegex, always anchor the pattern with ^ and $ and escape dots. An unanchored pattern like /example\.com/ also matches unintended origins such as https://evil-example.com or https://example.com.attacker.tld.

Copyright

2026 Dominik Zogg

About

A minimal cors middleware for chubbyts-undici-server.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages