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-bun.cjs
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
|
|
3
2
|
//#region src/symbols.ts
|
|
4
3
|
let WORKERS_MODULE_SYMBOL = Symbol("workers-module");
|
|
5
4
|
|
|
@@ -42,6 +41,9 @@ function typeForRpc(value) {
|
|
|
42
41
|
case AsyncFunction.prototype: return "function";
|
|
43
42
|
case Array.prototype: return "array";
|
|
44
43
|
case Date.prototype: return "date";
|
|
44
|
+
case RegExp.prototype: return "regexp";
|
|
45
|
+
case Set.prototype: return "set";
|
|
46
|
+
case Map.prototype: return "map";
|
|
45
47
|
case Uint8Array.prototype:
|
|
46
48
|
case BUFFER_PROTOTYPE:
|
|
47
49
|
case ArrayBuffer.prototype:
|
|
@@ -305,6 +307,9 @@ async function pullPromise(promise) {
|
|
|
305
307
|
if (pathIfPromise.length > 0) hook = hook.get(pathIfPromise);
|
|
306
308
|
return (await hook.pull()).deliverResolve();
|
|
307
309
|
}
|
|
310
|
+
function mapKeyToPropertyName(key) {
|
|
311
|
+
return typeof key === "string" || typeof key === "number" ? key : { mapKey: key };
|
|
312
|
+
}
|
|
308
313
|
var RpcPayload = class RpcPayload {
|
|
309
314
|
value;
|
|
310
315
|
source;
|
|
@@ -359,12 +364,13 @@ var RpcPayload = class RpcPayload {
|
|
|
359
364
|
return TargetStubHook.create(target, parent);
|
|
360
365
|
} else if (this.source === "return") {
|
|
361
366
|
let hook = this.rpcTargets?.get(target);
|
|
362
|
-
if (hook)
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
367
|
+
if (hook) {
|
|
368
|
+
if (dupStubs) return hook.dup();
|
|
369
|
+
else {
|
|
370
|
+
this.rpcTargets?.delete(target);
|
|
371
|
+
return hook;
|
|
372
|
+
}
|
|
373
|
+
} else {
|
|
368
374
|
hook = TargetStubHook.create(target, parent);
|
|
369
375
|
if (dupStubs) {
|
|
370
376
|
if (!this.rpcTargets) this.rpcTargets = /* @__PURE__ */ new Map();
|
|
@@ -378,12 +384,13 @@ var RpcPayload = class RpcPayload {
|
|
|
378
384
|
if (this.source === "params") return streamImpl.createWritableStreamHook(stream);
|
|
379
385
|
else if (this.source === "return") {
|
|
380
386
|
let hook = this.rpcTargets?.get(stream);
|
|
381
|
-
if (hook)
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
+
if (hook) {
|
|
388
|
+
if (dupStubs) return hook.dup();
|
|
389
|
+
else {
|
|
390
|
+
this.rpcTargets?.delete(stream);
|
|
391
|
+
return hook;
|
|
392
|
+
}
|
|
393
|
+
} else {
|
|
387
394
|
hook = streamImpl.createWritableStreamHook(stream);
|
|
388
395
|
if (dupStubs) {
|
|
389
396
|
if (!this.rpcTargets) this.rpcTargets = /* @__PURE__ */ new Map();
|
|
@@ -397,12 +404,13 @@ var RpcPayload = class RpcPayload {
|
|
|
397
404
|
if (this.source === "params") return streamImpl.createReadableStreamHook(stream);
|
|
398
405
|
else if (this.source === "return") {
|
|
399
406
|
let hook = this.rpcTargets?.get(stream);
|
|
400
|
-
if (hook)
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
407
|
+
if (hook) {
|
|
408
|
+
if (dupStubs) return hook.dup();
|
|
409
|
+
else {
|
|
410
|
+
this.rpcTargets?.delete(stream);
|
|
411
|
+
return hook;
|
|
412
|
+
}
|
|
413
|
+
} else {
|
|
406
414
|
hook = streamImpl.createReadableStreamHook(stream);
|
|
407
415
|
if (dupStubs) {
|
|
408
416
|
if (!this.rpcTargets) this.rpcTargets = /* @__PURE__ */ new Map();
|
|
@@ -410,7 +418,7 @@ var RpcPayload = class RpcPayload {
|
|
|
410
418
|
return hook.dup();
|
|
411
419
|
} else return hook;
|
|
412
420
|
}
|
|
413
|
-
} else
|
|
421
|
+
} else return streamImpl.createReadableStreamHook(stream);
|
|
414
422
|
}
|
|
415
423
|
deepCopy(value, oldParent, property, parent, dupStubs, owner) {
|
|
416
424
|
switch (typeForRpc(value)) {
|
|
@@ -421,6 +429,7 @@ var RpcPayload = class RpcPayload {
|
|
|
421
429
|
case "bytes":
|
|
422
430
|
case "blob":
|
|
423
431
|
case "url":
|
|
432
|
+
case "regexp":
|
|
424
433
|
case "error":
|
|
425
434
|
case "undefined": return value;
|
|
426
435
|
case "array": {
|
|
@@ -430,6 +439,35 @@ var RpcPayload = class RpcPayload {
|
|
|
430
439
|
for (let i = 0; i < len; i++) result[i] = this.deepCopy(array[i], array, i, result, dupStubs, owner);
|
|
431
440
|
return result;
|
|
432
441
|
}
|
|
442
|
+
case "set": {
|
|
443
|
+
let set = value;
|
|
444
|
+
let elements = [...set];
|
|
445
|
+
for (let val of elements) {
|
|
446
|
+
let kind = typeForRpc(val);
|
|
447
|
+
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.");
|
|
448
|
+
}
|
|
449
|
+
let result = /* @__PURE__ */ new Set();
|
|
450
|
+
let index = 0;
|
|
451
|
+
for (let val of elements) {
|
|
452
|
+
let copy = this.deepCopy(val, set, index++, result, dupStubs, owner);
|
|
453
|
+
result.add(copy);
|
|
454
|
+
}
|
|
455
|
+
return result;
|
|
456
|
+
}
|
|
457
|
+
case "map": {
|
|
458
|
+
let map = value;
|
|
459
|
+
let entries = [...map];
|
|
460
|
+
for (let [key] of entries) {
|
|
461
|
+
let kind = typeForRpc(key);
|
|
462
|
+
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.");
|
|
463
|
+
}
|
|
464
|
+
let result = /* @__PURE__ */ new Map();
|
|
465
|
+
for (let [key, val] of entries) {
|
|
466
|
+
let keyCopy = this.deepCopy(key, map, -1, result, dupStubs, owner);
|
|
467
|
+
result.set(keyCopy, this.deepCopy(val, map, mapKeyToPropertyName(keyCopy), result, dupStubs, owner));
|
|
468
|
+
}
|
|
469
|
+
return result;
|
|
470
|
+
}
|
|
433
471
|
case "object": {
|
|
434
472
|
let result = {};
|
|
435
473
|
let object = value;
|
|
@@ -487,8 +525,10 @@ var RpcPayload = class RpcPayload {
|
|
|
487
525
|
case "readable": {
|
|
488
526
|
let stream = value;
|
|
489
527
|
let hook;
|
|
490
|
-
if (owner)
|
|
491
|
-
|
|
528
|
+
if (owner) {
|
|
529
|
+
if (owner.source === "owned") throw new Error("Can't copy a ReadableStream.");
|
|
530
|
+
hook = owner.getHookForReadableStream(stream, oldParent, dupStubs);
|
|
531
|
+
} else hook = streamImpl.createReadableStreamHook(stream);
|
|
492
532
|
this.hooks.push(hook);
|
|
493
533
|
return stream;
|
|
494
534
|
}
|
|
@@ -514,6 +554,10 @@ var RpcPayload = class RpcPayload {
|
|
|
514
554
|
try {
|
|
515
555
|
this.value = this.deepCopy(this.value, void 0, "value", this, dupStubs, this);
|
|
516
556
|
} catch (err) {
|
|
557
|
+
if (dupStubs) {
|
|
558
|
+
this.hooks.forEach((hook) => hook.dispose());
|
|
559
|
+
this.promises.forEach((promise) => promise.promise[Symbol.dispose]());
|
|
560
|
+
}
|
|
517
561
|
this.hooks = void 0;
|
|
518
562
|
this.promises = void 0;
|
|
519
563
|
throw err;
|
|
@@ -527,7 +571,8 @@ var RpcPayload = class RpcPayload {
|
|
|
527
571
|
this.ensureDeepCopied();
|
|
528
572
|
if (this.value instanceof RpcPromise$1) RpcPayload.deliverRpcPromiseTo(this.value, parent, property, promises);
|
|
529
573
|
else {
|
|
530
|
-
parent
|
|
574
|
+
if (parent instanceof Map) parent.set(typeof property === "object" ? property.mapKey : property, this.value);
|
|
575
|
+
else parent[property] = this.value;
|
|
531
576
|
for (let record of this.promises) RpcPayload.deliverRpcPromiseTo(record.promise, record.parent, record.property, promises);
|
|
532
577
|
}
|
|
533
578
|
}
|
|
@@ -619,6 +664,7 @@ var RpcPayload = class RpcPayload {
|
|
|
619
664
|
case "blob":
|
|
620
665
|
case "date":
|
|
621
666
|
case "url":
|
|
667
|
+
case "regexp":
|
|
622
668
|
case "error":
|
|
623
669
|
case "undefined": return;
|
|
624
670
|
case "array": {
|
|
@@ -627,6 +673,19 @@ var RpcPayload = class RpcPayload {
|
|
|
627
673
|
for (let i = 0; i < len; i++) this.disposeImpl(array[i], array);
|
|
628
674
|
return;
|
|
629
675
|
}
|
|
676
|
+
case "set": {
|
|
677
|
+
let set = value;
|
|
678
|
+
for (let element of set) this.disposeImpl(element, set);
|
|
679
|
+
return;
|
|
680
|
+
}
|
|
681
|
+
case "map": {
|
|
682
|
+
let map = value;
|
|
683
|
+
for (let [key, val] of map) {
|
|
684
|
+
this.disposeImpl(key, map);
|
|
685
|
+
this.disposeImpl(val, map);
|
|
686
|
+
}
|
|
687
|
+
return;
|
|
688
|
+
}
|
|
630
689
|
case "object": {
|
|
631
690
|
let object = value;
|
|
632
691
|
for (let i in object) this.disposeImpl(object[i], object);
|
|
@@ -702,6 +761,7 @@ var RpcPayload = class RpcPayload {
|
|
|
702
761
|
case "writable":
|
|
703
762
|
case "readable":
|
|
704
763
|
case "url":
|
|
764
|
+
case "regexp":
|
|
705
765
|
case "headers":
|
|
706
766
|
case "request":
|
|
707
767
|
case "response": return;
|
|
@@ -711,6 +771,19 @@ var RpcPayload = class RpcPayload {
|
|
|
711
771
|
for (let i = 0; i < len; i++) this.ignoreUnhandledRejectionsImpl(array[i]);
|
|
712
772
|
return;
|
|
713
773
|
}
|
|
774
|
+
case "set": {
|
|
775
|
+
let set = value;
|
|
776
|
+
for (let element of set) this.ignoreUnhandledRejectionsImpl(element);
|
|
777
|
+
return;
|
|
778
|
+
}
|
|
779
|
+
case "map": {
|
|
780
|
+
let map = value;
|
|
781
|
+
for (let [key, val] of map) {
|
|
782
|
+
this.ignoreUnhandledRejectionsImpl(key);
|
|
783
|
+
this.ignoreUnhandledRejectionsImpl(val);
|
|
784
|
+
}
|
|
785
|
+
return;
|
|
786
|
+
}
|
|
714
787
|
case "object": {
|
|
715
788
|
let object = value;
|
|
716
789
|
for (let i in object) this.ignoreUnhandledRejectionsImpl(object[i]);
|
|
@@ -770,8 +843,11 @@ function followPath(value, parent, path, owner) {
|
|
|
770
843
|
case "bytes":
|
|
771
844
|
case "blob":
|
|
772
845
|
case "date":
|
|
846
|
+
case "set":
|
|
847
|
+
case "map":
|
|
773
848
|
case "error":
|
|
774
849
|
case "url":
|
|
850
|
+
case "regexp":
|
|
775
851
|
case "headers":
|
|
776
852
|
case "request":
|
|
777
853
|
case "response":
|
|
@@ -817,7 +893,8 @@ var ValueStubHook = class extends StubHook {
|
|
|
817
893
|
args.dispose();
|
|
818
894
|
return new ErrorStubHook(/* @__PURE__ */ new TypeError(`'${path.join(".")}' is not a function.`));
|
|
819
895
|
}
|
|
820
|
-
|
|
896
|
+
let promise = args.deliverCall(followResult.value, followResult.parent);
|
|
897
|
+
return new PromiseStubHook(promise.then((payload) => {
|
|
821
898
|
return new PayloadStubHook(payload);
|
|
822
899
|
}));
|
|
823
900
|
}
|
|
@@ -1015,7 +1092,7 @@ const DEFAULT_MAX_DEPTH = 256;
|
|
|
1015
1092
|
const DEFAULT_LIMITS = {
|
|
1016
1093
|
maxBigIntDigits: 16384,
|
|
1017
1094
|
maxDepth: 256,
|
|
1018
|
-
maxMessageSize:
|
|
1095
|
+
maxMessageSize: 33554432
|
|
1019
1096
|
};
|
|
1020
1097
|
const NATIVE_LITTLE_ENDIAN = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
|
|
1021
1098
|
const BYTE_CONTAINER_TYPE_NAMES = [
|
|
@@ -1077,9 +1154,7 @@ function swapByteOrder(bytes, elementSize) {
|
|
|
1077
1154
|
case 4:
|
|
1078
1155
|
view.setUint32(offset, view.getUint32(offset, false), true);
|
|
1079
1156
|
break;
|
|
1080
|
-
case 8:
|
|
1081
|
-
view.setBigUint64(offset, view.getBigUint64(offset, false), true);
|
|
1082
|
-
break;
|
|
1157
|
+
case 8: view.setBigUint64(offset, view.getBigUint64(offset, false), true);
|
|
1083
1158
|
}
|
|
1084
1159
|
}
|
|
1085
1160
|
var NullExporter = class {
|
|
@@ -1164,6 +1239,30 @@ var Devaluator = class Devaluator {
|
|
|
1164
1239
|
for (let i = 0; i < len; i++) result[i] = this.devaluateImpl(array[i], array, depth + 1);
|
|
1165
1240
|
return [result];
|
|
1166
1241
|
}
|
|
1242
|
+
case "set": {
|
|
1243
|
+
let set = value;
|
|
1244
|
+
let setElements = [...set];
|
|
1245
|
+
let elements = [];
|
|
1246
|
+
for (let element of setElements) switch (typeForRpc(element)) {
|
|
1247
|
+
case "blob": throw new TypeError("Cannot serialize a Blob as an element of a Set.");
|
|
1248
|
+
case "rpc-promise":
|
|
1249
|
+
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.");
|
|
1250
|
+
}
|
|
1251
|
+
for (let element of setElements) elements.push(this.devaluateImpl(element, set, depth + 1));
|
|
1252
|
+
return ["set", elements];
|
|
1253
|
+
}
|
|
1254
|
+
case "map": {
|
|
1255
|
+
let map = value;
|
|
1256
|
+
let mapEntries = [...map];
|
|
1257
|
+
let entries = [];
|
|
1258
|
+
for (let [key] of mapEntries) switch (typeForRpc(key)) {
|
|
1259
|
+
case "blob": throw new TypeError("Cannot serialize a Blob as a key of a Map.");
|
|
1260
|
+
case "rpc-promise":
|
|
1261
|
+
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.");
|
|
1262
|
+
}
|
|
1263
|
+
for (let [key, val] of mapEntries) entries.push([this.devaluateImpl(key, map, depth + 1), this.devaluateImpl(val, map, depth + 1)]);
|
|
1264
|
+
return ["map", entries];
|
|
1265
|
+
}
|
|
1167
1266
|
case "bigint":
|
|
1168
1267
|
if (this.encodingLevel === "structuredClonable") return value;
|
|
1169
1268
|
return ["bigint", value.toString()];
|
|
@@ -1172,6 +1271,15 @@ var Devaluator = class Devaluator {
|
|
|
1172
1271
|
const time = value.getTime();
|
|
1173
1272
|
return ["date", Number.isNaN(time) ? null : time];
|
|
1174
1273
|
}
|
|
1274
|
+
case "regexp": {
|
|
1275
|
+
if (this.encodingLevel === "structuredClonable") return value;
|
|
1276
|
+
let re = value;
|
|
1277
|
+
return re.flags ? [
|
|
1278
|
+
"regexp",
|
|
1279
|
+
re.source,
|
|
1280
|
+
re.flags
|
|
1281
|
+
] : ["regexp", re.source];
|
|
1282
|
+
}
|
|
1175
1283
|
case "bytes": {
|
|
1176
1284
|
let alternateTypeName = BYTE_CONTAINER_TYPE_BY_PROTOTYPE.get(Object.getPrototypeOf(value));
|
|
1177
1285
|
let bytes;
|
|
@@ -1186,11 +1294,14 @@ var Devaluator = class Devaluator {
|
|
|
1186
1294
|
swapByteOrder(bytes, elementSize);
|
|
1187
1295
|
}
|
|
1188
1296
|
}
|
|
1189
|
-
if (this.encodingLevel === "structuredClonable" || this.encodingLevel === "jsonCompatibleWithBytes")
|
|
1190
|
-
|
|
1191
|
-
bytes,
|
|
1192
|
-
|
|
1193
|
-
|
|
1297
|
+
if (this.encodingLevel === "structuredClonable" || this.encodingLevel === "jsonCompatibleWithBytes") {
|
|
1298
|
+
if (bytes.byteLength !== bytes.buffer.byteLength) bytes = new Uint8Array(bytes);
|
|
1299
|
+
return alternateTypeName === void 0 ? ["bytes", bytes] : [
|
|
1300
|
+
"bytes",
|
|
1301
|
+
bytes,
|
|
1302
|
+
alternateTypeName
|
|
1303
|
+
];
|
|
1304
|
+
}
|
|
1194
1305
|
let b64;
|
|
1195
1306
|
if (bytes.toBase64) b64 = bytes.toBase64({ omitPadding: true });
|
|
1196
1307
|
else if (typeof Buffer !== "undefined") b64 = (bytes instanceof Buffer ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)).toString("base64");
|
|
@@ -1329,13 +1440,16 @@ var Devaluator = class Devaluator {
|
|
|
1329
1440
|
if (!this.source) throw new Error("Can't serialize RPC stubs in this context.");
|
|
1330
1441
|
let { hook, pathIfPromise } = unwrapStubAndPath(value);
|
|
1331
1442
|
let importId = this.exporter.getImport(hook);
|
|
1332
|
-
if (importId !== void 0)
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1443
|
+
if (importId !== void 0) {
|
|
1444
|
+
if (pathIfPromise) {
|
|
1445
|
+
if (pathIfPromise.length > 0) return [
|
|
1446
|
+
"pipeline",
|
|
1447
|
+
importId,
|
|
1448
|
+
pathIfPromise
|
|
1449
|
+
];
|
|
1450
|
+
else return ["pipeline", importId];
|
|
1451
|
+
} else return ["import", importId];
|
|
1452
|
+
}
|
|
1339
1453
|
if (pathIfPromise) hook = hook.get(pathIfPromise);
|
|
1340
1454
|
else hook = hook.dup();
|
|
1341
1455
|
return this.devaluateHook(pathIfPromise ? "promise" : "export", hook);
|
|
@@ -1396,16 +1510,18 @@ var NullImporter = class {
|
|
|
1396
1510
|
};
|
|
1397
1511
|
const NULL_IMPORTER = new NullImporter();
|
|
1398
1512
|
function fixBrokenRequestBody(request, body) {
|
|
1399
|
-
|
|
1513
|
+
let promise = new Response(body).arrayBuffer().then((arrayBuffer) => {
|
|
1400
1514
|
let bytes = new Uint8Array(arrayBuffer);
|
|
1401
1515
|
let result = new Request(request, { body: bytes });
|
|
1402
1516
|
return new PayloadStubHook(RpcPayload.fromAppReturn(result));
|
|
1403
|
-
})
|
|
1517
|
+
});
|
|
1518
|
+
return new RpcPromise$1(new PromiseStubHook(promise), []);
|
|
1404
1519
|
}
|
|
1405
1520
|
function streamToBlobPromise(stream, type) {
|
|
1406
|
-
|
|
1521
|
+
let promise = streamToBlob(stream, type).then((blob) => {
|
|
1407
1522
|
return new PayloadStubHook(RpcPayload.fromAppReturn(blob));
|
|
1408
|
-
})
|
|
1523
|
+
});
|
|
1524
|
+
return new RpcPromise$1(new PromiseStubHook(promise), []);
|
|
1409
1525
|
}
|
|
1410
1526
|
var Evaluator = class Evaluator {
|
|
1411
1527
|
importer;
|
|
@@ -1438,7 +1554,7 @@ var Evaluator = class Evaluator {
|
|
|
1438
1554
|
let maxDepth = this.limits.maxDepth;
|
|
1439
1555
|
if (depth >= maxDepth) throw new TypeError(`Deserialization exceeded maximum allowed message depth of ${maxDepth}.`);
|
|
1440
1556
|
if (this.encodingLevel === "structuredClonable") {
|
|
1441
|
-
if (value instanceof Date || typeof value === "bigint") return value;
|
|
1557
|
+
if (value instanceof Date || value instanceof RegExp || typeof value === "bigint") return value;
|
|
1442
1558
|
}
|
|
1443
1559
|
if (value instanceof Array) {
|
|
1444
1560
|
if (value.length == 1 && value[0] instanceof Array) {
|
|
@@ -1458,18 +1574,48 @@ var Evaluator = class Evaluator {
|
|
|
1458
1574
|
if (value[1] === null) return /* @__PURE__ */ new Date(NaN);
|
|
1459
1575
|
if (typeof value[1] == "number") return new Date(value[1]);
|
|
1460
1576
|
break;
|
|
1577
|
+
case "regexp":
|
|
1578
|
+
if (typeof value[1] === "string" && (value.length === 2 || value.length === 3 && typeof value[2] === "string")) return new RegExp(value[1], value[2]);
|
|
1579
|
+
break;
|
|
1580
|
+
case "set":
|
|
1581
|
+
if (value.length === 2 && value[1] instanceof Array) {
|
|
1582
|
+
let elements = value[1];
|
|
1583
|
+
let set = /* @__PURE__ */ new Set();
|
|
1584
|
+
for (let i = 0; i < elements.length; i++) {
|
|
1585
|
+
let copy = this.evaluateImpl(elements[i], set, i, depth + 1);
|
|
1586
|
+
if (copy instanceof RpcPromise$1) throw new TypeError("Cannot deserialize a promise as an element of a Set.");
|
|
1587
|
+
set.add(copy);
|
|
1588
|
+
}
|
|
1589
|
+
return set;
|
|
1590
|
+
}
|
|
1591
|
+
break;
|
|
1592
|
+
case "map":
|
|
1593
|
+
if (value.length === 2 && value[1] instanceof Array) {
|
|
1594
|
+
let entries = value[1];
|
|
1595
|
+
let map = /* @__PURE__ */ new Map();
|
|
1596
|
+
for (let i = 0; i < entries.length; i++) {
|
|
1597
|
+
let entry = entries[i];
|
|
1598
|
+
if (!(entry instanceof Array) || entry.length !== 2) throw new TypeError("Map entries must be serialized as key/value pairs.");
|
|
1599
|
+
let key = this.evaluateImpl(entry[0], map, i, depth + 1);
|
|
1600
|
+
if (key instanceof RpcPromise$1) throw new TypeError("Cannot deserialize a promise as a key of a Map.");
|
|
1601
|
+
map.set(key, this.evaluateImpl(entry[1], map, mapKeyToPropertyName(key), depth + 1));
|
|
1602
|
+
}
|
|
1603
|
+
return map;
|
|
1604
|
+
}
|
|
1605
|
+
break;
|
|
1461
1606
|
case "bytes": {
|
|
1462
1607
|
let bytes;
|
|
1463
1608
|
if (value[1] instanceof Uint8Array) bytes = value[1];
|
|
1464
|
-
else if (typeof value[1] == "string")
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1609
|
+
else if (typeof value[1] == "string") {
|
|
1610
|
+
if (typeof Buffer !== "undefined") bytes = Buffer.from(value[1], "base64");
|
|
1611
|
+
else if (Uint8Array.fromBase64) bytes = Uint8Array.fromBase64(value[1]);
|
|
1612
|
+
else {
|
|
1613
|
+
let bs = atob(value[1]);
|
|
1614
|
+
let len = bs.length;
|
|
1615
|
+
bytes = new Uint8Array(len);
|
|
1616
|
+
for (let i = 0; i < len; i++) bytes[i] = bs.charCodeAt(i);
|
|
1617
|
+
}
|
|
1618
|
+
} else break;
|
|
1473
1619
|
if (value.length === 2) return bytes;
|
|
1474
1620
|
if (typeof value[2] !== "string") throw new TypeError(`Unknown bytes type marker type: ${typeof value[2]}`);
|
|
1475
1621
|
if (!isValidByteContainerName(value[2])) {
|
|
@@ -1495,7 +1641,6 @@ var Evaluator = class Evaluator {
|
|
|
1495
1641
|
case "BigUint64Array": return new BigUint64Array(buffer);
|
|
1496
1642
|
case "Float32Array": return new Float32Array(buffer);
|
|
1497
1643
|
case "Float64Array": return new Float64Array(buffer);
|
|
1498
|
-
default:
|
|
1499
1644
|
}
|
|
1500
1645
|
}
|
|
1501
1646
|
case "error":
|
|
@@ -1598,11 +1743,13 @@ var Evaluator = class Evaluator {
|
|
|
1598
1743
|
return promise;
|
|
1599
1744
|
} else {
|
|
1600
1745
|
this.hooks.push(hook);
|
|
1601
|
-
return new
|
|
1746
|
+
return new RpcStub$1(hook);
|
|
1602
1747
|
}
|
|
1603
1748
|
};
|
|
1604
|
-
if (value.length == 2)
|
|
1605
|
-
|
|
1749
|
+
if (value.length == 2) {
|
|
1750
|
+
if (isPromise) return addStub(hook.get([]));
|
|
1751
|
+
else return addStub(hook.dup());
|
|
1752
|
+
}
|
|
1606
1753
|
let path = value[2];
|
|
1607
1754
|
if (!(path instanceof Array)) break;
|
|
1608
1755
|
if (!path.every((part) => {
|
|
@@ -1632,7 +1779,8 @@ var Evaluator = class Evaluator {
|
|
|
1632
1779
|
}
|
|
1633
1780
|
});
|
|
1634
1781
|
let instructions = value[4];
|
|
1635
|
-
let
|
|
1782
|
+
let resultHook = hook.map(path, captures, instructions);
|
|
1783
|
+
let promise = new RpcPromise$1(resultHook, []);
|
|
1636
1784
|
this.promises.push({
|
|
1637
1785
|
promise,
|
|
1638
1786
|
parent,
|
|
@@ -1642,18 +1790,21 @@ var Evaluator = class Evaluator {
|
|
|
1642
1790
|
}
|
|
1643
1791
|
case "export":
|
|
1644
1792
|
case "promise":
|
|
1645
|
-
if (typeof value[1] == "number")
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1793
|
+
if (typeof value[1] == "number") {
|
|
1794
|
+
if (value[0] == "promise") {
|
|
1795
|
+
let hook = this.importer.importPromise(value[1]);
|
|
1796
|
+
let promise = new RpcPromise$1(hook, []);
|
|
1797
|
+
this.promises.push({
|
|
1798
|
+
parent,
|
|
1799
|
+
property,
|
|
1800
|
+
promise
|
|
1801
|
+
});
|
|
1802
|
+
return promise;
|
|
1803
|
+
} else {
|
|
1804
|
+
let hook = this.importer.importStub(value[1]);
|
|
1805
|
+
this.hooks.push(hook);
|
|
1806
|
+
return new RpcStub$1(hook);
|
|
1807
|
+
}
|
|
1657
1808
|
}
|
|
1658
1809
|
break;
|
|
1659
1810
|
case "writable":
|
|
@@ -1664,14 +1815,12 @@ var Evaluator = class Evaluator {
|
|
|
1664
1815
|
return stream;
|
|
1665
1816
|
}
|
|
1666
1817
|
break;
|
|
1667
|
-
case "readable":
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
}
|
|
1674
|
-
break;
|
|
1818
|
+
case "readable": if (typeof value[1] == "number") {
|
|
1819
|
+
let stream = this.importer.getPipeReadable(value[1]);
|
|
1820
|
+
let hook = streamImpl.createReadableStreamHook(stream);
|
|
1821
|
+
this.hooks.push(hook);
|
|
1822
|
+
return stream;
|
|
1823
|
+
}
|
|
1675
1824
|
}
|
|
1676
1825
|
throw new TypeError(`unknown special value: ${JSON.stringify(value)}`);
|
|
1677
1826
|
} else if (value instanceof Object) {
|
|
@@ -1716,6 +1865,7 @@ function estimateEncodedSize(value, seen, depth = 0) {
|
|
|
1716
1865
|
if (value instanceof ArrayBuffer) return ESTIMATED_BINARY_OVERHEAD + value.byteLength;
|
|
1717
1866
|
if (typeof Blob !== "undefined" && value instanceof Blob) return ESTIMATED_BINARY_OVERHEAD + value.size;
|
|
1718
1867
|
if (value instanceof Date) return 16;
|
|
1868
|
+
if (value instanceof RegExp) return ESTIMATED_OBJECT_OVERHEAD + estimateStringSize(value.source) + estimateStringSize(value.flags);
|
|
1719
1869
|
seen ??= /* @__PURE__ */ new WeakSet();
|
|
1720
1870
|
if (seen.has(value)) return ESTIMATED_ENTRY_OVERHEAD;
|
|
1721
1871
|
seen.add(value);
|
|
@@ -1739,10 +1889,11 @@ function estimateEncodedSize(value, seen, depth = 0) {
|
|
|
1739
1889
|
var ImportTableEntry = class {
|
|
1740
1890
|
session;
|
|
1741
1891
|
importId;
|
|
1892
|
+
pulling;
|
|
1742
1893
|
constructor(session, importId, pulling) {
|
|
1743
1894
|
this.session = session;
|
|
1744
1895
|
this.importId = importId;
|
|
1745
|
-
|
|
1896
|
+
this.pulling = pulling;
|
|
1746
1897
|
}
|
|
1747
1898
|
localRefcount = 0;
|
|
1748
1899
|
remoteRefcount = 1;
|
|
@@ -1773,7 +1924,7 @@ var ImportTableEntry = class {
|
|
|
1773
1924
|
}
|
|
1774
1925
|
async awaitResolution() {
|
|
1775
1926
|
if (!this.activePull) {
|
|
1776
|
-
this.session.sendPull(this.importId);
|
|
1927
|
+
if (!this.pulling) this.session.sendPull(this.importId);
|
|
1777
1928
|
this.activePull = Promise.withResolvers();
|
|
1778
1929
|
}
|
|
1779
1930
|
await this.activePull.promise;
|
|
@@ -2003,6 +2154,7 @@ var RpcSessionImpl = class {
|
|
|
2003
2154
|
let autoRelease = exp.autoRelease;
|
|
2004
2155
|
++this.pullCount;
|
|
2005
2156
|
exp.pull = resolve().then((payload) => {
|
|
2157
|
+
if (this.exports[exportId] !== exp) return;
|
|
2006
2158
|
let value = Devaluator.devaluate(payload.value, void 0, this, payload, this.encodingLevel);
|
|
2007
2159
|
this.send([
|
|
2008
2160
|
"resolve",
|
|
@@ -2011,6 +2163,7 @@ var RpcSessionImpl = class {
|
|
|
2011
2163
|
]);
|
|
2012
2164
|
if (autoRelease) this.releaseExport(exportId, 1);
|
|
2013
2165
|
}, (error) => {
|
|
2166
|
+
if (this.exports[exportId] !== exp) return;
|
|
2014
2167
|
this.send([
|
|
2015
2168
|
"reject",
|
|
2016
2169
|
exportId,
|
|
@@ -2036,7 +2189,7 @@ var RpcSessionImpl = class {
|
|
|
2036
2189
|
}
|
|
2037
2190
|
}
|
|
2038
2191
|
getImport(hook) {
|
|
2039
|
-
if (hook instanceof RpcImportHook && hook.entry && hook.entry.session === this) return hook.entry.importId;
|
|
2192
|
+
if (hook instanceof RpcImportHook && hook.entry && hook.entry.session === this && !hook.entry.resolution) return hook.entry.importId;
|
|
2040
2193
|
else return;
|
|
2041
2194
|
}
|
|
2042
2195
|
importStub(idx) {
|
|
@@ -2130,6 +2283,7 @@ var RpcSessionImpl = class {
|
|
|
2130
2283
|
value.push(devalue[0]);
|
|
2131
2284
|
}
|
|
2132
2285
|
this.send(["push", value]);
|
|
2286
|
+
args?.dispose();
|
|
2133
2287
|
let entry = new ImportTableEntry(this, this.imports.length, false);
|
|
2134
2288
|
this.imports.push(entry);
|
|
2135
2289
|
return new RpcImportHook(true, entry);
|
|
@@ -2154,6 +2308,7 @@ var RpcSessionImpl = class {
|
|
|
2154
2308
|
value.push(devalue[0]);
|
|
2155
2309
|
let msg = ["stream", value];
|
|
2156
2310
|
let size = this.send(msg);
|
|
2311
|
+
args.dispose();
|
|
2157
2312
|
if (size === void 0) size = estimateEncodedSize(msg);
|
|
2158
2313
|
let importId = this.imports.length;
|
|
2159
2314
|
let entry = new ImportTableEntry(this, importId, true);
|
|
@@ -2251,7 +2406,8 @@ var RpcSessionImpl = class {
|
|
|
2251
2406
|
if (msg instanceof Array) switch (msg[0]) {
|
|
2252
2407
|
case "push":
|
|
2253
2408
|
if (msg.length > 1) {
|
|
2254
|
-
let
|
|
2409
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[1]);
|
|
2410
|
+
let hook = new PayloadStubHook(payload);
|
|
2255
2411
|
hook.ignoreUnhandledRejections();
|
|
2256
2412
|
this.exports.push({
|
|
2257
2413
|
hook,
|
|
@@ -2262,7 +2418,8 @@ var RpcSessionImpl = class {
|
|
|
2262
2418
|
break;
|
|
2263
2419
|
case "stream":
|
|
2264
2420
|
if (msg.length > 1) {
|
|
2265
|
-
let
|
|
2421
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[1]);
|
|
2422
|
+
let hook = new PayloadStubHook(payload);
|
|
2266
2423
|
hook.ignoreUnhandledRejections();
|
|
2267
2424
|
let exportId = this.exports.length;
|
|
2268
2425
|
this.exports.push({
|
|
@@ -2297,13 +2454,14 @@ var RpcSessionImpl = class {
|
|
|
2297
2454
|
let importId = msg[1];
|
|
2298
2455
|
if (typeof importId == "number" && msg.length > 2) {
|
|
2299
2456
|
let imp = this.imports[importId];
|
|
2300
|
-
if (imp)
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2305
|
-
|
|
2306
|
-
|
|
2457
|
+
if (imp) {
|
|
2458
|
+
if (msg[0] == "resolve") imp.resolve(new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[2])));
|
|
2459
|
+
else {
|
|
2460
|
+
let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[2]);
|
|
2461
|
+
payload.dispose();
|
|
2462
|
+
imp.resolve(new ErrorStubHook(payload.value));
|
|
2463
|
+
}
|
|
2464
|
+
} else if (msg[0] == "resolve") new Evaluator(this, this.encodingLevel).evaluate(msg[2]).dispose();
|
|
2307
2465
|
continue;
|
|
2308
2466
|
}
|
|
2309
2467
|
break;
|
|
@@ -2375,7 +2533,8 @@ var RpcSession$1 = class {
|
|
|
2375
2533
|
const MAX_CLOSE_REASON_BYTES = 123;
|
|
2376
2534
|
function newWebSocketRpcSession$1(webSocket, localMain, options) {
|
|
2377
2535
|
if (typeof webSocket === "string") webSocket = new WebSocket(webSocket);
|
|
2378
|
-
|
|
2536
|
+
let transport = new WebSocketTransport(webSocket);
|
|
2537
|
+
return new RpcSession$1(transport, localMain, options).getRemoteMain();
|
|
2379
2538
|
}
|
|
2380
2539
|
/**
|
|
2381
2540
|
* For use in Cloudflare Workers: Construct an HTTP response that starts a WebSocket RPC session
|
|
@@ -2412,12 +2571,13 @@ var WebSocketTransport = class {
|
|
|
2412
2571
|
});
|
|
2413
2572
|
}
|
|
2414
2573
|
webSocket.addEventListener("message", (event) => {
|
|
2415
|
-
if (this.#error) {} else if (typeof event.data === "string" || event.data instanceof ArrayBuffer)
|
|
2416
|
-
this.#receiveResolver
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
|
|
2574
|
+
if (this.#error) {} else if (typeof event.data === "string" || event.data instanceof ArrayBuffer) {
|
|
2575
|
+
if (this.#receiveResolver) {
|
|
2576
|
+
this.#receiveResolver(event.data);
|
|
2577
|
+
this.#receiveResolver = void 0;
|
|
2578
|
+
this.#receiveRejecter = void 0;
|
|
2579
|
+
} else this.#receiveQueue.push(event.data);
|
|
2580
|
+
} else this.#receivedError(/* @__PURE__ */ new TypeError("Received unexpected message type from WebSocket."));
|
|
2421
2581
|
});
|
|
2422
2582
|
webSocket.addEventListener("close", (event) => {
|
|
2423
2583
|
this.#receivedError(/* @__PURE__ */ new Error(`Peer closed WebSocket: ${event.code} ${event.reason}`));
|
|
@@ -2509,7 +2669,8 @@ function newHttpBatchRpcSession$1(urlOrRequest, options) {
|
|
|
2509
2669
|
let body = await response.text();
|
|
2510
2670
|
return body == "" ? [] : body.split("\n");
|
|
2511
2671
|
};
|
|
2512
|
-
|
|
2672
|
+
let transport = new BatchClientTransport(sendBatch);
|
|
2673
|
+
return new RpcSession$1(transport, void 0, options).getRemoteMain();
|
|
2513
2674
|
}
|
|
2514
2675
|
var BatchServerTransport = class {
|
|
2515
2676
|
constructor(batch) {
|
|
@@ -2552,7 +2713,8 @@ var BatchServerTransport = class {
|
|
|
2552
2713
|
async function newHttpBatchRpcResponse(request, localMain, options) {
|
|
2553
2714
|
if (request.method !== "POST") return new Response("This endpoint only accepts POST requests.", { status: 405 });
|
|
2554
2715
|
let body = await request.text();
|
|
2555
|
-
let
|
|
2716
|
+
let batch = body === "" ? [] : body.split("\n");
|
|
2717
|
+
let transport = new BatchServerTransport(batch);
|
|
2556
2718
|
let rpc = new RpcSession$1(transport, localMain, options);
|
|
2557
2719
|
await transport.whenAllReceived();
|
|
2558
2720
|
await rpc.drain();
|
|
@@ -2582,7 +2744,8 @@ async function nodeHttpBatchRpcResponse(request, response, localMain, options) {
|
|
|
2582
2744
|
});
|
|
2583
2745
|
request.on("error", reject);
|
|
2584
2746
|
});
|
|
2585
|
-
let
|
|
2747
|
+
let batch = body === "" ? [] : body.split("\n");
|
|
2748
|
+
let transport = new BatchServerTransport(batch);
|
|
2586
2749
|
let rpc = new RpcSession$1(transport, localMain, options);
|
|
2587
2750
|
await transport.whenAllReceived();
|
|
2588
2751
|
await rpc.drain();
|
|
@@ -2593,7 +2756,8 @@ async function nodeHttpBatchRpcResponse(request, response, localMain, options) {
|
|
|
2593
2756
|
//#endregion
|
|
2594
2757
|
//#region src/messageport.ts
|
|
2595
2758
|
function newMessagePortRpcSession$1(port, localMain, options) {
|
|
2596
|
-
|
|
2759
|
+
let transport = new MessagePortTransport(port);
|
|
2760
|
+
return new RpcSession$1(transport, localMain, options).getRemoteMain();
|
|
2597
2761
|
}
|
|
2598
2762
|
var MessagePortTransport = class {
|
|
2599
2763
|
encodingLevel = "structuredClonable";
|
|
@@ -2843,7 +3007,8 @@ var MapApplicator = class {
|
|
|
2843
3007
|
}
|
|
2844
3008
|
};
|
|
2845
3009
|
function applyMapToElement(input, parent, owner, captures, instructions) {
|
|
2846
|
-
let
|
|
3010
|
+
let inputHook = new PayloadStubHook(RpcPayload.deepCopyFrom(input, parent, owner));
|
|
3011
|
+
let mapper = new MapApplicator(captures, inputHook);
|
|
2847
3012
|
try {
|
|
2848
3013
|
return mapper.apply(instructions);
|
|
2849
3014
|
} finally {
|
|
@@ -2876,9 +3041,10 @@ mapImpl.applyMap = (input, parent, owner, captures, instructions) => {
|
|
|
2876
3041
|
var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
|
|
2877
3042
|
state;
|
|
2878
3043
|
static create(stream) {
|
|
3044
|
+
let writer = stream.getWriter();
|
|
2879
3045
|
return new WritableStreamStubHook({
|
|
2880
3046
|
refcount: 1,
|
|
2881
|
-
writer
|
|
3047
|
+
writer,
|
|
2882
3048
|
closed: false
|
|
2883
3049
|
});
|
|
2884
3050
|
}
|
|
@@ -2898,7 +3064,8 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
|
|
|
2898
3064
|
const method = path[0];
|
|
2899
3065
|
if (method !== "write" && method !== "close" && method !== "abort") throw new Error(`Unknown WritableStream method: ${method}`);
|
|
2900
3066
|
if (method === "close" || method === "abort") state.closed = true;
|
|
2901
|
-
|
|
3067
|
+
let promise = method === "write" ? args.deliverStreamWrite(state.writer) : args.deliverCall(state.writer[method], state.writer);
|
|
3068
|
+
return new PromiseStubHook(promise.then((payload) => new PayloadStubHook(payload)));
|
|
2902
3069
|
} catch (err) {
|
|
2903
3070
|
args.dispose();
|
|
2904
3071
|
return new ErrorStubHook(err);
|
|
@@ -2912,7 +3079,8 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
|
|
|
2912
3079
|
return new ErrorStubHook(/* @__PURE__ */ new Error("Cannot access properties on a WritableStream stub"));
|
|
2913
3080
|
}
|
|
2914
3081
|
dup() {
|
|
2915
|
-
|
|
3082
|
+
let state = this.getState();
|
|
3083
|
+
return new WritableStreamStubHook(state, this);
|
|
2916
3084
|
}
|
|
2917
3085
|
pull() {
|
|
2918
3086
|
return Promise.reject(/* @__PURE__ */ new Error("Cannot pull a WritableStream stub"));
|
|
@@ -2930,9 +3098,9 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
|
|
|
2930
3098
|
}
|
|
2931
3099
|
onBroken(callback) {}
|
|
2932
3100
|
};
|
|
2933
|
-
const INITIAL_WINDOW =
|
|
2934
|
-
const MAX_WINDOW =
|
|
2935
|
-
const MIN_WINDOW =
|
|
3101
|
+
const INITIAL_WINDOW = 262144;
|
|
3102
|
+
const MAX_WINDOW = 1073741824;
|
|
3103
|
+
const MIN_WINDOW = 65536;
|
|
2936
3104
|
const STARTUP_GROWTH_FACTOR = 2;
|
|
2937
3105
|
const STEADY_GROWTH_FACTOR = 1.25;
|
|
2938
3106
|
const DECAY_FACTOR = .9;
|