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-bun.cjs
CHANGED
|
@@ -43,9 +43,22 @@ function typeForRpc(value) {
|
|
|
43
43
|
case Array.prototype: return "array";
|
|
44
44
|
case Date.prototype: return "date";
|
|
45
45
|
case Uint8Array.prototype:
|
|
46
|
-
case BUFFER_PROTOTYPE:
|
|
46
|
+
case BUFFER_PROTOTYPE:
|
|
47
|
+
case ArrayBuffer.prototype:
|
|
48
|
+
case DataView.prototype:
|
|
49
|
+
case Int8Array.prototype:
|
|
50
|
+
case Uint8ClampedArray.prototype:
|
|
51
|
+
case Int16Array.prototype:
|
|
52
|
+
case Uint16Array.prototype:
|
|
53
|
+
case Int32Array.prototype:
|
|
54
|
+
case Uint32Array.prototype:
|
|
55
|
+
case BigInt64Array.prototype:
|
|
56
|
+
case BigUint64Array.prototype:
|
|
57
|
+
case Float32Array.prototype:
|
|
58
|
+
case Float64Array.prototype: return "bytes";
|
|
47
59
|
case WritableStream.prototype: return "writable";
|
|
48
60
|
case ReadableStream.prototype: return "readable";
|
|
61
|
+
case URL.prototype: return "url";
|
|
49
62
|
case Headers.prototype: return "headers";
|
|
50
63
|
case Request.prototype: return "request";
|
|
51
64
|
case Response.prototype: return "response";
|
|
@@ -79,7 +92,14 @@ let streamImpl = {
|
|
|
79
92
|
};
|
|
80
93
|
var StubHook = class {
|
|
81
94
|
stream(path, args) {
|
|
82
|
-
let
|
|
95
|
+
let hook = this.call(path, args);
|
|
96
|
+
let pulled;
|
|
97
|
+
try {
|
|
98
|
+
pulled = hook.pull();
|
|
99
|
+
} catch (err) {
|
|
100
|
+
hook.dispose();
|
|
101
|
+
throw err;
|
|
102
|
+
}
|
|
83
103
|
let promise;
|
|
84
104
|
if (pulled instanceof Promise) promise = pulled.then((p) => {
|
|
85
105
|
p.dispose();
|
|
@@ -98,9 +118,11 @@ var ErrorStubHook = class extends StubHook {
|
|
|
98
118
|
this.error = error;
|
|
99
119
|
}
|
|
100
120
|
call(path, args) {
|
|
121
|
+
args.dispose();
|
|
101
122
|
return this;
|
|
102
123
|
}
|
|
103
124
|
map(path, captures, instructions) {
|
|
125
|
+
for (let cap of captures) cap.dispose();
|
|
104
126
|
return this;
|
|
105
127
|
}
|
|
106
128
|
get(path) {
|
|
@@ -380,6 +402,7 @@ var RpcPayload = class RpcPayload {
|
|
|
380
402
|
case "date":
|
|
381
403
|
case "bytes":
|
|
382
404
|
case "blob":
|
|
405
|
+
case "url":
|
|
383
406
|
case "error":
|
|
384
407
|
case "undefined": return value;
|
|
385
408
|
case "array": {
|
|
@@ -513,6 +536,30 @@ var RpcPayload = class RpcPayload {
|
|
|
513
536
|
this.dispose();
|
|
514
537
|
}
|
|
515
538
|
}
|
|
539
|
+
async deliverStreamWrite(writer) {
|
|
540
|
+
try {
|
|
541
|
+
let promises = [];
|
|
542
|
+
this.deliverTo(this, "value", promises);
|
|
543
|
+
if (promises.length > 0) await Promise.all(promises);
|
|
544
|
+
let chunk = this.value[0];
|
|
545
|
+
if ((this.hooks.length > 0 || this.promises.length > 0) && chunk instanceof Object) {
|
|
546
|
+
if (!(Symbol.dispose in chunk)) Object.defineProperty(chunk, Symbol.dispose, {
|
|
547
|
+
value: () => this.dispose(),
|
|
548
|
+
writable: true,
|
|
549
|
+
enumerable: false,
|
|
550
|
+
configurable: true
|
|
551
|
+
});
|
|
552
|
+
await writer.write(chunk);
|
|
553
|
+
return RpcPayload.fromAppReturn(void 0);
|
|
554
|
+
}
|
|
555
|
+
await writer.write(chunk);
|
|
556
|
+
this.dispose();
|
|
557
|
+
return RpcPayload.fromAppReturn(void 0);
|
|
558
|
+
} catch (err) {
|
|
559
|
+
this.dispose();
|
|
560
|
+
throw err;
|
|
561
|
+
}
|
|
562
|
+
}
|
|
516
563
|
async deliverResolve() {
|
|
517
564
|
try {
|
|
518
565
|
let promises = [];
|
|
@@ -553,6 +600,7 @@ var RpcPayload = class RpcPayload {
|
|
|
553
600
|
case "bytes":
|
|
554
601
|
case "blob":
|
|
555
602
|
case "date":
|
|
603
|
+
case "url":
|
|
556
604
|
case "error":
|
|
557
605
|
case "undefined": return;
|
|
558
606
|
case "array": {
|
|
@@ -635,6 +683,7 @@ var RpcPayload = class RpcPayload {
|
|
|
635
683
|
case "rpc-target":
|
|
636
684
|
case "writable":
|
|
637
685
|
case "readable":
|
|
686
|
+
case "url":
|
|
638
687
|
case "headers":
|
|
639
688
|
case "request":
|
|
640
689
|
case "response": return;
|
|
@@ -704,6 +753,7 @@ function followPath(value, parent, path, owner) {
|
|
|
704
753
|
case "blob":
|
|
705
754
|
case "date":
|
|
706
755
|
case "error":
|
|
756
|
+
case "url":
|
|
707
757
|
case "headers":
|
|
708
758
|
case "request":
|
|
709
759
|
case "response":
|
|
@@ -736,17 +786,22 @@ function followPath(value, parent, path, owner) {
|
|
|
736
786
|
}
|
|
737
787
|
var ValueStubHook = class extends StubHook {
|
|
738
788
|
call(path, args) {
|
|
789
|
+
let followResult;
|
|
739
790
|
try {
|
|
740
791
|
let { value, owner } = this.getValue();
|
|
741
|
-
|
|
742
|
-
if (followResult.hook) return followResult.hook.call(followResult.remainingPath, args);
|
|
743
|
-
if (typeof followResult.value != "function") throw new TypeError(`'${path.join(".")}' is not a function.`);
|
|
744
|
-
return new PromiseStubHook(args.deliverCall(followResult.value, followResult.parent).then((payload) => {
|
|
745
|
-
return new PayloadStubHook(payload);
|
|
746
|
-
}));
|
|
792
|
+
followResult = followPath(value, void 0, path, owner);
|
|
747
793
|
} catch (err) {
|
|
794
|
+
args.dispose();
|
|
748
795
|
return new ErrorStubHook(err);
|
|
749
796
|
}
|
|
797
|
+
if (followResult.hook) return followResult.hook.call(followResult.remainingPath, args);
|
|
798
|
+
if (typeof followResult.value != "function") {
|
|
799
|
+
args.dispose();
|
|
800
|
+
return new ErrorStubHook(/* @__PURE__ */ new TypeError(`'${path.join(".")}' is not a function.`));
|
|
801
|
+
}
|
|
802
|
+
return new PromiseStubHook(args.deliverCall(followResult.value, followResult.parent).then((payload) => {
|
|
803
|
+
return new PayloadStubHook(payload);
|
|
804
|
+
}));
|
|
750
805
|
}
|
|
751
806
|
map(path, captures, instructions) {
|
|
752
807
|
try {
|
|
@@ -884,12 +939,16 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
|
884
939
|
}
|
|
885
940
|
call(path, args) {
|
|
886
941
|
args.ensureDeepCopied();
|
|
887
|
-
return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args))
|
|
942
|
+
return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args), (err) => {
|
|
943
|
+
args.dispose();
|
|
944
|
+
throw err;
|
|
945
|
+
}));
|
|
888
946
|
}
|
|
889
947
|
stream(path, args) {
|
|
890
948
|
args.ensureDeepCopied();
|
|
891
|
-
return { promise: this.promise.then((hook) => {
|
|
892
|
-
|
|
949
|
+
return { promise: this.promise.then((hook) => hook.stream(path, args).promise, (err) => {
|
|
950
|
+
args.dispose();
|
|
951
|
+
throw err;
|
|
893
952
|
}) };
|
|
894
953
|
}
|
|
895
954
|
map(path, captures, instructions) {
|
|
@@ -916,10 +975,7 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
|
916
975
|
}, (err) => {});
|
|
917
976
|
}
|
|
918
977
|
dispose() {
|
|
919
|
-
|
|
920
|
-
else this.promise.then((hook) => {
|
|
921
|
-
hook.dispose();
|
|
922
|
-
}, (err) => {});
|
|
978
|
+
this.promise.then((hook) => hook.dispose(), () => {});
|
|
923
979
|
}
|
|
924
980
|
onBroken(callback) {
|
|
925
981
|
if (this.resolution) this.resolution.onBroken(callback);
|
|
@@ -931,6 +987,77 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
|
931
987
|
|
|
932
988
|
//#endregion
|
|
933
989
|
//#region src/serialize.ts
|
|
990
|
+
const DEFAULT_MAX_DEPTH = 256;
|
|
991
|
+
const DEFAULT_LIMITS = {
|
|
992
|
+
maxBigIntDigits: 16384,
|
|
993
|
+
maxDepth: 256,
|
|
994
|
+
maxMessageSize: 32 * 1024 * 1024
|
|
995
|
+
};
|
|
996
|
+
const NATIVE_LITTLE_ENDIAN = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
|
|
997
|
+
const BYTE_CONTAINER_TYPE_NAMES = [
|
|
998
|
+
"ArrayBuffer",
|
|
999
|
+
"DataView",
|
|
1000
|
+
"Int8Array",
|
|
1001
|
+
"Uint8Array",
|
|
1002
|
+
"Uint8ClampedArray",
|
|
1003
|
+
"Int16Array",
|
|
1004
|
+
"Uint16Array",
|
|
1005
|
+
"Int32Array",
|
|
1006
|
+
"Uint32Array",
|
|
1007
|
+
"BigInt64Array",
|
|
1008
|
+
"BigUint64Array",
|
|
1009
|
+
"Float32Array",
|
|
1010
|
+
"Float64Array"
|
|
1011
|
+
];
|
|
1012
|
+
function isValidByteContainerName(value) {
|
|
1013
|
+
return BYTE_CONTAINER_TYPE_NAMES.includes(value);
|
|
1014
|
+
}
|
|
1015
|
+
const TYPED_ARRAY_ELEMENT_SIZE = {
|
|
1016
|
+
ArrayBuffer: void 0,
|
|
1017
|
+
DataView: void 0,
|
|
1018
|
+
Int8Array: void 0,
|
|
1019
|
+
Uint8Array: void 0,
|
|
1020
|
+
Uint8ClampedArray: void 0,
|
|
1021
|
+
Int16Array: 2,
|
|
1022
|
+
Uint16Array: 2,
|
|
1023
|
+
Int32Array: 4,
|
|
1024
|
+
Uint32Array: 4,
|
|
1025
|
+
BigInt64Array: 8,
|
|
1026
|
+
BigUint64Array: 8,
|
|
1027
|
+
Float32Array: 4,
|
|
1028
|
+
Float64Array: 8
|
|
1029
|
+
};
|
|
1030
|
+
const BYTE_CONTAINER_PROTOTYPES = {
|
|
1031
|
+
ArrayBuffer: ArrayBuffer.prototype,
|
|
1032
|
+
DataView: DataView.prototype,
|
|
1033
|
+
Int8Array: Int8Array.prototype,
|
|
1034
|
+
Uint8ClampedArray: Uint8ClampedArray.prototype,
|
|
1035
|
+
Int16Array: Int16Array.prototype,
|
|
1036
|
+
Uint16Array: Uint16Array.prototype,
|
|
1037
|
+
Int32Array: Int32Array.prototype,
|
|
1038
|
+
Uint32Array: Uint32Array.prototype,
|
|
1039
|
+
BigInt64Array: BigInt64Array.prototype,
|
|
1040
|
+
BigUint64Array: BigUint64Array.prototype,
|
|
1041
|
+
Float32Array: Float32Array.prototype,
|
|
1042
|
+
Float64Array: Float64Array.prototype
|
|
1043
|
+
};
|
|
1044
|
+
const BYTE_CONTAINER_TYPE_BY_PROTOTYPE = /* @__PURE__ */ new Map();
|
|
1045
|
+
for (let type of Object.keys(BYTE_CONTAINER_PROTOTYPES)) BYTE_CONTAINER_TYPE_BY_PROTOTYPE.set(BYTE_CONTAINER_PROTOTYPES[type], type);
|
|
1046
|
+
function swapByteOrder(bytes, elementSize) {
|
|
1047
|
+
if (elementSize !== 2 && elementSize !== 4 && elementSize !== 8) throw new RangeError(`Unsupported element size: ${elementSize}`);
|
|
1048
|
+
let view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
1049
|
+
for (let offset = 0; offset < bytes.byteLength; offset += elementSize) switch (elementSize) {
|
|
1050
|
+
case 2:
|
|
1051
|
+
view.setUint16(offset, view.getUint16(offset, false), true);
|
|
1052
|
+
break;
|
|
1053
|
+
case 4:
|
|
1054
|
+
view.setUint32(offset, view.getUint32(offset, false), true);
|
|
1055
|
+
break;
|
|
1056
|
+
case 8:
|
|
1057
|
+
view.setBigUint64(offset, view.getBigUint64(offset, false), true);
|
|
1058
|
+
break;
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
934
1061
|
var NullExporter = class {
|
|
935
1062
|
exportStub(stub) {
|
|
936
1063
|
throw new Error("Cannot serialize RPC stubs without an RPC session.");
|
|
@@ -951,6 +1078,7 @@ async function streamToBlob(stream, type) {
|
|
|
951
1078
|
return b.type === type ? b : b.slice(0, b.size, type);
|
|
952
1079
|
}
|
|
953
1080
|
const ERROR_TYPES = {
|
|
1081
|
+
__proto__: null,
|
|
954
1082
|
Error,
|
|
955
1083
|
EvalError,
|
|
956
1084
|
RangeError,
|
|
@@ -963,12 +1091,14 @@ const ERROR_TYPES = {
|
|
|
963
1091
|
var Devaluator = class Devaluator {
|
|
964
1092
|
exporter;
|
|
965
1093
|
source;
|
|
966
|
-
|
|
1094
|
+
encodingLevel;
|
|
1095
|
+
constructor(exporter, source, encodingLevel) {
|
|
967
1096
|
this.exporter = exporter;
|
|
968
1097
|
this.source = source;
|
|
1098
|
+
this.encodingLevel = encodingLevel;
|
|
969
1099
|
}
|
|
970
|
-
static devaluate(value, parent, exporter = NULL_EXPORTER, source) {
|
|
971
|
-
let devaluator = new Devaluator(exporter, source);
|
|
1100
|
+
static devaluate(value, parent, exporter = NULL_EXPORTER, source, encodingLevel = "string") {
|
|
1101
|
+
let devaluator = new Devaluator(exporter, source, encodingLevel);
|
|
972
1102
|
try {
|
|
973
1103
|
return devaluator.devaluateImpl(value, parent, 0);
|
|
974
1104
|
} catch (err) {
|
|
@@ -980,7 +1110,7 @@ var Devaluator = class Devaluator {
|
|
|
980
1110
|
}
|
|
981
1111
|
exports;
|
|
982
1112
|
devaluateImpl(value, parent, depth) {
|
|
983
|
-
if (depth >=
|
|
1113
|
+
if (depth >= 256) throw new Error("Serialization exceeded maximum allowed depth. (Does the message contain cycles?)");
|
|
984
1114
|
switch (typeForRpc(value)) {
|
|
985
1115
|
case "unsupported": {
|
|
986
1116
|
let msg;
|
|
@@ -991,10 +1121,12 @@ var Devaluator = class Devaluator {
|
|
|
991
1121
|
}
|
|
992
1122
|
throw new TypeError(msg);
|
|
993
1123
|
}
|
|
994
|
-
case "primitive": if (typeof value === "number" && !isFinite(value))
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
1124
|
+
case "primitive": if (typeof value === "number" && !isFinite(value)) {
|
|
1125
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1126
|
+
if (value === Infinity) return ["inf"];
|
|
1127
|
+
else if (value === -Infinity) return ["-inf"];
|
|
1128
|
+
else return ["nan"];
|
|
1129
|
+
} else return value;
|
|
998
1130
|
case "object": {
|
|
999
1131
|
let object = value;
|
|
1000
1132
|
let result = {};
|
|
@@ -1008,23 +1140,49 @@ var Devaluator = class Devaluator {
|
|
|
1008
1140
|
for (let i = 0; i < len; i++) result[i] = this.devaluateImpl(array[i], array, depth + 1);
|
|
1009
1141
|
return [result];
|
|
1010
1142
|
}
|
|
1011
|
-
case "bigint":
|
|
1143
|
+
case "bigint":
|
|
1144
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1145
|
+
return ["bigint", value.toString()];
|
|
1012
1146
|
case "date": {
|
|
1147
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1013
1148
|
const time = value.getTime();
|
|
1014
1149
|
return ["date", Number.isNaN(time) ? null : time];
|
|
1015
1150
|
}
|
|
1016
1151
|
case "bytes": {
|
|
1017
|
-
let
|
|
1018
|
-
|
|
1152
|
+
let alternateTypeName = BYTE_CONTAINER_TYPE_BY_PROTOTYPE.get(Object.getPrototypeOf(value));
|
|
1153
|
+
let bytes;
|
|
1154
|
+
if (alternateTypeName === "ArrayBuffer") bytes = new Uint8Array(value);
|
|
1155
|
+
else if (alternateTypeName === void 0) bytes = value;
|
|
1156
|
+
else {
|
|
1157
|
+
let view = value;
|
|
1158
|
+
bytes = new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
|
|
1159
|
+
let elementSize = TYPED_ARRAY_ELEMENT_SIZE[alternateTypeName];
|
|
1160
|
+
if (!NATIVE_LITTLE_ENDIAN && elementSize) {
|
|
1161
|
+
bytes = bytes.slice();
|
|
1162
|
+
swapByteOrder(bytes, elementSize);
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
if (this.encodingLevel === "structuredClonable" || this.encodingLevel === "jsonCompatibleWithBytes") return alternateTypeName === void 0 ? ["bytes", bytes] : [
|
|
1166
|
+
"bytes",
|
|
1167
|
+
bytes,
|
|
1168
|
+
alternateTypeName
|
|
1169
|
+
];
|
|
1019
1170
|
let b64;
|
|
1020
|
-
if (
|
|
1171
|
+
if (bytes.toBase64) b64 = bytes.toBase64({ omitPadding: true });
|
|
1172
|
+
else if (typeof Buffer !== "undefined") b64 = (bytes instanceof Buffer ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)).toString("base64");
|
|
1021
1173
|
else {
|
|
1022
1174
|
let binary = "";
|
|
1023
1175
|
for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]);
|
|
1024
1176
|
b64 = btoa(binary);
|
|
1025
1177
|
}
|
|
1026
|
-
|
|
1178
|
+
b64 = b64.replace(/=+$/, "");
|
|
1179
|
+
return alternateTypeName === void 0 ? ["bytes", b64] : [
|
|
1180
|
+
"bytes",
|
|
1181
|
+
b64,
|
|
1182
|
+
alternateTypeName
|
|
1183
|
+
];
|
|
1027
1184
|
}
|
|
1185
|
+
case "url": return ["url", value.href];
|
|
1028
1186
|
case "headers": return ["headers", [...value]];
|
|
1029
1187
|
case "request": {
|
|
1030
1188
|
let req = value;
|
|
@@ -1139,7 +1297,9 @@ var Devaluator = class Devaluator {
|
|
|
1139
1297
|
} else if (rewritten && rewritten.stack) result.push(rewritten.stack);
|
|
1140
1298
|
return result;
|
|
1141
1299
|
}
|
|
1142
|
-
case "undefined":
|
|
1300
|
+
case "undefined":
|
|
1301
|
+
if (this.encodingLevel === "structuredClonable") return;
|
|
1302
|
+
return ["undefined"];
|
|
1143
1303
|
case "stub":
|
|
1144
1304
|
case "rpc-promise": {
|
|
1145
1305
|
if (!this.source) throw new Error("Can't serialize RPC stubs in this context.");
|
|
@@ -1206,6 +1366,9 @@ var NullImporter = class {
|
|
|
1206
1366
|
getPipeReadable(exportId) {
|
|
1207
1367
|
throw new Error("Cannot retrieve pipe readable without an RPC session.");
|
|
1208
1368
|
}
|
|
1369
|
+
getLimits() {
|
|
1370
|
+
return DEFAULT_LIMITS;
|
|
1371
|
+
}
|
|
1209
1372
|
};
|
|
1210
1373
|
const NULL_IMPORTER = new NullImporter();
|
|
1211
1374
|
function fixBrokenRequestBody(request, body) {
|
|
@@ -1222,15 +1385,22 @@ function streamToBlobPromise(stream, type) {
|
|
|
1222
1385
|
}
|
|
1223
1386
|
var Evaluator = class Evaluator {
|
|
1224
1387
|
importer;
|
|
1225
|
-
|
|
1388
|
+
encodingLevel;
|
|
1389
|
+
limits;
|
|
1390
|
+
constructor(importer, encodingLevel = "string") {
|
|
1226
1391
|
this.importer = importer;
|
|
1392
|
+
this.encodingLevel = encodingLevel;
|
|
1393
|
+
this.limits = importer.getLimits();
|
|
1227
1394
|
}
|
|
1228
1395
|
hooks = [];
|
|
1229
1396
|
promises = [];
|
|
1230
1397
|
evaluate(value) {
|
|
1398
|
+
return this.evaluateWithDepth(value, 0);
|
|
1399
|
+
}
|
|
1400
|
+
evaluateWithDepth(value, depth) {
|
|
1231
1401
|
let payload = RpcPayload.forEvaluate(this.hooks, this.promises);
|
|
1232
1402
|
try {
|
|
1233
|
-
payload.value = this.evaluateImpl(value, payload, "value");
|
|
1403
|
+
payload.value = this.evaluateImpl(value, payload, "value", depth);
|
|
1234
1404
|
return payload;
|
|
1235
1405
|
} catch (err) {
|
|
1236
1406
|
payload.dispose();
|
|
@@ -1240,31 +1410,70 @@ var Evaluator = class Evaluator {
|
|
|
1240
1410
|
evaluateCopy(value) {
|
|
1241
1411
|
return this.evaluate(structuredClone(value));
|
|
1242
1412
|
}
|
|
1243
|
-
evaluateImpl(value, parent, property) {
|
|
1413
|
+
evaluateImpl(value, parent, property, depth) {
|
|
1414
|
+
let maxDepth = this.limits.maxDepth;
|
|
1415
|
+
if (depth >= maxDepth) throw new TypeError(`Deserialization exceeded maximum allowed message depth of ${maxDepth}.`);
|
|
1416
|
+
if (this.encodingLevel === "structuredClonable") {
|
|
1417
|
+
if (value instanceof Date || typeof value === "bigint") return value;
|
|
1418
|
+
}
|
|
1244
1419
|
if (value instanceof Array) {
|
|
1245
1420
|
if (value.length == 1 && value[0] instanceof Array) {
|
|
1246
1421
|
let result = value[0];
|
|
1247
|
-
for (let i = 0; i < result.length; i++) result[i] = this.evaluateImpl(result[i], result, i);
|
|
1422
|
+
for (let i = 0; i < result.length; i++) result[i] = this.evaluateImpl(result[i], result, i, depth + 1);
|
|
1248
1423
|
return result;
|
|
1249
1424
|
} else switch (value[0]) {
|
|
1250
1425
|
case "bigint":
|
|
1251
|
-
if (typeof value[1] == "string")
|
|
1426
|
+
if (typeof value[1] == "string") {
|
|
1427
|
+
let digits = value[1];
|
|
1428
|
+
let maxBigIntDigits = this.limits.maxBigIntDigits;
|
|
1429
|
+
if (digits.length > maxBigIntDigits) throw new TypeError(`Deserialized bigint exceeds maximum length of ${maxBigIntDigits} digits.`);
|
|
1430
|
+
return BigInt(digits);
|
|
1431
|
+
}
|
|
1252
1432
|
break;
|
|
1253
1433
|
case "date":
|
|
1254
1434
|
if (value[1] === null) return /* @__PURE__ */ new Date(NaN);
|
|
1255
1435
|
if (typeof value[1] == "number") return new Date(value[1]);
|
|
1256
1436
|
break;
|
|
1257
|
-
case "bytes":
|
|
1258
|
-
|
|
1259
|
-
|
|
1437
|
+
case "bytes": {
|
|
1438
|
+
let bytes;
|
|
1439
|
+
if (value[1] instanceof Uint8Array) bytes = value[1];
|
|
1440
|
+
else if (typeof value[1] == "string") if (typeof Buffer !== "undefined") bytes = Buffer.from(value[1], "base64");
|
|
1441
|
+
else if (Uint8Array.fromBase64) bytes = Uint8Array.fromBase64(value[1]);
|
|
1260
1442
|
else {
|
|
1261
1443
|
let bs = atob(value[1]);
|
|
1262
1444
|
let len = bs.length;
|
|
1263
|
-
|
|
1445
|
+
bytes = new Uint8Array(len);
|
|
1264
1446
|
for (let i = 0; i < len; i++) bytes[i] = bs.charCodeAt(i);
|
|
1265
|
-
return bytes;
|
|
1266
1447
|
}
|
|
1267
|
-
break;
|
|
1448
|
+
else break;
|
|
1449
|
+
if (value.length === 2) return bytes;
|
|
1450
|
+
if (typeof value[2] !== "string") throw new TypeError(`Unknown bytes type marker type: ${typeof value[2]}`);
|
|
1451
|
+
if (!isValidByteContainerName(value[2])) {
|
|
1452
|
+
let marker = value[2].slice(0, 64);
|
|
1453
|
+
throw new TypeError(`Unknown bytes type marker: ${marker}`);
|
|
1454
|
+
}
|
|
1455
|
+
let marker = value[2];
|
|
1456
|
+
let elementSize = TYPED_ARRAY_ELEMENT_SIZE[marker];
|
|
1457
|
+
if (elementSize !== void 0 && bytes.byteLength % elementSize !== 0) throw new TypeError(`Invalid byte length ${bytes.byteLength} for ${marker}; expected a multiple of ${elementSize}`);
|
|
1458
|
+
let buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
|
1459
|
+
if (!NATIVE_LITTLE_ENDIAN && elementSize !== void 0) swapByteOrder(new Uint8Array(buffer), elementSize);
|
|
1460
|
+
switch (marker) {
|
|
1461
|
+
case "ArrayBuffer": return buffer;
|
|
1462
|
+
case "DataView": return new DataView(buffer);
|
|
1463
|
+
case "Int8Array": return new Int8Array(buffer);
|
|
1464
|
+
case "Uint8Array": return new Uint8Array(buffer);
|
|
1465
|
+
case "Uint8ClampedArray": return new Uint8ClampedArray(buffer);
|
|
1466
|
+
case "Int16Array": return new Int16Array(buffer);
|
|
1467
|
+
case "Uint16Array": return new Uint16Array(buffer);
|
|
1468
|
+
case "Int32Array": return new Int32Array(buffer);
|
|
1469
|
+
case "Uint32Array": return new Uint32Array(buffer);
|
|
1470
|
+
case "BigInt64Array": return new BigInt64Array(buffer);
|
|
1471
|
+
case "BigUint64Array": return new BigUint64Array(buffer);
|
|
1472
|
+
case "Float32Array": return new Float32Array(buffer);
|
|
1473
|
+
case "Float64Array": return new Float64Array(buffer);
|
|
1474
|
+
default:
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1268
1477
|
case "error":
|
|
1269
1478
|
if (value.length >= 3 && typeof value[1] === "string" && typeof value[2] === "string") {
|
|
1270
1479
|
let cls = ERROR_TYPES[value[1]] || Error;
|
|
@@ -1277,7 +1486,11 @@ var Evaluator = class Evaluator {
|
|
|
1277
1486
|
let propsObj = props;
|
|
1278
1487
|
for (let key of Object.keys(propsObj)) {
|
|
1279
1488
|
if (key === "name" || key === "message" || key === "stack") continue;
|
|
1280
|
-
|
|
1489
|
+
if (key in Object.prototype || key === "toJSON") {
|
|
1490
|
+
this.evaluateImpl(propsObj[key], result, key, depth + 1);
|
|
1491
|
+
continue;
|
|
1492
|
+
}
|
|
1493
|
+
anyResult[key] = this.evaluateImpl(propsObj[key], result, key, depth + 1);
|
|
1281
1494
|
}
|
|
1282
1495
|
}
|
|
1283
1496
|
return result;
|
|
@@ -1289,6 +1502,9 @@ var Evaluator = class Evaluator {
|
|
|
1289
1502
|
case "inf": return Infinity;
|
|
1290
1503
|
case "-inf": return -Infinity;
|
|
1291
1504
|
case "nan": return NaN;
|
|
1505
|
+
case "url":
|
|
1506
|
+
if (value.length === 2 && typeof value[1] === "string") return new URL(value[1]);
|
|
1507
|
+
break;
|
|
1292
1508
|
case "headers":
|
|
1293
1509
|
if (value.length === 2 && value[1] instanceof Array) return new Headers(value[1]);
|
|
1294
1510
|
break;
|
|
@@ -1298,11 +1514,11 @@ var Evaluator = class Evaluator {
|
|
|
1298
1514
|
let init = value[2];
|
|
1299
1515
|
if (typeof init !== "object" || init === null) break;
|
|
1300
1516
|
if (init.body) {
|
|
1301
|
-
init.body = this.evaluateImpl(init.body, init, "body");
|
|
1517
|
+
init.body = this.evaluateImpl(init.body, init, "body", depth + 1);
|
|
1302
1518
|
if (init.body === null || typeof init.body === "string" || init.body instanceof Uint8Array || init.body instanceof ReadableStream) {} else throw new TypeError("Request body must be of type ReadableStream.");
|
|
1303
1519
|
}
|
|
1304
1520
|
if (init.signal) {
|
|
1305
|
-
init.signal = this.evaluateImpl(init.signal, init, "signal");
|
|
1521
|
+
init.signal = this.evaluateImpl(init.signal, init, "signal", depth + 1);
|
|
1306
1522
|
if (!(init.signal instanceof AbortSignal)) throw new TypeError("Request siganl must be of type AbortSignal.");
|
|
1307
1523
|
}
|
|
1308
1524
|
if (init.headers && !(init.headers instanceof Array)) throw new TypeError("Request headers must be serialized as an array of pairs.");
|
|
@@ -1319,7 +1535,7 @@ var Evaluator = class Evaluator {
|
|
|
1319
1535
|
}
|
|
1320
1536
|
case "response": {
|
|
1321
1537
|
if (value.length !== 3) break;
|
|
1322
|
-
let body = this.evaluateImpl(value[1], parent, property);
|
|
1538
|
+
let body = this.evaluateImpl(value[1], parent, property, depth + 1);
|
|
1323
1539
|
if (body === null || typeof body === "string" || body instanceof Uint8Array || body instanceof ReadableStream) {} else throw new TypeError("Response body must be of type ReadableStream.");
|
|
1324
1540
|
let init = value[2];
|
|
1325
1541
|
if (typeof init !== "object" || init === null) break;
|
|
@@ -1330,7 +1546,7 @@ var Evaluator = class Evaluator {
|
|
|
1330
1546
|
case "blob": {
|
|
1331
1547
|
if (value.length !== 3 || typeof value[1] !== "string") break;
|
|
1332
1548
|
let contentType = value[1];
|
|
1333
|
-
let content = this.evaluateImpl(value[2], parent, property);
|
|
1549
|
+
let content = this.evaluateImpl(value[2], parent, property, depth + 1);
|
|
1334
1550
|
if (!(content instanceof ReadableStream)) throw new TypeError("Blob content must be serialized as a ReadableStream.");
|
|
1335
1551
|
let promise = streamToBlobPromise(content, contentType);
|
|
1336
1552
|
this.promises.push({
|
|
@@ -1371,7 +1587,7 @@ var Evaluator = class Evaluator {
|
|
|
1371
1587
|
if (value.length == 3) return addStub(hook.get(path));
|
|
1372
1588
|
let args = value[3];
|
|
1373
1589
|
if (!(args instanceof Array)) break;
|
|
1374
|
-
args = new Evaluator(this.importer).
|
|
1590
|
+
args = new Evaluator(this.importer).evaluateWithDepth([args], depth);
|
|
1375
1591
|
return addStub(hook.call(path, args));
|
|
1376
1592
|
}
|
|
1377
1593
|
case "remap": {
|
|
@@ -1437,9 +1653,9 @@ var Evaluator = class Evaluator {
|
|
|
1437
1653
|
} else if (value instanceof Object) {
|
|
1438
1654
|
let result = value;
|
|
1439
1655
|
for (let key in result) if (key in Object.prototype || key === "toJSON") {
|
|
1440
|
-
this.evaluateImpl(result[key], result, key);
|
|
1656
|
+
this.evaluateImpl(result[key], result, key, depth + 1);
|
|
1441
1657
|
delete result[key];
|
|
1442
|
-
} else result[key] = this.evaluateImpl(result[key], result, key);
|
|
1658
|
+
} else result[key] = this.evaluateImpl(result[key], result, key, depth + 1);
|
|
1443
1659
|
return result;
|
|
1444
1660
|
} else return value;
|
|
1445
1661
|
}
|
|
@@ -1455,6 +1671,47 @@ function deserialize(value) {
|
|
|
1455
1671
|
|
|
1456
1672
|
//#endregion
|
|
1457
1673
|
//#region src/rpc.ts
|
|
1674
|
+
const ESTIMATED_OBJECT_OVERHEAD = 16;
|
|
1675
|
+
const ESTIMATED_ENTRY_OVERHEAD = 8;
|
|
1676
|
+
const ESTIMATED_BINARY_OVERHEAD = 16;
|
|
1677
|
+
const MAX_ESTIMATE_DEPTH = 64;
|
|
1678
|
+
function estimateStringSize(value) {
|
|
1679
|
+
return 2 + value.length * 3;
|
|
1680
|
+
}
|
|
1681
|
+
function estimateEncodedSize(value, seen, depth = 0) {
|
|
1682
|
+
if (depth >= MAX_ESTIMATE_DEPTH) return ESTIMATED_ENTRY_OVERHEAD;
|
|
1683
|
+
switch (typeof value) {
|
|
1684
|
+
case "string": return estimateStringSize(value);
|
|
1685
|
+
case "number": return 16;
|
|
1686
|
+
case "bigint": return 16;
|
|
1687
|
+
case "boolean": return 8;
|
|
1688
|
+
case "undefined": return 16;
|
|
1689
|
+
case "object": {
|
|
1690
|
+
if (value === null) return 8;
|
|
1691
|
+
if (ArrayBuffer.isView(value)) return ESTIMATED_BINARY_OVERHEAD + value.byteLength;
|
|
1692
|
+
if (value instanceof ArrayBuffer) return ESTIMATED_BINARY_OVERHEAD + value.byteLength;
|
|
1693
|
+
if (typeof Blob !== "undefined" && value instanceof Blob) return ESTIMATED_BINARY_OVERHEAD + value.size;
|
|
1694
|
+
if (value instanceof Date) return 16;
|
|
1695
|
+
seen ??= /* @__PURE__ */ new WeakSet();
|
|
1696
|
+
if (seen.has(value)) return ESTIMATED_ENTRY_OVERHEAD;
|
|
1697
|
+
seen.add(value);
|
|
1698
|
+
if (value instanceof Array) {
|
|
1699
|
+
let size = ESTIMATED_OBJECT_OVERHEAD;
|
|
1700
|
+
for (let item of value) size += ESTIMATED_ENTRY_OVERHEAD + estimateEncodedSize(item, seen, depth + 1);
|
|
1701
|
+
return size;
|
|
1702
|
+
}
|
|
1703
|
+
if (value instanceof Error) {
|
|
1704
|
+
let size = ESTIMATED_OBJECT_OVERHEAD + estimateStringSize(value.name) + estimateStringSize(value.message) + estimateStringSize(value.stack ?? "");
|
|
1705
|
+
for (let key of Object.keys(value)) size += ESTIMATED_ENTRY_OVERHEAD + estimateStringSize(key) + estimateEncodedSize(value[key], seen, depth + 1);
|
|
1706
|
+
return size;
|
|
1707
|
+
}
|
|
1708
|
+
let size = ESTIMATED_OBJECT_OVERHEAD;
|
|
1709
|
+
for (let key of Object.keys(value)) size += ESTIMATED_ENTRY_OVERHEAD + estimateStringSize(key) + estimateEncodedSize(value[key], seen, depth + 1);
|
|
1710
|
+
return size;
|
|
1711
|
+
}
|
|
1712
|
+
default: return 16;
|
|
1713
|
+
}
|
|
1714
|
+
}
|
|
1458
1715
|
var ImportTableEntry = class {
|
|
1459
1716
|
session;
|
|
1460
1717
|
importId;
|
|
@@ -1547,24 +1804,28 @@ var RpcImportHook = class RpcImportHook extends StubHook {
|
|
|
1547
1804
|
if (this.entry) return this.entry;
|
|
1548
1805
|
else throw new Error("This RpcImportHook was already disposed.");
|
|
1549
1806
|
}
|
|
1807
|
+
getEntryTakingOwnership(disposeOwned) {
|
|
1808
|
+
try {
|
|
1809
|
+
return this.getEntry();
|
|
1810
|
+
} catch (err) {
|
|
1811
|
+
disposeOwned();
|
|
1812
|
+
throw err;
|
|
1813
|
+
}
|
|
1814
|
+
}
|
|
1550
1815
|
call(path, args) {
|
|
1551
|
-
let entry = this.
|
|
1816
|
+
let entry = this.getEntryTakingOwnership(() => args.dispose());
|
|
1552
1817
|
if (entry.resolution) return entry.resolution.call(path, args);
|
|
1553
1818
|
else return entry.session.sendCall(entry.importId, path, args);
|
|
1554
1819
|
}
|
|
1555
1820
|
stream(path, args) {
|
|
1556
|
-
let entry = this.
|
|
1821
|
+
let entry = this.getEntryTakingOwnership(() => args.dispose());
|
|
1557
1822
|
if (entry.resolution) return entry.resolution.stream(path, args);
|
|
1558
1823
|
else return entry.session.sendStream(entry.importId, path, args);
|
|
1559
1824
|
}
|
|
1560
1825
|
map(path, captures, instructions) {
|
|
1561
|
-
let entry
|
|
1562
|
-
try {
|
|
1563
|
-
entry = this.getEntry();
|
|
1564
|
-
} catch (err) {
|
|
1826
|
+
let entry = this.getEntryTakingOwnership(() => {
|
|
1565
1827
|
for (let cap of captures) cap.dispose();
|
|
1566
|
-
|
|
1567
|
-
}
|
|
1828
|
+
});
|
|
1568
1829
|
if (entry.resolution) return entry.resolution.map(path, captures, instructions);
|
|
1569
1830
|
else return entry.session.sendMap(entry.importId, path, captures, instructions);
|
|
1570
1831
|
}
|
|
@@ -1620,9 +1881,24 @@ var RpcSessionImpl = class {
|
|
|
1620
1881
|
onBatchDone;
|
|
1621
1882
|
pullCount = 0;
|
|
1622
1883
|
onBrokenCallbacks = [];
|
|
1884
|
+
encodingLevel;
|
|
1885
|
+
limits;
|
|
1623
1886
|
constructor(transport, mainHook, options) {
|
|
1624
1887
|
this.transport = transport;
|
|
1625
1888
|
this.options = options;
|
|
1889
|
+
let level = "string";
|
|
1890
|
+
if ("encodingLevel" in transport) {
|
|
1891
|
+
let raw = transport.encodingLevel;
|
|
1892
|
+
if (raw !== void 0) {
|
|
1893
|
+
if (raw !== "string" && raw !== "jsonCompatible" && raw !== "jsonCompatibleWithBytes" && raw !== "structuredClonable") throw new TypeError(`Unknown transport encodingLevel: ${String(raw)}`);
|
|
1894
|
+
level = raw;
|
|
1895
|
+
}
|
|
1896
|
+
}
|
|
1897
|
+
this.encodingLevel = level;
|
|
1898
|
+
this.limits = {
|
|
1899
|
+
...DEFAULT_LIMITS,
|
|
1900
|
+
...options.limits
|
|
1901
|
+
};
|
|
1626
1902
|
this.exports.push({
|
|
1627
1903
|
hook: mainHook,
|
|
1628
1904
|
refcount: 1
|
|
@@ -1703,7 +1979,7 @@ var RpcSessionImpl = class {
|
|
|
1703
1979
|
let autoRelease = exp.autoRelease;
|
|
1704
1980
|
++this.pullCount;
|
|
1705
1981
|
exp.pull = resolve().then((payload) => {
|
|
1706
|
-
let value = Devaluator.devaluate(payload.value, void 0, this, payload);
|
|
1982
|
+
let value = Devaluator.devaluate(payload.value, void 0, this, payload, this.encodingLevel);
|
|
1707
1983
|
this.send([
|
|
1708
1984
|
"resolve",
|
|
1709
1985
|
exportId,
|
|
@@ -1714,7 +1990,7 @@ var RpcSessionImpl = class {
|
|
|
1714
1990
|
this.send([
|
|
1715
1991
|
"reject",
|
|
1716
1992
|
exportId,
|
|
1717
|
-
Devaluator.devaluate(error, void 0, this)
|
|
1993
|
+
Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)
|
|
1718
1994
|
]);
|
|
1719
1995
|
if (autoRelease) this.releaseExport(exportId, 1);
|
|
1720
1996
|
}).catch((error) => {
|
|
@@ -1722,7 +1998,7 @@ var RpcSessionImpl = class {
|
|
|
1722
1998
|
this.send([
|
|
1723
1999
|
"reject",
|
|
1724
2000
|
exportId,
|
|
1725
|
-
Devaluator.devaluate(error, void 0, this)
|
|
2001
|
+
Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)
|
|
1726
2002
|
]);
|
|
1727
2003
|
if (autoRelease) this.releaseExport(exportId, 1);
|
|
1728
2004
|
} catch (error2) {
|
|
@@ -1765,6 +2041,9 @@ var RpcSessionImpl = class {
|
|
|
1765
2041
|
entry.pipeReadable = void 0;
|
|
1766
2042
|
return readable;
|
|
1767
2043
|
}
|
|
2044
|
+
getLimits() {
|
|
2045
|
+
return this.limits;
|
|
2046
|
+
}
|
|
1768
2047
|
createPipe(readable, readableHook) {
|
|
1769
2048
|
if (this.abortReason) throw this.abortReason;
|
|
1770
2049
|
this.send(["pipe"]);
|
|
@@ -1778,27 +2057,52 @@ var RpcSessionImpl = class {
|
|
|
1778
2057
|
}
|
|
1779
2058
|
send(msg) {
|
|
1780
2059
|
if (this.abortReason !== void 0) return 0;
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
msgText = JSON.stringify(msg);
|
|
1784
|
-
} catch (err) {
|
|
2060
|
+
if (this.encodingLevel === "string") {
|
|
2061
|
+
let msgText;
|
|
1785
2062
|
try {
|
|
1786
|
-
|
|
1787
|
-
} catch (
|
|
1788
|
-
|
|
2063
|
+
msgText = JSON.stringify(msg);
|
|
2064
|
+
} catch (err) {
|
|
2065
|
+
try {
|
|
2066
|
+
this.abort(err);
|
|
2067
|
+
} catch (err2) {}
|
|
2068
|
+
throw err;
|
|
2069
|
+
}
|
|
2070
|
+
try {
|
|
2071
|
+
let sent = this.transport.send(msgText);
|
|
2072
|
+
if (sent !== void 0 && typeof sent.catch === "function") sent.catch((err) => this.abort(err, false));
|
|
2073
|
+
} catch (err) {
|
|
2074
|
+
queueMicrotask(() => this.abort(err, false));
|
|
2075
|
+
}
|
|
2076
|
+
return msgText.length;
|
|
2077
|
+
} else try {
|
|
2078
|
+
let size = this.transport.send(msg);
|
|
2079
|
+
if (typeof size === "number") return size;
|
|
2080
|
+
let thenable = size;
|
|
2081
|
+
if (thenable && typeof thenable.then === "function") Promise.resolve(thenable).catch((err) => this.abort(err, false));
|
|
2082
|
+
return;
|
|
2083
|
+
} catch (err) {
|
|
2084
|
+
queueMicrotask(() => this.abort(err, false));
|
|
2085
|
+
return;
|
|
1789
2086
|
}
|
|
1790
|
-
this.transport.send(msgText).catch((err) => this.abort(err, false));
|
|
1791
|
-
return msgText.length;
|
|
1792
2087
|
}
|
|
1793
2088
|
sendCall(id, path, args) {
|
|
1794
|
-
if (this.abortReason)
|
|
2089
|
+
if (this.abortReason) {
|
|
2090
|
+
args?.dispose();
|
|
2091
|
+
throw this.abortReason;
|
|
2092
|
+
}
|
|
1795
2093
|
let value = [
|
|
1796
2094
|
"pipeline",
|
|
1797
2095
|
id,
|
|
1798
2096
|
path
|
|
1799
2097
|
];
|
|
1800
2098
|
if (args) {
|
|
1801
|
-
let devalue
|
|
2099
|
+
let devalue;
|
|
2100
|
+
try {
|
|
2101
|
+
devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
|
|
2102
|
+
} catch (err) {
|
|
2103
|
+
args.dispose();
|
|
2104
|
+
throw err;
|
|
2105
|
+
}
|
|
1802
2106
|
value.push(devalue[0]);
|
|
1803
2107
|
}
|
|
1804
2108
|
this.send(["push", value]);
|
|
@@ -1807,15 +2111,26 @@ var RpcSessionImpl = class {
|
|
|
1807
2111
|
return new RpcImportHook(true, entry);
|
|
1808
2112
|
}
|
|
1809
2113
|
sendStream(id, path, args) {
|
|
1810
|
-
if (this.abortReason)
|
|
2114
|
+
if (this.abortReason) {
|
|
2115
|
+
args.dispose();
|
|
2116
|
+
throw this.abortReason;
|
|
2117
|
+
}
|
|
1811
2118
|
let value = [
|
|
1812
2119
|
"pipeline",
|
|
1813
2120
|
id,
|
|
1814
2121
|
path
|
|
1815
2122
|
];
|
|
1816
|
-
let devalue
|
|
2123
|
+
let devalue;
|
|
2124
|
+
try {
|
|
2125
|
+
devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
|
|
2126
|
+
} catch (err) {
|
|
2127
|
+
args.dispose();
|
|
2128
|
+
throw err;
|
|
2129
|
+
}
|
|
1817
2130
|
value.push(devalue[0]);
|
|
1818
|
-
let
|
|
2131
|
+
let msg = ["stream", value];
|
|
2132
|
+
let size = this.send(msg);
|
|
2133
|
+
if (size === void 0) size = estimateEncodedSize(msg);
|
|
1819
2134
|
let importId = this.imports.length;
|
|
1820
2135
|
let entry = new ImportTableEntry(this, importId, true);
|
|
1821
2136
|
entry.remoteRefcount = 0;
|
|
@@ -1871,7 +2186,14 @@ var RpcSessionImpl = class {
|
|
|
1871
2186
|
this.cancelReadLoop?.(error);
|
|
1872
2187
|
this.cancelReadLoop = void 0;
|
|
1873
2188
|
if (trySendAbortMessage) try {
|
|
1874
|
-
|
|
2189
|
+
let abortMsg = ["abort", Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)];
|
|
2190
|
+
if (this.encodingLevel === "string") {
|
|
2191
|
+
let sent = this.transport.send(JSON.stringify(abortMsg));
|
|
2192
|
+
if (sent !== void 0 && typeof sent.catch === "function") sent.catch((err) => {});
|
|
2193
|
+
} else {
|
|
2194
|
+
let result = this.transport.send(abortMsg);
|
|
2195
|
+
if (result && typeof result.then === "function") Promise.resolve(result).catch((err) => {});
|
|
2196
|
+
}
|
|
1875
2197
|
} catch (err) {}
|
|
1876
2198
|
if (error === void 0) error = "undefined";
|
|
1877
2199
|
this.abortReason = error;
|
|
@@ -1893,18 +2215,19 @@ var RpcSessionImpl = class {
|
|
|
1893
2215
|
while (!this.abortReason) {
|
|
1894
2216
|
let readCanceled = Promise.withResolvers();
|
|
1895
2217
|
this.cancelReadLoop = readCanceled.reject;
|
|
1896
|
-
let
|
|
2218
|
+
let raw;
|
|
1897
2219
|
try {
|
|
1898
|
-
|
|
2220
|
+
raw = await Promise.race([this.transport.receive(), readCanceled.promise]);
|
|
1899
2221
|
} finally {
|
|
1900
2222
|
if (this.cancelReadLoop === readCanceled.reject) this.cancelReadLoop = void 0;
|
|
1901
2223
|
}
|
|
1902
|
-
|
|
2224
|
+
if (this.encodingLevel === "string" && raw.length > this.limits.maxMessageSize) throw new TypeError(`Incoming message exceeds maximum size of ${this.limits.maxMessageSize} UTF-16 code units.`);
|
|
1903
2225
|
if (this.abortReason) break;
|
|
2226
|
+
let msg = this.encodingLevel === "string" ? JSON.parse(raw) : raw;
|
|
1904
2227
|
if (msg instanceof Array) switch (msg[0]) {
|
|
1905
2228
|
case "push":
|
|
1906
2229
|
if (msg.length > 1) {
|
|
1907
|
-
let hook = new PayloadStubHook(new Evaluator(this).evaluate(msg[1]));
|
|
2230
|
+
let hook = new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[1]));
|
|
1908
2231
|
hook.ignoreUnhandledRejections();
|
|
1909
2232
|
this.exports.push({
|
|
1910
2233
|
hook,
|
|
@@ -1915,7 +2238,7 @@ var RpcSessionImpl = class {
|
|
|
1915
2238
|
break;
|
|
1916
2239
|
case "stream":
|
|
1917
2240
|
if (msg.length > 1) {
|
|
1918
|
-
let hook = new PayloadStubHook(new Evaluator(this).evaluate(msg[1]));
|
|
2241
|
+
let hook = new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[1]));
|
|
1919
2242
|
hook.ignoreUnhandledRejections();
|
|
1920
2243
|
let exportId = this.exports.length;
|
|
1921
2244
|
this.exports.push({
|
|
@@ -1950,13 +2273,13 @@ var RpcSessionImpl = class {
|
|
|
1950
2273
|
let importId = msg[1];
|
|
1951
2274
|
if (typeof importId == "number" && msg.length > 2) {
|
|
1952
2275
|
let imp = this.imports[importId];
|
|
1953
|
-
if (imp) if (msg[0] == "resolve") imp.resolve(new PayloadStubHook(new Evaluator(this).evaluate(msg[2])));
|
|
2276
|
+
if (imp) if (msg[0] == "resolve") imp.resolve(new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[2])));
|
|
1954
2277
|
else {
|
|
1955
|
-
let payload = new Evaluator(this).evaluate(msg[2]);
|
|
2278
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[2]);
|
|
1956
2279
|
payload.dispose();
|
|
1957
2280
|
imp.resolve(new ErrorStubHook(payload.value));
|
|
1958
2281
|
}
|
|
1959
|
-
else if (msg[0] == "resolve") new Evaluator(this).evaluate(msg[2]).dispose();
|
|
2282
|
+
else if (msg[0] == "resolve") new Evaluator(this, this.encodingLevel).evaluate(msg[2]).dispose();
|
|
1960
2283
|
continue;
|
|
1961
2284
|
}
|
|
1962
2285
|
break;
|
|
@@ -1971,9 +2294,9 @@ var RpcSessionImpl = class {
|
|
|
1971
2294
|
break;
|
|
1972
2295
|
}
|
|
1973
2296
|
case "abort": {
|
|
1974
|
-
let payload = new Evaluator(this).evaluate(msg[1]);
|
|
2297
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[1]);
|
|
1975
2298
|
payload.dispose();
|
|
1976
|
-
this.abort(payload, false);
|
|
2299
|
+
this.abort(payload.value, false);
|
|
1977
2300
|
break;
|
|
1978
2301
|
}
|
|
1979
2302
|
}
|
|
@@ -2024,6 +2347,8 @@ var RpcSession$1 = class {
|
|
|
2024
2347
|
|
|
2025
2348
|
//#endregion
|
|
2026
2349
|
//#region src/websocket.ts
|
|
2350
|
+
/** Close-frame reason max UTF-8 bytes: 125 payload - 2-byte status (RFC 6455 section 5.5). */
|
|
2351
|
+
const MAX_CLOSE_REASON_BYTES = 123;
|
|
2027
2352
|
function newWebSocketRpcSession$1(webSocket, localMain, options) {
|
|
2028
2353
|
if (typeof webSocket === "string") webSocket = new WebSocket(webSocket);
|
|
2029
2354
|
return new RpcSession$1(new WebSocketTransport(webSocket), localMain, options).getRemoteMain();
|
|
@@ -2043,9 +2368,14 @@ function newWorkersWebSocketRpcResponse(request, localMain, options) {
|
|
|
2043
2368
|
webSocket: pair[1]
|
|
2044
2369
|
});
|
|
2045
2370
|
}
|
|
2371
|
+
/**
|
|
2372
|
+
* Generic WebSocket transport. Default `T = string` is backward-compatible and satisfies
|
|
2373
|
+
* `RpcTransport`. Use `T = ArrayBuffer` as a building block for binary transports.
|
|
2374
|
+
*/
|
|
2046
2375
|
var WebSocketTransport = class {
|
|
2047
2376
|
constructor(webSocket) {
|
|
2048
2377
|
this.#webSocket = webSocket;
|
|
2378
|
+
webSocket.binaryType = "arraybuffer";
|
|
2049
2379
|
if (webSocket.readyState === WebSocket.CONNECTING) {
|
|
2050
2380
|
this.#sendQueue = [];
|
|
2051
2381
|
webSocket.addEventListener("open", (event) => {
|
|
@@ -2058,12 +2388,12 @@ var WebSocketTransport = class {
|
|
|
2058
2388
|
});
|
|
2059
2389
|
}
|
|
2060
2390
|
webSocket.addEventListener("message", (event) => {
|
|
2061
|
-
if (this.#error) {} else if (typeof event.data === "string") if (this.#receiveResolver) {
|
|
2391
|
+
if (this.#error) {} else if (typeof event.data === "string" || event.data instanceof ArrayBuffer) if (this.#receiveResolver) {
|
|
2062
2392
|
this.#receiveResolver(event.data);
|
|
2063
2393
|
this.#receiveResolver = void 0;
|
|
2064
2394
|
this.#receiveRejecter = void 0;
|
|
2065
2395
|
} else this.#receiveQueue.push(event.data);
|
|
2066
|
-
else this.#receivedError(/* @__PURE__ */ new TypeError("Received
|
|
2396
|
+
else this.#receivedError(/* @__PURE__ */ new TypeError("Received unexpected message type from WebSocket."));
|
|
2067
2397
|
});
|
|
2068
2398
|
webSocket.addEventListener("close", (event) => {
|
|
2069
2399
|
this.#receivedError(/* @__PURE__ */ new Error(`Peer closed WebSocket: ${event.code} ${event.reason}`));
|
|
@@ -2078,13 +2408,13 @@ var WebSocketTransport = class {
|
|
|
2078
2408
|
#receiveRejecter;
|
|
2079
2409
|
#receiveQueue = [];
|
|
2080
2410
|
#error;
|
|
2081
|
-
|
|
2411
|
+
send(message) {
|
|
2082
2412
|
if (this.#sendQueue === void 0) this.#webSocket.send(message);
|
|
2083
2413
|
else this.#sendQueue.push(message);
|
|
2084
2414
|
}
|
|
2085
|
-
|
|
2086
|
-
if (this.#receiveQueue.length > 0) return this.#receiveQueue.shift();
|
|
2087
|
-
else if (this.#error)
|
|
2415
|
+
receive() {
|
|
2416
|
+
if (this.#receiveQueue.length > 0) return Promise.resolve(this.#receiveQueue.shift());
|
|
2417
|
+
else if (this.#error) return Promise.reject(this.#error);
|
|
2088
2418
|
else return new Promise((resolve, reject) => {
|
|
2089
2419
|
this.#receiveResolver = resolve;
|
|
2090
2420
|
this.#receiveRejecter = reject;
|
|
@@ -2094,6 +2424,8 @@ var WebSocketTransport = class {
|
|
|
2094
2424
|
let message;
|
|
2095
2425
|
if (reason instanceof Error) message = reason.message;
|
|
2096
2426
|
else message = `${reason}`;
|
|
2427
|
+
let reasonBytes = new TextEncoder().encode(message);
|
|
2428
|
+
if (reasonBytes.length > 123) message = new TextDecoder().decode(reasonBytes.subarray(0, 123), { stream: true });
|
|
2097
2429
|
this.#webSocket.close(3e3, message);
|
|
2098
2430
|
if (!this.#error) this.#error = reason;
|
|
2099
2431
|
}
|
|
@@ -2111,6 +2443,7 @@ var WebSocketTransport = class {
|
|
|
2111
2443
|
|
|
2112
2444
|
//#endregion
|
|
2113
2445
|
//#region src/batch.ts
|
|
2446
|
+
const yieldToMacrotask = typeof setImmediate === "function" ? () => new Promise((resolve) => setImmediate(resolve)) : () => new Promise((resolve) => setTimeout(resolve, 0));
|
|
2114
2447
|
var BatchClientTransport = class {
|
|
2115
2448
|
constructor(sendBatch) {
|
|
2116
2449
|
this.#promise = this.#scheduleBatch(sendBatch);
|
|
@@ -2119,7 +2452,7 @@ var BatchClientTransport = class {
|
|
|
2119
2452
|
#aborted;
|
|
2120
2453
|
#batchToSend = [];
|
|
2121
2454
|
#batchToReceive = null;
|
|
2122
|
-
|
|
2455
|
+
send(message) {
|
|
2123
2456
|
if (this.#batchToSend !== null) this.#batchToSend.push(message);
|
|
2124
2457
|
}
|
|
2125
2458
|
async receive() {
|
|
@@ -2132,7 +2465,7 @@ var BatchClientTransport = class {
|
|
|
2132
2465
|
this.#aborted = reason;
|
|
2133
2466
|
}
|
|
2134
2467
|
async #scheduleBatch(sendBatch) {
|
|
2135
|
-
await
|
|
2468
|
+
await yieldToMacrotask();
|
|
2136
2469
|
if (this.#aborted !== void 0) throw this.#aborted;
|
|
2137
2470
|
let batch = this.#batchToSend;
|
|
2138
2471
|
this.#batchToSend = null;
|
|
@@ -2161,7 +2494,7 @@ var BatchServerTransport = class {
|
|
|
2161
2494
|
#batchToSend = [];
|
|
2162
2495
|
#batchToReceive;
|
|
2163
2496
|
#allReceived = Promise.withResolvers();
|
|
2164
|
-
|
|
2497
|
+
send(message) {
|
|
2165
2498
|
this.#batchToSend.push(message);
|
|
2166
2499
|
}
|
|
2167
2500
|
async receive() {
|
|
@@ -2210,7 +2543,11 @@ async function newHttpBatchRpcResponse(request, localMain, options) {
|
|
|
2210
2543
|
* @param options Optional RPC session options. You can also pass headers to set on the response.
|
|
2211
2544
|
*/
|
|
2212
2545
|
async function nodeHttpBatchRpcResponse(request, response, localMain, options) {
|
|
2213
|
-
if (request.method !== "POST")
|
|
2546
|
+
if (request.method !== "POST") {
|
|
2547
|
+
response.writeHead(405, "This endpoint only accepts POST requests.", options?.headers);
|
|
2548
|
+
response.end();
|
|
2549
|
+
return;
|
|
2550
|
+
}
|
|
2214
2551
|
let body = await new Promise((resolve, reject) => {
|
|
2215
2552
|
let chunks = [];
|
|
2216
2553
|
request.on("data", (chunk) => {
|
|
@@ -2235,17 +2572,17 @@ function newMessagePortRpcSession$1(port, localMain, options) {
|
|
|
2235
2572
|
return new RpcSession$1(new MessagePortTransport(port), localMain, options).getRemoteMain();
|
|
2236
2573
|
}
|
|
2237
2574
|
var MessagePortTransport = class {
|
|
2575
|
+
encodingLevel = "structuredClonable";
|
|
2238
2576
|
constructor(port) {
|
|
2239
2577
|
this.#port = port;
|
|
2240
2578
|
port.start();
|
|
2241
2579
|
port.addEventListener("message", (event) => {
|
|
2242
2580
|
if (this.#error) {} else if (event.data === null) this.#receivedError(/* @__PURE__ */ new Error("Peer closed MessagePort connection."));
|
|
2243
|
-
else if (
|
|
2581
|
+
else if (this.#receiveResolver) {
|
|
2244
2582
|
this.#receiveResolver(event.data);
|
|
2245
2583
|
this.#receiveResolver = void 0;
|
|
2246
2584
|
this.#receiveRejecter = void 0;
|
|
2247
2585
|
} else this.#receiveQueue.push(event.data);
|
|
2248
|
-
else this.#receivedError(/* @__PURE__ */ new TypeError("Received non-string message from MessagePort."));
|
|
2249
2586
|
});
|
|
2250
2587
|
port.addEventListener("messageerror", (event) => {
|
|
2251
2588
|
this.#receivedError(/* @__PURE__ */ new Error("MessagePort message error."));
|
|
@@ -2256,7 +2593,7 @@ var MessagePortTransport = class {
|
|
|
2256
2593
|
#receiveRejecter;
|
|
2257
2594
|
#receiveQueue = [];
|
|
2258
2595
|
#error;
|
|
2259
|
-
|
|
2596
|
+
send(message) {
|
|
2260
2597
|
if (this.#error) throw this.#error;
|
|
2261
2598
|
this.#port.postMessage(message);
|
|
2262
2599
|
}
|
|
@@ -2420,9 +2757,11 @@ var MapVariableHook = class extends StubHook {
|
|
|
2420
2757
|
else throwMapperBuilderUseError();
|
|
2421
2758
|
}
|
|
2422
2759
|
call(path, args) {
|
|
2760
|
+
args.dispose();
|
|
2423
2761
|
throwMapperBuilderUseError();
|
|
2424
2762
|
}
|
|
2425
2763
|
map(path, captures, instructions) {
|
|
2764
|
+
for (let cap of captures) cap.dispose();
|
|
2426
2765
|
throwMapperBuilderUseError();
|
|
2427
2766
|
}
|
|
2428
2767
|
pull() {
|
|
@@ -2475,6 +2814,9 @@ var MapApplicator = class {
|
|
|
2475
2814
|
getPipeReadable(exportId) {
|
|
2476
2815
|
throw new Error("A mapper function cannot use pipe readables.");
|
|
2477
2816
|
}
|
|
2817
|
+
getLimits() {
|
|
2818
|
+
return DEFAULT_LIMITS;
|
|
2819
|
+
}
|
|
2478
2820
|
};
|
|
2479
2821
|
function applyMapToElement(input, parent, owner, captures, instructions) {
|
|
2480
2822
|
let mapper = new MapApplicator(captures, new PayloadStubHook(RpcPayload.deepCopyFrom(input, parent, owner)));
|
|
@@ -2530,14 +2872,11 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
|
|
|
2530
2872
|
let state = this.getState();
|
|
2531
2873
|
if (path.length !== 1 || typeof path[0] !== "string") throw new Error("WritableStream stub only supports direct method calls");
|
|
2532
2874
|
const method = path[0];
|
|
2533
|
-
if (method !== "write" && method !== "close" && method !== "abort") {
|
|
2534
|
-
args.dispose();
|
|
2535
|
-
throw new Error(`Unknown WritableStream method: ${method}`);
|
|
2536
|
-
}
|
|
2875
|
+
if (method !== "write" && method !== "close" && method !== "abort") throw new Error(`Unknown WritableStream method: ${method}`);
|
|
2537
2876
|
if (method === "close" || method === "abort") state.closed = true;
|
|
2538
|
-
|
|
2539
|
-
return new PromiseStubHook(args.deliverCall(func, state.writer).then((payload) => new PayloadStubHook(payload)));
|
|
2877
|
+
return new PromiseStubHook((method === "write" ? args.deliverStreamWrite(state.writer) : args.deliverCall(state.writer[method], state.writer)).then((payload) => new PayloadStubHook(payload)));
|
|
2540
2878
|
} catch (err) {
|
|
2879
|
+
args.dispose();
|
|
2541
2880
|
return new ErrorStubHook(err);
|
|
2542
2881
|
}
|
|
2543
2882
|
}
|
|
@@ -2813,12 +3152,12 @@ let newMessagePortRpcSession = newMessagePortRpcSession$1;
|
|
|
2813
3152
|
* credentials as parameters and returns the authorized API), then cross-origin requests should
|
|
2814
3153
|
* be safe.
|
|
2815
3154
|
*/
|
|
2816
|
-
async function newWorkersRpcResponse(request, localMain) {
|
|
3155
|
+
async function newWorkersRpcResponse(request, localMain, options) {
|
|
2817
3156
|
if (request.method === "POST") {
|
|
2818
|
-
let response = await newHttpBatchRpcResponse(request, localMain);
|
|
3157
|
+
let response = await newHttpBatchRpcResponse(request, localMain, options);
|
|
2819
3158
|
response.headers.set("Access-Control-Allow-Origin", "*");
|
|
2820
3159
|
return response;
|
|
2821
|
-
} else if (request.headers.get("Upgrade")?.toLowerCase() === "websocket") return newWorkersWebSocketRpcResponse(request, localMain);
|
|
3160
|
+
} else if (request.headers.get("Upgrade")?.toLowerCase() === "websocket") return newWorkersWebSocketRpcResponse(request, localMain, options);
|
|
2822
3161
|
else return new Response("This endpoint only accepts POST or WebSocket requests.", { status: 400 });
|
|
2823
3162
|
}
|
|
2824
3163
|
|
|
@@ -2940,10 +3279,13 @@ let newBunWebSocketRpcSession = newBunWebSocketRpcSession$1;
|
|
|
2940
3279
|
|
|
2941
3280
|
//#endregion
|
|
2942
3281
|
exports.BunWebSocketTransport = BunWebSocketTransport;
|
|
3282
|
+
exports.DEFAULT_LIMITS = DEFAULT_LIMITS;
|
|
3283
|
+
exports.DEFAULT_MAX_DEPTH = DEFAULT_MAX_DEPTH;
|
|
2943
3284
|
exports.RpcPromise = RpcPromise;
|
|
2944
3285
|
exports.RpcSession = RpcSession;
|
|
2945
3286
|
exports.RpcStub = RpcStub;
|
|
2946
3287
|
exports.RpcTarget = RpcTarget;
|
|
3288
|
+
exports.WebSocketTransport = WebSocketTransport;
|
|
2947
3289
|
exports.deserialize = deserialize;
|
|
2948
3290
|
exports.newBunWebSocketRpcHandler = newBunWebSocketRpcHandler;
|
|
2949
3291
|
exports.newBunWebSocketRpcSession = newBunWebSocketRpcSession;
|