capnweb 0.12.0 → 0.13.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 +112 -760
- package/dist/index-bun.cjs +277 -109
- package/dist/index-bun.cjs.map +1 -1
- package/dist/index-bun.d.cts +55 -32
- package/dist/index-bun.d.ts +55 -32
- package/dist/index-bun.js +277 -108
- package/dist/index-bun.js.map +1 -1
- package/dist/index-workers.cjs +278 -109
- package/dist/index-workers.cjs.map +1 -1
- package/dist/index-workers.d.cts +51 -31
- package/dist/index-workers.d.ts +51 -31
- package/dist/index-workers.js +277 -108
- package/dist/index-workers.js.map +1 -1
- package/dist/index.cjs +277 -109
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +51 -31
- package/dist/index.d.ts +51 -31
- package/dist/index.js +277 -108
- package/dist/index.js.map +1 -1
- package/package.json +22 -19
package/dist/index-workers.cjs
CHANGED
|
@@ -20,7 +20,7 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
20
20
|
}
|
|
21
21
|
return to;
|
|
22
22
|
};
|
|
23
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
|
|
24
24
|
value: mod,
|
|
25
25
|
enumerable: true
|
|
26
26
|
}) : target, mod));
|
|
@@ -75,6 +75,9 @@ function typeForRpc(value) {
|
|
|
75
75
|
case AsyncFunction.prototype: return "function";
|
|
76
76
|
case Array.prototype: return "array";
|
|
77
77
|
case Date.prototype: return "date";
|
|
78
|
+
case RegExp.prototype: return "regexp";
|
|
79
|
+
case Set.prototype: return "set";
|
|
80
|
+
case Map.prototype: return "map";
|
|
78
81
|
case Uint8Array.prototype:
|
|
79
82
|
case BUFFER_PROTOTYPE:
|
|
80
83
|
case ArrayBuffer.prototype:
|
|
@@ -338,6 +341,9 @@ async function pullPromise(promise) {
|
|
|
338
341
|
if (pathIfPromise.length > 0) hook = hook.get(pathIfPromise);
|
|
339
342
|
return (await hook.pull()).deliverResolve();
|
|
340
343
|
}
|
|
344
|
+
function mapKeyToPropertyName(key) {
|
|
345
|
+
return typeof key === "string" || typeof key === "number" ? key : { mapKey: key };
|
|
346
|
+
}
|
|
341
347
|
var RpcPayload = class RpcPayload {
|
|
342
348
|
value;
|
|
343
349
|
source;
|
|
@@ -392,12 +398,13 @@ var RpcPayload = class RpcPayload {
|
|
|
392
398
|
return TargetStubHook.create(target, parent);
|
|
393
399
|
} else if (this.source === "return") {
|
|
394
400
|
let hook = this.rpcTargets?.get(target);
|
|
395
|
-
if (hook)
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
+
if (hook) {
|
|
402
|
+
if (dupStubs) return hook.dup();
|
|
403
|
+
else {
|
|
404
|
+
this.rpcTargets?.delete(target);
|
|
405
|
+
return hook;
|
|
406
|
+
}
|
|
407
|
+
} else {
|
|
401
408
|
hook = TargetStubHook.create(target, parent);
|
|
402
409
|
if (dupStubs) {
|
|
403
410
|
if (!this.rpcTargets) this.rpcTargets = /* @__PURE__ */ new Map();
|
|
@@ -411,12 +418,13 @@ var RpcPayload = class RpcPayload {
|
|
|
411
418
|
if (this.source === "params") return streamImpl.createWritableStreamHook(stream);
|
|
412
419
|
else if (this.source === "return") {
|
|
413
420
|
let hook = this.rpcTargets?.get(stream);
|
|
414
|
-
if (hook)
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
421
|
+
if (hook) {
|
|
422
|
+
if (dupStubs) return hook.dup();
|
|
423
|
+
else {
|
|
424
|
+
this.rpcTargets?.delete(stream);
|
|
425
|
+
return hook;
|
|
426
|
+
}
|
|
427
|
+
} else {
|
|
420
428
|
hook = streamImpl.createWritableStreamHook(stream);
|
|
421
429
|
if (dupStubs) {
|
|
422
430
|
if (!this.rpcTargets) this.rpcTargets = /* @__PURE__ */ new Map();
|
|
@@ -430,12 +438,13 @@ var RpcPayload = class RpcPayload {
|
|
|
430
438
|
if (this.source === "params") return streamImpl.createReadableStreamHook(stream);
|
|
431
439
|
else if (this.source === "return") {
|
|
432
440
|
let hook = this.rpcTargets?.get(stream);
|
|
433
|
-
if (hook)
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
441
|
+
if (hook) {
|
|
442
|
+
if (dupStubs) return hook.dup();
|
|
443
|
+
else {
|
|
444
|
+
this.rpcTargets?.delete(stream);
|
|
445
|
+
return hook;
|
|
446
|
+
}
|
|
447
|
+
} else {
|
|
439
448
|
hook = streamImpl.createReadableStreamHook(stream);
|
|
440
449
|
if (dupStubs) {
|
|
441
450
|
if (!this.rpcTargets) this.rpcTargets = /* @__PURE__ */ new Map();
|
|
@@ -443,7 +452,7 @@ var RpcPayload = class RpcPayload {
|
|
|
443
452
|
return hook.dup();
|
|
444
453
|
} else return hook;
|
|
445
454
|
}
|
|
446
|
-
} else
|
|
455
|
+
} else return streamImpl.createReadableStreamHook(stream);
|
|
447
456
|
}
|
|
448
457
|
deepCopy(value, oldParent, property, parent, dupStubs, owner) {
|
|
449
458
|
switch (typeForRpc(value)) {
|
|
@@ -454,6 +463,7 @@ var RpcPayload = class RpcPayload {
|
|
|
454
463
|
case "bytes":
|
|
455
464
|
case "blob":
|
|
456
465
|
case "url":
|
|
466
|
+
case "regexp":
|
|
457
467
|
case "error":
|
|
458
468
|
case "undefined": return value;
|
|
459
469
|
case "array": {
|
|
@@ -463,6 +473,35 @@ var RpcPayload = class RpcPayload {
|
|
|
463
473
|
for (let i = 0; i < len; i++) result[i] = this.deepCopy(array[i], array, i, result, dupStubs, owner);
|
|
464
474
|
return result;
|
|
465
475
|
}
|
|
476
|
+
case "set": {
|
|
477
|
+
let set = value;
|
|
478
|
+
let elements = [...set];
|
|
479
|
+
for (let val of elements) {
|
|
480
|
+
let kind = typeForRpc(val);
|
|
481
|
+
if (kind === "rpc-promise" || kind === "rpc-thenable") throw new TypeError("Cannot pass a promise as an element of a Set. Await the value before adding it to the Set.");
|
|
482
|
+
}
|
|
483
|
+
let result = /* @__PURE__ */ new Set();
|
|
484
|
+
let index = 0;
|
|
485
|
+
for (let val of elements) {
|
|
486
|
+
let copy = this.deepCopy(val, set, index++, result, dupStubs, owner);
|
|
487
|
+
result.add(copy);
|
|
488
|
+
}
|
|
489
|
+
return result;
|
|
490
|
+
}
|
|
491
|
+
case "map": {
|
|
492
|
+
let map = value;
|
|
493
|
+
let entries = [...map];
|
|
494
|
+
for (let [key] of entries) {
|
|
495
|
+
let kind = typeForRpc(key);
|
|
496
|
+
if (kind === "rpc-promise" || kind === "rpc-thenable") throw new TypeError("Cannot pass a promise as a key of a Map. Await the value before using it as a key.");
|
|
497
|
+
}
|
|
498
|
+
let result = /* @__PURE__ */ new Map();
|
|
499
|
+
for (let [key, val] of entries) {
|
|
500
|
+
let keyCopy = this.deepCopy(key, map, -1, result, dupStubs, owner);
|
|
501
|
+
result.set(keyCopy, this.deepCopy(val, map, mapKeyToPropertyName(keyCopy), result, dupStubs, owner));
|
|
502
|
+
}
|
|
503
|
+
return result;
|
|
504
|
+
}
|
|
466
505
|
case "object": {
|
|
467
506
|
let result = {};
|
|
468
507
|
let object = value;
|
|
@@ -520,8 +559,10 @@ var RpcPayload = class RpcPayload {
|
|
|
520
559
|
case "readable": {
|
|
521
560
|
let stream = value;
|
|
522
561
|
let hook;
|
|
523
|
-
if (owner)
|
|
524
|
-
|
|
562
|
+
if (owner) {
|
|
563
|
+
if (owner.source === "owned") throw new Error("Can't copy a ReadableStream.");
|
|
564
|
+
hook = owner.getHookForReadableStream(stream, oldParent, dupStubs);
|
|
565
|
+
} else hook = streamImpl.createReadableStreamHook(stream);
|
|
525
566
|
this.hooks.push(hook);
|
|
526
567
|
return stream;
|
|
527
568
|
}
|
|
@@ -547,6 +588,10 @@ var RpcPayload = class RpcPayload {
|
|
|
547
588
|
try {
|
|
548
589
|
this.value = this.deepCopy(this.value, void 0, "value", this, dupStubs, this);
|
|
549
590
|
} catch (err) {
|
|
591
|
+
if (dupStubs) {
|
|
592
|
+
this.hooks.forEach((hook) => hook.dispose());
|
|
593
|
+
this.promises.forEach((promise) => promise.promise[Symbol.dispose]());
|
|
594
|
+
}
|
|
550
595
|
this.hooks = void 0;
|
|
551
596
|
this.promises = void 0;
|
|
552
597
|
throw err;
|
|
@@ -560,7 +605,8 @@ var RpcPayload = class RpcPayload {
|
|
|
560
605
|
this.ensureDeepCopied();
|
|
561
606
|
if (this.value instanceof RpcPromise$1) RpcPayload.deliverRpcPromiseTo(this.value, parent, property, promises);
|
|
562
607
|
else {
|
|
563
|
-
parent
|
|
608
|
+
if (parent instanceof Map) parent.set(typeof property === "object" ? property.mapKey : property, this.value);
|
|
609
|
+
else parent[property] = this.value;
|
|
564
610
|
for (let record of this.promises) RpcPayload.deliverRpcPromiseTo(record.promise, record.parent, record.property, promises);
|
|
565
611
|
}
|
|
566
612
|
}
|
|
@@ -652,6 +698,7 @@ var RpcPayload = class RpcPayload {
|
|
|
652
698
|
case "blob":
|
|
653
699
|
case "date":
|
|
654
700
|
case "url":
|
|
701
|
+
case "regexp":
|
|
655
702
|
case "error":
|
|
656
703
|
case "undefined": return;
|
|
657
704
|
case "array": {
|
|
@@ -660,6 +707,19 @@ var RpcPayload = class RpcPayload {
|
|
|
660
707
|
for (let i = 0; i < len; i++) this.disposeImpl(array[i], array);
|
|
661
708
|
return;
|
|
662
709
|
}
|
|
710
|
+
case "set": {
|
|
711
|
+
let set = value;
|
|
712
|
+
for (let element of set) this.disposeImpl(element, set);
|
|
713
|
+
return;
|
|
714
|
+
}
|
|
715
|
+
case "map": {
|
|
716
|
+
let map = value;
|
|
717
|
+
for (let [key, val] of map) {
|
|
718
|
+
this.disposeImpl(key, map);
|
|
719
|
+
this.disposeImpl(val, map);
|
|
720
|
+
}
|
|
721
|
+
return;
|
|
722
|
+
}
|
|
663
723
|
case "object": {
|
|
664
724
|
let object = value;
|
|
665
725
|
for (let i in object) this.disposeImpl(object[i], object);
|
|
@@ -735,6 +795,7 @@ var RpcPayload = class RpcPayload {
|
|
|
735
795
|
case "writable":
|
|
736
796
|
case "readable":
|
|
737
797
|
case "url":
|
|
798
|
+
case "regexp":
|
|
738
799
|
case "headers":
|
|
739
800
|
case "request":
|
|
740
801
|
case "response": return;
|
|
@@ -744,6 +805,19 @@ var RpcPayload = class RpcPayload {
|
|
|
744
805
|
for (let i = 0; i < len; i++) this.ignoreUnhandledRejectionsImpl(array[i]);
|
|
745
806
|
return;
|
|
746
807
|
}
|
|
808
|
+
case "set": {
|
|
809
|
+
let set = value;
|
|
810
|
+
for (let element of set) this.ignoreUnhandledRejectionsImpl(element);
|
|
811
|
+
return;
|
|
812
|
+
}
|
|
813
|
+
case "map": {
|
|
814
|
+
let map = value;
|
|
815
|
+
for (let [key, val] of map) {
|
|
816
|
+
this.ignoreUnhandledRejectionsImpl(key);
|
|
817
|
+
this.ignoreUnhandledRejectionsImpl(val);
|
|
818
|
+
}
|
|
819
|
+
return;
|
|
820
|
+
}
|
|
747
821
|
case "object": {
|
|
748
822
|
let object = value;
|
|
749
823
|
for (let i in object) this.ignoreUnhandledRejectionsImpl(object[i]);
|
|
@@ -803,8 +877,11 @@ function followPath(value, parent, path, owner) {
|
|
|
803
877
|
case "bytes":
|
|
804
878
|
case "blob":
|
|
805
879
|
case "date":
|
|
880
|
+
case "set":
|
|
881
|
+
case "map":
|
|
806
882
|
case "error":
|
|
807
883
|
case "url":
|
|
884
|
+
case "regexp":
|
|
808
885
|
case "headers":
|
|
809
886
|
case "request":
|
|
810
887
|
case "response":
|
|
@@ -850,7 +927,8 @@ var ValueStubHook = class extends StubHook {
|
|
|
850
927
|
args.dispose();
|
|
851
928
|
return new ErrorStubHook(/* @__PURE__ */ new TypeError(`'${path.join(".")}' is not a function.`));
|
|
852
929
|
}
|
|
853
|
-
|
|
930
|
+
let promise = args.deliverCall(followResult.value, followResult.parent);
|
|
931
|
+
return new PromiseStubHook(promise.then((payload) => {
|
|
854
932
|
return new PayloadStubHook(payload);
|
|
855
933
|
}));
|
|
856
934
|
}
|
|
@@ -1048,7 +1126,7 @@ const DEFAULT_MAX_DEPTH = 256;
|
|
|
1048
1126
|
const DEFAULT_LIMITS = {
|
|
1049
1127
|
maxBigIntDigits: 16384,
|
|
1050
1128
|
maxDepth: 256,
|
|
1051
|
-
maxMessageSize:
|
|
1129
|
+
maxMessageSize: 33554432
|
|
1052
1130
|
};
|
|
1053
1131
|
const NATIVE_LITTLE_ENDIAN = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
|
|
1054
1132
|
const BYTE_CONTAINER_TYPE_NAMES = [
|
|
@@ -1110,9 +1188,7 @@ function swapByteOrder(bytes, elementSize) {
|
|
|
1110
1188
|
case 4:
|
|
1111
1189
|
view.setUint32(offset, view.getUint32(offset, false), true);
|
|
1112
1190
|
break;
|
|
1113
|
-
case 8:
|
|
1114
|
-
view.setBigUint64(offset, view.getBigUint64(offset, false), true);
|
|
1115
|
-
break;
|
|
1191
|
+
case 8: view.setBigUint64(offset, view.getBigUint64(offset, false), true);
|
|
1116
1192
|
}
|
|
1117
1193
|
}
|
|
1118
1194
|
var NullExporter = class {
|
|
@@ -1197,6 +1273,30 @@ var Devaluator = class Devaluator {
|
|
|
1197
1273
|
for (let i = 0; i < len; i++) result[i] = this.devaluateImpl(array[i], array, depth + 1);
|
|
1198
1274
|
return [result];
|
|
1199
1275
|
}
|
|
1276
|
+
case "set": {
|
|
1277
|
+
let set = value;
|
|
1278
|
+
let setElements = [...set];
|
|
1279
|
+
let elements = [];
|
|
1280
|
+
for (let element of setElements) switch (typeForRpc(element)) {
|
|
1281
|
+
case "blob": throw new TypeError("Cannot serialize a Blob as an element of a Set.");
|
|
1282
|
+
case "rpc-promise":
|
|
1283
|
+
case "rpc-thenable": throw new TypeError("Cannot serialize a promise as an element of a Set. Await the value before adding it to the Set.");
|
|
1284
|
+
}
|
|
1285
|
+
for (let element of setElements) elements.push(this.devaluateImpl(element, set, depth + 1));
|
|
1286
|
+
return ["set", elements];
|
|
1287
|
+
}
|
|
1288
|
+
case "map": {
|
|
1289
|
+
let map = value;
|
|
1290
|
+
let mapEntries = [...map];
|
|
1291
|
+
let entries = [];
|
|
1292
|
+
for (let [key] of mapEntries) switch (typeForRpc(key)) {
|
|
1293
|
+
case "blob": throw new TypeError("Cannot serialize a Blob as a key of a Map.");
|
|
1294
|
+
case "rpc-promise":
|
|
1295
|
+
case "rpc-thenable": throw new TypeError("Cannot serialize a promise as a key of a Map. Await the value before using it as a key.");
|
|
1296
|
+
}
|
|
1297
|
+
for (let [key, val] of mapEntries) entries.push([this.devaluateImpl(key, map, depth + 1), this.devaluateImpl(val, map, depth + 1)]);
|
|
1298
|
+
return ["map", entries];
|
|
1299
|
+
}
|
|
1200
1300
|
case "bigint":
|
|
1201
1301
|
if (this.encodingLevel === "structuredClonable") return value;
|
|
1202
1302
|
return ["bigint", value.toString()];
|
|
@@ -1205,6 +1305,15 @@ var Devaluator = class Devaluator {
|
|
|
1205
1305
|
const time = value.getTime();
|
|
1206
1306
|
return ["date", Number.isNaN(time) ? null : time];
|
|
1207
1307
|
}
|
|
1308
|
+
case "regexp": {
|
|
1309
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1310
|
+
let re = value;
|
|
1311
|
+
return re.flags ? [
|
|
1312
|
+
"regexp",
|
|
1313
|
+
re.source,
|
|
1314
|
+
re.flags
|
|
1315
|
+
] : ["regexp", re.source];
|
|
1316
|
+
}
|
|
1208
1317
|
case "bytes": {
|
|
1209
1318
|
let alternateTypeName = BYTE_CONTAINER_TYPE_BY_PROTOTYPE.get(Object.getPrototypeOf(value));
|
|
1210
1319
|
let bytes;
|
|
@@ -1219,11 +1328,14 @@ var Devaluator = class Devaluator {
|
|
|
1219
1328
|
swapByteOrder(bytes, elementSize);
|
|
1220
1329
|
}
|
|
1221
1330
|
}
|
|
1222
|
-
if (this.encodingLevel === "structuredClonable" || this.encodingLevel === "jsonCompatibleWithBytes")
|
|
1223
|
-
|
|
1224
|
-
bytes,
|
|
1225
|
-
|
|
1226
|
-
|
|
1331
|
+
if (this.encodingLevel === "structuredClonable" || this.encodingLevel === "jsonCompatibleWithBytes") {
|
|
1332
|
+
if (bytes.byteLength !== bytes.buffer.byteLength) bytes = new Uint8Array(bytes);
|
|
1333
|
+
return alternateTypeName === void 0 ? ["bytes", bytes] : [
|
|
1334
|
+
"bytes",
|
|
1335
|
+
bytes,
|
|
1336
|
+
alternateTypeName
|
|
1337
|
+
];
|
|
1338
|
+
}
|
|
1227
1339
|
let b64;
|
|
1228
1340
|
if (bytes.toBase64) b64 = bytes.toBase64({ omitPadding: true });
|
|
1229
1341
|
else if (typeof Buffer !== "undefined") b64 = (bytes instanceof Buffer ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)).toString("base64");
|
|
@@ -1362,13 +1474,16 @@ var Devaluator = class Devaluator {
|
|
|
1362
1474
|
if (!this.source) throw new Error("Can't serialize RPC stubs in this context.");
|
|
1363
1475
|
let { hook, pathIfPromise } = unwrapStubAndPath(value);
|
|
1364
1476
|
let importId = this.exporter.getImport(hook);
|
|
1365
|
-
if (importId !== void 0)
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1477
|
+
if (importId !== void 0) {
|
|
1478
|
+
if (pathIfPromise) {
|
|
1479
|
+
if (pathIfPromise.length > 0) return [
|
|
1480
|
+
"pipeline",
|
|
1481
|
+
importId,
|
|
1482
|
+
pathIfPromise
|
|
1483
|
+
];
|
|
1484
|
+
else return ["pipeline", importId];
|
|
1485
|
+
} else return ["import", importId];
|
|
1486
|
+
}
|
|
1372
1487
|
if (pathIfPromise) hook = hook.get(pathIfPromise);
|
|
1373
1488
|
else hook = hook.dup();
|
|
1374
1489
|
return this.devaluateHook(pathIfPromise ? "promise" : "export", hook);
|
|
@@ -1429,16 +1544,18 @@ var NullImporter = class {
|
|
|
1429
1544
|
};
|
|
1430
1545
|
const NULL_IMPORTER = new NullImporter();
|
|
1431
1546
|
function fixBrokenRequestBody(request, body) {
|
|
1432
|
-
|
|
1547
|
+
let promise = new Response(body).arrayBuffer().then((arrayBuffer) => {
|
|
1433
1548
|
let bytes = new Uint8Array(arrayBuffer);
|
|
1434
1549
|
let result = new Request(request, { body: bytes });
|
|
1435
1550
|
return new PayloadStubHook(RpcPayload.fromAppReturn(result));
|
|
1436
|
-
})
|
|
1551
|
+
});
|
|
1552
|
+
return new RpcPromise$1(new PromiseStubHook(promise), []);
|
|
1437
1553
|
}
|
|
1438
1554
|
function streamToBlobPromise(stream, type) {
|
|
1439
|
-
|
|
1555
|
+
let promise = streamToBlob(stream, type).then((blob) => {
|
|
1440
1556
|
return new PayloadStubHook(RpcPayload.fromAppReturn(blob));
|
|
1441
|
-
})
|
|
1557
|
+
});
|
|
1558
|
+
return new RpcPromise$1(new PromiseStubHook(promise), []);
|
|
1442
1559
|
}
|
|
1443
1560
|
var Evaluator = class Evaluator {
|
|
1444
1561
|
importer;
|
|
@@ -1471,7 +1588,7 @@ var Evaluator = class Evaluator {
|
|
|
1471
1588
|
let maxDepth = this.limits.maxDepth;
|
|
1472
1589
|
if (depth >= maxDepth) throw new TypeError(`Deserialization exceeded maximum allowed message depth of ${maxDepth}.`);
|
|
1473
1590
|
if (this.encodingLevel === "structuredClonable") {
|
|
1474
|
-
if (value instanceof Date || typeof value === "bigint") return value;
|
|
1591
|
+
if (value instanceof Date || value instanceof RegExp || typeof value === "bigint") return value;
|
|
1475
1592
|
}
|
|
1476
1593
|
if (value instanceof Array) {
|
|
1477
1594
|
if (value.length == 1 && value[0] instanceof Array) {
|
|
@@ -1491,18 +1608,48 @@ var Evaluator = class Evaluator {
|
|
|
1491
1608
|
if (value[1] === null) return /* @__PURE__ */ new Date(NaN);
|
|
1492
1609
|
if (typeof value[1] == "number") return new Date(value[1]);
|
|
1493
1610
|
break;
|
|
1611
|
+
case "regexp":
|
|
1612
|
+
if (typeof value[1] === "string" && (value.length === 2 || value.length === 3 && typeof value[2] === "string")) return new RegExp(value[1], value[2]);
|
|
1613
|
+
break;
|
|
1614
|
+
case "set":
|
|
1615
|
+
if (value.length === 2 && value[1] instanceof Array) {
|
|
1616
|
+
let elements = value[1];
|
|
1617
|
+
let set = /* @__PURE__ */ new Set();
|
|
1618
|
+
for (let i = 0; i < elements.length; i++) {
|
|
1619
|
+
let copy = this.evaluateImpl(elements[i], set, i, depth + 1);
|
|
1620
|
+
if (copy instanceof RpcPromise$1) throw new TypeError("Cannot deserialize a promise as an element of a Set.");
|
|
1621
|
+
set.add(copy);
|
|
1622
|
+
}
|
|
1623
|
+
return set;
|
|
1624
|
+
}
|
|
1625
|
+
break;
|
|
1626
|
+
case "map":
|
|
1627
|
+
if (value.length === 2 && value[1] instanceof Array) {
|
|
1628
|
+
let entries = value[1];
|
|
1629
|
+
let map = /* @__PURE__ */ new Map();
|
|
1630
|
+
for (let i = 0; i < entries.length; i++) {
|
|
1631
|
+
let entry = entries[i];
|
|
1632
|
+
if (!(entry instanceof Array) || entry.length !== 2) throw new TypeError("Map entries must be serialized as key/value pairs.");
|
|
1633
|
+
let key = this.evaluateImpl(entry[0], map, i, depth + 1);
|
|
1634
|
+
if (key instanceof RpcPromise$1) throw new TypeError("Cannot deserialize a promise as a key of a Map.");
|
|
1635
|
+
map.set(key, this.evaluateImpl(entry[1], map, mapKeyToPropertyName(key), depth + 1));
|
|
1636
|
+
}
|
|
1637
|
+
return map;
|
|
1638
|
+
}
|
|
1639
|
+
break;
|
|
1494
1640
|
case "bytes": {
|
|
1495
1641
|
let bytes;
|
|
1496
1642
|
if (value[1] instanceof Uint8Array) bytes = value[1];
|
|
1497
|
-
else if (typeof value[1] == "string")
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1643
|
+
else if (typeof value[1] == "string") {
|
|
1644
|
+
if (typeof Buffer !== "undefined") bytes = Buffer.from(value[1], "base64");
|
|
1645
|
+
else if (Uint8Array.fromBase64) bytes = Uint8Array.fromBase64(value[1]);
|
|
1646
|
+
else {
|
|
1647
|
+
let bs = atob(value[1]);
|
|
1648
|
+
let len = bs.length;
|
|
1649
|
+
bytes = new Uint8Array(len);
|
|
1650
|
+
for (let i = 0; i < len; i++) bytes[i] = bs.charCodeAt(i);
|
|
1651
|
+
}
|
|
1652
|
+
} else break;
|
|
1506
1653
|
if (value.length === 2) return bytes;
|
|
1507
1654
|
if (typeof value[2] !== "string") throw new TypeError(`Unknown bytes type marker type: ${typeof value[2]}`);
|
|
1508
1655
|
if (!isValidByteContainerName(value[2])) {
|
|
@@ -1528,7 +1675,6 @@ var Evaluator = class Evaluator {
|
|
|
1528
1675
|
case "BigUint64Array": return new BigUint64Array(buffer);
|
|
1529
1676
|
case "Float32Array": return new Float32Array(buffer);
|
|
1530
1677
|
case "Float64Array": return new Float64Array(buffer);
|
|
1531
|
-
default:
|
|
1532
1678
|
}
|
|
1533
1679
|
}
|
|
1534
1680
|
case "error":
|
|
@@ -1631,11 +1777,13 @@ var Evaluator = class Evaluator {
|
|
|
1631
1777
|
return promise;
|
|
1632
1778
|
} else {
|
|
1633
1779
|
this.hooks.push(hook);
|
|
1634
|
-
return new
|
|
1780
|
+
return new RpcStub$1(hook);
|
|
1635
1781
|
}
|
|
1636
1782
|
};
|
|
1637
|
-
if (value.length == 2)
|
|
1638
|
-
|
|
1783
|
+
if (value.length == 2) {
|
|
1784
|
+
if (isPromise) return addStub(hook.get([]));
|
|
1785
|
+
else return addStub(hook.dup());
|
|
1786
|
+
}
|
|
1639
1787
|
let path = value[2];
|
|
1640
1788
|
if (!(path instanceof Array)) break;
|
|
1641
1789
|
if (!path.every((part) => {
|
|
@@ -1665,7 +1813,8 @@ var Evaluator = class Evaluator {
|
|
|
1665
1813
|
}
|
|
1666
1814
|
});
|
|
1667
1815
|
let instructions = value[4];
|
|
1668
|
-
let
|
|
1816
|
+
let resultHook = hook.map(path, captures, instructions);
|
|
1817
|
+
let promise = new RpcPromise$1(resultHook, []);
|
|
1669
1818
|
this.promises.push({
|
|
1670
1819
|
promise,
|
|
1671
1820
|
parent,
|
|
@@ -1675,18 +1824,21 @@ var Evaluator = class Evaluator {
|
|
|
1675
1824
|
}
|
|
1676
1825
|
case "export":
|
|
1677
1826
|
case "promise":
|
|
1678
|
-
if (typeof value[1] == "number")
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1827
|
+
if (typeof value[1] == "number") {
|
|
1828
|
+
if (value[0] == "promise") {
|
|
1829
|
+
let hook = this.importer.importPromise(value[1]);
|
|
1830
|
+
let promise = new RpcPromise$1(hook, []);
|
|
1831
|
+
this.promises.push({
|
|
1832
|
+
parent,
|
|
1833
|
+
property,
|
|
1834
|
+
promise
|
|
1835
|
+
});
|
|
1836
|
+
return promise;
|
|
1837
|
+
} else {
|
|
1838
|
+
let hook = this.importer.importStub(value[1]);
|
|
1839
|
+
this.hooks.push(hook);
|
|
1840
|
+
return new RpcStub$1(hook);
|
|
1841
|
+
}
|
|
1690
1842
|
}
|
|
1691
1843
|
break;
|
|
1692
1844
|
case "writable":
|
|
@@ -1697,14 +1849,12 @@ var Evaluator = class Evaluator {
|
|
|
1697
1849
|
return stream;
|
|
1698
1850
|
}
|
|
1699
1851
|
break;
|
|
1700
|
-
case "readable":
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
}
|
|
1707
|
-
break;
|
|
1852
|
+
case "readable": if (typeof value[1] == "number") {
|
|
1853
|
+
let stream = this.importer.getPipeReadable(value[1]);
|
|
1854
|
+
let hook = streamImpl.createReadableStreamHook(stream);
|
|
1855
|
+
this.hooks.push(hook);
|
|
1856
|
+
return stream;
|
|
1857
|
+
}
|
|
1708
1858
|
}
|
|
1709
1859
|
throw new TypeError(`unknown special value: ${JSON.stringify(value)}`);
|
|
1710
1860
|
} else if (value instanceof Object) {
|
|
@@ -1749,6 +1899,7 @@ function estimateEncodedSize(value, seen, depth = 0) {
|
|
|
1749
1899
|
if (value instanceof ArrayBuffer) return ESTIMATED_BINARY_OVERHEAD + value.byteLength;
|
|
1750
1900
|
if (typeof Blob !== "undefined" && value instanceof Blob) return ESTIMATED_BINARY_OVERHEAD + value.size;
|
|
1751
1901
|
if (value instanceof Date) return 16;
|
|
1902
|
+
if (value instanceof RegExp) return ESTIMATED_OBJECT_OVERHEAD + estimateStringSize(value.source) + estimateStringSize(value.flags);
|
|
1752
1903
|
seen ??= /* @__PURE__ */ new WeakSet();
|
|
1753
1904
|
if (seen.has(value)) return ESTIMATED_ENTRY_OVERHEAD;
|
|
1754
1905
|
seen.add(value);
|
|
@@ -1772,10 +1923,11 @@ function estimateEncodedSize(value, seen, depth = 0) {
|
|
|
1772
1923
|
var ImportTableEntry = class {
|
|
1773
1924
|
session;
|
|
1774
1925
|
importId;
|
|
1926
|
+
pulling;
|
|
1775
1927
|
constructor(session, importId, pulling) {
|
|
1776
1928
|
this.session = session;
|
|
1777
1929
|
this.importId = importId;
|
|
1778
|
-
|
|
1930
|
+
this.pulling = pulling;
|
|
1779
1931
|
}
|
|
1780
1932
|
localRefcount = 0;
|
|
1781
1933
|
remoteRefcount = 1;
|
|
@@ -1806,7 +1958,7 @@ var ImportTableEntry = class {
|
|
|
1806
1958
|
}
|
|
1807
1959
|
async awaitResolution() {
|
|
1808
1960
|
if (!this.activePull) {
|
|
1809
|
-
this.session.sendPull(this.importId);
|
|
1961
|
+
if (!this.pulling) this.session.sendPull(this.importId);
|
|
1810
1962
|
this.activePull = Promise.withResolvers();
|
|
1811
1963
|
}
|
|
1812
1964
|
await this.activePull.promise;
|
|
@@ -2036,6 +2188,7 @@ var RpcSessionImpl = class {
|
|
|
2036
2188
|
let autoRelease = exp.autoRelease;
|
|
2037
2189
|
++this.pullCount;
|
|
2038
2190
|
exp.pull = resolve().then((payload) => {
|
|
2191
|
+
if (this.exports[exportId] !== exp) return;
|
|
2039
2192
|
let value = Devaluator.devaluate(payload.value, void 0, this, payload, this.encodingLevel);
|
|
2040
2193
|
this.send([
|
|
2041
2194
|
"resolve",
|
|
@@ -2044,6 +2197,7 @@ var RpcSessionImpl = class {
|
|
|
2044
2197
|
]);
|
|
2045
2198
|
if (autoRelease) this.releaseExport(exportId, 1);
|
|
2046
2199
|
}, (error) => {
|
|
2200
|
+
if (this.exports[exportId] !== exp) return;
|
|
2047
2201
|
this.send([
|
|
2048
2202
|
"reject",
|
|
2049
2203
|
exportId,
|
|
@@ -2069,7 +2223,7 @@ var RpcSessionImpl = class {
|
|
|
2069
2223
|
}
|
|
2070
2224
|
}
|
|
2071
2225
|
getImport(hook) {
|
|
2072
|
-
if (hook instanceof RpcImportHook && hook.entry && hook.entry.session === this) return hook.entry.importId;
|
|
2226
|
+
if (hook instanceof RpcImportHook && hook.entry && hook.entry.session === this && !hook.entry.resolution) return hook.entry.importId;
|
|
2073
2227
|
else return;
|
|
2074
2228
|
}
|
|
2075
2229
|
importStub(idx) {
|
|
@@ -2163,6 +2317,7 @@ var RpcSessionImpl = class {
|
|
|
2163
2317
|
value.push(devalue[0]);
|
|
2164
2318
|
}
|
|
2165
2319
|
this.send(["push", value]);
|
|
2320
|
+
args?.dispose();
|
|
2166
2321
|
let entry = new ImportTableEntry(this, this.imports.length, false);
|
|
2167
2322
|
this.imports.push(entry);
|
|
2168
2323
|
return new RpcImportHook(true, entry);
|
|
@@ -2187,6 +2342,7 @@ var RpcSessionImpl = class {
|
|
|
2187
2342
|
value.push(devalue[0]);
|
|
2188
2343
|
let msg = ["stream", value];
|
|
2189
2344
|
let size = this.send(msg);
|
|
2345
|
+
args.dispose();
|
|
2190
2346
|
if (size === void 0) size = estimateEncodedSize(msg);
|
|
2191
2347
|
let importId = this.imports.length;
|
|
2192
2348
|
let entry = new ImportTableEntry(this, importId, true);
|
|
@@ -2284,7 +2440,8 @@ var RpcSessionImpl = class {
|
|
|
2284
2440
|
if (msg instanceof Array) switch (msg[0]) {
|
|
2285
2441
|
case "push":
|
|
2286
2442
|
if (msg.length > 1) {
|
|
2287
|
-
let
|
|
2443
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[1]);
|
|
2444
|
+
let hook = new PayloadStubHook(payload);
|
|
2288
2445
|
hook.ignoreUnhandledRejections();
|
|
2289
2446
|
this.exports.push({
|
|
2290
2447
|
hook,
|
|
@@ -2295,7 +2452,8 @@ var RpcSessionImpl = class {
|
|
|
2295
2452
|
break;
|
|
2296
2453
|
case "stream":
|
|
2297
2454
|
if (msg.length > 1) {
|
|
2298
|
-
let
|
|
2455
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[1]);
|
|
2456
|
+
let hook = new PayloadStubHook(payload);
|
|
2299
2457
|
hook.ignoreUnhandledRejections();
|
|
2300
2458
|
let exportId = this.exports.length;
|
|
2301
2459
|
this.exports.push({
|
|
@@ -2330,13 +2488,14 @@ var RpcSessionImpl = class {
|
|
|
2330
2488
|
let importId = msg[1];
|
|
2331
2489
|
if (typeof importId == "number" && msg.length > 2) {
|
|
2332
2490
|
let imp = this.imports[importId];
|
|
2333
|
-
if (imp)
|
|
2334
|
-
|
|
2335
|
-
|
|
2336
|
-
|
|
2337
|
-
|
|
2338
|
-
|
|
2339
|
-
|
|
2491
|
+
if (imp) {
|
|
2492
|
+
if (msg[0] == "resolve") imp.resolve(new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[2])));
|
|
2493
|
+
else {
|
|
2494
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[2]);
|
|
2495
|
+
payload.dispose();
|
|
2496
|
+
imp.resolve(new ErrorStubHook(payload.value));
|
|
2497
|
+
}
|
|
2498
|
+
} else if (msg[0] == "resolve") new Evaluator(this, this.encodingLevel).evaluate(msg[2]).dispose();
|
|
2340
2499
|
continue;
|
|
2341
2500
|
}
|
|
2342
2501
|
break;
|
|
@@ -2408,7 +2567,8 @@ var RpcSession$1 = class {
|
|
|
2408
2567
|
const MAX_CLOSE_REASON_BYTES = 123;
|
|
2409
2568
|
function newWebSocketRpcSession$1(webSocket, localMain, options) {
|
|
2410
2569
|
if (typeof webSocket === "string") webSocket = new WebSocket(webSocket);
|
|
2411
|
-
|
|
2570
|
+
let transport = new WebSocketTransport(webSocket);
|
|
2571
|
+
return new RpcSession$1(transport, localMain, options).getRemoteMain();
|
|
2412
2572
|
}
|
|
2413
2573
|
/**
|
|
2414
2574
|
* For use in Cloudflare Workers: Construct an HTTP response that starts a WebSocket RPC session
|
|
@@ -2445,12 +2605,13 @@ var WebSocketTransport = class {
|
|
|
2445
2605
|
});
|
|
2446
2606
|
}
|
|
2447
2607
|
webSocket.addEventListener("message", (event) => {
|
|
2448
|
-
if (this.#error) {} else if (typeof event.data === "string" || event.data instanceof ArrayBuffer)
|
|
2449
|
-
this.#receiveResolver
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
|
|
2453
|
-
|
|
2608
|
+
if (this.#error) {} else if (typeof event.data === "string" || event.data instanceof ArrayBuffer) {
|
|
2609
|
+
if (this.#receiveResolver) {
|
|
2610
|
+
this.#receiveResolver(event.data);
|
|
2611
|
+
this.#receiveResolver = void 0;
|
|
2612
|
+
this.#receiveRejecter = void 0;
|
|
2613
|
+
} else this.#receiveQueue.push(event.data);
|
|
2614
|
+
} else this.#receivedError(/* @__PURE__ */ new TypeError("Received unexpected message type from WebSocket."));
|
|
2454
2615
|
});
|
|
2455
2616
|
webSocket.addEventListener("close", (event) => {
|
|
2456
2617
|
this.#receivedError(/* @__PURE__ */ new Error(`Peer closed WebSocket: ${event.code} ${event.reason}`));
|
|
@@ -2542,7 +2703,8 @@ function newHttpBatchRpcSession$1(urlOrRequest, options) {
|
|
|
2542
2703
|
let body = await response.text();
|
|
2543
2704
|
return body == "" ? [] : body.split("\n");
|
|
2544
2705
|
};
|
|
2545
|
-
|
|
2706
|
+
let transport = new BatchClientTransport(sendBatch);
|
|
2707
|
+
return new RpcSession$1(transport, void 0, options).getRemoteMain();
|
|
2546
2708
|
}
|
|
2547
2709
|
var BatchServerTransport = class {
|
|
2548
2710
|
constructor(batch) {
|
|
@@ -2585,7 +2747,8 @@ var BatchServerTransport = class {
|
|
|
2585
2747
|
async function newHttpBatchRpcResponse(request, localMain, options) {
|
|
2586
2748
|
if (request.method !== "POST") return new Response("This endpoint only accepts POST requests.", { status: 405 });
|
|
2587
2749
|
let body = await request.text();
|
|
2588
|
-
let
|
|
2750
|
+
let batch = body === "" ? [] : body.split("\n");
|
|
2751
|
+
let transport = new BatchServerTransport(batch);
|
|
2589
2752
|
let rpc = new RpcSession$1(transport, localMain, options);
|
|
2590
2753
|
await transport.whenAllReceived();
|
|
2591
2754
|
await rpc.drain();
|
|
@@ -2615,7 +2778,8 @@ async function nodeHttpBatchRpcResponse(request, response, localMain, options) {
|
|
|
2615
2778
|
});
|
|
2616
2779
|
request.on("error", reject);
|
|
2617
2780
|
});
|
|
2618
|
-
let
|
|
2781
|
+
let batch = body === "" ? [] : body.split("\n");
|
|
2782
|
+
let transport = new BatchServerTransport(batch);
|
|
2619
2783
|
let rpc = new RpcSession$1(transport, localMain, options);
|
|
2620
2784
|
await transport.whenAllReceived();
|
|
2621
2785
|
await rpc.drain();
|
|
@@ -2626,7 +2790,8 @@ async function nodeHttpBatchRpcResponse(request, response, localMain, options) {
|
|
|
2626
2790
|
//#endregion
|
|
2627
2791
|
//#region src/messageport.ts
|
|
2628
2792
|
function newMessagePortRpcSession$1(port, localMain, options) {
|
|
2629
|
-
|
|
2793
|
+
let transport = new MessagePortTransport(port);
|
|
2794
|
+
return new RpcSession$1(transport, localMain, options).getRemoteMain();
|
|
2630
2795
|
}
|
|
2631
2796
|
var MessagePortTransport = class {
|
|
2632
2797
|
encodingLevel = "structuredClonable";
|
|
@@ -2876,7 +3041,8 @@ var MapApplicator = class {
|
|
|
2876
3041
|
}
|
|
2877
3042
|
};
|
|
2878
3043
|
function applyMapToElement(input, parent, owner, captures, instructions) {
|
|
2879
|
-
let
|
|
3044
|
+
let inputHook = new PayloadStubHook(RpcPayload.deepCopyFrom(input, parent, owner));
|
|
3045
|
+
let mapper = new MapApplicator(captures, inputHook);
|
|
2880
3046
|
try {
|
|
2881
3047
|
return mapper.apply(instructions);
|
|
2882
3048
|
} finally {
|
|
@@ -2909,9 +3075,10 @@ mapImpl.applyMap = (input, parent, owner, captures, instructions) => {
|
|
|
2909
3075
|
var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
|
|
2910
3076
|
state;
|
|
2911
3077
|
static create(stream) {
|
|
3078
|
+
let writer = stream.getWriter();
|
|
2912
3079
|
return new WritableStreamStubHook({
|
|
2913
3080
|
refcount: 1,
|
|
2914
|
-
writer
|
|
3081
|
+
writer,
|
|
2915
3082
|
closed: false
|
|
2916
3083
|
});
|
|
2917
3084
|
}
|
|
@@ -2931,7 +3098,8 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
|
|
|
2931
3098
|
const method = path[0];
|
|
2932
3099
|
if (method !== "write" && method !== "close" && method !== "abort") throw new Error(`Unknown WritableStream method: ${method}`);
|
|
2933
3100
|
if (method === "close" || method === "abort") state.closed = true;
|
|
2934
|
-
|
|
3101
|
+
let promise = method === "write" ? args.deliverStreamWrite(state.writer) : args.deliverCall(state.writer[method], state.writer);
|
|
3102
|
+
return new PromiseStubHook(promise.then((payload) => new PayloadStubHook(payload)));
|
|
2935
3103
|
} catch (err) {
|
|
2936
3104
|
args.dispose();
|
|
2937
3105
|
return new ErrorStubHook(err);
|
|
@@ -2945,7 +3113,8 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
|
|
|
2945
3113
|
return new ErrorStubHook(/* @__PURE__ */ new Error("Cannot access properties on a WritableStream stub"));
|
|
2946
3114
|
}
|
|
2947
3115
|
dup() {
|
|
2948
|
-
|
|
3116
|
+
let state = this.getState();
|
|
3117
|
+
return new WritableStreamStubHook(state, this);
|
|
2949
3118
|
}
|
|
2950
3119
|
pull() {
|
|
2951
3120
|
return Promise.reject(/* @__PURE__ */ new Error("Cannot pull a WritableStream stub"));
|
|
@@ -2963,9 +3132,9 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
|
|
|
2963
3132
|
}
|
|
2964
3133
|
onBroken(callback) {}
|
|
2965
3134
|
};
|
|
2966
|
-
const INITIAL_WINDOW =
|
|
2967
|
-
const MAX_WINDOW =
|
|
2968
|
-
const MIN_WINDOW =
|
|
3135
|
+
const INITIAL_WINDOW = 262144;
|
|
3136
|
+
const MAX_WINDOW = 1073741824;
|
|
3137
|
+
const MIN_WINDOW = 65536;
|
|
2969
3138
|
const STARTUP_GROWTH_FACTOR = 2;
|
|
2970
3139
|
const STEADY_GROWTH_FACTOR = 1.25;
|
|
2971
3140
|
const DECAY_FACTOR = .9;
|