wrangler 2.0.12 → 2.0.16

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 (149) hide show
  1. package/README.md +7 -1
  2. package/bin/wrangler.js +111 -57
  3. package/miniflare-dist/index.mjs +9 -2
  4. package/package.json +156 -154
  5. package/src/__tests__/config-cache-without-cache-dir.test.ts +38 -0
  6. package/src/__tests__/config-cache.test.ts +30 -24
  7. package/src/__tests__/configuration.test.ts +3935 -3476
  8. package/src/__tests__/dev.test.tsx +1128 -979
  9. package/src/__tests__/guess-worker-format.test.ts +68 -68
  10. package/src/__tests__/helpers/cmd-shim.d.ts +6 -6
  11. package/src/__tests__/helpers/faye-websocket.d.ts +4 -4
  12. package/src/__tests__/helpers/mock-account-id.ts +24 -24
  13. package/src/__tests__/helpers/mock-bin.ts +20 -20
  14. package/src/__tests__/helpers/mock-cfetch.ts +92 -92
  15. package/src/__tests__/helpers/mock-console.ts +49 -39
  16. package/src/__tests__/helpers/mock-dialogs.ts +94 -71
  17. package/src/__tests__/helpers/mock-http-server.ts +30 -30
  18. package/src/__tests__/helpers/mock-istty.ts +65 -18
  19. package/src/__tests__/helpers/mock-kv.ts +26 -26
  20. package/src/__tests__/helpers/mock-oauth-flow.ts +223 -228
  21. package/src/__tests__/helpers/mock-process.ts +39 -0
  22. package/src/__tests__/helpers/mock-stdin.ts +82 -77
  23. package/src/__tests__/helpers/mock-web-socket.ts +21 -21
  24. package/src/__tests__/helpers/run-in-tmp.ts +27 -27
  25. package/src/__tests__/helpers/run-wrangler.ts +8 -8
  26. package/src/__tests__/helpers/write-worker-source.ts +16 -16
  27. package/src/__tests__/helpers/write-wrangler-toml.ts +9 -9
  28. package/src/__tests__/https-options.test.ts +104 -104
  29. package/src/__tests__/index.test.ts +239 -234
  30. package/src/__tests__/init.test.ts +1605 -1250
  31. package/src/__tests__/jest.setup.ts +63 -33
  32. package/src/__tests__/kv.test.ts +1128 -1011
  33. package/src/__tests__/logger.test.ts +100 -74
  34. package/src/__tests__/package-manager.test.ts +303 -303
  35. package/src/__tests__/pages.test.ts +1152 -652
  36. package/src/__tests__/parse.test.ts +252 -252
  37. package/src/__tests__/publish.test.ts +6371 -5622
  38. package/src/__tests__/pubsub.test.ts +367 -0
  39. package/src/__tests__/r2.test.ts +133 -133
  40. package/src/__tests__/route.test.ts +18 -18
  41. package/src/__tests__/secret.test.ts +382 -377
  42. package/src/__tests__/tail.test.ts +530 -530
  43. package/src/__tests__/user.test.ts +123 -111
  44. package/src/__tests__/whoami.test.tsx +198 -117
  45. package/src/__tests__/worker-namespace.test.ts +327 -0
  46. package/src/abort.d.ts +1 -1
  47. package/src/api/dev.ts +49 -0
  48. package/src/api/index.ts +1 -0
  49. package/src/bundle-reporter.tsx +29 -0
  50. package/src/bundle.ts +157 -149
  51. package/src/cfetch/index.ts +80 -80
  52. package/src/cfetch/internal.ts +90 -83
  53. package/src/cli.ts +21 -7
  54. package/src/config/config.ts +204 -195
  55. package/src/config/diagnostics.ts +61 -61
  56. package/src/config/environment.ts +390 -357
  57. package/src/config/index.ts +206 -193
  58. package/src/config/validation-helpers.ts +366 -366
  59. package/src/config/validation.ts +1573 -1376
  60. package/src/config-cache.ts +79 -41
  61. package/src/create-worker-preview.ts +206 -136
  62. package/src/create-worker-upload-form.ts +247 -238
  63. package/src/dev/dev-vars.ts +13 -13
  64. package/src/dev/dev.tsx +329 -307
  65. package/src/dev/local.tsx +304 -275
  66. package/src/dev/remote.tsx +366 -224
  67. package/src/dev/use-esbuild.ts +126 -91
  68. package/src/dev.tsx +538 -0
  69. package/src/dialogs.tsx +97 -97
  70. package/src/durable.ts +87 -87
  71. package/src/entry.ts +234 -228
  72. package/src/environment-variables.ts +23 -23
  73. package/src/errors.ts +6 -6
  74. package/src/generate.ts +33 -0
  75. package/src/git-client.ts +42 -0
  76. package/src/https-options.ts +79 -79
  77. package/src/index.tsx +1775 -2763
  78. package/src/init.ts +549 -0
  79. package/src/inspect.ts +593 -593
  80. package/src/intl-polyfill.d.ts +123 -123
  81. package/src/is-interactive.ts +12 -0
  82. package/src/kv.ts +277 -277
  83. package/src/logger.ts +46 -39
  84. package/src/miniflare-cli/enum-keys.ts +8 -8
  85. package/src/miniflare-cli/index.ts +42 -31
  86. package/src/miniflare-cli/request-context.ts +18 -18
  87. package/src/module-collection.ts +212 -212
  88. package/src/open-in-browser.ts +4 -6
  89. package/src/package-manager.ts +123 -123
  90. package/src/pages/build.tsx +202 -0
  91. package/src/pages/constants.ts +7 -0
  92. package/src/pages/deployments.tsx +101 -0
  93. package/src/pages/dev.tsx +964 -0
  94. package/src/pages/functions/buildPlugin.ts +105 -0
  95. package/src/pages/functions/buildWorker.ts +151 -0
  96. package/{pages → src/pages}/functions/filepath-routing.test.ts +113 -113
  97. package/src/pages/functions/filepath-routing.ts +189 -0
  98. package/src/pages/functions/identifiers.ts +78 -0
  99. package/src/pages/functions/routes.ts +151 -0
  100. package/src/pages/index.tsx +84 -0
  101. package/src/pages/projects.tsx +157 -0
  102. package/src/pages/publish.tsx +335 -0
  103. package/src/pages/types.ts +40 -0
  104. package/src/pages/upload.tsx +384 -0
  105. package/src/pages/utils.ts +12 -0
  106. package/src/parse.ts +202 -138
  107. package/src/paths.ts +6 -6
  108. package/src/preview.ts +31 -0
  109. package/src/proxy.ts +400 -402
  110. package/src/publish.ts +667 -621
  111. package/src/pubsub/index.ts +286 -0
  112. package/src/pubsub/pubsub-commands.tsx +577 -0
  113. package/src/r2.ts +19 -19
  114. package/src/selfsigned.d.ts +23 -23
  115. package/src/sites.tsx +271 -225
  116. package/src/tail/filters.ts +108 -108
  117. package/src/tail/index.ts +217 -217
  118. package/src/tail/printing.ts +45 -45
  119. package/src/update-check.ts +11 -11
  120. package/src/user/choose-account.tsx +60 -0
  121. package/src/user/env-vars.ts +46 -0
  122. package/src/user/generate-auth-url.ts +33 -0
  123. package/src/user/generate-random-state.ts +16 -0
  124. package/src/user/index.ts +3 -0
  125. package/src/user/user.tsx +1161 -0
  126. package/src/whoami.tsx +61 -42
  127. package/src/worker-namespace.ts +190 -0
  128. package/src/worker.ts +110 -100
  129. package/src/zones.ts +39 -36
  130. package/templates/checked-fetch.js +17 -0
  131. package/templates/new-worker-scheduled.js +3 -3
  132. package/templates/new-worker-scheduled.ts +15 -15
  133. package/templates/new-worker.js +3 -3
  134. package/templates/new-worker.ts +15 -15
  135. package/templates/no-op-worker.js +10 -0
  136. package/templates/pages-template-plugin.ts +155 -0
  137. package/templates/pages-template-worker.ts +161 -0
  138. package/templates/static-asset-facade.js +31 -31
  139. package/templates/tsconfig.json +95 -95
  140. package/wrangler-dist/cli.js +55383 -54138
  141. package/pages/functions/buildPlugin.ts +0 -105
  142. package/pages/functions/buildWorker.ts +0 -151
  143. package/pages/functions/filepath-routing.ts +0 -189
  144. package/pages/functions/identifiers.ts +0 -78
  145. package/pages/functions/routes.ts +0 -156
  146. package/pages/functions/template-plugin.ts +0 -147
  147. package/pages/functions/template-worker.ts +0 -143
  148. package/src/pages.tsx +0 -2093
  149. package/src/user.tsx +0 -1214
@@ -0,0 +1,384 @@
1
+ import { mkdir, readdir, readFile, stat, writeFile } from "node:fs/promises";
2
+ import {
3
+ basename,
4
+ dirname,
5
+ extname,
6
+ join,
7
+ relative,
8
+ resolve,
9
+ sep,
10
+ } from "node:path";
11
+ import { hash as blake3hash } from "blake3-wasm";
12
+ import { render, Text } from "ink";
13
+ import Spinner from "ink-spinner";
14
+ import { getType } from "mime";
15
+ import PQueue from "p-queue";
16
+ import prettyBytes from "pretty-bytes";
17
+ import React from "react";
18
+ import { fetchResult } from "../cfetch";
19
+ import { FatalError } from "../errors";
20
+ import { logger } from "../logger";
21
+ import {
22
+ BULK_UPLOAD_CONCURRENCY,
23
+ MAX_BUCKET_FILE_COUNT,
24
+ MAX_BUCKET_SIZE,
25
+ MAX_UPLOAD_ATTEMPTS,
26
+ } from "./constants";
27
+ import { pagesBetaWarning } from "./utils";
28
+ import type { UploadPayloadFile } from "./types";
29
+ import type { ArgumentsCamelCase, Argv } from "yargs";
30
+
31
+ type UploadArgs = {
32
+ directory: string;
33
+ "output-manifest-path"?: string;
34
+ };
35
+
36
+ export function Options(yargs: Argv): Argv<UploadArgs> {
37
+ return yargs
38
+ .positional("directory", {
39
+ type: "string",
40
+ demandOption: true,
41
+ description: "The directory of static files to upload",
42
+ })
43
+ .options({
44
+ "output-manifest-path": {
45
+ type: "string",
46
+ description: "The name of the project you want to deploy to",
47
+ },
48
+ })
49
+ .epilogue(pagesBetaWarning);
50
+ }
51
+
52
+ export const Handler = async ({
53
+ directory,
54
+ outputManifestPath,
55
+ }: ArgumentsCamelCase<UploadArgs>) => {
56
+ if (!directory) {
57
+ throw new FatalError("Must specify a directory.", 1);
58
+ }
59
+
60
+ if (!process.env.CF_PAGES_UPLOAD_JWT) {
61
+ throw new FatalError("No JWT given.", 1);
62
+ }
63
+
64
+ const manifest = await upload({
65
+ directory,
66
+ jwt: process.env.CF_PAGES_UPLOAD_JWT,
67
+ });
68
+
69
+ if (outputManifestPath) {
70
+ await mkdir(dirname(outputManifestPath), { recursive: true });
71
+ await writeFile(outputManifestPath, JSON.stringify(manifest));
72
+ }
73
+
74
+ logger.log(`✨ Upload complete!`);
75
+ };
76
+
77
+ export const upload = async (
78
+ args:
79
+ | {
80
+ directory: string;
81
+ jwt: string;
82
+ }
83
+ | { directory: string; accountId: string; projectName: string }
84
+ ) => {
85
+ async function fetchJwt(): Promise<string> {
86
+ if ("jwt" in args) {
87
+ return args.jwt;
88
+ } else {
89
+ return (
90
+ await fetchResult<{ jwt: string }>(
91
+ `/accounts/${args.accountId}/pages/projects/${args.projectName}/upload-token`
92
+ )
93
+ ).jwt;
94
+ }
95
+ }
96
+
97
+ type FileContainer = {
98
+ content: string;
99
+ contentType: string;
100
+ sizeInBytes: number;
101
+ hash: string;
102
+ };
103
+
104
+ const IGNORE_LIST = [
105
+ "_worker.js",
106
+ "_redirects",
107
+ "_headers",
108
+ ".DS_Store",
109
+ "node_modules",
110
+ ".git",
111
+ ];
112
+
113
+ const directory = resolve(args.directory);
114
+
115
+ const walk = async (
116
+ dir: string,
117
+ fileMap: Map<string, FileContainer> = new Map(),
118
+ startingDir: string = dir
119
+ ) => {
120
+ const files = await readdir(dir);
121
+
122
+ await Promise.all(
123
+ files.map(async (file) => {
124
+ const filepath = join(dir, file);
125
+ const filestat = await stat(filepath);
126
+
127
+ if (IGNORE_LIST.includes(file)) {
128
+ return;
129
+ }
130
+
131
+ if (filestat.isSymbolicLink()) {
132
+ return;
133
+ }
134
+
135
+ if (filestat.isDirectory()) {
136
+ fileMap = await walk(filepath, fileMap, startingDir);
137
+ } else {
138
+ const name = relative(startingDir, filepath).split(sep).join("/");
139
+
140
+ // TODO: Move this to later so we don't hold as much in memory
141
+ const fileContent = await readFile(filepath);
142
+
143
+ const base64Content = fileContent.toString("base64");
144
+ const extension = extname(basename(name)).substring(1);
145
+
146
+ if (filestat.size > 25 * 1024 * 1024) {
147
+ throw new FatalError(
148
+ `Error: Pages only supports files up to ${prettyBytes(
149
+ 25 * 1024 * 1024
150
+ )} in size\n${name} is ${prettyBytes(filestat.size)} in size`,
151
+ 1
152
+ );
153
+ }
154
+
155
+ fileMap.set(name, {
156
+ content: base64Content,
157
+ contentType: getType(name) || "application/octet-stream",
158
+ sizeInBytes: filestat.size,
159
+ hash: blake3hash(base64Content + extension)
160
+ .toString("hex")
161
+ .slice(0, 32),
162
+ });
163
+ }
164
+ })
165
+ );
166
+
167
+ return fileMap;
168
+ };
169
+
170
+ const fileMap = await walk(directory);
171
+
172
+ if (fileMap.size > 20000) {
173
+ throw new FatalError(
174
+ `Error: Pages only supports up to 20,000 files in a deployment. Ensure you have specified your build output directory correctly.`,
175
+ 1
176
+ );
177
+ }
178
+
179
+ const files = [...fileMap.values()];
180
+
181
+ let jwt = await fetchJwt();
182
+
183
+ const start = Date.now();
184
+
185
+ const missingHashes = await fetchResult<string[]>(
186
+ `/pages/assets/check-missing`,
187
+ {
188
+ method: "POST",
189
+ headers: {
190
+ "Content-Type": "application/json",
191
+ Authorization: `Bearer ${jwt}`,
192
+ },
193
+ body: JSON.stringify({
194
+ hashes: files.map(({ hash }) => hash),
195
+ }),
196
+ }
197
+ );
198
+
199
+ const sortedFiles = files
200
+ .filter((file) => missingHashes.includes(file.hash))
201
+ .sort((a, b) => b.sizeInBytes - a.sizeInBytes);
202
+
203
+ // Start with a few buckets so small projects still get
204
+ // the benefit of multiple upload streams
205
+ const buckets: {
206
+ files: FileContainer[];
207
+ remainingSize: number;
208
+ }[] = new Array(BULK_UPLOAD_CONCURRENCY).fill(null).map(() => ({
209
+ files: [],
210
+ remainingSize: MAX_BUCKET_SIZE,
211
+ }));
212
+
213
+ let bucketOffset = 0;
214
+ for (const file of sortedFiles) {
215
+ let inserted = false;
216
+
217
+ for (let i = 0; i < buckets.length; i++) {
218
+ // Start at a different bucket for each new file
219
+ const bucket = buckets[(i + bucketOffset) % buckets.length];
220
+ if (
221
+ bucket.remainingSize >= file.sizeInBytes &&
222
+ bucket.files.length < MAX_BUCKET_FILE_COUNT
223
+ ) {
224
+ bucket.files.push(file);
225
+ bucket.remainingSize -= file.sizeInBytes;
226
+ inserted = true;
227
+ break;
228
+ }
229
+ }
230
+
231
+ if (!inserted) {
232
+ buckets.push({
233
+ files: [file],
234
+ remainingSize: MAX_BUCKET_SIZE - file.sizeInBytes,
235
+ });
236
+ }
237
+ bucketOffset++;
238
+ }
239
+
240
+ let counter = fileMap.size - sortedFiles.length;
241
+ const { rerender, unmount } = render(
242
+ <Progress done={counter} total={fileMap.size} />
243
+ );
244
+
245
+ const queue = new PQueue({ concurrency: BULK_UPLOAD_CONCURRENCY });
246
+
247
+ for (const bucket of buckets) {
248
+ // Don't upload empty buckets (can happen for tiny projects)
249
+ if (bucket.files.length === 0) continue;
250
+
251
+ const payload: UploadPayloadFile[] = bucket.files.map((file) => ({
252
+ key: file.hash,
253
+ value: file.content,
254
+ metadata: {
255
+ contentType: file.contentType,
256
+ },
257
+ base64: true,
258
+ }));
259
+
260
+ let attempts = 0;
261
+ const doUpload = async (): Promise<void> => {
262
+ try {
263
+ return await fetchResult(`/pages/assets/upload`, {
264
+ method: "POST",
265
+ headers: {
266
+ "Content-Type": "application/json",
267
+ Authorization: `Bearer ${jwt}`,
268
+ },
269
+ body: JSON.stringify(payload),
270
+ });
271
+ } catch (e) {
272
+ if (attempts < MAX_UPLOAD_ATTEMPTS) {
273
+ // Linear backoff, 0 second first time, then 1 second etc.
274
+ await new Promise((resolvePromise) =>
275
+ setTimeout(resolvePromise, attempts++ * 1000)
276
+ );
277
+
278
+ if ((e as { code: number }).code === 8000013) {
279
+ // Looks like the JWT expired, fetch another one
280
+ jwt = await fetchJwt();
281
+ }
282
+ return doUpload();
283
+ } else {
284
+ throw e;
285
+ }
286
+ }
287
+ };
288
+
289
+ queue.add(() =>
290
+ doUpload().then(
291
+ () => {
292
+ counter += bucket.files.length;
293
+ rerender(<Progress done={counter} total={fileMap.size} />);
294
+ },
295
+ (error) => {
296
+ return Promise.reject(
297
+ new FatalError(
298
+ "Failed to upload files. Please try again.",
299
+ error.code || 1
300
+ )
301
+ );
302
+ }
303
+ )
304
+ );
305
+ }
306
+
307
+ await queue.onIdle();
308
+
309
+ unmount();
310
+
311
+ const uploadMs = Date.now() - start;
312
+
313
+ const skipped = fileMap.size - missingHashes.length;
314
+ const skippedMessage = skipped > 0 ? `(${skipped} already uploaded) ` : "";
315
+
316
+ logger.log(
317
+ `✨ Success! Uploaded ${
318
+ sortedFiles.length
319
+ } files ${skippedMessage}${formatTime(uploadMs)}\n`
320
+ );
321
+
322
+ const doUpsertHashes = async (): Promise<void> => {
323
+ try {
324
+ return await fetchResult(`/pages/assets/upsert-hashes`, {
325
+ method: "POST",
326
+ headers: {
327
+ "Content-Type": "application/json",
328
+ Authorization: `Bearer ${jwt}`,
329
+ },
330
+ body: JSON.stringify({
331
+ hashes: files.map(({ hash }) => hash),
332
+ }),
333
+ });
334
+ } catch (e) {
335
+ await new Promise((resolvePromise) => setTimeout(resolvePromise, 1000));
336
+
337
+ if ((e as { code: number }).code === 8000013) {
338
+ // Looks like the JWT expired, fetch another one
339
+ jwt = await fetchJwt();
340
+ }
341
+
342
+ return await fetchResult(`/pages/assets/upsert-hashes`, {
343
+ method: "POST",
344
+ headers: {
345
+ "Content-Type": "application/json",
346
+ Authorization: `Bearer ${jwt}`,
347
+ },
348
+ body: JSON.stringify({
349
+ hashes: files.map(({ hash }) => hash),
350
+ }),
351
+ });
352
+ }
353
+ };
354
+
355
+ try {
356
+ await doUpsertHashes();
357
+ } catch {
358
+ logger.warn(
359
+ "Failed to update file hashes. Every upload appeared to succeed for this deployment, but you might need to re-upload for future deployments. This shouldn't have any impact other than slowing the upload speed of your next deployment."
360
+ );
361
+ }
362
+
363
+ return Object.fromEntries(
364
+ [...fileMap.entries()].map(([fileName, file]) => [
365
+ `/${fileName}`,
366
+ file.hash,
367
+ ])
368
+ );
369
+ };
370
+
371
+ function formatTime(duration: number) {
372
+ return `(${(duration / 1000).toFixed(2)} sec)`;
373
+ }
374
+
375
+ function Progress({ done, total }: { done: number; total: number }) {
376
+ return (
377
+ <>
378
+ <Text>
379
+ <Spinner type="earth" />
380
+ {` Uploading... (${done}/${total})\n`}
381
+ </Text>
382
+ </>
383
+ );
384
+ }
@@ -0,0 +1,12 @@
1
+ import type { BuildResult } from "esbuild";
2
+
3
+ export const RUNNING_BUILDERS: BuildResult[] = [];
4
+
5
+ export const CLEANUP_CALLBACKS: (() => void)[] = [];
6
+ export const CLEANUP = () => {
7
+ CLEANUP_CALLBACKS.forEach((callback) => callback());
8
+ RUNNING_BUILDERS.forEach((builder) => builder.stop?.());
9
+ };
10
+
11
+ export const pagesBetaWarning =
12
+ "🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose";