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
package/src/pages.tsx DELETED
@@ -1,2093 +0,0 @@
1
- /* eslint-disable no-shadow */
2
-
3
- import { execSync, spawn } from "node:child_process";
4
- import { existsSync, lstatSync, readFileSync, writeFileSync } from "node:fs";
5
- import { readdir, readFile, stat } from "node:fs/promises";
6
- import { tmpdir } from "node:os";
7
- import { dirname, join, sep, extname, basename } from "node:path";
8
- import { cwd } from "node:process";
9
- import { URL } from "node:url";
10
- import { hash } from "blake3-wasm";
11
- import { watch } from "chokidar";
12
- import { render, Text } from "ink";
13
- import SelectInput from "ink-select-input";
14
- import Spinner from "ink-spinner";
15
- import Table from "ink-table";
16
- import { getType } from "mime";
17
- import PQueue from "p-queue";
18
- import prettyBytes from "pretty-bytes";
19
- import React from "react";
20
- import { format as timeagoFormat } from "timeago.js";
21
- import { File, FormData } from "undici";
22
- import { buildPlugin } from "../pages/functions/buildPlugin";
23
- import { buildWorker } from "../pages/functions/buildWorker";
24
- import { generateConfigFromFileTree } from "../pages/functions/filepath-routing";
25
- import { writeRoutesModule } from "../pages/functions/routes";
26
- import { fetchResult } from "./cfetch";
27
- import { getConfigCache, saveToConfigCache } from "./config-cache";
28
- import { prompt } from "./dialogs";
29
- import { FatalError } from "./errors";
30
- import { logger } from "./logger";
31
- import { getRequestContextCheckOptions } from "./miniflare-cli/request-context";
32
- import openInBrowser from "./open-in-browser";
33
- import { toUrlPath } from "./paths";
34
- import { requireAuth } from "./user";
35
- import type { Config } from "../pages/functions/routes";
36
- import type {
37
- Headers as MiniflareHeaders,
38
- Request as MiniflareRequest,
39
- fetch as miniflareFetch,
40
- } from "@miniflare/core";
41
- import type { BuildResult } from "esbuild";
42
- import type { MiniflareOptions } from "miniflare";
43
- import type { BuilderCallback, CommandModule } from "yargs";
44
-
45
- export type Project = {
46
- name: string;
47
- subdomain: string;
48
- domains: Array<string>;
49
- source?: {
50
- type: string;
51
- };
52
- latest_deployment?: {
53
- modified_on: string;
54
- };
55
- created_on: string;
56
- production_branch: string;
57
- };
58
-
59
- export type Deployment = {
60
- id: string;
61
- environment: string;
62
- deployment_trigger: {
63
- metadata: {
64
- commit_hash: string;
65
- branch: string;
66
- };
67
- };
68
- url: string;
69
- latest_stage: {
70
- status: string;
71
- ended_on: string;
72
- };
73
- project_name: string;
74
- };
75
-
76
- export type UploadPayloadFile = {
77
- key: string;
78
- value: string;
79
- metadata: { contentType: string };
80
- base64: boolean;
81
- };
82
-
83
- interface PagesConfigCache {
84
- account_id?: string;
85
- project_name?: string;
86
- }
87
-
88
- const PAGES_CONFIG_CACHE_FILENAME = "pages.json";
89
- const MAX_BUCKET_SIZE = 50 * 1024 * 1024;
90
- const MAX_BUCKET_FILE_COUNT = 5000;
91
- const BULK_UPLOAD_CONCURRENCY = 3;
92
- const MAX_UPLOAD_ATTEMPTS = 5;
93
-
94
- // Defer importing miniflare until we really need it. This takes ~0.5s
95
- // and also modifies some `stream/web` and `undici` prototypes, so we
96
- // don't want to do this if pages commands aren't being called.
97
-
98
- export const pagesBetaWarning =
99
- "🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose";
100
-
101
- const isInPagesCI = !!process.env.CF_PAGES;
102
-
103
- const CLEANUP_CALLBACKS: (() => void)[] = [];
104
- const CLEANUP = () => {
105
- CLEANUP_CALLBACKS.forEach((callback) => callback());
106
- RUNNING_BUILDERS.forEach((builder) => builder.stop?.());
107
- };
108
-
109
- process.on("SIGINT", () => {
110
- CLEANUP();
111
- process.exit();
112
- });
113
- process.on("SIGTERM", () => {
114
- CLEANUP();
115
- process.exit();
116
- });
117
-
118
- function isWindows() {
119
- return process.platform === "win32";
120
- }
121
-
122
- const SECONDS_TO_WAIT_FOR_PROXY = 5;
123
-
124
- async function sleep(ms: number) {
125
- await new Promise((resolve) => setTimeout(resolve, ms));
126
- }
127
-
128
- function getPids(pid: number) {
129
- const pids: number[] = [pid];
130
- let command: string, regExp: RegExp;
131
-
132
- if (isWindows()) {
133
- command = `wmic process where (ParentProcessId=${pid}) get ProcessId`;
134
- regExp = new RegExp(/(\d+)/);
135
- } else {
136
- command = `pgrep -P ${pid}`;
137
- regExp = new RegExp(/(\d+)/);
138
- }
139
-
140
- try {
141
- const newPids = (
142
- execSync(command)
143
- .toString()
144
- .split("\n")
145
- .map((line) => line.match(regExp))
146
- .filter((line) => line !== null) as RegExpExecArray[]
147
- ).map((match) => parseInt(match[1]));
148
-
149
- pids.push(...newPids.map(getPids).flat());
150
- } catch {}
151
-
152
- return pids;
153
- }
154
-
155
- function getPort(pid: number) {
156
- let command: string, regExp: RegExp;
157
-
158
- if (isWindows()) {
159
- command = "\\windows\\system32\\netstat.exe -nao";
160
- regExp = new RegExp(`TCP\\s+.*:(\\d+)\\s+.*:\\d+\\s+LISTENING\\s+${pid}`);
161
- } else {
162
- command = "lsof -nPi";
163
- regExp = new RegExp(`${pid}\\s+.*TCP\\s+.*:(\\d+)\\s+\\(LISTEN\\)`);
164
- }
165
-
166
- try {
167
- const matches = execSync(command)
168
- .toString()
169
- .split("\n")
170
- .map((line) => line.match(regExp))
171
- .filter((line) => line !== null) as RegExpExecArray[];
172
-
173
- const match = matches[0];
174
- if (match) return parseInt(match[1]);
175
- } catch (thrown) {
176
- logger.error(
177
- `Error scanning for ports of process with PID ${pid}: ${thrown}`
178
- );
179
- }
180
- }
181
-
182
- async function spawnProxyProcess({
183
- port,
184
- command,
185
- }: {
186
- port?: number;
187
- command: (string | number)[];
188
- }): Promise<void | number> {
189
- if (command.length === 0) {
190
- CLEANUP();
191
- throw new FatalError(
192
- "Must specify a directory of static assets to serve or a command to run.",
193
- 1
194
- );
195
- }
196
-
197
- logger.log(`Running ${command.join(" ")}...`);
198
- const proxy = spawn(
199
- command[0].toString(),
200
- command.slice(1).map((value) => value.toString()),
201
- {
202
- shell: isWindows(),
203
- env: {
204
- BROWSER: "none",
205
- ...process.env,
206
- },
207
- }
208
- );
209
- CLEANUP_CALLBACKS.push(() => {
210
- proxy.kill();
211
- });
212
-
213
- proxy.stdout.on("data", (data) => {
214
- logger.log(`[proxy]: ${data}`);
215
- });
216
-
217
- proxy.stderr.on("data", (data) => {
218
- logger.error(`[proxy]: ${data}`);
219
- });
220
-
221
- proxy.on("close", (code) => {
222
- logger.error(`Proxy exited with status ${code}.`);
223
- });
224
-
225
- // Wait for proxy process to start...
226
- while (!proxy.pid) {}
227
-
228
- if (port === undefined) {
229
- logger.log(
230
- `Sleeping ${SECONDS_TO_WAIT_FOR_PROXY} seconds to allow proxy process to start before attempting to automatically determine port...`
231
- );
232
- logger.log("To skip, specify the proxy port with --proxy.");
233
- await sleep(SECONDS_TO_WAIT_FOR_PROXY * 1000);
234
-
235
- port = getPids(proxy.pid)
236
- .map(getPort)
237
- .filter((port) => port !== undefined)[0];
238
-
239
- if (port === undefined) {
240
- CLEANUP();
241
- throw new FatalError(
242
- "Could not automatically determine proxy port. Please specify the proxy port with --proxy.",
243
- 1
244
- );
245
- } else {
246
- logger.log(`Automatically determined the proxy port to be ${port}.`);
247
- }
248
- }
249
-
250
- return port;
251
- }
252
-
253
- function escapeRegex(str: string) {
254
- return str.replace(/[-/\\^$*+?.()|[]{}]/g, "\\$&");
255
- }
256
-
257
- type Replacements = Record<string, string>;
258
-
259
- function replacer(str: string, replacements: Replacements) {
260
- for (const [replacement, value] of Object.entries(replacements)) {
261
- str = str.replace(`:${replacement}`, value);
262
- }
263
- return str;
264
- }
265
-
266
- function generateRulesMatcher<T>(
267
- rules?: Record<string, T>,
268
- replacer: (match: T, replacements: Replacements) => T = (match) => match
269
- ) {
270
- // TODO: How can you test cross-host rules?
271
- if (!rules) return () => [];
272
-
273
- const compiledRules = Object.entries(rules)
274
- .map(([rule, match]) => {
275
- const crossHost = rule.startsWith("https://");
276
-
277
- rule = rule.split("*").map(escapeRegex).join("(?<splat>.*)");
278
-
279
- const host_matches = rule.matchAll(
280
- /(?<=^https:\\\/\\\/[^/]*?):([^\\]+)(?=\\)/g
281
- );
282
- for (const match of host_matches) {
283
- rule = rule.split(match[0]).join(`(?<${match[1]}>[^/.]+)`);
284
- }
285
-
286
- const path_matches = rule.matchAll(/:(\w+)/g);
287
- for (const match of path_matches) {
288
- rule = rule.split(match[0]).join(`(?<${match[1]}>[^/]+)`);
289
- }
290
-
291
- rule = "^" + rule + "$";
292
-
293
- try {
294
- const regExp = new RegExp(rule);
295
- return [{ crossHost, regExp }, match];
296
- } catch {}
297
- })
298
- .filter((value) => value !== undefined) as [
299
- { crossHost: boolean; regExp: RegExp },
300
- T
301
- ][];
302
-
303
- return ({ request }: { request: MiniflareRequest }) => {
304
- const { pathname, host } = new URL(request.url);
305
-
306
- return compiledRules
307
- .map(([{ crossHost, regExp }, match]) => {
308
- const test = crossHost ? `https://${host}${pathname}` : pathname;
309
- const result = regExp.exec(test);
310
- if (result) {
311
- return replacer(match, result.groups || {});
312
- }
313
- })
314
- .filter((value) => value !== undefined) as T[];
315
- };
316
- }
317
-
318
- function generateHeadersMatcher(headersFile: string) {
319
- if (existsSync(headersFile)) {
320
- const contents = readFileSync(headersFile).toString();
321
-
322
- // TODO: Log errors
323
- const lines = contents
324
- .split("\n")
325
- .map((line) => line.trim())
326
- .filter((line) => !line.startsWith("#") && line !== "");
327
-
328
- const rules: Record<string, Record<string, string>> = {};
329
- let rule: { path: string; headers: Record<string, string> } | undefined =
330
- undefined;
331
-
332
- for (const line of lines) {
333
- if (/^([^\s]+:\/\/|^\/)/.test(line)) {
334
- if (rule && Object.keys(rule.headers).length > 0) {
335
- rules[rule.path] = rule.headers;
336
- }
337
-
338
- const path = validateURL(line);
339
- if (path) {
340
- rule = {
341
- path,
342
- headers: {},
343
- };
344
- continue;
345
- }
346
- }
347
-
348
- if (!line.includes(":")) continue;
349
-
350
- const [rawName, ...rawValue] = line.split(":");
351
- const name = rawName.trim().toLowerCase();
352
- const value = rawValue.join(":").trim();
353
-
354
- if (name === "") continue;
355
- if (!rule) continue;
356
-
357
- const existingValues = rule.headers[name];
358
- rule.headers[name] = existingValues
359
- ? `${existingValues}, ${value}`
360
- : value;
361
- }
362
-
363
- if (rule && Object.keys(rule.headers).length > 0) {
364
- rules[rule.path] = rule.headers;
365
- }
366
-
367
- const rulesMatcher = generateRulesMatcher(rules, (match, replacements) =>
368
- Object.fromEntries(
369
- Object.entries(match).map(([name, value]) => [
370
- name,
371
- replacer(value, replacements),
372
- ])
373
- )
374
- );
375
-
376
- return (request: MiniflareRequest) => {
377
- const matches = rulesMatcher({
378
- request,
379
- });
380
- if (matches) return matches;
381
- };
382
- } else {
383
- return () => undefined;
384
- }
385
- }
386
-
387
- function generateRedirectsMatcher(redirectsFile: string) {
388
- if (existsSync(redirectsFile)) {
389
- const contents = readFileSync(redirectsFile).toString();
390
-
391
- // TODO: Log errors
392
- const lines = contents
393
- .split("\n")
394
- .map((line) => line.trim())
395
- .filter((line) => !line.startsWith("#") && line !== "");
396
-
397
- const rules = Object.fromEntries(
398
- lines
399
- .map((line) => line.split(" "))
400
- .filter((tokens) => tokens.length === 2 || tokens.length === 3)
401
- .map((tokens) => {
402
- const from = validateURL(tokens[0], true, false, false);
403
- const to = validateURL(tokens[1], false, true, true);
404
- let status: number | undefined = parseInt(tokens[2]) || 302;
405
- status = [301, 302, 303, 307, 308].includes(status)
406
- ? status
407
- : undefined;
408
-
409
- return from && to && status ? [from, { to, status }] : undefined;
410
- })
411
- .filter((rule) => rule !== undefined) as [
412
- string,
413
- { to: string; status?: number }
414
- ][]
415
- );
416
-
417
- const rulesMatcher = generateRulesMatcher(
418
- rules,
419
- ({ status, to }, replacements) => ({
420
- status,
421
- to: replacer(to, replacements),
422
- })
423
- );
424
-
425
- return (request: MiniflareRequest) => {
426
- const match = rulesMatcher({
427
- request,
428
- })[0];
429
- if (match) return match;
430
- };
431
- } else {
432
- return () => undefined;
433
- }
434
- }
435
-
436
- function extractPathname(
437
- path = "/",
438
- includeSearch: boolean,
439
- includeHash: boolean
440
- ) {
441
- if (!path.startsWith("/")) path = `/${path}`;
442
- const url = new URL(`//${path}`, "relative://");
443
- return `${url.pathname}${includeSearch ? url.search : ""}${
444
- includeHash ? url.hash : ""
445
- }`;
446
- }
447
-
448
- function validateURL(
449
- token: string,
450
- onlyRelative = false,
451
- includeSearch = false,
452
- includeHash = false
453
- ) {
454
- const host = /^https:\/\/+(?<host>[^/]+)\/?(?<path>.*)/.exec(token);
455
- if (host && host.groups && host.groups.host) {
456
- if (onlyRelative) return;
457
-
458
- return `https://${host.groups.host}${extractPathname(
459
- host.groups.path,
460
- includeSearch,
461
- includeHash
462
- )}`;
463
- } else {
464
- if (!token.startsWith("/") && onlyRelative) token = `/${token}`;
465
-
466
- const path = /^\//.exec(token);
467
- if (path) {
468
- try {
469
- return extractPathname(token, includeSearch, includeHash);
470
- } catch {}
471
- }
472
- }
473
- return "";
474
- }
475
-
476
- function hasFileExtension(pathname: string) {
477
- return /\/.+\.[a-z0-9]+$/i.test(pathname);
478
- }
479
-
480
- async function generateAssetsFetch(
481
- directory: string
482
- ): Promise<typeof miniflareFetch> {
483
- // Defer importing miniflare until we really need it
484
- const { Headers, Request, Response } = await import("@miniflare/core");
485
-
486
- const headersFile = join(directory, "_headers");
487
- const redirectsFile = join(directory, "_redirects");
488
- const workerFile = join(directory, "_worker.js");
489
-
490
- const ignoredFiles = [headersFile, redirectsFile, workerFile];
491
-
492
- const assetExists = (path: string) => {
493
- path = join(directory, path);
494
- return (
495
- existsSync(path) &&
496
- lstatSync(path).isFile() &&
497
- !ignoredFiles.includes(path)
498
- );
499
- };
500
-
501
- const getAsset = (path: string) => {
502
- if (assetExists(path)) {
503
- return join(directory, path);
504
- }
505
- };
506
-
507
- let redirectsMatcher = generateRedirectsMatcher(redirectsFile);
508
- let headersMatcher = generateHeadersMatcher(headersFile);
509
-
510
- watch([headersFile, redirectsFile], {
511
- persistent: true,
512
- }).on("change", (path) => {
513
- switch (path) {
514
- case headersFile: {
515
- logger.log("_headers modified. Re-evaluating...");
516
- headersMatcher = generateHeadersMatcher(headersFile);
517
- break;
518
- }
519
- case redirectsFile: {
520
- logger.log("_redirects modified. Re-evaluating...");
521
- redirectsMatcher = generateRedirectsMatcher(redirectsFile);
522
- break;
523
- }
524
- }
525
- });
526
-
527
- const serveAsset = (file: string) => {
528
- return readFileSync(file);
529
- };
530
-
531
- const generateResponse = (request: MiniflareRequest) => {
532
- const url = new URL(request.url);
533
-
534
- const deconstructedResponse: {
535
- status: number;
536
- headers: MiniflareHeaders;
537
- body?: Buffer;
538
- } = {
539
- status: 200,
540
- headers: new Headers(),
541
- body: undefined,
542
- };
543
-
544
- const match = redirectsMatcher(request);
545
- if (match) {
546
- const { status, to } = match;
547
-
548
- let location = to;
549
- let search;
550
-
551
- if (to.startsWith("/")) {
552
- search = new URL(location, "http://fakehost").search;
553
- } else {
554
- search = new URL(location).search;
555
- }
556
-
557
- location = `${location}${search ? "" : url.search}`;
558
-
559
- if (status && [301, 302, 303, 307, 308].includes(status)) {
560
- deconstructedResponse.status = status;
561
- } else {
562
- deconstructedResponse.status = 302;
563
- }
564
-
565
- deconstructedResponse.headers.set("Location", location);
566
- return deconstructedResponse;
567
- }
568
-
569
- if (!request.method?.match(/^(get|head)$/i)) {
570
- deconstructedResponse.status = 405;
571
- return deconstructedResponse;
572
- }
573
-
574
- const notFound = () => {
575
- let cwd = url.pathname;
576
- while (cwd) {
577
- cwd = cwd.slice(0, cwd.lastIndexOf("/"));
578
-
579
- if ((asset = getAsset(`${cwd}/404.html`))) {
580
- deconstructedResponse.status = 404;
581
- deconstructedResponse.body = serveAsset(asset);
582
- deconstructedResponse.headers.set(
583
- "Content-Type",
584
- getType(asset) || "application/octet-stream"
585
- );
586
- return deconstructedResponse;
587
- }
588
- }
589
-
590
- if ((asset = getAsset(`/index.html`))) {
591
- deconstructedResponse.body = serveAsset(asset);
592
- deconstructedResponse.headers.set(
593
- "Content-Type",
594
- getType(asset) || "application/octet-stream"
595
- );
596
- return deconstructedResponse;
597
- }
598
-
599
- deconstructedResponse.status = 404;
600
- return deconstructedResponse;
601
- };
602
-
603
- let asset;
604
-
605
- if (url.pathname.endsWith("/")) {
606
- if ((asset = getAsset(`${url.pathname}/index.html`))) {
607
- deconstructedResponse.body = serveAsset(asset);
608
- deconstructedResponse.headers.set(
609
- "Content-Type",
610
- getType(asset) || "application/octet-stream"
611
- );
612
- return deconstructedResponse;
613
- } else if (
614
- (asset = getAsset(`${url.pathname.replace(/\/$/, ".html")}`))
615
- ) {
616
- deconstructedResponse.status = 301;
617
- deconstructedResponse.headers.set(
618
- "Location",
619
- `${url.pathname.slice(0, -1)}${url.search}`
620
- );
621
- return deconstructedResponse;
622
- }
623
- }
624
-
625
- if (url.pathname.endsWith("/index")) {
626
- deconstructedResponse.status = 301;
627
- deconstructedResponse.headers.set(
628
- "Location",
629
- `${url.pathname.slice(0, -"index".length)}${url.search}`
630
- );
631
- return deconstructedResponse;
632
- }
633
-
634
- if ((asset = getAsset(url.pathname))) {
635
- if (url.pathname.endsWith(".html")) {
636
- const extensionlessPath = url.pathname.slice(0, -".html".length);
637
- if (getAsset(extensionlessPath) || extensionlessPath === "/") {
638
- deconstructedResponse.body = serveAsset(asset);
639
- deconstructedResponse.headers.set(
640
- "Content-Type",
641
- getType(asset) || "application/octet-stream"
642
- );
643
- return deconstructedResponse;
644
- } else {
645
- deconstructedResponse.status = 301;
646
- deconstructedResponse.headers.set(
647
- "Location",
648
- `${extensionlessPath}${url.search}`
649
- );
650
- return deconstructedResponse;
651
- }
652
- } else {
653
- deconstructedResponse.body = serveAsset(asset);
654
- deconstructedResponse.headers.set(
655
- "Content-Type",
656
- getType(asset) || "application/octet-stream"
657
- );
658
- return deconstructedResponse;
659
- }
660
- } else if (hasFileExtension(url.pathname)) {
661
- notFound();
662
- return deconstructedResponse;
663
- }
664
-
665
- if ((asset = getAsset(`${url.pathname}.html`))) {
666
- deconstructedResponse.body = serveAsset(asset);
667
- deconstructedResponse.headers.set(
668
- "Content-Type",
669
- getType(asset) || "application/octet-stream"
670
- );
671
- return deconstructedResponse;
672
- }
673
-
674
- if ((asset = getAsset(`${url.pathname}/index.html`))) {
675
- deconstructedResponse.status = 301;
676
- deconstructedResponse.headers.set(
677
- "Location",
678
- `${url.pathname}/${url.search}`
679
- );
680
- return deconstructedResponse;
681
- } else {
682
- notFound();
683
- return deconstructedResponse;
684
- }
685
- };
686
-
687
- const attachHeaders = (
688
- request: MiniflareRequest,
689
- deconstructedResponse: {
690
- status: number;
691
- headers: MiniflareHeaders;
692
- body?: Buffer;
693
- }
694
- ) => {
695
- const headers = deconstructedResponse.headers;
696
- const newHeaders = new Headers({});
697
- const matches = headersMatcher(request) || [];
698
-
699
- matches.forEach((match) => {
700
- Object.entries(match).forEach(([name, value]) => {
701
- newHeaders.append(name, `${value}`);
702
- });
703
- });
704
-
705
- const combinedHeaders = {
706
- ...Object.fromEntries(headers.entries()),
707
- ...Object.fromEntries(newHeaders.entries()),
708
- };
709
-
710
- deconstructedResponse.headers = new Headers({});
711
- Object.entries(combinedHeaders).forEach(([name, value]) => {
712
- if (value) deconstructedResponse.headers.set(name, value);
713
- });
714
- };
715
-
716
- return async (input, init) => {
717
- const request = new Request(input, init);
718
- const deconstructedResponse = generateResponse(request);
719
- attachHeaders(request, deconstructedResponse);
720
-
721
- const headers = new Headers();
722
-
723
- [...deconstructedResponse.headers.entries()].forEach(([name, value]) => {
724
- if (value) headers.set(name, value);
725
- });
726
-
727
- return new Response(deconstructedResponse.body, {
728
- headers,
729
- status: deconstructedResponse.status,
730
- });
731
- };
732
- }
733
-
734
- const RUNNING_BUILDERS: BuildResult[] = [];
735
-
736
- async function buildFunctions({
737
- outfile,
738
- outputConfigPath,
739
- functionsDirectory,
740
- minify = false,
741
- sourcemap = false,
742
- fallbackService = "ASSETS",
743
- watch = false,
744
- onEnd,
745
- plugin = false,
746
- buildOutputDirectory,
747
- nodeCompat,
748
- }: {
749
- outfile: string;
750
- outputConfigPath?: string;
751
- functionsDirectory: string;
752
- minify?: boolean;
753
- sourcemap?: boolean;
754
- fallbackService?: string;
755
- watch?: boolean;
756
- onEnd?: () => void;
757
- plugin?: boolean;
758
- buildOutputDirectory?: string;
759
- nodeCompat?: boolean;
760
- }) {
761
- RUNNING_BUILDERS.forEach(
762
- (runningBuilder) => runningBuilder.stop && runningBuilder.stop()
763
- );
764
-
765
- const routesModule = join(tmpdir(), `./functionsRoutes-${Math.random()}.mjs`);
766
- const baseURL = toUrlPath("/");
767
-
768
- const config: Config = await generateConfigFromFileTree({
769
- baseDir: functionsDirectory,
770
- baseURL,
771
- });
772
-
773
- if (outputConfigPath) {
774
- writeFileSync(
775
- outputConfigPath,
776
- JSON.stringify({ ...config, baseURL }, null, 2)
777
- );
778
- }
779
-
780
- await writeRoutesModule({
781
- config,
782
- srcDir: functionsDirectory,
783
- outfile: routesModule,
784
- });
785
-
786
- if (plugin) {
787
- RUNNING_BUILDERS.push(
788
- await buildPlugin({
789
- routesModule,
790
- outfile,
791
- minify,
792
- sourcemap,
793
- watch,
794
- nodeCompat,
795
- onEnd,
796
- })
797
- );
798
- } else {
799
- RUNNING_BUILDERS.push(
800
- await buildWorker({
801
- routesModule,
802
- outfile,
803
- minify,
804
- sourcemap,
805
- fallbackService,
806
- watch,
807
- onEnd,
808
- buildOutputDirectory,
809
- nodeCompat,
810
- })
811
- );
812
- }
813
- }
814
-
815
- interface CreateDeploymentArgs {
816
- directory: string;
817
- projectName?: string;
818
- branch?: string;
819
- commitHash?: string;
820
- commitMessage?: string;
821
- commitDirty?: boolean;
822
- }
823
-
824
- const upload = async ({
825
- directory,
826
- accountId,
827
- projectName,
828
- }: {
829
- directory: string;
830
- accountId: string;
831
- projectName: string;
832
- }) => {
833
- type FileContainer = {
834
- content: string;
835
- contentType: string;
836
- sizeInBytes: number;
837
- hash: string;
838
- };
839
-
840
- const IGNORE_LIST = [
841
- "_worker.js",
842
- "_redirects",
843
- "_headers",
844
- ".DS_Store",
845
- "node_modules",
846
- ".git",
847
- ];
848
-
849
- const walk = async (
850
- dir: string,
851
- fileMap: Map<string, FileContainer> = new Map(),
852
- depth = 0
853
- ) => {
854
- const files = await readdir(dir);
855
-
856
- await Promise.all(
857
- files.map(async (file) => {
858
- const filepath = join(dir, file);
859
- const filestat = await stat(filepath);
860
-
861
- if (IGNORE_LIST.includes(file)) {
862
- return;
863
- }
864
-
865
- if (filestat.isSymbolicLink()) {
866
- return;
867
- }
868
-
869
- if (filestat.isDirectory()) {
870
- fileMap = await walk(filepath, fileMap, depth + 1);
871
- } else {
872
- let name;
873
- if (depth) {
874
- name = filepath.split(sep).slice(1).join("/");
875
- } else {
876
- name = file;
877
- }
878
-
879
- // TODO: Move this to later so we don't hold as much in memory
880
- const fileContent = await readFile(filepath);
881
-
882
- const base64Content = fileContent.toString("base64");
883
- const extension = extname(basename(name)).substring(1);
884
-
885
- if (filestat.size > 25 * 1024 * 1024) {
886
- throw new Error(
887
- `Error: Pages only supports files up to ${prettyBytes(
888
- 25 * 1024 * 1024
889
- )} in size\n${name} is ${prettyBytes(filestat.size)} in size`
890
- );
891
- }
892
-
893
- fileMap.set(name, {
894
- content: base64Content,
895
- contentType: getType(name) || "application/octet-stream",
896
- sizeInBytes: filestat.size,
897
- hash: hash(base64Content + extension)
898
- .toString("hex")
899
- .slice(0, 32),
900
- });
901
- }
902
- })
903
- );
904
-
905
- return fileMap;
906
- };
907
-
908
- const fileMap = await walk(directory);
909
-
910
- if (fileMap.size > 20000) {
911
- throw new FatalError(
912
- `Error: Pages only supports up to 20,000 files in a deployment. Ensure you have specified your build output directory correctly.`,
913
- 1
914
- );
915
- }
916
-
917
- const files = [...fileMap.values()];
918
-
919
- async function fetchJwt(): Promise<string> {
920
- return (
921
- await fetchResult<{ jwt: string }>(
922
- `/accounts/${accountId}/pages/projects/${projectName}/upload-token`
923
- )
924
- ).jwt;
925
- }
926
-
927
- let jwt = await fetchJwt();
928
-
929
- const start = Date.now();
930
-
931
- const missingHashes = await fetchResult<string[]>(
932
- `/pages/assets/check-missing`,
933
- {
934
- method: "POST",
935
- headers: {
936
- "Content-Type": "application/json",
937
- Authorization: `Bearer ${jwt}`,
938
- },
939
- body: JSON.stringify({
940
- hashes: files.map(({ hash }) => hash),
941
- }),
942
- }
943
- );
944
-
945
- const sortedFiles = files
946
- .filter((file) => missingHashes.includes(file.hash))
947
- .sort((a, b) => b.sizeInBytes - a.sizeInBytes);
948
-
949
- // Start with a few buckets so small projects still get
950
- // the benefit of multiple upload streams
951
- const buckets: {
952
- files: FileContainer[];
953
- remainingSize: number;
954
- }[] = new Array(BULK_UPLOAD_CONCURRENCY).fill(null).map(() => ({
955
- files: [],
956
- remainingSize: MAX_BUCKET_SIZE,
957
- }));
958
-
959
- let bucketOffset = 0;
960
- for (const file of sortedFiles) {
961
- let inserted = false;
962
-
963
- for (let i = 0; i < buckets.length; i++) {
964
- // Start at a different bucket for each new file
965
- const bucket = buckets[(i + bucketOffset) % buckets.length];
966
- if (
967
- bucket.remainingSize >= file.sizeInBytes &&
968
- bucket.files.length < MAX_BUCKET_FILE_COUNT
969
- ) {
970
- bucket.files.push(file);
971
- bucket.remainingSize -= file.sizeInBytes;
972
- inserted = true;
973
- break;
974
- }
975
- }
976
-
977
- if (!inserted) {
978
- buckets.push({
979
- files: [file],
980
- remainingSize: MAX_BUCKET_SIZE - file.sizeInBytes,
981
- });
982
- }
983
- bucketOffset++;
984
- }
985
-
986
- let counter = fileMap.size - sortedFiles.length;
987
- const { rerender, unmount } = render(
988
- <Progress done={counter} total={fileMap.size} />
989
- );
990
-
991
- const queue = new PQueue({ concurrency: BULK_UPLOAD_CONCURRENCY });
992
-
993
- for (const bucket of buckets) {
994
- // Don't upload empty buckets (can happen for tiny projects)
995
- if (bucket.files.length === 0) continue;
996
-
997
- const payload: UploadPayloadFile[] = bucket.files.map((file) => ({
998
- key: file.hash,
999
- value: file.content,
1000
- metadata: {
1001
- contentType: file.contentType,
1002
- },
1003
- base64: true,
1004
- }));
1005
-
1006
- let attempts = 0;
1007
- const doUpload = async (): Promise<void> => {
1008
- try {
1009
- return await fetchResult(`/pages/assets/upload`, {
1010
- method: "POST",
1011
- headers: {
1012
- "Content-Type": "application/json",
1013
- Authorization: `Bearer ${jwt}`,
1014
- },
1015
- body: JSON.stringify(payload),
1016
- });
1017
- } catch (e) {
1018
- if (attempts < MAX_UPLOAD_ATTEMPTS) {
1019
- // Linear backoff, 0 second first time, then 1 second etc.
1020
- await new Promise((resolve) =>
1021
- setTimeout(resolve, attempts++ * 1000)
1022
- );
1023
-
1024
- if ((e as { code: number }).code === 8000013) {
1025
- // Looks like the JWT expired, fetch another one
1026
- jwt = await fetchJwt();
1027
- }
1028
- return doUpload();
1029
- } else {
1030
- throw e;
1031
- }
1032
- }
1033
- };
1034
-
1035
- queue.add(() =>
1036
- doUpload().then(
1037
- () => {
1038
- counter += bucket.files.length;
1039
- rerender(<Progress done={counter} total={fileMap.size} />);
1040
- },
1041
- (error) => {
1042
- return Promise.reject(
1043
- new FatalError(
1044
- "Failed to upload files. Please try again.",
1045
- error.code || 1
1046
- )
1047
- );
1048
- }
1049
- )
1050
- );
1051
- }
1052
-
1053
- await queue.onIdle();
1054
-
1055
- unmount();
1056
-
1057
- const uploadMs = Date.now() - start;
1058
-
1059
- const skipped = fileMap.size - missingHashes.length;
1060
- const skippedMessage = skipped > 0 ? `(${skipped} already uploaded) ` : "";
1061
-
1062
- logger.log(
1063
- `✨ Success! Uploaded ${
1064
- sortedFiles.length
1065
- } files ${skippedMessage}${formatTime(uploadMs)}\n`
1066
- );
1067
-
1068
- const doUpsertHashes = async (): Promise<void> => {
1069
- try {
1070
- return await fetchResult(`/pages/assets/upsert-hashes`, {
1071
- method: "POST",
1072
- headers: {
1073
- "Content-Type": "application/json",
1074
- Authorization: `Bearer ${jwt}`,
1075
- },
1076
- body: JSON.stringify({
1077
- hashes: files.map(({ hash }) => hash),
1078
- }),
1079
- });
1080
- } catch (e) {
1081
- await new Promise((resolve) => setTimeout(resolve, 1000));
1082
-
1083
- if ((e as { code: number }).code === 8000013) {
1084
- // Looks like the JWT expired, fetch another one
1085
- jwt = await fetchJwt();
1086
- }
1087
-
1088
- return await fetchResult(`/pages/assets/upsert-hashes`, {
1089
- method: "POST",
1090
- headers: {
1091
- "Content-Type": "application/json",
1092
- Authorization: `Bearer ${jwt}`,
1093
- },
1094
- body: JSON.stringify({
1095
- hashes: files.map(({ hash }) => hash),
1096
- }),
1097
- });
1098
- }
1099
- };
1100
-
1101
- try {
1102
- await doUpsertHashes();
1103
- } catch {
1104
- logger.warn(
1105
- "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."
1106
- );
1107
- }
1108
-
1109
- return Object.fromEntries(
1110
- [...fileMap.entries()].map(([fileName, file]) => [
1111
- `/${fileName}`,
1112
- file.hash,
1113
- ])
1114
- );
1115
- };
1116
-
1117
- const createDeployment: CommandModule<
1118
- CreateDeploymentArgs,
1119
- CreateDeploymentArgs
1120
- > = {
1121
- describe: "🆙 Publish a directory of static assets as a Pages deployment",
1122
- builder: (yargs) => {
1123
- return yargs
1124
- .positional("directory", {
1125
- type: "string",
1126
- demandOption: true,
1127
- description: "The directory of static files to upload",
1128
- })
1129
- .options({
1130
- "project-name": {
1131
- type: "string",
1132
- description: "The name of the project you want to deploy to",
1133
- },
1134
- branch: {
1135
- type: "string",
1136
- description: "The name of the branch you want to deploy to",
1137
- },
1138
- "commit-hash": {
1139
- type: "string",
1140
- description: "The SHA to attach to this deployment",
1141
- },
1142
- "commit-message": {
1143
- type: "string",
1144
- description: "The commit message to attach to this deployment",
1145
- },
1146
- "commit-dirty": {
1147
- type: "boolean",
1148
- description:
1149
- "Whether or not the workspace should be considered dirty for this deployment",
1150
- },
1151
- })
1152
- .epilogue(pagesBetaWarning);
1153
- },
1154
- handler: async ({
1155
- directory,
1156
- projectName,
1157
- branch,
1158
- commitHash,
1159
- commitMessage,
1160
- commitDirty,
1161
- }) => {
1162
- if (!directory) {
1163
- throw new FatalError("Must specify a directory.", 1);
1164
- }
1165
-
1166
- const config = getConfigCache<PagesConfigCache>(
1167
- PAGES_CONFIG_CACHE_FILENAME
1168
- );
1169
- const accountId = await requireAuth(config);
1170
-
1171
- projectName ??= config.project_name;
1172
-
1173
- const isInteractive = process.stdin.isTTY;
1174
- if (!projectName && isInteractive) {
1175
- const projects = (await listProjects({ accountId })).filter(
1176
- (project) => !project.source
1177
- );
1178
-
1179
- let existingOrNew: "existing" | "new" = "new";
1180
-
1181
- if (projects.length > 0) {
1182
- existingOrNew = await new Promise<"new" | "existing">((resolve) => {
1183
- const { unmount } = render(
1184
- <>
1185
- <Text>
1186
- No project selected. Would you like to create one or use an
1187
- existing project?
1188
- </Text>
1189
- <SelectInput
1190
- items={[
1191
- {
1192
- key: "new",
1193
- label: "Create a new project",
1194
- value: "new",
1195
- },
1196
- {
1197
- key: "existing",
1198
- label: "Use an existing project",
1199
- value: "existing",
1200
- },
1201
- ]}
1202
- onSelect={async (selected) => {
1203
- resolve(selected.value as "new" | "existing");
1204
- unmount();
1205
- }}
1206
- />
1207
- </>
1208
- );
1209
- });
1210
- }
1211
-
1212
- switch (existingOrNew) {
1213
- case "existing": {
1214
- projectName = await new Promise((resolve) => {
1215
- const { unmount } = render(
1216
- <>
1217
- <Text>Select a project:</Text>
1218
- <SelectInput
1219
- items={projects.map((project) => ({
1220
- key: project.name,
1221
- label: project.name,
1222
- value: project,
1223
- }))}
1224
- onSelect={async (selected) => {
1225
- resolve(selected.value.name);
1226
- unmount();
1227
- }}
1228
- />
1229
- </>
1230
- );
1231
- });
1232
- break;
1233
- }
1234
- case "new": {
1235
- projectName = await prompt("Enter the name of your new project:");
1236
-
1237
- if (!projectName) {
1238
- throw new FatalError("Must specify a project name.", 1);
1239
- }
1240
-
1241
- let isGitDir = true;
1242
- try {
1243
- execSync(`git rev-parse --is-inside-work-tree`, {
1244
- stdio: "ignore",
1245
- });
1246
- } catch (err) {
1247
- isGitDir = false;
1248
- }
1249
-
1250
- const productionBranch = await prompt(
1251
- "Enter the production branch name:",
1252
- "text",
1253
- isGitDir
1254
- ? execSync(`git rev-parse --abbrev-ref HEAD`).toString().trim()
1255
- : "production"
1256
- );
1257
-
1258
- if (!productionBranch) {
1259
- throw new FatalError("Must specify a production branch.", 1);
1260
- }
1261
-
1262
- await fetchResult<Project>(`/accounts/${accountId}/pages/projects`, {
1263
- method: "POST",
1264
- body: JSON.stringify({
1265
- name: projectName,
1266
- production_branch: productionBranch,
1267
- }),
1268
- });
1269
-
1270
- saveToConfigCache<PagesConfigCache>(PAGES_CONFIG_CACHE_FILENAME, {
1271
- account_id: accountId,
1272
- project_name: projectName,
1273
- });
1274
-
1275
- logger.log(`✨ Successfully created the '${projectName}' project.`);
1276
- break;
1277
- }
1278
- }
1279
- }
1280
-
1281
- if (!projectName) {
1282
- throw new FatalError("Must specify a project name.", 1);
1283
- }
1284
-
1285
- // We infer git info by default is not passed in
1286
-
1287
- let isGitDir = true;
1288
- try {
1289
- execSync(`git rev-parse --is-inside-work-tree`, {
1290
- stdio: "ignore",
1291
- });
1292
- } catch (err) {
1293
- isGitDir = false;
1294
- }
1295
-
1296
- let isGitDirty = false;
1297
-
1298
- if (isGitDir) {
1299
- try {
1300
- isGitDirty = Boolean(
1301
- execSync(`git status --porcelain`).toString().length
1302
- );
1303
-
1304
- if (!branch) {
1305
- branch = execSync(`git rev-parse --abbrev-ref HEAD`)
1306
- .toString()
1307
- .trim();
1308
- }
1309
-
1310
- if (!commitHash) {
1311
- commitHash = execSync(`git rev-parse HEAD`).toString().trim();
1312
- }
1313
-
1314
- if (!commitMessage) {
1315
- commitMessage = execSync(`git show -s --format=%B ${commitHash}`)
1316
- .toString()
1317
- .trim();
1318
- }
1319
- } catch (err) {}
1320
-
1321
- if (isGitDirty && !commitDirty) {
1322
- logger.warn(
1323
- `Warning: Your working directory is a git repo and has uncommitted changes\nTo silence this warning, pass in --commit-dirty=true`
1324
- );
1325
- }
1326
-
1327
- if (commitDirty === undefined) {
1328
- commitDirty = isGitDirty;
1329
- }
1330
- }
1331
-
1332
- let builtFunctions: string | undefined = undefined;
1333
- const functionsDirectory = join(cwd(), "functions");
1334
- if (existsSync(functionsDirectory)) {
1335
- const outfile = join(tmpdir(), `./functionsWorker-${Math.random()}.js`);
1336
-
1337
- await new Promise((resolve) =>
1338
- buildFunctions({
1339
- outfile,
1340
- functionsDirectory,
1341
- onEnd: () => resolve(null),
1342
- buildOutputDirectory: dirname(outfile),
1343
- })
1344
- );
1345
-
1346
- builtFunctions = readFileSync(outfile, "utf-8");
1347
- }
1348
-
1349
- const manifest = await upload({ directory, accountId, projectName });
1350
-
1351
- const formData = new FormData();
1352
-
1353
- formData.append("manifest", JSON.stringify(manifest));
1354
-
1355
- if (branch) {
1356
- formData.append("branch", branch);
1357
- }
1358
-
1359
- if (commitMessage) {
1360
- formData.append("commit_message", commitMessage);
1361
- }
1362
-
1363
- if (commitHash) {
1364
- formData.append("commit_hash", commitHash);
1365
- }
1366
-
1367
- if (commitDirty !== undefined) {
1368
- formData.append("commit_dirty", commitDirty);
1369
- }
1370
-
1371
- let _headers: string | undefined,
1372
- _redirects: string | undefined,
1373
- _workerJS: string | undefined;
1374
-
1375
- try {
1376
- _headers = readFileSync(join(directory, "_headers"), "utf-8");
1377
- } catch {}
1378
-
1379
- try {
1380
- _redirects = readFileSync(join(directory, "_redirects"), "utf-8");
1381
- } catch {}
1382
-
1383
- try {
1384
- _workerJS = readFileSync(join(directory, "_worker.js"), "utf-8");
1385
- } catch {}
1386
-
1387
- if (_headers) {
1388
- formData.append("_headers", new File([_headers], "_headers"));
1389
- }
1390
-
1391
- if (_redirects) {
1392
- formData.append("_redirects", new File([_redirects], "_redirects"));
1393
- }
1394
-
1395
- if (builtFunctions) {
1396
- formData.append("_worker.js", new File([builtFunctions], "_worker.js"));
1397
- } else if (_workerJS) {
1398
- formData.append("_worker.js", new File([_workerJS], "_worker.js"));
1399
- }
1400
-
1401
- const deploymentResponse = await fetchResult<Deployment>(
1402
- `/accounts/${accountId}/pages/projects/${projectName}/deployments`,
1403
- {
1404
- method: "POST",
1405
- body: formData,
1406
- }
1407
- );
1408
-
1409
- saveToConfigCache<PagesConfigCache>(PAGES_CONFIG_CACHE_FILENAME, {
1410
- account_id: accountId,
1411
- project_name: projectName,
1412
- });
1413
-
1414
- logger.log(
1415
- `✨ Deployment complete! Take a peek over at ${deploymentResponse.url}`
1416
- );
1417
- },
1418
- };
1419
-
1420
- export const pages: BuilderCallback<unknown, unknown> = (yargs) => {
1421
- return yargs
1422
- .command(
1423
- "dev [directory] [-- command..]",
1424
- "🧑‍💻 Develop your full-stack Pages application locally",
1425
- (yargs) => {
1426
- return yargs
1427
- .positional("directory", {
1428
- type: "string",
1429
- demandOption: undefined,
1430
- description: "The directory of static assets to serve",
1431
- })
1432
- .positional("command", {
1433
- type: "string",
1434
- demandOption: undefined,
1435
- description: "The proxy command to run",
1436
- })
1437
- .options({
1438
- local: {
1439
- type: "boolean",
1440
- default: true,
1441
- description: "Run on my machine",
1442
- },
1443
- port: {
1444
- type: "number",
1445
- default: 8788,
1446
- description: "The port to listen on (serve from)",
1447
- },
1448
- proxy: {
1449
- type: "number",
1450
- description:
1451
- "The port to proxy (where the static assets are served)",
1452
- },
1453
- "script-path": {
1454
- type: "string",
1455
- default: "_worker.js",
1456
- description:
1457
- "The location of the single Worker script if not using functions",
1458
- },
1459
- binding: {
1460
- type: "array",
1461
- description: "Bind variable/secret (KEY=VALUE)",
1462
- alias: "b",
1463
- },
1464
- kv: {
1465
- type: "array",
1466
- description: "KV namespace to bind",
1467
- alias: "k",
1468
- },
1469
- do: {
1470
- type: "array",
1471
- description: "Durable Object to bind (NAME=CLASS)",
1472
- alias: "o",
1473
- },
1474
- "live-reload": {
1475
- type: "boolean",
1476
- default: false,
1477
- description: "Auto reload HTML pages when change is detected",
1478
- },
1479
- "node-compat": {
1480
- describe: "Enable node.js compatibility",
1481
- default: false,
1482
- type: "boolean",
1483
- hidden: true,
1484
- },
1485
- // TODO: Miniflare user options
1486
- })
1487
- .epilogue(pagesBetaWarning);
1488
- },
1489
- async ({
1490
- local,
1491
- directory,
1492
- port,
1493
- proxy: requestedProxyPort,
1494
- "script-path": singleWorkerScriptPath,
1495
- binding: bindings = [],
1496
- kv: kvs = [],
1497
- do: durableObjects = [],
1498
- "live-reload": liveReload,
1499
- "node-compat": nodeCompat,
1500
- _: [_pages, _dev, ...remaining],
1501
- }) => {
1502
- // Beta message for `wrangler pages <commands>` usage
1503
- logger.log(pagesBetaWarning);
1504
-
1505
- if (!local) {
1506
- logger.error("Only local mode is supported at the moment.");
1507
- return;
1508
- }
1509
-
1510
- const functionsDirectory = "./functions";
1511
- const usingFunctions = existsSync(functionsDirectory);
1512
-
1513
- const command = remaining as (string | number)[];
1514
-
1515
- let proxyPort: number | void;
1516
-
1517
- if (directory === undefined) {
1518
- proxyPort = await spawnProxyProcess({
1519
- port: requestedProxyPort,
1520
- command,
1521
- });
1522
- if (proxyPort === undefined) return undefined;
1523
- }
1524
-
1525
- let miniflareArgs: MiniflareOptions = {};
1526
-
1527
- let scriptReadyResolve: () => void;
1528
- const scriptReadyPromise = new Promise<void>(
1529
- (resolve) => (scriptReadyResolve = resolve)
1530
- );
1531
-
1532
- if (usingFunctions) {
1533
- const outfile = join(
1534
- tmpdir(),
1535
- `./functionsWorker-${Math.random()}.js`
1536
- );
1537
-
1538
- if (nodeCompat) {
1539
- console.warn(
1540
- "Enabling node.js compatibility mode for builtins and globals. This is experimental and has serious tradeoffs. Please see https://github.com/ionic-team/rollup-plugin-node-polyfills/ for more details."
1541
- );
1542
- }
1543
-
1544
- logger.log(`Compiling worker to "${outfile}"...`);
1545
-
1546
- try {
1547
- await buildFunctions({
1548
- outfile,
1549
- functionsDirectory,
1550
- sourcemap: true,
1551
- watch: true,
1552
- onEnd: () => scriptReadyResolve(),
1553
- buildOutputDirectory: directory,
1554
- nodeCompat,
1555
- });
1556
- } catch {}
1557
-
1558
- watch([functionsDirectory], {
1559
- persistent: true,
1560
- ignoreInitial: true,
1561
- }).on("all", async () => {
1562
- await buildFunctions({
1563
- outfile,
1564
- functionsDirectory,
1565
- sourcemap: true,
1566
- watch: true,
1567
- onEnd: () => scriptReadyResolve(),
1568
- buildOutputDirectory: directory,
1569
- nodeCompat,
1570
- });
1571
- });
1572
-
1573
- miniflareArgs = {
1574
- scriptPath: outfile,
1575
- };
1576
- } else {
1577
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
1578
- scriptReadyResolve!();
1579
-
1580
- const scriptPath =
1581
- directory !== undefined
1582
- ? join(directory, singleWorkerScriptPath)
1583
- : singleWorkerScriptPath;
1584
-
1585
- if (existsSync(scriptPath)) {
1586
- miniflareArgs = {
1587
- scriptPath,
1588
- };
1589
- } else {
1590
- logger.log("No functions. Shimming...");
1591
- miniflareArgs = {
1592
- // cfFetch sets the `cf` object that a function could expect
1593
- // If there are no functions, there's no reason to set this up (and not make that network call)
1594
- cfFetch: false,
1595
- // TODO: The fact that these request/response hacks are necessary is ridiculous.
1596
- // We need to eliminate them from env.ASSETS.fetch (not sure if just local or prod as well)
1597
- script: `
1598
- export default {
1599
- async fetch(request, env, context) {
1600
- const response = await env.ASSETS.fetch(request.url, request)
1601
- return new Response(response.body, response)
1602
- }
1603
- }`,
1604
- };
1605
- }
1606
- }
1607
-
1608
- // Defer importing miniflare until we really need it
1609
- const { Miniflare, Log, LogLevel } = await import("miniflare");
1610
- const { Response, fetch } = await import("@miniflare/core");
1611
-
1612
- // Wait for esbuild to finish building before starting Miniflare.
1613
- // This must be before the call to `new Miniflare`, as that will
1614
- // asynchronously start loading the script. `await startServer()`
1615
- // internally just waits for that promise to resolve.
1616
- await scriptReadyPromise;
1617
-
1618
- // `assetsFetch()` will only be called if there is `proxyPort` defined.
1619
- // We only define `proxyPort`, above, when there is no `directory` defined.
1620
- const assetsFetch =
1621
- directory !== undefined
1622
- ? await generateAssetsFetch(directory)
1623
- : invalidAssetsFetch;
1624
-
1625
- const requestContextCheckOptions =
1626
- await getRequestContextCheckOptions();
1627
-
1628
- const miniflare = new Miniflare({
1629
- port,
1630
- watch: true,
1631
- modules: true,
1632
-
1633
- log: new Log(LogLevel.ERROR, { prefix: "pages" }),
1634
- logUnhandledRejections: true,
1635
- sourceMap: true,
1636
-
1637
- kvNamespaces: kvs.map((kv) => kv.toString()),
1638
-
1639
- durableObjects: Object.fromEntries(
1640
- durableObjects.map((durableObject) =>
1641
- durableObject.toString().split("=")
1642
- )
1643
- ),
1644
-
1645
- // User bindings
1646
- bindings: {
1647
- ...Object.fromEntries(
1648
- bindings
1649
- .map((binding) => binding.toString().split("="))
1650
- .map(([key, ...values]) => [key, values.join("=")])
1651
- ),
1652
- },
1653
-
1654
- // env.ASSETS.fetch
1655
- serviceBindings: {
1656
- async ASSETS(request: MiniflareRequest) {
1657
- if (proxyPort) {
1658
- try {
1659
- const url = new URL(request.url);
1660
- url.host = `localhost:${proxyPort}`;
1661
- return await fetch(url, request);
1662
- } catch (thrown) {
1663
- logger.error(`Could not proxy request: ${thrown}`);
1664
-
1665
- // TODO: Pretty error page
1666
- return new Response(
1667
- `[wrangler] Could not proxy request: ${thrown}`,
1668
- { status: 502 }
1669
- );
1670
- }
1671
- } else {
1672
- try {
1673
- return await assetsFetch(request);
1674
- } catch (thrown) {
1675
- logger.error(`Could not serve static asset: ${thrown}`);
1676
-
1677
- // TODO: Pretty error page
1678
- return new Response(
1679
- `[wrangler] Could not serve static asset: ${thrown}`,
1680
- { status: 502 }
1681
- );
1682
- }
1683
- }
1684
- },
1685
- },
1686
-
1687
- kvPersist: true,
1688
- durableObjectsPersist: true,
1689
- cachePersist: true,
1690
- liveReload,
1691
-
1692
- ...requestContextCheckOptions,
1693
- ...miniflareArgs,
1694
- });
1695
-
1696
- try {
1697
- // `startServer` might throw if user code contains errors
1698
- const server = await miniflare.startServer();
1699
- logger.log(`Serving at http://localhost:${port}/`);
1700
-
1701
- if (process.env.BROWSER !== "none") {
1702
- await openInBrowser(`http://localhost:${port}/`);
1703
- }
1704
-
1705
- if (directory !== undefined && liveReload) {
1706
- watch([directory], {
1707
- persistent: true,
1708
- ignoreInitial: true,
1709
- }).on("all", async () => {
1710
- await miniflare.reload();
1711
- });
1712
- }
1713
-
1714
- CLEANUP_CALLBACKS.push(() => {
1715
- server.close();
1716
- miniflare.dispose().catch((err) => miniflare.log.error(err));
1717
- });
1718
- } catch (e) {
1719
- miniflare.log.error(e as Error);
1720
- CLEANUP();
1721
- throw new FatalError("Could not start Miniflare.", 1);
1722
- }
1723
- }
1724
- )
1725
- .command("functions", false, (yargs) =>
1726
- // we hide this command from help output because
1727
- // it's not meant to be used directly right now
1728
- yargs.command(
1729
- "build [directory]",
1730
- "Compile a folder of Cloudflare Pages Functions into a single Worker",
1731
- (yargs) =>
1732
- yargs
1733
- .positional("directory", {
1734
- type: "string",
1735
- default: "functions",
1736
- description: "The directory of Pages Functions",
1737
- })
1738
- .options({
1739
- outfile: {
1740
- type: "string",
1741
- default: "_worker.js",
1742
- description: "The location of the output Worker script",
1743
- },
1744
- "output-config-path": {
1745
- type: "string",
1746
- description: "The location for the output config file",
1747
- },
1748
- minify: {
1749
- type: "boolean",
1750
- default: false,
1751
- description: "Minify the output Worker script",
1752
- },
1753
- sourcemap: {
1754
- type: "boolean",
1755
- default: false,
1756
- description:
1757
- "Generate a sourcemap for the output Worker script",
1758
- },
1759
- "fallback-service": {
1760
- type: "string",
1761
- default: "ASSETS",
1762
- description:
1763
- "The service to fallback to at the end of the `next` chain. Setting to '' will fallback to the global `fetch`.",
1764
- },
1765
- watch: {
1766
- type: "boolean",
1767
- default: false,
1768
- description:
1769
- "Watch for changes to the functions and automatically rebuild the Worker script",
1770
- },
1771
- plugin: {
1772
- type: "boolean",
1773
- default: false,
1774
- description: "Build a plugin rather than a Worker script",
1775
- },
1776
- "build-output-directory": {
1777
- type: "string",
1778
- description: "The directory to output static assets to",
1779
- },
1780
- "node-compat": {
1781
- describe: "Enable node.js compatibility",
1782
- default: false,
1783
- type: "boolean",
1784
- hidden: true,
1785
- },
1786
- })
1787
- .epilogue(pagesBetaWarning),
1788
- async ({
1789
- directory,
1790
- outfile,
1791
- "output-config-path": outputConfigPath,
1792
- minify,
1793
- sourcemap,
1794
- fallbackService,
1795
- watch,
1796
- plugin,
1797
- "build-output-directory": buildOutputDirectory,
1798
- "node-compat": nodeCompat,
1799
- }) => {
1800
- if (!isInPagesCI) {
1801
- // Beta message for `wrangler pages <commands>` usage
1802
- logger.log(pagesBetaWarning);
1803
- }
1804
-
1805
- if (nodeCompat) {
1806
- console.warn(
1807
- "Enabling node.js compatibility mode for builtins and globals. This is experimental and has serious tradeoffs. Please see https://github.com/ionic-team/rollup-plugin-node-polyfills/ for more details."
1808
- );
1809
- }
1810
-
1811
- buildOutputDirectory ??= dirname(outfile);
1812
-
1813
- await buildFunctions({
1814
- outfile,
1815
- outputConfigPath,
1816
- functionsDirectory: directory,
1817
- minify,
1818
- sourcemap,
1819
- fallbackService,
1820
- watch,
1821
- plugin,
1822
- buildOutputDirectory,
1823
- nodeCompat,
1824
- });
1825
- }
1826
- )
1827
- )
1828
- .command("project", "⚡️ Interact with your Pages projects", (yargs) =>
1829
- yargs
1830
- .command(
1831
- "list",
1832
- "List your Cloudflare Pages projects",
1833
- (yargs) => yargs.epilogue(pagesBetaWarning),
1834
- async () => {
1835
- const config = getConfigCache<PagesConfigCache>(
1836
- PAGES_CONFIG_CACHE_FILENAME
1837
- );
1838
-
1839
- const accountId = await requireAuth(config);
1840
-
1841
- const projects: Array<Project> = await listProjects({ accountId });
1842
-
1843
- const data = projects.map((project) => {
1844
- return {
1845
- "Project Name": project.name,
1846
- "Project Domains": `${project.domains.join(", ")}`,
1847
- "Git Provider": project.source ? "Yes" : "No",
1848
- "Last Modified": project.latest_deployment
1849
- ? timeagoFormat(project.latest_deployment.modified_on)
1850
- : timeagoFormat(project.created_on),
1851
- };
1852
- });
1853
-
1854
- saveToConfigCache<PagesConfigCache>(PAGES_CONFIG_CACHE_FILENAME, {
1855
- account_id: accountId,
1856
- });
1857
-
1858
- render(<Table data={data}></Table>);
1859
- }
1860
- )
1861
- .command(
1862
- "create [project-name]",
1863
- "Create a new Cloudflare Pages project",
1864
- (yargs) =>
1865
- yargs
1866
- .positional("project-name", {
1867
- type: "string",
1868
- demandOption: true,
1869
- description: "The name of your Pages project",
1870
- })
1871
- .options({
1872
- "production-branch": {
1873
- type: "string",
1874
- description:
1875
- "The name of the production branch of your project",
1876
- },
1877
- })
1878
- .epilogue(pagesBetaWarning),
1879
- async ({ productionBranch, projectName }) => {
1880
- const config = getConfigCache<PagesConfigCache>(
1881
- PAGES_CONFIG_CACHE_FILENAME
1882
- );
1883
- const accountId = await requireAuth(config);
1884
-
1885
- const isInteractive = process.stdin.isTTY;
1886
- if (!projectName && isInteractive) {
1887
- projectName = await prompt("Enter the name of your new project:");
1888
- }
1889
-
1890
- if (!projectName) {
1891
- throw new FatalError("Must specify a project name.", 1);
1892
- }
1893
-
1894
- if (!productionBranch && isInteractive) {
1895
- let isGitDir = true;
1896
- try {
1897
- execSync(`git rev-parse --is-inside-work-tree`, {
1898
- stdio: "ignore",
1899
- });
1900
- } catch (err) {
1901
- isGitDir = false;
1902
- }
1903
-
1904
- productionBranch = await prompt(
1905
- "Enter the production branch name:",
1906
- "text",
1907
- isGitDir
1908
- ? execSync(`git rev-parse --abbrev-ref HEAD`)
1909
- .toString()
1910
- .trim()
1911
- : "production"
1912
- );
1913
- }
1914
-
1915
- if (!productionBranch) {
1916
- throw new FatalError("Must specify a production branch.", 1);
1917
- }
1918
-
1919
- const { subdomain } = await fetchResult<Project>(
1920
- `/accounts/${accountId}/pages/projects`,
1921
- {
1922
- method: "POST",
1923
- body: JSON.stringify({
1924
- name: projectName,
1925
- production_branch: productionBranch,
1926
- }),
1927
- }
1928
- );
1929
-
1930
- saveToConfigCache<PagesConfigCache>(PAGES_CONFIG_CACHE_FILENAME, {
1931
- account_id: accountId,
1932
- project_name: projectName,
1933
- });
1934
-
1935
- logger.log(
1936
- `✨ Successfully created the '${projectName}' project. It will be available at https://${subdomain}/ once you create your first deployment.`
1937
- );
1938
- logger.log(
1939
- `To deploy a folder of assets, run 'wrangler pages publish [directory]'.`
1940
- );
1941
- }
1942
- )
1943
- .epilogue(pagesBetaWarning)
1944
- )
1945
- .command(
1946
- "deployment",
1947
- "🚀 Interact with the deployments of a project",
1948
- (yargs) =>
1949
- yargs
1950
- .command(
1951
- "list",
1952
- "List deployments in your Cloudflare Pages project",
1953
- (yargs) =>
1954
- yargs
1955
- .options({
1956
- "project-name": {
1957
- type: "string",
1958
- description:
1959
- "The name of the project you would like to list deployments for",
1960
- },
1961
- })
1962
- .epilogue(pagesBetaWarning),
1963
- async ({ projectName }) => {
1964
- const config = getConfigCache<PagesConfigCache>(
1965
- PAGES_CONFIG_CACHE_FILENAME
1966
- );
1967
- const accountId = await requireAuth(config);
1968
-
1969
- projectName ??= config.project_name;
1970
-
1971
- const isInteractive = process.stdin.isTTY;
1972
- if (!projectName && isInteractive) {
1973
- const projects = await listProjects({ accountId });
1974
- projectName = await new Promise((resolve) => {
1975
- const { unmount } = render(
1976
- <>
1977
- <Text>Select a project:</Text>
1978
- <SelectInput
1979
- items={projects.map((project) => ({
1980
- key: project.name,
1981
- label: project.name,
1982
- value: project,
1983
- }))}
1984
- onSelect={async (selected) => {
1985
- resolve(selected.value.name);
1986
- unmount();
1987
- }}
1988
- />
1989
- </>
1990
- );
1991
- });
1992
- }
1993
-
1994
- if (!projectName) {
1995
- throw new FatalError("Must specify a project name.", 1);
1996
- }
1997
-
1998
- const deployments: Array<Deployment> = await fetchResult(
1999
- `/accounts/${accountId}/pages/projects/${projectName}/deployments`
2000
- );
2001
-
2002
- const titleCase = (word: string) =>
2003
- word.charAt(0).toUpperCase() + word.slice(1);
2004
-
2005
- const shortSha = (sha: string) => sha.slice(0, 7);
2006
-
2007
- const getStatus = (deployment: Deployment) => {
2008
- // Return a pretty time since timestamp if successful otherwise the status
2009
- if (deployment.latest_stage.status === `success`) {
2010
- return timeagoFormat(deployment.latest_stage.ended_on);
2011
- }
2012
- return titleCase(deployment.latest_stage.status);
2013
- };
2014
-
2015
- const data = deployments.map((deployment) => {
2016
- return {
2017
- Environment: titleCase(deployment.environment),
2018
- Branch: deployment.deployment_trigger.metadata.branch,
2019
- Source: shortSha(
2020
- deployment.deployment_trigger.metadata.commit_hash
2021
- ),
2022
- Deployment: deployment.url,
2023
- Status: getStatus(deployment),
2024
- // TODO: Use a url shortener
2025
- Build: `https://dash.cloudflare.com/${accountId}/pages/view/${deployment.project_name}/${deployment.id}`,
2026
- };
2027
- });
2028
-
2029
- saveToConfigCache<PagesConfigCache>(PAGES_CONFIG_CACHE_FILENAME, {
2030
- account_id: accountId,
2031
- });
2032
-
2033
- render(<Table data={data}></Table>);
2034
- }
2035
- )
2036
- .command({
2037
- command: "create [directory]",
2038
- ...createDeployment,
2039
- } as CommandModule)
2040
- .epilogue(pagesBetaWarning)
2041
- )
2042
- .command({
2043
- command: "publish [directory]",
2044
- ...createDeployment,
2045
- } as CommandModule);
2046
- };
2047
-
2048
- const invalidAssetsFetch: typeof miniflareFetch = () => {
2049
- throw new Error(
2050
- "Trying to fetch assets directly when there is no `directory` option specified, and not in `local` mode."
2051
- );
2052
- };
2053
-
2054
- const listProjects = async ({
2055
- accountId,
2056
- }: {
2057
- accountId: string;
2058
- }): Promise<Array<Project>> => {
2059
- const pageSize = 10;
2060
- let page = 1;
2061
- const results = [];
2062
- while (results.length % pageSize === 0) {
2063
- const json: Array<Project> = await fetchResult(
2064
- `/accounts/${accountId}/pages/projects`,
2065
- {},
2066
- new URLSearchParams({
2067
- per_page: pageSize.toString(),
2068
- page: page.toString(),
2069
- })
2070
- );
2071
- page++;
2072
- results.push(...json);
2073
- if (json.length < pageSize) {
2074
- break;
2075
- }
2076
- }
2077
- return results;
2078
- };
2079
-
2080
- function formatTime(duration: number) {
2081
- return `(${(duration / 1000).toFixed(2)} sec)`;
2082
- }
2083
-
2084
- function Progress({ done, total }: { done: number; total: number }) {
2085
- return (
2086
- <>
2087
- <Text>
2088
- <Spinner type="earth" />
2089
- {` Uploading... (${done}/${total})\n`}
2090
- </Text>
2091
- </>
2092
- );
2093
- }