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