A minimal cors middleware for chubbyts-undici-server.
- node: 22
- @chubbyts/chubbyts-undici-server: ^1.2.0
Through NPM as @chubbyts/chubbyts-undici-cors.
npm i @chubbyts/chubbyts-undici-cors@^1.3.0import { 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.
2026 Dominik Zogg