forked from isomorphic-git/cors-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmicro-cors.js
More file actions
52 lines (45 loc) · 1.3 KB
/
micro-cors.js
File metadata and controls
52 lines (45 loc) · 1.3 KB
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
// MIT License
// https://github.com/possibilities/micro-cors
// source: https://github.com/possibilities/micro-cors/pull/42
const DEFAULT_ALLOW_METHODS = [
'POST',
'GET',
'PUT',
'PATCH',
'DELETE',
'OPTIONS'
]
const DEFAULT_ALLOW_HEADERS = [
'X-Requested-With',
'Access-Control-Allow-Origin',
'X-HTTP-Method-Override',
'Content-Type',
'Authorization',
'Accept'
]
const DEFAULT_MAX_AGE_SECONDS = 60 * 60 * 24 // 24 hours
const cors = (options = {}) => handler => (req, res, ...restArgs) => {
const {
origin = '*',
maxAge = DEFAULT_MAX_AGE_SECONDS,
allowMethods = DEFAULT_ALLOW_METHODS,
allowHeaders = DEFAULT_ALLOW_HEADERS,
allowCredentials = true,
exposeHeaders = []
} = options
res.setHeader('Access-Control-Allow-Origin', origin)
if (allowCredentials) {
res.setHeader('Access-Control-Allow-Credentials', 'true')
}
if (exposeHeaders.length) {
res.setHeader('Access-Control-Expose-Headers', exposeHeaders.join(','))
}
const preFlight = req.method === 'OPTIONS'
if (preFlight) {
res.setHeader('Access-Control-Allow-Methods', allowMethods.join(','))
res.setHeader('Access-Control-Allow-Headers', allowHeaders.join(','))
res.setHeader('Access-Control-Max-Age', String(maxAge))
}
return handler(req, res, ...restArgs)
}
module.exports = cors