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.js
CHANGED
|
@@ -47,7 +47,19 @@ function typeForRpc(value) {
|
|
|
47
47
|
case Array.prototype: return "array";
|
|
48
48
|
case Date.prototype: return "date";
|
|
49
49
|
case Uint8Array.prototype:
|
|
50
|
-
case BUFFER_PROTOTYPE:
|
|
50
|
+
case BUFFER_PROTOTYPE:
|
|
51
|
+
case ArrayBuffer.prototype:
|
|
52
|
+
case DataView.prototype:
|
|
53
|
+
case Int8Array.prototype:
|
|
54
|
+
case Uint8ClampedArray.prototype:
|
|
55
|
+
case Int16Array.prototype:
|
|
56
|
+
case Uint16Array.prototype:
|
|
57
|
+
case Int32Array.prototype:
|
|
58
|
+
case Uint32Array.prototype:
|
|
59
|
+
case BigInt64Array.prototype:
|
|
60
|
+
case BigUint64Array.prototype:
|
|
61
|
+
case Float32Array.prototype:
|
|
62
|
+
case Float64Array.prototype: return "bytes";
|
|
51
63
|
case WritableStream.prototype: return "writable";
|
|
52
64
|
case ReadableStream.prototype: return "readable";
|
|
53
65
|
case Headers.prototype: return "headers";
|
|
@@ -517,6 +529,30 @@ var RpcPayload = class RpcPayload {
|
|
|
517
529
|
this.dispose();
|
|
518
530
|
}
|
|
519
531
|
}
|
|
532
|
+
async deliverStreamWrite(writer) {
|
|
533
|
+
try {
|
|
534
|
+
let promises = [];
|
|
535
|
+
this.deliverTo(this, "value", promises);
|
|
536
|
+
if (promises.length > 0) await Promise.all(promises);
|
|
537
|
+
let chunk = this.value[0];
|
|
538
|
+
if ((this.hooks.length > 0 || this.promises.length > 0) && chunk instanceof Object) {
|
|
539
|
+
if (!(Symbol.dispose in chunk)) Object.defineProperty(chunk, Symbol.dispose, {
|
|
540
|
+
value: () => this.dispose(),
|
|
541
|
+
writable: true,
|
|
542
|
+
enumerable: false,
|
|
543
|
+
configurable: true
|
|
544
|
+
});
|
|
545
|
+
await writer.write(chunk);
|
|
546
|
+
return RpcPayload.fromAppReturn(void 0);
|
|
547
|
+
}
|
|
548
|
+
await writer.write(chunk);
|
|
549
|
+
this.dispose();
|
|
550
|
+
return RpcPayload.fromAppReturn(void 0);
|
|
551
|
+
} catch (err) {
|
|
552
|
+
this.dispose();
|
|
553
|
+
throw err;
|
|
554
|
+
}
|
|
555
|
+
}
|
|
520
556
|
async deliverResolve() {
|
|
521
557
|
try {
|
|
522
558
|
let promises = [];
|
|
@@ -935,6 +971,77 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
|
935
971
|
|
|
936
972
|
//#endregion
|
|
937
973
|
//#region src/serialize.ts
|
|
974
|
+
const DEFAULT_MAX_DEPTH = 256;
|
|
975
|
+
const DEFAULT_LIMITS = {
|
|
976
|
+
maxBigIntDigits: 16384,
|
|
977
|
+
maxDepth: 256,
|
|
978
|
+
maxMessageSize: 32 * 1024 * 1024
|
|
979
|
+
};
|
|
980
|
+
const NATIVE_LITTLE_ENDIAN = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
|
|
981
|
+
const BYTE_CONTAINER_TYPE_NAMES = [
|
|
982
|
+
"ArrayBuffer",
|
|
983
|
+
"DataView",
|
|
984
|
+
"Int8Array",
|
|
985
|
+
"Uint8Array",
|
|
986
|
+
"Uint8ClampedArray",
|
|
987
|
+
"Int16Array",
|
|
988
|
+
"Uint16Array",
|
|
989
|
+
"Int32Array",
|
|
990
|
+
"Uint32Array",
|
|
991
|
+
"BigInt64Array",
|
|
992
|
+
"BigUint64Array",
|
|
993
|
+
"Float32Array",
|
|
994
|
+
"Float64Array"
|
|
995
|
+
];
|
|
996
|
+
function isValidByteContainerName(value) {
|
|
997
|
+
return BYTE_CONTAINER_TYPE_NAMES.includes(value);
|
|
998
|
+
}
|
|
999
|
+
const TYPED_ARRAY_ELEMENT_SIZE = {
|
|
1000
|
+
ArrayBuffer: void 0,
|
|
1001
|
+
DataView: void 0,
|
|
1002
|
+
Int8Array: void 0,
|
|
1003
|
+
Uint8Array: void 0,
|
|
1004
|
+
Uint8ClampedArray: void 0,
|
|
1005
|
+
Int16Array: 2,
|
|
1006
|
+
Uint16Array: 2,
|
|
1007
|
+
Int32Array: 4,
|
|
1008
|
+
Uint32Array: 4,
|
|
1009
|
+
BigInt64Array: 8,
|
|
1010
|
+
BigUint64Array: 8,
|
|
1011
|
+
Float32Array: 4,
|
|
1012
|
+
Float64Array: 8
|
|
1013
|
+
};
|
|
1014
|
+
const BYTE_CONTAINER_PROTOTYPES = {
|
|
1015
|
+
ArrayBuffer: ArrayBuffer.prototype,
|
|
1016
|
+
DataView: DataView.prototype,
|
|
1017
|
+
Int8Array: Int8Array.prototype,
|
|
1018
|
+
Uint8ClampedArray: Uint8ClampedArray.prototype,
|
|
1019
|
+
Int16Array: Int16Array.prototype,
|
|
1020
|
+
Uint16Array: Uint16Array.prototype,
|
|
1021
|
+
Int32Array: Int32Array.prototype,
|
|
1022
|
+
Uint32Array: Uint32Array.prototype,
|
|
1023
|
+
BigInt64Array: BigInt64Array.prototype,
|
|
1024
|
+
BigUint64Array: BigUint64Array.prototype,
|
|
1025
|
+
Float32Array: Float32Array.prototype,
|
|
1026
|
+
Float64Array: Float64Array.prototype
|
|
1027
|
+
};
|
|
1028
|
+
const BYTE_CONTAINER_TYPE_BY_PROTOTYPE = /* @__PURE__ */ new Map();
|
|
1029
|
+
for (let type of Object.keys(BYTE_CONTAINER_PROTOTYPES)) BYTE_CONTAINER_TYPE_BY_PROTOTYPE.set(BYTE_CONTAINER_PROTOTYPES[type], type);
|
|
1030
|
+
function swapByteOrder(bytes, elementSize) {
|
|
1031
|
+
if (elementSize !== 2 && elementSize !== 4 && elementSize !== 8) throw new RangeError(`Unsupported element size: ${elementSize}`);
|
|
1032
|
+
let view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
1033
|
+
for (let offset = 0; offset < bytes.byteLength; offset += elementSize) switch (elementSize) {
|
|
1034
|
+
case 2:
|
|
1035
|
+
view.setUint16(offset, view.getUint16(offset, false), true);
|
|
1036
|
+
break;
|
|
1037
|
+
case 4:
|
|
1038
|
+
view.setUint32(offset, view.getUint32(offset, false), true);
|
|
1039
|
+
break;
|
|
1040
|
+
case 8:
|
|
1041
|
+
view.setBigUint64(offset, view.getBigUint64(offset, false), true);
|
|
1042
|
+
break;
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
938
1045
|
var NullExporter = class {
|
|
939
1046
|
exportStub(stub) {
|
|
940
1047
|
throw new Error("Cannot serialize RPC stubs without an RPC session.");
|
|
@@ -955,6 +1062,7 @@ async function streamToBlob(stream, type) {
|
|
|
955
1062
|
return b.type === type ? b : b.slice(0, b.size, type);
|
|
956
1063
|
}
|
|
957
1064
|
const ERROR_TYPES = {
|
|
1065
|
+
__proto__: null,
|
|
958
1066
|
Error,
|
|
959
1067
|
EvalError,
|
|
960
1068
|
RangeError,
|
|
@@ -967,12 +1075,14 @@ const ERROR_TYPES = {
|
|
|
967
1075
|
var Devaluator = class Devaluator {
|
|
968
1076
|
exporter;
|
|
969
1077
|
source;
|
|
970
|
-
|
|
1078
|
+
encodingLevel;
|
|
1079
|
+
constructor(exporter, source, encodingLevel) {
|
|
971
1080
|
this.exporter = exporter;
|
|
972
1081
|
this.source = source;
|
|
1082
|
+
this.encodingLevel = encodingLevel;
|
|
973
1083
|
}
|
|
974
|
-
static devaluate(value, parent, exporter = NULL_EXPORTER, source) {
|
|
975
|
-
let devaluator = new Devaluator(exporter, source);
|
|
1084
|
+
static devaluate(value, parent, exporter = NULL_EXPORTER, source, encodingLevel = "string") {
|
|
1085
|
+
let devaluator = new Devaluator(exporter, source, encodingLevel);
|
|
976
1086
|
try {
|
|
977
1087
|
return devaluator.devaluateImpl(value, parent, 0);
|
|
978
1088
|
} catch (err) {
|
|
@@ -984,7 +1094,7 @@ var Devaluator = class Devaluator {
|
|
|
984
1094
|
}
|
|
985
1095
|
exports;
|
|
986
1096
|
devaluateImpl(value, parent, depth) {
|
|
987
|
-
if (depth >=
|
|
1097
|
+
if (depth >= 256) throw new Error("Serialization exceeded maximum allowed depth. (Does the message contain cycles?)");
|
|
988
1098
|
switch (typeForRpc(value)) {
|
|
989
1099
|
case "unsupported": {
|
|
990
1100
|
let msg;
|
|
@@ -995,10 +1105,12 @@ var Devaluator = class Devaluator {
|
|
|
995
1105
|
}
|
|
996
1106
|
throw new TypeError(msg);
|
|
997
1107
|
}
|
|
998
|
-
case "primitive": if (typeof value === "number" && !isFinite(value))
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1108
|
+
case "primitive": if (typeof value === "number" && !isFinite(value)) {
|
|
1109
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1110
|
+
if (value === Infinity) return ["inf"];
|
|
1111
|
+
else if (value === -Infinity) return ["-inf"];
|
|
1112
|
+
else return ["nan"];
|
|
1113
|
+
} else return value;
|
|
1002
1114
|
case "object": {
|
|
1003
1115
|
let object = value;
|
|
1004
1116
|
let result = {};
|
|
@@ -1012,22 +1124,47 @@ var Devaluator = class Devaluator {
|
|
|
1012
1124
|
for (let i = 0; i < len; i++) result[i] = this.devaluateImpl(array[i], array, depth + 1);
|
|
1013
1125
|
return [result];
|
|
1014
1126
|
}
|
|
1015
|
-
case "bigint":
|
|
1127
|
+
case "bigint":
|
|
1128
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1129
|
+
return ["bigint", value.toString()];
|
|
1016
1130
|
case "date": {
|
|
1131
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1017
1132
|
const time = value.getTime();
|
|
1018
1133
|
return ["date", Number.isNaN(time) ? null : time];
|
|
1019
1134
|
}
|
|
1020
1135
|
case "bytes": {
|
|
1021
|
-
let
|
|
1022
|
-
|
|
1136
|
+
let alternateTypeName = BYTE_CONTAINER_TYPE_BY_PROTOTYPE.get(Object.getPrototypeOf(value));
|
|
1137
|
+
let bytes;
|
|
1138
|
+
if (alternateTypeName === "ArrayBuffer") bytes = new Uint8Array(value);
|
|
1139
|
+
else if (alternateTypeName === void 0) bytes = value;
|
|
1140
|
+
else {
|
|
1141
|
+
let view = value;
|
|
1142
|
+
bytes = new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
|
|
1143
|
+
let elementSize = TYPED_ARRAY_ELEMENT_SIZE[alternateTypeName];
|
|
1144
|
+
if (!NATIVE_LITTLE_ENDIAN && elementSize) {
|
|
1145
|
+
bytes = bytes.slice();
|
|
1146
|
+
swapByteOrder(bytes, elementSize);
|
|
1147
|
+
}
|
|
1148
|
+
}
|
|
1149
|
+
if (this.encodingLevel === "structuredClonable" || this.encodingLevel === "jsonCompatibleWithBytes") return alternateTypeName === void 0 ? ["bytes", bytes] : [
|
|
1150
|
+
"bytes",
|
|
1151
|
+
bytes,
|
|
1152
|
+
alternateTypeName
|
|
1153
|
+
];
|
|
1023
1154
|
let b64;
|
|
1024
|
-
if (
|
|
1155
|
+
if (bytes.toBase64) b64 = bytes.toBase64({ omitPadding: true });
|
|
1156
|
+
else if (typeof Buffer !== "undefined") b64 = (bytes instanceof Buffer ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)).toString("base64");
|
|
1025
1157
|
else {
|
|
1026
1158
|
let binary = "";
|
|
1027
1159
|
for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]);
|
|
1028
1160
|
b64 = btoa(binary);
|
|
1029
1161
|
}
|
|
1030
|
-
|
|
1162
|
+
b64 = b64.replace(/=+$/, "");
|
|
1163
|
+
return alternateTypeName === void 0 ? ["bytes", b64] : [
|
|
1164
|
+
"bytes",
|
|
1165
|
+
b64,
|
|
1166
|
+
alternateTypeName
|
|
1167
|
+
];
|
|
1031
1168
|
}
|
|
1032
1169
|
case "headers": return ["headers", [...value]];
|
|
1033
1170
|
case "request": {
|
|
@@ -1143,7 +1280,9 @@ var Devaluator = class Devaluator {
|
|
|
1143
1280
|
} else if (rewritten && rewritten.stack) result.push(rewritten.stack);
|
|
1144
1281
|
return result;
|
|
1145
1282
|
}
|
|
1146
|
-
case "undefined":
|
|
1283
|
+
case "undefined":
|
|
1284
|
+
if (this.encodingLevel === "structuredClonable") return;
|
|
1285
|
+
return ["undefined"];
|
|
1147
1286
|
case "stub":
|
|
1148
1287
|
case "rpc-promise": {
|
|
1149
1288
|
if (!this.source) throw new Error("Can't serialize RPC stubs in this context.");
|
|
@@ -1210,6 +1349,9 @@ var NullImporter = class {
|
|
|
1210
1349
|
getPipeReadable(exportId) {
|
|
1211
1350
|
throw new Error("Cannot retrieve pipe readable without an RPC session.");
|
|
1212
1351
|
}
|
|
1352
|
+
getLimits() {
|
|
1353
|
+
return DEFAULT_LIMITS;
|
|
1354
|
+
}
|
|
1213
1355
|
};
|
|
1214
1356
|
const NULL_IMPORTER = new NullImporter();
|
|
1215
1357
|
function fixBrokenRequestBody(request, body) {
|
|
@@ -1226,15 +1368,22 @@ function streamToBlobPromise(stream, type) {
|
|
|
1226
1368
|
}
|
|
1227
1369
|
var Evaluator = class Evaluator {
|
|
1228
1370
|
importer;
|
|
1229
|
-
|
|
1371
|
+
encodingLevel;
|
|
1372
|
+
limits;
|
|
1373
|
+
constructor(importer, encodingLevel = "string") {
|
|
1230
1374
|
this.importer = importer;
|
|
1375
|
+
this.encodingLevel = encodingLevel;
|
|
1376
|
+
this.limits = importer.getLimits();
|
|
1231
1377
|
}
|
|
1232
1378
|
hooks = [];
|
|
1233
1379
|
promises = [];
|
|
1234
1380
|
evaluate(value) {
|
|
1381
|
+
return this.evaluateWithDepth(value, 0);
|
|
1382
|
+
}
|
|
1383
|
+
evaluateWithDepth(value, depth) {
|
|
1235
1384
|
let payload = RpcPayload.forEvaluate(this.hooks, this.promises);
|
|
1236
1385
|
try {
|
|
1237
|
-
payload.value = this.evaluateImpl(value, payload, "value");
|
|
1386
|
+
payload.value = this.evaluateImpl(value, payload, "value", depth);
|
|
1238
1387
|
return payload;
|
|
1239
1388
|
} catch (err) {
|
|
1240
1389
|
payload.dispose();
|
|
@@ -1244,31 +1393,70 @@ var Evaluator = class Evaluator {
|
|
|
1244
1393
|
evaluateCopy(value) {
|
|
1245
1394
|
return this.evaluate(structuredClone(value));
|
|
1246
1395
|
}
|
|
1247
|
-
evaluateImpl(value, parent, property) {
|
|
1396
|
+
evaluateImpl(value, parent, property, depth) {
|
|
1397
|
+
let maxDepth = this.limits.maxDepth;
|
|
1398
|
+
if (depth >= maxDepth) throw new TypeError(`Deserialization exceeded maximum allowed message depth of ${maxDepth}.`);
|
|
1399
|
+
if (this.encodingLevel === "structuredClonable") {
|
|
1400
|
+
if (value instanceof Date || typeof value === "bigint") return value;
|
|
1401
|
+
}
|
|
1248
1402
|
if (value instanceof Array) {
|
|
1249
1403
|
if (value.length == 1 && value[0] instanceof Array) {
|
|
1250
1404
|
let result = value[0];
|
|
1251
|
-
for (let i = 0; i < result.length; i++) result[i] = this.evaluateImpl(result[i], result, i);
|
|
1405
|
+
for (let i = 0; i < result.length; i++) result[i] = this.evaluateImpl(result[i], result, i, depth + 1);
|
|
1252
1406
|
return result;
|
|
1253
1407
|
} else switch (value[0]) {
|
|
1254
1408
|
case "bigint":
|
|
1255
|
-
if (typeof value[1] == "string")
|
|
1409
|
+
if (typeof value[1] == "string") {
|
|
1410
|
+
let digits = value[1];
|
|
1411
|
+
let maxBigIntDigits = this.limits.maxBigIntDigits;
|
|
1412
|
+
if (digits.length > maxBigIntDigits) throw new TypeError(`Deserialized bigint exceeds maximum length of ${maxBigIntDigits} digits.`);
|
|
1413
|
+
return BigInt(digits);
|
|
1414
|
+
}
|
|
1256
1415
|
break;
|
|
1257
1416
|
case "date":
|
|
1258
1417
|
if (value[1] === null) return /* @__PURE__ */ new Date(NaN);
|
|
1259
1418
|
if (typeof value[1] == "number") return new Date(value[1]);
|
|
1260
1419
|
break;
|
|
1261
|
-
case "bytes":
|
|
1262
|
-
|
|
1263
|
-
|
|
1420
|
+
case "bytes": {
|
|
1421
|
+
let bytes;
|
|
1422
|
+
if (value[1] instanceof Uint8Array) bytes = value[1];
|
|
1423
|
+
else if (typeof value[1] == "string") if (typeof Buffer !== "undefined") bytes = Buffer.from(value[1], "base64");
|
|
1424
|
+
else if (Uint8Array.fromBase64) bytes = Uint8Array.fromBase64(value[1]);
|
|
1264
1425
|
else {
|
|
1265
1426
|
let bs = atob(value[1]);
|
|
1266
1427
|
let len = bs.length;
|
|
1267
|
-
|
|
1428
|
+
bytes = new Uint8Array(len);
|
|
1268
1429
|
for (let i = 0; i < len; i++) bytes[i] = bs.charCodeAt(i);
|
|
1269
|
-
return bytes;
|
|
1270
1430
|
}
|
|
1271
|
-
break;
|
|
1431
|
+
else break;
|
|
1432
|
+
if (value.length === 2) return bytes;
|
|
1433
|
+
if (typeof value[2] !== "string") throw new TypeError(`Unknown bytes type marker type: ${typeof value[2]}`);
|
|
1434
|
+
if (!isValidByteContainerName(value[2])) {
|
|
1435
|
+
let marker = value[2].slice(0, 64);
|
|
1436
|
+
throw new TypeError(`Unknown bytes type marker: ${marker}`);
|
|
1437
|
+
}
|
|
1438
|
+
let marker = value[2];
|
|
1439
|
+
let elementSize = TYPED_ARRAY_ELEMENT_SIZE[marker];
|
|
1440
|
+
if (elementSize !== void 0 && bytes.byteLength % elementSize !== 0) throw new TypeError(`Invalid byte length ${bytes.byteLength} for ${marker}; expected a multiple of ${elementSize}`);
|
|
1441
|
+
let buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
|
1442
|
+
if (!NATIVE_LITTLE_ENDIAN && elementSize !== void 0) swapByteOrder(new Uint8Array(buffer), elementSize);
|
|
1443
|
+
switch (marker) {
|
|
1444
|
+
case "ArrayBuffer": return buffer;
|
|
1445
|
+
case "DataView": return new DataView(buffer);
|
|
1446
|
+
case "Int8Array": return new Int8Array(buffer);
|
|
1447
|
+
case "Uint8Array": return new Uint8Array(buffer);
|
|
1448
|
+
case "Uint8ClampedArray": return new Uint8ClampedArray(buffer);
|
|
1449
|
+
case "Int16Array": return new Int16Array(buffer);
|
|
1450
|
+
case "Uint16Array": return new Uint16Array(buffer);
|
|
1451
|
+
case "Int32Array": return new Int32Array(buffer);
|
|
1452
|
+
case "Uint32Array": return new Uint32Array(buffer);
|
|
1453
|
+
case "BigInt64Array": return new BigInt64Array(buffer);
|
|
1454
|
+
case "BigUint64Array": return new BigUint64Array(buffer);
|
|
1455
|
+
case "Float32Array": return new Float32Array(buffer);
|
|
1456
|
+
case "Float64Array": return new Float64Array(buffer);
|
|
1457
|
+
default:
|
|
1458
|
+
}
|
|
1459
|
+
}
|
|
1272
1460
|
case "error":
|
|
1273
1461
|
if (value.length >= 3 && typeof value[1] === "string" && typeof value[2] === "string") {
|
|
1274
1462
|
let cls = ERROR_TYPES[value[1]] || Error;
|
|
@@ -1281,7 +1469,11 @@ var Evaluator = class Evaluator {
|
|
|
1281
1469
|
let propsObj = props;
|
|
1282
1470
|
for (let key of Object.keys(propsObj)) {
|
|
1283
1471
|
if (key === "name" || key === "message" || key === "stack") continue;
|
|
1284
|
-
|
|
1472
|
+
if (key in Object.prototype || key === "toJSON") {
|
|
1473
|
+
this.evaluateImpl(propsObj[key], result, key, depth + 1);
|
|
1474
|
+
continue;
|
|
1475
|
+
}
|
|
1476
|
+
anyResult[key] = this.evaluateImpl(propsObj[key], result, key, depth + 1);
|
|
1285
1477
|
}
|
|
1286
1478
|
}
|
|
1287
1479
|
return result;
|
|
@@ -1302,11 +1494,11 @@ var Evaluator = class Evaluator {
|
|
|
1302
1494
|
let init = value[2];
|
|
1303
1495
|
if (typeof init !== "object" || init === null) break;
|
|
1304
1496
|
if (init.body) {
|
|
1305
|
-
init.body = this.evaluateImpl(init.body, init, "body");
|
|
1497
|
+
init.body = this.evaluateImpl(init.body, init, "body", depth + 1);
|
|
1306
1498
|
if (init.body === null || typeof init.body === "string" || init.body instanceof Uint8Array || init.body instanceof ReadableStream) {} else throw new TypeError("Request body must be of type ReadableStream.");
|
|
1307
1499
|
}
|
|
1308
1500
|
if (init.signal) {
|
|
1309
|
-
init.signal = this.evaluateImpl(init.signal, init, "signal");
|
|
1501
|
+
init.signal = this.evaluateImpl(init.signal, init, "signal", depth + 1);
|
|
1310
1502
|
if (!(init.signal instanceof AbortSignal)) throw new TypeError("Request siganl must be of type AbortSignal.");
|
|
1311
1503
|
}
|
|
1312
1504
|
if (init.headers && !(init.headers instanceof Array)) throw new TypeError("Request headers must be serialized as an array of pairs.");
|
|
@@ -1323,7 +1515,7 @@ var Evaluator = class Evaluator {
|
|
|
1323
1515
|
}
|
|
1324
1516
|
case "response": {
|
|
1325
1517
|
if (value.length !== 3) break;
|
|
1326
|
-
let body = this.evaluateImpl(value[1], parent, property);
|
|
1518
|
+
let body = this.evaluateImpl(value[1], parent, property, depth + 1);
|
|
1327
1519
|
if (body === null || typeof body === "string" || body instanceof Uint8Array || body instanceof ReadableStream) {} else throw new TypeError("Response body must be of type ReadableStream.");
|
|
1328
1520
|
let init = value[2];
|
|
1329
1521
|
if (typeof init !== "object" || init === null) break;
|
|
@@ -1334,7 +1526,7 @@ var Evaluator = class Evaluator {
|
|
|
1334
1526
|
case "blob": {
|
|
1335
1527
|
if (value.length !== 3 || typeof value[1] !== "string") break;
|
|
1336
1528
|
let contentType = value[1];
|
|
1337
|
-
let content = this.evaluateImpl(value[2], parent, property);
|
|
1529
|
+
let content = this.evaluateImpl(value[2], parent, property, depth + 1);
|
|
1338
1530
|
if (!(content instanceof ReadableStream)) throw new TypeError("Blob content must be serialized as a ReadableStream.");
|
|
1339
1531
|
let promise = streamToBlobPromise(content, contentType);
|
|
1340
1532
|
this.promises.push({
|
|
@@ -1375,7 +1567,7 @@ var Evaluator = class Evaluator {
|
|
|
1375
1567
|
if (value.length == 3) return addStub(hook.get(path));
|
|
1376
1568
|
let args = value[3];
|
|
1377
1569
|
if (!(args instanceof Array)) break;
|
|
1378
|
-
args = new Evaluator(this.importer).
|
|
1570
|
+
args = new Evaluator(this.importer).evaluateWithDepth([args], depth);
|
|
1379
1571
|
return addStub(hook.call(path, args));
|
|
1380
1572
|
}
|
|
1381
1573
|
case "remap": {
|
|
@@ -1441,9 +1633,9 @@ var Evaluator = class Evaluator {
|
|
|
1441
1633
|
} else if (value instanceof Object) {
|
|
1442
1634
|
let result = value;
|
|
1443
1635
|
for (let key in result) if (key in Object.prototype || key === "toJSON") {
|
|
1444
|
-
this.evaluateImpl(result[key], result, key);
|
|
1636
|
+
this.evaluateImpl(result[key], result, key, depth + 1);
|
|
1445
1637
|
delete result[key];
|
|
1446
|
-
} else result[key] = this.evaluateImpl(result[key], result, key);
|
|
1638
|
+
} else result[key] = this.evaluateImpl(result[key], result, key, depth + 1);
|
|
1447
1639
|
return result;
|
|
1448
1640
|
} else return value;
|
|
1449
1641
|
}
|
|
@@ -1459,6 +1651,47 @@ function deserialize(value) {
|
|
|
1459
1651
|
|
|
1460
1652
|
//#endregion
|
|
1461
1653
|
//#region src/rpc.ts
|
|
1654
|
+
const ESTIMATED_OBJECT_OVERHEAD = 16;
|
|
1655
|
+
const ESTIMATED_ENTRY_OVERHEAD = 8;
|
|
1656
|
+
const ESTIMATED_BINARY_OVERHEAD = 16;
|
|
1657
|
+
const MAX_ESTIMATE_DEPTH = 64;
|
|
1658
|
+
function estimateStringSize(value) {
|
|
1659
|
+
return 2 + value.length * 3;
|
|
1660
|
+
}
|
|
1661
|
+
function estimateEncodedSize(value, seen, depth = 0) {
|
|
1662
|
+
if (depth >= MAX_ESTIMATE_DEPTH) return ESTIMATED_ENTRY_OVERHEAD;
|
|
1663
|
+
switch (typeof value) {
|
|
1664
|
+
case "string": return estimateStringSize(value);
|
|
1665
|
+
case "number": return 16;
|
|
1666
|
+
case "bigint": return 16;
|
|
1667
|
+
case "boolean": return 8;
|
|
1668
|
+
case "undefined": return 16;
|
|
1669
|
+
case "object": {
|
|
1670
|
+
if (value === null) return 8;
|
|
1671
|
+
if (ArrayBuffer.isView(value)) return ESTIMATED_BINARY_OVERHEAD + value.byteLength;
|
|
1672
|
+
if (value instanceof ArrayBuffer) return ESTIMATED_BINARY_OVERHEAD + value.byteLength;
|
|
1673
|
+
if (typeof Blob !== "undefined" && value instanceof Blob) return ESTIMATED_BINARY_OVERHEAD + value.size;
|
|
1674
|
+
if (value instanceof Date) return 16;
|
|
1675
|
+
seen ??= /* @__PURE__ */ new WeakSet();
|
|
1676
|
+
if (seen.has(value)) return ESTIMATED_ENTRY_OVERHEAD;
|
|
1677
|
+
seen.add(value);
|
|
1678
|
+
if (value instanceof Array) {
|
|
1679
|
+
let size = ESTIMATED_OBJECT_OVERHEAD;
|
|
1680
|
+
for (let item of value) size += ESTIMATED_ENTRY_OVERHEAD + estimateEncodedSize(item, seen, depth + 1);
|
|
1681
|
+
return size;
|
|
1682
|
+
}
|
|
1683
|
+
if (value instanceof Error) {
|
|
1684
|
+
let size = ESTIMATED_OBJECT_OVERHEAD + estimateStringSize(value.name) + estimateStringSize(value.message) + estimateStringSize(value.stack ?? "");
|
|
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
|
+
let size = ESTIMATED_OBJECT_OVERHEAD;
|
|
1689
|
+
for (let key of Object.keys(value)) size += ESTIMATED_ENTRY_OVERHEAD + estimateStringSize(key) + estimateEncodedSize(value[key], seen, depth + 1);
|
|
1690
|
+
return size;
|
|
1691
|
+
}
|
|
1692
|
+
default: return 16;
|
|
1693
|
+
}
|
|
1694
|
+
}
|
|
1462
1695
|
var ImportTableEntry = class {
|
|
1463
1696
|
session;
|
|
1464
1697
|
importId;
|
|
@@ -1624,9 +1857,24 @@ var RpcSessionImpl = class {
|
|
|
1624
1857
|
onBatchDone;
|
|
1625
1858
|
pullCount = 0;
|
|
1626
1859
|
onBrokenCallbacks = [];
|
|
1860
|
+
encodingLevel;
|
|
1861
|
+
limits;
|
|
1627
1862
|
constructor(transport, mainHook, options) {
|
|
1628
1863
|
this.transport = transport;
|
|
1629
1864
|
this.options = options;
|
|
1865
|
+
let level = "string";
|
|
1866
|
+
if ("encodingLevel" in transport) {
|
|
1867
|
+
let raw = transport.encodingLevel;
|
|
1868
|
+
if (raw !== void 0) {
|
|
1869
|
+
if (raw !== "string" && raw !== "jsonCompatible" && raw !== "jsonCompatibleWithBytes" && raw !== "structuredClonable") throw new TypeError(`Unknown transport encodingLevel: ${String(raw)}`);
|
|
1870
|
+
level = raw;
|
|
1871
|
+
}
|
|
1872
|
+
}
|
|
1873
|
+
this.encodingLevel = level;
|
|
1874
|
+
this.limits = {
|
|
1875
|
+
...DEFAULT_LIMITS,
|
|
1876
|
+
...options.limits
|
|
1877
|
+
};
|
|
1630
1878
|
this.exports.push({
|
|
1631
1879
|
hook: mainHook,
|
|
1632
1880
|
refcount: 1
|
|
@@ -1707,7 +1955,7 @@ var RpcSessionImpl = class {
|
|
|
1707
1955
|
let autoRelease = exp.autoRelease;
|
|
1708
1956
|
++this.pullCount;
|
|
1709
1957
|
exp.pull = resolve().then((payload) => {
|
|
1710
|
-
let value = Devaluator.devaluate(payload.value, void 0, this, payload);
|
|
1958
|
+
let value = Devaluator.devaluate(payload.value, void 0, this, payload, this.encodingLevel);
|
|
1711
1959
|
this.send([
|
|
1712
1960
|
"resolve",
|
|
1713
1961
|
exportId,
|
|
@@ -1718,7 +1966,7 @@ var RpcSessionImpl = class {
|
|
|
1718
1966
|
this.send([
|
|
1719
1967
|
"reject",
|
|
1720
1968
|
exportId,
|
|
1721
|
-
Devaluator.devaluate(error, void 0, this)
|
|
1969
|
+
Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)
|
|
1722
1970
|
]);
|
|
1723
1971
|
if (autoRelease) this.releaseExport(exportId, 1);
|
|
1724
1972
|
}).catch((error) => {
|
|
@@ -1726,7 +1974,7 @@ var RpcSessionImpl = class {
|
|
|
1726
1974
|
this.send([
|
|
1727
1975
|
"reject",
|
|
1728
1976
|
exportId,
|
|
1729
|
-
Devaluator.devaluate(error, void 0, this)
|
|
1977
|
+
Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)
|
|
1730
1978
|
]);
|
|
1731
1979
|
if (autoRelease) this.releaseExport(exportId, 1);
|
|
1732
1980
|
} catch (error2) {
|
|
@@ -1769,6 +2017,9 @@ var RpcSessionImpl = class {
|
|
|
1769
2017
|
entry.pipeReadable = void 0;
|
|
1770
2018
|
return readable;
|
|
1771
2019
|
}
|
|
2020
|
+
getLimits() {
|
|
2021
|
+
return this.limits;
|
|
2022
|
+
}
|
|
1772
2023
|
createPipe(readable, readableHook) {
|
|
1773
2024
|
if (this.abortReason) throw this.abortReason;
|
|
1774
2025
|
this.send(["pipe"]);
|
|
@@ -1782,17 +2033,33 @@ var RpcSessionImpl = class {
|
|
|
1782
2033
|
}
|
|
1783
2034
|
send(msg) {
|
|
1784
2035
|
if (this.abortReason !== void 0) return 0;
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
msgText = JSON.stringify(msg);
|
|
1788
|
-
} catch (err) {
|
|
2036
|
+
if (this.encodingLevel === "string") {
|
|
2037
|
+
let msgText;
|
|
1789
2038
|
try {
|
|
1790
|
-
|
|
1791
|
-
} catch (
|
|
1792
|
-
|
|
2039
|
+
msgText = JSON.stringify(msg);
|
|
2040
|
+
} catch (err) {
|
|
2041
|
+
try {
|
|
2042
|
+
this.abort(err);
|
|
2043
|
+
} catch (err2) {}
|
|
2044
|
+
throw err;
|
|
2045
|
+
}
|
|
2046
|
+
try {
|
|
2047
|
+
let sent = this.transport.send(msgText);
|
|
2048
|
+
if (sent !== void 0 && typeof sent.catch === "function") sent.catch((err) => this.abort(err, false));
|
|
2049
|
+
} catch (err) {
|
|
2050
|
+
queueMicrotask(() => this.abort(err, false));
|
|
2051
|
+
}
|
|
2052
|
+
return msgText.length;
|
|
2053
|
+
} else try {
|
|
2054
|
+
let size = this.transport.send(msg);
|
|
2055
|
+
if (typeof size === "number") return size;
|
|
2056
|
+
let thenable = size;
|
|
2057
|
+
if (thenable && typeof thenable.then === "function") Promise.resolve(thenable).catch((err) => this.abort(err, false));
|
|
2058
|
+
return;
|
|
2059
|
+
} catch (err) {
|
|
2060
|
+
queueMicrotask(() => this.abort(err, false));
|
|
2061
|
+
return;
|
|
1793
2062
|
}
|
|
1794
|
-
this.transport.send(msgText).catch((err) => this.abort(err, false));
|
|
1795
|
-
return msgText.length;
|
|
1796
2063
|
}
|
|
1797
2064
|
sendCall(id, path, args) {
|
|
1798
2065
|
if (this.abortReason) throw this.abortReason;
|
|
@@ -1802,7 +2069,7 @@ var RpcSessionImpl = class {
|
|
|
1802
2069
|
path
|
|
1803
2070
|
];
|
|
1804
2071
|
if (args) {
|
|
1805
|
-
let devalue = Devaluator.devaluate(args.value, void 0, this, args);
|
|
2072
|
+
let devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
|
|
1806
2073
|
value.push(devalue[0]);
|
|
1807
2074
|
}
|
|
1808
2075
|
this.send(["push", value]);
|
|
@@ -1817,9 +2084,11 @@ var RpcSessionImpl = class {
|
|
|
1817
2084
|
id,
|
|
1818
2085
|
path
|
|
1819
2086
|
];
|
|
1820
|
-
let devalue = Devaluator.devaluate(args.value, void 0, this, args);
|
|
2087
|
+
let devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
|
|
1821
2088
|
value.push(devalue[0]);
|
|
1822
|
-
let
|
|
2089
|
+
let msg = ["stream", value];
|
|
2090
|
+
let size = this.send(msg);
|
|
2091
|
+
if (size === void 0) size = estimateEncodedSize(msg);
|
|
1823
2092
|
let importId = this.imports.length;
|
|
1824
2093
|
let entry = new ImportTableEntry(this, importId, true);
|
|
1825
2094
|
entry.remoteRefcount = 0;
|
|
@@ -1875,7 +2144,14 @@ var RpcSessionImpl = class {
|
|
|
1875
2144
|
this.cancelReadLoop?.(error);
|
|
1876
2145
|
this.cancelReadLoop = void 0;
|
|
1877
2146
|
if (trySendAbortMessage) try {
|
|
1878
|
-
|
|
2147
|
+
let abortMsg = ["abort", Devaluator.devaluate(error, void 0, this, void 0, this.encodingLevel)];
|
|
2148
|
+
if (this.encodingLevel === "string") {
|
|
2149
|
+
let sent = this.transport.send(JSON.stringify(abortMsg));
|
|
2150
|
+
if (sent !== void 0 && typeof sent.catch === "function") sent.catch((err) => {});
|
|
2151
|
+
} else {
|
|
2152
|
+
let result = this.transport.send(abortMsg);
|
|
2153
|
+
if (result && typeof result.then === "function") Promise.resolve(result).catch((err) => {});
|
|
2154
|
+
}
|
|
1879
2155
|
} catch (err) {}
|
|
1880
2156
|
if (error === void 0) error = "undefined";
|
|
1881
2157
|
this.abortReason = error;
|
|
@@ -1897,18 +2173,19 @@ var RpcSessionImpl = class {
|
|
|
1897
2173
|
while (!this.abortReason) {
|
|
1898
2174
|
let readCanceled = Promise.withResolvers();
|
|
1899
2175
|
this.cancelReadLoop = readCanceled.reject;
|
|
1900
|
-
let
|
|
2176
|
+
let raw;
|
|
1901
2177
|
try {
|
|
1902
|
-
|
|
2178
|
+
raw = await Promise.race([this.transport.receive(), readCanceled.promise]);
|
|
1903
2179
|
} finally {
|
|
1904
2180
|
if (this.cancelReadLoop === readCanceled.reject) this.cancelReadLoop = void 0;
|
|
1905
2181
|
}
|
|
1906
|
-
|
|
2182
|
+
if (this.encodingLevel === "string" && raw.length > this.limits.maxMessageSize) throw new TypeError(`Incoming message exceeds maximum size of ${this.limits.maxMessageSize} UTF-16 code units.`);
|
|
1907
2183
|
if (this.abortReason) break;
|
|
2184
|
+
let msg = this.encodingLevel === "string" ? JSON.parse(raw) : raw;
|
|
1908
2185
|
if (msg instanceof Array) switch (msg[0]) {
|
|
1909
2186
|
case "push":
|
|
1910
2187
|
if (msg.length > 1) {
|
|
1911
|
-
let hook = new PayloadStubHook(new Evaluator(this).evaluate(msg[1]));
|
|
2188
|
+
let hook = new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[1]));
|
|
1912
2189
|
hook.ignoreUnhandledRejections();
|
|
1913
2190
|
this.exports.push({
|
|
1914
2191
|
hook,
|
|
@@ -1919,7 +2196,7 @@ var RpcSessionImpl = class {
|
|
|
1919
2196
|
break;
|
|
1920
2197
|
case "stream":
|
|
1921
2198
|
if (msg.length > 1) {
|
|
1922
|
-
let hook = new PayloadStubHook(new Evaluator(this).evaluate(msg[1]));
|
|
2199
|
+
let hook = new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[1]));
|
|
1923
2200
|
hook.ignoreUnhandledRejections();
|
|
1924
2201
|
let exportId = this.exports.length;
|
|
1925
2202
|
this.exports.push({
|
|
@@ -1954,13 +2231,13 @@ var RpcSessionImpl = class {
|
|
|
1954
2231
|
let importId = msg[1];
|
|
1955
2232
|
if (typeof importId == "number" && msg.length > 2) {
|
|
1956
2233
|
let imp = this.imports[importId];
|
|
1957
|
-
if (imp) if (msg[0] == "resolve") imp.resolve(new PayloadStubHook(new Evaluator(this).evaluate(msg[2])));
|
|
2234
|
+
if (imp) if (msg[0] == "resolve") imp.resolve(new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[2])));
|
|
1958
2235
|
else {
|
|
1959
|
-
let payload = new Evaluator(this).evaluate(msg[2]);
|
|
2236
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[2]);
|
|
1960
2237
|
payload.dispose();
|
|
1961
2238
|
imp.resolve(new ErrorStubHook(payload.value));
|
|
1962
2239
|
}
|
|
1963
|
-
else if (msg[0] == "resolve") new Evaluator(this).evaluate(msg[2]).dispose();
|
|
2240
|
+
else if (msg[0] == "resolve") new Evaluator(this, this.encodingLevel).evaluate(msg[2]).dispose();
|
|
1964
2241
|
continue;
|
|
1965
2242
|
}
|
|
1966
2243
|
break;
|
|
@@ -1975,9 +2252,9 @@ var RpcSessionImpl = class {
|
|
|
1975
2252
|
break;
|
|
1976
2253
|
}
|
|
1977
2254
|
case "abort": {
|
|
1978
|
-
let payload = new Evaluator(this).evaluate(msg[1]);
|
|
2255
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[1]);
|
|
1979
2256
|
payload.dispose();
|
|
1980
|
-
this.abort(payload, false);
|
|
2257
|
+
this.abort(payload.value, false);
|
|
1981
2258
|
break;
|
|
1982
2259
|
}
|
|
1983
2260
|
}
|
|
@@ -2028,6 +2305,8 @@ var RpcSession$1 = class {
|
|
|
2028
2305
|
|
|
2029
2306
|
//#endregion
|
|
2030
2307
|
//#region src/websocket.ts
|
|
2308
|
+
/** Close-frame reason max UTF-8 bytes: 125 payload − 2-byte status (RFC 6455 §5.5). */
|
|
2309
|
+
const MAX_CLOSE_REASON_BYTES = 123;
|
|
2031
2310
|
function newWebSocketRpcSession$1(webSocket, localMain, options) {
|
|
2032
2311
|
if (typeof webSocket === "string") webSocket = new WebSocket(webSocket);
|
|
2033
2312
|
return new RpcSession$1(new WebSocketTransport(webSocket), localMain, options).getRemoteMain();
|
|
@@ -2047,9 +2326,14 @@ function newWorkersWebSocketRpcResponse(request, localMain, options) {
|
|
|
2047
2326
|
webSocket: pair[1]
|
|
2048
2327
|
});
|
|
2049
2328
|
}
|
|
2329
|
+
/**
|
|
2330
|
+
* Generic WebSocket transport. Default `T = string` is backward-compatible and satisfies
|
|
2331
|
+
* `RpcTransport`. Use `T = ArrayBuffer` as a building block for binary transports.
|
|
2332
|
+
*/
|
|
2050
2333
|
var WebSocketTransport = class {
|
|
2051
2334
|
constructor(webSocket) {
|
|
2052
2335
|
this.#webSocket = webSocket;
|
|
2336
|
+
webSocket.binaryType = "arraybuffer";
|
|
2053
2337
|
if (webSocket.readyState === WebSocket.CONNECTING) {
|
|
2054
2338
|
this.#sendQueue = [];
|
|
2055
2339
|
webSocket.addEventListener("open", (event) => {
|
|
@@ -2062,12 +2346,12 @@ var WebSocketTransport = class {
|
|
|
2062
2346
|
});
|
|
2063
2347
|
}
|
|
2064
2348
|
webSocket.addEventListener("message", (event) => {
|
|
2065
|
-
if (this.#error) {} else if (typeof event.data === "string") if (this.#receiveResolver) {
|
|
2349
|
+
if (this.#error) {} else if (typeof event.data === "string" || event.data instanceof ArrayBuffer) if (this.#receiveResolver) {
|
|
2066
2350
|
this.#receiveResolver(event.data);
|
|
2067
2351
|
this.#receiveResolver = void 0;
|
|
2068
2352
|
this.#receiveRejecter = void 0;
|
|
2069
2353
|
} else this.#receiveQueue.push(event.data);
|
|
2070
|
-
else this.#receivedError(/* @__PURE__ */ new TypeError("Received
|
|
2354
|
+
else this.#receivedError(/* @__PURE__ */ new TypeError("Received unexpected message type from WebSocket."));
|
|
2071
2355
|
});
|
|
2072
2356
|
webSocket.addEventListener("close", (event) => {
|
|
2073
2357
|
this.#receivedError(/* @__PURE__ */ new Error(`Peer closed WebSocket: ${event.code} ${event.reason}`));
|
|
@@ -2082,13 +2366,13 @@ var WebSocketTransport = class {
|
|
|
2082
2366
|
#receiveRejecter;
|
|
2083
2367
|
#receiveQueue = [];
|
|
2084
2368
|
#error;
|
|
2085
|
-
|
|
2369
|
+
send(message) {
|
|
2086
2370
|
if (this.#sendQueue === void 0) this.#webSocket.send(message);
|
|
2087
2371
|
else this.#sendQueue.push(message);
|
|
2088
2372
|
}
|
|
2089
|
-
|
|
2090
|
-
if (this.#receiveQueue.length > 0) return this.#receiveQueue.shift();
|
|
2091
|
-
else if (this.#error)
|
|
2373
|
+
receive() {
|
|
2374
|
+
if (this.#receiveQueue.length > 0) return Promise.resolve(this.#receiveQueue.shift());
|
|
2375
|
+
else if (this.#error) return Promise.reject(this.#error);
|
|
2092
2376
|
else return new Promise((resolve, reject) => {
|
|
2093
2377
|
this.#receiveResolver = resolve;
|
|
2094
2378
|
this.#receiveRejecter = reject;
|
|
@@ -2098,6 +2382,8 @@ var WebSocketTransport = class {
|
|
|
2098
2382
|
let message;
|
|
2099
2383
|
if (reason instanceof Error) message = reason.message;
|
|
2100
2384
|
else message = `${reason}`;
|
|
2385
|
+
let reasonBytes = new TextEncoder().encode(message);
|
|
2386
|
+
if (reasonBytes.length > 123) message = new TextDecoder().decode(reasonBytes.subarray(0, 123), { stream: true });
|
|
2101
2387
|
this.#webSocket.close(3e3, message);
|
|
2102
2388
|
if (!this.#error) this.#error = reason;
|
|
2103
2389
|
}
|
|
@@ -2115,6 +2401,7 @@ var WebSocketTransport = class {
|
|
|
2115
2401
|
|
|
2116
2402
|
//#endregion
|
|
2117
2403
|
//#region src/batch.ts
|
|
2404
|
+
const yieldToMacrotask = typeof setImmediate === "function" ? () => new Promise((resolve) => setImmediate(resolve)) : () => new Promise((resolve) => setTimeout(resolve, 0));
|
|
2118
2405
|
var BatchClientTransport = class {
|
|
2119
2406
|
constructor(sendBatch) {
|
|
2120
2407
|
this.#promise = this.#scheduleBatch(sendBatch);
|
|
@@ -2123,7 +2410,7 @@ var BatchClientTransport = class {
|
|
|
2123
2410
|
#aborted;
|
|
2124
2411
|
#batchToSend = [];
|
|
2125
2412
|
#batchToReceive = null;
|
|
2126
|
-
|
|
2413
|
+
send(message) {
|
|
2127
2414
|
if (this.#batchToSend !== null) this.#batchToSend.push(message);
|
|
2128
2415
|
}
|
|
2129
2416
|
async receive() {
|
|
@@ -2136,7 +2423,7 @@ var BatchClientTransport = class {
|
|
|
2136
2423
|
this.#aborted = reason;
|
|
2137
2424
|
}
|
|
2138
2425
|
async #scheduleBatch(sendBatch) {
|
|
2139
|
-
await
|
|
2426
|
+
await yieldToMacrotask();
|
|
2140
2427
|
if (this.#aborted !== void 0) throw this.#aborted;
|
|
2141
2428
|
let batch = this.#batchToSend;
|
|
2142
2429
|
this.#batchToSend = null;
|
|
@@ -2165,7 +2452,7 @@ var BatchServerTransport = class {
|
|
|
2165
2452
|
#batchToSend = [];
|
|
2166
2453
|
#batchToReceive;
|
|
2167
2454
|
#allReceived = Promise.withResolvers();
|
|
2168
|
-
|
|
2455
|
+
send(message) {
|
|
2169
2456
|
this.#batchToSend.push(message);
|
|
2170
2457
|
}
|
|
2171
2458
|
async receive() {
|
|
@@ -2214,7 +2501,11 @@ async function newHttpBatchRpcResponse(request, localMain, options) {
|
|
|
2214
2501
|
* @param options Optional RPC session options. You can also pass headers to set on the response.
|
|
2215
2502
|
*/
|
|
2216
2503
|
async function nodeHttpBatchRpcResponse(request, response, localMain, options) {
|
|
2217
|
-
if (request.method !== "POST")
|
|
2504
|
+
if (request.method !== "POST") {
|
|
2505
|
+
response.writeHead(405, "This endpoint only accepts POST requests.", options?.headers);
|
|
2506
|
+
response.end();
|
|
2507
|
+
return;
|
|
2508
|
+
}
|
|
2218
2509
|
let body = await new Promise((resolve, reject) => {
|
|
2219
2510
|
let chunks = [];
|
|
2220
2511
|
request.on("data", (chunk) => {
|
|
@@ -2239,17 +2530,17 @@ function newMessagePortRpcSession$1(port, localMain, options) {
|
|
|
2239
2530
|
return new RpcSession$1(new MessagePortTransport(port), localMain, options).getRemoteMain();
|
|
2240
2531
|
}
|
|
2241
2532
|
var MessagePortTransport = class {
|
|
2533
|
+
encodingLevel = "structuredClonable";
|
|
2242
2534
|
constructor(port) {
|
|
2243
2535
|
this.#port = port;
|
|
2244
2536
|
port.start();
|
|
2245
2537
|
port.addEventListener("message", (event) => {
|
|
2246
2538
|
if (this.#error) {} else if (event.data === null) this.#receivedError(/* @__PURE__ */ new Error("Peer closed MessagePort connection."));
|
|
2247
|
-
else if (
|
|
2539
|
+
else if (this.#receiveResolver) {
|
|
2248
2540
|
this.#receiveResolver(event.data);
|
|
2249
2541
|
this.#receiveResolver = void 0;
|
|
2250
2542
|
this.#receiveRejecter = void 0;
|
|
2251
2543
|
} else this.#receiveQueue.push(event.data);
|
|
2252
|
-
else this.#receivedError(/* @__PURE__ */ new TypeError("Received non-string message from MessagePort."));
|
|
2253
2544
|
});
|
|
2254
2545
|
port.addEventListener("messageerror", (event) => {
|
|
2255
2546
|
this.#receivedError(/* @__PURE__ */ new Error("MessagePort message error."));
|
|
@@ -2260,7 +2551,7 @@ var MessagePortTransport = class {
|
|
|
2260
2551
|
#receiveRejecter;
|
|
2261
2552
|
#receiveQueue = [];
|
|
2262
2553
|
#error;
|
|
2263
|
-
|
|
2554
|
+
send(message) {
|
|
2264
2555
|
if (this.#error) throw this.#error;
|
|
2265
2556
|
this.#port.postMessage(message);
|
|
2266
2557
|
}
|
|
@@ -2479,6 +2770,9 @@ var MapApplicator = class {
|
|
|
2479
2770
|
getPipeReadable(exportId) {
|
|
2480
2771
|
throw new Error("A mapper function cannot use pipe readables.");
|
|
2481
2772
|
}
|
|
2773
|
+
getLimits() {
|
|
2774
|
+
return DEFAULT_LIMITS;
|
|
2775
|
+
}
|
|
2482
2776
|
};
|
|
2483
2777
|
function applyMapToElement(input, parent, owner, captures, instructions) {
|
|
2484
2778
|
let mapper = new MapApplicator(captures, new PayloadStubHook(RpcPayload.deepCopyFrom(input, parent, owner)));
|
|
@@ -2539,8 +2833,7 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
|
|
|
2539
2833
|
throw new Error(`Unknown WritableStream method: ${method}`);
|
|
2540
2834
|
}
|
|
2541
2835
|
if (method === "close" || method === "abort") state.closed = true;
|
|
2542
|
-
|
|
2543
|
-
return new PromiseStubHook(args.deliverCall(func, state.writer).then((payload) => new PayloadStubHook(payload)));
|
|
2836
|
+
return new PromiseStubHook((method === "write" ? args.deliverStreamWrite(state.writer) : args.deliverCall(state.writer[method], state.writer)).then((payload) => new PayloadStubHook(payload)));
|
|
2544
2837
|
} catch (err) {
|
|
2545
2838
|
return new ErrorStubHook(err);
|
|
2546
2839
|
}
|
|
@@ -2817,15 +3110,15 @@ let newMessagePortRpcSession = newMessagePortRpcSession$1;
|
|
|
2817
3110
|
* credentials as parameters and returns the authorized API), then cross-origin requests should
|
|
2818
3111
|
* be safe.
|
|
2819
3112
|
*/
|
|
2820
|
-
async function newWorkersRpcResponse(request, localMain) {
|
|
3113
|
+
async function newWorkersRpcResponse(request, localMain, options) {
|
|
2821
3114
|
if (request.method === "POST") {
|
|
2822
|
-
let response = await newHttpBatchRpcResponse(request, localMain);
|
|
3115
|
+
let response = await newHttpBatchRpcResponse(request, localMain, options);
|
|
2823
3116
|
response.headers.set("Access-Control-Allow-Origin", "*");
|
|
2824
3117
|
return response;
|
|
2825
|
-
} else if (request.headers.get("Upgrade")?.toLowerCase() === "websocket") return newWorkersWebSocketRpcResponse(request, localMain);
|
|
3118
|
+
} else if (request.headers.get("Upgrade")?.toLowerCase() === "websocket") return newWorkersWebSocketRpcResponse(request, localMain, options);
|
|
2826
3119
|
else return new Response("This endpoint only accepts POST or WebSocket requests.", { status: 400 });
|
|
2827
3120
|
}
|
|
2828
3121
|
|
|
2829
3122
|
//#endregion
|
|
2830
|
-
export { RpcPromise, RpcSession, RpcStub, RpcTarget, deserialize, newHttpBatchRpcResponse, newHttpBatchRpcSession, newMessagePortRpcSession, newWebSocketRpcSession, newWorkersRpcResponse, newWorkersWebSocketRpcResponse, nodeHttpBatchRpcResponse, serialize };
|
|
3123
|
+
export { DEFAULT_LIMITS, DEFAULT_MAX_DEPTH, RpcPromise, RpcSession, RpcStub, RpcTarget, WebSocketTransport, deserialize, newHttpBatchRpcResponse, newHttpBatchRpcSession, newMessagePortRpcSession, newWebSocketRpcSession, newWorkersRpcResponse, newWorkersWebSocketRpcResponse, nodeHttpBatchRpcResponse, serialize };
|
|
2831
3124
|
//# sourceMappingURL=index-workers.js.map
|