capnweb 0.0.0-07032e2 → 0.0.0-0b19df9

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/dist/index.cjs CHANGED
@@ -43,6 +43,7 @@ function typeForRpc(value) {
43
43
  case Date.prototype: return "date";
44
44
  case RegExp.prototype: return "regexp";
45
45
  case Set.prototype: return "set";
46
+ case Map.prototype: return "map";
46
47
  case Uint8Array.prototype:
47
48
  case BUFFER_PROTOTYPE:
48
49
  case ArrayBuffer.prototype:
@@ -306,6 +307,9 @@ async function pullPromise(promise) {
306
307
  if (pathIfPromise.length > 0) hook = hook.get(pathIfPromise);
307
308
  return (await hook.pull()).deliverResolve();
308
309
  }
310
+ function mapKeyToPropertyName(key) {
311
+ return typeof key === "string" || typeof key === "number" ? key : { mapKey: key };
312
+ }
309
313
  var RpcPayload = class RpcPayload {
310
314
  value;
311
315
  source;
@@ -414,7 +418,7 @@ var RpcPayload = class RpcPayload {
414
418
  return hook.dup();
415
419
  } else return hook;
416
420
  }
417
- } else throw new Error("owned payload shouldn't contain raw ReadableStreams");
421
+ } else return streamImpl.createReadableStreamHook(stream);
418
422
  }
419
423
  deepCopy(value, oldParent, property, parent, dupStubs, owner) {
420
424
  switch (typeForRpc(value)) {
@@ -450,6 +454,20 @@ var RpcPayload = class RpcPayload {
450
454
  }
451
455
  return result;
452
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
+ }
453
471
  case "object": {
454
472
  let result = {};
455
473
  let object = value;
@@ -507,8 +525,10 @@ var RpcPayload = class RpcPayload {
507
525
  case "readable": {
508
526
  let stream = value;
509
527
  let hook;
510
- if (owner) hook = owner.getHookForReadableStream(stream, oldParent, dupStubs);
511
- else hook = streamImpl.createReadableStreamHook(stream);
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);
512
532
  this.hooks.push(hook);
513
533
  return stream;
514
534
  }
@@ -551,7 +571,8 @@ var RpcPayload = class RpcPayload {
551
571
  this.ensureDeepCopied();
552
572
  if (this.value instanceof RpcPromise$1) RpcPayload.deliverRpcPromiseTo(this.value, parent, property, promises);
553
573
  else {
554
- parent[property] = this.value;
574
+ if (parent instanceof Map) parent.set(typeof property === "object" ? property.mapKey : property, this.value);
575
+ else parent[property] = this.value;
555
576
  for (let record of this.promises) RpcPayload.deliverRpcPromiseTo(record.promise, record.parent, record.property, promises);
556
577
  }
557
578
  }
@@ -657,6 +678,14 @@ var RpcPayload = class RpcPayload {
657
678
  for (let element of set) this.disposeImpl(element, set);
658
679
  return;
659
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
+ }
660
689
  case "object": {
661
690
  let object = value;
662
691
  for (let i in object) this.disposeImpl(object[i], object);
@@ -747,6 +776,14 @@ var RpcPayload = class RpcPayload {
747
776
  for (let element of set) this.ignoreUnhandledRejectionsImpl(element);
748
777
  return;
749
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
+ }
750
787
  case "object": {
751
788
  let object = value;
752
789
  for (let i in object) this.ignoreUnhandledRejectionsImpl(object[i]);
@@ -807,6 +844,7 @@ function followPath(value, parent, path, owner) {
807
844
  case "blob":
808
845
  case "date":
809
846
  case "set":
847
+ case "map":
810
848
  case "error":
811
849
  case "url":
812
850
  case "regexp":
@@ -1213,6 +1251,18 @@ var Devaluator = class Devaluator {
1213
1251
  for (let element of setElements) elements.push(this.devaluateImpl(element, set, depth + 1));
1214
1252
  return ["set", elements];
1215
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
+ }
1216
1266
  case "bigint":
1217
1267
  if (this.encodingLevel === "structuredClonable") return value;
1218
1268
  return ["bigint", value.toString()];
@@ -1539,6 +1589,20 @@ var Evaluator = class Evaluator {
1539
1589
  return set;
1540
1590
  }
1541
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;
1542
1606
  case "bytes": {
1543
1607
  let bytes;
1544
1608
  if (value[1] instanceof Uint8Array) bytes = value[1];
@@ -1825,10 +1889,11 @@ function estimateEncodedSize(value, seen, depth = 0) {
1825
1889
  var ImportTableEntry = class {
1826
1890
  session;
1827
1891
  importId;
1892
+ pulling;
1828
1893
  constructor(session, importId, pulling) {
1829
1894
  this.session = session;
1830
1895
  this.importId = importId;
1831
- if (pulling) this.activePull = Promise.withResolvers();
1896
+ this.pulling = pulling;
1832
1897
  }
1833
1898
  localRefcount = 0;
1834
1899
  remoteRefcount = 1;
@@ -1859,7 +1924,7 @@ var ImportTableEntry = class {
1859
1924
  }
1860
1925
  async awaitResolution() {
1861
1926
  if (!this.activePull) {
1862
- this.session.sendPull(this.importId);
1927
+ if (!this.pulling) this.session.sendPull(this.importId);
1863
1928
  this.activePull = Promise.withResolvers();
1864
1929
  }
1865
1930
  await this.activePull.promise;
@@ -2089,6 +2154,7 @@ var RpcSessionImpl = class {
2089
2154
  let autoRelease = exp.autoRelease;
2090
2155
  ++this.pullCount;
2091
2156
  exp.pull = resolve().then((payload) => {
2157
+ if (this.exports[exportId] !== exp) return;
2092
2158
  let value = Devaluator.devaluate(payload.value, void 0, this, payload, this.encodingLevel);
2093
2159
  this.send([
2094
2160
  "resolve",
@@ -2097,6 +2163,7 @@ var RpcSessionImpl = class {
2097
2163
  ]);
2098
2164
  if (autoRelease) this.releaseExport(exportId, 1);
2099
2165
  }, (error) => {
2166
+ if (this.exports[exportId] !== exp) return;
2100
2167
  this.send([
2101
2168
  "reject",
2102
2169
  exportId,
@@ -2216,6 +2283,7 @@ var RpcSessionImpl = class {
2216
2283
  value.push(devalue[0]);
2217
2284
  }
2218
2285
  this.send(["push", value]);
2286
+ args?.dispose();
2219
2287
  let entry = new ImportTableEntry(this, this.imports.length, false);
2220
2288
  this.imports.push(entry);
2221
2289
  return new RpcImportHook(true, entry);
@@ -2240,6 +2308,7 @@ var RpcSessionImpl = class {
2240
2308
  value.push(devalue[0]);
2241
2309
  let msg = ["stream", value];
2242
2310
  let size = this.send(msg);
2311
+ args.dispose();
2243
2312
  if (size === void 0) size = estimateEncodedSize(msg);
2244
2313
  let importId = this.imports.length;
2245
2314
  let entry = new ImportTableEntry(this, importId, true);