wrangler 2.0.21 → 2.0.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -2
- package/bin/wrangler.js +1 -1
- package/miniflare-dist/index.mjs +527 -5
- package/package.json +18 -5
- package/src/__tests__/configuration.test.ts +88 -16
- package/src/__tests__/dev.test.tsx +95 -4
- package/src/__tests__/generate.test.ts +93 -0
- package/src/__tests__/helpers/mock-cfetch.ts +54 -2
- package/src/__tests__/index.test.ts +10 -27
- package/src/__tests__/jest.setup.ts +31 -1
- package/src/__tests__/kv.test.ts +82 -61
- package/src/__tests__/metrics.test.ts +35 -0
- package/src/__tests__/publish.test.ts +573 -254
- package/src/__tests__/r2.test.ts +155 -71
- package/src/__tests__/user.test.ts +1 -0
- package/src/__tests__/validate-dev-props.test.ts +56 -0
- package/src/__tests__/version.test.ts +35 -0
- package/src/__tests__/whoami.test.tsx +60 -1
- package/src/api/dev.ts +43 -9
- package/src/bundle.ts +297 -37
- package/src/cfetch/internal.ts +34 -2
- package/src/config/config.ts +14 -2
- package/src/config/environment.ts +40 -8
- package/src/config/index.ts +13 -0
- package/src/config/validation.ts +110 -8
- package/src/create-worker-preview.ts +3 -1
- package/src/create-worker-upload-form.ts +25 -0
- package/src/dev/dev.tsx +135 -31
- package/src/dev/local.tsx +48 -20
- package/src/dev/remote.tsx +39 -12
- package/src/dev/use-esbuild.ts +25 -0
- package/src/dev/validate-dev-props.ts +31 -0
- package/src/dev-registry.tsx +157 -0
- package/src/dev.tsx +137 -65
- package/src/generate.ts +112 -14
- package/src/index.tsx +222 -7
- package/src/inspect.ts +93 -5
- package/src/metrics/index.ts +1 -0
- package/src/metrics/is-ci.ts +14 -0
- package/src/metrics/metrics-config.ts +19 -2
- package/src/metrics/metrics-dispatcher.ts +1 -0
- package/src/metrics/metrics-usage-headers.ts +24 -0
- package/src/metrics/send-event.ts +2 -2
- package/src/miniflare-cli/assets.ts +543 -0
- package/src/miniflare-cli/index.ts +36 -4
- package/src/module-collection.ts +3 -3
- package/src/pages/constants.ts +1 -0
- package/src/pages/deployments.tsx +1 -1
- package/src/pages/dev.tsx +85 -639
- package/src/pages/publish.tsx +1 -1
- package/src/pages/upload.tsx +32 -13
- package/src/publish.ts +139 -112
- package/src/r2.ts +68 -0
- package/src/user/choose-account.tsx +20 -11
- package/src/user/user.tsx +20 -2
- package/src/whoami.tsx +79 -1
- package/src/worker.ts +12 -0
- package/templates/first-party-worker-module-facade.ts +18 -0
- package/templates/format-dev-errors.ts +32 -0
- package/templates/pages-shim.ts +9 -0
- package/templates/{static-asset-facade.js → serve-static-assets.ts} +21 -7
- package/templates/service-bindings-module-facade.js +51 -0
- package/templates/service-bindings-sw-facade.js +39 -0
- package/wrangler-dist/cli.d.ts +32 -3
- package/wrangler-dist/cli.js +45257 -25209
|
@@ -0,0 +1,543 @@
|
|
|
1
|
+
import { existsSync, lstatSync, readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { fetch as miniflareFetch } from "@miniflare/core";
|
|
4
|
+
import { watch } from "chokidar";
|
|
5
|
+
import { getType } from "mime";
|
|
6
|
+
import { Response } from "miniflare";
|
|
7
|
+
import type { Headers as MiniflareHeaders } from "@miniflare/core";
|
|
8
|
+
import type { Log } from "miniflare";
|
|
9
|
+
import type {
|
|
10
|
+
Request as MiniflareRequest,
|
|
11
|
+
RequestInfo,
|
|
12
|
+
RequestInit,
|
|
13
|
+
} from "miniflare";
|
|
14
|
+
|
|
15
|
+
export interface Options {
|
|
16
|
+
log: Log;
|
|
17
|
+
proxyPort?: number;
|
|
18
|
+
directory?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export default async function generateASSETSBinding(options: Options) {
|
|
22
|
+
const assetsFetch =
|
|
23
|
+
options.directory !== undefined
|
|
24
|
+
? await generateAssetsFetch(options.directory, options.log)
|
|
25
|
+
: invalidAssetsFetch;
|
|
26
|
+
|
|
27
|
+
return async function (request: MiniflareRequest) {
|
|
28
|
+
if (options.proxyPort) {
|
|
29
|
+
try {
|
|
30
|
+
const url = new URL(request.url);
|
|
31
|
+
url.host = `localhost:${options.proxyPort}`;
|
|
32
|
+
return await miniflareFetch(url, request);
|
|
33
|
+
} catch (thrown) {
|
|
34
|
+
options.log.error(new Error(`Could not proxy request: ${thrown}`));
|
|
35
|
+
|
|
36
|
+
// TODO: Pretty error page
|
|
37
|
+
return new Response(`[wrangler] Could not proxy request: ${thrown}`, {
|
|
38
|
+
status: 502,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
} else {
|
|
42
|
+
try {
|
|
43
|
+
return await assetsFetch(request);
|
|
44
|
+
} catch (thrown) {
|
|
45
|
+
options.log.error(new Error(`Could not serve static asset: ${thrown}`));
|
|
46
|
+
|
|
47
|
+
// TODO: Pretty error page
|
|
48
|
+
return new Response(
|
|
49
|
+
`[wrangler] Could not serve static asset: ${thrown}`,
|
|
50
|
+
{ status: 502 }
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function escapeRegex(str: string) {
|
|
58
|
+
return str.replace(/[-/\\^$*+?.()|[]{}]/g, "\\$&");
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
type Replacements = Record<string, string>;
|
|
62
|
+
|
|
63
|
+
function replacer(str: string, replacements: Replacements) {
|
|
64
|
+
for (const [replacement, value] of Object.entries(replacements)) {
|
|
65
|
+
str = str.replace(`:${replacement}`, value);
|
|
66
|
+
}
|
|
67
|
+
return str;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function generateRulesMatcher<T>(
|
|
71
|
+
rules?: Record<string, T>,
|
|
72
|
+
replacerFn: (match: T, replacements: Replacements) => T = (match) => match
|
|
73
|
+
) {
|
|
74
|
+
// TODO: How can you test cross-host rules?
|
|
75
|
+
if (!rules) return () => [];
|
|
76
|
+
|
|
77
|
+
const compiledRules = Object.entries(rules)
|
|
78
|
+
.map(([rule, match]) => {
|
|
79
|
+
const crossHost = rule.startsWith("https://");
|
|
80
|
+
|
|
81
|
+
rule = rule.split("*").map(escapeRegex).join("(?<splat>.*)");
|
|
82
|
+
|
|
83
|
+
const host_matches = rule.matchAll(
|
|
84
|
+
/(?<=^https:\\\/\\\/[^/]*?):([^\\]+)(?=\\)/g
|
|
85
|
+
);
|
|
86
|
+
for (const hostMatch of host_matches) {
|
|
87
|
+
rule = rule.split(hostMatch[0]).join(`(?<${hostMatch[1]}>[^/.]+)`);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const path_matches = rule.matchAll(/:(\w+)/g);
|
|
91
|
+
for (const pathMatch of path_matches) {
|
|
92
|
+
rule = rule.split(pathMatch[0]).join(`(?<${pathMatch[1]}>[^/]+)`);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
rule = "^" + rule + "$";
|
|
96
|
+
|
|
97
|
+
try {
|
|
98
|
+
const regExp = new RegExp(rule);
|
|
99
|
+
return [{ crossHost, regExp }, match];
|
|
100
|
+
} catch {}
|
|
101
|
+
})
|
|
102
|
+
.filter((value) => value !== undefined) as [
|
|
103
|
+
{ crossHost: boolean; regExp: RegExp },
|
|
104
|
+
T
|
|
105
|
+
][];
|
|
106
|
+
|
|
107
|
+
return ({ request }: { request: MiniflareRequest }) => {
|
|
108
|
+
const { pathname, host } = new URL(request.url);
|
|
109
|
+
|
|
110
|
+
return compiledRules
|
|
111
|
+
.map(([{ crossHost, regExp }, match]) => {
|
|
112
|
+
const test = crossHost ? `https://${host}${pathname}` : pathname;
|
|
113
|
+
const result = regExp.exec(test);
|
|
114
|
+
if (result) {
|
|
115
|
+
return replacerFn(match, result.groups || {});
|
|
116
|
+
}
|
|
117
|
+
})
|
|
118
|
+
.filter((value) => value !== undefined) as T[];
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function generateHeadersMatcher(headersFile: string) {
|
|
123
|
+
if (existsSync(headersFile)) {
|
|
124
|
+
const contents = readFileSync(headersFile).toString();
|
|
125
|
+
|
|
126
|
+
// TODO: Log errors
|
|
127
|
+
const lines = contents
|
|
128
|
+
.split("\n")
|
|
129
|
+
.map((line) => line.trim())
|
|
130
|
+
.filter((line) => !line.startsWith("#") && line !== "");
|
|
131
|
+
|
|
132
|
+
const rules: Record<string, Record<string, string>> = {};
|
|
133
|
+
let rule: { path: string; headers: Record<string, string> } | undefined =
|
|
134
|
+
undefined;
|
|
135
|
+
|
|
136
|
+
for (const line of lines) {
|
|
137
|
+
if (/^([^\s]+:\/\/|^\/)/.test(line)) {
|
|
138
|
+
if (rule && Object.keys(rule.headers).length > 0) {
|
|
139
|
+
rules[rule.path] = rule.headers;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const path = validateURL(line);
|
|
143
|
+
if (path) {
|
|
144
|
+
rule = {
|
|
145
|
+
path,
|
|
146
|
+
headers: {},
|
|
147
|
+
};
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (!line.includes(":")) continue;
|
|
153
|
+
|
|
154
|
+
const [rawName, ...rawValue] = line.split(":");
|
|
155
|
+
const name = rawName.trim().toLowerCase();
|
|
156
|
+
const value = rawValue.join(":").trim();
|
|
157
|
+
|
|
158
|
+
if (name === "") continue;
|
|
159
|
+
if (!rule) continue;
|
|
160
|
+
|
|
161
|
+
const existingValues = rule.headers[name];
|
|
162
|
+
rule.headers[name] = existingValues
|
|
163
|
+
? `${existingValues}, ${value}`
|
|
164
|
+
: value;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (rule && Object.keys(rule.headers).length > 0) {
|
|
168
|
+
rules[rule.path] = rule.headers;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const rulesMatcher = generateRulesMatcher(rules, (match, replacements) =>
|
|
172
|
+
Object.fromEntries(
|
|
173
|
+
Object.entries(match).map(([name, value]) => [
|
|
174
|
+
name,
|
|
175
|
+
replacer(value, replacements),
|
|
176
|
+
])
|
|
177
|
+
)
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
return (request: MiniflareRequest) => {
|
|
181
|
+
const matches = rulesMatcher({
|
|
182
|
+
request,
|
|
183
|
+
});
|
|
184
|
+
if (matches) return matches;
|
|
185
|
+
};
|
|
186
|
+
} else {
|
|
187
|
+
return () => undefined;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function generateRedirectsMatcher(redirectsFile: string) {
|
|
192
|
+
if (existsSync(redirectsFile)) {
|
|
193
|
+
const contents = readFileSync(redirectsFile).toString();
|
|
194
|
+
|
|
195
|
+
// TODO: Log errors
|
|
196
|
+
const lines = contents
|
|
197
|
+
.split("\n")
|
|
198
|
+
.map((line) => line.trim())
|
|
199
|
+
.filter((line) => !line.startsWith("#") && line !== "");
|
|
200
|
+
|
|
201
|
+
const rules = Object.fromEntries(
|
|
202
|
+
lines
|
|
203
|
+
.map((line) => line.split(" "))
|
|
204
|
+
.filter((tokens) => tokens.length === 2 || tokens.length === 3)
|
|
205
|
+
.map((tokens) => {
|
|
206
|
+
const from = validateURL(tokens[0], true, false, false);
|
|
207
|
+
const to = validateURL(tokens[1], false, true, true);
|
|
208
|
+
let status: number | undefined = parseInt(tokens[2]) || 302;
|
|
209
|
+
status = [301, 302, 303, 307, 308].includes(status)
|
|
210
|
+
? status
|
|
211
|
+
: undefined;
|
|
212
|
+
|
|
213
|
+
return from && to && status ? [from, { to, status }] : undefined;
|
|
214
|
+
})
|
|
215
|
+
.filter((rule) => rule !== undefined) as [
|
|
216
|
+
string,
|
|
217
|
+
{ to: string; status?: number }
|
|
218
|
+
][]
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
const rulesMatcher = generateRulesMatcher(
|
|
222
|
+
rules,
|
|
223
|
+
({ status, to }, replacements) => ({
|
|
224
|
+
status,
|
|
225
|
+
to: replacer(to, replacements),
|
|
226
|
+
})
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
return (request: MiniflareRequest) => {
|
|
230
|
+
const match = rulesMatcher({
|
|
231
|
+
request,
|
|
232
|
+
})[0];
|
|
233
|
+
if (match) return match;
|
|
234
|
+
};
|
|
235
|
+
} else {
|
|
236
|
+
return () => undefined;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function extractPathname(
|
|
241
|
+
path = "/",
|
|
242
|
+
includeSearch: boolean,
|
|
243
|
+
includeHash: boolean
|
|
244
|
+
) {
|
|
245
|
+
if (!path.startsWith("/")) path = `/${path}`;
|
|
246
|
+
const url = new URL(`//${path}`, "relative://");
|
|
247
|
+
return `${url.pathname}${includeSearch ? url.search : ""}${
|
|
248
|
+
includeHash ? url.hash : ""
|
|
249
|
+
}`;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function validateURL(
|
|
253
|
+
token: string,
|
|
254
|
+
onlyRelative = false,
|
|
255
|
+
includeSearch = false,
|
|
256
|
+
includeHash = false
|
|
257
|
+
) {
|
|
258
|
+
const host = /^https:\/\/+(?<host>[^/]+)\/?(?<path>.*)/.exec(token);
|
|
259
|
+
if (host && host.groups && host.groups.host) {
|
|
260
|
+
if (onlyRelative) return;
|
|
261
|
+
|
|
262
|
+
return `https://${host.groups.host}${extractPathname(
|
|
263
|
+
host.groups.path,
|
|
264
|
+
includeSearch,
|
|
265
|
+
includeHash
|
|
266
|
+
)}`;
|
|
267
|
+
} else {
|
|
268
|
+
if (!token.startsWith("/") && onlyRelative) token = `/${token}`;
|
|
269
|
+
|
|
270
|
+
const path = /^\//.exec(token);
|
|
271
|
+
if (path) {
|
|
272
|
+
try {
|
|
273
|
+
return extractPathname(token, includeSearch, includeHash);
|
|
274
|
+
} catch {}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return "";
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function hasFileExtension(pathname: string) {
|
|
281
|
+
return /\/.+\.[a-z0-9]+$/i.test(pathname);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
async function generateAssetsFetch(
|
|
285
|
+
directory: string,
|
|
286
|
+
log: Log
|
|
287
|
+
): Promise<typeof miniflareFetch> {
|
|
288
|
+
// Defer importing miniflare until we really need it
|
|
289
|
+
const { Headers, Request } = await import("@miniflare/core");
|
|
290
|
+
|
|
291
|
+
const headersFile = join(directory, "_headers");
|
|
292
|
+
const redirectsFile = join(directory, "_redirects");
|
|
293
|
+
const workerFile = join(directory, "_worker.js");
|
|
294
|
+
|
|
295
|
+
const ignoredFiles = [headersFile, redirectsFile, workerFile];
|
|
296
|
+
|
|
297
|
+
const assetExists = (path: string) => {
|
|
298
|
+
path = join(directory, path);
|
|
299
|
+
return (
|
|
300
|
+
existsSync(path) &&
|
|
301
|
+
lstatSync(path).isFile() &&
|
|
302
|
+
!ignoredFiles.includes(path)
|
|
303
|
+
);
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
const getAsset = (path: string) => {
|
|
307
|
+
if (assetExists(path)) {
|
|
308
|
+
return join(directory, path);
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
let redirectsMatcher = generateRedirectsMatcher(redirectsFile);
|
|
313
|
+
let headersMatcher = generateHeadersMatcher(headersFile);
|
|
314
|
+
|
|
315
|
+
watch([headersFile, redirectsFile], {
|
|
316
|
+
persistent: true,
|
|
317
|
+
}).on("change", (path) => {
|
|
318
|
+
switch (path) {
|
|
319
|
+
case headersFile: {
|
|
320
|
+
log.log("_headers modified. Re-evaluating...");
|
|
321
|
+
headersMatcher = generateHeadersMatcher(headersFile);
|
|
322
|
+
break;
|
|
323
|
+
}
|
|
324
|
+
case redirectsFile: {
|
|
325
|
+
log.log("_redirects modified. Re-evaluating...");
|
|
326
|
+
redirectsMatcher = generateRedirectsMatcher(redirectsFile);
|
|
327
|
+
break;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
const serveAsset = (file: string) => {
|
|
333
|
+
return readFileSync(file);
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
const generateResponse = (request: MiniflareRequest) => {
|
|
337
|
+
const url = new URL(request.url);
|
|
338
|
+
|
|
339
|
+
const deconstructedResponse: {
|
|
340
|
+
status: number;
|
|
341
|
+
headers: MiniflareHeaders;
|
|
342
|
+
body?: Buffer;
|
|
343
|
+
} = {
|
|
344
|
+
status: 200,
|
|
345
|
+
headers: new Headers(),
|
|
346
|
+
body: undefined,
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
const match = redirectsMatcher(request);
|
|
350
|
+
if (match) {
|
|
351
|
+
const { status, to } = match;
|
|
352
|
+
|
|
353
|
+
let location = to;
|
|
354
|
+
let search;
|
|
355
|
+
|
|
356
|
+
if (to.startsWith("/")) {
|
|
357
|
+
search = new URL(location, "http://fakehost").search;
|
|
358
|
+
} else {
|
|
359
|
+
search = new URL(location).search;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
location = `${location}${search ? "" : url.search}`;
|
|
363
|
+
|
|
364
|
+
if (status && [301, 302, 303, 307, 308].includes(status)) {
|
|
365
|
+
deconstructedResponse.status = status;
|
|
366
|
+
} else {
|
|
367
|
+
deconstructedResponse.status = 302;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
deconstructedResponse.headers.set("Location", location);
|
|
371
|
+
return deconstructedResponse;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
if (!request.method?.match(/^(get|head)$/i)) {
|
|
375
|
+
deconstructedResponse.status = 405;
|
|
376
|
+
return deconstructedResponse;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
const notFound = () => {
|
|
380
|
+
let cwd = url.pathname;
|
|
381
|
+
while (cwd) {
|
|
382
|
+
cwd = cwd.slice(0, cwd.lastIndexOf("/"));
|
|
383
|
+
|
|
384
|
+
if ((asset = getAsset(`${cwd}/404.html`))) {
|
|
385
|
+
deconstructedResponse.status = 404;
|
|
386
|
+
deconstructedResponse.body = serveAsset(asset);
|
|
387
|
+
deconstructedResponse.headers.set(
|
|
388
|
+
"Content-Type",
|
|
389
|
+
getType(asset) || "application/octet-stream"
|
|
390
|
+
);
|
|
391
|
+
return deconstructedResponse;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if ((asset = getAsset(`/index.html`))) {
|
|
396
|
+
deconstructedResponse.body = serveAsset(asset);
|
|
397
|
+
deconstructedResponse.headers.set(
|
|
398
|
+
"Content-Type",
|
|
399
|
+
getType(asset) || "application/octet-stream"
|
|
400
|
+
);
|
|
401
|
+
return deconstructedResponse;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
deconstructedResponse.status = 404;
|
|
405
|
+
return deconstructedResponse;
|
|
406
|
+
};
|
|
407
|
+
|
|
408
|
+
let asset;
|
|
409
|
+
|
|
410
|
+
if (url.pathname.endsWith("/")) {
|
|
411
|
+
if ((asset = getAsset(`${url.pathname}/index.html`))) {
|
|
412
|
+
deconstructedResponse.body = serveAsset(asset);
|
|
413
|
+
deconstructedResponse.headers.set(
|
|
414
|
+
"Content-Type",
|
|
415
|
+
getType(asset) || "application/octet-stream"
|
|
416
|
+
);
|
|
417
|
+
return deconstructedResponse;
|
|
418
|
+
} else if (
|
|
419
|
+
(asset = getAsset(`${url.pathname.replace(/\/$/, ".html")}`))
|
|
420
|
+
) {
|
|
421
|
+
deconstructedResponse.status = 301;
|
|
422
|
+
deconstructedResponse.headers.set(
|
|
423
|
+
"Location",
|
|
424
|
+
`${url.pathname.slice(0, -1)}${url.search}`
|
|
425
|
+
);
|
|
426
|
+
return deconstructedResponse;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
if (url.pathname.endsWith("/index")) {
|
|
431
|
+
deconstructedResponse.status = 301;
|
|
432
|
+
deconstructedResponse.headers.set(
|
|
433
|
+
"Location",
|
|
434
|
+
`${url.pathname.slice(0, -"index".length)}${url.search}`
|
|
435
|
+
);
|
|
436
|
+
return deconstructedResponse;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
if ((asset = getAsset(url.pathname))) {
|
|
440
|
+
if (url.pathname.endsWith(".html")) {
|
|
441
|
+
const extensionlessPath = url.pathname.slice(0, -".html".length);
|
|
442
|
+
if (getAsset(extensionlessPath) || extensionlessPath === "/") {
|
|
443
|
+
deconstructedResponse.body = serveAsset(asset);
|
|
444
|
+
deconstructedResponse.headers.set(
|
|
445
|
+
"Content-Type",
|
|
446
|
+
getType(asset) || "application/octet-stream"
|
|
447
|
+
);
|
|
448
|
+
return deconstructedResponse;
|
|
449
|
+
} else {
|
|
450
|
+
deconstructedResponse.status = 301;
|
|
451
|
+
deconstructedResponse.headers.set(
|
|
452
|
+
"Location",
|
|
453
|
+
`${extensionlessPath}${url.search}`
|
|
454
|
+
);
|
|
455
|
+
return deconstructedResponse;
|
|
456
|
+
}
|
|
457
|
+
} else {
|
|
458
|
+
deconstructedResponse.body = serveAsset(asset);
|
|
459
|
+
deconstructedResponse.headers.set(
|
|
460
|
+
"Content-Type",
|
|
461
|
+
getType(asset) || "application/octet-stream"
|
|
462
|
+
);
|
|
463
|
+
return deconstructedResponse;
|
|
464
|
+
}
|
|
465
|
+
} else if (hasFileExtension(url.pathname)) {
|
|
466
|
+
notFound();
|
|
467
|
+
return deconstructedResponse;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
if ((asset = getAsset(`${url.pathname}.html`))) {
|
|
471
|
+
deconstructedResponse.body = serveAsset(asset);
|
|
472
|
+
deconstructedResponse.headers.set(
|
|
473
|
+
"Content-Type",
|
|
474
|
+
getType(asset) || "application/octet-stream"
|
|
475
|
+
);
|
|
476
|
+
return deconstructedResponse;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
if ((asset = getAsset(`${url.pathname}/index.html`))) {
|
|
480
|
+
deconstructedResponse.status = 301;
|
|
481
|
+
deconstructedResponse.headers.set(
|
|
482
|
+
"Location",
|
|
483
|
+
`${url.pathname}/${url.search}`
|
|
484
|
+
);
|
|
485
|
+
return deconstructedResponse;
|
|
486
|
+
} else {
|
|
487
|
+
notFound();
|
|
488
|
+
return deconstructedResponse;
|
|
489
|
+
}
|
|
490
|
+
};
|
|
491
|
+
|
|
492
|
+
const attachHeaders = (
|
|
493
|
+
request: MiniflareRequest,
|
|
494
|
+
deconstructedResponse: {
|
|
495
|
+
status: number;
|
|
496
|
+
headers: MiniflareHeaders;
|
|
497
|
+
body?: Buffer;
|
|
498
|
+
}
|
|
499
|
+
) => {
|
|
500
|
+
const headers = deconstructedResponse.headers;
|
|
501
|
+
const newHeaders = new Headers({});
|
|
502
|
+
const matches = headersMatcher(request) || [];
|
|
503
|
+
|
|
504
|
+
matches.forEach((match) => {
|
|
505
|
+
Object.entries(match).forEach(([name, value]) => {
|
|
506
|
+
newHeaders.append(name, `${value}`);
|
|
507
|
+
});
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
const combinedHeaders = {
|
|
511
|
+
...Object.fromEntries(headers.entries()),
|
|
512
|
+
...Object.fromEntries(newHeaders.entries()),
|
|
513
|
+
};
|
|
514
|
+
|
|
515
|
+
deconstructedResponse.headers = new Headers({});
|
|
516
|
+
Object.entries(combinedHeaders).forEach(([name, value]) => {
|
|
517
|
+
if (value) deconstructedResponse.headers.set(name, value);
|
|
518
|
+
});
|
|
519
|
+
};
|
|
520
|
+
|
|
521
|
+
return async (input: RequestInfo, init?: RequestInit) => {
|
|
522
|
+
const request = new Request(input, init);
|
|
523
|
+
const deconstructedResponse = generateResponse(request);
|
|
524
|
+
attachHeaders(request, deconstructedResponse);
|
|
525
|
+
|
|
526
|
+
const headers = new Headers();
|
|
527
|
+
|
|
528
|
+
[...deconstructedResponse.headers.entries()].forEach(([name, value]) => {
|
|
529
|
+
if (value) headers.set(name, value);
|
|
530
|
+
});
|
|
531
|
+
|
|
532
|
+
return new Response(deconstructedResponse.body, {
|
|
533
|
+
headers,
|
|
534
|
+
status: deconstructedResponse.status,
|
|
535
|
+
});
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
const invalidAssetsFetch: typeof miniflareFetch = () => {
|
|
540
|
+
throw new Error(
|
|
541
|
+
"Trying to fetch assets directly when there is no `directory` option specified."
|
|
542
|
+
);
|
|
543
|
+
};
|
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
import { Log, LogLevel, Miniflare } from "miniflare";
|
|
2
2
|
import yargs from "yargs";
|
|
3
3
|
import { hideBin } from "yargs/helpers";
|
|
4
|
+
import generateASSETSBinding from "./assets";
|
|
4
5
|
import { enumKeys } from "./enum-keys";
|
|
5
6
|
import { getRequestContextCheckOptions } from "./request-context";
|
|
7
|
+
import type { Options } from "./assets";
|
|
8
|
+
|
|
9
|
+
export interface EnablePagesAssetsServiceBindingOptions {
|
|
10
|
+
proxyPort?: number;
|
|
11
|
+
directory?: string;
|
|
12
|
+
}
|
|
6
13
|
|
|
7
14
|
// miniflare defines this but importing it throws:
|
|
8
15
|
// Dynamic require of "path" is not supported
|
|
@@ -29,24 +36,49 @@ async function main() {
|
|
|
29
36
|
...requestContextCheckOptions,
|
|
30
37
|
};
|
|
31
38
|
//miniflare's logLevel 0 still logs routes, so lets override the logger
|
|
32
|
-
config.log = config.disableLogs
|
|
39
|
+
config.log = config.disableLogs
|
|
40
|
+
? new NoOpLog()
|
|
41
|
+
: new Log(logLevel, config.logOptions);
|
|
33
42
|
|
|
34
43
|
if (logLevel > LogLevel.INFO) {
|
|
35
44
|
console.log("OPTIONS:\n", JSON.stringify(config, null, 2));
|
|
36
45
|
}
|
|
37
46
|
|
|
38
|
-
|
|
47
|
+
let mf: Miniflare | undefined;
|
|
39
48
|
|
|
40
49
|
try {
|
|
50
|
+
if (args._[1]) {
|
|
51
|
+
const opts: EnablePagesAssetsServiceBindingOptions = JSON.parse(
|
|
52
|
+
args._[1] as string
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
if (isNaN(opts.proxyPort || NaN) && !opts.directory) {
|
|
56
|
+
throw new Error(
|
|
57
|
+
"MiniflareCLIOptions: built in service bindings set to true, but no port or directory provided"
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const options: Options = {
|
|
62
|
+
log: config.log,
|
|
63
|
+
proxyPort: opts.proxyPort,
|
|
64
|
+
directory: opts.directory,
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
config.serviceBindings = {
|
|
68
|
+
...config.serviceBindings,
|
|
69
|
+
ASSETS: await generateASSETSBinding(options),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
mf = new Miniflare(config);
|
|
41
73
|
// Start Miniflare development server
|
|
42
74
|
await mf.startServer();
|
|
43
75
|
await mf.startScheduler();
|
|
44
76
|
process.send && process.send("ready");
|
|
45
77
|
} catch (e) {
|
|
46
|
-
mf
|
|
78
|
+
mf?.log.error(e as Error);
|
|
47
79
|
process.exitCode = 1;
|
|
48
80
|
// Unmount any mounted workers
|
|
49
|
-
await mf
|
|
81
|
+
await mf?.dispose();
|
|
50
82
|
}
|
|
51
83
|
}
|
|
52
84
|
|
package/src/module-collection.ts
CHANGED
|
@@ -123,9 +123,9 @@ export default function createModuleCollector(props: {
|
|
|
123
123
|
{
|
|
124
124
|
filter: new RegExp(
|
|
125
125
|
"^(" +
|
|
126
|
-
[...props.wrangler1xlegacyModuleReferences.fileNames]
|
|
127
|
-
|
|
128
|
-
|
|
126
|
+
[...props.wrangler1xlegacyModuleReferences.fileNames]
|
|
127
|
+
.map((name) => name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
|
|
128
|
+
.join("|") +
|
|
129
129
|
")$"
|
|
130
130
|
),
|
|
131
131
|
},
|
package/src/pages/constants.ts
CHANGED
|
@@ -3,5 +3,6 @@ export const MAX_BUCKET_SIZE = 50 * 1024 * 1024;
|
|
|
3
3
|
export const MAX_BUCKET_FILE_COUNT = 5000;
|
|
4
4
|
export const BULK_UPLOAD_CONCURRENCY = 3;
|
|
5
5
|
export const MAX_UPLOAD_ATTEMPTS = 5;
|
|
6
|
+
export const MAX_CHECK_MISSING_ATTEMPTS = 5;
|
|
6
7
|
export const SECONDS_TO_WAIT_FOR_PROXY = 5;
|
|
7
8
|
export const isInPagesCI = !!process.env.CF_PAGES;
|