Skip to content

Commit 39cae22

Browse files
committed
Actually expose headers (like content-type) in responses!
1 parent 14e9636 commit 39cae22

File tree

3 files changed

+97
-8
lines changed

3 files changed

+97
-8
lines changed

index.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,24 @@ const allowHeaders = [
2020
'x-http-method-override',
2121
'x-requested-with',
2222
]
23-
const cors = require('micro-cors')({allowHeaders})
23+
const exposeHeaders = [
24+
'accept-ranges',
25+
'age',
26+
'cache-control',
27+
'content-length',
28+
'content-language',
29+
'content-type',
30+
'date',
31+
'etag',
32+
'expires',
33+
'last-modified',
34+
'pragma',
35+
'server',
36+
'transfer-encoding',
37+
'vary',
38+
'x-github-request-id',
39+
]
40+
const cors = require('./micro-cors.js')({allowHeaders, exposeHeaders})
2441
const fetch = require('node-fetch')
2542

2643
async function service (req, res) {
@@ -42,13 +59,13 @@ async function service (req, res) {
4259
It records the URL, origin IP, referer, and user-agent. None of the sensitive HTTP headers (including those used for
4360
HTTP Basic Auth and HTTP Token auth) are ever logged.
4461
<h2>Request API</h2>
45-
https://${process.env.NOW_URL}/domain/path?query
62+
${process.env.NOW_URL}/domain/path?query
4663
<ul>
4764
<li>domain - the destination host</li>
4865
<li>path - the rest of the URL</li>
4966
<li>query - optional query parameters</li>
5067
</ul>
51-
Example: https://${process.env.NOW_URL}/github.com/wmhilton/cors-buster?service=git-upload-pack
68+
Example: ${process.env.NOW_URL}/github.com/wmhilton/cors-buster?service=git-upload-pack
5269
<h2>Supported Protocols</h2>
5370
In order to protect users who might send their usernames and passwords through the proxy,
5471
all requests must be made using HTTPS. Plain old HTTP is insecure and therefore not allowed.
@@ -85,9 +102,14 @@ async function service (req, res) {
85102
{
86103
method: req.method,
87104
headers,
88-
body: req
105+
body: (req.method !== 'GET' && req.method !== 'HEAD') ? req : undefined
89106
}
90107
)
108+
for (let h of exposeHeaders) {
109+
if (f.headers.has(h)) {
110+
res.setHeader(h, f.headers.get(h))
111+
}
112+
}
91113
f.body.pipe(res)
92114
}
93115

micro-cors.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// MIT License
2+
// https://github.com/possibilities/micro-cors
3+
const DEFAULT_ALLOW_METHODS = [
4+
'POST',
5+
'GET',
6+
'PUT',
7+
'PATCH',
8+
'DELETE',
9+
'OPTIONS'
10+
]
11+
12+
const DEFAULT_ALLOW_HEADERS = [
13+
'X-Requested-With',
14+
'Access-Control-Allow-Origin',
15+
'X-HTTP-Method-Override',
16+
'Content-Type',
17+
'Authorization',
18+
'Accept'
19+
]
20+
21+
const DEFAULT_MAX_AGE_SECONDS = 60 * 60 * 24 // 24 hours
22+
23+
const cors = options => handler => (req, res, ...restArgs) => {
24+
const {
25+
maxAge,
26+
origin,
27+
allowHeaders,
28+
exposeHeaders,
29+
allowMethods
30+
} = (options || {})
31+
32+
res.setHeader(
33+
'Access-Control-Max-Age',
34+
'' + (maxAge || DEFAULT_MAX_AGE_SECONDS)
35+
)
36+
37+
res.setHeader(
38+
'Access-Control-Allow-Origin',
39+
(origin || '*')
40+
)
41+
42+
res.setHeader(
43+
'Access-Control-Allow-Methods',
44+
(allowMethods || DEFAULT_ALLOW_METHODS).join(',')
45+
)
46+
47+
res.setHeader(
48+
'Access-Control-Allow-Headers',
49+
(allowHeaders || DEFAULT_ALLOW_HEADERS).join(',')
50+
)
51+
52+
if (exposeHeaders && exposeHeaders.length) {
53+
res.setHeader(
54+
'Access-Control-Expose-Headers',
55+
exposeHeaders.join(',')
56+
)
57+
}
58+
59+
res.setHeader('Access-Control-Allow-Credentials', 'true')
60+
61+
return handler(req, res, ...restArgs)
62+
}
63+
64+
module.exports = cors

package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "When you need a file, but the headers ain't good, who you gonna call? CORS Buster!",
55
"main": "index.js",
66
"scripts": {
7-
"start": "micro"
7+
"start": "micro",
8+
"dev": "micro-dev"
89
},
910
"keywords": [],
1011
"author": "William Hilton <[email protected]>",
@@ -14,11 +15,13 @@
1415
"url": "git://github.com/wmhilton/cors-buster.git"
1516
},
1617
"dependencies": {
17-
"micro": "^8.0.1",
18-
"micro-cors": "0.0.4",
19-
"node-fetch": "^1.7.1"
18+
"micro": "^9.1.4",
19+
"node-fetch": "^2.1.2"
2020
},
2121
"now": {
2222
"public": true
23+
},
24+
"devDependencies": {
25+
"micro-dev": "^2.2.2"
2326
}
2427
}

0 commit comments

Comments
 (0)