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-workers.cjs
CHANGED
|
@@ -20,7 +20,7 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
20
20
|
}
|
|
21
21
|
return to;
|
|
22
22
|
};
|
|
23
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
|
|
24
24
|
value: mod,
|
|
25
25
|
enumerable: true
|
|
26
26
|
}) : target, mod));
|
|
@@ -75,10 +75,25 @@ function typeForRpc(value) {
|
|
|
75
75
|
case AsyncFunction.prototype: return "function";
|
|
76
76
|
case Array.prototype: return "array";
|
|
77
77
|
case Date.prototype: return "date";
|
|
78
|
+
case RegExp.prototype: return "regexp";
|
|
79
|
+
case Set.prototype: return "set";
|
|
78
80
|
case Uint8Array.prototype:
|
|
79
|
-
case BUFFER_PROTOTYPE:
|
|
81
|
+
case BUFFER_PROTOTYPE:
|
|
82
|
+
case ArrayBuffer.prototype:
|
|
83
|
+
case DataView.prototype:
|
|
84
|
+
case Int8Array.prototype:
|
|
85
|
+
case Uint8ClampedArray.prototype:
|
|
86
|
+
case Int16Array.prototype:
|
|
87
|
+
case Uint16Array.prototype:
|
|
88
|
+
case Int32Array.prototype:
|
|
89
|
+
case Uint32Array.prototype:
|
|
90
|
+
case BigInt64Array.prototype:
|
|
91
|
+
case BigUint64Array.prototype:
|
|
92
|
+
case Float32Array.prototype:
|
|
93
|
+
case Float64Array.prototype: return "bytes";
|
|
80
94
|
case WritableStream.prototype: return "writable";
|
|
81
95
|
case ReadableStream.prototype: return "readable";
|
|
96
|
+
case URL.prototype: return "url";
|
|
82
97
|
case Headers.prototype: return "headers";
|
|
83
98
|
case Request.prototype: return "request";
|
|
84
99
|
case Response.prototype: return "response";
|
|
@@ -112,7 +127,14 @@ let streamImpl = {
|
|
|
112
127
|
};
|
|
113
128
|
var StubHook = class {
|
|
114
129
|
stream(path, args) {
|
|
115
|
-
let
|
|
130
|
+
let hook = this.call(path, args);
|
|
131
|
+
let pulled;
|
|
132
|
+
try {
|
|
133
|
+
pulled = hook.pull();
|
|
134
|
+
} catch (err) {
|
|
135
|
+
hook.dispose();
|
|
136
|
+
throw err;
|
|
137
|
+
}
|
|
116
138
|
let promise;
|
|
117
139
|
if (pulled instanceof Promise) promise = pulled.then((p) => {
|
|
118
140
|
p.dispose();
|
|
@@ -131,9 +153,11 @@ var ErrorStubHook = class extends StubHook {
|
|
|
131
153
|
this.error = error;
|
|
132
154
|
}
|
|
133
155
|
call(path, args) {
|
|
156
|
+
args.dispose();
|
|
134
157
|
return this;
|
|
135
158
|
}
|
|
136
159
|
map(path, captures, instructions) {
|
|
160
|
+
for (let cap of captures) cap.dispose();
|
|
137
161
|
return this;
|
|
138
162
|
}
|
|
139
163
|
get(path) {
|
|
@@ -257,7 +281,25 @@ var RpcStub$1 = class RpcStub$1 extends RpcTarget$1 {
|
|
|
257
281
|
};
|
|
258
282
|
var RpcPromise$1 = class extends RpcStub$1 {
|
|
259
283
|
constructor(hook, pathIfPromise) {
|
|
260
|
-
super(hook, pathIfPromise);
|
|
284
|
+
if (hook instanceof StubHook) super(hook, pathIfPromise ?? []);
|
|
285
|
+
else {
|
|
286
|
+
if (pathIfPromise !== void 0) throw new TypeError("RpcPromise constructor expected one argument, received two.");
|
|
287
|
+
let kind = typeForRpc(hook);
|
|
288
|
+
if (kind === "rpc-promise") {
|
|
289
|
+
let raw = unwrapStubAndPath(hook);
|
|
290
|
+
if (raw.pathIfPromise.length > 0) super(raw.hook, raw.pathIfPromise);
|
|
291
|
+
else {
|
|
292
|
+
let adopted = raw.hook;
|
|
293
|
+
raw.hook = DISPOSED_HOOK;
|
|
294
|
+
super(adopted, []);
|
|
295
|
+
}
|
|
296
|
+
} else if (kind === "rpc-thenable") super(TargetStubHook.create(hook, void 0), []);
|
|
297
|
+
else {
|
|
298
|
+
let promiseHook = new PromiseStubHook(Promise.resolve(hook).then((value) => new PayloadStubHook(RpcPayload.fromAppReturn(value))));
|
|
299
|
+
promiseHook.ignoreUnhandledRejections();
|
|
300
|
+
super(promiseHook, []);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
261
303
|
}
|
|
262
304
|
then(onfulfilled, onrejected) {
|
|
263
305
|
return pullPromise(this).then(...arguments);
|
|
@@ -352,12 +394,13 @@ var RpcPayload = class RpcPayload {
|
|
|
352
394
|
return TargetStubHook.create(target, parent);
|
|
353
395
|
} else if (this.source === "return") {
|
|
354
396
|
let hook = this.rpcTargets?.get(target);
|
|
355
|
-
if (hook)
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
397
|
+
if (hook) {
|
|
398
|
+
if (dupStubs) return hook.dup();
|
|
399
|
+
else {
|
|
400
|
+
this.rpcTargets?.delete(target);
|
|
401
|
+
return hook;
|
|
402
|
+
}
|
|
403
|
+
} else {
|
|
361
404
|
hook = TargetStubHook.create(target, parent);
|
|
362
405
|
if (dupStubs) {
|
|
363
406
|
if (!this.rpcTargets) this.rpcTargets = /* @__PURE__ */ new Map();
|
|
@@ -371,12 +414,13 @@ var RpcPayload = class RpcPayload {
|
|
|
371
414
|
if (this.source === "params") return streamImpl.createWritableStreamHook(stream);
|
|
372
415
|
else if (this.source === "return") {
|
|
373
416
|
let hook = this.rpcTargets?.get(stream);
|
|
374
|
-
if (hook)
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
417
|
+
if (hook) {
|
|
418
|
+
if (dupStubs) return hook.dup();
|
|
419
|
+
else {
|
|
420
|
+
this.rpcTargets?.delete(stream);
|
|
421
|
+
return hook;
|
|
422
|
+
}
|
|
423
|
+
} else {
|
|
380
424
|
hook = streamImpl.createWritableStreamHook(stream);
|
|
381
425
|
if (dupStubs) {
|
|
382
426
|
if (!this.rpcTargets) this.rpcTargets = /* @__PURE__ */ new Map();
|
|
@@ -390,12 +434,13 @@ var RpcPayload = class RpcPayload {
|
|
|
390
434
|
if (this.source === "params") return streamImpl.createReadableStreamHook(stream);
|
|
391
435
|
else if (this.source === "return") {
|
|
392
436
|
let hook = this.rpcTargets?.get(stream);
|
|
393
|
-
if (hook)
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
437
|
+
if (hook) {
|
|
438
|
+
if (dupStubs) return hook.dup();
|
|
439
|
+
else {
|
|
440
|
+
this.rpcTargets?.delete(stream);
|
|
441
|
+
return hook;
|
|
442
|
+
}
|
|
443
|
+
} else {
|
|
399
444
|
hook = streamImpl.createReadableStreamHook(stream);
|
|
400
445
|
if (dupStubs) {
|
|
401
446
|
if (!this.rpcTargets) this.rpcTargets = /* @__PURE__ */ new Map();
|
|
@@ -413,6 +458,8 @@ var RpcPayload = class RpcPayload {
|
|
|
413
458
|
case "date":
|
|
414
459
|
case "bytes":
|
|
415
460
|
case "blob":
|
|
461
|
+
case "url":
|
|
462
|
+
case "regexp":
|
|
416
463
|
case "error":
|
|
417
464
|
case "undefined": return value;
|
|
418
465
|
case "array": {
|
|
@@ -422,6 +469,21 @@ var RpcPayload = class RpcPayload {
|
|
|
422
469
|
for (let i = 0; i < len; i++) result[i] = this.deepCopy(array[i], array, i, result, dupStubs, owner);
|
|
423
470
|
return result;
|
|
424
471
|
}
|
|
472
|
+
case "set": {
|
|
473
|
+
let set = value;
|
|
474
|
+
let elements = [...set];
|
|
475
|
+
for (let val of elements) {
|
|
476
|
+
let kind = typeForRpc(val);
|
|
477
|
+
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.");
|
|
478
|
+
}
|
|
479
|
+
let result = /* @__PURE__ */ new Set();
|
|
480
|
+
let index = 0;
|
|
481
|
+
for (let val of elements) {
|
|
482
|
+
let copy = this.deepCopy(val, set, index++, result, dupStubs, owner);
|
|
483
|
+
result.add(copy);
|
|
484
|
+
}
|
|
485
|
+
return result;
|
|
486
|
+
}
|
|
425
487
|
case "object": {
|
|
426
488
|
let result = {};
|
|
427
489
|
let object = value;
|
|
@@ -506,6 +568,10 @@ var RpcPayload = class RpcPayload {
|
|
|
506
568
|
try {
|
|
507
569
|
this.value = this.deepCopy(this.value, void 0, "value", this, dupStubs, this);
|
|
508
570
|
} catch (err) {
|
|
571
|
+
if (dupStubs) {
|
|
572
|
+
this.hooks.forEach((hook) => hook.dispose());
|
|
573
|
+
this.promises.forEach((promise) => promise.promise[Symbol.dispose]());
|
|
574
|
+
}
|
|
509
575
|
this.hooks = void 0;
|
|
510
576
|
this.promises = void 0;
|
|
511
577
|
throw err;
|
|
@@ -546,6 +612,30 @@ var RpcPayload = class RpcPayload {
|
|
|
546
612
|
this.dispose();
|
|
547
613
|
}
|
|
548
614
|
}
|
|
615
|
+
async deliverStreamWrite(writer) {
|
|
616
|
+
try {
|
|
617
|
+
let promises = [];
|
|
618
|
+
this.deliverTo(this, "value", promises);
|
|
619
|
+
if (promises.length > 0) await Promise.all(promises);
|
|
620
|
+
let chunk = this.value[0];
|
|
621
|
+
if ((this.hooks.length > 0 || this.promises.length > 0) && chunk instanceof Object) {
|
|
622
|
+
if (!(Symbol.dispose in chunk)) Object.defineProperty(chunk, Symbol.dispose, {
|
|
623
|
+
value: () => this.dispose(),
|
|
624
|
+
writable: true,
|
|
625
|
+
enumerable: false,
|
|
626
|
+
configurable: true
|
|
627
|
+
});
|
|
628
|
+
await writer.write(chunk);
|
|
629
|
+
return RpcPayload.fromAppReturn(void 0);
|
|
630
|
+
}
|
|
631
|
+
await writer.write(chunk);
|
|
632
|
+
this.dispose();
|
|
633
|
+
return RpcPayload.fromAppReturn(void 0);
|
|
634
|
+
} catch (err) {
|
|
635
|
+
this.dispose();
|
|
636
|
+
throw err;
|
|
637
|
+
}
|
|
638
|
+
}
|
|
549
639
|
async deliverResolve() {
|
|
550
640
|
try {
|
|
551
641
|
let promises = [];
|
|
@@ -586,6 +676,8 @@ var RpcPayload = class RpcPayload {
|
|
|
586
676
|
case "bytes":
|
|
587
677
|
case "blob":
|
|
588
678
|
case "date":
|
|
679
|
+
case "url":
|
|
680
|
+
case "regexp":
|
|
589
681
|
case "error":
|
|
590
682
|
case "undefined": return;
|
|
591
683
|
case "array": {
|
|
@@ -594,6 +686,11 @@ var RpcPayload = class RpcPayload {
|
|
|
594
686
|
for (let i = 0; i < len; i++) this.disposeImpl(array[i], array);
|
|
595
687
|
return;
|
|
596
688
|
}
|
|
689
|
+
case "set": {
|
|
690
|
+
let set = value;
|
|
691
|
+
for (let element of set) this.disposeImpl(element, set);
|
|
692
|
+
return;
|
|
693
|
+
}
|
|
597
694
|
case "object": {
|
|
598
695
|
let object = value;
|
|
599
696
|
for (let i in object) this.disposeImpl(object[i], object);
|
|
@@ -668,6 +765,8 @@ var RpcPayload = class RpcPayload {
|
|
|
668
765
|
case "rpc-target":
|
|
669
766
|
case "writable":
|
|
670
767
|
case "readable":
|
|
768
|
+
case "url":
|
|
769
|
+
case "regexp":
|
|
671
770
|
case "headers":
|
|
672
771
|
case "request":
|
|
673
772
|
case "response": return;
|
|
@@ -677,6 +776,11 @@ var RpcPayload = class RpcPayload {
|
|
|
677
776
|
for (let i = 0; i < len; i++) this.ignoreUnhandledRejectionsImpl(array[i]);
|
|
678
777
|
return;
|
|
679
778
|
}
|
|
779
|
+
case "set": {
|
|
780
|
+
let set = value;
|
|
781
|
+
for (let element of set) this.ignoreUnhandledRejectionsImpl(element);
|
|
782
|
+
return;
|
|
783
|
+
}
|
|
680
784
|
case "object": {
|
|
681
785
|
let object = value;
|
|
682
786
|
for (let i in object) this.ignoreUnhandledRejectionsImpl(object[i]);
|
|
@@ -736,7 +840,10 @@ function followPath(value, parent, path, owner) {
|
|
|
736
840
|
case "bytes":
|
|
737
841
|
case "blob":
|
|
738
842
|
case "date":
|
|
843
|
+
case "set":
|
|
739
844
|
case "error":
|
|
845
|
+
case "url":
|
|
846
|
+
case "regexp":
|
|
740
847
|
case "headers":
|
|
741
848
|
case "request":
|
|
742
849
|
case "response":
|
|
@@ -769,17 +876,23 @@ function followPath(value, parent, path, owner) {
|
|
|
769
876
|
}
|
|
770
877
|
var ValueStubHook = class extends StubHook {
|
|
771
878
|
call(path, args) {
|
|
879
|
+
let followResult;
|
|
772
880
|
try {
|
|
773
881
|
let { value, owner } = this.getValue();
|
|
774
|
-
|
|
775
|
-
if (followResult.hook) return followResult.hook.call(followResult.remainingPath, args);
|
|
776
|
-
if (typeof followResult.value != "function") throw new TypeError(`'${path.join(".")}' is not a function.`);
|
|
777
|
-
return new PromiseStubHook(args.deliverCall(followResult.value, followResult.parent).then((payload) => {
|
|
778
|
-
return new PayloadStubHook(payload);
|
|
779
|
-
}));
|
|
882
|
+
followResult = followPath(value, void 0, path, owner);
|
|
780
883
|
} catch (err) {
|
|
884
|
+
args.dispose();
|
|
781
885
|
return new ErrorStubHook(err);
|
|
782
886
|
}
|
|
887
|
+
if (followResult.hook) return followResult.hook.call(followResult.remainingPath, args);
|
|
888
|
+
if (typeof followResult.value != "function") {
|
|
889
|
+
args.dispose();
|
|
890
|
+
return new ErrorStubHook(/* @__PURE__ */ new TypeError(`'${path.join(".")}' is not a function.`));
|
|
891
|
+
}
|
|
892
|
+
let promise = args.deliverCall(followResult.value, followResult.parent);
|
|
893
|
+
return new PromiseStubHook(promise.then((payload) => {
|
|
894
|
+
return new PayloadStubHook(payload);
|
|
895
|
+
}));
|
|
783
896
|
}
|
|
784
897
|
map(path, captures, instructions) {
|
|
785
898
|
try {
|
|
@@ -800,7 +913,10 @@ var ValueStubHook = class extends StubHook {
|
|
|
800
913
|
get(path) {
|
|
801
914
|
try {
|
|
802
915
|
let { value, owner } = this.getValue();
|
|
803
|
-
if (path.length === 0 && owner === null)
|
|
916
|
+
if (path.length === 0 && owner === null) {
|
|
917
|
+
if (value instanceof Object && "then" in value) return this.dup();
|
|
918
|
+
throw new Error("Can't dup an RpcTarget stub as a promise.");
|
|
919
|
+
}
|
|
804
920
|
let followResult = followPath(value, void 0, path, owner);
|
|
805
921
|
if (followResult.hook) return followResult.hook.get(followResult.remainingPath);
|
|
806
922
|
return new PayloadStubHook(RpcPayload.deepCopyFrom(followResult.value, followResult.parent, followResult.owner));
|
|
@@ -903,7 +1019,10 @@ var TargetStubHook = class TargetStubHook extends ValueStubHook {
|
|
|
903
1019
|
this.target = void 0;
|
|
904
1020
|
}
|
|
905
1021
|
}
|
|
906
|
-
onBroken(callback) {
|
|
1022
|
+
onBroken(callback) {
|
|
1023
|
+
let target = this.target;
|
|
1024
|
+
if (target && "then" in target) Promise.resolve(target).then(() => {}, callback);
|
|
1025
|
+
}
|
|
907
1026
|
};
|
|
908
1027
|
var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
909
1028
|
promise;
|
|
@@ -917,12 +1036,16 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
|
917
1036
|
}
|
|
918
1037
|
call(path, args) {
|
|
919
1038
|
args.ensureDeepCopied();
|
|
920
|
-
return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args))
|
|
1039
|
+
return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args), (err) => {
|
|
1040
|
+
args.dispose();
|
|
1041
|
+
throw err;
|
|
1042
|
+
}));
|
|
921
1043
|
}
|
|
922
1044
|
stream(path, args) {
|
|
923
1045
|
args.ensureDeepCopied();
|
|
924
|
-
return { promise: this.promise.then((hook) => {
|
|
925
|
-
|
|
1046
|
+
return { promise: this.promise.then((hook) => hook.stream(path, args).promise, (err) => {
|
|
1047
|
+
args.dispose();
|
|
1048
|
+
throw err;
|
|
926
1049
|
}) };
|
|
927
1050
|
}
|
|
928
1051
|
map(path, captures, instructions) {
|
|
@@ -949,10 +1072,7 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
|
949
1072
|
}, (err) => {});
|
|
950
1073
|
}
|
|
951
1074
|
dispose() {
|
|
952
|
-
|
|
953
|
-
else this.promise.then((hook) => {
|
|
954
|
-
hook.dispose();
|
|
955
|
-
}, (err) => {});
|
|
1075
|
+
this.promise.then((hook) => hook.dispose(), () => {});
|
|
956
1076
|
}
|
|
957
1077
|
onBroken(callback) {
|
|
958
1078
|
if (this.resolution) this.resolution.onBroken(callback);
|
|
@@ -964,6 +1084,75 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
|
964
1084
|
|
|
965
1085
|
//#endregion
|
|
966
1086
|
//#region src/serialize.ts
|
|
1087
|
+
const DEFAULT_MAX_DEPTH = 256;
|
|
1088
|
+
const DEFAULT_LIMITS = {
|
|
1089
|
+
maxBigIntDigits: 16384,
|
|
1090
|
+
maxDepth: 256,
|
|
1091
|
+
maxMessageSize: 33554432
|
|
1092
|
+
};
|
|
1093
|
+
const NATIVE_LITTLE_ENDIAN = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
|
|
1094
|
+
const BYTE_CONTAINER_TYPE_NAMES = [
|
|
1095
|
+
"ArrayBuffer",
|
|
1096
|
+
"DataView",
|
|
1097
|
+
"Int8Array",
|
|
1098
|
+
"Uint8Array",
|
|
1099
|
+
"Uint8ClampedArray",
|
|
1100
|
+
"Int16Array",
|
|
1101
|
+
"Uint16Array",
|
|
1102
|
+
"Int32Array",
|
|
1103
|
+
"Uint32Array",
|
|
1104
|
+
"BigInt64Array",
|
|
1105
|
+
"BigUint64Array",
|
|
1106
|
+
"Float32Array",
|
|
1107
|
+
"Float64Array"
|
|
1108
|
+
];
|
|
1109
|
+
function isValidByteContainerName(value) {
|
|
1110
|
+
return BYTE_CONTAINER_TYPE_NAMES.includes(value);
|
|
1111
|
+
}
|
|
1112
|
+
const TYPED_ARRAY_ELEMENT_SIZE = {
|
|
1113
|
+
ArrayBuffer: void 0,
|
|
1114
|
+
DataView: void 0,
|
|
1115
|
+
Int8Array: void 0,
|
|
1116
|
+
Uint8Array: void 0,
|
|
1117
|
+
Uint8ClampedArray: void 0,
|
|
1118
|
+
Int16Array: 2,
|
|
1119
|
+
Uint16Array: 2,
|
|
1120
|
+
Int32Array: 4,
|
|
1121
|
+
Uint32Array: 4,
|
|
1122
|
+
BigInt64Array: 8,
|
|
1123
|
+
BigUint64Array: 8,
|
|
1124
|
+
Float32Array: 4,
|
|
1125
|
+
Float64Array: 8
|
|
1126
|
+
};
|
|
1127
|
+
const BYTE_CONTAINER_PROTOTYPES = {
|
|
1128
|
+
ArrayBuffer: ArrayBuffer.prototype,
|
|
1129
|
+
DataView: DataView.prototype,
|
|
1130
|
+
Int8Array: Int8Array.prototype,
|
|
1131
|
+
Uint8ClampedArray: Uint8ClampedArray.prototype,
|
|
1132
|
+
Int16Array: Int16Array.prototype,
|
|
1133
|
+
Uint16Array: Uint16Array.prototype,
|
|
1134
|
+
Int32Array: Int32Array.prototype,
|
|
1135
|
+
Uint32Array: Uint32Array.prototype,
|
|
1136
|
+
BigInt64Array: BigInt64Array.prototype,
|
|
1137
|
+
BigUint64Array: BigUint64Array.prototype,
|
|
1138
|
+
Float32Array: Float32Array.prototype,
|
|
1139
|
+
Float64Array: Float64Array.prototype
|
|
1140
|
+
};
|
|
1141
|
+
const BYTE_CONTAINER_TYPE_BY_PROTOTYPE = /* @__PURE__ */ new Map();
|
|
1142
|
+
for (let type of Object.keys(BYTE_CONTAINER_PROTOTYPES)) BYTE_CONTAINER_TYPE_BY_PROTOTYPE.set(BYTE_CONTAINER_PROTOTYPES[type], type);
|
|
1143
|
+
function swapByteOrder(bytes, elementSize) {
|
|
1144
|
+
if (elementSize !== 2 && elementSize !== 4 && elementSize !== 8) throw new RangeError(`Unsupported element size: ${elementSize}`);
|
|
1145
|
+
let view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
1146
|
+
for (let offset = 0; offset < bytes.byteLength; offset += elementSize) switch (elementSize) {
|
|
1147
|
+
case 2:
|
|
1148
|
+
view.setUint16(offset, view.getUint16(offset, false), true);
|
|
1149
|
+
break;
|
|
1150
|
+
case 4:
|
|
1151
|
+
view.setUint32(offset, view.getUint32(offset, false), true);
|
|
1152
|
+
break;
|
|
1153
|
+
case 8: view.setBigUint64(offset, view.getBigUint64(offset, false), true);
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
967
1156
|
var NullExporter = class {
|
|
968
1157
|
exportStub(stub) {
|
|
969
1158
|
throw new Error("Cannot serialize RPC stubs without an RPC session.");
|
|
@@ -984,6 +1173,7 @@ async function streamToBlob(stream, type) {
|
|
|
984
1173
|
return b.type === type ? b : b.slice(0, b.size, type);
|
|
985
1174
|
}
|
|
986
1175
|
const ERROR_TYPES = {
|
|
1176
|
+
__proto__: null,
|
|
987
1177
|
Error,
|
|
988
1178
|
EvalError,
|
|
989
1179
|
RangeError,
|
|
@@ -996,12 +1186,14 @@ const ERROR_TYPES = {
|
|
|
996
1186
|
var Devaluator = class Devaluator {
|
|
997
1187
|
exporter;
|
|
998
1188
|
source;
|
|
999
|
-
|
|
1189
|
+
encodingLevel;
|
|
1190
|
+
constructor(exporter, source, encodingLevel) {
|
|
1000
1191
|
this.exporter = exporter;
|
|
1001
1192
|
this.source = source;
|
|
1193
|
+
this.encodingLevel = encodingLevel;
|
|
1002
1194
|
}
|
|
1003
|
-
static devaluate(value, parent, exporter = NULL_EXPORTER, source) {
|
|
1004
|
-
let devaluator = new Devaluator(exporter, source);
|
|
1195
|
+
static devaluate(value, parent, exporter = NULL_EXPORTER, source, encodingLevel = "string") {
|
|
1196
|
+
let devaluator = new Devaluator(exporter, source, encodingLevel);
|
|
1005
1197
|
try {
|
|
1006
1198
|
return devaluator.devaluateImpl(value, parent, 0);
|
|
1007
1199
|
} catch (err) {
|
|
@@ -1013,7 +1205,7 @@ var Devaluator = class Devaluator {
|
|
|
1013
1205
|
}
|
|
1014
1206
|
exports;
|
|
1015
1207
|
devaluateImpl(value, parent, depth) {
|
|
1016
|
-
if (depth >=
|
|
1208
|
+
if (depth >= 256) throw new Error("Serialization exceeded maximum allowed depth. (Does the message contain cycles?)");
|
|
1017
1209
|
switch (typeForRpc(value)) {
|
|
1018
1210
|
case "unsupported": {
|
|
1019
1211
|
let msg;
|
|
@@ -1024,10 +1216,12 @@ var Devaluator = class Devaluator {
|
|
|
1024
1216
|
}
|
|
1025
1217
|
throw new TypeError(msg);
|
|
1026
1218
|
}
|
|
1027
|
-
case "primitive": if (typeof value === "number" && !isFinite(value))
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1219
|
+
case "primitive": if (typeof value === "number" && !isFinite(value)) {
|
|
1220
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1221
|
+
if (value === Infinity) return ["inf"];
|
|
1222
|
+
else if (value === -Infinity) return ["-inf"];
|
|
1223
|
+
else return ["nan"];
|
|
1224
|
+
} else return value;
|
|
1031
1225
|
case "object": {
|
|
1032
1226
|
let object = value;
|
|
1033
1227
|
let result = {};
|
|
@@ -1041,23 +1235,73 @@ var Devaluator = class Devaluator {
|
|
|
1041
1235
|
for (let i = 0; i < len; i++) result[i] = this.devaluateImpl(array[i], array, depth + 1);
|
|
1042
1236
|
return [result];
|
|
1043
1237
|
}
|
|
1044
|
-
case "
|
|
1238
|
+
case "set": {
|
|
1239
|
+
let set = value;
|
|
1240
|
+
let setElements = [...set];
|
|
1241
|
+
let elements = [];
|
|
1242
|
+
for (let element of setElements) switch (typeForRpc(element)) {
|
|
1243
|
+
case "blob": throw new TypeError("Cannot serialize a Blob as an element of a Set.");
|
|
1244
|
+
case "rpc-promise":
|
|
1245
|
+
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.");
|
|
1246
|
+
}
|
|
1247
|
+
for (let element of setElements) elements.push(this.devaluateImpl(element, set, depth + 1));
|
|
1248
|
+
return ["set", elements];
|
|
1249
|
+
}
|
|
1250
|
+
case "bigint":
|
|
1251
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1252
|
+
return ["bigint", value.toString()];
|
|
1045
1253
|
case "date": {
|
|
1254
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1046
1255
|
const time = value.getTime();
|
|
1047
1256
|
return ["date", Number.isNaN(time) ? null : time];
|
|
1048
1257
|
}
|
|
1258
|
+
case "regexp": {
|
|
1259
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1260
|
+
let re = value;
|
|
1261
|
+
return re.flags ? [
|
|
1262
|
+
"regexp",
|
|
1263
|
+
re.source,
|
|
1264
|
+
re.flags
|
|
1265
|
+
] : ["regexp", re.source];
|
|
1266
|
+
}
|
|
1049
1267
|
case "bytes": {
|
|
1050
|
-
let
|
|
1051
|
-
|
|
1268
|
+
let alternateTypeName = BYTE_CONTAINER_TYPE_BY_PROTOTYPE.get(Object.getPrototypeOf(value));
|
|
1269
|
+
let bytes;
|
|
1270
|
+
if (alternateTypeName === "ArrayBuffer") bytes = new Uint8Array(value);
|
|
1271
|
+
else if (alternateTypeName === void 0) bytes = value;
|
|
1272
|
+
else {
|
|
1273
|
+
let view = value;
|
|
1274
|
+
bytes = new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
|
|
1275
|
+
let elementSize = TYPED_ARRAY_ELEMENT_SIZE[alternateTypeName];
|
|
1276
|
+
if (!NATIVE_LITTLE_ENDIAN && elementSize) {
|
|
1277
|
+
bytes = bytes.slice();
|
|
1278
|
+
swapByteOrder(bytes, elementSize);
|
|
1279
|
+
}
|
|
1280
|
+
}
|
|
1281
|
+
if (this.encodingLevel === "structuredClonable" || this.encodingLevel === "jsonCompatibleWithBytes") {
|
|
1282
|
+
if (bytes.byteLength !== bytes.buffer.byteLength) bytes = new Uint8Array(bytes);
|
|
1283
|
+
return alternateTypeName === void 0 ? ["bytes", bytes] : [
|
|
1284
|
+
"bytes",
|
|
1285
|
+
bytes,
|
|
1286
|
+
alternateTypeName
|
|
1287
|
+
];
|
|
1288
|
+
}
|
|
1052
1289
|
let b64;
|
|
1053
|
-
if (
|
|
1290
|
+
if (bytes.toBase64) b64 = bytes.toBase64({ omitPadding: true });
|
|
1291
|
+
else if (typeof Buffer !== "undefined") b64 = (bytes instanceof Buffer ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)).toString("base64");
|
|
1054
1292
|
else {
|
|
1055
1293
|
let binary = "";
|
|
1056
1294
|
for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]);
|
|
1057
1295
|
b64 = btoa(binary);
|
|
1058
1296
|
}
|
|
1059
|
-
|
|
1297
|
+
b64 = b64.replace(/=+$/, "");
|
|
1298
|
+
return alternateTypeName === void 0 ? ["bytes", b64] : [
|
|
1299
|
+
"bytes",
|
|
1300
|
+
b64,
|
|
1301
|
+
alternateTypeName
|
|
1302
|
+
];
|
|
1060
1303
|
}
|
|
1304
|
+
case "url": return ["url", value.href];
|
|
1061
1305
|
case "headers": return ["headers", [...value]];
|
|
1062
1306
|
case "request": {
|
|
1063
1307
|
let req = value;
|
|
@@ -1172,19 +1416,24 @@ var Devaluator = class Devaluator {
|
|
|
1172
1416
|
} else if (rewritten && rewritten.stack) result.push(rewritten.stack);
|
|
1173
1417
|
return result;
|
|
1174
1418
|
}
|
|
1175
|
-
case "undefined":
|
|
1419
|
+
case "undefined":
|
|
1420
|
+
if (this.encodingLevel === "structuredClonable") return;
|
|
1421
|
+
return ["undefined"];
|
|
1176
1422
|
case "stub":
|
|
1177
1423
|
case "rpc-promise": {
|
|
1178
1424
|
if (!this.source) throw new Error("Can't serialize RPC stubs in this context.");
|
|
1179
1425
|
let { hook, pathIfPromise } = unwrapStubAndPath(value);
|
|
1180
1426
|
let importId = this.exporter.getImport(hook);
|
|
1181
|
-
if (importId !== void 0)
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1427
|
+
if (importId !== void 0) {
|
|
1428
|
+
if (pathIfPromise) {
|
|
1429
|
+
if (pathIfPromise.length > 0) return [
|
|
1430
|
+
"pipeline",
|
|
1431
|
+
importId,
|
|
1432
|
+
pathIfPromise
|
|
1433
|
+
];
|
|
1434
|
+
else return ["pipeline", importId];
|
|
1435
|
+
} else return ["import", importId];
|
|
1436
|
+
}
|
|
1188
1437
|
if (pathIfPromise) hook = hook.get(pathIfPromise);
|
|
1189
1438
|
else hook = hook.dup();
|
|
1190
1439
|
return this.devaluateHook(pathIfPromise ? "promise" : "export", hook);
|
|
@@ -1239,31 +1488,43 @@ var NullImporter = class {
|
|
|
1239
1488
|
getPipeReadable(exportId) {
|
|
1240
1489
|
throw new Error("Cannot retrieve pipe readable without an RPC session.");
|
|
1241
1490
|
}
|
|
1491
|
+
getLimits() {
|
|
1492
|
+
return DEFAULT_LIMITS;
|
|
1493
|
+
}
|
|
1242
1494
|
};
|
|
1243
1495
|
const NULL_IMPORTER = new NullImporter();
|
|
1244
1496
|
function fixBrokenRequestBody(request, body) {
|
|
1245
|
-
|
|
1497
|
+
let promise = new Response(body).arrayBuffer().then((arrayBuffer) => {
|
|
1246
1498
|
let bytes = new Uint8Array(arrayBuffer);
|
|
1247
1499
|
let result = new Request(request, { body: bytes });
|
|
1248
1500
|
return new PayloadStubHook(RpcPayload.fromAppReturn(result));
|
|
1249
|
-
})
|
|
1501
|
+
});
|
|
1502
|
+
return new RpcPromise$1(new PromiseStubHook(promise), []);
|
|
1250
1503
|
}
|
|
1251
1504
|
function streamToBlobPromise(stream, type) {
|
|
1252
|
-
|
|
1505
|
+
let promise = streamToBlob(stream, type).then((blob) => {
|
|
1253
1506
|
return new PayloadStubHook(RpcPayload.fromAppReturn(blob));
|
|
1254
|
-
})
|
|
1507
|
+
});
|
|
1508
|
+
return new RpcPromise$1(new PromiseStubHook(promise), []);
|
|
1255
1509
|
}
|
|
1256
1510
|
var Evaluator = class Evaluator {
|
|
1257
1511
|
importer;
|
|
1258
|
-
|
|
1512
|
+
encodingLevel;
|
|
1513
|
+
limits;
|
|
1514
|
+
constructor(importer, encodingLevel = "string") {
|
|
1259
1515
|
this.importer = importer;
|
|
1516
|
+
this.encodingLevel = encodingLevel;
|
|
1517
|
+
this.limits = importer.getLimits();
|
|
1260
1518
|
}
|
|
1261
1519
|
hooks = [];
|
|
1262
1520
|
promises = [];
|
|
1263
1521
|
evaluate(value) {
|
|
1522
|
+
return this.evaluateWithDepth(value, 0);
|
|
1523
|
+
}
|
|
1524
|
+
evaluateWithDepth(value, depth) {
|
|
1264
1525
|
let payload = RpcPayload.forEvaluate(this.hooks, this.promises);
|
|
1265
1526
|
try {
|
|
1266
|
-
payload.value = this.evaluateImpl(value, payload, "value");
|
|
1527
|
+
payload.value = this.evaluateImpl(value, payload, "value", depth);
|
|
1267
1528
|
return payload;
|
|
1268
1529
|
} catch (err) {
|
|
1269
1530
|
payload.dispose();
|
|
@@ -1273,31 +1534,85 @@ var Evaluator = class Evaluator {
|
|
|
1273
1534
|
evaluateCopy(value) {
|
|
1274
1535
|
return this.evaluate(structuredClone(value));
|
|
1275
1536
|
}
|
|
1276
|
-
evaluateImpl(value, parent, property) {
|
|
1537
|
+
evaluateImpl(value, parent, property, depth) {
|
|
1538
|
+
let maxDepth = this.limits.maxDepth;
|
|
1539
|
+
if (depth >= maxDepth) throw new TypeError(`Deserialization exceeded maximum allowed message depth of ${maxDepth}.`);
|
|
1540
|
+
if (this.encodingLevel === "structuredClonable") {
|
|
1541
|
+
if (value instanceof Date || value instanceof RegExp || typeof value === "bigint") return value;
|
|
1542
|
+
}
|
|
1277
1543
|
if (value instanceof Array) {
|
|
1278
1544
|
if (value.length == 1 && value[0] instanceof Array) {
|
|
1279
1545
|
let result = value[0];
|
|
1280
|
-
for (let i = 0; i < result.length; i++) result[i] = this.evaluateImpl(result[i], result, i);
|
|
1546
|
+
for (let i = 0; i < result.length; i++) result[i] = this.evaluateImpl(result[i], result, i, depth + 1);
|
|
1281
1547
|
return result;
|
|
1282
1548
|
} else switch (value[0]) {
|
|
1283
1549
|
case "bigint":
|
|
1284
|
-
if (typeof value[1] == "string")
|
|
1550
|
+
if (typeof value[1] == "string") {
|
|
1551
|
+
let digits = value[1];
|
|
1552
|
+
let maxBigIntDigits = this.limits.maxBigIntDigits;
|
|
1553
|
+
if (digits.length > maxBigIntDigits) throw new TypeError(`Deserialized bigint exceeds maximum length of ${maxBigIntDigits} digits.`);
|
|
1554
|
+
return BigInt(digits);
|
|
1555
|
+
}
|
|
1285
1556
|
break;
|
|
1286
1557
|
case "date":
|
|
1287
1558
|
if (value[1] === null) return /* @__PURE__ */ new Date(NaN);
|
|
1288
1559
|
if (typeof value[1] == "number") return new Date(value[1]);
|
|
1289
1560
|
break;
|
|
1290
|
-
case "
|
|
1291
|
-
if (typeof value[1]
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
let
|
|
1296
|
-
let
|
|
1297
|
-
for (let i = 0; i <
|
|
1298
|
-
|
|
1561
|
+
case "regexp":
|
|
1562
|
+
if (typeof value[1] === "string" && (value.length === 2 || value.length === 3 && typeof value[2] === "string")) return new RegExp(value[1], value[2]);
|
|
1563
|
+
break;
|
|
1564
|
+
case "set":
|
|
1565
|
+
if (value.length === 2 && value[1] instanceof Array) {
|
|
1566
|
+
let elements = value[1];
|
|
1567
|
+
let set = /* @__PURE__ */ new Set();
|
|
1568
|
+
for (let i = 0; i < elements.length; i++) {
|
|
1569
|
+
let copy = this.evaluateImpl(elements[i], set, i, depth + 1);
|
|
1570
|
+
if (copy instanceof RpcPromise$1) throw new TypeError("Cannot deserialize a promise as an element of a Set.");
|
|
1571
|
+
set.add(copy);
|
|
1572
|
+
}
|
|
1573
|
+
return set;
|
|
1299
1574
|
}
|
|
1300
1575
|
break;
|
|
1576
|
+
case "bytes": {
|
|
1577
|
+
let bytes;
|
|
1578
|
+
if (value[1] instanceof Uint8Array) bytes = value[1];
|
|
1579
|
+
else if (typeof value[1] == "string") {
|
|
1580
|
+
if (typeof Buffer !== "undefined") bytes = Buffer.from(value[1], "base64");
|
|
1581
|
+
else if (Uint8Array.fromBase64) bytes = Uint8Array.fromBase64(value[1]);
|
|
1582
|
+
else {
|
|
1583
|
+
let bs = atob(value[1]);
|
|
1584
|
+
let len = bs.length;
|
|
1585
|
+
bytes = new Uint8Array(len);
|
|
1586
|
+
for (let i = 0; i < len; i++) bytes[i] = bs.charCodeAt(i);
|
|
1587
|
+
}
|
|
1588
|
+
} else break;
|
|
1589
|
+
if (value.length === 2) return bytes;
|
|
1590
|
+
if (typeof value[2] !== "string") throw new TypeError(`Unknown bytes type marker type: ${typeof value[2]}`);
|
|
1591
|
+
if (!isValidByteContainerName(value[2])) {
|
|
1592
|
+
let marker = value[2].slice(0, 64);
|
|
1593
|
+
throw new TypeError(`Unknown bytes type marker: ${marker}`);
|
|
1594
|
+
}
|
|
1595
|
+
let marker = value[2];
|
|
1596
|
+
let elementSize = TYPED_ARRAY_ELEMENT_SIZE[marker];
|
|
1597
|
+
if (elementSize !== void 0 && bytes.byteLength % elementSize !== 0) throw new TypeError(`Invalid byte length ${bytes.byteLength} for ${marker}; expected a multiple of ${elementSize}`);
|
|
1598
|
+
let buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
|
1599
|
+
if (!NATIVE_LITTLE_ENDIAN && elementSize !== void 0) swapByteOrder(new Uint8Array(buffer), elementSize);
|
|
1600
|
+
switch (marker) {
|
|
1601
|
+
case "ArrayBuffer": return buffer;
|
|
1602
|
+
case "DataView": return new DataView(buffer);
|
|
1603
|
+
case "Int8Array": return new Int8Array(buffer);
|
|
1604
|
+
case "Uint8Array": return new Uint8Array(buffer);
|
|
1605
|
+
case "Uint8ClampedArray": return new Uint8ClampedArray(buffer);
|
|
1606
|
+
case "Int16Array": return new Int16Array(buffer);
|
|
1607
|
+
case "Uint16Array": return new Uint16Array(buffer);
|
|
1608
|
+
case "Int32Array": return new Int32Array(buffer);
|
|
1609
|
+
case "Uint32Array": return new Uint32Array(buffer);
|
|
1610
|
+
case "BigInt64Array": return new BigInt64Array(buffer);
|
|
1611
|
+
case "BigUint64Array": return new BigUint64Array(buffer);
|
|
1612
|
+
case "Float32Array": return new Float32Array(buffer);
|
|
1613
|
+
case "Float64Array": return new Float64Array(buffer);
|
|
1614
|
+
}
|
|
1615
|
+
}
|
|
1301
1616
|
case "error":
|
|
1302
1617
|
if (value.length >= 3 && typeof value[1] === "string" && typeof value[2] === "string") {
|
|
1303
1618
|
let cls = ERROR_TYPES[value[1]] || Error;
|
|
@@ -1310,7 +1625,11 @@ var Evaluator = class Evaluator {
|
|
|
1310
1625
|
let propsObj = props;
|
|
1311
1626
|
for (let key of Object.keys(propsObj)) {
|
|
1312
1627
|
if (key === "name" || key === "message" || key === "stack") continue;
|
|
1313
|
-
|
|
1628
|
+
if (key in Object.prototype || key === "toJSON") {
|
|
1629
|
+
this.evaluateImpl(propsObj[key], result, key, depth + 1);
|
|
1630
|
+
continue;
|
|
1631
|
+
}
|
|
1632
|
+
anyResult[key] = this.evaluateImpl(propsObj[key], result, key, depth + 1);
|
|
1314
1633
|
}
|
|
1315
1634
|
}
|
|
1316
1635
|
return result;
|
|
@@ -1322,6 +1641,9 @@ var Evaluator = class Evaluator {
|
|
|
1322
1641
|
case "inf": return Infinity;
|
|
1323
1642
|
case "-inf": return -Infinity;
|
|
1324
1643
|
case "nan": return NaN;
|
|
1644
|
+
case "url":
|
|
1645
|
+
if (value.length === 2 && typeof value[1] === "string") return new URL(value[1]);
|
|
1646
|
+
break;
|
|
1325
1647
|
case "headers":
|
|
1326
1648
|
if (value.length === 2 && value[1] instanceof Array) return new Headers(value[1]);
|
|
1327
1649
|
break;
|
|
@@ -1331,11 +1653,11 @@ var Evaluator = class Evaluator {
|
|
|
1331
1653
|
let init = value[2];
|
|
1332
1654
|
if (typeof init !== "object" || init === null) break;
|
|
1333
1655
|
if (init.body) {
|
|
1334
|
-
init.body = this.evaluateImpl(init.body, init, "body");
|
|
1656
|
+
init.body = this.evaluateImpl(init.body, init, "body", depth + 1);
|
|
1335
1657
|
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.");
|
|
1336
1658
|
}
|
|
1337
1659
|
if (init.signal) {
|
|
1338
|
-
init.signal = this.evaluateImpl(init.signal, init, "signal");
|
|
1660
|
+
init.signal = this.evaluateImpl(init.signal, init, "signal", depth + 1);
|
|
1339
1661
|
if (!(init.signal instanceof AbortSignal)) throw new TypeError("Request siganl must be of type AbortSignal.");
|
|
1340
1662
|
}
|
|
1341
1663
|
if (init.headers && !(init.headers instanceof Array)) throw new TypeError("Request headers must be serialized as an array of pairs.");
|
|
@@ -1352,7 +1674,7 @@ var Evaluator = class Evaluator {
|
|
|
1352
1674
|
}
|
|
1353
1675
|
case "response": {
|
|
1354
1676
|
if (value.length !== 3) break;
|
|
1355
|
-
let body = this.evaluateImpl(value[1], parent, property);
|
|
1677
|
+
let body = this.evaluateImpl(value[1], parent, property, depth + 1);
|
|
1356
1678
|
if (body === null || typeof body === "string" || body instanceof Uint8Array || body instanceof ReadableStream) {} else throw new TypeError("Response body must be of type ReadableStream.");
|
|
1357
1679
|
let init = value[2];
|
|
1358
1680
|
if (typeof init !== "object" || init === null) break;
|
|
@@ -1363,7 +1685,7 @@ var Evaluator = class Evaluator {
|
|
|
1363
1685
|
case "blob": {
|
|
1364
1686
|
if (value.length !== 3 || typeof value[1] !== "string") break;
|
|
1365
1687
|
let contentType = value[1];
|
|
1366
|
-
let content = this.evaluateImpl(value[2], parent, property);
|
|
1688
|
+
let content = this.evaluateImpl(value[2], parent, property, depth + 1);
|
|
1367
1689
|
if (!(content instanceof ReadableStream)) throw new TypeError("Blob content must be serialized as a ReadableStream.");
|
|
1368
1690
|
let promise = streamToBlobPromise(content, contentType);
|
|
1369
1691
|
this.promises.push({
|
|
@@ -1391,11 +1713,13 @@ var Evaluator = class Evaluator {
|
|
|
1391
1713
|
return promise;
|
|
1392
1714
|
} else {
|
|
1393
1715
|
this.hooks.push(hook);
|
|
1394
|
-
return new
|
|
1716
|
+
return new RpcStub$1(hook);
|
|
1395
1717
|
}
|
|
1396
1718
|
};
|
|
1397
|
-
if (value.length == 2)
|
|
1398
|
-
|
|
1719
|
+
if (value.length == 2) {
|
|
1720
|
+
if (isPromise) return addStub(hook.get([]));
|
|
1721
|
+
else return addStub(hook.dup());
|
|
1722
|
+
}
|
|
1399
1723
|
let path = value[2];
|
|
1400
1724
|
if (!(path instanceof Array)) break;
|
|
1401
1725
|
if (!path.every((part) => {
|
|
@@ -1404,7 +1728,7 @@ var Evaluator = class Evaluator {
|
|
|
1404
1728
|
if (value.length == 3) return addStub(hook.get(path));
|
|
1405
1729
|
let args = value[3];
|
|
1406
1730
|
if (!(args instanceof Array)) break;
|
|
1407
|
-
args = new Evaluator(this.importer).
|
|
1731
|
+
args = new Evaluator(this.importer).evaluateWithDepth([args], depth);
|
|
1408
1732
|
return addStub(hook.call(path, args));
|
|
1409
1733
|
}
|
|
1410
1734
|
case "remap": {
|
|
@@ -1425,7 +1749,8 @@ var Evaluator = class Evaluator {
|
|
|
1425
1749
|
}
|
|
1426
1750
|
});
|
|
1427
1751
|
let instructions = value[4];
|
|
1428
|
-
let
|
|
1752
|
+
let resultHook = hook.map(path, captures, instructions);
|
|
1753
|
+
let promise = new RpcPromise$1(resultHook, []);
|
|
1429
1754
|
this.promises.push({
|
|
1430
1755
|
promise,
|
|
1431
1756
|
parent,
|
|
@@ -1435,18 +1760,21 @@ var Evaluator = class Evaluator {
|
|
|
1435
1760
|
}
|
|
1436
1761
|
case "export":
|
|
1437
1762
|
case "promise":
|
|
1438
|
-
if (typeof value[1] == "number")
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1763
|
+
if (typeof value[1] == "number") {
|
|
1764
|
+
if (value[0] == "promise") {
|
|
1765
|
+
let hook = this.importer.importPromise(value[1]);
|
|
1766
|
+
let promise = new RpcPromise$1(hook, []);
|
|
1767
|
+
this.promises.push({
|
|
1768
|
+
parent,
|
|
1769
|
+
property,
|
|
1770
|
+
promise
|
|
1771
|
+
});
|
|
1772
|
+
return promise;
|
|
1773
|
+
} else {
|
|
1774
|
+
let hook = this.importer.importStub(value[1]);
|
|
1775
|
+
this.hooks.push(hook);
|
|
1776
|
+
return new RpcStub$1(hook);
|
|
1777
|
+
}
|
|
1450
1778
|
}
|
|
1451
1779
|
break;
|
|
1452
1780
|
case "writable":
|
|
@@ -1457,22 +1785,20 @@ var Evaluator = class Evaluator {
|
|
|
1457
1785
|
return stream;
|
|
1458
1786
|
}
|
|
1459
1787
|
break;
|
|
1460
|
-
case "readable":
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
}
|
|
1467
|
-
break;
|
|
1788
|
+
case "readable": if (typeof value[1] == "number") {
|
|
1789
|
+
let stream = this.importer.getPipeReadable(value[1]);
|
|
1790
|
+
let hook = streamImpl.createReadableStreamHook(stream);
|
|
1791
|
+
this.hooks.push(hook);
|
|
1792
|
+
return stream;
|
|
1793
|
+
}
|
|
1468
1794
|
}
|
|
1469
1795
|
throw new TypeError(`unknown special value: ${JSON.stringify(value)}`);
|
|
1470
1796
|
} else if (value instanceof Object) {
|
|
1471
1797
|
let result = value;
|
|
1472
1798
|
for (let key in result) if (key in Object.prototype || key === "toJSON") {
|
|
1473
|
-
this.evaluateImpl(result[key], result, key);
|
|
1799
|
+
this.evaluateImpl(result[key], result, key, depth + 1);
|
|
1474
1800
|
delete result[key];
|
|
1475
|
-
} else result[key] = this.evaluateImpl(result[key], result, key);
|
|
1801
|
+
} else result[key] = this.evaluateImpl(result[key], result, key, depth + 1);
|
|
1476
1802
|
return result;
|
|
1477
1803
|
} else return value;
|
|
1478
1804
|
}
|
|
@@ -1488,6 +1814,48 @@ function deserialize(value) {
|
|
|
1488
1814
|
|
|
1489
1815
|
//#endregion
|
|
1490
1816
|
//#region src/rpc.ts
|
|
1817
|
+
const ESTIMATED_OBJECT_OVERHEAD = 16;
|
|
1818
|
+
const ESTIMATED_ENTRY_OVERHEAD = 8;
|
|
1819
|
+
const ESTIMATED_BINARY_OVERHEAD = 16;
|
|
1820
|
+
const MAX_ESTIMATE_DEPTH = 64;
|
|
1821
|
+
function estimateStringSize(value) {
|
|
1822
|
+
return 2 + value.length * 3;
|
|
1823
|
+
}
|
|
1824
|
+
function estimateEncodedSize(value, seen, depth = 0) {
|
|
1825
|
+
if (depth >= MAX_ESTIMATE_DEPTH) return ESTIMATED_ENTRY_OVERHEAD;
|
|
1826
|
+
switch (typeof value) {
|
|
1827
|
+
case "string": return estimateStringSize(value);
|
|
1828
|
+
case "number": return 16;
|
|
1829
|
+
case "bigint": return 16;
|
|
1830
|
+
case "boolean": return 8;
|
|
1831
|
+
case "undefined": return 16;
|
|
1832
|
+
case "object": {
|
|
1833
|
+
if (value === null) return 8;
|
|
1834
|
+
if (ArrayBuffer.isView(value)) return ESTIMATED_BINARY_OVERHEAD + value.byteLength;
|
|
1835
|
+
if (value instanceof ArrayBuffer) return ESTIMATED_BINARY_OVERHEAD + value.byteLength;
|
|
1836
|
+
if (typeof Blob !== "undefined" && value instanceof Blob) return ESTIMATED_BINARY_OVERHEAD + value.size;
|
|
1837
|
+
if (value instanceof Date) return 16;
|
|
1838
|
+
if (value instanceof RegExp) return ESTIMATED_OBJECT_OVERHEAD + estimateStringSize(value.source) + estimateStringSize(value.flags);
|
|
1839
|
+
seen ??= /* @__PURE__ */ new WeakSet();
|
|
1840
|
+
if (seen.has(value)) return ESTIMATED_ENTRY_OVERHEAD;
|
|
1841
|
+
seen.add(value);
|
|
1842
|
+
if (value instanceof Array) {
|
|
1843
|
+
let size = ESTIMATED_OBJECT_OVERHEAD;
|
|
1844
|
+
for (let item of value) size += ESTIMATED_ENTRY_OVERHEAD + estimateEncodedSize(item, seen, depth + 1);
|
|
1845
|
+
return size;
|
|
1846
|
+
}
|
|
1847
|
+
if (value instanceof Error) {
|
|
1848
|
+
let size = ESTIMATED_OBJECT_OVERHEAD + estimateStringSize(value.name) + estimateStringSize(value.message) + estimateStringSize(value.stack ?? "");
|
|
1849
|
+
for (let key of Object.keys(value)) size += ESTIMATED_ENTRY_OVERHEAD + estimateStringSize(key) + estimateEncodedSize(value[key], seen, depth + 1);
|
|
1850
|
+
return size;
|
|
1851
|
+
}
|
|
1852
|
+
let size = ESTIMATED_OBJECT_OVERHEAD;
|
|
1853
|
+
for (let key of Object.keys(value)) size += ESTIMATED_ENTRY_OVERHEAD + estimateStringSize(key) + estimateEncodedSize(value[key], seen, depth + 1);
|
|
1854
|
+
return size;
|
|
1855
|
+
}
|
|
1856
|
+
default: return 16;
|
|
1857
|
+
}
|
|
1858
|
+
}
|
|
1491
1859
|
var ImportTableEntry = class {
|
|
1492
1860
|
session;
|
|
1493
1861
|
importId;
|
|
@@ -1580,24 +1948,28 @@ var RpcImportHook = class RpcImportHook extends StubHook {
|
|
|
1580
1948
|
if (this.entry) return this.entry;
|
|
1581
1949
|
else throw new Error("This RpcImportHook was already disposed.");
|
|
1582
1950
|
}
|
|
1951
|
+
getEntryTakingOwnership(disposeOwned) {
|
|
1952
|
+
try {
|
|
1953
|
+
return this.getEntry();
|
|
1954
|
+
} catch (err) {
|
|
1955
|
+
disposeOwned();
|
|
1956
|
+
throw err;
|
|
1957
|
+
}
|
|
1958
|
+
}
|
|
1583
1959
|
call(path, args) {
|
|
1584
|
-
let entry = this.
|
|
1960
|
+
let entry = this.getEntryTakingOwnership(() => args.dispose());
|
|
1585
1961
|
if (entry.resolution) return entry.resolution.call(path, args);
|
|
1586
1962
|
else return entry.session.sendCall(entry.importId, path, args);
|
|
1587
1963
|
}
|
|
1588
1964
|
stream(path, args) {
|
|
1589
|
-
let entry = this.
|
|
1965
|
+
let entry = this.getEntryTakingOwnership(() => args.dispose());
|
|
1590
1966
|
if (entry.resolution) return entry.resolution.stream(path, args);
|
|
1591
1967
|
else return entry.session.sendStream(entry.importId, path, args);
|
|
1592
1968
|
}
|
|
1593
1969
|
map(path, captures, instructions) {
|
|
1594
|
-
let entry
|
|
1595
|
-
try {
|
|
1596
|
-
entry = this.getEntry();
|
|
1597
|
-
} catch (err) {
|
|
1970
|
+
let entry = this.getEntryTakingOwnership(() => {
|
|
1598
1971
|
for (let cap of captures) cap.dispose();
|
|
1599
|
-
|
|
1600
|
-
}
|
|
1972
|
+
});
|
|
1601
1973
|
if (entry.resolution) return entry.resolution.map(path, captures, instructions);
|
|
1602
1974
|
else return entry.session.sendMap(entry.importId, path, captures, instructions);
|
|
1603
1975
|
}
|
|
@@ -1653,9 +2025,24 @@ var RpcSessionImpl = class {
|
|
|
1653
2025
|
onBatchDone;
|
|
1654
2026
|
pullCount = 0;
|
|
1655
2027
|
onBrokenCallbacks = [];
|
|
2028
|
+
encodingLevel;
|
|
2029
|
+
limits;
|
|
1656
2030
|
constructor(transport, mainHook, options) {
|
|
1657
2031
|
this.transport = transport;
|
|
1658
2032
|
this.options = options;
|
|
2033
|
+
let level = "string";
|
|
2034
|
+
if ("encodingLevel" in transport) {
|
|
2035
|
+
let raw = transport.encodingLevel;
|
|
2036
|
+
if (raw !== void 0) {
|
|
2037
|
+
if (raw !== "string" && raw !== "jsonCompatible" && raw !== "jsonCompatibleWithBytes" && raw !== "structuredClonable") throw new TypeError(`Unknown transport encodingLevel: ${String(raw)}`);
|
|
2038
|
+
level = raw;
|
|
2039
|
+
}
|
|
2040
|
+
}
|
|
2041
|
+
this.encodingLevel = level;
|
|
2042
|
+
this.limits = {
|
|
2043
|
+
...DEFAULT_LIMITS,
|
|
2044
|
+
...options.limits
|
|
2045
|
+
};
|
|
1659
2046
|
this.exports.push({
|
|
1660
2047
|
hook: mainHook,
|
|
1661
2048
|
refcount: 1
|
|
@@ -1736,7 +2123,7 @@ var RpcSessionImpl = class {
|
|
|
1736
2123
|
let autoRelease = exp.autoRelease;
|
|
1737
2124
|
++this.pullCount;
|
|
1738
2125
|
exp.pull = resolve().then((payload) => {
|
|
1739
|
-
let value = Devaluator.devaluate(payload.value, void 0, this, payload);
|
|
2126
|
+
let value = Devaluator.devaluate(payload.value, void 0, this, payload, this.encodingLevel);
|
|
1740
2127
|
this.send([
|
|
1741
2128
|
"resolve",
|
|
1742
2129
|
exportId,
|
|
@@ -1747,7 +2134,7 @@ var RpcSessionImpl = class {
|
|
|
1747
2134
|
this.send([
|
|
1748
2135
|
"reject",
|
|
1749
2136
|
exportId,
|
|
1750
|
-
Devaluator.devaluate(error, void 0, this)
|
|
2137
|
+
Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)
|
|
1751
2138
|
]);
|
|
1752
2139
|
if (autoRelease) this.releaseExport(exportId, 1);
|
|
1753
2140
|
}).catch((error) => {
|
|
@@ -1755,7 +2142,7 @@ var RpcSessionImpl = class {
|
|
|
1755
2142
|
this.send([
|
|
1756
2143
|
"reject",
|
|
1757
2144
|
exportId,
|
|
1758
|
-
Devaluator.devaluate(error, void 0, this)
|
|
2145
|
+
Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)
|
|
1759
2146
|
]);
|
|
1760
2147
|
if (autoRelease) this.releaseExport(exportId, 1);
|
|
1761
2148
|
} catch (error2) {
|
|
@@ -1798,6 +2185,9 @@ var RpcSessionImpl = class {
|
|
|
1798
2185
|
entry.pipeReadable = void 0;
|
|
1799
2186
|
return readable;
|
|
1800
2187
|
}
|
|
2188
|
+
getLimits() {
|
|
2189
|
+
return this.limits;
|
|
2190
|
+
}
|
|
1801
2191
|
createPipe(readable, readableHook) {
|
|
1802
2192
|
if (this.abortReason) throw this.abortReason;
|
|
1803
2193
|
this.send(["pipe"]);
|
|
@@ -1811,27 +2201,52 @@ var RpcSessionImpl = class {
|
|
|
1811
2201
|
}
|
|
1812
2202
|
send(msg) {
|
|
1813
2203
|
if (this.abortReason !== void 0) return 0;
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
msgText = JSON.stringify(msg);
|
|
1817
|
-
} catch (err) {
|
|
2204
|
+
if (this.encodingLevel === "string") {
|
|
2205
|
+
let msgText;
|
|
1818
2206
|
try {
|
|
1819
|
-
|
|
1820
|
-
} catch (
|
|
1821
|
-
|
|
2207
|
+
msgText = JSON.stringify(msg);
|
|
2208
|
+
} catch (err) {
|
|
2209
|
+
try {
|
|
2210
|
+
this.abort(err);
|
|
2211
|
+
} catch (err2) {}
|
|
2212
|
+
throw err;
|
|
2213
|
+
}
|
|
2214
|
+
try {
|
|
2215
|
+
let sent = this.transport.send(msgText);
|
|
2216
|
+
if (sent !== void 0 && typeof sent.catch === "function") sent.catch((err) => this.abort(err, false));
|
|
2217
|
+
} catch (err) {
|
|
2218
|
+
queueMicrotask(() => this.abort(err, false));
|
|
2219
|
+
}
|
|
2220
|
+
return msgText.length;
|
|
2221
|
+
} else try {
|
|
2222
|
+
let size = this.transport.send(msg);
|
|
2223
|
+
if (typeof size === "number") return size;
|
|
2224
|
+
let thenable = size;
|
|
2225
|
+
if (thenable && typeof thenable.then === "function") Promise.resolve(thenable).catch((err) => this.abort(err, false));
|
|
2226
|
+
return;
|
|
2227
|
+
} catch (err) {
|
|
2228
|
+
queueMicrotask(() => this.abort(err, false));
|
|
2229
|
+
return;
|
|
1822
2230
|
}
|
|
1823
|
-
this.transport.send(msgText).catch((err) => this.abort(err, false));
|
|
1824
|
-
return msgText.length;
|
|
1825
2231
|
}
|
|
1826
2232
|
sendCall(id, path, args) {
|
|
1827
|
-
if (this.abortReason)
|
|
2233
|
+
if (this.abortReason) {
|
|
2234
|
+
args?.dispose();
|
|
2235
|
+
throw this.abortReason;
|
|
2236
|
+
}
|
|
1828
2237
|
let value = [
|
|
1829
2238
|
"pipeline",
|
|
1830
2239
|
id,
|
|
1831
2240
|
path
|
|
1832
2241
|
];
|
|
1833
2242
|
if (args) {
|
|
1834
|
-
let devalue
|
|
2243
|
+
let devalue;
|
|
2244
|
+
try {
|
|
2245
|
+
devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
|
|
2246
|
+
} catch (err) {
|
|
2247
|
+
args.dispose();
|
|
2248
|
+
throw err;
|
|
2249
|
+
}
|
|
1835
2250
|
value.push(devalue[0]);
|
|
1836
2251
|
}
|
|
1837
2252
|
this.send(["push", value]);
|
|
@@ -1840,15 +2255,26 @@ var RpcSessionImpl = class {
|
|
|
1840
2255
|
return new RpcImportHook(true, entry);
|
|
1841
2256
|
}
|
|
1842
2257
|
sendStream(id, path, args) {
|
|
1843
|
-
if (this.abortReason)
|
|
2258
|
+
if (this.abortReason) {
|
|
2259
|
+
args.dispose();
|
|
2260
|
+
throw this.abortReason;
|
|
2261
|
+
}
|
|
1844
2262
|
let value = [
|
|
1845
2263
|
"pipeline",
|
|
1846
2264
|
id,
|
|
1847
2265
|
path
|
|
1848
2266
|
];
|
|
1849
|
-
let devalue
|
|
2267
|
+
let devalue;
|
|
2268
|
+
try {
|
|
2269
|
+
devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
|
|
2270
|
+
} catch (err) {
|
|
2271
|
+
args.dispose();
|
|
2272
|
+
throw err;
|
|
2273
|
+
}
|
|
1850
2274
|
value.push(devalue[0]);
|
|
1851
|
-
let
|
|
2275
|
+
let msg = ["stream", value];
|
|
2276
|
+
let size = this.send(msg);
|
|
2277
|
+
if (size === void 0) size = estimateEncodedSize(msg);
|
|
1852
2278
|
let importId = this.imports.length;
|
|
1853
2279
|
let entry = new ImportTableEntry(this, importId, true);
|
|
1854
2280
|
entry.remoteRefcount = 0;
|
|
@@ -1904,7 +2330,14 @@ var RpcSessionImpl = class {
|
|
|
1904
2330
|
this.cancelReadLoop?.(error);
|
|
1905
2331
|
this.cancelReadLoop = void 0;
|
|
1906
2332
|
if (trySendAbortMessage) try {
|
|
1907
|
-
|
|
2333
|
+
let abortMsg = ["abort", Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)];
|
|
2334
|
+
if (this.encodingLevel === "string") {
|
|
2335
|
+
let sent = this.transport.send(JSON.stringify(abortMsg));
|
|
2336
|
+
if (sent !== void 0 && typeof sent.catch === "function") sent.catch((err) => {});
|
|
2337
|
+
} else {
|
|
2338
|
+
let result = this.transport.send(abortMsg);
|
|
2339
|
+
if (result && typeof result.then === "function") Promise.resolve(result).catch((err) => {});
|
|
2340
|
+
}
|
|
1908
2341
|
} catch (err) {}
|
|
1909
2342
|
if (error === void 0) error = "undefined";
|
|
1910
2343
|
this.abortReason = error;
|
|
@@ -1926,18 +2359,20 @@ var RpcSessionImpl = class {
|
|
|
1926
2359
|
while (!this.abortReason) {
|
|
1927
2360
|
let readCanceled = Promise.withResolvers();
|
|
1928
2361
|
this.cancelReadLoop = readCanceled.reject;
|
|
1929
|
-
let
|
|
2362
|
+
let raw;
|
|
1930
2363
|
try {
|
|
1931
|
-
|
|
2364
|
+
raw = await Promise.race([this.transport.receive(), readCanceled.promise]);
|
|
1932
2365
|
} finally {
|
|
1933
2366
|
if (this.cancelReadLoop === readCanceled.reject) this.cancelReadLoop = void 0;
|
|
1934
2367
|
}
|
|
1935
|
-
|
|
2368
|
+
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.`);
|
|
1936
2369
|
if (this.abortReason) break;
|
|
2370
|
+
let msg = this.encodingLevel === "string" ? JSON.parse(raw) : raw;
|
|
1937
2371
|
if (msg instanceof Array) switch (msg[0]) {
|
|
1938
2372
|
case "push":
|
|
1939
2373
|
if (msg.length > 1) {
|
|
1940
|
-
let
|
|
2374
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[1]);
|
|
2375
|
+
let hook = new PayloadStubHook(payload);
|
|
1941
2376
|
hook.ignoreUnhandledRejections();
|
|
1942
2377
|
this.exports.push({
|
|
1943
2378
|
hook,
|
|
@@ -1948,7 +2383,8 @@ var RpcSessionImpl = class {
|
|
|
1948
2383
|
break;
|
|
1949
2384
|
case "stream":
|
|
1950
2385
|
if (msg.length > 1) {
|
|
1951
|
-
let
|
|
2386
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[1]);
|
|
2387
|
+
let hook = new PayloadStubHook(payload);
|
|
1952
2388
|
hook.ignoreUnhandledRejections();
|
|
1953
2389
|
let exportId = this.exports.length;
|
|
1954
2390
|
this.exports.push({
|
|
@@ -1983,13 +2419,14 @@ var RpcSessionImpl = class {
|
|
|
1983
2419
|
let importId = msg[1];
|
|
1984
2420
|
if (typeof importId == "number" && msg.length > 2) {
|
|
1985
2421
|
let imp = this.imports[importId];
|
|
1986
|
-
if (imp)
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
2422
|
+
if (imp) {
|
|
2423
|
+
if (msg[0] == "resolve") imp.resolve(new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[2])));
|
|
2424
|
+
else {
|
|
2425
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[2]);
|
|
2426
|
+
payload.dispose();
|
|
2427
|
+
imp.resolve(new ErrorStubHook(payload.value));
|
|
2428
|
+
}
|
|
2429
|
+
} else if (msg[0] == "resolve") new Evaluator(this, this.encodingLevel).evaluate(msg[2]).dispose();
|
|
1993
2430
|
continue;
|
|
1994
2431
|
}
|
|
1995
2432
|
break;
|
|
@@ -2004,9 +2441,9 @@ var RpcSessionImpl = class {
|
|
|
2004
2441
|
break;
|
|
2005
2442
|
}
|
|
2006
2443
|
case "abort": {
|
|
2007
|
-
let payload = new Evaluator(this).evaluate(msg[1]);
|
|
2444
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[1]);
|
|
2008
2445
|
payload.dispose();
|
|
2009
|
-
this.abort(payload, false);
|
|
2446
|
+
this.abort(payload.value, false);
|
|
2010
2447
|
break;
|
|
2011
2448
|
}
|
|
2012
2449
|
}
|
|
@@ -2057,9 +2494,12 @@ var RpcSession$1 = class {
|
|
|
2057
2494
|
|
|
2058
2495
|
//#endregion
|
|
2059
2496
|
//#region src/websocket.ts
|
|
2497
|
+
/** Close-frame reason max UTF-8 bytes: 125 payload - 2-byte status (RFC 6455 section 5.5). */
|
|
2498
|
+
const MAX_CLOSE_REASON_BYTES = 123;
|
|
2060
2499
|
function newWebSocketRpcSession$1(webSocket, localMain, options) {
|
|
2061
2500
|
if (typeof webSocket === "string") webSocket = new WebSocket(webSocket);
|
|
2062
|
-
|
|
2501
|
+
let transport = new WebSocketTransport(webSocket);
|
|
2502
|
+
return new RpcSession$1(transport, localMain, options).getRemoteMain();
|
|
2063
2503
|
}
|
|
2064
2504
|
/**
|
|
2065
2505
|
* For use in Cloudflare Workers: Construct an HTTP response that starts a WebSocket RPC session
|
|
@@ -2076,9 +2516,14 @@ function newWorkersWebSocketRpcResponse(request, localMain, options) {
|
|
|
2076
2516
|
webSocket: pair[1]
|
|
2077
2517
|
});
|
|
2078
2518
|
}
|
|
2519
|
+
/**
|
|
2520
|
+
* Generic WebSocket transport. Default `T = string` is backward-compatible and satisfies
|
|
2521
|
+
* `RpcTransport`. Use `T = ArrayBuffer` as a building block for binary transports.
|
|
2522
|
+
*/
|
|
2079
2523
|
var WebSocketTransport = class {
|
|
2080
2524
|
constructor(webSocket) {
|
|
2081
2525
|
this.#webSocket = webSocket;
|
|
2526
|
+
webSocket.binaryType = "arraybuffer";
|
|
2082
2527
|
if (webSocket.readyState === WebSocket.CONNECTING) {
|
|
2083
2528
|
this.#sendQueue = [];
|
|
2084
2529
|
webSocket.addEventListener("open", (event) => {
|
|
@@ -2091,12 +2536,13 @@ var WebSocketTransport = class {
|
|
|
2091
2536
|
});
|
|
2092
2537
|
}
|
|
2093
2538
|
webSocket.addEventListener("message", (event) => {
|
|
2094
|
-
if (this.#error) {} else if (typeof event.data === "string"
|
|
2095
|
-
this.#receiveResolver
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2539
|
+
if (this.#error) {} else if (typeof event.data === "string" || event.data instanceof ArrayBuffer) {
|
|
2540
|
+
if (this.#receiveResolver) {
|
|
2541
|
+
this.#receiveResolver(event.data);
|
|
2542
|
+
this.#receiveResolver = void 0;
|
|
2543
|
+
this.#receiveRejecter = void 0;
|
|
2544
|
+
} else this.#receiveQueue.push(event.data);
|
|
2545
|
+
} else this.#receivedError(/* @__PURE__ */ new TypeError("Received unexpected message type from WebSocket."));
|
|
2100
2546
|
});
|
|
2101
2547
|
webSocket.addEventListener("close", (event) => {
|
|
2102
2548
|
this.#receivedError(/* @__PURE__ */ new Error(`Peer closed WebSocket: ${event.code} ${event.reason}`));
|
|
@@ -2111,13 +2557,13 @@ var WebSocketTransport = class {
|
|
|
2111
2557
|
#receiveRejecter;
|
|
2112
2558
|
#receiveQueue = [];
|
|
2113
2559
|
#error;
|
|
2114
|
-
|
|
2560
|
+
send(message) {
|
|
2115
2561
|
if (this.#sendQueue === void 0) this.#webSocket.send(message);
|
|
2116
2562
|
else this.#sendQueue.push(message);
|
|
2117
2563
|
}
|
|
2118
|
-
|
|
2119
|
-
if (this.#receiveQueue.length > 0) return this.#receiveQueue.shift();
|
|
2120
|
-
else if (this.#error)
|
|
2564
|
+
receive() {
|
|
2565
|
+
if (this.#receiveQueue.length > 0) return Promise.resolve(this.#receiveQueue.shift());
|
|
2566
|
+
else if (this.#error) return Promise.reject(this.#error);
|
|
2121
2567
|
else return new Promise((resolve, reject) => {
|
|
2122
2568
|
this.#receiveResolver = resolve;
|
|
2123
2569
|
this.#receiveRejecter = reject;
|
|
@@ -2127,6 +2573,8 @@ var WebSocketTransport = class {
|
|
|
2127
2573
|
let message;
|
|
2128
2574
|
if (reason instanceof Error) message = reason.message;
|
|
2129
2575
|
else message = `${reason}`;
|
|
2576
|
+
let reasonBytes = new TextEncoder().encode(message);
|
|
2577
|
+
if (reasonBytes.length > 123) message = new TextDecoder().decode(reasonBytes.subarray(0, 123), { stream: true });
|
|
2130
2578
|
this.#webSocket.close(3e3, message);
|
|
2131
2579
|
if (!this.#error) this.#error = reason;
|
|
2132
2580
|
}
|
|
@@ -2144,6 +2592,7 @@ var WebSocketTransport = class {
|
|
|
2144
2592
|
|
|
2145
2593
|
//#endregion
|
|
2146
2594
|
//#region src/batch.ts
|
|
2595
|
+
const yieldToMacrotask = typeof setImmediate === "function" ? () => new Promise((resolve) => setImmediate(resolve)) : () => new Promise((resolve) => setTimeout(resolve, 0));
|
|
2147
2596
|
var BatchClientTransport = class {
|
|
2148
2597
|
constructor(sendBatch) {
|
|
2149
2598
|
this.#promise = this.#scheduleBatch(sendBatch);
|
|
@@ -2152,7 +2601,7 @@ var BatchClientTransport = class {
|
|
|
2152
2601
|
#aborted;
|
|
2153
2602
|
#batchToSend = [];
|
|
2154
2603
|
#batchToReceive = null;
|
|
2155
|
-
|
|
2604
|
+
send(message) {
|
|
2156
2605
|
if (this.#batchToSend !== null) this.#batchToSend.push(message);
|
|
2157
2606
|
}
|
|
2158
2607
|
async receive() {
|
|
@@ -2165,7 +2614,7 @@ var BatchClientTransport = class {
|
|
|
2165
2614
|
this.#aborted = reason;
|
|
2166
2615
|
}
|
|
2167
2616
|
async #scheduleBatch(sendBatch) {
|
|
2168
|
-
await
|
|
2617
|
+
await yieldToMacrotask();
|
|
2169
2618
|
if (this.#aborted !== void 0) throw this.#aborted;
|
|
2170
2619
|
let batch = this.#batchToSend;
|
|
2171
2620
|
this.#batchToSend = null;
|
|
@@ -2185,7 +2634,8 @@ function newHttpBatchRpcSession$1(urlOrRequest, options) {
|
|
|
2185
2634
|
let body = await response.text();
|
|
2186
2635
|
return body == "" ? [] : body.split("\n");
|
|
2187
2636
|
};
|
|
2188
|
-
|
|
2637
|
+
let transport = new BatchClientTransport(sendBatch);
|
|
2638
|
+
return new RpcSession$1(transport, void 0, options).getRemoteMain();
|
|
2189
2639
|
}
|
|
2190
2640
|
var BatchServerTransport = class {
|
|
2191
2641
|
constructor(batch) {
|
|
@@ -2194,7 +2644,7 @@ var BatchServerTransport = class {
|
|
|
2194
2644
|
#batchToSend = [];
|
|
2195
2645
|
#batchToReceive;
|
|
2196
2646
|
#allReceived = Promise.withResolvers();
|
|
2197
|
-
|
|
2647
|
+
send(message) {
|
|
2198
2648
|
this.#batchToSend.push(message);
|
|
2199
2649
|
}
|
|
2200
2650
|
async receive() {
|
|
@@ -2228,7 +2678,8 @@ var BatchServerTransport = class {
|
|
|
2228
2678
|
async function newHttpBatchRpcResponse(request, localMain, options) {
|
|
2229
2679
|
if (request.method !== "POST") return new Response("This endpoint only accepts POST requests.", { status: 405 });
|
|
2230
2680
|
let body = await request.text();
|
|
2231
|
-
let
|
|
2681
|
+
let batch = body === "" ? [] : body.split("\n");
|
|
2682
|
+
let transport = new BatchServerTransport(batch);
|
|
2232
2683
|
let rpc = new RpcSession$1(transport, localMain, options);
|
|
2233
2684
|
await transport.whenAllReceived();
|
|
2234
2685
|
await rpc.drain();
|
|
@@ -2243,7 +2694,11 @@ async function newHttpBatchRpcResponse(request, localMain, options) {
|
|
|
2243
2694
|
* @param options Optional RPC session options. You can also pass headers to set on the response.
|
|
2244
2695
|
*/
|
|
2245
2696
|
async function nodeHttpBatchRpcResponse(request, response, localMain, options) {
|
|
2246
|
-
if (request.method !== "POST")
|
|
2697
|
+
if (request.method !== "POST") {
|
|
2698
|
+
response.writeHead(405, "This endpoint only accepts POST requests.", options?.headers);
|
|
2699
|
+
response.end();
|
|
2700
|
+
return;
|
|
2701
|
+
}
|
|
2247
2702
|
let body = await new Promise((resolve, reject) => {
|
|
2248
2703
|
let chunks = [];
|
|
2249
2704
|
request.on("data", (chunk) => {
|
|
@@ -2254,7 +2709,8 @@ async function nodeHttpBatchRpcResponse(request, response, localMain, options) {
|
|
|
2254
2709
|
});
|
|
2255
2710
|
request.on("error", reject);
|
|
2256
2711
|
});
|
|
2257
|
-
let
|
|
2712
|
+
let batch = body === "" ? [] : body.split("\n");
|
|
2713
|
+
let transport = new BatchServerTransport(batch);
|
|
2258
2714
|
let rpc = new RpcSession$1(transport, localMain, options);
|
|
2259
2715
|
await transport.whenAllReceived();
|
|
2260
2716
|
await rpc.drain();
|
|
@@ -2265,20 +2721,21 @@ async function nodeHttpBatchRpcResponse(request, response, localMain, options) {
|
|
|
2265
2721
|
//#endregion
|
|
2266
2722
|
//#region src/messageport.ts
|
|
2267
2723
|
function newMessagePortRpcSession$1(port, localMain, options) {
|
|
2268
|
-
|
|
2724
|
+
let transport = new MessagePortTransport(port);
|
|
2725
|
+
return new RpcSession$1(transport, localMain, options).getRemoteMain();
|
|
2269
2726
|
}
|
|
2270
2727
|
var MessagePortTransport = class {
|
|
2728
|
+
encodingLevel = "structuredClonable";
|
|
2271
2729
|
constructor(port) {
|
|
2272
2730
|
this.#port = port;
|
|
2273
2731
|
port.start();
|
|
2274
2732
|
port.addEventListener("message", (event) => {
|
|
2275
2733
|
if (this.#error) {} else if (event.data === null) this.#receivedError(/* @__PURE__ */ new Error("Peer closed MessagePort connection."));
|
|
2276
|
-
else if (
|
|
2734
|
+
else if (this.#receiveResolver) {
|
|
2277
2735
|
this.#receiveResolver(event.data);
|
|
2278
2736
|
this.#receiveResolver = void 0;
|
|
2279
2737
|
this.#receiveRejecter = void 0;
|
|
2280
2738
|
} else this.#receiveQueue.push(event.data);
|
|
2281
|
-
else this.#receivedError(/* @__PURE__ */ new TypeError("Received non-string message from MessagePort."));
|
|
2282
2739
|
});
|
|
2283
2740
|
port.addEventListener("messageerror", (event) => {
|
|
2284
2741
|
this.#receivedError(/* @__PURE__ */ new Error("MessagePort message error."));
|
|
@@ -2289,7 +2746,7 @@ var MessagePortTransport = class {
|
|
|
2289
2746
|
#receiveRejecter;
|
|
2290
2747
|
#receiveQueue = [];
|
|
2291
2748
|
#error;
|
|
2292
|
-
|
|
2749
|
+
send(message) {
|
|
2293
2750
|
if (this.#error) throw this.#error;
|
|
2294
2751
|
this.#port.postMessage(message);
|
|
2295
2752
|
}
|
|
@@ -2453,9 +2910,11 @@ var MapVariableHook = class extends StubHook {
|
|
|
2453
2910
|
else throwMapperBuilderUseError();
|
|
2454
2911
|
}
|
|
2455
2912
|
call(path, args) {
|
|
2913
|
+
args.dispose();
|
|
2456
2914
|
throwMapperBuilderUseError();
|
|
2457
2915
|
}
|
|
2458
2916
|
map(path, captures, instructions) {
|
|
2917
|
+
for (let cap of captures) cap.dispose();
|
|
2459
2918
|
throwMapperBuilderUseError();
|
|
2460
2919
|
}
|
|
2461
2920
|
pull() {
|
|
@@ -2508,9 +2967,13 @@ var MapApplicator = class {
|
|
|
2508
2967
|
getPipeReadable(exportId) {
|
|
2509
2968
|
throw new Error("A mapper function cannot use pipe readables.");
|
|
2510
2969
|
}
|
|
2970
|
+
getLimits() {
|
|
2971
|
+
return DEFAULT_LIMITS;
|
|
2972
|
+
}
|
|
2511
2973
|
};
|
|
2512
2974
|
function applyMapToElement(input, parent, owner, captures, instructions) {
|
|
2513
|
-
let
|
|
2975
|
+
let inputHook = new PayloadStubHook(RpcPayload.deepCopyFrom(input, parent, owner));
|
|
2976
|
+
let mapper = new MapApplicator(captures, inputHook);
|
|
2514
2977
|
try {
|
|
2515
2978
|
return mapper.apply(instructions);
|
|
2516
2979
|
} finally {
|
|
@@ -2543,9 +3006,10 @@ mapImpl.applyMap = (input, parent, owner, captures, instructions) => {
|
|
|
2543
3006
|
var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
|
|
2544
3007
|
state;
|
|
2545
3008
|
static create(stream) {
|
|
3009
|
+
let writer = stream.getWriter();
|
|
2546
3010
|
return new WritableStreamStubHook({
|
|
2547
3011
|
refcount: 1,
|
|
2548
|
-
writer
|
|
3012
|
+
writer,
|
|
2549
3013
|
closed: false
|
|
2550
3014
|
});
|
|
2551
3015
|
}
|
|
@@ -2563,14 +3027,12 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
|
|
|
2563
3027
|
let state = this.getState();
|
|
2564
3028
|
if (path.length !== 1 || typeof path[0] !== "string") throw new Error("WritableStream stub only supports direct method calls");
|
|
2565
3029
|
const method = path[0];
|
|
2566
|
-
if (method !== "write" && method !== "close" && method !== "abort") {
|
|
2567
|
-
args.dispose();
|
|
2568
|
-
throw new Error(`Unknown WritableStream method: ${method}`);
|
|
2569
|
-
}
|
|
3030
|
+
if (method !== "write" && method !== "close" && method !== "abort") throw new Error(`Unknown WritableStream method: ${method}`);
|
|
2570
3031
|
if (method === "close" || method === "abort") state.closed = true;
|
|
2571
|
-
let
|
|
2572
|
-
return new PromiseStubHook(
|
|
3032
|
+
let promise = method === "write" ? args.deliverStreamWrite(state.writer) : args.deliverCall(state.writer[method], state.writer);
|
|
3033
|
+
return new PromiseStubHook(promise.then((payload) => new PayloadStubHook(payload)));
|
|
2573
3034
|
} catch (err) {
|
|
3035
|
+
args.dispose();
|
|
2574
3036
|
return new ErrorStubHook(err);
|
|
2575
3037
|
}
|
|
2576
3038
|
}
|
|
@@ -2582,7 +3044,8 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
|
|
|
2582
3044
|
return new ErrorStubHook(/* @__PURE__ */ new Error("Cannot access properties on a WritableStream stub"));
|
|
2583
3045
|
}
|
|
2584
3046
|
dup() {
|
|
2585
|
-
|
|
3047
|
+
let state = this.getState();
|
|
3048
|
+
return new WritableStreamStubHook(state, this);
|
|
2586
3049
|
}
|
|
2587
3050
|
pull() {
|
|
2588
3051
|
return Promise.reject(/* @__PURE__ */ new Error("Cannot pull a WritableStream stub"));
|
|
@@ -2600,9 +3063,9 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
|
|
|
2600
3063
|
}
|
|
2601
3064
|
onBroken(callback) {}
|
|
2602
3065
|
};
|
|
2603
|
-
const INITIAL_WINDOW =
|
|
2604
|
-
const MAX_WINDOW =
|
|
2605
|
-
const MIN_WINDOW =
|
|
3066
|
+
const INITIAL_WINDOW = 262144;
|
|
3067
|
+
const MAX_WINDOW = 1073741824;
|
|
3068
|
+
const MIN_WINDOW = 65536;
|
|
2606
3069
|
const STARTUP_GROWTH_FACTOR = 2;
|
|
2607
3070
|
const STEADY_GROWTH_FACTOR = 1.25;
|
|
2608
3071
|
const DECAY_FACTOR = .9;
|
|
@@ -2846,20 +3309,23 @@ let newMessagePortRpcSession = newMessagePortRpcSession$1;
|
|
|
2846
3309
|
* credentials as parameters and returns the authorized API), then cross-origin requests should
|
|
2847
3310
|
* be safe.
|
|
2848
3311
|
*/
|
|
2849
|
-
async function newWorkersRpcResponse(request, localMain) {
|
|
3312
|
+
async function newWorkersRpcResponse(request, localMain, options) {
|
|
2850
3313
|
if (request.method === "POST") {
|
|
2851
|
-
let response = await newHttpBatchRpcResponse(request, localMain);
|
|
3314
|
+
let response = await newHttpBatchRpcResponse(request, localMain, options);
|
|
2852
3315
|
response.headers.set("Access-Control-Allow-Origin", "*");
|
|
2853
3316
|
return response;
|
|
2854
|
-
} else if (request.headers.get("Upgrade")?.toLowerCase() === "websocket") return newWorkersWebSocketRpcResponse(request, localMain);
|
|
3317
|
+
} else if (request.headers.get("Upgrade")?.toLowerCase() === "websocket") return newWorkersWebSocketRpcResponse(request, localMain, options);
|
|
2855
3318
|
else return new Response("This endpoint only accepts POST or WebSocket requests.", { status: 400 });
|
|
2856
3319
|
}
|
|
2857
3320
|
|
|
2858
3321
|
//#endregion
|
|
3322
|
+
exports.DEFAULT_LIMITS = DEFAULT_LIMITS;
|
|
3323
|
+
exports.DEFAULT_MAX_DEPTH = DEFAULT_MAX_DEPTH;
|
|
2859
3324
|
exports.RpcPromise = RpcPromise;
|
|
2860
3325
|
exports.RpcSession = RpcSession;
|
|
2861
3326
|
exports.RpcStub = RpcStub;
|
|
2862
3327
|
exports.RpcTarget = RpcTarget;
|
|
3328
|
+
exports.WebSocketTransport = WebSocketTransport;
|
|
2863
3329
|
exports.deserialize = deserialize;
|
|
2864
3330
|
exports.newHttpBatchRpcResponse = newHttpBatchRpcResponse;
|
|
2865
3331
|
exports.newHttpBatchRpcSession = newHttpBatchRpcSession;
|