-
Notifications
You must be signed in to change notification settings - Fork 756
/
deploy.ts
1327 lines (1211 loc) · 40.6 KB
/
deploy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import assert from "node:assert";
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
import path from "node:path";
import { URLSearchParams } from "node:url";
import { cancel } from "@cloudflare/cli";
import { syncAssets } from "../assets";
import { fetchListResult, fetchResult } from "../cfetch";
import { configFileName, formatConfigSnippet } from "../config";
import { getBindings, provisionBindings } from "../deployment-bundle/bindings";
import { bundleWorker } from "../deployment-bundle/bundle";
import {
printBundleSize,
printOffendingDependencies,
} from "../deployment-bundle/bundle-reporter";
import { getBundleType } from "../deployment-bundle/bundle-type";
import { createWorkerUploadForm } from "../deployment-bundle/create-worker-upload-form";
import { logBuildOutput } from "../deployment-bundle/esbuild-plugins/log-build-output";
import {
findAdditionalModules,
writeAdditionalModules,
} from "../deployment-bundle/find-additional-modules";
import {
createModuleCollector,
getWrangler1xLegacyModuleReferences,
} from "../deployment-bundle/module-collection";
import { validateNodeCompatMode } from "../deployment-bundle/node-compat";
import { loadSourceMaps } from "../deployment-bundle/source-maps";
import { confirm } from "../dialogs";
import { getMigrationsToUpload } from "../durable";
import { UserError } from "../errors";
import { logger } from "../logger";
import { getMetricsUsageHeaders } from "../metrics";
import { isNavigatorDefined } from "../navigator-user-agent";
import { APIError, ParseError, parseNonHyphenedUuid } from "../parse";
import { getWranglerTmpDir } from "../paths";
import {
ensureQueuesExistByConfig,
getQueue,
postConsumer,
putConsumer,
putConsumerById,
putQueue,
} from "../queues/client";
import { syncLegacyAssets } from "../sites";
import {
getSourceMappedString,
maybeRetrieveFileSourceMap,
} from "../sourcemap";
import triggersDeploy from "../triggers/deploy";
import { printBindings } from "../utils/print-bindings";
import { retryOnError } from "../utils/retry";
import {
createDeployment,
patchNonVersionedScriptSettings,
} from "../versions/api";
import { confirmLatestDeploymentOverwrite } from "../versions/deploy";
import { getZoneForRoute } from "../zones";
import type { AssetsOptions } from "../assets";
import type { Config } from "../config";
import type {
CustomDomainRoute,
Route,
Rule,
ZoneIdRoute,
ZoneNameRoute,
} from "../config/environment";
import type { Entry } from "../deployment-bundle/entry";
import type {
CfModule,
CfPlacement,
CfWorkerInit,
} from "../deployment-bundle/worker";
import type { PostQueueBody, PostTypedConsumerBody } from "../queues/client";
import type { LegacyAssetPaths } from "../sites";
import type { RetrieveSourceMapFunction } from "../sourcemap";
import type { ApiVersion, Percentage, VersionId } from "../versions/types";
type Props = {
config: Config;
accountId: string | undefined;
entry: Entry;
rules: Config["rules"];
name: string;
env: string | undefined;
compatibilityDate: string | undefined;
compatibilityFlags: string[] | undefined;
legacyAssetPaths: LegacyAssetPaths | undefined;
assetsOptions: AssetsOptions | undefined;
vars: Record<string, string> | undefined;
defines: Record<string, string> | undefined;
alias: Record<string, string> | undefined;
triggers: string[] | undefined;
routes: string[] | undefined;
legacyEnv: boolean | undefined;
jsxFactory: string | undefined;
jsxFragment: string | undefined;
tsconfig: string | undefined;
isWorkersSite: boolean;
minify: boolean | undefined;
nodeCompat: boolean | undefined;
outDir: string | undefined;
dryRun: boolean | undefined;
noBundle: boolean | undefined;
keepVars: boolean | undefined;
logpush: boolean | undefined;
uploadSourceMaps: boolean | undefined;
oldAssetTtl: number | undefined;
projectRoot: string | undefined;
dispatchNamespace: string | undefined;
experimentalVersions: boolean | undefined;
experimentalAutoCreate: boolean;
};
export type RouteObject = ZoneIdRoute | ZoneNameRoute | CustomDomainRoute;
export type CustomDomain = {
id: string;
zone_id: string;
zone_name: string;
hostname: string;
service: string;
environment: string;
};
type UpdatedCustomDomain = CustomDomain & { modified: boolean };
type ConflictingCustomDomain = CustomDomain & {
external_dns_record_id?: string;
external_cert_id?: string;
};
export type CustomDomainChangeset = {
added: CustomDomain[];
removed: CustomDomain[];
updated: UpdatedCustomDomain[];
conflicting: ConflictingCustomDomain[];
};
export function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
const scriptStartupErrorRegex = /startup/i;
function errIsScriptSize(err: unknown): err is { code: 10027 } {
if (!err) {
return false;
}
// 10027 = workers.api.error.script_too_large
if ((err as { code: number }).code === 10027) {
return true;
}
return false;
}
function errIsStartupErr(err: unknown): err is ParseError & { code: 10021 } {
if (!err) {
return false;
}
// 10021 = validation error
// no explicit error code for more granular errors than "invalid script"
// but the error will contain a string error message directly from the
// validator.
// the error always SHOULD look like "Script startup exceeded CPU limit."
// (or the less likely "Script startup exceeded memory limits.")
if (
(err as { code: number }).code === 10021 &&
err instanceof ParseError &&
scriptStartupErrorRegex.test(err.notes[0]?.text)
) {
return true;
}
return false;
}
export const validateRoutes = (routes: Route[], assets?: AssetsOptions) => {
const invalidRoutes: Record<string, string[]> = {};
const mountedAssetRoutes: string[] = [];
for (const route of routes) {
if (typeof route !== "string" && route.custom_domain) {
if (route.pattern.includes("*")) {
invalidRoutes[route.pattern] ??= [];
invalidRoutes[route.pattern].push(
`Wildcard operators (*) are not allowed in Custom Domains`
);
}
if (route.pattern.includes("/")) {
invalidRoutes[route.pattern] ??= [];
invalidRoutes[route.pattern].push(
`Paths are not allowed in Custom Domains`
);
}
// If we have Assets but we're not always hitting the Worker then validate
} else if (
assets?.directory !== undefined &&
assets.assetConfig.serve_directly !== true
) {
const pattern = typeof route === "string" ? route : route.pattern;
const components = pattern.split("/");
// If this isn't `domain.com/*` then we're mounting to a path
if (!(components.length === 2 && components[1] === "*")) {
mountedAssetRoutes.push(pattern);
}
}
}
if (Object.keys(invalidRoutes).length > 0) {
throw new UserError(
`Invalid Routes:\n` +
Object.entries(invalidRoutes)
.map(([route, errors]) => `${route}:\n` + errors.join("\n"))
.join(`\n\n`)
);
}
if (mountedAssetRoutes.length > 0 && assets?.directory !== undefined) {
const relativeAssetsDir = path.relative(process.cwd(), assets.directory);
logger.once.warn(
`Warning: The following routes will attempt to serve Assets on a configured path:\n${mountedAssetRoutes
.map((route) => {
const routeNoScheme = route.replace(/https?:\/\//g, "");
const assetPath = path.join(
relativeAssetsDir,
routeNoScheme.substring(routeNoScheme.indexOf("/"))
);
return ` • ${route} (Will match assets: ${assetPath})`;
})
.join("\n")}` +
(assets?.routingConfig.has_user_worker
? "\n\nRequests not matching an asset will be forwarded to the Worker's code."
: "")
);
}
};
export function renderRoute(route: Route): string {
let result = "";
if (typeof route === "string") {
result = route;
} else {
result = route.pattern;
const isCustomDomain = Boolean(
"custom_domain" in route && route.custom_domain
);
if (isCustomDomain && "zone_id" in route) {
result += ` (custom domain - zone id: ${route.zone_id})`;
} else if (isCustomDomain && "zone_name" in route) {
result += ` (custom domain - zone name: ${route.zone_name})`;
} else if (isCustomDomain) {
result += ` (custom domain)`;
} else if ("zone_id" in route) {
result += ` (zone id: ${route.zone_id})`;
} else if ("zone_name" in route) {
result += ` (zone name: ${route.zone_name})`;
}
}
return result;
}
// publishing to custom domains involves a few more steps than just updating
// the routing table, and thus the api implementing it is fairly defensive -
// it will error eagerly on conflicts against existing domains or existing
// managed DNS records
// however, you can pass params to override the errors. to know if we should
// override the current state, we generate a "changeset" of required actions
// to get to the state we want (specified by the list of custom domains). the
// changeset returns an "updated" collection (existing custom domains
// connected to other scripts) and a "conflicting" collection (the requested
// custom domains that have a managed, conflicting DNS record preventing the
// host's use as a custom domain). with this information, we can prompt to
// the user what will occur if we create the custom domains requested, and
// add the override param if they confirm the action
//
// if a user does not confirm that they want to override, we skip publishing
// to these custom domains, but continue on through the rest of the
// deploy stage
export async function publishCustomDomains(
workerUrl: string,
accountId: string,
domains: Array<RouteObject>
): Promise<string[]> {
const config = {
override_scope: true,
override_existing_origin: false,
override_existing_dns_record: false,
};
const origins = domains.map((domainRoute) => {
return {
hostname: domainRoute.pattern,
zone_id: "zone_id" in domainRoute ? domainRoute.zone_id : undefined,
zone_name: "zone_name" in domainRoute ? domainRoute.zone_name : undefined,
};
});
const fail = () => {
return [
domains.length > 1
? `Publishing to ${domains.length} Custom Domains was skipped, fix conflicts and try again`
: `Publishing to Custom Domain "${domains[0].pattern}" was skipped, fix conflict and try again`,
];
};
if (!process.stdout.isTTY) {
// running in non-interactive mode.
// existing origins / dns records are not indicative of errors,
// so we aggressively update rather than aggressively fail
config.override_existing_origin = true;
config.override_existing_dns_record = true;
} else {
// get a changeset for operations required to achieve a state with the requested domains
const changeset = await fetchResult<CustomDomainChangeset>(
`${workerUrl}/domains/changeset?replace_state=true`,
{
method: "POST",
body: JSON.stringify(origins),
headers: {
"Content-Type": "application/json",
},
}
);
const updatesRequired = changeset.updated.filter(
(domain) => domain.modified
);
if (updatesRequired.length > 0) {
// find out which scripts the conflict domains are already attached to
// so we can provide that in the confirmation prompt
const existing = await Promise.all(
updatesRequired.map((domain) =>
fetchResult<CustomDomain>(
`/accounts/${accountId}/workers/domains/records/${domain.id}`
)
)
);
const existingRendered = existing
.map(
(domain) =>
`\t• ${domain.hostname} (used as a domain for "${domain.service}")`
)
.join("\n");
const message = `Custom Domains already exist for these domains:
${existingRendered}
Update them to point to this script instead?`;
if (!(await confirm(message))) {
return fail();
}
config.override_existing_origin = true;
}
if (changeset.conflicting.length > 0) {
const conflicitingRendered = changeset.conflicting
.map((domain) => `\t• ${domain.hostname}`)
.join("\n");
const message = `You already have DNS records that conflict for these Custom Domains:
${conflicitingRendered}
Update them to point to this script instead?`;
if (!(await confirm(message))) {
return fail();
}
config.override_existing_dns_record = true;
}
}
// deploy to domains
await fetchResult(`${workerUrl}/domains/records`, {
method: "PUT",
body: JSON.stringify({ ...config, origins }),
headers: {
"Content-Type": "application/json",
},
});
return domains.map((domain) => renderRoute(domain));
}
export default async function deploy(props: Props): Promise<{
sourceMapSize?: number;
versionId: string | null;
workerTag: string | null;
targets?: string[];
}> {
// TODO: warn if git/hg has uncommitted changes
const { config, accountId, name } = props;
let workerTag: string | null = null;
let versionId: string | null = null;
let workerExists: boolean = true;
if (!props.dispatchNamespace && accountId) {
try {
const serviceMetaData = await fetchResult<{
default_environment: {
script: {
tag: string;
last_deployed_from: "dash" | "wrangler" | "api";
};
};
}>(`/accounts/${accountId}/workers/services/${name}`);
const {
default_environment: { script },
} = serviceMetaData;
workerTag = script.tag;
if (script.last_deployed_from === "dash") {
logger.warn(
`You are about to publish a Workers Service that was last published via the Cloudflare Dashboard.\nEdits that have been made via the dashboard will be overridden by your local code and config.`
);
if (!(await confirm("Would you like to continue?"))) {
return { versionId, workerTag };
}
} else if (script.last_deployed_from === "api") {
logger.warn(
`You are about to publish a Workers Service that was last updated via the script API.\nEdits that have been made via the script API will be overridden by your local code and config.`
);
if (!(await confirm("Would you like to continue?"))) {
return { versionId, workerTag };
}
}
} catch (e) {
// code: 10090, message: workers.api.error.service_not_found
// is thrown from the above fetchResult on the first deploy of a Worker
if ((e as { code?: number }).code !== 10090) {
throw e;
} else {
workerExists = false;
}
}
}
if (!(props.compatibilityDate || config.compatibility_date)) {
const compatibilityDateStr = `${new Date().getFullYear()}-${(
new Date().getMonth() +
1 +
""
).padStart(2, "0")}-${(new Date().getDate() + "").padStart(2, "0")}`;
throw new UserError(`A compatibility_date is required when publishing. Add the following to your ${configFileName(config.configPath)} file:
\`\`\`
${formatConfigSnippet({ compatibility_date: compatibilityDateStr }, config.configPath, false)}
\`\`\`
Or you could pass it in your terminal as \`--compatibility-date ${compatibilityDateStr}\`
See https://developers.cloudflare.com/workers/platform/compatibility-dates for more information.`);
}
const routes =
props.routes ?? config.routes ?? (config.route ? [config.route] : []) ?? [];
validateRoutes(routes, props.assetsOptions);
const jsxFactory = props.jsxFactory || config.jsx_factory;
const jsxFragment = props.jsxFragment || config.jsx_fragment;
const keepVars = props.keepVars || config.keep_vars;
const minify = props.minify ?? config.minify;
const compatibilityDate =
props.compatibilityDate ?? config.compatibility_date;
const compatibilityFlags =
props.compatibilityFlags ?? config.compatibility_flags;
const nodejsCompatMode = validateNodeCompatMode(
compatibilityDate,
compatibilityFlags,
{
nodeCompat: props.nodeCompat ?? config.node_compat,
noBundle: props.noBundle ?? config.no_bundle,
}
);
// Warn if user tries minify with no-bundle
if (props.noBundle && minify) {
logger.warn(
"`--minify` and `--no-bundle` can't be used together. If you want to minify your Worker and disable Wrangler's bundling, please minify as part of your own bundling process."
);
}
const scriptName = props.name;
assert(
!config.site || config.site.bucket,
"A [site] definition requires a `bucket` field with a path to the site's assets directory."
);
if (props.outDir) {
// we're using a custom output directory,
// so let's first ensure it exists
mkdirSync(props.outDir, { recursive: true });
// add a README
const readmePath = path.join(props.outDir, "README.md");
writeFileSync(
readmePath,
`This folder contains the built output assets for the worker "${scriptName}" generated at ${new Date().toISOString()}.`
);
}
const destination =
props.outDir ?? getWranglerTmpDir(props.projectRoot, "deploy");
const envName = props.env ?? "production";
const start = Date.now();
const prod = Boolean(props.legacyEnv || !props.env);
const notProd = !prod;
const workerName = notProd ? `${scriptName} (${envName})` : scriptName;
const workerUrl = props.dispatchNamespace
? `/accounts/${accountId}/workers/dispatch/namespaces/${props.dispatchNamespace}/scripts/${scriptName}`
: notProd
? `/accounts/${accountId}/workers/services/${scriptName}/environments/${envName}`
: `/accounts/${accountId}/workers/scripts/${scriptName}`;
const { format } = props.entry;
if (!props.dispatchNamespace && prod && accountId && scriptName) {
const yes = await confirmLatestDeploymentOverwrite(accountId, scriptName);
if (!yes) {
cancel("Aborting deploy...");
return { versionId, workerTag };
}
}
if (
!props.isWorkersSite &&
Boolean(props.legacyAssetPaths) &&
format === "service-worker"
) {
throw new UserError(
"You cannot use the service-worker format with an `assets` directory yet. For information on how to migrate to the module-worker format, see: https://developers.cloudflare.com/workers/learning/migrating-to-module-workers/"
);
}
if (config.wasm_modules && format === "modules") {
throw new UserError(
"You cannot configure [wasm_modules] with an ES module worker. Instead, import the .wasm module directly in your code"
);
}
if (config.text_blobs && format === "modules") {
throw new UserError(
`You cannot configure [text_blobs] with an ES module worker. Instead, import the file directly in your code, and optionally configure \`[rules]\` in your ${configFileName(config.configPath)} file`
);
}
if (config.data_blobs && format === "modules") {
throw new UserError(
`You cannot configure [data_blobs] with an ES module worker. Instead, import the file directly in your code, and optionally configure \`[rules]\` in your ${configFileName(config.configPath)} file`
);
}
let sourceMapSize;
try {
if (props.noBundle) {
// if we're not building, let's just copy the entry to the destination directory
const destinationDir =
typeof destination === "string" ? destination : destination.path;
mkdirSync(destinationDir, { recursive: true });
writeFileSync(
path.join(destinationDir, path.basename(props.entry.file)),
readFileSync(props.entry.file, "utf-8")
);
}
const entryDirectory = path.dirname(props.entry.file);
const moduleCollector = createModuleCollector({
wrangler1xLegacyModuleReferences: getWrangler1xLegacyModuleReferences(
entryDirectory,
props.entry.file
),
entry: props.entry,
// `moduleCollector` doesn't get used when `props.noBundle` is set, so
// `findAdditionalModules` always defaults to `false`
findAdditionalModules: config.find_additional_modules ?? false,
rules: props.rules,
preserveFileNames: config.preserve_file_names ?? false,
});
const uploadSourceMaps =
props.uploadSourceMaps ?? config.upload_source_maps;
const {
modules,
dependencies,
resolvedEntryPointPath,
bundleType,
...bundle
} = props.noBundle
? await noBundleWorker(props.entry, props.rules, props.outDir)
: await bundleWorker(
props.entry,
typeof destination === "string" ? destination : destination.path,
{
bundle: true,
additionalModules: [],
moduleCollector,
serveLegacyAssetsFromWorker:
!props.isWorkersSite && Boolean(props.legacyAssetPaths),
doBindings: config.durable_objects.bindings,
workflowBindings: config.workflows ?? [],
jsxFactory,
jsxFragment,
tsconfig: props.tsconfig ?? config.tsconfig,
minify,
sourcemap: uploadSourceMaps,
nodejsCompatMode,
define: { ...config.define, ...props.defines },
checkFetch: false,
alias: config.alias,
legacyAssets: config.legacy_assets,
// We do not mock AE datasets when deploying
mockAnalyticsEngineDatasets: [],
// enable the cache when publishing
bypassAssetCache: false,
// We want to know if the build is for development or publishing
// This could potentially cause issues as we no longer have identical behaviour between dev and deploy?
targetConsumer: "deploy",
local: false,
projectRoot: props.projectRoot,
defineNavigatorUserAgent: isNavigatorDefined(
props.compatibilityDate ?? config.compatibility_date,
props.compatibilityFlags ?? config.compatibility_flags
),
plugins: [logBuildOutput(nodejsCompatMode)],
// Pages specific options used by wrangler pages commands
entryName: undefined,
inject: undefined,
isOutfile: undefined,
external: undefined,
// These options are dev-only
testScheduled: undefined,
watch: undefined,
}
);
// Add modules to dependencies for size warning
for (const module of modules) {
const modulePath =
module.filePath === undefined
? module.name
: path.relative("", module.filePath);
const bytesInOutput =
typeof module.content === "string"
? Buffer.byteLength(module.content)
: module.content.byteLength;
dependencies[modulePath] = { bytesInOutput };
}
const content = readFileSync(resolvedEntryPointPath, {
encoding: "utf-8",
});
// durable object migrations
const migrations = !props.dryRun
? await getMigrationsToUpload(scriptName, {
accountId,
config,
legacyEnv: props.legacyEnv,
env: props.env,
dispatchNamespace: props.dispatchNamespace,
})
: undefined;
// Upload assets if assets is being used
const assetsJwt =
props.assetsOptions && !props.dryRun
? await syncAssets(
accountId,
props.assetsOptions.directory,
scriptName,
props.dispatchNamespace
)
: undefined;
const legacyAssets = await syncLegacyAssets(
accountId,
// When we're using the newer service environments, we wouldn't
// have added the env name on to the script name. However, we must
// include it in the kv namespace name regardless (since there's no
// concept of service environments for kv namespaces yet).
scriptName + (!props.legacyEnv && props.env ? `-${props.env}` : ""),
props.legacyAssetPaths,
false,
props.dryRun,
props.oldAssetTtl
);
const bindings = getBindings({
...config,
kv_namespaces: config.kv_namespaces.concat(
legacyAssets.namespace
? { binding: "__STATIC_CONTENT", id: legacyAssets.namespace }
: []
),
vars: { ...config.vars, ...props.vars },
text_blobs: {
...config.text_blobs,
...(legacyAssets.manifest &&
format === "service-worker" && {
__STATIC_CONTENT_MANIFEST: "__STATIC_CONTENT_MANIFEST",
}),
},
});
if (legacyAssets.manifest) {
modules.push({
name: "__STATIC_CONTENT_MANIFEST",
filePath: undefined,
content: JSON.stringify(legacyAssets.manifest),
type: "text",
});
}
// The upload API only accepts an empty string or no specified placement for the "off" mode.
const placement: CfPlacement | undefined =
config.placement?.mode === "smart"
? { mode: "smart", hint: config.placement.hint }
: undefined;
const entryPointName = path.basename(resolvedEntryPointPath);
const main: CfModule = {
name: entryPointName,
filePath: resolvedEntryPointPath,
content: content,
type: bundleType,
};
const worker: CfWorkerInit = {
name: scriptName,
main,
bindings,
migrations,
modules,
sourceMaps: uploadSourceMaps
? loadSourceMaps(main, modules, bundle)
: undefined,
compatibility_date: props.compatibilityDate ?? config.compatibility_date,
compatibility_flags: compatibilityFlags,
keepVars,
keepSecrets: keepVars, // keepVars implies keepSecrets
logpush: props.logpush !== undefined ? props.logpush : config.logpush,
placement,
tail_consumers: config.tail_consumers,
limits: config.limits,
assets:
props.assetsOptions && assetsJwt
? {
jwt: assetsJwt,
routingConfig: props.assetsOptions.routingConfig,
assetConfig: props.assetsOptions.assetConfig,
}
: undefined,
observability: config.observability,
};
sourceMapSize = worker.sourceMaps?.reduce(
(acc, m) => acc + m.content.length,
0
);
await printBundleSize(
{ name: path.basename(resolvedEntryPointPath), content: content },
modules
);
const withoutStaticAssets = {
...bindings,
kv_namespaces: config.kv_namespaces,
text_blobs: config.text_blobs,
};
// mask anything that was overridden in cli args
// so that we don't log potential secrets into the terminal
const maskedVars = { ...withoutStaticAssets.vars };
for (const key of Object.keys(maskedVars)) {
if (maskedVars[key] !== config.vars[key]) {
// This means it was overridden in cli args
// so let's mask it
maskedVars[key] = "(hidden)";
}
}
// We can use the new versions/deployments APIs if we:
// * have --x-versions enabled (default, but can be disabled with --no-x-versions)
// * are uploading a worker that already exists
// * aren't a dispatch namespace deploy
// * aren't a service env deploy
// * aren't a service Worker
// * we don't have DO migrations
// * we aren't an fpw
const canUseNewVersionsDeploymentsApi =
props.experimentalVersions &&
workerExists &&
props.dispatchNamespace === undefined &&
prod &&
format === "modules" &&
migrations === undefined &&
!config.first_party_worker;
if (props.dryRun) {
printBindings({ ...withoutStaticAssets, vars: maskedVars });
} else {
assert(accountId, "Missing accountId");
await provisionBindings(
bindings,
accountId,
scriptName,
props.experimentalAutoCreate,
props.config
);
await ensureQueuesExistByConfig(config);
let bindingsPrinted = false;
// Upload the script so it has time to propagate.
try {
let result: {
id: string | null;
etag: string | null;
pipeline_hash: string | null;
mutable_pipeline_id: string | null;
deployment_id: string | null;
startup_time_ms?: number;
};
// If we're using the new APIs, first upload the version
if (canUseNewVersionsDeploymentsApi) {
// Upload new version
const versionResult = await retryOnError(async () =>
fetchResult<ApiVersion>(
`/accounts/${accountId}/workers/scripts/${scriptName}/versions`,
{
method: "POST",
body: createWorkerUploadForm(worker),
headers: await getMetricsUsageHeaders(config.send_metrics),
}
)
);
// Deploy new version to 100%
const versionMap = new Map<VersionId, Percentage>();
versionMap.set(versionResult.id, 100);
await createDeployment(accountId, scriptName, versionMap, undefined);
// Update tail consumers, logpush, and observability settings
await patchNonVersionedScriptSettings(accountId, scriptName, {
tail_consumers: worker.tail_consumers,
logpush: worker.logpush,
// If the user hasn't specified observability assume that they want it disabled if they have it on.
// This is a no-op in the event that they don't have observability enabled, but will remove observability
// if it has been removed from their Wrangler configuration file
observability: worker.observability ?? { enabled: false },
});
result = {
id: null, // fpw - ignore
etag: versionResult.resources.script.etag,
pipeline_hash: null, // fpw - ignore
mutable_pipeline_id: null, // fpw - ignore
deployment_id: versionResult.id, // version id not deployment id but easier to adapt here
startup_time_ms: versionResult.startup_time_ms,
};
} else {
result = await retryOnError(async () =>
fetchResult<{
id: string | null;
etag: string | null;
pipeline_hash: string | null;
mutable_pipeline_id: string | null;
deployment_id: string | null;
startup_time_ms: number;
}>(
workerUrl,
{
method: "PUT",
body: createWorkerUploadForm(worker),
headers: await getMetricsUsageHeaders(config.send_metrics),
},
new URLSearchParams({
// pass excludeScript so the whole body of the
// script doesn't get included in the response
excludeScript: "true",
})
)
);
}
if (result.startup_time_ms) {
logger.log("Worker Startup Time:", result.startup_time_ms, "ms");
}
bindingsPrinted = true;
printBindings({ ...withoutStaticAssets, vars: maskedVars });
versionId = parseNonHyphenedUuid(result.deployment_id);
if (config.first_party_worker) {
// Print some useful information returned after publishing
// Not all fields will be populated for every worker
// These fields are likely to be scraped by tools, so do not rename
if (result.id) {
logger.log("Worker ID: ", result.id);
}
if (result.etag) {
logger.log("Worker ETag: ", result.etag);
}
if (result.pipeline_hash) {
logger.log("Worker PipelineHash: ", result.pipeline_hash);
}
if (result.mutable_pipeline_id) {
logger.log(
"Worker Mutable PipelineID (Development ONLY!):",
result.mutable_pipeline_id
);
}
}
} catch (err) {
if (!bindingsPrinted) {
printBindings({ ...withoutStaticAssets, vars: maskedVars });
}
helpIfErrorIsSizeOrScriptStartup(err, dependencies);
// Apply source mapping to validation startup errors if possible
if (
err instanceof APIError &&
"code" in err &&
err.code === 10021 /* validation error */ &&
err.notes.length > 0
) {
err.preventReport();
if (
err.notes[0].text ===
"binding DB of type d1 must have a valid `id` specified [code: 10021]"
) {
throw new UserError(
"You must use a real database in the database_id configuration. You can find your databases using 'wrangler d1 list', or read how to develop locally with D1 here: https://developers.cloudflare.com/d1/configuration/local-development"
);
}
const maybeNameToFilePath = (moduleName: string) => {
// If this is a service worker, always return the entrypoint path.
// Service workers can't have additional JavaScript modules.
if (bundleType === "commonjs") {
return resolvedEntryPointPath;
}
// Similarly, if the name matches the entrypoint, return its path
if (moduleName === entryPointName) {
return resolvedEntryPointPath;
}
// Otherwise, return the file path of the matching module (if any)
for (const module of modules) {
if (moduleName === module.name) {
return module.filePath;
}
}
};
const retrieveSourceMap: RetrieveSourceMapFunction = (moduleName) =>
maybeRetrieveFileSourceMap(maybeNameToFilePath(moduleName));
err.notes[0].text = getSourceMappedString(
err.notes[0].text,
retrieveSourceMap
);
}
throw err;
}
}
} finally {
if (typeof destination !== "string") {
// this means we're using a temp dir,
// so let's clean up before we proceed
destination.remove();
}
}
if (props.dryRun) {
logger.log(`--dry-run: exiting now.`);
return { versionId, workerTag };
}
const uploadMs = Date.now() - start;
logger.log("Uploaded", workerName, formatTime(uploadMs));
// Early exit for WfP since it doesn't need the below code
if (props.dispatchNamespace !== undefined) {
deployWfpUserWorker(props.dispatchNamespace, versionId);
return { versionId, workerTag };
}
// deploy triggers
const targets = await triggersDeploy(props);
logger.log("Current Version ID:", versionId);
return {
sourceMapSize,
versionId,