wrangler 2.0.6 → 2.0.9

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 (46) hide show
  1. package/README.md +1 -1
  2. package/bin/wrangler.js +16 -4
  3. package/package.json +6 -4
  4. package/pages/functions/buildPlugin.ts +13 -0
  5. package/pages/functions/buildWorker.ts +13 -0
  6. package/src/__tests__/configuration.test.ts +132 -60
  7. package/src/__tests__/dev.test.tsx +168 -67
  8. package/src/__tests__/helpers/mock-dialogs.ts +41 -1
  9. package/src/__tests__/index.test.ts +25 -10
  10. package/src/__tests__/init.test.ts +252 -131
  11. package/src/__tests__/kv.test.ts +16 -16
  12. package/src/__tests__/package-manager.test.ts +154 -7
  13. package/src/__tests__/pages.test.ts +442 -38
  14. package/src/__tests__/parse.test.ts +5 -1
  15. package/src/__tests__/publish.test.ts +377 -84
  16. package/src/__tests__/secret.test.ts +4 -4
  17. package/src/__tests__/whoami.test.tsx +34 -0
  18. package/src/abort.d.ts +3 -0
  19. package/src/cfetch/index.ts +21 -4
  20. package/src/cfetch/internal.ts +20 -18
  21. package/src/config/config.ts +1 -1
  22. package/src/config/index.ts +162 -0
  23. package/src/config/validation.ts +77 -29
  24. package/src/create-worker-preview.ts +32 -22
  25. package/src/dev/dev.tsx +6 -16
  26. package/src/dev/remote.tsx +40 -16
  27. package/src/dialogs.tsx +48 -0
  28. package/src/durable.ts +102 -0
  29. package/src/index.tsx +291 -207
  30. package/src/inspect.ts +39 -0
  31. package/src/kv.ts +74 -25
  32. package/src/open-in-browser.ts +5 -12
  33. package/src/package-manager.ts +50 -3
  34. package/src/pages.tsx +218 -61
  35. package/src/parse.ts +21 -4
  36. package/src/proxy.ts +38 -22
  37. package/src/publish.ts +166 -108
  38. package/src/sites.tsx +8 -8
  39. package/src/user.tsx +12 -1
  40. package/src/whoami.tsx +3 -2
  41. package/src/worker.ts +2 -1
  42. package/src/zones.ts +73 -0
  43. package/templates/new-worker-scheduled.js +17 -0
  44. package/templates/new-worker-scheduled.ts +32 -0
  45. package/templates/new-worker.ts +16 -1
  46. package/wrangler-dist/cli.js +33066 -20052
package/src/dev/dev.tsx CHANGED
@@ -10,9 +10,9 @@ import onExit from "signal-exit";
10
10
  import tmp from "tmp-promise";
11
11
  import { fetch } from "undici";
12
12
  import { runCustomBuild } from "../entry";
13
+ import { openInspector } from "../inspect";
13
14
  import { logger } from "../logger";
14
15
  import openInBrowser from "../open-in-browser";
15
- import { getAPIToken } from "../user";
16
16
  import { Local } from "./local";
17
17
  import { Remote } from "./remote";
18
18
  import { useEsbuild } from "./use-esbuild";
@@ -53,16 +53,11 @@ export type DevProps = {
53
53
  };
54
54
  env: string | undefined;
55
55
  legacyEnv: boolean;
56
- zone:
57
- | {
58
- id: string;
59
- host: string;
60
- }
61
- | undefined;
56
+ zone: string | undefined;
57
+ host: string | undefined;
62
58
  };
63
59
 
64
60
  export function DevImplementation(props: DevProps): JSX.Element {
65
- const apiToken = props.initialMode === "remote" ? getAPIToken() : undefined;
66
61
  const directory = useTmpDir();
67
62
 
68
63
  useCustomBuild(props.entry, props.build);
@@ -107,19 +102,17 @@ export function DevImplementation(props: DevProps): JSX.Element {
107
102
  // only load the UI if we're running in a supported environment
108
103
  const { isRawModeSupported } = useStdin();
109
104
  return isRawModeSupported ? (
110
- <InteractiveDevSession {...props} bundle={bundle} apiToken={apiToken} />
105
+ <InteractiveDevSession {...props} bundle={bundle} />
111
106
  ) : (
112
107
  <DevSession
113
108
  {...props}
114
109
  bundle={bundle}
115
- apiToken={apiToken}
116
110
  local={props.initialMode === "local"}
117
111
  />
118
112
  );
119
113
  }
120
114
 
121
115
  type InteractiveDevSessionProps = DevProps & {
122
- apiToken: string | undefined;
123
116
  bundle: EsbuildBundle | undefined;
124
117
  };
125
118
 
@@ -182,7 +175,6 @@ function DevSession(props: DevSessionProps) {
182
175
  bundle={props.bundle}
183
176
  format={props.entry.format}
184
177
  accountId={props.accountId}
185
- apiToken={props.apiToken}
186
178
  bindings={props.bindings}
187
179
  assetPaths={props.assetPaths}
188
180
  public={props.public}
@@ -196,6 +188,7 @@ function DevSession(props: DevSessionProps) {
196
188
  env={props.env}
197
189
  legacyEnv={props.legacyEnv}
198
190
  zone={props.zone}
191
+ host={props.host}
199
192
  />
200
193
  );
201
194
  }
@@ -378,10 +371,7 @@ function useHotkeys(
378
371
  }
379
372
  // toggle inspector
380
373
  case "d": {
381
- await openInBrowser(
382
- `https://built-devtools.pages.dev/js_app?experiments=true&v8only=true&ws=localhost:${inspectorPort}/ws`,
383
- { forceChromium: true }
384
- );
374
+ await openInspector(inspectorPort);
385
375
  break;
386
376
  }
387
377
  // toggle local
@@ -1,4 +1,3 @@
1
- import assert from "node:assert";
2
1
  import { readFile } from "node:fs/promises";
3
2
  import path from "node:path";
4
3
  import { useState, useEffect, useRef } from "react";
@@ -7,6 +6,7 @@ import useInspector from "../inspect";
7
6
  import { logger } from "../logger";
8
7
  import { usePreviewServer } from "../proxy";
9
8
  import { syncAssets } from "../sites";
9
+ import { requireApiToken, requireAuth } from "../user";
10
10
  import type { CfPreviewToken } from "../create-worker-preview";
11
11
  import type { AssetPaths } from "../sites";
12
12
  import type { CfModule, CfWorkerInit, CfScriptFormat } from "../worker";
@@ -23,24 +23,21 @@ export function Remote(props: {
23
23
  localProtocol: "https" | "http";
24
24
  inspectorPort: number;
25
25
  accountId: string | undefined;
26
- apiToken: string | undefined;
27
26
  bindings: CfWorkerInit["bindings"];
28
27
  compatibilityDate: string;
29
28
  compatibilityFlags: string[] | undefined;
30
29
  usageModel: "bundled" | "unbound" | undefined;
31
30
  env: string | undefined;
32
31
  legacyEnv: boolean | undefined;
33
- zone: { id: string; host: string } | undefined;
32
+ zone: string | undefined;
33
+ host: string | undefined;
34
34
  }) {
35
- assert(props.accountId, "accountId is required");
36
- assert(props.apiToken, "apiToken is required");
37
35
  const previewToken = useWorker({
38
36
  name: props.name,
39
37
  bundle: props.bundle,
40
38
  format: props.format,
41
39
  modules: props.bundle ? props.bundle.modules : [],
42
40
  accountId: props.accountId,
43
- apiToken: props.apiToken,
44
41
  bindings: props.bindings,
45
42
  assetPaths: props.assetPaths,
46
43
  port: props.port,
@@ -50,6 +47,7 @@ export function Remote(props: {
50
47
  env: props.env,
51
48
  legacyEnv: props.legacyEnv,
52
49
  zone: props.zone,
50
+ host: props.host,
53
51
  });
54
52
 
55
53
  usePreviewServer({
@@ -73,8 +71,7 @@ export function useWorker(props: {
73
71
  bundle: EsbuildBundle | undefined;
74
72
  format: CfScriptFormat | undefined;
75
73
  modules: CfModule[];
76
- accountId: string;
77
- apiToken: string;
74
+ accountId: string | undefined;
78
75
  bindings: CfWorkerInit["bindings"];
79
76
  assetPaths: AssetPaths | undefined;
80
77
  port: number;
@@ -83,7 +80,8 @@ export function useWorker(props: {
83
80
  usageModel: "bundled" | "unbound" | undefined;
84
81
  env: string | undefined;
85
82
  legacyEnv: boolean | undefined;
86
- zone: { id: string; host: string } | undefined;
83
+ zone: string | undefined;
84
+ host: string | undefined;
87
85
  }): CfPreviewToken | undefined {
88
86
  const {
89
87
  name,
@@ -91,7 +89,6 @@ export function useWorker(props: {
91
89
  format,
92
90
  modules,
93
91
  accountId,
94
- apiToken,
95
92
  bindings,
96
93
  assetPaths,
97
94
  compatibilityDate,
@@ -105,6 +102,9 @@ export function useWorker(props: {
105
102
  // something's "happened" in our system; We make a ref and
106
103
  // mark it once we log our initial message. Refs are vars!
107
104
  const startedRef = useRef(false);
105
+ // This ref holds the actual accountId being used, the `accountId` prop could be undefined
106
+ // as it is only what is retrieved from the wrangler.toml config.
107
+ const accountIdRef = useRef(accountId);
108
108
 
109
109
  useEffect(() => {
110
110
  const abortController = new AbortController();
@@ -119,8 +119,13 @@ export function useWorker(props: {
119
119
  logger.log("⎔ Detected changes, restarted server.");
120
120
  }
121
121
 
122
+ // Ensure we have an account id, even if it means logging in here.
123
+ accountIdRef.current = await requireAuth({
124
+ account_id: accountIdRef.current,
125
+ });
126
+
122
127
  const assets = await syncAssets(
123
- accountId,
128
+ accountIdRef.current,
124
129
  // When we're using the newer service environments, we wouldn't
125
130
  // have added the env name on to the script name. However, we must
126
131
  // include it in the kv namespace name regardless (since there's no
@@ -173,10 +178,15 @@ export function useWorker(props: {
173
178
  await createWorkerPreview(
174
179
  init,
175
180
  {
176
- accountId,
177
- apiToken,
181
+ accountId: accountIdRef.current,
182
+ apiToken: requireApiToken(),
183
+ },
184
+ {
185
+ env: props.env,
186
+ legacyEnv: props.legacyEnv,
187
+ zone: props.zone,
188
+ host: props.host,
178
189
  },
179
- { env: props.env, legacyEnv: props.legacyEnv, zone: props.zone },
180
190
  abortController.signal
181
191
  )
182
192
  );
@@ -185,7 +195,21 @@ export function useWorker(props: {
185
195
  // we want to log the error, but not end the process
186
196
  // since it could recover after the developer fixes whatever's wrong
187
197
  if ((err as { code: string }).code !== "ABORT_ERR") {
188
- logger.error("Error on remote worker:", err);
198
+ // instead of logging the raw API error to the user,
199
+ // give them friendly instructions
200
+ // for error 10063 (workers.dev subdomain required)
201
+ if (err.code === 10063) {
202
+ const errorMessage =
203
+ "Error: You need to register a workers.dev subdomain before running the dev command in remote mode";
204
+ const solutionMessage =
205
+ "You can either enable local mode by pressing l, or register a workers.dev subdomain here:";
206
+ const onboardingLink = `https://dash.cloudflare.com/${accountIdRef.current}/workers/onboarding`;
207
+ logger.error(
208
+ `${errorMessage}\n${solutionMessage}\n${onboardingLink}`
209
+ );
210
+ } else {
211
+ logger.error("Error on remote worker:", err);
212
+ }
189
213
  }
190
214
  });
191
215
 
@@ -197,7 +221,6 @@ export function useWorker(props: {
197
221
  bundle,
198
222
  format,
199
223
  accountId,
200
- apiToken,
201
224
  port,
202
225
  assetPaths,
203
226
  compatibilityDate,
@@ -208,6 +231,7 @@ export function useWorker(props: {
208
231
  props.env,
209
232
  props.legacyEnv,
210
233
  props.zone,
234
+ props.host,
211
235
  ]);
212
236
  return token;
213
237
  }
package/src/dialogs.tsx CHANGED
@@ -1,5 +1,6 @@
1
1
  import chalk from "chalk";
2
2
  import { Box, Text, useInput, render } from "ink";
3
+ import SelectInput from "ink-select-input";
3
4
  import TextInput from "ink-text-input";
4
5
  import * as React from "react";
5
6
  import { useState } from "react";
@@ -85,3 +86,50 @@ export async function prompt(
85
86
  );
86
87
  });
87
88
  }
89
+
90
+ type SelectOption = {
91
+ value: string;
92
+ label: string;
93
+ };
94
+
95
+ type SelectProps = {
96
+ text: string;
97
+ options: SelectOption[];
98
+ initialIndex: number;
99
+ onSelect: (value: string) => void;
100
+ };
101
+
102
+ function Select(props: SelectProps) {
103
+ return (
104
+ <Box flexDirection="column">
105
+ <Text>{props.text}</Text>
106
+ <SelectInput
107
+ initialIndex={props.initialIndex}
108
+ items={props.options}
109
+ onSelect={async (selected) => {
110
+ props.onSelect(selected.value);
111
+ }}
112
+ />
113
+ </Box>
114
+ );
115
+ }
116
+
117
+ export function select(
118
+ text: string,
119
+ options: SelectOption[],
120
+ initialIndex: number
121
+ ): Promise<string> {
122
+ return new Promise((resolve) => {
123
+ const { unmount } = render(
124
+ <Select
125
+ text={text}
126
+ options={options}
127
+ initialIndex={initialIndex}
128
+ onSelect={(option: string) => {
129
+ unmount();
130
+ resolve(option);
131
+ }}
132
+ />
133
+ );
134
+ });
135
+ }
package/src/durable.ts ADDED
@@ -0,0 +1,102 @@
1
+ import assert from "node:assert";
2
+ import { fetchResult } from "./cfetch";
3
+ import { logger } from "./logger";
4
+ import type { Config } from "./config";
5
+ import type { CfWorkerInit } from "./worker";
6
+
7
+ /**
8
+ * For a given Worker + migrations config, figure out which migrations
9
+ * to upload based on the current migration tag of the deployed Worker.
10
+ */
11
+ export async function getMigrationsToUpload(
12
+ scriptName: string,
13
+ props: {
14
+ accountId: string | undefined;
15
+ config: Config;
16
+ legacyEnv: boolean | undefined;
17
+ env: string | undefined;
18
+ }
19
+ ): Promise<CfWorkerInit["migrations"]> {
20
+ const { config, accountId } = props;
21
+
22
+ assert(accountId, "Missing accountId");
23
+ // if config.migrations
24
+ let migrations;
25
+ if (config.migrations.length > 0) {
26
+ // get current migration tag
27
+ type ScriptData = { id: string; migration_tag?: string };
28
+ let script: ScriptData | undefined;
29
+ if (!props.legacyEnv) {
30
+ try {
31
+ if (props.env) {
32
+ const scriptData = await fetchResult<{
33
+ script: ScriptData;
34
+ }>(
35
+ `/accounts/${accountId}/workers/services/${scriptName}/environments/${props.env}`
36
+ );
37
+ script = scriptData.script;
38
+ } else {
39
+ const scriptData = await fetchResult<{
40
+ default_environment: {
41
+ script: ScriptData;
42
+ };
43
+ }>(`/accounts/${accountId}/workers/services/${scriptName}`);
44
+ script = scriptData.default_environment.script;
45
+ }
46
+ } catch (err) {
47
+ if (
48
+ ![
49
+ 10090, // corresponds to workers.api.error.service_not_found, so the script wasn't previously published at all
50
+ 10092, // workers.api.error.environment_not_found, so the script wasn't published to this environment yet
51
+ ].includes((err as { code: number }).code)
52
+ ) {
53
+ throw err;
54
+ }
55
+ // else it's a 404, no script found, and we can proceed
56
+ }
57
+ } else {
58
+ const scripts = await fetchResult<ScriptData[]>(
59
+ `/accounts/${accountId}/workers/scripts`
60
+ );
61
+ script = scripts.find(({ id }) => id === scriptName);
62
+ }
63
+
64
+ if (script?.migration_tag) {
65
+ // was already published once
66
+ const scriptMigrationTag = script.migration_tag;
67
+ const foundIndex = config.migrations.findIndex(
68
+ (migration) => migration.tag === scriptMigrationTag
69
+ );
70
+ if (foundIndex === -1) {
71
+ logger.warn(
72
+ `The published script ${scriptName} has a migration tag "${script.migration_tag}, which was not found in wrangler.toml. You may have already deleted it. Applying all available migrations to the script...`
73
+ );
74
+ migrations = {
75
+ old_tag: script.migration_tag,
76
+ new_tag: config.migrations[config.migrations.length - 1].tag,
77
+ steps: config.migrations.map(({ tag: _tag, ...rest }) => rest),
78
+ };
79
+ } else {
80
+ if (foundIndex !== config.migrations.length - 1) {
81
+ // there are new migrations to send up
82
+ migrations = {
83
+ old_tag: script.migration_tag,
84
+ new_tag: config.migrations[config.migrations.length - 1].tag,
85
+ steps: config.migrations
86
+ .slice(foundIndex + 1)
87
+ .map(({ tag: _tag, ...rest }) => rest),
88
+ };
89
+ }
90
+ // else, we're up to date, no migrations to send
91
+ }
92
+ } else {
93
+ // first time publishing durable objects to this script,
94
+ // so we send all the migrations
95
+ migrations = {
96
+ new_tag: config.migrations[config.migrations.length - 1].tag,
97
+ steps: config.migrations.map(({ tag: _tag, ...rest }) => rest),
98
+ };
99
+ }
100
+ }
101
+ return migrations;
102
+ }