wrangler 0.0.0-ece06ea → 0.0.0-ed08df4

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 (121) hide show
  1. package/Cloudflare_CA.pem +18 -0
  2. package/README.md +27 -14
  3. package/bin/wrangler.js +34 -2
  4. package/miniflare-dist/index.mjs +4886 -0
  5. package/package.json +71 -36
  6. package/pages/functions/buildPlugin.ts +92 -0
  7. package/pages/functions/buildWorker.ts +138 -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 +3125 -0
  16. package/src/__tests__/dev.test.tsx +829 -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 +90 -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 +221 -132
  38. package/src/__tests__/init.test.ts +1516 -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 +206 -0
  43. package/src/__tests__/pages.test.ts +327 -0
  44. package/src/__tests__/parse.test.ts +326 -0
  45. package/src/__tests__/publish.test.ts +4996 -0
  46. package/src/__tests__/r2.test.ts +210 -0
  47. package/src/__tests__/route.test.ts +45 -0
  48. package/src/__tests__/secret.test.ts +446 -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/bundle.ts +169 -0
  53. package/src/cfetch/index.ts +104 -0
  54. package/src/cfetch/internal.ts +140 -0
  55. package/src/cli.ts +10 -7
  56. package/src/config/README.md +107 -0
  57. package/src/config/config.ts +234 -0
  58. package/src/config/diagnostics.ts +80 -0
  59. package/src/config/environment.ts +394 -0
  60. package/src/config/index.ts +63 -0
  61. package/src/config/validation-helpers.ts +575 -0
  62. package/src/config/validation.ts +1424 -0
  63. package/src/config-cache.ts +45 -0
  64. package/src/create-worker-preview.ts +175 -0
  65. package/src/create-worker-upload-form.ts +240 -0
  66. package/src/dev/dev-vars.ts +36 -0
  67. package/src/dev/dev.tsx +421 -0
  68. package/src/dev/local.tsx +312 -0
  69. package/src/dev/remote.tsx +213 -0
  70. package/src/dev/use-esbuild.ts +118 -0
  71. package/src/dialogs.tsx +22 -20
  72. package/src/entry.ts +322 -0
  73. package/src/environment-variables.ts +35 -0
  74. package/src/errors.ts +11 -0
  75. package/src/https-options.ts +123 -0
  76. package/src/index.tsx +1869 -1020
  77. package/src/inspect.ts +623 -0
  78. package/src/intl-polyfill.d.ts +139 -0
  79. package/src/kv.ts +344 -0
  80. package/src/logger.ts +66 -0
  81. package/src/miniflare-cli/README.md +30 -0
  82. package/src/miniflare-cli/enum-keys.ts +17 -0
  83. package/src/miniflare-cli/index.ts +41 -0
  84. package/src/miniflare-cli/request-context.ts +40 -0
  85. package/src/module-collection.ts +258 -0
  86. package/src/open-in-browser.ts +26 -0
  87. package/src/package-manager.ts +127 -0
  88. package/src/pages.tsx +1756 -218
  89. package/src/parse.ts +185 -0
  90. package/src/paths.ts +26 -0
  91. package/src/proxy.ts +449 -0
  92. package/src/publish.ts +377 -262
  93. package/src/r2.ts +50 -0
  94. package/src/selfsigned.d.ts +29 -0
  95. package/src/sites.tsx +237 -59
  96. package/src/tail/filters.ts +279 -0
  97. package/src/tail/index.ts +299 -0
  98. package/src/tail/printing.ts +67 -0
  99. package/src/update-check.ts +19 -0
  100. package/src/user.tsx +395 -221
  101. package/src/whoami.tsx +70 -0
  102. package/src/worker.ts +172 -0
  103. package/templates/gitignore +171 -0
  104. package/templates/new-worker.js +15 -0
  105. package/templates/new-worker.ts +15 -0
  106. package/{static-asset-facade.js → templates/static-asset-facade.js} +1 -1
  107. package/templates/tsconfig.json +105 -0
  108. package/wrangler-dist/cli.js +108921 -132528
  109. package/src/__tests__/fixtures/init/.gitkeep +0 -0
  110. package/src/__tests__/mock-cfetch.js +0 -46
  111. package/src/api/form_data.ts +0 -158
  112. package/src/api/inspect.ts +0 -441
  113. package/src/api/preview.ts +0 -123
  114. package/src/api/worker.ts +0 -161
  115. package/src/cfetch.ts +0 -72
  116. package/src/config.ts +0 -124
  117. package/src/dev.tsx +0 -736
  118. package/src/kv.tsx +0 -211
  119. package/src/tail.tsx +0 -71
  120. package/src/util/fetch.ts +0 -74
  121. package/wrangler-dist/cli.js.map +0 -7
@@ -1,153 +1,242 @@
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
- });
12
-
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
- }
19
-
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
- }
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 type { PackageManager } from "../package-manager";
45
6
 
46
7
  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
- --config Path to .toml configuration file [string]
66
- --help Show help [boolean]
67
- --version Show version number [boolean]
68
-
69
- Options:
70
- --local Run on my machine [boolean] [default: false]"
71
- `);
8
+ let mockPackageManager: PackageManager;
9
+ runInTempDir();
10
+
11
+ beforeEach(() => {
12
+ mockPackageManager = {
13
+ cwd: process.cwd(),
14
+ // @ts-expect-error we're making a fake package manager here
15
+ type: "mockpm",
16
+ addDevDeps: jest.fn(),
17
+ install: jest.fn(),
18
+ };
19
+ (getPackageManager as jest.Mock).mockResolvedValue(mockPackageManager);
72
20
  });
73
21
 
74
- describe("init", () => {
75
- const ogcwd = process.cwd();
76
-
77
- beforeEach(() => {
78
- process.chdir(path.join(__dirname, "fixtures", "init"));
22
+ const std = mockConsoleMethods();
23
+
24
+ describe("no command", () => {
25
+ it("should display a list of available commands", async () => {
26
+ await runWrangler();
27
+
28
+ expect(std.out).toMatchInlineSnapshot(`
29
+ "wrangler
30
+
31
+ Commands:
32
+ wrangler init [name] 📥 Create a wrangler.toml configuration file
33
+ wrangler dev [script] 👂 Start a local server for developing your worker
34
+ wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
35
+ wrangler tail [name] 🦚 Starts a log tailing session for a published Worker.
36
+ wrangler secret 🤫 Generate a secret that can be referenced in the worker script
37
+ wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
38
+ wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
39
+ wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
40
+ wrangler pages ⚡️ Configure Cloudflare Pages
41
+ wrangler r2 📦 Interact with an R2 store
42
+ wrangler login 🔓 Login to Cloudflare
43
+ wrangler logout 🚪 Logout from Cloudflare
44
+ wrangler whoami 🕵️ Retrieve your user info and test your auth config
45
+
46
+ Flags:
47
+ -c, --config Path to .toml configuration file [string]
48
+ -h, --help Show help [boolean]
49
+ -v, --version Show version number [boolean]"
50
+ `);
51
+
52
+ expect(std.err).toMatchInlineSnapshot(`""`);
79
53
  });
54
+ });
80
55
 
81
- afterEach(async () => {
82
- await fsp.rm("./wrangler.toml");
83
- process.chdir(ogcwd);
84
- });
56
+ describe("invalid command", () => {
57
+ it("should display an error", async () => {
58
+ await expect(
59
+ runWrangler("invalid-command")
60
+ ).rejects.toThrowErrorMatchingInlineSnapshot(
61
+ `"Unknown argument: invalid-command"`
62
+ );
85
63
 
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");
64
+ expect(std.out).toMatchInlineSnapshot(`
65
+ "
66
+ "
67
+ `);
68
+ expect(std.err).toMatchInlineSnapshot(`
69
+ "wrangler
70
+
71
+ Commands:
72
+ wrangler init [name] 📥 Create a wrangler.toml configuration file
73
+ wrangler dev [script] 👂 Start a local server for developing your worker
74
+ wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
75
+ wrangler tail [name] 🦚 Starts a log tailing session for a published Worker.
76
+ wrangler secret 🤫 Generate a secret that can be referenced in the worker script
77
+ wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
78
+ wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
79
+ wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
80
+ wrangler pages ⚡️ Configure Cloudflare Pages
81
+ wrangler r2 📦 Interact with an R2 store
82
+ wrangler login 🔓 Login to Cloudflare
83
+ wrangler logout 🚪 Logout from Cloudflare
84
+ wrangler whoami 🕵️ Retrieve your user info and test your auth config
85
+
86
+ Flags:
87
+ -c, --config Path to .toml configuration file [string]
88
+ -h, --help Show help [boolean]
89
+ -v, --version Show version number [boolean]
90
+ X [ERROR] Unknown argument: invalid-command
91
+
92
+ "
93
+ `);
90
94
  });
95
+ });
91
96
 
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);
97
+ describe("preview", () => {
98
+ it("should throw an error if the deprecated command is used with positional arguments", async () => {
99
+ await expect(runWrangler("preview GET")).rejects
100
+ .toThrowErrorMatchingInlineSnapshot(`
101
+ "Deprecation:
102
+ The \`wrangler preview\` command has been deprecated.
103
+ Try using \`wrangler dev\` to to try out a worker during development.
104
+ "
105
+ `);
106
+ await expect(runWrangler(`preview GET "SomeBody"`)).rejects
107
+ .toThrowErrorMatchingInlineSnapshot(`
108
+ "Deprecation:
109
+ The \`wrangler preview\` command has been deprecated.
110
+ Try using \`wrangler dev\` to to try out a worker during development.
111
+ "
112
+ `);
96
113
  });
97
114
  });
98
115
 
99
- describe("kv:namespace", () => {
100
- afterAll(() => {
101
- unsetAllMocks();
116
+ describe("subcommand implicit help ran on incomplete command execution", () => {
117
+ function endEventLoop() {
118
+ return new Promise((resolve) => setImmediate(resolve));
119
+ }
120
+ it("no subcommand for 'secret' should display a list of available subcommands", async () => {
121
+ await runWrangler("secret");
122
+ await endEventLoop();
123
+ expect(std.out).toMatchInlineSnapshot(`
124
+ "wrangler secret
125
+
126
+ 🤫 Generate a secret that can be referenced in the worker script
127
+
128
+ Commands:
129
+ wrangler secret put <key> Create or update a secret variable for a script
130
+ wrangler secret delete <key> Delete a secret variable from a script
131
+ wrangler secret list List all secrets for a script
132
+
133
+ Flags:
134
+ -c, --config Path to .toml configuration file [string]
135
+ -h, --help Show help [boolean]
136
+ -v, --version Show version number [boolean]"
137
+ `);
102
138
  });
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
139
 
113
- await w("kv:namespace create UnitTestNamespace");
114
- expect(
115
- KVNamespaces.find((ns) => ns.title === `worker-UnitTestNamespace`)
116
- ).toBeTruthy();
140
+ it("no subcommand 'kv:namespace' should display a list of available subcommands", async () => {
141
+ await runWrangler("kv:namespace");
142
+ await endEventLoop();
143
+ expect(std.out).toMatchInlineSnapshot(`
144
+ "wrangler kv:namespace
145
+
146
+ 🗂️ Interact with your Workers KV Namespaces
147
+
148
+ Commands:
149
+ wrangler kv:namespace create <namespace> Create a new namespace
150
+ wrangler kv:namespace list Outputs a list of all KV namespaces associated with your account id.
151
+ wrangler kv:namespace delete Deletes a given namespace.
152
+
153
+ Flags:
154
+ -c, --config Path to .toml configuration file [string]
155
+ -h, --help Show help [boolean]
156
+ -v, --version Show version number [boolean]"
157
+ `);
117
158
  });
118
159
 
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");
160
+ it("no subcommand 'kv:key' should display a list of available subcommands", async () => {
161
+ await runWrangler("kv:key");
162
+ await endEventLoop();
163
+ expect(std.out).toMatchInlineSnapshot(`
164
+ "wrangler kv:key
165
+
166
+ 🔑 Individually manage Workers KV key-value pairs
167
+
168
+ Commands:
169
+ wrangler kv:key put <key> [value] Writes a single key/value pair to the given namespace.
170
+ wrangler kv:key list Outputs a list of all keys in a given namespace.
171
+ wrangler kv:key get <key> Reads a single value by key from the given namespace.
172
+ wrangler kv:key delete <key> Removes a single key value pair from the given namespace.
173
+
174
+ Flags:
175
+ -c, --config Path to .toml configuration file [string]
176
+ -h, --help Show help [boolean]
177
+ -v, --version Show version number [boolean]"
178
+ `);
134
179
  });
135
180
 
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
- );
181
+ it("no subcommand 'kv:bulk' should display a list of available subcommands", async () => {
182
+ await runWrangler("kv:bulk");
183
+ await endEventLoop();
184
+ expect(std.out).toMatchInlineSnapshot(`
185
+ "wrangler kv:bulk
186
+
187
+ 💪 Interact with multiple Workers KV key-value pairs at once
188
+
189
+ Commands:
190
+ wrangler kv:bulk put <filename> Upload multiple key-value pairs to a namespace
191
+ wrangler kv:bulk delete <filename> Delete multiple key-value pairs from a namespace
192
+
193
+ Flags:
194
+ -c, --config Path to .toml configuration file [string]
195
+ -h, --help Show help [boolean]
196
+ -v, --version Show version number [boolean]"
197
+ `);
198
+ });
199
+ it("no subcommand 'r2' should display a list of available subcommands", async () => {
200
+ await runWrangler("r2");
201
+ await endEventLoop();
202
+ expect(std.out).toMatchInlineSnapshot(`
203
+ "wrangler r2
204
+
205
+ 📦 Interact with an R2 store
206
+
207
+ Commands:
208
+ wrangler r2 bucket Manage R2 buckets
209
+
210
+ Flags:
211
+ -c, --config Path to .toml configuration file [string]
212
+ -h, --help Show help [boolean]
213
+ -v, --version Show version number [boolean]"
214
+ `);
215
+ });
216
+ });
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
+ });
232
+ });
233
+ it("should print a deprecation message for 'build'", async () => {
234
+ await runWrangler("build").catch((err) => {
235
+ expect(err.message).toMatchInlineSnapshot(`
236
+ "Deprecation:
237
+ \`wrangler build\` has been deprecated, please refer to https://developers.cloudflare.com/workers/wrangler/migration/deprecations/#build for alternatives"
238
+ `);
239
+ });
151
240
  });
152
241
  });
153
242
  });