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,762 +0,0 @@
1
- import * as fs from "node:fs";
2
- import patchConsole from "patch-console";
3
- import dedent from "ts-dedent";
4
- import Dev from "../dev/dev";
5
- import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
6
- import { setMockResponse, unsetAllMocks } from "./helpers/mock-cfetch";
7
- import { mockConsoleMethods } from "./helpers/mock-console";
8
- import { runInTempDir } from "./helpers/run-in-tmp";
9
- import { runWrangler } from "./helpers/run-wrangler";
10
- import writeWranglerToml from "./helpers/write-wrangler-toml";
11
-
12
- describe("wrangler dev", () => {
13
- mockAccountId();
14
- mockApiToken();
15
- runInTempDir();
16
- const std = mockConsoleMethods();
17
- afterEach(() => {
18
- (Dev as jest.Mock).mockClear();
19
- patchConsole(() => {});
20
- unsetAllMocks();
21
- });
22
-
23
- describe("compatibility-date", () => {
24
- it("should not warn if there is no wrangler.toml and no compatibility-date specified", async () => {
25
- fs.writeFileSync("index.js", `export default {};`);
26
- await runWrangler("dev index.js");
27
- expect(std.out).toMatchInlineSnapshot(`""`);
28
- expect(std.warn).toMatchInlineSnapshot(`""`);
29
- expect(std.err).toMatchInlineSnapshot(`""`);
30
- });
31
-
32
- it("should warn if there is a wrangler.toml but no compatibility-date", async () => {
33
- writeWranglerToml({
34
- main: "index.js",
35
- compatibility_date: undefined,
36
- });
37
- fs.writeFileSync("index.js", `export default {};`);
38
- await runWrangler("dev");
39
- const currentDate = new Date().toISOString().substring(0, 10);
40
- expect(std.out).toMatchInlineSnapshot(`""`);
41
- expect(std.warn.replaceAll(currentDate, "<current-date>"))
42
- .toMatchInlineSnapshot(`
43
- "â–² [WARNING] No compatibility_date was specified. Using today's date: <current-date>.
44
-
45
- Add one to your wrangler.toml file:
46
- \`\`\`
47
- compatibility_date = \\"<current-date>\\"
48
- \`\`\`
49
- or pass it in your terminal:
50
- \`\`\`
51
- --compatibility-date=<current-date>
52
- \`\`\`
53
- See https://developers.cloudflare.com/workers/platform/compatibility-dates for more information.
54
-
55
- "
56
- `);
57
- expect(std.err).toMatchInlineSnapshot(`""`);
58
- });
59
-
60
- it("should not warn if there is a wrangler.toml but compatibility-date is specified at the command line", async () => {
61
- writeWranglerToml({
62
- main: "index.js",
63
- compatibility_date: undefined,
64
- });
65
- fs.writeFileSync("index.js", `export default {};`);
66
- await runWrangler("dev --compatibility-date=2020-01-01");
67
- expect(std.out).toMatchInlineSnapshot(`""`);
68
- expect(std.warn).toMatchInlineSnapshot(`""`);
69
- expect(std.err).toMatchInlineSnapshot(`""`);
70
- });
71
- });
72
-
73
- describe("entry-points", () => {
74
- it("should error if there is no entry-point specified", async () => {
75
- writeWranglerToml();
76
-
77
- await expect(
78
- runWrangler("dev")
79
- ).rejects.toThrowErrorMatchingInlineSnapshot(
80
- `"Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler dev path/to/script\`) or the \`main\` config field."`
81
- );
82
-
83
- expect(std.out).toMatchInlineSnapshot(`
84
- "
85
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
86
- `);
87
- expect(std.err).toMatchInlineSnapshot(`
88
- "X [ERROR] Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler dev path/to/script\`) or the \`main\` config field.
89
-
90
- "
91
- `);
92
- });
93
-
94
- it("should use `main` from the top-level environment", async () => {
95
- writeWranglerToml({
96
- main: "index.js",
97
- });
98
- fs.writeFileSync("index.js", `export default {};`);
99
- await runWrangler("dev");
100
- expect((Dev as jest.Mock).mock.calls[0][0].entry.file).toMatch(
101
- /index\.js$/
102
- );
103
- expect(std.out).toMatchInlineSnapshot(`""`);
104
- expect(std.warn).toMatchInlineSnapshot(`""`);
105
- expect(std.err).toMatchInlineSnapshot(`""`);
106
- });
107
-
108
- it("should use `main` from a named environment", async () => {
109
- writeWranglerToml({
110
- env: {
111
- ENV1: {
112
- main: "index.js",
113
- },
114
- },
115
- });
116
- fs.writeFileSync("index.js", `export default {};`);
117
- await runWrangler("dev --env=ENV1");
118
- expect((Dev as jest.Mock).mock.calls[0][0].entry.file).toMatch(
119
- /index\.js$/
120
- );
121
- expect(std.out).toMatchInlineSnapshot(`""`);
122
- expect(std.warn).toMatchInlineSnapshot(`""`);
123
- expect(std.err).toMatchInlineSnapshot(`""`);
124
- });
125
-
126
- it("should use `main` from a named environment, rather than the top-level", async () => {
127
- writeWranglerToml({
128
- main: "other.js",
129
- env: {
130
- ENV1: {
131
- main: "index.js",
132
- },
133
- },
134
- });
135
- fs.writeFileSync("index.js", `export default {};`);
136
- await runWrangler("dev --env=ENV1");
137
- expect((Dev as jest.Mock).mock.calls[0][0].entry.file).toMatch(
138
- /index\.js$/
139
- );
140
- expect(std.out).toMatchInlineSnapshot(`""`);
141
- expect(std.warn).toMatchInlineSnapshot(`""`);
142
- expect(std.err).toMatchInlineSnapshot(`""`);
143
- });
144
- });
145
-
146
- describe("host", () => {
147
- it("should resolve a host to its zone", async () => {
148
- writeWranglerToml({
149
- main: "index.js",
150
- });
151
- fs.writeFileSync("index.js", `export default {};`);
152
- mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
153
- await runWrangler("dev --host some-host.com");
154
- expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
155
- host: "some-host.com",
156
- id: "some-zone-id",
157
- });
158
- });
159
-
160
- it("should read wrangler.toml's dev.host", async () => {
161
- writeWranglerToml({
162
- main: "index.js",
163
- dev: {
164
- host: "some-host.com",
165
- },
166
- });
167
- fs.writeFileSync("index.js", `export default {};`);
168
- mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
169
- await runWrangler("dev");
170
- expect((Dev as jest.Mock).mock.calls[0][0].zone.host).toEqual(
171
- "some-host.com"
172
- );
173
- });
174
-
175
- it("should read --route", async () => {
176
- writeWranglerToml({
177
- main: "index.js",
178
- });
179
- fs.writeFileSync("index.js", `export default {};`);
180
- mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
181
- await runWrangler("dev --route http://some-host.com/some/path/*");
182
- expect((Dev as jest.Mock).mock.calls[0][0].zone.host).toEqual(
183
- "some-host.com"
184
- );
185
- });
186
-
187
- it("should read wrangler.toml's routes", async () => {
188
- writeWranglerToml({
189
- main: "index.js",
190
- routes: [
191
- "http://some-host.com/some/path/*",
192
- "http://some-other-host.com/path/*",
193
- ],
194
- });
195
- fs.writeFileSync("index.js", `export default {};`);
196
- mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
197
- await runWrangler("dev");
198
- expect((Dev as jest.Mock).mock.calls[0][0].zone.host).toEqual(
199
- "some-host.com"
200
- );
201
- });
202
-
203
- it("should read wrangler.toml's environment specific routes", async () => {
204
- writeWranglerToml({
205
- main: "index.js",
206
- routes: [
207
- "http://a-host.com/some/path/*",
208
- "http://another-host.com/path/*",
209
- ],
210
- env: {
211
- staging: {
212
- routes: [
213
- "http://some-host.com/some/path/*",
214
- "http://some-other-host.com/path/*",
215
- ],
216
- },
217
- },
218
- });
219
- fs.writeFileSync("index.js", `export default {};`);
220
- mockGetZones("some-host.com", [{ id: "some-zone-id" }]);
221
- await runWrangler("dev --env staging");
222
- expect((Dev as jest.Mock).mock.calls[0][0].zone.host).toEqual(
223
- "some-host.com"
224
- );
225
- });
226
-
227
- it("should, when provided, use a configured zone_id", async () => {
228
- writeWranglerToml({
229
- main: "index.js",
230
- routes: [
231
- { pattern: "https://some-domain.com/*", zone_id: "some-zone-id" },
232
- ],
233
- });
234
- fs.writeFileSync("index.js", `export default {};`);
235
- await runWrangler("dev");
236
- expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
237
- host: "some-domain.com",
238
- id: "some-zone-id",
239
- });
240
- });
241
-
242
- it("should, when provided, use a zone_name to get a zone_id", async () => {
243
- writeWranglerToml({
244
- main: "index.js",
245
- routes: [
246
- { pattern: "https://some-domain.com/*", zone_name: "some-zone.com" },
247
- ],
248
- });
249
- fs.writeFileSync("index.js", `export default {};`);
250
- mockGetZones("some-zone.com", [{ id: "a-zone-id" }]);
251
- await runWrangler("dev");
252
- expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
253
- // note that it uses the provided zone_name as a host too
254
- host: "some-zone.com",
255
- id: "a-zone-id",
256
- });
257
- });
258
-
259
- it("given a long host, it should use the longest subdomain that resolves to a zone", async () => {
260
- writeWranglerToml({
261
- main: "index.js",
262
- });
263
- fs.writeFileSync("index.js", `export default {};`);
264
- mockGetZones("111.222.333.some-host.com", []);
265
- mockGetZones("222.333.some-host.com", []);
266
- mockGetZones("333.some-host.com", [{ id: "some-zone-id" }]);
267
- await runWrangler("dev --host 111.222.333.some-host.com");
268
- expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
269
- host: "111.222.333.some-host.com",
270
- id: "some-zone-id",
271
- });
272
- });
273
-
274
- it("should, in order, use args.host/config.dev.host/args.routes/(config.route|config.routes)", async () => {
275
- // This test might seem like it's testing implementation details, but let's be specific and consider it a spec
276
-
277
- fs.writeFileSync("index.js", `export default {};`);
278
-
279
- // config.routes
280
- mockGetZones("5.some-host.com", [{ id: "some-zone-id-5" }]);
281
- writeWranglerToml({
282
- main: "index.js",
283
- routes: ["http://5.some-host.com/some/path/*"],
284
- });
285
- await runWrangler("dev");
286
- expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
287
- host: "5.some-host.com",
288
- id: "some-zone-id-5",
289
- });
290
- (Dev as jest.Mock).mockClear();
291
-
292
- // config.route
293
- mockGetZones("4.some-host.com", [{ id: "some-zone-id-4" }]);
294
- writeWranglerToml({
295
- main: "index.js",
296
- route: "https://4.some-host.com/some/path/*",
297
- });
298
- await runWrangler("dev");
299
- expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
300
- host: "4.some-host.com",
301
- id: "some-zone-id-4",
302
- });
303
- (Dev as jest.Mock).mockClear();
304
-
305
- // --routes
306
- mockGetZones("3.some-host.com", [{ id: "some-zone-id-3" }]);
307
- writeWranglerToml({
308
- main: "index.js",
309
- route: "https://4.some-host.com/some/path/*",
310
- });
311
- await runWrangler("dev --routes http://3.some-host.com/some/path/*");
312
- expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
313
- host: "3.some-host.com",
314
- id: "some-zone-id-3",
315
- });
316
- (Dev as jest.Mock).mockClear();
317
-
318
- // config.dev.host
319
- mockGetZones("2.some-host.com", [{ id: "some-zone-id-2" }]);
320
- writeWranglerToml({
321
- main: "index.js",
322
- dev: {
323
- host: `2.some-host.com`,
324
- },
325
- route: "4.some-host.com/some/path/*",
326
- });
327
- await runWrangler("dev --routes http://3.some-host.com/some/path/*");
328
- expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
329
- host: "2.some-host.com",
330
- id: "some-zone-id-2",
331
- });
332
- (Dev as jest.Mock).mockClear();
333
-
334
- // --host
335
- mockGetZones("1.some-host.com", [{ id: "some-zone-id-1" }]);
336
- writeWranglerToml({
337
- main: "index.js",
338
- dev: {
339
- host: `2.some-host.com`,
340
- },
341
- route: "4.some-host.com/some/path/*",
342
- });
343
- await runWrangler(
344
- "dev --routes http://3.some-host.com/some/path/* --host 1.some-host.com"
345
- );
346
- expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual({
347
- host: "1.some-host.com",
348
- id: "some-zone-id-1",
349
- });
350
- (Dev as jest.Mock).mockClear();
351
- });
352
-
353
- it("should error if a host can't resolve to a zone", async () => {
354
- writeWranglerToml({
355
- main: "index.js",
356
- });
357
- fs.writeFileSync("index.js", `export default {};`);
358
- mockGetZones("some-host.com", []);
359
- await expect(
360
- runWrangler("dev --host some-host.com")
361
- ).rejects.toThrowErrorMatchingInlineSnapshot(
362
- `"Could not find zone for some-host.com"`
363
- );
364
- });
365
-
366
- it("should not try to resolve a zone when starting in local mode", async () => {
367
- writeWranglerToml({
368
- main: "index.js",
369
- });
370
- fs.writeFileSync("index.js", `export default {};`);
371
- await runWrangler("dev --host some-host.com --local");
372
- expect((Dev as jest.Mock).mock.calls[0][0].zone).toEqual(undefined);
373
- });
374
- });
375
-
376
- describe("custom builds", () => {
377
- it("should run a custom build before starting `dev`", async () => {
378
- writeWranglerToml({
379
- build: {
380
- command: `node -e "console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')"`,
381
- },
382
- });
383
-
384
- await runWrangler("dev index.js");
385
-
386
- expect(fs.readFileSync("index.js", "utf-8")).toMatchInlineSnapshot(
387
- `"export default { fetch(){ return new Response(123) } }"`
388
- );
389
-
390
- // and the command would pass through
391
- expect((Dev as jest.Mock).mock.calls[0][0].build).toEqual({
392
- command:
393
- "node -e \"console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')\"",
394
- cwd: undefined,
395
- watch_dir: "src",
396
- });
397
- expect(std.out).toMatchInlineSnapshot(
398
- `"Running custom build: node -e \\"console.log('custom build'); require('fs').writeFileSync('index.js', 'export default { fetch(){ return new Response(123) } }')\\""`
399
- );
400
- expect(std.err).toMatchInlineSnapshot(`""`);
401
- expect(std.warn).toMatchInlineSnapshot(`""`);
402
- });
403
-
404
- if (process.platform !== "win32") {
405
- it("should run a custom build of multiple steps combined by && before starting `dev`", async () => {
406
- writeWranglerToml({
407
- build: {
408
- command: `echo "custom build" && echo "export default { fetch(){ return new Response(123) } }" > index.js`,
409
- },
410
- });
411
-
412
- await runWrangler("dev index.js");
413
-
414
- expect(fs.readFileSync("index.js", "utf-8")).toMatchInlineSnapshot(`
415
- "export default { fetch(){ return new Response(123) } }
416
- "
417
- `);
418
-
419
- expect(std.out).toMatchInlineSnapshot(
420
- `"Running custom build: echo \\"custom build\\" && echo \\"export default { fetch(){ return new Response(123) } }\\" > index.js"`
421
- );
422
- expect(std.err).toMatchInlineSnapshot(`""`);
423
- expect(std.warn).toMatchInlineSnapshot(`""`);
424
- });
425
- }
426
-
427
- it("should throw an error if the entry doesn't exist after the build finishes", async () => {
428
- writeWranglerToml({
429
- main: "index.js",
430
- build: {
431
- command: `node -e "console.log('custom build');"`,
432
- },
433
- });
434
-
435
- await expect(
436
- runWrangler("dev")
437
- ).rejects.toThrowErrorMatchingInlineSnapshot(
438
- `"Could not resolve \\"index.js\\" after running custom build: node -e \\"console.log('custom build');\\""`
439
- );
440
- expect(std.out).toMatchInlineSnapshot(`
441
- "Running custom build: node -e \\"console.log('custom build');\\"
442
-
443
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
444
- `);
445
- expect(std.err).toMatchInlineSnapshot(`
446
- "X [ERROR] Could not resolve \\"index.js\\" after running custom build: node -e \\"console.log('custom build');\\"
447
-
448
- "
449
- `);
450
- expect(std.warn).toMatchInlineSnapshot(`""`);
451
- });
452
- });
453
-
454
- describe("upstream-protocol", () => {
455
- it("should default upstream-protocol to `https`", async () => {
456
- writeWranglerToml({
457
- main: "index.js",
458
- });
459
- fs.writeFileSync("index.js", `export default {};`);
460
- await runWrangler("dev");
461
- expect((Dev as jest.Mock).mock.calls[0][0].upstreamProtocol).toEqual(
462
- "https"
463
- );
464
- expect(std.out).toMatchInlineSnapshot(`""`);
465
- expect(std.warn).toMatchInlineSnapshot(`""`);
466
- expect(std.err).toMatchInlineSnapshot(`""`);
467
- });
468
-
469
- it("should warn if `--upstream-protocol=http` is used", async () => {
470
- writeWranglerToml({
471
- main: "index.js",
472
- });
473
- fs.writeFileSync("index.js", `export default {};`);
474
- await runWrangler("dev --upstream-protocol=http");
475
- expect((Dev as jest.Mock).mock.calls[0][0].upstreamProtocol).toEqual(
476
- "http"
477
- );
478
- expect(std.out).toMatchInlineSnapshot(`""`);
479
- expect(std.warn).toMatchInlineSnapshot(`
480
- "â–² [WARNING] Setting upstream-protocol to http is not currently implemented.
481
-
482
- If this is required in your project, please add your use case to the following issue:
483
- https://github.com/cloudflare/wrangler2/issues/583.
484
-
485
- "
486
- `);
487
- expect(std.err).toMatchInlineSnapshot(`""`);
488
- });
489
- });
490
-
491
- describe("local-protocol", () => {
492
- it("should default local-protocol to `http`", async () => {
493
- writeWranglerToml({
494
- main: "index.js",
495
- });
496
- fs.writeFileSync("index.js", `export default {};`);
497
- await runWrangler("dev");
498
- expect((Dev as jest.Mock).mock.calls[0][0].localProtocol).toEqual("http");
499
- expect(std.out).toMatchInlineSnapshot(`""`);
500
- expect(std.warn).toMatchInlineSnapshot(`""`);
501
- expect(std.err).toMatchInlineSnapshot(`""`);
502
- });
503
-
504
- it("should use `local_protocol` from `wrangler.toml`, if available", async () => {
505
- writeWranglerToml({
506
- main: "index.js",
507
- dev: {
508
- local_protocol: "https",
509
- },
510
- });
511
- fs.writeFileSync("index.js", `export default {};`);
512
- await runWrangler("dev");
513
- expect((Dev as jest.Mock).mock.calls[0][0].localProtocol).toEqual(
514
- "https"
515
- );
516
- expect(std.out).toMatchInlineSnapshot(`""`);
517
- expect(std.warn).toMatchInlineSnapshot(`""`);
518
- expect(std.err).toMatchInlineSnapshot(`""`);
519
- });
520
-
521
- it("should use --local-protocol command line arg, if provided", async () => {
522
- // Here we show that the command line overrides the wrangler.toml by
523
- // setting the config to https, and then setting it back to http on the command line.
524
- writeWranglerToml({
525
- main: "index.js",
526
- dev: {
527
- local_protocol: "https",
528
- },
529
- });
530
- fs.writeFileSync("index.js", `export default {};`);
531
- await runWrangler("dev --local-protocol=http");
532
- expect((Dev as jest.Mock).mock.calls[0][0].localProtocol).toEqual("http");
533
- expect(std.out).toMatchInlineSnapshot(`""`);
534
- expect(std.warn).toMatchInlineSnapshot(`""`);
535
- expect(std.err).toMatchInlineSnapshot(`""`);
536
- });
537
- });
538
-
539
- describe("ip", () => {
540
- it("should default ip to localhost", async () => {
541
- writeWranglerToml({
542
- main: "index.js",
543
- });
544
- fs.writeFileSync("index.js", `export default {};`);
545
- await runWrangler("dev");
546
- expect((Dev as jest.Mock).mock.calls[0][0].ip).toEqual("localhost");
547
- expect(std.out).toMatchInlineSnapshot(`""`);
548
- expect(std.warn).toMatchInlineSnapshot(`""`);
549
- expect(std.err).toMatchInlineSnapshot(`""`);
550
- });
551
-
552
- it("should use to `ip` from `wrangler.toml`, if available", async () => {
553
- writeWranglerToml({
554
- main: "index.js",
555
- dev: {
556
- ip: "0.0.0.0",
557
- },
558
- });
559
- fs.writeFileSync("index.js", `export default {};`);
560
- await runWrangler("dev");
561
- expect((Dev as jest.Mock).mock.calls[0][0].ip).toEqual("0.0.0.0");
562
- expect(std.out).toMatchInlineSnapshot(`""`);
563
- expect(std.warn).toMatchInlineSnapshot(`""`);
564
- expect(std.err).toMatchInlineSnapshot(`""`);
565
- });
566
-
567
- it("should use --ip command line arg, if provided", async () => {
568
- writeWranglerToml({
569
- main: "index.js",
570
- dev: {
571
- ip: "1.1.1.1",
572
- },
573
- });
574
- fs.writeFileSync("index.js", `export default {};`);
575
- await runWrangler("dev --ip=0.0.0.0");
576
- expect((Dev as jest.Mock).mock.calls[0][0].ip).toEqual("0.0.0.0");
577
- expect(std.out).toMatchInlineSnapshot(`""`);
578
- expect(std.warn).toMatchInlineSnapshot(`""`);
579
- expect(std.err).toMatchInlineSnapshot(`""`);
580
- });
581
- });
582
-
583
- describe("durable_objects", () => {
584
- it("should warn if there is one or more remote Durable Object", async () => {
585
- writeWranglerToml({
586
- main: "index.js",
587
- durable_objects: {
588
- bindings: [
589
- { name: "NAME_1", class_name: "CLASS_1" },
590
- {
591
- name: "NAME_2",
592
- class_name: "CLASS_2",
593
- script_name: "SCRIPT_A",
594
- },
595
- { name: "NAME_3", class_name: "CLASS_3" },
596
- {
597
- name: "NAME_4",
598
- class_name: "CLASS_4",
599
- script_name: "SCRIPT_B",
600
- },
601
- ],
602
- },
603
- });
604
- fs.writeFileSync("index.js", `export default {};`);
605
- await runWrangler("dev");
606
- expect((Dev as jest.Mock).mock.calls[0][0].ip).toEqual("localhost");
607
- expect(std.out).toMatchInlineSnapshot(`""`);
608
- expect(std.warn).toMatchInlineSnapshot(`
609
- "â–² [WARNING] WARNING: You have Durable Object bindings that are not defined locally in the worker being developed.
610
-
611
- Be aware that changes to the data stored in these Durable Objects will be permanent and affect the
612
- live instances.
613
- Remote Durable Objects that are affected:
614
- - {\\"name\\":\\"NAME_2\\",\\"class_name\\":\\"CLASS_2\\",\\"script_name\\":\\"SCRIPT_A\\"}
615
- - {\\"name\\":\\"NAME_4\\",\\"class_name\\":\\"CLASS_4\\",\\"script_name\\":\\"SCRIPT_B\\"}
616
-
617
- "
618
- `);
619
- expect(std.err).toMatchInlineSnapshot(`""`);
620
- });
621
- });
622
-
623
- describe(".dev.vars", () => {
624
- it("should override `vars` bindings from `wrangler.toml` with values in `.dev.vars`", async () => {
625
- fs.writeFileSync("index.js", `export default {};`);
626
-
627
- const localVarsEnvContent = dedent`
628
- # Preceding comment
629
- VAR_1="var #1 value" # End of line comment
630
- VAR_3="var #3 value"
631
- VAR_MULTI_LINE_1="A: line 1
632
- line 2"
633
- VAR_MULTI_LINE_2="B: line 1\\nline 2"
634
- EMPTY=
635
- UNQUOTED= unquoted value
636
- `;
637
- fs.writeFileSync(".dev.vars", localVarsEnvContent, "utf8");
638
-
639
- writeWranglerToml({
640
- main: "index.js",
641
- vars: {
642
- VAR_1: "original value 1",
643
- VAR_2: "original value 2", // should not get overridden
644
- VAR_3: "original value 3",
645
- VAR_MULTI_LINE_1: "original multi-line 1",
646
- VAR_MULTI_LINE_2: "original multi-line 2",
647
- EMPTY: "original empty",
648
- UNQUOTED: "original unquoted",
649
- },
650
- });
651
- await runWrangler("dev");
652
- const varBindings: Record<string, unknown> = (Dev as jest.Mock).mock
653
- .calls[0][0].bindings.vars;
654
-
655
- expect(varBindings).toEqual({
656
- VAR_1: "var #1 value",
657
- VAR_2: "original value 2",
658
- VAR_3: "var #3 value",
659
- VAR_MULTI_LINE_1: "A: line 1\nline 2",
660
- VAR_MULTI_LINE_2: "B: line 1\nline 2",
661
- EMPTY: "",
662
- UNQUOTED: "unquoted value", // Note that whitespace is trimmed
663
- });
664
- expect(std.out).toMatchInlineSnapshot(
665
- `"Using vars defined in .dev.vars"`
666
- );
667
- expect(std.warn).toMatchInlineSnapshot(`""`);
668
- expect(std.err).toMatchInlineSnapshot(`""`);
669
- });
670
- });
671
-
672
- describe("site", () => {
673
- it("should error if --site is used with no value", async () => {
674
- await expect(
675
- runWrangler("dev --site")
676
- ).rejects.toThrowErrorMatchingInlineSnapshot(
677
- `"Not enough arguments following: site"`
678
- );
679
-
680
- expect(std).toMatchInlineSnapshot(`
681
- Object {
682
- "debug": "",
683
- "err": "wrangler dev [script]
684
-
685
- 👂 Start a local server for developing your worker
686
-
687
- Positionals:
688
- script The path to an entry point for your worker [string]
689
-
690
- Flags:
691
- -c, --config Path to .toml configuration file [string]
692
- -h, --help Show help [boolean]
693
- -v, --version Show version number [boolean]
694
- --legacy-env Use legacy environments [boolean]
695
-
696
- Options:
697
- --name Name of the worker [string]
698
- --format Choose an entry type [deprecated] [choices: \\"modules\\", \\"service-worker\\"]
699
- -e, --env Perform on a specific environment [string]
700
- --compatibility-date Date to use for compatibility checks [string]
701
- --compatibility-flags, --compatibility-flag Flags to use for compatibility checks [array]
702
- --latest Use the latest version of the worker runtime [boolean] [default: true]
703
- --ip IP address to listen on, defaults to \`localhost\` [string]
704
- --port Port to listen on [number]
705
- --inspector-port Port for devtools to connect to [number]
706
- --routes, --route Routes to upload [array]
707
- --host Host to forward requests to, defaults to the zone of project [string]
708
- --local-protocol Protocol to listen to requests on, defaults to http. [choices: \\"http\\", \\"https\\"]
709
- --experimental-public Static assets to be served [string]
710
- --site Root folder of static assets for Workers Sites [string]
711
- --site-include Array of .gitignore-style patterns that match file or directory names from the sites directory. Only matched items will be uploaded. [array]
712
- --site-exclude Array of .gitignore-style patterns that match file or directory names from the sites directory. Matched items will not be uploaded. [array]
713
- --upstream-protocol Protocol to forward requests to host on, defaults to https. [choices: \\"http\\", \\"https\\"]
714
- --jsx-factory The function that is called for each JSX element [string]
715
- --jsx-fragment The function that is called for each JSX fragment [string]
716
- --tsconfig Path to a custom tsconfig.json file [string]
717
- -l, --local Run on my machine [boolean] [default: false]
718
- --minify Minify the script [boolean]
719
- --node-compat Enable node.js compatibility [boolean]
720
- --experimental-enable-local-persistence Enable persistence for this session (only for local mode) [boolean]
721
- --inspect Enable dev tools [deprecated] [boolean]
722
- X [ERROR] Not enough arguments following: site
723
-
724
- ",
725
- "out": "
726
- ",
727
- "warn": "",
728
- }
729
- `);
730
- });
731
- });
732
-
733
- describe("--inspect", () => {
734
- it("should warn if --inspect is used", async () => {
735
- fs.writeFileSync("index.js", `export default {};`);
736
- await runWrangler("dev index.js --inspect");
737
- expect(std).toMatchInlineSnapshot(`
738
- Object {
739
- "debug": "",
740
- "err": "",
741
- "out": "",
742
- "warn": "â–² [WARNING] Passing --inspect is unnecessary, now you can always connect to devtools.
743
-
744
- ",
745
- }
746
- `);
747
- });
748
- });
749
- });
750
-
751
- function mockGetZones(domain: string, zones: { id: string }[] = []) {
752
- const removeMock = setMockResponse(
753
- "/zones",
754
- "GET",
755
- (_urlPieces, _init, queryParams) => {
756
- expect([...queryParams.entries()]).toEqual([["name", domain]]);
757
- // Because the API URL `/zones` is the same for each request, we can get into a situation where earlier mocks get triggered for later requests. So, we simply clear the mock on every trigger.
758
- removeMock();
759
- return zones;
760
- }
761
- );
762
- }