wrangler 2.0.19 → 2.0.23

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.
@@ -53,28 +53,40 @@ export type EventNames =
53
53
  | "run dev"
54
54
  | "run pages dev";
55
55
 
56
- export function sendMetricsEvent(event: EventNames): void;
57
- export function sendMetricsEvent(
58
- event: EventNames,
59
- options: MetricsConfigOptions
60
- ): void;
56
+ /**
57
+ * Send a metrics event, with no extra properties, to Cloudflare, if usage tracking is enabled.
58
+ *
59
+ * This overload assumes that you do not want to configure analytics with options.
60
+ */
61
+ export function sendMetricsEvent(event: EventNames): Promise<void>;
62
+ /**
63
+ * Send a metrics event, with no extra properties, to Cloudflare, if usage tracking is enabled.
64
+ */
61
65
  export function sendMetricsEvent(
62
66
  event: EventNames,
63
- properties: Properties,
64
67
  options: MetricsConfigOptions
65
- ): void;
68
+ ): Promise<void>;
66
69
  /**
67
70
  * Send a metrics event to Cloudflare, if usage tracking is enabled.
71
+ *
72
+ * Generally you should pass the `send_metrics` property from the wrangler.toml config here,
73
+ * which would override any user permissions.
68
74
  */
69
75
  export function sendMetricsEvent(
76
+ event: EventNames,
77
+ properties: Properties,
78
+ options: MetricsConfigOptions
79
+ ): Promise<void>;
80
+ export async function sendMetricsEvent(
70
81
  event: EventNames,
71
82
  ...args: [] | [MetricsConfigOptions] | [Properties, MetricsConfigOptions]
72
- ): void {
73
- const options = args.pop() ?? {};
74
- const properties = (args.pop() ?? {}) as Properties;
75
- getMetricsDispatcher(options)
76
- .then((metricsDispatcher) => metricsDispatcher.sendEvent(event, properties))
77
- .catch((err) => {
78
- logger.debug("Error sending metrics event", err);
79
- });
83
+ ): Promise<void> {
84
+ try {
85
+ const options = args.pop() ?? {};
86
+ const properties = (args.pop() ?? {}) as Properties;
87
+ const metricsDispatcher = await getMetricsDispatcher(options);
88
+ await metricsDispatcher.sendEvent(event, properties);
89
+ } catch (err) {
90
+ logger.debug("Error sending metrics event", err);
91
+ }
80
92
  }
@@ -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 ? new NoOpLog() : new Log(logLevel);
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
- const mf = new Miniflare(config);
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.log.error(e as Error);
78
+ mf?.log.error(e as Error);
47
79
  process.exitCode = 1;
48
80
  // Unmount any mounted workers
49
- await mf.dispose();
81
+ await mf?.dispose();
50
82
  }
51
83
  }
52
84
 
@@ -121,7 +121,7 @@ export const Handler = async ({
121
121
  buildOutputDirectory,
122
122
  nodeCompat,
123
123
  });
124
- metrics.sendMetricsEvent("build pages functions");
124
+ await metrics.sendMetricsEvent("build pages functions");
125
125
  };
126
126
 
127
127
  export async function buildFunctions({
@@ -98,6 +98,9 @@ export async function ListHandler({
98
98
  account_id: accountId,
99
99
  });
100
100
 
101
- render(<Table data={data}></Table>, { patchConsole: false });
102
- metrics.sendMetricsEvent("list pages projects deployments");
101
+ const { unmount } = render(<Table data={data}></Table>, {
102
+ patchConsole: false,
103
+ });
104
+ unmount();
105
+ await metrics.sendMetricsEvent("list pages projects deployments");
103
106
  }