wrangler 0.0.0-e95bea2 → 0.0.0-e95e5a0

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.
Files changed (93) hide show
  1. package/README.md +25 -14
  2. package/bin/wrangler.js +13 -1
  3. package/miniflare-dist/index.mjs +4886 -0
  4. package/package.json +33 -25
  5. package/pages/functions/buildPlugin.ts +92 -0
  6. package/pages/functions/buildWorker.ts +80 -4
  7. package/pages/functions/filepath-routing.test.ts +136 -25
  8. package/pages/functions/filepath-routing.ts +14 -3
  9. package/pages/functions/routes.ts +6 -1
  10. package/pages/functions/template-plugin.ts +146 -0
  11. package/pages/functions/template-worker.ts +21 -8
  12. package/src/__tests__/config-cache.test.ts +36 -0
  13. package/src/__tests__/configuration.test.ts +1179 -271
  14. package/src/__tests__/dev.test.tsx +382 -25
  15. package/src/__tests__/guess-worker-format.test.ts +28 -1
  16. package/src/__tests__/helpers/mock-account-id.ts +4 -0
  17. package/src/__tests__/helpers/mock-cfetch.ts +5 -1
  18. package/src/__tests__/helpers/mock-console.ts +20 -3
  19. package/src/__tests__/helpers/mock-dialogs.ts +26 -1
  20. package/src/__tests__/helpers/mock-http-server.ts +46 -0
  21. package/src/__tests__/helpers/mock-istty.ts +27 -0
  22. package/src/__tests__/helpers/mock-kv.ts +9 -16
  23. package/src/__tests__/helpers/mock-oauth-flow.ts +255 -0
  24. package/src/__tests__/helpers/run-in-tmp.ts +10 -0
  25. package/src/__tests__/helpers/write-worker-source.ts +31 -0
  26. package/src/__tests__/https-options.test.ts +5 -2
  27. package/src/__tests__/index.test.ts +27 -769
  28. package/src/__tests__/init.test.ts +1516 -0
  29. package/src/__tests__/jest.setup.ts +4 -4
  30. package/src/__tests__/kv.test.ts +172 -126
  31. package/src/__tests__/logger.test.ts +115 -0
  32. package/src/__tests__/pages.test.ts +311 -26
  33. package/src/__tests__/publish.test.ts +2946 -524
  34. package/src/__tests__/r2.test.ts +26 -10
  35. package/src/__tests__/route.test.ts +4 -4
  36. package/src/__tests__/secret.test.ts +55 -34
  37. package/src/__tests__/tail.test.ts +146 -20
  38. package/src/__tests__/user.test.ts +131 -0
  39. package/src/__tests__/whoami.test.tsx +26 -7
  40. package/src/bundle.ts +30 -3
  41. package/src/cfetch/internal.ts +9 -7
  42. package/src/cli.ts +5 -11
  43. package/src/config/README.md +7 -7
  44. package/src/config/config.ts +33 -78
  45. package/src/config/environment.ts +93 -4
  46. package/src/config/index.ts +15 -4
  47. package/src/config/validation-helpers.ts +202 -15
  48. package/src/config/validation.ts +530 -133
  49. package/src/config-cache.ts +45 -0
  50. package/src/create-worker-preview.ts +22 -13
  51. package/src/create-worker-upload-form.ts +32 -9
  52. package/src/dev/dev-vars.ts +36 -0
  53. package/src/dev/dev.tsx +136 -75
  54. package/src/dev/local.tsx +196 -125
  55. package/src/dev/remote.tsx +25 -13
  56. package/src/dev/use-esbuild.ts +23 -5
  57. package/src/dialogs.tsx +16 -6
  58. package/src/entry.ts +119 -11
  59. package/src/environment-variables.ts +3 -1
  60. package/src/errors.ts +11 -0
  61. package/src/https-options.ts +3 -2
  62. package/src/index.tsx +739 -400
  63. package/src/inspect.ts +17 -11
  64. package/src/intl-polyfill.d.ts +139 -0
  65. package/src/{kv/index.tsx → kv.ts} +8 -28
  66. package/src/logger.ts +66 -0
  67. package/src/miniflare-cli/README.md +30 -0
  68. package/src/miniflare-cli/enum-keys.ts +17 -0
  69. package/src/miniflare-cli/index.ts +41 -0
  70. package/src/miniflare-cli/request-context.ts +40 -0
  71. package/src/module-collection.ts +63 -58
  72. package/src/open-in-browser.ts +5 -4
  73. package/src/package-manager.ts +9 -8
  74. package/src/pages.tsx +883 -62
  75. package/src/parse.ts +1 -1
  76. package/src/proxy.ts +32 -21
  77. package/src/publish.ts +276 -161
  78. package/src/sites.tsx +88 -33
  79. package/src/tail/index.ts +2 -2
  80. package/src/tail/printing.ts +13 -8
  81. package/src/update-check.ts +19 -0
  82. package/src/user.tsx +231 -103
  83. package/src/whoami.tsx +2 -1
  84. package/src/worker.ts +15 -6
  85. package/templates/gitignore +171 -0
  86. package/templates/static-asset-facade.js +1 -1
  87. package/wrangler-dist/cli.js +22087 -49340
  88. package/src/__tests__/dev2.test.tsx +0 -152
  89. package/src/__tests__/helpers/mock-user.ts +0 -27
  90. package/src/__tests__/logout.test.ts +0 -50
  91. package/src/__tests__/sentry.test.ts +0 -146
  92. package/src/reporting.ts +0 -131
  93. package/wrangler-dist/cli.js.map +0 -7
package/src/entry.ts CHANGED
@@ -3,6 +3,7 @@ import { existsSync } from "node:fs";
3
3
  import path from "node:path";
4
4
  import * as esbuild from "esbuild";
5
5
  import { execaCommand } from "execa";
6
+ import { logger } from "./logger";
6
7
  import type { Config } from "./config";
7
8
  import type { CfScriptFormat } from "./worker";
8
9
  import type { Metafile } from "esbuild";
@@ -20,7 +21,7 @@ export type Entry = { file: string; directory: string; format: CfScriptFormat };
20
21
  export async function getEntry(
21
22
  args: { script?: string; format?: CfScriptFormat | undefined },
22
23
  config: Config,
23
- command: string
24
+ command: "dev" | "publish"
24
25
  ): Promise<Entry> {
25
26
  let file: string;
26
27
  let directory = process.cwd();
@@ -28,9 +29,17 @@ export async function getEntry(
28
29
  // If the script name comes from the command line it is relative to the current working directory.
29
30
  file = path.resolve(args.script);
30
31
  } else if (config.main === undefined) {
31
- throw new Error(
32
- `Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler ${command} path/to/script\`) or the \`main\` config field.`
33
- );
32
+ if (config.site?.["entry-point"]) {
33
+ directory = path.resolve(path.dirname(config.configPath ?? "."));
34
+ file = path.extname(config.site?.["entry-point"])
35
+ ? path.resolve(config.site?.["entry-point"])
36
+ : // site.entry-point could be a directory
37
+ path.resolve(config.site?.["entry-point"], "index.js");
38
+ } else {
39
+ throw new Error(
40
+ `Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler ${command} path/to/script\`) or the \`main\` config field.`
41
+ );
42
+ }
34
43
  } else {
35
44
  directory = path.resolve(path.dirname(config.configPath ?? "."));
36
45
  file = path.resolve(directory, config.main);
@@ -46,8 +55,37 @@ export async function getEntry(
46
55
  const format = await guessWorkerFormat(
47
56
  file,
48
57
  directory,
49
- args.format ?? config.build?.upload?.format
58
+ args.format ?? config.build?.upload?.format,
59
+ config.tsconfig
50
60
  );
61
+
62
+ const { localBindings, remoteBindings } =
63
+ partitionDurableObjectBindings(config);
64
+
65
+ if (command === "dev" && remoteBindings.length > 0) {
66
+ logger.warn(
67
+ "WARNING: You have Durable Object bindings that are not defined locally in the worker being developed.\n" +
68
+ "Be aware that changes to the data stored in these Durable Objects will be permanent and affect the live instances.\n" +
69
+ "Remote Durable Objects that are affected:\n" +
70
+ remoteBindings.map((b) => `- ${JSON.stringify(b)}`).join("\n")
71
+ );
72
+ }
73
+
74
+ if (format === "service-worker" && localBindings.length > 0) {
75
+ const errorMessage =
76
+ "You seem to be trying to use Durable Objects in a Worker written as a service-worker.";
77
+ const addScriptName =
78
+ "You can use Durable Objects defined in other Workers by specifying a `script_name` in your wrangler.toml, where `script_name` is the name of the Worker that implements that Durable Object. For example:";
79
+ const addScriptNameExamples = generateAddScriptNameExamples(localBindings);
80
+ const migrateText =
81
+ "Alternatively, migrate your worker to ES Module syntax to implement a Durable Object in this Worker:";
82
+ const migrateUrl =
83
+ "https://developers.cloudflare.com/workers/learning/migrating-to-module-workers/";
84
+ throw new Error(
85
+ `${errorMessage}\n${addScriptName}\n${addScriptNameExamples}\n${migrateText}\n${migrateUrl}`
86
+ );
87
+ }
88
+
51
89
  return { file, directory, format };
52
90
  }
53
91
 
@@ -57,12 +95,13 @@ export async function runCustomBuild(
57
95
  ) {
58
96
  if (build?.command) {
59
97
  // TODO: add a deprecation message here?
60
- console.log("running:", build.command);
98
+ logger.log("Running custom build:", build.command);
61
99
  await execaCommand(build.command, {
62
100
  shell: true,
101
+ // we keep these two as "inherit" so that
102
+ // logs are still visible.
63
103
  stdout: "inherit",
64
104
  stderr: "inherit",
65
- timeout: 1000 * 30,
66
105
  ...(build.cwd && { cwd: build.cwd }),
67
106
  });
68
107
 
@@ -88,7 +127,8 @@ export async function runCustomBuild(
88
127
  export default async function guessWorkerFormat(
89
128
  entryFile: string,
90
129
  entryWorkingDirectory: string,
91
- hint: CfScriptFormat | undefined
130
+ hint: CfScriptFormat | undefined,
131
+ tsconfig?: string | undefined
92
132
  ): Promise<CfScriptFormat> {
93
133
  const result = await esbuild.build({
94
134
  entryPoints: [entryFile],
@@ -96,7 +136,14 @@ export default async function guessWorkerFormat(
96
136
  metafile: true,
97
137
  bundle: false,
98
138
  format: "esm",
139
+ target: "es2020",
99
140
  write: false,
141
+ loader: {
142
+ ".js": "jsx",
143
+ ".mjs": "jsx",
144
+ ".cjs": "jsx",
145
+ },
146
+ ...(tsconfig && { tsconfig }),
100
147
  });
101
148
  // result.metafile is defined because of the `metafile: true` option above.
102
149
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
@@ -114,14 +161,30 @@ export default async function guessWorkerFormat(
114
161
  "More than one entry-point found for generated bundle." +
115
162
  listEntryPoints(entryPoints)
116
163
  );
117
- const guessedWorkerFormat =
118
- entryPoints[0][1].exports.length > 0 ? "modules" : "service-worker";
164
+
165
+ let guessedWorkerFormat: CfScriptFormat;
166
+ const scriptExports = entryPoints[0][1].exports;
167
+ if (scriptExports.length > 0) {
168
+ if (scriptExports.includes("default")) {
169
+ guessedWorkerFormat = "modules";
170
+ } else {
171
+ logger.warn(
172
+ `The entrypoint ${path.relative(
173
+ process.cwd(),
174
+ entryFile
175
+ )} has exports like an ES Module, but hasn't defined a default export like a module worker normally would. Building the worker using "service-worker" format...`
176
+ );
177
+ guessedWorkerFormat = "service-worker";
178
+ }
179
+ } else {
180
+ guessedWorkerFormat = "service-worker";
181
+ }
119
182
 
120
183
  if (hint) {
121
184
  if (hint !== guessedWorkerFormat) {
122
185
  if (hint === "service-worker") {
123
186
  throw new Error(
124
- "You configured this worker to be a 'service-worker', but the file you are trying to build appears to have ES module exports. Please pass `--format modules`, or simply remove the configuration."
187
+ "You configured this worker to be a 'service-worker', but the file you are trying to build appears to have a `default` export like a module worker. Please pass `--format modules`, or simply remove the configuration."
125
188
  );
126
189
  } else {
127
190
  throw new Error(
@@ -157,3 +220,48 @@ export function fileExists(filePath: string): boolean {
157
220
  }
158
221
  return false;
159
222
  }
223
+
224
+ type DurableObjectBindings = Config["durable_objects"]["bindings"];
225
+
226
+ /**
227
+ * Groups the durable object bindings into two lists:
228
+ * those that are defined locally and those that refer to a durable object defined in another script.
229
+ */
230
+ function partitionDurableObjectBindings(config: Config): {
231
+ localBindings: DurableObjectBindings;
232
+ remoteBindings: DurableObjectBindings;
233
+ } {
234
+ const localBindings: DurableObjectBindings = [];
235
+ const remoteBindings: DurableObjectBindings = [];
236
+ for (const binding of config.durable_objects.bindings) {
237
+ if (binding.script_name === undefined) {
238
+ localBindings.push(binding);
239
+ } else {
240
+ remoteBindings.push(binding);
241
+ }
242
+ }
243
+ return { localBindings, remoteBindings };
244
+ }
245
+
246
+ /**
247
+ * Generates some help text based on the Durable Object bindings in a given
248
+ * config indicating how the user can add a `script_name` field to bind an
249
+ * externally defined Durable Object.
250
+ */
251
+ function generateAddScriptNameExamples(
252
+ localBindings: DurableObjectBindings
253
+ ): string {
254
+ function exampleScriptName(binding_name: string): string {
255
+ return `${binding_name.toLowerCase().replaceAll("_", "-")}-worker`;
256
+ }
257
+
258
+ return localBindings
259
+ .map(({ name, class_name }) => {
260
+ const script_name = exampleScriptName(name);
261
+ const currentBinding = `{ name = ${name}, class_name = ${class_name} }`;
262
+ const fixedBinding = `{ name = ${name}, class_name = ${class_name}, script_name = ${script_name} }`;
263
+
264
+ return `${currentBinding} ==> ${fixedBinding}`;
265
+ })
266
+ .join("\n");
267
+ }
@@ -1,3 +1,5 @@
1
+ import { logger } from "./logger";
2
+
1
3
  /**
2
4
  * Create a function used to access an environment variable.
3
5
  *
@@ -21,7 +23,7 @@ export function getEnvironmentVariableFactory({
21
23
  if (!hasWarned) {
22
24
  // Only show the warning once.
23
25
  hasWarned = true;
24
- console.warn(
26
+ logger.warn(
25
27
  `Using "${deprecatedName}" environment variable. This is deprecated. Please use "${variableName}", instead.`
26
28
  );
27
29
  }
package/src/errors.ts ADDED
@@ -0,0 +1,11 @@
1
+ export class DeprecationError extends Error {
2
+ constructor(message: string) {
3
+ super(`Deprecation:\n${message}`);
4
+ }
5
+ }
6
+
7
+ export class FatalError extends Error {
8
+ constructor(message?: string, readonly code?: number) {
9
+ super(message);
10
+ }
11
+ }
@@ -2,6 +2,7 @@ import * as fs from "node:fs";
2
2
  import { homedir, networkInterfaces } from "node:os";
3
3
  import * as path from "node:path";
4
4
  import { promisify } from "node:util";
5
+ import { logger } from "./logger";
5
6
  import type { Attributes, Options } from "selfsigned";
6
7
 
7
8
  // Most of this file has been borrowed from the implementation in Miniflare.
@@ -26,7 +27,7 @@ export async function getHttpsOptions() {
26
27
  hasCertificateExpired(keyPath, certPath);
27
28
 
28
29
  if (regenerate) {
29
- console.log("Generating new self-signed certificate...");
30
+ logger.log("Generating new self-signed certificate...");
30
31
  const { key, cert } = await generateCertificate();
31
32
  try {
32
33
  // Write certificate files so we can reuse them later.
@@ -35,7 +36,7 @@ export async function getHttpsOptions() {
35
36
  fs.writeFileSync(certPath, cert, "utf8");
36
37
  } catch (e) {
37
38
  const message = e instanceof Error ? e.message : `${e}`;
38
- console.warn(
39
+ logger.warn(
39
40
  `Unable to cache generated self-signed certificate in ${certDirectory}.\n${message}`
40
41
  );
41
42
  }