capnweb 0.0.0-92baaed → 0.0.0-9751a4e

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.
@@ -47,9 +47,22 @@ function typeForRpc(value) {
47
47
  case Array.prototype: return "array";
48
48
  case Date.prototype: return "date";
49
49
  case Uint8Array.prototype:
50
- case BUFFER_PROTOTYPE: return "bytes";
50
+ case BUFFER_PROTOTYPE:
51
+ case ArrayBuffer.prototype:
52
+ case DataView.prototype:
53
+ case Int8Array.prototype:
54
+ case Uint8ClampedArray.prototype:
55
+ case Int16Array.prototype:
56
+ case Uint16Array.prototype:
57
+ case Int32Array.prototype:
58
+ case Uint32Array.prototype:
59
+ case BigInt64Array.prototype:
60
+ case BigUint64Array.prototype:
61
+ case Float32Array.prototype:
62
+ case Float64Array.prototype: return "bytes";
51
63
  case WritableStream.prototype: return "writable";
52
64
  case ReadableStream.prototype: return "readable";
65
+ case URL.prototype: return "url";
53
66
  case Headers.prototype: return "headers";
54
67
  case Request.prototype: return "request";
55
68
  case Response.prototype: return "response";
@@ -83,7 +96,14 @@ let streamImpl = {
83
96
  };
84
97
  var StubHook = class {
85
98
  stream(path, args) {
86
- let pulled = this.call(path, args).pull();
99
+ let hook = this.call(path, args);
100
+ let pulled;
101
+ try {
102
+ pulled = hook.pull();
103
+ } catch (err) {
104
+ hook.dispose();
105
+ throw err;
106
+ }
87
107
  let promise;
88
108
  if (pulled instanceof Promise) promise = pulled.then((p) => {
89
109
  p.dispose();
@@ -102,9 +122,11 @@ var ErrorStubHook = class extends StubHook {
102
122
  this.error = error;
103
123
  }
104
124
  call(path, args) {
125
+ args.dispose();
105
126
  return this;
106
127
  }
107
128
  map(path, captures, instructions) {
129
+ for (let cap of captures) cap.dispose();
108
130
  return this;
109
131
  }
110
132
  get(path) {
@@ -228,7 +250,25 @@ var RpcStub$1 = class RpcStub$1 extends RpcTarget$1 {
228
250
  };
229
251
  var RpcPromise$1 = class extends RpcStub$1 {
230
252
  constructor(hook, pathIfPromise) {
231
- super(hook, pathIfPromise);
253
+ if (hook instanceof StubHook) super(hook, pathIfPromise ?? []);
254
+ else {
255
+ if (pathIfPromise !== void 0) throw new TypeError("RpcPromise constructor expected one argument, received two.");
256
+ let kind = typeForRpc(hook);
257
+ if (kind === "rpc-promise") {
258
+ let raw = unwrapStubAndPath(hook);
259
+ if (raw.pathIfPromise.length > 0) super(raw.hook, raw.pathIfPromise);
260
+ else {
261
+ let adopted = raw.hook;
262
+ raw.hook = DISPOSED_HOOK;
263
+ super(adopted, []);
264
+ }
265
+ } else if (kind === "rpc-thenable") super(TargetStubHook.create(hook, void 0), []);
266
+ else {
267
+ let promiseHook = new PromiseStubHook(Promise.resolve(hook).then((value) => new PayloadStubHook(RpcPayload.fromAppReturn(value))));
268
+ promiseHook.ignoreUnhandledRejections();
269
+ super(promiseHook, []);
270
+ }
271
+ }
232
272
  }
233
273
  then(onfulfilled, onrejected) {
234
274
  return pullPromise(this).then(...arguments);
@@ -384,6 +424,7 @@ var RpcPayload = class RpcPayload {
384
424
  case "date":
385
425
  case "bytes":
386
426
  case "blob":
427
+ case "url":
387
428
  case "error":
388
429
  case "undefined": return value;
389
430
  case "array": {
@@ -517,6 +558,30 @@ var RpcPayload = class RpcPayload {
517
558
  this.dispose();
518
559
  }
519
560
  }
561
+ async deliverStreamWrite(writer) {
562
+ try {
563
+ let promises = [];
564
+ this.deliverTo(this, "value", promises);
565
+ if (promises.length > 0) await Promise.all(promises);
566
+ let chunk = this.value[0];
567
+ if ((this.hooks.length > 0 || this.promises.length > 0) && chunk instanceof Object) {
568
+ if (!(Symbol.dispose in chunk)) Object.defineProperty(chunk, Symbol.dispose, {
569
+ value: () => this.dispose(),
570
+ writable: true,
571
+ enumerable: false,
572
+ configurable: true
573
+ });
574
+ await writer.write(chunk);
575
+ return RpcPayload.fromAppReturn(void 0);
576
+ }
577
+ await writer.write(chunk);
578
+ this.dispose();
579
+ return RpcPayload.fromAppReturn(void 0);
580
+ } catch (err) {
581
+ this.dispose();
582
+ throw err;
583
+ }
584
+ }
520
585
  async deliverResolve() {
521
586
  try {
522
587
  let promises = [];
@@ -557,6 +622,7 @@ var RpcPayload = class RpcPayload {
557
622
  case "bytes":
558
623
  case "blob":
559
624
  case "date":
625
+ case "url":
560
626
  case "error":
561
627
  case "undefined": return;
562
628
  case "array": {
@@ -639,6 +705,7 @@ var RpcPayload = class RpcPayload {
639
705
  case "rpc-target":
640
706
  case "writable":
641
707
  case "readable":
708
+ case "url":
642
709
  case "headers":
643
710
  case "request":
644
711
  case "response": return;
@@ -708,6 +775,7 @@ function followPath(value, parent, path, owner) {
708
775
  case "blob":
709
776
  case "date":
710
777
  case "error":
778
+ case "url":
711
779
  case "headers":
712
780
  case "request":
713
781
  case "response":
@@ -740,17 +808,22 @@ function followPath(value, parent, path, owner) {
740
808
  }
741
809
  var ValueStubHook = class extends StubHook {
742
810
  call(path, args) {
811
+ let followResult;
743
812
  try {
744
813
  let { value, owner } = this.getValue();
745
- let followResult = followPath(value, void 0, path, owner);
746
- if (followResult.hook) return followResult.hook.call(followResult.remainingPath, args);
747
- if (typeof followResult.value != "function") throw new TypeError(`'${path.join(".")}' is not a function.`);
748
- return new PromiseStubHook(args.deliverCall(followResult.value, followResult.parent).then((payload) => {
749
- return new PayloadStubHook(payload);
750
- }));
814
+ followResult = followPath(value, void 0, path, owner);
751
815
  } catch (err) {
816
+ args.dispose();
752
817
  return new ErrorStubHook(err);
753
818
  }
819
+ if (followResult.hook) return followResult.hook.call(followResult.remainingPath, args);
820
+ if (typeof followResult.value != "function") {
821
+ args.dispose();
822
+ return new ErrorStubHook(/* @__PURE__ */ new TypeError(`'${path.join(".")}' is not a function.`));
823
+ }
824
+ return new PromiseStubHook(args.deliverCall(followResult.value, followResult.parent).then((payload) => {
825
+ return new PayloadStubHook(payload);
826
+ }));
754
827
  }
755
828
  map(path, captures, instructions) {
756
829
  try {
@@ -771,7 +844,10 @@ var ValueStubHook = class extends StubHook {
771
844
  get(path) {
772
845
  try {
773
846
  let { value, owner } = this.getValue();
774
- if (path.length === 0 && owner === null) throw new Error("Can't dup an RpcTarget stub as a promise.");
847
+ if (path.length === 0 && owner === null) {
848
+ if (value instanceof Object && "then" in value) return this.dup();
849
+ throw new Error("Can't dup an RpcTarget stub as a promise.");
850
+ }
775
851
  let followResult = followPath(value, void 0, path, owner);
776
852
  if (followResult.hook) return followResult.hook.get(followResult.remainingPath);
777
853
  return new PayloadStubHook(RpcPayload.deepCopyFrom(followResult.value, followResult.parent, followResult.owner));
@@ -874,7 +950,10 @@ var TargetStubHook = class TargetStubHook extends ValueStubHook {
874
950
  this.target = void 0;
875
951
  }
876
952
  }
877
- onBroken(callback) {}
953
+ onBroken(callback) {
954
+ let target = this.target;
955
+ if (target && "then" in target) Promise.resolve(target).then(() => {}, callback);
956
+ }
878
957
  };
879
958
  var PromiseStubHook = class PromiseStubHook extends StubHook {
880
959
  promise;
@@ -888,12 +967,16 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
888
967
  }
889
968
  call(path, args) {
890
969
  args.ensureDeepCopied();
891
- return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args)));
970
+ return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args), (err) => {
971
+ args.dispose();
972
+ throw err;
973
+ }));
892
974
  }
893
975
  stream(path, args) {
894
976
  args.ensureDeepCopied();
895
- return { promise: this.promise.then((hook) => {
896
- return hook.stream(path, args).promise;
977
+ return { promise: this.promise.then((hook) => hook.stream(path, args).promise, (err) => {
978
+ args.dispose();
979
+ throw err;
897
980
  }) };
898
981
  }
899
982
  map(path, captures, instructions) {
@@ -920,10 +1003,7 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
920
1003
  }, (err) => {});
921
1004
  }
922
1005
  dispose() {
923
- if (this.resolution) this.resolution.dispose();
924
- else this.promise.then((hook) => {
925
- hook.dispose();
926
- }, (err) => {});
1006
+ this.promise.then((hook) => hook.dispose(), () => {});
927
1007
  }
928
1008
  onBroken(callback) {
929
1009
  if (this.resolution) this.resolution.onBroken(callback);
@@ -941,6 +1021,71 @@ const DEFAULT_LIMITS = {
941
1021
  maxDepth: 256,
942
1022
  maxMessageSize: 32 * 1024 * 1024
943
1023
  };
1024
+ const NATIVE_LITTLE_ENDIAN = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
1025
+ const BYTE_CONTAINER_TYPE_NAMES = [
1026
+ "ArrayBuffer",
1027
+ "DataView",
1028
+ "Int8Array",
1029
+ "Uint8Array",
1030
+ "Uint8ClampedArray",
1031
+ "Int16Array",
1032
+ "Uint16Array",
1033
+ "Int32Array",
1034
+ "Uint32Array",
1035
+ "BigInt64Array",
1036
+ "BigUint64Array",
1037
+ "Float32Array",
1038
+ "Float64Array"
1039
+ ];
1040
+ function isValidByteContainerName(value) {
1041
+ return BYTE_CONTAINER_TYPE_NAMES.includes(value);
1042
+ }
1043
+ const TYPED_ARRAY_ELEMENT_SIZE = {
1044
+ ArrayBuffer: void 0,
1045
+ DataView: void 0,
1046
+ Int8Array: void 0,
1047
+ Uint8Array: void 0,
1048
+ Uint8ClampedArray: void 0,
1049
+ Int16Array: 2,
1050
+ Uint16Array: 2,
1051
+ Int32Array: 4,
1052
+ Uint32Array: 4,
1053
+ BigInt64Array: 8,
1054
+ BigUint64Array: 8,
1055
+ Float32Array: 4,
1056
+ Float64Array: 8
1057
+ };
1058
+ const BYTE_CONTAINER_PROTOTYPES = {
1059
+ ArrayBuffer: ArrayBuffer.prototype,
1060
+ DataView: DataView.prototype,
1061
+ Int8Array: Int8Array.prototype,
1062
+ Uint8ClampedArray: Uint8ClampedArray.prototype,
1063
+ Int16Array: Int16Array.prototype,
1064
+ Uint16Array: Uint16Array.prototype,
1065
+ Int32Array: Int32Array.prototype,
1066
+ Uint32Array: Uint32Array.prototype,
1067
+ BigInt64Array: BigInt64Array.prototype,
1068
+ BigUint64Array: BigUint64Array.prototype,
1069
+ Float32Array: Float32Array.prototype,
1070
+ Float64Array: Float64Array.prototype
1071
+ };
1072
+ const BYTE_CONTAINER_TYPE_BY_PROTOTYPE = /* @__PURE__ */ new Map();
1073
+ for (let type of Object.keys(BYTE_CONTAINER_PROTOTYPES)) BYTE_CONTAINER_TYPE_BY_PROTOTYPE.set(BYTE_CONTAINER_PROTOTYPES[type], type);
1074
+ function swapByteOrder(bytes, elementSize) {
1075
+ if (elementSize !== 2 && elementSize !== 4 && elementSize !== 8) throw new RangeError(`Unsupported element size: ${elementSize}`);
1076
+ let view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
1077
+ for (let offset = 0; offset < bytes.byteLength; offset += elementSize) switch (elementSize) {
1078
+ case 2:
1079
+ view.setUint16(offset, view.getUint16(offset, false), true);
1080
+ break;
1081
+ case 4:
1082
+ view.setUint32(offset, view.getUint32(offset, false), true);
1083
+ break;
1084
+ case 8:
1085
+ view.setBigUint64(offset, view.getBigUint64(offset, false), true);
1086
+ break;
1087
+ }
1088
+ }
944
1089
  var NullExporter = class {
945
1090
  exportStub(stub) {
946
1091
  throw new Error("Cannot serialize RPC stubs without an RPC session.");
@@ -960,7 +1105,8 @@ async function streamToBlob(stream, type) {
960
1105
  let b = await new Response(stream).blob();
961
1106
  return b.type === type ? b : b.slice(0, b.size, type);
962
1107
  }
963
- const ERROR_TYPES = Object.assign(Object.create(null), {
1108
+ const ERROR_TYPES = {
1109
+ __proto__: null,
964
1110
  Error,
965
1111
  EvalError,
966
1112
  RangeError,
@@ -969,7 +1115,7 @@ const ERROR_TYPES = Object.assign(Object.create(null), {
969
1115
  TypeError,
970
1116
  URIError,
971
1117
  AggregateError
972
- });
1118
+ };
973
1119
  var Devaluator = class Devaluator {
974
1120
  exporter;
975
1121
  source;
@@ -1031,18 +1177,40 @@ var Devaluator = class Devaluator {
1031
1177
  return ["date", Number.isNaN(time) ? null : time];
1032
1178
  }
1033
1179
  case "bytes": {
1034
- let bytes = value;
1035
- if (this.encodingLevel === "structuredClonable" || this.encodingLevel === "jsonCompatibleWithBytes") return ["bytes", bytes];
1036
- if (bytes.toBase64) return ["bytes", bytes.toBase64({ omitPadding: true })];
1180
+ let alternateTypeName = BYTE_CONTAINER_TYPE_BY_PROTOTYPE.get(Object.getPrototypeOf(value));
1181
+ let bytes;
1182
+ if (alternateTypeName === "ArrayBuffer") bytes = new Uint8Array(value);
1183
+ else if (alternateTypeName === void 0) bytes = value;
1184
+ else {
1185
+ let view = value;
1186
+ bytes = new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
1187
+ let elementSize = TYPED_ARRAY_ELEMENT_SIZE[alternateTypeName];
1188
+ if (!NATIVE_LITTLE_ENDIAN && elementSize) {
1189
+ bytes = bytes.slice();
1190
+ swapByteOrder(bytes, elementSize);
1191
+ }
1192
+ }
1193
+ if (this.encodingLevel === "structuredClonable" || this.encodingLevel === "jsonCompatibleWithBytes") return alternateTypeName === void 0 ? ["bytes", bytes] : [
1194
+ "bytes",
1195
+ bytes,
1196
+ alternateTypeName
1197
+ ];
1037
1198
  let b64;
1038
- if (typeof Buffer !== "undefined") b64 = (bytes instanceof Buffer ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)).toString("base64");
1199
+ if (bytes.toBase64) b64 = bytes.toBase64({ omitPadding: true });
1200
+ else if (typeof Buffer !== "undefined") b64 = (bytes instanceof Buffer ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)).toString("base64");
1039
1201
  else {
1040
1202
  let binary = "";
1041
1203
  for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]);
1042
1204
  b64 = btoa(binary);
1043
1205
  }
1044
- return ["bytes", b64.replace(/=+$/, "")];
1206
+ b64 = b64.replace(/=+$/, "");
1207
+ return alternateTypeName === void 0 ? ["bytes", b64] : [
1208
+ "bytes",
1209
+ b64,
1210
+ alternateTypeName
1211
+ ];
1045
1212
  }
1213
+ case "url": return ["url", value.href];
1046
1214
  case "headers": return ["headers", [...value]];
1047
1215
  case "request": {
1048
1216
  let req = value;
@@ -1294,18 +1462,46 @@ var Evaluator = class Evaluator {
1294
1462
  if (value[1] === null) return /* @__PURE__ */ new Date(NaN);
1295
1463
  if (typeof value[1] == "number") return new Date(value[1]);
1296
1464
  break;
1297
- case "bytes":
1298
- if (value[1] instanceof Uint8Array) return value[1];
1299
- if (typeof value[1] == "string") if (typeof Buffer !== "undefined") return Buffer.from(value[1], "base64");
1300
- else if (Uint8Array.fromBase64) return Uint8Array.fromBase64(value[1]);
1465
+ case "bytes": {
1466
+ let bytes;
1467
+ if (value[1] instanceof Uint8Array) bytes = value[1];
1468
+ else if (typeof value[1] == "string") if (typeof Buffer !== "undefined") bytes = Buffer.from(value[1], "base64");
1469
+ else if (Uint8Array.fromBase64) bytes = Uint8Array.fromBase64(value[1]);
1301
1470
  else {
1302
1471
  let bs = atob(value[1]);
1303
1472
  let len = bs.length;
1304
- let bytes = new Uint8Array(len);
1473
+ bytes = new Uint8Array(len);
1305
1474
  for (let i = 0; i < len; i++) bytes[i] = bs.charCodeAt(i);
1306
- return bytes;
1307
1475
  }
1308
- break;
1476
+ else break;
1477
+ if (value.length === 2) return bytes;
1478
+ if (typeof value[2] !== "string") throw new TypeError(`Unknown bytes type marker type: ${typeof value[2]}`);
1479
+ if (!isValidByteContainerName(value[2])) {
1480
+ let marker = value[2].slice(0, 64);
1481
+ throw new TypeError(`Unknown bytes type marker: ${marker}`);
1482
+ }
1483
+ let marker = value[2];
1484
+ let elementSize = TYPED_ARRAY_ELEMENT_SIZE[marker];
1485
+ if (elementSize !== void 0 && bytes.byteLength % elementSize !== 0) throw new TypeError(`Invalid byte length ${bytes.byteLength} for ${marker}; expected a multiple of ${elementSize}`);
1486
+ let buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
1487
+ if (!NATIVE_LITTLE_ENDIAN && elementSize !== void 0) swapByteOrder(new Uint8Array(buffer), elementSize);
1488
+ switch (marker) {
1489
+ case "ArrayBuffer": return buffer;
1490
+ case "DataView": return new DataView(buffer);
1491
+ case "Int8Array": return new Int8Array(buffer);
1492
+ case "Uint8Array": return new Uint8Array(buffer);
1493
+ case "Uint8ClampedArray": return new Uint8ClampedArray(buffer);
1494
+ case "Int16Array": return new Int16Array(buffer);
1495
+ case "Uint16Array": return new Uint16Array(buffer);
1496
+ case "Int32Array": return new Int32Array(buffer);
1497
+ case "Uint32Array": return new Uint32Array(buffer);
1498
+ case "BigInt64Array": return new BigInt64Array(buffer);
1499
+ case "BigUint64Array": return new BigUint64Array(buffer);
1500
+ case "Float32Array": return new Float32Array(buffer);
1501
+ case "Float64Array": return new Float64Array(buffer);
1502
+ default:
1503
+ }
1504
+ }
1309
1505
  case "error":
1310
1506
  if (value.length >= 3 && typeof value[1] === "string" && typeof value[2] === "string") {
1311
1507
  let cls = ERROR_TYPES[value[1]] || Error;
@@ -1334,6 +1530,9 @@ var Evaluator = class Evaluator {
1334
1530
  case "inf": return Infinity;
1335
1531
  case "-inf": return -Infinity;
1336
1532
  case "nan": return NaN;
1533
+ case "url":
1534
+ if (value.length === 2 && typeof value[1] === "string") return new URL(value[1]);
1535
+ break;
1337
1536
  case "headers":
1338
1537
  if (value.length === 2 && value[1] instanceof Array) return new Headers(value[1]);
1339
1538
  break;
@@ -1555,10 +1754,6 @@ var ImportTableEntry = class {
1555
1754
  resolution;
1556
1755
  onBrokenRegistrations;
1557
1756
  resolve(resolution) {
1558
- if (this.resolution) {
1559
- resolution.dispose();
1560
- return;
1561
- }
1562
1757
  if (this.localRefcount == 0) {
1563
1758
  resolution.dispose();
1564
1759
  return;
@@ -1637,24 +1832,28 @@ var RpcImportHook = class RpcImportHook extends StubHook {
1637
1832
  if (this.entry) return this.entry;
1638
1833
  else throw new Error("This RpcImportHook was already disposed.");
1639
1834
  }
1835
+ getEntryTakingOwnership(disposeOwned) {
1836
+ try {
1837
+ return this.getEntry();
1838
+ } catch (err) {
1839
+ disposeOwned();
1840
+ throw err;
1841
+ }
1842
+ }
1640
1843
  call(path, args) {
1641
- let entry = this.getEntry();
1844
+ let entry = this.getEntryTakingOwnership(() => args.dispose());
1642
1845
  if (entry.resolution) return entry.resolution.call(path, args);
1643
1846
  else return entry.session.sendCall(entry.importId, path, args);
1644
1847
  }
1645
1848
  stream(path, args) {
1646
- let entry = this.getEntry();
1849
+ let entry = this.getEntryTakingOwnership(() => args.dispose());
1647
1850
  if (entry.resolution) return entry.resolution.stream(path, args);
1648
1851
  else return entry.session.sendStream(entry.importId, path, args);
1649
1852
  }
1650
1853
  map(path, captures, instructions) {
1651
- let entry;
1652
- try {
1653
- entry = this.getEntry();
1654
- } catch (err) {
1854
+ let entry = this.getEntryTakingOwnership(() => {
1655
1855
  for (let cap of captures) cap.dispose();
1656
- throw err;
1657
- }
1856
+ });
1658
1857
  if (entry.resolution) return entry.resolution.map(path, captures, instructions);
1659
1858
  else return entry.session.sendMap(entry.importId, path, captures, instructions);
1660
1859
  }
@@ -1915,14 +2114,23 @@ var RpcSessionImpl = class {
1915
2114
  }
1916
2115
  }
1917
2116
  sendCall(id, path, args) {
1918
- if (this.abortReason) throw this.abortReason;
2117
+ if (this.abortReason) {
2118
+ args?.dispose();
2119
+ throw this.abortReason;
2120
+ }
1919
2121
  let value = [
1920
2122
  "pipeline",
1921
2123
  id,
1922
2124
  path
1923
2125
  ];
1924
2126
  if (args) {
1925
- let devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
2127
+ let devalue;
2128
+ try {
2129
+ devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
2130
+ } catch (err) {
2131
+ args.dispose();
2132
+ throw err;
2133
+ }
1926
2134
  value.push(devalue[0]);
1927
2135
  }
1928
2136
  this.send(["push", value]);
@@ -1931,13 +2139,22 @@ var RpcSessionImpl = class {
1931
2139
  return new RpcImportHook(true, entry);
1932
2140
  }
1933
2141
  sendStream(id, path, args) {
1934
- if (this.abortReason) throw this.abortReason;
2142
+ if (this.abortReason) {
2143
+ args.dispose();
2144
+ throw this.abortReason;
2145
+ }
1935
2146
  let value = [
1936
2147
  "pipeline",
1937
2148
  id,
1938
2149
  path
1939
2150
  ];
1940
- 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
+ }
1941
2158
  value.push(devalue[0]);
1942
2159
  let msg = ["stream", value];
1943
2160
  let size = this.send(msg);
@@ -2158,6 +2375,8 @@ var RpcSession$1 = class {
2158
2375
 
2159
2376
  //#endregion
2160
2377
  //#region src/websocket.ts
2378
+ /** Close-frame reason max UTF-8 bytes: 125 payload - 2-byte status (RFC 6455 section 5.5). */
2379
+ const MAX_CLOSE_REASON_BYTES = 123;
2161
2380
  function newWebSocketRpcSession$1(webSocket, localMain, options) {
2162
2381
  if (typeof webSocket === "string") webSocket = new WebSocket(webSocket);
2163
2382
  return new RpcSession$1(new WebSocketTransport(webSocket), localMain, options).getRemoteMain();
@@ -2252,6 +2471,7 @@ var WebSocketTransport = class {
2252
2471
 
2253
2472
  //#endregion
2254
2473
  //#region src/batch.ts
2474
+ const yieldToMacrotask = typeof setImmediate === "function" ? () => new Promise((resolve) => setImmediate(resolve)) : () => new Promise((resolve) => setTimeout(resolve, 0));
2255
2475
  var BatchClientTransport = class {
2256
2476
  constructor(sendBatch) {
2257
2477
  this.#promise = this.#scheduleBatch(sendBatch);
@@ -2273,7 +2493,7 @@ var BatchClientTransport = class {
2273
2493
  this.#aborted = reason;
2274
2494
  }
2275
2495
  async #scheduleBatch(sendBatch) {
2276
- await new Promise((resolve) => setTimeout(resolve, 0));
2496
+ await yieldToMacrotask();
2277
2497
  if (this.#aborted !== void 0) throw this.#aborted;
2278
2498
  let batch = this.#batchToSend;
2279
2499
  this.#batchToSend = null;
@@ -2565,9 +2785,11 @@ var MapVariableHook = class extends StubHook {
2565
2785
  else throwMapperBuilderUseError();
2566
2786
  }
2567
2787
  call(path, args) {
2788
+ args.dispose();
2568
2789
  throwMapperBuilderUseError();
2569
2790
  }
2570
2791
  map(path, captures, instructions) {
2792
+ for (let cap of captures) cap.dispose();
2571
2793
  throwMapperBuilderUseError();
2572
2794
  }
2573
2795
  pull() {
@@ -2678,14 +2900,11 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
2678
2900
  let state = this.getState();
2679
2901
  if (path.length !== 1 || typeof path[0] !== "string") throw new Error("WritableStream stub only supports direct method calls");
2680
2902
  const method = path[0];
2681
- if (method !== "write" && method !== "close" && method !== "abort") {
2682
- args.dispose();
2683
- throw new Error(`Unknown WritableStream method: ${method}`);
2684
- }
2903
+ if (method !== "write" && method !== "close" && method !== "abort") throw new Error(`Unknown WritableStream method: ${method}`);
2685
2904
  if (method === "close" || method === "abort") state.closed = true;
2686
- let func = state.writer[method];
2687
- return new PromiseStubHook(args.deliverCall(func, state.writer).then((payload) => new PayloadStubHook(payload)));
2905
+ return new PromiseStubHook((method === "write" ? args.deliverStreamWrite(state.writer) : args.deliverCall(state.writer[method], state.writer)).then((payload) => new PayloadStubHook(payload)));
2688
2906
  } catch (err) {
2907
+ args.dispose();
2689
2908
  return new ErrorStubHook(err);
2690
2909
  }
2691
2910
  }