wrangler 0.0.0-e95e5a0 → 0.0.0-e98269a59

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 (185) hide show
  1. package/README.md +42 -34
  2. package/bin/wrangler.js +90 -39
  3. package/config-schema.json +3370 -0
  4. package/kv-asset-handler.js +1 -0
  5. package/package.json +133 -98
  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 +19 -0
  11. package/templates/gitignore +2 -3
  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 +134 -0
  17. package/templates/middleware/loader-sw.ts +229 -0
  18. package/templates/middleware/middleware-ensure-req-body-drained.ts +18 -0
  19. package/templates/middleware/middleware-miniflare3-json-error.ts +32 -0
  20. package/templates/middleware/middleware-mock-analytics-engine.d.ts +3 -0
  21. package/templates/middleware/middleware-mock-analytics-engine.ts +30 -0
  22. package/templates/middleware/middleware-patch-console-prefix.d.ts +3 -0
  23. package/templates/middleware/middleware-patch-console-prefix.ts +21 -0
  24. package/templates/middleware/middleware-pretty-error.ts +40 -0
  25. package/templates/middleware/middleware-scheduled.ts +29 -0
  26. package/templates/middleware/middleware-serve-static-assets.d.ts +6 -0
  27. package/templates/middleware/middleware-serve-static-assets.ts +56 -0
  28. package/templates/modules-watch-stub.js +4 -0
  29. package/templates/new-worker-scheduled.js +17 -0
  30. package/templates/new-worker-scheduled.ts +32 -0
  31. package/templates/new-worker.js +3 -3
  32. package/templates/new-worker.ts +22 -4
  33. package/templates/no-op-worker.js +10 -0
  34. package/templates/pages-dev-pipeline.ts +32 -0
  35. package/templates/pages-dev-util.ts +55 -0
  36. package/templates/pages-shim.ts +9 -0
  37. package/templates/pages-template-plugin.ts +190 -0
  38. package/templates/pages-template-worker.ts +198 -0
  39. package/templates/startDevWorker/InspectorProxyWorker.ts +664 -0
  40. package/templates/startDevWorker/ProxyWorker.ts +336 -0
  41. package/templates/tsconfig-sanity.ts +11 -0
  42. package/templates/tsconfig.init.json +22 -0
  43. package/templates/tsconfig.json +6 -103
  44. package/wrangler-dist/InspectorProxyWorker.js +464 -0
  45. package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
  46. package/wrangler-dist/ProxyWorker.js +241 -0
  47. package/wrangler-dist/ProxyWorker.js.map +6 -0
  48. package/wrangler-dist/cli.d.ts +26730 -0
  49. package/wrangler-dist/cli.js +166828 -106751
  50. package/import_meta_url.js +0 -3
  51. package/miniflare-config-stubs/.env.empty +0 -0
  52. package/miniflare-config-stubs/package.empty.json +0 -1
  53. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  54. package/miniflare-dist/index.mjs +0 -4886
  55. package/pages/functions/buildPlugin.ts +0 -92
  56. package/pages/functions/buildWorker.ts +0 -138
  57. package/pages/functions/filepath-routing.test.ts +0 -234
  58. package/pages/functions/filepath-routing.ts +0 -189
  59. package/pages/functions/identifiers.ts +0 -78
  60. package/pages/functions/routes.ts +0 -156
  61. package/pages/functions/template-plugin.ts +0 -146
  62. package/pages/functions/template-worker.ts +0 -143
  63. package/src/__tests__/config-cache.test.ts +0 -36
  64. package/src/__tests__/configuration.test.ts +0 -3125
  65. package/src/__tests__/dev.test.tsx +0 -762
  66. package/src/__tests__/guess-worker-format.test.ts +0 -85
  67. package/src/__tests__/helpers/clipboardy-mock.js +0 -4
  68. package/src/__tests__/helpers/cmd-shim.d.ts +0 -11
  69. package/src/__tests__/helpers/faye-websocket.d.ts +0 -6
  70. package/src/__tests__/helpers/mock-account-id.ts +0 -48
  71. package/src/__tests__/helpers/mock-bin.ts +0 -36
  72. package/src/__tests__/helpers/mock-cfetch.ts +0 -211
  73. package/src/__tests__/helpers/mock-console.ts +0 -87
  74. package/src/__tests__/helpers/mock-dialogs.ts +0 -90
  75. package/src/__tests__/helpers/mock-http-server.ts +0 -46
  76. package/src/__tests__/helpers/mock-istty.ts +0 -27
  77. package/src/__tests__/helpers/mock-kv.ts +0 -33
  78. package/src/__tests__/helpers/mock-oauth-flow.ts +0 -255
  79. package/src/__tests__/helpers/mock-stdin.ts +0 -103
  80. package/src/__tests__/helpers/mock-web-socket.ts +0 -29
  81. package/src/__tests__/helpers/run-in-tmp.ts +0 -39
  82. package/src/__tests__/helpers/run-wrangler.ts +0 -16
  83. package/src/__tests__/helpers/write-worker-source.ts +0 -31
  84. package/src/__tests__/helpers/write-wrangler-toml.ts +0 -17
  85. package/src/__tests__/https-options.test.ts +0 -130
  86. package/src/__tests__/index.test.ts +0 -249
  87. package/src/__tests__/init.test.ts +0 -1516
  88. package/src/__tests__/jest.setup.ts +0 -69
  89. package/src/__tests__/kv.test.ts +0 -1454
  90. package/src/__tests__/logger.test.ts +0 -115
  91. package/src/__tests__/package-manager.test.ts +0 -206
  92. package/src/__tests__/pages.test.ts +0 -329
  93. package/src/__tests__/parse.test.ts +0 -322
  94. package/src/__tests__/publish.test.ts +0 -4875
  95. package/src/__tests__/r2.test.ts +0 -214
  96. package/src/__tests__/route.test.ts +0 -45
  97. package/src/__tests__/secret.test.ts +0 -446
  98. package/src/__tests__/tail.test.ts +0 -690
  99. package/src/__tests__/user.test.ts +0 -131
  100. package/src/__tests__/whoami.test.tsx +0 -105
  101. package/src/bundle.ts +0 -169
  102. package/src/cfetch/index.ts +0 -104
  103. package/src/cfetch/internal.ts +0 -137
  104. package/src/cli.ts +0 -13
  105. package/src/config/README.md +0 -107
  106. package/src/config/config.ts +0 -234
  107. package/src/config/diagnostics.ts +0 -80
  108. package/src/config/environment.ts +0 -394
  109. package/src/config/index.ts +0 -63
  110. package/src/config/validation-helpers.ts +0 -575
  111. package/src/config/validation.ts +0 -1424
  112. package/src/config-cache.ts +0 -45
  113. package/src/create-worker-preview.ts +0 -175
  114. package/src/create-worker-upload-form.ts +0 -240
  115. package/src/dev/dev-vars.ts +0 -36
  116. package/src/dev/dev.tsx +0 -418
  117. package/src/dev/local.tsx +0 -312
  118. package/src/dev/remote.tsx +0 -213
  119. package/src/dev/use-esbuild.ts +0 -118
  120. package/src/dialogs.tsx +0 -87
  121. package/src/entry.ts +0 -267
  122. package/src/environment-variables.ts +0 -35
  123. package/src/errors.ts +0 -11
  124. package/src/https-options.ts +0 -123
  125. package/src/index.tsx +0 -2668
  126. package/src/inspect.ts +0 -623
  127. package/src/intl-polyfill.d.ts +0 -139
  128. package/src/kv.ts +0 -319
  129. package/src/logger.ts +0 -66
  130. package/src/miniflare-cli/README.md +0 -30
  131. package/src/miniflare-cli/enum-keys.ts +0 -17
  132. package/src/miniflare-cli/index.ts +0 -41
  133. package/src/miniflare-cli/request-context.ts +0 -40
  134. package/src/module-collection.ts +0 -258
  135. package/src/open-in-browser.ts +0 -26
  136. package/src/package-manager.ts +0 -127
  137. package/src/pages.tsx +0 -1866
  138. package/src/parse.ts +0 -183
  139. package/src/paths.ts +0 -26
  140. package/src/proxy.ts +0 -449
  141. package/src/publish.ts +0 -469
  142. package/src/r2.ts +0 -50
  143. package/src/selfsigned.d.ts +0 -29
  144. package/src/sites.tsx +0 -293
  145. package/src/tail/filters.ts +0 -279
  146. package/src/tail/index.ts +0 -299
  147. package/src/tail/printing.ts +0 -67
  148. package/src/update-check.ts +0 -19
  149. package/src/user.tsx +0 -1193
  150. package/src/whoami.tsx +0 -70
  151. package/src/worker.ts +0 -172
  152. package/templates/static-asset-facade.js +0 -47
  153. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  154. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  155. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  156. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  157. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  158. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  159. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  160. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  161. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  162. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  163. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  164. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  165. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  166. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  167. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  168. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  169. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  170. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  171. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  172. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  173. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  174. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  175. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  176. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  177. package/vendor/wrangler-mime/LICENSE +0 -21
  178. package/vendor/wrangler-mime/Mime.js +0 -97
  179. package/vendor/wrangler-mime/README.md +0 -187
  180. package/vendor/wrangler-mime/cli.js +0 -46
  181. package/vendor/wrangler-mime/index.js +0 -4
  182. package/vendor/wrangler-mime/lite.js +0 -4
  183. package/vendor/wrangler-mime/package.json +0 -52
  184. package/vendor/wrangler-mime/types/other.js +0 -1
  185. package/vendor/wrangler-mime/types/standard.js +0 -1
@@ -1,214 +0,0 @@
1
- import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
2
- import { setMockResponse, unsetAllMocks } from "./helpers/mock-cfetch";
3
- import { mockConsoleMethods } from "./helpers/mock-console";
4
- import { runInTempDir } from "./helpers/run-in-tmp";
5
- import { runWrangler } from "./helpers/run-wrangler";
6
- import type { R2BucketInfo } from "../r2";
7
-
8
- describe("wrangler", () => {
9
- mockAccountId();
10
- mockApiToken();
11
- runInTempDir();
12
- const std = mockConsoleMethods();
13
-
14
- afterEach(() => {
15
- unsetAllMocks();
16
- });
17
-
18
- describe("r2", () => {
19
- describe("bucket", () => {
20
- describe("list", () => {
21
- function mockListRequest(buckets: R2BucketInfo[]) {
22
- const requests = { count: 0 };
23
- setMockResponse(
24
- "/accounts/:accountId/r2/buckets",
25
- ([_url, accountId], init) => {
26
- requests.count++;
27
- expect(accountId).toEqual("some-account-id");
28
- expect(init).toEqual({});
29
- return { buckets };
30
- }
31
- );
32
- return requests;
33
- }
34
-
35
- it("should list buckets", async () => {
36
- const expectedBuckets: R2BucketInfo[] = [
37
- { name: "bucket-1", creation_date: "01-01-2001" },
38
- { name: "bucket-2", creation_date: "01-01-2001" },
39
- ];
40
- mockListRequest(expectedBuckets);
41
- await runWrangler("r2 bucket list");
42
-
43
- expect(std.err).toMatchInlineSnapshot(`""`);
44
- const buckets = JSON.parse(std.out);
45
- expect(buckets).toEqual(expectedBuckets);
46
- });
47
- });
48
-
49
- describe("create", () => {
50
- function mockCreateRequest(expectedBucketName: string) {
51
- const requests = { count: 0 };
52
- setMockResponse(
53
- "/accounts/:accountId/r2/buckets/:bucketName",
54
- "PUT",
55
- ([_url, accountId, bucketName]) => {
56
- expect(accountId).toEqual("some-account-id");
57
- expect(bucketName).toEqual(expectedBucketName);
58
- requests.count += 1;
59
- }
60
- );
61
- return requests;
62
- }
63
-
64
- it("should error if no bucket name is given", async () => {
65
- await expect(
66
- runWrangler("r2 bucket create")
67
- ).rejects.toThrowErrorMatchingInlineSnapshot(
68
- `"Not enough non-option arguments: got 0, need at least 1"`
69
- );
70
- expect(std.out).toMatchInlineSnapshot(`
71
- "
72
- "
73
- `);
74
- expect(std.err).toMatchInlineSnapshot(`
75
- "wrangler r2 bucket create <name>
76
-
77
- Create a new R2 bucket
78
-
79
- Positionals:
80
- name The name of the new bucket [string] [required]
81
-
82
- Flags:
83
- -c, --config Path to .toml configuration file [string]
84
- -h, --help Show help [boolean]
85
- -v, --version Show version number [boolean]
86
- --legacy-env Use legacy environments [boolean]
87
- X [ERROR] Not enough non-option arguments: got 0, need at least 1
88
-
89
- "
90
- `);
91
- });
92
-
93
- it("should error if the bucket to create contains spaces", async () => {
94
- await expect(
95
- runWrangler("r2 bucket create abc def ghi")
96
- ).rejects.toThrowErrorMatchingInlineSnapshot(
97
- `"Unknown arguments: def, ghi"`
98
- );
99
- expect(std.out).toMatchInlineSnapshot(`
100
- "
101
- "
102
- `);
103
- expect(std.err).toMatchInlineSnapshot(`
104
- "wrangler r2 bucket create <name>
105
-
106
- Create a new R2 bucket
107
-
108
- Positionals:
109
- name The name of the new bucket [string] [required]
110
-
111
- Flags:
112
- -c, --config Path to .toml configuration file [string]
113
- -h, --help Show help [boolean]
114
- -v, --version Show version number [boolean]
115
- --legacy-env Use legacy environments [boolean]
116
- X [ERROR] Unknown arguments: def, ghi
117
-
118
- "
119
- `);
120
- });
121
-
122
- it("should create a bucket", async () => {
123
- const requests = mockCreateRequest("testBucket");
124
- await runWrangler("r2 bucket create testBucket");
125
- expect(std.out).toMatchInlineSnapshot(`
126
- "Creating bucket testBucket.
127
- Created bucket testBucket."
128
- `);
129
- expect(requests.count).toEqual(1);
130
- });
131
- });
132
-
133
- describe("delete", () => {
134
- function mockDeleteRequest(expectedBucketName: string) {
135
- const requests = { count: 0 };
136
- setMockResponse(
137
- "/accounts/:accountId/r2/buckets/:bucketName",
138
- "DELETE",
139
- ([_url, accountId, bucketName]) => {
140
- expect(accountId).toEqual("some-account-id");
141
- expect(bucketName).toEqual(expectedBucketName);
142
- requests.count += 1;
143
- }
144
- );
145
- return requests;
146
- }
147
-
148
- it("should error if no bucket name is given", async () => {
149
- await expect(
150
- runWrangler("r2 bucket delete")
151
- ).rejects.toThrowErrorMatchingInlineSnapshot(
152
- `"Not enough non-option arguments: got 0, need at least 1"`
153
- );
154
- expect(std.out).toMatchInlineSnapshot(`
155
- "
156
- "
157
- `);
158
- expect(std.err).toMatchInlineSnapshot(`
159
- "wrangler r2 bucket delete <name>
160
-
161
- Delete an R2 bucket
162
-
163
- Positionals:
164
- name The name of the bucket to delete [string] [required]
165
-
166
- Flags:
167
- -c, --config Path to .toml configuration file [string]
168
- -h, --help Show help [boolean]
169
- -v, --version Show version number [boolean]
170
- --legacy-env Use legacy environments [boolean]
171
- X [ERROR] Not enough non-option arguments: got 0, need at least 1
172
-
173
- "
174
- `);
175
- });
176
-
177
- it("should error if the bucket name to delete contains spaces", async () => {
178
- await expect(
179
- runWrangler("r2 bucket delete abc def ghi")
180
- ).rejects.toThrowErrorMatchingInlineSnapshot(
181
- `"Unknown arguments: def, ghi"`
182
- );
183
- expect(std.out).toMatchInlineSnapshot(`
184
- "
185
- "
186
- `);
187
- expect(std.err).toMatchInlineSnapshot(`
188
- "wrangler r2 bucket delete <name>
189
-
190
- Delete an R2 bucket
191
-
192
- Positionals:
193
- name The name of the bucket to delete [string] [required]
194
-
195
- Flags:
196
- -c, --config Path to .toml configuration file [string]
197
- -h, --help Show help [boolean]
198
- -v, --version Show version number [boolean]
199
- --legacy-env Use legacy environments [boolean]
200
- X [ERROR] Unknown arguments: def, ghi
201
-
202
- "
203
- `);
204
- });
205
-
206
- it("should delete a bucket specified by name", async () => {
207
- const requests = mockDeleteRequest("some-bucket");
208
- await runWrangler(`r2 bucket delete some-bucket`);
209
- expect(requests.count).toEqual(1);
210
- });
211
- });
212
- });
213
- });
214
- });
@@ -1,45 +0,0 @@
1
- import { mockConsoleMethods } from "./helpers/mock-console";
2
- import { runInTempDir } from "./helpers/run-in-tmp";
3
- import { runWrangler } from "./helpers/run-wrangler";
4
-
5
- describe("wrangler route", () => {
6
- mockConsoleMethods();
7
- runInTempDir();
8
-
9
- it("shows a deprecation notice when `wrangler route` is run", async () => {
10
- await expect(runWrangler("route")).rejects
11
- .toThrowErrorMatchingInlineSnapshot(`
12
- "Deprecation:
13
- \`wrangler route\` has been deprecated.
14
- Please use wrangler.toml and/or \`wrangler publish --routes\` to modify routes"
15
- `);
16
- });
17
-
18
- it("shows a deprecation notice when `wrangler route delete` is run", async () => {
19
- await expect(runWrangler("route delete")).rejects
20
- .toThrowErrorMatchingInlineSnapshot(`
21
- "Deprecation:
22
- \`wrangler route delete\` has been deprecated.
23
- Remove the unwanted route(s) from wrangler.toml and run \`wrangler publish\` to remove your worker from those routes."
24
- `);
25
- });
26
-
27
- it("shows a deprecation notice when `wrangler route delete <id>` is run", async () => {
28
- await expect(runWrangler("route delete some-zone-id")).rejects
29
- .toThrowErrorMatchingInlineSnapshot(`
30
- "Deprecation:
31
- \`wrangler route delete\` has been deprecated.
32
- Remove the unwanted route(s) from wrangler.toml and run \`wrangler publish\` to remove your worker from those routes."
33
- `);
34
- });
35
-
36
- it("shows a deprecation notice when `wrangler route list` is run", async () => {
37
- await expect(runWrangler("route list")).rejects
38
- .toThrowErrorMatchingInlineSnapshot(`
39
- "Deprecation:
40
- \`wrangler route list\` has been deprecated.
41
- Refer to wrangler.toml for a list of routes the worker will be deployed to upon publishing.
42
- Refer to the Cloudflare Dashboard to see the routes this worker is currently running on."
43
- `);
44
- });
45
- });
@@ -1,446 +0,0 @@
1
- import * as fs from "node:fs";
2
- import * as TOML from "@iarna/toml";
3
- import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
4
- import { setMockResponse, unsetAllMocks } from "./helpers/mock-cfetch";
5
- import { mockConsoleMethods } from "./helpers/mock-console";
6
- import {
7
- mockConfirm,
8
- mockPrompt,
9
- clearConfirmMocks,
10
- clearPromptMocks,
11
- } from "./helpers/mock-dialogs";
12
- import { mockOAuthFlow } from "./helpers/mock-oauth-flow";
13
- import { useMockStdin } from "./helpers/mock-stdin";
14
- import { runInTempDir } from "./helpers/run-in-tmp";
15
- import { runWrangler } from "./helpers/run-wrangler";
16
-
17
- describe("wrangler secret", () => {
18
- const std = mockConsoleMethods();
19
- const { mockGetMemberships } = mockOAuthFlow();
20
-
21
- runInTempDir();
22
- mockAccountId();
23
- mockApiToken();
24
-
25
- afterEach(() => {
26
- unsetAllMocks();
27
- clearConfirmMocks();
28
- clearPromptMocks();
29
- });
30
-
31
- describe("put", () => {
32
- function mockPutRequest(
33
- input: { name: string; text: string },
34
- env?: string,
35
- legacyEnv = false
36
- ) {
37
- const servicesOrScripts = env && !legacyEnv ? "services" : "scripts";
38
- const environment = env && !legacyEnv ? "/environments/:envName" : "";
39
- setMockResponse(
40
- `/accounts/:accountId/workers/${servicesOrScripts}/:scriptName${environment}/secrets`,
41
- "PUT",
42
- ([_url, accountId, scriptName, envName], { body }) => {
43
- expect(accountId).toEqual("some-account-id");
44
- expect(scriptName).toEqual(
45
- legacyEnv && env ? `script-name-${env}` : "script-name"
46
- );
47
- if (!legacyEnv) {
48
- expect(envName).toEqual(env);
49
- }
50
- const { name, text, type } = JSON.parse(body as string);
51
- expect(type).toEqual("secret_text");
52
- expect(name).toEqual(input.name);
53
- expect(text).toEqual(input.text);
54
-
55
- return { name, type };
56
- }
57
- );
58
- }
59
-
60
- describe("interactive", () => {
61
- useMockStdin({ isTTY: true });
62
-
63
- it("should create a secret", async () => {
64
- mockPrompt({
65
- text: "Enter a secret value:",
66
- type: "password",
67
- result: "the-secret",
68
- });
69
-
70
- mockPutRequest({ name: "the-key", text: "the-secret" });
71
- await runWrangler("secret put the-key --name script-name");
72
-
73
- expect(std.out).toMatchInlineSnapshot(`
74
- "🌀 Creating the secret for script script-name
75
- ✨ Success! Uploaded secret the-key"
76
- `);
77
- expect(std.err).toMatchInlineSnapshot(`""`);
78
- });
79
-
80
- it("should create a secret: legacy envs", async () => {
81
- mockPrompt({
82
- text: "Enter a secret value:",
83
- type: "password",
84
- result: "the-secret",
85
- });
86
-
87
- mockPutRequest(
88
- { name: "the-key", text: "the-secret" },
89
- "some-env",
90
- true
91
- );
92
- await runWrangler(
93
- "secret put the-key --name script-name --env some-env --legacy-env"
94
- );
95
-
96
- expect(std.out).toMatchInlineSnapshot(`
97
- "🌀 Creating the secret for script script-name-some-env
98
- ✨ Success! Uploaded secret the-key"
99
- `);
100
- expect(std.err).toMatchInlineSnapshot(`""`);
101
- });
102
-
103
- it("should create a secret: service envs", async () => {
104
- mockPrompt({
105
- text: "Enter a secret value:",
106
- type: "password",
107
- result: "the-secret",
108
- });
109
-
110
- mockPutRequest(
111
- { name: "the-key", text: "the-secret" },
112
- "some-env",
113
- false
114
- );
115
- await runWrangler(
116
- "secret put the-key --name script-name --env some-env --legacy-env false"
117
- );
118
-
119
- expect(std.out).toMatchInlineSnapshot(`
120
- "🌀 Creating the secret for script script-name (some-env)
121
- ✨ Success! Uploaded secret the-key"
122
- `);
123
- expect(std.err).toMatchInlineSnapshot(`""`);
124
- });
125
-
126
- it("should error without a script name", async () => {
127
- let error: Error | undefined;
128
- try {
129
- await runWrangler("secret put the-key");
130
- } catch (e) {
131
- error = e as Error;
132
- }
133
- expect(std.out).toMatchInlineSnapshot(`
134
- "
135
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
136
- `);
137
- expect(std.err).toMatchInlineSnapshot(`
138
- "X [ERROR] Missing script name
139
-
140
- "
141
- `);
142
- expect(error).toMatchInlineSnapshot(`[Error: Missing script name]`);
143
- });
144
- });
145
-
146
- describe("non-interactive", () => {
147
- const mockStdIn = useMockStdin({ isTTY: false });
148
-
149
- it("should create a secret, from piped input", async () => {
150
- mockPutRequest({ name: "the-key", text: "the-secret" });
151
- // Pipe the secret in as three chunks to test that we reconstitute it correctly.
152
- mockStdIn.send("the", "-", "secret");
153
- await runWrangler("secret put the-key --name script-name");
154
-
155
- expect(std.out).toMatchInlineSnapshot(`
156
- "🌀 Creating the secret for script script-name
157
- ✨ Success! Uploaded secret the-key"
158
- `);
159
- expect(std.warn).toMatchInlineSnapshot(`""`);
160
- expect(std.err).toMatchInlineSnapshot(`""`);
161
- });
162
-
163
- it("should error if the piped input fails", async () => {
164
- mockPutRequest({ name: "the-key", text: "the-secret" });
165
- mockStdIn.throwError(new Error("Error in stdin stream"));
166
- await expect(
167
- runWrangler("secret put the-key --name script-name")
168
- ).rejects.toThrowErrorMatchingInlineSnapshot(`"Error in stdin stream"`);
169
-
170
- expect(std.out).toMatchInlineSnapshot(`
171
- "
172
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
173
- `);
174
- expect(std.warn).toMatchInlineSnapshot(`""`);
175
- });
176
-
177
- describe("with accountId", () => {
178
- mockAccountId({ accountId: null });
179
-
180
- it("should error if a user has no account", async () => {
181
- mockGetMemberships({
182
- success: false,
183
- result: [],
184
- });
185
- await expect(
186
- runWrangler("secret put the-key --name script-name")
187
- ).rejects.toThrowErrorMatchingInlineSnapshot(
188
- `"Failed to automatically retrieve account IDs for the logged in user. In a non-interactive environment, it is mandatory to specify an account ID, either by assigning its value to CLOUDFLARE_ACCOUNT_ID, or as \`account_id\` in your \`wrangler.toml\` file."`
189
- );
190
- });
191
-
192
- it("should use the account from wrangler.toml", async () => {
193
- fs.writeFileSync(
194
- "wrangler.toml",
195
- TOML.stringify({
196
- account_id: "some-account-id",
197
- }),
198
- "utf-8"
199
- );
200
- mockStdIn.send("the-secret");
201
- mockPutRequest({ name: "the-key", text: "the-secret" });
202
- await runWrangler("secret put the-key --name script-name");
203
- expect(std.out).toMatchInlineSnapshot(`
204
- "🌀 Creating the secret for script script-name
205
- ✨ Success! Uploaded secret the-key"
206
- `);
207
- expect(std.warn).toMatchInlineSnapshot(`""`);
208
- expect(std.err).toMatchInlineSnapshot(`""`);
209
- });
210
-
211
- it("should error if a user has multiple accounts, and has not specified an account in wrangler.toml", async () => {
212
- mockGetMemberships({
213
- success: true,
214
- result: [
215
- {
216
- id: "1",
217
- account: { id: "account-id-1", name: "account-name-1" },
218
- },
219
- {
220
- id: "2",
221
- account: { id: "account-id-2", name: "account-name-2" },
222
- },
223
- {
224
- id: "3",
225
- account: { id: "account-id-3", name: "account-name-3" },
226
- },
227
- ],
228
- });
229
-
230
- await expect(runWrangler("secret put the-key --name script-name"))
231
- .rejects.toThrowErrorMatchingInlineSnapshot(`
232
- "More than one account available but unable to select one in non-interactive mode.
233
- Please set the appropriate \`account_id\` in your \`wrangler.toml\` file.
234
- Available accounts are (\\"<name>\\" - \\"<id>\\"):
235
- \\"account-name-1\\" - \\"account-id-1\\")
236
- \\"account-name-2\\" - \\"account-id-2\\")
237
- \\"account-name-3\\" - \\"account-id-3\\")"
238
- `);
239
- });
240
- });
241
- });
242
- });
243
-
244
- describe("delete", () => {
245
- function mockDeleteRequest(
246
- input: {
247
- scriptName: string;
248
- secretName: string;
249
- },
250
- env?: string,
251
- legacyEnv = false
252
- ) {
253
- const servicesOrScripts = env && !legacyEnv ? "services" : "scripts";
254
- const environment = env && !legacyEnv ? "/environments/:envName" : "";
255
- setMockResponse(
256
- `/accounts/:accountId/workers/${servicesOrScripts}/:scriptName${environment}/secrets/:secretName`,
257
- "DELETE",
258
- ([_url, accountId, scriptName, envOrSecretName, secretName]) => {
259
- expect(accountId).toEqual("some-account-id");
260
- expect(scriptName).toEqual(
261
- legacyEnv && env ? `script-name-${env}` : "script-name"
262
- );
263
- if (!legacyEnv) {
264
- if (env) {
265
- expect(envOrSecretName).toEqual(env);
266
- expect(secretName).toEqual(input.secretName);
267
- } else {
268
- expect(envOrSecretName).toEqual(input.secretName);
269
- }
270
- } else {
271
- expect(envOrSecretName).toEqual(input.secretName);
272
- }
273
- return null;
274
- }
275
- );
276
- }
277
- it("should delete a secret", async () => {
278
- mockDeleteRequest({ scriptName: "script-name", secretName: "the-key" });
279
- mockConfirm({
280
- text: "Are you sure you want to permanently delete the variable the-key on the script script-name?",
281
- result: true,
282
- });
283
- await runWrangler("secret delete the-key --name script-name");
284
- expect(std.out).toMatchInlineSnapshot(`
285
- "🌀 Deleting the secret the-key on script script-name
286
- ✨ Success! Deleted secret the-key"
287
- `);
288
- expect(std.err).toMatchInlineSnapshot(`""`);
289
- });
290
-
291
- it("should delete a secret: legacy envs", async () => {
292
- mockDeleteRequest(
293
- { scriptName: "script-name", secretName: "the-key" },
294
- "some-env",
295
- true
296
- );
297
- mockConfirm({
298
- text: "Are you sure you want to permanently delete the variable the-key on the script script-name-some-env?",
299
- result: true,
300
- });
301
- await runWrangler(
302
- "secret delete the-key --name script-name --env some-env --legacy-env"
303
- );
304
- expect(std.out).toMatchInlineSnapshot(`
305
- "🌀 Deleting the secret the-key on script script-name-some-env
306
- ✨ Success! Deleted secret the-key"
307
- `);
308
- expect(std.err).toMatchInlineSnapshot(`""`);
309
- });
310
-
311
- it("should delete a secret: service envs", async () => {
312
- mockDeleteRequest(
313
- { scriptName: "script-name", secretName: "the-key" },
314
- "some-env"
315
- );
316
- mockConfirm({
317
- text: "Are you sure you want to permanently delete the variable the-key on the script script-name (some-env)?",
318
- result: true,
319
- });
320
- await runWrangler(
321
- "secret delete the-key --name script-name --env some-env --legacy-env false"
322
- );
323
- expect(std.out).toMatchInlineSnapshot(`
324
- "🌀 Deleting the secret the-key on script script-name (some-env)
325
- ✨ Success! Deleted secret the-key"
326
- `);
327
- expect(std.err).toMatchInlineSnapshot(`""`);
328
- });
329
-
330
- it("should error without a script name", async () => {
331
- let error: Error | undefined;
332
-
333
- try {
334
- await runWrangler("secret delete the-key");
335
- } catch (e) {
336
- error = e as Error;
337
- }
338
- expect(std.out).toMatchInlineSnapshot(`
339
- "
340
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
341
- `);
342
- expect(std.err).toMatchInlineSnapshot(`
343
- "X [ERROR] Missing script name
344
-
345
- "
346
- `);
347
- expect(error).toMatchInlineSnapshot(`[Error: Missing script name]`);
348
- });
349
- });
350
-
351
- describe("list", () => {
352
- function mockListRequest(
353
- input: { scriptName: string },
354
- env?: string,
355
- legacyEnv = false
356
- ) {
357
- const servicesOrScripts = env && !legacyEnv ? "services" : "scripts";
358
- const environment = env && !legacyEnv ? "/environments/:envName" : "";
359
- setMockResponse(
360
- `/accounts/:accountId/workers/${servicesOrScripts}/:scriptName${environment}/secrets`,
361
- "GET",
362
- ([_url, accountId, scriptName, envName]) => {
363
- expect(accountId).toEqual("some-account-id");
364
- expect(scriptName).toEqual(
365
- legacyEnv && env ? `script-name-${env}` : "script-name"
366
- );
367
- if (!legacyEnv) {
368
- expect(envName).toEqual(env);
369
- }
370
-
371
- return [
372
- {
373
- name: "the-secret-name",
374
- type: "secret_text",
375
- },
376
- ];
377
- }
378
- );
379
- }
380
-
381
- it("should list secrets", async () => {
382
- mockListRequest({ scriptName: "script-name" });
383
- await runWrangler("secret list --name script-name");
384
- expect(std.out).toMatchInlineSnapshot(`
385
- "[
386
- {
387
- \\"name\\": \\"the-secret-name\\",
388
- \\"type\\": \\"secret_text\\"
389
- }
390
- ]"
391
- `);
392
- expect(std.err).toMatchInlineSnapshot(`""`);
393
- });
394
-
395
- it("should list secrets: legacy envs", async () => {
396
- mockListRequest({ scriptName: "script-name" }, "some-env", true);
397
- await runWrangler(
398
- "secret list --name script-name --env some-env --legacy-env"
399
- );
400
- expect(std.out).toMatchInlineSnapshot(`
401
- "[
402
- {
403
- \\"name\\": \\"the-secret-name\\",
404
- \\"type\\": \\"secret_text\\"
405
- }
406
- ]"
407
- `);
408
- expect(std.err).toMatchInlineSnapshot(`""`);
409
- });
410
-
411
- it("should list secrets: service envs", async () => {
412
- mockListRequest({ scriptName: "script-name" }, "some-env");
413
- await runWrangler(
414
- "secret list --name script-name --env some-env --legacy-env false"
415
- );
416
- expect(std.out).toMatchInlineSnapshot(`
417
- "[
418
- {
419
- \\"name\\": \\"the-secret-name\\",
420
- \\"type\\": \\"secret_text\\"
421
- }
422
- ]"
423
- `);
424
- expect(std.err).toMatchInlineSnapshot(`""`);
425
- });
426
-
427
- it("should error without a script name", async () => {
428
- let error: Error | undefined;
429
- try {
430
- await runWrangler("secret list");
431
- } catch (e) {
432
- error = e as Error;
433
- }
434
- expect(std.out).toMatchInlineSnapshot(`
435
- "
436
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
437
- `);
438
- expect(std.err).toMatchInlineSnapshot(`
439
- "X [ERROR] Missing script name
440
-
441
- "
442
- `);
443
- expect(error).toMatchInlineSnapshot(`[Error: Missing script name]`);
444
- });
445
- });
446
- });