capnweb 0.0.0-2cb51eb → 0.0.0-2de5871
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 +45 -2
- package/dist/index-bun.cjs +448 -106
- package/dist/index-bun.cjs.map +1 -1
- package/dist/index-bun.d.cts +115 -12
- package/dist/index-bun.d.ts +115 -12
- package/dist/index-bun.js +446 -107
- package/dist/index-bun.js.map +1 -1
- package/dist/index-workers.cjs +448 -106
- package/dist/index-workers.cjs.map +1 -1
- package/dist/index-workers.d.cts +112 -12
- package/dist/index-workers.d.ts +112 -12
- package/dist/index-workers.js +446 -107
- package/dist/index-workers.js.map +1 -1
- package/dist/index.cjs +448 -106
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +112 -12
- package/dist/index.d.ts +112 -12
- package/dist/index.js +446 -107
- 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) {
|
|
@@ -413,6 +435,7 @@ var RpcPayload = class RpcPayload {
|
|
|
413
435
|
case "date":
|
|
414
436
|
case "bytes":
|
|
415
437
|
case "blob":
|
|
438
|
+
case "url":
|
|
416
439
|
case "error":
|
|
417
440
|
case "undefined": return value;
|
|
418
441
|
case "array": {
|
|
@@ -546,6 +569,30 @@ var RpcPayload = class RpcPayload {
|
|
|
546
569
|
this.dispose();
|
|
547
570
|
}
|
|
548
571
|
}
|
|
572
|
+
async deliverStreamWrite(writer) {
|
|
573
|
+
try {
|
|
574
|
+
let promises = [];
|
|
575
|
+
this.deliverTo(this, "value", promises);
|
|
576
|
+
if (promises.length > 0) await Promise.all(promises);
|
|
577
|
+
let chunk = this.value[0];
|
|
578
|
+
if ((this.hooks.length > 0 || this.promises.length > 0) && chunk instanceof Object) {
|
|
579
|
+
if (!(Symbol.dispose in chunk)) Object.defineProperty(chunk, Symbol.dispose, {
|
|
580
|
+
value: () => this.dispose(),
|
|
581
|
+
writable: true,
|
|
582
|
+
enumerable: false,
|
|
583
|
+
configurable: true
|
|
584
|
+
});
|
|
585
|
+
await writer.write(chunk);
|
|
586
|
+
return RpcPayload.fromAppReturn(void 0);
|
|
587
|
+
}
|
|
588
|
+
await writer.write(chunk);
|
|
589
|
+
this.dispose();
|
|
590
|
+
return RpcPayload.fromAppReturn(void 0);
|
|
591
|
+
} catch (err) {
|
|
592
|
+
this.dispose();
|
|
593
|
+
throw err;
|
|
594
|
+
}
|
|
595
|
+
}
|
|
549
596
|
async deliverResolve() {
|
|
550
597
|
try {
|
|
551
598
|
let promises = [];
|
|
@@ -586,6 +633,7 @@ var RpcPayload = class RpcPayload {
|
|
|
586
633
|
case "bytes":
|
|
587
634
|
case "blob":
|
|
588
635
|
case "date":
|
|
636
|
+
case "url":
|
|
589
637
|
case "error":
|
|
590
638
|
case "undefined": return;
|
|
591
639
|
case "array": {
|
|
@@ -668,6 +716,7 @@ var RpcPayload = class RpcPayload {
|
|
|
668
716
|
case "rpc-target":
|
|
669
717
|
case "writable":
|
|
670
718
|
case "readable":
|
|
719
|
+
case "url":
|
|
671
720
|
case "headers":
|
|
672
721
|
case "request":
|
|
673
722
|
case "response": return;
|
|
@@ -737,6 +786,7 @@ function followPath(value, parent, path, owner) {
|
|
|
737
786
|
case "blob":
|
|
738
787
|
case "date":
|
|
739
788
|
case "error":
|
|
789
|
+
case "url":
|
|
740
790
|
case "headers":
|
|
741
791
|
case "request":
|
|
742
792
|
case "response":
|
|
@@ -769,17 +819,22 @@ function followPath(value, parent, path, owner) {
|
|
|
769
819
|
}
|
|
770
820
|
var ValueStubHook = class extends StubHook {
|
|
771
821
|
call(path, args) {
|
|
822
|
+
let followResult;
|
|
772
823
|
try {
|
|
773
824
|
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
|
-
}));
|
|
825
|
+
followResult = followPath(value, void 0, path, owner);
|
|
780
826
|
} catch (err) {
|
|
827
|
+
args.dispose();
|
|
781
828
|
return new ErrorStubHook(err);
|
|
782
829
|
}
|
|
830
|
+
if (followResult.hook) return followResult.hook.call(followResult.remainingPath, args);
|
|
831
|
+
if (typeof followResult.value != "function") {
|
|
832
|
+
args.dispose();
|
|
833
|
+
return new ErrorStubHook(/* @__PURE__ */ new TypeError(`'${path.join(".")}' is not a function.`));
|
|
834
|
+
}
|
|
835
|
+
return new PromiseStubHook(args.deliverCall(followResult.value, followResult.parent).then((payload) => {
|
|
836
|
+
return new PayloadStubHook(payload);
|
|
837
|
+
}));
|
|
783
838
|
}
|
|
784
839
|
map(path, captures, instructions) {
|
|
785
840
|
try {
|
|
@@ -917,12 +972,16 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
|
917
972
|
}
|
|
918
973
|
call(path, args) {
|
|
919
974
|
args.ensureDeepCopied();
|
|
920
|
-
return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args))
|
|
975
|
+
return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args), (err) => {
|
|
976
|
+
args.dispose();
|
|
977
|
+
throw err;
|
|
978
|
+
}));
|
|
921
979
|
}
|
|
922
980
|
stream(path, args) {
|
|
923
981
|
args.ensureDeepCopied();
|
|
924
|
-
return { promise: this.promise.then((hook) => {
|
|
925
|
-
|
|
982
|
+
return { promise: this.promise.then((hook) => hook.stream(path, args).promise, (err) => {
|
|
983
|
+
args.dispose();
|
|
984
|
+
throw err;
|
|
926
985
|
}) };
|
|
927
986
|
}
|
|
928
987
|
map(path, captures, instructions) {
|
|
@@ -949,10 +1008,7 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
|
949
1008
|
}, (err) => {});
|
|
950
1009
|
}
|
|
951
1010
|
dispose() {
|
|
952
|
-
|
|
953
|
-
else this.promise.then((hook) => {
|
|
954
|
-
hook.dispose();
|
|
955
|
-
}, (err) => {});
|
|
1011
|
+
this.promise.then((hook) => hook.dispose(), () => {});
|
|
956
1012
|
}
|
|
957
1013
|
onBroken(callback) {
|
|
958
1014
|
if (this.resolution) this.resolution.onBroken(callback);
|
|
@@ -964,6 +1020,77 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
|
964
1020
|
|
|
965
1021
|
//#endregion
|
|
966
1022
|
//#region src/serialize.ts
|
|
1023
|
+
const DEFAULT_MAX_DEPTH = 256;
|
|
1024
|
+
const DEFAULT_LIMITS = {
|
|
1025
|
+
maxBigIntDigits: 16384,
|
|
1026
|
+
maxDepth: 256,
|
|
1027
|
+
maxMessageSize: 32 * 1024 * 1024
|
|
1028
|
+
};
|
|
1029
|
+
const NATIVE_LITTLE_ENDIAN = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
|
|
1030
|
+
const BYTE_CONTAINER_TYPE_NAMES = [
|
|
1031
|
+
"ArrayBuffer",
|
|
1032
|
+
"DataView",
|
|
1033
|
+
"Int8Array",
|
|
1034
|
+
"Uint8Array",
|
|
1035
|
+
"Uint8ClampedArray",
|
|
1036
|
+
"Int16Array",
|
|
1037
|
+
"Uint16Array",
|
|
1038
|
+
"Int32Array",
|
|
1039
|
+
"Uint32Array",
|
|
1040
|
+
"BigInt64Array",
|
|
1041
|
+
"BigUint64Array",
|
|
1042
|
+
"Float32Array",
|
|
1043
|
+
"Float64Array"
|
|
1044
|
+
];
|
|
1045
|
+
function isValidByteContainerName(value) {
|
|
1046
|
+
return BYTE_CONTAINER_TYPE_NAMES.includes(value);
|
|
1047
|
+
}
|
|
1048
|
+
const TYPED_ARRAY_ELEMENT_SIZE = {
|
|
1049
|
+
ArrayBuffer: void 0,
|
|
1050
|
+
DataView: void 0,
|
|
1051
|
+
Int8Array: void 0,
|
|
1052
|
+
Uint8Array: void 0,
|
|
1053
|
+
Uint8ClampedArray: void 0,
|
|
1054
|
+
Int16Array: 2,
|
|
1055
|
+
Uint16Array: 2,
|
|
1056
|
+
Int32Array: 4,
|
|
1057
|
+
Uint32Array: 4,
|
|
1058
|
+
BigInt64Array: 8,
|
|
1059
|
+
BigUint64Array: 8,
|
|
1060
|
+
Float32Array: 4,
|
|
1061
|
+
Float64Array: 8
|
|
1062
|
+
};
|
|
1063
|
+
const BYTE_CONTAINER_PROTOTYPES = {
|
|
1064
|
+
ArrayBuffer: ArrayBuffer.prototype,
|
|
1065
|
+
DataView: DataView.prototype,
|
|
1066
|
+
Int8Array: Int8Array.prototype,
|
|
1067
|
+
Uint8ClampedArray: Uint8ClampedArray.prototype,
|
|
1068
|
+
Int16Array: Int16Array.prototype,
|
|
1069
|
+
Uint16Array: Uint16Array.prototype,
|
|
1070
|
+
Int32Array: Int32Array.prototype,
|
|
1071
|
+
Uint32Array: Uint32Array.prototype,
|
|
1072
|
+
BigInt64Array: BigInt64Array.prototype,
|
|
1073
|
+
BigUint64Array: BigUint64Array.prototype,
|
|
1074
|
+
Float32Array: Float32Array.prototype,
|
|
1075
|
+
Float64Array: Float64Array.prototype
|
|
1076
|
+
};
|
|
1077
|
+
const BYTE_CONTAINER_TYPE_BY_PROTOTYPE = /* @__PURE__ */ new Map();
|
|
1078
|
+
for (let type of Object.keys(BYTE_CONTAINER_PROTOTYPES)) BYTE_CONTAINER_TYPE_BY_PROTOTYPE.set(BYTE_CONTAINER_PROTOTYPES[type], type);
|
|
1079
|
+
function swapByteOrder(bytes, elementSize) {
|
|
1080
|
+
if (elementSize !== 2 && elementSize !== 4 && elementSize !== 8) throw new RangeError(`Unsupported element size: ${elementSize}`);
|
|
1081
|
+
let view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
1082
|
+
for (let offset = 0; offset < bytes.byteLength; offset += elementSize) switch (elementSize) {
|
|
1083
|
+
case 2:
|
|
1084
|
+
view.setUint16(offset, view.getUint16(offset, false), true);
|
|
1085
|
+
break;
|
|
1086
|
+
case 4:
|
|
1087
|
+
view.setUint32(offset, view.getUint32(offset, false), true);
|
|
1088
|
+
break;
|
|
1089
|
+
case 8:
|
|
1090
|
+
view.setBigUint64(offset, view.getBigUint64(offset, false), true);
|
|
1091
|
+
break;
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
967
1094
|
var NullExporter = class {
|
|
968
1095
|
exportStub(stub) {
|
|
969
1096
|
throw new Error("Cannot serialize RPC stubs without an RPC session.");
|
|
@@ -984,6 +1111,7 @@ async function streamToBlob(stream, type) {
|
|
|
984
1111
|
return b.type === type ? b : b.slice(0, b.size, type);
|
|
985
1112
|
}
|
|
986
1113
|
const ERROR_TYPES = {
|
|
1114
|
+
__proto__: null,
|
|
987
1115
|
Error,
|
|
988
1116
|
EvalError,
|
|
989
1117
|
RangeError,
|
|
@@ -996,12 +1124,14 @@ const ERROR_TYPES = {
|
|
|
996
1124
|
var Devaluator = class Devaluator {
|
|
997
1125
|
exporter;
|
|
998
1126
|
source;
|
|
999
|
-
|
|
1127
|
+
encodingLevel;
|
|
1128
|
+
constructor(exporter, source, encodingLevel) {
|
|
1000
1129
|
this.exporter = exporter;
|
|
1001
1130
|
this.source = source;
|
|
1131
|
+
this.encodingLevel = encodingLevel;
|
|
1002
1132
|
}
|
|
1003
|
-
static devaluate(value, parent, exporter = NULL_EXPORTER, source) {
|
|
1004
|
-
let devaluator = new Devaluator(exporter, source);
|
|
1133
|
+
static devaluate(value, parent, exporter = NULL_EXPORTER, source, encodingLevel = "string") {
|
|
1134
|
+
let devaluator = new Devaluator(exporter, source, encodingLevel);
|
|
1005
1135
|
try {
|
|
1006
1136
|
return devaluator.devaluateImpl(value, parent, 0);
|
|
1007
1137
|
} catch (err) {
|
|
@@ -1013,7 +1143,7 @@ var Devaluator = class Devaluator {
|
|
|
1013
1143
|
}
|
|
1014
1144
|
exports;
|
|
1015
1145
|
devaluateImpl(value, parent, depth) {
|
|
1016
|
-
if (depth >=
|
|
1146
|
+
if (depth >= 256) throw new Error("Serialization exceeded maximum allowed depth. (Does the message contain cycles?)");
|
|
1017
1147
|
switch (typeForRpc(value)) {
|
|
1018
1148
|
case "unsupported": {
|
|
1019
1149
|
let msg;
|
|
@@ -1024,10 +1154,12 @@ var Devaluator = class Devaluator {
|
|
|
1024
1154
|
}
|
|
1025
1155
|
throw new TypeError(msg);
|
|
1026
1156
|
}
|
|
1027
|
-
case "primitive": if (typeof value === "number" && !isFinite(value))
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1157
|
+
case "primitive": if (typeof value === "number" && !isFinite(value)) {
|
|
1158
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1159
|
+
if (value === Infinity) return ["inf"];
|
|
1160
|
+
else if (value === -Infinity) return ["-inf"];
|
|
1161
|
+
else return ["nan"];
|
|
1162
|
+
} else return value;
|
|
1031
1163
|
case "object": {
|
|
1032
1164
|
let object = value;
|
|
1033
1165
|
let result = {};
|
|
@@ -1041,23 +1173,49 @@ var Devaluator = class Devaluator {
|
|
|
1041
1173
|
for (let i = 0; i < len; i++) result[i] = this.devaluateImpl(array[i], array, depth + 1);
|
|
1042
1174
|
return [result];
|
|
1043
1175
|
}
|
|
1044
|
-
case "bigint":
|
|
1176
|
+
case "bigint":
|
|
1177
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1178
|
+
return ["bigint", value.toString()];
|
|
1045
1179
|
case "date": {
|
|
1180
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1046
1181
|
const time = value.getTime();
|
|
1047
1182
|
return ["date", Number.isNaN(time) ? null : time];
|
|
1048
1183
|
}
|
|
1049
1184
|
case "bytes": {
|
|
1050
|
-
let
|
|
1051
|
-
|
|
1185
|
+
let alternateTypeName = BYTE_CONTAINER_TYPE_BY_PROTOTYPE.get(Object.getPrototypeOf(value));
|
|
1186
|
+
let bytes;
|
|
1187
|
+
if (alternateTypeName === "ArrayBuffer") bytes = new Uint8Array(value);
|
|
1188
|
+
else if (alternateTypeName === void 0) bytes = value;
|
|
1189
|
+
else {
|
|
1190
|
+
let view = value;
|
|
1191
|
+
bytes = new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
|
|
1192
|
+
let elementSize = TYPED_ARRAY_ELEMENT_SIZE[alternateTypeName];
|
|
1193
|
+
if (!NATIVE_LITTLE_ENDIAN && elementSize) {
|
|
1194
|
+
bytes = bytes.slice();
|
|
1195
|
+
swapByteOrder(bytes, elementSize);
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
if (this.encodingLevel === "structuredClonable" || this.encodingLevel === "jsonCompatibleWithBytes") return alternateTypeName === void 0 ? ["bytes", bytes] : [
|
|
1199
|
+
"bytes",
|
|
1200
|
+
bytes,
|
|
1201
|
+
alternateTypeName
|
|
1202
|
+
];
|
|
1052
1203
|
let b64;
|
|
1053
|
-
if (
|
|
1204
|
+
if (bytes.toBase64) b64 = bytes.toBase64({ omitPadding: true });
|
|
1205
|
+
else if (typeof Buffer !== "undefined") b64 = (bytes instanceof Buffer ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)).toString("base64");
|
|
1054
1206
|
else {
|
|
1055
1207
|
let binary = "";
|
|
1056
1208
|
for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]);
|
|
1057
1209
|
b64 = btoa(binary);
|
|
1058
1210
|
}
|
|
1059
|
-
|
|
1211
|
+
b64 = b64.replace(/=+$/, "");
|
|
1212
|
+
return alternateTypeName === void 0 ? ["bytes", b64] : [
|
|
1213
|
+
"bytes",
|
|
1214
|
+
b64,
|
|
1215
|
+
alternateTypeName
|
|
1216
|
+
];
|
|
1060
1217
|
}
|
|
1218
|
+
case "url": return ["url", value.href];
|
|
1061
1219
|
case "headers": return ["headers", [...value]];
|
|
1062
1220
|
case "request": {
|
|
1063
1221
|
let req = value;
|
|
@@ -1172,7 +1330,9 @@ var Devaluator = class Devaluator {
|
|
|
1172
1330
|
} else if (rewritten && rewritten.stack) result.push(rewritten.stack);
|
|
1173
1331
|
return result;
|
|
1174
1332
|
}
|
|
1175
|
-
case "undefined":
|
|
1333
|
+
case "undefined":
|
|
1334
|
+
if (this.encodingLevel === "structuredClonable") return;
|
|
1335
|
+
return ["undefined"];
|
|
1176
1336
|
case "stub":
|
|
1177
1337
|
case "rpc-promise": {
|
|
1178
1338
|
if (!this.source) throw new Error("Can't serialize RPC stubs in this context.");
|
|
@@ -1239,6 +1399,9 @@ var NullImporter = class {
|
|
|
1239
1399
|
getPipeReadable(exportId) {
|
|
1240
1400
|
throw new Error("Cannot retrieve pipe readable without an RPC session.");
|
|
1241
1401
|
}
|
|
1402
|
+
getLimits() {
|
|
1403
|
+
return DEFAULT_LIMITS;
|
|
1404
|
+
}
|
|
1242
1405
|
};
|
|
1243
1406
|
const NULL_IMPORTER = new NullImporter();
|
|
1244
1407
|
function fixBrokenRequestBody(request, body) {
|
|
@@ -1255,15 +1418,22 @@ function streamToBlobPromise(stream, type) {
|
|
|
1255
1418
|
}
|
|
1256
1419
|
var Evaluator = class Evaluator {
|
|
1257
1420
|
importer;
|
|
1258
|
-
|
|
1421
|
+
encodingLevel;
|
|
1422
|
+
limits;
|
|
1423
|
+
constructor(importer, encodingLevel = "string") {
|
|
1259
1424
|
this.importer = importer;
|
|
1425
|
+
this.encodingLevel = encodingLevel;
|
|
1426
|
+
this.limits = importer.getLimits();
|
|
1260
1427
|
}
|
|
1261
1428
|
hooks = [];
|
|
1262
1429
|
promises = [];
|
|
1263
1430
|
evaluate(value) {
|
|
1431
|
+
return this.evaluateWithDepth(value, 0);
|
|
1432
|
+
}
|
|
1433
|
+
evaluateWithDepth(value, depth) {
|
|
1264
1434
|
let payload = RpcPayload.forEvaluate(this.hooks, this.promises);
|
|
1265
1435
|
try {
|
|
1266
|
-
payload.value = this.evaluateImpl(value, payload, "value");
|
|
1436
|
+
payload.value = this.evaluateImpl(value, payload, "value", depth);
|
|
1267
1437
|
return payload;
|
|
1268
1438
|
} catch (err) {
|
|
1269
1439
|
payload.dispose();
|
|
@@ -1273,31 +1443,70 @@ var Evaluator = class Evaluator {
|
|
|
1273
1443
|
evaluateCopy(value) {
|
|
1274
1444
|
return this.evaluate(structuredClone(value));
|
|
1275
1445
|
}
|
|
1276
|
-
evaluateImpl(value, parent, property) {
|
|
1446
|
+
evaluateImpl(value, parent, property, depth) {
|
|
1447
|
+
let maxDepth = this.limits.maxDepth;
|
|
1448
|
+
if (depth >= maxDepth) throw new TypeError(`Deserialization exceeded maximum allowed message depth of ${maxDepth}.`);
|
|
1449
|
+
if (this.encodingLevel === "structuredClonable") {
|
|
1450
|
+
if (value instanceof Date || typeof value === "bigint") return value;
|
|
1451
|
+
}
|
|
1277
1452
|
if (value instanceof Array) {
|
|
1278
1453
|
if (value.length == 1 && value[0] instanceof Array) {
|
|
1279
1454
|
let result = value[0];
|
|
1280
|
-
for (let i = 0; i < result.length; i++) result[i] = this.evaluateImpl(result[i], result, i);
|
|
1455
|
+
for (let i = 0; i < result.length; i++) result[i] = this.evaluateImpl(result[i], result, i, depth + 1);
|
|
1281
1456
|
return result;
|
|
1282
1457
|
} else switch (value[0]) {
|
|
1283
1458
|
case "bigint":
|
|
1284
|
-
if (typeof value[1] == "string")
|
|
1459
|
+
if (typeof value[1] == "string") {
|
|
1460
|
+
let digits = value[1];
|
|
1461
|
+
let maxBigIntDigits = this.limits.maxBigIntDigits;
|
|
1462
|
+
if (digits.length > maxBigIntDigits) throw new TypeError(`Deserialized bigint exceeds maximum length of ${maxBigIntDigits} digits.`);
|
|
1463
|
+
return BigInt(digits);
|
|
1464
|
+
}
|
|
1285
1465
|
break;
|
|
1286
1466
|
case "date":
|
|
1287
1467
|
if (value[1] === null) return /* @__PURE__ */ new Date(NaN);
|
|
1288
1468
|
if (typeof value[1] == "number") return new Date(value[1]);
|
|
1289
1469
|
break;
|
|
1290
|
-
case "bytes":
|
|
1291
|
-
|
|
1292
|
-
|
|
1470
|
+
case "bytes": {
|
|
1471
|
+
let bytes;
|
|
1472
|
+
if (value[1] instanceof Uint8Array) bytes = value[1];
|
|
1473
|
+
else if (typeof value[1] == "string") if (typeof Buffer !== "undefined") bytes = Buffer.from(value[1], "base64");
|
|
1474
|
+
else if (Uint8Array.fromBase64) bytes = Uint8Array.fromBase64(value[1]);
|
|
1293
1475
|
else {
|
|
1294
1476
|
let bs = atob(value[1]);
|
|
1295
1477
|
let len = bs.length;
|
|
1296
|
-
|
|
1478
|
+
bytes = new Uint8Array(len);
|
|
1297
1479
|
for (let i = 0; i < len; i++) bytes[i] = bs.charCodeAt(i);
|
|
1298
|
-
return bytes;
|
|
1299
1480
|
}
|
|
1300
|
-
break;
|
|
1481
|
+
else break;
|
|
1482
|
+
if (value.length === 2) return bytes;
|
|
1483
|
+
if (typeof value[2] !== "string") throw new TypeError(`Unknown bytes type marker type: ${typeof value[2]}`);
|
|
1484
|
+
if (!isValidByteContainerName(value[2])) {
|
|
1485
|
+
let marker = value[2].slice(0, 64);
|
|
1486
|
+
throw new TypeError(`Unknown bytes type marker: ${marker}`);
|
|
1487
|
+
}
|
|
1488
|
+
let marker = value[2];
|
|
1489
|
+
let elementSize = TYPED_ARRAY_ELEMENT_SIZE[marker];
|
|
1490
|
+
if (elementSize !== void 0 && bytes.byteLength % elementSize !== 0) throw new TypeError(`Invalid byte length ${bytes.byteLength} for ${marker}; expected a multiple of ${elementSize}`);
|
|
1491
|
+
let buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
|
1492
|
+
if (!NATIVE_LITTLE_ENDIAN && elementSize !== void 0) swapByteOrder(new Uint8Array(buffer), elementSize);
|
|
1493
|
+
switch (marker) {
|
|
1494
|
+
case "ArrayBuffer": return buffer;
|
|
1495
|
+
case "DataView": return new DataView(buffer);
|
|
1496
|
+
case "Int8Array": return new Int8Array(buffer);
|
|
1497
|
+
case "Uint8Array": return new Uint8Array(buffer);
|
|
1498
|
+
case "Uint8ClampedArray": return new Uint8ClampedArray(buffer);
|
|
1499
|
+
case "Int16Array": return new Int16Array(buffer);
|
|
1500
|
+
case "Uint16Array": return new Uint16Array(buffer);
|
|
1501
|
+
case "Int32Array": return new Int32Array(buffer);
|
|
1502
|
+
case "Uint32Array": return new Uint32Array(buffer);
|
|
1503
|
+
case "BigInt64Array": return new BigInt64Array(buffer);
|
|
1504
|
+
case "BigUint64Array": return new BigUint64Array(buffer);
|
|
1505
|
+
case "Float32Array": return new Float32Array(buffer);
|
|
1506
|
+
case "Float64Array": return new Float64Array(buffer);
|
|
1507
|
+
default:
|
|
1508
|
+
}
|
|
1509
|
+
}
|
|
1301
1510
|
case "error":
|
|
1302
1511
|
if (value.length >= 3 && typeof value[1] === "string" && typeof value[2] === "string") {
|
|
1303
1512
|
let cls = ERROR_TYPES[value[1]] || Error;
|
|
@@ -1310,7 +1519,11 @@ var Evaluator = class Evaluator {
|
|
|
1310
1519
|
let propsObj = props;
|
|
1311
1520
|
for (let key of Object.keys(propsObj)) {
|
|
1312
1521
|
if (key === "name" || key === "message" || key === "stack") continue;
|
|
1313
|
-
|
|
1522
|
+
if (key in Object.prototype || key === "toJSON") {
|
|
1523
|
+
this.evaluateImpl(propsObj[key], result, key, depth + 1);
|
|
1524
|
+
continue;
|
|
1525
|
+
}
|
|
1526
|
+
anyResult[key] = this.evaluateImpl(propsObj[key], result, key, depth + 1);
|
|
1314
1527
|
}
|
|
1315
1528
|
}
|
|
1316
1529
|
return result;
|
|
@@ -1322,6 +1535,9 @@ var Evaluator = class Evaluator {
|
|
|
1322
1535
|
case "inf": return Infinity;
|
|
1323
1536
|
case "-inf": return -Infinity;
|
|
1324
1537
|
case "nan": return NaN;
|
|
1538
|
+
case "url":
|
|
1539
|
+
if (value.length === 2 && typeof value[1] === "string") return new URL(value[1]);
|
|
1540
|
+
break;
|
|
1325
1541
|
case "headers":
|
|
1326
1542
|
if (value.length === 2 && value[1] instanceof Array) return new Headers(value[1]);
|
|
1327
1543
|
break;
|
|
@@ -1331,11 +1547,11 @@ var Evaluator = class Evaluator {
|
|
|
1331
1547
|
let init = value[2];
|
|
1332
1548
|
if (typeof init !== "object" || init === null) break;
|
|
1333
1549
|
if (init.body) {
|
|
1334
|
-
init.body = this.evaluateImpl(init.body, init, "body");
|
|
1550
|
+
init.body = this.evaluateImpl(init.body, init, "body", depth + 1);
|
|
1335
1551
|
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
1552
|
}
|
|
1337
1553
|
if (init.signal) {
|
|
1338
|
-
init.signal = this.evaluateImpl(init.signal, init, "signal");
|
|
1554
|
+
init.signal = this.evaluateImpl(init.signal, init, "signal", depth + 1);
|
|
1339
1555
|
if (!(init.signal instanceof AbortSignal)) throw new TypeError("Request siganl must be of type AbortSignal.");
|
|
1340
1556
|
}
|
|
1341
1557
|
if (init.headers && !(init.headers instanceof Array)) throw new TypeError("Request headers must be serialized as an array of pairs.");
|
|
@@ -1352,7 +1568,7 @@ var Evaluator = class Evaluator {
|
|
|
1352
1568
|
}
|
|
1353
1569
|
case "response": {
|
|
1354
1570
|
if (value.length !== 3) break;
|
|
1355
|
-
let body = this.evaluateImpl(value[1], parent, property);
|
|
1571
|
+
let body = this.evaluateImpl(value[1], parent, property, depth + 1);
|
|
1356
1572
|
if (body === null || typeof body === "string" || body instanceof Uint8Array || body instanceof ReadableStream) {} else throw new TypeError("Response body must be of type ReadableStream.");
|
|
1357
1573
|
let init = value[2];
|
|
1358
1574
|
if (typeof init !== "object" || init === null) break;
|
|
@@ -1363,7 +1579,7 @@ var Evaluator = class Evaluator {
|
|
|
1363
1579
|
case "blob": {
|
|
1364
1580
|
if (value.length !== 3 || typeof value[1] !== "string") break;
|
|
1365
1581
|
let contentType = value[1];
|
|
1366
|
-
let content = this.evaluateImpl(value[2], parent, property);
|
|
1582
|
+
let content = this.evaluateImpl(value[2], parent, property, depth + 1);
|
|
1367
1583
|
if (!(content instanceof ReadableStream)) throw new TypeError("Blob content must be serialized as a ReadableStream.");
|
|
1368
1584
|
let promise = streamToBlobPromise(content, contentType);
|
|
1369
1585
|
this.promises.push({
|
|
@@ -1404,7 +1620,7 @@ var Evaluator = class Evaluator {
|
|
|
1404
1620
|
if (value.length == 3) return addStub(hook.get(path));
|
|
1405
1621
|
let args = value[3];
|
|
1406
1622
|
if (!(args instanceof Array)) break;
|
|
1407
|
-
args = new Evaluator(this.importer).
|
|
1623
|
+
args = new Evaluator(this.importer).evaluateWithDepth([args], depth);
|
|
1408
1624
|
return addStub(hook.call(path, args));
|
|
1409
1625
|
}
|
|
1410
1626
|
case "remap": {
|
|
@@ -1470,9 +1686,9 @@ var Evaluator = class Evaluator {
|
|
|
1470
1686
|
} else if (value instanceof Object) {
|
|
1471
1687
|
let result = value;
|
|
1472
1688
|
for (let key in result) if (key in Object.prototype || key === "toJSON") {
|
|
1473
|
-
this.evaluateImpl(result[key], result, key);
|
|
1689
|
+
this.evaluateImpl(result[key], result, key, depth + 1);
|
|
1474
1690
|
delete result[key];
|
|
1475
|
-
} else result[key] = this.evaluateImpl(result[key], result, key);
|
|
1691
|
+
} else result[key] = this.evaluateImpl(result[key], result, key, depth + 1);
|
|
1476
1692
|
return result;
|
|
1477
1693
|
} else return value;
|
|
1478
1694
|
}
|
|
@@ -1488,6 +1704,47 @@ function deserialize(value) {
|
|
|
1488
1704
|
|
|
1489
1705
|
//#endregion
|
|
1490
1706
|
//#region src/rpc.ts
|
|
1707
|
+
const ESTIMATED_OBJECT_OVERHEAD = 16;
|
|
1708
|
+
const ESTIMATED_ENTRY_OVERHEAD = 8;
|
|
1709
|
+
const ESTIMATED_BINARY_OVERHEAD = 16;
|
|
1710
|
+
const MAX_ESTIMATE_DEPTH = 64;
|
|
1711
|
+
function estimateStringSize(value) {
|
|
1712
|
+
return 2 + value.length * 3;
|
|
1713
|
+
}
|
|
1714
|
+
function estimateEncodedSize(value, seen, depth = 0) {
|
|
1715
|
+
if (depth >= MAX_ESTIMATE_DEPTH) return ESTIMATED_ENTRY_OVERHEAD;
|
|
1716
|
+
switch (typeof value) {
|
|
1717
|
+
case "string": return estimateStringSize(value);
|
|
1718
|
+
case "number": return 16;
|
|
1719
|
+
case "bigint": return 16;
|
|
1720
|
+
case "boolean": return 8;
|
|
1721
|
+
case "undefined": return 16;
|
|
1722
|
+
case "object": {
|
|
1723
|
+
if (value === null) return 8;
|
|
1724
|
+
if (ArrayBuffer.isView(value)) return ESTIMATED_BINARY_OVERHEAD + value.byteLength;
|
|
1725
|
+
if (value instanceof ArrayBuffer) return ESTIMATED_BINARY_OVERHEAD + value.byteLength;
|
|
1726
|
+
if (typeof Blob !== "undefined" && value instanceof Blob) return ESTIMATED_BINARY_OVERHEAD + value.size;
|
|
1727
|
+
if (value instanceof Date) return 16;
|
|
1728
|
+
seen ??= /* @__PURE__ */ new WeakSet();
|
|
1729
|
+
if (seen.has(value)) return ESTIMATED_ENTRY_OVERHEAD;
|
|
1730
|
+
seen.add(value);
|
|
1731
|
+
if (value instanceof Array) {
|
|
1732
|
+
let size = ESTIMATED_OBJECT_OVERHEAD;
|
|
1733
|
+
for (let item of value) size += ESTIMATED_ENTRY_OVERHEAD + estimateEncodedSize(item, seen, depth + 1);
|
|
1734
|
+
return size;
|
|
1735
|
+
}
|
|
1736
|
+
if (value instanceof Error) {
|
|
1737
|
+
let size = ESTIMATED_OBJECT_OVERHEAD + estimateStringSize(value.name) + estimateStringSize(value.message) + estimateStringSize(value.stack ?? "");
|
|
1738
|
+
for (let key of Object.keys(value)) size += ESTIMATED_ENTRY_OVERHEAD + estimateStringSize(key) + estimateEncodedSize(value[key], seen, depth + 1);
|
|
1739
|
+
return size;
|
|
1740
|
+
}
|
|
1741
|
+
let size = ESTIMATED_OBJECT_OVERHEAD;
|
|
1742
|
+
for (let key of Object.keys(value)) size += ESTIMATED_ENTRY_OVERHEAD + estimateStringSize(key) + estimateEncodedSize(value[key], seen, depth + 1);
|
|
1743
|
+
return size;
|
|
1744
|
+
}
|
|
1745
|
+
default: return 16;
|
|
1746
|
+
}
|
|
1747
|
+
}
|
|
1491
1748
|
var ImportTableEntry = class {
|
|
1492
1749
|
session;
|
|
1493
1750
|
importId;
|
|
@@ -1580,24 +1837,28 @@ var RpcImportHook = class RpcImportHook extends StubHook {
|
|
|
1580
1837
|
if (this.entry) return this.entry;
|
|
1581
1838
|
else throw new Error("This RpcImportHook was already disposed.");
|
|
1582
1839
|
}
|
|
1840
|
+
getEntryTakingOwnership(disposeOwned) {
|
|
1841
|
+
try {
|
|
1842
|
+
return this.getEntry();
|
|
1843
|
+
} catch (err) {
|
|
1844
|
+
disposeOwned();
|
|
1845
|
+
throw err;
|
|
1846
|
+
}
|
|
1847
|
+
}
|
|
1583
1848
|
call(path, args) {
|
|
1584
|
-
let entry = this.
|
|
1849
|
+
let entry = this.getEntryTakingOwnership(() => args.dispose());
|
|
1585
1850
|
if (entry.resolution) return entry.resolution.call(path, args);
|
|
1586
1851
|
else return entry.session.sendCall(entry.importId, path, args);
|
|
1587
1852
|
}
|
|
1588
1853
|
stream(path, args) {
|
|
1589
|
-
let entry = this.
|
|
1854
|
+
let entry = this.getEntryTakingOwnership(() => args.dispose());
|
|
1590
1855
|
if (entry.resolution) return entry.resolution.stream(path, args);
|
|
1591
1856
|
else return entry.session.sendStream(entry.importId, path, args);
|
|
1592
1857
|
}
|
|
1593
1858
|
map(path, captures, instructions) {
|
|
1594
|
-
let entry
|
|
1595
|
-
try {
|
|
1596
|
-
entry = this.getEntry();
|
|
1597
|
-
} catch (err) {
|
|
1859
|
+
let entry = this.getEntryTakingOwnership(() => {
|
|
1598
1860
|
for (let cap of captures) cap.dispose();
|
|
1599
|
-
|
|
1600
|
-
}
|
|
1861
|
+
});
|
|
1601
1862
|
if (entry.resolution) return entry.resolution.map(path, captures, instructions);
|
|
1602
1863
|
else return entry.session.sendMap(entry.importId, path, captures, instructions);
|
|
1603
1864
|
}
|
|
@@ -1653,9 +1914,24 @@ var RpcSessionImpl = class {
|
|
|
1653
1914
|
onBatchDone;
|
|
1654
1915
|
pullCount = 0;
|
|
1655
1916
|
onBrokenCallbacks = [];
|
|
1917
|
+
encodingLevel;
|
|
1918
|
+
limits;
|
|
1656
1919
|
constructor(transport, mainHook, options) {
|
|
1657
1920
|
this.transport = transport;
|
|
1658
1921
|
this.options = options;
|
|
1922
|
+
let level = "string";
|
|
1923
|
+
if ("encodingLevel" in transport) {
|
|
1924
|
+
let raw = transport.encodingLevel;
|
|
1925
|
+
if (raw !== void 0) {
|
|
1926
|
+
if (raw !== "string" && raw !== "jsonCompatible" && raw !== "jsonCompatibleWithBytes" && raw !== "structuredClonable") throw new TypeError(`Unknown transport encodingLevel: ${String(raw)}`);
|
|
1927
|
+
level = raw;
|
|
1928
|
+
}
|
|
1929
|
+
}
|
|
1930
|
+
this.encodingLevel = level;
|
|
1931
|
+
this.limits = {
|
|
1932
|
+
...DEFAULT_LIMITS,
|
|
1933
|
+
...options.limits
|
|
1934
|
+
};
|
|
1659
1935
|
this.exports.push({
|
|
1660
1936
|
hook: mainHook,
|
|
1661
1937
|
refcount: 1
|
|
@@ -1736,7 +2012,7 @@ var RpcSessionImpl = class {
|
|
|
1736
2012
|
let autoRelease = exp.autoRelease;
|
|
1737
2013
|
++this.pullCount;
|
|
1738
2014
|
exp.pull = resolve().then((payload) => {
|
|
1739
|
-
let value = Devaluator.devaluate(payload.value, void 0, this, payload);
|
|
2015
|
+
let value = Devaluator.devaluate(payload.value, void 0, this, payload, this.encodingLevel);
|
|
1740
2016
|
this.send([
|
|
1741
2017
|
"resolve",
|
|
1742
2018
|
exportId,
|
|
@@ -1747,7 +2023,7 @@ var RpcSessionImpl = class {
|
|
|
1747
2023
|
this.send([
|
|
1748
2024
|
"reject",
|
|
1749
2025
|
exportId,
|
|
1750
|
-
Devaluator.devaluate(error, void 0, this)
|
|
2026
|
+
Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)
|
|
1751
2027
|
]);
|
|
1752
2028
|
if (autoRelease) this.releaseExport(exportId, 1);
|
|
1753
2029
|
}).catch((error) => {
|
|
@@ -1755,7 +2031,7 @@ var RpcSessionImpl = class {
|
|
|
1755
2031
|
this.send([
|
|
1756
2032
|
"reject",
|
|
1757
2033
|
exportId,
|
|
1758
|
-
Devaluator.devaluate(error, void 0, this)
|
|
2034
|
+
Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)
|
|
1759
2035
|
]);
|
|
1760
2036
|
if (autoRelease) this.releaseExport(exportId, 1);
|
|
1761
2037
|
} catch (error2) {
|
|
@@ -1798,6 +2074,9 @@ var RpcSessionImpl = class {
|
|
|
1798
2074
|
entry.pipeReadable = void 0;
|
|
1799
2075
|
return readable;
|
|
1800
2076
|
}
|
|
2077
|
+
getLimits() {
|
|
2078
|
+
return this.limits;
|
|
2079
|
+
}
|
|
1801
2080
|
createPipe(readable, readableHook) {
|
|
1802
2081
|
if (this.abortReason) throw this.abortReason;
|
|
1803
2082
|
this.send(["pipe"]);
|
|
@@ -1811,27 +2090,52 @@ var RpcSessionImpl = class {
|
|
|
1811
2090
|
}
|
|
1812
2091
|
send(msg) {
|
|
1813
2092
|
if (this.abortReason !== void 0) return 0;
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
msgText = JSON.stringify(msg);
|
|
1817
|
-
} catch (err) {
|
|
2093
|
+
if (this.encodingLevel === "string") {
|
|
2094
|
+
let msgText;
|
|
1818
2095
|
try {
|
|
1819
|
-
|
|
1820
|
-
} catch (
|
|
1821
|
-
|
|
2096
|
+
msgText = JSON.stringify(msg);
|
|
2097
|
+
} catch (err) {
|
|
2098
|
+
try {
|
|
2099
|
+
this.abort(err);
|
|
2100
|
+
} catch (err2) {}
|
|
2101
|
+
throw err;
|
|
2102
|
+
}
|
|
2103
|
+
try {
|
|
2104
|
+
let sent = this.transport.send(msgText);
|
|
2105
|
+
if (sent !== void 0 && typeof sent.catch === "function") sent.catch((err) => this.abort(err, false));
|
|
2106
|
+
} catch (err) {
|
|
2107
|
+
queueMicrotask(() => this.abort(err, false));
|
|
2108
|
+
}
|
|
2109
|
+
return msgText.length;
|
|
2110
|
+
} else try {
|
|
2111
|
+
let size = this.transport.send(msg);
|
|
2112
|
+
if (typeof size === "number") return size;
|
|
2113
|
+
let thenable = size;
|
|
2114
|
+
if (thenable && typeof thenable.then === "function") Promise.resolve(thenable).catch((err) => this.abort(err, false));
|
|
2115
|
+
return;
|
|
2116
|
+
} catch (err) {
|
|
2117
|
+
queueMicrotask(() => this.abort(err, false));
|
|
2118
|
+
return;
|
|
1822
2119
|
}
|
|
1823
|
-
this.transport.send(msgText).catch((err) => this.abort(err, false));
|
|
1824
|
-
return msgText.length;
|
|
1825
2120
|
}
|
|
1826
2121
|
sendCall(id, path, args) {
|
|
1827
|
-
if (this.abortReason)
|
|
2122
|
+
if (this.abortReason) {
|
|
2123
|
+
args?.dispose();
|
|
2124
|
+
throw this.abortReason;
|
|
2125
|
+
}
|
|
1828
2126
|
let value = [
|
|
1829
2127
|
"pipeline",
|
|
1830
2128
|
id,
|
|
1831
2129
|
path
|
|
1832
2130
|
];
|
|
1833
2131
|
if (args) {
|
|
1834
|
-
let devalue
|
|
2132
|
+
let devalue;
|
|
2133
|
+
try {
|
|
2134
|
+
devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
|
|
2135
|
+
} catch (err) {
|
|
2136
|
+
args.dispose();
|
|
2137
|
+
throw err;
|
|
2138
|
+
}
|
|
1835
2139
|
value.push(devalue[0]);
|
|
1836
2140
|
}
|
|
1837
2141
|
this.send(["push", value]);
|
|
@@ -1840,15 +2144,26 @@ var RpcSessionImpl = class {
|
|
|
1840
2144
|
return new RpcImportHook(true, entry);
|
|
1841
2145
|
}
|
|
1842
2146
|
sendStream(id, path, args) {
|
|
1843
|
-
if (this.abortReason)
|
|
2147
|
+
if (this.abortReason) {
|
|
2148
|
+
args.dispose();
|
|
2149
|
+
throw this.abortReason;
|
|
2150
|
+
}
|
|
1844
2151
|
let value = [
|
|
1845
2152
|
"pipeline",
|
|
1846
2153
|
id,
|
|
1847
2154
|
path
|
|
1848
2155
|
];
|
|
1849
|
-
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
|
+
}
|
|
1850
2163
|
value.push(devalue[0]);
|
|
1851
|
-
let
|
|
2164
|
+
let msg = ["stream", value];
|
|
2165
|
+
let size = this.send(msg);
|
|
2166
|
+
if (size === void 0) size = estimateEncodedSize(msg);
|
|
1852
2167
|
let importId = this.imports.length;
|
|
1853
2168
|
let entry = new ImportTableEntry(this, importId, true);
|
|
1854
2169
|
entry.remoteRefcount = 0;
|
|
@@ -1904,7 +2219,14 @@ var RpcSessionImpl = class {
|
|
|
1904
2219
|
this.cancelReadLoop?.(error);
|
|
1905
2220
|
this.cancelReadLoop = void 0;
|
|
1906
2221
|
if (trySendAbortMessage) try {
|
|
1907
|
-
|
|
2222
|
+
let abortMsg = ["abort", Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)];
|
|
2223
|
+
if (this.encodingLevel === "string") {
|
|
2224
|
+
let sent = this.transport.send(JSON.stringify(abortMsg));
|
|
2225
|
+
if (sent !== void 0 && typeof sent.catch === "function") sent.catch((err) => {});
|
|
2226
|
+
} else {
|
|
2227
|
+
let result = this.transport.send(abortMsg);
|
|
2228
|
+
if (result && typeof result.then === "function") Promise.resolve(result).catch((err) => {});
|
|
2229
|
+
}
|
|
1908
2230
|
} catch (err) {}
|
|
1909
2231
|
if (error === void 0) error = "undefined";
|
|
1910
2232
|
this.abortReason = error;
|
|
@@ -1926,18 +2248,19 @@ var RpcSessionImpl = class {
|
|
|
1926
2248
|
while (!this.abortReason) {
|
|
1927
2249
|
let readCanceled = Promise.withResolvers();
|
|
1928
2250
|
this.cancelReadLoop = readCanceled.reject;
|
|
1929
|
-
let
|
|
2251
|
+
let raw;
|
|
1930
2252
|
try {
|
|
1931
|
-
|
|
2253
|
+
raw = await Promise.race([this.transport.receive(), readCanceled.promise]);
|
|
1932
2254
|
} finally {
|
|
1933
2255
|
if (this.cancelReadLoop === readCanceled.reject) this.cancelReadLoop = void 0;
|
|
1934
2256
|
}
|
|
1935
|
-
|
|
2257
|
+
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
2258
|
if (this.abortReason) break;
|
|
2259
|
+
let msg = this.encodingLevel === "string" ? JSON.parse(raw) : raw;
|
|
1937
2260
|
if (msg instanceof Array) switch (msg[0]) {
|
|
1938
2261
|
case "push":
|
|
1939
2262
|
if (msg.length > 1) {
|
|
1940
|
-
let hook = new PayloadStubHook(new Evaluator(this).evaluate(msg[1]));
|
|
2263
|
+
let hook = new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[1]));
|
|
1941
2264
|
hook.ignoreUnhandledRejections();
|
|
1942
2265
|
this.exports.push({
|
|
1943
2266
|
hook,
|
|
@@ -1948,7 +2271,7 @@ var RpcSessionImpl = class {
|
|
|
1948
2271
|
break;
|
|
1949
2272
|
case "stream":
|
|
1950
2273
|
if (msg.length > 1) {
|
|
1951
|
-
let hook = new PayloadStubHook(new Evaluator(this).evaluate(msg[1]));
|
|
2274
|
+
let hook = new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[1]));
|
|
1952
2275
|
hook.ignoreUnhandledRejections();
|
|
1953
2276
|
let exportId = this.exports.length;
|
|
1954
2277
|
this.exports.push({
|
|
@@ -1983,13 +2306,13 @@ var RpcSessionImpl = class {
|
|
|
1983
2306
|
let importId = msg[1];
|
|
1984
2307
|
if (typeof importId == "number" && msg.length > 2) {
|
|
1985
2308
|
let imp = this.imports[importId];
|
|
1986
|
-
if (imp) if (msg[0] == "resolve") imp.resolve(new PayloadStubHook(new Evaluator(this).evaluate(msg[2])));
|
|
2309
|
+
if (imp) if (msg[0] == "resolve") imp.resolve(new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[2])));
|
|
1987
2310
|
else {
|
|
1988
|
-
let payload = new Evaluator(this).evaluate(msg[2]);
|
|
2311
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[2]);
|
|
1989
2312
|
payload.dispose();
|
|
1990
2313
|
imp.resolve(new ErrorStubHook(payload.value));
|
|
1991
2314
|
}
|
|
1992
|
-
else if (msg[0] == "resolve") new Evaluator(this).evaluate(msg[2]).dispose();
|
|
2315
|
+
else if (msg[0] == "resolve") new Evaluator(this, this.encodingLevel).evaluate(msg[2]).dispose();
|
|
1993
2316
|
continue;
|
|
1994
2317
|
}
|
|
1995
2318
|
break;
|
|
@@ -2004,9 +2327,9 @@ var RpcSessionImpl = class {
|
|
|
2004
2327
|
break;
|
|
2005
2328
|
}
|
|
2006
2329
|
case "abort": {
|
|
2007
|
-
let payload = new Evaluator(this).evaluate(msg[1]);
|
|
2330
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[1]);
|
|
2008
2331
|
payload.dispose();
|
|
2009
|
-
this.abort(payload, false);
|
|
2332
|
+
this.abort(payload.value, false);
|
|
2010
2333
|
break;
|
|
2011
2334
|
}
|
|
2012
2335
|
}
|
|
@@ -2057,6 +2380,8 @@ var RpcSession$1 = class {
|
|
|
2057
2380
|
|
|
2058
2381
|
//#endregion
|
|
2059
2382
|
//#region src/websocket.ts
|
|
2383
|
+
/** Close-frame reason max UTF-8 bytes: 125 payload - 2-byte status (RFC 6455 section 5.5). */
|
|
2384
|
+
const MAX_CLOSE_REASON_BYTES = 123;
|
|
2060
2385
|
function newWebSocketRpcSession$1(webSocket, localMain, options) {
|
|
2061
2386
|
if (typeof webSocket === "string") webSocket = new WebSocket(webSocket);
|
|
2062
2387
|
return new RpcSession$1(new WebSocketTransport(webSocket), localMain, options).getRemoteMain();
|
|
@@ -2076,9 +2401,14 @@ function newWorkersWebSocketRpcResponse(request, localMain, options) {
|
|
|
2076
2401
|
webSocket: pair[1]
|
|
2077
2402
|
});
|
|
2078
2403
|
}
|
|
2404
|
+
/**
|
|
2405
|
+
* Generic WebSocket transport. Default `T = string` is backward-compatible and satisfies
|
|
2406
|
+
* `RpcTransport`. Use `T = ArrayBuffer` as a building block for binary transports.
|
|
2407
|
+
*/
|
|
2079
2408
|
var WebSocketTransport = class {
|
|
2080
2409
|
constructor(webSocket) {
|
|
2081
2410
|
this.#webSocket = webSocket;
|
|
2411
|
+
webSocket.binaryType = "arraybuffer";
|
|
2082
2412
|
if (webSocket.readyState === WebSocket.CONNECTING) {
|
|
2083
2413
|
this.#sendQueue = [];
|
|
2084
2414
|
webSocket.addEventListener("open", (event) => {
|
|
@@ -2091,12 +2421,12 @@ var WebSocketTransport = class {
|
|
|
2091
2421
|
});
|
|
2092
2422
|
}
|
|
2093
2423
|
webSocket.addEventListener("message", (event) => {
|
|
2094
|
-
if (this.#error) {} else if (typeof event.data === "string") if (this.#receiveResolver) {
|
|
2424
|
+
if (this.#error) {} else if (typeof event.data === "string" || event.data instanceof ArrayBuffer) if (this.#receiveResolver) {
|
|
2095
2425
|
this.#receiveResolver(event.data);
|
|
2096
2426
|
this.#receiveResolver = void 0;
|
|
2097
2427
|
this.#receiveRejecter = void 0;
|
|
2098
2428
|
} else this.#receiveQueue.push(event.data);
|
|
2099
|
-
else this.#receivedError(/* @__PURE__ */ new TypeError("Received
|
|
2429
|
+
else this.#receivedError(/* @__PURE__ */ new TypeError("Received unexpected message type from WebSocket."));
|
|
2100
2430
|
});
|
|
2101
2431
|
webSocket.addEventListener("close", (event) => {
|
|
2102
2432
|
this.#receivedError(/* @__PURE__ */ new Error(`Peer closed WebSocket: ${event.code} ${event.reason}`));
|
|
@@ -2111,13 +2441,13 @@ var WebSocketTransport = class {
|
|
|
2111
2441
|
#receiveRejecter;
|
|
2112
2442
|
#receiveQueue = [];
|
|
2113
2443
|
#error;
|
|
2114
|
-
|
|
2444
|
+
send(message) {
|
|
2115
2445
|
if (this.#sendQueue === void 0) this.#webSocket.send(message);
|
|
2116
2446
|
else this.#sendQueue.push(message);
|
|
2117
2447
|
}
|
|
2118
|
-
|
|
2119
|
-
if (this.#receiveQueue.length > 0) return this.#receiveQueue.shift();
|
|
2120
|
-
else if (this.#error)
|
|
2448
|
+
receive() {
|
|
2449
|
+
if (this.#receiveQueue.length > 0) return Promise.resolve(this.#receiveQueue.shift());
|
|
2450
|
+
else if (this.#error) return Promise.reject(this.#error);
|
|
2121
2451
|
else return new Promise((resolve, reject) => {
|
|
2122
2452
|
this.#receiveResolver = resolve;
|
|
2123
2453
|
this.#receiveRejecter = reject;
|
|
@@ -2127,6 +2457,8 @@ var WebSocketTransport = class {
|
|
|
2127
2457
|
let message;
|
|
2128
2458
|
if (reason instanceof Error) message = reason.message;
|
|
2129
2459
|
else message = `${reason}`;
|
|
2460
|
+
let reasonBytes = new TextEncoder().encode(message);
|
|
2461
|
+
if (reasonBytes.length > 123) message = new TextDecoder().decode(reasonBytes.subarray(0, 123), { stream: true });
|
|
2130
2462
|
this.#webSocket.close(3e3, message);
|
|
2131
2463
|
if (!this.#error) this.#error = reason;
|
|
2132
2464
|
}
|
|
@@ -2144,6 +2476,7 @@ var WebSocketTransport = class {
|
|
|
2144
2476
|
|
|
2145
2477
|
//#endregion
|
|
2146
2478
|
//#region src/batch.ts
|
|
2479
|
+
const yieldToMacrotask = typeof setImmediate === "function" ? () => new Promise((resolve) => setImmediate(resolve)) : () => new Promise((resolve) => setTimeout(resolve, 0));
|
|
2147
2480
|
var BatchClientTransport = class {
|
|
2148
2481
|
constructor(sendBatch) {
|
|
2149
2482
|
this.#promise = this.#scheduleBatch(sendBatch);
|
|
@@ -2152,7 +2485,7 @@ var BatchClientTransport = class {
|
|
|
2152
2485
|
#aborted;
|
|
2153
2486
|
#batchToSend = [];
|
|
2154
2487
|
#batchToReceive = null;
|
|
2155
|
-
|
|
2488
|
+
send(message) {
|
|
2156
2489
|
if (this.#batchToSend !== null) this.#batchToSend.push(message);
|
|
2157
2490
|
}
|
|
2158
2491
|
async receive() {
|
|
@@ -2165,7 +2498,7 @@ var BatchClientTransport = class {
|
|
|
2165
2498
|
this.#aborted = reason;
|
|
2166
2499
|
}
|
|
2167
2500
|
async #scheduleBatch(sendBatch) {
|
|
2168
|
-
await
|
|
2501
|
+
await yieldToMacrotask();
|
|
2169
2502
|
if (this.#aborted !== void 0) throw this.#aborted;
|
|
2170
2503
|
let batch = this.#batchToSend;
|
|
2171
2504
|
this.#batchToSend = null;
|
|
@@ -2194,7 +2527,7 @@ var BatchServerTransport = class {
|
|
|
2194
2527
|
#batchToSend = [];
|
|
2195
2528
|
#batchToReceive;
|
|
2196
2529
|
#allReceived = Promise.withResolvers();
|
|
2197
|
-
|
|
2530
|
+
send(message) {
|
|
2198
2531
|
this.#batchToSend.push(message);
|
|
2199
2532
|
}
|
|
2200
2533
|
async receive() {
|
|
@@ -2243,7 +2576,11 @@ async function newHttpBatchRpcResponse(request, localMain, options) {
|
|
|
2243
2576
|
* @param options Optional RPC session options. You can also pass headers to set on the response.
|
|
2244
2577
|
*/
|
|
2245
2578
|
async function nodeHttpBatchRpcResponse(request, response, localMain, options) {
|
|
2246
|
-
if (request.method !== "POST")
|
|
2579
|
+
if (request.method !== "POST") {
|
|
2580
|
+
response.writeHead(405, "This endpoint only accepts POST requests.", options?.headers);
|
|
2581
|
+
response.end();
|
|
2582
|
+
return;
|
|
2583
|
+
}
|
|
2247
2584
|
let body = await new Promise((resolve, reject) => {
|
|
2248
2585
|
let chunks = [];
|
|
2249
2586
|
request.on("data", (chunk) => {
|
|
@@ -2268,17 +2605,17 @@ function newMessagePortRpcSession$1(port, localMain, options) {
|
|
|
2268
2605
|
return new RpcSession$1(new MessagePortTransport(port), localMain, options).getRemoteMain();
|
|
2269
2606
|
}
|
|
2270
2607
|
var MessagePortTransport = class {
|
|
2608
|
+
encodingLevel = "structuredClonable";
|
|
2271
2609
|
constructor(port) {
|
|
2272
2610
|
this.#port = port;
|
|
2273
2611
|
port.start();
|
|
2274
2612
|
port.addEventListener("message", (event) => {
|
|
2275
2613
|
if (this.#error) {} else if (event.data === null) this.#receivedError(/* @__PURE__ */ new Error("Peer closed MessagePort connection."));
|
|
2276
|
-
else if (
|
|
2614
|
+
else if (this.#receiveResolver) {
|
|
2277
2615
|
this.#receiveResolver(event.data);
|
|
2278
2616
|
this.#receiveResolver = void 0;
|
|
2279
2617
|
this.#receiveRejecter = void 0;
|
|
2280
2618
|
} else this.#receiveQueue.push(event.data);
|
|
2281
|
-
else this.#receivedError(/* @__PURE__ */ new TypeError("Received non-string message from MessagePort."));
|
|
2282
2619
|
});
|
|
2283
2620
|
port.addEventListener("messageerror", (event) => {
|
|
2284
2621
|
this.#receivedError(/* @__PURE__ */ new Error("MessagePort message error."));
|
|
@@ -2289,7 +2626,7 @@ var MessagePortTransport = class {
|
|
|
2289
2626
|
#receiveRejecter;
|
|
2290
2627
|
#receiveQueue = [];
|
|
2291
2628
|
#error;
|
|
2292
|
-
|
|
2629
|
+
send(message) {
|
|
2293
2630
|
if (this.#error) throw this.#error;
|
|
2294
2631
|
this.#port.postMessage(message);
|
|
2295
2632
|
}
|
|
@@ -2453,9 +2790,11 @@ var MapVariableHook = class extends StubHook {
|
|
|
2453
2790
|
else throwMapperBuilderUseError();
|
|
2454
2791
|
}
|
|
2455
2792
|
call(path, args) {
|
|
2793
|
+
args.dispose();
|
|
2456
2794
|
throwMapperBuilderUseError();
|
|
2457
2795
|
}
|
|
2458
2796
|
map(path, captures, instructions) {
|
|
2797
|
+
for (let cap of captures) cap.dispose();
|
|
2459
2798
|
throwMapperBuilderUseError();
|
|
2460
2799
|
}
|
|
2461
2800
|
pull() {
|
|
@@ -2508,6 +2847,9 @@ var MapApplicator = class {
|
|
|
2508
2847
|
getPipeReadable(exportId) {
|
|
2509
2848
|
throw new Error("A mapper function cannot use pipe readables.");
|
|
2510
2849
|
}
|
|
2850
|
+
getLimits() {
|
|
2851
|
+
return DEFAULT_LIMITS;
|
|
2852
|
+
}
|
|
2511
2853
|
};
|
|
2512
2854
|
function applyMapToElement(input, parent, owner, captures, instructions) {
|
|
2513
2855
|
let mapper = new MapApplicator(captures, new PayloadStubHook(RpcPayload.deepCopyFrom(input, parent, owner)));
|
|
@@ -2563,14 +2905,11 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
|
|
|
2563
2905
|
let state = this.getState();
|
|
2564
2906
|
if (path.length !== 1 || typeof path[0] !== "string") throw new Error("WritableStream stub only supports direct method calls");
|
|
2565
2907
|
const method = path[0];
|
|
2566
|
-
if (method !== "write" && method !== "close" && method !== "abort") {
|
|
2567
|
-
args.dispose();
|
|
2568
|
-
throw new Error(`Unknown WritableStream method: ${method}`);
|
|
2569
|
-
}
|
|
2908
|
+
if (method !== "write" && method !== "close" && method !== "abort") throw new Error(`Unknown WritableStream method: ${method}`);
|
|
2570
2909
|
if (method === "close" || method === "abort") state.closed = true;
|
|
2571
|
-
|
|
2572
|
-
return new PromiseStubHook(args.deliverCall(func, state.writer).then((payload) => new PayloadStubHook(payload)));
|
|
2910
|
+
return new PromiseStubHook((method === "write" ? args.deliverStreamWrite(state.writer) : args.deliverCall(state.writer[method], state.writer)).then((payload) => new PayloadStubHook(payload)));
|
|
2573
2911
|
} catch (err) {
|
|
2912
|
+
args.dispose();
|
|
2574
2913
|
return new ErrorStubHook(err);
|
|
2575
2914
|
}
|
|
2576
2915
|
}
|
|
@@ -2846,20 +3185,23 @@ let newMessagePortRpcSession = newMessagePortRpcSession$1;
|
|
|
2846
3185
|
* credentials as parameters and returns the authorized API), then cross-origin requests should
|
|
2847
3186
|
* be safe.
|
|
2848
3187
|
*/
|
|
2849
|
-
async function newWorkersRpcResponse(request, localMain) {
|
|
3188
|
+
async function newWorkersRpcResponse(request, localMain, options) {
|
|
2850
3189
|
if (request.method === "POST") {
|
|
2851
|
-
let response = await newHttpBatchRpcResponse(request, localMain);
|
|
3190
|
+
let response = await newHttpBatchRpcResponse(request, localMain, options);
|
|
2852
3191
|
response.headers.set("Access-Control-Allow-Origin", "*");
|
|
2853
3192
|
return response;
|
|
2854
|
-
} else if (request.headers.get("Upgrade")?.toLowerCase() === "websocket") return newWorkersWebSocketRpcResponse(request, localMain);
|
|
3193
|
+
} else if (request.headers.get("Upgrade")?.toLowerCase() === "websocket") return newWorkersWebSocketRpcResponse(request, localMain, options);
|
|
2855
3194
|
else return new Response("This endpoint only accepts POST or WebSocket requests.", { status: 400 });
|
|
2856
3195
|
}
|
|
2857
3196
|
|
|
2858
3197
|
//#endregion
|
|
3198
|
+
exports.DEFAULT_LIMITS = DEFAULT_LIMITS;
|
|
3199
|
+
exports.DEFAULT_MAX_DEPTH = DEFAULT_MAX_DEPTH;
|
|
2859
3200
|
exports.RpcPromise = RpcPromise;
|
|
2860
3201
|
exports.RpcSession = RpcSession;
|
|
2861
3202
|
exports.RpcStub = RpcStub;
|
|
2862
3203
|
exports.RpcTarget = RpcTarget;
|
|
3204
|
+
exports.WebSocketTransport = WebSocketTransport;
|
|
2863
3205
|
exports.deserialize = deserialize;
|
|
2864
3206
|
exports.newHttpBatchRpcResponse = newHttpBatchRpcResponse;
|
|
2865
3207
|
exports.newHttpBatchRpcSession = newHttpBatchRpcSession;
|