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