@cloudflare/deploy-helpers 0.5.1 → 0.6.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.
@@ -0,0 +1,1144 @@
1
+ import { __name } from './chunk-Q72B4Q5Z.mjs';
2
+ import assert from 'node:assert';
3
+ import { readFileSync } from 'node:fs';
4
+ import path, { resolve } from 'node:path';
5
+ import { UserError, INHERIT_SYMBOL } from '@cloudflare/workers-utils';
6
+ import { FormData } from 'undici';
7
+ import { spawnSync } from 'node:child_process';
8
+ import { sync } from 'command-exists';
9
+
10
+ // src/deploy/helpers/binding-utils.ts
11
+ function assertNever(_value) {
12
+ }
13
+ __name(assertNever, "assertNever");
14
+ function convertConfigToBindings(config, options) {
15
+ const { usePreviewIds = false, pages = false } = options ?? {};
16
+ const output = {};
17
+ const configIterable = Object.entries(config);
18
+ for (const [type, info] of configIterable) {
19
+ if (info === void 0) {
20
+ continue;
21
+ }
22
+ switch (type) {
23
+ case "vars": {
24
+ for (const [key, value] of Object.entries(info)) {
25
+ if (typeof value === "string") {
26
+ output[key] = { type: "plain_text", value };
27
+ } else {
28
+ output[key] = { type: "json", value };
29
+ }
30
+ }
31
+ break;
32
+ }
33
+ case "kv_namespaces": {
34
+ for (const { binding, ...x } of info) {
35
+ output[binding] = {
36
+ type: "kv_namespace",
37
+ ...x,
38
+ id: usePreviewIds ? x.preview_id ?? x.id : x.id
39
+ };
40
+ }
41
+ break;
42
+ }
43
+ case "send_email": {
44
+ if (pages) {
45
+ break;
46
+ }
47
+ for (const { name, ...x } of info) {
48
+ output[name] = { type: "send_email", ...x };
49
+ }
50
+ break;
51
+ }
52
+ case "wasm_modules": {
53
+ if (pages) {
54
+ break;
55
+ }
56
+ for (const [key, value] of Object.entries(info)) {
57
+ if (typeof value === "string") {
58
+ output[key] = { type: "wasm_module", source: { path: value } };
59
+ } else {
60
+ output[key] = { type: "wasm_module", source: { contents: value } };
61
+ }
62
+ }
63
+ break;
64
+ }
65
+ case "text_blobs": {
66
+ if (pages) {
67
+ break;
68
+ }
69
+ for (const [key, value] of Object.entries(info)) {
70
+ output[key] = { type: "text_blob", source: { path: value } };
71
+ }
72
+ break;
73
+ }
74
+ case "data_blobs": {
75
+ if (pages) {
76
+ break;
77
+ }
78
+ for (const [key, value] of Object.entries(info)) {
79
+ if (typeof value === "string") {
80
+ output[key] = { type: "data_blob", source: { path: value } };
81
+ } else {
82
+ output[key] = { type: "data_blob", source: { contents: value } };
83
+ }
84
+ }
85
+ break;
86
+ }
87
+ case "browser": {
88
+ const { binding, ...x } = info;
89
+ output[binding] = { type: "browser", ...x };
90
+ break;
91
+ }
92
+ case "durable_objects": {
93
+ for (const { name, ...x } of info.bindings ?? []) {
94
+ output[name] = { type: "durable_object_namespace", ...x };
95
+ }
96
+ break;
97
+ }
98
+ case "workflows": {
99
+ for (const { binding, ...x } of info) {
100
+ output[binding] = { type: "workflow", ...x };
101
+ }
102
+ break;
103
+ }
104
+ case "queues": {
105
+ for (const { binding, ...x } of info.producers ?? []) {
106
+ output[binding] = {
107
+ type: "queue",
108
+ queue_name: x.queue,
109
+ ...x
110
+ };
111
+ }
112
+ break;
113
+ }
114
+ case "r2_buckets": {
115
+ for (const { binding, ...x } of info) {
116
+ output[binding] = {
117
+ type: "r2_bucket",
118
+ ...x,
119
+ bucket_name: usePreviewIds ? x.preview_bucket_name ?? x.bucket_name : x.bucket_name
120
+ };
121
+ }
122
+ break;
123
+ }
124
+ case "d1_databases": {
125
+ for (const { binding, ...x } of info) {
126
+ output[binding] = {
127
+ type: "d1",
128
+ ...x,
129
+ database_id: usePreviewIds ? x.preview_database_id ?? x.database_id : x.database_id
130
+ };
131
+ }
132
+ break;
133
+ }
134
+ case "services": {
135
+ for (const { binding, ...x } of info) {
136
+ output[binding] = { type: "service", ...x };
137
+ }
138
+ break;
139
+ }
140
+ case "analytics_engine_datasets": {
141
+ for (const { binding, ...x } of info) {
142
+ output[binding] = { type: "analytics_engine", ...x };
143
+ }
144
+ break;
145
+ }
146
+ case "dispatch_namespaces": {
147
+ if (pages) {
148
+ break;
149
+ }
150
+ for (const { binding, ...x } of info) {
151
+ output[binding] = { type: "dispatch_namespace", ...x };
152
+ }
153
+ break;
154
+ }
155
+ case "mtls_certificates": {
156
+ for (const { binding, ...x } of info) {
157
+ output[binding] = { type: "mtls_certificate", ...x };
158
+ }
159
+ break;
160
+ }
161
+ case "logfwdr": {
162
+ if (pages) {
163
+ break;
164
+ }
165
+ for (const { name, ...x } of info.bindings ?? []) {
166
+ output[name] = { type: "logfwdr", ...x };
167
+ }
168
+ break;
169
+ }
170
+ case "ai": {
171
+ const { binding, ...x } = info;
172
+ output[binding] = { type: "ai", ...x };
173
+ break;
174
+ }
175
+ case "images": {
176
+ const { binding, ...x } = info;
177
+ output[binding] = { type: "images", ...x };
178
+ break;
179
+ }
180
+ case "stream": {
181
+ const { binding, ...x } = info;
182
+ output[binding] = { type: "stream", ...x };
183
+ break;
184
+ }
185
+ case "version_metadata": {
186
+ const { binding, ...x } = info;
187
+ output[binding] = { type: "version_metadata", ...x };
188
+ break;
189
+ }
190
+ case "hyperdrive": {
191
+ for (const { binding, ...x } of info) {
192
+ output[binding] = { type: "hyperdrive", ...x };
193
+ }
194
+ break;
195
+ }
196
+ case "vectorize": {
197
+ for (const { binding, ...x } of info) {
198
+ output[binding] = { type: "vectorize", ...x };
199
+ }
200
+ break;
201
+ }
202
+ case "ai_search_namespaces": {
203
+ for (const { binding, ...x } of info) {
204
+ output[binding] = { type: "ai_search_namespace", ...x };
205
+ }
206
+ break;
207
+ }
208
+ case "ai_search": {
209
+ for (const { binding, ...x } of info) {
210
+ output[binding] = { type: "ai_search", ...x };
211
+ }
212
+ break;
213
+ }
214
+ case "websearch": {
215
+ const { binding, ...x } = info;
216
+ output[binding] = { type: "websearch", ...x };
217
+ break;
218
+ }
219
+ case "agent_memory": {
220
+ for (const { binding, ...x } of info) {
221
+ output[binding] = { type: "agent_memory", ...x };
222
+ }
223
+ break;
224
+ }
225
+ case "unsafe": {
226
+ if (pages) {
227
+ break;
228
+ }
229
+ for (const { type: unsafeType, name, ...data } of info.bindings ?? []) {
230
+ output[name] = { type: `unsafe_${unsafeType}`, ...data };
231
+ }
232
+ break;
233
+ }
234
+ case "assets": {
235
+ if (pages) {
236
+ break;
237
+ }
238
+ if (info.binding) {
239
+ output[info.binding] = { type: "assets" };
240
+ }
241
+ break;
242
+ }
243
+ case "pipelines": {
244
+ if (pages) {
245
+ break;
246
+ }
247
+ for (const { binding, ...x } of info) {
248
+ output[binding] = { type: "pipeline", ...x };
249
+ }
250
+ break;
251
+ }
252
+ case "secrets_store_secrets": {
253
+ for (const { binding, ...x } of info) {
254
+ output[binding] = { type: "secrets_store_secret", ...x };
255
+ }
256
+ break;
257
+ }
258
+ case "artifacts": {
259
+ for (const { binding, ...x } of info) {
260
+ output[binding] = { type: "artifacts", ...x };
261
+ }
262
+ break;
263
+ }
264
+ case "unsafe_hello_world": {
265
+ if (pages) {
266
+ break;
267
+ }
268
+ for (const { binding, ...x } of info) {
269
+ output[binding] = { type: "unsafe_hello_world", ...x };
270
+ }
271
+ break;
272
+ }
273
+ case "flagship": {
274
+ for (const { binding, ...x } of info) {
275
+ output[binding] = { type: "flagship", ...x };
276
+ }
277
+ break;
278
+ }
279
+ case "ratelimits": {
280
+ for (const { name, ...x } of info) {
281
+ output[name] = { type: "ratelimit", ...x };
282
+ }
283
+ break;
284
+ }
285
+ case "worker_loaders": {
286
+ for (const { binding, ...x } of info) {
287
+ output[binding] = { type: "worker_loader", ...x };
288
+ }
289
+ break;
290
+ }
291
+ case "vpc_services": {
292
+ for (const { binding, ...x } of info) {
293
+ output[binding] = { type: "vpc_service", ...x };
294
+ }
295
+ break;
296
+ }
297
+ case "vpc_networks": {
298
+ for (const { binding, ...x } of info) {
299
+ output[binding] = { type: "vpc_network", ...x };
300
+ }
301
+ break;
302
+ }
303
+ case "media": {
304
+ const { binding, ...x } = info;
305
+ output[binding] = { type: "media", ...x };
306
+ break;
307
+ }
308
+ }
309
+ }
310
+ return output;
311
+ }
312
+ __name(convertConfigToBindings, "convertConfigToBindings");
313
+ function isUnsafeBindingType(type) {
314
+ return type.startsWith("unsafe_");
315
+ }
316
+ __name(isUnsafeBindingType, "isUnsafeBindingType");
317
+ var nameBindings = [
318
+ "durable_object_namespace",
319
+ "logfwdr",
320
+ "ratelimit",
321
+ "unsafe_ratelimit",
322
+ "send_email"
323
+ ];
324
+ function getBindingKey(type) {
325
+ if (nameBindings.includes(type)) {
326
+ return "name";
327
+ }
328
+ return "binding";
329
+ }
330
+ __name(getBindingKey, "getBindingKey");
331
+ function extractBindingsOfType(type, bindings) {
332
+ return Object.entries(bindings ?? {}).filter(
333
+ (binding) => binding[1].type === type
334
+ ).map((binding) => ({
335
+ ...binding[1],
336
+ [getBindingKey(type)]: binding[0]
337
+ }));
338
+ }
339
+ __name(extractBindingsOfType, "extractBindingsOfType");
340
+ function getBindings(config, options) {
341
+ if (!config) {
342
+ return {};
343
+ }
344
+ return convertConfigToBindings(config, {
345
+ usePreviewIds: false,
346
+ pages: options?.pages
347
+ });
348
+ }
349
+ __name(getBindings, "getBindings");
350
+ function handleUnsafeCapnp(capnp) {
351
+ if (capnp.compiled_schema) {
352
+ return readFileSync(resolve(capnp.compiled_schema));
353
+ }
354
+ const { base_path, source_schemas } = capnp;
355
+ const capnpSchemas = (source_schemas ?? []).map(
356
+ (x) => resolve(base_path, x)
357
+ );
358
+ if (!sync("capnp")) {
359
+ throw new UserError(
360
+ "The capnp compiler is required to upload capnp schemas, but is not present.",
361
+ { telemetryMessage: "capnp compiler missing" }
362
+ );
363
+ }
364
+ const srcPrefix = resolve(base_path ?? ".");
365
+ const capnpProcess = spawnSync(
366
+ "capnp",
367
+ ["compile", "-o-", `--src-prefix=${srcPrefix}`, ...capnpSchemas],
368
+ // This number was chosen arbitrarily. If you get ENOBUFS because your compiled schema is still
369
+ // too large, then we may need to bump this again or figure out another approach.
370
+ // https://github.com/cloudflare/workers-sdk/pull/10217
371
+ { maxBuffer: 3 * 1024 * 1024 }
372
+ );
373
+ if (capnpProcess.error) {
374
+ throw capnpProcess.error;
375
+ }
376
+ if (capnpProcess.stderr.length) {
377
+ throw new Error(capnpProcess.stderr.toString());
378
+ }
379
+ return capnpProcess.stdout;
380
+ }
381
+ __name(handleUnsafeCapnp, "handleUnsafeCapnp");
382
+
383
+ // src/deploy/helpers/create-worker-upload-form.ts
384
+ var moduleTypeMimeType = {
385
+ esm: "application/javascript+module",
386
+ commonjs: "application/javascript",
387
+ "compiled-wasm": "application/wasm",
388
+ buffer: "application/octet-stream",
389
+ text: "text/plain",
390
+ python: "text/x-python",
391
+ "python-requirement": "text/x-python-requirement"
392
+ };
393
+ function toMimeType(type) {
394
+ const mimeType = moduleTypeMimeType[type];
395
+ if (mimeType === void 0) {
396
+ throw new TypeError("Unsupported module: " + type);
397
+ }
398
+ return mimeType;
399
+ }
400
+ __name(toMimeType, "toMimeType");
401
+ function fromMimeType(mimeType) {
402
+ const moduleType = Object.keys(moduleTypeMimeType).find(
403
+ (type) => moduleTypeMimeType[type] === mimeType
404
+ );
405
+ if (moduleType === void 0) {
406
+ throw new TypeError("Unsupported mime type: " + mimeType);
407
+ }
408
+ return moduleType;
409
+ }
410
+ __name(fromMimeType, "fromMimeType");
411
+ function createWorkerUploadForm(worker, bindings, options) {
412
+ const formData = new FormData();
413
+ const {
414
+ main,
415
+ sourceMaps,
416
+ migrations,
417
+ exports: configuredExports,
418
+ compatibility_date,
419
+ compatibility_flags,
420
+ keepVars,
421
+ keepSecrets,
422
+ keepBindings,
423
+ logpush,
424
+ placement,
425
+ tail_consumers,
426
+ streaming_tail_consumers,
427
+ limits,
428
+ annotations,
429
+ keep_assets,
430
+ assets,
431
+ observability,
432
+ cache,
433
+ package_dependencies
434
+ } = worker;
435
+ const assetConfig = {
436
+ html_handling: assets?.assetConfig?.html_handling,
437
+ not_found_handling: assets?.assetConfig?.not_found_handling,
438
+ run_worker_first: assets?.run_worker_first,
439
+ _redirects: assets?._redirects,
440
+ _headers: assets?._headers
441
+ };
442
+ if (assets && !assets.routerConfig.has_user_worker) {
443
+ formData.set(
444
+ "metadata",
445
+ JSON.stringify({
446
+ assets: {
447
+ jwt: assets.jwt,
448
+ config: assetConfig
449
+ },
450
+ ...annotations && { annotations },
451
+ ...compatibility_date && { compatibility_date },
452
+ ...compatibility_flags && { compatibility_flags }
453
+ })
454
+ );
455
+ return formData;
456
+ }
457
+ let { modules } = worker;
458
+ const metadataBindings = [];
459
+ const plain_text = extractBindingsOfType("plain_text", bindings);
460
+ const json_bindings = extractBindingsOfType("json", bindings);
461
+ const secret_text = extractBindingsOfType("secret_text", bindings);
462
+ const kv_namespaces = extractBindingsOfType("kv_namespace", bindings);
463
+ const send_email = extractBindingsOfType("send_email", bindings);
464
+ const durable_objects = extractBindingsOfType(
465
+ "durable_object_namespace",
466
+ bindings
467
+ );
468
+ const workflows = extractBindingsOfType("workflow", bindings);
469
+ const queues = extractBindingsOfType("queue", bindings);
470
+ const r2_buckets = extractBindingsOfType("r2_bucket", bindings);
471
+ const d1_databases = extractBindingsOfType("d1", bindings);
472
+ const vectorize = extractBindingsOfType("vectorize", bindings);
473
+ const ai_search_namespaces = extractBindingsOfType(
474
+ "ai_search_namespace",
475
+ bindings
476
+ );
477
+ const ai_search = extractBindingsOfType("ai_search", bindings);
478
+ const websearch = extractBindingsOfType("websearch", bindings)[0];
479
+ const agent_memory = extractBindingsOfType("agent_memory", bindings);
480
+ const hyperdrive = extractBindingsOfType("hyperdrive", bindings);
481
+ const secrets_store_secrets = extractBindingsOfType(
482
+ "secrets_store_secret",
483
+ bindings
484
+ );
485
+ const artifacts = extractBindingsOfType("artifacts", bindings);
486
+ const unsafe_hello_world = extractBindingsOfType(
487
+ "unsafe_hello_world",
488
+ bindings
489
+ );
490
+ const flagship = extractBindingsOfType("flagship", bindings);
491
+ const ratelimits = extractBindingsOfType("ratelimit", bindings);
492
+ const vpc_services = extractBindingsOfType("vpc_service", bindings);
493
+ const vpc_networks = extractBindingsOfType("vpc_network", bindings);
494
+ const services = extractBindingsOfType("service", bindings);
495
+ const analytics_engine_datasets = extractBindingsOfType(
496
+ "analytics_engine",
497
+ bindings
498
+ );
499
+ const dispatch_namespaces = extractBindingsOfType(
500
+ "dispatch_namespace",
501
+ bindings
502
+ );
503
+ const mtls_certificates = extractBindingsOfType("mtls_certificate", bindings);
504
+ const pipelines = extractBindingsOfType("pipeline", bindings);
505
+ const worker_loaders = extractBindingsOfType("worker_loader", bindings);
506
+ const logfwdr = extractBindingsOfType("logfwdr", bindings);
507
+ const wasm_modules = extractBindingsOfType("wasm_module", bindings);
508
+ const browser = extractBindingsOfType("browser", bindings)[0];
509
+ const ai = extractBindingsOfType("ai", bindings)[0];
510
+ const images = extractBindingsOfType("images", bindings)[0];
511
+ const stream = extractBindingsOfType("stream", bindings)[0];
512
+ const media = extractBindingsOfType("media", bindings)[0];
513
+ const version_metadata = extractBindingsOfType(
514
+ "version_metadata",
515
+ bindings
516
+ )[0];
517
+ const assetsBinding = extractBindingsOfType("assets", bindings)[0];
518
+ const text_blobs = extractBindingsOfType("text_blob", bindings);
519
+ const data_blobs = extractBindingsOfType("data_blob", bindings);
520
+ const inherit_bindings = extractBindingsOfType("inherit", bindings);
521
+ inherit_bindings.forEach(({ binding }) => {
522
+ metadataBindings.push({ name: binding, type: "inherit" });
523
+ });
524
+ plain_text.forEach(({ binding, value }) => {
525
+ metadataBindings.push({ name: binding, type: "plain_text", text: value });
526
+ });
527
+ json_bindings.forEach(({ binding, value }) => {
528
+ metadataBindings.push({ name: binding, type: "json", json: value });
529
+ });
530
+ secret_text.forEach(({ binding, value }) => {
531
+ metadataBindings.push({ name: binding, type: "secret_text", text: value });
532
+ });
533
+ kv_namespaces.forEach(({ id, binding, raw }) => {
534
+ if (options?.dryRun) {
535
+ id ??= INHERIT_SYMBOL;
536
+ }
537
+ if (id === void 0) {
538
+ throw new UserError(`${binding} bindings must have an "id" field`, {
539
+ telemetryMessage: "kv namespace binding missing id"
540
+ });
541
+ }
542
+ if (id === INHERIT_SYMBOL) {
543
+ metadataBindings.push({
544
+ name: binding,
545
+ type: "inherit"
546
+ });
547
+ } else {
548
+ metadataBindings.push({
549
+ name: binding,
550
+ type: "kv_namespace",
551
+ namespace_id: id,
552
+ raw
553
+ });
554
+ }
555
+ });
556
+ send_email.forEach((emailBinding) => {
557
+ const destination_address = "destination_address" in emailBinding ? emailBinding.destination_address : void 0;
558
+ const allowed_destination_addresses = "allowed_destination_addresses" in emailBinding ? emailBinding.allowed_destination_addresses : void 0;
559
+ const allowed_sender_addresses = "allowed_sender_addresses" in emailBinding ? emailBinding.allowed_sender_addresses : void 0;
560
+ metadataBindings.push({
561
+ name: emailBinding.name,
562
+ type: "send_email",
563
+ destination_address,
564
+ allowed_destination_addresses,
565
+ allowed_sender_addresses
566
+ });
567
+ });
568
+ durable_objects.forEach(({ name, class_name, script_name, environment }) => {
569
+ metadataBindings.push({
570
+ name,
571
+ type: "durable_object_namespace",
572
+ class_name,
573
+ ...script_name && { script_name },
574
+ ...environment && { environment }
575
+ });
576
+ });
577
+ workflows.forEach(({ binding, name, class_name, script_name, raw }) => {
578
+ metadataBindings.push({
579
+ type: "workflow",
580
+ name: binding,
581
+ workflow_name: name,
582
+ class_name,
583
+ script_name,
584
+ raw
585
+ });
586
+ });
587
+ queues.forEach(({ binding, queue_name, delivery_delay, raw }) => {
588
+ if (options?.dryRun) {
589
+ queue_name ??= INHERIT_SYMBOL;
590
+ }
591
+ if (queue_name === void 0) {
592
+ throw new UserError(
593
+ `${binding} bindings must have a "queue" field. Add a "queue" field to the producer in your Worker configuration specifying the name of the Queue to bind to.`,
594
+ {
595
+ telemetryMessage: "queue binding missing name"
596
+ }
597
+ );
598
+ }
599
+ if (queue_name === INHERIT_SYMBOL) {
600
+ metadataBindings.push({ name: binding, type: "inherit" });
601
+ } else {
602
+ metadataBindings.push({
603
+ type: "queue",
604
+ name: binding,
605
+ queue_name,
606
+ delivery_delay,
607
+ raw
608
+ });
609
+ }
610
+ });
611
+ r2_buckets.forEach(({ binding, bucket_name, jurisdiction, raw }) => {
612
+ if (options?.dryRun) {
613
+ bucket_name ??= INHERIT_SYMBOL;
614
+ }
615
+ if (bucket_name === void 0) {
616
+ throw new UserError(
617
+ `${binding} bindings must have a "bucket_name" field. Add a "bucket_name" field to the binding in your Worker configuration specifying the name of the R2 bucket to bind to.`,
618
+ { telemetryMessage: "r2 bucket binding missing bucket_name" }
619
+ );
620
+ }
621
+ if (bucket_name === INHERIT_SYMBOL) {
622
+ metadataBindings.push({
623
+ name: binding,
624
+ type: "inherit"
625
+ });
626
+ } else {
627
+ metadataBindings.push({
628
+ name: binding,
629
+ type: "r2_bucket",
630
+ bucket_name,
631
+ jurisdiction,
632
+ raw
633
+ });
634
+ }
635
+ });
636
+ d1_databases.forEach(
637
+ ({ binding, database_id, database_internal_env, raw }) => {
638
+ if (options?.dryRun) {
639
+ database_id ??= INHERIT_SYMBOL;
640
+ }
641
+ if (database_id === void 0) {
642
+ throw new UserError(
643
+ `${binding} bindings must have a "database_id" field. Add a "database_id" field to the binding in your Worker configuration specifying the ID of the D1 database to bind to.`,
644
+ { telemetryMessage: "d1 database binding missing database_id" }
645
+ );
646
+ }
647
+ if (database_id === INHERIT_SYMBOL) {
648
+ metadataBindings.push({
649
+ name: binding,
650
+ type: "inherit"
651
+ });
652
+ } else {
653
+ metadataBindings.push({
654
+ name: binding,
655
+ type: "d1",
656
+ id: database_id,
657
+ internalEnv: database_internal_env,
658
+ raw
659
+ });
660
+ }
661
+ }
662
+ );
663
+ vectorize.forEach(({ binding, index_name, raw }) => {
664
+ metadataBindings.push({
665
+ name: binding,
666
+ type: "vectorize",
667
+ index_name,
668
+ raw
669
+ });
670
+ });
671
+ ai_search_namespaces.forEach(({ binding, namespace }) => {
672
+ if (options?.dryRun) {
673
+ namespace ??= INHERIT_SYMBOL;
674
+ }
675
+ if (namespace === void 0) {
676
+ throw new UserError(
677
+ `${binding} bindings must have a "namespace" field. Add a "namespace" field to the binding in your Worker configuration specifying the AI Search namespace to bind to.`,
678
+ {
679
+ telemetryMessage: "ai search namespace binding missing namespace"
680
+ }
681
+ );
682
+ }
683
+ if (namespace === INHERIT_SYMBOL) {
684
+ metadataBindings.push({
685
+ name: binding,
686
+ type: "inherit"
687
+ });
688
+ } else {
689
+ metadataBindings.push({
690
+ name: binding,
691
+ type: "ai_search_namespace",
692
+ namespace
693
+ });
694
+ }
695
+ });
696
+ ai_search.forEach(({ binding, instance_name }) => {
697
+ metadataBindings.push({
698
+ name: binding,
699
+ type: "ai_search",
700
+ instance_name
701
+ });
702
+ });
703
+ if (websearch !== void 0) {
704
+ metadataBindings.push({
705
+ name: websearch.binding,
706
+ type: "websearch"
707
+ });
708
+ }
709
+ agent_memory.forEach(({ binding, namespace }) => {
710
+ if (options?.dryRun) {
711
+ namespace ??= INHERIT_SYMBOL;
712
+ }
713
+ if (namespace === void 0) {
714
+ throw new UserError(
715
+ `${binding} bindings must have a "namespace" field. Add a "namespace" field to the binding in your Worker configuration specifying the namespace to bind to.`,
716
+ {
717
+ telemetryMessage: false
718
+ }
719
+ );
720
+ }
721
+ if (namespace === INHERIT_SYMBOL) {
722
+ metadataBindings.push({
723
+ name: binding,
724
+ type: "inherit"
725
+ });
726
+ } else {
727
+ metadataBindings.push({
728
+ name: binding,
729
+ type: "agent_memory",
730
+ namespace
731
+ });
732
+ }
733
+ });
734
+ hyperdrive.forEach(({ binding, id }) => {
735
+ metadataBindings.push({
736
+ name: binding,
737
+ type: "hyperdrive",
738
+ id
739
+ });
740
+ });
741
+ secrets_store_secrets.forEach(({ binding, store_id, secret_name }) => {
742
+ metadataBindings.push({
743
+ name: binding,
744
+ type: "secrets_store_secret",
745
+ store_id,
746
+ secret_name
747
+ });
748
+ });
749
+ artifacts.forEach(({ binding, namespace }) => {
750
+ metadataBindings.push({
751
+ name: binding,
752
+ type: "artifacts",
753
+ namespace
754
+ });
755
+ });
756
+ unsafe_hello_world.forEach(({ binding, enable_timer }) => {
757
+ metadataBindings.push({
758
+ name: binding,
759
+ type: "unsafe_hello_world",
760
+ enable_timer
761
+ });
762
+ });
763
+ flagship.forEach(({ binding, app_id }) => {
764
+ if (options?.dryRun) {
765
+ app_id ??= INHERIT_SYMBOL;
766
+ }
767
+ if (app_id === void 0) {
768
+ throw new UserError(
769
+ `${binding} bindings must have an "app_id" field. Add an "app_id" field to the binding in your Worker configuration specifying the Flagship app to bind to.`,
770
+ {
771
+ telemetryMessage: "flagship binding missing app id"
772
+ }
773
+ );
774
+ }
775
+ metadataBindings.push(
776
+ app_id === INHERIT_SYMBOL ? { name: binding, type: "inherit" } : { name: binding, type: "flagship", app_id }
777
+ );
778
+ });
779
+ ratelimits.forEach(({ name, namespace_id, simple }) => {
780
+ metadataBindings.push({
781
+ name,
782
+ type: "ratelimit",
783
+ namespace_id,
784
+ simple
785
+ });
786
+ });
787
+ vpc_services.forEach(({ binding, service_id }) => {
788
+ metadataBindings.push({
789
+ name: binding,
790
+ type: "vpc_service",
791
+ service_id
792
+ });
793
+ });
794
+ vpc_networks.forEach(({ binding, tunnel_id, network_id }) => {
795
+ metadataBindings.push({
796
+ name: binding,
797
+ type: "vpc_network",
798
+ ...tunnel_id !== void 0 ? { tunnel_id } : { network_id }
799
+ });
800
+ });
801
+ services.forEach(
802
+ ({
803
+ binding,
804
+ service,
805
+ environment,
806
+ entrypoint,
807
+ props,
808
+ cross_account_grant
809
+ }) => {
810
+ metadataBindings.push({
811
+ name: binding,
812
+ type: "service",
813
+ service,
814
+ cross_account_grant,
815
+ ...environment && { environment },
816
+ ...entrypoint && { entrypoint },
817
+ ...props && { props }
818
+ });
819
+ }
820
+ );
821
+ analytics_engine_datasets.forEach(({ binding, dataset }) => {
822
+ metadataBindings.push({
823
+ name: binding,
824
+ type: "analytics_engine",
825
+ dataset
826
+ });
827
+ });
828
+ dispatch_namespaces.forEach(({ binding, namespace, outbound }) => {
829
+ if (options?.dryRun) {
830
+ namespace ??= INHERIT_SYMBOL;
831
+ }
832
+ if (namespace === void 0) {
833
+ throw new UserError(
834
+ `${binding} bindings must have a "namespace" field. Add a "namespace" field to the binding in your Worker configuration specifying the dispatch namespace to bind to.`,
835
+ {
836
+ telemetryMessage: "dispatch namespace binding missing namespace"
837
+ }
838
+ );
839
+ }
840
+ if (namespace === INHERIT_SYMBOL) {
841
+ metadataBindings.push({ name: binding, type: "inherit" });
842
+ } else {
843
+ metadataBindings.push({
844
+ name: binding,
845
+ type: "dispatch_namespace",
846
+ namespace,
847
+ ...outbound && {
848
+ outbound: {
849
+ worker: {
850
+ service: outbound.service,
851
+ environment: outbound.environment
852
+ },
853
+ params: outbound.parameters?.map((p) => ({ name: p }))
854
+ }
855
+ }
856
+ });
857
+ }
858
+ });
859
+ mtls_certificates.forEach(({ binding, certificate_id }) => {
860
+ metadataBindings.push({
861
+ name: binding,
862
+ type: "mtls_certificate",
863
+ certificate_id
864
+ });
865
+ });
866
+ pipelines.forEach(({ binding, stream: pipelineStream, pipeline }) => {
867
+ if (pipelineStream) {
868
+ metadataBindings.push({
869
+ name: binding,
870
+ type: "pipelines",
871
+ stream: pipelineStream
872
+ });
873
+ } else if (pipeline) {
874
+ metadataBindings.push({
875
+ name: binding,
876
+ type: "pipelines",
877
+ pipeline
878
+ });
879
+ } else {
880
+ throw new Error("Pipeline binding must specify a stream or pipeline");
881
+ }
882
+ });
883
+ worker_loaders.forEach(({ binding }) => {
884
+ metadataBindings.push({
885
+ name: binding,
886
+ type: "worker_loader"
887
+ });
888
+ });
889
+ logfwdr.forEach(({ name, destination }) => {
890
+ metadataBindings.push({
891
+ name,
892
+ type: "logfwdr",
893
+ destination
894
+ });
895
+ });
896
+ wasm_modules.forEach(({ binding: name, source }) => {
897
+ metadataBindings.push({
898
+ name,
899
+ type: "wasm_module",
900
+ part: name
901
+ });
902
+ formData.set(
903
+ name,
904
+ new File(
905
+ [
906
+ "contents" in source ? source.contents : readFileSync(source.path)
907
+ ],
908
+ "path" in source ? source.path ?? name : name,
909
+ { type: "application/wasm" }
910
+ )
911
+ );
912
+ });
913
+ if (browser !== void 0) {
914
+ metadataBindings.push({
915
+ name: browser.binding,
916
+ type: "browser",
917
+ raw: browser.raw
918
+ });
919
+ }
920
+ if (ai !== void 0) {
921
+ metadataBindings.push({
922
+ name: ai.binding,
923
+ staging: ai.staging,
924
+ type: "ai",
925
+ raw: ai.raw
926
+ });
927
+ }
928
+ if (images !== void 0) {
929
+ metadataBindings.push({
930
+ name: images.binding,
931
+ type: "images",
932
+ raw: images.raw
933
+ });
934
+ }
935
+ if (stream !== void 0) {
936
+ metadataBindings.push({
937
+ name: stream.binding,
938
+ type: "stream"
939
+ });
940
+ }
941
+ if (media !== void 0) {
942
+ metadataBindings.push({
943
+ name: media.binding,
944
+ type: "media"
945
+ });
946
+ }
947
+ if (version_metadata !== void 0) {
948
+ metadataBindings.push({
949
+ name: version_metadata.binding,
950
+ type: "version_metadata"
951
+ });
952
+ }
953
+ if (assetsBinding !== void 0) {
954
+ metadataBindings.push({
955
+ name: assetsBinding.binding,
956
+ type: "assets"
957
+ });
958
+ }
959
+ text_blobs.forEach(({ binding: name, source }) => {
960
+ metadataBindings.push({
961
+ name,
962
+ type: "text_blob",
963
+ part: name
964
+ });
965
+ if (name !== "__STATIC_CONTENT_MANIFEST") {
966
+ if ("contents" in source) {
967
+ formData.set(
968
+ name,
969
+ new File([source.contents], source.path ?? name, {
970
+ type: "text/plain"
971
+ })
972
+ );
973
+ } else {
974
+ formData.set(
975
+ name,
976
+ new File([readFileSync(source.path)], source.path, {
977
+ type: "text/plain"
978
+ })
979
+ );
980
+ }
981
+ }
982
+ });
983
+ data_blobs.forEach(({ binding: name, source }) => {
984
+ metadataBindings.push({
985
+ name,
986
+ type: "data_blob",
987
+ part: name
988
+ });
989
+ formData.set(
990
+ name,
991
+ new File(
992
+ [
993
+ "contents" in source ? source.contents : readFileSync(source.path)
994
+ ],
995
+ "path" in source ? source.path ?? name : name,
996
+ { type: "application/octet-stream" }
997
+ )
998
+ );
999
+ });
1000
+ for (const [bindingName, config] of Object.entries(bindings ?? {})) {
1001
+ if (isUnsafeBindingType(config.type) && config.type !== "unsafe_hello_world") {
1002
+ const { type, ...data } = config;
1003
+ metadataBindings.push({
1004
+ name: bindingName,
1005
+ type: type.slice("unsafe_".length),
1006
+ ...data
1007
+ });
1008
+ }
1009
+ }
1010
+ const manifestModuleName = "__STATIC_CONTENT_MANIFEST";
1011
+ const hasManifest = modules?.some(({ name }) => name === manifestModuleName);
1012
+ if (hasManifest && main.type === "esm") {
1013
+ assert(modules !== void 0);
1014
+ const subDirs = new Set(
1015
+ modules.map((module) => path.posix.dirname(module.name))
1016
+ );
1017
+ for (const subDir of subDirs) {
1018
+ if (subDir === ".") {
1019
+ continue;
1020
+ }
1021
+ const relativePath = path.posix.relative(subDir, manifestModuleName);
1022
+ const filePath = path.posix.join(subDir, manifestModuleName);
1023
+ modules.push({
1024
+ name: filePath,
1025
+ filePath,
1026
+ content: `export { default } from ${JSON.stringify(relativePath)};`,
1027
+ type: "esm"
1028
+ });
1029
+ }
1030
+ }
1031
+ if (main.type === "commonjs") {
1032
+ for (const module of Object.values([...modules || []])) {
1033
+ if (module.name === "__STATIC_CONTENT_MANIFEST") {
1034
+ formData.set(
1035
+ module.name,
1036
+ new File([module.content], module.name, {
1037
+ type: "text/plain"
1038
+ })
1039
+ );
1040
+ modules = modules?.filter((m) => m !== module);
1041
+ } else if (module.type === "compiled-wasm" || module.type === "text" || module.type === "buffer") {
1042
+ const name = module.name.replace(/[^a-zA-Z0-9_$]/g, "_");
1043
+ metadataBindings.push({
1044
+ name,
1045
+ type: module.type === "compiled-wasm" ? "wasm_module" : module.type === "text" ? "text_blob" : "data_blob",
1046
+ part: name
1047
+ });
1048
+ formData.set(
1049
+ name,
1050
+ new File([module.content], module.name, {
1051
+ type: module.type === "compiled-wasm" ? "application/wasm" : module.type === "text" ? "text/plain" : "application/octet-stream"
1052
+ })
1053
+ );
1054
+ modules = modules?.filter((m) => m !== module);
1055
+ }
1056
+ }
1057
+ }
1058
+ let capnpSchemaOutputFile;
1059
+ if (options?.unsafe?.capnp) {
1060
+ const capnpOutput = handleUnsafeCapnp(options.unsafe.capnp);
1061
+ capnpSchemaOutputFile = `./capnp-${Date.now()}.compiled`;
1062
+ formData.set(
1063
+ capnpSchemaOutputFile,
1064
+ new File([capnpOutput], capnpSchemaOutputFile, {
1065
+ type: "application/octet-stream"
1066
+ })
1067
+ );
1068
+ }
1069
+ let keep_bindings = void 0;
1070
+ if (keepVars) {
1071
+ keep_bindings ??= [];
1072
+ keep_bindings.push("plain_text", "json");
1073
+ }
1074
+ if (keepSecrets) {
1075
+ keep_bindings ??= [];
1076
+ keep_bindings.push("secret_text", "secret_key");
1077
+ }
1078
+ if (keepBindings) {
1079
+ keep_bindings ??= [];
1080
+ keep_bindings.push(...keepBindings);
1081
+ }
1082
+ const metadata = {
1083
+ ...main.type !== "commonjs" ? { main_module: main.name } : { body_part: main.name },
1084
+ bindings: metadataBindings,
1085
+ containers: worker.containers === void 0 ? void 0 : worker.containers.map((c) => ({ class_name: c.class_name })),
1086
+ ...compatibility_date && { compatibility_date },
1087
+ ...compatibility_flags && {
1088
+ compatibility_flags
1089
+ },
1090
+ ...migrations && { migrations },
1091
+ ...configuredExports && Object.keys(configuredExports).length > 0 && {
1092
+ exports: configuredExports
1093
+ },
1094
+ capnp_schema: capnpSchemaOutputFile,
1095
+ ...keep_bindings && { keep_bindings },
1096
+ ...logpush !== void 0 && { logpush },
1097
+ ...placement && { placement },
1098
+ ...tail_consumers && { tail_consumers },
1099
+ ...streaming_tail_consumers && { streaming_tail_consumers },
1100
+ ...limits && { limits },
1101
+ ...annotations && { annotations },
1102
+ ...keep_assets !== void 0 && { keep_assets },
1103
+ ...assets && {
1104
+ assets: {
1105
+ jwt: assets.jwt,
1106
+ config: assetConfig
1107
+ }
1108
+ },
1109
+ ...observability && { observability },
1110
+ ...cache && { cache_options: cache },
1111
+ ...package_dependencies?.length && { package_dependencies }
1112
+ };
1113
+ if (options?.unsafe?.metadata !== void 0) {
1114
+ for (const key of Object.keys(options.unsafe.metadata)) {
1115
+ metadata[key] = options.unsafe.metadata[key];
1116
+ }
1117
+ }
1118
+ formData.set("metadata", JSON.stringify(metadata));
1119
+ if (main.type === "commonjs" && modules && modules.length > 0) {
1120
+ throw new TypeError(
1121
+ "More than one module can only be specified when type = 'esm'"
1122
+ );
1123
+ }
1124
+ for (const module of [main].concat(modules || [])) {
1125
+ formData.set(
1126
+ module.name,
1127
+ new File([module.content], module.name, {
1128
+ type: toMimeType(module.type ?? main.type ?? "esm")
1129
+ })
1130
+ );
1131
+ }
1132
+ for (const sourceMap of sourceMaps || []) {
1133
+ formData.set(
1134
+ sourceMap.name,
1135
+ new File([sourceMap.content], sourceMap.name, {
1136
+ type: "application/source-map"
1137
+ })
1138
+ );
1139
+ }
1140
+ return formData;
1141
+ }
1142
+ __name(createWorkerUploadForm, "createWorkerUploadForm");
1143
+
1144
+ export { convertConfigToBindings, createWorkerUploadForm, extractBindingsOfType, fromMimeType, getBindings, handleUnsafeCapnp, isUnsafeBindingType, moduleTypeMimeType };