Skip to content

Commit ea17fca

Browse files
committed
fix cors issue
1 parent 1d3f505 commit ea17fca

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

apps/server/src/http.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
HttpBody,
66
HttpClient,
77
HttpClientResponse,
8+
HttpMiddleware,
89
HttpRouter,
910
HttpServerResponse,
1011
HttpServerRequest,
@@ -84,7 +85,11 @@ export function isAllowedBrowserApiCorsOrigin(origin: string | undefined): boole
8485
return false;
8586
}
8687

87-
const [a, b] = octets;
88+
const a = octets[0];
89+
const b = octets[1];
90+
if (a === undefined || b === undefined) {
91+
return false;
92+
}
8893
if (a === 10) {
8994
return true;
9095
}
@@ -102,13 +107,16 @@ export function isAllowedBrowserApiCorsOrigin(origin: string | undefined): boole
102107
return false;
103108
}
104109

105-
export const browserApiCorsLayer = HttpRouter.cors({
106-
allowedMethods: ["GET", "POST", "OPTIONS"],
107-
allowedHeaders: ["authorization", "b3", "traceparent", "content-type"],
108-
maxAge: 600,
109-
credentials: true,
110-
allowedOrigins: isAllowedBrowserApiCorsOrigin as (origin: string) => boolean,
111-
});
110+
export const browserApiCorsLayer = HttpRouter.middleware(
111+
HttpMiddleware.cors({
112+
allowedMethods: ["GET", "POST", "OPTIONS"],
113+
allowedHeaders: ["authorization", "b3", "traceparent", "content-type"],
114+
maxAge: 600,
115+
credentials: true,
116+
allowedOrigins: isAllowedBrowserApiCorsOrigin,
117+
}),
118+
{ global: true },
119+
);
112120

113121
export function resolveDevRedirectUrl(devUrl: URL, requestUrl: URL): string {
114122
const redirectUrl = new URL(devUrl.toString());

0 commit comments

Comments
 (0)