capnweb 0.0.0-409821 → 0.0.0-46de5a7
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 +145 -17
- package/dist/index-bun.d.ts +145 -17
- 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 +142 -17
- package/dist/index-workers.d.ts +142 -17
- 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 +142 -17
- package/dist/index.d.ts +142 -17
- package/dist/index.js +446 -107
- package/dist/index.js.map +1 -1
- package/package.json +6 -5
package/dist/index-workers.js
CHANGED
|
@@ -47,9 +47,22 @@ function typeForRpc(value) {
|
|
|
47
47
|
case Array.prototype: return "array";
|
|
48
48
|
case Date.prototype: return "date";
|
|
49
49
|
case Uint8Array.prototype:
|
|
50
|
-
case BUFFER_PROTOTYPE:
|
|
50
|
+
case BUFFER_PROTOTYPE:
|
|
51
|
+
case ArrayBuffer.prototype:
|
|
52
|
+
case DataView.prototype:
|
|
53
|
+
case Int8Array.prototype:
|
|
54
|
+
case Uint8ClampedArray.prototype:
|
|
55
|
+
case Int16Array.prototype:
|
|
56
|
+
case Uint16Array.prototype:
|
|
57
|
+
case Int32Array.prototype:
|
|
58
|
+
case Uint32Array.prototype:
|
|
59
|
+
case BigInt64Array.prototype:
|
|
60
|
+
case BigUint64Array.prototype:
|
|
61
|
+
case Float32Array.prototype:
|
|
62
|
+
case Float64Array.prototype: return "bytes";
|
|
51
63
|
case WritableStream.prototype: return "writable";
|
|
52
64
|
case ReadableStream.prototype: return "readable";
|
|
65
|
+
case URL.prototype: return "url";
|
|
53
66
|
case Headers.prototype: return "headers";
|
|
54
67
|
case Request.prototype: return "request";
|
|
55
68
|
case Response.prototype: return "response";
|
|
@@ -83,7 +96,14 @@ let streamImpl = {
|
|
|
83
96
|
};
|
|
84
97
|
var StubHook = class {
|
|
85
98
|
stream(path, args) {
|
|
86
|
-
let
|
|
99
|
+
let hook = this.call(path, args);
|
|
100
|
+
let pulled;
|
|
101
|
+
try {
|
|
102
|
+
pulled = hook.pull();
|
|
103
|
+
} catch (err) {
|
|
104
|
+
hook.dispose();
|
|
105
|
+
throw err;
|
|
106
|
+
}
|
|
87
107
|
let promise;
|
|
88
108
|
if (pulled instanceof Promise) promise = pulled.then((p) => {
|
|
89
109
|
p.dispose();
|
|
@@ -102,9 +122,11 @@ var ErrorStubHook = class extends StubHook {
|
|
|
102
122
|
this.error = error;
|
|
103
123
|
}
|
|
104
124
|
call(path, args) {
|
|
125
|
+
args.dispose();
|
|
105
126
|
return this;
|
|
106
127
|
}
|
|
107
128
|
map(path, captures, instructions) {
|
|
129
|
+
for (let cap of captures) cap.dispose();
|
|
108
130
|
return this;
|
|
109
131
|
}
|
|
110
132
|
get(path) {
|
|
@@ -384,6 +406,7 @@ var RpcPayload = class RpcPayload {
|
|
|
384
406
|
case "date":
|
|
385
407
|
case "bytes":
|
|
386
408
|
case "blob":
|
|
409
|
+
case "url":
|
|
387
410
|
case "error":
|
|
388
411
|
case "undefined": return value;
|
|
389
412
|
case "array": {
|
|
@@ -517,6 +540,30 @@ var RpcPayload = class RpcPayload {
|
|
|
517
540
|
this.dispose();
|
|
518
541
|
}
|
|
519
542
|
}
|
|
543
|
+
async deliverStreamWrite(writer) {
|
|
544
|
+
try {
|
|
545
|
+
let promises = [];
|
|
546
|
+
this.deliverTo(this, "value", promises);
|
|
547
|
+
if (promises.length > 0) await Promise.all(promises);
|
|
548
|
+
let chunk = this.value[0];
|
|
549
|
+
if ((this.hooks.length > 0 || this.promises.length > 0) && chunk instanceof Object) {
|
|
550
|
+
if (!(Symbol.dispose in chunk)) Object.defineProperty(chunk, Symbol.dispose, {
|
|
551
|
+
value: () => this.dispose(),
|
|
552
|
+
writable: true,
|
|
553
|
+
enumerable: false,
|
|
554
|
+
configurable: true
|
|
555
|
+
});
|
|
556
|
+
await writer.write(chunk);
|
|
557
|
+
return RpcPayload.fromAppReturn(void 0);
|
|
558
|
+
}
|
|
559
|
+
await writer.write(chunk);
|
|
560
|
+
this.dispose();
|
|
561
|
+
return RpcPayload.fromAppReturn(void 0);
|
|
562
|
+
} catch (err) {
|
|
563
|
+
this.dispose();
|
|
564
|
+
throw err;
|
|
565
|
+
}
|
|
566
|
+
}
|
|
520
567
|
async deliverResolve() {
|
|
521
568
|
try {
|
|
522
569
|
let promises = [];
|
|
@@ -557,6 +604,7 @@ var RpcPayload = class RpcPayload {
|
|
|
557
604
|
case "bytes":
|
|
558
605
|
case "blob":
|
|
559
606
|
case "date":
|
|
607
|
+
case "url":
|
|
560
608
|
case "error":
|
|
561
609
|
case "undefined": return;
|
|
562
610
|
case "array": {
|
|
@@ -639,6 +687,7 @@ var RpcPayload = class RpcPayload {
|
|
|
639
687
|
case "rpc-target":
|
|
640
688
|
case "writable":
|
|
641
689
|
case "readable":
|
|
690
|
+
case "url":
|
|
642
691
|
case "headers":
|
|
643
692
|
case "request":
|
|
644
693
|
case "response": return;
|
|
@@ -708,6 +757,7 @@ function followPath(value, parent, path, owner) {
|
|
|
708
757
|
case "blob":
|
|
709
758
|
case "date":
|
|
710
759
|
case "error":
|
|
760
|
+
case "url":
|
|
711
761
|
case "headers":
|
|
712
762
|
case "request":
|
|
713
763
|
case "response":
|
|
@@ -740,17 +790,22 @@ function followPath(value, parent, path, owner) {
|
|
|
740
790
|
}
|
|
741
791
|
var ValueStubHook = class extends StubHook {
|
|
742
792
|
call(path, args) {
|
|
793
|
+
let followResult;
|
|
743
794
|
try {
|
|
744
795
|
let { value, owner } = this.getValue();
|
|
745
|
-
|
|
746
|
-
if (followResult.hook) return followResult.hook.call(followResult.remainingPath, args);
|
|
747
|
-
if (typeof followResult.value != "function") throw new TypeError(`'${path.join(".")}' is not a function.`);
|
|
748
|
-
return new PromiseStubHook(args.deliverCall(followResult.value, followResult.parent).then((payload) => {
|
|
749
|
-
return new PayloadStubHook(payload);
|
|
750
|
-
}));
|
|
796
|
+
followResult = followPath(value, void 0, path, owner);
|
|
751
797
|
} catch (err) {
|
|
798
|
+
args.dispose();
|
|
752
799
|
return new ErrorStubHook(err);
|
|
753
800
|
}
|
|
801
|
+
if (followResult.hook) return followResult.hook.call(followResult.remainingPath, args);
|
|
802
|
+
if (typeof followResult.value != "function") {
|
|
803
|
+
args.dispose();
|
|
804
|
+
return new ErrorStubHook(/* @__PURE__ */ new TypeError(`'${path.join(".")}' is not a function.`));
|
|
805
|
+
}
|
|
806
|
+
return new PromiseStubHook(args.deliverCall(followResult.value, followResult.parent).then((payload) => {
|
|
807
|
+
return new PayloadStubHook(payload);
|
|
808
|
+
}));
|
|
754
809
|
}
|
|
755
810
|
map(path, captures, instructions) {
|
|
756
811
|
try {
|
|
@@ -888,12 +943,16 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
|
888
943
|
}
|
|
889
944
|
call(path, args) {
|
|
890
945
|
args.ensureDeepCopied();
|
|
891
|
-
return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args))
|
|
946
|
+
return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args), (err) => {
|
|
947
|
+
args.dispose();
|
|
948
|
+
throw err;
|
|
949
|
+
}));
|
|
892
950
|
}
|
|
893
951
|
stream(path, args) {
|
|
894
952
|
args.ensureDeepCopied();
|
|
895
|
-
return { promise: this.promise.then((hook) => {
|
|
896
|
-
|
|
953
|
+
return { promise: this.promise.then((hook) => hook.stream(path, args).promise, (err) => {
|
|
954
|
+
args.dispose();
|
|
955
|
+
throw err;
|
|
897
956
|
}) };
|
|
898
957
|
}
|
|
899
958
|
map(path, captures, instructions) {
|
|
@@ -920,10 +979,7 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
|
920
979
|
}, (err) => {});
|
|
921
980
|
}
|
|
922
981
|
dispose() {
|
|
923
|
-
|
|
924
|
-
else this.promise.then((hook) => {
|
|
925
|
-
hook.dispose();
|
|
926
|
-
}, (err) => {});
|
|
982
|
+
this.promise.then((hook) => hook.dispose(), () => {});
|
|
927
983
|
}
|
|
928
984
|
onBroken(callback) {
|
|
929
985
|
if (this.resolution) this.resolution.onBroken(callback);
|
|
@@ -935,6 +991,77 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
|
935
991
|
|
|
936
992
|
//#endregion
|
|
937
993
|
//#region src/serialize.ts
|
|
994
|
+
const DEFAULT_MAX_DEPTH = 256;
|
|
995
|
+
const DEFAULT_LIMITS = {
|
|
996
|
+
maxBigIntDigits: 16384,
|
|
997
|
+
maxDepth: 256,
|
|
998
|
+
maxMessageSize: 32 * 1024 * 1024
|
|
999
|
+
};
|
|
1000
|
+
const NATIVE_LITTLE_ENDIAN = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
|
|
1001
|
+
const BYTE_CONTAINER_TYPE_NAMES = [
|
|
1002
|
+
"ArrayBuffer",
|
|
1003
|
+
"DataView",
|
|
1004
|
+
"Int8Array",
|
|
1005
|
+
"Uint8Array",
|
|
1006
|
+
"Uint8ClampedArray",
|
|
1007
|
+
"Int16Array",
|
|
1008
|
+
"Uint16Array",
|
|
1009
|
+
"Int32Array",
|
|
1010
|
+
"Uint32Array",
|
|
1011
|
+
"BigInt64Array",
|
|
1012
|
+
"BigUint64Array",
|
|
1013
|
+
"Float32Array",
|
|
1014
|
+
"Float64Array"
|
|
1015
|
+
];
|
|
1016
|
+
function isValidByteContainerName(value) {
|
|
1017
|
+
return BYTE_CONTAINER_TYPE_NAMES.includes(value);
|
|
1018
|
+
}
|
|
1019
|
+
const TYPED_ARRAY_ELEMENT_SIZE = {
|
|
1020
|
+
ArrayBuffer: void 0,
|
|
1021
|
+
DataView: void 0,
|
|
1022
|
+
Int8Array: void 0,
|
|
1023
|
+
Uint8Array: void 0,
|
|
1024
|
+
Uint8ClampedArray: void 0,
|
|
1025
|
+
Int16Array: 2,
|
|
1026
|
+
Uint16Array: 2,
|
|
1027
|
+
Int32Array: 4,
|
|
1028
|
+
Uint32Array: 4,
|
|
1029
|
+
BigInt64Array: 8,
|
|
1030
|
+
BigUint64Array: 8,
|
|
1031
|
+
Float32Array: 4,
|
|
1032
|
+
Float64Array: 8
|
|
1033
|
+
};
|
|
1034
|
+
const BYTE_CONTAINER_PROTOTYPES = {
|
|
1035
|
+
ArrayBuffer: ArrayBuffer.prototype,
|
|
1036
|
+
DataView: DataView.prototype,
|
|
1037
|
+
Int8Array: Int8Array.prototype,
|
|
1038
|
+
Uint8ClampedArray: Uint8ClampedArray.prototype,
|
|
1039
|
+
Int16Array: Int16Array.prototype,
|
|
1040
|
+
Uint16Array: Uint16Array.prototype,
|
|
1041
|
+
Int32Array: Int32Array.prototype,
|
|
1042
|
+
Uint32Array: Uint32Array.prototype,
|
|
1043
|
+
BigInt64Array: BigInt64Array.prototype,
|
|
1044
|
+
BigUint64Array: BigUint64Array.prototype,
|
|
1045
|
+
Float32Array: Float32Array.prototype,
|
|
1046
|
+
Float64Array: Float64Array.prototype
|
|
1047
|
+
};
|
|
1048
|
+
const BYTE_CONTAINER_TYPE_BY_PROTOTYPE = /* @__PURE__ */ new Map();
|
|
1049
|
+
for (let type of Object.keys(BYTE_CONTAINER_PROTOTYPES)) BYTE_CONTAINER_TYPE_BY_PROTOTYPE.set(BYTE_CONTAINER_PROTOTYPES[type], type);
|
|
1050
|
+
function swapByteOrder(bytes, elementSize) {
|
|
1051
|
+
if (elementSize !== 2 && elementSize !== 4 && elementSize !== 8) throw new RangeError(`Unsupported element size: ${elementSize}`);
|
|
1052
|
+
let view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
1053
|
+
for (let offset = 0; offset < bytes.byteLength; offset += elementSize) switch (elementSize) {
|
|
1054
|
+
case 2:
|
|
1055
|
+
view.setUint16(offset, view.getUint16(offset, false), true);
|
|
1056
|
+
break;
|
|
1057
|
+
case 4:
|
|
1058
|
+
view.setUint32(offset, view.getUint32(offset, false), true);
|
|
1059
|
+
break;
|
|
1060
|
+
case 8:
|
|
1061
|
+
view.setBigUint64(offset, view.getBigUint64(offset, false), true);
|
|
1062
|
+
break;
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
938
1065
|
var NullExporter = class {
|
|
939
1066
|
exportStub(stub) {
|
|
940
1067
|
throw new Error("Cannot serialize RPC stubs without an RPC session.");
|
|
@@ -955,6 +1082,7 @@ async function streamToBlob(stream, type) {
|
|
|
955
1082
|
return b.type === type ? b : b.slice(0, b.size, type);
|
|
956
1083
|
}
|
|
957
1084
|
const ERROR_TYPES = {
|
|
1085
|
+
__proto__: null,
|
|
958
1086
|
Error,
|
|
959
1087
|
EvalError,
|
|
960
1088
|
RangeError,
|
|
@@ -967,12 +1095,14 @@ const ERROR_TYPES = {
|
|
|
967
1095
|
var Devaluator = class Devaluator {
|
|
968
1096
|
exporter;
|
|
969
1097
|
source;
|
|
970
|
-
|
|
1098
|
+
encodingLevel;
|
|
1099
|
+
constructor(exporter, source, encodingLevel) {
|
|
971
1100
|
this.exporter = exporter;
|
|
972
1101
|
this.source = source;
|
|
1102
|
+
this.encodingLevel = encodingLevel;
|
|
973
1103
|
}
|
|
974
|
-
static devaluate(value, parent, exporter = NULL_EXPORTER, source) {
|
|
975
|
-
let devaluator = new Devaluator(exporter, source);
|
|
1104
|
+
static devaluate(value, parent, exporter = NULL_EXPORTER, source, encodingLevel = "string") {
|
|
1105
|
+
let devaluator = new Devaluator(exporter, source, encodingLevel);
|
|
976
1106
|
try {
|
|
977
1107
|
return devaluator.devaluateImpl(value, parent, 0);
|
|
978
1108
|
} catch (err) {
|
|
@@ -984,7 +1114,7 @@ var Devaluator = class Devaluator {
|
|
|
984
1114
|
}
|
|
985
1115
|
exports;
|
|
986
1116
|
devaluateImpl(value, parent, depth) {
|
|
987
|
-
if (depth >=
|
|
1117
|
+
if (depth >= 256) throw new Error("Serialization exceeded maximum allowed depth. (Does the message contain cycles?)");
|
|
988
1118
|
switch (typeForRpc(value)) {
|
|
989
1119
|
case "unsupported": {
|
|
990
1120
|
let msg;
|
|
@@ -995,10 +1125,12 @@ var Devaluator = class Devaluator {
|
|
|
995
1125
|
}
|
|
996
1126
|
throw new TypeError(msg);
|
|
997
1127
|
}
|
|
998
|
-
case "primitive": if (typeof value === "number" && !isFinite(value))
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1128
|
+
case "primitive": if (typeof value === "number" && !isFinite(value)) {
|
|
1129
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1130
|
+
if (value === Infinity) return ["inf"];
|
|
1131
|
+
else if (value === -Infinity) return ["-inf"];
|
|
1132
|
+
else return ["nan"];
|
|
1133
|
+
} else return value;
|
|
1002
1134
|
case "object": {
|
|
1003
1135
|
let object = value;
|
|
1004
1136
|
let result = {};
|
|
@@ -1012,23 +1144,49 @@ var Devaluator = class Devaluator {
|
|
|
1012
1144
|
for (let i = 0; i < len; i++) result[i] = this.devaluateImpl(array[i], array, depth + 1);
|
|
1013
1145
|
return [result];
|
|
1014
1146
|
}
|
|
1015
|
-
case "bigint":
|
|
1147
|
+
case "bigint":
|
|
1148
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1149
|
+
return ["bigint", value.toString()];
|
|
1016
1150
|
case "date": {
|
|
1151
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1017
1152
|
const time = value.getTime();
|
|
1018
1153
|
return ["date", Number.isNaN(time) ? null : time];
|
|
1019
1154
|
}
|
|
1020
1155
|
case "bytes": {
|
|
1021
|
-
let
|
|
1022
|
-
|
|
1156
|
+
let alternateTypeName = BYTE_CONTAINER_TYPE_BY_PROTOTYPE.get(Object.getPrototypeOf(value));
|
|
1157
|
+
let bytes;
|
|
1158
|
+
if (alternateTypeName === "ArrayBuffer") bytes = new Uint8Array(value);
|
|
1159
|
+
else if (alternateTypeName === void 0) bytes = value;
|
|
1160
|
+
else {
|
|
1161
|
+
let view = value;
|
|
1162
|
+
bytes = new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
|
|
1163
|
+
let elementSize = TYPED_ARRAY_ELEMENT_SIZE[alternateTypeName];
|
|
1164
|
+
if (!NATIVE_LITTLE_ENDIAN && elementSize) {
|
|
1165
|
+
bytes = bytes.slice();
|
|
1166
|
+
swapByteOrder(bytes, elementSize);
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
if (this.encodingLevel === "structuredClonable" || this.encodingLevel === "jsonCompatibleWithBytes") return alternateTypeName === void 0 ? ["bytes", bytes] : [
|
|
1170
|
+
"bytes",
|
|
1171
|
+
bytes,
|
|
1172
|
+
alternateTypeName
|
|
1173
|
+
];
|
|
1023
1174
|
let b64;
|
|
1024
|
-
if (
|
|
1175
|
+
if (bytes.toBase64) b64 = bytes.toBase64({ omitPadding: true });
|
|
1176
|
+
else if (typeof Buffer !== "undefined") b64 = (bytes instanceof Buffer ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)).toString("base64");
|
|
1025
1177
|
else {
|
|
1026
1178
|
let binary = "";
|
|
1027
1179
|
for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]);
|
|
1028
1180
|
b64 = btoa(binary);
|
|
1029
1181
|
}
|
|
1030
|
-
|
|
1182
|
+
b64 = b64.replace(/=+$/, "");
|
|
1183
|
+
return alternateTypeName === void 0 ? ["bytes", b64] : [
|
|
1184
|
+
"bytes",
|
|
1185
|
+
b64,
|
|
1186
|
+
alternateTypeName
|
|
1187
|
+
];
|
|
1031
1188
|
}
|
|
1189
|
+
case "url": return ["url", value.href];
|
|
1032
1190
|
case "headers": return ["headers", [...value]];
|
|
1033
1191
|
case "request": {
|
|
1034
1192
|
let req = value;
|
|
@@ -1143,7 +1301,9 @@ var Devaluator = class Devaluator {
|
|
|
1143
1301
|
} else if (rewritten && rewritten.stack) result.push(rewritten.stack);
|
|
1144
1302
|
return result;
|
|
1145
1303
|
}
|
|
1146
|
-
case "undefined":
|
|
1304
|
+
case "undefined":
|
|
1305
|
+
if (this.encodingLevel === "structuredClonable") return;
|
|
1306
|
+
return ["undefined"];
|
|
1147
1307
|
case "stub":
|
|
1148
1308
|
case "rpc-promise": {
|
|
1149
1309
|
if (!this.source) throw new Error("Can't serialize RPC stubs in this context.");
|
|
@@ -1210,6 +1370,9 @@ var NullImporter = class {
|
|
|
1210
1370
|
getPipeReadable(exportId) {
|
|
1211
1371
|
throw new Error("Cannot retrieve pipe readable without an RPC session.");
|
|
1212
1372
|
}
|
|
1373
|
+
getLimits() {
|
|
1374
|
+
return DEFAULT_LIMITS;
|
|
1375
|
+
}
|
|
1213
1376
|
};
|
|
1214
1377
|
const NULL_IMPORTER = new NullImporter();
|
|
1215
1378
|
function fixBrokenRequestBody(request, body) {
|
|
@@ -1226,15 +1389,22 @@ function streamToBlobPromise(stream, type) {
|
|
|
1226
1389
|
}
|
|
1227
1390
|
var Evaluator = class Evaluator {
|
|
1228
1391
|
importer;
|
|
1229
|
-
|
|
1392
|
+
encodingLevel;
|
|
1393
|
+
limits;
|
|
1394
|
+
constructor(importer, encodingLevel = "string") {
|
|
1230
1395
|
this.importer = importer;
|
|
1396
|
+
this.encodingLevel = encodingLevel;
|
|
1397
|
+
this.limits = importer.getLimits();
|
|
1231
1398
|
}
|
|
1232
1399
|
hooks = [];
|
|
1233
1400
|
promises = [];
|
|
1234
1401
|
evaluate(value) {
|
|
1402
|
+
return this.evaluateWithDepth(value, 0);
|
|
1403
|
+
}
|
|
1404
|
+
evaluateWithDepth(value, depth) {
|
|
1235
1405
|
let payload = RpcPayload.forEvaluate(this.hooks, this.promises);
|
|
1236
1406
|
try {
|
|
1237
|
-
payload.value = this.evaluateImpl(value, payload, "value");
|
|
1407
|
+
payload.value = this.evaluateImpl(value, payload, "value", depth);
|
|
1238
1408
|
return payload;
|
|
1239
1409
|
} catch (err) {
|
|
1240
1410
|
payload.dispose();
|
|
@@ -1244,31 +1414,70 @@ var Evaluator = class Evaluator {
|
|
|
1244
1414
|
evaluateCopy(value) {
|
|
1245
1415
|
return this.evaluate(structuredClone(value));
|
|
1246
1416
|
}
|
|
1247
|
-
evaluateImpl(value, parent, property) {
|
|
1417
|
+
evaluateImpl(value, parent, property, depth) {
|
|
1418
|
+
let maxDepth = this.limits.maxDepth;
|
|
1419
|
+
if (depth >= maxDepth) throw new TypeError(`Deserialization exceeded maximum allowed message depth of ${maxDepth}.`);
|
|
1420
|
+
if (this.encodingLevel === "structuredClonable") {
|
|
1421
|
+
if (value instanceof Date || typeof value === "bigint") return value;
|
|
1422
|
+
}
|
|
1248
1423
|
if (value instanceof Array) {
|
|
1249
1424
|
if (value.length == 1 && value[0] instanceof Array) {
|
|
1250
1425
|
let result = value[0];
|
|
1251
|
-
for (let i = 0; i < result.length; i++) result[i] = this.evaluateImpl(result[i], result, i);
|
|
1426
|
+
for (let i = 0; i < result.length; i++) result[i] = this.evaluateImpl(result[i], result, i, depth + 1);
|
|
1252
1427
|
return result;
|
|
1253
1428
|
} else switch (value[0]) {
|
|
1254
1429
|
case "bigint":
|
|
1255
|
-
if (typeof value[1] == "string")
|
|
1430
|
+
if (typeof value[1] == "string") {
|
|
1431
|
+
let digits = value[1];
|
|
1432
|
+
let maxBigIntDigits = this.limits.maxBigIntDigits;
|
|
1433
|
+
if (digits.length > maxBigIntDigits) throw new TypeError(`Deserialized bigint exceeds maximum length of ${maxBigIntDigits} digits.`);
|
|
1434
|
+
return BigInt(digits);
|
|
1435
|
+
}
|
|
1256
1436
|
break;
|
|
1257
1437
|
case "date":
|
|
1258
1438
|
if (value[1] === null) return /* @__PURE__ */ new Date(NaN);
|
|
1259
1439
|
if (typeof value[1] == "number") return new Date(value[1]);
|
|
1260
1440
|
break;
|
|
1261
|
-
case "bytes":
|
|
1262
|
-
|
|
1263
|
-
|
|
1441
|
+
case "bytes": {
|
|
1442
|
+
let bytes;
|
|
1443
|
+
if (value[1] instanceof Uint8Array) bytes = value[1];
|
|
1444
|
+
else if (typeof value[1] == "string") if (typeof Buffer !== "undefined") bytes = Buffer.from(value[1], "base64");
|
|
1445
|
+
else if (Uint8Array.fromBase64) bytes = Uint8Array.fromBase64(value[1]);
|
|
1264
1446
|
else {
|
|
1265
1447
|
let bs = atob(value[1]);
|
|
1266
1448
|
let len = bs.length;
|
|
1267
|
-
|
|
1449
|
+
bytes = new Uint8Array(len);
|
|
1268
1450
|
for (let i = 0; i < len; i++) bytes[i] = bs.charCodeAt(i);
|
|
1269
|
-
return bytes;
|
|
1270
1451
|
}
|
|
1271
|
-
break;
|
|
1452
|
+
else break;
|
|
1453
|
+
if (value.length === 2) return bytes;
|
|
1454
|
+
if (typeof value[2] !== "string") throw new TypeError(`Unknown bytes type marker type: ${typeof value[2]}`);
|
|
1455
|
+
if (!isValidByteContainerName(value[2])) {
|
|
1456
|
+
let marker = value[2].slice(0, 64);
|
|
1457
|
+
throw new TypeError(`Unknown bytes type marker: ${marker}`);
|
|
1458
|
+
}
|
|
1459
|
+
let marker = value[2];
|
|
1460
|
+
let elementSize = TYPED_ARRAY_ELEMENT_SIZE[marker];
|
|
1461
|
+
if (elementSize !== void 0 && bytes.byteLength % elementSize !== 0) throw new TypeError(`Invalid byte length ${bytes.byteLength} for ${marker}; expected a multiple of ${elementSize}`);
|
|
1462
|
+
let buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
|
1463
|
+
if (!NATIVE_LITTLE_ENDIAN && elementSize !== void 0) swapByteOrder(new Uint8Array(buffer), elementSize);
|
|
1464
|
+
switch (marker) {
|
|
1465
|
+
case "ArrayBuffer": return buffer;
|
|
1466
|
+
case "DataView": return new DataView(buffer);
|
|
1467
|
+
case "Int8Array": return new Int8Array(buffer);
|
|
1468
|
+
case "Uint8Array": return new Uint8Array(buffer);
|
|
1469
|
+
case "Uint8ClampedArray": return new Uint8ClampedArray(buffer);
|
|
1470
|
+
case "Int16Array": return new Int16Array(buffer);
|
|
1471
|
+
case "Uint16Array": return new Uint16Array(buffer);
|
|
1472
|
+
case "Int32Array": return new Int32Array(buffer);
|
|
1473
|
+
case "Uint32Array": return new Uint32Array(buffer);
|
|
1474
|
+
case "BigInt64Array": return new BigInt64Array(buffer);
|
|
1475
|
+
case "BigUint64Array": return new BigUint64Array(buffer);
|
|
1476
|
+
case "Float32Array": return new Float32Array(buffer);
|
|
1477
|
+
case "Float64Array": return new Float64Array(buffer);
|
|
1478
|
+
default:
|
|
1479
|
+
}
|
|
1480
|
+
}
|
|
1272
1481
|
case "error":
|
|
1273
1482
|
if (value.length >= 3 && typeof value[1] === "string" && typeof value[2] === "string") {
|
|
1274
1483
|
let cls = ERROR_TYPES[value[1]] || Error;
|
|
@@ -1281,7 +1490,11 @@ var Evaluator = class Evaluator {
|
|
|
1281
1490
|
let propsObj = props;
|
|
1282
1491
|
for (let key of Object.keys(propsObj)) {
|
|
1283
1492
|
if (key === "name" || key === "message" || key === "stack") continue;
|
|
1284
|
-
|
|
1493
|
+
if (key in Object.prototype || key === "toJSON") {
|
|
1494
|
+
this.evaluateImpl(propsObj[key], result, key, depth + 1);
|
|
1495
|
+
continue;
|
|
1496
|
+
}
|
|
1497
|
+
anyResult[key] = this.evaluateImpl(propsObj[key], result, key, depth + 1);
|
|
1285
1498
|
}
|
|
1286
1499
|
}
|
|
1287
1500
|
return result;
|
|
@@ -1293,6 +1506,9 @@ var Evaluator = class Evaluator {
|
|
|
1293
1506
|
case "inf": return Infinity;
|
|
1294
1507
|
case "-inf": return -Infinity;
|
|
1295
1508
|
case "nan": return NaN;
|
|
1509
|
+
case "url":
|
|
1510
|
+
if (value.length === 2 && typeof value[1] === "string") return new URL(value[1]);
|
|
1511
|
+
break;
|
|
1296
1512
|
case "headers":
|
|
1297
1513
|
if (value.length === 2 && value[1] instanceof Array) return new Headers(value[1]);
|
|
1298
1514
|
break;
|
|
@@ -1302,11 +1518,11 @@ var Evaluator = class Evaluator {
|
|
|
1302
1518
|
let init = value[2];
|
|
1303
1519
|
if (typeof init !== "object" || init === null) break;
|
|
1304
1520
|
if (init.body) {
|
|
1305
|
-
init.body = this.evaluateImpl(init.body, init, "body");
|
|
1521
|
+
init.body = this.evaluateImpl(init.body, init, "body", depth + 1);
|
|
1306
1522
|
if (init.body === null || typeof init.body === "string" || init.body instanceof Uint8Array || init.body instanceof ReadableStream) {} else throw new TypeError("Request body must be of type ReadableStream.");
|
|
1307
1523
|
}
|
|
1308
1524
|
if (init.signal) {
|
|
1309
|
-
init.signal = this.evaluateImpl(init.signal, init, "signal");
|
|
1525
|
+
init.signal = this.evaluateImpl(init.signal, init, "signal", depth + 1);
|
|
1310
1526
|
if (!(init.signal instanceof AbortSignal)) throw new TypeError("Request siganl must be of type AbortSignal.");
|
|
1311
1527
|
}
|
|
1312
1528
|
if (init.headers && !(init.headers instanceof Array)) throw new TypeError("Request headers must be serialized as an array of pairs.");
|
|
@@ -1323,7 +1539,7 @@ var Evaluator = class Evaluator {
|
|
|
1323
1539
|
}
|
|
1324
1540
|
case "response": {
|
|
1325
1541
|
if (value.length !== 3) break;
|
|
1326
|
-
let body = this.evaluateImpl(value[1], parent, property);
|
|
1542
|
+
let body = this.evaluateImpl(value[1], parent, property, depth + 1);
|
|
1327
1543
|
if (body === null || typeof body === "string" || body instanceof Uint8Array || body instanceof ReadableStream) {} else throw new TypeError("Response body must be of type ReadableStream.");
|
|
1328
1544
|
let init = value[2];
|
|
1329
1545
|
if (typeof init !== "object" || init === null) break;
|
|
@@ -1334,7 +1550,7 @@ var Evaluator = class Evaluator {
|
|
|
1334
1550
|
case "blob": {
|
|
1335
1551
|
if (value.length !== 3 || typeof value[1] !== "string") break;
|
|
1336
1552
|
let contentType = value[1];
|
|
1337
|
-
let content = this.evaluateImpl(value[2], parent, property);
|
|
1553
|
+
let content = this.evaluateImpl(value[2], parent, property, depth + 1);
|
|
1338
1554
|
if (!(content instanceof ReadableStream)) throw new TypeError("Blob content must be serialized as a ReadableStream.");
|
|
1339
1555
|
let promise = streamToBlobPromise(content, contentType);
|
|
1340
1556
|
this.promises.push({
|
|
@@ -1375,7 +1591,7 @@ var Evaluator = class Evaluator {
|
|
|
1375
1591
|
if (value.length == 3) return addStub(hook.get(path));
|
|
1376
1592
|
let args = value[3];
|
|
1377
1593
|
if (!(args instanceof Array)) break;
|
|
1378
|
-
args = new Evaluator(this.importer).
|
|
1594
|
+
args = new Evaluator(this.importer).evaluateWithDepth([args], depth);
|
|
1379
1595
|
return addStub(hook.call(path, args));
|
|
1380
1596
|
}
|
|
1381
1597
|
case "remap": {
|
|
@@ -1441,9 +1657,9 @@ var Evaluator = class Evaluator {
|
|
|
1441
1657
|
} else if (value instanceof Object) {
|
|
1442
1658
|
let result = value;
|
|
1443
1659
|
for (let key in result) if (key in Object.prototype || key === "toJSON") {
|
|
1444
|
-
this.evaluateImpl(result[key], result, key);
|
|
1660
|
+
this.evaluateImpl(result[key], result, key, depth + 1);
|
|
1445
1661
|
delete result[key];
|
|
1446
|
-
} else result[key] = this.evaluateImpl(result[key], result, key);
|
|
1662
|
+
} else result[key] = this.evaluateImpl(result[key], result, key, depth + 1);
|
|
1447
1663
|
return result;
|
|
1448
1664
|
} else return value;
|
|
1449
1665
|
}
|
|
@@ -1459,6 +1675,47 @@ function deserialize(value) {
|
|
|
1459
1675
|
|
|
1460
1676
|
//#endregion
|
|
1461
1677
|
//#region src/rpc.ts
|
|
1678
|
+
const ESTIMATED_OBJECT_OVERHEAD = 16;
|
|
1679
|
+
const ESTIMATED_ENTRY_OVERHEAD = 8;
|
|
1680
|
+
const ESTIMATED_BINARY_OVERHEAD = 16;
|
|
1681
|
+
const MAX_ESTIMATE_DEPTH = 64;
|
|
1682
|
+
function estimateStringSize(value) {
|
|
1683
|
+
return 2 + value.length * 3;
|
|
1684
|
+
}
|
|
1685
|
+
function estimateEncodedSize(value, seen, depth = 0) {
|
|
1686
|
+
if (depth >= MAX_ESTIMATE_DEPTH) return ESTIMATED_ENTRY_OVERHEAD;
|
|
1687
|
+
switch (typeof value) {
|
|
1688
|
+
case "string": return estimateStringSize(value);
|
|
1689
|
+
case "number": return 16;
|
|
1690
|
+
case "bigint": return 16;
|
|
1691
|
+
case "boolean": return 8;
|
|
1692
|
+
case "undefined": return 16;
|
|
1693
|
+
case "object": {
|
|
1694
|
+
if (value === null) return 8;
|
|
1695
|
+
if (ArrayBuffer.isView(value)) return ESTIMATED_BINARY_OVERHEAD + value.byteLength;
|
|
1696
|
+
if (value instanceof ArrayBuffer) return ESTIMATED_BINARY_OVERHEAD + value.byteLength;
|
|
1697
|
+
if (typeof Blob !== "undefined" && value instanceof Blob) return ESTIMATED_BINARY_OVERHEAD + value.size;
|
|
1698
|
+
if (value instanceof Date) return 16;
|
|
1699
|
+
seen ??= /* @__PURE__ */ new WeakSet();
|
|
1700
|
+
if (seen.has(value)) return ESTIMATED_ENTRY_OVERHEAD;
|
|
1701
|
+
seen.add(value);
|
|
1702
|
+
if (value instanceof Array) {
|
|
1703
|
+
let size = ESTIMATED_OBJECT_OVERHEAD;
|
|
1704
|
+
for (let item of value) size += ESTIMATED_ENTRY_OVERHEAD + estimateEncodedSize(item, seen, depth + 1);
|
|
1705
|
+
return size;
|
|
1706
|
+
}
|
|
1707
|
+
if (value instanceof Error) {
|
|
1708
|
+
let size = ESTIMATED_OBJECT_OVERHEAD + estimateStringSize(value.name) + estimateStringSize(value.message) + estimateStringSize(value.stack ?? "");
|
|
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
|
+
let size = ESTIMATED_OBJECT_OVERHEAD;
|
|
1713
|
+
for (let key of Object.keys(value)) size += ESTIMATED_ENTRY_OVERHEAD + estimateStringSize(key) + estimateEncodedSize(value[key], seen, depth + 1);
|
|
1714
|
+
return size;
|
|
1715
|
+
}
|
|
1716
|
+
default: return 16;
|
|
1717
|
+
}
|
|
1718
|
+
}
|
|
1462
1719
|
var ImportTableEntry = class {
|
|
1463
1720
|
session;
|
|
1464
1721
|
importId;
|
|
@@ -1551,24 +1808,28 @@ var RpcImportHook = class RpcImportHook extends StubHook {
|
|
|
1551
1808
|
if (this.entry) return this.entry;
|
|
1552
1809
|
else throw new Error("This RpcImportHook was already disposed.");
|
|
1553
1810
|
}
|
|
1811
|
+
getEntryTakingOwnership(disposeOwned) {
|
|
1812
|
+
try {
|
|
1813
|
+
return this.getEntry();
|
|
1814
|
+
} catch (err) {
|
|
1815
|
+
disposeOwned();
|
|
1816
|
+
throw err;
|
|
1817
|
+
}
|
|
1818
|
+
}
|
|
1554
1819
|
call(path, args) {
|
|
1555
|
-
let entry = this.
|
|
1820
|
+
let entry = this.getEntryTakingOwnership(() => args.dispose());
|
|
1556
1821
|
if (entry.resolution) return entry.resolution.call(path, args);
|
|
1557
1822
|
else return entry.session.sendCall(entry.importId, path, args);
|
|
1558
1823
|
}
|
|
1559
1824
|
stream(path, args) {
|
|
1560
|
-
let entry = this.
|
|
1825
|
+
let entry = this.getEntryTakingOwnership(() => args.dispose());
|
|
1561
1826
|
if (entry.resolution) return entry.resolution.stream(path, args);
|
|
1562
1827
|
else return entry.session.sendStream(entry.importId, path, args);
|
|
1563
1828
|
}
|
|
1564
1829
|
map(path, captures, instructions) {
|
|
1565
|
-
let entry
|
|
1566
|
-
try {
|
|
1567
|
-
entry = this.getEntry();
|
|
1568
|
-
} catch (err) {
|
|
1830
|
+
let entry = this.getEntryTakingOwnership(() => {
|
|
1569
1831
|
for (let cap of captures) cap.dispose();
|
|
1570
|
-
|
|
1571
|
-
}
|
|
1832
|
+
});
|
|
1572
1833
|
if (entry.resolution) return entry.resolution.map(path, captures, instructions);
|
|
1573
1834
|
else return entry.session.sendMap(entry.importId, path, captures, instructions);
|
|
1574
1835
|
}
|
|
@@ -1624,9 +1885,24 @@ var RpcSessionImpl = class {
|
|
|
1624
1885
|
onBatchDone;
|
|
1625
1886
|
pullCount = 0;
|
|
1626
1887
|
onBrokenCallbacks = [];
|
|
1888
|
+
encodingLevel;
|
|
1889
|
+
limits;
|
|
1627
1890
|
constructor(transport, mainHook, options) {
|
|
1628
1891
|
this.transport = transport;
|
|
1629
1892
|
this.options = options;
|
|
1893
|
+
let level = "string";
|
|
1894
|
+
if ("encodingLevel" in transport) {
|
|
1895
|
+
let raw = transport.encodingLevel;
|
|
1896
|
+
if (raw !== void 0) {
|
|
1897
|
+
if (raw !== "string" && raw !== "jsonCompatible" && raw !== "jsonCompatibleWithBytes" && raw !== "structuredClonable") throw new TypeError(`Unknown transport encodingLevel: ${String(raw)}`);
|
|
1898
|
+
level = raw;
|
|
1899
|
+
}
|
|
1900
|
+
}
|
|
1901
|
+
this.encodingLevel = level;
|
|
1902
|
+
this.limits = {
|
|
1903
|
+
...DEFAULT_LIMITS,
|
|
1904
|
+
...options.limits
|
|
1905
|
+
};
|
|
1630
1906
|
this.exports.push({
|
|
1631
1907
|
hook: mainHook,
|
|
1632
1908
|
refcount: 1
|
|
@@ -1707,7 +1983,7 @@ var RpcSessionImpl = class {
|
|
|
1707
1983
|
let autoRelease = exp.autoRelease;
|
|
1708
1984
|
++this.pullCount;
|
|
1709
1985
|
exp.pull = resolve().then((payload) => {
|
|
1710
|
-
let value = Devaluator.devaluate(payload.value, void 0, this, payload);
|
|
1986
|
+
let value = Devaluator.devaluate(payload.value, void 0, this, payload, this.encodingLevel);
|
|
1711
1987
|
this.send([
|
|
1712
1988
|
"resolve",
|
|
1713
1989
|
exportId,
|
|
@@ -1718,7 +1994,7 @@ var RpcSessionImpl = class {
|
|
|
1718
1994
|
this.send([
|
|
1719
1995
|
"reject",
|
|
1720
1996
|
exportId,
|
|
1721
|
-
Devaluator.devaluate(error, void 0, this)
|
|
1997
|
+
Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)
|
|
1722
1998
|
]);
|
|
1723
1999
|
if (autoRelease) this.releaseExport(exportId, 1);
|
|
1724
2000
|
}).catch((error) => {
|
|
@@ -1726,7 +2002,7 @@ var RpcSessionImpl = class {
|
|
|
1726
2002
|
this.send([
|
|
1727
2003
|
"reject",
|
|
1728
2004
|
exportId,
|
|
1729
|
-
Devaluator.devaluate(error, void 0, this)
|
|
2005
|
+
Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)
|
|
1730
2006
|
]);
|
|
1731
2007
|
if (autoRelease) this.releaseExport(exportId, 1);
|
|
1732
2008
|
} catch (error2) {
|
|
@@ -1769,6 +2045,9 @@ var RpcSessionImpl = class {
|
|
|
1769
2045
|
entry.pipeReadable = void 0;
|
|
1770
2046
|
return readable;
|
|
1771
2047
|
}
|
|
2048
|
+
getLimits() {
|
|
2049
|
+
return this.limits;
|
|
2050
|
+
}
|
|
1772
2051
|
createPipe(readable, readableHook) {
|
|
1773
2052
|
if (this.abortReason) throw this.abortReason;
|
|
1774
2053
|
this.send(["pipe"]);
|
|
@@ -1782,27 +2061,52 @@ var RpcSessionImpl = class {
|
|
|
1782
2061
|
}
|
|
1783
2062
|
send(msg) {
|
|
1784
2063
|
if (this.abortReason !== void 0) return 0;
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
msgText = JSON.stringify(msg);
|
|
1788
|
-
} catch (err) {
|
|
2064
|
+
if (this.encodingLevel === "string") {
|
|
2065
|
+
let msgText;
|
|
1789
2066
|
try {
|
|
1790
|
-
|
|
1791
|
-
} catch (
|
|
1792
|
-
|
|
2067
|
+
msgText = JSON.stringify(msg);
|
|
2068
|
+
} catch (err) {
|
|
2069
|
+
try {
|
|
2070
|
+
this.abort(err);
|
|
2071
|
+
} catch (err2) {}
|
|
2072
|
+
throw err;
|
|
2073
|
+
}
|
|
2074
|
+
try {
|
|
2075
|
+
let sent = this.transport.send(msgText);
|
|
2076
|
+
if (sent !== void 0 && typeof sent.catch === "function") sent.catch((err) => this.abort(err, false));
|
|
2077
|
+
} catch (err) {
|
|
2078
|
+
queueMicrotask(() => this.abort(err, false));
|
|
2079
|
+
}
|
|
2080
|
+
return msgText.length;
|
|
2081
|
+
} else try {
|
|
2082
|
+
let size = this.transport.send(msg);
|
|
2083
|
+
if (typeof size === "number") return size;
|
|
2084
|
+
let thenable = size;
|
|
2085
|
+
if (thenable && typeof thenable.then === "function") Promise.resolve(thenable).catch((err) => this.abort(err, false));
|
|
2086
|
+
return;
|
|
2087
|
+
} catch (err) {
|
|
2088
|
+
queueMicrotask(() => this.abort(err, false));
|
|
2089
|
+
return;
|
|
1793
2090
|
}
|
|
1794
|
-
this.transport.send(msgText).catch((err) => this.abort(err, false));
|
|
1795
|
-
return msgText.length;
|
|
1796
2091
|
}
|
|
1797
2092
|
sendCall(id, path, args) {
|
|
1798
|
-
if (this.abortReason)
|
|
2093
|
+
if (this.abortReason) {
|
|
2094
|
+
args?.dispose();
|
|
2095
|
+
throw this.abortReason;
|
|
2096
|
+
}
|
|
1799
2097
|
let value = [
|
|
1800
2098
|
"pipeline",
|
|
1801
2099
|
id,
|
|
1802
2100
|
path
|
|
1803
2101
|
];
|
|
1804
2102
|
if (args) {
|
|
1805
|
-
let devalue
|
|
2103
|
+
let devalue;
|
|
2104
|
+
try {
|
|
2105
|
+
devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
|
|
2106
|
+
} catch (err) {
|
|
2107
|
+
args.dispose();
|
|
2108
|
+
throw err;
|
|
2109
|
+
}
|
|
1806
2110
|
value.push(devalue[0]);
|
|
1807
2111
|
}
|
|
1808
2112
|
this.send(["push", value]);
|
|
@@ -1811,15 +2115,26 @@ var RpcSessionImpl = class {
|
|
|
1811
2115
|
return new RpcImportHook(true, entry);
|
|
1812
2116
|
}
|
|
1813
2117
|
sendStream(id, path, args) {
|
|
1814
|
-
if (this.abortReason)
|
|
2118
|
+
if (this.abortReason) {
|
|
2119
|
+
args.dispose();
|
|
2120
|
+
throw this.abortReason;
|
|
2121
|
+
}
|
|
1815
2122
|
let value = [
|
|
1816
2123
|
"pipeline",
|
|
1817
2124
|
id,
|
|
1818
2125
|
path
|
|
1819
2126
|
];
|
|
1820
|
-
let devalue
|
|
2127
|
+
let devalue;
|
|
2128
|
+
try {
|
|
2129
|
+
devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
|
|
2130
|
+
} catch (err) {
|
|
2131
|
+
args.dispose();
|
|
2132
|
+
throw err;
|
|
2133
|
+
}
|
|
1821
2134
|
value.push(devalue[0]);
|
|
1822
|
-
let
|
|
2135
|
+
let msg = ["stream", value];
|
|
2136
|
+
let size = this.send(msg);
|
|
2137
|
+
if (size === void 0) size = estimateEncodedSize(msg);
|
|
1823
2138
|
let importId = this.imports.length;
|
|
1824
2139
|
let entry = new ImportTableEntry(this, importId, true);
|
|
1825
2140
|
entry.remoteRefcount = 0;
|
|
@@ -1875,7 +2190,14 @@ var RpcSessionImpl = class {
|
|
|
1875
2190
|
this.cancelReadLoop?.(error);
|
|
1876
2191
|
this.cancelReadLoop = void 0;
|
|
1877
2192
|
if (trySendAbortMessage) try {
|
|
1878
|
-
|
|
2193
|
+
let abortMsg = ["abort", Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)];
|
|
2194
|
+
if (this.encodingLevel === "string") {
|
|
2195
|
+
let sent = this.transport.send(JSON.stringify(abortMsg));
|
|
2196
|
+
if (sent !== void 0 && typeof sent.catch === "function") sent.catch((err) => {});
|
|
2197
|
+
} else {
|
|
2198
|
+
let result = this.transport.send(abortMsg);
|
|
2199
|
+
if (result && typeof result.then === "function") Promise.resolve(result).catch((err) => {});
|
|
2200
|
+
}
|
|
1879
2201
|
} catch (err) {}
|
|
1880
2202
|
if (error === void 0) error = "undefined";
|
|
1881
2203
|
this.abortReason = error;
|
|
@@ -1897,18 +2219,19 @@ var RpcSessionImpl = class {
|
|
|
1897
2219
|
while (!this.abortReason) {
|
|
1898
2220
|
let readCanceled = Promise.withResolvers();
|
|
1899
2221
|
this.cancelReadLoop = readCanceled.reject;
|
|
1900
|
-
let
|
|
2222
|
+
let raw;
|
|
1901
2223
|
try {
|
|
1902
|
-
|
|
2224
|
+
raw = await Promise.race([this.transport.receive(), readCanceled.promise]);
|
|
1903
2225
|
} finally {
|
|
1904
2226
|
if (this.cancelReadLoop === readCanceled.reject) this.cancelReadLoop = void 0;
|
|
1905
2227
|
}
|
|
1906
|
-
|
|
2228
|
+
if (this.encodingLevel === "string" && raw.length > this.limits.maxMessageSize) throw new TypeError(`Incoming message exceeds maximum size of ${this.limits.maxMessageSize} UTF-16 code units.`);
|
|
1907
2229
|
if (this.abortReason) break;
|
|
2230
|
+
let msg = this.encodingLevel === "string" ? JSON.parse(raw) : raw;
|
|
1908
2231
|
if (msg instanceof Array) switch (msg[0]) {
|
|
1909
2232
|
case "push":
|
|
1910
2233
|
if (msg.length > 1) {
|
|
1911
|
-
let hook = new PayloadStubHook(new Evaluator(this).evaluate(msg[1]));
|
|
2234
|
+
let hook = new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[1]));
|
|
1912
2235
|
hook.ignoreUnhandledRejections();
|
|
1913
2236
|
this.exports.push({
|
|
1914
2237
|
hook,
|
|
@@ -1919,7 +2242,7 @@ var RpcSessionImpl = class {
|
|
|
1919
2242
|
break;
|
|
1920
2243
|
case "stream":
|
|
1921
2244
|
if (msg.length > 1) {
|
|
1922
|
-
let hook = new PayloadStubHook(new Evaluator(this).evaluate(msg[1]));
|
|
2245
|
+
let hook = new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[1]));
|
|
1923
2246
|
hook.ignoreUnhandledRejections();
|
|
1924
2247
|
let exportId = this.exports.length;
|
|
1925
2248
|
this.exports.push({
|
|
@@ -1954,13 +2277,13 @@ var RpcSessionImpl = class {
|
|
|
1954
2277
|
let importId = msg[1];
|
|
1955
2278
|
if (typeof importId == "number" && msg.length > 2) {
|
|
1956
2279
|
let imp = this.imports[importId];
|
|
1957
|
-
if (imp) if (msg[0] == "resolve") imp.resolve(new PayloadStubHook(new Evaluator(this).evaluate(msg[2])));
|
|
2280
|
+
if (imp) if (msg[0] == "resolve") imp.resolve(new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[2])));
|
|
1958
2281
|
else {
|
|
1959
|
-
let payload = new Evaluator(this).evaluate(msg[2]);
|
|
2282
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[2]);
|
|
1960
2283
|
payload.dispose();
|
|
1961
2284
|
imp.resolve(new ErrorStubHook(payload.value));
|
|
1962
2285
|
}
|
|
1963
|
-
else if (msg[0] == "resolve") new Evaluator(this).evaluate(msg[2]).dispose();
|
|
2286
|
+
else if (msg[0] == "resolve") new Evaluator(this, this.encodingLevel).evaluate(msg[2]).dispose();
|
|
1964
2287
|
continue;
|
|
1965
2288
|
}
|
|
1966
2289
|
break;
|
|
@@ -1975,9 +2298,9 @@ var RpcSessionImpl = class {
|
|
|
1975
2298
|
break;
|
|
1976
2299
|
}
|
|
1977
2300
|
case "abort": {
|
|
1978
|
-
let payload = new Evaluator(this).evaluate(msg[1]);
|
|
2301
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[1]);
|
|
1979
2302
|
payload.dispose();
|
|
1980
|
-
this.abort(payload, false);
|
|
2303
|
+
this.abort(payload.value, false);
|
|
1981
2304
|
break;
|
|
1982
2305
|
}
|
|
1983
2306
|
}
|
|
@@ -2028,6 +2351,8 @@ var RpcSession$1 = class {
|
|
|
2028
2351
|
|
|
2029
2352
|
//#endregion
|
|
2030
2353
|
//#region src/websocket.ts
|
|
2354
|
+
/** Close-frame reason max UTF-8 bytes: 125 payload - 2-byte status (RFC 6455 section 5.5). */
|
|
2355
|
+
const MAX_CLOSE_REASON_BYTES = 123;
|
|
2031
2356
|
function newWebSocketRpcSession$1(webSocket, localMain, options) {
|
|
2032
2357
|
if (typeof webSocket === "string") webSocket = new WebSocket(webSocket);
|
|
2033
2358
|
return new RpcSession$1(new WebSocketTransport(webSocket), localMain, options).getRemoteMain();
|
|
@@ -2047,9 +2372,14 @@ function newWorkersWebSocketRpcResponse(request, localMain, options) {
|
|
|
2047
2372
|
webSocket: pair[1]
|
|
2048
2373
|
});
|
|
2049
2374
|
}
|
|
2375
|
+
/**
|
|
2376
|
+
* Generic WebSocket transport. Default `T = string` is backward-compatible and satisfies
|
|
2377
|
+
* `RpcTransport`. Use `T = ArrayBuffer` as a building block for binary transports.
|
|
2378
|
+
*/
|
|
2050
2379
|
var WebSocketTransport = class {
|
|
2051
2380
|
constructor(webSocket) {
|
|
2052
2381
|
this.#webSocket = webSocket;
|
|
2382
|
+
webSocket.binaryType = "arraybuffer";
|
|
2053
2383
|
if (webSocket.readyState === WebSocket.CONNECTING) {
|
|
2054
2384
|
this.#sendQueue = [];
|
|
2055
2385
|
webSocket.addEventListener("open", (event) => {
|
|
@@ -2062,12 +2392,12 @@ var WebSocketTransport = class {
|
|
|
2062
2392
|
});
|
|
2063
2393
|
}
|
|
2064
2394
|
webSocket.addEventListener("message", (event) => {
|
|
2065
|
-
if (this.#error) {} else if (typeof event.data === "string") if (this.#receiveResolver) {
|
|
2395
|
+
if (this.#error) {} else if (typeof event.data === "string" || event.data instanceof ArrayBuffer) if (this.#receiveResolver) {
|
|
2066
2396
|
this.#receiveResolver(event.data);
|
|
2067
2397
|
this.#receiveResolver = void 0;
|
|
2068
2398
|
this.#receiveRejecter = void 0;
|
|
2069
2399
|
} else this.#receiveQueue.push(event.data);
|
|
2070
|
-
else this.#receivedError(/* @__PURE__ */ new TypeError("Received
|
|
2400
|
+
else this.#receivedError(/* @__PURE__ */ new TypeError("Received unexpected message type from WebSocket."));
|
|
2071
2401
|
});
|
|
2072
2402
|
webSocket.addEventListener("close", (event) => {
|
|
2073
2403
|
this.#receivedError(/* @__PURE__ */ new Error(`Peer closed WebSocket: ${event.code} ${event.reason}`));
|
|
@@ -2082,13 +2412,13 @@ var WebSocketTransport = class {
|
|
|
2082
2412
|
#receiveRejecter;
|
|
2083
2413
|
#receiveQueue = [];
|
|
2084
2414
|
#error;
|
|
2085
|
-
|
|
2415
|
+
send(message) {
|
|
2086
2416
|
if (this.#sendQueue === void 0) this.#webSocket.send(message);
|
|
2087
2417
|
else this.#sendQueue.push(message);
|
|
2088
2418
|
}
|
|
2089
|
-
|
|
2090
|
-
if (this.#receiveQueue.length > 0) return this.#receiveQueue.shift();
|
|
2091
|
-
else if (this.#error)
|
|
2419
|
+
receive() {
|
|
2420
|
+
if (this.#receiveQueue.length > 0) return Promise.resolve(this.#receiveQueue.shift());
|
|
2421
|
+
else if (this.#error) return Promise.reject(this.#error);
|
|
2092
2422
|
else return new Promise((resolve, reject) => {
|
|
2093
2423
|
this.#receiveResolver = resolve;
|
|
2094
2424
|
this.#receiveRejecter = reject;
|
|
@@ -2098,6 +2428,8 @@ var WebSocketTransport = class {
|
|
|
2098
2428
|
let message;
|
|
2099
2429
|
if (reason instanceof Error) message = reason.message;
|
|
2100
2430
|
else message = `${reason}`;
|
|
2431
|
+
let reasonBytes = new TextEncoder().encode(message);
|
|
2432
|
+
if (reasonBytes.length > 123) message = new TextDecoder().decode(reasonBytes.subarray(0, 123), { stream: true });
|
|
2101
2433
|
this.#webSocket.close(3e3, message);
|
|
2102
2434
|
if (!this.#error) this.#error = reason;
|
|
2103
2435
|
}
|
|
@@ -2115,6 +2447,7 @@ var WebSocketTransport = class {
|
|
|
2115
2447
|
|
|
2116
2448
|
//#endregion
|
|
2117
2449
|
//#region src/batch.ts
|
|
2450
|
+
const yieldToMacrotask = typeof setImmediate === "function" ? () => new Promise((resolve) => setImmediate(resolve)) : () => new Promise((resolve) => setTimeout(resolve, 0));
|
|
2118
2451
|
var BatchClientTransport = class {
|
|
2119
2452
|
constructor(sendBatch) {
|
|
2120
2453
|
this.#promise = this.#scheduleBatch(sendBatch);
|
|
@@ -2123,7 +2456,7 @@ var BatchClientTransport = class {
|
|
|
2123
2456
|
#aborted;
|
|
2124
2457
|
#batchToSend = [];
|
|
2125
2458
|
#batchToReceive = null;
|
|
2126
|
-
|
|
2459
|
+
send(message) {
|
|
2127
2460
|
if (this.#batchToSend !== null) this.#batchToSend.push(message);
|
|
2128
2461
|
}
|
|
2129
2462
|
async receive() {
|
|
@@ -2136,7 +2469,7 @@ var BatchClientTransport = class {
|
|
|
2136
2469
|
this.#aborted = reason;
|
|
2137
2470
|
}
|
|
2138
2471
|
async #scheduleBatch(sendBatch) {
|
|
2139
|
-
await
|
|
2472
|
+
await yieldToMacrotask();
|
|
2140
2473
|
if (this.#aborted !== void 0) throw this.#aborted;
|
|
2141
2474
|
let batch = this.#batchToSend;
|
|
2142
2475
|
this.#batchToSend = null;
|
|
@@ -2165,7 +2498,7 @@ var BatchServerTransport = class {
|
|
|
2165
2498
|
#batchToSend = [];
|
|
2166
2499
|
#batchToReceive;
|
|
2167
2500
|
#allReceived = Promise.withResolvers();
|
|
2168
|
-
|
|
2501
|
+
send(message) {
|
|
2169
2502
|
this.#batchToSend.push(message);
|
|
2170
2503
|
}
|
|
2171
2504
|
async receive() {
|
|
@@ -2214,7 +2547,11 @@ async function newHttpBatchRpcResponse(request, localMain, options) {
|
|
|
2214
2547
|
* @param options Optional RPC session options. You can also pass headers to set on the response.
|
|
2215
2548
|
*/
|
|
2216
2549
|
async function nodeHttpBatchRpcResponse(request, response, localMain, options) {
|
|
2217
|
-
if (request.method !== "POST")
|
|
2550
|
+
if (request.method !== "POST") {
|
|
2551
|
+
response.writeHead(405, "This endpoint only accepts POST requests.", options?.headers);
|
|
2552
|
+
response.end();
|
|
2553
|
+
return;
|
|
2554
|
+
}
|
|
2218
2555
|
let body = await new Promise((resolve, reject) => {
|
|
2219
2556
|
let chunks = [];
|
|
2220
2557
|
request.on("data", (chunk) => {
|
|
@@ -2239,17 +2576,17 @@ function newMessagePortRpcSession$1(port, localMain, options) {
|
|
|
2239
2576
|
return new RpcSession$1(new MessagePortTransport(port), localMain, options).getRemoteMain();
|
|
2240
2577
|
}
|
|
2241
2578
|
var MessagePortTransport = class {
|
|
2579
|
+
encodingLevel = "structuredClonable";
|
|
2242
2580
|
constructor(port) {
|
|
2243
2581
|
this.#port = port;
|
|
2244
2582
|
port.start();
|
|
2245
2583
|
port.addEventListener("message", (event) => {
|
|
2246
2584
|
if (this.#error) {} else if (event.data === null) this.#receivedError(/* @__PURE__ */ new Error("Peer closed MessagePort connection."));
|
|
2247
|
-
else if (
|
|
2585
|
+
else if (this.#receiveResolver) {
|
|
2248
2586
|
this.#receiveResolver(event.data);
|
|
2249
2587
|
this.#receiveResolver = void 0;
|
|
2250
2588
|
this.#receiveRejecter = void 0;
|
|
2251
2589
|
} else this.#receiveQueue.push(event.data);
|
|
2252
|
-
else this.#receivedError(/* @__PURE__ */ new TypeError("Received non-string message from MessagePort."));
|
|
2253
2590
|
});
|
|
2254
2591
|
port.addEventListener("messageerror", (event) => {
|
|
2255
2592
|
this.#receivedError(/* @__PURE__ */ new Error("MessagePort message error."));
|
|
@@ -2260,7 +2597,7 @@ var MessagePortTransport = class {
|
|
|
2260
2597
|
#receiveRejecter;
|
|
2261
2598
|
#receiveQueue = [];
|
|
2262
2599
|
#error;
|
|
2263
|
-
|
|
2600
|
+
send(message) {
|
|
2264
2601
|
if (this.#error) throw this.#error;
|
|
2265
2602
|
this.#port.postMessage(message);
|
|
2266
2603
|
}
|
|
@@ -2424,9 +2761,11 @@ var MapVariableHook = class extends StubHook {
|
|
|
2424
2761
|
else throwMapperBuilderUseError();
|
|
2425
2762
|
}
|
|
2426
2763
|
call(path, args) {
|
|
2764
|
+
args.dispose();
|
|
2427
2765
|
throwMapperBuilderUseError();
|
|
2428
2766
|
}
|
|
2429
2767
|
map(path, captures, instructions) {
|
|
2768
|
+
for (let cap of captures) cap.dispose();
|
|
2430
2769
|
throwMapperBuilderUseError();
|
|
2431
2770
|
}
|
|
2432
2771
|
pull() {
|
|
@@ -2479,6 +2818,9 @@ var MapApplicator = class {
|
|
|
2479
2818
|
getPipeReadable(exportId) {
|
|
2480
2819
|
throw new Error("A mapper function cannot use pipe readables.");
|
|
2481
2820
|
}
|
|
2821
|
+
getLimits() {
|
|
2822
|
+
return DEFAULT_LIMITS;
|
|
2823
|
+
}
|
|
2482
2824
|
};
|
|
2483
2825
|
function applyMapToElement(input, parent, owner, captures, instructions) {
|
|
2484
2826
|
let mapper = new MapApplicator(captures, new PayloadStubHook(RpcPayload.deepCopyFrom(input, parent, owner)));
|
|
@@ -2534,14 +2876,11 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
|
|
|
2534
2876
|
let state = this.getState();
|
|
2535
2877
|
if (path.length !== 1 || typeof path[0] !== "string") throw new Error("WritableStream stub only supports direct method calls");
|
|
2536
2878
|
const method = path[0];
|
|
2537
|
-
if (method !== "write" && method !== "close" && method !== "abort") {
|
|
2538
|
-
args.dispose();
|
|
2539
|
-
throw new Error(`Unknown WritableStream method: ${method}`);
|
|
2540
|
-
}
|
|
2879
|
+
if (method !== "write" && method !== "close" && method !== "abort") throw new Error(`Unknown WritableStream method: ${method}`);
|
|
2541
2880
|
if (method === "close" || method === "abort") state.closed = true;
|
|
2542
|
-
|
|
2543
|
-
return new PromiseStubHook(args.deliverCall(func, state.writer).then((payload) => new PayloadStubHook(payload)));
|
|
2881
|
+
return new PromiseStubHook((method === "write" ? args.deliverStreamWrite(state.writer) : args.deliverCall(state.writer[method], state.writer)).then((payload) => new PayloadStubHook(payload)));
|
|
2544
2882
|
} catch (err) {
|
|
2883
|
+
args.dispose();
|
|
2545
2884
|
return new ErrorStubHook(err);
|
|
2546
2885
|
}
|
|
2547
2886
|
}
|
|
@@ -2817,15 +3156,15 @@ let newMessagePortRpcSession = newMessagePortRpcSession$1;
|
|
|
2817
3156
|
* credentials as parameters and returns the authorized API), then cross-origin requests should
|
|
2818
3157
|
* be safe.
|
|
2819
3158
|
*/
|
|
2820
|
-
async function newWorkersRpcResponse(request, localMain) {
|
|
3159
|
+
async function newWorkersRpcResponse(request, localMain, options) {
|
|
2821
3160
|
if (request.method === "POST") {
|
|
2822
|
-
let response = await newHttpBatchRpcResponse(request, localMain);
|
|
3161
|
+
let response = await newHttpBatchRpcResponse(request, localMain, options);
|
|
2823
3162
|
response.headers.set("Access-Control-Allow-Origin", "*");
|
|
2824
3163
|
return response;
|
|
2825
|
-
} else if (request.headers.get("Upgrade")?.toLowerCase() === "websocket") return newWorkersWebSocketRpcResponse(request, localMain);
|
|
3164
|
+
} else if (request.headers.get("Upgrade")?.toLowerCase() === "websocket") return newWorkersWebSocketRpcResponse(request, localMain, options);
|
|
2826
3165
|
else return new Response("This endpoint only accepts POST or WebSocket requests.", { status: 400 });
|
|
2827
3166
|
}
|
|
2828
3167
|
|
|
2829
3168
|
//#endregion
|
|
2830
|
-
export { RpcPromise, RpcSession, RpcStub, RpcTarget, deserialize, newHttpBatchRpcResponse, newHttpBatchRpcSession, newMessagePortRpcSession, newWebSocketRpcSession, newWorkersRpcResponse, newWorkersWebSocketRpcResponse, nodeHttpBatchRpcResponse, serialize };
|
|
3169
|
+
export { DEFAULT_LIMITS, DEFAULT_MAX_DEPTH, RpcPromise, RpcSession, RpcStub, RpcTarget, WebSocketTransport, deserialize, newHttpBatchRpcResponse, newHttpBatchRpcSession, newMessagePortRpcSession, newWebSocketRpcSession, newWorkersRpcResponse, newWorkersWebSocketRpcResponse, nodeHttpBatchRpcResponse, serialize };
|
|
2831
3170
|
//# sourceMappingURL=index-workers.js.map
|