Skip to content

Commit 5f78f44

Browse files
committed
perf: reduce bundle size by another 16 bytes
1 parent 9fa3ca2 commit 5f78f44

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
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 `924 bytes` 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 `908 bytes` added to your bundle).
44

55
## Installation
66

src/index.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ const VARY = 'Vary';
1818
const ORIGIN = 'Origin';
1919
const HEADERS = 'Headers';
2020

21-
const setHeader = (response: Response, name: string, value: string) => response.headers.set(name, value);
22-
const getHeader = (request: Request, name: string) => request.headers.get(name);
21+
const { isArray } = Array;
22+
23+
const HEADERS_ = 'headers' as const;
24+
25+
const setHeader = (response: Response, name: string, value: string) => response[HEADERS_].set(name, value);
26+
const getHeader = (request: Request, name: string) => request[HEADERS_].get(name);
2327

2428
interface StringArrayJoinWithComma {
2529
(arr: string[]): string,
@@ -60,18 +64,18 @@ export const createCors = ({
6064
} else {
6165
findAllowOrigin = (origin: string) => (optsOrigin === origin ? origin : null);
6266
}
63-
} else if (typeof optsOrigin === 'function') {
64-
findAllowOrigin = optsOrigin;
65-
} else {
67+
} else if (isArray(optsOrigin)) {
6668
const allowedOrigins = new Set(optsOrigin);
6769
findAllowOrigin = (origin: string) => (allowedOrigins.has(origin) ? origin : null);
70+
} else {
71+
findAllowOrigin = optsOrigin;
6872
}
6973

7074
let findAllowMethods: (origin: string) => Promise<string[]> | string[];
71-
if (typeof optsAllowMethods === 'function') {
72-
findAllowMethods = optsAllowMethods;
73-
} else if (Array.isArray(optsAllowMethods)) {
75+
if (isArray(optsAllowMethods)) {
7476
findAllowMethods = () => optsAllowMethods;
77+
} else if (typeof optsAllowMethods === 'function') {
78+
findAllowMethods = optsAllowMethods;
7579
} else {
7680
findAllowMethods = () => [];
7781
}
@@ -118,14 +122,8 @@ export const createCors = ({
118122
const allowHeader = joinedAllowHeaders || getHeader(request, ACCESS_CONTROL_REQUEST_HEADERS);
119123
if (allowHeader) {
120124
setHeader(response, ACCESS_CONTROL_PREFIX + ALLOW_PREFIX + HEADERS, allowHeader);
121-
response.headers.append(VARY, ACCESS_CONTROL_REQUEST_HEADERS);
125+
response[HEADERS_].append(VARY, ACCESS_CONTROL_REQUEST_HEADERS);
122126
}
123-
124-
// return new Response(null, {
125-
// headers: c.res.headers,
126-
// status: 204,
127-
// statusText: 'No Content'
128-
// });
129127
}
130128

131129
return response;

0 commit comments

Comments
 (0)