-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
152 lines (130 loc) · 4.6 KB
/
index.ts
File metadata and controls
152 lines (130 loc) · 4.6 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* Files Worker - Serves code/html block content from R2
*
* Runs on a separate domain to isolate user-generated content
* from the main application for security.
*
* URL patterns (compatible with Wikidot's wdfiles.com):
* - /local--html/<page>/<hash> - HTML block content
* - /local--code/<page>/<index> - Code block content
* - /common--javascript/html-block-iframe.js - Resize script for iframe
*/
import { HTML_BLOCK_RESIZE_SCRIPT } from "@wdprlib/runtime";
interface Env {
FILES: R2Bucket;
ALLOWED_ORIGIN?: string;
}
const HTML_BLOCK_CSS_URL = "https://wdpr-demo-v1.r74.tech/common--theme/base/css/html-block.css";
const HTML_WRAPPER_TEMPLATE = `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html id="html-block-html" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" href="${HTML_BLOCK_CSS_URL}"/>
</head>
<body></body>
</html>`;
function addResizeScript(content: string): Response {
const hasBody = /<body[\s>]/i.test(content);
if (hasBody) {
const response = new Response(content, {
headers: { "Content-Type": "text/html; charset=utf-8" },
});
return new HTMLRewriter()
.on("body", {
element(element) {
element.append(
'<script type="text/javascript" src="/common--javascript/html-block-iframe.js"></script>',
{ html: true },
);
},
})
.transform(response);
}
const templateResponse = new Response(HTML_WRAPPER_TEMPLATE, {
headers: { "Content-Type": "text/html; charset=utf-8" },
});
return new HTMLRewriter()
.on("body", {
element(element) {
element.append(content, { html: true });
element.append(
'<script type="text/javascript" src="/common--javascript/html-block-iframe.js"></script>',
{ html: true },
);
},
})
.transform(templateResponse);
}
export default {
async fetch(request: Request, env: Env): Promise<Response> {
const url = new URL(request.url);
const path = url.pathname;
const corsHeaders: Record<string, string> = {
"Access-Control-Allow-Origin": env.ALLOWED_ORIGIN || "*",
"Access-Control-Allow-Methods": "GET, HEAD, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type",
};
if (request.method === "OPTIONS") {
return new Response(null, { headers: corsHeaders });
}
if (request.method !== "GET" && request.method !== "HEAD") {
return new Response("Method not allowed", { status: 405 });
}
// Serve resize script
if (path === "/common--javascript/html-block-iframe.js") {
return new Response(HTML_BLOCK_RESIZE_SCRIPT, {
headers: {
...corsHeaders,
"Content-Type": "text/javascript; charset=utf-8",
"Cache-Control": "public, max-age=31536000, immutable",
},
});
}
const htmlMatch = path.match(/^\/local--html\/([^/]+)\/([^/]+)$/);
const codeMatch = path.match(/^\/local--code\/([^/]+)\/([^/]+)$/);
let key: string;
let contentType: string;
let securityHeaders: Record<string, string> = {};
if (htmlMatch) {
const [, page, hash] = htmlMatch;
key = `local--html/${page}/${hash}`;
contentType = "text/html; charset=utf-8";
securityHeaders = {
"X-Content-Type-Options": "nosniff",
};
} else if (codeMatch) {
const [, page, index] = codeMatch;
key = `local--code/${page}/${index}`;
contentType = "text/plain; charset=utf-8";
securityHeaders = {
"X-Content-Type-Options": "nosniff",
};
} else {
return new Response("Not found", { status: 404 });
}
const object = await env.FILES.get(key);
if (!object) {
return new Response("Not found", { status: 404 });
}
const headers = new Headers({
...corsHeaders,
...securityHeaders,
"Content-Type": object.httpMetadata?.contentType || contentType,
ETag: object.httpEtag,
"Cache-Control": "public, max-age=31536000, immutable",
});
if (request.method === "HEAD") {
return new Response(null, { headers });
}
if (htmlMatch) {
const rawContent = await object.text();
const transformed = addResizeScript(rawContent);
const newHeaders = new Headers(transformed.headers);
for (const [key, value] of headers.entries()) {
newHeaders.set(key, value);
}
return new Response(transformed.body, { headers: newHeaders });
}
return new Response(object.body, { headers });
},
};