Skip to content

Commit 0d105ba

Browse files
committed
perf: reduce bundle size by another 100 bytes
1 parent 64ff186 commit 0d105ba

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
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.3 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 `1.19 KiB` added to your bundle).
44

55
## Installation
66

src/index.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ const defaults: CorsOptions = {
2020
};
2121

2222
const ACCESS_CONTROL_PREFIX = 'Access-Control-';
23+
const ALLOW_PREFIX = 'Allow-';
2324
const VARY = 'Vary';
25+
const ORIGIN = 'Origin';
26+
const HEADERS = 'Headers';
27+
28+
function setHeader(response: Response, name: string, value: string) {
29+
return response.headers.set(name, value);
30+
}
2431

2532
/**
2633
* A very simple CORS implementation for using in simple serverless workers
@@ -73,55 +80,50 @@ export function createCors(options?: CorsOptions) {
7380
const shouldVaryIncludeOrigin = optsOrigin !== '*';
7481

7582
return async function simpleCors(request: Request, response: Response): Promise<Response> {
76-
const originHeaderValue = request.headers.get('Origin') || '';
83+
const originHeaderValue = request.headers.get(ORIGIN) || '';
7784
let allowOrigin = findAllowOrigin(originHeaderValue);
7885
if (allowOrigin && typeof allowOrigin === 'object' && 'then' in allowOrigin) {
7986
allowOrigin = await allowOrigin;
8087
}
8188
if (allowOrigin) {
82-
response.headers.set(ACCESS_CONTROL_PREFIX + 'Allow-Origin', allowOrigin);
89+
setHeader(response, ACCESS_CONTROL_PREFIX + ALLOW_PREFIX + ORIGIN, allowOrigin);
8390
}
8491
// Suppose the server sends a response with an Access-Control-Allow-Origin value with an explicit origin (rather than the "*" wildcard).
8592
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
8693
if (shouldVaryIncludeOrigin) {
87-
const existingVary = request.headers.get(VARY);
88-
89-
if (existingVary) {
90-
response.headers.set(VARY, existingVary);
91-
} else {
92-
response.headers.set(VARY, 'Origin');
93-
}
94+
setHeader(response, VARY, request.headers.get(VARY) /** existing Vary */ || ORIGIN);
9495
}
9596
if (opts.credentials) {
96-
response.headers.set(ACCESS_CONTROL_PREFIX + 'Allow-Credentials', 'true');
97+
setHeader(response, ACCESS_CONTROL_PREFIX + ALLOW_PREFIX + 'Credentials', 'true');
9798
}
9899
if (opts.exposeHeaders?.length) {
99-
response.headers.set(ACCESS_CONTROL_PREFIX + 'Expose-Headers', fastStringArrayJoin(opts.exposeHeaders, ','));
100+
setHeader(response, ACCESS_CONTROL_PREFIX + 'Expose-' + HEADERS, fastStringArrayJoin(opts.exposeHeaders, ','));
100101
}
101102

102103
let allowMethods = findAllowMethods(originHeaderValue);
103104
if ('then' in allowMethods) {
104105
allowMethods = await allowMethods;
105106
}
106107
if (allowMethods.length) {
107-
response.headers.set(ACCESS_CONTROL_PREFIX + 'Allow-Methods', fastStringArrayJoin(allowMethods, ','));
108+
setHeader(response, ACCESS_CONTROL_PREFIX + ALLOW_PREFIX + 'Methods', fastStringArrayJoin(allowMethods, ','));
108109
}
109110

110111
if (request.method === 'OPTIONS') {
111112
if (opts.maxAge != null) {
112-
response.headers.set(ACCESS_CONTROL_PREFIX + 'Max-Age', opts.maxAge.toString());
113+
setHeader(response, ACCESS_CONTROL_PREFIX + 'Max-Age', '' + opts.maxAge);
113114
}
114115

115116
let headers = opts.allowHeaders;
117+
const ACCESS_CONTROL_REQUEST_HEADERS = ACCESS_CONTROL_PREFIX + 'Request-' + HEADERS;
116118
if (!headers?.length) {
117-
const requestHeaders = request.headers.get(ACCESS_CONTROL_PREFIX + 'Request-Headers');
119+
const requestHeaders = request.headers.get(ACCESS_CONTROL_REQUEST_HEADERS);
118120
if (requestHeaders) {
119121
headers = requestHeaders.split(/\s*,\s*/);
120122
}
121123
}
122124
if (headers?.length) {
123-
response.headers.set(ACCESS_CONTROL_PREFIX + 'Allow-Headers', fastStringArrayJoin(headers, ','));
124-
response.headers.append(VARY, ACCESS_CONTROL_PREFIX + 'Request-Headers');
125+
setHeader(response, ACCESS_CONTROL_PREFIX + ALLOW_PREFIX + HEADERS, fastStringArrayJoin(headers, ','));
126+
response.headers.append(VARY, ACCESS_CONTROL_REQUEST_HEADERS);
125127
}
126128

127129
// return new Response(null, {

0 commit comments

Comments
 (0)