wrangler 0.0.0-e928f94 → 0.0.0-e978986

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 (124) hide show
  1. package/Cloudflare_CA.pem +18 -0
  2. package/README.md +27 -14
  3. package/bin/wrangler.js +48 -4
  4. package/miniflare-dist/index.mjs +4886 -0
  5. package/package.json +74 -34
  6. package/pages/functions/buildPlugin.ts +105 -0
  7. package/pages/functions/buildWorker.ts +151 -0
  8. package/pages/functions/filepath-routing.test.ts +234 -0
  9. package/pages/functions/filepath-routing.ts +189 -0
  10. package/pages/functions/identifiers.ts +78 -0
  11. package/pages/functions/routes.ts +156 -0
  12. package/pages/functions/template-plugin.ts +147 -0
  13. package/pages/functions/template-worker.ts +143 -0
  14. package/src/__tests__/config-cache.test.ts +36 -0
  15. package/src/__tests__/configuration.test.ts +3374 -0
  16. package/src/__tests__/dev.test.tsx +980 -0
  17. package/src/__tests__/guess-worker-format.test.ts +85 -0
  18. package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
  19. package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
  20. package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
  21. package/src/__tests__/helpers/mock-account-id.ts +48 -0
  22. package/src/__tests__/helpers/mock-bin.ts +36 -0
  23. package/src/__tests__/helpers/mock-cfetch.ts +211 -0
  24. package/src/__tests__/helpers/mock-console.ts +87 -0
  25. package/src/__tests__/helpers/mock-dialogs.ts +130 -0
  26. package/src/__tests__/helpers/mock-http-server.ts +46 -0
  27. package/src/__tests__/helpers/mock-istty.ts +27 -0
  28. package/src/__tests__/helpers/mock-kv.ts +33 -0
  29. package/src/__tests__/helpers/mock-oauth-flow.ts +255 -0
  30. package/src/__tests__/helpers/mock-stdin.ts +103 -0
  31. package/src/__tests__/helpers/mock-web-socket.ts +29 -0
  32. package/src/__tests__/helpers/run-in-tmp.ts +39 -0
  33. package/src/__tests__/helpers/run-wrangler.ts +16 -0
  34. package/src/__tests__/helpers/write-worker-source.ts +31 -0
  35. package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
  36. package/src/__tests__/https-options.test.ts +130 -0
  37. package/src/__tests__/index.test.ts +233 -130
  38. package/src/__tests__/init.test.ts +1567 -0
  39. package/src/__tests__/jest.setup.ts +77 -0
  40. package/src/__tests__/kv.test.ts +1481 -0
  41. package/src/__tests__/logger.test.ts +115 -0
  42. package/src/__tests__/package-manager.test.ts +353 -0
  43. package/src/__tests__/pages.test.ts +657 -0
  44. package/src/__tests__/parse.test.ts +330 -0
  45. package/src/__tests__/publish.test.ts +5468 -0
  46. package/src/__tests__/r2.test.ts +233 -0
  47. package/src/__tests__/route.test.ts +45 -0
  48. package/src/__tests__/secret.test.ts +481 -0
  49. package/src/__tests__/tail.test.ts +690 -0
  50. package/src/__tests__/user.test.ts +131 -0
  51. package/src/__tests__/whoami.test.tsx +105 -0
  52. package/src/abort.d.ts +3 -0
  53. package/src/bundle.ts +200 -0
  54. package/src/cfetch/index.ts +121 -0
  55. package/src/cfetch/internal.ts +145 -0
  56. package/src/cli.ts +10 -7
  57. package/src/config/README.md +107 -0
  58. package/src/config/config.ts +234 -0
  59. package/src/config/diagnostics.ts +80 -0
  60. package/src/config/environment.ts +420 -0
  61. package/src/config/index.ts +225 -0
  62. package/src/config/validation-helpers.ts +575 -0
  63. package/src/config/validation.ts +1539 -0
  64. package/src/config-cache.ts +45 -0
  65. package/src/create-worker-preview.ts +185 -0
  66. package/src/create-worker-upload-form.ts +254 -0
  67. package/src/dev/dev-vars.ts +36 -0
  68. package/src/dev/dev.tsx +419 -0
  69. package/src/dev/local.tsx +318 -0
  70. package/src/dev/remote.tsx +227 -0
  71. package/src/dev/use-esbuild.ts +118 -0
  72. package/src/dialogs.tsx +67 -17
  73. package/src/durable.ts +102 -0
  74. package/src/entry.ts +322 -0
  75. package/src/environment-variables.ts +35 -0
  76. package/src/errors.ts +11 -0
  77. package/src/https-options.ts +123 -0
  78. package/src/index.tsx +2031 -1041
  79. package/src/inspect.ts +662 -0
  80. package/src/intl-polyfill.d.ts +139 -0
  81. package/src/kv.ts +408 -0
  82. package/src/logger.ts +66 -0
  83. package/src/miniflare-cli/README.md +30 -0
  84. package/src/miniflare-cli/enum-keys.ts +17 -0
  85. package/src/miniflare-cli/index.ts +41 -0
  86. package/src/miniflare-cli/request-context.ts +40 -0
  87. package/src/module-collection.ts +229 -35
  88. package/src/open-in-browser.ts +19 -0
  89. package/src/package-manager.ts +174 -0
  90. package/src/pages.tsx +1444 -260
  91. package/src/parse.ts +202 -0
  92. package/src/paths.ts +26 -0
  93. package/src/proxy.ts +465 -0
  94. package/src/publish.ts +498 -261
  95. package/src/r2.ts +50 -0
  96. package/src/selfsigned.d.ts +29 -0
  97. package/src/sites.tsx +240 -60
  98. package/src/tail/filters.ts +279 -0
  99. package/src/tail/index.ts +299 -0
  100. package/src/tail/printing.ts +67 -0
  101. package/src/update-check.ts +19 -0
  102. package/src/user.tsx +395 -221
  103. package/src/whoami.tsx +70 -0
  104. package/src/worker.ts +180 -0
  105. package/templates/gitignore +171 -0
  106. package/templates/new-worker-scheduled.js +17 -0
  107. package/templates/new-worker-scheduled.ts +32 -0
  108. package/templates/new-worker.js +15 -0
  109. package/templates/new-worker.ts +30 -0
  110. package/{static-asset-facade.js → templates/static-asset-facade.js} +1 -1
  111. package/templates/tsconfig.json +105 -0
  112. package/wrangler-dist/cli.js +118298 -116056
  113. package/src/__tests__/fixtures/init/.gitkeep +0 -0
  114. package/src/__tests__/mock-cfetch.js +0 -46
  115. package/src/api/form_data.ts +0 -141
  116. package/src/api/inspect.ts +0 -441
  117. package/src/api/preview.ts +0 -128
  118. package/src/api/worker.ts +0 -161
  119. package/src/cfetch.ts +0 -72
  120. package/src/config.ts +0 -122
  121. package/src/dev.tsx +0 -724
  122. package/src/kv.tsx +0 -211
  123. package/src/tail.tsx +0 -71
  124. package/wrangler-dist/cli.js.map +0 -7
@@ -1,153 +1,256 @@
1
- import * as fs from "node:fs";
2
- import * as fsp from "node:fs/promises";
3
- import * as path from "node:path";
4
- import * as TOML from "@iarna/toml";
5
- import { main } from "../index";
6
- // @ts-expect-error we're mocking cfetch, so of course setMock isn't a thing
7
- import { setMock, unsetAllMocks } from "../cfetch";
8
-
9
- jest.mock("../cfetch", () => {
10
- return jest.requireActual("./mock-cfetch");
11
- });
1
+ import { getPackageManager } from "../package-manager";
2
+ import { mockConsoleMethods } from "./helpers/mock-console";
3
+ import { runInTempDir } from "./helpers/run-in-tmp";
4
+ import { runWrangler } from "./helpers/run-wrangler";
5
+ import { writeWorkerSource } from "./helpers/write-worker-source";
6
+ import writeWranglerToml from "./helpers/write-wrangler-toml";
7
+ import type { PackageManager } from "../package-manager";
12
8
 
13
- async function w(cmd: void | string, options?: { tap: boolean }) {
14
- const tapped = options?.tap ? tap() : undefined;
15
- await main([...(cmd ? cmd.split(" ") : [])]);
16
- tapped?.off();
17
- return { stdout: tapped?.out, stderr: tapped?.err };
18
- }
9
+ describe("wrangler", () => {
10
+ let mockPackageManager: PackageManager;
11
+ runInTempDir();
19
12
 
20
- function tap() {
21
- const oldLog = console.log;
22
- const oldError = console.error;
23
-
24
- const toReturn = {
25
- off: () => {
26
- console.log = oldLog;
27
- console.error = oldError;
28
- },
29
- out: "",
30
- err: "",
31
- };
32
-
33
- console.log = (...args) => {
34
- toReturn.out += args.join("");
35
- oldLog.apply(console, args);
36
- // console.trace(...args); // use this if you want to find the true source of your console.log
37
- };
38
- console.error = (...args) => {
39
- toReturn.err += args.join("");
40
- oldError.apply(console, args);
41
- };
42
-
43
- return toReturn;
44
- }
13
+ beforeEach(() => {
14
+ mockPackageManager = {
15
+ cwd: process.cwd(),
16
+ // @ts-expect-error we're making a fake package manager here
17
+ type: "mockpm",
18
+ addDevDeps: jest.fn(),
19
+ install: jest.fn(),
20
+ };
21
+ (getPackageManager as jest.Mock).mockResolvedValue(mockPackageManager);
22
+ });
45
23
 
46
- describe("wrangler", () => {
47
- it("should run", async () => {
48
- const { stdout } = await w(undefined, { tap: true });
49
-
50
- expect(stdout).toMatchInlineSnapshot(`
51
- "wrangler
52
-
53
- Commands:
54
- wrangler init [name] 📥 Create a wrangler.toml configuration file
55
- wrangler dev <filename> 👂 Start a local server for developing your worker
56
- wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
57
- wrangler tail [name] 🦚 Starts a log tailing session for a deployed Worker.
58
- wrangler secret 🤫 Generate a secret that can be referenced in the worker script
59
- wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
60
- wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
61
- wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
62
- wrangler pages ⚡️ Configure Cloudflare Pages
63
-
64
- Flags:
65
- -c, --config Path to .toml configuration file [string]
66
- -h, --help Show help [boolean]
67
- -v, --version Show version number [boolean]
68
-
69
- Options:
70
- -l, --local Run on my machine [boolean] [default: false]"
71
- `);
24
+ const std = mockConsoleMethods();
25
+
26
+ describe("no command", () => {
27
+ it("should display a list of available commands", async () => {
28
+ await runWrangler();
29
+
30
+ expect(std.out).toMatchInlineSnapshot(`
31
+ "wrangler
32
+
33
+ Commands:
34
+ wrangler init [name] 📥 Create a wrangler.toml configuration file
35
+ wrangler dev [script] 👂 Start a local server for developing your worker
36
+ wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
37
+ wrangler tail [name] 🦚 Starts a log tailing session for a published Worker.
38
+ wrangler secret 🤫 Generate a secret that can be referenced in the worker script
39
+ wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
40
+ wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
41
+ wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
42
+ wrangler pages ⚡️ Configure Cloudflare Pages
43
+ wrangler r2 📦 Interact with an R2 store
44
+ wrangler login 🔓 Login to Cloudflare
45
+ wrangler logout 🚪 Logout from Cloudflare
46
+ wrangler whoami 🕵️ Retrieve your user info and test your auth config
47
+
48
+ Flags:
49
+ -c, --config Path to .toml configuration file [string]
50
+ -h, --help Show help [boolean]
51
+ -v, --version Show version number [boolean]"
52
+ `);
53
+
54
+ expect(std.err).toMatchInlineSnapshot(`""`);
55
+ });
72
56
  });
73
57
 
74
- describe("init", () => {
75
- const ogcwd = process.cwd();
58
+ describe("invalid command", () => {
59
+ it("should display an error", async () => {
60
+ await expect(
61
+ runWrangler("invalid-command")
62
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
63
+ `"Unknown argument: invalid-command"`
64
+ );
76
65
 
77
- beforeEach(() => {
78
- process.chdir(path.join(__dirname, "fixtures", "init"));
66
+ expect(std.out).toMatchInlineSnapshot(`
67
+ "
68
+ wrangler
69
+
70
+ Commands:
71
+ wrangler init [name] 📥 Create a wrangler.toml configuration file
72
+ wrangler dev [script] 👂 Start a local server for developing your worker
73
+ wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
74
+ wrangler tail [name] 🦚 Starts a log tailing session for a published Worker.
75
+ wrangler secret 🤫 Generate a secret that can be referenced in the worker script
76
+ wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
77
+ wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
78
+ wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
79
+ wrangler pages ⚡️ Configure Cloudflare Pages
80
+ wrangler r2 📦 Interact with an R2 store
81
+ wrangler login 🔓 Login to Cloudflare
82
+ wrangler logout 🚪 Logout from Cloudflare
83
+ wrangler whoami 🕵️ Retrieve your user info and test your auth config
84
+
85
+ Flags:
86
+ -c, --config Path to .toml configuration file [string]
87
+ -h, --help Show help [boolean]
88
+ -v, --version Show version number [boolean]"
89
+ `);
90
+ expect(std.err).toMatchInlineSnapshot(`
91
+ "X [ERROR] Unknown argument: invalid-command
92
+
93
+ "
94
+ `);
79
95
  });
96
+ });
80
97
 
81
- afterEach(async () => {
82
- await fsp.rm("./wrangler.toml");
83
- process.chdir(ogcwd);
98
+ describe("preview", () => {
99
+ it("should throw an error if the deprecated command is used with positional arguments", async () => {
100
+ await expect(runWrangler("preview GET")).rejects
101
+ .toThrowErrorMatchingInlineSnapshot(`
102
+ "Deprecation:
103
+ The \`wrangler preview\` command has been deprecated.
104
+ Try using \`wrangler dev\` to to try out a worker during development.
105
+ "
106
+ `);
107
+ await expect(runWrangler(`preview GET "SomeBody"`)).rejects
108
+ .toThrowErrorMatchingInlineSnapshot(`
109
+ "Deprecation:
110
+ The \`wrangler preview\` command has been deprecated.
111
+ Try using \`wrangler dev\` to to try out a worker during development.
112
+ "
113
+ `);
84
114
  });
115
+ });
116
+
117
+ describe("subcommand implicit help ran on incomplete command execution", () => {
118
+ it("no subcommand for 'secret' should display a list of available subcommands", async () => {
119
+ await runWrangler("secret");
120
+ await endEventLoop();
121
+ expect(std.out).toMatchInlineSnapshot(`
122
+ "wrangler secret
123
+
124
+ 🤫 Generate a secret that can be referenced in the worker script
85
125
 
86
- it("should create a wrangler.toml", async () => {
87
- await w("init");
88
- const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
89
- expect(typeof parsed.compatibility_date).toBe("string");
126
+ Commands:
127
+ wrangler secret put <key> Create or update a secret variable for a script
128
+ wrangler secret delete <key> Delete a secret variable from a script
129
+ wrangler secret list List all secrets for a script
130
+
131
+ Flags:
132
+ -c, --config Path to .toml configuration file [string]
133
+ -h, --help Show help [boolean]
134
+ -v, --version Show version number [boolean]"
135
+ `);
90
136
  });
91
137
 
92
- it("should error when wrangler.toml already exists", async () => {
93
- fs.closeSync(fs.openSync("./wrangler.toml", "w"));
94
- const { stderr } = await w("init", { tap: true });
95
- expect(stderr.endsWith("wrangler.toml already exists.")).toBe(true);
138
+ it("no subcommand 'kv:namespace' should display a list of available subcommands", async () => {
139
+ await runWrangler("kv:namespace");
140
+ await endEventLoop();
141
+ expect(std.out).toMatchInlineSnapshot(`
142
+ "wrangler kv:namespace
143
+
144
+ 🗂️ Interact with your Workers KV Namespaces
145
+
146
+ Commands:
147
+ wrangler kv:namespace create <namespace> Create a new namespace
148
+ wrangler kv:namespace list Outputs a list of all KV namespaces associated with your account id.
149
+ wrangler kv:namespace delete Deletes a given namespace.
150
+
151
+ Flags:
152
+ -c, --config Path to .toml configuration file [string]
153
+ -h, --help Show help [boolean]
154
+ -v, --version Show version number [boolean]"
155
+ `);
96
156
  });
97
- });
98
157
 
99
- describe("kv:namespace", () => {
100
- afterAll(() => {
101
- unsetAllMocks();
158
+ it("no subcommand 'kv:key' should display a list of available subcommands", async () => {
159
+ await runWrangler("kv:key");
160
+ await endEventLoop();
161
+ expect(std.out).toMatchInlineSnapshot(`
162
+ "wrangler kv:key
163
+
164
+ 🔑 Individually manage Workers KV key-value pairs
165
+
166
+ Commands:
167
+ wrangler kv:key put <key> [value] Writes a single key/value pair to the given namespace.
168
+ wrangler kv:key list Outputs a list of all keys in a given namespace.
169
+ wrangler kv:key get <key> Reads a single value by key from the given namespace.
170
+ wrangler kv:key delete <key> Removes a single key value pair from the given namespace.
171
+
172
+ Flags:
173
+ -c, --config Path to .toml configuration file [string]
174
+ -h, --help Show help [boolean]
175
+ -v, --version Show version number [boolean]"
176
+ `);
102
177
  });
103
- let KVNamespaces: { title: string; id: string }[] = [];
104
- it("can create a namespace", async () => {
105
- setMock("/accounts/:accountId/storage/kv/namespaces", (uri, init) => {
106
- expect(init.method === "POST");
107
- const body = JSON.parse(init.body);
108
- expect(body.title).toBe("worker-UnitTestNamespace");
109
- KVNamespaces.push({ title: body.title, id: "some-namespace-id" });
110
- return { id: "some-namespace-id" };
111
- });
112
178
 
113
- await w("kv:namespace create UnitTestNamespace");
114
- expect(
115
- KVNamespaces.find((ns) => ns.title === `worker-UnitTestNamespace`)
116
- ).toBeTruthy();
179
+ it("no subcommand 'kv:bulk' should display a list of available subcommands", async () => {
180
+ await runWrangler("kv:bulk");
181
+ await endEventLoop();
182
+ expect(std.out).toMatchInlineSnapshot(`
183
+ "wrangler kv:bulk
184
+
185
+ 💪 Interact with multiple Workers KV key-value pairs at once
186
+
187
+ Commands:
188
+ wrangler kv:bulk put <filename> Upload multiple key-value pairs to a namespace
189
+ wrangler kv:bulk delete <filename> Delete multiple key-value pairs from a namespace
190
+
191
+ Flags:
192
+ -c, --config Path to .toml configuration file [string]
193
+ -h, --help Show help [boolean]
194
+ -v, --version Show version number [boolean]"
195
+ `);
117
196
  });
118
197
 
119
- let createdNamespace: { id: string; title: string };
120
- it("can list namespaces", async () => {
121
- setMock(
122
- "/accounts/:accountId/storage/kv/namespaces\\?:qs",
123
- (uri, init) => {
124
- expect(init).toBe(undefined);
125
- return KVNamespaces;
126
- }
127
- );
128
- const { stdout } = await w("kv:namespace list", { tap: true });
129
- const namespaces = JSON.parse(stdout);
130
- createdNamespace = namespaces.find(
131
- (ns) => ns.title === "worker-UnitTestNamespace"
132
- );
133
- expect(createdNamespace.title).toBe("worker-UnitTestNamespace");
198
+ it("no subcommand 'r2' should display a list of available subcommands", async () => {
199
+ await runWrangler("r2");
200
+ await endEventLoop();
201
+ expect(std.out).toMatchInlineSnapshot(`
202
+ "wrangler r2
203
+
204
+ 📦 Interact with an R2 store
205
+
206
+ Commands:
207
+ wrangler r2 bucket Manage R2 buckets
208
+
209
+ Flags:
210
+ -c, --config Path to .toml configuration file [string]
211
+ -h, --help Show help [boolean]
212
+ -v, --version Show version number [boolean]"
213
+ `);
134
214
  });
215
+ });
135
216
 
136
- it("can delete a namespace", async () => {
137
- const namespaceIdToDelete = createdNamespace.id;
138
- setMock(
139
- "/accounts/:accountId/storage/kv/namespaces/:namespaceId",
140
- (uri, init) => {
141
- expect(init.method).toBe("DELETE");
142
- KVNamespaces = KVNamespaces.filter(
143
- (ns) => ns.id !== namespaceIdToDelete
144
- );
145
- }
146
- );
147
- await w(`kv:namespace delete --namespace-id ${namespaceIdToDelete}`);
148
- expect(KVNamespaces.find((ns) => ns.id === namespaceIdToDelete)).toBe(
149
- undefined
150
- );
217
+ describe("Deprecated commands", () => {
218
+ it("should print a deprecation message for 'generate'", async () => {
219
+ await runWrangler("generate").catch((err) => {
220
+ expect(err.message).toMatchInlineSnapshot(`
221
+ "Deprecation:
222
+ \`wrangler generate\` has been deprecated.
223
+ Try running \`wrangler init\` to generate a basic Worker, or cloning the template repository instead:
224
+
225
+ \`\`\`
226
+ git clone https://github.com/cloudflare/worker-template
227
+ \`\`\`
228
+
229
+ Please refer to https://developers.cloudflare.com/workers/wrangler/deprecations/#generate for more information."
230
+ `);
231
+ });
151
232
  });
152
233
  });
234
+
235
+ it("should print a deprecation message for 'build' and then try to run `publish --dry-run --outdir`", async () => {
236
+ writeWranglerToml({
237
+ main: "index.js",
238
+ });
239
+ writeWorkerSource();
240
+ await runWrangler("build");
241
+ await endEventLoop();
242
+ expect(std.out).toMatchInlineSnapshot(`
243
+ "▲ [WARNING] Deprecation: \`wrangler build\` has been deprecated.
244
+
245
+ Please refer to https://developers.cloudflare.com/workers/wrangler/migration/deprecations/#build for more information.
246
+ Attempting to run \`wrangler publish --dry-run --outdir=dist\` for you instead:
247
+
248
+
249
+ --dry-run: exiting now."
250
+ `);
251
+ });
153
252
  });
253
+
254
+ function endEventLoop() {
255
+ return new Promise((resolve) => setImmediate(resolve));
256
+ }