wrangler 0.0.0-e0d2f35 → 0.0.0-e144f63f

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 (127) hide show
  1. package/README.md +47 -16
  2. package/bin/wrangler.js +94 -31
  3. package/config-schema.json +2947 -0
  4. package/kv-asset-handler.js +1 -0
  5. package/package.json +145 -82
  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 +167 -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 +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-pretty-error.ts +40 -0
  21. package/templates/middleware/middleware-scheduled.ts +15 -0
  22. package/templates/middleware/middleware-serve-static-assets.d.ts +6 -0
  23. package/templates/middleware/middleware-serve-static-assets.ts +56 -0
  24. package/templates/modules-watch-stub.js +4 -0
  25. package/templates/new-worker-scheduled.js +17 -0
  26. package/templates/new-worker-scheduled.ts +32 -0
  27. package/templates/new-worker.js +15 -0
  28. package/templates/new-worker.ts +33 -0
  29. package/templates/no-op-worker.js +10 -0
  30. package/templates/pages-dev-pipeline.ts +32 -0
  31. package/templates/pages-dev-util.ts +55 -0
  32. package/templates/pages-shim.ts +9 -0
  33. package/templates/pages-template-plugin.ts +190 -0
  34. package/templates/pages-template-worker.ts +198 -0
  35. package/templates/startDevWorker/InspectorProxyWorker.ts +664 -0
  36. package/templates/startDevWorker/ProxyWorker.ts +334 -0
  37. package/templates/tsconfig-sanity.ts +11 -0
  38. package/templates/tsconfig.init.json +105 -0
  39. package/templates/tsconfig.json +8 -0
  40. package/wrangler-dist/InspectorProxyWorker.js +464 -0
  41. package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
  42. package/wrangler-dist/ProxyWorker.js +240 -0
  43. package/wrangler-dist/ProxyWorker.js.map +6 -0
  44. package/wrangler-dist/cli.d.ts +24341 -0
  45. package/wrangler-dist/cli.js +201242 -117397
  46. package/wrangler-dist/wasm-sync.wasm +0 -0
  47. package/import_meta_url.js +0 -3
  48. package/miniflare-config-stubs/.env.empty +0 -0
  49. package/miniflare-config-stubs/package.empty.json +0 -1
  50. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  51. package/pages/functions/buildWorker.ts +0 -62
  52. package/pages/functions/filepath-routing.test.ts +0 -39
  53. package/pages/functions/filepath-routing.ts +0 -228
  54. package/pages/functions/identifiers.ts +0 -78
  55. package/pages/functions/routes.ts +0 -158
  56. package/pages/functions/template-worker.ts +0 -136
  57. package/src/__tests__/clipboardy-mock.js +0 -4
  58. package/src/__tests__/dev.test.tsx +0 -71
  59. package/src/__tests__/index.test.ts +0 -424
  60. package/src/__tests__/jest.setup.ts +0 -29
  61. package/src/__tests__/kv.test.ts +0 -1224
  62. package/src/__tests__/logout.test.ts +0 -64
  63. package/src/__tests__/mock-cfetch.ts +0 -173
  64. package/src/__tests__/mock-console.ts +0 -34
  65. package/src/__tests__/mock-dialogs.ts +0 -65
  66. package/src/__tests__/mock-kv.ts +0 -40
  67. package/src/__tests__/mock-user.ts +0 -27
  68. package/src/__tests__/publish.test.ts +0 -1206
  69. package/src/__tests__/run-in-tmp.ts +0 -29
  70. package/src/__tests__/run-wrangler.ts +0 -8
  71. package/src/__tests__/secret.test.ts +0 -206
  72. package/src/__tests__/whoami.test.tsx +0 -101
  73. package/src/api/form_data.ts +0 -135
  74. package/src/api/preview.ts +0 -128
  75. package/src/api/worker.ts +0 -155
  76. package/src/cfetch/index.ts +0 -111
  77. package/src/cfetch/internal.ts +0 -71
  78. package/src/cli.ts +0 -9
  79. package/src/config.ts +0 -641
  80. package/src/dev.tsx +0 -830
  81. package/src/dialogs.tsx +0 -77
  82. package/src/index.tsx +0 -2078
  83. package/src/inspect.ts +0 -595
  84. package/src/kv.tsx +0 -276
  85. package/src/module-collection.ts +0 -63
  86. package/src/pages.tsx +0 -1043
  87. package/src/proxy.ts +0 -370
  88. package/src/publish.ts +0 -410
  89. package/src/sites.tsx +0 -240
  90. package/src/tail.tsx +0 -73
  91. package/src/user.tsx +0 -1019
  92. package/src/whoami.tsx +0 -69
  93. package/static-asset-facade.js +0 -47
  94. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  95. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  96. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  97. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  98. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  99. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  100. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  101. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  102. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  103. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  104. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  105. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  106. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  107. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  108. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  109. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  110. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  111. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  112. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  113. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  114. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  115. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  116. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  117. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  118. package/vendor/wrangler-mime/LICENSE +0 -21
  119. package/vendor/wrangler-mime/Mime.js +0 -97
  120. package/vendor/wrangler-mime/README.md +0 -187
  121. package/vendor/wrangler-mime/cli.js +0 -46
  122. package/vendor/wrangler-mime/index.js +0 -4
  123. package/vendor/wrangler-mime/lite.js +0 -4
  124. package/vendor/wrangler-mime/package.json +0 -52
  125. package/vendor/wrangler-mime/types/other.js +0 -1
  126. package/vendor/wrangler-mime/types/standard.js +0 -1
  127. package/wrangler-dist/cli.js.map +0 -7
@@ -1,1206 +0,0 @@
1
- import * as fs from "node:fs";
2
- import * as path from "node:path";
3
- import type { KVNamespaceInfo } from "../kv";
4
- import { mockKeyListRequest } from "./mock-kv";
5
- import { setMockResponse, unsetAllMocks } from "./mock-cfetch";
6
- import { runInTempDir } from "./run-in-tmp";
7
- import { runWrangler } from "./run-wrangler";
8
- import { mockConsoleMethods } from "./mock-console";
9
- import type { Config } from "../config";
10
- import * as TOML from "@iarna/toml";
11
- import type { WorkerMetadata } from "../api/form_data";
12
-
13
- describe("publish", () => {
14
- runInTempDir();
15
- const std = mockConsoleMethods();
16
-
17
- afterEach(() => {
18
- unsetAllMocks();
19
- });
20
-
21
- describe("entry-points", () => {
22
- it("should be able to use `index` with no extension as the entry-point (esm)", async () => {
23
- writeWranglerToml();
24
- writeWorkerSource();
25
- mockUploadWorkerRequest({ expectedType: "esm" });
26
- mockSubDomainRequest();
27
-
28
- await runWrangler("publish ./index");
29
-
30
- expect(stripTimings(std.out)).toMatchInlineSnapshot(`
31
- "Uploaded
32
- test-name
33
- (TIMINGS)
34
- Deployed
35
- test-name
36
- (TIMINGS)
37
-
38
- test-name.test-sub-domain.workers.dev"
39
- `);
40
- expect(std.err).toMatchInlineSnapshot(`""`);
41
- });
42
- it("should be able to use `index` with no extension as the entry-point (sw)", async () => {
43
- writeWranglerToml();
44
- writeWorkerSource({ type: "sw" });
45
- mockUploadWorkerRequest({ expectedType: "sw" });
46
- mockSubDomainRequest();
47
-
48
- await runWrangler("publish ./index");
49
-
50
- expect(stripTimings(std.out)).toMatchInlineSnapshot(`
51
- "Uploaded
52
- test-name
53
- (TIMINGS)
54
- Deployed
55
- test-name
56
- (TIMINGS)
57
-
58
- test-name.test-sub-domain.workers.dev"
59
- `);
60
- expect(std.err).toMatchInlineSnapshot(`""`);
61
- });
62
-
63
- it("should be able to use the `build.upload.main` config as the entry-point for ESM sources", async () => {
64
- writeWranglerToml({ build: { upload: { main: "./index.js" } } });
65
- writeWorkerSource();
66
- mockUploadWorkerRequest();
67
- mockSubDomainRequest();
68
-
69
- await runWrangler("publish");
70
-
71
- expect(stripTimings(std.out)).toMatchInlineSnapshot(`
72
- "Uploaded
73
- test-name
74
- (TIMINGS)
75
- Deployed
76
- test-name
77
- (TIMINGS)
78
-
79
- test-name.test-sub-domain.workers.dev"
80
- `);
81
- expect(std.err).toMatchInlineSnapshot(`""`);
82
- });
83
-
84
- it("should be able to transpile TypeScript (esm)", async () => {
85
- writeWranglerToml();
86
- writeWorkerSource({ format: "ts" });
87
- mockUploadWorkerRequest({ expectedEntry: "var foo = 100;" });
88
- mockSubDomainRequest();
89
- await runWrangler("publish index.ts");
90
-
91
- expect(stripTimings(std.out)).toMatchInlineSnapshot(`
92
- "Uploaded
93
- test-name
94
- (TIMINGS)
95
- Deployed
96
- test-name
97
- (TIMINGS)
98
-
99
- test-name.test-sub-domain.workers.dev"
100
- `);
101
- expect(std.err).toMatchInlineSnapshot(`""`);
102
- });
103
-
104
- it("should be able to transpile TypeScript (sw)", async () => {
105
- writeWranglerToml();
106
- writeWorkerSource({ format: "ts", type: "sw" });
107
- mockUploadWorkerRequest({
108
- expectedEntry: "var foo = 100;",
109
- expectedType: "sw",
110
- });
111
- mockSubDomainRequest();
112
- await runWrangler("publish index.ts");
113
-
114
- expect(stripTimings(std.out)).toMatchInlineSnapshot(`
115
- "Uploaded
116
- test-name
117
- (TIMINGS)
118
- Deployed
119
- test-name
120
- (TIMINGS)
121
-
122
- test-name.test-sub-domain.workers.dev"
123
- `);
124
- expect(std.err).toMatchInlineSnapshot(`""`);
125
- });
126
-
127
- it("should inline referenced text modules into the worker", async () => {
128
- writeWranglerToml();
129
- fs.writeFileSync(
130
- "./index.js",
131
- `
132
- import txt from './textfile.txt';
133
- export default{
134
- fetch(){
135
- return new Response(txt);
136
- }
137
- }
138
- `
139
- );
140
- fs.writeFileSync("./textfile.txt", "Hello, World!");
141
- mockUploadWorkerRequest({ expectedEntry: "Hello, World!" });
142
- mockSubDomainRequest();
143
- await runWrangler("publish index.js");
144
- expect(stripTimings(std.out)).toMatchInlineSnapshot(`
145
- "Uploaded
146
- test-name
147
- (TIMINGS)
148
- Deployed
149
- test-name
150
- (TIMINGS)
151
-
152
- test-name.test-sub-domain.workers.dev"
153
- `);
154
- expect(std.err).toMatchInlineSnapshot(`""`);
155
- });
156
-
157
- it("should be able to transpile entry-points in sub-directories (esm)", async () => {
158
- writeWranglerToml();
159
- writeWorkerSource({ basePath: "./src" });
160
- mockUploadWorkerRequest({ expectedEntry: "var foo = 100;" });
161
- mockSubDomainRequest();
162
-
163
- await runWrangler("publish ./src/index.js");
164
-
165
- expect(stripTimings(std.out)).toMatchInlineSnapshot(`
166
- "Uploaded
167
- test-name
168
- (TIMINGS)
169
- Deployed
170
- test-name
171
- (TIMINGS)
172
-
173
- test-name.test-sub-domain.workers.dev"
174
- `);
175
- expect(std.err).toMatchInlineSnapshot(`""`);
176
- });
177
-
178
- it("should be able to transpile entry-points in sub-directories (sw)", async () => {
179
- writeWranglerToml();
180
- writeWorkerSource({ basePath: "./src", type: "sw" });
181
- mockUploadWorkerRequest({
182
- expectedEntry: "var foo = 100;",
183
- expectedType: "sw",
184
- });
185
- mockSubDomainRequest();
186
-
187
- await runWrangler("publish ./src/index.js");
188
-
189
- expect(stripTimings(std.out)).toMatchInlineSnapshot(`
190
- "Uploaded
191
- test-name
192
- (TIMINGS)
193
- Deployed
194
- test-name
195
- (TIMINGS)
196
-
197
- test-name.test-sub-domain.workers.dev"
198
- `);
199
- expect(std.err).toMatchInlineSnapshot(`""`);
200
- });
201
-
202
- it('should error if a site definition doesn\'t have a "bucket" field', async () => {
203
- writeWranglerToml({
204
- // @ts-expect-error we're purposely missing the required `site.bucket` field
205
- site: {
206
- "entry-point": "./index.js",
207
- },
208
- });
209
- writeWorkerSource();
210
- mockUploadWorkerRequest();
211
- mockSubDomainRequest();
212
- let error: Error | undefined;
213
- try {
214
- await runWrangler("publish ./index.js");
215
- } catch (e) {
216
- error = e;
217
- }
218
-
219
- expect(std.out).toMatchInlineSnapshot(`""`);
220
- expect(std.err).toMatchInlineSnapshot(`
221
- "A [site] definition requires a \`bucket\` field with a path to the site's public directory.
222
-
223
- %s
224
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
225
- `);
226
- expect(std.warn).toMatchInlineSnapshot(`
227
- "Deprecation notice: The \`site.entry-point\` config field is no longer used.
228
- The entry-point should be specified via the command line (e.g. \`wrangler publish path/to/script\`) or the \`build.upload.main\` config field.
229
- Please remove the \`site.entry-point\` field from the \`wrangler.toml\` file."
230
- `);
231
- expect(error).toMatchInlineSnapshot(
232
- `[AssertionError: A [site] definition requires a \`bucket\` field with a path to the site's public directory.]`
233
- );
234
- });
235
-
236
- it("should warn if there is a `site.entry-point` configuration", async () => {
237
- const assets = [
238
- { filePath: "assets/file-1.txt", content: "Content of file-1" },
239
- { filePath: "assets/file-2.txt", content: "Content of file-2" },
240
- ];
241
- const kvNamespace = {
242
- title: "__test-name-workers_sites_assets",
243
- id: "__test-name-workers_sites_assets-id",
244
- };
245
-
246
- writeWranglerToml({
247
- site: {
248
- "entry-point": "./index.js",
249
- bucket: "assets",
250
- },
251
- });
252
- writeWorkerSource();
253
- writeAssets(assets);
254
- mockUploadWorkerRequest();
255
- mockSubDomainRequest();
256
- mockListKVNamespacesRequest(kvNamespace);
257
- mockKeyListRequest(kvNamespace.id, []);
258
- mockUploadAssetsToKVRequest(kvNamespace.id, assets);
259
-
260
- await runWrangler("publish ./index.js");
261
-
262
- expect(stripTimings(std.out)).toMatchInlineSnapshot(`
263
- "reading assets/file-1.txt...
264
- uploading as assets/file-1.2ca234f380.txt...
265
- reading assets/file-2.txt...
266
- uploading as assets/file-2.5938485188.txt...
267
- Uploaded
268
- test-name
269
- (TIMINGS)
270
- Deployed
271
- test-name
272
- (TIMINGS)
273
-
274
- test-name.test-sub-domain.workers.dev"
275
- `);
276
- expect(std.err).toMatchInlineSnapshot(`""`);
277
- expect(std.warn).toMatchInlineSnapshot(`
278
- "Deprecation notice: The \`site.entry-point\` config field is no longer used.
279
- The entry-point should be specified via the command line (e.g. \`wrangler publish path/to/script\`) or the \`build.upload.main\` config field.
280
- Please remove the \`site.entry-point\` field from the \`wrangler.toml\` file."
281
- `);
282
- });
283
-
284
- it("should error if there is no entry-point specified", async () => {
285
- writeWranglerToml();
286
- writeWorkerSource();
287
- mockUploadWorkerRequest();
288
- mockSubDomainRequest();
289
- let error: Error | undefined;
290
- try {
291
- await runWrangler("publish");
292
- } catch (e) {
293
- error = e;
294
- }
295
-
296
- expect(stripTimings(std.out)).toMatchInlineSnapshot(`""`);
297
- expect(std.err).toMatchInlineSnapshot(`
298
- "Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler publish path/to/script\`) or the \`build.upload.main\` config field.
299
-
300
- %s
301
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
302
- `);
303
- expect(error).toMatchInlineSnapshot(
304
- `[Error: Missing entry-point: The entry-point should be specified via the command line (e.g. \`wrangler publish path/to/script\`) or the \`build.upload.main\` config field.]`
305
- );
306
- });
307
- });
308
-
309
- describe("asset upload", () => {
310
- it("should upload all the files in the directory specified by `config.site.bucket`", async () => {
311
- const assets = [
312
- { filePath: "assets/file-1.txt", content: "Content of file-1" },
313
- { filePath: "assets/file-2.txt", content: "Content of file-2" },
314
- ];
315
- const kvNamespace = {
316
- title: "__test-name-workers_sites_assets",
317
- id: "__test-name-workers_sites_assets-id",
318
- };
319
- writeWranglerToml({
320
- build: { upload: { main: "./index.js" } },
321
- site: {
322
- bucket: "assets",
323
- },
324
- });
325
- writeWorkerSource();
326
- writeAssets(assets);
327
- mockUploadWorkerRequest();
328
- mockSubDomainRequest();
329
- mockListKVNamespacesRequest(kvNamespace);
330
- mockKeyListRequest(kvNamespace.id, []);
331
- mockUploadAssetsToKVRequest(kvNamespace.id, assets);
332
- await runWrangler("publish");
333
-
334
- expect(stripTimings(std.out)).toMatchInlineSnapshot(`
335
- "reading assets/file-1.txt...
336
- uploading as assets/file-1.2ca234f380.txt...
337
- reading assets/file-2.txt...
338
- uploading as assets/file-2.5938485188.txt...
339
- Uploaded
340
- test-name
341
- (TIMINGS)
342
- Deployed
343
- test-name
344
- (TIMINGS)
345
-
346
- test-name.test-sub-domain.workers.dev"
347
- `);
348
- expect(std.err).toMatchInlineSnapshot(`""`);
349
- });
350
-
351
- it("when using a service worker type, it should inline an asset manifest, and bind to a namespace", async () => {
352
- const assets = [
353
- { filePath: "assets/file-1.txt", content: "Content of file-1" },
354
- { filePath: "assets/file-2.txt", content: "Content of file-2" },
355
- ];
356
- const kvNamespace = {
357
- title: "__test-name-workers_sites_assets",
358
- id: "__test-name-workers_sites_assets-id",
359
- };
360
- writeWranglerToml({
361
- build: { upload: { main: "./index.js" } },
362
- site: {
363
- bucket: "assets",
364
- },
365
- });
366
- writeWorkerSource({ type: "sw" });
367
- writeAssets(assets);
368
- mockUploadWorkerRequest({
369
- expectedType: "sw",
370
- expectedEntry: `const __STATIC_CONTENT_MANIFEST = {"file-1.txt":"assets/file-1.2ca234f380.txt","file-2.txt":"assets/file-2.5938485188.txt"};`,
371
- expectedBindings: [
372
- {
373
- name: "__STATIC_CONTENT",
374
- namespace_id: "__test-name-workers_sites_assets-id",
375
- type: "kv_namespace",
376
- },
377
- ],
378
- });
379
- mockSubDomainRequest();
380
- mockListKVNamespacesRequest(kvNamespace);
381
- mockKeyListRequest(kvNamespace.id, []);
382
- mockUploadAssetsToKVRequest(kvNamespace.id, assets);
383
-
384
- await runWrangler("publish");
385
-
386
- expect(stripTimings(std.out)).toMatchInlineSnapshot(`
387
- "reading assets/file-1.txt...
388
- uploading as assets/file-1.2ca234f380.txt...
389
- reading assets/file-2.txt...
390
- uploading as assets/file-2.5938485188.txt...
391
- Uploaded
392
- test-name
393
- (TIMINGS)
394
- Deployed
395
- test-name
396
- (TIMINGS)
397
-
398
- test-name.test-sub-domain.workers.dev"
399
- `);
400
- expect(stripTimings(std.err)).toMatchInlineSnapshot(`""`);
401
- });
402
-
403
- it("when using a module worker type, it should add an asset manifest module, and bind to a namespace", async () => {
404
- const assets = [
405
- { filePath: "assets/file-1.txt", content: "Content of file-1" },
406
- { filePath: "assets/file-2.txt", content: "Content of file-2" },
407
- ];
408
- const kvNamespace = {
409
- title: "__test-name-workers_sites_assets",
410
- id: "__test-name-workers_sites_assets-id",
411
- };
412
- writeWranglerToml({
413
- build: { upload: { main: "./index.js" } },
414
- site: {
415
- bucket: "assets",
416
- },
417
- });
418
- writeWorkerSource({ type: "esm" });
419
- writeAssets(assets);
420
- mockUploadWorkerRequest({
421
- expectedBindings: [
422
- {
423
- name: "__STATIC_CONTENT",
424
- namespace_id: "__test-name-workers_sites_assets-id",
425
- type: "kv_namespace",
426
- },
427
- ],
428
- expectedModules: {
429
- __STATIC_CONTENT_MANIFEST:
430
- '{"file-1.txt":"assets/file-1.2ca234f380.txt","file-2.txt":"assets/file-2.5938485188.txt"}',
431
- },
432
- });
433
- mockSubDomainRequest();
434
- mockListKVNamespacesRequest(kvNamespace);
435
- mockKeyListRequest(kvNamespace.id, []);
436
- mockUploadAssetsToKVRequest(kvNamespace.id, assets);
437
-
438
- await runWrangler("publish");
439
-
440
- expect(stripTimings(std.out)).toMatchInlineSnapshot(`
441
- "reading assets/file-1.txt...
442
- uploading as assets/file-1.2ca234f380.txt...
443
- reading assets/file-2.txt...
444
- uploading as assets/file-2.5938485188.txt...
445
- Uploaded
446
- test-name
447
- (TIMINGS)
448
- Deployed
449
- test-name
450
- (TIMINGS)
451
-
452
- test-name.test-sub-domain.workers.dev"
453
- `);
454
- expect(stripTimings(std.err)).toMatchInlineSnapshot(`""`);
455
- });
456
-
457
- it("should make environment specific kv namespace for assets", async () => {
458
- // This is the same test as the one before this, but with an env arg
459
- const assets = [
460
- { filePath: "assets/file-1.txt", content: "Content of file-1" },
461
- { filePath: "assets/file-2.txt", content: "Content of file-2" },
462
- ];
463
- const kvNamespace = {
464
- title: "__test-name-some-env-workers_sites_assets",
465
- id: "__test-name-some-env-workers_sites_assets-id",
466
- };
467
- writeWranglerToml({
468
- build: { upload: { main: "./index.js" } },
469
- site: {
470
- bucket: "assets",
471
- },
472
- });
473
- writeWorkerSource();
474
- writeAssets(assets);
475
- mockUploadWorkerRequest({
476
- env: "some-env",
477
- expectedBindings: [
478
- {
479
- name: "__STATIC_CONTENT",
480
- namespace_id: "__test-name-some-env-workers_sites_assets-id",
481
- type: "kv_namespace",
482
- },
483
- ],
484
- });
485
- mockSubDomainRequest();
486
- mockListKVNamespacesRequest(kvNamespace);
487
- mockKeyListRequest(kvNamespace.id, []);
488
- mockUploadAssetsToKVRequest(kvNamespace.id, assets);
489
- await runWrangler("publish --env some-env");
490
-
491
- expect(stripTimings(std.out)).toMatchInlineSnapshot(`
492
- "reading assets/file-1.txt...
493
- uploading as assets/file-1.2ca234f380.txt...
494
- reading assets/file-2.txt...
495
- uploading as assets/file-2.5938485188.txt...
496
- Uploaded
497
- test-name (some-env)
498
- (TIMINGS)
499
- Deployed
500
- test-name (some-env)
501
- (TIMINGS)
502
-
503
- some-env.test-name.test-sub-domain.workers.dev"
504
- `);
505
- expect(std.err).toMatchInlineSnapshot(`""`);
506
- });
507
-
508
- it("should only upload files that are not already in the KV namespace", async () => {
509
- const assets = [
510
- { filePath: "assets/file-1.txt", content: "Content of file-1" },
511
- { filePath: "assets/file-2.txt", content: "Content of file-2" },
512
- ];
513
- const kvNamespace = {
514
- title: "__test-name-workers_sites_assets",
515
- id: "__test-name-workers_sites_assets-id",
516
- };
517
- writeWranglerToml({
518
- build: { upload: { main: "./index.js" } },
519
- site: {
520
- bucket: "assets",
521
- },
522
- });
523
- writeWorkerSource();
524
- writeAssets(assets);
525
- mockUploadWorkerRequest();
526
- mockSubDomainRequest();
527
- mockListKVNamespacesRequest(kvNamespace);
528
- // Put file-1 in the KV namespace
529
- mockKeyListRequest(kvNamespace.id, ["assets/file-1.2ca234f380.txt"]);
530
- // Check we do not upload file-1
531
- mockUploadAssetsToKVRequest(
532
- kvNamespace.id,
533
- assets.filter((a) => a.filePath !== "assets/file-1.txt")
534
- );
535
- await runWrangler("publish");
536
-
537
- expect(stripTimings(std.out)).toMatchInlineSnapshot(`
538
- "reading assets/file-1.txt...
539
- skipping - already uploaded
540
- reading assets/file-2.txt...
541
- uploading as assets/file-2.5938485188.txt...
542
- Uploaded
543
- test-name
544
- (TIMINGS)
545
- Deployed
546
- test-name
547
- (TIMINGS)
548
-
549
- test-name.test-sub-domain.workers.dev"
550
- `);
551
- expect(std.err).toMatchInlineSnapshot(`""`);
552
- });
553
-
554
- it("should only upload files that match the `site-include` arg", async () => {
555
- const assets = [
556
- { filePath: "assets/file-1.txt", content: "Content of file-1" },
557
- { filePath: "assets/file-2.txt", content: "Content of file-2" },
558
- ];
559
- const kvNamespace = {
560
- title: "__test-name-workers_sites_assets",
561
- id: "__test-name-workers_sites_assets-id",
562
- };
563
- writeWranglerToml({
564
- build: { upload: { main: "./index.js" } },
565
- site: {
566
- bucket: "assets",
567
- },
568
- });
569
- writeWorkerSource();
570
- writeAssets(assets);
571
- mockUploadWorkerRequest();
572
- mockSubDomainRequest();
573
- mockListKVNamespacesRequest(kvNamespace);
574
- mockKeyListRequest(kvNamespace.id, []);
575
- // Check we only upload file-1
576
- mockUploadAssetsToKVRequest(
577
- kvNamespace.id,
578
- assets.filter((a) => a.filePath === "assets/file-1.txt")
579
- );
580
- await runWrangler("publish --site-include file-1.txt");
581
-
582
- expect(stripTimings(std.out)).toMatchInlineSnapshot(`
583
- "reading assets/file-1.txt...
584
- uploading as assets/file-1.2ca234f380.txt...
585
- Uploaded
586
- test-name
587
- (TIMINGS)
588
- Deployed
589
- test-name
590
- (TIMINGS)
591
-
592
- test-name.test-sub-domain.workers.dev"
593
- `);
594
- expect(std.err).toMatchInlineSnapshot(`""`);
595
- });
596
-
597
- it("should not upload files that match the `site-exclude` arg", async () => {
598
- const assets = [
599
- { filePath: "assets/file-1.txt", content: "Content of file-1" },
600
- { filePath: "assets/file-2.txt", content: "Content of file-2" },
601
- ];
602
- const kvNamespace = {
603
- title: "__test-name-workers_sites_assets",
604
- id: "__test-name-workers_sites_assets-id",
605
- };
606
- writeWranglerToml({
607
- build: { upload: { main: "./index.js" } },
608
- site: {
609
- bucket: "assets",
610
- },
611
- });
612
- writeWorkerSource();
613
- writeAssets(assets);
614
- mockUploadWorkerRequest();
615
- mockSubDomainRequest();
616
- mockListKVNamespacesRequest(kvNamespace);
617
- mockKeyListRequest(kvNamespace.id, []);
618
- // Check we only upload file-1
619
- mockUploadAssetsToKVRequest(
620
- kvNamespace.id,
621
- assets.filter((a) => a.filePath === "assets/file-1.txt")
622
- );
623
- await runWrangler("publish --site-exclude file-2.txt");
624
-
625
- expect(stripTimings(std.out)).toMatchInlineSnapshot(`
626
- "reading assets/file-1.txt...
627
- uploading as assets/file-1.2ca234f380.txt...
628
- Uploaded
629
- test-name
630
- (TIMINGS)
631
- Deployed
632
- test-name
633
- (TIMINGS)
634
-
635
- test-name.test-sub-domain.workers.dev"
636
- `);
637
- expect(std.err).toMatchInlineSnapshot(`""`);
638
- });
639
-
640
- it("should only upload files that match the `site.include` config", async () => {
641
- const assets = [
642
- { filePath: "assets/file-1.txt", content: "Content of file-1" },
643
- { filePath: "assets/file-2.txt", content: "Content of file-2" },
644
- ];
645
- const kvNamespace = {
646
- title: "__test-name-workers_sites_assets",
647
- id: "__test-name-workers_sites_assets-id",
648
- };
649
- writeWranglerToml({
650
- build: { upload: { main: "./index.js" } },
651
- site: {
652
- bucket: "assets",
653
- include: ["file-1.txt"],
654
- },
655
- });
656
- writeWorkerSource();
657
- writeAssets(assets);
658
- mockUploadWorkerRequest();
659
- mockSubDomainRequest();
660
- mockListKVNamespacesRequest(kvNamespace);
661
- mockKeyListRequest(kvNamespace.id, []);
662
- // Check we only upload file-1
663
- mockUploadAssetsToKVRequest(
664
- kvNamespace.id,
665
- assets.filter((a) => a.filePath === "assets/file-1.txt")
666
- );
667
- await runWrangler("publish");
668
-
669
- expect(stripTimings(std.out)).toMatchInlineSnapshot(`
670
- "reading assets/file-1.txt...
671
- uploading as assets/file-1.2ca234f380.txt...
672
- Uploaded
673
- test-name
674
- (TIMINGS)
675
- Deployed
676
- test-name
677
- (TIMINGS)
678
-
679
- test-name.test-sub-domain.workers.dev"
680
- `);
681
- expect(std.err).toMatchInlineSnapshot(`""`);
682
- });
683
-
684
- it("should not upload files that match the `site.exclude` config", async () => {
685
- const assets = [
686
- { filePath: "assets/file-1.txt", content: "Content of file-1" },
687
- { filePath: "assets/file-2.txt", content: "Content of file-2" },
688
- ];
689
- const kvNamespace = {
690
- title: "__test-name-workers_sites_assets",
691
- id: "__test-name-workers_sites_assets-id",
692
- };
693
- writeWranglerToml({
694
- build: { upload: { main: "./index.js" } },
695
- site: {
696
- bucket: "assets",
697
- exclude: ["file-2.txt"],
698
- },
699
- });
700
- writeWorkerSource();
701
- writeAssets(assets);
702
- mockUploadWorkerRequest();
703
- mockSubDomainRequest();
704
- mockListKVNamespacesRequest(kvNamespace);
705
- mockKeyListRequest(kvNamespace.id, []);
706
- // Check we only upload file-1
707
- mockUploadAssetsToKVRequest(
708
- kvNamespace.id,
709
- assets.filter((a) => a.filePath === "assets/file-1.txt")
710
- );
711
- await runWrangler("publish");
712
-
713
- expect(stripTimings(std.out)).toMatchInlineSnapshot(`
714
- "reading assets/file-1.txt...
715
- uploading as assets/file-1.2ca234f380.txt...
716
- Uploaded
717
- test-name
718
- (TIMINGS)
719
- Deployed
720
- test-name
721
- (TIMINGS)
722
-
723
- test-name.test-sub-domain.workers.dev"
724
- `);
725
- expect(std.err).toMatchInlineSnapshot(`""`);
726
- });
727
-
728
- it("should use `site-include` arg over `site.include` config", async () => {
729
- const assets = [
730
- { filePath: "assets/file-1.txt", content: "Content of file-1" },
731
- { filePath: "assets/file-2.txt", content: "Content of file-2" },
732
- ];
733
- const kvNamespace = {
734
- title: "__test-name-workers_sites_assets",
735
- id: "__test-name-workers_sites_assets-id",
736
- };
737
- writeWranglerToml({
738
- build: { upload: { main: "./index.js" } },
739
- site: {
740
- bucket: "assets",
741
- include: ["file-2.txt"],
742
- },
743
- });
744
- writeWorkerSource();
745
- writeAssets(assets);
746
- mockUploadWorkerRequest();
747
- mockSubDomainRequest();
748
- mockListKVNamespacesRequest(kvNamespace);
749
- mockKeyListRequest(kvNamespace.id, []);
750
- // Check we only upload file-1
751
- mockUploadAssetsToKVRequest(
752
- kvNamespace.id,
753
- assets.filter((a) => a.filePath === "assets/file-1.txt")
754
- );
755
- await runWrangler("publish --site-include file-1.txt");
756
-
757
- expect(stripTimings(std.out)).toMatchInlineSnapshot(`
758
- "reading assets/file-1.txt...
759
- uploading as assets/file-1.2ca234f380.txt...
760
- Uploaded
761
- test-name
762
- (TIMINGS)
763
- Deployed
764
- test-name
765
- (TIMINGS)
766
-
767
- test-name.test-sub-domain.workers.dev"
768
- `);
769
- expect(std.err).toMatchInlineSnapshot(`""`);
770
- });
771
-
772
- it("should use `site-exclude` arg over `site.exclude` config", async () => {
773
- const assets = [
774
- { filePath: "assets/file-1.txt", content: "Content of file-1" },
775
- { filePath: "assets/file-2.txt", content: "Content of file-2" },
776
- ];
777
- const kvNamespace = {
778
- title: "__test-name-workers_sites_assets",
779
- id: "__test-name-workers_sites_assets-id",
780
- };
781
- writeWranglerToml({
782
- build: { upload: { main: "./index.js" } },
783
- site: {
784
- bucket: "assets",
785
- exclude: ["assets/file-1.txt"],
786
- },
787
- });
788
- writeWorkerSource();
789
- writeAssets(assets);
790
- mockUploadWorkerRequest();
791
- mockSubDomainRequest();
792
- mockListKVNamespacesRequest(kvNamespace);
793
- mockKeyListRequest(kvNamespace.id, []);
794
- // Check we only upload file-1
795
- mockUploadAssetsToKVRequest(
796
- kvNamespace.id,
797
- assets.filter((a) => a.filePath.endsWith("assets/file-1.txt"))
798
- );
799
- await runWrangler("publish --site-exclude file-2.txt");
800
-
801
- expect(stripTimings(std.out)).toMatchInlineSnapshot(`
802
- "reading assets/file-1.txt...
803
- uploading as assets/file-1.2ca234f380.txt...
804
- Uploaded
805
- test-name
806
- (TIMINGS)
807
- Deployed
808
- test-name
809
- (TIMINGS)
810
-
811
- test-name.test-sub-domain.workers.dev"
812
- `);
813
- expect(std.err).toMatchInlineSnapshot(`""`);
814
- });
815
-
816
- it("should walk directories except node_modules", async () => {
817
- const assets = [
818
- {
819
- filePath: "assets/directory-1/file-1.txt",
820
- content: "Content of file-1",
821
- },
822
- {
823
- filePath: "assets/node_modules/file-2.txt",
824
- content: "Content of file-2",
825
- },
826
- ];
827
- const kvNamespace = {
828
- title: "__test-name-workers_sites_assets",
829
- id: "__test-name-workers_sites_assets-id",
830
- };
831
- writeWranglerToml({
832
- build: { upload: { main: "./index.js" } },
833
- site: {
834
- bucket: "assets",
835
- },
836
- });
837
- writeWorkerSource();
838
- writeAssets(assets);
839
- mockUploadWorkerRequest();
840
- mockSubDomainRequest();
841
- mockListKVNamespacesRequest(kvNamespace);
842
- mockKeyListRequest(kvNamespace.id, []);
843
- // Only expect file-1 to be uploaded
844
- mockUploadAssetsToKVRequest(kvNamespace.id, assets.slice(0, 1));
845
- await runWrangler("publish");
846
-
847
- expect(stripTimings(std.out)).toMatchInlineSnapshot(`
848
- "reading assets/directory-1/file-1.txt...
849
- uploading as assets/directory-1/file-1.2ca234f380.txt...
850
- Uploaded
851
- test-name
852
- (TIMINGS)
853
- Deployed
854
- test-name
855
- (TIMINGS)
856
-
857
- test-name.test-sub-domain.workers.dev"
858
- `);
859
- expect(std.err).toMatchInlineSnapshot(`""`);
860
- });
861
-
862
- it("should skip hidden files and directories except `.well-known`", async () => {
863
- const assets = [
864
- {
865
- filePath: "assets/.hidden-file.txt",
866
- content: "Content of hidden-file",
867
- },
868
- {
869
- filePath: "assets/.hidden/file-1.txt",
870
- content: "Content of file-1",
871
- },
872
- {
873
- filePath: "assets/.well-known/file-2.txt",
874
- content: "Content of file-2",
875
- },
876
- ];
877
- const kvNamespace = {
878
- title: "__test-name-workers_sites_assets",
879
- id: "__test-name-workers_sites_assets-id",
880
- };
881
- writeWranglerToml({
882
- build: { upload: { main: "./index.js" } },
883
- site: {
884
- bucket: "assets",
885
- },
886
- });
887
- writeWorkerSource();
888
- writeAssets(assets);
889
- mockUploadWorkerRequest();
890
- mockSubDomainRequest();
891
- mockListKVNamespacesRequest(kvNamespace);
892
- mockKeyListRequest(kvNamespace.id, []);
893
- // Only expect file-2 to be uploaded
894
- mockUploadAssetsToKVRequest(kvNamespace.id, assets.slice(2));
895
- await runWrangler("publish");
896
-
897
- expect(stripTimings(std.out)).toMatchInlineSnapshot(`
898
- "reading assets/.well-known/file-2.txt...
899
- uploading as assets/.well-known/file-2.5938485188.txt...
900
- Uploaded
901
- test-name
902
- (TIMINGS)
903
- Deployed
904
- test-name
905
- (TIMINGS)
906
-
907
- test-name.test-sub-domain.workers.dev"
908
- `);
909
- expect(std.err).toMatchInlineSnapshot(`""`);
910
- });
911
-
912
- it("should error if the asset is over 25Mb", async () => {
913
- const assets = [
914
- {
915
- filePath: "assets/large-file.txt",
916
- // This file is greater than 25MiB when base64 encoded but small enough to be uploaded.
917
- content: "X".repeat(25 * 1024 * 1024 * 0.8 + 1),
918
- },
919
- {
920
- filePath: "assets/too-large-file.txt",
921
- content: "X".repeat(25 * 1024 * 1024 + 1),
922
- },
923
- ];
924
- const kvNamespace = {
925
- title: "__test-name-workers_sites_assets",
926
- id: "__test-name-workers_sites_assets-id",
927
- };
928
- writeWranglerToml({
929
- build: { upload: { main: "./index.js" } },
930
- site: {
931
- bucket: "assets",
932
- exclude: ["assets/file-1.txt"],
933
- },
934
- });
935
- writeWorkerSource();
936
- writeAssets(assets);
937
- mockUploadWorkerRequest();
938
- mockSubDomainRequest();
939
- mockListKVNamespacesRequest(kvNamespace);
940
- mockKeyListRequest(kvNamespace.id, []);
941
-
942
- let error: Error | undefined;
943
- try {
944
- await runWrangler("publish");
945
- } catch (e) {
946
- error = e;
947
- }
948
- expect(std.out).toMatchInlineSnapshot(`
949
- "reading assets/large-file.txt...
950
- uploading as assets/large-file.0ea0637a45.txt..."
951
- `);
952
- expect(std.err).toMatchInlineSnapshot(`
953
- "File assets/too-large-file.txt is too big, it should be under 25 MiB. See https://developers.cloudflare.com/workers/platform/limits#kv-limits
954
-
955
- %s
956
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
957
- `);
958
- expect(error).toMatchInlineSnapshot(
959
- `[Error: File assets/too-large-file.txt is too big, it should be under 25 MiB. See https://developers.cloudflare.com/workers/platform/limits#kv-limits]`
960
- );
961
- });
962
-
963
- it("should error if the asset key is over 512 characters", async () => {
964
- const longFilePathAsset = {
965
- filePath: "assets/" + "folder/".repeat(100) + "file.txt",
966
- content: "content of file",
967
- };
968
- const kvNamespace = {
969
- title: "__test-name-workers_sites_assets",
970
- id: "__test-name-workers_sites_assets-id",
971
- };
972
- writeWranglerToml({
973
- build: { upload: { main: "./index.js" } },
974
- site: {
975
- bucket: "assets",
976
- },
977
- });
978
- writeWorkerSource();
979
- writeAssets([longFilePathAsset]);
980
- mockUploadWorkerRequest();
981
- mockSubDomainRequest();
982
- mockListKVNamespacesRequest(kvNamespace);
983
- mockKeyListRequest(kvNamespace.id, []);
984
-
985
- let error: Error | undefined;
986
- try {
987
- await runWrangler("publish");
988
- } catch (e) {
989
- error = e;
990
- }
991
-
992
- expect(std.out).toMatchInlineSnapshot(
993
- `"reading assets/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/file.txt..."`
994
- );
995
- expect(std.err).toMatchInlineSnapshot(`
996
- "The asset path key \\"assets/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/file.3da0d0cd12.txt\\" exceeds the maximum key size limit of 512. See https://developers.cloudflare.com/workers/platform/limits#kv-limits\\",
997
-
998
- %s
999
- If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new."
1000
- `);
1001
- expect(error).toMatchInlineSnapshot(
1002
- `[Error: The asset path key "assets/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/folder/file.3da0d0cd12.txt" exceeds the maximum key size limit of 512. See https://developers.cloudflare.com/workers/platform/limits#kv-limits",]`
1003
- );
1004
- });
1005
- });
1006
-
1007
- describe("custom builds", () => {
1008
- it("should run a custom build before publishing", async () => {
1009
- writeWranglerToml({
1010
- build: {
1011
- command: `echo "custom build" && echo "export default { fetch(){ return new Response(123) } }" > index.js`,
1012
- },
1013
- });
1014
-
1015
- mockUploadWorkerRequest({
1016
- expectedEntry: "return new Response(123)",
1017
- });
1018
- mockSubDomainRequest();
1019
-
1020
- await runWrangler("publish index.js");
1021
- expect(stripTimings(std.out)).toMatchInlineSnapshot(`
1022
- "running:
1023
- echo \\"custom build\\" && echo \\"export default { fetch(){ return new Response(123) } }\\" > index.js
1024
- Uploaded
1025
- test-name
1026
- (TIMINGS)
1027
- Deployed
1028
- test-name
1029
- (TIMINGS)
1030
-
1031
- test-name.test-sub-domain.workers.dev"
1032
- `);
1033
- expect(std.err).toMatchInlineSnapshot(`""`);
1034
- expect(std.warn).toMatchInlineSnapshot(`""`);
1035
- });
1036
- });
1037
- });
1038
-
1039
- /** Write a mock wrangler.toml file to disk. */
1040
- function writeWranglerToml(config: Omit<Config, "env"> = {}) {
1041
- // We Omit `env` from config because TOML.stringify() appears to
1042
- // have a weird type signature that appears to fail. We'll revisit this
1043
- // when we write tests for publishing environments
1044
- fs.writeFileSync(
1045
- "./wrangler.toml",
1046
- TOML.stringify({
1047
- compatibility_date: "2022-01-12",
1048
- name: "test-name",
1049
- ...config,
1050
- }),
1051
-
1052
- "utf-8"
1053
- );
1054
- }
1055
-
1056
- /** Write a mock Worker script to disk. */
1057
- function writeWorkerSource({
1058
- basePath = ".",
1059
- format = "js",
1060
- type = "esm",
1061
- }: {
1062
- basePath?: string;
1063
- format?: "js" | "ts" | "jsx" | "tsx" | "mjs";
1064
- type?: "esm" | "sw";
1065
- } = {}) {
1066
- if (basePath !== ".") {
1067
- fs.mkdirSync(basePath, { recursive: true });
1068
- }
1069
- fs.writeFileSync(
1070
- `${basePath}/index.${format}`,
1071
- type === "esm"
1072
- ? `import { foo } from "./another";
1073
- export default {
1074
- async fetch(request) {
1075
- return new Response('Hello' + foo);
1076
- },
1077
- };`
1078
- : `import { foo } from "./another";
1079
- addEventListener('fetch', event => {
1080
- event.respondWith(new Response('Hello' + foo));
1081
- })`
1082
- );
1083
- fs.writeFileSync(`${basePath}/another.${format}`, `export const foo = 100;`);
1084
- }
1085
-
1086
- /** Write mock assets to the file system so they can be uploaded. */
1087
- function writeAssets(assets: { filePath: string; content: string }[]) {
1088
- for (const asset of assets) {
1089
- fs.mkdirSync(path.dirname(asset.filePath), { recursive: true });
1090
- fs.writeFileSync(asset.filePath, asset.content);
1091
- }
1092
- }
1093
-
1094
- /** Create a mock handler for the request to upload a worker script. */
1095
- function mockUploadWorkerRequest({
1096
- available_on_subdomain = true,
1097
- expectedEntry,
1098
- expectedType = "esm",
1099
- expectedBindings,
1100
- expectedModules = {},
1101
- env = undefined,
1102
- }: {
1103
- available_on_subdomain?: boolean;
1104
- expectedEntry?: string;
1105
- expectedType?: "esm" | "sw";
1106
- expectedBindings?: unknown;
1107
- expectedModules?: Record<string, string>;
1108
- env?: string | undefined;
1109
- } = {}) {
1110
- setMockResponse(
1111
- env
1112
- ? `/accounts/:accountId/workers/services/:scriptName/environments/:envName`
1113
- : "/accounts/:accountId/workers/scripts/:scriptName",
1114
- "PUT",
1115
- async ([_url, accountId, scriptName], { body }, queryParams) => {
1116
- expect(accountId).toEqual("some-account-id");
1117
- expect(scriptName).toEqual("test-name");
1118
- expect(queryParams.get("available_on_subdomains")).toEqual("true");
1119
- const formBody = body as FormData;
1120
- if (expectedEntry !== undefined) {
1121
- expect(await (formBody.get("index.js") as File).text()).toMatch(
1122
- expectedEntry
1123
- );
1124
- }
1125
-
1126
- const metadata = JSON.parse(
1127
- formBody.get("metadata") as string
1128
- ) as WorkerMetadata;
1129
- if (expectedType === "esm") {
1130
- expect(metadata.main_module).toEqual("index.js");
1131
- } else {
1132
- expect(metadata.body_part).toEqual("index.js");
1133
- }
1134
- if (expectedBindings !== undefined) {
1135
- expect(metadata.bindings).toEqual(expectedBindings);
1136
- }
1137
- for (const [name, content] of Object.entries(expectedModules)) {
1138
- expect(await (formBody.get(name) as File).text()).toMatch(content);
1139
- }
1140
-
1141
- return { available_on_subdomain };
1142
- }
1143
- );
1144
- }
1145
-
1146
- /** Create a mock handler for the request to get the account's subdomain. */
1147
- function mockSubDomainRequest(subdomain = "test-sub-domain") {
1148
- setMockResponse("/accounts/:accountId/workers/subdomain", () => {
1149
- return { subdomain };
1150
- });
1151
- }
1152
-
1153
- /** Create a mock handler for the request to get a list of all KV namespaces. */
1154
- function mockListKVNamespacesRequest(...namespaces: KVNamespaceInfo[]) {
1155
- setMockResponse(
1156
- "/accounts/:accountId/storage/kv/namespaces",
1157
- "GET",
1158
- ([_url, accountId]) => {
1159
- expect(accountId).toEqual("some-account-id");
1160
- return namespaces;
1161
- }
1162
- );
1163
- }
1164
-
1165
- /** Create a mock handler for the request that tries to do a bulk upload of assets to a KV namespace. */
1166
- function mockUploadAssetsToKVRequest(
1167
- expectedNamespaceId: string,
1168
- assets: { filePath: string; content: string }[]
1169
- ) {
1170
- setMockResponse(
1171
- "/accounts/:accountId/storage/kv/namespaces/:namespaceId/bulk",
1172
- "PUT",
1173
- ([_url, accountId, namespaceId], { body }) => {
1174
- expect(accountId).toEqual("some-account-id");
1175
- expect(namespaceId).toEqual(expectedNamespaceId);
1176
- const uploads = JSON.parse(body as string);
1177
- expect(assets.length).toEqual(uploads.length);
1178
- for (let i = 0; i < uploads.length; i++) {
1179
- const asset = assets[i];
1180
- const upload = uploads[i];
1181
- // The asset key consists of:
1182
- // - the basename of the filepath
1183
- // - some hash value
1184
- // - the extension
1185
- const keyMatcher = new RegExp(
1186
- "^" +
1187
- asset.filePath
1188
- .replace(/(\.[^.]+)$/, ".[a-z0-9]+$1")
1189
- .replace(/\./g, "\\.")
1190
- );
1191
- expect(upload.key).toMatch(keyMatcher);
1192
- // The asset value is base64 encoded.
1193
- expect(upload.base64).toBe(true);
1194
- expect(Buffer.from(upload.value, "base64").toString()).toEqual(
1195
- asset.content
1196
- );
1197
- }
1198
- return null;
1199
- }
1200
- );
1201
- }
1202
-
1203
- /** Strip timing data out of the stdout, since this is not always deterministic. */
1204
- function stripTimings(stdout: string): string {
1205
- return stdout.replace(/\(\d+\.\d+ sec\)/g, "(TIMINGS)");
1206
- }