Skip to content

Commit c57a875

Browse files
committed
perf: reduce bundle size by another 39 bytes
1 parent 594259b commit c57a875

File tree

3 files changed

+9
-12
lines changed

3 files changed

+9
-12
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 `976 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 `937 bytes` added to your bundle).
44

55
## Installation
66

src/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe('merge-headers', () => {
6161

6262
expect(res.status).toBe(204);
6363
expect(res.headers.get('Access-Control-Allow-Methods')?.split(',')[0]).toBe('GET');
64-
expect(res.headers.get('Access-Control-Allow-Headers')?.split(',')).toEqual([
64+
expect(res.headers.get('Access-Control-Allow-Headers')?.split(',').map(h => h.trim())).toEqual([
6565
'X-PINGOTHER',
6666
'Content-Type'
6767
]);

src/index.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export const createCors = ({
5050
let findAllowOrigin: (origin: string) => Promise<string | undefined | null> | string | undefined | null;
5151
if (typeof optsOrigin === 'string') {
5252
if (optsOrigin === '*') {
53-
findAllowOrigin = () => '*';
53+
findAllowOrigin = () => optsOrigin;
5454
} else {
5555
findAllowOrigin = (origin: string) => (optsOrigin === origin ? origin : null);
5656
}
@@ -106,16 +106,13 @@ export const createCors = ({
106106
setHeader(response, ACCESS_CONTROL_PREFIX + ALLOW_PREFIX + 'Methods', stringArrayJoinWithComma(allowMethods));
107107
}
108108

109-
let headers = optsAllowHeaders;
110109
const ACCESS_CONTROL_REQUEST_HEADERS = ACCESS_CONTROL_PREFIX + 'Request-' + HEADERS;
111-
if (!headers?.length) {
112-
const requestHeaders = getHeader(request, ACCESS_CONTROL_REQUEST_HEADERS);
113-
if (requestHeaders) {
114-
headers = requestHeaders.split(/\s*,\s*/);
115-
}
116-
}
117-
if (headers?.length) {
118-
setHeader(response, ACCESS_CONTROL_PREFIX + ALLOW_PREFIX + HEADERS, stringArrayJoinWithComma(headers));
110+
const allowHeader = optsAllowHeaders?.length
111+
? stringArrayJoinWithComma(optsAllowHeaders)
112+
: getHeader(request, ACCESS_CONTROL_REQUEST_HEADERS);
113+
114+
if (allowHeader) {
115+
setHeader(response, ACCESS_CONTROL_PREFIX + ALLOW_PREFIX + HEADERS, allowHeader);
119116
response.headers.append(VARY, ACCESS_CONTROL_REQUEST_HEADERS);
120117
}
121118

0 commit comments

Comments
 (0)