Skip to content

Commit f77137f

Browse files
committed
perf: reduce bundle size by another 30 bytes
1 parent f7f0fd9 commit f77137f

File tree

2 files changed

+16
-24
lines changed

2 files changed

+16
-24
lines changed

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 `986 bytes` added to your bundle).
44

55
## Installation
66

src/index.ts

Lines changed: 15 additions & 23 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,13 +12,6 @@ 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';
@@ -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,7 +69,6 @@ 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> => {
8274
const originHeaderValue = request.headers.get(ORIGIN) || '';
@@ -92,11 +84,11 @@ export const createCors = (options?: CorsOptions) => {
9284
if (shouldVaryIncludeOrigin) {
9385
setHeader(response, VARY, request.headers.get(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,11 +100,11 @@ 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) {
118110
const requestHeaders = request.headers.get(ACCESS_CONTROL_REQUEST_HEADERS);

0 commit comments

Comments
 (0)