wrangler 0.0.0-e29a241 → 0.0.0-e2f9bb2

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 (88) hide show
  1. package/README.md +25 -14
  2. package/bin/wrangler.js +13 -1
  3. package/miniflare-dist/index.mjs +4828 -21
  4. package/package.json +29 -24
  5. package/pages/functions/buildPlugin.ts +92 -0
  6. package/pages/functions/buildWorker.ts +80 -4
  7. package/pages/functions/filepath-routing.test.ts +11 -0
  8. package/pages/functions/filepath-routing.ts +8 -0
  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 +692 -321
  14. package/src/__tests__/dev.test.tsx +284 -18
  15. package/src/__tests__/guess-worker-format.test.ts +28 -1
  16. package/src/__tests__/helpers/mock-cfetch.ts +5 -1
  17. package/src/__tests__/helpers/mock-console.ts +20 -3
  18. package/src/__tests__/helpers/mock-dialogs.ts +24 -0
  19. package/src/__tests__/helpers/mock-http-server.ts +46 -0
  20. package/src/__tests__/helpers/mock-istty.ts +27 -0
  21. package/src/__tests__/helpers/mock-oauth-flow.ts +255 -0
  22. package/src/__tests__/helpers/run-in-tmp.ts +7 -0
  23. package/src/__tests__/helpers/write-worker-source.ts +31 -0
  24. package/src/__tests__/https-options.test.ts +5 -2
  25. package/src/__tests__/index.test.ts +27 -769
  26. package/src/__tests__/init.test.ts +1042 -0
  27. package/src/__tests__/jest.setup.ts +4 -4
  28. package/src/__tests__/kv.test.ts +133 -61
  29. package/src/__tests__/logger.test.ts +115 -0
  30. package/src/__tests__/pages.test.ts +311 -26
  31. package/src/__tests__/publish.test.ts +2789 -618
  32. package/src/__tests__/r2.test.ts +26 -10
  33. package/src/__tests__/route.test.ts +4 -4
  34. package/src/__tests__/secret.test.ts +55 -34
  35. package/src/__tests__/tail.test.ts +146 -20
  36. package/src/__tests__/user.test.ts +131 -0
  37. package/src/__tests__/whoami.test.tsx +14 -9
  38. package/src/bundle.ts +30 -3
  39. package/src/cfetch/internal.ts +0 -2
  40. package/src/cli.ts +5 -11
  41. package/src/config/README.md +1 -1
  42. package/src/config/config.ts +13 -53
  43. package/src/config/environment.ts +88 -4
  44. package/src/config/index.ts +11 -4
  45. package/src/config/validation-helpers.ts +96 -7
  46. package/src/config/validation.ts +407 -79
  47. package/src/config-cache.ts +45 -0
  48. package/src/create-worker-preview.ts +22 -13
  49. package/src/create-worker-upload-form.ts +32 -9
  50. package/src/dev/dev-vars.ts +36 -0
  51. package/src/dev/dev.tsx +132 -78
  52. package/src/dev/local.tsx +115 -29
  53. package/src/dev/remote.tsx +23 -13
  54. package/src/dev/use-esbuild.ts +23 -5
  55. package/src/dialogs.tsx +16 -6
  56. package/src/entry.ts +83 -24
  57. package/src/environment-variables.ts +3 -1
  58. package/src/errors.ts +11 -0
  59. package/src/https-options.ts +3 -2
  60. package/src/index.tsx +619 -377
  61. package/src/inspect.ts +17 -11
  62. package/src/intl-polyfill.d.ts +139 -0
  63. package/src/logger.ts +66 -0
  64. package/src/miniflare-cli/index.ts +4 -0
  65. package/src/miniflare-cli/request-context.ts +40 -0
  66. package/src/module-collection.ts +7 -7
  67. package/src/open-in-browser.ts +5 -4
  68. package/src/package-manager.ts +9 -8
  69. package/src/pages.tsx +883 -62
  70. package/src/parse.ts +1 -1
  71. package/src/proxy.ts +25 -11
  72. package/src/publish.ts +263 -141
  73. package/src/sites.tsx +27 -14
  74. package/src/tail/index.ts +2 -2
  75. package/src/tail/printing.ts +13 -8
  76. package/src/update-check.ts +19 -0
  77. package/src/user.tsx +116 -41
  78. package/src/whoami.tsx +2 -1
  79. package/src/worker.ts +15 -6
  80. package/templates/gitignore +171 -0
  81. package/templates/static-asset-facade.js +1 -1
  82. package/wrangler-dist/cli.js +21167 -50229
  83. package/miniflare-dist/index.mjs.map +0 -7
  84. package/src/__tests__/dev2.test.tsx +0 -154
  85. package/src/__tests__/logout.test.ts +0 -52
  86. package/src/__tests__/sentry.test.ts +0 -146
  87. package/src/reporting.ts +0 -131
  88. 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,15 +55,28 @@ 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
  );
51
61
 
52
- if (format === "service-worker" && hasDurableObjectImplementations(config)) {
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) {
53
75
  const errorMessage =
54
- "You seem to be trying to use Durable Objects in a Worker written with Service Worker syntax.";
76
+ "You seem to be trying to use Durable Objects in a Worker written as a service-worker.";
55
77
  const addScriptName =
56
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:";
57
- const addScriptNameExamples = generateAddScriptNameExamples(config);
79
+ const addScriptNameExamples = generateAddScriptNameExamples(localBindings);
58
80
  const migrateText =
59
81
  "Alternatively, migrate your worker to ES Module syntax to implement a Durable Object in this Worker:";
60
82
  const migrateUrl =
@@ -73,12 +95,13 @@ export async function runCustomBuild(
73
95
  ) {
74
96
  if (build?.command) {
75
97
  // TODO: add a deprecation message here?
76
- console.log("running:", build.command);
98
+ logger.log("Running custom build:", build.command);
77
99
  await execaCommand(build.command, {
78
100
  shell: true,
101
+ // we keep these two as "inherit" so that
102
+ // logs are still visible.
79
103
  stdout: "inherit",
80
104
  stderr: "inherit",
81
- timeout: 1000 * 30,
82
105
  ...(build.cwd && { cwd: build.cwd }),
83
106
  });
84
107
 
@@ -104,7 +127,8 @@ export async function runCustomBuild(
104
127
  export default async function guessWorkerFormat(
105
128
  entryFile: string,
106
129
  entryWorkingDirectory: string,
107
- hint: CfScriptFormat | undefined
130
+ hint: CfScriptFormat | undefined,
131
+ tsconfig?: string | undefined
108
132
  ): Promise<CfScriptFormat> {
109
133
  const result = await esbuild.build({
110
134
  entryPoints: [entryFile],
@@ -112,7 +136,14 @@ export default async function guessWorkerFormat(
112
136
  metafile: true,
113
137
  bundle: false,
114
138
  format: "esm",
139
+ target: "es2020",
115
140
  write: false,
141
+ loader: {
142
+ ".js": "jsx",
143
+ ".mjs": "jsx",
144
+ ".cjs": "jsx",
145
+ },
146
+ ...(tsconfig && { tsconfig }),
116
147
  });
117
148
  // result.metafile is defined because of the `metafile: true` option above.
118
149
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
@@ -130,14 +161,30 @@ export default async function guessWorkerFormat(
130
161
  "More than one entry-point found for generated bundle." +
131
162
  listEntryPoints(entryPoints)
132
163
  );
133
- const guessedWorkerFormat =
134
- 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
+ }
135
182
 
136
183
  if (hint) {
137
184
  if (hint !== guessedWorkerFormat) {
138
185
  if (hint === "service-worker") {
139
186
  throw new Error(
140
- "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."
141
188
  );
142
189
  } else {
143
190
  throw new Error(
@@ -174,15 +221,26 @@ export function fileExists(filePath: string): boolean {
174
221
  return false;
175
222
  }
176
223
 
224
+ type DurableObjectBindings = Config["durable_objects"]["bindings"];
225
+
177
226
  /**
178
- * Returns true if the given config contains Durable Object bindings that are implemented
179
- * in this worker instead of being implemented elsewhere, and bound via a `script_name`
180
- * property in wrangler.toml
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.
181
229
  */
182
- function hasDurableObjectImplementations(config: Config): boolean {
183
- return config.durable_objects.bindings.some(
184
- (binding) => binding.script_name === undefined
185
- );
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 };
186
244
  }
187
245
 
188
246
  /**
@@ -190,13 +248,14 @@ function hasDurableObjectImplementations(config: Config): boolean {
190
248
  * config indicating how the user can add a `script_name` field to bind an
191
249
  * externally defined Durable Object.
192
250
  */
193
- function generateAddScriptNameExamples(config: Config): string {
251
+ function generateAddScriptNameExamples(
252
+ localBindings: DurableObjectBindings
253
+ ): string {
194
254
  function exampleScriptName(binding_name: string): string {
195
255
  return `${binding_name.toLowerCase().replaceAll("_", "-")}-worker`;
196
256
  }
197
257
 
198
- return config.durable_objects.bindings
199
- .filter((binding) => binding.script_name === undefined)
258
+ return localBindings
200
259
  .map(({ name, class_name }) => {
201
260
  const script_name = exampleScriptName(name);
202
261
  const currentBinding = `{ name = ${name}, class_name = ${class_name} }`;
@@ -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
  }