capnweb 0.0.0-20fe0fb → 0.0.0-296d9c0

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.
@@ -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,7 @@ 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
45
  case Uint8Array.prototype:
46
46
  case BUFFER_PROTOTYPE:
47
47
  case ArrayBuffer.prototype:
@@ -92,7 +92,14 @@ let streamImpl = {
92
92
  };
93
93
  var StubHook = class {
94
94
  stream(path, args) {
95
- let pulled = this.call(path, args).pull();
95
+ let hook = this.call(path, args);
96
+ let pulled;
97
+ try {
98
+ pulled = hook.pull();
99
+ } catch (err) {
100
+ hook.dispose();
101
+ throw err;
102
+ }
96
103
  let promise;
97
104
  if (pulled instanceof Promise) promise = pulled.then((p) => {
98
105
  p.dispose();
@@ -111,9 +118,11 @@ var ErrorStubHook = class extends StubHook {
111
118
  this.error = error;
112
119
  }
113
120
  call(path, args) {
121
+ args.dispose();
114
122
  return this;
115
123
  }
116
124
  map(path, captures, instructions) {
125
+ for (let cap of captures) cap.dispose();
117
126
  return this;
118
127
  }
119
128
  get(path) {
@@ -237,7 +246,25 @@ var RpcStub$1 = class RpcStub$1 extends RpcTarget$1 {
237
246
  };
238
247
  var RpcPromise$1 = class extends RpcStub$1 {
239
248
  constructor(hook, pathIfPromise) {
240
- super(hook, pathIfPromise);
249
+ if (hook instanceof StubHook) super(hook, pathIfPromise ?? []);
250
+ else {
251
+ if (pathIfPromise !== void 0) throw new TypeError("RpcPromise constructor expected one argument, received two.");
252
+ let kind = typeForRpc(hook);
253
+ if (kind === "rpc-promise") {
254
+ let raw = unwrapStubAndPath(hook);
255
+ if (raw.pathIfPromise.length > 0) super(raw.hook, raw.pathIfPromise);
256
+ else {
257
+ let adopted = raw.hook;
258
+ raw.hook = DISPOSED_HOOK;
259
+ super(adopted, []);
260
+ }
261
+ } else if (kind === "rpc-thenable") super(TargetStubHook.create(hook, void 0), []);
262
+ else {
263
+ let promiseHook = new PromiseStubHook(Promise.resolve(hook).then((value) => new PayloadStubHook(RpcPayload.fromAppReturn(value))));
264
+ promiseHook.ignoreUnhandledRejections();
265
+ super(promiseHook, []);
266
+ }
267
+ }
241
268
  }
242
269
  then(onfulfilled, onrejected) {
243
270
  return pullPromise(this).then(...arguments);
@@ -332,12 +359,13 @@ var RpcPayload = class RpcPayload {
332
359
  return TargetStubHook.create(target, parent);
333
360
  } else if (this.source === "return") {
334
361
  let hook = this.rpcTargets?.get(target);
335
- if (hook) if (dupStubs) return hook.dup();
336
- else {
337
- this.rpcTargets?.delete(target);
338
- return hook;
339
- }
340
- else {
362
+ if (hook) {
363
+ if (dupStubs) return hook.dup();
364
+ else {
365
+ this.rpcTargets?.delete(target);
366
+ return hook;
367
+ }
368
+ } else {
341
369
  hook = TargetStubHook.create(target, parent);
342
370
  if (dupStubs) {
343
371
  if (!this.rpcTargets) this.rpcTargets = /* @__PURE__ */ new Map();
@@ -351,12 +379,13 @@ var RpcPayload = class RpcPayload {
351
379
  if (this.source === "params") return streamImpl.createWritableStreamHook(stream);
352
380
  else if (this.source === "return") {
353
381
  let hook = this.rpcTargets?.get(stream);
354
- if (hook) if (dupStubs) return hook.dup();
355
- else {
356
- this.rpcTargets?.delete(stream);
357
- return hook;
358
- }
359
- else {
382
+ if (hook) {
383
+ if (dupStubs) return hook.dup();
384
+ else {
385
+ this.rpcTargets?.delete(stream);
386
+ return hook;
387
+ }
388
+ } else {
360
389
  hook = streamImpl.createWritableStreamHook(stream);
361
390
  if (dupStubs) {
362
391
  if (!this.rpcTargets) this.rpcTargets = /* @__PURE__ */ new Map();
@@ -370,12 +399,13 @@ var RpcPayload = class RpcPayload {
370
399
  if (this.source === "params") return streamImpl.createReadableStreamHook(stream);
371
400
  else if (this.source === "return") {
372
401
  let hook = this.rpcTargets?.get(stream);
373
- if (hook) if (dupStubs) return hook.dup();
374
- else {
375
- this.rpcTargets?.delete(stream);
376
- return hook;
377
- }
378
- else {
402
+ if (hook) {
403
+ if (dupStubs) return hook.dup();
404
+ else {
405
+ this.rpcTargets?.delete(stream);
406
+ return hook;
407
+ }
408
+ } else {
379
409
  hook = streamImpl.createReadableStreamHook(stream);
380
410
  if (dupStubs) {
381
411
  if (!this.rpcTargets) this.rpcTargets = /* @__PURE__ */ new Map();
@@ -394,6 +424,7 @@ var RpcPayload = class RpcPayload {
394
424
  case "bytes":
395
425
  case "blob":
396
426
  case "url":
427
+ case "regexp":
397
428
  case "error":
398
429
  case "undefined": return value;
399
430
  case "array": {
@@ -592,6 +623,7 @@ var RpcPayload = class RpcPayload {
592
623
  case "blob":
593
624
  case "date":
594
625
  case "url":
626
+ case "regexp":
595
627
  case "error":
596
628
  case "undefined": return;
597
629
  case "array": {
@@ -675,6 +707,7 @@ var RpcPayload = class RpcPayload {
675
707
  case "writable":
676
708
  case "readable":
677
709
  case "url":
710
+ case "regexp":
678
711
  case "headers":
679
712
  case "request":
680
713
  case "response": return;
@@ -745,6 +778,7 @@ function followPath(value, parent, path, owner) {
745
778
  case "date":
746
779
  case "error":
747
780
  case "url":
781
+ case "regexp":
748
782
  case "headers":
749
783
  case "request":
750
784
  case "response":
@@ -777,17 +811,23 @@ function followPath(value, parent, path, owner) {
777
811
  }
778
812
  var ValueStubHook = class extends StubHook {
779
813
  call(path, args) {
814
+ let followResult;
780
815
  try {
781
816
  let { value, owner } = this.getValue();
782
- let followResult = followPath(value, void 0, path, owner);
783
- if (followResult.hook) return followResult.hook.call(followResult.remainingPath, args);
784
- if (typeof followResult.value != "function") throw new TypeError(`'${path.join(".")}' is not a function.`);
785
- return new PromiseStubHook(args.deliverCall(followResult.value, followResult.parent).then((payload) => {
786
- return new PayloadStubHook(payload);
787
- }));
817
+ followResult = followPath(value, void 0, path, owner);
788
818
  } catch (err) {
819
+ args.dispose();
789
820
  return new ErrorStubHook(err);
790
821
  }
822
+ if (followResult.hook) return followResult.hook.call(followResult.remainingPath, args);
823
+ if (typeof followResult.value != "function") {
824
+ args.dispose();
825
+ return new ErrorStubHook(/* @__PURE__ */ new TypeError(`'${path.join(".")}' is not a function.`));
826
+ }
827
+ let promise = args.deliverCall(followResult.value, followResult.parent);
828
+ return new PromiseStubHook(promise.then((payload) => {
829
+ return new PayloadStubHook(payload);
830
+ }));
791
831
  }
792
832
  map(path, captures, instructions) {
793
833
  try {
@@ -808,7 +848,10 @@ var ValueStubHook = class extends StubHook {
808
848
  get(path) {
809
849
  try {
810
850
  let { value, owner } = this.getValue();
811
- if (path.length === 0 && owner === null) throw new Error("Can't dup an RpcTarget stub as a promise.");
851
+ if (path.length === 0 && owner === null) {
852
+ if (value instanceof Object && "then" in value) return this.dup();
853
+ throw new Error("Can't dup an RpcTarget stub as a promise.");
854
+ }
812
855
  let followResult = followPath(value, void 0, path, owner);
813
856
  if (followResult.hook) return followResult.hook.get(followResult.remainingPath);
814
857
  return new PayloadStubHook(RpcPayload.deepCopyFrom(followResult.value, followResult.parent, followResult.owner));
@@ -911,7 +954,10 @@ var TargetStubHook = class TargetStubHook extends ValueStubHook {
911
954
  this.target = void 0;
912
955
  }
913
956
  }
914
- onBroken(callback) {}
957
+ onBroken(callback) {
958
+ let target = this.target;
959
+ if (target && "then" in target) Promise.resolve(target).then(() => {}, callback);
960
+ }
915
961
  };
916
962
  var PromiseStubHook = class PromiseStubHook extends StubHook {
917
963
  promise;
@@ -925,12 +971,16 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
925
971
  }
926
972
  call(path, args) {
927
973
  args.ensureDeepCopied();
928
- return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args)));
974
+ return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args), (err) => {
975
+ args.dispose();
976
+ throw err;
977
+ }));
929
978
  }
930
979
  stream(path, args) {
931
980
  args.ensureDeepCopied();
932
- return { promise: this.promise.then((hook) => {
933
- return hook.stream(path, args).promise;
981
+ return { promise: this.promise.then((hook) => hook.stream(path, args).promise, (err) => {
982
+ args.dispose();
983
+ throw err;
934
984
  }) };
935
985
  }
936
986
  map(path, captures, instructions) {
@@ -957,10 +1007,7 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
957
1007
  }, (err) => {});
958
1008
  }
959
1009
  dispose() {
960
- if (this.resolution) this.resolution.dispose();
961
- else this.promise.then((hook) => {
962
- hook.dispose();
963
- }, (err) => {});
1010
+ this.promise.then((hook) => hook.dispose(), () => {});
964
1011
  }
965
1012
  onBroken(callback) {
966
1013
  if (this.resolution) this.resolution.onBroken(callback);
@@ -976,7 +1023,7 @@ const DEFAULT_MAX_DEPTH = 256;
976
1023
  const DEFAULT_LIMITS = {
977
1024
  maxBigIntDigits: 16384,
978
1025
  maxDepth: 256,
979
- maxMessageSize: 32 * 1024 * 1024
1026
+ maxMessageSize: 33554432
980
1027
  };
981
1028
  const NATIVE_LITTLE_ENDIAN = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
982
1029
  const BYTE_CONTAINER_TYPE_NAMES = [
@@ -1038,9 +1085,7 @@ function swapByteOrder(bytes, elementSize) {
1038
1085
  case 4:
1039
1086
  view.setUint32(offset, view.getUint32(offset, false), true);
1040
1087
  break;
1041
- case 8:
1042
- view.setBigUint64(offset, view.getBigUint64(offset, false), true);
1043
- break;
1088
+ case 8: view.setBigUint64(offset, view.getBigUint64(offset, false), true);
1044
1089
  }
1045
1090
  }
1046
1091
  var NullExporter = class {
@@ -1133,6 +1178,15 @@ var Devaluator = class Devaluator {
1133
1178
  const time = value.getTime();
1134
1179
  return ["date", Number.isNaN(time) ? null : time];
1135
1180
  }
1181
+ case "regexp": {
1182
+ if (this.encodingLevel === "structuredClonable") return value;
1183
+ let re = value;
1184
+ return re.flags ? [
1185
+ "regexp",
1186
+ re.source,
1187
+ re.flags
1188
+ ] : ["regexp", re.source];
1189
+ }
1136
1190
  case "bytes": {
1137
1191
  let alternateTypeName = BYTE_CONTAINER_TYPE_BY_PROTOTYPE.get(Object.getPrototypeOf(value));
1138
1192
  let bytes;
@@ -1290,13 +1344,16 @@ var Devaluator = class Devaluator {
1290
1344
  if (!this.source) throw new Error("Can't serialize RPC stubs in this context.");
1291
1345
  let { hook, pathIfPromise } = unwrapStubAndPath(value);
1292
1346
  let importId = this.exporter.getImport(hook);
1293
- if (importId !== void 0) if (pathIfPromise) if (pathIfPromise.length > 0) return [
1294
- "pipeline",
1295
- importId,
1296
- pathIfPromise
1297
- ];
1298
- else return ["pipeline", importId];
1299
- else return ["import", importId];
1347
+ if (importId !== void 0) {
1348
+ if (pathIfPromise) {
1349
+ if (pathIfPromise.length > 0) return [
1350
+ "pipeline",
1351
+ importId,
1352
+ pathIfPromise
1353
+ ];
1354
+ else return ["pipeline", importId];
1355
+ } else return ["import", importId];
1356
+ }
1300
1357
  if (pathIfPromise) hook = hook.get(pathIfPromise);
1301
1358
  else hook = hook.dup();
1302
1359
  return this.devaluateHook(pathIfPromise ? "promise" : "export", hook);
@@ -1357,16 +1414,18 @@ var NullImporter = class {
1357
1414
  };
1358
1415
  const NULL_IMPORTER = new NullImporter();
1359
1416
  function fixBrokenRequestBody(request, body) {
1360
- return new RpcPromise$1(new PromiseStubHook(new Response(body).arrayBuffer().then((arrayBuffer) => {
1417
+ let promise = new Response(body).arrayBuffer().then((arrayBuffer) => {
1361
1418
  let bytes = new Uint8Array(arrayBuffer);
1362
1419
  let result = new Request(request, { body: bytes });
1363
1420
  return new PayloadStubHook(RpcPayload.fromAppReturn(result));
1364
- })), []);
1421
+ });
1422
+ return new RpcPromise$1(new PromiseStubHook(promise), []);
1365
1423
  }
1366
1424
  function streamToBlobPromise(stream, type) {
1367
- return new RpcPromise$1(new PromiseStubHook(streamToBlob(stream, type).then((blob) => {
1425
+ let promise = streamToBlob(stream, type).then((blob) => {
1368
1426
  return new PayloadStubHook(RpcPayload.fromAppReturn(blob));
1369
- })), []);
1427
+ });
1428
+ return new RpcPromise$1(new PromiseStubHook(promise), []);
1370
1429
  }
1371
1430
  var Evaluator = class Evaluator {
1372
1431
  importer;
@@ -1399,7 +1458,7 @@ var Evaluator = class Evaluator {
1399
1458
  let maxDepth = this.limits.maxDepth;
1400
1459
  if (depth >= maxDepth) throw new TypeError(`Deserialization exceeded maximum allowed message depth of ${maxDepth}.`);
1401
1460
  if (this.encodingLevel === "structuredClonable") {
1402
- if (value instanceof Date || typeof value === "bigint") return value;
1461
+ if (value instanceof Date || value instanceof RegExp || typeof value === "bigint") return value;
1403
1462
  }
1404
1463
  if (value instanceof Array) {
1405
1464
  if (value.length == 1 && value[0] instanceof Array) {
@@ -1419,18 +1478,22 @@ var Evaluator = class Evaluator {
1419
1478
  if (value[1] === null) return /* @__PURE__ */ new Date(NaN);
1420
1479
  if (typeof value[1] == "number") return new Date(value[1]);
1421
1480
  break;
1481
+ case "regexp":
1482
+ if (typeof value[1] === "string" && (value.length === 2 || value.length === 3 && typeof value[2] === "string")) return new RegExp(value[1], value[2]);
1483
+ break;
1422
1484
  case "bytes": {
1423
1485
  let bytes;
1424
1486
  if (value[1] instanceof Uint8Array) bytes = value[1];
1425
- else if (typeof value[1] == "string") if (typeof Buffer !== "undefined") bytes = Buffer.from(value[1], "base64");
1426
- else if (Uint8Array.fromBase64) bytes = Uint8Array.fromBase64(value[1]);
1427
- else {
1428
- let bs = atob(value[1]);
1429
- let len = bs.length;
1430
- bytes = new Uint8Array(len);
1431
- for (let i = 0; i < len; i++) bytes[i] = bs.charCodeAt(i);
1432
- }
1433
- else break;
1487
+ else if (typeof value[1] == "string") {
1488
+ if (typeof Buffer !== "undefined") bytes = Buffer.from(value[1], "base64");
1489
+ else if (Uint8Array.fromBase64) bytes = Uint8Array.fromBase64(value[1]);
1490
+ else {
1491
+ let bs = atob(value[1]);
1492
+ let len = bs.length;
1493
+ bytes = new Uint8Array(len);
1494
+ for (let i = 0; i < len; i++) bytes[i] = bs.charCodeAt(i);
1495
+ }
1496
+ } else break;
1434
1497
  if (value.length === 2) return bytes;
1435
1498
  if (typeof value[2] !== "string") throw new TypeError(`Unknown bytes type marker type: ${typeof value[2]}`);
1436
1499
  if (!isValidByteContainerName(value[2])) {
@@ -1456,7 +1519,6 @@ var Evaluator = class Evaluator {
1456
1519
  case "BigUint64Array": return new BigUint64Array(buffer);
1457
1520
  case "Float32Array": return new Float32Array(buffer);
1458
1521
  case "Float64Array": return new Float64Array(buffer);
1459
- default:
1460
1522
  }
1461
1523
  }
1462
1524
  case "error":
@@ -1562,8 +1624,10 @@ var Evaluator = class Evaluator {
1562
1624
  return new RpcPromise$1(hook, []);
1563
1625
  }
1564
1626
  };
1565
- if (value.length == 2) if (isPromise) return addStub(hook.get([]));
1566
- else return addStub(hook.dup());
1627
+ if (value.length == 2) {
1628
+ if (isPromise) return addStub(hook.get([]));
1629
+ else return addStub(hook.dup());
1630
+ }
1567
1631
  let path = value[2];
1568
1632
  if (!(path instanceof Array)) break;
1569
1633
  if (!path.every((part) => {
@@ -1593,7 +1657,8 @@ var Evaluator = class Evaluator {
1593
1657
  }
1594
1658
  });
1595
1659
  let instructions = value[4];
1596
- let promise = new RpcPromise$1(hook.map(path, captures, instructions), []);
1660
+ let resultHook = hook.map(path, captures, instructions);
1661
+ let promise = new RpcPromise$1(resultHook, []);
1597
1662
  this.promises.push({
1598
1663
  promise,
1599
1664
  parent,
@@ -1603,18 +1668,21 @@ var Evaluator = class Evaluator {
1603
1668
  }
1604
1669
  case "export":
1605
1670
  case "promise":
1606
- if (typeof value[1] == "number") if (value[0] == "promise") {
1607
- let promise = new RpcPromise$1(this.importer.importPromise(value[1]), []);
1608
- this.promises.push({
1609
- parent,
1610
- property,
1611
- promise
1612
- });
1613
- return promise;
1614
- } else {
1615
- let hook = this.importer.importStub(value[1]);
1616
- this.hooks.push(hook);
1617
- return new RpcStub$1(hook);
1671
+ if (typeof value[1] == "number") {
1672
+ if (value[0] == "promise") {
1673
+ let hook = this.importer.importPromise(value[1]);
1674
+ let promise = new RpcPromise$1(hook, []);
1675
+ this.promises.push({
1676
+ parent,
1677
+ property,
1678
+ promise
1679
+ });
1680
+ return promise;
1681
+ } else {
1682
+ let hook = this.importer.importStub(value[1]);
1683
+ this.hooks.push(hook);
1684
+ return new RpcStub$1(hook);
1685
+ }
1618
1686
  }
1619
1687
  break;
1620
1688
  case "writable":
@@ -1625,14 +1693,12 @@ var Evaluator = class Evaluator {
1625
1693
  return stream;
1626
1694
  }
1627
1695
  break;
1628
- case "readable":
1629
- if (typeof value[1] == "number") {
1630
- let stream = this.importer.getPipeReadable(value[1]);
1631
- let hook = streamImpl.createReadableStreamHook(stream);
1632
- this.hooks.push(hook);
1633
- return stream;
1634
- }
1635
- break;
1696
+ case "readable": if (typeof value[1] == "number") {
1697
+ let stream = this.importer.getPipeReadable(value[1]);
1698
+ let hook = streamImpl.createReadableStreamHook(stream);
1699
+ this.hooks.push(hook);
1700
+ return stream;
1701
+ }
1636
1702
  }
1637
1703
  throw new TypeError(`unknown special value: ${JSON.stringify(value)}`);
1638
1704
  } else if (value instanceof Object) {
@@ -1677,6 +1743,7 @@ function estimateEncodedSize(value, seen, depth = 0) {
1677
1743
  if (value instanceof ArrayBuffer) return ESTIMATED_BINARY_OVERHEAD + value.byteLength;
1678
1744
  if (typeof Blob !== "undefined" && value instanceof Blob) return ESTIMATED_BINARY_OVERHEAD + value.size;
1679
1745
  if (value instanceof Date) return 16;
1746
+ if (value instanceof RegExp) return ESTIMATED_OBJECT_OVERHEAD + estimateStringSize(value.source) + estimateStringSize(value.flags);
1680
1747
  seen ??= /* @__PURE__ */ new WeakSet();
1681
1748
  if (seen.has(value)) return ESTIMATED_ENTRY_OVERHEAD;
1682
1749
  seen.add(value);
@@ -1789,24 +1856,28 @@ var RpcImportHook = class RpcImportHook extends StubHook {
1789
1856
  if (this.entry) return this.entry;
1790
1857
  else throw new Error("This RpcImportHook was already disposed.");
1791
1858
  }
1859
+ getEntryTakingOwnership(disposeOwned) {
1860
+ try {
1861
+ return this.getEntry();
1862
+ } catch (err) {
1863
+ disposeOwned();
1864
+ throw err;
1865
+ }
1866
+ }
1792
1867
  call(path, args) {
1793
- let entry = this.getEntry();
1868
+ let entry = this.getEntryTakingOwnership(() => args.dispose());
1794
1869
  if (entry.resolution) return entry.resolution.call(path, args);
1795
1870
  else return entry.session.sendCall(entry.importId, path, args);
1796
1871
  }
1797
1872
  stream(path, args) {
1798
- let entry = this.getEntry();
1873
+ let entry = this.getEntryTakingOwnership(() => args.dispose());
1799
1874
  if (entry.resolution) return entry.resolution.stream(path, args);
1800
1875
  else return entry.session.sendStream(entry.importId, path, args);
1801
1876
  }
1802
1877
  map(path, captures, instructions) {
1803
- let entry;
1804
- try {
1805
- entry = this.getEntry();
1806
- } catch (err) {
1878
+ let entry = this.getEntryTakingOwnership(() => {
1807
1879
  for (let cap of captures) cap.dispose();
1808
- throw err;
1809
- }
1880
+ });
1810
1881
  if (entry.resolution) return entry.resolution.map(path, captures, instructions);
1811
1882
  else return entry.session.sendMap(entry.importId, path, captures, instructions);
1812
1883
  }
@@ -2067,14 +2138,23 @@ var RpcSessionImpl = class {
2067
2138
  }
2068
2139
  }
2069
2140
  sendCall(id, path, args) {
2070
- if (this.abortReason) throw this.abortReason;
2141
+ if (this.abortReason) {
2142
+ args?.dispose();
2143
+ throw this.abortReason;
2144
+ }
2071
2145
  let value = [
2072
2146
  "pipeline",
2073
2147
  id,
2074
2148
  path
2075
2149
  ];
2076
2150
  if (args) {
2077
- let devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
2151
+ let devalue;
2152
+ try {
2153
+ devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
2154
+ } catch (err) {
2155
+ args.dispose();
2156
+ throw err;
2157
+ }
2078
2158
  value.push(devalue[0]);
2079
2159
  }
2080
2160
  this.send(["push", value]);
@@ -2083,13 +2163,22 @@ var RpcSessionImpl = class {
2083
2163
  return new RpcImportHook(true, entry);
2084
2164
  }
2085
2165
  sendStream(id, path, args) {
2086
- if (this.abortReason) throw this.abortReason;
2166
+ if (this.abortReason) {
2167
+ args.dispose();
2168
+ throw this.abortReason;
2169
+ }
2087
2170
  let value = [
2088
2171
  "pipeline",
2089
2172
  id,
2090
2173
  path
2091
2174
  ];
2092
- let devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
2175
+ let devalue;
2176
+ try {
2177
+ devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
2178
+ } catch (err) {
2179
+ args.dispose();
2180
+ throw err;
2181
+ }
2093
2182
  value.push(devalue[0]);
2094
2183
  let msg = ["stream", value];
2095
2184
  let size = this.send(msg);
@@ -2190,7 +2279,8 @@ var RpcSessionImpl = class {
2190
2279
  if (msg instanceof Array) switch (msg[0]) {
2191
2280
  case "push":
2192
2281
  if (msg.length > 1) {
2193
- let hook = new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[1]));
2282
+ let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[1]);
2283
+ let hook = new PayloadStubHook(payload);
2194
2284
  hook.ignoreUnhandledRejections();
2195
2285
  this.exports.push({
2196
2286
  hook,
@@ -2201,7 +2291,8 @@ var RpcSessionImpl = class {
2201
2291
  break;
2202
2292
  case "stream":
2203
2293
  if (msg.length > 1) {
2204
- let hook = new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[1]));
2294
+ let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[1]);
2295
+ let hook = new PayloadStubHook(payload);
2205
2296
  hook.ignoreUnhandledRejections();
2206
2297
  let exportId = this.exports.length;
2207
2298
  this.exports.push({
@@ -2236,13 +2327,14 @@ var RpcSessionImpl = class {
2236
2327
  let importId = msg[1];
2237
2328
  if (typeof importId == "number" && msg.length > 2) {
2238
2329
  let imp = this.imports[importId];
2239
- if (imp) if (msg[0] == "resolve") imp.resolve(new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[2])));
2240
- else {
2241
- let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[2]);
2242
- payload.dispose();
2243
- imp.resolve(new ErrorStubHook(payload.value));
2244
- }
2245
- else if (msg[0] == "resolve") new Evaluator(this, this.encodingLevel).evaluate(msg[2]).dispose();
2330
+ if (imp) {
2331
+ if (msg[0] == "resolve") imp.resolve(new PayloadStubHook(new Evaluator(this, this.encodingLevel).evaluate(msg[2])));
2332
+ else {
2333
+ let payload = new Evaluator(this, this.encodingLevel).evaluate(msg[2]);
2334
+ payload.dispose();
2335
+ imp.resolve(new ErrorStubHook(payload.value));
2336
+ }
2337
+ } else if (msg[0] == "resolve") new Evaluator(this, this.encodingLevel).evaluate(msg[2]).dispose();
2246
2338
  continue;
2247
2339
  }
2248
2340
  break;
@@ -2314,7 +2406,8 @@ var RpcSession$1 = class {
2314
2406
  const MAX_CLOSE_REASON_BYTES = 123;
2315
2407
  function newWebSocketRpcSession$1(webSocket, localMain, options) {
2316
2408
  if (typeof webSocket === "string") webSocket = new WebSocket(webSocket);
2317
- return new RpcSession$1(new WebSocketTransport(webSocket), localMain, options).getRemoteMain();
2409
+ let transport = new WebSocketTransport(webSocket);
2410
+ return new RpcSession$1(transport, localMain, options).getRemoteMain();
2318
2411
  }
2319
2412
  /**
2320
2413
  * For use in Cloudflare Workers: Construct an HTTP response that starts a WebSocket RPC session
@@ -2351,12 +2444,13 @@ var WebSocketTransport = class {
2351
2444
  });
2352
2445
  }
2353
2446
  webSocket.addEventListener("message", (event) => {
2354
- if (this.#error) {} else if (typeof event.data === "string" || event.data instanceof ArrayBuffer) if (this.#receiveResolver) {
2355
- this.#receiveResolver(event.data);
2356
- this.#receiveResolver = void 0;
2357
- this.#receiveRejecter = void 0;
2358
- } else this.#receiveQueue.push(event.data);
2359
- else this.#receivedError(/* @__PURE__ */ new TypeError("Received unexpected message type from WebSocket."));
2447
+ if (this.#error) {} else if (typeof event.data === "string" || event.data instanceof ArrayBuffer) {
2448
+ if (this.#receiveResolver) {
2449
+ this.#receiveResolver(event.data);
2450
+ this.#receiveResolver = void 0;
2451
+ this.#receiveRejecter = void 0;
2452
+ } else this.#receiveQueue.push(event.data);
2453
+ } else this.#receivedError(/* @__PURE__ */ new TypeError("Received unexpected message type from WebSocket."));
2360
2454
  });
2361
2455
  webSocket.addEventListener("close", (event) => {
2362
2456
  this.#receivedError(/* @__PURE__ */ new Error(`Peer closed WebSocket: ${event.code} ${event.reason}`));
@@ -2448,7 +2542,8 @@ function newHttpBatchRpcSession$1(urlOrRequest, options) {
2448
2542
  let body = await response.text();
2449
2543
  return body == "" ? [] : body.split("\n");
2450
2544
  };
2451
- return new RpcSession$1(new BatchClientTransport(sendBatch), void 0, options).getRemoteMain();
2545
+ let transport = new BatchClientTransport(sendBatch);
2546
+ return new RpcSession$1(transport, void 0, options).getRemoteMain();
2452
2547
  }
2453
2548
  var BatchServerTransport = class {
2454
2549
  constructor(batch) {
@@ -2491,7 +2586,8 @@ var BatchServerTransport = class {
2491
2586
  async function newHttpBatchRpcResponse(request, localMain, options) {
2492
2587
  if (request.method !== "POST") return new Response("This endpoint only accepts POST requests.", { status: 405 });
2493
2588
  let body = await request.text();
2494
- let transport = new BatchServerTransport(body === "" ? [] : body.split("\n"));
2589
+ let batch = body === "" ? [] : body.split("\n");
2590
+ let transport = new BatchServerTransport(batch);
2495
2591
  let rpc = new RpcSession$1(transport, localMain, options);
2496
2592
  await transport.whenAllReceived();
2497
2593
  await rpc.drain();
@@ -2521,7 +2617,8 @@ async function nodeHttpBatchRpcResponse(request, response, localMain, options) {
2521
2617
  });
2522
2618
  request.on("error", reject);
2523
2619
  });
2524
- let transport = new BatchServerTransport(body === "" ? [] : body.split("\n"));
2620
+ let batch = body === "" ? [] : body.split("\n");
2621
+ let transport = new BatchServerTransport(batch);
2525
2622
  let rpc = new RpcSession$1(transport, localMain, options);
2526
2623
  await transport.whenAllReceived();
2527
2624
  await rpc.drain();
@@ -2532,7 +2629,8 @@ async function nodeHttpBatchRpcResponse(request, response, localMain, options) {
2532
2629
  //#endregion
2533
2630
  //#region src/messageport.ts
2534
2631
  function newMessagePortRpcSession$1(port, localMain, options) {
2535
- return new RpcSession$1(new MessagePortTransport(port), localMain, options).getRemoteMain();
2632
+ let transport = new MessagePortTransport(port);
2633
+ return new RpcSession$1(transport, localMain, options).getRemoteMain();
2536
2634
  }
2537
2635
  var MessagePortTransport = class {
2538
2636
  encodingLevel = "structuredClonable";
@@ -2720,9 +2818,11 @@ var MapVariableHook = class extends StubHook {
2720
2818
  else throwMapperBuilderUseError();
2721
2819
  }
2722
2820
  call(path, args) {
2821
+ args.dispose();
2723
2822
  throwMapperBuilderUseError();
2724
2823
  }
2725
2824
  map(path, captures, instructions) {
2825
+ for (let cap of captures) cap.dispose();
2726
2826
  throwMapperBuilderUseError();
2727
2827
  }
2728
2828
  pull() {
@@ -2780,7 +2880,8 @@ var MapApplicator = class {
2780
2880
  }
2781
2881
  };
2782
2882
  function applyMapToElement(input, parent, owner, captures, instructions) {
2783
- let mapper = new MapApplicator(captures, new PayloadStubHook(RpcPayload.deepCopyFrom(input, parent, owner)));
2883
+ let inputHook = new PayloadStubHook(RpcPayload.deepCopyFrom(input, parent, owner));
2884
+ let mapper = new MapApplicator(captures, inputHook);
2784
2885
  try {
2785
2886
  return mapper.apply(instructions);
2786
2887
  } finally {
@@ -2813,9 +2914,10 @@ mapImpl.applyMap = (input, parent, owner, captures, instructions) => {
2813
2914
  var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
2814
2915
  state;
2815
2916
  static create(stream) {
2917
+ let writer = stream.getWriter();
2816
2918
  return new WritableStreamStubHook({
2817
2919
  refcount: 1,
2818
- writer: stream.getWriter(),
2920
+ writer,
2819
2921
  closed: false
2820
2922
  });
2821
2923
  }
@@ -2833,13 +2935,12 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
2833
2935
  let state = this.getState();
2834
2936
  if (path.length !== 1 || typeof path[0] !== "string") throw new Error("WritableStream stub only supports direct method calls");
2835
2937
  const method = path[0];
2836
- if (method !== "write" && method !== "close" && method !== "abort") {
2837
- args.dispose();
2838
- throw new Error(`Unknown WritableStream method: ${method}`);
2839
- }
2938
+ if (method !== "write" && method !== "close" && method !== "abort") throw new Error(`Unknown WritableStream method: ${method}`);
2840
2939
  if (method === "close" || method === "abort") state.closed = true;
2841
- return new PromiseStubHook((method === "write" ? args.deliverStreamWrite(state.writer) : args.deliverCall(state.writer[method], state.writer)).then((payload) => new PayloadStubHook(payload)));
2940
+ let promise = method === "write" ? args.deliverStreamWrite(state.writer) : args.deliverCall(state.writer[method], state.writer);
2941
+ return new PromiseStubHook(promise.then((payload) => new PayloadStubHook(payload)));
2842
2942
  } catch (err) {
2943
+ args.dispose();
2843
2944
  return new ErrorStubHook(err);
2844
2945
  }
2845
2946
  }
@@ -2851,7 +2952,8 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
2851
2952
  return new ErrorStubHook(/* @__PURE__ */ new Error("Cannot access properties on a WritableStream stub"));
2852
2953
  }
2853
2954
  dup() {
2854
- return new WritableStreamStubHook(this.getState(), this);
2955
+ let state = this.getState();
2956
+ return new WritableStreamStubHook(state, this);
2855
2957
  }
2856
2958
  pull() {
2857
2959
  return Promise.reject(/* @__PURE__ */ new Error("Cannot pull a WritableStream stub"));
@@ -2869,9 +2971,9 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
2869
2971
  }
2870
2972
  onBroken(callback) {}
2871
2973
  };
2872
- const INITIAL_WINDOW = 256 * 1024;
2873
- const MAX_WINDOW = 1024 * 1024 * 1024;
2874
- const MIN_WINDOW = 64 * 1024;
2974
+ const INITIAL_WINDOW = 262144;
2975
+ const MAX_WINDOW = 1073741824;
2976
+ const MIN_WINDOW = 65536;
2875
2977
  const STARTUP_GROWTH_FACTOR = 2;
2876
2978
  const STEADY_GROWTH_FACTOR = 1.25;
2877
2979
  const DECAY_FACTOR = .9;