capnweb 0.0.0-0d6b2ff → 0.0.0-143d13d
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +112 -695
- package/dist/index-bun.cjs +650 -185
- package/dist/index-bun.cjs.map +1 -1
- package/dist/index-bun.d.cts +208 -39
- package/dist/index-bun.d.ts +208 -39
- package/dist/index-bun.js +648 -185
- package/dist/index-bun.js.map +1 -1
- package/dist/index-workers.cjs +651 -185
- package/dist/index-workers.cjs.map +1 -1
- package/dist/index-workers.d.cts +204 -38
- package/dist/index-workers.d.ts +204 -38
- package/dist/index-workers.js +648 -185
- package/dist/index-workers.js.map +1 -1
- package/dist/index.cjs +650 -185
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +204 -38
- package/dist/index.d.ts +204 -38
- package/dist/index.js +648 -185
- package/dist/index.js.map +1 -1
- package/package.json +25 -21
package/dist/index-bun.cjs
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
|
|
3
2
|
//#region src/symbols.ts
|
|
4
3
|
let WORKERS_MODULE_SYMBOL = Symbol("workers-module");
|
|
5
4
|
|
|
@@ -42,10 +41,25 @@ function typeForRpc(value) {
|
|
|
42
41
|
case AsyncFunction.prototype: return "function";
|
|
43
42
|
case Array.prototype: return "array";
|
|
44
43
|
case Date.prototype: return "date";
|
|
44
|
+
case RegExp.prototype: return "regexp";
|
|
45
|
+
case Set.prototype: return "set";
|
|
45
46
|
case Uint8Array.prototype:
|
|
46
|
-
case BUFFER_PROTOTYPE:
|
|
47
|
+
case BUFFER_PROTOTYPE:
|
|
48
|
+
case ArrayBuffer.prototype:
|
|
49
|
+
case DataView.prototype:
|
|
50
|
+
case Int8Array.prototype:
|
|
51
|
+
case Uint8ClampedArray.prototype:
|
|
52
|
+
case Int16Array.prototype:
|
|
53
|
+
case Uint16Array.prototype:
|
|
54
|
+
case Int32Array.prototype:
|
|
55
|
+
case Uint32Array.prototype:
|
|
56
|
+
case BigInt64Array.prototype:
|
|
57
|
+
case BigUint64Array.prototype:
|
|
58
|
+
case Float32Array.prototype:
|
|
59
|
+
case Float64Array.prototype: return "bytes";
|
|
47
60
|
case WritableStream.prototype: return "writable";
|
|
48
61
|
case ReadableStream.prototype: return "readable";
|
|
62
|
+
case URL.prototype: return "url";
|
|
49
63
|
case Headers.prototype: return "headers";
|
|
50
64
|
case Request.prototype: return "request";
|
|
51
65
|
case Response.prototype: return "response";
|
|
@@ -79,7 +93,14 @@ let streamImpl = {
|
|
|
79
93
|
};
|
|
80
94
|
var StubHook = class {
|
|
81
95
|
stream(path, args) {
|
|
82
|
-
let
|
|
96
|
+
let hook = this.call(path, args);
|
|
97
|
+
let pulled;
|
|
98
|
+
try {
|
|
99
|
+
pulled = hook.pull();
|
|
100
|
+
} catch (err) {
|
|
101
|
+
hook.dispose();
|
|
102
|
+
throw err;
|
|
103
|
+
}
|
|
83
104
|
let promise;
|
|
84
105
|
if (pulled instanceof Promise) promise = pulled.then((p) => {
|
|
85
106
|
p.dispose();
|
|
@@ -98,9 +119,11 @@ var ErrorStubHook = class extends StubHook {
|
|
|
98
119
|
this.error = error;
|
|
99
120
|
}
|
|
100
121
|
call(path, args) {
|
|
122
|
+
args.dispose();
|
|
101
123
|
return this;
|
|
102
124
|
}
|
|
103
125
|
map(path, captures, instructions) {
|
|
126
|
+
for (let cap of captures) cap.dispose();
|
|
104
127
|
return this;
|
|
105
128
|
}
|
|
106
129
|
get(path) {
|
|
@@ -224,7 +247,25 @@ var RpcStub$1 = class RpcStub$1 extends RpcTarget$1 {
|
|
|
224
247
|
};
|
|
225
248
|
var RpcPromise$1 = class extends RpcStub$1 {
|
|
226
249
|
constructor(hook, pathIfPromise) {
|
|
227
|
-
super(hook, pathIfPromise);
|
|
250
|
+
if (hook instanceof StubHook) super(hook, pathIfPromise ?? []);
|
|
251
|
+
else {
|
|
252
|
+
if (pathIfPromise !== void 0) throw new TypeError("RpcPromise constructor expected one argument, received two.");
|
|
253
|
+
let kind = typeForRpc(hook);
|
|
254
|
+
if (kind === "rpc-promise") {
|
|
255
|
+
let raw = unwrapStubAndPath(hook);
|
|
256
|
+
if (raw.pathIfPromise.length > 0) super(raw.hook, raw.pathIfPromise);
|
|
257
|
+
else {
|
|
258
|
+
let adopted = raw.hook;
|
|
259
|
+
raw.hook = DISPOSED_HOOK;
|
|
260
|
+
super(adopted, []);
|
|
261
|
+
}
|
|
262
|
+
} else if (kind === "rpc-thenable") super(TargetStubHook.create(hook, void 0), []);
|
|
263
|
+
else {
|
|
264
|
+
let promiseHook = new PromiseStubHook(Promise.resolve(hook).then((value) => new PayloadStubHook(RpcPayload.fromAppReturn(value))));
|
|
265
|
+
promiseHook.ignoreUnhandledRejections();
|
|
266
|
+
super(promiseHook, []);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
228
269
|
}
|
|
229
270
|
then(onfulfilled, onrejected) {
|
|
230
271
|
return pullPromise(this).then(...arguments);
|
|
@@ -319,12 +360,13 @@ var RpcPayload = class RpcPayload {
|
|
|
319
360
|
return TargetStubHook.create(target, parent);
|
|
320
361
|
} else if (this.source === "return") {
|
|
321
362
|
let hook = this.rpcTargets?.get(target);
|
|
322
|
-
if (hook)
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
363
|
+
if (hook) {
|
|
364
|
+
if (dupStubs) return hook.dup();
|
|
365
|
+
else {
|
|
366
|
+
this.rpcTargets?.delete(target);
|
|
367
|
+
return hook;
|
|
368
|
+
}
|
|
369
|
+
} else {
|
|
328
370
|
hook = TargetStubHook.create(target, parent);
|
|
329
371
|
if (dupStubs) {
|
|
330
372
|
if (!this.rpcTargets) this.rpcTargets = /* @__PURE__ */ new Map();
|
|
@@ -338,12 +380,13 @@ var RpcPayload = class RpcPayload {
|
|
|
338
380
|
if (this.source === "params") return streamImpl.createWritableStreamHook(stream);
|
|
339
381
|
else if (this.source === "return") {
|
|
340
382
|
let hook = this.rpcTargets?.get(stream);
|
|
341
|
-
if (hook)
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
383
|
+
if (hook) {
|
|
384
|
+
if (dupStubs) return hook.dup();
|
|
385
|
+
else {
|
|
386
|
+
this.rpcTargets?.delete(stream);
|
|
387
|
+
return hook;
|
|
388
|
+
}
|
|
389
|
+
} else {
|
|
347
390
|
hook = streamImpl.createWritableStreamHook(stream);
|
|
348
391
|
if (dupStubs) {
|
|
349
392
|
if (!this.rpcTargets) this.rpcTargets = /* @__PURE__ */ new Map();
|
|
@@ -357,12 +400,13 @@ var RpcPayload = class RpcPayload {
|
|
|
357
400
|
if (this.source === "params") return streamImpl.createReadableStreamHook(stream);
|
|
358
401
|
else if (this.source === "return") {
|
|
359
402
|
let hook = this.rpcTargets?.get(stream);
|
|
360
|
-
if (hook)
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
403
|
+
if (hook) {
|
|
404
|
+
if (dupStubs) return hook.dup();
|
|
405
|
+
else {
|
|
406
|
+
this.rpcTargets?.delete(stream);
|
|
407
|
+
return hook;
|
|
408
|
+
}
|
|
409
|
+
} else {
|
|
366
410
|
hook = streamImpl.createReadableStreamHook(stream);
|
|
367
411
|
if (dupStubs) {
|
|
368
412
|
if (!this.rpcTargets) this.rpcTargets = /* @__PURE__ */ new Map();
|
|
@@ -380,6 +424,8 @@ var RpcPayload = class RpcPayload {
|
|
|
380
424
|
case "date":
|
|
381
425
|
case "bytes":
|
|
382
426
|
case "blob":
|
|
427
|
+
case "url":
|
|
428
|
+
case "regexp":
|
|
383
429
|
case "error":
|
|
384
430
|
case "undefined": return value;
|
|
385
431
|
case "array": {
|
|
@@ -389,6 +435,21 @@ var RpcPayload = class RpcPayload {
|
|
|
389
435
|
for (let i = 0; i < len; i++) result[i] = this.deepCopy(array[i], array, i, result, dupStubs, owner);
|
|
390
436
|
return result;
|
|
391
437
|
}
|
|
438
|
+
case "set": {
|
|
439
|
+
let set = value;
|
|
440
|
+
let elements = [...set];
|
|
441
|
+
for (let val of elements) {
|
|
442
|
+
let kind = typeForRpc(val);
|
|
443
|
+
if (kind === "rpc-promise" || kind === "rpc-thenable") throw new TypeError("Cannot pass a promise as an element of a Set. Await the value before adding it to the Set.");
|
|
444
|
+
}
|
|
445
|
+
let result = /* @__PURE__ */ new Set();
|
|
446
|
+
let index = 0;
|
|
447
|
+
for (let val of elements) {
|
|
448
|
+
let copy = this.deepCopy(val, set, index++, result, dupStubs, owner);
|
|
449
|
+
result.add(copy);
|
|
450
|
+
}
|
|
451
|
+
return result;
|
|
452
|
+
}
|
|
392
453
|
case "object": {
|
|
393
454
|
let result = {};
|
|
394
455
|
let object = value;
|
|
@@ -473,6 +534,10 @@ var RpcPayload = class RpcPayload {
|
|
|
473
534
|
try {
|
|
474
535
|
this.value = this.deepCopy(this.value, void 0, "value", this, dupStubs, this);
|
|
475
536
|
} catch (err) {
|
|
537
|
+
if (dupStubs) {
|
|
538
|
+
this.hooks.forEach((hook) => hook.dispose());
|
|
539
|
+
this.promises.forEach((promise) => promise.promise[Symbol.dispose]());
|
|
540
|
+
}
|
|
476
541
|
this.hooks = void 0;
|
|
477
542
|
this.promises = void 0;
|
|
478
543
|
throw err;
|
|
@@ -513,6 +578,30 @@ var RpcPayload = class RpcPayload {
|
|
|
513
578
|
this.dispose();
|
|
514
579
|
}
|
|
515
580
|
}
|
|
581
|
+
async deliverStreamWrite(writer) {
|
|
582
|
+
try {
|
|
583
|
+
let promises = [];
|
|
584
|
+
this.deliverTo(this, "value", promises);
|
|
585
|
+
if (promises.length > 0) await Promise.all(promises);
|
|
586
|
+
let chunk = this.value[0];
|
|
587
|
+
if ((this.hooks.length > 0 || this.promises.length > 0) && chunk instanceof Object) {
|
|
588
|
+
if (!(Symbol.dispose in chunk)) Object.defineProperty(chunk, Symbol.dispose, {
|
|
589
|
+
value: () => this.dispose(),
|
|
590
|
+
writable: true,
|
|
591
|
+
enumerable: false,
|
|
592
|
+
configurable: true
|
|
593
|
+
});
|
|
594
|
+
await writer.write(chunk);
|
|
595
|
+
return RpcPayload.fromAppReturn(void 0);
|
|
596
|
+
}
|
|
597
|
+
await writer.write(chunk);
|
|
598
|
+
this.dispose();
|
|
599
|
+
return RpcPayload.fromAppReturn(void 0);
|
|
600
|
+
} catch (err) {
|
|
601
|
+
this.dispose();
|
|
602
|
+
throw err;
|
|
603
|
+
}
|
|
604
|
+
}
|
|
516
605
|
async deliverResolve() {
|
|
517
606
|
try {
|
|
518
607
|
let promises = [];
|
|
@@ -553,6 +642,8 @@ var RpcPayload = class RpcPayload {
|
|
|
553
642
|
case "bytes":
|
|
554
643
|
case "blob":
|
|
555
644
|
case "date":
|
|
645
|
+
case "url":
|
|
646
|
+
case "regexp":
|
|
556
647
|
case "error":
|
|
557
648
|
case "undefined": return;
|
|
558
649
|
case "array": {
|
|
@@ -561,6 +652,11 @@ var RpcPayload = class RpcPayload {
|
|
|
561
652
|
for (let i = 0; i < len; i++) this.disposeImpl(array[i], array);
|
|
562
653
|
return;
|
|
563
654
|
}
|
|
655
|
+
case "set": {
|
|
656
|
+
let set = value;
|
|
657
|
+
for (let element of set) this.disposeImpl(element, set);
|
|
658
|
+
return;
|
|
659
|
+
}
|
|
564
660
|
case "object": {
|
|
565
661
|
let object = value;
|
|
566
662
|
for (let i in object) this.disposeImpl(object[i], object);
|
|
@@ -635,6 +731,8 @@ var RpcPayload = class RpcPayload {
|
|
|
635
731
|
case "rpc-target":
|
|
636
732
|
case "writable":
|
|
637
733
|
case "readable":
|
|
734
|
+
case "url":
|
|
735
|
+
case "regexp":
|
|
638
736
|
case "headers":
|
|
639
737
|
case "request":
|
|
640
738
|
case "response": return;
|
|
@@ -644,6 +742,11 @@ var RpcPayload = class RpcPayload {
|
|
|
644
742
|
for (let i = 0; i < len; i++) this.ignoreUnhandledRejectionsImpl(array[i]);
|
|
645
743
|
return;
|
|
646
744
|
}
|
|
745
|
+
case "set": {
|
|
746
|
+
let set = value;
|
|
747
|
+
for (let element of set) this.ignoreUnhandledRejectionsImpl(element);
|
|
748
|
+
return;
|
|
749
|
+
}
|
|
647
750
|
case "object": {
|
|
648
751
|
let object = value;
|
|
649
752
|
for (let i in object) this.ignoreUnhandledRejectionsImpl(object[i]);
|
|
@@ -703,7 +806,10 @@ function followPath(value, parent, path, owner) {
|
|
|
703
806
|
case "bytes":
|
|
704
807
|
case "blob":
|
|
705
808
|
case "date":
|
|
809
|
+
case "set":
|
|
706
810
|
case "error":
|
|
811
|
+
case "url":
|
|
812
|
+
case "regexp":
|
|
707
813
|
case "headers":
|
|
708
814
|
case "request":
|
|
709
815
|
case "response":
|
|
@@ -736,17 +842,23 @@ function followPath(value, parent, path, owner) {
|
|
|
736
842
|
}
|
|
737
843
|
var ValueStubHook = class extends StubHook {
|
|
738
844
|
call(path, args) {
|
|
845
|
+
let followResult;
|
|
739
846
|
try {
|
|
740
847
|
let { value, owner } = this.getValue();
|
|
741
|
-
|
|
742
|
-
if (followResult.hook) return followResult.hook.call(followResult.remainingPath, args);
|
|
743
|
-
if (typeof followResult.value != "function") throw new TypeError(`'${path.join(".")}' is not a function.`);
|
|
744
|
-
return new PromiseStubHook(args.deliverCall(followResult.value, followResult.parent).then((payload) => {
|
|
745
|
-
return new PayloadStubHook(payload);
|
|
746
|
-
}));
|
|
848
|
+
followResult = followPath(value, void 0, path, owner);
|
|
747
849
|
} catch (err) {
|
|
850
|
+
args.dispose();
|
|
748
851
|
return new ErrorStubHook(err);
|
|
749
852
|
}
|
|
853
|
+
if (followResult.hook) return followResult.hook.call(followResult.remainingPath, args);
|
|
854
|
+
if (typeof followResult.value != "function") {
|
|
855
|
+
args.dispose();
|
|
856
|
+
return new ErrorStubHook(/* @__PURE__ */ new TypeError(`'${path.join(".")}' is not a function.`));
|
|
857
|
+
}
|
|
858
|
+
let promise = args.deliverCall(followResult.value, followResult.parent);
|
|
859
|
+
return new PromiseStubHook(promise.then((payload) => {
|
|
860
|
+
return new PayloadStubHook(payload);
|
|
861
|
+
}));
|
|
750
862
|
}
|
|
751
863
|
map(path, captures, instructions) {
|
|
752
864
|
try {
|
|
@@ -767,7 +879,10 @@ var ValueStubHook = class extends StubHook {
|
|
|
767
879
|
get(path) {
|
|
768
880
|
try {
|
|
769
881
|
let { value, owner } = this.getValue();
|
|
770
|
-
if (path.length === 0 && owner === null)
|
|
882
|
+
if (path.length === 0 && owner === null) {
|
|
883
|
+
if (value instanceof Object && "then" in value) return this.dup();
|
|
884
|
+
throw new Error("Can't dup an RpcTarget stub as a promise.");
|
|
885
|
+
}
|
|
771
886
|
let followResult = followPath(value, void 0, path, owner);
|
|
772
887
|
if (followResult.hook) return followResult.hook.get(followResult.remainingPath);
|
|
773
888
|
return new PayloadStubHook(RpcPayload.deepCopyFrom(followResult.value, followResult.parent, followResult.owner));
|
|
@@ -870,7 +985,10 @@ var TargetStubHook = class TargetStubHook extends ValueStubHook {
|
|
|
870
985
|
this.target = void 0;
|
|
871
986
|
}
|
|
872
987
|
}
|
|
873
|
-
onBroken(callback) {
|
|
988
|
+
onBroken(callback) {
|
|
989
|
+
let target = this.target;
|
|
990
|
+
if (target && "then" in target) Promise.resolve(target).then(() => {}, callback);
|
|
991
|
+
}
|
|
874
992
|
};
|
|
875
993
|
var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
876
994
|
promise;
|
|
@@ -884,12 +1002,16 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
|
884
1002
|
}
|
|
885
1003
|
call(path, args) {
|
|
886
1004
|
args.ensureDeepCopied();
|
|
887
|
-
return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args))
|
|
1005
|
+
return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args), (err) => {
|
|
1006
|
+
args.dispose();
|
|
1007
|
+
throw err;
|
|
1008
|
+
}));
|
|
888
1009
|
}
|
|
889
1010
|
stream(path, args) {
|
|
890
1011
|
args.ensureDeepCopied();
|
|
891
|
-
return { promise: this.promise.then((hook) => {
|
|
892
|
-
|
|
1012
|
+
return { promise: this.promise.then((hook) => hook.stream(path, args).promise, (err) => {
|
|
1013
|
+
args.dispose();
|
|
1014
|
+
throw err;
|
|
893
1015
|
}) };
|
|
894
1016
|
}
|
|
895
1017
|
map(path, captures, instructions) {
|
|
@@ -916,10 +1038,7 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
|
916
1038
|
}, (err) => {});
|
|
917
1039
|
}
|
|
918
1040
|
dispose() {
|
|
919
|
-
|
|
920
|
-
else this.promise.then((hook) => {
|
|
921
|
-
hook.dispose();
|
|
922
|
-
}, (err) => {});
|
|
1041
|
+
this.promise.then((hook) => hook.dispose(), () => {});
|
|
923
1042
|
}
|
|
924
1043
|
onBroken(callback) {
|
|
925
1044
|
if (this.resolution) this.resolution.onBroken(callback);
|
|
@@ -931,6 +1050,75 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
|
931
1050
|
|
|
932
1051
|
//#endregion
|
|
933
1052
|
//#region src/serialize.ts
|
|
1053
|
+
const DEFAULT_MAX_DEPTH = 256;
|
|
1054
|
+
const DEFAULT_LIMITS = {
|
|
1055
|
+
maxBigIntDigits: 16384,
|
|
1056
|
+
maxDepth: 256,
|
|
1057
|
+
maxMessageSize: 33554432
|
|
1058
|
+
};
|
|
1059
|
+
const NATIVE_LITTLE_ENDIAN = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
|
|
1060
|
+
const BYTE_CONTAINER_TYPE_NAMES = [
|
|
1061
|
+
"ArrayBuffer",
|
|
1062
|
+
"DataView",
|
|
1063
|
+
"Int8Array",
|
|
1064
|
+
"Uint8Array",
|
|
1065
|
+
"Uint8ClampedArray",
|
|
1066
|
+
"Int16Array",
|
|
1067
|
+
"Uint16Array",
|
|
1068
|
+
"Int32Array",
|
|
1069
|
+
"Uint32Array",
|
|
1070
|
+
"BigInt64Array",
|
|
1071
|
+
"BigUint64Array",
|
|
1072
|
+
"Float32Array",
|
|
1073
|
+
"Float64Array"
|
|
1074
|
+
];
|
|
1075
|
+
function isValidByteContainerName(value) {
|
|
1076
|
+
return BYTE_CONTAINER_TYPE_NAMES.includes(value);
|
|
1077
|
+
}
|
|
1078
|
+
const TYPED_ARRAY_ELEMENT_SIZE = {
|
|
1079
|
+
ArrayBuffer: void 0,
|
|
1080
|
+
DataView: void 0,
|
|
1081
|
+
Int8Array: void 0,
|
|
1082
|
+
Uint8Array: void 0,
|
|
1083
|
+
Uint8ClampedArray: void 0,
|
|
1084
|
+
Int16Array: 2,
|
|
1085
|
+
Uint16Array: 2,
|
|
1086
|
+
Int32Array: 4,
|
|
1087
|
+
Uint32Array: 4,
|
|
1088
|
+
BigInt64Array: 8,
|
|
1089
|
+
BigUint64Array: 8,
|
|
1090
|
+
Float32Array: 4,
|
|
1091
|
+
Float64Array: 8
|
|
1092
|
+
};
|
|
1093
|
+
const BYTE_CONTAINER_PROTOTYPES = {
|
|
1094
|
+
ArrayBuffer: ArrayBuffer.prototype,
|
|
1095
|
+
DataView: DataView.prototype,
|
|
1096
|
+
Int8Array: Int8Array.prototype,
|
|
1097
|
+
Uint8ClampedArray: Uint8ClampedArray.prototype,
|
|
1098
|
+
Int16Array: Int16Array.prototype,
|
|
1099
|
+
Uint16Array: Uint16Array.prototype,
|
|
1100
|
+
Int32Array: Int32Array.prototype,
|
|
1101
|
+
Uint32Array: Uint32Array.prototype,
|
|
1102
|
+
BigInt64Array: BigInt64Array.prototype,
|
|
1103
|
+
BigUint64Array: BigUint64Array.prototype,
|
|
1104
|
+
Float32Array: Float32Array.prototype,
|
|
1105
|
+
Float64Array: Float64Array.prototype
|
|
1106
|
+
};
|
|
1107
|
+
const BYTE_CONTAINER_TYPE_BY_PROTOTYPE = /* @__PURE__ */ new Map();
|
|
1108
|
+
for (let type of Object.keys(BYTE_CONTAINER_PROTOTYPES)) BYTE_CONTAINER_TYPE_BY_PROTOTYPE.set(BYTE_CONTAINER_PROTOTYPES[type], type);
|
|
1109
|
+
function swapByteOrder(bytes, elementSize) {
|
|
1110
|
+
if (elementSize !== 2 && elementSize !== 4 && elementSize !== 8) throw new RangeError(`Unsupported element size: ${elementSize}`);
|
|
1111
|
+
let view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
1112
|
+
for (let offset = 0; offset < bytes.byteLength; offset += elementSize) switch (elementSize) {
|
|
1113
|
+
case 2:
|
|
1114
|
+
view.setUint16(offset, view.getUint16(offset, false), true);
|
|
1115
|
+
break;
|
|
1116
|
+
case 4:
|
|
1117
|
+
view.setUint32(offset, view.getUint32(offset, false), true);
|
|
1118
|
+
break;
|
|
1119
|
+
case 8: view.setBigUint64(offset, view.getBigUint64(offset, false), true);
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
934
1122
|
var NullExporter = class {
|
|
935
1123
|
exportStub(stub) {
|
|
936
1124
|
throw new Error("Cannot serialize RPC stubs without an RPC session.");
|
|
@@ -951,6 +1139,7 @@ async function streamToBlob(stream, type) {
|
|
|
951
1139
|
return b.type === type ? b : b.slice(0, b.size, type);
|
|
952
1140
|
}
|
|
953
1141
|
const ERROR_TYPES = {
|
|
1142
|
+
__proto__: null,
|
|
954
1143
|
Error,
|
|
955
1144
|
EvalError,
|
|
956
1145
|
RangeError,
|
|
@@ -963,12 +1152,14 @@ const ERROR_TYPES = {
|
|
|
963
1152
|
var Devaluator = class Devaluator {
|
|
964
1153
|
exporter;
|
|
965
1154
|
source;
|
|
966
|
-
|
|
1155
|
+
encodingLevel;
|
|
1156
|
+
constructor(exporter, source, encodingLevel) {
|
|
967
1157
|
this.exporter = exporter;
|
|
968
1158
|
this.source = source;
|
|
1159
|
+
this.encodingLevel = encodingLevel;
|
|
969
1160
|
}
|
|
970
|
-
static devaluate(value, parent, exporter = NULL_EXPORTER, source) {
|
|
971
|
-
let devaluator = new Devaluator(exporter, source);
|
|
1161
|
+
static devaluate(value, parent, exporter = NULL_EXPORTER, source, encodingLevel = "string") {
|
|
1162
|
+
let devaluator = new Devaluator(exporter, source, encodingLevel);
|
|
972
1163
|
try {
|
|
973
1164
|
return devaluator.devaluateImpl(value, parent, 0);
|
|
974
1165
|
} catch (err) {
|
|
@@ -980,7 +1171,7 @@ var Devaluator = class Devaluator {
|
|
|
980
1171
|
}
|
|
981
1172
|
exports;
|
|
982
1173
|
devaluateImpl(value, parent, depth) {
|
|
983
|
-
if (depth >=
|
|
1174
|
+
if (depth >= 256) throw new Error("Serialization exceeded maximum allowed depth. (Does the message contain cycles?)");
|
|
984
1175
|
switch (typeForRpc(value)) {
|
|
985
1176
|
case "unsupported": {
|
|
986
1177
|
let msg;
|
|
@@ -991,10 +1182,12 @@ var Devaluator = class Devaluator {
|
|
|
991
1182
|
}
|
|
992
1183
|
throw new TypeError(msg);
|
|
993
1184
|
}
|
|
994
|
-
case "primitive": if (typeof value === "number" && !isFinite(value))
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
1185
|
+
case "primitive": if (typeof value === "number" && !isFinite(value)) {
|
|
1186
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1187
|
+
if (value === Infinity) return ["inf"];
|
|
1188
|
+
else if (value === -Infinity) return ["-inf"];
|
|
1189
|
+
else return ["nan"];
|
|
1190
|
+
} else return value;
|
|
998
1191
|
case "object": {
|
|
999
1192
|
let object = value;
|
|
1000
1193
|
let result = {};
|
|
@@ -1008,23 +1201,73 @@ var Devaluator = class Devaluator {
|
|
|
1008
1201
|
for (let i = 0; i < len; i++) result[i] = this.devaluateImpl(array[i], array, depth + 1);
|
|
1009
1202
|
return [result];
|
|
1010
1203
|
}
|
|
1011
|
-
case "
|
|
1204
|
+
case "set": {
|
|
1205
|
+
let set = value;
|
|
1206
|
+
let setElements = [...set];
|
|
1207
|
+
let elements = [];
|
|
1208
|
+
for (let element of setElements) switch (typeForRpc(element)) {
|
|
1209
|
+
case "blob": throw new TypeError("Cannot serialize a Blob as an element of a Set.");
|
|
1210
|
+
case "rpc-promise":
|
|
1211
|
+
case "rpc-thenable": throw new TypeError("Cannot serialize a promise as an element of a Set. Await the value before adding it to the Set.");
|
|
1212
|
+
}
|
|
1213
|
+
for (let element of setElements) elements.push(this.devaluateImpl(element, set, depth + 1));
|
|
1214
|
+
return ["set", elements];
|
|
1215
|
+
}
|
|
1216
|
+
case "bigint":
|
|
1217
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1218
|
+
return ["bigint", value.toString()];
|
|
1012
1219
|
case "date": {
|
|
1220
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1013
1221
|
const time = value.getTime();
|
|
1014
1222
|
return ["date", Number.isNaN(time) ? null : time];
|
|
1015
1223
|
}
|
|
1224
|
+
case "regexp": {
|
|
1225
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1226
|
+
let re = value;
|
|
1227
|
+
return re.flags ? [
|
|
1228
|
+
"regexp",
|
|
1229
|
+
re.source,
|
|
1230
|
+
re.flags
|
|
1231
|
+
] : ["regexp", re.source];
|
|
1232
|
+
}
|
|
1016
1233
|
case "bytes": {
|
|
1017
|
-
let
|
|
1018
|
-
|
|
1234
|
+
let alternateTypeName = BYTE_CONTAINER_TYPE_BY_PROTOTYPE.get(Object.getPrototypeOf(value));
|
|
1235
|
+
let bytes;
|
|
1236
|
+
if (alternateTypeName === "ArrayBuffer") bytes = new Uint8Array(value);
|
|
1237
|
+
else if (alternateTypeName === void 0) bytes = value;
|
|
1238
|
+
else {
|
|
1239
|
+
let view = value;
|
|
1240
|
+
bytes = new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
|
|
1241
|
+
let elementSize = TYPED_ARRAY_ELEMENT_SIZE[alternateTypeName];
|
|
1242
|
+
if (!NATIVE_LITTLE_ENDIAN && elementSize) {
|
|
1243
|
+
bytes = bytes.slice();
|
|
1244
|
+
swapByteOrder(bytes, elementSize);
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
if (this.encodingLevel === "structuredClonable" || this.encodingLevel === "jsonCompatibleWithBytes") {
|
|
1248
|
+
if (bytes.byteLength !== bytes.buffer.byteLength) bytes = new Uint8Array(bytes);
|
|
1249
|
+
return alternateTypeName === void 0 ? ["bytes", bytes] : [
|
|
1250
|
+
"bytes",
|
|
1251
|
+
bytes,
|
|
1252
|
+
alternateTypeName
|
|
1253
|
+
];
|
|
1254
|
+
}
|
|
1019
1255
|
let b64;
|
|
1020
|
-
if (
|
|
1256
|
+
if (bytes.toBase64) b64 = bytes.toBase64({ omitPadding: true });
|
|
1257
|
+
else if (typeof Buffer !== "undefined") b64 = (bytes instanceof Buffer ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)).toString("base64");
|
|
1021
1258
|
else {
|
|
1022
1259
|
let binary = "";
|
|
1023
1260
|
for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]);
|
|
1024
1261
|
b64 = btoa(binary);
|
|
1025
1262
|
}
|
|
1026
|
-
|
|
1263
|
+
b64 = b64.replace(/=+$/, "");
|
|
1264
|
+
return alternateTypeName === void 0 ? ["bytes", b64] : [
|
|
1265
|
+
"bytes",
|
|
1266
|
+
b64,
|
|
1267
|
+
alternateTypeName
|
|
1268
|
+
];
|
|
1027
1269
|
}
|
|
1270
|
+
case "url": return ["url", value.href];
|
|
1028
1271
|
case "headers": return ["headers", [...value]];
|
|
1029
1272
|
case "request": {
|
|
1030
1273
|
let req = value;
|
|
@@ -1139,19 +1382,24 @@ var Devaluator = class Devaluator {
|
|
|
1139
1382
|
} else if (rewritten && rewritten.stack) result.push(rewritten.stack);
|
|
1140
1383
|
return result;
|
|
1141
1384
|
}
|
|
1142
|
-
case "undefined":
|
|
1385
|
+
case "undefined":
|
|
1386
|
+
if (this.encodingLevel === "structuredClonable") return;
|
|
1387
|
+
return ["undefined"];
|
|
1143
1388
|
case "stub":
|
|
1144
1389
|
case "rpc-promise": {
|
|
1145
1390
|
if (!this.source) throw new Error("Can't serialize RPC stubs in this context.");
|
|
1146
1391
|
let { hook, pathIfPromise } = unwrapStubAndPath(value);
|
|
1147
1392
|
let importId = this.exporter.getImport(hook);
|
|
1148
|
-
if (importId !== void 0)
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1393
|
+
if (importId !== void 0) {
|
|
1394
|
+
if (pathIfPromise) {
|
|
1395
|
+
if (pathIfPromise.length > 0) return [
|
|
1396
|
+
"pipeline",
|
|
1397
|
+
importId,
|
|
1398
|
+
pathIfPromise
|
|
1399
|
+
];
|
|
1400
|
+
else return ["pipeline", importId];
|
|
1401
|
+
} else return ["import", importId];
|
|
1402
|
+
}
|
|
1155
1403
|
if (pathIfPromise) hook = hook.get(pathIfPromise);
|
|
1156
1404
|
else hook = hook.dup();
|
|
1157
1405
|
return this.devaluateHook(pathIfPromise ? "promise" : "export", hook);
|
|
@@ -1206,31 +1454,43 @@ var NullImporter = class {
|
|
|
1206
1454
|
getPipeReadable(exportId) {
|
|
1207
1455
|
throw new Error("Cannot retrieve pipe readable without an RPC session.");
|
|
1208
1456
|
}
|
|
1457
|
+
getLimits() {
|
|
1458
|
+
return DEFAULT_LIMITS;
|
|
1459
|
+
}
|
|
1209
1460
|
};
|
|
1210
1461
|
const NULL_IMPORTER = new NullImporter();
|
|
1211
1462
|
function fixBrokenRequestBody(request, body) {
|
|
1212
|
-
|
|
1463
|
+
let promise = new Response(body).arrayBuffer().then((arrayBuffer) => {
|
|
1213
1464
|
let bytes = new Uint8Array(arrayBuffer);
|
|
1214
1465
|
let result = new Request(request, { body: bytes });
|
|
1215
1466
|
return new PayloadStubHook(RpcPayload.fromAppReturn(result));
|
|
1216
|
-
})
|
|
1467
|
+
});
|
|
1468
|
+
return new RpcPromise$1(new PromiseStubHook(promise), []);
|
|
1217
1469
|
}
|
|
1218
1470
|
function streamToBlobPromise(stream, type) {
|
|
1219
|
-
|
|
1471
|
+
let promise = streamToBlob(stream, type).then((blob) => {
|
|
1220
1472
|
return new PayloadStubHook(RpcPayload.fromAppReturn(blob));
|
|
1221
|
-
})
|
|
1473
|
+
});
|
|
1474
|
+
return new RpcPromise$1(new PromiseStubHook(promise), []);
|
|
1222
1475
|
}
|
|
1223
1476
|
var Evaluator = class Evaluator {
|
|
1224
1477
|
importer;
|
|
1225
|
-
|
|
1478
|
+
encodingLevel;
|
|
1479
|
+
limits;
|
|
1480
|
+
constructor(importer, encodingLevel = "string") {
|
|
1226
1481
|
this.importer = importer;
|
|
1482
|
+
this.encodingLevel = encodingLevel;
|
|
1483
|
+
this.limits = importer.getLimits();
|
|
1227
1484
|
}
|
|
1228
1485
|
hooks = [];
|
|
1229
1486
|
promises = [];
|
|
1230
1487
|
evaluate(value) {
|
|
1488
|
+
return this.evaluateWithDepth(value, 0);
|
|
1489
|
+
}
|
|
1490
|
+
evaluateWithDepth(value, depth) {
|
|
1231
1491
|
let payload = RpcPayload.forEvaluate(this.hooks, this.promises);
|
|
1232
1492
|
try {
|
|
1233
|
-
payload.value = this.evaluateImpl(value, payload, "value");
|
|
1493
|
+
payload.value = this.evaluateImpl(value, payload, "value", depth);
|
|
1234
1494
|
return payload;
|
|
1235
1495
|
} catch (err) {
|
|
1236
1496
|
payload.dispose();
|
|
@@ -1240,31 +1500,85 @@ var Evaluator = class Evaluator {
|
|
|
1240
1500
|
evaluateCopy(value) {
|
|
1241
1501
|
return this.evaluate(structuredClone(value));
|
|
1242
1502
|
}
|
|
1243
|
-
evaluateImpl(value, parent, property) {
|
|
1503
|
+
evaluateImpl(value, parent, property, depth) {
|
|
1504
|
+
let maxDepth = this.limits.maxDepth;
|
|
1505
|
+
if (depth >= maxDepth) throw new TypeError(`Deserialization exceeded maximum allowed message depth of ${maxDepth}.`);
|
|
1506
|
+
if (this.encodingLevel === "structuredClonable") {
|
|
1507
|
+
if (value instanceof Date || value instanceof RegExp || typeof value === "bigint") return value;
|
|
1508
|
+
}
|
|
1244
1509
|
if (value instanceof Array) {
|
|
1245
1510
|
if (value.length == 1 && value[0] instanceof Array) {
|
|
1246
1511
|
let result = value[0];
|
|
1247
|
-
for (let i = 0; i < result.length; i++) result[i] = this.evaluateImpl(result[i], result, i);
|
|
1512
|
+
for (let i = 0; i < result.length; i++) result[i] = this.evaluateImpl(result[i], result, i, depth + 1);
|
|
1248
1513
|
return result;
|
|
1249
1514
|
} else switch (value[0]) {
|
|
1250
1515
|
case "bigint":
|
|
1251
|
-
if (typeof value[1] == "string")
|
|
1516
|
+
if (typeof value[1] == "string") {
|
|
1517
|
+
let digits = value[1];
|
|
1518
|
+
let maxBigIntDigits = this.limits.maxBigIntDigits;
|
|
1519
|
+
if (digits.length > maxBigIntDigits) throw new TypeError(`Deserialized bigint exceeds maximum length of ${maxBigIntDigits} digits.`);
|
|
1520
|
+
return BigInt(digits);
|
|
1521
|
+
}
|
|
1252
1522
|
break;
|
|
1253
1523
|
case "date":
|
|
1254
1524
|
if (value[1] === null) return /* @__PURE__ */ new Date(NaN);
|
|
1255
1525
|
if (typeof value[1] == "number") return new Date(value[1]);
|
|
1256
1526
|
break;
|
|
1257
|
-
case "
|
|
1258
|
-
if (typeof value[1]
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
let
|
|
1263
|
-
let
|
|
1264
|
-
for (let i = 0; i <
|
|
1265
|
-
|
|
1527
|
+
case "regexp":
|
|
1528
|
+
if (typeof value[1] === "string" && (value.length === 2 || value.length === 3 && typeof value[2] === "string")) return new RegExp(value[1], value[2]);
|
|
1529
|
+
break;
|
|
1530
|
+
case "set":
|
|
1531
|
+
if (value.length === 2 && value[1] instanceof Array) {
|
|
1532
|
+
let elements = value[1];
|
|
1533
|
+
let set = /* @__PURE__ */ new Set();
|
|
1534
|
+
for (let i = 0; i < elements.length; i++) {
|
|
1535
|
+
let copy = this.evaluateImpl(elements[i], set, i, depth + 1);
|
|
1536
|
+
if (copy instanceof RpcPromise$1) throw new TypeError("Cannot deserialize a promise as an element of a Set.");
|
|
1537
|
+
set.add(copy);
|
|
1538
|
+
}
|
|
1539
|
+
return set;
|
|
1266
1540
|
}
|
|
1267
1541
|
break;
|
|
1542
|
+
case "bytes": {
|
|
1543
|
+
let bytes;
|
|
1544
|
+
if (value[1] instanceof Uint8Array) bytes = value[1];
|
|
1545
|
+
else if (typeof value[1] == "string") {
|
|
1546
|
+
if (typeof Buffer !== "undefined") bytes = Buffer.from(value[1], "base64");
|
|
1547
|
+
else if (Uint8Array.fromBase64) bytes = Uint8Array.fromBase64(value[1]);
|
|
1548
|
+
else {
|
|
1549
|
+
let bs = atob(value[1]);
|
|
1550
|
+
let len = bs.length;
|
|
1551
|
+
bytes = new Uint8Array(len);
|
|
1552
|
+
for (let i = 0; i < len; i++) bytes[i] = bs.charCodeAt(i);
|
|
1553
|
+
}
|
|
1554
|
+
} else break;
|
|
1555
|
+
if (value.length === 2) return bytes;
|
|
1556
|
+
if (typeof value[2] !== "string") throw new TypeError(`Unknown bytes type marker type: ${typeof value[2]}`);
|
|
1557
|
+
if (!isValidByteContainerName(value[2])) {
|
|
1558
|
+
let marker = value[2].slice(0, 64);
|
|
1559
|
+
throw new TypeError(`Unknown bytes type marker: ${marker}`);
|
|
1560
|
+
}
|
|
1561
|
+
let marker = value[2];
|
|
1562
|
+
let elementSize = TYPED_ARRAY_ELEMENT_SIZE[marker];
|
|
1563
|
+
if (elementSize !== void 0 && bytes.byteLength % elementSize !== 0) throw new TypeError(`Invalid byte length ${bytes.byteLength} for ${marker}; expected a multiple of ${elementSize}`);
|
|
1564
|
+
let buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
|
1565
|
+
if (!NATIVE_LITTLE_ENDIAN && elementSize !== void 0) swapByteOrder(new Uint8Array(buffer), elementSize);
|
|
1566
|
+
switch (marker) {
|
|
1567
|
+
case "ArrayBuffer": return buffer;
|
|
1568
|
+
case "DataView": return new DataView(buffer);
|
|
1569
|
+
case "Int8Array": return new Int8Array(buffer);
|
|
1570
|
+
case "Uint8Array": return new Uint8Array(buffer);
|
|
1571
|
+
case "Uint8ClampedArray": return new Uint8ClampedArray(buffer);
|
|
1572
|
+
case "Int16Array": return new Int16Array(buffer);
|
|
1573
|
+
case "Uint16Array": return new Uint16Array(buffer);
|
|
1574
|
+
case "Int32Array": return new Int32Array(buffer);
|
|
1575
|
+
case "Uint32Array": return new Uint32Array(buffer);
|
|
1576
|
+
case "BigInt64Array": return new BigInt64Array(buffer);
|
|
1577
|
+
case "BigUint64Array": return new BigUint64Array(buffer);
|
|
1578
|
+
case "Float32Array": return new Float32Array(buffer);
|
|
1579
|
+
case "Float64Array": return new Float64Array(buffer);
|
|
1580
|
+
}
|
|
1581
|
+
}
|
|
1268
1582
|
case "error":
|
|
1269
1583
|
if (value.length >= 3 && typeof value[1] === "string" && typeof value[2] === "string") {
|
|
1270
1584
|
let cls = ERROR_TYPES[value[1]] || Error;
|
|
@@ -1277,7 +1591,11 @@ var Evaluator = class Evaluator {
|
|
|
1277
1591
|
let propsObj = props;
|
|
1278
1592
|
for (let key of Object.keys(propsObj)) {
|
|
1279
1593
|
if (key === "name" || key === "message" || key === "stack") continue;
|
|
1280
|
-
|
|
1594
|
+
if (key in Object.prototype || key === "toJSON") {
|
|
1595
|
+
this.evaluateImpl(propsObj[key], result, key, depth + 1);
|
|
1596
|
+
continue;
|
|
1597
|
+
}
|
|
1598
|
+
anyResult[key] = this.evaluateImpl(propsObj[key], result, key, depth + 1);
|
|
1281
1599
|
}
|
|
1282
1600
|
}
|
|
1283
1601
|
return result;
|
|
@@ -1289,6 +1607,9 @@ var Evaluator = class Evaluator {
|
|
|
1289
1607
|
case "inf": return Infinity;
|
|
1290
1608
|
case "-inf": return -Infinity;
|
|
1291
1609
|
case "nan": return NaN;
|
|
1610
|
+
case "url":
|
|
1611
|
+
if (value.length === 2 && typeof value[1] === "string") return new URL(value[1]);
|
|
1612
|
+
break;
|
|
1292
1613
|
case "headers":
|
|
1293
1614
|
if (value.length === 2 && value[1] instanceof Array) return new Headers(value[1]);
|
|
1294
1615
|
break;
|
|
@@ -1298,11 +1619,11 @@ var Evaluator = class Evaluator {
|
|
|
1298
1619
|
let init = value[2];
|
|
1299
1620
|
if (typeof init !== "object" || init === null) break;
|
|
1300
1621
|
if (init.body) {
|
|
1301
|
-
init.body = this.evaluateImpl(init.body, init, "body");
|
|
1622
|
+
init.body = this.evaluateImpl(init.body, init, "body", depth + 1);
|
|
1302
1623
|
if (init.body === null || typeof init.body === "string" || init.body instanceof Uint8Array || init.body instanceof ReadableStream) {} else throw new TypeError("Request body must be of type ReadableStream.");
|
|
1303
1624
|
}
|
|
1304
1625
|
if (init.signal) {
|
|
1305
|
-
init.signal = this.evaluateImpl(init.signal, init, "signal");
|
|
1626
|
+
init.signal = this.evaluateImpl(init.signal, init, "signal", depth + 1);
|
|
1306
1627
|
if (!(init.signal instanceof AbortSignal)) throw new TypeError("Request siganl must be of type AbortSignal.");
|
|
1307
1628
|
}
|
|
1308
1629
|
if (init.headers && !(init.headers instanceof Array)) throw new TypeError("Request headers must be serialized as an array of pairs.");
|
|
@@ -1319,7 +1640,7 @@ var Evaluator = class Evaluator {
|
|
|
1319
1640
|
}
|
|
1320
1641
|
case "response": {
|
|
1321
1642
|
if (value.length !== 3) break;
|
|
1322
|
-
let body = this.evaluateImpl(value[1], parent, property);
|
|
1643
|
+
let body = this.evaluateImpl(value[1], parent, property, depth + 1);
|
|
1323
1644
|
if (body === null || typeof body === "string" || body instanceof Uint8Array || body instanceof ReadableStream) {} else throw new TypeError("Response body must be of type ReadableStream.");
|
|
1324
1645
|
let init = value[2];
|
|
1325
1646
|
if (typeof init !== "object" || init === null) break;
|
|
@@ -1330,7 +1651,7 @@ var Evaluator = class Evaluator {
|
|
|
1330
1651
|
case "blob": {
|
|
1331
1652
|
if (value.length !== 3 || typeof value[1] !== "string") break;
|
|
1332
1653
|
let contentType = value[1];
|
|
1333
|
-
let content = this.evaluateImpl(value[2], parent, property);
|
|
1654
|
+
let content = this.evaluateImpl(value[2], parent, property, depth + 1);
|
|
1334
1655
|
if (!(content instanceof ReadableStream)) throw new TypeError("Blob content must be serialized as a ReadableStream.");
|
|
1335
1656
|
let promise = streamToBlobPromise(content, contentType);
|
|
1336
1657
|
this.promises.push({
|
|
@@ -1358,11 +1679,13 @@ var Evaluator = class Evaluator {
|
|
|
1358
1679
|
return promise;
|
|
1359
1680
|
} else {
|
|
1360
1681
|
this.hooks.push(hook);
|
|
1361
|
-
return new
|
|
1682
|
+
return new RpcStub$1(hook);
|
|
1362
1683
|
}
|
|
1363
1684
|
};
|
|
1364
|
-
if (value.length == 2)
|
|
1365
|
-
|
|
1685
|
+
if (value.length == 2) {
|
|
1686
|
+
if (isPromise) return addStub(hook.get([]));
|
|
1687
|
+
else return addStub(hook.dup());
|
|
1688
|
+
}
|
|
1366
1689
|
let path = value[2];
|
|
1367
1690
|
if (!(path instanceof Array)) break;
|
|
1368
1691
|
if (!path.every((part) => {
|
|
@@ -1371,7 +1694,7 @@ var Evaluator = class Evaluator {
|
|
|
1371
1694
|
if (value.length == 3) return addStub(hook.get(path));
|
|
1372
1695
|
let args = value[3];
|
|
1373
1696
|
if (!(args instanceof Array)) break;
|
|
1374
|
-
args = new Evaluator(this.importer).
|
|
1697
|
+
args = new Evaluator(this.importer).evaluateWithDepth([args], depth);
|
|
1375
1698
|
return addStub(hook.call(path, args));
|
|
1376
1699
|
}
|
|
1377
1700
|
case "remap": {
|
|
@@ -1392,7 +1715,8 @@ var Evaluator = class Evaluator {
|
|
|
1392
1715
|
}
|
|
1393
1716
|
});
|
|
1394
1717
|
let instructions = value[4];
|
|
1395
|
-
let
|
|
1718
|
+
let resultHook = hook.map(path, captures, instructions);
|
|
1719
|
+
let promise = new RpcPromise$1(resultHook, []);
|
|
1396
1720
|
this.promises.push({
|
|
1397
1721
|
promise,
|
|
1398
1722
|
parent,
|
|
@@ -1402,18 +1726,21 @@ var Evaluator = class Evaluator {
|
|
|
1402
1726
|
}
|
|
1403
1727
|
case "export":
|
|
1404
1728
|
case "promise":
|
|
1405
|
-
if (typeof value[1] == "number")
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1729
|
+
if (typeof value[1] == "number") {
|
|
1730
|
+
if (value[0] == "promise") {
|
|
1731
|
+
let hook = this.importer.importPromise(value[1]);
|
|
1732
|
+
let promise = new RpcPromise$1(hook, []);
|
|
1733
|
+
this.promises.push({
|
|
1734
|
+
parent,
|
|
1735
|
+
property,
|
|
1736
|
+
promise
|
|
1737
|
+
});
|
|
1738
|
+
return promise;
|
|
1739
|
+
} else {
|
|
1740
|
+
let hook = this.importer.importStub(value[1]);
|
|
1741
|
+
this.hooks.push(hook);
|
|
1742
|
+
return new RpcStub$1(hook);
|
|
1743
|
+
}
|
|
1417
1744
|
}
|
|
1418
1745
|
break;
|
|
1419
1746
|
case "writable":
|
|
@@ -1424,22 +1751,20 @@ var Evaluator = class Evaluator {
|
|
|
1424
1751
|
return stream;
|
|
1425
1752
|
}
|
|
1426
1753
|
break;
|
|
1427
|
-
case "readable":
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
}
|
|
1434
|
-
break;
|
|
1754
|
+
case "readable": if (typeof value[1] == "number") {
|
|
1755
|
+
let stream = this.importer.getPipeReadable(value[1]);
|
|
1756
|
+
let hook = streamImpl.createReadableStreamHook(stream);
|
|
1757
|
+
this.hooks.push(hook);
|
|
1758
|
+
return stream;
|
|
1759
|
+
}
|
|
1435
1760
|
}
|
|
1436
1761
|
throw new TypeError(`unknown special value: ${JSON.stringify(value)}`);
|
|
1437
1762
|
} else if (value instanceof Object) {
|
|
1438
1763
|
let result = value;
|
|
1439
1764
|
for (let key in result) if (key in Object.prototype || key === "toJSON") {
|
|
1440
|
-
this.evaluateImpl(result[key], result, key);
|
|
1765
|
+
this.evaluateImpl(result[key], result, key, depth + 1);
|
|
1441
1766
|
delete result[key];
|
|
1442
|
-
} else result[key] = this.evaluateImpl(result[key], result, key);
|
|
1767
|
+
} else result[key] = this.evaluateImpl(result[key], result, key, depth + 1);
|
|
1443
1768
|
return result;
|
|
1444
1769
|
} else return value;
|
|
1445
1770
|
}
|
|
@@ -1455,6 +1780,48 @@ function deserialize(value) {
|
|
|
1455
1780
|
|
|
1456
1781
|
//#endregion
|
|
1457
1782
|
//#region src/rpc.ts
|
|
1783
|
+
const ESTIMATED_OBJECT_OVERHEAD = 16;
|
|
1784
|
+
const ESTIMATED_ENTRY_OVERHEAD = 8;
|
|
1785
|
+
const ESTIMATED_BINARY_OVERHEAD = 16;
|
|
1786
|
+
const MAX_ESTIMATE_DEPTH = 64;
|
|
1787
|
+
function estimateStringSize(value) {
|
|
1788
|
+
return 2 + value.length * 3;
|
|
1789
|
+
}
|
|
1790
|
+
function estimateEncodedSize(value, seen, depth = 0) {
|
|
1791
|
+
if (depth >= MAX_ESTIMATE_DEPTH) return ESTIMATED_ENTRY_OVERHEAD;
|
|
1792
|
+
switch (typeof value) {
|
|
1793
|
+
case "string": return estimateStringSize(value);
|
|
1794
|
+
case "number": return 16;
|
|
1795
|
+
case "bigint": return 16;
|
|
1796
|
+
case "boolean": return 8;
|
|
1797
|
+
case "undefined": return 16;
|
|
1798
|
+
case "object": {
|
|
1799
|
+
if (value === null) return 8;
|
|
1800
|
+
if (ArrayBuffer.isView(value)) return ESTIMATED_BINARY_OVERHEAD + value.byteLength;
|
|
1801
|
+
if (value instanceof ArrayBuffer) return ESTIMATED_BINARY_OVERHEAD + value.byteLength;
|
|
1802
|
+
if (typeof Blob !== "undefined" && value instanceof Blob) return ESTIMATED_BINARY_OVERHEAD + value.size;
|
|
1803
|
+
if (value instanceof Date) return 16;
|
|
1804
|
+
if (value instanceof RegExp) return ESTIMATED_OBJECT_OVERHEAD + estimateStringSize(value.source) + estimateStringSize(value.flags);
|
|
1805
|
+
seen ??= /* @__PURE__ */ new WeakSet();
|
|
1806
|
+
if (seen.has(value)) return ESTIMATED_ENTRY_OVERHEAD;
|
|
1807
|
+
seen.add(value);
|
|
1808
|
+
if (value instanceof Array) {
|
|
1809
|
+
let size = ESTIMATED_OBJECT_OVERHEAD;
|
|
1810
|
+
for (let item of value) size += ESTIMATED_ENTRY_OVERHEAD + estimateEncodedSize(item, seen, depth + 1);
|
|
1811
|
+
return size;
|
|
1812
|
+
}
|
|
1813
|
+
if (value instanceof Error) {
|
|
1814
|
+
let size = ESTIMATED_OBJECT_OVERHEAD + estimateStringSize(value.name) + estimateStringSize(value.message) + estimateStringSize(value.stack ?? "");
|
|
1815
|
+
for (let key of Object.keys(value)) size += ESTIMATED_ENTRY_OVERHEAD + estimateStringSize(key) + estimateEncodedSize(value[key], seen, depth + 1);
|
|
1816
|
+
return size;
|
|
1817
|
+
}
|
|
1818
|
+
let size = ESTIMATED_OBJECT_OVERHEAD;
|
|
1819
|
+
for (let key of Object.keys(value)) size += ESTIMATED_ENTRY_OVERHEAD + estimateStringSize(key) + estimateEncodedSize(value[key], seen, depth + 1);
|
|
1820
|
+
return size;
|
|
1821
|
+
}
|
|
1822
|
+
default: return 16;
|
|
1823
|
+
}
|
|
1824
|
+
}
|
|
1458
1825
|
var ImportTableEntry = class {
|
|
1459
1826
|
session;
|
|
1460
1827
|
importId;
|
|
@@ -1547,24 +1914,28 @@ var RpcImportHook = class RpcImportHook extends StubHook {
|
|
|
1547
1914
|
if (this.entry) return this.entry;
|
|
1548
1915
|
else throw new Error("This RpcImportHook was already disposed.");
|
|
1549
1916
|
}
|
|
1917
|
+
getEntryTakingOwnership(disposeOwned) {
|
|
1918
|
+
try {
|
|
1919
|
+
return this.getEntry();
|
|
1920
|
+
} catch (err) {
|
|
1921
|
+
disposeOwned();
|
|
1922
|
+
throw err;
|
|
1923
|
+
}
|
|
1924
|
+
}
|
|
1550
1925
|
call(path, args) {
|
|
1551
|
-
let entry = this.
|
|
1926
|
+
let entry = this.getEntryTakingOwnership(() => args.dispose());
|
|
1552
1927
|
if (entry.resolution) return entry.resolution.call(path, args);
|
|
1553
1928
|
else return entry.session.sendCall(entry.importId, path, args);
|
|
1554
1929
|
}
|
|
1555
1930
|
stream(path, args) {
|
|
1556
|
-
let entry = this.
|
|
1931
|
+
let entry = this.getEntryTakingOwnership(() => args.dispose());
|
|
1557
1932
|
if (entry.resolution) return entry.resolution.stream(path, args);
|
|
1558
1933
|
else return entry.session.sendStream(entry.importId, path, args);
|
|
1559
1934
|
}
|
|
1560
1935
|
map(path, captures, instructions) {
|
|
1561
|
-
let entry
|
|
1562
|
-
try {
|
|
1563
|
-
entry = this.getEntry();
|
|
1564
|
-
} catch (err) {
|
|
1936
|
+
let entry = this.getEntryTakingOwnership(() => {
|
|
1565
1937
|
for (let cap of captures) cap.dispose();
|
|
1566
|
-
|
|
1567
|
-
}
|
|
1938
|
+
});
|
|
1568
1939
|
if (entry.resolution) return entry.resolution.map(path, captures, instructions);
|
|
1569
1940
|
else return entry.session.sendMap(entry.importId, path, captures, instructions);
|
|
1570
1941
|
}
|
|
@@ -1620,9 +1991,24 @@ var RpcSessionImpl = class {
|
|
|
1620
1991
|
onBatchDone;
|
|
1621
1992
|
pullCount = 0;
|
|
1622
1993
|
onBrokenCallbacks = [];
|
|
1994
|
+
encodingLevel;
|
|
1995
|
+
limits;
|
|
1623
1996
|
constructor(transport, mainHook, options) {
|
|
1624
1997
|
this.transport = transport;
|
|
1625
1998
|
this.options = options;
|
|
1999
|
+
let level = "string";
|
|
2000
|
+
if ("encodingLevel" in transport) {
|
|
2001
|
+
let raw = transport.encodingLevel;
|
|
2002
|
+
if (raw !== void 0) {
|
|
2003
|
+
if (raw !== "string" && raw !== "jsonCompatible" && raw !== "jsonCompatibleWithBytes" && raw !== "structuredClonable") throw new TypeError(`Unknown transport encodingLevel: ${String(raw)}`);
|
|
2004
|
+
level = raw;
|
|
2005
|
+
}
|
|
2006
|
+
}
|
|
2007
|
+
this.encodingLevel = level;
|
|
2008
|
+
this.limits = {
|
|
2009
|
+
...DEFAULT_LIMITS,
|
|
2010
|
+
...options.limits
|
|
2011
|
+
};
|
|
1626
2012
|
this.exports.push({
|
|
1627
2013
|
hook: mainHook,
|
|
1628
2014
|
refcount: 1
|
|
@@ -1703,7 +2089,7 @@ var RpcSessionImpl = class {
|
|
|
1703
2089
|
let autoRelease = exp.autoRelease;
|
|
1704
2090
|
++this.pullCount;
|
|
1705
2091
|
exp.pull = resolve().then((payload) => {
|
|
1706
|
-
let value = Devaluator.devaluate(payload.value, void 0, this, payload);
|
|
2092
|
+
let value = Devaluator.devaluate(payload.value, void 0, this, payload, this.encodingLevel);
|
|
1707
2093
|
this.send([
|
|
1708
2094
|
"resolve",
|
|
1709
2095
|
exportId,
|
|
@@ -1714,7 +2100,7 @@ var RpcSessionImpl = class {
|
|
|
1714
2100
|
this.send([
|
|
1715
2101
|
"reject",
|
|
1716
2102
|
exportId,
|
|
1717
|
-
Devaluator.devaluate(error, void 0, this)
|
|
2103
|
+
Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)
|
|
1718
2104
|
]);
|
|
1719
2105
|
if (autoRelease) this.releaseExport(exportId, 1);
|
|
1720
2106
|
}).catch((error) => {
|
|
@@ -1722,7 +2108,7 @@ var RpcSessionImpl = class {
|
|
|
1722
2108
|
this.send([
|
|
1723
2109
|
"reject",
|
|
1724
2110
|
exportId,
|
|
1725
|
-
Devaluator.devaluate(error, void 0, this)
|
|
2111
|
+
Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)
|
|
1726
2112
|
]);
|
|
1727
2113
|
if (autoRelease) this.releaseExport(exportId, 1);
|
|
1728
2114
|
} catch (error2) {
|
|
@@ -1765,6 +2151,9 @@ var RpcSessionImpl = class {
|
|
|
1765
2151
|
entry.pipeReadable = void 0;
|
|
1766
2152
|
return readable;
|
|
1767
2153
|
}
|
|
2154
|
+
getLimits() {
|
|
2155
|
+
return this.limits;
|
|
2156
|
+
}
|
|
1768
2157
|
createPipe(readable, readableHook) {
|
|
1769
2158
|
if (this.abortReason) throw this.abortReason;
|
|
1770
2159
|
this.send(["pipe"]);
|
|
@@ -1778,27 +2167,52 @@ var RpcSessionImpl = class {
|
|
|
1778
2167
|
}
|
|
1779
2168
|
send(msg) {
|
|
1780
2169
|
if (this.abortReason !== void 0) return 0;
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
msgText = JSON.stringify(msg);
|
|
1784
|
-
} catch (err) {
|
|
2170
|
+
if (this.encodingLevel === "string") {
|
|
2171
|
+
let msgText;
|
|
1785
2172
|
try {
|
|
1786
|
-
|
|
1787
|
-
} catch (
|
|
1788
|
-
|
|
2173
|
+
msgText = JSON.stringify(msg);
|
|
2174
|
+
} catch (err) {
|
|
2175
|
+
try {
|
|
2176
|
+
this.abort(err);
|
|
2177
|
+
} catch (err2) {}
|
|
2178
|
+
throw err;
|
|
2179
|
+
}
|
|
2180
|
+
try {
|
|
2181
|
+
let sent = this.transport.send(msgText);
|
|
2182
|
+
if (sent !== void 0 && typeof sent.catch === "function") sent.catch((err) => this.abort(err, false));
|
|
2183
|
+
} catch (err) {
|
|
2184
|
+
queueMicrotask(() => this.abort(err, false));
|
|
2185
|
+
}
|
|
2186
|
+
return msgText.length;
|
|
2187
|
+
} else try {
|
|
2188
|
+
let size = this.transport.send(msg);
|
|
2189
|
+
if (typeof size === "number") return size;
|
|
2190
|
+
let thenable = size;
|
|
2191
|
+
if (thenable && typeof thenable.then === "function") Promise.resolve(thenable).catch((err) => this.abort(err, false));
|
|
2192
|
+
return;
|
|
2193
|
+
} catch (err) {
|
|
2194
|
+
queueMicrotask(() => this.abort(err, false));
|
|
2195
|
+
return;
|
|
1789
2196
|
}
|
|
1790
|
-
this.transport.send(msgText).catch((err) => this.abort(err, false));
|
|
1791
|
-
return msgText.length;
|
|
1792
2197
|
}
|
|
1793
2198
|
sendCall(id, path, args) {
|
|
1794
|
-
if (this.abortReason)
|
|
2199
|
+
if (this.abortReason) {
|
|
2200
|
+
args?.dispose();
|
|
2201
|
+
throw this.abortReason;
|
|
2202
|
+
}
|
|
1795
2203
|
let value = [
|
|
1796
2204
|
"pipeline",
|
|
1797
2205
|
id,
|
|
1798
2206
|
path
|
|
1799
2207
|
];
|
|
1800
2208
|
if (args) {
|
|
1801
|
-
let devalue
|
|
2209
|
+
let devalue;
|
|
2210
|
+
try {
|
|
2211
|
+
devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
|
|
2212
|
+
} catch (err) {
|
|
2213
|
+
args.dispose();
|
|
2214
|
+
throw err;
|
|
2215
|
+
}
|
|
1802
2216
|
value.push(devalue[0]);
|
|
1803
2217
|
}
|
|
1804
2218
|
this.send(["push", value]);
|
|
@@ -1807,15 +2221,26 @@ var RpcSessionImpl = class {
|
|
|
1807
2221
|
return new RpcImportHook(true, entry);
|
|
1808
2222
|
}
|
|
1809
2223
|
sendStream(id, path, args) {
|
|
1810
|
-
if (this.abortReason)
|
|
2224
|
+
if (this.abortReason) {
|
|
2225
|
+
args.dispose();
|
|
2226
|
+
throw this.abortReason;
|
|
2227
|
+
}
|
|
1811
2228
|
let value = [
|
|
1812
2229
|
"pipeline",
|
|
1813
2230
|
id,
|
|
1814
2231
|
path
|
|
1815
2232
|
];
|
|
1816
|
-
let devalue
|
|
2233
|
+
let devalue;
|
|
2234
|
+
try {
|
|
2235
|
+
devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
|
|
2236
|
+
} catch (err) {
|
|
2237
|
+
args.dispose();
|
|
2238
|
+
throw err;
|
|
2239
|
+
}
|
|
1817
2240
|
value.push(devalue[0]);
|
|
1818
|
-
let
|
|
2241
|
+
let msg = ["stream", value];
|
|
2242
|
+
let size = this.send(msg);
|
|
2243
|
+
if (size === void 0) size = estimateEncodedSize(msg);
|
|
1819
2244
|
let importId = this.imports.length;
|
|
1820
2245
|
let entry = new ImportTableEntry(this, importId, true);
|
|
1821
2246
|
entry.remoteRefcount = 0;
|
|
@@ -1871,7 +2296,14 @@ var RpcSessionImpl = class {
|
|
|
1871
2296
|
this.cancelReadLoop?.(error);
|
|
1872
2297
|
this.cancelReadLoop = void 0;
|
|
1873
2298
|
if (trySendAbortMessage) try {
|
|
1874
|
-
|
|
2299
|
+
let abortMsg = ["abort", Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)];
|
|
2300
|
+
if (this.encodingLevel === "string") {
|
|
2301
|
+
let sent = this.transport.send(JSON.stringify(abortMsg));
|
|
2302
|
+
if (sent !== void 0 && typeof sent.catch === "function") sent.catch((err) => {});
|
|
2303
|
+
} else {
|
|
2304
|
+
let result = this.transport.send(abortMsg);
|
|
2305
|
+
if (result && typeof result.then === "function") Promise.resolve(result).catch((err) => {});
|
|
2306
|
+
}
|
|
1875
2307
|
} catch (err) {}
|
|
1876
2308
|
if (error === void 0) error = "undefined";
|
|
1877
2309
|
this.abortReason = error;
|
|
@@ -1893,18 +2325,20 @@ var RpcSessionImpl = class {
|
|
|
1893
2325
|
while (!this.abortReason) {
|
|
1894
2326
|
let readCanceled = Promise.withResolvers();
|
|
1895
2327
|
this.cancelReadLoop = readCanceled.reject;
|
|
1896
|
-
let
|
|
2328
|
+
let raw;
|
|
1897
2329
|
try {
|
|
1898
|
-
|
|
2330
|
+
raw = await Promise.race([this.transport.receive(), readCanceled.promise]);
|
|
1899
2331
|
} finally {
|
|
1900
2332
|
if (this.cancelReadLoop === readCanceled.reject) this.cancelReadLoop = void 0;
|
|
1901
2333
|
}
|
|
1902
|
-
|
|
2334
|
+
if (this.encodingLevel === "string" && raw.length > this.limits.maxMessageSize) throw new TypeError(`Incoming message exceeds maximum size of ${this.limits.maxMessageSize} UTF-16 code units.`);
|
|
1903
2335
|
if (this.abortReason) break;
|
|
2336
|
+
let msg = this.encodingLevel === "string" ? JSON.parse(raw) : raw;
|
|
1904
2337
|
if (msg instanceof Array) switch (msg[0]) {
|
|
1905
2338
|
case "push":
|
|
1906
2339
|
if (msg.length > 1) {
|
|
1907
|
-
let
|
|
2340
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[1]);
|
|
2341
|
+
let hook = new PayloadStubHook(payload);
|
|
1908
2342
|
hook.ignoreUnhandledRejections();
|
|
1909
2343
|
this.exports.push({
|
|
1910
2344
|
hook,
|
|
@@ -1915,7 +2349,8 @@ var RpcSessionImpl = class {
|
|
|
1915
2349
|
break;
|
|
1916
2350
|
case "stream":
|
|
1917
2351
|
if (msg.length > 1) {
|
|
1918
|
-
let
|
|
2352
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[1]);
|
|
2353
|
+
let hook = new PayloadStubHook(payload);
|
|
1919
2354
|
hook.ignoreUnhandledRejections();
|
|
1920
2355
|
let exportId = this.exports.length;
|
|
1921
2356
|
this.exports.push({
|
|
@@ -1950,13 +2385,14 @@ var RpcSessionImpl = class {
|
|
|
1950
2385
|
let importId = msg[1];
|
|
1951
2386
|
if (typeof importId == "number" && msg.length > 2) {
|
|
1952
2387
|
let imp = this.imports[importId];
|
|
1953
|
-
if (imp)
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
2388
|
+
if (imp) {
|
|
2389
|
+
if (msg[0] == "resolve") imp.resolve(new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[2])));
|
|
2390
|
+
else {
|
|
2391
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[2]);
|
|
2392
|
+
payload.dispose();
|
|
2393
|
+
imp.resolve(new ErrorStubHook(payload.value));
|
|
2394
|
+
}
|
|
2395
|
+
} else if (msg[0] == "resolve") new Evaluator(this, this.encodingLevel).evaluate(msg[2]).dispose();
|
|
1960
2396
|
continue;
|
|
1961
2397
|
}
|
|
1962
2398
|
break;
|
|
@@ -1971,9 +2407,9 @@ var RpcSessionImpl = class {
|
|
|
1971
2407
|
break;
|
|
1972
2408
|
}
|
|
1973
2409
|
case "abort": {
|
|
1974
|
-
let payload = new Evaluator(this).evaluate(msg[1]);
|
|
2410
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[1]);
|
|
1975
2411
|
payload.dispose();
|
|
1976
|
-
this.abort(payload, false);
|
|
2412
|
+
this.abort(payload.value, false);
|
|
1977
2413
|
break;
|
|
1978
2414
|
}
|
|
1979
2415
|
}
|
|
@@ -2024,9 +2460,12 @@ var RpcSession$1 = class {
|
|
|
2024
2460
|
|
|
2025
2461
|
//#endregion
|
|
2026
2462
|
//#region src/websocket.ts
|
|
2463
|
+
/** Close-frame reason max UTF-8 bytes: 125 payload - 2-byte status (RFC 6455 section 5.5). */
|
|
2464
|
+
const MAX_CLOSE_REASON_BYTES = 123;
|
|
2027
2465
|
function newWebSocketRpcSession$1(webSocket, localMain, options) {
|
|
2028
2466
|
if (typeof webSocket === "string") webSocket = new WebSocket(webSocket);
|
|
2029
|
-
|
|
2467
|
+
let transport = new WebSocketTransport(webSocket);
|
|
2468
|
+
return new RpcSession$1(transport, localMain, options).getRemoteMain();
|
|
2030
2469
|
}
|
|
2031
2470
|
/**
|
|
2032
2471
|
* For use in Cloudflare Workers: Construct an HTTP response that starts a WebSocket RPC session
|
|
@@ -2043,9 +2482,14 @@ function newWorkersWebSocketRpcResponse(request, localMain, options) {
|
|
|
2043
2482
|
webSocket: pair[1]
|
|
2044
2483
|
});
|
|
2045
2484
|
}
|
|
2485
|
+
/**
|
|
2486
|
+
* Generic WebSocket transport. Default `T = string` is backward-compatible and satisfies
|
|
2487
|
+
* `RpcTransport`. Use `T = ArrayBuffer` as a building block for binary transports.
|
|
2488
|
+
*/
|
|
2046
2489
|
var WebSocketTransport = class {
|
|
2047
2490
|
constructor(webSocket) {
|
|
2048
2491
|
this.#webSocket = webSocket;
|
|
2492
|
+
webSocket.binaryType = "arraybuffer";
|
|
2049
2493
|
if (webSocket.readyState === WebSocket.CONNECTING) {
|
|
2050
2494
|
this.#sendQueue = [];
|
|
2051
2495
|
webSocket.addEventListener("open", (event) => {
|
|
@@ -2058,12 +2502,13 @@ var WebSocketTransport = class {
|
|
|
2058
2502
|
});
|
|
2059
2503
|
}
|
|
2060
2504
|
webSocket.addEventListener("message", (event) => {
|
|
2061
|
-
if (this.#error) {} else if (typeof event.data === "string"
|
|
2062
|
-
this.#receiveResolver
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2505
|
+
if (this.#error) {} else if (typeof event.data === "string" || event.data instanceof ArrayBuffer) {
|
|
2506
|
+
if (this.#receiveResolver) {
|
|
2507
|
+
this.#receiveResolver(event.data);
|
|
2508
|
+
this.#receiveResolver = void 0;
|
|
2509
|
+
this.#receiveRejecter = void 0;
|
|
2510
|
+
} else this.#receiveQueue.push(event.data);
|
|
2511
|
+
} else this.#receivedError(/* @__PURE__ */ new TypeError("Received unexpected message type from WebSocket."));
|
|
2067
2512
|
});
|
|
2068
2513
|
webSocket.addEventListener("close", (event) => {
|
|
2069
2514
|
this.#receivedError(/* @__PURE__ */ new Error(`Peer closed WebSocket: ${event.code} ${event.reason}`));
|
|
@@ -2078,13 +2523,13 @@ var WebSocketTransport = class {
|
|
|
2078
2523
|
#receiveRejecter;
|
|
2079
2524
|
#receiveQueue = [];
|
|
2080
2525
|
#error;
|
|
2081
|
-
|
|
2526
|
+
send(message) {
|
|
2082
2527
|
if (this.#sendQueue === void 0) this.#webSocket.send(message);
|
|
2083
2528
|
else this.#sendQueue.push(message);
|
|
2084
2529
|
}
|
|
2085
|
-
|
|
2086
|
-
if (this.#receiveQueue.length > 0) return this.#receiveQueue.shift();
|
|
2087
|
-
else if (this.#error)
|
|
2530
|
+
receive() {
|
|
2531
|
+
if (this.#receiveQueue.length > 0) return Promise.resolve(this.#receiveQueue.shift());
|
|
2532
|
+
else if (this.#error) return Promise.reject(this.#error);
|
|
2088
2533
|
else return new Promise((resolve, reject) => {
|
|
2089
2534
|
this.#receiveResolver = resolve;
|
|
2090
2535
|
this.#receiveRejecter = reject;
|
|
@@ -2094,6 +2539,8 @@ var WebSocketTransport = class {
|
|
|
2094
2539
|
let message;
|
|
2095
2540
|
if (reason instanceof Error) message = reason.message;
|
|
2096
2541
|
else message = `${reason}`;
|
|
2542
|
+
let reasonBytes = new TextEncoder().encode(message);
|
|
2543
|
+
if (reasonBytes.length > 123) message = new TextDecoder().decode(reasonBytes.subarray(0, 123), { stream: true });
|
|
2097
2544
|
this.#webSocket.close(3e3, message);
|
|
2098
2545
|
if (!this.#error) this.#error = reason;
|
|
2099
2546
|
}
|
|
@@ -2111,6 +2558,7 @@ var WebSocketTransport = class {
|
|
|
2111
2558
|
|
|
2112
2559
|
//#endregion
|
|
2113
2560
|
//#region src/batch.ts
|
|
2561
|
+
const yieldToMacrotask = typeof setImmediate === "function" ? () => new Promise((resolve) => setImmediate(resolve)) : () => new Promise((resolve) => setTimeout(resolve, 0));
|
|
2114
2562
|
var BatchClientTransport = class {
|
|
2115
2563
|
constructor(sendBatch) {
|
|
2116
2564
|
this.#promise = this.#scheduleBatch(sendBatch);
|
|
@@ -2119,7 +2567,7 @@ var BatchClientTransport = class {
|
|
|
2119
2567
|
#aborted;
|
|
2120
2568
|
#batchToSend = [];
|
|
2121
2569
|
#batchToReceive = null;
|
|
2122
|
-
|
|
2570
|
+
send(message) {
|
|
2123
2571
|
if (this.#batchToSend !== null) this.#batchToSend.push(message);
|
|
2124
2572
|
}
|
|
2125
2573
|
async receive() {
|
|
@@ -2132,7 +2580,7 @@ var BatchClientTransport = class {
|
|
|
2132
2580
|
this.#aborted = reason;
|
|
2133
2581
|
}
|
|
2134
2582
|
async #scheduleBatch(sendBatch) {
|
|
2135
|
-
await
|
|
2583
|
+
await yieldToMacrotask();
|
|
2136
2584
|
if (this.#aborted !== void 0) throw this.#aborted;
|
|
2137
2585
|
let batch = this.#batchToSend;
|
|
2138
2586
|
this.#batchToSend = null;
|
|
@@ -2152,7 +2600,8 @@ function newHttpBatchRpcSession$1(urlOrRequest, options) {
|
|
|
2152
2600
|
let body = await response.text();
|
|
2153
2601
|
return body == "" ? [] : body.split("\n");
|
|
2154
2602
|
};
|
|
2155
|
-
|
|
2603
|
+
let transport = new BatchClientTransport(sendBatch);
|
|
2604
|
+
return new RpcSession$1(transport, void 0, options).getRemoteMain();
|
|
2156
2605
|
}
|
|
2157
2606
|
var BatchServerTransport = class {
|
|
2158
2607
|
constructor(batch) {
|
|
@@ -2161,7 +2610,7 @@ var BatchServerTransport = class {
|
|
|
2161
2610
|
#batchToSend = [];
|
|
2162
2611
|
#batchToReceive;
|
|
2163
2612
|
#allReceived = Promise.withResolvers();
|
|
2164
|
-
|
|
2613
|
+
send(message) {
|
|
2165
2614
|
this.#batchToSend.push(message);
|
|
2166
2615
|
}
|
|
2167
2616
|
async receive() {
|
|
@@ -2195,7 +2644,8 @@ var BatchServerTransport = class {
|
|
|
2195
2644
|
async function newHttpBatchRpcResponse(request, localMain, options) {
|
|
2196
2645
|
if (request.method !== "POST") return new Response("This endpoint only accepts POST requests.", { status: 405 });
|
|
2197
2646
|
let body = await request.text();
|
|
2198
|
-
let
|
|
2647
|
+
let batch = body === "" ? [] : body.split("\n");
|
|
2648
|
+
let transport = new BatchServerTransport(batch);
|
|
2199
2649
|
let rpc = new RpcSession$1(transport, localMain, options);
|
|
2200
2650
|
await transport.whenAllReceived();
|
|
2201
2651
|
await rpc.drain();
|
|
@@ -2210,7 +2660,11 @@ async function newHttpBatchRpcResponse(request, localMain, options) {
|
|
|
2210
2660
|
* @param options Optional RPC session options. You can also pass headers to set on the response.
|
|
2211
2661
|
*/
|
|
2212
2662
|
async function nodeHttpBatchRpcResponse(request, response, localMain, options) {
|
|
2213
|
-
if (request.method !== "POST")
|
|
2663
|
+
if (request.method !== "POST") {
|
|
2664
|
+
response.writeHead(405, "This endpoint only accepts POST requests.", options?.headers);
|
|
2665
|
+
response.end();
|
|
2666
|
+
return;
|
|
2667
|
+
}
|
|
2214
2668
|
let body = await new Promise((resolve, reject) => {
|
|
2215
2669
|
let chunks = [];
|
|
2216
2670
|
request.on("data", (chunk) => {
|
|
@@ -2221,7 +2675,8 @@ async function nodeHttpBatchRpcResponse(request, response, localMain, options) {
|
|
|
2221
2675
|
});
|
|
2222
2676
|
request.on("error", reject);
|
|
2223
2677
|
});
|
|
2224
|
-
let
|
|
2678
|
+
let batch = body === "" ? [] : body.split("\n");
|
|
2679
|
+
let transport = new BatchServerTransport(batch);
|
|
2225
2680
|
let rpc = new RpcSession$1(transport, localMain, options);
|
|
2226
2681
|
await transport.whenAllReceived();
|
|
2227
2682
|
await rpc.drain();
|
|
@@ -2232,20 +2687,21 @@ async function nodeHttpBatchRpcResponse(request, response, localMain, options) {
|
|
|
2232
2687
|
//#endregion
|
|
2233
2688
|
//#region src/messageport.ts
|
|
2234
2689
|
function newMessagePortRpcSession$1(port, localMain, options) {
|
|
2235
|
-
|
|
2690
|
+
let transport = new MessagePortTransport(port);
|
|
2691
|
+
return new RpcSession$1(transport, localMain, options).getRemoteMain();
|
|
2236
2692
|
}
|
|
2237
2693
|
var MessagePortTransport = class {
|
|
2694
|
+
encodingLevel = "structuredClonable";
|
|
2238
2695
|
constructor(port) {
|
|
2239
2696
|
this.#port = port;
|
|
2240
2697
|
port.start();
|
|
2241
2698
|
port.addEventListener("message", (event) => {
|
|
2242
2699
|
if (this.#error) {} else if (event.data === null) this.#receivedError(/* @__PURE__ */ new Error("Peer closed MessagePort connection."));
|
|
2243
|
-
else if (
|
|
2700
|
+
else if (this.#receiveResolver) {
|
|
2244
2701
|
this.#receiveResolver(event.data);
|
|
2245
2702
|
this.#receiveResolver = void 0;
|
|
2246
2703
|
this.#receiveRejecter = void 0;
|
|
2247
2704
|
} else this.#receiveQueue.push(event.data);
|
|
2248
|
-
else this.#receivedError(/* @__PURE__ */ new TypeError("Received non-string message from MessagePort."));
|
|
2249
2705
|
});
|
|
2250
2706
|
port.addEventListener("messageerror", (event) => {
|
|
2251
2707
|
this.#receivedError(/* @__PURE__ */ new Error("MessagePort message error."));
|
|
@@ -2256,7 +2712,7 @@ var MessagePortTransport = class {
|
|
|
2256
2712
|
#receiveRejecter;
|
|
2257
2713
|
#receiveQueue = [];
|
|
2258
2714
|
#error;
|
|
2259
|
-
|
|
2715
|
+
send(message) {
|
|
2260
2716
|
if (this.#error) throw this.#error;
|
|
2261
2717
|
this.#port.postMessage(message);
|
|
2262
2718
|
}
|
|
@@ -2420,9 +2876,11 @@ var MapVariableHook = class extends StubHook {
|
|
|
2420
2876
|
else throwMapperBuilderUseError();
|
|
2421
2877
|
}
|
|
2422
2878
|
call(path, args) {
|
|
2879
|
+
args.dispose();
|
|
2423
2880
|
throwMapperBuilderUseError();
|
|
2424
2881
|
}
|
|
2425
2882
|
map(path, captures, instructions) {
|
|
2883
|
+
for (let cap of captures) cap.dispose();
|
|
2426
2884
|
throwMapperBuilderUseError();
|
|
2427
2885
|
}
|
|
2428
2886
|
pull() {
|
|
@@ -2475,9 +2933,13 @@ var MapApplicator = class {
|
|
|
2475
2933
|
getPipeReadable(exportId) {
|
|
2476
2934
|
throw new Error("A mapper function cannot use pipe readables.");
|
|
2477
2935
|
}
|
|
2936
|
+
getLimits() {
|
|
2937
|
+
return DEFAULT_LIMITS;
|
|
2938
|
+
}
|
|
2478
2939
|
};
|
|
2479
2940
|
function applyMapToElement(input, parent, owner, captures, instructions) {
|
|
2480
|
-
let
|
|
2941
|
+
let inputHook = new PayloadStubHook(RpcPayload.deepCopyFrom(input, parent, owner));
|
|
2942
|
+
let mapper = new MapApplicator(captures, inputHook);
|
|
2481
2943
|
try {
|
|
2482
2944
|
return mapper.apply(instructions);
|
|
2483
2945
|
} finally {
|
|
@@ -2510,9 +2972,10 @@ mapImpl.applyMap = (input, parent, owner, captures, instructions) => {
|
|
|
2510
2972
|
var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
|
|
2511
2973
|
state;
|
|
2512
2974
|
static create(stream) {
|
|
2975
|
+
let writer = stream.getWriter();
|
|
2513
2976
|
return new WritableStreamStubHook({
|
|
2514
2977
|
refcount: 1,
|
|
2515
|
-
writer
|
|
2978
|
+
writer,
|
|
2516
2979
|
closed: false
|
|
2517
2980
|
});
|
|
2518
2981
|
}
|
|
@@ -2530,14 +2993,12 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
|
|
|
2530
2993
|
let state = this.getState();
|
|
2531
2994
|
if (path.length !== 1 || typeof path[0] !== "string") throw new Error("WritableStream stub only supports direct method calls");
|
|
2532
2995
|
const method = path[0];
|
|
2533
|
-
if (method !== "write" && method !== "close" && method !== "abort") {
|
|
2534
|
-
args.dispose();
|
|
2535
|
-
throw new Error(`Unknown WritableStream method: ${method}`);
|
|
2536
|
-
}
|
|
2996
|
+
if (method !== "write" && method !== "close" && method !== "abort") throw new Error(`Unknown WritableStream method: ${method}`);
|
|
2537
2997
|
if (method === "close" || method === "abort") state.closed = true;
|
|
2538
|
-
let
|
|
2539
|
-
return new PromiseStubHook(
|
|
2998
|
+
let promise = method === "write" ? args.deliverStreamWrite(state.writer) : args.deliverCall(state.writer[method], state.writer);
|
|
2999
|
+
return new PromiseStubHook(promise.then((payload) => new PayloadStubHook(payload)));
|
|
2540
3000
|
} catch (err) {
|
|
3001
|
+
args.dispose();
|
|
2541
3002
|
return new ErrorStubHook(err);
|
|
2542
3003
|
}
|
|
2543
3004
|
}
|
|
@@ -2549,7 +3010,8 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
|
|
|
2549
3010
|
return new ErrorStubHook(/* @__PURE__ */ new Error("Cannot access properties on a WritableStream stub"));
|
|
2550
3011
|
}
|
|
2551
3012
|
dup() {
|
|
2552
|
-
|
|
3013
|
+
let state = this.getState();
|
|
3014
|
+
return new WritableStreamStubHook(state, this);
|
|
2553
3015
|
}
|
|
2554
3016
|
pull() {
|
|
2555
3017
|
return Promise.reject(/* @__PURE__ */ new Error("Cannot pull a WritableStream stub"));
|
|
@@ -2567,9 +3029,9 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
|
|
|
2567
3029
|
}
|
|
2568
3030
|
onBroken(callback) {}
|
|
2569
3031
|
};
|
|
2570
|
-
const INITIAL_WINDOW =
|
|
2571
|
-
const MAX_WINDOW =
|
|
2572
|
-
const MIN_WINDOW =
|
|
3032
|
+
const INITIAL_WINDOW = 262144;
|
|
3033
|
+
const MAX_WINDOW = 1073741824;
|
|
3034
|
+
const MIN_WINDOW = 65536;
|
|
2573
3035
|
const STARTUP_GROWTH_FACTOR = 2;
|
|
2574
3036
|
const STEADY_GROWTH_FACTOR = 1.25;
|
|
2575
3037
|
const DECAY_FACTOR = .9;
|
|
@@ -2813,12 +3275,12 @@ let newMessagePortRpcSession = newMessagePortRpcSession$1;
|
|
|
2813
3275
|
* credentials as parameters and returns the authorized API), then cross-origin requests should
|
|
2814
3276
|
* be safe.
|
|
2815
3277
|
*/
|
|
2816
|
-
async function newWorkersRpcResponse(request, localMain) {
|
|
3278
|
+
async function newWorkersRpcResponse(request, localMain, options) {
|
|
2817
3279
|
if (request.method === "POST") {
|
|
2818
|
-
let response = await newHttpBatchRpcResponse(request, localMain);
|
|
3280
|
+
let response = await newHttpBatchRpcResponse(request, localMain, options);
|
|
2819
3281
|
response.headers.set("Access-Control-Allow-Origin", "*");
|
|
2820
3282
|
return response;
|
|
2821
|
-
} else if (request.headers.get("Upgrade")?.toLowerCase() === "websocket") return newWorkersWebSocketRpcResponse(request, localMain);
|
|
3283
|
+
} else if (request.headers.get("Upgrade")?.toLowerCase() === "websocket") return newWorkersWebSocketRpcResponse(request, localMain, options);
|
|
2822
3284
|
else return new Response("This endpoint only accepts POST or WebSocket requests.", { status: 400 });
|
|
2823
3285
|
}
|
|
2824
3286
|
|
|
@@ -2940,10 +3402,13 @@ let newBunWebSocketRpcSession = newBunWebSocketRpcSession$1;
|
|
|
2940
3402
|
|
|
2941
3403
|
//#endregion
|
|
2942
3404
|
exports.BunWebSocketTransport = BunWebSocketTransport;
|
|
3405
|
+
exports.DEFAULT_LIMITS = DEFAULT_LIMITS;
|
|
3406
|
+
exports.DEFAULT_MAX_DEPTH = DEFAULT_MAX_DEPTH;
|
|
2943
3407
|
exports.RpcPromise = RpcPromise;
|
|
2944
3408
|
exports.RpcSession = RpcSession;
|
|
2945
3409
|
exports.RpcStub = RpcStub;
|
|
2946
3410
|
exports.RpcTarget = RpcTarget;
|
|
3411
|
+
exports.WebSocketTransport = WebSocketTransport;
|
|
2947
3412
|
exports.deserialize = deserialize;
|
|
2948
3413
|
exports.newBunWebSocketRpcHandler = newBunWebSocketRpcHandler;
|
|
2949
3414
|
exports.newBunWebSocketRpcSession = newBunWebSocketRpcSession;
|