wrangler 2.17.0 → 2.19.0

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.
package/src/sites.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import assert from "node:assert";
2
2
  import { readdir, readFile, stat } from "node:fs/promises";
3
3
  import * as path from "node:path";
4
+ import chalk from "chalk";
4
5
  import ignore from "ignore";
5
6
  import xxhash from "xxhash-wasm";
6
7
  import {
@@ -9,8 +10,10 @@ import {
9
10
  listKVNamespaces,
10
11
  putKVBulkKeyValue,
11
12
  deleteKVBulkKeyValue,
13
+ BATCH_KEY_MAX,
14
+ formatNumber,
12
15
  } from "./kv/helpers";
13
- import { logger } from "./logger";
16
+ import { logger, LOGGER_LEVELS } from "./logger";
14
17
  import type { Config } from "./config";
15
18
  import type { KeyValue } from "./kv/helpers";
16
19
  import type { XXHashAPI } from "xxhash-wasm";
@@ -92,6 +95,15 @@ async function createKVNamespaceIfNotAlreadyExisting(
92
95
  };
93
96
  }
94
97
 
98
+ const MAX_DIFF_LINES = 100;
99
+ const MAX_BUCKET_SIZE = 98 * 1000 * 1000;
100
+ const MAX_BUCKET_KEYS = BATCH_KEY_MAX;
101
+ const MAX_BATCH_OPERATIONS = 5;
102
+
103
+ function pluralise(count: number) {
104
+ return count === 1 ? "" : "s";
105
+ }
106
+
95
107
  /**
96
108
  * Upload the assets found within the `dirPath` directory to the sites assets KV namespace for
97
109
  * the worker given by `scriptName`.
@@ -116,13 +128,13 @@ export async function syncAssets(
116
128
  if (siteAssets === undefined) {
117
129
  return { manifest: undefined, namespace: undefined };
118
130
  }
119
-
120
131
  if (dryRun) {
121
132
  logger.log("(Note: doing a dry run, not uploading or deleting anything.)");
122
133
  return { manifest: undefined, namespace: undefined };
123
134
  }
124
135
  assert(accountId, "Missing accountId");
125
136
 
137
+ // Create assets namespace if it doesn't exist
126
138
  const title = `__${scriptName}-workers_sites_assets${
127
139
  preview ? "_preview" : ""
128
140
  }`;
@@ -131,55 +143,81 @@ export async function syncAssets(
131
143
  title,
132
144
  accountId
133
145
  );
134
-
135
- // let's get all the keys in this namespace
146
+ // Get all existing keys in asset namespace
147
+ logger.info("Fetching list of already uploaded assets...");
136
148
  const namespaceKeysResponse = await listKVNamespaceKeys(accountId, namespace);
137
149
  const namespaceKeys = new Set(namespaceKeysResponse.map((x) => x.name));
138
150
 
139
- const manifest: Record<string, string> = {};
140
-
141
- // A batch of uploads where each bucket has to be less than 98mb
142
- const uploadBuckets: KeyValue[][] = [];
143
- // The "live" bucket that we'll keep filling until it's just below 98mb
144
- let uploadBucket: KeyValue[] = [];
145
- // A size counter for the live bucket
146
- let uploadBucketSize = 0;
147
-
148
- const include = createPatternMatcher(siteAssets.includePatterns, false);
149
- const exclude = createPatternMatcher(siteAssets.excludePatterns, true);
150
- const hasher = await xxhash();
151
-
152
151
  const assetDirectory = path.join(
153
152
  siteAssets.baseDirectory,
154
153
  siteAssets.assetDirectory
155
154
  );
155
+ const include = createPatternMatcher(siteAssets.includePatterns, false);
156
+ const exclude = createPatternMatcher(siteAssets.excludePatterns, true);
157
+ const hasher = await xxhash();
158
+
159
+ // Find and validate all assets before we make any changes (can't store base64
160
+ // contents in memory for upload as users may have *lots* of files, and we
161
+ // don't want to OOM: https://github.com/cloudflare/workers-sdk/issues/2223)
162
+
163
+ const manifest: Record<string, string> = {};
164
+ type PathKey = [path: string, key: string];
165
+ // A batch of uploads where each bucket has to be less than 100 MiB and
166
+ // contain less than 10,000 keys (although we limit to 98 MB and 5000 keys)
167
+ const uploadBuckets: PathKey[][] = [];
168
+ // The "live" bucket we'll keep filling until it's just below the size limit
169
+ let uploadBucket: PathKey[] = [];
170
+ // Current size of the live bucket in bytes (just base64 encoded values)
171
+ let uploadBucketSize = 0;
172
+
173
+ let uploadCount = 0;
174
+ let skipCount = 0;
175
+
176
+ // Always log the first MAX_DIFF_LINES lines, then require the debug log level
177
+ let diffCount = 0;
178
+ function logDiff(line: string) {
179
+ const level = logger.loggerLevel;
180
+ if (LOGGER_LEVELS[level] >= LOGGER_LEVELS.debug) {
181
+ // If we're logging as debug level, we want *all* diff lines to be logged
182
+ // at debug level, not just the first MAX_DIFF_LINES
183
+ logger.debug(line);
184
+ } else if (diffCount < MAX_DIFF_LINES) {
185
+ // Otherwise, log the first MAX_DIFF_LINES diffs at info level...
186
+ logger.info(line);
187
+ } else if (diffCount === MAX_DIFF_LINES) {
188
+ // ...and warn when we start to truncate it
189
+ const msg =
190
+ " (truncating changed assets log, set `WRANGLER_LOG=debug` environment variable to see full diff)";
191
+ logger.info(chalk.dim(msg));
192
+ }
193
+ diffCount++;
194
+ }
195
+
196
+ logger.info("Building list of assets to upload...");
156
197
  for await (const absAssetFile of getFilesInFolder(assetDirectory)) {
157
198
  const assetFile = path.relative(assetDirectory, absAssetFile);
158
- if (!include(assetFile)) {
159
- continue;
160
- }
161
- if (exclude(assetFile)) {
162
- continue;
163
- }
199
+ if (!include(assetFile) || exclude(assetFile)) continue;
164
200
 
165
- logger.log(`Reading ${assetFile}...`);
166
201
  const content = await readFile(absAssetFile, "base64");
167
- await validateAssetSize(absAssetFile, assetFile);
168
- // while KV accepts files that are 25 MiB **before** b64 encoding
202
+ // While KV accepts files that are 25 MiB **before** b64 encoding
169
203
  // the overall bucket size must be below 100 MB **after** b64 encoding
170
- const assetSize = Buffer.from(content).length;
204
+ const assetSize = Buffer.byteLength(content);
205
+ await validateAssetSize(absAssetFile, assetFile);
171
206
  const assetKey = hashAsset(hasher, assetFile, content);
172
207
  validateAssetKey(assetKey);
173
208
 
174
- // now put each of the files into kv
175
209
  if (!namespaceKeys.has(assetKey)) {
176
- logger.log(`Uploading as ${assetKey}...`);
177
-
178
- // Check if adding this asset to the bucket would
179
- // push it over the 98 MiB limit KV bulk API limit
180
- if (uploadBucketSize + assetSize > 98 * 1000 * 1000) {
181
- // If so, move the current bucket into the batch,
182
- // and reset the counter/bucket
210
+ logDiff(
211
+ chalk.green(` + ${assetKey} (uploading new version of ${assetFile})`)
212
+ );
213
+
214
+ // Check if adding this asset to the bucket would push it over the KV
215
+ // bulk API limits
216
+ if (
217
+ uploadBucketSize + assetSize > MAX_BUCKET_SIZE ||
218
+ uploadBucket.length + 1 > MAX_BUCKET_KEYS
219
+ ) {
220
+ // If so, record the current bucket and reset it
183
221
  uploadBuckets.push(uploadBucket);
184
222
  uploadBucketSize = 0;
185
223
  uploadBucket = [];
@@ -187,13 +225,11 @@ export async function syncAssets(
187
225
 
188
226
  // Update the bucket and the size counter
189
227
  uploadBucketSize += assetSize;
190
- uploadBucket.push({
191
- key: assetKey,
192
- value: content,
193
- base64: true,
194
- });
228
+ uploadBucket.push([absAssetFile, assetKey]);
229
+ uploadCount++;
195
230
  } else {
196
- logger.log(`Skipping - already uploaded.`);
231
+ logDiff(chalk.dim(` = ${assetKey} (already uploaded ${assetFile})`));
232
+ skipCount++;
197
233
  }
198
234
 
199
235
  // Remove the key from the set so we know what we've already uploaded
@@ -203,23 +239,99 @@ export async function syncAssets(
203
239
  const manifestKey = urlSafe(path.relative(assetDirectory, absAssetFile));
204
240
  manifest[manifestKey] = assetKey;
205
241
  }
242
+ // Add the last (potentially only or empty) bucket to the batch
243
+ if (uploadBucket.length > 0) uploadBuckets.push(uploadBucket);
206
244
 
207
- // Add the last (potentially only) bucket to the batch
208
- uploadBuckets.push(uploadBucket);
209
-
210
- // keys now contains all the files we're deleting
211
245
  for (const key of namespaceKeys) {
212
- logger.log(`Deleting ${key} from the asset store...`);
246
+ logDiff(chalk.red(` - ${key} (removing as stale)`));
247
+ }
248
+
249
+ // Upload new assets, with 5 concurrent uploaders
250
+ if (uploadCount > 0) {
251
+ const s = pluralise(uploadCount);
252
+ logger.info(`Uploading ${formatNumber(uploadCount)} new asset${s}...`);
253
+ }
254
+ if (skipCount > 0) {
255
+ const s = pluralise(skipCount);
256
+ logger.info(
257
+ `Skipped uploading ${formatNumber(skipCount)} existing asset${s}.`
258
+ );
213
259
  }
260
+ let uploadedCount = 0;
261
+ const controller = new AbortController();
262
+ const uploaders = Array.from(Array(MAX_BATCH_OPERATIONS)).map(async () => {
263
+ while (!controller.signal.aborted) {
264
+ // Get the next bucket to upload. If there is none, stop this uploader.
265
+ // JavaScript is single(ish)-threaded, so we don't need to worry about
266
+ // parallel access here.
267
+ const nextBucket = uploadBuckets.shift();
268
+ if (nextBucket === undefined) break;
269
+
270
+ // Read all files in the bucket as base64
271
+ // TODO(perf): consider streaming the bulk upload body, rather than
272
+ // buffering all base64 contents then JSON-stringifying. This probably
273
+ // doesn't matter *too* much: we know buckets will be about 100MB, so
274
+ // with 5 uploaders, we could load about 500MB into memory (+ extra
275
+ // object keys/tags/copies/etc).
276
+ const bucket: KeyValue[] = [];
277
+ for (const [absAssetFile, assetKey] of nextBucket) {
278
+ bucket.push({
279
+ key: assetKey,
280
+ value: await readFile(absAssetFile, "base64"),
281
+ base64: true,
282
+ });
283
+ if (controller.signal.aborted) break;
284
+ }
214
285
 
215
- // upload each bucket in parallel
216
- const bucketsToPut = [];
217
- for (const bucket of uploadBuckets) {
218
- bucketsToPut.push(putKVBulkKeyValue(accountId, namespace, bucket));
286
+ // Upload the bucket to the KV namespace, suppressing logs, we do our own
287
+ try {
288
+ await putKVBulkKeyValue(
289
+ accountId,
290
+ namespace,
291
+ bucket,
292
+ /* quiet */ true,
293
+ controller.signal
294
+ );
295
+ } catch (e) {
296
+ // https://developer.mozilla.org/en-US/docs/Web/API/DOMException#error_names
297
+ // https://github.com/nodejs/undici/blob/a3efc9814447001a43a976f1c64adc41995df7e3/lib/core/errors.js#L89
298
+ if (
299
+ typeof e === "object" &&
300
+ e !== null &&
301
+ "name" in e &&
302
+ // @ts-expect-error `e.name` should be typed `unknown`, fixed in
303
+ // TypeScript 4.9
304
+ e.name === "AbortError"
305
+ ) {
306
+ break;
307
+ }
308
+ throw e;
309
+ }
310
+ uploadedCount += nextBucket.length;
311
+ const percent = Math.floor((100 * uploadedCount) / uploadCount);
312
+ logger.info(
313
+ `Uploaded ${percent}% [${formatNumber(
314
+ uploadedCount
315
+ )} out of ${formatNumber(uploadCount)}]`
316
+ );
317
+ }
318
+ });
319
+ try {
320
+ // Wait for all uploaders to complete, or one to fail
321
+ await Promise.all(uploaders);
322
+ } catch (e) {
323
+ // If any uploader fails, abort the others
324
+ logger.info(`Upload failed, aborting...`);
325
+ controller.abort();
326
+ throw e;
219
327
  }
220
- await Promise.all(bucketsToPut);
221
328
 
222
- // then delete all the assets that aren't used anymore
329
+ // Delete stale assets
330
+ const deleteCount = namespaceKeys.size;
331
+ if (deleteCount > 0) {
332
+ const s = pluralise(deleteCount);
333
+ logger.info(`Removing ${formatNumber(deleteCount)} stale asset${s}...`);
334
+ }
223
335
  await deleteKVBulkKeyValue(accountId, namespace, Array.from(namespaceKeys));
224
336
 
225
337
  logger.log("↗️ Done syncing assets");
package/src/user/user.ts CHANGED
@@ -347,6 +347,7 @@ const Scopes = {
347
347
  "See and change Cloudflare Pages projects, settings and deployments.",
348
348
  "zone:read": "Grants read level access to account zone.",
349
349
  "ssl_certs:write": "See and manage mTLS certificates for your account",
350
+ "constellation:write": "Manage Constellation AI projects/models",
350
351
  } as const;
351
352
 
352
353
  /**
@@ -1320,6 +1320,7 @@ declare function publish({ directory, accountId, projectName, branch, skipCachin
1320
1320
  environment: "production" | "preview";
1321
1321
  id: string;
1322
1322
  url: string;
1323
+ project_id: string;
1323
1324
  project_name: string;
1324
1325
  build_config: {
1325
1326
  build_command: string;
@@ -1331,7 +1332,6 @@ declare function publish({ directory, accountId, projectName, branch, skipCachin
1331
1332
  };
1332
1333
  created_on: string;
1333
1334
  production_branch: string;
1334
- project_id: string;
1335
1335
  deployment_trigger: {
1336
1336
  type: string;
1337
1337
  metadata: {