wrangler 2.12.3 → 2.13.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/bundle.ts CHANGED
@@ -94,6 +94,15 @@ const nodejsCompatPlugin: esbuild.Plugin = {
94
94
  },
95
95
  };
96
96
 
97
+ const cloudflareJsPlugin: esbuild.Plugin = {
98
+ name: "cloudflare javascript Plugin",
99
+ setup(pluginBuild) {
100
+ pluginBuild.onResolve({ filter: /^cloudflare:.*/ }, () => {
101
+ return { external: true };
102
+ });
103
+ },
104
+ };
105
+
97
106
  /**
98
107
  * Generate a bundle for the worker identified by the arguments passed in.
99
108
  */
@@ -379,6 +388,7 @@ export async function bundleWorker(
379
388
  ? [NodeGlobalsPolyfills({ buffer: true }), NodeModulesPolyfills()]
380
389
  : []),
381
390
  ...(nodejsCompat ? [nodejsCompatPlugin] : []),
391
+ ...[cloudflareJsPlugin],
382
392
  ...(plugins || []),
383
393
  ],
384
394
  ...(jsxFactory && { jsxFactory }),
@@ -374,6 +374,9 @@ interface EnvironmentNonInheritable {
374
374
 
375
375
  /** The queue to send messages that failed to be consumed. */
376
376
  dead_letter_queue?: string;
377
+
378
+ /** The maximum number of concurrent consumer Worker invocations. Leaving this unset will allow your consumer to scale to the maximum concurrency needed to keep up with the message backlog. */
379
+ max_concurrency?: number | null;
377
380
  }[];
378
381
  };
379
382
 
@@ -2215,6 +2215,7 @@ const validateConsumer: ValidatorFn = (diagnostics, field, value, _config) => {
2215
2215
  "max_batch_timeout",
2216
2216
  "max_retries",
2217
2217
  "dead_letter_queue",
2218
+ "max_concurrency",
2218
2219
  ])
2219
2220
  ) {
2220
2221
  isValid = false;
@@ -2230,12 +2231,13 @@ const validateConsumer: ValidatorFn = (diagnostics, field, value, _config) => {
2230
2231
 
2231
2232
  const options: {
2232
2233
  key: string;
2233
- type: "number" | "string";
2234
+ type: "number" | "string" | "boolean";
2234
2235
  }[] = [
2235
2236
  { key: "max_batch_size", type: "number" },
2236
2237
  { key: "max_batch_timeout", type: "number" },
2237
2238
  { key: "max_retries", type: "number" },
2238
2239
  { key: "dead_letter_queue", type: "string" },
2240
+ { key: "max_concurrency", type: "number" },
2239
2241
  ];
2240
2242
  for (const optionalOpt of options) {
2241
2243
  if (!isOptionalProperty(value, optionalOpt.key, optionalOpt.type)) {
@@ -23,7 +23,7 @@ export function toMimeType(type: CfModuleType): string {
23
23
  }
24
24
  }
25
25
 
26
- type WorkerMetadataBinding =
26
+ export type WorkerMetadataBinding =
27
27
  // If you add any new binding types here, also add it to safeBindings
28
28
  // under validateUnsafeBinding in config/validation.ts
29
29
  | { type: "plain_text"; name: string; text: string }
@@ -172,10 +172,11 @@ Your database may not be available to serve requests during the migration, conti
172
172
  }
173
173
  } catch (e) {
174
174
  const err = e as ParseError;
175
+ const maybeCause = (err.cause ?? err) as Error;
175
176
 
176
177
  success = false;
177
178
  errorNotes = err.notes?.map((msg) => msg.text) ?? [
178
- err.message ?? err.toString(),
179
+ maybeCause?.message ?? maybeCause.toString(),
179
180
  ];
180
181
  }
181
182
 
@@ -1,9 +1,21 @@
1
1
  import { URLSearchParams } from "url";
2
+ import TOML from "@iarna/toml";
3
+ import chalk from "chalk";
4
+ import { FormData } from "undici";
2
5
  import { fetchResult } from "./cfetch";
6
+ import { readConfig } from "./config";
7
+ import { confirm, prompt } from "./dialogs";
8
+ import { mapBindings } from "./init";
3
9
  import { logger } from "./logger";
4
10
  import * as metrics from "./metrics";
11
+ import { requireAuth } from "./user";
12
+ import { getScriptName, printWranglerBanner } from ".";
13
+
5
14
  import type { Config } from "./config";
15
+ import type { WorkerMetadataBinding } from "./create-worker-upload-form";
6
16
  import type { ServiceMetadataRes } from "./init";
17
+ import type { CommonYargsOptions } from "./yargs-types";
18
+ import type { ArgumentsCamelCase } from "yargs";
7
19
 
8
20
  type DeploymentDetails = {
9
21
  id: string;
@@ -11,6 +23,7 @@ type DeploymentDetails = {
11
23
  annotations: {
12
24
  "workers/triggered_by": string;
13
25
  "workers/rollback_from": string;
26
+ "workers/message": string;
14
27
  };
15
28
  metadata: {
16
29
  author_id: string;
@@ -20,8 +33,15 @@ type DeploymentDetails = {
20
33
  modified_on: string;
21
34
  };
22
35
  resources: {
23
- script: string;
24
- bindings: unknown[];
36
+ script: {
37
+ handlers: string[];
38
+ };
39
+ bindings: WorkerMetadataBinding[];
40
+ script_runtime: {
41
+ compatibility_date: string | undefined;
42
+ compatibility_flags: string[] | undefined;
43
+ usage_model: string | undefined;
44
+ };
25
45
  };
26
46
  };
27
47
 
@@ -49,11 +69,12 @@ export async function deployments(
49
69
  }
50
70
  );
51
71
 
52
- const scriptMetadata = await fetchResult<ServiceMetadataRes>(
53
- `/accounts/${accountId}/workers/services/${scriptName}`
54
- );
72
+ const scriptTag = (
73
+ await fetchResult<ServiceMetadataRes>(
74
+ `/accounts/${accountId}/workers/services/${scriptName}`
75
+ )
76
+ ).default_environment.script.tag;
55
77
 
56
- const scriptTag = scriptMetadata.default_environment.script.tag;
57
78
  const params = new URLSearchParams({ order: "asc" });
58
79
  const { items: deploys } = await fetchResult<DeploymentListResult>(
59
80
  `/accounts/${accountId}/workers/deployments/by-script/${scriptTag}`,
@@ -72,12 +93,16 @@ export async function deployments(
72
93
  Deployment ID: ${versions.id}
73
94
  Created on: ${versions.metadata.created_on}
74
95
  Author: ${versions.metadata.author_email}
75
- Trigger: ${triggerStr}`;
96
+ Source: ${triggerStr}`;
76
97
 
77
98
  if (versions.annotations?.["workers/rollback_from"]) {
78
99
  version += `\nRollback from: ${versions.annotations["workers/rollback_from"]}`;
79
100
  }
80
101
 
102
+ if (versions.annotations?.["workers/message"]) {
103
+ version += `\nMessage: ${versions.annotations["workers/message"]}`;
104
+ }
105
+
81
106
  return version + `\n`;
82
107
  });
83
108
 
@@ -99,7 +124,6 @@ function formatSource(source: string): string {
99
124
  return "Other";
100
125
  }
101
126
  }
102
-
103
127
  function formatTrigger(trigger: string): string {
104
128
  switch (trigger) {
105
129
  case "upload":
@@ -114,3 +138,231 @@ function formatTrigger(trigger: string): string {
114
138
  return "Unknown";
115
139
  }
116
140
  }
141
+
142
+ export async function rollbackDeployment(
143
+ accountId: string,
144
+ scriptName: string | undefined,
145
+ { send_metrics: sendMetrics }: { send_metrics?: Config["send_metrics"] } = {},
146
+ deploymentId: string | undefined,
147
+ message: string | undefined
148
+ ) {
149
+ if (deploymentId === undefined) {
150
+ const scriptTag = (
151
+ await fetchResult<ServiceMetadataRes>(
152
+ `/accounts/${accountId}/workers/services/${scriptName}`
153
+ )
154
+ ).default_environment.script.tag;
155
+
156
+ const params = new URLSearchParams({ order: "asc" });
157
+ const { items: deploys } = await fetchResult<DeploymentListResult>(
158
+ `/accounts/${accountId}/workers/deployments/by-script/${scriptTag}`,
159
+ undefined,
160
+ params
161
+ );
162
+
163
+ if (deploys.length < 2) {
164
+ throw new Error(
165
+ "Cannot rollback to previous deployment since there are less than 2 deployemnts"
166
+ );
167
+ }
168
+
169
+ deploymentId = deploys.at(-2)?.id;
170
+ if (deploymentId === undefined) {
171
+ throw new Error("Cannot find previous deployment");
172
+ }
173
+ }
174
+
175
+ const firstHash = deploymentId.substring(0, deploymentId.indexOf("-"));
176
+
177
+ let rollbackMessage = "";
178
+ if (message !== undefined) {
179
+ rollbackMessage = message;
180
+ } else {
181
+ if (
182
+ !(await confirm(
183
+ `This deployment ${chalk.underline(
184
+ firstHash
185
+ )} will immediately replace the current deployment and become the active deployment across all your deployed routes and domains. However, your local development environment will not be affected by this rollback. ${chalk.blue.bold(
186
+ "Note:"
187
+ )} Rolling back to a previous deployment will not rollback any of the bound resources (Durable Object, R2, KV, etc.).`
188
+ ))
189
+ ) {
190
+ return;
191
+ }
192
+
193
+ rollbackMessage = await prompt(
194
+ "Please provide a message for this rollback (120 characters max)",
195
+ { defaultValue: "" }
196
+ );
197
+ }
198
+
199
+ let deployment_id = await rollbackRequest(
200
+ accountId,
201
+ scriptName,
202
+ deploymentId,
203
+ rollbackMessage
204
+ );
205
+
206
+ await metrics.sendMetricsEvent(
207
+ "rollback deployments",
208
+ { view: scriptName ? "single" : "all" },
209
+ {
210
+ sendMetrics,
211
+ }
212
+ );
213
+
214
+ deploymentId = addHyphens(deploymentId) ?? deploymentId;
215
+ deployment_id = addHyphens(deployment_id) ?? deployment_id;
216
+
217
+ logger.log(`\nSuccessfully rolled back to Deployment ID: ${deploymentId}`);
218
+ logger.log("Current Deployment ID:", deployment_id);
219
+ }
220
+
221
+ async function rollbackRequest(
222
+ accountId: string,
223
+ scriptName: string | undefined,
224
+ deploymentId: string,
225
+ rollbackReason: string
226
+ ): Promise<string | null> {
227
+ const body = new FormData();
228
+ body.set("message", rollbackReason);
229
+
230
+ const { deployment_id } = await fetchResult<{
231
+ deployment_id: string | null;
232
+ }>(
233
+ `/accounts/${accountId}/workers/scripts/${scriptName}?rollback_to=${deploymentId}`,
234
+ {
235
+ method: "PUT",
236
+ body,
237
+ }
238
+ );
239
+
240
+ return deployment_id;
241
+ }
242
+
243
+ export async function viewDeployment(
244
+ accountId: string,
245
+ scriptName: string | undefined,
246
+ { send_metrics: sendMetrics }: { send_metrics?: Config["send_metrics"] } = {},
247
+ deploymentId: string | undefined
248
+ ) {
249
+ await metrics.sendMetricsEvent(
250
+ "view deployments",
251
+ { view: scriptName ? "single" : "all" },
252
+ {
253
+ sendMetrics,
254
+ }
255
+ );
256
+
257
+ const scriptTag = (
258
+ await fetchResult<ServiceMetadataRes>(
259
+ `/accounts/${accountId}/workers/services/${scriptName}`
260
+ )
261
+ ).default_environment.script.tag;
262
+
263
+ if (deploymentId === undefined) {
264
+ const params = new URLSearchParams({ order: "asc" });
265
+ const { latest } = await fetchResult<DeploymentListResult>(
266
+ `/accounts/${accountId}/workers/deployments/by-script/${scriptTag}`,
267
+ undefined,
268
+ params
269
+ );
270
+
271
+ deploymentId = latest.id;
272
+ if (deploymentId === undefined) {
273
+ throw new Error("Cannot find previous deployment");
274
+ }
275
+ }
276
+
277
+ const deploymentDetails = await fetchResult<DeploymentListResult["latest"]>(
278
+ `/accounts/${accountId}/workers/deployments/by-script/${scriptTag}/detail/${deploymentId}`
279
+ );
280
+
281
+ const triggerStr = deploymentDetails.annotations?.["workers/triggered_by"]
282
+ ? `${formatTrigger(
283
+ deploymentDetails.annotations["workers/triggered_by"]
284
+ )} from ${formatSource(deploymentDetails.metadata.source)}`
285
+ : `${formatSource(deploymentDetails.metadata.source)}`;
286
+
287
+ const rollbackStr = deploymentDetails.annotations?.["workers/rollback_from"]
288
+ ? `\nRollback from: ${deploymentDetails.annotations["workers/rollback_from"]}`
289
+ : ``;
290
+
291
+ const reasonStr = deploymentDetails.annotations?.["workers/message"]
292
+ ? `\nMessage: ${deploymentDetails.annotations["workers/message"]}`
293
+ : ``;
294
+
295
+ const compatDateStr = deploymentDetails.resources.script_runtime
296
+ ?.compatibility_date
297
+ ? `\nCompatibility Date: ${deploymentDetails.resources.script_runtime?.compatibility_date}`
298
+ : ``;
299
+ const compatFlagsStr = deploymentDetails.resources.script_runtime
300
+ ?.compatibility_flags
301
+ ? `\nCompatibility Flags: ${deploymentDetails.resources.script_runtime?.compatibility_flags}`
302
+ : ``;
303
+
304
+ const bindings = deploymentDetails.resources.bindings;
305
+
306
+ const version = `
307
+ Deployment ID: ${deploymentDetails.id}
308
+ Created on: ${deploymentDetails.metadata.created_on}
309
+ Author: ${deploymentDetails.metadata.author_email}
310
+ Source: ${triggerStr}${rollbackStr}${reasonStr}
311
+ ------------------------------------------------------------
312
+ Author ID: ${deploymentDetails.metadata.author_id}
313
+ Usage Model: ${deploymentDetails.resources.script_runtime.usage_model}
314
+ Handlers: ${
315
+ deploymentDetails.resources.script.handlers
316
+ }${compatDateStr}${compatFlagsStr}
317
+ --------------------------bindings--------------------------
318
+ ${
319
+ bindings.length > 0
320
+ ? TOML.stringify(mapBindings(bindings) as TOML.JsonMap)
321
+ : `None`
322
+ }
323
+ `;
324
+
325
+ logger.log(version);
326
+
327
+ // early return to skip the deployments listings
328
+ return;
329
+ }
330
+
331
+ export async function commonDeploymentCMDSetup(
332
+ yargs: ArgumentsCamelCase<CommonYargsOptions>,
333
+ deploymentsWarning: string
334
+ ) {
335
+ await printWranglerBanner();
336
+ const config = readConfig(yargs.config, yargs);
337
+ const accountId = await requireAuth(config);
338
+ const scriptName = getScriptName(
339
+ { name: yargs.name as string, env: undefined },
340
+ config
341
+ );
342
+
343
+ logger.log(`${deploymentsWarning}\n`);
344
+
345
+ return { accountId, scriptName, config };
346
+ }
347
+
348
+ export function addHyphens(uuid: string | null): string | null {
349
+ if (uuid == null) {
350
+ return uuid;
351
+ }
352
+
353
+ if (uuid.length != 32) {
354
+ return null;
355
+ }
356
+
357
+ const uuid_parts: string[] = [];
358
+ uuid_parts.push(uuid.slice(0, 8));
359
+ uuid_parts.push(uuid.slice(8, 12));
360
+ uuid_parts.push(uuid.slice(12, 16));
361
+ uuid_parts.push(uuid.slice(16, 20));
362
+ uuid_parts.push(uuid.slice(20));
363
+
364
+ let hyphenated = "";
365
+ uuid_parts.forEach((part) => (hyphenated += part + "-"));
366
+
367
+ return hyphenated.slice(0, 36);
368
+ }
package/src/index.ts CHANGED
@@ -9,7 +9,12 @@ import { isBuildFailure } from "./bundle";
9
9
  import { loadDotEnv, readConfig } from "./config";
10
10
  import { d1 } from "./d1";
11
11
  import { deleteHandler, deleteOptions } from "./delete";
12
- import { deployments } from "./deployments";
12
+ import {
13
+ deployments,
14
+ commonDeploymentCMDSetup,
15
+ rollbackDeployment,
16
+ viewDeployment,
17
+ } from "./deployments";
13
18
  import {
14
19
  buildHandler,
15
20
  buildOptions,
@@ -41,13 +46,7 @@ import { secret, secretBulkHandler, secretBulkOptions } from "./secret";
41
46
  import { tailOptions, tailHandler } from "./tail";
42
47
  import { generateTypes } from "./type-generation";
43
48
  import { updateCheck } from "./update-check";
44
- import {
45
- listScopes,
46
- login,
47
- logout,
48
- requireAuth,
49
- validateScopeKeys,
50
- } from "./user";
49
+ import { listScopes, login, logout, validateScopeKeys } from "./user";
51
50
  import { whoami } from "./whoami";
52
51
 
53
52
  import type { Config } from "./config";
@@ -581,27 +580,81 @@ export function createCLIParser(argv: string[]) {
581
580
  "🚧`wrangler deployments` is a beta command. Please report any issues to https://github.com/cloudflare/workers-sdk/issues/new/choose";
582
581
  wrangler.command(
583
582
  "deployments",
584
- "🚢 Displays the 10 most recent deployments for a worker",
583
+ "🚢 List and view details for deployments",
585
584
  (yargs) =>
586
585
  yargs
587
586
  .option("name", {
588
587
  describe: "The name of your worker",
589
588
  type: "string",
590
589
  })
591
- .epilogue(deploymentsWarning),
592
- async (deploymentsYargs) => {
593
- await printWranglerBanner();
594
- const config = readConfig(deploymentsYargs.config, deploymentsYargs);
595
- const accountId = await requireAuth(config);
596
- const scriptName = getScriptName(
597
- { name: deploymentsYargs.name, env: undefined },
598
- config
599
- );
600
-
601
- logger.log(`${deploymentsWarning}\n`);
602
- await deployments(accountId, scriptName, config);
603
- }
590
+ .command(
591
+ "list",
592
+ "🚢 Displays the 10 most recent deployments for a worker",
593
+ async (listYargs) => listYargs,
594
+ async (listYargs) => {
595
+ const { accountId, scriptName, config } =
596
+ await commonDeploymentCMDSetup(listYargs, deploymentsWarning);
597
+ await deployments(accountId, scriptName, config);
598
+ }
599
+ )
600
+ .command(
601
+ "view [deployment-id]",
602
+ "🔍 View a deployment",
603
+ async (viewYargs) =>
604
+ viewYargs.positional("deployment-id", {
605
+ describe: "The ID of the deployment you want to inspect",
606
+ type: "string",
607
+ demandOption: false,
608
+ }),
609
+ async (viewYargs) => {
610
+ const { accountId, scriptName, config } =
611
+ await commonDeploymentCMDSetup(viewYargs, deploymentsWarning);
612
+
613
+ await viewDeployment(
614
+ accountId,
615
+ scriptName,
616
+ config,
617
+ viewYargs.deploymentId
618
+ );
619
+ }
620
+ )
621
+ .command(subHelp)
622
+ .epilogue(deploymentsWarning)
604
623
  );
624
+ const rollbackWarning =
625
+ "🚧`wrangler rollback` is a beta command. Please report any issues to https://github.com/cloudflare/workers-sdk/issues/new/choose";
626
+ wrangler
627
+ .command(
628
+ "rollback [deployment-id]",
629
+ "🔙 Rollback a deployment",
630
+ (rollbackYargs) =>
631
+ rollbackYargs
632
+ .positional("deployment-id", {
633
+ describe: "The ID of the deployment to rollback to",
634
+ type: "string",
635
+ demandOption: false,
636
+ })
637
+ .option("message", {
638
+ alias: "m",
639
+ describe:
640
+ "Skip confirmation and message prompts, uses provided argument as message",
641
+ type: "string",
642
+ default: undefined,
643
+ }),
644
+ async (rollbackYargs) => {
645
+ const { accountId, scriptName, config } =
646
+ await commonDeploymentCMDSetup(rollbackYargs, rollbackWarning);
647
+
648
+ await rollbackDeployment(
649
+ accountId,
650
+ scriptName,
651
+ config,
652
+ rollbackYargs.deploymentId,
653
+ rollbackYargs.message
654
+ );
655
+ }
656
+ )
657
+ .epilogue(rollbackWarning);
605
658
 
606
659
  // This set to false to allow overwrite of default behaviour
607
660
  wrangler.version(false);