wrangler 4.30.0 → 4.31.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,1139 @@
1
+ // ../../vendor/jsrpc/dist/index.js
2
+ Symbol.dispose || (Symbol.dispose = Symbol.for("dispose"));
3
+ Symbol.asyncDispose || (Symbol.asyncDispose = Symbol.for("asyncDispose"));
4
+ var N = navigator.userAgent === "Cloudflare-Workers" ? "cloudflare:workers" : null;
5
+ var v;
6
+ N && (v = await import(N));
7
+ var E = v ? v.RpcTarget : class {
8
+ };
9
+ function T(s9) {
10
+ switch (typeof s9) {
11
+ case "boolean":
12
+ case "number":
13
+ case "string":
14
+ return "primitive";
15
+ case "undefined":
16
+ return "undefined";
17
+ case "object":
18
+ case "function":
19
+ break;
20
+ default:
21
+ return "unsupported";
22
+ }
23
+ if (s9 === null) return "primitive";
24
+ let e = Object.getPrototypeOf(s9);
25
+ switch (e) {
26
+ case Object.prototype:
27
+ return "object";
28
+ case Function.prototype:
29
+ return "function";
30
+ case Array.prototype:
31
+ return "array";
32
+ case Date.prototype:
33
+ return "date";
34
+ case h.prototype:
35
+ return "stub";
36
+ case c.prototype:
37
+ return "rpc-promise";
38
+ default:
39
+ return v && (e == v.RpcStub.prototype || e == v.RpcPromise.prototype || e == v.RpcProperty.prototype || e == v.ServiceStub.prototype) || s9 instanceof E ? "rpc-target" : s9 instanceof Error ? "error" : "unsupported";
40
+ }
41
+ }
42
+ var y = class {
43
+ };
44
+ var f = class extends y {
45
+ constructor(t) {
46
+ super();
47
+ this.error = t;
48
+ }
49
+ call(t, r) {
50
+ return this;
51
+ }
52
+ get(t) {
53
+ return this;
54
+ }
55
+ dup() {
56
+ return this;
57
+ }
58
+ pull() {
59
+ return Promise.reject(this.error);
60
+ }
61
+ dispose() {
62
+ }
63
+ onBroken(t) {
64
+ try {
65
+ t(this.error);
66
+ } catch (r) {
67
+ Promise.resolve(r);
68
+ }
69
+ }
70
+ };
71
+ var J = new f(new Error("Attempted to use RPC stub after it has been disposed."));
72
+ var I = Symbol("realStub");
73
+ var G = { apply(s9, e, t) {
74
+ let r = s9.raw;
75
+ return new c(r.hook.call(r.pathIfPromise || [], b.fromApp(t)), []);
76
+ }, get(s9, e, t) {
77
+ let r = s9.raw;
78
+ return e === I ? r : e in c.prototype ? r[e] : typeof e == "string" ? new c(r.hook, r.pathIfPromise ? [...r.pathIfPromise, e] : [e]) : e === Symbol.dispose && (!r.pathIfPromise || r.pathIfPromise.length == 0) ? () => {
79
+ r.hook.dispose(), r.hook = J;
80
+ } : void 0;
81
+ }, has(s9, e) {
82
+ let t = s9.raw;
83
+ return e === I ? true : e in c.prototype ? e in t : typeof e == "string" ? true : e === Symbol.dispose && (!t.pathIfPromise || t.pathIfPromise.length == 0);
84
+ }, construct(s9, e) {
85
+ throw new Error("An RPC stub cannot be used as a constructor.");
86
+ }, defineProperty(s9, e, t) {
87
+ throw new Error("Can't define properties on RPC stubs.");
88
+ }, deleteProperty(s9, e) {
89
+ throw new Error("Can't delete properties on RPC stubs.");
90
+ }, getOwnPropertyDescriptor(s9, e) {
91
+ }, getPrototypeOf(s9) {
92
+ return Object.getPrototypeOf(s9.raw);
93
+ }, isExtensible(s9) {
94
+ return false;
95
+ }, ownKeys(s9) {
96
+ return [];
97
+ }, preventExtensions(s9) {
98
+ return true;
99
+ }, set(s9, e, t, r) {
100
+ throw new Error("Can't assign properties on RPC stubs.");
101
+ }, setPrototypeOf(s9, e) {
102
+ throw new Error("Can't override prototype of RPC stubs.");
103
+ } };
104
+ var h = class s extends E {
105
+ constructor(e, t) {
106
+ if (super(), !(e instanceof y)) {
107
+ let o = e;
108
+ if (o instanceof E || o instanceof Function ? e = C.create(o, void 0) : e = new w(b.fromApp(o)), t) throw new TypeError("RpcStub constructor expected one argument, received two.");
109
+ }
110
+ this.hook = e, this.pathIfPromise = t;
111
+ let r = () => {
112
+ };
113
+ return r.raw = this, new Proxy(r, G);
114
+ }
115
+ hook;
116
+ pathIfPromise;
117
+ dup() {
118
+ let e = this[I];
119
+ return e.pathIfPromise ? new s(e.hook.get(e.pathIfPromise)) : new s(e.hook.dup());
120
+ }
121
+ onRpcBroken(e) {
122
+ this[I].hook.onBroken(e);
123
+ }
124
+ };
125
+ var c = class extends h {
126
+ constructor(e, t) {
127
+ super(e, t);
128
+ }
129
+ then(e, t) {
130
+ return B(this).then(...arguments);
131
+ }
132
+ catch(e) {
133
+ return B(this).catch(...arguments);
134
+ }
135
+ finally(e) {
136
+ return B(this).finally(...arguments);
137
+ }
138
+ };
139
+ function m(s9) {
140
+ return s9[I];
141
+ }
142
+ async function B(s9) {
143
+ let { hook: e, pathIfPromise: t } = m(s9);
144
+ return t.length > 0 && (e = e.get(t)), (await e.pull()).deliverResolve();
145
+ }
146
+ var b = class s2 {
147
+ constructor(e, t, r) {
148
+ this.value = e;
149
+ this.stubs = t;
150
+ this.promises = r;
151
+ }
152
+ static fromApp(e) {
153
+ return new s2(e);
154
+ }
155
+ static forEvaluate(e, t) {
156
+ return new s2(null, e, t);
157
+ }
158
+ static deepCopyFrom(e, t, r) {
159
+ let o = new s2(null);
160
+ return o.stubs = [], o.promises = [], o.value = o.deepCopy(e, t, "value", o, true, r), o;
161
+ }
162
+ rpcTargets;
163
+ isFromApp() {
164
+ return !this.stubs;
165
+ }
166
+ getHookForRpcTarget(e, t) {
167
+ this.rpcTargets || (this.rpcTargets = /* @__PURE__ */ new Map());
168
+ let r = this.rpcTargets.get(e);
169
+ return r || (r = C.create(e, t), this.rpcTargets.set(e, r)), r;
170
+ }
171
+ deepCopy(e, t, r, o, n, i) {
172
+ let a = T(e);
173
+ switch (a) {
174
+ case "unsupported":
175
+ return e;
176
+ case "primitive":
177
+ case "date":
178
+ case "error":
179
+ case "undefined":
180
+ return e;
181
+ case "array": {
182
+ let p = e, d = p.length, l = new Array(d);
183
+ for (let u = 0; u < d; u++) l[u] = this.deepCopy(p[u], p, u, l, n, i);
184
+ return l;
185
+ }
186
+ case "object": {
187
+ let p = {}, d = e;
188
+ for (let l in d) p[l] = this.deepCopy(d[l], d, l, p, n, i);
189
+ return p;
190
+ }
191
+ case "stub":
192
+ case "rpc-promise": {
193
+ let p = e, { hook: d, pathIfPromise: l } = m(p);
194
+ return l ? p = new c(d.get(l), []) : n && (p = p.dup()), p instanceof c ? this.promises.push({ parent: o, property: r, promise: p }) : this.stubs.push(p), p;
195
+ }
196
+ case "function":
197
+ case "rpc-target": {
198
+ let p = e;
199
+ return i ? new h(i.getHookForRpcTarget(p, t).dup()) : new h(C.create(p, t));
200
+ }
201
+ default:
202
+ throw new Error("unreachable");
203
+ }
204
+ }
205
+ ensureDeepCopied() {
206
+ this.stubs === void 0 && (this.stubs = [], this.promises = [], this.value = this.deepCopy(this.value, void 0, "value", this, false, this), this.rpcTargets = void 0);
207
+ }
208
+ deliverTo(e, t, r) {
209
+ if (this.ensureDeepCopied(), this.value instanceof c) s2.deliverRpcPromiseTo(this.value, e, t, r);
210
+ else {
211
+ e[t] = this.value;
212
+ for (let o of this.promises) s2.deliverRpcPromiseTo(o.promise, o.parent, o.property, r);
213
+ }
214
+ }
215
+ static deliverRpcPromiseTo(e, t, r, o) {
216
+ let { hook: n, pathIfPromise: i } = m(e);
217
+ if (i && i.length > 0) throw new Error("property promises should have been resolved earlier");
218
+ let a = n.pull();
219
+ a instanceof s2 ? a.deliverTo(t, r, o) : o.push(a.then((p) => {
220
+ let d = [];
221
+ if (p.deliverTo(t, r, d), d.length > 0) return Promise.all(d);
222
+ }));
223
+ }
224
+ async deliverCall(e, t) {
225
+ try {
226
+ let r = [];
227
+ this.deliverTo(this, "value", r), r.length > 0 && await Promise.all(r);
228
+ let o = Function.prototype.apply.call(e, t, this.value);
229
+ return o instanceof c ? s2.fromApp(o) : s2.fromApp(await o);
230
+ } finally {
231
+ this.dispose();
232
+ }
233
+ }
234
+ async deliverResolve() {
235
+ try {
236
+ let e = [];
237
+ this.deliverTo(this, "value", e), e.length > 0 && await Promise.all(e);
238
+ let t = this.value;
239
+ return t instanceof Object && (Symbol.dispose in t || (t[Symbol.dispose] = () => this.dispose())), t;
240
+ } catch (e) {
241
+ throw this.dispose(), e;
242
+ }
243
+ }
244
+ dispose() {
245
+ this.stubs ? (this.stubs.forEach((e) => e[Symbol.dispose]()), this.promises.forEach((e) => e.promise[Symbol.dispose]())) : this.disposeImpl(this.value, void 0), this.stubs = [], this.promises = [];
246
+ }
247
+ disposeImpl(e, t) {
248
+ let r = T(e);
249
+ switch (r) {
250
+ case "unsupported":
251
+ case "primitive":
252
+ case "date":
253
+ case "error":
254
+ case "undefined":
255
+ return;
256
+ case "array": {
257
+ let o = e, n = o.length;
258
+ for (let i = 0; i < n; i++) this.disposeImpl(o[i], o);
259
+ return;
260
+ }
261
+ case "object": {
262
+ let o = e;
263
+ for (let n in o) this.disposeImpl(o[n], o);
264
+ return;
265
+ }
266
+ case "stub":
267
+ case "rpc-promise": {
268
+ let o = e, { hook: n, pathIfPromise: i } = m(o);
269
+ i || n.dispose();
270
+ return;
271
+ }
272
+ case "function":
273
+ case "rpc-target": {
274
+ let o = e;
275
+ this.getHookForRpcTarget(o, t).dispose();
276
+ return;
277
+ }
278
+ default:
279
+ return;
280
+ }
281
+ }
282
+ };
283
+ function x(s9, e) {
284
+ if (e === 0) throw new TypeError(`RPC object has no property '${s9[e]}'`);
285
+ {
286
+ let t = s9.slice(0, e).join(".");
287
+ throw new TypeError(`'${t}' has no property '${s9[e]}'`);
288
+ }
289
+ }
290
+ function H(s9, e, t, r) {
291
+ for (let o = 0; o < t.length; o++) {
292
+ e = s9;
293
+ let n = t[o];
294
+ (n === "__proto__" || n === "constructor") && x(t, o);
295
+ let i = T(s9);
296
+ switch (i) {
297
+ case "object":
298
+ case "array":
299
+ case "function":
300
+ Object.hasOwn(s9, n) || x(t, o), s9 = s9[n];
301
+ break;
302
+ case "rpc-target": {
303
+ Object.hasOwn(s9, n) && x(t, o), s9 = s9[n], (!s9 || s9 === Object.prototype[n]) && x(t, o), r = null;
304
+ break;
305
+ }
306
+ case "stub":
307
+ case "rpc-promise": {
308
+ let { hook: a, pathIfPromise: p } = m(s9);
309
+ return { hook: a, remainingPath: p ? p.concat(t.slice(o)) : t.slice(o) };
310
+ }
311
+ case "primitive":
312
+ case "date":
313
+ case "error":
314
+ case "undefined":
315
+ x(t, o);
316
+ case "unsupported": {
317
+ if (o === 0) throw new TypeError("RPC stub points at a non-serializable type.");
318
+ {
319
+ let a = t.slice(0, o).join("."), p = t.slice(0, o).join(".");
320
+ throw new TypeError(`'${a}' is not a serializable type, so property ${p} cannot be accessed.`);
321
+ }
322
+ }
323
+ default:
324
+ throw new TypeError("unreachable");
325
+ }
326
+ }
327
+ return { value: s9, parent: e, owner: r };
328
+ }
329
+ var w = class s3 extends y {
330
+ constructor(e) {
331
+ super(), this.payload = e;
332
+ }
333
+ payload;
334
+ getPayload() {
335
+ if (this.payload) return this.payload;
336
+ throw new Error("Attempted to use an RPC StubHook after it was disposed.");
337
+ }
338
+ call(e, t) {
339
+ try {
340
+ let r = this.getPayload(), o = H(r.value, void 0, e, r);
341
+ if (o.hook) return o.hook.call(o.remainingPath, t);
342
+ if (typeof o.value != "function") throw new TypeError(`'${e.join(".")}' is not a function.`);
343
+ let n = t.deliverCall(o.value, o.parent);
344
+ return new j(n.then((i) => new s3(i)));
345
+ } catch (r) {
346
+ return new f(r);
347
+ }
348
+ }
349
+ get(e) {
350
+ try {
351
+ let t = this.getPayload(), r = H(t.value, void 0, e, t);
352
+ return r.hook ? r.hook.get(r.remainingPath) : new s3(b.deepCopyFrom(r.value, r.parent, r.owner));
353
+ } catch (t) {
354
+ return new f(t);
355
+ }
356
+ }
357
+ dup() {
358
+ let e = this.getPayload();
359
+ return new s3(b.deepCopyFrom(e.value, void 0, e));
360
+ }
361
+ pull() {
362
+ return this.getPayload();
363
+ }
364
+ dispose() {
365
+ this.payload && (this.payload.dispose(), this.payload = void 0);
366
+ }
367
+ onBroken(e) {
368
+ this.payload && this.payload.value instanceof h && this.payload.value.onRpcBroken(e);
369
+ }
370
+ };
371
+ var C = class s4 extends y {
372
+ static create(e, t) {
373
+ return typeof e != "function" && (t = void 0), new s4(e, t);
374
+ }
375
+ constructor(e, t, r) {
376
+ super(), this.target = e, this.parent = t, r ? r.refcount && (this.refcount = r.refcount, ++this.refcount.count) : Symbol.dispose in e && (this.refcount = { count: 1 });
377
+ }
378
+ target;
379
+ parent;
380
+ refcount;
381
+ getTarget() {
382
+ if (this.target) return this.target;
383
+ throw new Error("Attempted to use an RPC StubHook after it was disposed.");
384
+ }
385
+ call(e, t) {
386
+ try {
387
+ let r = this.getTarget(), o = H(r, this.parent, e, null);
388
+ if (o.hook) return o.hook.call(o.remainingPath, t);
389
+ if (typeof o.value != "function") throw new TypeError(`'${e.join(".")}' is not a function.`);
390
+ let n = t.deliverCall(o.value, o.parent);
391
+ return new j(n.then((i) => new w(i)));
392
+ } catch (r) {
393
+ return new f(r);
394
+ }
395
+ }
396
+ get(e) {
397
+ try {
398
+ if (e.length == 0) throw new Error("Can't dup an RpcTarget stub as a promise.");
399
+ let t = this.getTarget(), r = H(t, this.parent, e, null);
400
+ return r.hook ? r.hook.get(r.remainingPath) : new w(b.deepCopyFrom(r.value, r.parent, r.owner));
401
+ } catch (t) {
402
+ return new f(t);
403
+ }
404
+ }
405
+ dup() {
406
+ return new s4(this.getTarget(), this.parent, this);
407
+ }
408
+ pull() {
409
+ return Promise.reject(new Error("Tried to resolve a non-promise stub."));
410
+ }
411
+ dispose() {
412
+ if (this.target) {
413
+ if (this.refcount && --this.refcount.count == 0 && Symbol.dispose in this.target) try {
414
+ this.target[Symbol.dispose]();
415
+ } catch (e) {
416
+ Promise.reject(e);
417
+ }
418
+ this.target = void 0;
419
+ }
420
+ }
421
+ onBroken(e) {
422
+ }
423
+ };
424
+ var j = class s5 extends y {
425
+ promise;
426
+ resolution;
427
+ constructor(e) {
428
+ super(), this.promise = e.then((t) => (this.resolution = t, t));
429
+ }
430
+ call(e, t) {
431
+ return new s5(this.promise.then((r) => r.call(e, t)));
432
+ }
433
+ get(e) {
434
+ return new s5(this.promise.then((t) => t.get(e)));
435
+ }
436
+ dup() {
437
+ return this.resolution ? this.resolution.dup() : new s5(this.promise.then((e) => e.dup()));
438
+ }
439
+ pull() {
440
+ return this.resolution ? this.resolution.pull() : this.promise.then((e) => e.pull());
441
+ }
442
+ dispose() {
443
+ this.resolution ? this.resolution.dispose() : this.promise.then((e) => {
444
+ e.dispose();
445
+ }, (e) => {
446
+ });
447
+ }
448
+ onBroken(e) {
449
+ this.resolution ? this.resolution.onBroken(e) : this.promise.then((t) => {
450
+ t.onBroken(e);
451
+ }, e);
452
+ }
453
+ };
454
+ var O = class {
455
+ exportStub(e) {
456
+ throw new Error("Cannot serialize RPC stubs without an RPC session.");
457
+ }
458
+ exportPromise(e) {
459
+ throw new Error("Cannot serialize RPC stubs without an RPC session.");
460
+ }
461
+ getImport(e) {
462
+ }
463
+ unexport(e) {
464
+ }
465
+ onSendError(e) {
466
+ }
467
+ };
468
+ var K = new O();
469
+ var Q = { Error, EvalError, RangeError, ReferenceError, SyntaxError, TypeError, URIError, AggregateError };
470
+ var R = class s6 {
471
+ constructor(e, t, r) {
472
+ this.exporter = e;
473
+ this.source = t;
474
+ this.takeOwnership = r;
475
+ }
476
+ static devaluate(e, t, r = K, o, n = true) {
477
+ let i = new s6(r, o, n);
478
+ try {
479
+ return { value: i.devaluateImpl(e, t, 0), deferredDisposals: i.deferredDisposals };
480
+ } catch (a) {
481
+ if (i.exports) try {
482
+ r.unexport(i.exports);
483
+ } catch {
484
+ }
485
+ throw i.deferredDisposals && i.deferredDisposals.forEach((p) => p.dispose()), a;
486
+ }
487
+ }
488
+ exports;
489
+ deferredDisposals;
490
+ devaluateImpl(e, t, r) {
491
+ if (r >= 64) throw new Error("Serialization exceeded maximum allowed depth. (Does the message contain cycles?)");
492
+ let o = T(e);
493
+ switch (o) {
494
+ case "unsupported":
495
+ throw new TypeError("cannot serialize: " + e);
496
+ case "primitive":
497
+ return e;
498
+ case "object": {
499
+ let n = e, i = {};
500
+ for (let a in n) i[a] = this.devaluateImpl(n[a], n, r + 1);
501
+ return i;
502
+ }
503
+ case "array": {
504
+ let n = e, i = n.length, a = new Array(i);
505
+ for (let p = 0; p < i; p++) a[p] = this.devaluateImpl(n[p], n, r + 1);
506
+ return [a];
507
+ }
508
+ case "date":
509
+ return ["date", e.getTime()];
510
+ case "error": {
511
+ let n = e, i = this.exporter.onSendError(n);
512
+ i && (n = i);
513
+ let a = ["error", n.name, n.message];
514
+ return i && i.stack && a.push(i.stack), a;
515
+ }
516
+ case "undefined":
517
+ return ["undefined"];
518
+ case "stub":
519
+ case "rpc-promise": {
520
+ if (!this.source) throw new Error("Can't serialize RPC stubs in this context.");
521
+ let { hook: n, pathIfPromise: i } = m(e);
522
+ return this.devaluateHook(n, i);
523
+ }
524
+ case "function":
525
+ case "rpc-target": {
526
+ if (!this.source) throw new Error("Can't serialize RPC stubs in this context.");
527
+ let n = this.source.getHookForRpcTarget(e, t);
528
+ return this.devaluateHook(n, void 0);
529
+ }
530
+ default:
531
+ throw new Error("unreachable");
532
+ }
533
+ }
534
+ devaluateHook(e, t) {
535
+ let r = this.exporter.getImport(e);
536
+ if (r !== void 0) return t ? t.length > 0 ? ["pipeline", r, t] : ["pipeline", r] : (this.takeOwnership && (this.deferredDisposals || (this.deferredDisposals = []), this.deferredDisposals.push(e)), ["import", r]);
537
+ if (this.exports || (this.exports = []), t) {
538
+ if (this.source && this.source.isFromApp() || !this.takeOwnership) e = e.get(t);
539
+ else if (t.length > 0) throw new Error("RPC system bug: Unexpected uncloned promise in from-app payload.");
540
+ let o = this.exporter.exportPromise(e);
541
+ return this.exports.push(o), ["promise", o];
542
+ } else {
543
+ this.takeOwnership || (e = e.dup());
544
+ let o = this.exporter.exportStub(e);
545
+ return this.exports.push(o), ["export", o];
546
+ }
547
+ }
548
+ };
549
+ var A = class {
550
+ importStub(e) {
551
+ throw new Error("Cannot deserialize RPC stubs without an RPC session.");
552
+ }
553
+ importPromise(e) {
554
+ throw new Error("Cannot deserialize RPC stubs without an RPC session.");
555
+ }
556
+ getExport(e) {
557
+ }
558
+ };
559
+ var Y = new A();
560
+ var g = class s7 {
561
+ constructor(e) {
562
+ this.importer = e;
563
+ }
564
+ stubs = [];
565
+ promises = [];
566
+ evaluate(e) {
567
+ let t = b.forEvaluate(this.stubs, this.promises);
568
+ try {
569
+ return t.value = this.evaluateImpl(e, t, "value"), t;
570
+ } catch (r) {
571
+ throw t.dispose(), r;
572
+ }
573
+ }
574
+ evaluateImpl(e, t, r) {
575
+ if (e instanceof Array) {
576
+ if (e.length == 1 && e[0] instanceof Array) {
577
+ let o = e[0];
578
+ for (let n = 0; n < o.length; n++) o[n] = this.evaluateImpl(o[n], o, n);
579
+ return o;
580
+ } else switch (e[0]) {
581
+ case "date":
582
+ if (typeof e[1] == "number") return new Date(e[1]);
583
+ break;
584
+ case "error":
585
+ if (e.length >= 3 && typeof e[1] == "string" && typeof e[2] == "string") {
586
+ let l = Q[e[1]] || Error, u = new l(e[2]);
587
+ return typeof e[3] == "string" && (u.stack = e[3]), u;
588
+ }
589
+ break;
590
+ case "undefined":
591
+ if (e.length === 1) return;
592
+ break;
593
+ case "import":
594
+ case "pipeline":
595
+ if (e.length < 2 || e.length > 4 || typeof e[1] != "number") break;
596
+ let o = this.importer.getExport(e[1]);
597
+ if (!o) throw new Error(`no such entry on exports table: ${e[1]}`);
598
+ let n = e[0] == "pipeline", i = (l) => {
599
+ if (n) {
600
+ let u = new c(l, []);
601
+ return this.promises.push({ promise: u, parent: t, property: r }), u;
602
+ } else {
603
+ let u = new c(l, []);
604
+ return this.stubs.push(u), u;
605
+ }
606
+ };
607
+ if (e.length == 2) return i(n ? o.get([]) : o.dup());
608
+ let a = e[2];
609
+ if (!(a instanceof Array) || !a.every((l) => typeof l == "string" || typeof l == "number")) break;
610
+ if (e.length == 3) return i(o.get(a));
611
+ let p = e[3];
612
+ if (!(p instanceof Array)) break;
613
+ return p = new s7(this.importer).evaluate([p]), i(o.call(a, p));
614
+ case "export":
615
+ case "promise":
616
+ if (typeof e[1] == "number") if (e[0] == "promise") {
617
+ let l = this.importer.importPromise(e[1]), u = new c(l, []);
618
+ return this.promises.push({ parent: t, property: r, promise: u }), u;
619
+ } else {
620
+ let l = this.importer.importStub(e[1]), u = new h(l);
621
+ return this.stubs.push(u), u;
622
+ }
623
+ break;
624
+ }
625
+ throw new TypeError(`unknown special value: ${JSON.stringify(e)}`);
626
+ } else if (e instanceof Object) {
627
+ let o = e;
628
+ for (let n in o) o[n] = this.evaluateImpl(o[n], o, n);
629
+ return o;
630
+ } else return e;
631
+ }
632
+ };
633
+ var S = class {
634
+ constructor(e, t, r) {
635
+ this.session = e;
636
+ this.importId = t;
637
+ r && (this.activePull = Promise.withResolvers());
638
+ }
639
+ localRefcount = 0;
640
+ remoteRefcount = 1;
641
+ activePull;
642
+ resolution;
643
+ onBrokenRegistrations;
644
+ resolve(e) {
645
+ if (this.localRefcount == 0) {
646
+ e.dispose();
647
+ return;
648
+ }
649
+ if (this.resolution = e, this.sendRelease(), this.onBrokenRegistrations) {
650
+ for (let t of this.onBrokenRegistrations) {
651
+ let r = this.session.onBrokenCallbacks[t], o = this.session.onBrokenCallbacks.length;
652
+ e.onBroken(r), this.session.onBrokenCallbacks[o] === r ? delete this.session.onBrokenCallbacks[o] : delete this.session.onBrokenCallbacks[t];
653
+ }
654
+ this.onBrokenRegistrations = void 0;
655
+ }
656
+ this.activePull && (this.activePull.resolve(), this.activePull = void 0);
657
+ }
658
+ async awaitResolution() {
659
+ return this.activePull || (this.session.sendPull(this.importId), this.activePull = Promise.withResolvers()), await this.activePull.promise, this.resolution.pull();
660
+ }
661
+ dispose() {
662
+ this.resolution ? this.resolution.dispose() : (this.abort(new Error("RPC was canceled because the RpcPromise was disposed.")), this.sendRelease());
663
+ }
664
+ abort(e) {
665
+ this.resolution || (this.resolution = new f(e), this.activePull && (this.activePull.reject(e), this.activePull = void 0), this.onBrokenRegistrations = void 0);
666
+ }
667
+ onBroken(e) {
668
+ if (this.resolution) this.resolution.onBroken(e);
669
+ else {
670
+ let t = this.session.onBrokenCallbacks.length;
671
+ this.session.onBrokenCallbacks.push(e), this.onBrokenRegistrations || (this.onBrokenRegistrations = []), this.onBrokenRegistrations.push(t);
672
+ }
673
+ }
674
+ sendRelease() {
675
+ this.remoteRefcount > 0 && (this.session.sendRelease(this.importId, this.remoteRefcount), this.remoteRefcount = 0);
676
+ }
677
+ };
678
+ var k = class s8 extends y {
679
+ constructor(t, r) {
680
+ super();
681
+ this.isPromise = t;
682
+ ++r.localRefcount, this.entry = r;
683
+ }
684
+ entry;
685
+ collectPath(t) {
686
+ return this;
687
+ }
688
+ getEntry() {
689
+ if (this.entry) return this.entry;
690
+ throw new Error("This RpcImportHook was already disposed.");
691
+ }
692
+ call(t, r) {
693
+ let o = this.getEntry();
694
+ return o.resolution ? o.resolution.call(t, r) : o.session.sendCall(o.importId, t, r);
695
+ }
696
+ get(t) {
697
+ let r = this.getEntry();
698
+ return r.resolution ? r.resolution.get(t) : r.session.sendCall(r.importId, t);
699
+ }
700
+ dup() {
701
+ return new s8(false, this.getEntry());
702
+ }
703
+ pull() {
704
+ let t = this.getEntry();
705
+ if (!this.isPromise) throw new Error("Can't pull this hook because it's not a promise hook.");
706
+ return t.resolution ? t.resolution.pull() : t.awaitResolution();
707
+ }
708
+ dispose() {
709
+ let t = this.entry;
710
+ this.entry = void 0, t && --t.localRefcount === 0 && t.dispose();
711
+ }
712
+ onBroken(t) {
713
+ this.entry && this.entry.onBroken(t);
714
+ }
715
+ };
716
+ var D = class extends k {
717
+ session;
718
+ constructor(e) {
719
+ super(false, e), this.session = e.session;
720
+ }
721
+ dispose() {
722
+ if (this.session) {
723
+ let e = this.session;
724
+ this.session = void 0, e.shutdown();
725
+ }
726
+ }
727
+ };
728
+ var F = class {
729
+ constructor(e, t, r) {
730
+ this.transport = e;
731
+ this.options = r;
732
+ this.exports.push({ hook: t, refcount: 1 }), this.imports.push(new S(this, 0, false));
733
+ let o, n = new Promise((i, a) => {
734
+ o = a;
735
+ });
736
+ this.cancelReadLoop = o, this.readLoop(n).catch((i) => this.abort(i));
737
+ }
738
+ exports = [];
739
+ reverseExports = /* @__PURE__ */ new Map();
740
+ imports = [];
741
+ abortReason;
742
+ cancelReadLoop;
743
+ nextExportId = -1;
744
+ onBatchDone;
745
+ pullCount = 0;
746
+ onBrokenCallbacks = [];
747
+ getMainImport() {
748
+ return new D(this.imports[0]);
749
+ }
750
+ shutdown() {
751
+ this.abort(new Error("RPC session was shut down by disposing the main stub"), false);
752
+ }
753
+ exportStub(e) {
754
+ if (this.abortReason) throw this.abortReason;
755
+ let t = this.reverseExports.get(e);
756
+ if (t !== void 0) return ++this.exports[t].refcount, t;
757
+ {
758
+ let r = this.nextExportId--;
759
+ return this.exports[r] = { hook: e, refcount: 1 }, this.reverseExports.set(e, r), r;
760
+ }
761
+ }
762
+ exportPromise(e) {
763
+ if (this.abortReason) throw this.abortReason;
764
+ let t = this.nextExportId--;
765
+ return this.exports[t] = { hook: e, refcount: 1 }, this.reverseExports.set(e, t), this.ensureResolvingExport(t), t;
766
+ }
767
+ unexport(e) {
768
+ for (let t of e) this.releaseExport(t, 1);
769
+ }
770
+ releaseExport(e, t) {
771
+ let r = this.exports[e];
772
+ if (!r) throw new Error(`no such export ID: ${e}`);
773
+ if (r.refcount < t) throw new Error(`refcount would go negative: ${r.refcount} < ${t}`);
774
+ r.refcount -= t, r.refcount === 0 && (delete this.exports[e], this.reverseExports.delete(r.hook), r.hook.dispose());
775
+ }
776
+ onSendError(e) {
777
+ if (this.options.onSendError) return this.options.onSendError(e);
778
+ }
779
+ ensureResolvingExport(e) {
780
+ let t = this.exports[e];
781
+ if (!t) throw new Error(`no such export ID: ${e}`);
782
+ if (!t.pull) {
783
+ let r = async () => {
784
+ let o = t.hook;
785
+ for (; ; ) {
786
+ let n = await o.pull();
787
+ if (n.value instanceof h) {
788
+ let { hook: i, pathIfPromise: a } = m(n.value);
789
+ if (a && a.length == 0 && this.getImport(o) === void 0) {
790
+ o = i;
791
+ continue;
792
+ }
793
+ }
794
+ return n;
795
+ }
796
+ };
797
+ ++this.pullCount, t.pull = r().then((o) => {
798
+ let { value: n, deferredDisposals: i } = R.devaluate(o.value, void 0, this, o, false);
799
+ this.send(["resolve", e, n]), i && i?.forEach((a) => a.dispose());
800
+ }, (o) => {
801
+ this.send(["reject", e, R.devaluate(o, void 0, this).value]);
802
+ }).catch((o) => {
803
+ try {
804
+ this.send(["reject", e, R.devaluate(o, void 0, this).value]);
805
+ } catch (n) {
806
+ this.abort(n);
807
+ }
808
+ }).finally(() => {
809
+ --this.pullCount === 0 && this.onBatchDone && this.onBatchDone.resolve();
810
+ });
811
+ }
812
+ }
813
+ getImport(e) {
814
+ if (e instanceof k && e.entry && e.entry.session === this) return e.entry.importId;
815
+ }
816
+ importStub(e) {
817
+ if (this.abortReason) throw this.abortReason;
818
+ let t = this.imports[e];
819
+ return t || (t = new S(this, e, false), this.imports[e] = t), new k(false, t);
820
+ }
821
+ importPromise(e) {
822
+ if (this.abortReason) throw this.abortReason;
823
+ if (this.imports[e]) return new f(new Error("Bug in RPC system: The peer sent a promise reusing an existing export ID."));
824
+ let t = new S(this, e, true);
825
+ return this.imports[e] = t, new k(true, t);
826
+ }
827
+ getExport(e) {
828
+ return this.exports[e]?.hook;
829
+ }
830
+ send(e) {
831
+ if (this.abortReason !== void 0) return;
832
+ let t;
833
+ try {
834
+ t = JSON.stringify(e);
835
+ } catch (r) {
836
+ throw r;
837
+ }
838
+ this.transport.send(t).catch((r) => this.abort(r, false));
839
+ }
840
+ sendCall(e, t, r) {
841
+ if (this.abortReason) throw this.abortReason;
842
+ let o = ["pipeline", e, t], n;
843
+ if (r) {
844
+ let a = R.devaluate(r.value, void 0, this, r, true);
845
+ n = a.deferredDisposals, o.push(a.value[0]);
846
+ }
847
+ this.send(["push", o]), n && n?.forEach((a) => a.dispose());
848
+ let i = new S(this, this.imports.length, false);
849
+ return this.imports.push(i), new k(true, i);
850
+ }
851
+ sendPull(e) {
852
+ if (this.abortReason) throw this.abortReason;
853
+ this.send(["pull", e]);
854
+ }
855
+ sendRelease(e, t) {
856
+ this.abortReason || (this.send(["release", e, t]), delete this.imports[e]);
857
+ }
858
+ abort(e, t = true) {
859
+ if (this.abortReason === void 0) {
860
+ if (this.cancelReadLoop(e), t) try {
861
+ this.transport.send(JSON.stringify(["abort", R.devaluate(e, void 0, this)]));
862
+ } catch {
863
+ }
864
+ if (e === void 0 && (e = "undefined"), this.abortReason = e, this.onBatchDone && this.onBatchDone.reject(e), this.transport.abort) try {
865
+ this.transport.abort(e);
866
+ } catch (r) {
867
+ Promise.resolve(r);
868
+ }
869
+ for (let r in this.onBrokenCallbacks) try {
870
+ this.onBrokenCallbacks[r](e);
871
+ } catch (o) {
872
+ Promise.resolve(o);
873
+ }
874
+ for (let r in this.imports) this.imports[r].abort(e);
875
+ for (let r in this.exports) this.exports[r].hook.dispose();
876
+ }
877
+ }
878
+ async readLoop(e) {
879
+ for (; !this.abortReason; ) {
880
+ let t = JSON.parse(await Promise.race([this.transport.receive(), e]));
881
+ if (this.abortReason) break;
882
+ if (t instanceof Array) switch (t[0]) {
883
+ case "push":
884
+ if (t.length > 1) {
885
+ let r = new g(this).evaluate(t[1]);
886
+ this.exports.push({ hook: new w(r), refcount: 1 });
887
+ continue;
888
+ }
889
+ break;
890
+ case "pull": {
891
+ let r = t[1];
892
+ if (typeof r == "number") {
893
+ this.ensureResolvingExport(r);
894
+ continue;
895
+ }
896
+ break;
897
+ }
898
+ case "resolve":
899
+ case "reject": {
900
+ let r = t[1];
901
+ if (typeof r == "number" && t.length > 2) {
902
+ let o = this.imports[r];
903
+ if (o) if (t[0] == "resolve") o.resolve(new w(new g(this).evaluate(t[2])));
904
+ else {
905
+ let n = new g(this).evaluate(t[2]);
906
+ n.dispose(), o.resolve(new f(n.value));
907
+ }
908
+ else t[0] == "resolve" && new g(this).evaluate(t[2]).dispose();
909
+ continue;
910
+ }
911
+ break;
912
+ }
913
+ case "release": {
914
+ let r = t[1], o = t[2];
915
+ if (typeof r == "number" && typeof o == "number") {
916
+ this.releaseExport(r, o);
917
+ continue;
918
+ }
919
+ break;
920
+ }
921
+ case "abort": {
922
+ let r = new g(this).evaluate(t[1]);
923
+ r.dispose(), this.abort(r, false);
924
+ break;
925
+ }
926
+ }
927
+ throw new Error(`bad RPC message: ${JSON.stringify(t)}`);
928
+ }
929
+ }
930
+ async drain() {
931
+ if (this.abortReason) throw this.abortReason;
932
+ if (this.pullCount > 0) {
933
+ let { promise: e, resolve: t, reject: r } = Promise.withResolvers();
934
+ this.onBatchDone = { resolve: t, reject: r }, await e;
935
+ }
936
+ }
937
+ getStats() {
938
+ let e = { imports: 0, exports: 0 };
939
+ for (let t in this.imports) ++e.imports;
940
+ for (let t in this.exports) ++e.exports;
941
+ return e;
942
+ }
943
+ };
944
+ var P = class {
945
+ #t;
946
+ #e;
947
+ constructor(e, t, r = {}) {
948
+ let o;
949
+ t ? o = new w(b.fromApp(t)) : o = new f(new Error("This connection has no main object.")), this.#t = new F(e, o, r), this.#e = new h(this.#t.getMainImport());
950
+ }
951
+ getRemoteMain() {
952
+ return this.#e;
953
+ }
954
+ getStats() {
955
+ return this.#t.getStats();
956
+ }
957
+ drain() {
958
+ return this.#t.drain();
959
+ }
960
+ };
961
+ function z(s9, e, t) {
962
+ typeof s9 == "string" && (s9 = new WebSocket(s9));
963
+ let r = new W(s9);
964
+ return new P(r, e, t).getRemoteMain();
965
+ }
966
+ function M(s9, e, t) {
967
+ if (s9.headers.get("Upgrade")?.toLowerCase() !== "websocket") return new Response("This endpoint only accepts WebSocket requests.", { status: 400 });
968
+ let r = new WebSocketPair(), o = r[0];
969
+ return o.accept(), z(o, e, t), new Response(null, { status: 101, webSocket: r[1] });
970
+ }
971
+ var W = class {
972
+ constructor(e) {
973
+ this.#t = e, e.readyState === WebSocket.CONNECTING && (this.#e = [], e.addEventListener("open", (t) => {
974
+ try {
975
+ for (let r of this.#e) e.send(r);
976
+ } catch (r) {
977
+ this.#i(r);
978
+ }
979
+ this.#e = void 0;
980
+ })), e.addEventListener("message", (t) => {
981
+ this.#s || (typeof t.data == "string" ? this.#r ? (this.#r(t.data), this.#r = void 0, this.#o = void 0) : this.#n.push(t.data) : this.#i(new TypeError("Received non-string message from WebSocket.")));
982
+ }), e.addEventListener("close", (t) => {
983
+ this.#i(new Error(`Peer closed WebSocket: ${t.code} ${t.reason}`));
984
+ }), e.addEventListener("error", (t) => {
985
+ this.#i(new Error("WebSocket connection failed."));
986
+ });
987
+ }
988
+ #t;
989
+ #e;
990
+ #r;
991
+ #o;
992
+ #n = [];
993
+ #s;
994
+ async send(e) {
995
+ this.#e === void 0 ? this.#t.send(e) : this.#e.push(e);
996
+ }
997
+ async receive() {
998
+ if (this.#n.length > 0) return this.#n.shift();
999
+ if (this.#s) throw this.#s;
1000
+ return new Promise((e, t) => {
1001
+ this.#r = e, this.#o = t;
1002
+ });
1003
+ }
1004
+ abort(e) {
1005
+ let t;
1006
+ e instanceof Error ? t = e.message : t = `${e}`, this.#t.close(3e3, t), this.#s || (this.#s = e);
1007
+ }
1008
+ #i(e) {
1009
+ this.#s || (this.#s = e, this.#o && (this.#o(e), this.#r = void 0, this.#o = void 0));
1010
+ }
1011
+ };
1012
+ var $ = class {
1013
+ constructor(e) {
1014
+ this.#e = e;
1015
+ }
1016
+ #t = [];
1017
+ #e;
1018
+ #r = Promise.withResolvers();
1019
+ async send(e) {
1020
+ this.#t.push(e);
1021
+ }
1022
+ async receive() {
1023
+ let e = this.#e.shift();
1024
+ return e !== void 0 ? e : (this.#r.resolve(), new Promise((t) => {
1025
+ }));
1026
+ }
1027
+ abort(e) {
1028
+ this.#r.reject(e);
1029
+ }
1030
+ whenAllReceived() {
1031
+ return this.#r.promise;
1032
+ }
1033
+ getResponseBody() {
1034
+ return this.#t.join(`
1035
+ `);
1036
+ }
1037
+ };
1038
+ async function U(s9, e, t) {
1039
+ if (s9.method !== "POST") return new Response("This endpoint only accepts POST requests.", { status: 405 });
1040
+ let r = await s9.text(), o = r === "" ? [] : r.split(`
1041
+ `), n = new $(o), i = new P(n, e, t);
1042
+ return await n.whenAllReceived(), await i.drain(), new Response(n.getResponseBody());
1043
+ }
1044
+ function We(s9, e) {
1045
+ return s9.method === "POST" ? U(s9, e) : s9.headers.get("Upgrade")?.toLowerCase() === "websocket" ? M(s9, e) : new Response("This endpoint only accepts POST or WebSocket requests.", { status: 400 });
1046
+ }
1047
+
1048
+ // templates/remoteBindings/ProxyServerWorker.ts
1049
+ import { EmailMessage } from "cloudflare:email";
1050
+ var BindingNotFoundError = class extends Error {
1051
+ constructor(name) {
1052
+ super(`Binding ${name ? `"${name}"` : ""} not found`);
1053
+ }
1054
+ };
1055
+ function getExposedJSRPCBinding(request, env) {
1056
+ const url = new URL(request.url);
1057
+ const bindingName = url.searchParams.get("MF-Binding");
1058
+ if (!bindingName) {
1059
+ throw new BindingNotFoundError();
1060
+ }
1061
+ const targetBinding = env[bindingName];
1062
+ if (!targetBinding) {
1063
+ throw new BindingNotFoundError(bindingName);
1064
+ }
1065
+ if (targetBinding.constructor.name === "SendEmail") {
1066
+ return {
1067
+ async send(e) {
1068
+ const message = new EmailMessage(e.from, e.to, e["EmailMessage::raw"]);
1069
+ return targetBinding.send(message);
1070
+ }
1071
+ };
1072
+ }
1073
+ if (url.searchParams.has("MF-Dispatch-Namespace-Options")) {
1074
+ const { name, args, options } = JSON.parse(
1075
+ url.searchParams.get("MF-Dispatch-Namespace-Options")
1076
+ );
1077
+ return targetBinding.get(name, args, options);
1078
+ }
1079
+ return targetBinding;
1080
+ }
1081
+ function getExposedFetcher(request, env) {
1082
+ const bindingName = request.headers.get("MF-Binding");
1083
+ if (!bindingName) {
1084
+ throw new BindingNotFoundError();
1085
+ }
1086
+ const targetBinding = env[bindingName];
1087
+ if (!targetBinding) {
1088
+ throw new BindingNotFoundError(bindingName);
1089
+ }
1090
+ const dispatchNamespaceOptions = request.headers.get(
1091
+ "MF-Dispatch-Namespace-Options"
1092
+ );
1093
+ if (dispatchNamespaceOptions) {
1094
+ const { name, args, options } = JSON.parse(dispatchNamespaceOptions);
1095
+ return targetBinding.get(name, args, options);
1096
+ }
1097
+ return targetBinding;
1098
+ }
1099
+ function isJSRPCBinding(request) {
1100
+ const url = new URL(request.url);
1101
+ return request.headers.has("Upgrade") && url.searchParams.has("MF-Binding");
1102
+ }
1103
+ var ProxyServerWorker_default = {
1104
+ async fetch(request, env) {
1105
+ try {
1106
+ if (isJSRPCBinding(request)) {
1107
+ return We(
1108
+ request,
1109
+ getExposedJSRPCBinding(request, env)
1110
+ );
1111
+ } else {
1112
+ const fetcher = getExposedFetcher(request, env);
1113
+ const originalHeaders = new Headers();
1114
+ for (const [name, value] of request.headers) {
1115
+ if (name.startsWith("mf-header-")) {
1116
+ originalHeaders.set(name.slice("mf-header-".length), value);
1117
+ } else if (name === "upgrade") {
1118
+ originalHeaders.set(name, value);
1119
+ }
1120
+ }
1121
+ return fetcher.fetch(
1122
+ request.headers.get("MF-URL") ?? "http://example.com",
1123
+ new Request(request, {
1124
+ redirect: "manual",
1125
+ headers: originalHeaders
1126
+ })
1127
+ );
1128
+ }
1129
+ } catch (e) {
1130
+ if (e instanceof BindingNotFoundError) {
1131
+ return new Response(e.message, { status: 400 });
1132
+ }
1133
+ return new Response(e.message, { status: 500 });
1134
+ }
1135
+ }
1136
+ };
1137
+ export {
1138
+ ProxyServerWorker_default as default
1139
+ };