-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.js
62 lines (58 loc) · 2.34 KB
/
index.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
const version = "0.0.3"
let allowedDomains = process?.env?.ALLOWED_REMOTE_DOMAINS?.split(",") || ["*"];
let imgproxyUrl = process?.env?.IMGPROXY_URL || "http://imgproxy:8080";
if (process.env.NODE_ENV === "development") {
imgproxyUrl = "http://localhost:8888"
}
allowedDomains = allowedDomains.map(d => d.trim());
Bun.serve({
port: 3000,
async fetch(req) {
const url = new URL(req.url);
if (url.pathname === "/") {
return new Response(`<h3>Next Image Transformation v${version}</h3>More info <a href="https://github.com/coollabsio/next-image-transformation">https://github.com/coollabsio/next-image-transformation</a>.`, {
headers: {
"Content-Type": "text/html",
},
});
}
if (url.pathname === "/health") {
return new Response("OK");
};
if (url.pathname.startsWith("/image/")) return await resize(url);
return Response.redirect("https://github.com/coollabsio/next-image-transformation", 302);
}
});
async function resize(url) {
const preset = "pr:sharp"
const src = url.pathname.split("/").slice(2).join("/");
const origin = new URL(src).hostname;
const allowed = allowedDomains.filter(domain => {
if (domain === "*") return true;
if (domain === origin) return true;
if (domain.startsWith("*.") && origin.endsWith(domain.split("*.").pop())) return true;
return false;
})
if (allowed.length === 0) {
return new Response(`Domain (${origin}) not allowed. More details here: https://github.com/coollabsio/next-image-transformation`, { status: 403 });
}
const width = url.searchParams.get("width") || 0;
const height = url.searchParams.get("height") || 0;
const quality = url.searchParams.get("quality") || 75;
try {
const url = `${imgproxyUrl}/${preset}/resize:fill:${width}:${height}/q:${quality}/plain/${src}`
const image = await fetch(url, {
headers: {
"Accept": "image/avif,image/webp,image/apng,*/*",
}
})
const headers = new Headers(image.headers);
headers.set("Server", "NextImageTransformation");
return new Response(image.body, {
headers
})
} catch (e) {
console.log(e)
return new Response("Error resizing image")
}
}