Skip to content

Commit 64ff186

Browse files
committed
perf: reduce bundle size by 110 bytes
1 parent 066e59c commit 64ff186

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
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.4 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.3 KiB` added to your bundle).
44

55
## Installation
66

src/index.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ const defaults: CorsOptions = {
1919
exposeHeaders: []
2020
};
2121

22+
const ACCESS_CONTROL_PREFIX = 'Access-Control-';
23+
const VARY = 'Vary';
24+
2225
/**
2326
* A very simple CORS implementation for using in simple serverless workers
2427
*
@@ -70,54 +73,55 @@ export function createCors(options?: CorsOptions) {
7073
const shouldVaryIncludeOrigin = optsOrigin !== '*';
7174

7275
return async function simpleCors(request: Request, response: Response): Promise<Response> {
73-
let allowOrigin = findAllowOrigin(request.headers.get('Origin') || '');
76+
const originHeaderValue = request.headers.get('Origin') || '';
77+
let allowOrigin = findAllowOrigin(originHeaderValue);
7478
if (allowOrigin && typeof allowOrigin === 'object' && 'then' in allowOrigin) {
7579
allowOrigin = await allowOrigin;
7680
}
7781
if (allowOrigin) {
78-
response.headers.set('Access-Control-Allow-Origin', allowOrigin);
82+
response.headers.set(ACCESS_CONTROL_PREFIX + 'Allow-Origin', allowOrigin);
7983
}
8084
// Suppose the server sends a response with an Access-Control-Allow-Origin value with an explicit origin (rather than the "*" wildcard).
8185
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
8286
if (shouldVaryIncludeOrigin) {
83-
const existingVary = request.headers.get('Vary');
87+
const existingVary = request.headers.get(VARY);
8488

8589
if (existingVary) {
86-
response.headers.set('Vary', existingVary);
90+
response.headers.set(VARY, existingVary);
8791
} else {
88-
response.headers.set('Vary', 'Origin');
92+
response.headers.set(VARY, 'Origin');
8993
}
9094
}
9195
if (opts.credentials) {
92-
response.headers.set('Access-Control-Allow-Credentials', 'true');
96+
response.headers.set(ACCESS_CONTROL_PREFIX + 'Allow-Credentials', 'true');
9397
}
9498
if (opts.exposeHeaders?.length) {
95-
response.headers.set('Access-Control-Expose-Headers', fastStringArrayJoin(opts.exposeHeaders, ','));
99+
response.headers.set(ACCESS_CONTROL_PREFIX + 'Expose-Headers', fastStringArrayJoin(opts.exposeHeaders, ','));
96100
}
97101

98-
let allowMethods = findAllowMethods(request.headers.get('origin') || '');
102+
let allowMethods = findAllowMethods(originHeaderValue);
99103
if ('then' in allowMethods) {
100104
allowMethods = await allowMethods;
101105
}
102106
if (allowMethods.length) {
103-
response.headers.set('Access-Control-Allow-Methods', fastStringArrayJoin(allowMethods, ','));
107+
response.headers.set(ACCESS_CONTROL_PREFIX + 'Allow-Methods', fastStringArrayJoin(allowMethods, ','));
104108
}
105109

106110
if (request.method === 'OPTIONS') {
107111
if (opts.maxAge != null) {
108-
response.headers.set('Access-Control-Max-Age', opts.maxAge.toString());
112+
response.headers.set(ACCESS_CONTROL_PREFIX + 'Max-Age', opts.maxAge.toString());
109113
}
110114

111115
let headers = opts.allowHeaders;
112116
if (!headers?.length) {
113-
const requestHeaders = request.headers.get('Access-Control-Request-Headers');
117+
const requestHeaders = request.headers.get(ACCESS_CONTROL_PREFIX + 'Request-Headers');
114118
if (requestHeaders) {
115119
headers = requestHeaders.split(/\s*,\s*/);
116120
}
117121
}
118122
if (headers?.length) {
119-
response.headers.set('Access-Control-Allow-Headers', fastStringArrayJoin(headers, ','));
120-
response.headers.append('Vary', 'Access-Control-Request-Headers');
123+
response.headers.set(ACCESS_CONTROL_PREFIX + 'Allow-Headers', fastStringArrayJoin(headers, ','));
124+
response.headers.append(VARY, ACCESS_CONTROL_PREFIX + 'Request-Headers');
121125
}
122126

123127
// return new Response(null, {

0 commit comments

Comments
 (0)