wrangler 0.0.0-e9a1820 → 0.0.0-e9c2b04a

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 (113) hide show
  1. package/Cloudflare_CA.pem +18 -0
  2. package/README.md +47 -16
  3. package/bin/wrangler.js +122 -31
  4. package/kv-asset-handler.js +1 -0
  5. package/package.json +168 -74
  6. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  7. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  8. package/templates/__tests__/tsconfig.json +8 -0
  9. package/templates/checked-fetch.js +30 -0
  10. package/templates/facade.d.ts +17 -0
  11. package/templates/gitignore +172 -0
  12. package/templates/init-tests/test-jest-new-worker.js +23 -0
  13. package/templates/init-tests/test-vitest-new-worker.js +24 -0
  14. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  15. package/templates/middleware/common.ts +67 -0
  16. package/templates/middleware/loader-modules.ts +137 -0
  17. package/templates/middleware/loader-sw.ts +228 -0
  18. package/templates/middleware/middleware-d1-beta.d.ts +3 -0
  19. package/templates/middleware/middleware-miniflare3-json-error.ts +33 -0
  20. package/templates/middleware/middleware-multiworker-dev.d.ts +4 -0
  21. package/templates/middleware/middleware-multiworker-dev.ts +68 -0
  22. package/templates/middleware/middleware-pretty-error.ts +40 -0
  23. package/templates/middleware/middleware-scheduled.ts +15 -0
  24. package/templates/middleware/middleware-serve-static-assets.d.ts +6 -0
  25. package/templates/middleware/middleware-serve-static-assets.ts +56 -0
  26. package/templates/modules-watch-stub.js +4 -0
  27. package/templates/new-worker-scheduled.js +17 -0
  28. package/templates/new-worker-scheduled.ts +32 -0
  29. package/templates/new-worker.js +15 -0
  30. package/templates/new-worker.ts +33 -0
  31. package/templates/no-op-worker.js +10 -0
  32. package/templates/pages-dev-pipeline.ts +31 -0
  33. package/templates/pages-dev-util.ts +55 -0
  34. package/templates/pages-shim.ts +9 -0
  35. package/templates/pages-template-plugin.ts +190 -0
  36. package/templates/pages-template-worker.ts +198 -0
  37. package/templates/startDevWorker/InspectorProxyWorker.ts +652 -0
  38. package/templates/startDevWorker/ProxyWorker.ts +252 -0
  39. package/templates/tsconfig-sanity.ts +11 -0
  40. package/templates/tsconfig.init.json +105 -0
  41. package/templates/tsconfig.json +8 -0
  42. package/wrangler-dist/InspectorProxyWorker.js +460 -0
  43. package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
  44. package/wrangler-dist/ProxyWorker.js +186 -0
  45. package/wrangler-dist/ProxyWorker.js.map +6 -0
  46. package/wrangler-dist/cli.d.ts +23507 -0
  47. package/wrangler-dist/cli.js +153110 -109544
  48. package/wrangler-dist/wasm-sync.wasm +0 -0
  49. package/import_meta_url.js +0 -3
  50. package/miniflare-config-stubs/.env.empty +0 -0
  51. package/miniflare-config-stubs/package.empty.json +0 -1
  52. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  53. package/src/__tests__/clipboardy-mock.js +0 -4
  54. package/src/__tests__/index.test.ts +0 -287
  55. package/src/__tests__/jest.setup.ts +0 -18
  56. package/src/__tests__/kv.test.ts +0 -322
  57. package/src/__tests__/mock-cfetch.ts +0 -63
  58. package/src/__tests__/mock-dialogs.ts +0 -65
  59. package/src/__tests__/run-in-tmp.ts +0 -19
  60. package/src/__tests__/run-wrangler.ts +0 -32
  61. package/src/api/form_data.ts +0 -131
  62. package/src/api/inspect.ts +0 -430
  63. package/src/api/preview.ts +0 -128
  64. package/src/api/worker.ts +0 -155
  65. package/src/cfetch.ts +0 -72
  66. package/src/cli.ts +0 -9
  67. package/src/config.ts +0 -131
  68. package/src/dev.tsx +0 -879
  69. package/src/dialogs.tsx +0 -77
  70. package/src/index.tsx +0 -1902
  71. package/src/kv.tsx +0 -257
  72. package/src/module-collection.ts +0 -64
  73. package/src/pages.tsx +0 -851
  74. package/src/proxy.ts +0 -103
  75. package/src/publish.ts +0 -350
  76. package/src/sites.tsx +0 -115
  77. package/src/tail.tsx +0 -71
  78. package/src/user.tsx +0 -1022
  79. package/static-asset-facade.js +0 -47
  80. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  81. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  82. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  83. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  84. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  85. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  86. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  87. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  88. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  89. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  90. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  91. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  92. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  93. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  94. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  95. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  96. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  97. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  98. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  99. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  100. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  101. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  102. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  103. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  104. package/vendor/wrangler-mime/LICENSE +0 -21
  105. package/vendor/wrangler-mime/Mime.js +0 -97
  106. package/vendor/wrangler-mime/README.md +0 -187
  107. package/vendor/wrangler-mime/cli.js +0 -46
  108. package/vendor/wrangler-mime/index.js +0 -4
  109. package/vendor/wrangler-mime/lite.js +0 -4
  110. package/vendor/wrangler-mime/package.json +0 -52
  111. package/vendor/wrangler-mime/types/other.js +0 -1
  112. package/vendor/wrangler-mime/types/standard.js +0 -1
  113. package/wrangler-dist/cli.js.map +0 -7
Binary file
@@ -1,3 +0,0 @@
1
- // as per https://github.com/evanw/esbuild/issues/1492
2
- // to make some libs work
3
- export const import_meta_url = require("url").pathToFileURL(__filename);
File without changes
@@ -1 +0,0 @@
1
- {}
File without changes
@@ -1,4 +0,0 @@
1
- // We use this mock during tests because we're having trouble
2
- // properly compiling clipboardy for jest
3
- // We can revisit this if and when we write tests that test
4
- // the copying part of `dev`
@@ -1,287 +0,0 @@
1
- import * as fsp from "node:fs/promises";
2
- import * as TOML from "@iarna/toml";
3
- import { mockConfirm } from "./mock-dialogs";
4
- import { runWrangler } from "./run-wrangler";
5
- import { runInTempDir } from "./run-in-tmp";
6
- import * as fs from "node:fs";
7
-
8
- describe("wrangler", () => {
9
- runInTempDir();
10
-
11
- describe("no command", () => {
12
- it("should display a list of available commands", async () => {
13
- const { stdout, stderr } = await runWrangler();
14
-
15
- expect(stdout).toMatchInlineSnapshot(`
16
- "wrangler
17
-
18
- Commands:
19
- wrangler init [name] 📥 Create a wrangler.toml configuration file
20
- wrangler dev <filename> 👂 Start a local server for developing your worker
21
- wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
22
- wrangler tail [name] 🦚 Starts a log tailing session for a deployed Worker.
23
- wrangler secret 🤫 Generate a secret that can be referenced in the worker script
24
- wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
25
- wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
26
- wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
27
- wrangler pages ⚡️ Configure Cloudflare Pages
28
-
29
- Flags:
30
- -c, --config Path to .toml configuration file [string]
31
- -h, --help Show help [boolean]
32
- -v, --version Show version number [boolean]
33
-
34
- Options:
35
- -l, --local Run on my machine [boolean] [default: false]"
36
- `);
37
-
38
- expect(stderr).toMatchInlineSnapshot(`""`);
39
- });
40
- });
41
-
42
- describe("invalid command", () => {
43
- it("should display an error", async () => {
44
- const { error, stdout, stderr } = await runWrangler("invalid-command");
45
-
46
- expect(stdout).toMatchInlineSnapshot(`""`);
47
- expect(stderr).toMatchInlineSnapshot(`
48
- "wrangler
49
-
50
- Commands:
51
- wrangler init [name] 📥 Create a wrangler.toml configuration file
52
- wrangler dev <filename> 👂 Start a local server for developing your worker
53
- wrangler publish [script] 🆙 Publish your Worker to Cloudflare.
54
- wrangler tail [name] 🦚 Starts a log tailing session for a deployed Worker.
55
- wrangler secret 🤫 Generate a secret that can be referenced in the worker script
56
- wrangler kv:namespace 🗂️ Interact with your Workers KV Namespaces
57
- wrangler kv:key 🔑 Individually manage Workers KV key-value pairs
58
- wrangler kv:bulk 💪 Interact with multiple Workers KV key-value pairs at once
59
- wrangler pages ⚡️ Configure Cloudflare Pages
60
-
61
- Flags:
62
- -c, --config Path to .toml configuration file [string]
63
- -h, --help Show help [boolean]
64
- -v, --version Show version number [boolean]
65
-
66
- Options:
67
- -l, --local Run on my machine [boolean] [default: false]
68
-
69
- Unknown command: invalid-command."
70
- `);
71
- expect(error).toMatchInlineSnapshot(
72
- `[Error: Unknown command: invalid-command.]`
73
- );
74
- });
75
- });
76
-
77
- describe("init", () => {
78
- it("should create a wrangler.toml", async () => {
79
- mockConfirm({
80
- text: "No package.json found. Would you like to create one?",
81
- result: false,
82
- });
83
- await runWrangler("init");
84
- const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
85
- expect(typeof parsed.compatibility_date).toBe("string");
86
- expect(fs.existsSync("./package.json")).toBe(false);
87
- expect(fs.existsSync("./tsconfig.json")).toBe(false);
88
- });
89
-
90
- it("should display warning when wrangler.toml already exists, and exit if user does not want to carry on", async () => {
91
- fs.writeFileSync("./wrangler.toml", "", "utf-8");
92
- mockConfirm({
93
- text: "Do you want to continue initializing this project?",
94
- result: false,
95
- });
96
- const { warnings } = await runWrangler("init");
97
- expect(warnings).toContain("wrangler.toml file already exists!");
98
- const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
99
- expect(typeof parsed.compatibility_date).toBe("undefined");
100
- });
101
-
102
- it("should display warning when wrangler.toml already exists, but continue if user does want to carry on", async () => {
103
- fs.writeFileSync("./wrangler.toml", "", "utf-8");
104
- mockConfirm(
105
- {
106
- text: "Do you want to continue initializing this project?",
107
- result: true,
108
- },
109
- {
110
- text: "No package.json found. Would you like to create one?",
111
- result: false,
112
- }
113
- );
114
- const { warnings } = await runWrangler("init");
115
- expect(warnings).toContain("wrangler.toml file already exists!");
116
- const parsed = TOML.parse(await fsp.readFile("./wrangler.toml", "utf-8"));
117
- expect(typeof parsed.compatibility_date).toBe("string");
118
- });
119
-
120
- it("should create a package.json if none is found and user confirms", async () => {
121
- mockConfirm(
122
- {
123
- text: "No package.json found. Would you like to create one?",
124
- result: true,
125
- },
126
- {
127
- text: "Would you like to use typescript?",
128
- result: false,
129
- }
130
- );
131
- await runWrangler("init");
132
- expect(fs.existsSync("./package.json")).toBe(true);
133
- const packageJson = JSON.parse(
134
- fs.readFileSync("./package.json", "utf-8")
135
- );
136
- expect(packageJson.name).toEqual("worker"); // TODO: should we infer the name from the directory?
137
- expect(packageJson.version).toEqual("0.0.1");
138
- expect(fs.existsSync("./tsconfig.json")).toBe(false);
139
- });
140
-
141
- it("should not touch an existing package.json in the same directory", async () => {
142
- mockConfirm({
143
- text: "Would you like to use typescript?",
144
- result: false,
145
- });
146
-
147
- fs.writeFileSync(
148
- "./package.json",
149
- JSON.stringify({ name: "test", version: "1.0.0" }),
150
- "utf-8"
151
- );
152
-
153
- await runWrangler("init");
154
- const packageJson = JSON.parse(
155
- fs.readFileSync("./package.json", "utf-8")
156
- );
157
- expect(packageJson.name).toEqual("test");
158
- expect(packageJson.version).toEqual("1.0.0");
159
- });
160
-
161
- it("should not touch an existing package.json in an ancestor directory", async () => {
162
- mockConfirm({
163
- text: "Would you like to use typescript?",
164
- result: false,
165
- });
166
-
167
- fs.writeFileSync(
168
- "./package.json",
169
- JSON.stringify({ name: "test", version: "1.0.0" }),
170
- "utf-8"
171
- );
172
-
173
- fs.mkdirSync("./sub-1/sub-2", { recursive: true });
174
- process.chdir("./sub-1/sub-2");
175
-
176
- await runWrangler("init");
177
- expect(fs.existsSync("./package.json")).toBe(false);
178
- expect(fs.existsSync("../../package.json")).toBe(true);
179
-
180
- const packageJson = JSON.parse(
181
- fs.readFileSync("../../package.json", "utf-8")
182
- );
183
- expect(packageJson.name).toEqual("test");
184
- expect(packageJson.version).toEqual("1.0.0");
185
- });
186
-
187
- it("should create a tsconfig.json and install `workers-types` if none is found and user confirms", async () => {
188
- mockConfirm(
189
- {
190
- text: "No package.json found. Would you like to create one?",
191
- result: true,
192
- },
193
- {
194
- text: "Would you like to use typescript?",
195
- result: true,
196
- }
197
- );
198
- await runWrangler("init");
199
- expect(fs.existsSync("./tsconfig.json")).toBe(true);
200
- const tsconfigJson = JSON.parse(
201
- fs.readFileSync("./tsconfig.json", "utf-8")
202
- );
203
- expect(tsconfigJson.compilerOptions.types).toEqual([
204
- "@cloudflare/workers-types",
205
- ]);
206
- const packageJson = JSON.parse(
207
- fs.readFileSync("./package.json", "utf-8")
208
- );
209
- expect(packageJson.devDependencies).toEqual({
210
- "@cloudflare/workers-types": expect.any(String),
211
- });
212
- });
213
-
214
- it("should not touch an existing tsconfig.json in the same directory", async () => {
215
- fs.writeFileSync(
216
- "./package.json",
217
- JSON.stringify({ name: "test", version: "1.0.0" }),
218
- "utf-8"
219
- );
220
- fs.writeFileSync(
221
- "./tsconfig.json",
222
- JSON.stringify({ compilerOptions: {} }),
223
- "utf-8"
224
- );
225
-
226
- await runWrangler("init");
227
- const tsconfigJson = JSON.parse(
228
- fs.readFileSync("./tsconfig.json", "utf-8")
229
- );
230
- expect(tsconfigJson.compilerOptions).toEqual({});
231
- });
232
-
233
- it("should not touch an existing package.json in an ancestor directory", async () => {
234
- fs.writeFileSync(
235
- "./package.json",
236
- JSON.stringify({ name: "test", version: "1.0.0" }),
237
- "utf-8"
238
- );
239
- fs.writeFileSync(
240
- "./tsconfig.json",
241
- JSON.stringify({ compilerOptions: {} }),
242
- "utf-8"
243
- );
244
-
245
- fs.mkdirSync("./sub-1/sub-2", { recursive: true });
246
- process.chdir("./sub-1/sub-2");
247
-
248
- await runWrangler("init");
249
- expect(fs.existsSync("./tsconfig.json")).toBe(false);
250
- expect(fs.existsSync("../../tsconfig.json")).toBe(true);
251
-
252
- const tsconfigJson = JSON.parse(
253
- fs.readFileSync("../../tsconfig.json", "utf-8")
254
- );
255
- expect(tsconfigJson.compilerOptions).toEqual({});
256
- });
257
-
258
- it("should error if `--type` is used", async () => {
259
- const { error } = await runWrangler("init --type");
260
- expect(error).toMatchInlineSnapshot(
261
- `[Error: The --type option is no longer supported.]`
262
- );
263
- });
264
-
265
- it("should error if `--type javascript` is used", async () => {
266
- const { error } = await runWrangler("init --type javascript");
267
- expect(error).toMatchInlineSnapshot(
268
- `[Error: The --type option is no longer supported.]`
269
- );
270
- });
271
-
272
- it("should error if `--type rust` is used", async () => {
273
- const { error } = await runWrangler("init --type rust");
274
- expect(error).toMatchInlineSnapshot(
275
- `[Error: The --type option is no longer supported.]`
276
- );
277
- });
278
-
279
- it("should error if `--type webpack` is used", async () => {
280
- const { error } = await runWrangler("init --type webpack");
281
- expect(error).toMatchInlineSnapshot(`
282
- [Error: The --type option is no longer supported.
283
- If you wish to use webpack then you will need to create a custom build.]
284
- `);
285
- });
286
- });
287
- });
@@ -1,18 +0,0 @@
1
- import { confirm, prompt } from "../dialogs";
2
-
3
- jest.mock("../cfetch", () => jest.requireActual("./mock-cfetch"));
4
- jest.mock("../dialogs");
5
-
6
- // By default (if not configured by mockConfirm()) calls to `confirm()` should throw.
7
- (confirm as jest.Mock).mockImplementation(() => {
8
- throw new Error(
9
- "Unexpected call to `confirm()`. You should use `mockConfirm()` to mock calls to `confirm()` with expectations. Search the codebase for `mockConfirm` to learn more."
10
- );
11
- });
12
-
13
- // By default (if not configured by mockPrompt()) calls to `prompt()` should throw.
14
- (prompt as jest.Mock).mockImplementation(() => {
15
- throw new Error(
16
- "Unexpected call to `prompt()`. You should use `mockPrompt()` to mock calls to `prompt()` with expectations. Search the codebase for `mockPrompt` to learn more."
17
- );
18
- });
@@ -1,322 +0,0 @@
1
- import { setMock, unsetAllMocks } from "./mock-cfetch";
2
- import { runWrangler } from "./run-wrangler";
3
- import { runInTempDir } from "./run-in-tmp";
4
- import { writeFileSync } from "fs";
5
-
6
- describe("wrangler", () => {
7
- runInTempDir();
8
-
9
- afterEach(() => {
10
- unsetAllMocks();
11
- });
12
-
13
- describe("kv:namespace", () => {
14
- describe("create", () => {
15
- function mockCreateRequest(expectedTitle: string) {
16
- setMock(
17
- "/accounts/:accountId/storage/kv/namespaces",
18
- "POST",
19
- ([_url, accountId], { body }) => {
20
- expect(accountId).toEqual("some-account-id");
21
- const title = JSON.parse(body).title;
22
- expect(title).toEqual(expectedTitle);
23
- return { id: "some-namespace-id" };
24
- }
25
- );
26
- }
27
-
28
- it("should error if no namespace is given", async () => {
29
- const { error, stdout, stderr } = await runWrangler(
30
- "kv:namespace create"
31
- );
32
- expect(stdout).toMatchInlineSnapshot(`""`);
33
- expect(stderr).toMatchInlineSnapshot(`
34
- "wrangler kv:namespace create <namespace>
35
-
36
- Create a new namespace
37
-
38
- Positionals:
39
- namespace The name of the new namespace [string] [required]
40
-
41
- Flags:
42
- -c, --config Path to .toml configuration file [string]
43
- -h, --help Show help [boolean]
44
- -v, --version Show version number [boolean]
45
-
46
- Options:
47
- -l, --local Run on my machine [boolean] [default: false]
48
- --env Perform on a specific environment [string]
49
- --preview Interact with a preview namespace [boolean]
50
-
51
- Not enough non-option arguments: got 0, need at least 1"
52
- `);
53
- expect(error).toMatchInlineSnapshot(
54
- `[Error: Not enough non-option arguments: got 0, need at least 1]`
55
- );
56
- });
57
-
58
- it("should error if the namespace to create contains spaces", async () => {
59
- const { error, stdout, stderr } = await runWrangler(
60
- "kv:namespace create abc def ghi"
61
- );
62
- expect(stdout).toMatchInlineSnapshot(`""`);
63
- expect(stderr).toMatchInlineSnapshot(`
64
- "wrangler kv:namespace create <namespace>
65
-
66
- Create a new namespace
67
-
68
- Positionals:
69
- namespace The name of the new namespace [string] [required]
70
-
71
- Flags:
72
- -c, --config Path to .toml configuration file [string]
73
- -h, --help Show help [boolean]
74
- -v, --version Show version number [boolean]
75
-
76
- Options:
77
- -l, --local Run on my machine [boolean] [default: false]
78
- --env Perform on a specific environment [string]
79
- --preview Interact with a preview namespace [boolean]
80
-
81
- Unexpected additional positional arguments \\"def ghi\\"."
82
- `);
83
- expect(error).toMatchInlineSnapshot(
84
- `[Error: Unexpected additional positional arguments "def ghi".]`
85
- );
86
- });
87
-
88
- it("should error if the namespace to create is not valid", async () => {
89
- const { error, stdout, stderr } = await runWrangler(
90
- "kv:namespace create abc-def"
91
- );
92
- expect(stdout).toMatchInlineSnapshot(`""`);
93
- expect(stderr).toMatchInlineSnapshot(`
94
- "wrangler kv:namespace create <namespace>
95
-
96
- Create a new namespace
97
-
98
- Positionals:
99
- namespace The name of the new namespace [string] [required]
100
-
101
- Flags:
102
- -c, --config Path to .toml configuration file [string]
103
- -h, --help Show help [boolean]
104
- -v, --version Show version number [boolean]
105
-
106
- Options:
107
- -l, --local Run on my machine [boolean] [default: false]
108
- --env Perform on a specific environment [string]
109
- --preview Interact with a preview namespace [boolean]
110
-
111
- The namespace binding name \\"abc-def\\" is invalid. It can only have alphanumeric and _ characters, and cannot begin with a number."
112
- `);
113
- expect(error).toMatchInlineSnapshot(
114
- `[Error: The namespace binding name "abc-def" is invalid. It can only have alphanumeric and _ characters, and cannot begin with a number.]`
115
- );
116
- });
117
-
118
- it("should create a namespace", async () => {
119
- mockCreateRequest("worker-UnitTestNamespace");
120
- const { stdout } = await runWrangler(
121
- "kv:namespace create UnitTestNamespace"
122
- );
123
- expect(stdout).toMatchInlineSnapshot(`
124
- "🌀 Creating namespace with title \\"worker-UnitTestNamespace\\"
125
- ✨ Success!
126
- Add the following to your configuration file in your kv_namespaces array:
127
- { binding = \\"UnitTestNamespace\\", id = \\"some-namespace-id\\" }"
128
- `);
129
- });
130
-
131
- it("should create a preview namespace if configured to do so", async () => {
132
- mockCreateRequest("worker-UnitTestNamespace_preview");
133
- const { stdout } = await runWrangler(
134
- "kv:namespace create UnitTestNamespace --preview"
135
- );
136
- expect(stdout).toMatchInlineSnapshot(`
137
- "🌀 Creating namespace with title \\"worker-UnitTestNamespace_preview\\"
138
- ✨ Success!
139
- Add the following to your configuration file in your kv_namespaces array:
140
- { binding = \\"UnitTestNamespace\\", preview_id = \\"some-namespace-id\\" }"
141
- `);
142
- });
143
-
144
- it("should create a namespace using configured worker name", async () => {
145
- writeFileSync("./wrangler.toml", 'name = "otherWorker"', "utf-8");
146
- mockCreateRequest("otherWorker-UnitTestNamespace");
147
- const { stdout } = await runWrangler(
148
- "kv:namespace create UnitTestNamespace"
149
- );
150
- expect(stdout).toMatchInlineSnapshot(`
151
- "🌀 Creating namespace with title \\"otherWorker-UnitTestNamespace\\"
152
- ✨ Success!
153
- Add the following to your configuration file in your kv_namespaces array:
154
- { binding = \\"UnitTestNamespace\\", id = \\"some-namespace-id\\" }"
155
- `);
156
- });
157
-
158
- it("should create a namespace in an environment if configured to do so", async () => {
159
- mockCreateRequest("worker-customEnv-UnitTestNamespace");
160
- const { stdout } = await runWrangler(
161
- "kv:namespace create UnitTestNamespace --env customEnv"
162
- );
163
- expect(stdout).toMatchInlineSnapshot(`
164
- "🌀 Creating namespace with title \\"worker-customEnv-UnitTestNamespace\\"
165
- ✨ Success!
166
- Add the following to your configuration file in your kv_namespaces array under [env.customEnv]:
167
- { binding = \\"UnitTestNamespace\\", id = \\"some-namespace-id\\" }"
168
- `);
169
- });
170
- });
171
-
172
- describe("list", () => {
173
- function mockListRequest(namespaces: unknown[]) {
174
- const requests = { count: 0 };
175
- setMock(
176
- "/accounts/:accountId/storage/kv/namespaces\\?:qs",
177
- ([_url, accountId, query], init) => {
178
- requests.count++;
179
- expect(accountId).toEqual("some-account-id");
180
- expect(query).toContain("per_page=100");
181
- expect(query).toContain("order=title");
182
- expect(query).toContain("direction=asc");
183
- expect(query).toContain("page=");
184
- expect(init).toBe(undefined);
185
- const pageSize = Number(/\bper_page=(\d+)\b/.exec(query)[1]);
186
- const page = Number(/\bpage=(\d+)/.exec(query)[1]);
187
- return namespaces.slice((page - 1) * pageSize, page * pageSize);
188
- }
189
- );
190
- return requests;
191
- }
192
-
193
- it("should list namespaces", async () => {
194
- const KVNamespaces = [
195
- { title: "title-1", id: "id-1" },
196
- { title: "title-2", id: "id-2" },
197
- ];
198
- mockListRequest(KVNamespaces);
199
- const { stdout } = await runWrangler("kv:namespace list");
200
- const namespaces = JSON.parse(stdout);
201
- expect(namespaces).toEqual(KVNamespaces);
202
- });
203
-
204
- it("should make multiple requests for paginated results", async () => {
205
- // Create a lot of mock namespaces, so that the cfetch requests will be paginated
206
- const KVNamespaces = [];
207
- for (let i = 0; i < 550; i++) {
208
- KVNamespaces.push({ title: "title-" + i, id: "id-" + i });
209
- }
210
- const requests = mockListRequest(KVNamespaces);
211
- const { stdout } = await runWrangler("kv:namespace list");
212
- const namespaces = JSON.parse(stdout);
213
- expect(namespaces).toEqual(KVNamespaces);
214
- expect(requests.count).toEqual(6);
215
- });
216
- });
217
-
218
- describe("delete", () => {
219
- function mockDeleteRequest(expectedNamespaceId: string) {
220
- const requests = { count: 0 };
221
- setMock(
222
- "/accounts/:accountId/storage/kv/namespaces/:namespaceId",
223
- "DELETE",
224
- ([_url, accountId, namespaceId]) => {
225
- requests.count++;
226
- expect(accountId).toEqual("some-account-id");
227
- expect(namespaceId).toEqual(expectedNamespaceId);
228
- return null;
229
- }
230
- );
231
- return requests;
232
- }
233
-
234
- function writeWranglerConfig() {
235
- writeFileSync(
236
- "./wrangler.toml",
237
- [
238
- 'name = "otherWorker"',
239
- "kv_namespaces = [",
240
- ' { binding = "someBinding", id = "bound-id", preview_id = "preview-bound-id" }',
241
- "]",
242
- "",
243
- "[env.some-environment]",
244
- "kv_namespaces = [",
245
- ' { binding = "someBinding", id = "env-bound-id", preview_id = "preview-env-bound-id" }',
246
- "]",
247
- ].join("\n"),
248
- "utf-8"
249
- );
250
- }
251
-
252
- it("should delete a namespace specified by id", async () => {
253
- const requests = mockDeleteRequest("some-namespace-id");
254
- await runWrangler(
255
- `kv:namespace delete --namespace-id some-namespace-id`
256
- );
257
- expect(requests.count).toEqual(1);
258
- });
259
-
260
- it("should delete a namespace specified by binding name", async () => {
261
- writeWranglerConfig();
262
- const requests = mockDeleteRequest("bound-id");
263
- await runWrangler(`kv:namespace delete --binding someBinding`);
264
- expect(requests.count).toEqual(1);
265
- });
266
-
267
- it("should delete a preview namespace specified by binding name", async () => {
268
- writeWranglerConfig();
269
- const requests = mockDeleteRequest("preview-bound-id");
270
- await runWrangler(
271
- `kv:namespace delete --binding someBinding --preview`
272
- );
273
- expect(requests.count).toEqual(1);
274
- });
275
-
276
- it("should error if a given binding name is not in the configured kv namespaces", async () => {
277
- writeWranglerConfig();
278
- const { stderr } = await runWrangler(
279
- `kv:namespace delete --binding otherBinding`
280
- );
281
- expect(stderr).toMatchInlineSnapshot(`
282
- "wrangler kv:namespace delete
283
-
284
- Deletes a given namespace.
285
-
286
- Flags:
287
- -c, --config Path to .toml configuration file [string]
288
- -h, --help Show help [boolean]
289
- -v, --version Show version number [boolean]
290
-
291
- Options:
292
- -l, --local Run on my machine [boolean] [default: false]
293
- --binding The name of the namespace to delete [string]
294
- --namespace-id The id of the namespace to delete [string]
295
- --env Perform on a specific environment [string]
296
- --preview Interact with a preview namespace [boolean]
297
-
298
- Not able to delete namespace.
299
- A namespace with binding name \\"otherBinding\\" was not found in the configured \\"kv_namespaces\\"."
300
- `);
301
- });
302
-
303
- it("should delete a namespace specified by binding name in a given environment", async () => {
304
- writeWranglerConfig();
305
- const requests = mockDeleteRequest("env-bound-id");
306
- await runWrangler(
307
- `kv:namespace delete --binding someBinding --env some-environment`
308
- );
309
- expect(requests.count).toEqual(1);
310
- });
311
-
312
- it("should delete a preview namespace specified by binding name in a given environment", async () => {
313
- writeWranglerConfig();
314
- const requests = mockDeleteRequest("preview-env-bound-id");
315
- await runWrangler(
316
- `kv:namespace delete --binding someBinding --env some-environment --preview`
317
- );
318
- expect(requests.count).toEqual(1);
319
- });
320
- });
321
- });
322
- });