Skip to content

Commit 8111048

Browse files
committed
perf: reduce bundle size by another 26 bytes
1 parent f19ce30 commit 8111048

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
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 `847 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 `821 bytes` added to your bundle).
44

55
## Installation
66

src/index.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ const { isArray } = Array;
2222

2323
const HEADERS_ = 'headers' as const;
2424

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);
27-
2825
interface StringArrayJoinWithComma {
2926
(arr: string[]): string,
3027
(arr: undefined | null): undefined,
@@ -85,37 +82,40 @@ export const createCors = ({
8582
const joinedAllowHeaders = stringArrayJoinWithComma(optsAllowHeaders);
8683

8784
return async (request: Request, response: Response): Promise<Response> => {
88-
const originHeaderValue = getHeader(request, ORIGIN) || '';
85+
const setHeader = (name: string, value: string) => response[HEADERS_].set(name, value);
86+
const getHeader = (name: string) => request[HEADERS_].get(name);
87+
88+
const originHeaderValue = getHeader(ORIGIN) || '';
8989
const allowOrigin = await findAllowOrigin(originHeaderValue);
9090
if (allowOrigin) {
91-
setHeader(response, ACCESS_CONTROL_PREFIX + ALLOW_PREFIX + ORIGIN, allowOrigin);
91+
setHeader(ACCESS_CONTROL_PREFIX + ALLOW_PREFIX + ORIGIN, allowOrigin);
9292
}
9393
// Suppose the server sends a response with an Access-Control-Allow-Origin value with an explicit origin (rather than the "*" wildcard).
9494
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
9595
if (shouldVaryIncludeOrigin) {
96-
setHeader(response, VARY, getHeader(request, VARY) /** existing Vary */ || ORIGIN);
96+
setHeader(VARY, getHeader(VARY) /** existing Vary */ || ORIGIN);
9797
}
9898
if (optsCredentials) {
99-
setHeader(response, ACCESS_CONTROL_PREFIX + ALLOW_PREFIX + 'Credentials', '' + optsCredentials);
99+
setHeader(ACCESS_CONTROL_PREFIX + ALLOW_PREFIX + 'Credentials', '' + optsCredentials);
100100
}
101101
if (exposeHeaders) {
102-
setHeader(response, ACCESS_CONTROL_PREFIX + 'Expose-' + HEADERS, exposeHeaders);
102+
setHeader(ACCESS_CONTROL_PREFIX + 'Expose-' + HEADERS, exposeHeaders);
103103
}
104104

105105
if (request.method === 'OPTIONS') {
106106
if (optsMaxAge != null) {
107-
setHeader(response, ACCESS_CONTROL_PREFIX + 'Max-Age', '' + optsMaxAge);
107+
setHeader(ACCESS_CONTROL_PREFIX + 'Max-Age', '' + optsMaxAge);
108108
}
109109

110110
const allowMethods = await findAllowMethods(originHeaderValue);
111111
if (allowMethods.length) {
112-
setHeader(response, ACCESS_CONTROL_PREFIX + ALLOW_PREFIX + 'Methods', stringArrayJoinWithComma(allowMethods));
112+
setHeader(ACCESS_CONTROL_PREFIX + ALLOW_PREFIX + 'Methods', stringArrayJoinWithComma(allowMethods));
113113
}
114114

115115
const ACCESS_CONTROL_REQUEST_HEADERS = ACCESS_CONTROL_PREFIX + 'Request-' + HEADERS;
116-
const allowHeader = joinedAllowHeaders || getHeader(request, ACCESS_CONTROL_REQUEST_HEADERS);
116+
const allowHeader = joinedAllowHeaders || getHeader(ACCESS_CONTROL_REQUEST_HEADERS);
117117
if (allowHeader) {
118-
setHeader(response, ACCESS_CONTROL_PREFIX + ALLOW_PREFIX + HEADERS, allowHeader);
118+
setHeader(ACCESS_CONTROL_PREFIX + ALLOW_PREFIX + HEADERS, allowHeader);
119119
response[HEADERS_].append(VARY, ACCESS_CONTROL_REQUEST_HEADERS);
120120
}
121121
}

0 commit comments

Comments
 (0)