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.cjs
CHANGED
|
@@ -76,9 +76,22 @@ function typeForRpc(value) {
|
|
|
76
76
|
case Array.prototype: return "array";
|
|
77
77
|
case Date.prototype: return "date";
|
|
78
78
|
case Uint8Array.prototype:
|
|
79
|
-
case BUFFER_PROTOTYPE:
|
|
79
|
+
case BUFFER_PROTOTYPE:
|
|
80
|
+
case ArrayBuffer.prototype:
|
|
81
|
+
case DataView.prototype:
|
|
82
|
+
case Int8Array.prototype:
|
|
83
|
+
case Uint8ClampedArray.prototype:
|
|
84
|
+
case Int16Array.prototype:
|
|
85
|
+
case Uint16Array.prototype:
|
|
86
|
+
case Int32Array.prototype:
|
|
87
|
+
case Uint32Array.prototype:
|
|
88
|
+
case BigInt64Array.prototype:
|
|
89
|
+
case BigUint64Array.prototype:
|
|
90
|
+
case Float32Array.prototype:
|
|
91
|
+
case Float64Array.prototype: return "bytes";
|
|
80
92
|
case WritableStream.prototype: return "writable";
|
|
81
93
|
case ReadableStream.prototype: return "readable";
|
|
94
|
+
case URL.prototype: return "url";
|
|
82
95
|
case Headers.prototype: return "headers";
|
|
83
96
|
case Request.prototype: return "request";
|
|
84
97
|
case Response.prototype: return "response";
|
|
@@ -112,7 +125,14 @@ let streamImpl = {
|
|
|
112
125
|
};
|
|
113
126
|
var StubHook = class {
|
|
114
127
|
stream(path, args) {
|
|
115
|
-
let
|
|
128
|
+
let hook = this.call(path, args);
|
|
129
|
+
let pulled;
|
|
130
|
+
try {
|
|
131
|
+
pulled = hook.pull();
|
|
132
|
+
} catch (err) {
|
|
133
|
+
hook.dispose();
|
|
134
|
+
throw err;
|
|
135
|
+
}
|
|
116
136
|
let promise;
|
|
117
137
|
if (pulled instanceof Promise) promise = pulled.then((p) => {
|
|
118
138
|
p.dispose();
|
|
@@ -131,9 +151,11 @@ var ErrorStubHook = class extends StubHook {
|
|
|
131
151
|
this.error = error;
|
|
132
152
|
}
|
|
133
153
|
call(path, args) {
|
|
154
|
+
args.dispose();
|
|
134
155
|
return this;
|
|
135
156
|
}
|
|
136
157
|
map(path, captures, instructions) {
|
|
158
|
+
for (let cap of captures) cap.dispose();
|
|
137
159
|
return this;
|
|
138
160
|
}
|
|
139
161
|
get(path) {
|
|
@@ -257,7 +279,25 @@ var RpcStub$1 = class RpcStub$1 extends RpcTarget$1 {
|
|
|
257
279
|
};
|
|
258
280
|
var RpcPromise$1 = class extends RpcStub$1 {
|
|
259
281
|
constructor(hook, pathIfPromise) {
|
|
260
|
-
super(hook, pathIfPromise);
|
|
282
|
+
if (hook instanceof StubHook) super(hook, pathIfPromise ?? []);
|
|
283
|
+
else {
|
|
284
|
+
if (pathIfPromise !== void 0) throw new TypeError("RpcPromise constructor expected one argument, received two.");
|
|
285
|
+
let kind = typeForRpc(hook);
|
|
286
|
+
if (kind === "rpc-promise") {
|
|
287
|
+
let raw = unwrapStubAndPath(hook);
|
|
288
|
+
if (raw.pathIfPromise.length > 0) super(raw.hook, raw.pathIfPromise);
|
|
289
|
+
else {
|
|
290
|
+
let adopted = raw.hook;
|
|
291
|
+
raw.hook = DISPOSED_HOOK;
|
|
292
|
+
super(adopted, []);
|
|
293
|
+
}
|
|
294
|
+
} else if (kind === "rpc-thenable") super(TargetStubHook.create(hook, void 0), []);
|
|
295
|
+
else {
|
|
296
|
+
let promiseHook = new PromiseStubHook(Promise.resolve(hook).then((value) => new PayloadStubHook(RpcPayload.fromAppReturn(value))));
|
|
297
|
+
promiseHook.ignoreUnhandledRejections();
|
|
298
|
+
super(promiseHook, []);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
261
301
|
}
|
|
262
302
|
then(onfulfilled, onrejected) {
|
|
263
303
|
return pullPromise(this).then(...arguments);
|
|
@@ -413,6 +453,7 @@ var RpcPayload = class RpcPayload {
|
|
|
413
453
|
case "date":
|
|
414
454
|
case "bytes":
|
|
415
455
|
case "blob":
|
|
456
|
+
case "url":
|
|
416
457
|
case "error":
|
|
417
458
|
case "undefined": return value;
|
|
418
459
|
case "array": {
|
|
@@ -546,6 +587,30 @@ var RpcPayload = class RpcPayload {
|
|
|
546
587
|
this.dispose();
|
|
547
588
|
}
|
|
548
589
|
}
|
|
590
|
+
async deliverStreamWrite(writer) {
|
|
591
|
+
try {
|
|
592
|
+
let promises = [];
|
|
593
|
+
this.deliverTo(this, "value", promises);
|
|
594
|
+
if (promises.length > 0) await Promise.all(promises);
|
|
595
|
+
let chunk = this.value[0];
|
|
596
|
+
if ((this.hooks.length > 0 || this.promises.length > 0) && chunk instanceof Object) {
|
|
597
|
+
if (!(Symbol.dispose in chunk)) Object.defineProperty(chunk, Symbol.dispose, {
|
|
598
|
+
value: () => this.dispose(),
|
|
599
|
+
writable: true,
|
|
600
|
+
enumerable: false,
|
|
601
|
+
configurable: true
|
|
602
|
+
});
|
|
603
|
+
await writer.write(chunk);
|
|
604
|
+
return RpcPayload.fromAppReturn(void 0);
|
|
605
|
+
}
|
|
606
|
+
await writer.write(chunk);
|
|
607
|
+
this.dispose();
|
|
608
|
+
return RpcPayload.fromAppReturn(void 0);
|
|
609
|
+
} catch (err) {
|
|
610
|
+
this.dispose();
|
|
611
|
+
throw err;
|
|
612
|
+
}
|
|
613
|
+
}
|
|
549
614
|
async deliverResolve() {
|
|
550
615
|
try {
|
|
551
616
|
let promises = [];
|
|
@@ -586,6 +651,7 @@ var RpcPayload = class RpcPayload {
|
|
|
586
651
|
case "bytes":
|
|
587
652
|
case "blob":
|
|
588
653
|
case "date":
|
|
654
|
+
case "url":
|
|
589
655
|
case "error":
|
|
590
656
|
case "undefined": return;
|
|
591
657
|
case "array": {
|
|
@@ -668,6 +734,7 @@ var RpcPayload = class RpcPayload {
|
|
|
668
734
|
case "rpc-target":
|
|
669
735
|
case "writable":
|
|
670
736
|
case "readable":
|
|
737
|
+
case "url":
|
|
671
738
|
case "headers":
|
|
672
739
|
case "request":
|
|
673
740
|
case "response": return;
|
|
@@ -737,6 +804,7 @@ function followPath(value, parent, path, owner) {
|
|
|
737
804
|
case "blob":
|
|
738
805
|
case "date":
|
|
739
806
|
case "error":
|
|
807
|
+
case "url":
|
|
740
808
|
case "headers":
|
|
741
809
|
case "request":
|
|
742
810
|
case "response":
|
|
@@ -769,17 +837,22 @@ function followPath(value, parent, path, owner) {
|
|
|
769
837
|
}
|
|
770
838
|
var ValueStubHook = class extends StubHook {
|
|
771
839
|
call(path, args) {
|
|
840
|
+
let followResult;
|
|
772
841
|
try {
|
|
773
842
|
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
|
-
}));
|
|
843
|
+
followResult = followPath(value, void 0, path, owner);
|
|
780
844
|
} catch (err) {
|
|
845
|
+
args.dispose();
|
|
781
846
|
return new ErrorStubHook(err);
|
|
782
847
|
}
|
|
848
|
+
if (followResult.hook) return followResult.hook.call(followResult.remainingPath, args);
|
|
849
|
+
if (typeof followResult.value != "function") {
|
|
850
|
+
args.dispose();
|
|
851
|
+
return new ErrorStubHook(/* @__PURE__ */ new TypeError(`'${path.join(".")}' is not a function.`));
|
|
852
|
+
}
|
|
853
|
+
return new PromiseStubHook(args.deliverCall(followResult.value, followResult.parent).then((payload) => {
|
|
854
|
+
return new PayloadStubHook(payload);
|
|
855
|
+
}));
|
|
783
856
|
}
|
|
784
857
|
map(path, captures, instructions) {
|
|
785
858
|
try {
|
|
@@ -800,7 +873,10 @@ var ValueStubHook = class extends StubHook {
|
|
|
800
873
|
get(path) {
|
|
801
874
|
try {
|
|
802
875
|
let { value, owner } = this.getValue();
|
|
803
|
-
if (path.length === 0 && owner === null)
|
|
876
|
+
if (path.length === 0 && owner === null) {
|
|
877
|
+
if (value instanceof Object && "then" in value) return this.dup();
|
|
878
|
+
throw new Error("Can't dup an RpcTarget stub as a promise.");
|
|
879
|
+
}
|
|
804
880
|
let followResult = followPath(value, void 0, path, owner);
|
|
805
881
|
if (followResult.hook) return followResult.hook.get(followResult.remainingPath);
|
|
806
882
|
return new PayloadStubHook(RpcPayload.deepCopyFrom(followResult.value, followResult.parent, followResult.owner));
|
|
@@ -903,7 +979,10 @@ var TargetStubHook = class TargetStubHook extends ValueStubHook {
|
|
|
903
979
|
this.target = void 0;
|
|
904
980
|
}
|
|
905
981
|
}
|
|
906
|
-
onBroken(callback) {
|
|
982
|
+
onBroken(callback) {
|
|
983
|
+
let target = this.target;
|
|
984
|
+
if (target && "then" in target) Promise.resolve(target).then(() => {}, callback);
|
|
985
|
+
}
|
|
907
986
|
};
|
|
908
987
|
var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
909
988
|
promise;
|
|
@@ -917,12 +996,16 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
|
917
996
|
}
|
|
918
997
|
call(path, args) {
|
|
919
998
|
args.ensureDeepCopied();
|
|
920
|
-
return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args))
|
|
999
|
+
return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args), (err) => {
|
|
1000
|
+
args.dispose();
|
|
1001
|
+
throw err;
|
|
1002
|
+
}));
|
|
921
1003
|
}
|
|
922
1004
|
stream(path, args) {
|
|
923
1005
|
args.ensureDeepCopied();
|
|
924
|
-
return { promise: this.promise.then((hook) => {
|
|
925
|
-
|
|
1006
|
+
return { promise: this.promise.then((hook) => hook.stream(path, args).promise, (err) => {
|
|
1007
|
+
args.dispose();
|
|
1008
|
+
throw err;
|
|
926
1009
|
}) };
|
|
927
1010
|
}
|
|
928
1011
|
map(path, captures, instructions) {
|
|
@@ -949,10 +1032,7 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
|
949
1032
|
}, (err) => {});
|
|
950
1033
|
}
|
|
951
1034
|
dispose() {
|
|
952
|
-
|
|
953
|
-
else this.promise.then((hook) => {
|
|
954
|
-
hook.dispose();
|
|
955
|
-
}, (err) => {});
|
|
1035
|
+
this.promise.then((hook) => hook.dispose(), () => {});
|
|
956
1036
|
}
|
|
957
1037
|
onBroken(callback) {
|
|
958
1038
|
if (this.resolution) this.resolution.onBroken(callback);
|
|
@@ -964,6 +1044,77 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
|
964
1044
|
|
|
965
1045
|
//#endregion
|
|
966
1046
|
//#region src/serialize.ts
|
|
1047
|
+
const DEFAULT_MAX_DEPTH = 256;
|
|
1048
|
+
const DEFAULT_LIMITS = {
|
|
1049
|
+
maxBigIntDigits: 16384,
|
|
1050
|
+
maxDepth: 256,
|
|
1051
|
+
maxMessageSize: 32 * 1024 * 1024
|
|
1052
|
+
};
|
|
1053
|
+
const NATIVE_LITTLE_ENDIAN = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
|
|
1054
|
+
const BYTE_CONTAINER_TYPE_NAMES = [
|
|
1055
|
+
"ArrayBuffer",
|
|
1056
|
+
"DataView",
|
|
1057
|
+
"Int8Array",
|
|
1058
|
+
"Uint8Array",
|
|
1059
|
+
"Uint8ClampedArray",
|
|
1060
|
+
"Int16Array",
|
|
1061
|
+
"Uint16Array",
|
|
1062
|
+
"Int32Array",
|
|
1063
|
+
"Uint32Array",
|
|
1064
|
+
"BigInt64Array",
|
|
1065
|
+
"BigUint64Array",
|
|
1066
|
+
"Float32Array",
|
|
1067
|
+
"Float64Array"
|
|
1068
|
+
];
|
|
1069
|
+
function isValidByteContainerName(value) {
|
|
1070
|
+
return BYTE_CONTAINER_TYPE_NAMES.includes(value);
|
|
1071
|
+
}
|
|
1072
|
+
const TYPED_ARRAY_ELEMENT_SIZE = {
|
|
1073
|
+
ArrayBuffer: void 0,
|
|
1074
|
+
DataView: void 0,
|
|
1075
|
+
Int8Array: void 0,
|
|
1076
|
+
Uint8Array: void 0,
|
|
1077
|
+
Uint8ClampedArray: void 0,
|
|
1078
|
+
Int16Array: 2,
|
|
1079
|
+
Uint16Array: 2,
|
|
1080
|
+
Int32Array: 4,
|
|
1081
|
+
Uint32Array: 4,
|
|
1082
|
+
BigInt64Array: 8,
|
|
1083
|
+
BigUint64Array: 8,
|
|
1084
|
+
Float32Array: 4,
|
|
1085
|
+
Float64Array: 8
|
|
1086
|
+
};
|
|
1087
|
+
const BYTE_CONTAINER_PROTOTYPES = {
|
|
1088
|
+
ArrayBuffer: ArrayBuffer.prototype,
|
|
1089
|
+
DataView: DataView.prototype,
|
|
1090
|
+
Int8Array: Int8Array.prototype,
|
|
1091
|
+
Uint8ClampedArray: Uint8ClampedArray.prototype,
|
|
1092
|
+
Int16Array: Int16Array.prototype,
|
|
1093
|
+
Uint16Array: Uint16Array.prototype,
|
|
1094
|
+
Int32Array: Int32Array.prototype,
|
|
1095
|
+
Uint32Array: Uint32Array.prototype,
|
|
1096
|
+
BigInt64Array: BigInt64Array.prototype,
|
|
1097
|
+
BigUint64Array: BigUint64Array.prototype,
|
|
1098
|
+
Float32Array: Float32Array.prototype,
|
|
1099
|
+
Float64Array: Float64Array.prototype
|
|
1100
|
+
};
|
|
1101
|
+
const BYTE_CONTAINER_TYPE_BY_PROTOTYPE = /* @__PURE__ */ new Map();
|
|
1102
|
+
for (let type of Object.keys(BYTE_CONTAINER_PROTOTYPES)) BYTE_CONTAINER_TYPE_BY_PROTOTYPE.set(BYTE_CONTAINER_PROTOTYPES[type], type);
|
|
1103
|
+
function swapByteOrder(bytes, elementSize) {
|
|
1104
|
+
if (elementSize !== 2 && elementSize !== 4 && elementSize !== 8) throw new RangeError(`Unsupported element size: ${elementSize}`);
|
|
1105
|
+
let view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
1106
|
+
for (let offset = 0; offset < bytes.byteLength; offset += elementSize) switch (elementSize) {
|
|
1107
|
+
case 2:
|
|
1108
|
+
view.setUint16(offset, view.getUint16(offset, false), true);
|
|
1109
|
+
break;
|
|
1110
|
+
case 4:
|
|
1111
|
+
view.setUint32(offset, view.getUint32(offset, false), true);
|
|
1112
|
+
break;
|
|
1113
|
+
case 8:
|
|
1114
|
+
view.setBigUint64(offset, view.getBigUint64(offset, false), true);
|
|
1115
|
+
break;
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
967
1118
|
var NullExporter = class {
|
|
968
1119
|
exportStub(stub) {
|
|
969
1120
|
throw new Error("Cannot serialize RPC stubs without an RPC session.");
|
|
@@ -984,6 +1135,7 @@ async function streamToBlob(stream, type) {
|
|
|
984
1135
|
return b.type === type ? b : b.slice(0, b.size, type);
|
|
985
1136
|
}
|
|
986
1137
|
const ERROR_TYPES = {
|
|
1138
|
+
__proto__: null,
|
|
987
1139
|
Error,
|
|
988
1140
|
EvalError,
|
|
989
1141
|
RangeError,
|
|
@@ -996,12 +1148,14 @@ const ERROR_TYPES = {
|
|
|
996
1148
|
var Devaluator = class Devaluator {
|
|
997
1149
|
exporter;
|
|
998
1150
|
source;
|
|
999
|
-
|
|
1151
|
+
encodingLevel;
|
|
1152
|
+
constructor(exporter, source, encodingLevel) {
|
|
1000
1153
|
this.exporter = exporter;
|
|
1001
1154
|
this.source = source;
|
|
1155
|
+
this.encodingLevel = encodingLevel;
|
|
1002
1156
|
}
|
|
1003
|
-
static devaluate(value, parent, exporter = NULL_EXPORTER, source) {
|
|
1004
|
-
let devaluator = new Devaluator(exporter, source);
|
|
1157
|
+
static devaluate(value, parent, exporter = NULL_EXPORTER, source, encodingLevel = "string") {
|
|
1158
|
+
let devaluator = new Devaluator(exporter, source, encodingLevel);
|
|
1005
1159
|
try {
|
|
1006
1160
|
return devaluator.devaluateImpl(value, parent, 0);
|
|
1007
1161
|
} catch (err) {
|
|
@@ -1013,7 +1167,7 @@ var Devaluator = class Devaluator {
|
|
|
1013
1167
|
}
|
|
1014
1168
|
exports;
|
|
1015
1169
|
devaluateImpl(value, parent, depth) {
|
|
1016
|
-
if (depth >=
|
|
1170
|
+
if (depth >= 256) throw new Error("Serialization exceeded maximum allowed depth. (Does the message contain cycles?)");
|
|
1017
1171
|
switch (typeForRpc(value)) {
|
|
1018
1172
|
case "unsupported": {
|
|
1019
1173
|
let msg;
|
|
@@ -1024,10 +1178,12 @@ var Devaluator = class Devaluator {
|
|
|
1024
1178
|
}
|
|
1025
1179
|
throw new TypeError(msg);
|
|
1026
1180
|
}
|
|
1027
|
-
case "primitive": if (typeof value === "number" && !isFinite(value))
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1181
|
+
case "primitive": if (typeof value === "number" && !isFinite(value)) {
|
|
1182
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1183
|
+
if (value === Infinity) return ["inf"];
|
|
1184
|
+
else if (value === -Infinity) return ["-inf"];
|
|
1185
|
+
else return ["nan"];
|
|
1186
|
+
} else return value;
|
|
1031
1187
|
case "object": {
|
|
1032
1188
|
let object = value;
|
|
1033
1189
|
let result = {};
|
|
@@ -1041,23 +1197,49 @@ var Devaluator = class Devaluator {
|
|
|
1041
1197
|
for (let i = 0; i < len; i++) result[i] = this.devaluateImpl(array[i], array, depth + 1);
|
|
1042
1198
|
return [result];
|
|
1043
1199
|
}
|
|
1044
|
-
case "bigint":
|
|
1200
|
+
case "bigint":
|
|
1201
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1202
|
+
return ["bigint", value.toString()];
|
|
1045
1203
|
case "date": {
|
|
1204
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1046
1205
|
const time = value.getTime();
|
|
1047
1206
|
return ["date", Number.isNaN(time) ? null : time];
|
|
1048
1207
|
}
|
|
1049
1208
|
case "bytes": {
|
|
1050
|
-
let
|
|
1051
|
-
|
|
1209
|
+
let alternateTypeName = BYTE_CONTAINER_TYPE_BY_PROTOTYPE.get(Object.getPrototypeOf(value));
|
|
1210
|
+
let bytes;
|
|
1211
|
+
if (alternateTypeName === "ArrayBuffer") bytes = new Uint8Array(value);
|
|
1212
|
+
else if (alternateTypeName === void 0) bytes = value;
|
|
1213
|
+
else {
|
|
1214
|
+
let view = value;
|
|
1215
|
+
bytes = new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
|
|
1216
|
+
let elementSize = TYPED_ARRAY_ELEMENT_SIZE[alternateTypeName];
|
|
1217
|
+
if (!NATIVE_LITTLE_ENDIAN && elementSize) {
|
|
1218
|
+
bytes = bytes.slice();
|
|
1219
|
+
swapByteOrder(bytes, elementSize);
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1222
|
+
if (this.encodingLevel === "structuredClonable" || this.encodingLevel === "jsonCompatibleWithBytes") return alternateTypeName === void 0 ? ["bytes", bytes] : [
|
|
1223
|
+
"bytes",
|
|
1224
|
+
bytes,
|
|
1225
|
+
alternateTypeName
|
|
1226
|
+
];
|
|
1052
1227
|
let b64;
|
|
1053
|
-
if (
|
|
1228
|
+
if (bytes.toBase64) b64 = bytes.toBase64({ omitPadding: true });
|
|
1229
|
+
else if (typeof Buffer !== "undefined") b64 = (bytes instanceof Buffer ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)).toString("base64");
|
|
1054
1230
|
else {
|
|
1055
1231
|
let binary = "";
|
|
1056
1232
|
for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]);
|
|
1057
1233
|
b64 = btoa(binary);
|
|
1058
1234
|
}
|
|
1059
|
-
|
|
1235
|
+
b64 = b64.replace(/=+$/, "");
|
|
1236
|
+
return alternateTypeName === void 0 ? ["bytes", b64] : [
|
|
1237
|
+
"bytes",
|
|
1238
|
+
b64,
|
|
1239
|
+
alternateTypeName
|
|
1240
|
+
];
|
|
1060
1241
|
}
|
|
1242
|
+
case "url": return ["url", value.href];
|
|
1061
1243
|
case "headers": return ["headers", [...value]];
|
|
1062
1244
|
case "request": {
|
|
1063
1245
|
let req = value;
|
|
@@ -1172,7 +1354,9 @@ var Devaluator = class Devaluator {
|
|
|
1172
1354
|
} else if (rewritten && rewritten.stack) result.push(rewritten.stack);
|
|
1173
1355
|
return result;
|
|
1174
1356
|
}
|
|
1175
|
-
case "undefined":
|
|
1357
|
+
case "undefined":
|
|
1358
|
+
if (this.encodingLevel === "structuredClonable") return;
|
|
1359
|
+
return ["undefined"];
|
|
1176
1360
|
case "stub":
|
|
1177
1361
|
case "rpc-promise": {
|
|
1178
1362
|
if (!this.source) throw new Error("Can't serialize RPC stubs in this context.");
|
|
@@ -1239,6 +1423,9 @@ var NullImporter = class {
|
|
|
1239
1423
|
getPipeReadable(exportId) {
|
|
1240
1424
|
throw new Error("Cannot retrieve pipe readable without an RPC session.");
|
|
1241
1425
|
}
|
|
1426
|
+
getLimits() {
|
|
1427
|
+
return DEFAULT_LIMITS;
|
|
1428
|
+
}
|
|
1242
1429
|
};
|
|
1243
1430
|
const NULL_IMPORTER = new NullImporter();
|
|
1244
1431
|
function fixBrokenRequestBody(request, body) {
|
|
@@ -1255,15 +1442,22 @@ function streamToBlobPromise(stream, type) {
|
|
|
1255
1442
|
}
|
|
1256
1443
|
var Evaluator = class Evaluator {
|
|
1257
1444
|
importer;
|
|
1258
|
-
|
|
1445
|
+
encodingLevel;
|
|
1446
|
+
limits;
|
|
1447
|
+
constructor(importer, encodingLevel = "string") {
|
|
1259
1448
|
this.importer = importer;
|
|
1449
|
+
this.encodingLevel = encodingLevel;
|
|
1450
|
+
this.limits = importer.getLimits();
|
|
1260
1451
|
}
|
|
1261
1452
|
hooks = [];
|
|
1262
1453
|
promises = [];
|
|
1263
1454
|
evaluate(value) {
|
|
1455
|
+
return this.evaluateWithDepth(value, 0);
|
|
1456
|
+
}
|
|
1457
|
+
evaluateWithDepth(value, depth) {
|
|
1264
1458
|
let payload = RpcPayload.forEvaluate(this.hooks, this.promises);
|
|
1265
1459
|
try {
|
|
1266
|
-
payload.value = this.evaluateImpl(value, payload, "value");
|
|
1460
|
+
payload.value = this.evaluateImpl(value, payload, "value", depth);
|
|
1267
1461
|
return payload;
|
|
1268
1462
|
} catch (err) {
|
|
1269
1463
|
payload.dispose();
|
|
@@ -1273,31 +1467,70 @@ var Evaluator = class Evaluator {
|
|
|
1273
1467
|
evaluateCopy(value) {
|
|
1274
1468
|
return this.evaluate(structuredClone(value));
|
|
1275
1469
|
}
|
|
1276
|
-
evaluateImpl(value, parent, property) {
|
|
1470
|
+
evaluateImpl(value, parent, property, depth) {
|
|
1471
|
+
let maxDepth = this.limits.maxDepth;
|
|
1472
|
+
if (depth >= maxDepth) throw new TypeError(`Deserialization exceeded maximum allowed message depth of ${maxDepth}.`);
|
|
1473
|
+
if (this.encodingLevel === "structuredClonable") {
|
|
1474
|
+
if (value instanceof Date || typeof value === "bigint") return value;
|
|
1475
|
+
}
|
|
1277
1476
|
if (value instanceof Array) {
|
|
1278
1477
|
if (value.length == 1 && value[0] instanceof Array) {
|
|
1279
1478
|
let result = value[0];
|
|
1280
|
-
for (let i = 0; i < result.length; i++) result[i] = this.evaluateImpl(result[i], result, i);
|
|
1479
|
+
for (let i = 0; i < result.length; i++) result[i] = this.evaluateImpl(result[i], result, i, depth + 1);
|
|
1281
1480
|
return result;
|
|
1282
1481
|
} else switch (value[0]) {
|
|
1283
1482
|
case "bigint":
|
|
1284
|
-
if (typeof value[1] == "string")
|
|
1483
|
+
if (typeof value[1] == "string") {
|
|
1484
|
+
let digits = value[1];
|
|
1485
|
+
let maxBigIntDigits = this.limits.maxBigIntDigits;
|
|
1486
|
+
if (digits.length > maxBigIntDigits) throw new TypeError(`Deserialized bigint exceeds maximum length of ${maxBigIntDigits} digits.`);
|
|
1487
|
+
return BigInt(digits);
|
|
1488
|
+
}
|
|
1285
1489
|
break;
|
|
1286
1490
|
case "date":
|
|
1287
1491
|
if (value[1] === null) return /* @__PURE__ */ new Date(NaN);
|
|
1288
1492
|
if (typeof value[1] == "number") return new Date(value[1]);
|
|
1289
1493
|
break;
|
|
1290
|
-
case "bytes":
|
|
1291
|
-
|
|
1292
|
-
|
|
1494
|
+
case "bytes": {
|
|
1495
|
+
let bytes;
|
|
1496
|
+
if (value[1] instanceof Uint8Array) bytes = value[1];
|
|
1497
|
+
else if (typeof value[1] == "string") if (typeof Buffer !== "undefined") bytes = Buffer.from(value[1], "base64");
|
|
1498
|
+
else if (Uint8Array.fromBase64) bytes = Uint8Array.fromBase64(value[1]);
|
|
1293
1499
|
else {
|
|
1294
1500
|
let bs = atob(value[1]);
|
|
1295
1501
|
let len = bs.length;
|
|
1296
|
-
|
|
1502
|
+
bytes = new Uint8Array(len);
|
|
1297
1503
|
for (let i = 0; i < len; i++) bytes[i] = bs.charCodeAt(i);
|
|
1298
|
-
return bytes;
|
|
1299
1504
|
}
|
|
1300
|
-
break;
|
|
1505
|
+
else break;
|
|
1506
|
+
if (value.length === 2) return bytes;
|
|
1507
|
+
if (typeof value[2] !== "string") throw new TypeError(`Unknown bytes type marker type: ${typeof value[2]}`);
|
|
1508
|
+
if (!isValidByteContainerName(value[2])) {
|
|
1509
|
+
let marker = value[2].slice(0, 64);
|
|
1510
|
+
throw new TypeError(`Unknown bytes type marker: ${marker}`);
|
|
1511
|
+
}
|
|
1512
|
+
let marker = value[2];
|
|
1513
|
+
let elementSize = TYPED_ARRAY_ELEMENT_SIZE[marker];
|
|
1514
|
+
if (elementSize !== void 0 && bytes.byteLength % elementSize !== 0) throw new TypeError(`Invalid byte length ${bytes.byteLength} for ${marker}; expected a multiple of ${elementSize}`);
|
|
1515
|
+
let buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
|
1516
|
+
if (!NATIVE_LITTLE_ENDIAN && elementSize !== void 0) swapByteOrder(new Uint8Array(buffer), elementSize);
|
|
1517
|
+
switch (marker) {
|
|
1518
|
+
case "ArrayBuffer": return buffer;
|
|
1519
|
+
case "DataView": return new DataView(buffer);
|
|
1520
|
+
case "Int8Array": return new Int8Array(buffer);
|
|
1521
|
+
case "Uint8Array": return new Uint8Array(buffer);
|
|
1522
|
+
case "Uint8ClampedArray": return new Uint8ClampedArray(buffer);
|
|
1523
|
+
case "Int16Array": return new Int16Array(buffer);
|
|
1524
|
+
case "Uint16Array": return new Uint16Array(buffer);
|
|
1525
|
+
case "Int32Array": return new Int32Array(buffer);
|
|
1526
|
+
case "Uint32Array": return new Uint32Array(buffer);
|
|
1527
|
+
case "BigInt64Array": return new BigInt64Array(buffer);
|
|
1528
|
+
case "BigUint64Array": return new BigUint64Array(buffer);
|
|
1529
|
+
case "Float32Array": return new Float32Array(buffer);
|
|
1530
|
+
case "Float64Array": return new Float64Array(buffer);
|
|
1531
|
+
default:
|
|
1532
|
+
}
|
|
1533
|
+
}
|
|
1301
1534
|
case "error":
|
|
1302
1535
|
if (value.length >= 3 && typeof value[1] === "string" && typeof value[2] === "string") {
|
|
1303
1536
|
let cls = ERROR_TYPES[value[1]] || Error;
|
|
@@ -1310,7 +1543,11 @@ var Evaluator = class Evaluator {
|
|
|
1310
1543
|
let propsObj = props;
|
|
1311
1544
|
for (let key of Object.keys(propsObj)) {
|
|
1312
1545
|
if (key === "name" || key === "message" || key === "stack") continue;
|
|
1313
|
-
|
|
1546
|
+
if (key in Object.prototype || key === "toJSON") {
|
|
1547
|
+
this.evaluateImpl(propsObj[key], result, key, depth + 1);
|
|
1548
|
+
continue;
|
|
1549
|
+
}
|
|
1550
|
+
anyResult[key] = this.evaluateImpl(propsObj[key], result, key, depth + 1);
|
|
1314
1551
|
}
|
|
1315
1552
|
}
|
|
1316
1553
|
return result;
|
|
@@ -1322,6 +1559,9 @@ var Evaluator = class Evaluator {
|
|
|
1322
1559
|
case "inf": return Infinity;
|
|
1323
1560
|
case "-inf": return -Infinity;
|
|
1324
1561
|
case "nan": return NaN;
|
|
1562
|
+
case "url":
|
|
1563
|
+
if (value.length === 2 && typeof value[1] === "string") return new URL(value[1]);
|
|
1564
|
+
break;
|
|
1325
1565
|
case "headers":
|
|
1326
1566
|
if (value.length === 2 && value[1] instanceof Array) return new Headers(value[1]);
|
|
1327
1567
|
break;
|
|
@@ -1331,11 +1571,11 @@ var Evaluator = class Evaluator {
|
|
|
1331
1571
|
let init = value[2];
|
|
1332
1572
|
if (typeof init !== "object" || init === null) break;
|
|
1333
1573
|
if (init.body) {
|
|
1334
|
-
init.body = this.evaluateImpl(init.body, init, "body");
|
|
1574
|
+
init.body = this.evaluateImpl(init.body, init, "body", depth + 1);
|
|
1335
1575
|
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
1576
|
}
|
|
1337
1577
|
if (init.signal) {
|
|
1338
|
-
init.signal = this.evaluateImpl(init.signal, init, "signal");
|
|
1578
|
+
init.signal = this.evaluateImpl(init.signal, init, "signal", depth + 1);
|
|
1339
1579
|
if (!(init.signal instanceof AbortSignal)) throw new TypeError("Request siganl must be of type AbortSignal.");
|
|
1340
1580
|
}
|
|
1341
1581
|
if (init.headers && !(init.headers instanceof Array)) throw new TypeError("Request headers must be serialized as an array of pairs.");
|
|
@@ -1352,7 +1592,7 @@ var Evaluator = class Evaluator {
|
|
|
1352
1592
|
}
|
|
1353
1593
|
case "response": {
|
|
1354
1594
|
if (value.length !== 3) break;
|
|
1355
|
-
let body = this.evaluateImpl(value[1], parent, property);
|
|
1595
|
+
let body = this.evaluateImpl(value[1], parent, property, depth + 1);
|
|
1356
1596
|
if (body === null || typeof body === "string" || body instanceof Uint8Array || body instanceof ReadableStream) {} else throw new TypeError("Response body must be of type ReadableStream.");
|
|
1357
1597
|
let init = value[2];
|
|
1358
1598
|
if (typeof init !== "object" || init === null) break;
|
|
@@ -1363,7 +1603,7 @@ var Evaluator = class Evaluator {
|
|
|
1363
1603
|
case "blob": {
|
|
1364
1604
|
if (value.length !== 3 || typeof value[1] !== "string") break;
|
|
1365
1605
|
let contentType = value[1];
|
|
1366
|
-
let content = this.evaluateImpl(value[2], parent, property);
|
|
1606
|
+
let content = this.evaluateImpl(value[2], parent, property, depth + 1);
|
|
1367
1607
|
if (!(content instanceof ReadableStream)) throw new TypeError("Blob content must be serialized as a ReadableStream.");
|
|
1368
1608
|
let promise = streamToBlobPromise(content, contentType);
|
|
1369
1609
|
this.promises.push({
|
|
@@ -1404,7 +1644,7 @@ var Evaluator = class Evaluator {
|
|
|
1404
1644
|
if (value.length == 3) return addStub(hook.get(path));
|
|
1405
1645
|
let args = value[3];
|
|
1406
1646
|
if (!(args instanceof Array)) break;
|
|
1407
|
-
args = new Evaluator(this.importer).
|
|
1647
|
+
args = new Evaluator(this.importer).evaluateWithDepth([args], depth);
|
|
1408
1648
|
return addStub(hook.call(path, args));
|
|
1409
1649
|
}
|
|
1410
1650
|
case "remap": {
|
|
@@ -1470,9 +1710,9 @@ var Evaluator = class Evaluator {
|
|
|
1470
1710
|
} else if (value instanceof Object) {
|
|
1471
1711
|
let result = value;
|
|
1472
1712
|
for (let key in result) if (key in Object.prototype || key === "toJSON") {
|
|
1473
|
-
this.evaluateImpl(result[key], result, key);
|
|
1713
|
+
this.evaluateImpl(result[key], result, key, depth + 1);
|
|
1474
1714
|
delete result[key];
|
|
1475
|
-
} else result[key] = this.evaluateImpl(result[key], result, key);
|
|
1715
|
+
} else result[key] = this.evaluateImpl(result[key], result, key, depth + 1);
|
|
1476
1716
|
return result;
|
|
1477
1717
|
} else return value;
|
|
1478
1718
|
}
|
|
@@ -1488,6 +1728,47 @@ function deserialize(value) {
|
|
|
1488
1728
|
|
|
1489
1729
|
//#endregion
|
|
1490
1730
|
//#region src/rpc.ts
|
|
1731
|
+
const ESTIMATED_OBJECT_OVERHEAD = 16;
|
|
1732
|
+
const ESTIMATED_ENTRY_OVERHEAD = 8;
|
|
1733
|
+
const ESTIMATED_BINARY_OVERHEAD = 16;
|
|
1734
|
+
const MAX_ESTIMATE_DEPTH = 64;
|
|
1735
|
+
function estimateStringSize(value) {
|
|
1736
|
+
return 2 + value.length * 3;
|
|
1737
|
+
}
|
|
1738
|
+
function estimateEncodedSize(value, seen, depth = 0) {
|
|
1739
|
+
if (depth >= MAX_ESTIMATE_DEPTH) return ESTIMATED_ENTRY_OVERHEAD;
|
|
1740
|
+
switch (typeof value) {
|
|
1741
|
+
case "string": return estimateStringSize(value);
|
|
1742
|
+
case "number": return 16;
|
|
1743
|
+
case "bigint": return 16;
|
|
1744
|
+
case "boolean": return 8;
|
|
1745
|
+
case "undefined": return 16;
|
|
1746
|
+
case "object": {
|
|
1747
|
+
if (value === null) return 8;
|
|
1748
|
+
if (ArrayBuffer.isView(value)) return ESTIMATED_BINARY_OVERHEAD + value.byteLength;
|
|
1749
|
+
if (value instanceof ArrayBuffer) return ESTIMATED_BINARY_OVERHEAD + value.byteLength;
|
|
1750
|
+
if (typeof Blob !== "undefined" && value instanceof Blob) return ESTIMATED_BINARY_OVERHEAD + value.size;
|
|
1751
|
+
if (value instanceof Date) return 16;
|
|
1752
|
+
seen ??= /* @__PURE__ */ new WeakSet();
|
|
1753
|
+
if (seen.has(value)) return ESTIMATED_ENTRY_OVERHEAD;
|
|
1754
|
+
seen.add(value);
|
|
1755
|
+
if (value instanceof Array) {
|
|
1756
|
+
let size = ESTIMATED_OBJECT_OVERHEAD;
|
|
1757
|
+
for (let item of value) size += ESTIMATED_ENTRY_OVERHEAD + estimateEncodedSize(item, seen, depth + 1);
|
|
1758
|
+
return size;
|
|
1759
|
+
}
|
|
1760
|
+
if (value instanceof Error) {
|
|
1761
|
+
let size = ESTIMATED_OBJECT_OVERHEAD + estimateStringSize(value.name) + estimateStringSize(value.message) + estimateStringSize(value.stack ?? "");
|
|
1762
|
+
for (let key of Object.keys(value)) size += ESTIMATED_ENTRY_OVERHEAD + estimateStringSize(key) + estimateEncodedSize(value[key], seen, depth + 1);
|
|
1763
|
+
return size;
|
|
1764
|
+
}
|
|
1765
|
+
let size = ESTIMATED_OBJECT_OVERHEAD;
|
|
1766
|
+
for (let key of Object.keys(value)) size += ESTIMATED_ENTRY_OVERHEAD + estimateStringSize(key) + estimateEncodedSize(value[key], seen, depth + 1);
|
|
1767
|
+
return size;
|
|
1768
|
+
}
|
|
1769
|
+
default: return 16;
|
|
1770
|
+
}
|
|
1771
|
+
}
|
|
1491
1772
|
var ImportTableEntry = class {
|
|
1492
1773
|
session;
|
|
1493
1774
|
importId;
|
|
@@ -1580,24 +1861,28 @@ var RpcImportHook = class RpcImportHook extends StubHook {
|
|
|
1580
1861
|
if (this.entry) return this.entry;
|
|
1581
1862
|
else throw new Error("This RpcImportHook was already disposed.");
|
|
1582
1863
|
}
|
|
1864
|
+
getEntryTakingOwnership(disposeOwned) {
|
|
1865
|
+
try {
|
|
1866
|
+
return this.getEntry();
|
|
1867
|
+
} catch (err) {
|
|
1868
|
+
disposeOwned();
|
|
1869
|
+
throw err;
|
|
1870
|
+
}
|
|
1871
|
+
}
|
|
1583
1872
|
call(path, args) {
|
|
1584
|
-
let entry = this.
|
|
1873
|
+
let entry = this.getEntryTakingOwnership(() => args.dispose());
|
|
1585
1874
|
if (entry.resolution) return entry.resolution.call(path, args);
|
|
1586
1875
|
else return entry.session.sendCall(entry.importId, path, args);
|
|
1587
1876
|
}
|
|
1588
1877
|
stream(path, args) {
|
|
1589
|
-
let entry = this.
|
|
1878
|
+
let entry = this.getEntryTakingOwnership(() => args.dispose());
|
|
1590
1879
|
if (entry.resolution) return entry.resolution.stream(path, args);
|
|
1591
1880
|
else return entry.session.sendStream(entry.importId, path, args);
|
|
1592
1881
|
}
|
|
1593
1882
|
map(path, captures, instructions) {
|
|
1594
|
-
let entry
|
|
1595
|
-
try {
|
|
1596
|
-
entry = this.getEntry();
|
|
1597
|
-
} catch (err) {
|
|
1883
|
+
let entry = this.getEntryTakingOwnership(() => {
|
|
1598
1884
|
for (let cap of captures) cap.dispose();
|
|
1599
|
-
|
|
1600
|
-
}
|
|
1885
|
+
});
|
|
1601
1886
|
if (entry.resolution) return entry.resolution.map(path, captures, instructions);
|
|
1602
1887
|
else return entry.session.sendMap(entry.importId, path, captures, instructions);
|
|
1603
1888
|
}
|
|
@@ -1653,9 +1938,24 @@ var RpcSessionImpl = class {
|
|
|
1653
1938
|
onBatchDone;
|
|
1654
1939
|
pullCount = 0;
|
|
1655
1940
|
onBrokenCallbacks = [];
|
|
1941
|
+
encodingLevel;
|
|
1942
|
+
limits;
|
|
1656
1943
|
constructor(transport, mainHook, options) {
|
|
1657
1944
|
this.transport = transport;
|
|
1658
1945
|
this.options = options;
|
|
1946
|
+
let level = "string";
|
|
1947
|
+
if ("encodingLevel" in transport) {
|
|
1948
|
+
let raw = transport.encodingLevel;
|
|
1949
|
+
if (raw !== void 0) {
|
|
1950
|
+
if (raw !== "string" && raw !== "jsonCompatible" && raw !== "jsonCompatibleWithBytes" && raw !== "structuredClonable") throw new TypeError(`Unknown transport encodingLevel: ${String(raw)}`);
|
|
1951
|
+
level = raw;
|
|
1952
|
+
}
|
|
1953
|
+
}
|
|
1954
|
+
this.encodingLevel = level;
|
|
1955
|
+
this.limits = {
|
|
1956
|
+
...DEFAULT_LIMITS,
|
|
1957
|
+
...options.limits
|
|
1958
|
+
};
|
|
1659
1959
|
this.exports.push({
|
|
1660
1960
|
hook: mainHook,
|
|
1661
1961
|
refcount: 1
|
|
@@ -1736,7 +2036,7 @@ var RpcSessionImpl = class {
|
|
|
1736
2036
|
let autoRelease = exp.autoRelease;
|
|
1737
2037
|
++this.pullCount;
|
|
1738
2038
|
exp.pull = resolve().then((payload) => {
|
|
1739
|
-
let value = Devaluator.devaluate(payload.value, void 0, this, payload);
|
|
2039
|
+
let value = Devaluator.devaluate(payload.value, void 0, this, payload, this.encodingLevel);
|
|
1740
2040
|
this.send([
|
|
1741
2041
|
"resolve",
|
|
1742
2042
|
exportId,
|
|
@@ -1747,7 +2047,7 @@ var RpcSessionImpl = class {
|
|
|
1747
2047
|
this.send([
|
|
1748
2048
|
"reject",
|
|
1749
2049
|
exportId,
|
|
1750
|
-
Devaluator.devaluate(error, void 0, this)
|
|
2050
|
+
Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)
|
|
1751
2051
|
]);
|
|
1752
2052
|
if (autoRelease) this.releaseExport(exportId, 1);
|
|
1753
2053
|
}).catch((error) => {
|
|
@@ -1755,7 +2055,7 @@ var RpcSessionImpl = class {
|
|
|
1755
2055
|
this.send([
|
|
1756
2056
|
"reject",
|
|
1757
2057
|
exportId,
|
|
1758
|
-
Devaluator.devaluate(error, void 0, this)
|
|
2058
|
+
Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)
|
|
1759
2059
|
]);
|
|
1760
2060
|
if (autoRelease) this.releaseExport(exportId, 1);
|
|
1761
2061
|
} catch (error2) {
|
|
@@ -1798,6 +2098,9 @@ var RpcSessionImpl = class {
|
|
|
1798
2098
|
entry.pipeReadable = void 0;
|
|
1799
2099
|
return readable;
|
|
1800
2100
|
}
|
|
2101
|
+
getLimits() {
|
|
2102
|
+
return this.limits;
|
|
2103
|
+
}
|
|
1801
2104
|
createPipe(readable, readableHook) {
|
|
1802
2105
|
if (this.abortReason) throw this.abortReason;
|
|
1803
2106
|
this.send(["pipe"]);
|
|
@@ -1811,27 +2114,52 @@ var RpcSessionImpl = class {
|
|
|
1811
2114
|
}
|
|
1812
2115
|
send(msg) {
|
|
1813
2116
|
if (this.abortReason !== void 0) return 0;
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
msgText = JSON.stringify(msg);
|
|
1817
|
-
} catch (err) {
|
|
2117
|
+
if (this.encodingLevel === "string") {
|
|
2118
|
+
let msgText;
|
|
1818
2119
|
try {
|
|
1819
|
-
|
|
1820
|
-
} catch (
|
|
1821
|
-
|
|
2120
|
+
msgText = JSON.stringify(msg);
|
|
2121
|
+
} catch (err) {
|
|
2122
|
+
try {
|
|
2123
|
+
this.abort(err);
|
|
2124
|
+
} catch (err2) {}
|
|
2125
|
+
throw err;
|
|
2126
|
+
}
|
|
2127
|
+
try {
|
|
2128
|
+
let sent = this.transport.send(msgText);
|
|
2129
|
+
if (sent !== void 0 && typeof sent.catch === "function") sent.catch((err) => this.abort(err, false));
|
|
2130
|
+
} catch (err) {
|
|
2131
|
+
queueMicrotask(() => this.abort(err, false));
|
|
2132
|
+
}
|
|
2133
|
+
return msgText.length;
|
|
2134
|
+
} else try {
|
|
2135
|
+
let size = this.transport.send(msg);
|
|
2136
|
+
if (typeof size === "number") return size;
|
|
2137
|
+
let thenable = size;
|
|
2138
|
+
if (thenable && typeof thenable.then === "function") Promise.resolve(thenable).catch((err) => this.abort(err, false));
|
|
2139
|
+
return;
|
|
2140
|
+
} catch (err) {
|
|
2141
|
+
queueMicrotask(() => this.abort(err, false));
|
|
2142
|
+
return;
|
|
1822
2143
|
}
|
|
1823
|
-
this.transport.send(msgText).catch((err) => this.abort(err, false));
|
|
1824
|
-
return msgText.length;
|
|
1825
2144
|
}
|
|
1826
2145
|
sendCall(id, path, args) {
|
|
1827
|
-
if (this.abortReason)
|
|
2146
|
+
if (this.abortReason) {
|
|
2147
|
+
args?.dispose();
|
|
2148
|
+
throw this.abortReason;
|
|
2149
|
+
}
|
|
1828
2150
|
let value = [
|
|
1829
2151
|
"pipeline",
|
|
1830
2152
|
id,
|
|
1831
2153
|
path
|
|
1832
2154
|
];
|
|
1833
2155
|
if (args) {
|
|
1834
|
-
let devalue
|
|
2156
|
+
let devalue;
|
|
2157
|
+
try {
|
|
2158
|
+
devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
|
|
2159
|
+
} catch (err) {
|
|
2160
|
+
args.dispose();
|
|
2161
|
+
throw err;
|
|
2162
|
+
}
|
|
1835
2163
|
value.push(devalue[0]);
|
|
1836
2164
|
}
|
|
1837
2165
|
this.send(["push", value]);
|
|
@@ -1840,15 +2168,26 @@ var RpcSessionImpl = class {
|
|
|
1840
2168
|
return new RpcImportHook(true, entry);
|
|
1841
2169
|
}
|
|
1842
2170
|
sendStream(id, path, args) {
|
|
1843
|
-
if (this.abortReason)
|
|
2171
|
+
if (this.abortReason) {
|
|
2172
|
+
args.dispose();
|
|
2173
|
+
throw this.abortReason;
|
|
2174
|
+
}
|
|
1844
2175
|
let value = [
|
|
1845
2176
|
"pipeline",
|
|
1846
2177
|
id,
|
|
1847
2178
|
path
|
|
1848
2179
|
];
|
|
1849
|
-
let devalue
|
|
2180
|
+
let devalue;
|
|
2181
|
+
try {
|
|
2182
|
+
devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
|
|
2183
|
+
} catch (err) {
|
|
2184
|
+
args.dispose();
|
|
2185
|
+
throw err;
|
|
2186
|
+
}
|
|
1850
2187
|
value.push(devalue[0]);
|
|
1851
|
-
let
|
|
2188
|
+
let msg = ["stream", value];
|
|
2189
|
+
let size = this.send(msg);
|
|
2190
|
+
if (size === void 0) size = estimateEncodedSize(msg);
|
|
1852
2191
|
let importId = this.imports.length;
|
|
1853
2192
|
let entry = new ImportTableEntry(this, importId, true);
|
|
1854
2193
|
entry.remoteRefcount = 0;
|
|
@@ -1904,7 +2243,14 @@ var RpcSessionImpl = class {
|
|
|
1904
2243
|
this.cancelReadLoop?.(error);
|
|
1905
2244
|
this.cancelReadLoop = void 0;
|
|
1906
2245
|
if (trySendAbortMessage) try {
|
|
1907
|
-
|
|
2246
|
+
let abortMsg = ["abort", Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)];
|
|
2247
|
+
if (this.encodingLevel === "string") {
|
|
2248
|
+
let sent = this.transport.send(JSON.stringify(abortMsg));
|
|
2249
|
+
if (sent !== void 0 && typeof sent.catch === "function") sent.catch((err) => {});
|
|
2250
|
+
} else {
|
|
2251
|
+
let result = this.transport.send(abortMsg);
|
|
2252
|
+
if (result && typeof result.then === "function") Promise.resolve(result).catch((err) => {});
|
|
2253
|
+
}
|
|
1908
2254
|
} catch (err) {}
|
|
1909
2255
|
if (error === void 0) error = "undefined";
|
|
1910
2256
|
this.abortReason = error;
|
|
@@ -1926,18 +2272,19 @@ var RpcSessionImpl = class {
|
|
|
1926
2272
|
while (!this.abortReason) {
|
|
1927
2273
|
let readCanceled = Promise.withResolvers();
|
|
1928
2274
|
this.cancelReadLoop = readCanceled.reject;
|
|
1929
|
-
let
|
|
2275
|
+
let raw;
|
|
1930
2276
|
try {
|
|
1931
|
-
|
|
2277
|
+
raw = await Promise.race([this.transport.receive(), readCanceled.promise]);
|
|
1932
2278
|
} finally {
|
|
1933
2279
|
if (this.cancelReadLoop === readCanceled.reject) this.cancelReadLoop = void 0;
|
|
1934
2280
|
}
|
|
1935
|
-
|
|
2281
|
+
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
2282
|
if (this.abortReason) break;
|
|
2283
|
+
let msg = this.encodingLevel === "string" ? JSON.parse(raw) : raw;
|
|
1937
2284
|
if (msg instanceof Array) switch (msg[0]) {
|
|
1938
2285
|
case "push":
|
|
1939
2286
|
if (msg.length > 1) {
|
|
1940
|
-
let hook = new PayloadStubHook(new Evaluator(this).evaluate(msg[1]));
|
|
2287
|
+
let hook = new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[1]));
|
|
1941
2288
|
hook.ignoreUnhandledRejections();
|
|
1942
2289
|
this.exports.push({
|
|
1943
2290
|
hook,
|
|
@@ -1948,7 +2295,7 @@ var RpcSessionImpl = class {
|
|
|
1948
2295
|
break;
|
|
1949
2296
|
case "stream":
|
|
1950
2297
|
if (msg.length > 1) {
|
|
1951
|
-
let hook = new PayloadStubHook(new Evaluator(this).evaluate(msg[1]));
|
|
2298
|
+
let hook = new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[1]));
|
|
1952
2299
|
hook.ignoreUnhandledRejections();
|
|
1953
2300
|
let exportId = this.exports.length;
|
|
1954
2301
|
this.exports.push({
|
|
@@ -1983,13 +2330,13 @@ var RpcSessionImpl = class {
|
|
|
1983
2330
|
let importId = msg[1];
|
|
1984
2331
|
if (typeof importId == "number" && msg.length > 2) {
|
|
1985
2332
|
let imp = this.imports[importId];
|
|
1986
|
-
if (imp) if (msg[0] == "resolve") imp.resolve(new PayloadStubHook(new Evaluator(this).evaluate(msg[2])));
|
|
2333
|
+
if (imp) if (msg[0] == "resolve") imp.resolve(new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[2])));
|
|
1987
2334
|
else {
|
|
1988
|
-
let payload = new Evaluator(this).evaluate(msg[2]);
|
|
2335
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[2]);
|
|
1989
2336
|
payload.dispose();
|
|
1990
2337
|
imp.resolve(new ErrorStubHook(payload.value));
|
|
1991
2338
|
}
|
|
1992
|
-
else if (msg[0] == "resolve") new Evaluator(this).evaluate(msg[2]).dispose();
|
|
2339
|
+
else if (msg[0] == "resolve") new Evaluator(this, this.encodingLevel).evaluate(msg[2]).dispose();
|
|
1993
2340
|
continue;
|
|
1994
2341
|
}
|
|
1995
2342
|
break;
|
|
@@ -2004,9 +2351,9 @@ var RpcSessionImpl = class {
|
|
|
2004
2351
|
break;
|
|
2005
2352
|
}
|
|
2006
2353
|
case "abort": {
|
|
2007
|
-
let payload = new Evaluator(this).evaluate(msg[1]);
|
|
2354
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[1]);
|
|
2008
2355
|
payload.dispose();
|
|
2009
|
-
this.abort(payload, false);
|
|
2356
|
+
this.abort(payload.value, false);
|
|
2010
2357
|
break;
|
|
2011
2358
|
}
|
|
2012
2359
|
}
|
|
@@ -2057,6 +2404,8 @@ var RpcSession$1 = class {
|
|
|
2057
2404
|
|
|
2058
2405
|
//#endregion
|
|
2059
2406
|
//#region src/websocket.ts
|
|
2407
|
+
/** Close-frame reason max UTF-8 bytes: 125 payload - 2-byte status (RFC 6455 section 5.5). */
|
|
2408
|
+
const MAX_CLOSE_REASON_BYTES = 123;
|
|
2060
2409
|
function newWebSocketRpcSession$1(webSocket, localMain, options) {
|
|
2061
2410
|
if (typeof webSocket === "string") webSocket = new WebSocket(webSocket);
|
|
2062
2411
|
return new RpcSession$1(new WebSocketTransport(webSocket), localMain, options).getRemoteMain();
|
|
@@ -2076,9 +2425,14 @@ function newWorkersWebSocketRpcResponse(request, localMain, options) {
|
|
|
2076
2425
|
webSocket: pair[1]
|
|
2077
2426
|
});
|
|
2078
2427
|
}
|
|
2428
|
+
/**
|
|
2429
|
+
* Generic WebSocket transport. Default `T = string` is backward-compatible and satisfies
|
|
2430
|
+
* `RpcTransport`. Use `T = ArrayBuffer` as a building block for binary transports.
|
|
2431
|
+
*/
|
|
2079
2432
|
var WebSocketTransport = class {
|
|
2080
2433
|
constructor(webSocket) {
|
|
2081
2434
|
this.#webSocket = webSocket;
|
|
2435
|
+
webSocket.binaryType = "arraybuffer";
|
|
2082
2436
|
if (webSocket.readyState === WebSocket.CONNECTING) {
|
|
2083
2437
|
this.#sendQueue = [];
|
|
2084
2438
|
webSocket.addEventListener("open", (event) => {
|
|
@@ -2091,12 +2445,12 @@ var WebSocketTransport = class {
|
|
|
2091
2445
|
});
|
|
2092
2446
|
}
|
|
2093
2447
|
webSocket.addEventListener("message", (event) => {
|
|
2094
|
-
if (this.#error) {} else if (typeof event.data === "string") if (this.#receiveResolver) {
|
|
2448
|
+
if (this.#error) {} else if (typeof event.data === "string" || event.data instanceof ArrayBuffer) if (this.#receiveResolver) {
|
|
2095
2449
|
this.#receiveResolver(event.data);
|
|
2096
2450
|
this.#receiveResolver = void 0;
|
|
2097
2451
|
this.#receiveRejecter = void 0;
|
|
2098
2452
|
} else this.#receiveQueue.push(event.data);
|
|
2099
|
-
else this.#receivedError(/* @__PURE__ */ new TypeError("Received
|
|
2453
|
+
else this.#receivedError(/* @__PURE__ */ new TypeError("Received unexpected message type from WebSocket."));
|
|
2100
2454
|
});
|
|
2101
2455
|
webSocket.addEventListener("close", (event) => {
|
|
2102
2456
|
this.#receivedError(/* @__PURE__ */ new Error(`Peer closed WebSocket: ${event.code} ${event.reason}`));
|
|
@@ -2111,13 +2465,13 @@ var WebSocketTransport = class {
|
|
|
2111
2465
|
#receiveRejecter;
|
|
2112
2466
|
#receiveQueue = [];
|
|
2113
2467
|
#error;
|
|
2114
|
-
|
|
2468
|
+
send(message) {
|
|
2115
2469
|
if (this.#sendQueue === void 0) this.#webSocket.send(message);
|
|
2116
2470
|
else this.#sendQueue.push(message);
|
|
2117
2471
|
}
|
|
2118
|
-
|
|
2119
|
-
if (this.#receiveQueue.length > 0) return this.#receiveQueue.shift();
|
|
2120
|
-
else if (this.#error)
|
|
2472
|
+
receive() {
|
|
2473
|
+
if (this.#receiveQueue.length > 0) return Promise.resolve(this.#receiveQueue.shift());
|
|
2474
|
+
else if (this.#error) return Promise.reject(this.#error);
|
|
2121
2475
|
else return new Promise((resolve, reject) => {
|
|
2122
2476
|
this.#receiveResolver = resolve;
|
|
2123
2477
|
this.#receiveRejecter = reject;
|
|
@@ -2127,6 +2481,8 @@ var WebSocketTransport = class {
|
|
|
2127
2481
|
let message;
|
|
2128
2482
|
if (reason instanceof Error) message = reason.message;
|
|
2129
2483
|
else message = `${reason}`;
|
|
2484
|
+
let reasonBytes = new TextEncoder().encode(message);
|
|
2485
|
+
if (reasonBytes.length > 123) message = new TextDecoder().decode(reasonBytes.subarray(0, 123), { stream: true });
|
|
2130
2486
|
this.#webSocket.close(3e3, message);
|
|
2131
2487
|
if (!this.#error) this.#error = reason;
|
|
2132
2488
|
}
|
|
@@ -2144,6 +2500,7 @@ var WebSocketTransport = class {
|
|
|
2144
2500
|
|
|
2145
2501
|
//#endregion
|
|
2146
2502
|
//#region src/batch.ts
|
|
2503
|
+
const yieldToMacrotask = typeof setImmediate === "function" ? () => new Promise((resolve) => setImmediate(resolve)) : () => new Promise((resolve) => setTimeout(resolve, 0));
|
|
2147
2504
|
var BatchClientTransport = class {
|
|
2148
2505
|
constructor(sendBatch) {
|
|
2149
2506
|
this.#promise = this.#scheduleBatch(sendBatch);
|
|
@@ -2152,7 +2509,7 @@ var BatchClientTransport = class {
|
|
|
2152
2509
|
#aborted;
|
|
2153
2510
|
#batchToSend = [];
|
|
2154
2511
|
#batchToReceive = null;
|
|
2155
|
-
|
|
2512
|
+
send(message) {
|
|
2156
2513
|
if (this.#batchToSend !== null) this.#batchToSend.push(message);
|
|
2157
2514
|
}
|
|
2158
2515
|
async receive() {
|
|
@@ -2165,7 +2522,7 @@ var BatchClientTransport = class {
|
|
|
2165
2522
|
this.#aborted = reason;
|
|
2166
2523
|
}
|
|
2167
2524
|
async #scheduleBatch(sendBatch) {
|
|
2168
|
-
await
|
|
2525
|
+
await yieldToMacrotask();
|
|
2169
2526
|
if (this.#aborted !== void 0) throw this.#aborted;
|
|
2170
2527
|
let batch = this.#batchToSend;
|
|
2171
2528
|
this.#batchToSend = null;
|
|
@@ -2194,7 +2551,7 @@ var BatchServerTransport = class {
|
|
|
2194
2551
|
#batchToSend = [];
|
|
2195
2552
|
#batchToReceive;
|
|
2196
2553
|
#allReceived = Promise.withResolvers();
|
|
2197
|
-
|
|
2554
|
+
send(message) {
|
|
2198
2555
|
this.#batchToSend.push(message);
|
|
2199
2556
|
}
|
|
2200
2557
|
async receive() {
|
|
@@ -2243,7 +2600,11 @@ async function newHttpBatchRpcResponse(request, localMain, options) {
|
|
|
2243
2600
|
* @param options Optional RPC session options. You can also pass headers to set on the response.
|
|
2244
2601
|
*/
|
|
2245
2602
|
async function nodeHttpBatchRpcResponse(request, response, localMain, options) {
|
|
2246
|
-
if (request.method !== "POST")
|
|
2603
|
+
if (request.method !== "POST") {
|
|
2604
|
+
response.writeHead(405, "This endpoint only accepts POST requests.", options?.headers);
|
|
2605
|
+
response.end();
|
|
2606
|
+
return;
|
|
2607
|
+
}
|
|
2247
2608
|
let body = await new Promise((resolve, reject) => {
|
|
2248
2609
|
let chunks = [];
|
|
2249
2610
|
request.on("data", (chunk) => {
|
|
@@ -2268,17 +2629,17 @@ function newMessagePortRpcSession$1(port, localMain, options) {
|
|
|
2268
2629
|
return new RpcSession$1(new MessagePortTransport(port), localMain, options).getRemoteMain();
|
|
2269
2630
|
}
|
|
2270
2631
|
var MessagePortTransport = class {
|
|
2632
|
+
encodingLevel = "structuredClonable";
|
|
2271
2633
|
constructor(port) {
|
|
2272
2634
|
this.#port = port;
|
|
2273
2635
|
port.start();
|
|
2274
2636
|
port.addEventListener("message", (event) => {
|
|
2275
2637
|
if (this.#error) {} else if (event.data === null) this.#receivedError(/* @__PURE__ */ new Error("Peer closed MessagePort connection."));
|
|
2276
|
-
else if (
|
|
2638
|
+
else if (this.#receiveResolver) {
|
|
2277
2639
|
this.#receiveResolver(event.data);
|
|
2278
2640
|
this.#receiveResolver = void 0;
|
|
2279
2641
|
this.#receiveRejecter = void 0;
|
|
2280
2642
|
} else this.#receiveQueue.push(event.data);
|
|
2281
|
-
else this.#receivedError(/* @__PURE__ */ new TypeError("Received non-string message from MessagePort."));
|
|
2282
2643
|
});
|
|
2283
2644
|
port.addEventListener("messageerror", (event) => {
|
|
2284
2645
|
this.#receivedError(/* @__PURE__ */ new Error("MessagePort message error."));
|
|
@@ -2289,7 +2650,7 @@ var MessagePortTransport = class {
|
|
|
2289
2650
|
#receiveRejecter;
|
|
2290
2651
|
#receiveQueue = [];
|
|
2291
2652
|
#error;
|
|
2292
|
-
|
|
2653
|
+
send(message) {
|
|
2293
2654
|
if (this.#error) throw this.#error;
|
|
2294
2655
|
this.#port.postMessage(message);
|
|
2295
2656
|
}
|
|
@@ -2453,9 +2814,11 @@ var MapVariableHook = class extends StubHook {
|
|
|
2453
2814
|
else throwMapperBuilderUseError();
|
|
2454
2815
|
}
|
|
2455
2816
|
call(path, args) {
|
|
2817
|
+
args.dispose();
|
|
2456
2818
|
throwMapperBuilderUseError();
|
|
2457
2819
|
}
|
|
2458
2820
|
map(path, captures, instructions) {
|
|
2821
|
+
for (let cap of captures) cap.dispose();
|
|
2459
2822
|
throwMapperBuilderUseError();
|
|
2460
2823
|
}
|
|
2461
2824
|
pull() {
|
|
@@ -2508,6 +2871,9 @@ var MapApplicator = class {
|
|
|
2508
2871
|
getPipeReadable(exportId) {
|
|
2509
2872
|
throw new Error("A mapper function cannot use pipe readables.");
|
|
2510
2873
|
}
|
|
2874
|
+
getLimits() {
|
|
2875
|
+
return DEFAULT_LIMITS;
|
|
2876
|
+
}
|
|
2511
2877
|
};
|
|
2512
2878
|
function applyMapToElement(input, parent, owner, captures, instructions) {
|
|
2513
2879
|
let mapper = new MapApplicator(captures, new PayloadStubHook(RpcPayload.deepCopyFrom(input, parent, owner)));
|
|
@@ -2563,14 +2929,11 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
|
|
|
2563
2929
|
let state = this.getState();
|
|
2564
2930
|
if (path.length !== 1 || typeof path[0] !== "string") throw new Error("WritableStream stub only supports direct method calls");
|
|
2565
2931
|
const method = path[0];
|
|
2566
|
-
if (method !== "write" && method !== "close" && method !== "abort") {
|
|
2567
|
-
args.dispose();
|
|
2568
|
-
throw new Error(`Unknown WritableStream method: ${method}`);
|
|
2569
|
-
}
|
|
2932
|
+
if (method !== "write" && method !== "close" && method !== "abort") throw new Error(`Unknown WritableStream method: ${method}`);
|
|
2570
2933
|
if (method === "close" || method === "abort") state.closed = true;
|
|
2571
|
-
|
|
2572
|
-
return new PromiseStubHook(args.deliverCall(func, state.writer).then((payload) => new PayloadStubHook(payload)));
|
|
2934
|
+
return new PromiseStubHook((method === "write" ? args.deliverStreamWrite(state.writer) : args.deliverCall(state.writer[method], state.writer)).then((payload) => new PayloadStubHook(payload)));
|
|
2573
2935
|
} catch (err) {
|
|
2936
|
+
args.dispose();
|
|
2574
2937
|
return new ErrorStubHook(err);
|
|
2575
2938
|
}
|
|
2576
2939
|
}
|
|
@@ -2846,20 +3209,23 @@ let newMessagePortRpcSession = newMessagePortRpcSession$1;
|
|
|
2846
3209
|
* credentials as parameters and returns the authorized API), then cross-origin requests should
|
|
2847
3210
|
* be safe.
|
|
2848
3211
|
*/
|
|
2849
|
-
async function newWorkersRpcResponse(request, localMain) {
|
|
3212
|
+
async function newWorkersRpcResponse(request, localMain, options) {
|
|
2850
3213
|
if (request.method === "POST") {
|
|
2851
|
-
let response = await newHttpBatchRpcResponse(request, localMain);
|
|
3214
|
+
let response = await newHttpBatchRpcResponse(request, localMain, options);
|
|
2852
3215
|
response.headers.set("Access-Control-Allow-Origin", "*");
|
|
2853
3216
|
return response;
|
|
2854
|
-
} else if (request.headers.get("Upgrade")?.toLowerCase() === "websocket") return newWorkersWebSocketRpcResponse(request, localMain);
|
|
3217
|
+
} else if (request.headers.get("Upgrade")?.toLowerCase() === "websocket") return newWorkersWebSocketRpcResponse(request, localMain, options);
|
|
2855
3218
|
else return new Response("This endpoint only accepts POST or WebSocket requests.", { status: 400 });
|
|
2856
3219
|
}
|
|
2857
3220
|
|
|
2858
3221
|
//#endregion
|
|
3222
|
+
exports.DEFAULT_LIMITS = DEFAULT_LIMITS;
|
|
3223
|
+
exports.DEFAULT_MAX_DEPTH = DEFAULT_MAX_DEPTH;
|
|
2859
3224
|
exports.RpcPromise = RpcPromise;
|
|
2860
3225
|
exports.RpcSession = RpcSession;
|
|
2861
3226
|
exports.RpcStub = RpcStub;
|
|
2862
3227
|
exports.RpcTarget = RpcTarget;
|
|
3228
|
+
exports.WebSocketTransport = WebSocketTransport;
|
|
2863
3229
|
exports.deserialize = deserialize;
|
|
2864
3230
|
exports.newHttpBatchRpcResponse = newHttpBatchRpcResponse;
|
|
2865
3231
|
exports.newHttpBatchRpcSession = newHttpBatchRpcSession;
|