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-workers.cjs
CHANGED
|
@@ -76,7 +76,19 @@ function typeForRpc(value) {
|
|
|
76
76
|
case Array.prototype: return "array";
|
|
77
77
|
case Date.prototype: return "date";
|
|
78
78
|
case Uint8Array.prototype:
|
|
79
|
-
case BUFFER_PROTOTYPE:
|
|
79
|
+
case BUFFER_PROTOTYPE:
|
|
80
|
+
case ArrayBuffer.prototype:
|
|
81
|
+
case DataView.prototype:
|
|
82
|
+
case Int8Array.prototype:
|
|
83
|
+
case Uint8ClampedArray.prototype:
|
|
84
|
+
case Int16Array.prototype:
|
|
85
|
+
case Uint16Array.prototype:
|
|
86
|
+
case Int32Array.prototype:
|
|
87
|
+
case Uint32Array.prototype:
|
|
88
|
+
case BigInt64Array.prototype:
|
|
89
|
+
case BigUint64Array.prototype:
|
|
90
|
+
case Float32Array.prototype:
|
|
91
|
+
case Float64Array.prototype: return "bytes";
|
|
80
92
|
case WritableStream.prototype: return "writable";
|
|
81
93
|
case ReadableStream.prototype: return "readable";
|
|
82
94
|
case Headers.prototype: return "headers";
|
|
@@ -546,6 +558,30 @@ var RpcPayload = class RpcPayload {
|
|
|
546
558
|
this.dispose();
|
|
547
559
|
}
|
|
548
560
|
}
|
|
561
|
+
async deliverStreamWrite(writer) {
|
|
562
|
+
try {
|
|
563
|
+
let promises = [];
|
|
564
|
+
this.deliverTo(this, "value", promises);
|
|
565
|
+
if (promises.length > 0) await Promise.all(promises);
|
|
566
|
+
let chunk = this.value[0];
|
|
567
|
+
if ((this.hooks.length > 0 || this.promises.length > 0) && chunk instanceof Object) {
|
|
568
|
+
if (!(Symbol.dispose in chunk)) Object.defineProperty(chunk, Symbol.dispose, {
|
|
569
|
+
value: () => this.dispose(),
|
|
570
|
+
writable: true,
|
|
571
|
+
enumerable: false,
|
|
572
|
+
configurable: true
|
|
573
|
+
});
|
|
574
|
+
await writer.write(chunk);
|
|
575
|
+
return RpcPayload.fromAppReturn(void 0);
|
|
576
|
+
}
|
|
577
|
+
await writer.write(chunk);
|
|
578
|
+
this.dispose();
|
|
579
|
+
return RpcPayload.fromAppReturn(void 0);
|
|
580
|
+
} catch (err) {
|
|
581
|
+
this.dispose();
|
|
582
|
+
throw err;
|
|
583
|
+
}
|
|
584
|
+
}
|
|
549
585
|
async deliverResolve() {
|
|
550
586
|
try {
|
|
551
587
|
let promises = [];
|
|
@@ -964,6 +1000,77 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
|
964
1000
|
|
|
965
1001
|
//#endregion
|
|
966
1002
|
//#region src/serialize.ts
|
|
1003
|
+
const DEFAULT_MAX_DEPTH = 256;
|
|
1004
|
+
const DEFAULT_LIMITS = {
|
|
1005
|
+
maxBigIntDigits: 16384,
|
|
1006
|
+
maxDepth: 256,
|
|
1007
|
+
maxMessageSize: 32 * 1024 * 1024
|
|
1008
|
+
};
|
|
1009
|
+
const NATIVE_LITTLE_ENDIAN = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
|
|
1010
|
+
const BYTE_CONTAINER_TYPE_NAMES = [
|
|
1011
|
+
"ArrayBuffer",
|
|
1012
|
+
"DataView",
|
|
1013
|
+
"Int8Array",
|
|
1014
|
+
"Uint8Array",
|
|
1015
|
+
"Uint8ClampedArray",
|
|
1016
|
+
"Int16Array",
|
|
1017
|
+
"Uint16Array",
|
|
1018
|
+
"Int32Array",
|
|
1019
|
+
"Uint32Array",
|
|
1020
|
+
"BigInt64Array",
|
|
1021
|
+
"BigUint64Array",
|
|
1022
|
+
"Float32Array",
|
|
1023
|
+
"Float64Array"
|
|
1024
|
+
];
|
|
1025
|
+
function isValidByteContainerName(value) {
|
|
1026
|
+
return BYTE_CONTAINER_TYPE_NAMES.includes(value);
|
|
1027
|
+
}
|
|
1028
|
+
const TYPED_ARRAY_ELEMENT_SIZE = {
|
|
1029
|
+
ArrayBuffer: void 0,
|
|
1030
|
+
DataView: void 0,
|
|
1031
|
+
Int8Array: void 0,
|
|
1032
|
+
Uint8Array: void 0,
|
|
1033
|
+
Uint8ClampedArray: void 0,
|
|
1034
|
+
Int16Array: 2,
|
|
1035
|
+
Uint16Array: 2,
|
|
1036
|
+
Int32Array: 4,
|
|
1037
|
+
Uint32Array: 4,
|
|
1038
|
+
BigInt64Array: 8,
|
|
1039
|
+
BigUint64Array: 8,
|
|
1040
|
+
Float32Array: 4,
|
|
1041
|
+
Float64Array: 8
|
|
1042
|
+
};
|
|
1043
|
+
const BYTE_CONTAINER_PROTOTYPES = {
|
|
1044
|
+
ArrayBuffer: ArrayBuffer.prototype,
|
|
1045
|
+
DataView: DataView.prototype,
|
|
1046
|
+
Int8Array: Int8Array.prototype,
|
|
1047
|
+
Uint8ClampedArray: Uint8ClampedArray.prototype,
|
|
1048
|
+
Int16Array: Int16Array.prototype,
|
|
1049
|
+
Uint16Array: Uint16Array.prototype,
|
|
1050
|
+
Int32Array: Int32Array.prototype,
|
|
1051
|
+
Uint32Array: Uint32Array.prototype,
|
|
1052
|
+
BigInt64Array: BigInt64Array.prototype,
|
|
1053
|
+
BigUint64Array: BigUint64Array.prototype,
|
|
1054
|
+
Float32Array: Float32Array.prototype,
|
|
1055
|
+
Float64Array: Float64Array.prototype
|
|
1056
|
+
};
|
|
1057
|
+
const BYTE_CONTAINER_TYPE_BY_PROTOTYPE = /* @__PURE__ */ new Map();
|
|
1058
|
+
for (let type of Object.keys(BYTE_CONTAINER_PROTOTYPES)) BYTE_CONTAINER_TYPE_BY_PROTOTYPE.set(BYTE_CONTAINER_PROTOTYPES[type], type);
|
|
1059
|
+
function swapByteOrder(bytes, elementSize) {
|
|
1060
|
+
if (elementSize !== 2 && elementSize !== 4 && elementSize !== 8) throw new RangeError(`Unsupported element size: ${elementSize}`);
|
|
1061
|
+
let view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
1062
|
+
for (let offset = 0; offset < bytes.byteLength; offset += elementSize) switch (elementSize) {
|
|
1063
|
+
case 2:
|
|
1064
|
+
view.setUint16(offset, view.getUint16(offset, false), true);
|
|
1065
|
+
break;
|
|
1066
|
+
case 4:
|
|
1067
|
+
view.setUint32(offset, view.getUint32(offset, false), true);
|
|
1068
|
+
break;
|
|
1069
|
+
case 8:
|
|
1070
|
+
view.setBigUint64(offset, view.getBigUint64(offset, false), true);
|
|
1071
|
+
break;
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
967
1074
|
var NullExporter = class {
|
|
968
1075
|
exportStub(stub) {
|
|
969
1076
|
throw new Error("Cannot serialize RPC stubs without an RPC session.");
|
|
@@ -984,6 +1091,7 @@ async function streamToBlob(stream, type) {
|
|
|
984
1091
|
return b.type === type ? b : b.slice(0, b.size, type);
|
|
985
1092
|
}
|
|
986
1093
|
const ERROR_TYPES = {
|
|
1094
|
+
__proto__: null,
|
|
987
1095
|
Error,
|
|
988
1096
|
EvalError,
|
|
989
1097
|
RangeError,
|
|
@@ -996,12 +1104,14 @@ const ERROR_TYPES = {
|
|
|
996
1104
|
var Devaluator = class Devaluator {
|
|
997
1105
|
exporter;
|
|
998
1106
|
source;
|
|
999
|
-
|
|
1107
|
+
encodingLevel;
|
|
1108
|
+
constructor(exporter, source, encodingLevel) {
|
|
1000
1109
|
this.exporter = exporter;
|
|
1001
1110
|
this.source = source;
|
|
1111
|
+
this.encodingLevel = encodingLevel;
|
|
1002
1112
|
}
|
|
1003
|
-
static devaluate(value, parent, exporter = NULL_EXPORTER, source) {
|
|
1004
|
-
let devaluator = new Devaluator(exporter, source);
|
|
1113
|
+
static devaluate(value, parent, exporter = NULL_EXPORTER, source, encodingLevel = "string") {
|
|
1114
|
+
let devaluator = new Devaluator(exporter, source, encodingLevel);
|
|
1005
1115
|
try {
|
|
1006
1116
|
return devaluator.devaluateImpl(value, parent, 0);
|
|
1007
1117
|
} catch (err) {
|
|
@@ -1013,7 +1123,7 @@ var Devaluator = class Devaluator {
|
|
|
1013
1123
|
}
|
|
1014
1124
|
exports;
|
|
1015
1125
|
devaluateImpl(value, parent, depth) {
|
|
1016
|
-
if (depth >=
|
|
1126
|
+
if (depth >= 256) throw new Error("Serialization exceeded maximum allowed depth. (Does the message contain cycles?)");
|
|
1017
1127
|
switch (typeForRpc(value)) {
|
|
1018
1128
|
case "unsupported": {
|
|
1019
1129
|
let msg;
|
|
@@ -1024,10 +1134,12 @@ var Devaluator = class Devaluator {
|
|
|
1024
1134
|
}
|
|
1025
1135
|
throw new TypeError(msg);
|
|
1026
1136
|
}
|
|
1027
|
-
case "primitive": if (typeof value === "number" && !isFinite(value))
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1137
|
+
case "primitive": if (typeof value === "number" && !isFinite(value)) {
|
|
1138
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1139
|
+
if (value === Infinity) return ["inf"];
|
|
1140
|
+
else if (value === -Infinity) return ["-inf"];
|
|
1141
|
+
else return ["nan"];
|
|
1142
|
+
} else return value;
|
|
1031
1143
|
case "object": {
|
|
1032
1144
|
let object = value;
|
|
1033
1145
|
let result = {};
|
|
@@ -1041,22 +1153,47 @@ var Devaluator = class Devaluator {
|
|
|
1041
1153
|
for (let i = 0; i < len; i++) result[i] = this.devaluateImpl(array[i], array, depth + 1);
|
|
1042
1154
|
return [result];
|
|
1043
1155
|
}
|
|
1044
|
-
case "bigint":
|
|
1156
|
+
case "bigint":
|
|
1157
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1158
|
+
return ["bigint", value.toString()];
|
|
1045
1159
|
case "date": {
|
|
1160
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1046
1161
|
const time = value.getTime();
|
|
1047
1162
|
return ["date", Number.isNaN(time) ? null : time];
|
|
1048
1163
|
}
|
|
1049
1164
|
case "bytes": {
|
|
1050
|
-
let
|
|
1051
|
-
|
|
1165
|
+
let alternateTypeName = BYTE_CONTAINER_TYPE_BY_PROTOTYPE.get(Object.getPrototypeOf(value));
|
|
1166
|
+
let bytes;
|
|
1167
|
+
if (alternateTypeName === "ArrayBuffer") bytes = new Uint8Array(value);
|
|
1168
|
+
else if (alternateTypeName === void 0) bytes = value;
|
|
1169
|
+
else {
|
|
1170
|
+
let view = value;
|
|
1171
|
+
bytes = new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
|
|
1172
|
+
let elementSize = TYPED_ARRAY_ELEMENT_SIZE[alternateTypeName];
|
|
1173
|
+
if (!NATIVE_LITTLE_ENDIAN && elementSize) {
|
|
1174
|
+
bytes = bytes.slice();
|
|
1175
|
+
swapByteOrder(bytes, elementSize);
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
if (this.encodingLevel === "structuredClonable" || this.encodingLevel === "jsonCompatibleWithBytes") return alternateTypeName === void 0 ? ["bytes", bytes] : [
|
|
1179
|
+
"bytes",
|
|
1180
|
+
bytes,
|
|
1181
|
+
alternateTypeName
|
|
1182
|
+
];
|
|
1052
1183
|
let b64;
|
|
1053
|
-
if (
|
|
1184
|
+
if (bytes.toBase64) b64 = bytes.toBase64({ omitPadding: true });
|
|
1185
|
+
else if (typeof Buffer !== "undefined") b64 = (bytes instanceof Buffer ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)).toString("base64");
|
|
1054
1186
|
else {
|
|
1055
1187
|
let binary = "";
|
|
1056
1188
|
for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]);
|
|
1057
1189
|
b64 = btoa(binary);
|
|
1058
1190
|
}
|
|
1059
|
-
|
|
1191
|
+
b64 = b64.replace(/=+$/, "");
|
|
1192
|
+
return alternateTypeName === void 0 ? ["bytes", b64] : [
|
|
1193
|
+
"bytes",
|
|
1194
|
+
b64,
|
|
1195
|
+
alternateTypeName
|
|
1196
|
+
];
|
|
1060
1197
|
}
|
|
1061
1198
|
case "headers": return ["headers", [...value]];
|
|
1062
1199
|
case "request": {
|
|
@@ -1172,7 +1309,9 @@ var Devaluator = class Devaluator {
|
|
|
1172
1309
|
} else if (rewritten && rewritten.stack) result.push(rewritten.stack);
|
|
1173
1310
|
return result;
|
|
1174
1311
|
}
|
|
1175
|
-
case "undefined":
|
|
1312
|
+
case "undefined":
|
|
1313
|
+
if (this.encodingLevel === "structuredClonable") return;
|
|
1314
|
+
return ["undefined"];
|
|
1176
1315
|
case "stub":
|
|
1177
1316
|
case "rpc-promise": {
|
|
1178
1317
|
if (!this.source) throw new Error("Can't serialize RPC stubs in this context.");
|
|
@@ -1239,6 +1378,9 @@ var NullImporter = class {
|
|
|
1239
1378
|
getPipeReadable(exportId) {
|
|
1240
1379
|
throw new Error("Cannot retrieve pipe readable without an RPC session.");
|
|
1241
1380
|
}
|
|
1381
|
+
getLimits() {
|
|
1382
|
+
return DEFAULT_LIMITS;
|
|
1383
|
+
}
|
|
1242
1384
|
};
|
|
1243
1385
|
const NULL_IMPORTER = new NullImporter();
|
|
1244
1386
|
function fixBrokenRequestBody(request, body) {
|
|
@@ -1255,15 +1397,22 @@ function streamToBlobPromise(stream, type) {
|
|
|
1255
1397
|
}
|
|
1256
1398
|
var Evaluator = class Evaluator {
|
|
1257
1399
|
importer;
|
|
1258
|
-
|
|
1400
|
+
encodingLevel;
|
|
1401
|
+
limits;
|
|
1402
|
+
constructor(importer, encodingLevel = "string") {
|
|
1259
1403
|
this.importer = importer;
|
|
1404
|
+
this.encodingLevel = encodingLevel;
|
|
1405
|
+
this.limits = importer.getLimits();
|
|
1260
1406
|
}
|
|
1261
1407
|
hooks = [];
|
|
1262
1408
|
promises = [];
|
|
1263
1409
|
evaluate(value) {
|
|
1410
|
+
return this.evaluateWithDepth(value, 0);
|
|
1411
|
+
}
|
|
1412
|
+
evaluateWithDepth(value, depth) {
|
|
1264
1413
|
let payload = RpcPayload.forEvaluate(this.hooks, this.promises);
|
|
1265
1414
|
try {
|
|
1266
|
-
payload.value = this.evaluateImpl(value, payload, "value");
|
|
1415
|
+
payload.value = this.evaluateImpl(value, payload, "value", depth);
|
|
1267
1416
|
return payload;
|
|
1268
1417
|
} catch (err) {
|
|
1269
1418
|
payload.dispose();
|
|
@@ -1273,31 +1422,70 @@ var Evaluator = class Evaluator {
|
|
|
1273
1422
|
evaluateCopy(value) {
|
|
1274
1423
|
return this.evaluate(structuredClone(value));
|
|
1275
1424
|
}
|
|
1276
|
-
evaluateImpl(value, parent, property) {
|
|
1425
|
+
evaluateImpl(value, parent, property, depth) {
|
|
1426
|
+
let maxDepth = this.limits.maxDepth;
|
|
1427
|
+
if (depth >= maxDepth) throw new TypeError(`Deserialization exceeded maximum allowed message depth of ${maxDepth}.`);
|
|
1428
|
+
if (this.encodingLevel === "structuredClonable") {
|
|
1429
|
+
if (value instanceof Date || typeof value === "bigint") return value;
|
|
1430
|
+
}
|
|
1277
1431
|
if (value instanceof Array) {
|
|
1278
1432
|
if (value.length == 1 && value[0] instanceof Array) {
|
|
1279
1433
|
let result = value[0];
|
|
1280
|
-
for (let i = 0; i < result.length; i++) result[i] = this.evaluateImpl(result[i], result, i);
|
|
1434
|
+
for (let i = 0; i < result.length; i++) result[i] = this.evaluateImpl(result[i], result, i, depth + 1);
|
|
1281
1435
|
return result;
|
|
1282
1436
|
} else switch (value[0]) {
|
|
1283
1437
|
case "bigint":
|
|
1284
|
-
if (typeof value[1] == "string")
|
|
1438
|
+
if (typeof value[1] == "string") {
|
|
1439
|
+
let digits = value[1];
|
|
1440
|
+
let maxBigIntDigits = this.limits.maxBigIntDigits;
|
|
1441
|
+
if (digits.length > maxBigIntDigits) throw new TypeError(`Deserialized bigint exceeds maximum length of ${maxBigIntDigits} digits.`);
|
|
1442
|
+
return BigInt(digits);
|
|
1443
|
+
}
|
|
1285
1444
|
break;
|
|
1286
1445
|
case "date":
|
|
1287
1446
|
if (value[1] === null) return /* @__PURE__ */ new Date(NaN);
|
|
1288
1447
|
if (typeof value[1] == "number") return new Date(value[1]);
|
|
1289
1448
|
break;
|
|
1290
|
-
case "bytes":
|
|
1291
|
-
|
|
1292
|
-
|
|
1449
|
+
case "bytes": {
|
|
1450
|
+
let bytes;
|
|
1451
|
+
if (value[1] instanceof Uint8Array) bytes = value[1];
|
|
1452
|
+
else if (typeof value[1] == "string") if (typeof Buffer !== "undefined") bytes = Buffer.from(value[1], "base64");
|
|
1453
|
+
else if (Uint8Array.fromBase64) bytes = Uint8Array.fromBase64(value[1]);
|
|
1293
1454
|
else {
|
|
1294
1455
|
let bs = atob(value[1]);
|
|
1295
1456
|
let len = bs.length;
|
|
1296
|
-
|
|
1457
|
+
bytes = new Uint8Array(len);
|
|
1297
1458
|
for (let i = 0; i < len; i++) bytes[i] = bs.charCodeAt(i);
|
|
1298
|
-
return bytes;
|
|
1299
1459
|
}
|
|
1300
|
-
break;
|
|
1460
|
+
else break;
|
|
1461
|
+
if (value.length === 2) return bytes;
|
|
1462
|
+
if (typeof value[2] !== "string") throw new TypeError(`Unknown bytes type marker type: ${typeof value[2]}`);
|
|
1463
|
+
if (!isValidByteContainerName(value[2])) {
|
|
1464
|
+
let marker = value[2].slice(0, 64);
|
|
1465
|
+
throw new TypeError(`Unknown bytes type marker: ${marker}`);
|
|
1466
|
+
}
|
|
1467
|
+
let marker = value[2];
|
|
1468
|
+
let elementSize = TYPED_ARRAY_ELEMENT_SIZE[marker];
|
|
1469
|
+
if (elementSize !== void 0 && bytes.byteLength % elementSize !== 0) throw new TypeError(`Invalid byte length ${bytes.byteLength} for ${marker}; expected a multiple of ${elementSize}`);
|
|
1470
|
+
let buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
|
1471
|
+
if (!NATIVE_LITTLE_ENDIAN && elementSize !== void 0) swapByteOrder(new Uint8Array(buffer), elementSize);
|
|
1472
|
+
switch (marker) {
|
|
1473
|
+
case "ArrayBuffer": return buffer;
|
|
1474
|
+
case "DataView": return new DataView(buffer);
|
|
1475
|
+
case "Int8Array": return new Int8Array(buffer);
|
|
1476
|
+
case "Uint8Array": return new Uint8Array(buffer);
|
|
1477
|
+
case "Uint8ClampedArray": return new Uint8ClampedArray(buffer);
|
|
1478
|
+
case "Int16Array": return new Int16Array(buffer);
|
|
1479
|
+
case "Uint16Array": return new Uint16Array(buffer);
|
|
1480
|
+
case "Int32Array": return new Int32Array(buffer);
|
|
1481
|
+
case "Uint32Array": return new Uint32Array(buffer);
|
|
1482
|
+
case "BigInt64Array": return new BigInt64Array(buffer);
|
|
1483
|
+
case "BigUint64Array": return new BigUint64Array(buffer);
|
|
1484
|
+
case "Float32Array": return new Float32Array(buffer);
|
|
1485
|
+
case "Float64Array": return new Float64Array(buffer);
|
|
1486
|
+
default:
|
|
1487
|
+
}
|
|
1488
|
+
}
|
|
1301
1489
|
case "error":
|
|
1302
1490
|
if (value.length >= 3 && typeof value[1] === "string" && typeof value[2] === "string") {
|
|
1303
1491
|
let cls = ERROR_TYPES[value[1]] || Error;
|
|
@@ -1310,7 +1498,11 @@ var Evaluator = class Evaluator {
|
|
|
1310
1498
|
let propsObj = props;
|
|
1311
1499
|
for (let key of Object.keys(propsObj)) {
|
|
1312
1500
|
if (key === "name" || key === "message" || key === "stack") continue;
|
|
1313
|
-
|
|
1501
|
+
if (key in Object.prototype || key === "toJSON") {
|
|
1502
|
+
this.evaluateImpl(propsObj[key], result, key, depth + 1);
|
|
1503
|
+
continue;
|
|
1504
|
+
}
|
|
1505
|
+
anyResult[key] = this.evaluateImpl(propsObj[key], result, key, depth + 1);
|
|
1314
1506
|
}
|
|
1315
1507
|
}
|
|
1316
1508
|
return result;
|
|
@@ -1331,11 +1523,11 @@ var Evaluator = class Evaluator {
|
|
|
1331
1523
|
let init = value[2];
|
|
1332
1524
|
if (typeof init !== "object" || init === null) break;
|
|
1333
1525
|
if (init.body) {
|
|
1334
|
-
init.body = this.evaluateImpl(init.body, init, "body");
|
|
1526
|
+
init.body = this.evaluateImpl(init.body, init, "body", depth + 1);
|
|
1335
1527
|
if (init.body === null || typeof init.body === "string" || init.body instanceof Uint8Array || init.body instanceof ReadableStream) {} else throw new TypeError("Request body must be of type ReadableStream.");
|
|
1336
1528
|
}
|
|
1337
1529
|
if (init.signal) {
|
|
1338
|
-
init.signal = this.evaluateImpl(init.signal, init, "signal");
|
|
1530
|
+
init.signal = this.evaluateImpl(init.signal, init, "signal", depth + 1);
|
|
1339
1531
|
if (!(init.signal instanceof AbortSignal)) throw new TypeError("Request siganl must be of type AbortSignal.");
|
|
1340
1532
|
}
|
|
1341
1533
|
if (init.headers && !(init.headers instanceof Array)) throw new TypeError("Request headers must be serialized as an array of pairs.");
|
|
@@ -1352,7 +1544,7 @@ var Evaluator = class Evaluator {
|
|
|
1352
1544
|
}
|
|
1353
1545
|
case "response": {
|
|
1354
1546
|
if (value.length !== 3) break;
|
|
1355
|
-
let body = this.evaluateImpl(value[1], parent, property);
|
|
1547
|
+
let body = this.evaluateImpl(value[1], parent, property, depth + 1);
|
|
1356
1548
|
if (body === null || typeof body === "string" || body instanceof Uint8Array || body instanceof ReadableStream) {} else throw new TypeError("Response body must be of type ReadableStream.");
|
|
1357
1549
|
let init = value[2];
|
|
1358
1550
|
if (typeof init !== "object" || init === null) break;
|
|
@@ -1363,7 +1555,7 @@ var Evaluator = class Evaluator {
|
|
|
1363
1555
|
case "blob": {
|
|
1364
1556
|
if (value.length !== 3 || typeof value[1] !== "string") break;
|
|
1365
1557
|
let contentType = value[1];
|
|
1366
|
-
let content = this.evaluateImpl(value[2], parent, property);
|
|
1558
|
+
let content = this.evaluateImpl(value[2], parent, property, depth + 1);
|
|
1367
1559
|
if (!(content instanceof ReadableStream)) throw new TypeError("Blob content must be serialized as a ReadableStream.");
|
|
1368
1560
|
let promise = streamToBlobPromise(content, contentType);
|
|
1369
1561
|
this.promises.push({
|
|
@@ -1404,7 +1596,7 @@ var Evaluator = class Evaluator {
|
|
|
1404
1596
|
if (value.length == 3) return addStub(hook.get(path));
|
|
1405
1597
|
let args = value[3];
|
|
1406
1598
|
if (!(args instanceof Array)) break;
|
|
1407
|
-
args = new Evaluator(this.importer).
|
|
1599
|
+
args = new Evaluator(this.importer).evaluateWithDepth([args], depth);
|
|
1408
1600
|
return addStub(hook.call(path, args));
|
|
1409
1601
|
}
|
|
1410
1602
|
case "remap": {
|
|
@@ -1470,9 +1662,9 @@ var Evaluator = class Evaluator {
|
|
|
1470
1662
|
} else if (value instanceof Object) {
|
|
1471
1663
|
let result = value;
|
|
1472
1664
|
for (let key in result) if (key in Object.prototype || key === "toJSON") {
|
|
1473
|
-
this.evaluateImpl(result[key], result, key);
|
|
1665
|
+
this.evaluateImpl(result[key], result, key, depth + 1);
|
|
1474
1666
|
delete result[key];
|
|
1475
|
-
} else result[key] = this.evaluateImpl(result[key], result, key);
|
|
1667
|
+
} else result[key] = this.evaluateImpl(result[key], result, key, depth + 1);
|
|
1476
1668
|
return result;
|
|
1477
1669
|
} else return value;
|
|
1478
1670
|
}
|
|
@@ -1488,6 +1680,47 @@ function deserialize(value) {
|
|
|
1488
1680
|
|
|
1489
1681
|
//#endregion
|
|
1490
1682
|
//#region src/rpc.ts
|
|
1683
|
+
const ESTIMATED_OBJECT_OVERHEAD = 16;
|
|
1684
|
+
const ESTIMATED_ENTRY_OVERHEAD = 8;
|
|
1685
|
+
const ESTIMATED_BINARY_OVERHEAD = 16;
|
|
1686
|
+
const MAX_ESTIMATE_DEPTH = 64;
|
|
1687
|
+
function estimateStringSize(value) {
|
|
1688
|
+
return 2 + value.length * 3;
|
|
1689
|
+
}
|
|
1690
|
+
function estimateEncodedSize(value, seen, depth = 0) {
|
|
1691
|
+
if (depth >= MAX_ESTIMATE_DEPTH) return ESTIMATED_ENTRY_OVERHEAD;
|
|
1692
|
+
switch (typeof value) {
|
|
1693
|
+
case "string": return estimateStringSize(value);
|
|
1694
|
+
case "number": return 16;
|
|
1695
|
+
case "bigint": return 16;
|
|
1696
|
+
case "boolean": return 8;
|
|
1697
|
+
case "undefined": return 16;
|
|
1698
|
+
case "object": {
|
|
1699
|
+
if (value === null) return 8;
|
|
1700
|
+
if (ArrayBuffer.isView(value)) return ESTIMATED_BINARY_OVERHEAD + value.byteLength;
|
|
1701
|
+
if (value instanceof ArrayBuffer) return ESTIMATED_BINARY_OVERHEAD + value.byteLength;
|
|
1702
|
+
if (typeof Blob !== "undefined" && value instanceof Blob) return ESTIMATED_BINARY_OVERHEAD + value.size;
|
|
1703
|
+
if (value instanceof Date) return 16;
|
|
1704
|
+
seen ??= /* @__PURE__ */ new WeakSet();
|
|
1705
|
+
if (seen.has(value)) return ESTIMATED_ENTRY_OVERHEAD;
|
|
1706
|
+
seen.add(value);
|
|
1707
|
+
if (value instanceof Array) {
|
|
1708
|
+
let size = ESTIMATED_OBJECT_OVERHEAD;
|
|
1709
|
+
for (let item of value) size += ESTIMATED_ENTRY_OVERHEAD + estimateEncodedSize(item, seen, depth + 1);
|
|
1710
|
+
return size;
|
|
1711
|
+
}
|
|
1712
|
+
if (value instanceof Error) {
|
|
1713
|
+
let size = ESTIMATED_OBJECT_OVERHEAD + estimateStringSize(value.name) + estimateStringSize(value.message) + estimateStringSize(value.stack ?? "");
|
|
1714
|
+
for (let key of Object.keys(value)) size += ESTIMATED_ENTRY_OVERHEAD + estimateStringSize(key) + estimateEncodedSize(value[key], seen, depth + 1);
|
|
1715
|
+
return size;
|
|
1716
|
+
}
|
|
1717
|
+
let size = ESTIMATED_OBJECT_OVERHEAD;
|
|
1718
|
+
for (let key of Object.keys(value)) size += ESTIMATED_ENTRY_OVERHEAD + estimateStringSize(key) + estimateEncodedSize(value[key], seen, depth + 1);
|
|
1719
|
+
return size;
|
|
1720
|
+
}
|
|
1721
|
+
default: return 16;
|
|
1722
|
+
}
|
|
1723
|
+
}
|
|
1491
1724
|
var ImportTableEntry = class {
|
|
1492
1725
|
session;
|
|
1493
1726
|
importId;
|
|
@@ -1653,9 +1886,24 @@ var RpcSessionImpl = class {
|
|
|
1653
1886
|
onBatchDone;
|
|
1654
1887
|
pullCount = 0;
|
|
1655
1888
|
onBrokenCallbacks = [];
|
|
1889
|
+
encodingLevel;
|
|
1890
|
+
limits;
|
|
1656
1891
|
constructor(transport, mainHook, options) {
|
|
1657
1892
|
this.transport = transport;
|
|
1658
1893
|
this.options = options;
|
|
1894
|
+
let level = "string";
|
|
1895
|
+
if ("encodingLevel" in transport) {
|
|
1896
|
+
let raw = transport.encodingLevel;
|
|
1897
|
+
if (raw !== void 0) {
|
|
1898
|
+
if (raw !== "string" && raw !== "jsonCompatible" && raw !== "jsonCompatibleWithBytes" && raw !== "structuredClonable") throw new TypeError(`Unknown transport encodingLevel: ${String(raw)}`);
|
|
1899
|
+
level = raw;
|
|
1900
|
+
}
|
|
1901
|
+
}
|
|
1902
|
+
this.encodingLevel = level;
|
|
1903
|
+
this.limits = {
|
|
1904
|
+
...DEFAULT_LIMITS,
|
|
1905
|
+
...options.limits
|
|
1906
|
+
};
|
|
1659
1907
|
this.exports.push({
|
|
1660
1908
|
hook: mainHook,
|
|
1661
1909
|
refcount: 1
|
|
@@ -1736,7 +1984,7 @@ var RpcSessionImpl = class {
|
|
|
1736
1984
|
let autoRelease = exp.autoRelease;
|
|
1737
1985
|
++this.pullCount;
|
|
1738
1986
|
exp.pull = resolve().then((payload) => {
|
|
1739
|
-
let value = Devaluator.devaluate(payload.value, void 0, this, payload);
|
|
1987
|
+
let value = Devaluator.devaluate(payload.value, void 0, this, payload, this.encodingLevel);
|
|
1740
1988
|
this.send([
|
|
1741
1989
|
"resolve",
|
|
1742
1990
|
exportId,
|
|
@@ -1747,7 +1995,7 @@ var RpcSessionImpl = class {
|
|
|
1747
1995
|
this.send([
|
|
1748
1996
|
"reject",
|
|
1749
1997
|
exportId,
|
|
1750
|
-
Devaluator.devaluate(error, void 0, this)
|
|
1998
|
+
Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)
|
|
1751
1999
|
]);
|
|
1752
2000
|
if (autoRelease) this.releaseExport(exportId, 1);
|
|
1753
2001
|
}).catch((error) => {
|
|
@@ -1755,7 +2003,7 @@ var RpcSessionImpl = class {
|
|
|
1755
2003
|
this.send([
|
|
1756
2004
|
"reject",
|
|
1757
2005
|
exportId,
|
|
1758
|
-
Devaluator.devaluate(error, void 0, this)
|
|
2006
|
+
Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)
|
|
1759
2007
|
]);
|
|
1760
2008
|
if (autoRelease) this.releaseExport(exportId, 1);
|
|
1761
2009
|
} catch (error2) {
|
|
@@ -1798,6 +2046,9 @@ var RpcSessionImpl = class {
|
|
|
1798
2046
|
entry.pipeReadable = void 0;
|
|
1799
2047
|
return readable;
|
|
1800
2048
|
}
|
|
2049
|
+
getLimits() {
|
|
2050
|
+
return this.limits;
|
|
2051
|
+
}
|
|
1801
2052
|
createPipe(readable, readableHook) {
|
|
1802
2053
|
if (this.abortReason) throw this.abortReason;
|
|
1803
2054
|
this.send(["pipe"]);
|
|
@@ -1811,17 +2062,33 @@ var RpcSessionImpl = class {
|
|
|
1811
2062
|
}
|
|
1812
2063
|
send(msg) {
|
|
1813
2064
|
if (this.abortReason !== void 0) return 0;
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
msgText = JSON.stringify(msg);
|
|
1817
|
-
} catch (err) {
|
|
2065
|
+
if (this.encodingLevel === "string") {
|
|
2066
|
+
let msgText;
|
|
1818
2067
|
try {
|
|
1819
|
-
|
|
1820
|
-
} catch (
|
|
1821
|
-
|
|
2068
|
+
msgText = JSON.stringify(msg);
|
|
2069
|
+
} catch (err) {
|
|
2070
|
+
try {
|
|
2071
|
+
this.abort(err);
|
|
2072
|
+
} catch (err2) {}
|
|
2073
|
+
throw err;
|
|
2074
|
+
}
|
|
2075
|
+
try {
|
|
2076
|
+
let sent = this.transport.send(msgText);
|
|
2077
|
+
if (sent !== void 0 && typeof sent.catch === "function") sent.catch((err) => this.abort(err, false));
|
|
2078
|
+
} catch (err) {
|
|
2079
|
+
queueMicrotask(() => this.abort(err, false));
|
|
2080
|
+
}
|
|
2081
|
+
return msgText.length;
|
|
2082
|
+
} else try {
|
|
2083
|
+
let size = this.transport.send(msg);
|
|
2084
|
+
if (typeof size === "number") return size;
|
|
2085
|
+
let thenable = size;
|
|
2086
|
+
if (thenable && typeof thenable.then === "function") Promise.resolve(thenable).catch((err) => this.abort(err, false));
|
|
2087
|
+
return;
|
|
2088
|
+
} catch (err) {
|
|
2089
|
+
queueMicrotask(() => this.abort(err, false));
|
|
2090
|
+
return;
|
|
1822
2091
|
}
|
|
1823
|
-
this.transport.send(msgText).catch((err) => this.abort(err, false));
|
|
1824
|
-
return msgText.length;
|
|
1825
2092
|
}
|
|
1826
2093
|
sendCall(id, path, args) {
|
|
1827
2094
|
if (this.abortReason) throw this.abortReason;
|
|
@@ -1831,7 +2098,7 @@ var RpcSessionImpl = class {
|
|
|
1831
2098
|
path
|
|
1832
2099
|
];
|
|
1833
2100
|
if (args) {
|
|
1834
|
-
let devalue = Devaluator.devaluate(args.value, void 0, this, args);
|
|
2101
|
+
let devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
|
|
1835
2102
|
value.push(devalue[0]);
|
|
1836
2103
|
}
|
|
1837
2104
|
this.send(["push", value]);
|
|
@@ -1846,9 +2113,11 @@ var RpcSessionImpl = class {
|
|
|
1846
2113
|
id,
|
|
1847
2114
|
path
|
|
1848
2115
|
];
|
|
1849
|
-
let devalue = Devaluator.devaluate(args.value, void 0, this, args);
|
|
2116
|
+
let devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
|
|
1850
2117
|
value.push(devalue[0]);
|
|
1851
|
-
let
|
|
2118
|
+
let msg = ["stream", value];
|
|
2119
|
+
let size = this.send(msg);
|
|
2120
|
+
if (size === void 0) size = estimateEncodedSize(msg);
|
|
1852
2121
|
let importId = this.imports.length;
|
|
1853
2122
|
let entry = new ImportTableEntry(this, importId, true);
|
|
1854
2123
|
entry.remoteRefcount = 0;
|
|
@@ -1904,7 +2173,14 @@ var RpcSessionImpl = class {
|
|
|
1904
2173
|
this.cancelReadLoop?.(error);
|
|
1905
2174
|
this.cancelReadLoop = void 0;
|
|
1906
2175
|
if (trySendAbortMessage) try {
|
|
1907
|
-
|
|
2176
|
+
let abortMsg = ["abort", Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)];
|
|
2177
|
+
if (this.encodingLevel === "string") {
|
|
2178
|
+
let sent = this.transport.send(JSON.stringify(abortMsg));
|
|
2179
|
+
if (sent !== void 0 && typeof sent.catch === "function") sent.catch((err) => {});
|
|
2180
|
+
} else {
|
|
2181
|
+
let result = this.transport.send(abortMsg);
|
|
2182
|
+
if (result && typeof result.then === "function") Promise.resolve(result).catch((err) => {});
|
|
2183
|
+
}
|
|
1908
2184
|
} catch (err) {}
|
|
1909
2185
|
if (error === void 0) error = "undefined";
|
|
1910
2186
|
this.abortReason = error;
|
|
@@ -1926,18 +2202,19 @@ var RpcSessionImpl = class {
|
|
|
1926
2202
|
while (!this.abortReason) {
|
|
1927
2203
|
let readCanceled = Promise.withResolvers();
|
|
1928
2204
|
this.cancelReadLoop = readCanceled.reject;
|
|
1929
|
-
let
|
|
2205
|
+
let raw;
|
|
1930
2206
|
try {
|
|
1931
|
-
|
|
2207
|
+
raw = await Promise.race([this.transport.receive(), readCanceled.promise]);
|
|
1932
2208
|
} finally {
|
|
1933
2209
|
if (this.cancelReadLoop === readCanceled.reject) this.cancelReadLoop = void 0;
|
|
1934
2210
|
}
|
|
1935
|
-
|
|
2211
|
+
if (this.encodingLevel === "string" && raw.length > this.limits.maxMessageSize) throw new TypeError(`Incoming message exceeds maximum size of ${this.limits.maxMessageSize} UTF-16 code units.`);
|
|
1936
2212
|
if (this.abortReason) break;
|
|
2213
|
+
let msg = this.encodingLevel === "string" ? JSON.parse(raw) : raw;
|
|
1937
2214
|
if (msg instanceof Array) switch (msg[0]) {
|
|
1938
2215
|
case "push":
|
|
1939
2216
|
if (msg.length > 1) {
|
|
1940
|
-
let hook = new PayloadStubHook(new Evaluator(this).evaluate(msg[1]));
|
|
2217
|
+
let hook = new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[1]));
|
|
1941
2218
|
hook.ignoreUnhandledRejections();
|
|
1942
2219
|
this.exports.push({
|
|
1943
2220
|
hook,
|
|
@@ -1948,7 +2225,7 @@ var RpcSessionImpl = class {
|
|
|
1948
2225
|
break;
|
|
1949
2226
|
case "stream":
|
|
1950
2227
|
if (msg.length > 1) {
|
|
1951
|
-
let hook = new PayloadStubHook(new Evaluator(this).evaluate(msg[1]));
|
|
2228
|
+
let hook = new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[1]));
|
|
1952
2229
|
hook.ignoreUnhandledRejections();
|
|
1953
2230
|
let exportId = this.exports.length;
|
|
1954
2231
|
this.exports.push({
|
|
@@ -1983,13 +2260,13 @@ var RpcSessionImpl = class {
|
|
|
1983
2260
|
let importId = msg[1];
|
|
1984
2261
|
if (typeof importId == "number" && msg.length > 2) {
|
|
1985
2262
|
let imp = this.imports[importId];
|
|
1986
|
-
if (imp) if (msg[0] == "resolve") imp.resolve(new PayloadStubHook(new Evaluator(this).evaluate(msg[2])));
|
|
2263
|
+
if (imp) if (msg[0] == "resolve") imp.resolve(new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[2])));
|
|
1987
2264
|
else {
|
|
1988
|
-
let payload = new Evaluator(this).evaluate(msg[2]);
|
|
2265
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[2]);
|
|
1989
2266
|
payload.dispose();
|
|
1990
2267
|
imp.resolve(new ErrorStubHook(payload.value));
|
|
1991
2268
|
}
|
|
1992
|
-
else if (msg[0] == "resolve") new Evaluator(this).evaluate(msg[2]).dispose();
|
|
2269
|
+
else if (msg[0] == "resolve") new Evaluator(this, this.encodingLevel).evaluate(msg[2]).dispose();
|
|
1993
2270
|
continue;
|
|
1994
2271
|
}
|
|
1995
2272
|
break;
|
|
@@ -2004,9 +2281,9 @@ var RpcSessionImpl = class {
|
|
|
2004
2281
|
break;
|
|
2005
2282
|
}
|
|
2006
2283
|
case "abort": {
|
|
2007
|
-
let payload = new Evaluator(this).evaluate(msg[1]);
|
|
2284
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[1]);
|
|
2008
2285
|
payload.dispose();
|
|
2009
|
-
this.abort(payload, false);
|
|
2286
|
+
this.abort(payload.value, false);
|
|
2010
2287
|
break;
|
|
2011
2288
|
}
|
|
2012
2289
|
}
|
|
@@ -2057,6 +2334,8 @@ var RpcSession$1 = class {
|
|
|
2057
2334
|
|
|
2058
2335
|
//#endregion
|
|
2059
2336
|
//#region src/websocket.ts
|
|
2337
|
+
/** Close-frame reason max UTF-8 bytes: 125 payload − 2-byte status (RFC 6455 §5.5). */
|
|
2338
|
+
const MAX_CLOSE_REASON_BYTES = 123;
|
|
2060
2339
|
function newWebSocketRpcSession$1(webSocket, localMain, options) {
|
|
2061
2340
|
if (typeof webSocket === "string") webSocket = new WebSocket(webSocket);
|
|
2062
2341
|
return new RpcSession$1(new WebSocketTransport(webSocket), localMain, options).getRemoteMain();
|
|
@@ -2076,9 +2355,14 @@ function newWorkersWebSocketRpcResponse(request, localMain, options) {
|
|
|
2076
2355
|
webSocket: pair[1]
|
|
2077
2356
|
});
|
|
2078
2357
|
}
|
|
2358
|
+
/**
|
|
2359
|
+
* Generic WebSocket transport. Default `T = string` is backward-compatible and satisfies
|
|
2360
|
+
* `RpcTransport`. Use `T = ArrayBuffer` as a building block for binary transports.
|
|
2361
|
+
*/
|
|
2079
2362
|
var WebSocketTransport = class {
|
|
2080
2363
|
constructor(webSocket) {
|
|
2081
2364
|
this.#webSocket = webSocket;
|
|
2365
|
+
webSocket.binaryType = "arraybuffer";
|
|
2082
2366
|
if (webSocket.readyState === WebSocket.CONNECTING) {
|
|
2083
2367
|
this.#sendQueue = [];
|
|
2084
2368
|
webSocket.addEventListener("open", (event) => {
|
|
@@ -2091,12 +2375,12 @@ var WebSocketTransport = class {
|
|
|
2091
2375
|
});
|
|
2092
2376
|
}
|
|
2093
2377
|
webSocket.addEventListener("message", (event) => {
|
|
2094
|
-
if (this.#error) {} else if (typeof event.data === "string") if (this.#receiveResolver) {
|
|
2378
|
+
if (this.#error) {} else if (typeof event.data === "string" || event.data instanceof ArrayBuffer) if (this.#receiveResolver) {
|
|
2095
2379
|
this.#receiveResolver(event.data);
|
|
2096
2380
|
this.#receiveResolver = void 0;
|
|
2097
2381
|
this.#receiveRejecter = void 0;
|
|
2098
2382
|
} else this.#receiveQueue.push(event.data);
|
|
2099
|
-
else this.#receivedError(/* @__PURE__ */ new TypeError("Received
|
|
2383
|
+
else this.#receivedError(/* @__PURE__ */ new TypeError("Received unexpected message type from WebSocket."));
|
|
2100
2384
|
});
|
|
2101
2385
|
webSocket.addEventListener("close", (event) => {
|
|
2102
2386
|
this.#receivedError(/* @__PURE__ */ new Error(`Peer closed WebSocket: ${event.code} ${event.reason}`));
|
|
@@ -2111,13 +2395,13 @@ var WebSocketTransport = class {
|
|
|
2111
2395
|
#receiveRejecter;
|
|
2112
2396
|
#receiveQueue = [];
|
|
2113
2397
|
#error;
|
|
2114
|
-
|
|
2398
|
+
send(message) {
|
|
2115
2399
|
if (this.#sendQueue === void 0) this.#webSocket.send(message);
|
|
2116
2400
|
else this.#sendQueue.push(message);
|
|
2117
2401
|
}
|
|
2118
|
-
|
|
2119
|
-
if (this.#receiveQueue.length > 0) return this.#receiveQueue.shift();
|
|
2120
|
-
else if (this.#error)
|
|
2402
|
+
receive() {
|
|
2403
|
+
if (this.#receiveQueue.length > 0) return Promise.resolve(this.#receiveQueue.shift());
|
|
2404
|
+
else if (this.#error) return Promise.reject(this.#error);
|
|
2121
2405
|
else return new Promise((resolve, reject) => {
|
|
2122
2406
|
this.#receiveResolver = resolve;
|
|
2123
2407
|
this.#receiveRejecter = reject;
|
|
@@ -2127,6 +2411,8 @@ var WebSocketTransport = class {
|
|
|
2127
2411
|
let message;
|
|
2128
2412
|
if (reason instanceof Error) message = reason.message;
|
|
2129
2413
|
else message = `${reason}`;
|
|
2414
|
+
let reasonBytes = new TextEncoder().encode(message);
|
|
2415
|
+
if (reasonBytes.length > 123) message = new TextDecoder().decode(reasonBytes.subarray(0, 123), { stream: true });
|
|
2130
2416
|
this.#webSocket.close(3e3, message);
|
|
2131
2417
|
if (!this.#error) this.#error = reason;
|
|
2132
2418
|
}
|
|
@@ -2144,6 +2430,7 @@ var WebSocketTransport = class {
|
|
|
2144
2430
|
|
|
2145
2431
|
//#endregion
|
|
2146
2432
|
//#region src/batch.ts
|
|
2433
|
+
const yieldToMacrotask = typeof setImmediate === "function" ? () => new Promise((resolve) => setImmediate(resolve)) : () => new Promise((resolve) => setTimeout(resolve, 0));
|
|
2147
2434
|
var BatchClientTransport = class {
|
|
2148
2435
|
constructor(sendBatch) {
|
|
2149
2436
|
this.#promise = this.#scheduleBatch(sendBatch);
|
|
@@ -2152,7 +2439,7 @@ var BatchClientTransport = class {
|
|
|
2152
2439
|
#aborted;
|
|
2153
2440
|
#batchToSend = [];
|
|
2154
2441
|
#batchToReceive = null;
|
|
2155
|
-
|
|
2442
|
+
send(message) {
|
|
2156
2443
|
if (this.#batchToSend !== null) this.#batchToSend.push(message);
|
|
2157
2444
|
}
|
|
2158
2445
|
async receive() {
|
|
@@ -2165,7 +2452,7 @@ var BatchClientTransport = class {
|
|
|
2165
2452
|
this.#aborted = reason;
|
|
2166
2453
|
}
|
|
2167
2454
|
async #scheduleBatch(sendBatch) {
|
|
2168
|
-
await
|
|
2455
|
+
await yieldToMacrotask();
|
|
2169
2456
|
if (this.#aborted !== void 0) throw this.#aborted;
|
|
2170
2457
|
let batch = this.#batchToSend;
|
|
2171
2458
|
this.#batchToSend = null;
|
|
@@ -2194,7 +2481,7 @@ var BatchServerTransport = class {
|
|
|
2194
2481
|
#batchToSend = [];
|
|
2195
2482
|
#batchToReceive;
|
|
2196
2483
|
#allReceived = Promise.withResolvers();
|
|
2197
|
-
|
|
2484
|
+
send(message) {
|
|
2198
2485
|
this.#batchToSend.push(message);
|
|
2199
2486
|
}
|
|
2200
2487
|
async receive() {
|
|
@@ -2243,7 +2530,11 @@ async function newHttpBatchRpcResponse(request, localMain, options) {
|
|
|
2243
2530
|
* @param options Optional RPC session options. You can also pass headers to set on the response.
|
|
2244
2531
|
*/
|
|
2245
2532
|
async function nodeHttpBatchRpcResponse(request, response, localMain, options) {
|
|
2246
|
-
if (request.method !== "POST")
|
|
2533
|
+
if (request.method !== "POST") {
|
|
2534
|
+
response.writeHead(405, "This endpoint only accepts POST requests.", options?.headers);
|
|
2535
|
+
response.end();
|
|
2536
|
+
return;
|
|
2537
|
+
}
|
|
2247
2538
|
let body = await new Promise((resolve, reject) => {
|
|
2248
2539
|
let chunks = [];
|
|
2249
2540
|
request.on("data", (chunk) => {
|
|
@@ -2268,17 +2559,17 @@ function newMessagePortRpcSession$1(port, localMain, options) {
|
|
|
2268
2559
|
return new RpcSession$1(new MessagePortTransport(port), localMain, options).getRemoteMain();
|
|
2269
2560
|
}
|
|
2270
2561
|
var MessagePortTransport = class {
|
|
2562
|
+
encodingLevel = "structuredClonable";
|
|
2271
2563
|
constructor(port) {
|
|
2272
2564
|
this.#port = port;
|
|
2273
2565
|
port.start();
|
|
2274
2566
|
port.addEventListener("message", (event) => {
|
|
2275
2567
|
if (this.#error) {} else if (event.data === null) this.#receivedError(/* @__PURE__ */ new Error("Peer closed MessagePort connection."));
|
|
2276
|
-
else if (
|
|
2568
|
+
else if (this.#receiveResolver) {
|
|
2277
2569
|
this.#receiveResolver(event.data);
|
|
2278
2570
|
this.#receiveResolver = void 0;
|
|
2279
2571
|
this.#receiveRejecter = void 0;
|
|
2280
2572
|
} else this.#receiveQueue.push(event.data);
|
|
2281
|
-
else this.#receivedError(/* @__PURE__ */ new TypeError("Received non-string message from MessagePort."));
|
|
2282
2573
|
});
|
|
2283
2574
|
port.addEventListener("messageerror", (event) => {
|
|
2284
2575
|
this.#receivedError(/* @__PURE__ */ new Error("MessagePort message error."));
|
|
@@ -2289,7 +2580,7 @@ var MessagePortTransport = class {
|
|
|
2289
2580
|
#receiveRejecter;
|
|
2290
2581
|
#receiveQueue = [];
|
|
2291
2582
|
#error;
|
|
2292
|
-
|
|
2583
|
+
send(message) {
|
|
2293
2584
|
if (this.#error) throw this.#error;
|
|
2294
2585
|
this.#port.postMessage(message);
|
|
2295
2586
|
}
|
|
@@ -2508,6 +2799,9 @@ var MapApplicator = class {
|
|
|
2508
2799
|
getPipeReadable(exportId) {
|
|
2509
2800
|
throw new Error("A mapper function cannot use pipe readables.");
|
|
2510
2801
|
}
|
|
2802
|
+
getLimits() {
|
|
2803
|
+
return DEFAULT_LIMITS;
|
|
2804
|
+
}
|
|
2511
2805
|
};
|
|
2512
2806
|
function applyMapToElement(input, parent, owner, captures, instructions) {
|
|
2513
2807
|
let mapper = new MapApplicator(captures, new PayloadStubHook(RpcPayload.deepCopyFrom(input, parent, owner)));
|
|
@@ -2568,8 +2862,7 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
|
|
|
2568
2862
|
throw new Error(`Unknown WritableStream method: ${method}`);
|
|
2569
2863
|
}
|
|
2570
2864
|
if (method === "close" || method === "abort") state.closed = true;
|
|
2571
|
-
|
|
2572
|
-
return new PromiseStubHook(args.deliverCall(func, state.writer).then((payload) => new PayloadStubHook(payload)));
|
|
2865
|
+
return new PromiseStubHook((method === "write" ? args.deliverStreamWrite(state.writer) : args.deliverCall(state.writer[method], state.writer)).then((payload) => new PayloadStubHook(payload)));
|
|
2573
2866
|
} catch (err) {
|
|
2574
2867
|
return new ErrorStubHook(err);
|
|
2575
2868
|
}
|
|
@@ -2846,20 +3139,23 @@ let newMessagePortRpcSession = newMessagePortRpcSession$1;
|
|
|
2846
3139
|
* credentials as parameters and returns the authorized API), then cross-origin requests should
|
|
2847
3140
|
* be safe.
|
|
2848
3141
|
*/
|
|
2849
|
-
async function newWorkersRpcResponse(request, localMain) {
|
|
3142
|
+
async function newWorkersRpcResponse(request, localMain, options) {
|
|
2850
3143
|
if (request.method === "POST") {
|
|
2851
|
-
let response = await newHttpBatchRpcResponse(request, localMain);
|
|
3144
|
+
let response = await newHttpBatchRpcResponse(request, localMain, options);
|
|
2852
3145
|
response.headers.set("Access-Control-Allow-Origin", "*");
|
|
2853
3146
|
return response;
|
|
2854
|
-
} else if (request.headers.get("Upgrade")?.toLowerCase() === "websocket") return newWorkersWebSocketRpcResponse(request, localMain);
|
|
3147
|
+
} else if (request.headers.get("Upgrade")?.toLowerCase() === "websocket") return newWorkersWebSocketRpcResponse(request, localMain, options);
|
|
2855
3148
|
else return new Response("This endpoint only accepts POST or WebSocket requests.", { status: 400 });
|
|
2856
3149
|
}
|
|
2857
3150
|
|
|
2858
3151
|
//#endregion
|
|
3152
|
+
exports.DEFAULT_LIMITS = DEFAULT_LIMITS;
|
|
3153
|
+
exports.DEFAULT_MAX_DEPTH = DEFAULT_MAX_DEPTH;
|
|
2859
3154
|
exports.RpcPromise = RpcPromise;
|
|
2860
3155
|
exports.RpcSession = RpcSession;
|
|
2861
3156
|
exports.RpcStub = RpcStub;
|
|
2862
3157
|
exports.RpcTarget = RpcTarget;
|
|
3158
|
+
exports.WebSocketTransport = WebSocketTransport;
|
|
2863
3159
|
exports.deserialize = deserialize;
|
|
2864
3160
|
exports.newHttpBatchRpcResponse = newHttpBatchRpcResponse;
|
|
2865
3161
|
exports.newHttpBatchRpcSession = newHttpBatchRpcSession;
|