capnweb 0.0.0-952a541 → 0.0.0-9751a4e
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 +67 -2
- package/dist/index-bun.cjs +475 -109
- package/dist/index-bun.cjs.map +1 -1
- package/dist/index-bun.d.cts +158 -19
- package/dist/index-bun.d.ts +158 -19
- package/dist/index-bun.js +473 -110
- package/dist/index-bun.js.map +1 -1
- package/dist/index-workers.cjs +475 -109
- package/dist/index-workers.cjs.map +1 -1
- package/dist/index-workers.d.cts +154 -18
- package/dist/index-workers.d.ts +154 -18
- package/dist/index-workers.js +473 -110
- package/dist/index-workers.js.map +1 -1
- package/dist/index.cjs +475 -109
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +154 -18
- package/dist/index.d.ts +154 -18
- package/dist/index.js +473 -110
- package/dist/index.js.map +1 -1
- package/package.json +6 -5
package/dist/index-workers.js
CHANGED
|
@@ -47,9 +47,22 @@ function typeForRpc(value) {
|
|
|
47
47
|
case Array.prototype: return "array";
|
|
48
48
|
case Date.prototype: return "date";
|
|
49
49
|
case Uint8Array.prototype:
|
|
50
|
-
case BUFFER_PROTOTYPE:
|
|
50
|
+
case BUFFER_PROTOTYPE:
|
|
51
|
+
case ArrayBuffer.prototype:
|
|
52
|
+
case DataView.prototype:
|
|
53
|
+
case Int8Array.prototype:
|
|
54
|
+
case Uint8ClampedArray.prototype:
|
|
55
|
+
case Int16Array.prototype:
|
|
56
|
+
case Uint16Array.prototype:
|
|
57
|
+
case Int32Array.prototype:
|
|
58
|
+
case Uint32Array.prototype:
|
|
59
|
+
case BigInt64Array.prototype:
|
|
60
|
+
case BigUint64Array.prototype:
|
|
61
|
+
case Float32Array.prototype:
|
|
62
|
+
case Float64Array.prototype: return "bytes";
|
|
51
63
|
case WritableStream.prototype: return "writable";
|
|
52
64
|
case ReadableStream.prototype: return "readable";
|
|
65
|
+
case URL.prototype: return "url";
|
|
53
66
|
case Headers.prototype: return "headers";
|
|
54
67
|
case Request.prototype: return "request";
|
|
55
68
|
case Response.prototype: return "response";
|
|
@@ -83,7 +96,14 @@ let streamImpl = {
|
|
|
83
96
|
};
|
|
84
97
|
var StubHook = class {
|
|
85
98
|
stream(path, args) {
|
|
86
|
-
let
|
|
99
|
+
let hook = this.call(path, args);
|
|
100
|
+
let pulled;
|
|
101
|
+
try {
|
|
102
|
+
pulled = hook.pull();
|
|
103
|
+
} catch (err) {
|
|
104
|
+
hook.dispose();
|
|
105
|
+
throw err;
|
|
106
|
+
}
|
|
87
107
|
let promise;
|
|
88
108
|
if (pulled instanceof Promise) promise = pulled.then((p) => {
|
|
89
109
|
p.dispose();
|
|
@@ -102,9 +122,11 @@ var ErrorStubHook = class extends StubHook {
|
|
|
102
122
|
this.error = error;
|
|
103
123
|
}
|
|
104
124
|
call(path, args) {
|
|
125
|
+
args.dispose();
|
|
105
126
|
return this;
|
|
106
127
|
}
|
|
107
128
|
map(path, captures, instructions) {
|
|
129
|
+
for (let cap of captures) cap.dispose();
|
|
108
130
|
return this;
|
|
109
131
|
}
|
|
110
132
|
get(path) {
|
|
@@ -228,7 +250,25 @@ var RpcStub$1 = class RpcStub$1 extends RpcTarget$1 {
|
|
|
228
250
|
};
|
|
229
251
|
var RpcPromise$1 = class extends RpcStub$1 {
|
|
230
252
|
constructor(hook, pathIfPromise) {
|
|
231
|
-
super(hook, pathIfPromise);
|
|
253
|
+
if (hook instanceof StubHook) super(hook, pathIfPromise ?? []);
|
|
254
|
+
else {
|
|
255
|
+
if (pathIfPromise !== void 0) throw new TypeError("RpcPromise constructor expected one argument, received two.");
|
|
256
|
+
let kind = typeForRpc(hook);
|
|
257
|
+
if (kind === "rpc-promise") {
|
|
258
|
+
let raw = unwrapStubAndPath(hook);
|
|
259
|
+
if (raw.pathIfPromise.length > 0) super(raw.hook, raw.pathIfPromise);
|
|
260
|
+
else {
|
|
261
|
+
let adopted = raw.hook;
|
|
262
|
+
raw.hook = DISPOSED_HOOK;
|
|
263
|
+
super(adopted, []);
|
|
264
|
+
}
|
|
265
|
+
} else if (kind === "rpc-thenable") super(TargetStubHook.create(hook, void 0), []);
|
|
266
|
+
else {
|
|
267
|
+
let promiseHook = new PromiseStubHook(Promise.resolve(hook).then((value) => new PayloadStubHook(RpcPayload.fromAppReturn(value))));
|
|
268
|
+
promiseHook.ignoreUnhandledRejections();
|
|
269
|
+
super(promiseHook, []);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
232
272
|
}
|
|
233
273
|
then(onfulfilled, onrejected) {
|
|
234
274
|
return pullPromise(this).then(...arguments);
|
|
@@ -384,6 +424,7 @@ var RpcPayload = class RpcPayload {
|
|
|
384
424
|
case "date":
|
|
385
425
|
case "bytes":
|
|
386
426
|
case "blob":
|
|
427
|
+
case "url":
|
|
387
428
|
case "error":
|
|
388
429
|
case "undefined": return value;
|
|
389
430
|
case "array": {
|
|
@@ -517,6 +558,30 @@ var RpcPayload = class RpcPayload {
|
|
|
517
558
|
this.dispose();
|
|
518
559
|
}
|
|
519
560
|
}
|
|
561
|
+
async deliverStreamWrite(writer) {
|
|
562
|
+
try {
|
|
563
|
+
let promises = [];
|
|
564
|
+
this.deliverTo(this, "value", promises);
|
|
565
|
+
if (promises.length > 0) await Promise.all(promises);
|
|
566
|
+
let chunk = this.value[0];
|
|
567
|
+
if ((this.hooks.length > 0 || this.promises.length > 0) && chunk instanceof Object) {
|
|
568
|
+
if (!(Symbol.dispose in chunk)) Object.defineProperty(chunk, Symbol.dispose, {
|
|
569
|
+
value: () => this.dispose(),
|
|
570
|
+
writable: true,
|
|
571
|
+
enumerable: false,
|
|
572
|
+
configurable: true
|
|
573
|
+
});
|
|
574
|
+
await writer.write(chunk);
|
|
575
|
+
return RpcPayload.fromAppReturn(void 0);
|
|
576
|
+
}
|
|
577
|
+
await writer.write(chunk);
|
|
578
|
+
this.dispose();
|
|
579
|
+
return RpcPayload.fromAppReturn(void 0);
|
|
580
|
+
} catch (err) {
|
|
581
|
+
this.dispose();
|
|
582
|
+
throw err;
|
|
583
|
+
}
|
|
584
|
+
}
|
|
520
585
|
async deliverResolve() {
|
|
521
586
|
try {
|
|
522
587
|
let promises = [];
|
|
@@ -557,6 +622,7 @@ var RpcPayload = class RpcPayload {
|
|
|
557
622
|
case "bytes":
|
|
558
623
|
case "blob":
|
|
559
624
|
case "date":
|
|
625
|
+
case "url":
|
|
560
626
|
case "error":
|
|
561
627
|
case "undefined": return;
|
|
562
628
|
case "array": {
|
|
@@ -639,6 +705,7 @@ var RpcPayload = class RpcPayload {
|
|
|
639
705
|
case "rpc-target":
|
|
640
706
|
case "writable":
|
|
641
707
|
case "readable":
|
|
708
|
+
case "url":
|
|
642
709
|
case "headers":
|
|
643
710
|
case "request":
|
|
644
711
|
case "response": return;
|
|
@@ -708,6 +775,7 @@ function followPath(value, parent, path, owner) {
|
|
|
708
775
|
case "blob":
|
|
709
776
|
case "date":
|
|
710
777
|
case "error":
|
|
778
|
+
case "url":
|
|
711
779
|
case "headers":
|
|
712
780
|
case "request":
|
|
713
781
|
case "response":
|
|
@@ -740,17 +808,22 @@ function followPath(value, parent, path, owner) {
|
|
|
740
808
|
}
|
|
741
809
|
var ValueStubHook = class extends StubHook {
|
|
742
810
|
call(path, args) {
|
|
811
|
+
let followResult;
|
|
743
812
|
try {
|
|
744
813
|
let { value, owner } = this.getValue();
|
|
745
|
-
|
|
746
|
-
if (followResult.hook) return followResult.hook.call(followResult.remainingPath, args);
|
|
747
|
-
if (typeof followResult.value != "function") throw new TypeError(`'${path.join(".")}' is not a function.`);
|
|
748
|
-
return new PromiseStubHook(args.deliverCall(followResult.value, followResult.parent).then((payload) => {
|
|
749
|
-
return new PayloadStubHook(payload);
|
|
750
|
-
}));
|
|
814
|
+
followResult = followPath(value, void 0, path, owner);
|
|
751
815
|
} catch (err) {
|
|
816
|
+
args.dispose();
|
|
752
817
|
return new ErrorStubHook(err);
|
|
753
818
|
}
|
|
819
|
+
if (followResult.hook) return followResult.hook.call(followResult.remainingPath, args);
|
|
820
|
+
if (typeof followResult.value != "function") {
|
|
821
|
+
args.dispose();
|
|
822
|
+
return new ErrorStubHook(/* @__PURE__ */ new TypeError(`'${path.join(".")}' is not a function.`));
|
|
823
|
+
}
|
|
824
|
+
return new PromiseStubHook(args.deliverCall(followResult.value, followResult.parent).then((payload) => {
|
|
825
|
+
return new PayloadStubHook(payload);
|
|
826
|
+
}));
|
|
754
827
|
}
|
|
755
828
|
map(path, captures, instructions) {
|
|
756
829
|
try {
|
|
@@ -771,7 +844,10 @@ var ValueStubHook = class extends StubHook {
|
|
|
771
844
|
get(path) {
|
|
772
845
|
try {
|
|
773
846
|
let { value, owner } = this.getValue();
|
|
774
|
-
if (path.length === 0 && owner === null)
|
|
847
|
+
if (path.length === 0 && owner === null) {
|
|
848
|
+
if (value instanceof Object && "then" in value) return this.dup();
|
|
849
|
+
throw new Error("Can't dup an RpcTarget stub as a promise.");
|
|
850
|
+
}
|
|
775
851
|
let followResult = followPath(value, void 0, path, owner);
|
|
776
852
|
if (followResult.hook) return followResult.hook.get(followResult.remainingPath);
|
|
777
853
|
return new PayloadStubHook(RpcPayload.deepCopyFrom(followResult.value, followResult.parent, followResult.owner));
|
|
@@ -874,7 +950,10 @@ var TargetStubHook = class TargetStubHook extends ValueStubHook {
|
|
|
874
950
|
this.target = void 0;
|
|
875
951
|
}
|
|
876
952
|
}
|
|
877
|
-
onBroken(callback) {
|
|
953
|
+
onBroken(callback) {
|
|
954
|
+
let target = this.target;
|
|
955
|
+
if (target && "then" in target) Promise.resolve(target).then(() => {}, callback);
|
|
956
|
+
}
|
|
878
957
|
};
|
|
879
958
|
var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
880
959
|
promise;
|
|
@@ -888,12 +967,16 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
|
888
967
|
}
|
|
889
968
|
call(path, args) {
|
|
890
969
|
args.ensureDeepCopied();
|
|
891
|
-
return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args))
|
|
970
|
+
return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args), (err) => {
|
|
971
|
+
args.dispose();
|
|
972
|
+
throw err;
|
|
973
|
+
}));
|
|
892
974
|
}
|
|
893
975
|
stream(path, args) {
|
|
894
976
|
args.ensureDeepCopied();
|
|
895
|
-
return { promise: this.promise.then((hook) => {
|
|
896
|
-
|
|
977
|
+
return { promise: this.promise.then((hook) => hook.stream(path, args).promise, (err) => {
|
|
978
|
+
args.dispose();
|
|
979
|
+
throw err;
|
|
897
980
|
}) };
|
|
898
981
|
}
|
|
899
982
|
map(path, captures, instructions) {
|
|
@@ -920,10 +1003,7 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
|
920
1003
|
}, (err) => {});
|
|
921
1004
|
}
|
|
922
1005
|
dispose() {
|
|
923
|
-
|
|
924
|
-
else this.promise.then((hook) => {
|
|
925
|
-
hook.dispose();
|
|
926
|
-
}, (err) => {});
|
|
1006
|
+
this.promise.then((hook) => hook.dispose(), () => {});
|
|
927
1007
|
}
|
|
928
1008
|
onBroken(callback) {
|
|
929
1009
|
if (this.resolution) this.resolution.onBroken(callback);
|
|
@@ -935,6 +1015,77 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
|
935
1015
|
|
|
936
1016
|
//#endregion
|
|
937
1017
|
//#region src/serialize.ts
|
|
1018
|
+
const DEFAULT_MAX_DEPTH = 256;
|
|
1019
|
+
const DEFAULT_LIMITS = {
|
|
1020
|
+
maxBigIntDigits: 16384,
|
|
1021
|
+
maxDepth: 256,
|
|
1022
|
+
maxMessageSize: 32 * 1024 * 1024
|
|
1023
|
+
};
|
|
1024
|
+
const NATIVE_LITTLE_ENDIAN = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
|
|
1025
|
+
const BYTE_CONTAINER_TYPE_NAMES = [
|
|
1026
|
+
"ArrayBuffer",
|
|
1027
|
+
"DataView",
|
|
1028
|
+
"Int8Array",
|
|
1029
|
+
"Uint8Array",
|
|
1030
|
+
"Uint8ClampedArray",
|
|
1031
|
+
"Int16Array",
|
|
1032
|
+
"Uint16Array",
|
|
1033
|
+
"Int32Array",
|
|
1034
|
+
"Uint32Array",
|
|
1035
|
+
"BigInt64Array",
|
|
1036
|
+
"BigUint64Array",
|
|
1037
|
+
"Float32Array",
|
|
1038
|
+
"Float64Array"
|
|
1039
|
+
];
|
|
1040
|
+
function isValidByteContainerName(value) {
|
|
1041
|
+
return BYTE_CONTAINER_TYPE_NAMES.includes(value);
|
|
1042
|
+
}
|
|
1043
|
+
const TYPED_ARRAY_ELEMENT_SIZE = {
|
|
1044
|
+
ArrayBuffer: void 0,
|
|
1045
|
+
DataView: void 0,
|
|
1046
|
+
Int8Array: void 0,
|
|
1047
|
+
Uint8Array: void 0,
|
|
1048
|
+
Uint8ClampedArray: void 0,
|
|
1049
|
+
Int16Array: 2,
|
|
1050
|
+
Uint16Array: 2,
|
|
1051
|
+
Int32Array: 4,
|
|
1052
|
+
Uint32Array: 4,
|
|
1053
|
+
BigInt64Array: 8,
|
|
1054
|
+
BigUint64Array: 8,
|
|
1055
|
+
Float32Array: 4,
|
|
1056
|
+
Float64Array: 8
|
|
1057
|
+
};
|
|
1058
|
+
const BYTE_CONTAINER_PROTOTYPES = {
|
|
1059
|
+
ArrayBuffer: ArrayBuffer.prototype,
|
|
1060
|
+
DataView: DataView.prototype,
|
|
1061
|
+
Int8Array: Int8Array.prototype,
|
|
1062
|
+
Uint8ClampedArray: Uint8ClampedArray.prototype,
|
|
1063
|
+
Int16Array: Int16Array.prototype,
|
|
1064
|
+
Uint16Array: Uint16Array.prototype,
|
|
1065
|
+
Int32Array: Int32Array.prototype,
|
|
1066
|
+
Uint32Array: Uint32Array.prototype,
|
|
1067
|
+
BigInt64Array: BigInt64Array.prototype,
|
|
1068
|
+
BigUint64Array: BigUint64Array.prototype,
|
|
1069
|
+
Float32Array: Float32Array.prototype,
|
|
1070
|
+
Float64Array: Float64Array.prototype
|
|
1071
|
+
};
|
|
1072
|
+
const BYTE_CONTAINER_TYPE_BY_PROTOTYPE = /* @__PURE__ */ new Map();
|
|
1073
|
+
for (let type of Object.keys(BYTE_CONTAINER_PROTOTYPES)) BYTE_CONTAINER_TYPE_BY_PROTOTYPE.set(BYTE_CONTAINER_PROTOTYPES[type], type);
|
|
1074
|
+
function swapByteOrder(bytes, elementSize) {
|
|
1075
|
+
if (elementSize !== 2 && elementSize !== 4 && elementSize !== 8) throw new RangeError(`Unsupported element size: ${elementSize}`);
|
|
1076
|
+
let view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
1077
|
+
for (let offset = 0; offset < bytes.byteLength; offset += elementSize) switch (elementSize) {
|
|
1078
|
+
case 2:
|
|
1079
|
+
view.setUint16(offset, view.getUint16(offset, false), true);
|
|
1080
|
+
break;
|
|
1081
|
+
case 4:
|
|
1082
|
+
view.setUint32(offset, view.getUint32(offset, false), true);
|
|
1083
|
+
break;
|
|
1084
|
+
case 8:
|
|
1085
|
+
view.setBigUint64(offset, view.getBigUint64(offset, false), true);
|
|
1086
|
+
break;
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
938
1089
|
var NullExporter = class {
|
|
939
1090
|
exportStub(stub) {
|
|
940
1091
|
throw new Error("Cannot serialize RPC stubs without an RPC session.");
|
|
@@ -955,6 +1106,7 @@ async function streamToBlob(stream, type) {
|
|
|
955
1106
|
return b.type === type ? b : b.slice(0, b.size, type);
|
|
956
1107
|
}
|
|
957
1108
|
const ERROR_TYPES = {
|
|
1109
|
+
__proto__: null,
|
|
958
1110
|
Error,
|
|
959
1111
|
EvalError,
|
|
960
1112
|
RangeError,
|
|
@@ -967,12 +1119,14 @@ const ERROR_TYPES = {
|
|
|
967
1119
|
var Devaluator = class Devaluator {
|
|
968
1120
|
exporter;
|
|
969
1121
|
source;
|
|
970
|
-
|
|
1122
|
+
encodingLevel;
|
|
1123
|
+
constructor(exporter, source, encodingLevel) {
|
|
971
1124
|
this.exporter = exporter;
|
|
972
1125
|
this.source = source;
|
|
1126
|
+
this.encodingLevel = encodingLevel;
|
|
973
1127
|
}
|
|
974
|
-
static devaluate(value, parent, exporter = NULL_EXPORTER, source) {
|
|
975
|
-
let devaluator = new Devaluator(exporter, source);
|
|
1128
|
+
static devaluate(value, parent, exporter = NULL_EXPORTER, source, encodingLevel = "string") {
|
|
1129
|
+
let devaluator = new Devaluator(exporter, source, encodingLevel);
|
|
976
1130
|
try {
|
|
977
1131
|
return devaluator.devaluateImpl(value, parent, 0);
|
|
978
1132
|
} catch (err) {
|
|
@@ -984,7 +1138,7 @@ var Devaluator = class Devaluator {
|
|
|
984
1138
|
}
|
|
985
1139
|
exports;
|
|
986
1140
|
devaluateImpl(value, parent, depth) {
|
|
987
|
-
if (depth >=
|
|
1141
|
+
if (depth >= 256) throw new Error("Serialization exceeded maximum allowed depth. (Does the message contain cycles?)");
|
|
988
1142
|
switch (typeForRpc(value)) {
|
|
989
1143
|
case "unsupported": {
|
|
990
1144
|
let msg;
|
|
@@ -995,10 +1149,12 @@ var Devaluator = class Devaluator {
|
|
|
995
1149
|
}
|
|
996
1150
|
throw new TypeError(msg);
|
|
997
1151
|
}
|
|
998
|
-
case "primitive": if (typeof value === "number" && !isFinite(value))
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1152
|
+
case "primitive": if (typeof value === "number" && !isFinite(value)) {
|
|
1153
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1154
|
+
if (value === Infinity) return ["inf"];
|
|
1155
|
+
else if (value === -Infinity) return ["-inf"];
|
|
1156
|
+
else return ["nan"];
|
|
1157
|
+
} else return value;
|
|
1002
1158
|
case "object": {
|
|
1003
1159
|
let object = value;
|
|
1004
1160
|
let result = {};
|
|
@@ -1012,23 +1168,49 @@ var Devaluator = class Devaluator {
|
|
|
1012
1168
|
for (let i = 0; i < len; i++) result[i] = this.devaluateImpl(array[i], array, depth + 1);
|
|
1013
1169
|
return [result];
|
|
1014
1170
|
}
|
|
1015
|
-
case "bigint":
|
|
1171
|
+
case "bigint":
|
|
1172
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1173
|
+
return ["bigint", value.toString()];
|
|
1016
1174
|
case "date": {
|
|
1175
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1017
1176
|
const time = value.getTime();
|
|
1018
1177
|
return ["date", Number.isNaN(time) ? null : time];
|
|
1019
1178
|
}
|
|
1020
1179
|
case "bytes": {
|
|
1021
|
-
let
|
|
1022
|
-
|
|
1180
|
+
let alternateTypeName = BYTE_CONTAINER_TYPE_BY_PROTOTYPE.get(Object.getPrototypeOf(value));
|
|
1181
|
+
let bytes;
|
|
1182
|
+
if (alternateTypeName === "ArrayBuffer") bytes = new Uint8Array(value);
|
|
1183
|
+
else if (alternateTypeName === void 0) bytes = value;
|
|
1184
|
+
else {
|
|
1185
|
+
let view = value;
|
|
1186
|
+
bytes = new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
|
|
1187
|
+
let elementSize = TYPED_ARRAY_ELEMENT_SIZE[alternateTypeName];
|
|
1188
|
+
if (!NATIVE_LITTLE_ENDIAN && elementSize) {
|
|
1189
|
+
bytes = bytes.slice();
|
|
1190
|
+
swapByteOrder(bytes, elementSize);
|
|
1191
|
+
}
|
|
1192
|
+
}
|
|
1193
|
+
if (this.encodingLevel === "structuredClonable" || this.encodingLevel === "jsonCompatibleWithBytes") return alternateTypeName === void 0 ? ["bytes", bytes] : [
|
|
1194
|
+
"bytes",
|
|
1195
|
+
bytes,
|
|
1196
|
+
alternateTypeName
|
|
1197
|
+
];
|
|
1023
1198
|
let b64;
|
|
1024
|
-
if (
|
|
1199
|
+
if (bytes.toBase64) b64 = bytes.toBase64({ omitPadding: true });
|
|
1200
|
+
else if (typeof Buffer !== "undefined") b64 = (bytes instanceof Buffer ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)).toString("base64");
|
|
1025
1201
|
else {
|
|
1026
1202
|
let binary = "";
|
|
1027
1203
|
for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]);
|
|
1028
1204
|
b64 = btoa(binary);
|
|
1029
1205
|
}
|
|
1030
|
-
|
|
1206
|
+
b64 = b64.replace(/=+$/, "");
|
|
1207
|
+
return alternateTypeName === void 0 ? ["bytes", b64] : [
|
|
1208
|
+
"bytes",
|
|
1209
|
+
b64,
|
|
1210
|
+
alternateTypeName
|
|
1211
|
+
];
|
|
1031
1212
|
}
|
|
1213
|
+
case "url": return ["url", value.href];
|
|
1032
1214
|
case "headers": return ["headers", [...value]];
|
|
1033
1215
|
case "request": {
|
|
1034
1216
|
let req = value;
|
|
@@ -1143,7 +1325,9 @@ var Devaluator = class Devaluator {
|
|
|
1143
1325
|
} else if (rewritten && rewritten.stack) result.push(rewritten.stack);
|
|
1144
1326
|
return result;
|
|
1145
1327
|
}
|
|
1146
|
-
case "undefined":
|
|
1328
|
+
case "undefined":
|
|
1329
|
+
if (this.encodingLevel === "structuredClonable") return;
|
|
1330
|
+
return ["undefined"];
|
|
1147
1331
|
case "stub":
|
|
1148
1332
|
case "rpc-promise": {
|
|
1149
1333
|
if (!this.source) throw new Error("Can't serialize RPC stubs in this context.");
|
|
@@ -1210,6 +1394,9 @@ var NullImporter = class {
|
|
|
1210
1394
|
getPipeReadable(exportId) {
|
|
1211
1395
|
throw new Error("Cannot retrieve pipe readable without an RPC session.");
|
|
1212
1396
|
}
|
|
1397
|
+
getLimits() {
|
|
1398
|
+
return DEFAULT_LIMITS;
|
|
1399
|
+
}
|
|
1213
1400
|
};
|
|
1214
1401
|
const NULL_IMPORTER = new NullImporter();
|
|
1215
1402
|
function fixBrokenRequestBody(request, body) {
|
|
@@ -1226,15 +1413,22 @@ function streamToBlobPromise(stream, type) {
|
|
|
1226
1413
|
}
|
|
1227
1414
|
var Evaluator = class Evaluator {
|
|
1228
1415
|
importer;
|
|
1229
|
-
|
|
1416
|
+
encodingLevel;
|
|
1417
|
+
limits;
|
|
1418
|
+
constructor(importer, encodingLevel = "string") {
|
|
1230
1419
|
this.importer = importer;
|
|
1420
|
+
this.encodingLevel = encodingLevel;
|
|
1421
|
+
this.limits = importer.getLimits();
|
|
1231
1422
|
}
|
|
1232
1423
|
hooks = [];
|
|
1233
1424
|
promises = [];
|
|
1234
1425
|
evaluate(value) {
|
|
1426
|
+
return this.evaluateWithDepth(value, 0);
|
|
1427
|
+
}
|
|
1428
|
+
evaluateWithDepth(value, depth) {
|
|
1235
1429
|
let payload = RpcPayload.forEvaluate(this.hooks, this.promises);
|
|
1236
1430
|
try {
|
|
1237
|
-
payload.value = this.evaluateImpl(value, payload, "value");
|
|
1431
|
+
payload.value = this.evaluateImpl(value, payload, "value", depth);
|
|
1238
1432
|
return payload;
|
|
1239
1433
|
} catch (err) {
|
|
1240
1434
|
payload.dispose();
|
|
@@ -1244,31 +1438,70 @@ var Evaluator = class Evaluator {
|
|
|
1244
1438
|
evaluateCopy(value) {
|
|
1245
1439
|
return this.evaluate(structuredClone(value));
|
|
1246
1440
|
}
|
|
1247
|
-
evaluateImpl(value, parent, property) {
|
|
1441
|
+
evaluateImpl(value, parent, property, depth) {
|
|
1442
|
+
let maxDepth = this.limits.maxDepth;
|
|
1443
|
+
if (depth >= maxDepth) throw new TypeError(`Deserialization exceeded maximum allowed message depth of ${maxDepth}.`);
|
|
1444
|
+
if (this.encodingLevel === "structuredClonable") {
|
|
1445
|
+
if (value instanceof Date || typeof value === "bigint") return value;
|
|
1446
|
+
}
|
|
1248
1447
|
if (value instanceof Array) {
|
|
1249
1448
|
if (value.length == 1 && value[0] instanceof Array) {
|
|
1250
1449
|
let result = value[0];
|
|
1251
|
-
for (let i = 0; i < result.length; i++) result[i] = this.evaluateImpl(result[i], result, i);
|
|
1450
|
+
for (let i = 0; i < result.length; i++) result[i] = this.evaluateImpl(result[i], result, i, depth + 1);
|
|
1252
1451
|
return result;
|
|
1253
1452
|
} else switch (value[0]) {
|
|
1254
1453
|
case "bigint":
|
|
1255
|
-
if (typeof value[1] == "string")
|
|
1454
|
+
if (typeof value[1] == "string") {
|
|
1455
|
+
let digits = value[1];
|
|
1456
|
+
let maxBigIntDigits = this.limits.maxBigIntDigits;
|
|
1457
|
+
if (digits.length > maxBigIntDigits) throw new TypeError(`Deserialized bigint exceeds maximum length of ${maxBigIntDigits} digits.`);
|
|
1458
|
+
return BigInt(digits);
|
|
1459
|
+
}
|
|
1256
1460
|
break;
|
|
1257
1461
|
case "date":
|
|
1258
1462
|
if (value[1] === null) return /* @__PURE__ */ new Date(NaN);
|
|
1259
1463
|
if (typeof value[1] == "number") return new Date(value[1]);
|
|
1260
1464
|
break;
|
|
1261
|
-
case "bytes":
|
|
1262
|
-
|
|
1263
|
-
|
|
1465
|
+
case "bytes": {
|
|
1466
|
+
let bytes;
|
|
1467
|
+
if (value[1] instanceof Uint8Array) bytes = value[1];
|
|
1468
|
+
else if (typeof value[1] == "string") if (typeof Buffer !== "undefined") bytes = Buffer.from(value[1], "base64");
|
|
1469
|
+
else if (Uint8Array.fromBase64) bytes = Uint8Array.fromBase64(value[1]);
|
|
1264
1470
|
else {
|
|
1265
1471
|
let bs = atob(value[1]);
|
|
1266
1472
|
let len = bs.length;
|
|
1267
|
-
|
|
1473
|
+
bytes = new Uint8Array(len);
|
|
1268
1474
|
for (let i = 0; i < len; i++) bytes[i] = bs.charCodeAt(i);
|
|
1269
|
-
return bytes;
|
|
1270
1475
|
}
|
|
1271
|
-
break;
|
|
1476
|
+
else break;
|
|
1477
|
+
if (value.length === 2) return bytes;
|
|
1478
|
+
if (typeof value[2] !== "string") throw new TypeError(`Unknown bytes type marker type: ${typeof value[2]}`);
|
|
1479
|
+
if (!isValidByteContainerName(value[2])) {
|
|
1480
|
+
let marker = value[2].slice(0, 64);
|
|
1481
|
+
throw new TypeError(`Unknown bytes type marker: ${marker}`);
|
|
1482
|
+
}
|
|
1483
|
+
let marker = value[2];
|
|
1484
|
+
let elementSize = TYPED_ARRAY_ELEMENT_SIZE[marker];
|
|
1485
|
+
if (elementSize !== void 0 && bytes.byteLength % elementSize !== 0) throw new TypeError(`Invalid byte length ${bytes.byteLength} for ${marker}; expected a multiple of ${elementSize}`);
|
|
1486
|
+
let buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
|
1487
|
+
if (!NATIVE_LITTLE_ENDIAN && elementSize !== void 0) swapByteOrder(new Uint8Array(buffer), elementSize);
|
|
1488
|
+
switch (marker) {
|
|
1489
|
+
case "ArrayBuffer": return buffer;
|
|
1490
|
+
case "DataView": return new DataView(buffer);
|
|
1491
|
+
case "Int8Array": return new Int8Array(buffer);
|
|
1492
|
+
case "Uint8Array": return new Uint8Array(buffer);
|
|
1493
|
+
case "Uint8ClampedArray": return new Uint8ClampedArray(buffer);
|
|
1494
|
+
case "Int16Array": return new Int16Array(buffer);
|
|
1495
|
+
case "Uint16Array": return new Uint16Array(buffer);
|
|
1496
|
+
case "Int32Array": return new Int32Array(buffer);
|
|
1497
|
+
case "Uint32Array": return new Uint32Array(buffer);
|
|
1498
|
+
case "BigInt64Array": return new BigInt64Array(buffer);
|
|
1499
|
+
case "BigUint64Array": return new BigUint64Array(buffer);
|
|
1500
|
+
case "Float32Array": return new Float32Array(buffer);
|
|
1501
|
+
case "Float64Array": return new Float64Array(buffer);
|
|
1502
|
+
default:
|
|
1503
|
+
}
|
|
1504
|
+
}
|
|
1272
1505
|
case "error":
|
|
1273
1506
|
if (value.length >= 3 && typeof value[1] === "string" && typeof value[2] === "string") {
|
|
1274
1507
|
let cls = ERROR_TYPES[value[1]] || Error;
|
|
@@ -1281,7 +1514,11 @@ var Evaluator = class Evaluator {
|
|
|
1281
1514
|
let propsObj = props;
|
|
1282
1515
|
for (let key of Object.keys(propsObj)) {
|
|
1283
1516
|
if (key === "name" || key === "message" || key === "stack") continue;
|
|
1284
|
-
|
|
1517
|
+
if (key in Object.prototype || key === "toJSON") {
|
|
1518
|
+
this.evaluateImpl(propsObj[key], result, key, depth + 1);
|
|
1519
|
+
continue;
|
|
1520
|
+
}
|
|
1521
|
+
anyResult[key] = this.evaluateImpl(propsObj[key], result, key, depth + 1);
|
|
1285
1522
|
}
|
|
1286
1523
|
}
|
|
1287
1524
|
return result;
|
|
@@ -1293,6 +1530,9 @@ var Evaluator = class Evaluator {
|
|
|
1293
1530
|
case "inf": return Infinity;
|
|
1294
1531
|
case "-inf": return -Infinity;
|
|
1295
1532
|
case "nan": return NaN;
|
|
1533
|
+
case "url":
|
|
1534
|
+
if (value.length === 2 && typeof value[1] === "string") return new URL(value[1]);
|
|
1535
|
+
break;
|
|
1296
1536
|
case "headers":
|
|
1297
1537
|
if (value.length === 2 && value[1] instanceof Array) return new Headers(value[1]);
|
|
1298
1538
|
break;
|
|
@@ -1302,11 +1542,11 @@ var Evaluator = class Evaluator {
|
|
|
1302
1542
|
let init = value[2];
|
|
1303
1543
|
if (typeof init !== "object" || init === null) break;
|
|
1304
1544
|
if (init.body) {
|
|
1305
|
-
init.body = this.evaluateImpl(init.body, init, "body");
|
|
1545
|
+
init.body = this.evaluateImpl(init.body, init, "body", depth + 1);
|
|
1306
1546
|
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.");
|
|
1307
1547
|
}
|
|
1308
1548
|
if (init.signal) {
|
|
1309
|
-
init.signal = this.evaluateImpl(init.signal, init, "signal");
|
|
1549
|
+
init.signal = this.evaluateImpl(init.signal, init, "signal", depth + 1);
|
|
1310
1550
|
if (!(init.signal instanceof AbortSignal)) throw new TypeError("Request siganl must be of type AbortSignal.");
|
|
1311
1551
|
}
|
|
1312
1552
|
if (init.headers && !(init.headers instanceof Array)) throw new TypeError("Request headers must be serialized as an array of pairs.");
|
|
@@ -1323,7 +1563,7 @@ var Evaluator = class Evaluator {
|
|
|
1323
1563
|
}
|
|
1324
1564
|
case "response": {
|
|
1325
1565
|
if (value.length !== 3) break;
|
|
1326
|
-
let body = this.evaluateImpl(value[1], parent, property);
|
|
1566
|
+
let body = this.evaluateImpl(value[1], parent, property, depth + 1);
|
|
1327
1567
|
if (body === null || typeof body === "string" || body instanceof Uint8Array || body instanceof ReadableStream) {} else throw new TypeError("Response body must be of type ReadableStream.");
|
|
1328
1568
|
let init = value[2];
|
|
1329
1569
|
if (typeof init !== "object" || init === null) break;
|
|
@@ -1334,7 +1574,7 @@ var Evaluator = class Evaluator {
|
|
|
1334
1574
|
case "blob": {
|
|
1335
1575
|
if (value.length !== 3 || typeof value[1] !== "string") break;
|
|
1336
1576
|
let contentType = value[1];
|
|
1337
|
-
let content = this.evaluateImpl(value[2], parent, property);
|
|
1577
|
+
let content = this.evaluateImpl(value[2], parent, property, depth + 1);
|
|
1338
1578
|
if (!(content instanceof ReadableStream)) throw new TypeError("Blob content must be serialized as a ReadableStream.");
|
|
1339
1579
|
let promise = streamToBlobPromise(content, contentType);
|
|
1340
1580
|
this.promises.push({
|
|
@@ -1375,7 +1615,7 @@ var Evaluator = class Evaluator {
|
|
|
1375
1615
|
if (value.length == 3) return addStub(hook.get(path));
|
|
1376
1616
|
let args = value[3];
|
|
1377
1617
|
if (!(args instanceof Array)) break;
|
|
1378
|
-
args = new Evaluator(this.importer).
|
|
1618
|
+
args = new Evaluator(this.importer).evaluateWithDepth([args], depth);
|
|
1379
1619
|
return addStub(hook.call(path, args));
|
|
1380
1620
|
}
|
|
1381
1621
|
case "remap": {
|
|
@@ -1441,9 +1681,9 @@ var Evaluator = class Evaluator {
|
|
|
1441
1681
|
} else if (value instanceof Object) {
|
|
1442
1682
|
let result = value;
|
|
1443
1683
|
for (let key in result) if (key in Object.prototype || key === "toJSON") {
|
|
1444
|
-
this.evaluateImpl(result[key], result, key);
|
|
1684
|
+
this.evaluateImpl(result[key], result, key, depth + 1);
|
|
1445
1685
|
delete result[key];
|
|
1446
|
-
} else result[key] = this.evaluateImpl(result[key], result, key);
|
|
1686
|
+
} else result[key] = this.evaluateImpl(result[key], result, key, depth + 1);
|
|
1447
1687
|
return result;
|
|
1448
1688
|
} else return value;
|
|
1449
1689
|
}
|
|
@@ -1459,6 +1699,47 @@ function deserialize(value) {
|
|
|
1459
1699
|
|
|
1460
1700
|
//#endregion
|
|
1461
1701
|
//#region src/rpc.ts
|
|
1702
|
+
const ESTIMATED_OBJECT_OVERHEAD = 16;
|
|
1703
|
+
const ESTIMATED_ENTRY_OVERHEAD = 8;
|
|
1704
|
+
const ESTIMATED_BINARY_OVERHEAD = 16;
|
|
1705
|
+
const MAX_ESTIMATE_DEPTH = 64;
|
|
1706
|
+
function estimateStringSize(value) {
|
|
1707
|
+
return 2 + value.length * 3;
|
|
1708
|
+
}
|
|
1709
|
+
function estimateEncodedSize(value, seen, depth = 0) {
|
|
1710
|
+
if (depth >= MAX_ESTIMATE_DEPTH) return ESTIMATED_ENTRY_OVERHEAD;
|
|
1711
|
+
switch (typeof value) {
|
|
1712
|
+
case "string": return estimateStringSize(value);
|
|
1713
|
+
case "number": return 16;
|
|
1714
|
+
case "bigint": return 16;
|
|
1715
|
+
case "boolean": return 8;
|
|
1716
|
+
case "undefined": return 16;
|
|
1717
|
+
case "object": {
|
|
1718
|
+
if (value === null) return 8;
|
|
1719
|
+
if (ArrayBuffer.isView(value)) return ESTIMATED_BINARY_OVERHEAD + value.byteLength;
|
|
1720
|
+
if (value instanceof ArrayBuffer) return ESTIMATED_BINARY_OVERHEAD + value.byteLength;
|
|
1721
|
+
if (typeof Blob !== "undefined" && value instanceof Blob) return ESTIMATED_BINARY_OVERHEAD + value.size;
|
|
1722
|
+
if (value instanceof Date) return 16;
|
|
1723
|
+
seen ??= /* @__PURE__ */ new WeakSet();
|
|
1724
|
+
if (seen.has(value)) return ESTIMATED_ENTRY_OVERHEAD;
|
|
1725
|
+
seen.add(value);
|
|
1726
|
+
if (value instanceof Array) {
|
|
1727
|
+
let size = ESTIMATED_OBJECT_OVERHEAD;
|
|
1728
|
+
for (let item of value) size += ESTIMATED_ENTRY_OVERHEAD + estimateEncodedSize(item, seen, depth + 1);
|
|
1729
|
+
return size;
|
|
1730
|
+
}
|
|
1731
|
+
if (value instanceof Error) {
|
|
1732
|
+
let size = ESTIMATED_OBJECT_OVERHEAD + estimateStringSize(value.name) + estimateStringSize(value.message) + estimateStringSize(value.stack ?? "");
|
|
1733
|
+
for (let key of Object.keys(value)) size += ESTIMATED_ENTRY_OVERHEAD + estimateStringSize(key) + estimateEncodedSize(value[key], seen, depth + 1);
|
|
1734
|
+
return size;
|
|
1735
|
+
}
|
|
1736
|
+
let size = ESTIMATED_OBJECT_OVERHEAD;
|
|
1737
|
+
for (let key of Object.keys(value)) size += ESTIMATED_ENTRY_OVERHEAD + estimateStringSize(key) + estimateEncodedSize(value[key], seen, depth + 1);
|
|
1738
|
+
return size;
|
|
1739
|
+
}
|
|
1740
|
+
default: return 16;
|
|
1741
|
+
}
|
|
1742
|
+
}
|
|
1462
1743
|
var ImportTableEntry = class {
|
|
1463
1744
|
session;
|
|
1464
1745
|
importId;
|
|
@@ -1551,24 +1832,28 @@ var RpcImportHook = class RpcImportHook extends StubHook {
|
|
|
1551
1832
|
if (this.entry) return this.entry;
|
|
1552
1833
|
else throw new Error("This RpcImportHook was already disposed.");
|
|
1553
1834
|
}
|
|
1835
|
+
getEntryTakingOwnership(disposeOwned) {
|
|
1836
|
+
try {
|
|
1837
|
+
return this.getEntry();
|
|
1838
|
+
} catch (err) {
|
|
1839
|
+
disposeOwned();
|
|
1840
|
+
throw err;
|
|
1841
|
+
}
|
|
1842
|
+
}
|
|
1554
1843
|
call(path, args) {
|
|
1555
|
-
let entry = this.
|
|
1844
|
+
let entry = this.getEntryTakingOwnership(() => args.dispose());
|
|
1556
1845
|
if (entry.resolution) return entry.resolution.call(path, args);
|
|
1557
1846
|
else return entry.session.sendCall(entry.importId, path, args);
|
|
1558
1847
|
}
|
|
1559
1848
|
stream(path, args) {
|
|
1560
|
-
let entry = this.
|
|
1849
|
+
let entry = this.getEntryTakingOwnership(() => args.dispose());
|
|
1561
1850
|
if (entry.resolution) return entry.resolution.stream(path, args);
|
|
1562
1851
|
else return entry.session.sendStream(entry.importId, path, args);
|
|
1563
1852
|
}
|
|
1564
1853
|
map(path, captures, instructions) {
|
|
1565
|
-
let entry
|
|
1566
|
-
try {
|
|
1567
|
-
entry = this.getEntry();
|
|
1568
|
-
} catch (err) {
|
|
1854
|
+
let entry = this.getEntryTakingOwnership(() => {
|
|
1569
1855
|
for (let cap of captures) cap.dispose();
|
|
1570
|
-
|
|
1571
|
-
}
|
|
1856
|
+
});
|
|
1572
1857
|
if (entry.resolution) return entry.resolution.map(path, captures, instructions);
|
|
1573
1858
|
else return entry.session.sendMap(entry.importId, path, captures, instructions);
|
|
1574
1859
|
}
|
|
@@ -1624,9 +1909,24 @@ var RpcSessionImpl = class {
|
|
|
1624
1909
|
onBatchDone;
|
|
1625
1910
|
pullCount = 0;
|
|
1626
1911
|
onBrokenCallbacks = [];
|
|
1912
|
+
encodingLevel;
|
|
1913
|
+
limits;
|
|
1627
1914
|
constructor(transport, mainHook, options) {
|
|
1628
1915
|
this.transport = transport;
|
|
1629
1916
|
this.options = options;
|
|
1917
|
+
let level = "string";
|
|
1918
|
+
if ("encodingLevel" in transport) {
|
|
1919
|
+
let raw = transport.encodingLevel;
|
|
1920
|
+
if (raw !== void 0) {
|
|
1921
|
+
if (raw !== "string" && raw !== "jsonCompatible" && raw !== "jsonCompatibleWithBytes" && raw !== "structuredClonable") throw new TypeError(`Unknown transport encodingLevel: ${String(raw)}`);
|
|
1922
|
+
level = raw;
|
|
1923
|
+
}
|
|
1924
|
+
}
|
|
1925
|
+
this.encodingLevel = level;
|
|
1926
|
+
this.limits = {
|
|
1927
|
+
...DEFAULT_LIMITS,
|
|
1928
|
+
...options.limits
|
|
1929
|
+
};
|
|
1630
1930
|
this.exports.push({
|
|
1631
1931
|
hook: mainHook,
|
|
1632
1932
|
refcount: 1
|
|
@@ -1707,7 +2007,7 @@ var RpcSessionImpl = class {
|
|
|
1707
2007
|
let autoRelease = exp.autoRelease;
|
|
1708
2008
|
++this.pullCount;
|
|
1709
2009
|
exp.pull = resolve().then((payload) => {
|
|
1710
|
-
let value = Devaluator.devaluate(payload.value, void 0, this, payload);
|
|
2010
|
+
let value = Devaluator.devaluate(payload.value, void 0, this, payload, this.encodingLevel);
|
|
1711
2011
|
this.send([
|
|
1712
2012
|
"resolve",
|
|
1713
2013
|
exportId,
|
|
@@ -1718,7 +2018,7 @@ var RpcSessionImpl = class {
|
|
|
1718
2018
|
this.send([
|
|
1719
2019
|
"reject",
|
|
1720
2020
|
exportId,
|
|
1721
|
-
Devaluator.devaluate(error, void 0, this)
|
|
2021
|
+
Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)
|
|
1722
2022
|
]);
|
|
1723
2023
|
if (autoRelease) this.releaseExport(exportId, 1);
|
|
1724
2024
|
}).catch((error) => {
|
|
@@ -1726,7 +2026,7 @@ var RpcSessionImpl = class {
|
|
|
1726
2026
|
this.send([
|
|
1727
2027
|
"reject",
|
|
1728
2028
|
exportId,
|
|
1729
|
-
Devaluator.devaluate(error, void 0, this)
|
|
2029
|
+
Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)
|
|
1730
2030
|
]);
|
|
1731
2031
|
if (autoRelease) this.releaseExport(exportId, 1);
|
|
1732
2032
|
} catch (error2) {
|
|
@@ -1769,6 +2069,9 @@ var RpcSessionImpl = class {
|
|
|
1769
2069
|
entry.pipeReadable = void 0;
|
|
1770
2070
|
return readable;
|
|
1771
2071
|
}
|
|
2072
|
+
getLimits() {
|
|
2073
|
+
return this.limits;
|
|
2074
|
+
}
|
|
1772
2075
|
createPipe(readable, readableHook) {
|
|
1773
2076
|
if (this.abortReason) throw this.abortReason;
|
|
1774
2077
|
this.send(["pipe"]);
|
|
@@ -1782,27 +2085,52 @@ var RpcSessionImpl = class {
|
|
|
1782
2085
|
}
|
|
1783
2086
|
send(msg) {
|
|
1784
2087
|
if (this.abortReason !== void 0) return 0;
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
msgText = JSON.stringify(msg);
|
|
1788
|
-
} catch (err) {
|
|
2088
|
+
if (this.encodingLevel === "string") {
|
|
2089
|
+
let msgText;
|
|
1789
2090
|
try {
|
|
1790
|
-
|
|
1791
|
-
} catch (
|
|
1792
|
-
|
|
2091
|
+
msgText = JSON.stringify(msg);
|
|
2092
|
+
} catch (err) {
|
|
2093
|
+
try {
|
|
2094
|
+
this.abort(err);
|
|
2095
|
+
} catch (err2) {}
|
|
2096
|
+
throw err;
|
|
2097
|
+
}
|
|
2098
|
+
try {
|
|
2099
|
+
let sent = this.transport.send(msgText);
|
|
2100
|
+
if (sent !== void 0 && typeof sent.catch === "function") sent.catch((err) => this.abort(err, false));
|
|
2101
|
+
} catch (err) {
|
|
2102
|
+
queueMicrotask(() => this.abort(err, false));
|
|
2103
|
+
}
|
|
2104
|
+
return msgText.length;
|
|
2105
|
+
} else try {
|
|
2106
|
+
let size = this.transport.send(msg);
|
|
2107
|
+
if (typeof size === "number") return size;
|
|
2108
|
+
let thenable = size;
|
|
2109
|
+
if (thenable && typeof thenable.then === "function") Promise.resolve(thenable).catch((err) => this.abort(err, false));
|
|
2110
|
+
return;
|
|
2111
|
+
} catch (err) {
|
|
2112
|
+
queueMicrotask(() => this.abort(err, false));
|
|
2113
|
+
return;
|
|
1793
2114
|
}
|
|
1794
|
-
this.transport.send(msgText).catch((err) => this.abort(err, false));
|
|
1795
|
-
return msgText.length;
|
|
1796
2115
|
}
|
|
1797
2116
|
sendCall(id, path, args) {
|
|
1798
|
-
if (this.abortReason)
|
|
2117
|
+
if (this.abortReason) {
|
|
2118
|
+
args?.dispose();
|
|
2119
|
+
throw this.abortReason;
|
|
2120
|
+
}
|
|
1799
2121
|
let value = [
|
|
1800
2122
|
"pipeline",
|
|
1801
2123
|
id,
|
|
1802
2124
|
path
|
|
1803
2125
|
];
|
|
1804
2126
|
if (args) {
|
|
1805
|
-
let devalue
|
|
2127
|
+
let devalue;
|
|
2128
|
+
try {
|
|
2129
|
+
devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
|
|
2130
|
+
} catch (err) {
|
|
2131
|
+
args.dispose();
|
|
2132
|
+
throw err;
|
|
2133
|
+
}
|
|
1806
2134
|
value.push(devalue[0]);
|
|
1807
2135
|
}
|
|
1808
2136
|
this.send(["push", value]);
|
|
@@ -1811,15 +2139,26 @@ var RpcSessionImpl = class {
|
|
|
1811
2139
|
return new RpcImportHook(true, entry);
|
|
1812
2140
|
}
|
|
1813
2141
|
sendStream(id, path, args) {
|
|
1814
|
-
if (this.abortReason)
|
|
2142
|
+
if (this.abortReason) {
|
|
2143
|
+
args.dispose();
|
|
2144
|
+
throw this.abortReason;
|
|
2145
|
+
}
|
|
1815
2146
|
let value = [
|
|
1816
2147
|
"pipeline",
|
|
1817
2148
|
id,
|
|
1818
2149
|
path
|
|
1819
2150
|
];
|
|
1820
|
-
let devalue
|
|
2151
|
+
let devalue;
|
|
2152
|
+
try {
|
|
2153
|
+
devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
|
|
2154
|
+
} catch (err) {
|
|
2155
|
+
args.dispose();
|
|
2156
|
+
throw err;
|
|
2157
|
+
}
|
|
1821
2158
|
value.push(devalue[0]);
|
|
1822
|
-
let
|
|
2159
|
+
let msg = ["stream", value];
|
|
2160
|
+
let size = this.send(msg);
|
|
2161
|
+
if (size === void 0) size = estimateEncodedSize(msg);
|
|
1823
2162
|
let importId = this.imports.length;
|
|
1824
2163
|
let entry = new ImportTableEntry(this, importId, true);
|
|
1825
2164
|
entry.remoteRefcount = 0;
|
|
@@ -1875,7 +2214,14 @@ var RpcSessionImpl = class {
|
|
|
1875
2214
|
this.cancelReadLoop?.(error);
|
|
1876
2215
|
this.cancelReadLoop = void 0;
|
|
1877
2216
|
if (trySendAbortMessage) try {
|
|
1878
|
-
|
|
2217
|
+
let abortMsg = ["abort", Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)];
|
|
2218
|
+
if (this.encodingLevel === "string") {
|
|
2219
|
+
let sent = this.transport.send(JSON.stringify(abortMsg));
|
|
2220
|
+
if (sent !== void 0 && typeof sent.catch === "function") sent.catch((err) => {});
|
|
2221
|
+
} else {
|
|
2222
|
+
let result = this.transport.send(abortMsg);
|
|
2223
|
+
if (result && typeof result.then === "function") Promise.resolve(result).catch((err) => {});
|
|
2224
|
+
}
|
|
1879
2225
|
} catch (err) {}
|
|
1880
2226
|
if (error === void 0) error = "undefined";
|
|
1881
2227
|
this.abortReason = error;
|
|
@@ -1897,18 +2243,19 @@ var RpcSessionImpl = class {
|
|
|
1897
2243
|
while (!this.abortReason) {
|
|
1898
2244
|
let readCanceled = Promise.withResolvers();
|
|
1899
2245
|
this.cancelReadLoop = readCanceled.reject;
|
|
1900
|
-
let
|
|
2246
|
+
let raw;
|
|
1901
2247
|
try {
|
|
1902
|
-
|
|
2248
|
+
raw = await Promise.race([this.transport.receive(), readCanceled.promise]);
|
|
1903
2249
|
} finally {
|
|
1904
2250
|
if (this.cancelReadLoop === readCanceled.reject) this.cancelReadLoop = void 0;
|
|
1905
2251
|
}
|
|
1906
|
-
|
|
2252
|
+
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.`);
|
|
1907
2253
|
if (this.abortReason) break;
|
|
2254
|
+
let msg = this.encodingLevel === "string" ? JSON.parse(raw) : raw;
|
|
1908
2255
|
if (msg instanceof Array) switch (msg[0]) {
|
|
1909
2256
|
case "push":
|
|
1910
2257
|
if (msg.length > 1) {
|
|
1911
|
-
let hook = new PayloadStubHook(new Evaluator(this).evaluate(msg[1]));
|
|
2258
|
+
let hook = new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[1]));
|
|
1912
2259
|
hook.ignoreUnhandledRejections();
|
|
1913
2260
|
this.exports.push({
|
|
1914
2261
|
hook,
|
|
@@ -1919,7 +2266,7 @@ var RpcSessionImpl = class {
|
|
|
1919
2266
|
break;
|
|
1920
2267
|
case "stream":
|
|
1921
2268
|
if (msg.length > 1) {
|
|
1922
|
-
let hook = new PayloadStubHook(new Evaluator(this).evaluate(msg[1]));
|
|
2269
|
+
let hook = new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[1]));
|
|
1923
2270
|
hook.ignoreUnhandledRejections();
|
|
1924
2271
|
let exportId = this.exports.length;
|
|
1925
2272
|
this.exports.push({
|
|
@@ -1954,13 +2301,13 @@ var RpcSessionImpl = class {
|
|
|
1954
2301
|
let importId = msg[1];
|
|
1955
2302
|
if (typeof importId == "number" && msg.length > 2) {
|
|
1956
2303
|
let imp = this.imports[importId];
|
|
1957
|
-
if (imp) if (msg[0] == "resolve") imp.resolve(new PayloadStubHook(new Evaluator(this).evaluate(msg[2])));
|
|
2304
|
+
if (imp) if (msg[0] == "resolve") imp.resolve(new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[2])));
|
|
1958
2305
|
else {
|
|
1959
|
-
let payload = new Evaluator(this).evaluate(msg[2]);
|
|
2306
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[2]);
|
|
1960
2307
|
payload.dispose();
|
|
1961
2308
|
imp.resolve(new ErrorStubHook(payload.value));
|
|
1962
2309
|
}
|
|
1963
|
-
else if (msg[0] == "resolve") new Evaluator(this).evaluate(msg[2]).dispose();
|
|
2310
|
+
else if (msg[0] == "resolve") new Evaluator(this, this.encodingLevel).evaluate(msg[2]).dispose();
|
|
1964
2311
|
continue;
|
|
1965
2312
|
}
|
|
1966
2313
|
break;
|
|
@@ -1975,9 +2322,9 @@ var RpcSessionImpl = class {
|
|
|
1975
2322
|
break;
|
|
1976
2323
|
}
|
|
1977
2324
|
case "abort": {
|
|
1978
|
-
let payload = new Evaluator(this).evaluate(msg[1]);
|
|
2325
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[1]);
|
|
1979
2326
|
payload.dispose();
|
|
1980
|
-
this.abort(payload, false);
|
|
2327
|
+
this.abort(payload.value, false);
|
|
1981
2328
|
break;
|
|
1982
2329
|
}
|
|
1983
2330
|
}
|
|
@@ -2028,6 +2375,8 @@ var RpcSession$1 = class {
|
|
|
2028
2375
|
|
|
2029
2376
|
//#endregion
|
|
2030
2377
|
//#region src/websocket.ts
|
|
2378
|
+
/** Close-frame reason max UTF-8 bytes: 125 payload - 2-byte status (RFC 6455 section 5.5). */
|
|
2379
|
+
const MAX_CLOSE_REASON_BYTES = 123;
|
|
2031
2380
|
function newWebSocketRpcSession$1(webSocket, localMain, options) {
|
|
2032
2381
|
if (typeof webSocket === "string") webSocket = new WebSocket(webSocket);
|
|
2033
2382
|
return new RpcSession$1(new WebSocketTransport(webSocket), localMain, options).getRemoteMain();
|
|
@@ -2047,9 +2396,14 @@ function newWorkersWebSocketRpcResponse(request, localMain, options) {
|
|
|
2047
2396
|
webSocket: pair[1]
|
|
2048
2397
|
});
|
|
2049
2398
|
}
|
|
2399
|
+
/**
|
|
2400
|
+
* Generic WebSocket transport. Default `T = string` is backward-compatible and satisfies
|
|
2401
|
+
* `RpcTransport`. Use `T = ArrayBuffer` as a building block for binary transports.
|
|
2402
|
+
*/
|
|
2050
2403
|
var WebSocketTransport = class {
|
|
2051
2404
|
constructor(webSocket) {
|
|
2052
2405
|
this.#webSocket = webSocket;
|
|
2406
|
+
webSocket.binaryType = "arraybuffer";
|
|
2053
2407
|
if (webSocket.readyState === WebSocket.CONNECTING) {
|
|
2054
2408
|
this.#sendQueue = [];
|
|
2055
2409
|
webSocket.addEventListener("open", (event) => {
|
|
@@ -2062,12 +2416,12 @@ var WebSocketTransport = class {
|
|
|
2062
2416
|
});
|
|
2063
2417
|
}
|
|
2064
2418
|
webSocket.addEventListener("message", (event) => {
|
|
2065
|
-
if (this.#error) {} else if (typeof event.data === "string") if (this.#receiveResolver) {
|
|
2419
|
+
if (this.#error) {} else if (typeof event.data === "string" || event.data instanceof ArrayBuffer) if (this.#receiveResolver) {
|
|
2066
2420
|
this.#receiveResolver(event.data);
|
|
2067
2421
|
this.#receiveResolver = void 0;
|
|
2068
2422
|
this.#receiveRejecter = void 0;
|
|
2069
2423
|
} else this.#receiveQueue.push(event.data);
|
|
2070
|
-
else this.#receivedError(/* @__PURE__ */ new TypeError("Received
|
|
2424
|
+
else this.#receivedError(/* @__PURE__ */ new TypeError("Received unexpected message type from WebSocket."));
|
|
2071
2425
|
});
|
|
2072
2426
|
webSocket.addEventListener("close", (event) => {
|
|
2073
2427
|
this.#receivedError(/* @__PURE__ */ new Error(`Peer closed WebSocket: ${event.code} ${event.reason}`));
|
|
@@ -2082,13 +2436,13 @@ var WebSocketTransport = class {
|
|
|
2082
2436
|
#receiveRejecter;
|
|
2083
2437
|
#receiveQueue = [];
|
|
2084
2438
|
#error;
|
|
2085
|
-
|
|
2439
|
+
send(message) {
|
|
2086
2440
|
if (this.#sendQueue === void 0) this.#webSocket.send(message);
|
|
2087
2441
|
else this.#sendQueue.push(message);
|
|
2088
2442
|
}
|
|
2089
|
-
|
|
2090
|
-
if (this.#receiveQueue.length > 0) return this.#receiveQueue.shift();
|
|
2091
|
-
else if (this.#error)
|
|
2443
|
+
receive() {
|
|
2444
|
+
if (this.#receiveQueue.length > 0) return Promise.resolve(this.#receiveQueue.shift());
|
|
2445
|
+
else if (this.#error) return Promise.reject(this.#error);
|
|
2092
2446
|
else return new Promise((resolve, reject) => {
|
|
2093
2447
|
this.#receiveResolver = resolve;
|
|
2094
2448
|
this.#receiveRejecter = reject;
|
|
@@ -2098,6 +2452,8 @@ var WebSocketTransport = class {
|
|
|
2098
2452
|
let message;
|
|
2099
2453
|
if (reason instanceof Error) message = reason.message;
|
|
2100
2454
|
else message = `${reason}`;
|
|
2455
|
+
let reasonBytes = new TextEncoder().encode(message);
|
|
2456
|
+
if (reasonBytes.length > 123) message = new TextDecoder().decode(reasonBytes.subarray(0, 123), { stream: true });
|
|
2101
2457
|
this.#webSocket.close(3e3, message);
|
|
2102
2458
|
if (!this.#error) this.#error = reason;
|
|
2103
2459
|
}
|
|
@@ -2115,6 +2471,7 @@ var WebSocketTransport = class {
|
|
|
2115
2471
|
|
|
2116
2472
|
//#endregion
|
|
2117
2473
|
//#region src/batch.ts
|
|
2474
|
+
const yieldToMacrotask = typeof setImmediate === "function" ? () => new Promise((resolve) => setImmediate(resolve)) : () => new Promise((resolve) => setTimeout(resolve, 0));
|
|
2118
2475
|
var BatchClientTransport = class {
|
|
2119
2476
|
constructor(sendBatch) {
|
|
2120
2477
|
this.#promise = this.#scheduleBatch(sendBatch);
|
|
@@ -2123,7 +2480,7 @@ var BatchClientTransport = class {
|
|
|
2123
2480
|
#aborted;
|
|
2124
2481
|
#batchToSend = [];
|
|
2125
2482
|
#batchToReceive = null;
|
|
2126
|
-
|
|
2483
|
+
send(message) {
|
|
2127
2484
|
if (this.#batchToSend !== null) this.#batchToSend.push(message);
|
|
2128
2485
|
}
|
|
2129
2486
|
async receive() {
|
|
@@ -2136,7 +2493,7 @@ var BatchClientTransport = class {
|
|
|
2136
2493
|
this.#aborted = reason;
|
|
2137
2494
|
}
|
|
2138
2495
|
async #scheduleBatch(sendBatch) {
|
|
2139
|
-
await
|
|
2496
|
+
await yieldToMacrotask();
|
|
2140
2497
|
if (this.#aborted !== void 0) throw this.#aborted;
|
|
2141
2498
|
let batch = this.#batchToSend;
|
|
2142
2499
|
this.#batchToSend = null;
|
|
@@ -2165,7 +2522,7 @@ var BatchServerTransport = class {
|
|
|
2165
2522
|
#batchToSend = [];
|
|
2166
2523
|
#batchToReceive;
|
|
2167
2524
|
#allReceived = Promise.withResolvers();
|
|
2168
|
-
|
|
2525
|
+
send(message) {
|
|
2169
2526
|
this.#batchToSend.push(message);
|
|
2170
2527
|
}
|
|
2171
2528
|
async receive() {
|
|
@@ -2214,7 +2571,11 @@ async function newHttpBatchRpcResponse(request, localMain, options) {
|
|
|
2214
2571
|
* @param options Optional RPC session options. You can also pass headers to set on the response.
|
|
2215
2572
|
*/
|
|
2216
2573
|
async function nodeHttpBatchRpcResponse(request, response, localMain, options) {
|
|
2217
|
-
if (request.method !== "POST")
|
|
2574
|
+
if (request.method !== "POST") {
|
|
2575
|
+
response.writeHead(405, "This endpoint only accepts POST requests.", options?.headers);
|
|
2576
|
+
response.end();
|
|
2577
|
+
return;
|
|
2578
|
+
}
|
|
2218
2579
|
let body = await new Promise((resolve, reject) => {
|
|
2219
2580
|
let chunks = [];
|
|
2220
2581
|
request.on("data", (chunk) => {
|
|
@@ -2239,17 +2600,17 @@ function newMessagePortRpcSession$1(port, localMain, options) {
|
|
|
2239
2600
|
return new RpcSession$1(new MessagePortTransport(port), localMain, options).getRemoteMain();
|
|
2240
2601
|
}
|
|
2241
2602
|
var MessagePortTransport = class {
|
|
2603
|
+
encodingLevel = "structuredClonable";
|
|
2242
2604
|
constructor(port) {
|
|
2243
2605
|
this.#port = port;
|
|
2244
2606
|
port.start();
|
|
2245
2607
|
port.addEventListener("message", (event) => {
|
|
2246
2608
|
if (this.#error) {} else if (event.data === null) this.#receivedError(/* @__PURE__ */ new Error("Peer closed MessagePort connection."));
|
|
2247
|
-
else if (
|
|
2609
|
+
else if (this.#receiveResolver) {
|
|
2248
2610
|
this.#receiveResolver(event.data);
|
|
2249
2611
|
this.#receiveResolver = void 0;
|
|
2250
2612
|
this.#receiveRejecter = void 0;
|
|
2251
2613
|
} else this.#receiveQueue.push(event.data);
|
|
2252
|
-
else this.#receivedError(/* @__PURE__ */ new TypeError("Received non-string message from MessagePort."));
|
|
2253
2614
|
});
|
|
2254
2615
|
port.addEventListener("messageerror", (event) => {
|
|
2255
2616
|
this.#receivedError(/* @__PURE__ */ new Error("MessagePort message error."));
|
|
@@ -2260,7 +2621,7 @@ var MessagePortTransport = class {
|
|
|
2260
2621
|
#receiveRejecter;
|
|
2261
2622
|
#receiveQueue = [];
|
|
2262
2623
|
#error;
|
|
2263
|
-
|
|
2624
|
+
send(message) {
|
|
2264
2625
|
if (this.#error) throw this.#error;
|
|
2265
2626
|
this.#port.postMessage(message);
|
|
2266
2627
|
}
|
|
@@ -2424,9 +2785,11 @@ var MapVariableHook = class extends StubHook {
|
|
|
2424
2785
|
else throwMapperBuilderUseError();
|
|
2425
2786
|
}
|
|
2426
2787
|
call(path, args) {
|
|
2788
|
+
args.dispose();
|
|
2427
2789
|
throwMapperBuilderUseError();
|
|
2428
2790
|
}
|
|
2429
2791
|
map(path, captures, instructions) {
|
|
2792
|
+
for (let cap of captures) cap.dispose();
|
|
2430
2793
|
throwMapperBuilderUseError();
|
|
2431
2794
|
}
|
|
2432
2795
|
pull() {
|
|
@@ -2479,6 +2842,9 @@ var MapApplicator = class {
|
|
|
2479
2842
|
getPipeReadable(exportId) {
|
|
2480
2843
|
throw new Error("A mapper function cannot use pipe readables.");
|
|
2481
2844
|
}
|
|
2845
|
+
getLimits() {
|
|
2846
|
+
return DEFAULT_LIMITS;
|
|
2847
|
+
}
|
|
2482
2848
|
};
|
|
2483
2849
|
function applyMapToElement(input, parent, owner, captures, instructions) {
|
|
2484
2850
|
let mapper = new MapApplicator(captures, new PayloadStubHook(RpcPayload.deepCopyFrom(input, parent, owner)));
|
|
@@ -2534,14 +2900,11 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
|
|
|
2534
2900
|
let state = this.getState();
|
|
2535
2901
|
if (path.length !== 1 || typeof path[0] !== "string") throw new Error("WritableStream stub only supports direct method calls");
|
|
2536
2902
|
const method = path[0];
|
|
2537
|
-
if (method !== "write" && method !== "close" && method !== "abort") {
|
|
2538
|
-
args.dispose();
|
|
2539
|
-
throw new Error(`Unknown WritableStream method: ${method}`);
|
|
2540
|
-
}
|
|
2903
|
+
if (method !== "write" && method !== "close" && method !== "abort") throw new Error(`Unknown WritableStream method: ${method}`);
|
|
2541
2904
|
if (method === "close" || method === "abort") state.closed = true;
|
|
2542
|
-
|
|
2543
|
-
return new PromiseStubHook(args.deliverCall(func, state.writer).then((payload) => new PayloadStubHook(payload)));
|
|
2905
|
+
return new PromiseStubHook((method === "write" ? args.deliverStreamWrite(state.writer) : args.deliverCall(state.writer[method], state.writer)).then((payload) => new PayloadStubHook(payload)));
|
|
2544
2906
|
} catch (err) {
|
|
2907
|
+
args.dispose();
|
|
2545
2908
|
return new ErrorStubHook(err);
|
|
2546
2909
|
}
|
|
2547
2910
|
}
|
|
@@ -2817,15 +3180,15 @@ let newMessagePortRpcSession = newMessagePortRpcSession$1;
|
|
|
2817
3180
|
* credentials as parameters and returns the authorized API), then cross-origin requests should
|
|
2818
3181
|
* be safe.
|
|
2819
3182
|
*/
|
|
2820
|
-
async function newWorkersRpcResponse(request, localMain) {
|
|
3183
|
+
async function newWorkersRpcResponse(request, localMain, options) {
|
|
2821
3184
|
if (request.method === "POST") {
|
|
2822
|
-
let response = await newHttpBatchRpcResponse(request, localMain);
|
|
3185
|
+
let response = await newHttpBatchRpcResponse(request, localMain, options);
|
|
2823
3186
|
response.headers.set("Access-Control-Allow-Origin", "*");
|
|
2824
3187
|
return response;
|
|
2825
|
-
} else if (request.headers.get("Upgrade")?.toLowerCase() === "websocket") return newWorkersWebSocketRpcResponse(request, localMain);
|
|
3188
|
+
} else if (request.headers.get("Upgrade")?.toLowerCase() === "websocket") return newWorkersWebSocketRpcResponse(request, localMain, options);
|
|
2826
3189
|
else return new Response("This endpoint only accepts POST or WebSocket requests.", { status: 400 });
|
|
2827
3190
|
}
|
|
2828
3191
|
|
|
2829
3192
|
//#endregion
|
|
2830
|
-
export { RpcPromise, RpcSession, RpcStub, RpcTarget, deserialize, newHttpBatchRpcResponse, newHttpBatchRpcSession, newMessagePortRpcSession, newWebSocketRpcSession, newWorkersRpcResponse, newWorkersWebSocketRpcResponse, nodeHttpBatchRpcResponse, serialize };
|
|
3193
|
+
export { DEFAULT_LIMITS, DEFAULT_MAX_DEPTH, RpcPromise, RpcSession, RpcStub, RpcTarget, WebSocketTransport, deserialize, newHttpBatchRpcResponse, newHttpBatchRpcSession, newMessagePortRpcSession, newWebSocketRpcSession, newWorkersRpcResponse, newWorkersWebSocketRpcResponse, nodeHttpBatchRpcResponse, serialize };
|
|
2831
3194
|
//# sourceMappingURL=index-workers.js.map
|