-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhandleRequest.ts
More file actions
269 lines (241 loc) · 8.42 KB
/
handleRequest.ts
File metadata and controls
269 lines (241 loc) · 8.42 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import { env } from "cloudflare:workers";
import { renderHome } from "./home.jsx";
import oldMappings from "./old_redirects.json" with { type: "json" };
import {
type PluginUrlResult,
tryResolveAssetUrl,
tryResolveLatestJson,
tryResolvePluginUrl,
tryResolveSchemaUrl,
} from "./plugins.js";
import { readInfoFile } from "./readInfoFile.js";
import robotsTxt from "./robots.txt";
import styleCSS from "./style.css";
import { fetchWithRetries } from "./utils/fetchWithRetries.js";
import { LruCache } from "./utils/LruCache.js";
import { getCliInfo } from "./utils/mod.js";
import { r2Get, r2Put } from "./utils/r2Cache.js";
const MAX_MEM_CACHE_BODY_SIZE = 10 * 1024 * 1024; // 10MB
const contentTypes = {
css: "text/css; charset=utf-8",
html: "text/html; charset=utf-8",
json: "application/json; charset=utf-8",
plain: "text/plain; charset=utf-8",
wasm: "application/wasm",
octetStream: "application/octet-stream",
};
export function createRequestHandler() {
const memoryCache = new LruCache<string, { body: ArrayBuffer; contentType: string }>({ size: 50 });
return {
async handleRequest(request: Request, ctx?: ExecutionContext) {
const url = new URL(request.url);
const assetResult = tryResolveAssetUrl(url);
if (assetResult != null) {
if (!assetResult.shouldCache) {
return Response.redirect(assetResult.githubUrl, 302);
}
return servePlugin(request, assetResult.githubUrl, ctx);
}
const githubUrl = await resolvePluginOrSchemaUrl(url);
if (githubUrl != null) {
if (!githubUrl.endsWith(".wasm")) {
// redirect non-wasm files to asset URL so relative URLs in
// plugin.json files resolve correctly
const assetPath = githubUrlToAssetPath(githubUrl);
if (assetPath != null) {
return Response.redirect(`${url.origin}${assetPath}`, 302);
}
}
return servePlugin(request, githubUrl, ctx);
}
const userLatestInfo = await tryResolveLatestJson(url);
if (userLatestInfo != null) {
if (userLatestInfo === 404) {
return new Response("Not Found", {
status: 404,
});
} else {
return createJsonResponse(
JSON.stringify(userLatestInfo, undefined, 2),
request,
);
}
}
if (url.pathname.startsWith("/info.json")) {
const infoFileData = await readInfoFile(url.origin);
return createJsonResponse(
JSON.stringify(infoFileData, null, 2),
request,
);
}
if (url.pathname.startsWith("/cli.json")) {
const cliInfo = await getCliInfo();
return createJsonResponse(JSON.stringify(cliInfo, null, 2), request);
}
if (url.pathname === "/robots.txt") {
return new Response(robotsTxt, {
headers: { "content-type": contentTypes.plain },
status: 200,
});
}
if (url.pathname === "/style.css") {
return new Response(styleCSS, {
headers: {
"content-type": contentTypes.css,
},
status: 200,
});
}
if (url.pathname === "/") {
return renderHome(url.origin).then((home) =>
new Response(home, {
headers: {
"content-type": contentTypes.html,
},
status: 200,
})
).catch((err) =>
new Response(`${err.toString?.() ?? err}`, {
headers: {
"content-type": contentTypes.plain,
},
status: 500,
})
);
}
return create404Response();
},
};
async function servePlugin(request: Request, githubUrl: string, ctx?: ExecutionContext) {
const result = await resolveBodyWithMemoryCache(githubUrl, ctx);
return new Response(result.body, {
headers: {
"content-type": result.contentType,
"Access-Control-Allow-Origin": getAccessControlAllowOrigin(request),
},
status: result.status,
});
}
async function resolveBodyWithMemoryCache(
githubUrl: string,
ctx?: ExecutionContext,
): Promise<{ body: ArrayBuffer | ReadableStream | null; status: number; contentType: string }> {
// L1: in-memory cache
const cached = memoryCache.get(githubUrl);
if (cached != null) {
return { body: cached.body, status: 200, contentType: cached.contentType };
}
const result = await fetchBody(githubUrl, ctx);
if (
result.status === 200 && result.body instanceof ArrayBuffer && result.body.byteLength <= MAX_MEM_CACHE_BODY_SIZE
) {
memoryCache.set(githubUrl, { body: result.body, contentType: result.contentType });
}
return result;
}
async function fetchBody(
githubUrl: string,
ctx?: ExecutionContext,
): Promise<{ body: ArrayBuffer | ReadableStream | null; status: number; contentType: string }> {
// L2: R2 (stores original content)
const r2Object = await r2Get(githubUrl);
if (r2Object != null) {
const contentType = r2Object.httpMetadata?.contentType ?? contentTypeForUrl(githubUrl);
if (r2Object.size <= MAX_MEM_CACHE_BODY_SIZE) {
return { body: await r2Object.arrayBuffer(), status: 200, contentType };
}
// large — stream directly without buffering
return { body: r2Object.body, status: 200, contentType };
}
// L3: fetch from GitHub
const response = await fetchWithRetries(githubUrl, {
// don't need the github token here because these assets
// are not part of the github api
headers: { "user-agent": "dprint-plugins" },
});
if (!response.ok) {
if (response.status !== 404) {
console.error(`GitHub fetch error: ${response.status} ${response.statusText} for ${githubUrl}`);
}
return {
body: response.body,
status: response.status,
contentType: response.headers.get("content-type") ?? contentTypes.plain,
};
}
const contentType = response.headers.get("content-type") ?? contentTypeForUrl(githubUrl);
const body = await response.arrayBuffer();
// store original in R2
const r2Promise = r2Put(githubUrl, body, contentType);
if (ctx != null) {
ctx.waitUntil(r2Promise);
} else {
await r2Promise;
}
return { body, status: 200, contentType };
}
}
export async function resolvePluginOrSchemaUrl(url: URL) {
const oldMapping = (oldMappings as { [oldUrl: string]: string })[url.toString()];
if (oldMapping != null) {
return oldMapping;
}
const pluginResult = await tryResolvePluginUrl(url);
if (pluginResult != null) {
trackPluginDownload(pluginResult);
return pluginResult.githubUrl;
}
return await tryResolveSchemaUrl(url);
}
function getAccessControlAllowOrigin(request: Request) {
const origin = request.headers.get("origin");
return origin != null && isLocalHostname(new URL(origin).hostname)
? origin
: "https://dprint.dev";
}
function isLocalHostname(hostname: string) {
return hostname === "localhost" || hostname === "127.0.0.1";
}
function createJsonResponse(text: string, request: Request) {
return new Response(text, {
headers: {
"content-type": contentTypes.json,
"Access-Control-Allow-Origin": getAccessControlAllowOrigin(request),
},
status: 200,
});
}
// converts a GitHub release URL to a local asset path
// e.g. https://github.com/dprint/dprint-plugin-prettier/releases/download/0.7.0/plugin.json
// -> /dprint/dprint-plugin-prettier/0.7.0/asset/plugin.json
const githubReleasePattern = /^https:\/\/github\.com\/([^/]+)\/([^/]+)\/releases\/download\/([^/]+)\/(.+)$/;
function githubUrlToAssetPath(githubUrl: string) {
const match = githubReleasePattern.exec(githubUrl);
if (match == null) {
return undefined;
}
const [, username, repo, tag, fileName] = match;
return `/${username}/${repo}/${tag}/asset/${fileName}`;
}
function contentTypeForUrl(url: string) {
if (url.endsWith(".wasm")) return contentTypes.wasm;
if (url.endsWith(".json") || url.endsWith(".exe-plugin")) return contentTypes.json;
return contentTypes.octetStream;
}
function trackPluginDownload(result: PluginUrlResult) {
try {
env.DPRINT_PLUGIN_DOWNLOAD_ANALYTICS.writeDataPoint({
indexes: [`${result.username}/${result.repo}`],
blobs: [result.tag],
doubles: [1],
});
} catch (err) {
console.warn("Failed to write analytics:", err);
}
}
function create404Response() {
return new Response(null, {
status: 404,
statusText: "Not Found",
});
}