@cloudflare/workers-utils 0.0.2 → 0.1.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,3 @@
1
+ export { g as constructWranglerConfig } from './browser-BbMtmAbH.mjs';
2
+ import '@cloudflare/workers-shared';
3
+ import 'cloudflare';
@@ -0,0 +1 @@
1
+ export { constructWranglerConfig } from './chunk-LFBFF6MG.mjs';
@@ -0,0 +1,533 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
8
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
9
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
10
+ }) : x)(function(x) {
11
+ if (typeof require !== "undefined") return require.apply(this, arguments);
12
+ throw Error('Dynamic require of "' + x + '" is not supported');
13
+ });
14
+ var __commonJS = (cb, mod) => function __require2() {
15
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
16
+ };
17
+ var __copyProps = (to, from, except, desc) => {
18
+ if (from && typeof from === "object" || typeof from === "function") {
19
+ for (let key of __getOwnPropNames(from))
20
+ if (!__hasOwnProp.call(to, key) && key !== except)
21
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
22
+ }
23
+ return to;
24
+ };
25
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
26
+ // If the importer is in node compatibility mode or this is not an ESM
27
+ // file that has been converted to a CommonJS file using a Babel-
28
+ // compatible transform (i.e. "__esModule" has not been set), then set
29
+ // "default" to the CommonJS "module.exports" for node compatibility.
30
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
31
+ mod
32
+ ));
33
+
34
+ // src/constants.ts
35
+ var INHERIT_SYMBOL = Symbol.for("inherit_binding");
36
+ var SERVICE_TAG_PREFIX = "cf:service=";
37
+ var ENVIRONMENT_TAG_PREFIX = "cf:environment=";
38
+ var PATH_TO_DEPLOY_CONFIG = ".wrangler/deploy/config.json";
39
+
40
+ // src/format-compatibility-date.ts
41
+ function formatCompatibilityDate(date) {
42
+ return date.toISOString().slice(0, 10);
43
+ }
44
+ __name(formatCompatibilityDate, "formatCompatibilityDate");
45
+
46
+ // src/assert-never.ts
47
+ function assertNever(_value) {
48
+ }
49
+ __name(assertNever, "assertNever");
50
+
51
+ // src/errors.ts
52
+ var UserError = class extends Error {
53
+ static {
54
+ __name(this, "UserError");
55
+ }
56
+ telemetryMessage;
57
+ constructor(message, options) {
58
+ super(message, options);
59
+ Object.setPrototypeOf(this, new.target.prototype);
60
+ this.telemetryMessage = options?.telemetryMessage === true ? message : options?.telemetryMessage;
61
+ }
62
+ };
63
+ var DeprecationError = class extends UserError {
64
+ static {
65
+ __name(this, "DeprecationError");
66
+ }
67
+ constructor(message, options) {
68
+ super(`Deprecation:
69
+ ${message}`, options);
70
+ }
71
+ };
72
+ var FatalError = class extends UserError {
73
+ constructor(message, code, options) {
74
+ super(message, options);
75
+ this.code = code;
76
+ }
77
+ static {
78
+ __name(this, "FatalError");
79
+ }
80
+ };
81
+ var CommandLineArgsError = class extends UserError {
82
+ static {
83
+ __name(this, "CommandLineArgsError");
84
+ }
85
+ };
86
+ var JsonFriendlyFatalError = class extends FatalError {
87
+ constructor(message, code, options) {
88
+ super(message, code, options);
89
+ this.code = code;
90
+ }
91
+ static {
92
+ __name(this, "JsonFriendlyFatalError");
93
+ }
94
+ };
95
+ var MissingConfigError = class extends Error {
96
+ static {
97
+ __name(this, "MissingConfigError");
98
+ }
99
+ telemetryMessage;
100
+ constructor(key) {
101
+ super(`Missing config value for ${key}`);
102
+ this.telemetryMessage = `Missing config value for ${key}`;
103
+ }
104
+ };
105
+ function createFatalError(message, isJson, code, telemetryMessage) {
106
+ if (isJson) {
107
+ return new JsonFriendlyFatalError(
108
+ JSON.stringify(message),
109
+ code,
110
+ telemetryMessage
111
+ );
112
+ } else {
113
+ return new FatalError(`${message}`, code, telemetryMessage);
114
+ }
115
+ }
116
+ __name(createFatalError, "createFatalError");
117
+
118
+ // src/map-worker-metadata-bindings.ts
119
+ function mapWorkerMetadataBindings(bindings) {
120
+ return bindings.filter((binding) => binding.type !== "secret_text").reduce((configObj, binding) => {
121
+ switch (binding.type) {
122
+ case "plain_text":
123
+ {
124
+ configObj.vars = {
125
+ ...configObj.vars ?? {},
126
+ [binding.name]: binding.text
127
+ };
128
+ }
129
+ break;
130
+ case "json":
131
+ {
132
+ configObj.vars = {
133
+ ...configObj.vars ?? {},
134
+ name: binding.name,
135
+ json: binding.json
136
+ };
137
+ }
138
+ break;
139
+ case "kv_namespace":
140
+ {
141
+ configObj.kv_namespaces = [
142
+ ...configObj.kv_namespaces ?? [],
143
+ { id: binding.namespace_id, binding: binding.name }
144
+ ];
145
+ }
146
+ break;
147
+ case "durable_object_namespace":
148
+ {
149
+ configObj.durable_objects = {
150
+ bindings: [
151
+ ...configObj.durable_objects?.bindings ?? [],
152
+ {
153
+ name: binding.name,
154
+ class_name: binding.class_name,
155
+ script_name: binding.script_name,
156
+ environment: binding.environment
157
+ }
158
+ ]
159
+ };
160
+ }
161
+ break;
162
+ case "d1":
163
+ {
164
+ configObj.d1_databases = [
165
+ ...configObj.d1_databases ?? [],
166
+ {
167
+ binding: binding.name,
168
+ database_id: binding.id
169
+ }
170
+ ];
171
+ }
172
+ break;
173
+ case "browser":
174
+ {
175
+ configObj.browser = {
176
+ binding: binding.name
177
+ };
178
+ }
179
+ break;
180
+ case "ai":
181
+ {
182
+ configObj.ai = {
183
+ binding: binding.name
184
+ };
185
+ }
186
+ break;
187
+ case "images":
188
+ {
189
+ configObj.images = {
190
+ binding: binding.name
191
+ };
192
+ }
193
+ break;
194
+ case "media":
195
+ {
196
+ configObj.media = {
197
+ binding: binding.name
198
+ };
199
+ }
200
+ break;
201
+ case "r2_bucket":
202
+ {
203
+ configObj.r2_buckets = [
204
+ ...configObj.r2_buckets ?? [],
205
+ {
206
+ binding: binding.name,
207
+ bucket_name: binding.bucket_name,
208
+ jurisdiction: binding.jurisdiction
209
+ }
210
+ ];
211
+ }
212
+ break;
213
+ case "secrets_store_secret":
214
+ {
215
+ configObj.secrets_store_secrets = [
216
+ ...configObj.secrets_store_secrets ?? [],
217
+ {
218
+ binding: binding.name,
219
+ store_id: binding.store_id,
220
+ secret_name: binding.secret_name
221
+ }
222
+ ];
223
+ }
224
+ break;
225
+ case "unsafe_hello_world": {
226
+ configObj.unsafe_hello_world = [
227
+ ...configObj.unsafe_hello_world ?? [],
228
+ {
229
+ binding: binding.name,
230
+ enable_timer: binding.enable_timer
231
+ }
232
+ ];
233
+ break;
234
+ }
235
+ case "service":
236
+ {
237
+ configObj.services = [
238
+ ...configObj.services ?? [],
239
+ {
240
+ binding: binding.name,
241
+ service: binding.service,
242
+ environment: binding.environment,
243
+ entrypoint: binding.entrypoint
244
+ }
245
+ ];
246
+ }
247
+ break;
248
+ case "analytics_engine":
249
+ {
250
+ configObj.analytics_engine_datasets = [
251
+ ...configObj.analytics_engine_datasets ?? [],
252
+ { binding: binding.name, dataset: binding.dataset }
253
+ ];
254
+ }
255
+ break;
256
+ case "dispatch_namespace":
257
+ {
258
+ configObj.dispatch_namespaces = [
259
+ ...configObj.dispatch_namespaces ?? [],
260
+ {
261
+ binding: binding.name,
262
+ namespace: binding.namespace,
263
+ ...binding.outbound && {
264
+ outbound: {
265
+ service: binding.outbound.worker.service,
266
+ environment: binding.outbound.worker.environment,
267
+ parameters: binding.outbound.params?.map((p) => p.name) ?? []
268
+ }
269
+ }
270
+ }
271
+ ];
272
+ }
273
+ break;
274
+ case "logfwdr":
275
+ {
276
+ configObj.logfwdr = {
277
+ bindings: [
278
+ ...configObj.logfwdr?.bindings ?? [],
279
+ { name: binding.name, destination: binding.destination }
280
+ ]
281
+ };
282
+ }
283
+ break;
284
+ case "wasm_module":
285
+ {
286
+ configObj.wasm_modules = {
287
+ ...configObj.wasm_modules ?? {},
288
+ [binding.name]: binding.part
289
+ };
290
+ }
291
+ break;
292
+ case "text_blob":
293
+ {
294
+ configObj.text_blobs = {
295
+ ...configObj.text_blobs ?? {},
296
+ [binding.name]: binding.part
297
+ };
298
+ }
299
+ break;
300
+ case "data_blob":
301
+ {
302
+ configObj.data_blobs = {
303
+ ...configObj.data_blobs ?? {},
304
+ [binding.name]: binding.part
305
+ };
306
+ }
307
+ break;
308
+ case "secret_text":
309
+ break;
310
+ case "version_metadata": {
311
+ {
312
+ configObj.version_metadata = {
313
+ binding: binding.name
314
+ };
315
+ }
316
+ break;
317
+ }
318
+ case "send_email": {
319
+ configObj.send_email = [
320
+ ...configObj.send_email ?? [],
321
+ {
322
+ name: binding.name,
323
+ destination_address: binding.destination_address,
324
+ allowed_destination_addresses: binding.allowed_destination_addresses,
325
+ allowed_sender_addresses: binding.allowed_sender_addresses
326
+ }
327
+ ];
328
+ break;
329
+ }
330
+ case "queue":
331
+ configObj.queues ??= { producers: [] };
332
+ configObj.queues.producers = [
333
+ ...configObj.queues.producers ?? [],
334
+ {
335
+ binding: binding.name,
336
+ queue: binding.queue_name,
337
+ delivery_delay: binding.delivery_delay
338
+ }
339
+ ];
340
+ break;
341
+ case "vectorize":
342
+ configObj.vectorize = [
343
+ ...configObj.vectorize ?? [],
344
+ {
345
+ binding: binding.name,
346
+ index_name: binding.index_name
347
+ }
348
+ ];
349
+ break;
350
+ case "hyperdrive":
351
+ configObj.hyperdrive = [
352
+ ...configObj.hyperdrive ?? [],
353
+ {
354
+ binding: binding.name,
355
+ id: binding.id
356
+ }
357
+ ];
358
+ break;
359
+ case "mtls_certificate":
360
+ configObj.mtls_certificates = [
361
+ ...configObj.mtls_certificates ?? [],
362
+ {
363
+ binding: binding.name,
364
+ certificate_id: binding.certificate_id
365
+ }
366
+ ];
367
+ break;
368
+ case "pipelines":
369
+ configObj.pipelines = [
370
+ ...configObj.pipelines ?? [],
371
+ {
372
+ binding: binding.name,
373
+ pipeline: binding.pipeline
374
+ }
375
+ ];
376
+ break;
377
+ case "assets":
378
+ throw new FatalError(
379
+ "`wrangler init --from-dash` is not yet supported for Workers with Assets"
380
+ );
381
+ case "inherit":
382
+ configObj.unsafe = {
383
+ bindings: [...configObj.unsafe?.bindings ?? [], binding],
384
+ metadata: configObj.unsafe?.metadata ?? void 0
385
+ };
386
+ break;
387
+ case "workflow":
388
+ {
389
+ configObj.workflows = [
390
+ ...configObj.workflows ?? [],
391
+ {
392
+ binding: binding.name,
393
+ name: binding.workflow_name,
394
+ class_name: binding.class_name,
395
+ script_name: binding.script_name
396
+ }
397
+ ];
398
+ }
399
+ break;
400
+ case "worker_loader":
401
+ {
402
+ configObj.worker_loaders = [
403
+ ...configObj.worker_loaders ?? [],
404
+ {
405
+ binding: binding.name
406
+ }
407
+ ];
408
+ }
409
+ break;
410
+ case "ratelimit":
411
+ {
412
+ configObj.ratelimits = [
413
+ ...configObj.ratelimits ?? [],
414
+ {
415
+ name: binding.name,
416
+ namespace_id: binding.namespace_id,
417
+ simple: {
418
+ limit: binding.simple.limit,
419
+ period: binding.simple.period
420
+ }
421
+ }
422
+ ];
423
+ }
424
+ break;
425
+ case "vpc_service":
426
+ {
427
+ configObj.vpc_services = [
428
+ ...configObj.vpc_services ?? [],
429
+ {
430
+ binding: binding.name,
431
+ service_id: binding.service_id
432
+ }
433
+ ];
434
+ }
435
+ break;
436
+ default: {
437
+ configObj.unsafe = {
438
+ bindings: [...configObj.unsafe?.bindings ?? [], binding],
439
+ metadata: configObj.unsafe?.metadata ?? void 0
440
+ };
441
+ }
442
+ }
443
+ return configObj;
444
+ }, {});
445
+ }
446
+ __name(mapWorkerMetadataBindings, "mapWorkerMetadataBindings");
447
+
448
+ // src/construct-wrangler-config.ts
449
+ function convertWorkerToWranglerConfig(config) {
450
+ const mappedBindings = mapWorkerMetadataBindings(config.bindings);
451
+ const durableObjectClassNames = config.bindings.filter(
452
+ (binding) => binding.type === "durable_object_namespace" && binding.script_name === config.name
453
+ ).map(
454
+ (durableObject) => durableObject.class_name
455
+ );
456
+ const allRoutes = [
457
+ ...config.routes.map((r) => ({
458
+ pattern: r.pattern,
459
+ zone_name: r.zone_name
460
+ })),
461
+ ...config.domains.map((c) => ({
462
+ pattern: c.hostname,
463
+ zone_name: c.zone_name,
464
+ custom_domain: true
465
+ }))
466
+ ];
467
+ return {
468
+ name: config.name,
469
+ main: config.entrypoint,
470
+ workers_dev: config.subdomain.enabled,
471
+ preview_urls: config.subdomain.previews_enabled,
472
+ compatibility_date: config.compatibility_date ?? formatCompatibilityDate(/* @__PURE__ */ new Date()),
473
+ compatibility_flags: config.compatibility_flags,
474
+ ...allRoutes.length ? { routes: allRoutes } : {},
475
+ placement: config.placement?.mode === "smart" ? { mode: "smart" } : void 0,
476
+ limits: config.limits,
477
+ ...durableObjectClassNames.length && config.migration_tag ? {
478
+ migrations: [
479
+ {
480
+ tag: config.migration_tag,
481
+ new_classes: durableObjectClassNames
482
+ }
483
+ ]
484
+ } : {},
485
+ ...config.schedules.length ? {
486
+ triggers: {
487
+ crons: config.schedules.map((scheduled) => scheduled.cron)
488
+ }
489
+ } : {},
490
+ tail_consumers: config.tail_consumers,
491
+ observability: config.observability,
492
+ ...mappedBindings
493
+ };
494
+ }
495
+ __name(convertWorkerToWranglerConfig, "convertWorkerToWranglerConfig");
496
+ function constructWranglerConfig(workerOrWorkers) {
497
+ let workers;
498
+ if (Array.isArray(workerOrWorkers)) {
499
+ workers = workerOrWorkers;
500
+ } else {
501
+ workers = [workerOrWorkers];
502
+ }
503
+ const topLevelEnv = workers.find(
504
+ (w) => !w.tags?.some((t) => t.startsWith(ENVIRONMENT_TAG_PREFIX))
505
+ );
506
+ const workerName = topLevelEnv?.name ?? workers[0].name;
507
+ const entrypoint = topLevelEnv?.entrypoint ?? workers[0].entrypoint;
508
+ let combinedConfig;
509
+ if (topLevelEnv) {
510
+ combinedConfig = convertWorkerToWranglerConfig(topLevelEnv);
511
+ } else {
512
+ combinedConfig = {
513
+ name: workerName,
514
+ main: entrypoint
515
+ };
516
+ }
517
+ for (const env of workers) {
518
+ const serviceTag = env.tags?.find(
519
+ (t) => t === `${SERVICE_TAG_PREFIX}${workerName}`
520
+ );
521
+ const envTag = env.tags?.find((t) => t.startsWith(ENVIRONMENT_TAG_PREFIX));
522
+ if (serviceTag !== `${SERVICE_TAG_PREFIX}${workerName}` || envTag === void 0) {
523
+ continue;
524
+ }
525
+ const [_, envName] = envTag.split("=");
526
+ combinedConfig.env ??= {};
527
+ combinedConfig.env[envName] = convertWorkerToWranglerConfig(env);
528
+ }
529
+ return combinedConfig;
530
+ }
531
+ __name(constructWranglerConfig, "constructWranglerConfig");
532
+
533
+ export { CommandLineArgsError, DeprecationError, ENVIRONMENT_TAG_PREFIX, FatalError, INHERIT_SYMBOL, JsonFriendlyFatalError, MissingConfigError, PATH_TO_DEPLOY_CONFIG, SERVICE_TAG_PREFIX, UserError, __commonJS, __name, __require, __toESM, assertNever, constructWranglerConfig, createFatalError, formatCompatibilityDate, mapWorkerMetadataBindings };