-
Notifications
You must be signed in to change notification settings - Fork 1
/
cors.ts
84 lines (80 loc) · 2.48 KB
/
cors.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { send } from "./response.ts";
import { CorsOptions, MiddlewareFunc } from "./model.ts";
const defaultMethods = ["GET", "POST", "PUT", "DELETE", "HEAD", "PATCH"];
export function cors(options: CorsOptions = {}): MiddlewareFunc {
let realOrigin = "";
const {
origin = "*",
allowCredentials = true,
allowMethods = defaultMethods,
exposeHeaders,
maxAge,
allowHeaders,
} = options;
return async function (request, response, next) {
const method = request.method.toUpperCase();
const Origin = request.headers.get("Origin");
// set vary
response.headers.set("Vary", request.headers.get("Origin") || "");
// get real origin
realOrigin = typeof origin === "function"
? await origin(request) || "*"
: origin;
if (!Origin || !realOrigin) return await next();
// set all common response header
response.headers.set("Access-Control-Allow-Origin", realOrigin);
// set expose headers
exposeHeaders
? response.headers.set(
"Access-Control-Expose-Headers",
exposeHeaders.join(","),
)
: "";
// allow cookies
if (allowCredentials) {
// if origin is '*', should not contains cookies
realOrigin === "*"
? response.headers.delete("Access-Control-Allow-Credentials")
: response.headers.set("Access-Control-Allow-Credentials", "true");
}
// check method
if (method === "OPTIONS") {
// prefix request,
// this attr means the real request method,
// may not be options
if (!request.headers.get("Access-Control-Request-Method")) {
return await next();
}
// allow request methods
response.headers.set(
"Access-Control-Allow-Methods",
allowMethods ? allowMethods.join(",") : defaultMethods.join(","),
);
// allow request headers
response.headers.set(
"Access-Control-Allow-Headers",
allowHeaders
? allowHeaders.join(",")
: request.headers.get("Access-Control-Request-Headers") || "",
);
// set max age
maxAge &&
response.headers.set("Access-Control-Max-Age", String(maxAge));
response.status = 204;
return send(request, response);
} else {
// set expose headers
exposeHeaders
? response.headers.set(
"Access-Control-Expose-Headers",
exposeHeaders.join(","),
)
: "";
try {
await next();
} catch (e) {
console.log(e);
}
}
};
}