capnweb 0.0.0-409821 → 0.0.0-43aa384
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 +10 -2
- package/dist/index-bun.cjs +374 -78
- package/dist/index-bun.cjs.map +1 -1
- package/dist/index-bun.d.cts +105 -8
- package/dist/index-bun.d.ts +105 -8
- package/dist/index-bun.js +372 -79
- package/dist/index-bun.js.map +1 -1
- package/dist/index-workers.cjs +374 -78
- package/dist/index-workers.cjs.map +1 -1
- package/dist/index-workers.d.cts +102 -8
- package/dist/index-workers.d.ts +102 -8
- package/dist/index-workers.js +372 -79
- package/dist/index-workers.js.map +1 -1
- package/dist/index.cjs +374 -78
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +102 -8
- package/dist/index.d.ts +102 -8
- package/dist/index.js +372 -79
- package/dist/index.js.map +1 -1
- package/package.json +5 -4
package/dist/index.cjs
CHANGED
|
@@ -43,7 +43,19 @@ function typeForRpc(value) {
|
|
|
43
43
|
case Array.prototype: return "array";
|
|
44
44
|
case Date.prototype: return "date";
|
|
45
45
|
case Uint8Array.prototype:
|
|
46
|
-
case BUFFER_PROTOTYPE:
|
|
46
|
+
case BUFFER_PROTOTYPE:
|
|
47
|
+
case ArrayBuffer.prototype:
|
|
48
|
+
case DataView.prototype:
|
|
49
|
+
case Int8Array.prototype:
|
|
50
|
+
case Uint8ClampedArray.prototype:
|
|
51
|
+
case Int16Array.prototype:
|
|
52
|
+
case Uint16Array.prototype:
|
|
53
|
+
case Int32Array.prototype:
|
|
54
|
+
case Uint32Array.prototype:
|
|
55
|
+
case BigInt64Array.prototype:
|
|
56
|
+
case BigUint64Array.prototype:
|
|
57
|
+
case Float32Array.prototype:
|
|
58
|
+
case Float64Array.prototype: return "bytes";
|
|
47
59
|
case WritableStream.prototype: return "writable";
|
|
48
60
|
case ReadableStream.prototype: return "readable";
|
|
49
61
|
case Headers.prototype: return "headers";
|
|
@@ -513,6 +525,30 @@ var RpcPayload = class RpcPayload {
|
|
|
513
525
|
this.dispose();
|
|
514
526
|
}
|
|
515
527
|
}
|
|
528
|
+
async deliverStreamWrite(writer) {
|
|
529
|
+
try {
|
|
530
|
+
let promises = [];
|
|
531
|
+
this.deliverTo(this, "value", promises);
|
|
532
|
+
if (promises.length > 0) await Promise.all(promises);
|
|
533
|
+
let chunk = this.value[0];
|
|
534
|
+
if ((this.hooks.length > 0 || this.promises.length > 0) && chunk instanceof Object) {
|
|
535
|
+
if (!(Symbol.dispose in chunk)) Object.defineProperty(chunk, Symbol.dispose, {
|
|
536
|
+
value: () => this.dispose(),
|
|
537
|
+
writable: true,
|
|
538
|
+
enumerable: false,
|
|
539
|
+
configurable: true
|
|
540
|
+
});
|
|
541
|
+
await writer.write(chunk);
|
|
542
|
+
return RpcPayload.fromAppReturn(void 0);
|
|
543
|
+
}
|
|
544
|
+
await writer.write(chunk);
|
|
545
|
+
this.dispose();
|
|
546
|
+
return RpcPayload.fromAppReturn(void 0);
|
|
547
|
+
} catch (err) {
|
|
548
|
+
this.dispose();
|
|
549
|
+
throw err;
|
|
550
|
+
}
|
|
551
|
+
}
|
|
516
552
|
async deliverResolve() {
|
|
517
553
|
try {
|
|
518
554
|
let promises = [];
|
|
@@ -931,6 +967,77 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
|
931
967
|
|
|
932
968
|
//#endregion
|
|
933
969
|
//#region src/serialize.ts
|
|
970
|
+
const DEFAULT_MAX_DEPTH = 256;
|
|
971
|
+
const DEFAULT_LIMITS = {
|
|
972
|
+
maxBigIntDigits: 16384,
|
|
973
|
+
maxDepth: 256,
|
|
974
|
+
maxMessageSize: 32 * 1024 * 1024
|
|
975
|
+
};
|
|
976
|
+
const NATIVE_LITTLE_ENDIAN = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
|
|
977
|
+
const BYTE_CONTAINER_TYPE_NAMES = [
|
|
978
|
+
"ArrayBuffer",
|
|
979
|
+
"DataView",
|
|
980
|
+
"Int8Array",
|
|
981
|
+
"Uint8Array",
|
|
982
|
+
"Uint8ClampedArray",
|
|
983
|
+
"Int16Array",
|
|
984
|
+
"Uint16Array",
|
|
985
|
+
"Int32Array",
|
|
986
|
+
"Uint32Array",
|
|
987
|
+
"BigInt64Array",
|
|
988
|
+
"BigUint64Array",
|
|
989
|
+
"Float32Array",
|
|
990
|
+
"Float64Array"
|
|
991
|
+
];
|
|
992
|
+
function isValidByteContainerName(value) {
|
|
993
|
+
return BYTE_CONTAINER_TYPE_NAMES.includes(value);
|
|
994
|
+
}
|
|
995
|
+
const TYPED_ARRAY_ELEMENT_SIZE = {
|
|
996
|
+
ArrayBuffer: void 0,
|
|
997
|
+
DataView: void 0,
|
|
998
|
+
Int8Array: void 0,
|
|
999
|
+
Uint8Array: void 0,
|
|
1000
|
+
Uint8ClampedArray: void 0,
|
|
1001
|
+
Int16Array: 2,
|
|
1002
|
+
Uint16Array: 2,
|
|
1003
|
+
Int32Array: 4,
|
|
1004
|
+
Uint32Array: 4,
|
|
1005
|
+
BigInt64Array: 8,
|
|
1006
|
+
BigUint64Array: 8,
|
|
1007
|
+
Float32Array: 4,
|
|
1008
|
+
Float64Array: 8
|
|
1009
|
+
};
|
|
1010
|
+
const BYTE_CONTAINER_PROTOTYPES = {
|
|
1011
|
+
ArrayBuffer: ArrayBuffer.prototype,
|
|
1012
|
+
DataView: DataView.prototype,
|
|
1013
|
+
Int8Array: Int8Array.prototype,
|
|
1014
|
+
Uint8ClampedArray: Uint8ClampedArray.prototype,
|
|
1015
|
+
Int16Array: Int16Array.prototype,
|
|
1016
|
+
Uint16Array: Uint16Array.prototype,
|
|
1017
|
+
Int32Array: Int32Array.prototype,
|
|
1018
|
+
Uint32Array: Uint32Array.prototype,
|
|
1019
|
+
BigInt64Array: BigInt64Array.prototype,
|
|
1020
|
+
BigUint64Array: BigUint64Array.prototype,
|
|
1021
|
+
Float32Array: Float32Array.prototype,
|
|
1022
|
+
Float64Array: Float64Array.prototype
|
|
1023
|
+
};
|
|
1024
|
+
const BYTE_CONTAINER_TYPE_BY_PROTOTYPE = /* @__PURE__ */ new Map();
|
|
1025
|
+
for (let type of Object.keys(BYTE_CONTAINER_PROTOTYPES)) BYTE_CONTAINER_TYPE_BY_PROTOTYPE.set(BYTE_CONTAINER_PROTOTYPES[type], type);
|
|
1026
|
+
function swapByteOrder(bytes, elementSize) {
|
|
1027
|
+
if (elementSize !== 2 && elementSize !== 4 && elementSize !== 8) throw new RangeError(`Unsupported element size: ${elementSize}`);
|
|
1028
|
+
let view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
1029
|
+
for (let offset = 0; offset < bytes.byteLength; offset += elementSize) switch (elementSize) {
|
|
1030
|
+
case 2:
|
|
1031
|
+
view.setUint16(offset, view.getUint16(offset, false), true);
|
|
1032
|
+
break;
|
|
1033
|
+
case 4:
|
|
1034
|
+
view.setUint32(offset, view.getUint32(offset, false), true);
|
|
1035
|
+
break;
|
|
1036
|
+
case 8:
|
|
1037
|
+
view.setBigUint64(offset, view.getBigUint64(offset, false), true);
|
|
1038
|
+
break;
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
934
1041
|
var NullExporter = class {
|
|
935
1042
|
exportStub(stub) {
|
|
936
1043
|
throw new Error("Cannot serialize RPC stubs without an RPC session.");
|
|
@@ -951,6 +1058,7 @@ async function streamToBlob(stream, type) {
|
|
|
951
1058
|
return b.type === type ? b : b.slice(0, b.size, type);
|
|
952
1059
|
}
|
|
953
1060
|
const ERROR_TYPES = {
|
|
1061
|
+
__proto__: null,
|
|
954
1062
|
Error,
|
|
955
1063
|
EvalError,
|
|
956
1064
|
RangeError,
|
|
@@ -963,12 +1071,14 @@ const ERROR_TYPES = {
|
|
|
963
1071
|
var Devaluator = class Devaluator {
|
|
964
1072
|
exporter;
|
|
965
1073
|
source;
|
|
966
|
-
|
|
1074
|
+
encodingLevel;
|
|
1075
|
+
constructor(exporter, source, encodingLevel) {
|
|
967
1076
|
this.exporter = exporter;
|
|
968
1077
|
this.source = source;
|
|
1078
|
+
this.encodingLevel = encodingLevel;
|
|
969
1079
|
}
|
|
970
|
-
static devaluate(value, parent, exporter = NULL_EXPORTER, source) {
|
|
971
|
-
let devaluator = new Devaluator(exporter, source);
|
|
1080
|
+
static devaluate(value, parent, exporter = NULL_EXPORTER, source, encodingLevel = "string") {
|
|
1081
|
+
let devaluator = new Devaluator(exporter, source, encodingLevel);
|
|
972
1082
|
try {
|
|
973
1083
|
return devaluator.devaluateImpl(value, parent, 0);
|
|
974
1084
|
} catch (err) {
|
|
@@ -980,7 +1090,7 @@ var Devaluator = class Devaluator {
|
|
|
980
1090
|
}
|
|
981
1091
|
exports;
|
|
982
1092
|
devaluateImpl(value, parent, depth) {
|
|
983
|
-
if (depth >=
|
|
1093
|
+
if (depth >= 256) throw new Error("Serialization exceeded maximum allowed depth. (Does the message contain cycles?)");
|
|
984
1094
|
switch (typeForRpc(value)) {
|
|
985
1095
|
case "unsupported": {
|
|
986
1096
|
let msg;
|
|
@@ -991,10 +1101,12 @@ var Devaluator = class Devaluator {
|
|
|
991
1101
|
}
|
|
992
1102
|
throw new TypeError(msg);
|
|
993
1103
|
}
|
|
994
|
-
case "primitive": if (typeof value === "number" && !isFinite(value))
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
1104
|
+
case "primitive": if (typeof value === "number" && !isFinite(value)) {
|
|
1105
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1106
|
+
if (value === Infinity) return ["inf"];
|
|
1107
|
+
else if (value === -Infinity) return ["-inf"];
|
|
1108
|
+
else return ["nan"];
|
|
1109
|
+
} else return value;
|
|
998
1110
|
case "object": {
|
|
999
1111
|
let object = value;
|
|
1000
1112
|
let result = {};
|
|
@@ -1008,22 +1120,47 @@ var Devaluator = class Devaluator {
|
|
|
1008
1120
|
for (let i = 0; i < len; i++) result[i] = this.devaluateImpl(array[i], array, depth + 1);
|
|
1009
1121
|
return [result];
|
|
1010
1122
|
}
|
|
1011
|
-
case "bigint":
|
|
1123
|
+
case "bigint":
|
|
1124
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1125
|
+
return ["bigint", value.toString()];
|
|
1012
1126
|
case "date": {
|
|
1127
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1013
1128
|
const time = value.getTime();
|
|
1014
1129
|
return ["date", Number.isNaN(time) ? null : time];
|
|
1015
1130
|
}
|
|
1016
1131
|
case "bytes": {
|
|
1017
|
-
let
|
|
1018
|
-
|
|
1132
|
+
let alternateTypeName = BYTE_CONTAINER_TYPE_BY_PROTOTYPE.get(Object.getPrototypeOf(value));
|
|
1133
|
+
let bytes;
|
|
1134
|
+
if (alternateTypeName === "ArrayBuffer") bytes = new Uint8Array(value);
|
|
1135
|
+
else if (alternateTypeName === void 0) bytes = value;
|
|
1136
|
+
else {
|
|
1137
|
+
let view = value;
|
|
1138
|
+
bytes = new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
|
|
1139
|
+
let elementSize = TYPED_ARRAY_ELEMENT_SIZE[alternateTypeName];
|
|
1140
|
+
if (!NATIVE_LITTLE_ENDIAN && elementSize) {
|
|
1141
|
+
bytes = bytes.slice();
|
|
1142
|
+
swapByteOrder(bytes, elementSize);
|
|
1143
|
+
}
|
|
1144
|
+
}
|
|
1145
|
+
if (this.encodingLevel === "structuredClonable" || this.encodingLevel === "jsonCompatibleWithBytes") return alternateTypeName === void 0 ? ["bytes", bytes] : [
|
|
1146
|
+
"bytes",
|
|
1147
|
+
bytes,
|
|
1148
|
+
alternateTypeName
|
|
1149
|
+
];
|
|
1019
1150
|
let b64;
|
|
1020
|
-
if (
|
|
1151
|
+
if (bytes.toBase64) b64 = bytes.toBase64({ omitPadding: true });
|
|
1152
|
+
else if (typeof Buffer !== "undefined") b64 = (bytes instanceof Buffer ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)).toString("base64");
|
|
1021
1153
|
else {
|
|
1022
1154
|
let binary = "";
|
|
1023
1155
|
for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]);
|
|
1024
1156
|
b64 = btoa(binary);
|
|
1025
1157
|
}
|
|
1026
|
-
|
|
1158
|
+
b64 = b64.replace(/=+$/, "");
|
|
1159
|
+
return alternateTypeName === void 0 ? ["bytes", b64] : [
|
|
1160
|
+
"bytes",
|
|
1161
|
+
b64,
|
|
1162
|
+
alternateTypeName
|
|
1163
|
+
];
|
|
1027
1164
|
}
|
|
1028
1165
|
case "headers": return ["headers", [...value]];
|
|
1029
1166
|
case "request": {
|
|
@@ -1139,7 +1276,9 @@ var Devaluator = class Devaluator {
|
|
|
1139
1276
|
} else if (rewritten && rewritten.stack) result.push(rewritten.stack);
|
|
1140
1277
|
return result;
|
|
1141
1278
|
}
|
|
1142
|
-
case "undefined":
|
|
1279
|
+
case "undefined":
|
|
1280
|
+
if (this.encodingLevel === "structuredClonable") return;
|
|
1281
|
+
return ["undefined"];
|
|
1143
1282
|
case "stub":
|
|
1144
1283
|
case "rpc-promise": {
|
|
1145
1284
|
if (!this.source) throw new Error("Can't serialize RPC stubs in this context.");
|
|
@@ -1206,6 +1345,9 @@ var NullImporter = class {
|
|
|
1206
1345
|
getPipeReadable(exportId) {
|
|
1207
1346
|
throw new Error("Cannot retrieve pipe readable without an RPC session.");
|
|
1208
1347
|
}
|
|
1348
|
+
getLimits() {
|
|
1349
|
+
return DEFAULT_LIMITS;
|
|
1350
|
+
}
|
|
1209
1351
|
};
|
|
1210
1352
|
const NULL_IMPORTER = new NullImporter();
|
|
1211
1353
|
function fixBrokenRequestBody(request, body) {
|
|
@@ -1222,15 +1364,22 @@ function streamToBlobPromise(stream, type) {
|
|
|
1222
1364
|
}
|
|
1223
1365
|
var Evaluator = class Evaluator {
|
|
1224
1366
|
importer;
|
|
1225
|
-
|
|
1367
|
+
encodingLevel;
|
|
1368
|
+
limits;
|
|
1369
|
+
constructor(importer, encodingLevel = "string") {
|
|
1226
1370
|
this.importer = importer;
|
|
1371
|
+
this.encodingLevel = encodingLevel;
|
|
1372
|
+
this.limits = importer.getLimits();
|
|
1227
1373
|
}
|
|
1228
1374
|
hooks = [];
|
|
1229
1375
|
promises = [];
|
|
1230
1376
|
evaluate(value) {
|
|
1377
|
+
return this.evaluateWithDepth(value, 0);
|
|
1378
|
+
}
|
|
1379
|
+
evaluateWithDepth(value, depth) {
|
|
1231
1380
|
let payload = RpcPayload.forEvaluate(this.hooks, this.promises);
|
|
1232
1381
|
try {
|
|
1233
|
-
payload.value = this.evaluateImpl(value, payload, "value");
|
|
1382
|
+
payload.value = this.evaluateImpl(value, payload, "value", depth);
|
|
1234
1383
|
return payload;
|
|
1235
1384
|
} catch (err) {
|
|
1236
1385
|
payload.dispose();
|
|
@@ -1240,31 +1389,70 @@ var Evaluator = class Evaluator {
|
|
|
1240
1389
|
evaluateCopy(value) {
|
|
1241
1390
|
return this.evaluate(structuredClone(value));
|
|
1242
1391
|
}
|
|
1243
|
-
evaluateImpl(value, parent, property) {
|
|
1392
|
+
evaluateImpl(value, parent, property, depth) {
|
|
1393
|
+
let maxDepth = this.limits.maxDepth;
|
|
1394
|
+
if (depth >= maxDepth) throw new TypeError(`Deserialization exceeded maximum allowed message depth of ${maxDepth}.`);
|
|
1395
|
+
if (this.encodingLevel === "structuredClonable") {
|
|
1396
|
+
if (value instanceof Date || typeof value === "bigint") return value;
|
|
1397
|
+
}
|
|
1244
1398
|
if (value instanceof Array) {
|
|
1245
1399
|
if (value.length == 1 && value[0] instanceof Array) {
|
|
1246
1400
|
let result = value[0];
|
|
1247
|
-
for (let i = 0; i < result.length; i++) result[i] = this.evaluateImpl(result[i], result, i);
|
|
1401
|
+
for (let i = 0; i < result.length; i++) result[i] = this.evaluateImpl(result[i], result, i, depth + 1);
|
|
1248
1402
|
return result;
|
|
1249
1403
|
} else switch (value[0]) {
|
|
1250
1404
|
case "bigint":
|
|
1251
|
-
if (typeof value[1] == "string")
|
|
1405
|
+
if (typeof value[1] == "string") {
|
|
1406
|
+
let digits = value[1];
|
|
1407
|
+
let maxBigIntDigits = this.limits.maxBigIntDigits;
|
|
1408
|
+
if (digits.length > maxBigIntDigits) throw new TypeError(`Deserialized bigint exceeds maximum length of ${maxBigIntDigits} digits.`);
|
|
1409
|
+
return BigInt(digits);
|
|
1410
|
+
}
|
|
1252
1411
|
break;
|
|
1253
1412
|
case "date":
|
|
1254
1413
|
if (value[1] === null) return /* @__PURE__ */ new Date(NaN);
|
|
1255
1414
|
if (typeof value[1] == "number") return new Date(value[1]);
|
|
1256
1415
|
break;
|
|
1257
|
-
case "bytes":
|
|
1258
|
-
|
|
1259
|
-
|
|
1416
|
+
case "bytes": {
|
|
1417
|
+
let bytes;
|
|
1418
|
+
if (value[1] instanceof Uint8Array) bytes = value[1];
|
|
1419
|
+
else if (typeof value[1] == "string") if (typeof Buffer !== "undefined") bytes = Buffer.from(value[1], "base64");
|
|
1420
|
+
else if (Uint8Array.fromBase64) bytes = Uint8Array.fromBase64(value[1]);
|
|
1260
1421
|
else {
|
|
1261
1422
|
let bs = atob(value[1]);
|
|
1262
1423
|
let len = bs.length;
|
|
1263
|
-
|
|
1424
|
+
bytes = new Uint8Array(len);
|
|
1264
1425
|
for (let i = 0; i < len; i++) bytes[i] = bs.charCodeAt(i);
|
|
1265
|
-
return bytes;
|
|
1266
1426
|
}
|
|
1267
|
-
break;
|
|
1427
|
+
else break;
|
|
1428
|
+
if (value.length === 2) return bytes;
|
|
1429
|
+
if (typeof value[2] !== "string") throw new TypeError(`Unknown bytes type marker type: ${typeof value[2]}`);
|
|
1430
|
+
if (!isValidByteContainerName(value[2])) {
|
|
1431
|
+
let marker = value[2].slice(0, 64);
|
|
1432
|
+
throw new TypeError(`Unknown bytes type marker: ${marker}`);
|
|
1433
|
+
}
|
|
1434
|
+
let marker = value[2];
|
|
1435
|
+
let elementSize = TYPED_ARRAY_ELEMENT_SIZE[marker];
|
|
1436
|
+
if (elementSize !== void 0 && bytes.byteLength % elementSize !== 0) throw new TypeError(`Invalid byte length ${bytes.byteLength} for ${marker}; expected a multiple of ${elementSize}`);
|
|
1437
|
+
let buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
|
1438
|
+
if (!NATIVE_LITTLE_ENDIAN && elementSize !== void 0) swapByteOrder(new Uint8Array(buffer), elementSize);
|
|
1439
|
+
switch (marker) {
|
|
1440
|
+
case "ArrayBuffer": return buffer;
|
|
1441
|
+
case "DataView": return new DataView(buffer);
|
|
1442
|
+
case "Int8Array": return new Int8Array(buffer);
|
|
1443
|
+
case "Uint8Array": return new Uint8Array(buffer);
|
|
1444
|
+
case "Uint8ClampedArray": return new Uint8ClampedArray(buffer);
|
|
1445
|
+
case "Int16Array": return new Int16Array(buffer);
|
|
1446
|
+
case "Uint16Array": return new Uint16Array(buffer);
|
|
1447
|
+
case "Int32Array": return new Int32Array(buffer);
|
|
1448
|
+
case "Uint32Array": return new Uint32Array(buffer);
|
|
1449
|
+
case "BigInt64Array": return new BigInt64Array(buffer);
|
|
1450
|
+
case "BigUint64Array": return new BigUint64Array(buffer);
|
|
1451
|
+
case "Float32Array": return new Float32Array(buffer);
|
|
1452
|
+
case "Float64Array": return new Float64Array(buffer);
|
|
1453
|
+
default:
|
|
1454
|
+
}
|
|
1455
|
+
}
|
|
1268
1456
|
case "error":
|
|
1269
1457
|
if (value.length >= 3 && typeof value[1] === "string" && typeof value[2] === "string") {
|
|
1270
1458
|
let cls = ERROR_TYPES[value[1]] || Error;
|
|
@@ -1277,7 +1465,11 @@ var Evaluator = class Evaluator {
|
|
|
1277
1465
|
let propsObj = props;
|
|
1278
1466
|
for (let key of Object.keys(propsObj)) {
|
|
1279
1467
|
if (key === "name" || key === "message" || key === "stack") continue;
|
|
1280
|
-
|
|
1468
|
+
if (key in Object.prototype || key === "toJSON") {
|
|
1469
|
+
this.evaluateImpl(propsObj[key], result, key, depth + 1);
|
|
1470
|
+
continue;
|
|
1471
|
+
}
|
|
1472
|
+
anyResult[key] = this.evaluateImpl(propsObj[key], result, key, depth + 1);
|
|
1281
1473
|
}
|
|
1282
1474
|
}
|
|
1283
1475
|
return result;
|
|
@@ -1298,11 +1490,11 @@ var Evaluator = class Evaluator {
|
|
|
1298
1490
|
let init = value[2];
|
|
1299
1491
|
if (typeof init !== "object" || init === null) break;
|
|
1300
1492
|
if (init.body) {
|
|
1301
|
-
init.body = this.evaluateImpl(init.body, init, "body");
|
|
1493
|
+
init.body = this.evaluateImpl(init.body, init, "body", depth + 1);
|
|
1302
1494
|
if (init.body === null || typeof init.body === "string" || init.body instanceof Uint8Array || init.body instanceof ReadableStream) {} else throw new TypeError("Request body must be of type ReadableStream.");
|
|
1303
1495
|
}
|
|
1304
1496
|
if (init.signal) {
|
|
1305
|
-
init.signal = this.evaluateImpl(init.signal, init, "signal");
|
|
1497
|
+
init.signal = this.evaluateImpl(init.signal, init, "signal", depth + 1);
|
|
1306
1498
|
if (!(init.signal instanceof AbortSignal)) throw new TypeError("Request siganl must be of type AbortSignal.");
|
|
1307
1499
|
}
|
|
1308
1500
|
if (init.headers && !(init.headers instanceof Array)) throw new TypeError("Request headers must be serialized as an array of pairs.");
|
|
@@ -1319,7 +1511,7 @@ var Evaluator = class Evaluator {
|
|
|
1319
1511
|
}
|
|
1320
1512
|
case "response": {
|
|
1321
1513
|
if (value.length !== 3) break;
|
|
1322
|
-
let body = this.evaluateImpl(value[1], parent, property);
|
|
1514
|
+
let body = this.evaluateImpl(value[1], parent, property, depth + 1);
|
|
1323
1515
|
if (body === null || typeof body === "string" || body instanceof Uint8Array || body instanceof ReadableStream) {} else throw new TypeError("Response body must be of type ReadableStream.");
|
|
1324
1516
|
let init = value[2];
|
|
1325
1517
|
if (typeof init !== "object" || init === null) break;
|
|
@@ -1330,7 +1522,7 @@ var Evaluator = class Evaluator {
|
|
|
1330
1522
|
case "blob": {
|
|
1331
1523
|
if (value.length !== 3 || typeof value[1] !== "string") break;
|
|
1332
1524
|
let contentType = value[1];
|
|
1333
|
-
let content = this.evaluateImpl(value[2], parent, property);
|
|
1525
|
+
let content = this.evaluateImpl(value[2], parent, property, depth + 1);
|
|
1334
1526
|
if (!(content instanceof ReadableStream)) throw new TypeError("Blob content must be serialized as a ReadableStream.");
|
|
1335
1527
|
let promise = streamToBlobPromise(content, contentType);
|
|
1336
1528
|
this.promises.push({
|
|
@@ -1371,7 +1563,7 @@ var Evaluator = class Evaluator {
|
|
|
1371
1563
|
if (value.length == 3) return addStub(hook.get(path));
|
|
1372
1564
|
let args = value[3];
|
|
1373
1565
|
if (!(args instanceof Array)) break;
|
|
1374
|
-
args = new Evaluator(this.importer).
|
|
1566
|
+
args = new Evaluator(this.importer).evaluateWithDepth([args], depth);
|
|
1375
1567
|
return addStub(hook.call(path, args));
|
|
1376
1568
|
}
|
|
1377
1569
|
case "remap": {
|
|
@@ -1437,9 +1629,9 @@ var Evaluator = class Evaluator {
|
|
|
1437
1629
|
} else if (value instanceof Object) {
|
|
1438
1630
|
let result = value;
|
|
1439
1631
|
for (let key in result) if (key in Object.prototype || key === "toJSON") {
|
|
1440
|
-
this.evaluateImpl(result[key], result, key);
|
|
1632
|
+
this.evaluateImpl(result[key], result, key, depth + 1);
|
|
1441
1633
|
delete result[key];
|
|
1442
|
-
} else result[key] = this.evaluateImpl(result[key], result, key);
|
|
1634
|
+
} else result[key] = this.evaluateImpl(result[key], result, key, depth + 1);
|
|
1443
1635
|
return result;
|
|
1444
1636
|
} else return value;
|
|
1445
1637
|
}
|
|
@@ -1455,6 +1647,47 @@ function deserialize(value) {
|
|
|
1455
1647
|
|
|
1456
1648
|
//#endregion
|
|
1457
1649
|
//#region src/rpc.ts
|
|
1650
|
+
const ESTIMATED_OBJECT_OVERHEAD = 16;
|
|
1651
|
+
const ESTIMATED_ENTRY_OVERHEAD = 8;
|
|
1652
|
+
const ESTIMATED_BINARY_OVERHEAD = 16;
|
|
1653
|
+
const MAX_ESTIMATE_DEPTH = 64;
|
|
1654
|
+
function estimateStringSize(value) {
|
|
1655
|
+
return 2 + value.length * 3;
|
|
1656
|
+
}
|
|
1657
|
+
function estimateEncodedSize(value, seen, depth = 0) {
|
|
1658
|
+
if (depth >= MAX_ESTIMATE_DEPTH) return ESTIMATED_ENTRY_OVERHEAD;
|
|
1659
|
+
switch (typeof value) {
|
|
1660
|
+
case "string": return estimateStringSize(value);
|
|
1661
|
+
case "number": return 16;
|
|
1662
|
+
case "bigint": return 16;
|
|
1663
|
+
case "boolean": return 8;
|
|
1664
|
+
case "undefined": return 16;
|
|
1665
|
+
case "object": {
|
|
1666
|
+
if (value === null) return 8;
|
|
1667
|
+
if (ArrayBuffer.isView(value)) return ESTIMATED_BINARY_OVERHEAD + value.byteLength;
|
|
1668
|
+
if (value instanceof ArrayBuffer) return ESTIMATED_BINARY_OVERHEAD + value.byteLength;
|
|
1669
|
+
if (typeof Blob !== "undefined" && value instanceof Blob) return ESTIMATED_BINARY_OVERHEAD + value.size;
|
|
1670
|
+
if (value instanceof Date) return 16;
|
|
1671
|
+
seen ??= /* @__PURE__ */ new WeakSet();
|
|
1672
|
+
if (seen.has(value)) return ESTIMATED_ENTRY_OVERHEAD;
|
|
1673
|
+
seen.add(value);
|
|
1674
|
+
if (value instanceof Array) {
|
|
1675
|
+
let size = ESTIMATED_OBJECT_OVERHEAD;
|
|
1676
|
+
for (let item of value) size += ESTIMATED_ENTRY_OVERHEAD + estimateEncodedSize(item, seen, depth + 1);
|
|
1677
|
+
return size;
|
|
1678
|
+
}
|
|
1679
|
+
if (value instanceof Error) {
|
|
1680
|
+
let size = ESTIMATED_OBJECT_OVERHEAD + estimateStringSize(value.name) + estimateStringSize(value.message) + estimateStringSize(value.stack ?? "");
|
|
1681
|
+
for (let key of Object.keys(value)) size += ESTIMATED_ENTRY_OVERHEAD + estimateStringSize(key) + estimateEncodedSize(value[key], seen, depth + 1);
|
|
1682
|
+
return size;
|
|
1683
|
+
}
|
|
1684
|
+
let size = ESTIMATED_OBJECT_OVERHEAD;
|
|
1685
|
+
for (let key of Object.keys(value)) size += ESTIMATED_ENTRY_OVERHEAD + estimateStringSize(key) + estimateEncodedSize(value[key], seen, depth + 1);
|
|
1686
|
+
return size;
|
|
1687
|
+
}
|
|
1688
|
+
default: return 16;
|
|
1689
|
+
}
|
|
1690
|
+
}
|
|
1458
1691
|
var ImportTableEntry = class {
|
|
1459
1692
|
session;
|
|
1460
1693
|
importId;
|
|
@@ -1620,9 +1853,24 @@ var RpcSessionImpl = class {
|
|
|
1620
1853
|
onBatchDone;
|
|
1621
1854
|
pullCount = 0;
|
|
1622
1855
|
onBrokenCallbacks = [];
|
|
1856
|
+
encodingLevel;
|
|
1857
|
+
limits;
|
|
1623
1858
|
constructor(transport, mainHook, options) {
|
|
1624
1859
|
this.transport = transport;
|
|
1625
1860
|
this.options = options;
|
|
1861
|
+
let level = "string";
|
|
1862
|
+
if ("encodingLevel" in transport) {
|
|
1863
|
+
let raw = transport.encodingLevel;
|
|
1864
|
+
if (raw !== void 0) {
|
|
1865
|
+
if (raw !== "string" && raw !== "jsonCompatible" && raw !== "jsonCompatibleWithBytes" && raw !== "structuredClonable") throw new TypeError(`Unknown transport encodingLevel: ${String(raw)}`);
|
|
1866
|
+
level = raw;
|
|
1867
|
+
}
|
|
1868
|
+
}
|
|
1869
|
+
this.encodingLevel = level;
|
|
1870
|
+
this.limits = {
|
|
1871
|
+
...DEFAULT_LIMITS,
|
|
1872
|
+
...options.limits
|
|
1873
|
+
};
|
|
1626
1874
|
this.exports.push({
|
|
1627
1875
|
hook: mainHook,
|
|
1628
1876
|
refcount: 1
|
|
@@ -1703,7 +1951,7 @@ var RpcSessionImpl = class {
|
|
|
1703
1951
|
let autoRelease = exp.autoRelease;
|
|
1704
1952
|
++this.pullCount;
|
|
1705
1953
|
exp.pull = resolve().then((payload) => {
|
|
1706
|
-
let value = Devaluator.devaluate(payload.value, void 0, this, payload);
|
|
1954
|
+
let value = Devaluator.devaluate(payload.value, void 0, this, payload, this.encodingLevel);
|
|
1707
1955
|
this.send([
|
|
1708
1956
|
"resolve",
|
|
1709
1957
|
exportId,
|
|
@@ -1714,7 +1962,7 @@ var RpcSessionImpl = class {
|
|
|
1714
1962
|
this.send([
|
|
1715
1963
|
"reject",
|
|
1716
1964
|
exportId,
|
|
1717
|
-
Devaluator.devaluate(error, void 0, this)
|
|
1965
|
+
Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)
|
|
1718
1966
|
]);
|
|
1719
1967
|
if (autoRelease) this.releaseExport(exportId, 1);
|
|
1720
1968
|
}).catch((error) => {
|
|
@@ -1722,7 +1970,7 @@ var RpcSessionImpl = class {
|
|
|
1722
1970
|
this.send([
|
|
1723
1971
|
"reject",
|
|
1724
1972
|
exportId,
|
|
1725
|
-
Devaluator.devaluate(error, void 0, this)
|
|
1973
|
+
Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)
|
|
1726
1974
|
]);
|
|
1727
1975
|
if (autoRelease) this.releaseExport(exportId, 1);
|
|
1728
1976
|
} catch (error2) {
|
|
@@ -1765,6 +2013,9 @@ var RpcSessionImpl = class {
|
|
|
1765
2013
|
entry.pipeReadable = void 0;
|
|
1766
2014
|
return readable;
|
|
1767
2015
|
}
|
|
2016
|
+
getLimits() {
|
|
2017
|
+
return this.limits;
|
|
2018
|
+
}
|
|
1768
2019
|
createPipe(readable, readableHook) {
|
|
1769
2020
|
if (this.abortReason) throw this.abortReason;
|
|
1770
2021
|
this.send(["pipe"]);
|
|
@@ -1778,17 +2029,33 @@ var RpcSessionImpl = class {
|
|
|
1778
2029
|
}
|
|
1779
2030
|
send(msg) {
|
|
1780
2031
|
if (this.abortReason !== void 0) return 0;
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
msgText = JSON.stringify(msg);
|
|
1784
|
-
} catch (err) {
|
|
2032
|
+
if (this.encodingLevel === "string") {
|
|
2033
|
+
let msgText;
|
|
1785
2034
|
try {
|
|
1786
|
-
|
|
1787
|
-
} catch (
|
|
1788
|
-
|
|
2035
|
+
msgText = JSON.stringify(msg);
|
|
2036
|
+
} catch (err) {
|
|
2037
|
+
try {
|
|
2038
|
+
this.abort(err);
|
|
2039
|
+
} catch (err2) {}
|
|
2040
|
+
throw err;
|
|
2041
|
+
}
|
|
2042
|
+
try {
|
|
2043
|
+
let sent = this.transport.send(msgText);
|
|
2044
|
+
if (sent !== void 0 && typeof sent.catch === "function") sent.catch((err) => this.abort(err, false));
|
|
2045
|
+
} catch (err) {
|
|
2046
|
+
queueMicrotask(() => this.abort(err, false));
|
|
2047
|
+
}
|
|
2048
|
+
return msgText.length;
|
|
2049
|
+
} else try {
|
|
2050
|
+
let size = this.transport.send(msg);
|
|
2051
|
+
if (typeof size === "number") return size;
|
|
2052
|
+
let thenable = size;
|
|
2053
|
+
if (thenable && typeof thenable.then === "function") Promise.resolve(thenable).catch((err) => this.abort(err, false));
|
|
2054
|
+
return;
|
|
2055
|
+
} catch (err) {
|
|
2056
|
+
queueMicrotask(() => this.abort(err, false));
|
|
2057
|
+
return;
|
|
1789
2058
|
}
|
|
1790
|
-
this.transport.send(msgText).catch((err) => this.abort(err, false));
|
|
1791
|
-
return msgText.length;
|
|
1792
2059
|
}
|
|
1793
2060
|
sendCall(id, path, args) {
|
|
1794
2061
|
if (this.abortReason) throw this.abortReason;
|
|
@@ -1798,7 +2065,7 @@ var RpcSessionImpl = class {
|
|
|
1798
2065
|
path
|
|
1799
2066
|
];
|
|
1800
2067
|
if (args) {
|
|
1801
|
-
let devalue = Devaluator.devaluate(args.value, void 0, this, args);
|
|
2068
|
+
let devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
|
|
1802
2069
|
value.push(devalue[0]);
|
|
1803
2070
|
}
|
|
1804
2071
|
this.send(["push", value]);
|
|
@@ -1813,9 +2080,11 @@ var RpcSessionImpl = class {
|
|
|
1813
2080
|
id,
|
|
1814
2081
|
path
|
|
1815
2082
|
];
|
|
1816
|
-
let devalue = Devaluator.devaluate(args.value, void 0, this, args);
|
|
2083
|
+
let devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
|
|
1817
2084
|
value.push(devalue[0]);
|
|
1818
|
-
let
|
|
2085
|
+
let msg = ["stream", value];
|
|
2086
|
+
let size = this.send(msg);
|
|
2087
|
+
if (size === void 0) size = estimateEncodedSize(msg);
|
|
1819
2088
|
let importId = this.imports.length;
|
|
1820
2089
|
let entry = new ImportTableEntry(this, importId, true);
|
|
1821
2090
|
entry.remoteRefcount = 0;
|
|
@@ -1871,7 +2140,14 @@ var RpcSessionImpl = class {
|
|
|
1871
2140
|
this.cancelReadLoop?.(error);
|
|
1872
2141
|
this.cancelReadLoop = void 0;
|
|
1873
2142
|
if (trySendAbortMessage) try {
|
|
1874
|
-
|
|
2143
|
+
let abortMsg = ["abort", Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)];
|
|
2144
|
+
if (this.encodingLevel === "string") {
|
|
2145
|
+
let sent = this.transport.send(JSON.stringify(abortMsg));
|
|
2146
|
+
if (sent !== void 0 && typeof sent.catch === "function") sent.catch((err) => {});
|
|
2147
|
+
} else {
|
|
2148
|
+
let result = this.transport.send(abortMsg);
|
|
2149
|
+
if (result && typeof result.then === "function") Promise.resolve(result).catch((err) => {});
|
|
2150
|
+
}
|
|
1875
2151
|
} catch (err) {}
|
|
1876
2152
|
if (error === void 0) error = "undefined";
|
|
1877
2153
|
this.abortReason = error;
|
|
@@ -1893,18 +2169,19 @@ var RpcSessionImpl = class {
|
|
|
1893
2169
|
while (!this.abortReason) {
|
|
1894
2170
|
let readCanceled = Promise.withResolvers();
|
|
1895
2171
|
this.cancelReadLoop = readCanceled.reject;
|
|
1896
|
-
let
|
|
2172
|
+
let raw;
|
|
1897
2173
|
try {
|
|
1898
|
-
|
|
2174
|
+
raw = await Promise.race([this.transport.receive(), readCanceled.promise]);
|
|
1899
2175
|
} finally {
|
|
1900
2176
|
if (this.cancelReadLoop === readCanceled.reject) this.cancelReadLoop = void 0;
|
|
1901
2177
|
}
|
|
1902
|
-
|
|
2178
|
+
if (this.encodingLevel === "string" && raw.length > this.limits.maxMessageSize) throw new TypeError(`Incoming message exceeds maximum size of ${this.limits.maxMessageSize} UTF-16 code units.`);
|
|
1903
2179
|
if (this.abortReason) break;
|
|
2180
|
+
let msg = this.encodingLevel === "string" ? JSON.parse(raw) : raw;
|
|
1904
2181
|
if (msg instanceof Array) switch (msg[0]) {
|
|
1905
2182
|
case "push":
|
|
1906
2183
|
if (msg.length > 1) {
|
|
1907
|
-
let hook = new PayloadStubHook(new Evaluator(this).evaluate(msg[1]));
|
|
2184
|
+
let hook = new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[1]));
|
|
1908
2185
|
hook.ignoreUnhandledRejections();
|
|
1909
2186
|
this.exports.push({
|
|
1910
2187
|
hook,
|
|
@@ -1915,7 +2192,7 @@ var RpcSessionImpl = class {
|
|
|
1915
2192
|
break;
|
|
1916
2193
|
case "stream":
|
|
1917
2194
|
if (msg.length > 1) {
|
|
1918
|
-
let hook = new PayloadStubHook(new Evaluator(this).evaluate(msg[1]));
|
|
2195
|
+
let hook = new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[1]));
|
|
1919
2196
|
hook.ignoreUnhandledRejections();
|
|
1920
2197
|
let exportId = this.exports.length;
|
|
1921
2198
|
this.exports.push({
|
|
@@ -1950,13 +2227,13 @@ var RpcSessionImpl = class {
|
|
|
1950
2227
|
let importId = msg[1];
|
|
1951
2228
|
if (typeof importId == "number" && msg.length > 2) {
|
|
1952
2229
|
let imp = this.imports[importId];
|
|
1953
|
-
if (imp) if (msg[0] == "resolve") imp.resolve(new PayloadStubHook(new Evaluator(this).evaluate(msg[2])));
|
|
2230
|
+
if (imp) if (msg[0] == "resolve") imp.resolve(new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[2])));
|
|
1954
2231
|
else {
|
|
1955
|
-
let payload = new Evaluator(this).evaluate(msg[2]);
|
|
2232
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[2]);
|
|
1956
2233
|
payload.dispose();
|
|
1957
2234
|
imp.resolve(new ErrorStubHook(payload.value));
|
|
1958
2235
|
}
|
|
1959
|
-
else if (msg[0] == "resolve") new Evaluator(this).evaluate(msg[2]).dispose();
|
|
2236
|
+
else if (msg[0] == "resolve") new Evaluator(this, this.encodingLevel).evaluate(msg[2]).dispose();
|
|
1960
2237
|
continue;
|
|
1961
2238
|
}
|
|
1962
2239
|
break;
|
|
@@ -1971,9 +2248,9 @@ var RpcSessionImpl = class {
|
|
|
1971
2248
|
break;
|
|
1972
2249
|
}
|
|
1973
2250
|
case "abort": {
|
|
1974
|
-
let payload = new Evaluator(this).evaluate(msg[1]);
|
|
2251
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[1]);
|
|
1975
2252
|
payload.dispose();
|
|
1976
|
-
this.abort(payload, false);
|
|
2253
|
+
this.abort(payload.value, false);
|
|
1977
2254
|
break;
|
|
1978
2255
|
}
|
|
1979
2256
|
}
|
|
@@ -2024,6 +2301,8 @@ var RpcSession$1 = class {
|
|
|
2024
2301
|
|
|
2025
2302
|
//#endregion
|
|
2026
2303
|
//#region src/websocket.ts
|
|
2304
|
+
/** Close-frame reason max UTF-8 bytes: 125 payload − 2-byte status (RFC 6455 §5.5). */
|
|
2305
|
+
const MAX_CLOSE_REASON_BYTES = 123;
|
|
2027
2306
|
function newWebSocketRpcSession$1(webSocket, localMain, options) {
|
|
2028
2307
|
if (typeof webSocket === "string") webSocket = new WebSocket(webSocket);
|
|
2029
2308
|
return new RpcSession$1(new WebSocketTransport(webSocket), localMain, options).getRemoteMain();
|
|
@@ -2043,9 +2322,14 @@ function newWorkersWebSocketRpcResponse(request, localMain, options) {
|
|
|
2043
2322
|
webSocket: pair[1]
|
|
2044
2323
|
});
|
|
2045
2324
|
}
|
|
2325
|
+
/**
|
|
2326
|
+
* Generic WebSocket transport. Default `T = string` is backward-compatible and satisfies
|
|
2327
|
+
* `RpcTransport`. Use `T = ArrayBuffer` as a building block for binary transports.
|
|
2328
|
+
*/
|
|
2046
2329
|
var WebSocketTransport = class {
|
|
2047
2330
|
constructor(webSocket) {
|
|
2048
2331
|
this.#webSocket = webSocket;
|
|
2332
|
+
webSocket.binaryType = "arraybuffer";
|
|
2049
2333
|
if (webSocket.readyState === WebSocket.CONNECTING) {
|
|
2050
2334
|
this.#sendQueue = [];
|
|
2051
2335
|
webSocket.addEventListener("open", (event) => {
|
|
@@ -2058,12 +2342,12 @@ var WebSocketTransport = class {
|
|
|
2058
2342
|
});
|
|
2059
2343
|
}
|
|
2060
2344
|
webSocket.addEventListener("message", (event) => {
|
|
2061
|
-
if (this.#error) {} else if (typeof event.data === "string") if (this.#receiveResolver) {
|
|
2345
|
+
if (this.#error) {} else if (typeof event.data === "string" || event.data instanceof ArrayBuffer) if (this.#receiveResolver) {
|
|
2062
2346
|
this.#receiveResolver(event.data);
|
|
2063
2347
|
this.#receiveResolver = void 0;
|
|
2064
2348
|
this.#receiveRejecter = void 0;
|
|
2065
2349
|
} else this.#receiveQueue.push(event.data);
|
|
2066
|
-
else this.#receivedError(/* @__PURE__ */ new TypeError("Received
|
|
2350
|
+
else this.#receivedError(/* @__PURE__ */ new TypeError("Received unexpected message type from WebSocket."));
|
|
2067
2351
|
});
|
|
2068
2352
|
webSocket.addEventListener("close", (event) => {
|
|
2069
2353
|
this.#receivedError(/* @__PURE__ */ new Error(`Peer closed WebSocket: ${event.code} ${event.reason}`));
|
|
@@ -2078,13 +2362,13 @@ var WebSocketTransport = class {
|
|
|
2078
2362
|
#receiveRejecter;
|
|
2079
2363
|
#receiveQueue = [];
|
|
2080
2364
|
#error;
|
|
2081
|
-
|
|
2365
|
+
send(message) {
|
|
2082
2366
|
if (this.#sendQueue === void 0) this.#webSocket.send(message);
|
|
2083
2367
|
else this.#sendQueue.push(message);
|
|
2084
2368
|
}
|
|
2085
|
-
|
|
2086
|
-
if (this.#receiveQueue.length > 0) return this.#receiveQueue.shift();
|
|
2087
|
-
else if (this.#error)
|
|
2369
|
+
receive() {
|
|
2370
|
+
if (this.#receiveQueue.length > 0) return Promise.resolve(this.#receiveQueue.shift());
|
|
2371
|
+
else if (this.#error) return Promise.reject(this.#error);
|
|
2088
2372
|
else return new Promise((resolve, reject) => {
|
|
2089
2373
|
this.#receiveResolver = resolve;
|
|
2090
2374
|
this.#receiveRejecter = reject;
|
|
@@ -2094,6 +2378,8 @@ var WebSocketTransport = class {
|
|
|
2094
2378
|
let message;
|
|
2095
2379
|
if (reason instanceof Error) message = reason.message;
|
|
2096
2380
|
else message = `${reason}`;
|
|
2381
|
+
let reasonBytes = new TextEncoder().encode(message);
|
|
2382
|
+
if (reasonBytes.length > 123) message = new TextDecoder().decode(reasonBytes.subarray(0, 123), { stream: true });
|
|
2097
2383
|
this.#webSocket.close(3e3, message);
|
|
2098
2384
|
if (!this.#error) this.#error = reason;
|
|
2099
2385
|
}
|
|
@@ -2111,6 +2397,7 @@ var WebSocketTransport = class {
|
|
|
2111
2397
|
|
|
2112
2398
|
//#endregion
|
|
2113
2399
|
//#region src/batch.ts
|
|
2400
|
+
const yieldToMacrotask = typeof setImmediate === "function" ? () => new Promise((resolve) => setImmediate(resolve)) : () => new Promise((resolve) => setTimeout(resolve, 0));
|
|
2114
2401
|
var BatchClientTransport = class {
|
|
2115
2402
|
constructor(sendBatch) {
|
|
2116
2403
|
this.#promise = this.#scheduleBatch(sendBatch);
|
|
@@ -2119,7 +2406,7 @@ var BatchClientTransport = class {
|
|
|
2119
2406
|
#aborted;
|
|
2120
2407
|
#batchToSend = [];
|
|
2121
2408
|
#batchToReceive = null;
|
|
2122
|
-
|
|
2409
|
+
send(message) {
|
|
2123
2410
|
if (this.#batchToSend !== null) this.#batchToSend.push(message);
|
|
2124
2411
|
}
|
|
2125
2412
|
async receive() {
|
|
@@ -2132,7 +2419,7 @@ var BatchClientTransport = class {
|
|
|
2132
2419
|
this.#aborted = reason;
|
|
2133
2420
|
}
|
|
2134
2421
|
async #scheduleBatch(sendBatch) {
|
|
2135
|
-
await
|
|
2422
|
+
await yieldToMacrotask();
|
|
2136
2423
|
if (this.#aborted !== void 0) throw this.#aborted;
|
|
2137
2424
|
let batch = this.#batchToSend;
|
|
2138
2425
|
this.#batchToSend = null;
|
|
@@ -2161,7 +2448,7 @@ var BatchServerTransport = class {
|
|
|
2161
2448
|
#batchToSend = [];
|
|
2162
2449
|
#batchToReceive;
|
|
2163
2450
|
#allReceived = Promise.withResolvers();
|
|
2164
|
-
|
|
2451
|
+
send(message) {
|
|
2165
2452
|
this.#batchToSend.push(message);
|
|
2166
2453
|
}
|
|
2167
2454
|
async receive() {
|
|
@@ -2210,7 +2497,11 @@ async function newHttpBatchRpcResponse(request, localMain, options) {
|
|
|
2210
2497
|
* @param options Optional RPC session options. You can also pass headers to set on the response.
|
|
2211
2498
|
*/
|
|
2212
2499
|
async function nodeHttpBatchRpcResponse(request, response, localMain, options) {
|
|
2213
|
-
if (request.method !== "POST")
|
|
2500
|
+
if (request.method !== "POST") {
|
|
2501
|
+
response.writeHead(405, "This endpoint only accepts POST requests.", options?.headers);
|
|
2502
|
+
response.end();
|
|
2503
|
+
return;
|
|
2504
|
+
}
|
|
2214
2505
|
let body = await new Promise((resolve, reject) => {
|
|
2215
2506
|
let chunks = [];
|
|
2216
2507
|
request.on("data", (chunk) => {
|
|
@@ -2235,17 +2526,17 @@ function newMessagePortRpcSession$1(port, localMain, options) {
|
|
|
2235
2526
|
return new RpcSession$1(new MessagePortTransport(port), localMain, options).getRemoteMain();
|
|
2236
2527
|
}
|
|
2237
2528
|
var MessagePortTransport = class {
|
|
2529
|
+
encodingLevel = "structuredClonable";
|
|
2238
2530
|
constructor(port) {
|
|
2239
2531
|
this.#port = port;
|
|
2240
2532
|
port.start();
|
|
2241
2533
|
port.addEventListener("message", (event) => {
|
|
2242
2534
|
if (this.#error) {} else if (event.data === null) this.#receivedError(/* @__PURE__ */ new Error("Peer closed MessagePort connection."));
|
|
2243
|
-
else if (
|
|
2535
|
+
else if (this.#receiveResolver) {
|
|
2244
2536
|
this.#receiveResolver(event.data);
|
|
2245
2537
|
this.#receiveResolver = void 0;
|
|
2246
2538
|
this.#receiveRejecter = void 0;
|
|
2247
2539
|
} else this.#receiveQueue.push(event.data);
|
|
2248
|
-
else this.#receivedError(/* @__PURE__ */ new TypeError("Received non-string message from MessagePort."));
|
|
2249
2540
|
});
|
|
2250
2541
|
port.addEventListener("messageerror", (event) => {
|
|
2251
2542
|
this.#receivedError(/* @__PURE__ */ new Error("MessagePort message error."));
|
|
@@ -2256,7 +2547,7 @@ var MessagePortTransport = class {
|
|
|
2256
2547
|
#receiveRejecter;
|
|
2257
2548
|
#receiveQueue = [];
|
|
2258
2549
|
#error;
|
|
2259
|
-
|
|
2550
|
+
send(message) {
|
|
2260
2551
|
if (this.#error) throw this.#error;
|
|
2261
2552
|
this.#port.postMessage(message);
|
|
2262
2553
|
}
|
|
@@ -2475,6 +2766,9 @@ var MapApplicator = class {
|
|
|
2475
2766
|
getPipeReadable(exportId) {
|
|
2476
2767
|
throw new Error("A mapper function cannot use pipe readables.");
|
|
2477
2768
|
}
|
|
2769
|
+
getLimits() {
|
|
2770
|
+
return DEFAULT_LIMITS;
|
|
2771
|
+
}
|
|
2478
2772
|
};
|
|
2479
2773
|
function applyMapToElement(input, parent, owner, captures, instructions) {
|
|
2480
2774
|
let mapper = new MapApplicator(captures, new PayloadStubHook(RpcPayload.deepCopyFrom(input, parent, owner)));
|
|
@@ -2535,8 +2829,7 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
|
|
|
2535
2829
|
throw new Error(`Unknown WritableStream method: ${method}`);
|
|
2536
2830
|
}
|
|
2537
2831
|
if (method === "close" || method === "abort") state.closed = true;
|
|
2538
|
-
|
|
2539
|
-
return new PromiseStubHook(args.deliverCall(func, state.writer).then((payload) => new PayloadStubHook(payload)));
|
|
2832
|
+
return new PromiseStubHook((method === "write" ? args.deliverStreamWrite(state.writer) : args.deliverCall(state.writer[method], state.writer)).then((payload) => new PayloadStubHook(payload)));
|
|
2540
2833
|
} catch (err) {
|
|
2541
2834
|
return new ErrorStubHook(err);
|
|
2542
2835
|
}
|
|
@@ -2813,20 +3106,23 @@ let newMessagePortRpcSession = newMessagePortRpcSession$1;
|
|
|
2813
3106
|
* credentials as parameters and returns the authorized API), then cross-origin requests should
|
|
2814
3107
|
* be safe.
|
|
2815
3108
|
*/
|
|
2816
|
-
async function newWorkersRpcResponse(request, localMain) {
|
|
3109
|
+
async function newWorkersRpcResponse(request, localMain, options) {
|
|
2817
3110
|
if (request.method === "POST") {
|
|
2818
|
-
let response = await newHttpBatchRpcResponse(request, localMain);
|
|
3111
|
+
let response = await newHttpBatchRpcResponse(request, localMain, options);
|
|
2819
3112
|
response.headers.set("Access-Control-Allow-Origin", "*");
|
|
2820
3113
|
return response;
|
|
2821
|
-
} else if (request.headers.get("Upgrade")?.toLowerCase() === "websocket") return newWorkersWebSocketRpcResponse(request, localMain);
|
|
3114
|
+
} else if (request.headers.get("Upgrade")?.toLowerCase() === "websocket") return newWorkersWebSocketRpcResponse(request, localMain, options);
|
|
2822
3115
|
else return new Response("This endpoint only accepts POST or WebSocket requests.", { status: 400 });
|
|
2823
3116
|
}
|
|
2824
3117
|
|
|
2825
3118
|
//#endregion
|
|
3119
|
+
exports.DEFAULT_LIMITS = DEFAULT_LIMITS;
|
|
3120
|
+
exports.DEFAULT_MAX_DEPTH = DEFAULT_MAX_DEPTH;
|
|
2826
3121
|
exports.RpcPromise = RpcPromise;
|
|
2827
3122
|
exports.RpcSession = RpcSession;
|
|
2828
3123
|
exports.RpcStub = RpcStub;
|
|
2829
3124
|
exports.RpcTarget = RpcTarget;
|
|
3125
|
+
exports.WebSocketTransport = WebSocketTransport;
|
|
2830
3126
|
exports.deserialize = deserialize;
|
|
2831
3127
|
exports.newHttpBatchRpcResponse = newHttpBatchRpcResponse;
|
|
2832
3128
|
exports.newHttpBatchRpcSession = newHttpBatchRpcSession;
|