capnweb 0.9.1 → 0.11.0
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 +38 -2
- package/dist/index-bun.cjs +236 -33
- package/dist/index-bun.cjs.map +1 -1
- package/dist/index-bun.d.cts +34 -7
- package/dist/index-bun.d.ts +34 -7
- package/dist/index-bun.js +235 -34
- package/dist/index-bun.js.map +1 -1
- package/dist/index-workers.cjs +236 -33
- package/dist/index-workers.cjs.map +1 -1
- package/dist/index-workers.d.cts +31 -7
- package/dist/index-workers.d.ts +31 -7
- package/dist/index-workers.js +235 -34
- package/dist/index-workers.js.map +1 -1
- package/dist/index.cjs +236 -33
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +31 -7
- package/dist/index.d.ts +31 -7
- package/dist/index.js +235 -34
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index-workers.js
CHANGED
|
@@ -47,9 +47,22 @@ function typeForRpc(value) {
|
|
|
47
47
|
case Array.prototype: return "array";
|
|
48
48
|
case Date.prototype: return "date";
|
|
49
49
|
case Uint8Array.prototype:
|
|
50
|
-
case BUFFER_PROTOTYPE:
|
|
50
|
+
case BUFFER_PROTOTYPE:
|
|
51
|
+
case ArrayBuffer.prototype:
|
|
52
|
+
case DataView.prototype:
|
|
53
|
+
case Int8Array.prototype:
|
|
54
|
+
case Uint8ClampedArray.prototype:
|
|
55
|
+
case Int16Array.prototype:
|
|
56
|
+
case Uint16Array.prototype:
|
|
57
|
+
case Int32Array.prototype:
|
|
58
|
+
case Uint32Array.prototype:
|
|
59
|
+
case BigInt64Array.prototype:
|
|
60
|
+
case BigUint64Array.prototype:
|
|
61
|
+
case Float32Array.prototype:
|
|
62
|
+
case Float64Array.prototype: return "bytes";
|
|
51
63
|
case WritableStream.prototype: return "writable";
|
|
52
64
|
case ReadableStream.prototype: return "readable";
|
|
65
|
+
case URL.prototype: return "url";
|
|
53
66
|
case Headers.prototype: return "headers";
|
|
54
67
|
case Request.prototype: return "request";
|
|
55
68
|
case Response.prototype: return "response";
|
|
@@ -384,6 +397,7 @@ var RpcPayload = class RpcPayload {
|
|
|
384
397
|
case "date":
|
|
385
398
|
case "bytes":
|
|
386
399
|
case "blob":
|
|
400
|
+
case "url":
|
|
387
401
|
case "error":
|
|
388
402
|
case "undefined": return value;
|
|
389
403
|
case "array": {
|
|
@@ -517,6 +531,30 @@ var RpcPayload = class RpcPayload {
|
|
|
517
531
|
this.dispose();
|
|
518
532
|
}
|
|
519
533
|
}
|
|
534
|
+
async deliverStreamWrite(writer) {
|
|
535
|
+
try {
|
|
536
|
+
let promises = [];
|
|
537
|
+
this.deliverTo(this, "value", promises);
|
|
538
|
+
if (promises.length > 0) await Promise.all(promises);
|
|
539
|
+
let chunk = this.value[0];
|
|
540
|
+
if ((this.hooks.length > 0 || this.promises.length > 0) && chunk instanceof Object) {
|
|
541
|
+
if (!(Symbol.dispose in chunk)) Object.defineProperty(chunk, Symbol.dispose, {
|
|
542
|
+
value: () => this.dispose(),
|
|
543
|
+
writable: true,
|
|
544
|
+
enumerable: false,
|
|
545
|
+
configurable: true
|
|
546
|
+
});
|
|
547
|
+
await writer.write(chunk);
|
|
548
|
+
return RpcPayload.fromAppReturn(void 0);
|
|
549
|
+
}
|
|
550
|
+
await writer.write(chunk);
|
|
551
|
+
this.dispose();
|
|
552
|
+
return RpcPayload.fromAppReturn(void 0);
|
|
553
|
+
} catch (err) {
|
|
554
|
+
this.dispose();
|
|
555
|
+
throw err;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
520
558
|
async deliverResolve() {
|
|
521
559
|
try {
|
|
522
560
|
let promises = [];
|
|
@@ -557,6 +595,7 @@ var RpcPayload = class RpcPayload {
|
|
|
557
595
|
case "bytes":
|
|
558
596
|
case "blob":
|
|
559
597
|
case "date":
|
|
598
|
+
case "url":
|
|
560
599
|
case "error":
|
|
561
600
|
case "undefined": return;
|
|
562
601
|
case "array": {
|
|
@@ -639,6 +678,7 @@ var RpcPayload = class RpcPayload {
|
|
|
639
678
|
case "rpc-target":
|
|
640
679
|
case "writable":
|
|
641
680
|
case "readable":
|
|
681
|
+
case "url":
|
|
642
682
|
case "headers":
|
|
643
683
|
case "request":
|
|
644
684
|
case "response": return;
|
|
@@ -708,6 +748,7 @@ function followPath(value, parent, path, owner) {
|
|
|
708
748
|
case "blob":
|
|
709
749
|
case "date":
|
|
710
750
|
case "error":
|
|
751
|
+
case "url":
|
|
711
752
|
case "headers":
|
|
712
753
|
case "request":
|
|
713
754
|
case "response":
|
|
@@ -935,6 +976,77 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
|
|
|
935
976
|
|
|
936
977
|
//#endregion
|
|
937
978
|
//#region src/serialize.ts
|
|
979
|
+
const DEFAULT_MAX_DEPTH = 256;
|
|
980
|
+
const DEFAULT_LIMITS = {
|
|
981
|
+
maxBigIntDigits: 16384,
|
|
982
|
+
maxDepth: 256,
|
|
983
|
+
maxMessageSize: 32 * 1024 * 1024
|
|
984
|
+
};
|
|
985
|
+
const NATIVE_LITTLE_ENDIAN = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
|
|
986
|
+
const BYTE_CONTAINER_TYPE_NAMES = [
|
|
987
|
+
"ArrayBuffer",
|
|
988
|
+
"DataView",
|
|
989
|
+
"Int8Array",
|
|
990
|
+
"Uint8Array",
|
|
991
|
+
"Uint8ClampedArray",
|
|
992
|
+
"Int16Array",
|
|
993
|
+
"Uint16Array",
|
|
994
|
+
"Int32Array",
|
|
995
|
+
"Uint32Array",
|
|
996
|
+
"BigInt64Array",
|
|
997
|
+
"BigUint64Array",
|
|
998
|
+
"Float32Array",
|
|
999
|
+
"Float64Array"
|
|
1000
|
+
];
|
|
1001
|
+
function isValidByteContainerName(value) {
|
|
1002
|
+
return BYTE_CONTAINER_TYPE_NAMES.includes(value);
|
|
1003
|
+
}
|
|
1004
|
+
const TYPED_ARRAY_ELEMENT_SIZE = {
|
|
1005
|
+
ArrayBuffer: void 0,
|
|
1006
|
+
DataView: void 0,
|
|
1007
|
+
Int8Array: void 0,
|
|
1008
|
+
Uint8Array: void 0,
|
|
1009
|
+
Uint8ClampedArray: void 0,
|
|
1010
|
+
Int16Array: 2,
|
|
1011
|
+
Uint16Array: 2,
|
|
1012
|
+
Int32Array: 4,
|
|
1013
|
+
Uint32Array: 4,
|
|
1014
|
+
BigInt64Array: 8,
|
|
1015
|
+
BigUint64Array: 8,
|
|
1016
|
+
Float32Array: 4,
|
|
1017
|
+
Float64Array: 8
|
|
1018
|
+
};
|
|
1019
|
+
const BYTE_CONTAINER_PROTOTYPES = {
|
|
1020
|
+
ArrayBuffer: ArrayBuffer.prototype,
|
|
1021
|
+
DataView: DataView.prototype,
|
|
1022
|
+
Int8Array: Int8Array.prototype,
|
|
1023
|
+
Uint8ClampedArray: Uint8ClampedArray.prototype,
|
|
1024
|
+
Int16Array: Int16Array.prototype,
|
|
1025
|
+
Uint16Array: Uint16Array.prototype,
|
|
1026
|
+
Int32Array: Int32Array.prototype,
|
|
1027
|
+
Uint32Array: Uint32Array.prototype,
|
|
1028
|
+
BigInt64Array: BigInt64Array.prototype,
|
|
1029
|
+
BigUint64Array: BigUint64Array.prototype,
|
|
1030
|
+
Float32Array: Float32Array.prototype,
|
|
1031
|
+
Float64Array: Float64Array.prototype
|
|
1032
|
+
};
|
|
1033
|
+
const BYTE_CONTAINER_TYPE_BY_PROTOTYPE = /* @__PURE__ */ new Map();
|
|
1034
|
+
for (let type of Object.keys(BYTE_CONTAINER_PROTOTYPES)) BYTE_CONTAINER_TYPE_BY_PROTOTYPE.set(BYTE_CONTAINER_PROTOTYPES[type], type);
|
|
1035
|
+
function swapByteOrder(bytes, elementSize) {
|
|
1036
|
+
if (elementSize !== 2 && elementSize !== 4 && elementSize !== 8) throw new RangeError(`Unsupported element size: ${elementSize}`);
|
|
1037
|
+
let view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
1038
|
+
for (let offset = 0; offset < bytes.byteLength; offset += elementSize) switch (elementSize) {
|
|
1039
|
+
case 2:
|
|
1040
|
+
view.setUint16(offset, view.getUint16(offset, false), true);
|
|
1041
|
+
break;
|
|
1042
|
+
case 4:
|
|
1043
|
+
view.setUint32(offset, view.getUint32(offset, false), true);
|
|
1044
|
+
break;
|
|
1045
|
+
case 8:
|
|
1046
|
+
view.setBigUint64(offset, view.getBigUint64(offset, false), true);
|
|
1047
|
+
break;
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
938
1050
|
var NullExporter = class {
|
|
939
1051
|
exportStub(stub) {
|
|
940
1052
|
throw new Error("Cannot serialize RPC stubs without an RPC session.");
|
|
@@ -955,6 +1067,7 @@ async function streamToBlob(stream, type) {
|
|
|
955
1067
|
return b.type === type ? b : b.slice(0, b.size, type);
|
|
956
1068
|
}
|
|
957
1069
|
const ERROR_TYPES = {
|
|
1070
|
+
__proto__: null,
|
|
958
1071
|
Error,
|
|
959
1072
|
EvalError,
|
|
960
1073
|
RangeError,
|
|
@@ -986,7 +1099,7 @@ var Devaluator = class Devaluator {
|
|
|
986
1099
|
}
|
|
987
1100
|
exports;
|
|
988
1101
|
devaluateImpl(value, parent, depth) {
|
|
989
|
-
if (depth >=
|
|
1102
|
+
if (depth >= 256) throw new Error("Serialization exceeded maximum allowed depth. (Does the message contain cycles?)");
|
|
990
1103
|
switch (typeForRpc(value)) {
|
|
991
1104
|
case "unsupported": {
|
|
992
1105
|
let msg;
|
|
@@ -1025,18 +1138,40 @@ var Devaluator = class Devaluator {
|
|
|
1025
1138
|
return ["date", Number.isNaN(time) ? null : time];
|
|
1026
1139
|
}
|
|
1027
1140
|
case "bytes": {
|
|
1028
|
-
let
|
|
1029
|
-
|
|
1030
|
-
if (
|
|
1141
|
+
let alternateTypeName = BYTE_CONTAINER_TYPE_BY_PROTOTYPE.get(Object.getPrototypeOf(value));
|
|
1142
|
+
let bytes;
|
|
1143
|
+
if (alternateTypeName === "ArrayBuffer") bytes = new Uint8Array(value);
|
|
1144
|
+
else if (alternateTypeName === void 0) bytes = value;
|
|
1145
|
+
else {
|
|
1146
|
+
let view = value;
|
|
1147
|
+
bytes = new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
|
|
1148
|
+
let elementSize = TYPED_ARRAY_ELEMENT_SIZE[alternateTypeName];
|
|
1149
|
+
if (!NATIVE_LITTLE_ENDIAN && elementSize) {
|
|
1150
|
+
bytes = bytes.slice();
|
|
1151
|
+
swapByteOrder(bytes, elementSize);
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
if (this.encodingLevel === "structuredClonable" || this.encodingLevel === "jsonCompatibleWithBytes") return alternateTypeName === void 0 ? ["bytes", bytes] : [
|
|
1155
|
+
"bytes",
|
|
1156
|
+
bytes,
|
|
1157
|
+
alternateTypeName
|
|
1158
|
+
];
|
|
1031
1159
|
let b64;
|
|
1032
|
-
if (
|
|
1160
|
+
if (bytes.toBase64) b64 = bytes.toBase64({ omitPadding: true });
|
|
1161
|
+
else if (typeof Buffer !== "undefined") b64 = (bytes instanceof Buffer ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)).toString("base64");
|
|
1033
1162
|
else {
|
|
1034
1163
|
let binary = "";
|
|
1035
1164
|
for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]);
|
|
1036
1165
|
b64 = btoa(binary);
|
|
1037
1166
|
}
|
|
1038
|
-
|
|
1167
|
+
b64 = b64.replace(/=+$/, "");
|
|
1168
|
+
return alternateTypeName === void 0 ? ["bytes", b64] : [
|
|
1169
|
+
"bytes",
|
|
1170
|
+
b64,
|
|
1171
|
+
alternateTypeName
|
|
1172
|
+
];
|
|
1039
1173
|
}
|
|
1174
|
+
case "url": return ["url", value.href];
|
|
1040
1175
|
case "headers": return ["headers", [...value]];
|
|
1041
1176
|
case "request": {
|
|
1042
1177
|
let req = value;
|
|
@@ -1220,6 +1355,9 @@ var NullImporter = class {
|
|
|
1220
1355
|
getPipeReadable(exportId) {
|
|
1221
1356
|
throw new Error("Cannot retrieve pipe readable without an RPC session.");
|
|
1222
1357
|
}
|
|
1358
|
+
getLimits() {
|
|
1359
|
+
return DEFAULT_LIMITS;
|
|
1360
|
+
}
|
|
1223
1361
|
};
|
|
1224
1362
|
const NULL_IMPORTER = new NullImporter();
|
|
1225
1363
|
function fixBrokenRequestBody(request, body) {
|
|
@@ -1237,16 +1375,21 @@ function streamToBlobPromise(stream, type) {
|
|
|
1237
1375
|
var Evaluator = class Evaluator {
|
|
1238
1376
|
importer;
|
|
1239
1377
|
encodingLevel;
|
|
1378
|
+
limits;
|
|
1240
1379
|
constructor(importer, encodingLevel = "string") {
|
|
1241
1380
|
this.importer = importer;
|
|
1242
1381
|
this.encodingLevel = encodingLevel;
|
|
1382
|
+
this.limits = importer.getLimits();
|
|
1243
1383
|
}
|
|
1244
1384
|
hooks = [];
|
|
1245
1385
|
promises = [];
|
|
1246
1386
|
evaluate(value) {
|
|
1387
|
+
return this.evaluateWithDepth(value, 0);
|
|
1388
|
+
}
|
|
1389
|
+
evaluateWithDepth(value, depth) {
|
|
1247
1390
|
let payload = RpcPayload.forEvaluate(this.hooks, this.promises);
|
|
1248
1391
|
try {
|
|
1249
|
-
payload.value = this.evaluateImpl(value, payload, "value");
|
|
1392
|
+
payload.value = this.evaluateImpl(value, payload, "value", depth);
|
|
1250
1393
|
return payload;
|
|
1251
1394
|
} catch (err) {
|
|
1252
1395
|
payload.dispose();
|
|
@@ -1256,35 +1399,70 @@ var Evaluator = class Evaluator {
|
|
|
1256
1399
|
evaluateCopy(value) {
|
|
1257
1400
|
return this.evaluate(structuredClone(value));
|
|
1258
1401
|
}
|
|
1259
|
-
evaluateImpl(value, parent, property) {
|
|
1402
|
+
evaluateImpl(value, parent, property, depth) {
|
|
1403
|
+
let maxDepth = this.limits.maxDepth;
|
|
1404
|
+
if (depth >= maxDepth) throw new TypeError(`Deserialization exceeded maximum allowed message depth of ${maxDepth}.`);
|
|
1260
1405
|
if (this.encodingLevel === "structuredClonable") {
|
|
1261
1406
|
if (value instanceof Date || typeof value === "bigint") return value;
|
|
1262
1407
|
}
|
|
1263
1408
|
if (value instanceof Array) {
|
|
1264
1409
|
if (value.length == 1 && value[0] instanceof Array) {
|
|
1265
1410
|
let result = value[0];
|
|
1266
|
-
for (let i = 0; i < result.length; i++) result[i] = this.evaluateImpl(result[i], result, i);
|
|
1411
|
+
for (let i = 0; i < result.length; i++) result[i] = this.evaluateImpl(result[i], result, i, depth + 1);
|
|
1267
1412
|
return result;
|
|
1268
1413
|
} else switch (value[0]) {
|
|
1269
1414
|
case "bigint":
|
|
1270
|
-
if (typeof value[1] == "string")
|
|
1415
|
+
if (typeof value[1] == "string") {
|
|
1416
|
+
let digits = value[1];
|
|
1417
|
+
let maxBigIntDigits = this.limits.maxBigIntDigits;
|
|
1418
|
+
if (digits.length > maxBigIntDigits) throw new TypeError(`Deserialized bigint exceeds maximum length of ${maxBigIntDigits} digits.`);
|
|
1419
|
+
return BigInt(digits);
|
|
1420
|
+
}
|
|
1271
1421
|
break;
|
|
1272
1422
|
case "date":
|
|
1273
1423
|
if (value[1] === null) return /* @__PURE__ */ new Date(NaN);
|
|
1274
1424
|
if (typeof value[1] == "number") return new Date(value[1]);
|
|
1275
1425
|
break;
|
|
1276
|
-
case "bytes":
|
|
1277
|
-
|
|
1278
|
-
if (
|
|
1279
|
-
else if (
|
|
1426
|
+
case "bytes": {
|
|
1427
|
+
let bytes;
|
|
1428
|
+
if (value[1] instanceof Uint8Array) bytes = value[1];
|
|
1429
|
+
else if (typeof value[1] == "string") if (typeof Buffer !== "undefined") bytes = Buffer.from(value[1], "base64");
|
|
1430
|
+
else if (Uint8Array.fromBase64) bytes = Uint8Array.fromBase64(value[1]);
|
|
1280
1431
|
else {
|
|
1281
1432
|
let bs = atob(value[1]);
|
|
1282
1433
|
let len = bs.length;
|
|
1283
|
-
|
|
1434
|
+
bytes = new Uint8Array(len);
|
|
1284
1435
|
for (let i = 0; i < len; i++) bytes[i] = bs.charCodeAt(i);
|
|
1285
|
-
return bytes;
|
|
1286
1436
|
}
|
|
1287
|
-
break;
|
|
1437
|
+
else break;
|
|
1438
|
+
if (value.length === 2) return bytes;
|
|
1439
|
+
if (typeof value[2] !== "string") throw new TypeError(`Unknown bytes type marker type: ${typeof value[2]}`);
|
|
1440
|
+
if (!isValidByteContainerName(value[2])) {
|
|
1441
|
+
let marker = value[2].slice(0, 64);
|
|
1442
|
+
throw new TypeError(`Unknown bytes type marker: ${marker}`);
|
|
1443
|
+
}
|
|
1444
|
+
let marker = value[2];
|
|
1445
|
+
let elementSize = TYPED_ARRAY_ELEMENT_SIZE[marker];
|
|
1446
|
+
if (elementSize !== void 0 && bytes.byteLength % elementSize !== 0) throw new TypeError(`Invalid byte length ${bytes.byteLength} for ${marker}; expected a multiple of ${elementSize}`);
|
|
1447
|
+
let buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
|
1448
|
+
if (!NATIVE_LITTLE_ENDIAN && elementSize !== void 0) swapByteOrder(new Uint8Array(buffer), elementSize);
|
|
1449
|
+
switch (marker) {
|
|
1450
|
+
case "ArrayBuffer": return buffer;
|
|
1451
|
+
case "DataView": return new DataView(buffer);
|
|
1452
|
+
case "Int8Array": return new Int8Array(buffer);
|
|
1453
|
+
case "Uint8Array": return new Uint8Array(buffer);
|
|
1454
|
+
case "Uint8ClampedArray": return new Uint8ClampedArray(buffer);
|
|
1455
|
+
case "Int16Array": return new Int16Array(buffer);
|
|
1456
|
+
case "Uint16Array": return new Uint16Array(buffer);
|
|
1457
|
+
case "Int32Array": return new Int32Array(buffer);
|
|
1458
|
+
case "Uint32Array": return new Uint32Array(buffer);
|
|
1459
|
+
case "BigInt64Array": return new BigInt64Array(buffer);
|
|
1460
|
+
case "BigUint64Array": return new BigUint64Array(buffer);
|
|
1461
|
+
case "Float32Array": return new Float32Array(buffer);
|
|
1462
|
+
case "Float64Array": return new Float64Array(buffer);
|
|
1463
|
+
default:
|
|
1464
|
+
}
|
|
1465
|
+
}
|
|
1288
1466
|
case "error":
|
|
1289
1467
|
if (value.length >= 3 && typeof value[1] === "string" && typeof value[2] === "string") {
|
|
1290
1468
|
let cls = ERROR_TYPES[value[1]] || Error;
|
|
@@ -1297,7 +1475,11 @@ var Evaluator = class Evaluator {
|
|
|
1297
1475
|
let propsObj = props;
|
|
1298
1476
|
for (let key of Object.keys(propsObj)) {
|
|
1299
1477
|
if (key === "name" || key === "message" || key === "stack") continue;
|
|
1300
|
-
|
|
1478
|
+
if (key in Object.prototype || key === "toJSON") {
|
|
1479
|
+
this.evaluateImpl(propsObj[key], result, key, depth + 1);
|
|
1480
|
+
continue;
|
|
1481
|
+
}
|
|
1482
|
+
anyResult[key] = this.evaluateImpl(propsObj[key], result, key, depth + 1);
|
|
1301
1483
|
}
|
|
1302
1484
|
}
|
|
1303
1485
|
return result;
|
|
@@ -1309,6 +1491,9 @@ var Evaluator = class Evaluator {
|
|
|
1309
1491
|
case "inf": return Infinity;
|
|
1310
1492
|
case "-inf": return -Infinity;
|
|
1311
1493
|
case "nan": return NaN;
|
|
1494
|
+
case "url":
|
|
1495
|
+
if (value.length === 2 && typeof value[1] === "string") return new URL(value[1]);
|
|
1496
|
+
break;
|
|
1312
1497
|
case "headers":
|
|
1313
1498
|
if (value.length === 2 && value[1] instanceof Array) return new Headers(value[1]);
|
|
1314
1499
|
break;
|
|
@@ -1318,11 +1503,11 @@ var Evaluator = class Evaluator {
|
|
|
1318
1503
|
let init = value[2];
|
|
1319
1504
|
if (typeof init !== "object" || init === null) break;
|
|
1320
1505
|
if (init.body) {
|
|
1321
|
-
init.body = this.evaluateImpl(init.body, init, "body");
|
|
1506
|
+
init.body = this.evaluateImpl(init.body, init, "body", depth + 1);
|
|
1322
1507
|
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.");
|
|
1323
1508
|
}
|
|
1324
1509
|
if (init.signal) {
|
|
1325
|
-
init.signal = this.evaluateImpl(init.signal, init, "signal");
|
|
1510
|
+
init.signal = this.evaluateImpl(init.signal, init, "signal", depth + 1);
|
|
1326
1511
|
if (!(init.signal instanceof AbortSignal)) throw new TypeError("Request siganl must be of type AbortSignal.");
|
|
1327
1512
|
}
|
|
1328
1513
|
if (init.headers && !(init.headers instanceof Array)) throw new TypeError("Request headers must be serialized as an array of pairs.");
|
|
@@ -1339,7 +1524,7 @@ var Evaluator = class Evaluator {
|
|
|
1339
1524
|
}
|
|
1340
1525
|
case "response": {
|
|
1341
1526
|
if (value.length !== 3) break;
|
|
1342
|
-
let body = this.evaluateImpl(value[1], parent, property);
|
|
1527
|
+
let body = this.evaluateImpl(value[1], parent, property, depth + 1);
|
|
1343
1528
|
if (body === null || typeof body === "string" || body instanceof Uint8Array || body instanceof ReadableStream) {} else throw new TypeError("Response body must be of type ReadableStream.");
|
|
1344
1529
|
let init = value[2];
|
|
1345
1530
|
if (typeof init !== "object" || init === null) break;
|
|
@@ -1350,7 +1535,7 @@ var Evaluator = class Evaluator {
|
|
|
1350
1535
|
case "blob": {
|
|
1351
1536
|
if (value.length !== 3 || typeof value[1] !== "string") break;
|
|
1352
1537
|
let contentType = value[1];
|
|
1353
|
-
let content = this.evaluateImpl(value[2], parent, property);
|
|
1538
|
+
let content = this.evaluateImpl(value[2], parent, property, depth + 1);
|
|
1354
1539
|
if (!(content instanceof ReadableStream)) throw new TypeError("Blob content must be serialized as a ReadableStream.");
|
|
1355
1540
|
let promise = streamToBlobPromise(content, contentType);
|
|
1356
1541
|
this.promises.push({
|
|
@@ -1391,7 +1576,7 @@ var Evaluator = class Evaluator {
|
|
|
1391
1576
|
if (value.length == 3) return addStub(hook.get(path));
|
|
1392
1577
|
let args = value[3];
|
|
1393
1578
|
if (!(args instanceof Array)) break;
|
|
1394
|
-
args = new Evaluator(this.importer).
|
|
1579
|
+
args = new Evaluator(this.importer).evaluateWithDepth([args], depth);
|
|
1395
1580
|
return addStub(hook.call(path, args));
|
|
1396
1581
|
}
|
|
1397
1582
|
case "remap": {
|
|
@@ -1457,9 +1642,9 @@ var Evaluator = class Evaluator {
|
|
|
1457
1642
|
} else if (value instanceof Object) {
|
|
1458
1643
|
let result = value;
|
|
1459
1644
|
for (let key in result) if (key in Object.prototype || key === "toJSON") {
|
|
1460
|
-
this.evaluateImpl(result[key], result, key);
|
|
1645
|
+
this.evaluateImpl(result[key], result, key, depth + 1);
|
|
1461
1646
|
delete result[key];
|
|
1462
|
-
} else result[key] = this.evaluateImpl(result[key], result, key);
|
|
1647
|
+
} else result[key] = this.evaluateImpl(result[key], result, key, depth + 1);
|
|
1463
1648
|
return result;
|
|
1464
1649
|
} else return value;
|
|
1465
1650
|
}
|
|
@@ -1682,6 +1867,7 @@ var RpcSessionImpl = class {
|
|
|
1682
1867
|
pullCount = 0;
|
|
1683
1868
|
onBrokenCallbacks = [];
|
|
1684
1869
|
encodingLevel;
|
|
1870
|
+
limits;
|
|
1685
1871
|
constructor(transport, mainHook, options) {
|
|
1686
1872
|
this.transport = transport;
|
|
1687
1873
|
this.options = options;
|
|
@@ -1694,6 +1880,10 @@ var RpcSessionImpl = class {
|
|
|
1694
1880
|
}
|
|
1695
1881
|
}
|
|
1696
1882
|
this.encodingLevel = level;
|
|
1883
|
+
this.limits = {
|
|
1884
|
+
...DEFAULT_LIMITS,
|
|
1885
|
+
...options.limits
|
|
1886
|
+
};
|
|
1697
1887
|
this.exports.push({
|
|
1698
1888
|
hook: mainHook,
|
|
1699
1889
|
refcount: 1
|
|
@@ -1836,6 +2026,9 @@ var RpcSessionImpl = class {
|
|
|
1836
2026
|
entry.pipeReadable = void 0;
|
|
1837
2027
|
return readable;
|
|
1838
2028
|
}
|
|
2029
|
+
getLimits() {
|
|
2030
|
+
return this.limits;
|
|
2031
|
+
}
|
|
1839
2032
|
createPipe(readable, readableHook) {
|
|
1840
2033
|
if (this.abortReason) throw this.abortReason;
|
|
1841
2034
|
this.send(["pipe"]);
|
|
@@ -1995,6 +2188,7 @@ var RpcSessionImpl = class {
|
|
|
1995
2188
|
} finally {
|
|
1996
2189
|
if (this.cancelReadLoop === readCanceled.reject) this.cancelReadLoop = void 0;
|
|
1997
2190
|
}
|
|
2191
|
+
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.`);
|
|
1998
2192
|
if (this.abortReason) break;
|
|
1999
2193
|
let msg = this.encodingLevel === "string" ? JSON.parse(raw) : raw;
|
|
2000
2194
|
if (msg instanceof Array) switch (msg[0]) {
|
|
@@ -2069,7 +2263,7 @@ var RpcSessionImpl = class {
|
|
|
2069
2263
|
case "abort": {
|
|
2070
2264
|
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[1]);
|
|
2071
2265
|
payload.dispose();
|
|
2072
|
-
this.abort(payload, false);
|
|
2266
|
+
this.abort(payload.value, false);
|
|
2073
2267
|
break;
|
|
2074
2268
|
}
|
|
2075
2269
|
}
|
|
@@ -2120,6 +2314,8 @@ var RpcSession$1 = class {
|
|
|
2120
2314
|
|
|
2121
2315
|
//#endregion
|
|
2122
2316
|
//#region src/websocket.ts
|
|
2317
|
+
/** Close-frame reason max UTF-8 bytes: 125 payload − 2-byte status (RFC 6455 §5.5). */
|
|
2318
|
+
const MAX_CLOSE_REASON_BYTES = 123;
|
|
2123
2319
|
function newWebSocketRpcSession$1(webSocket, localMain, options) {
|
|
2124
2320
|
if (typeof webSocket === "string") webSocket = new WebSocket(webSocket);
|
|
2125
2321
|
return new RpcSession$1(new WebSocketTransport(webSocket), localMain, options).getRemoteMain();
|
|
@@ -2195,6 +2391,8 @@ var WebSocketTransport = class {
|
|
|
2195
2391
|
let message;
|
|
2196
2392
|
if (reason instanceof Error) message = reason.message;
|
|
2197
2393
|
else message = `${reason}`;
|
|
2394
|
+
let reasonBytes = new TextEncoder().encode(message);
|
|
2395
|
+
if (reasonBytes.length > 123) message = new TextDecoder().decode(reasonBytes.subarray(0, 123), { stream: true });
|
|
2198
2396
|
this.#webSocket.close(3e3, message);
|
|
2199
2397
|
if (!this.#error) this.#error = reason;
|
|
2200
2398
|
}
|
|
@@ -2212,6 +2410,7 @@ var WebSocketTransport = class {
|
|
|
2212
2410
|
|
|
2213
2411
|
//#endregion
|
|
2214
2412
|
//#region src/batch.ts
|
|
2413
|
+
const yieldToMacrotask = typeof setImmediate === "function" ? () => new Promise((resolve) => setImmediate(resolve)) : () => new Promise((resolve) => setTimeout(resolve, 0));
|
|
2215
2414
|
var BatchClientTransport = class {
|
|
2216
2415
|
constructor(sendBatch) {
|
|
2217
2416
|
this.#promise = this.#scheduleBatch(sendBatch);
|
|
@@ -2233,7 +2432,7 @@ var BatchClientTransport = class {
|
|
|
2233
2432
|
this.#aborted = reason;
|
|
2234
2433
|
}
|
|
2235
2434
|
async #scheduleBatch(sendBatch) {
|
|
2236
|
-
await
|
|
2435
|
+
await yieldToMacrotask();
|
|
2237
2436
|
if (this.#aborted !== void 0) throw this.#aborted;
|
|
2238
2437
|
let batch = this.#batchToSend;
|
|
2239
2438
|
this.#batchToSend = null;
|
|
@@ -2580,6 +2779,9 @@ var MapApplicator = class {
|
|
|
2580
2779
|
getPipeReadable(exportId) {
|
|
2581
2780
|
throw new Error("A mapper function cannot use pipe readables.");
|
|
2582
2781
|
}
|
|
2782
|
+
getLimits() {
|
|
2783
|
+
return DEFAULT_LIMITS;
|
|
2784
|
+
}
|
|
2583
2785
|
};
|
|
2584
2786
|
function applyMapToElement(input, parent, owner, captures, instructions) {
|
|
2585
2787
|
let mapper = new MapApplicator(captures, new PayloadStubHook(RpcPayload.deepCopyFrom(input, parent, owner)));
|
|
@@ -2640,8 +2842,7 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
|
|
|
2640
2842
|
throw new Error(`Unknown WritableStream method: ${method}`);
|
|
2641
2843
|
}
|
|
2642
2844
|
if (method === "close" || method === "abort") state.closed = true;
|
|
2643
|
-
|
|
2644
|
-
return new PromiseStubHook(args.deliverCall(func, state.writer).then((payload) => new PayloadStubHook(payload)));
|
|
2845
|
+
return new PromiseStubHook((method === "write" ? args.deliverStreamWrite(state.writer) : args.deliverCall(state.writer[method], state.writer)).then((payload) => new PayloadStubHook(payload)));
|
|
2645
2846
|
} catch (err) {
|
|
2646
2847
|
return new ErrorStubHook(err);
|
|
2647
2848
|
}
|
|
@@ -2918,15 +3119,15 @@ let newMessagePortRpcSession = newMessagePortRpcSession$1;
|
|
|
2918
3119
|
* credentials as parameters and returns the authorized API), then cross-origin requests should
|
|
2919
3120
|
* be safe.
|
|
2920
3121
|
*/
|
|
2921
|
-
async function newWorkersRpcResponse(request, localMain) {
|
|
3122
|
+
async function newWorkersRpcResponse(request, localMain, options) {
|
|
2922
3123
|
if (request.method === "POST") {
|
|
2923
|
-
let response = await newHttpBatchRpcResponse(request, localMain);
|
|
3124
|
+
let response = await newHttpBatchRpcResponse(request, localMain, options);
|
|
2924
3125
|
response.headers.set("Access-Control-Allow-Origin", "*");
|
|
2925
3126
|
return response;
|
|
2926
|
-
} else if (request.headers.get("Upgrade")?.toLowerCase() === "websocket") return newWorkersWebSocketRpcResponse(request, localMain);
|
|
3127
|
+
} else if (request.headers.get("Upgrade")?.toLowerCase() === "websocket") return newWorkersWebSocketRpcResponse(request, localMain, options);
|
|
2927
3128
|
else return new Response("This endpoint only accepts POST or WebSocket requests.", { status: 400 });
|
|
2928
3129
|
}
|
|
2929
3130
|
|
|
2930
3131
|
//#endregion
|
|
2931
|
-
export { RpcPromise, RpcSession, RpcStub, RpcTarget, WebSocketTransport, deserialize, newHttpBatchRpcResponse, newHttpBatchRpcSession, newMessagePortRpcSession, newWebSocketRpcSession, newWorkersRpcResponse, newWorkersWebSocketRpcResponse, nodeHttpBatchRpcResponse, serialize };
|
|
3132
|
+
export { DEFAULT_LIMITS, DEFAULT_MAX_DEPTH, RpcPromise, RpcSession, RpcStub, RpcTarget, WebSocketTransport, deserialize, newHttpBatchRpcResponse, newHttpBatchRpcSession, newMessagePortRpcSession, newWebSocketRpcSession, newWorkersRpcResponse, newWorkersWebSocketRpcResponse, nodeHttpBatchRpcResponse, serialize };
|
|
2932
3133
|
//# sourceMappingURL=index-workers.js.map
|