forked from transitive-bullshit/cf-image-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch-request.js
64 lines (52 loc) · 1.55 KB
/
fetch-request.js
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
import * as globalHeaders from './global-res-headers'
const headerWhitelist = new Set([
'connection',
'content-disposition',
'content-type',
'content-length',
'cf-polished',
'date',
'status',
'transfer-encoding'
])
export async function fetchRequest(event, { originReq }) {
// const originRes = await fetch(originReq)
// console.log(
// 'req',
// originReq.url,
// Object.fromEntries(originReq.headers.entries())
// )
const originRes = await fetch(originReq, {
cf: {
polish: 'lossy',
cacheEverything: true
}
})
// Construct a new response so we can mutate its headers
const res = new Response(originRes.body, originRes)
// console.log('res0', res.status, Object.fromEntries(res.headers.entries()))
// Stripe additional headers from the response that may impact cacheability
// like content security policy stuff
normalizeResponseHeaders(res)
// Override cache-control
res.headers.set(
'cache-control',
'public, immutable, s-maxage=31536000, max-age=31536000, stale-while-revalidate=60'
)
// Set CORS headers
for (const header of globalHeaders.globalResHeadersKeys) {
res.headers.set(header, globalHeaders.globalResHeaders[header])
}
// console.log('res1', res.status, Object.fromEntries(res.headers.entries()))
return res
}
function normalizeResponseHeaders(res) {
const headers = Object.fromEntries(res.headers.entries())
const keys = Object.keys(headers)
for (const key of keys) {
if (!headerWhitelist.has(key)) {
res.headers.delete(key)
}
}
return res
}