-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathindex.js
More file actions
77 lines (65 loc) · 2.58 KB
/
index.js
File metadata and controls
77 lines (65 loc) · 2.58 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
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
/*global CUSTOM_HEADER*/
/*global ALLOWED_ORIGINS*/
w /**
* REQUIRED ENVIRONMENT VARIABLES (configure in wrangler.toml [vars] or Worker dashboard):
*
* ALLOWED_ORIGINS - Comma-separated list of allowed hostnames, e.g. "www.example.com,cdn.example.com"
* Leave empty to allow all origins.
* CUSTOM_HEADER - Optional custom request header to forward, formatted as "header-name,header-value"
* e.g. "x-my-token,secret123". Leave empty if not needed.
*/
import ImageComponents from './src/imageComponents'
import ResizerOptions from './src/resizerOptions'
addEventListener('fetch', (event) => {
/* Return the origin image directly if the request is from the resizer itself */
if (/image-resizing/.test(event.request.headers.get('via'))) {
event.respondWith(fetch(event.request))
} else {
event.respondWith(handleRequest(event.request))
}
})
async function handleRequest(request) {
try {
/* ALLOWED_ORIGINS is a comma-separated string of hostnames */
const imgComponents = new ImageComponents(
request,
ALLOWED_ORIGINS.split(',').map((o) => o.trim()),
CUSTOM_HEADER
)
if (!imgComponents.isResizeAllowed() || !imgComponents.isOriginAllowed()) {
return fetch(request)
}
const imageResizerOptions = new ResizerOptions(
request.headers,
imgComponents.getSize(),
imgComponents.getExtension()
)
/* Build new headers to avoid mutating the immutable original request headers */
const newHeaders = new Headers(request.headers)
if (imgComponents.hasCustomHeader()) {
const customHeader = imgComponents.getCustomHeader()
newHeaders.append(customHeader.name, customHeader.value)
}
const imageRequest = new Request(imgComponents.getUnsizedUrl(), {
headers: newHeaders,
})
const response = await fetch(imageRequest, imageResizerOptions.getOptions())
if (response.ok) {
return response
}
/* Resizing failed — fall back to fetching the original image from origin */
console.log(
`Image resizing failed with status ${response.status}, falling back to origin`
)
return fetch(new Request(imgComponents.getUnsizedUrl(), { headers: newHeaders }))
} catch (err) {
/* Last-resort fallback — serve the original request unmodified */
console.log('Error during image resizing, falling back to origin: ' + err)
try {
return fetch(request)
} catch (fallbackErr) {
console.log('Origin fallback also failed: ' + fallbackErr)
return new Response('Error fetching image', { status: 500 })
}
}
}