Skip to content

Commit 6438369

Browse files
committed
perf: reduce bundle size by another 36 bytes
1 parent f7f0fd9 commit 6438369

2 files changed

Lines changed: 20 additions & 28 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# cors-edge
22

3-
You are writing a very simple functions that runs on edge (either on Cloudflare Workers, Fastly Edge Compute, Vercel Edge Functions, etc.) consisting of less than 100 lines of code. You want to enable CORS (Cross-Origin Resource Sharing) for your endpoint but you do not want to pull in a heavy framework with middlewares just for that. This platform-agnostic package provides a simple way to add CORS support with minimal footprint (with only `1.06 KiB` added to your bundle).
3+
You are writing a very simple functions that runs on edge (either on Cloudflare Workers, Fastly Edge Compute, Vercel Edge Functions, etc.) consisting of less than 100 lines of code. You want to enable CORS (Cross-Origin Resource Sharing) for your endpoint but you do not want to pull in a heavy framework with middlewares just for that. This platform-agnostic package provides a simple way to add CORS support with minimal footprint (with only `980 bytes` added to your bundle).
44

55
## Installation
66

src/index.ts

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable antfu/top-level-function -- bundle size hack */
22

33
export interface CorsOptions {
4-
origin:
4+
origin?:
55
| string
66
| string[]
77
| ((origin: string) => Promise<string | undefined | null> | string | undefined | null),
@@ -12,21 +12,14 @@ export interface CorsOptions {
1212
exposeHeaders?: string[]
1313
};
1414

15-
const defaults: CorsOptions = {
16-
origin: '*',
17-
allowMethods: ['GET', 'HEAD', 'PUT', 'POST', 'DELETE', 'PATCH'],
18-
allowHeaders: [],
19-
exposeHeaders: []
20-
};
21-
2215
const ACCESS_CONTROL_PREFIX = 'Access-Control-';
2316
const ALLOW_PREFIX = 'Allow-';
2417
const VARY = 'Vary';
2518
const ORIGIN = 'Origin';
2619
const HEADERS = 'Headers';
2720

2821
const setHeader = (response: Response, name: string, value: string) => response.headers.set(name, value);
29-
22+
const getHeader = (request: Request, name: string) => request.headers.get(name);
3023
/**
3124
* A very simple CORS implementation for using in simple serverless workers
3225
*
@@ -44,14 +37,15 @@ const setHeader = (response: Response, name: string, value: string) => response.
4437
* }
4538
* ```
4639
*/
47-
export const createCors = (options?: CorsOptions) => {
48-
const opts = {
49-
...defaults,
50-
...options
51-
};
52-
40+
export const createCors = ({
41+
origin: optsOrigin = '*',
42+
allowMethods: optsAllowMethods = ['GET', 'HEAD', 'PUT', 'POST', 'DELETE', 'PATCH'],
43+
allowHeaders: optsAllowHeaders,
44+
maxAge: optsMaxAge,
45+
credentials: optsCredentials = false,
46+
exposeHeaders: optsExposeHeaders
47+
}: CorsOptions = {}) => {
5348
let findAllowOrigin: (origin: string) => Promise<string | undefined | null> | string | undefined | null;
54-
const optsOrigin = opts.origin;
5549
if (typeof optsOrigin === 'string') {
5650
if (optsOrigin === '*') {
5751
findAllowOrigin = () => '*';
@@ -66,7 +60,6 @@ export const createCors = (options?: CorsOptions) => {
6660
}
6761

6862
let findAllowMethods: (origin: string) => Promise<string[]> | string[];
69-
const optsAllowMethods = opts.allowMethods;
7063
if (typeof optsAllowMethods === 'function') {
7164
findAllowMethods = optsAllowMethods;
7265
} else if (Array.isArray(optsAllowMethods)) {
@@ -76,10 +69,9 @@ export const createCors = (options?: CorsOptions) => {
7669
}
7770

7871
const shouldVaryIncludeOrigin = optsOrigin !== '*';
79-
const exposeHeaders = opts.exposeHeaders || [];
8072

8173
return async (request: Request, response: Response): Promise<Response> => {
82-
const originHeaderValue = request.headers.get(ORIGIN) || '';
74+
const originHeaderValue = getHeader(request, ORIGIN) || '';
8375
let allowOrigin = findAllowOrigin(originHeaderValue);
8476
if (allowOrigin && typeof allowOrigin === 'object' && 'then' in allowOrigin) {
8577
allowOrigin = await allowOrigin;
@@ -90,13 +82,13 @@ export const createCors = (options?: CorsOptions) => {
9082
// Suppose the server sends a response with an Access-Control-Allow-Origin value with an explicit origin (rather than the "*" wildcard).
9183
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
9284
if (shouldVaryIncludeOrigin) {
93-
setHeader(response, VARY, request.headers.get(VARY) /** existing Vary */ || ORIGIN);
85+
setHeader(response, VARY, getHeader(request, VARY) /** existing Vary */ || ORIGIN);
9486
}
95-
if (opts.credentials) {
87+
if (optsCredentials) {
9688
setHeader(response, ACCESS_CONTROL_PREFIX + ALLOW_PREFIX + 'Credentials', 'true');
9789
}
98-
if (exposeHeaders.length) {
99-
setHeader(response, ACCESS_CONTROL_PREFIX + 'Expose-' + HEADERS, exposeHeaders.join(','));
90+
if (optsExposeHeaders?.length) {
91+
setHeader(response, ACCESS_CONTROL_PREFIX + 'Expose-' + HEADERS, optsExposeHeaders.join(','));
10092
}
10193

10294
let allowMethods = findAllowMethods(originHeaderValue);
@@ -108,14 +100,14 @@ export const createCors = (options?: CorsOptions) => {
108100
}
109101

110102
if (request.method === 'OPTIONS') {
111-
if (opts.maxAge != null) {
112-
setHeader(response, ACCESS_CONTROL_PREFIX + 'Max-Age', '' + opts.maxAge);
103+
if (optsMaxAge != null) {
104+
setHeader(response, ACCESS_CONTROL_PREFIX + 'Max-Age', '' + optsMaxAge);
113105
}
114106

115-
let headers = opts.allowHeaders;
107+
let headers = optsAllowHeaders;
116108
const ACCESS_CONTROL_REQUEST_HEADERS = ACCESS_CONTROL_PREFIX + 'Request-' + HEADERS;
117109
if (!headers?.length) {
118-
const requestHeaders = request.headers.get(ACCESS_CONTROL_REQUEST_HEADERS);
110+
const requestHeaders = getHeader(request, ACCESS_CONTROL_REQUEST_HEADERS);
119111
if (requestHeaders) {
120112
headers = requestHeaders.split(/\s*,\s*/);
121113
}

0 commit comments

Comments
 (0)