-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (58 loc) · 1.47 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
// src/error.ts
var ResponseError = class extends Error {
status;
constructor(message, status) {
super(message);
this.name = "ResponseError";
this.status = status;
}
};
// src/index.ts
var handleRequest = async (request) => {
const { searchParams } = new URL(request.url);
const params = {};
for (const [key, value] of searchParams) {
params[key] = value;
}
const url = params.url;
if (!url)
throw new ResponseError("Missing url parameter", 400);
delete params.url;
let response;
try {
response = await fetch(url, {
method: "GET"
});
} catch {
throw new ResponseError("Failed to fetch url", 500);
}
if (response.headers.get("content-type") !== "image/svg+xml") {
throw new ResponseError("Invalid content-type, must be image/svg+xml", 400);
}
const newResponse = new Response(response.body, response);
newResponse.headers.set("cache-control", "max-age=604800");
return new HTMLRewriter().on("svg", {
element(element) {
for (const [name, value] of Object.entries(params)) {
element.setAttribute(name, value);
}
}
}).transform(newResponse);
};
var handleRequestWrapper = async (request) => {
try {
return await handleRequest(request);
} catch (err) {
if (err instanceof ResponseError) {
return new Response(err.message);
} else
throw err;
}
};
var src_default = {
fetch: handleRequestWrapper
};
export {
src_default as default
};
//# sourceMappingURL=index.js.map