-
Notifications
You must be signed in to change notification settings - Fork 372
/
Copy pathindex.ts
38 lines (32 loc) · 987 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import type { IncomingMessage, ServerResponse } from "http";
export interface CrossOriginEmbedderPolicyOptions {
policy?: "require-corp" | "credentialless";
}
const ALLOWED_POLICIES = new Set(["require-corp", "credentialless"]);
function getHeaderValueFromOptions({
policy = "require-corp",
}: Readonly<CrossOriginEmbedderPolicyOptions>): string {
if (ALLOWED_POLICIES.has(policy)) {
return policy;
} else {
throw new Error(
`Cross-Origin-Embedder-Policy does not support the ${JSON.stringify(
policy,
)} policy`,
);
}
}
function crossOriginEmbedderPolicy(
options: Readonly<CrossOriginEmbedderPolicyOptions> = {},
) {
const headerValue = getHeaderValueFromOptions(options);
return function crossOriginEmbedderPolicyMiddleware(
_req: IncomingMessage,
res: ServerResponse,
next: () => void,
) {
res.setHeader("Cross-Origin-Embedder-Policy", headerValue);
next();
};
}
export default crossOriginEmbedderPolicy;