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.
package/dist/index-bun.js CHANGED
@@ -41,9 +41,22 @@ function typeForRpc(value) {
41
41
  case Array.prototype: return "array";
42
42
  case Date.prototype: return "date";
43
43
  case Uint8Array.prototype:
44
- case BUFFER_PROTOTYPE: return "bytes";
44
+ case BUFFER_PROTOTYPE:
45
+ case ArrayBuffer.prototype:
46
+ case DataView.prototype:
47
+ case Int8Array.prototype:
48
+ case Uint8ClampedArray.prototype:
49
+ case Int16Array.prototype:
50
+ case Uint16Array.prototype:
51
+ case Int32Array.prototype:
52
+ case Uint32Array.prototype:
53
+ case BigInt64Array.prototype:
54
+ case BigUint64Array.prototype:
55
+ case Float32Array.prototype:
56
+ case Float64Array.prototype: return "bytes";
45
57
  case WritableStream.prototype: return "writable";
46
58
  case ReadableStream.prototype: return "readable";
59
+ case URL.prototype: return "url";
47
60
  case Headers.prototype: return "headers";
48
61
  case Request.prototype: return "request";
49
62
  case Response.prototype: return "response";
@@ -77,7 +90,14 @@ let streamImpl = {
77
90
  };
78
91
  var StubHook = class {
79
92
  stream(path, args) {
80
- let pulled = this.call(path, args).pull();
93
+ let hook = this.call(path, args);
94
+ let pulled;
95
+ try {
96
+ pulled = hook.pull();
97
+ } catch (err) {
98
+ hook.dispose();
99
+ throw err;
100
+ }
81
101
  let promise;
82
102
  if (pulled instanceof Promise) promise = pulled.then((p) => {
83
103
  p.dispose();
@@ -96,9 +116,11 @@ var ErrorStubHook = class extends StubHook {
96
116
  this.error = error;
97
117
  }
98
118
  call(path, args) {
119
+ args.dispose();
99
120
  return this;
100
121
  }
101
122
  map(path, captures, instructions) {
123
+ for (let cap of captures) cap.dispose();
102
124
  return this;
103
125
  }
104
126
  get(path) {
@@ -222,7 +244,25 @@ var RpcStub$1 = class RpcStub$1 extends RpcTarget$1 {
222
244
  };
223
245
  var RpcPromise$1 = class extends RpcStub$1 {
224
246
  constructor(hook, pathIfPromise) {
225
- super(hook, pathIfPromise);
247
+ if (hook instanceof StubHook) super(hook, pathIfPromise ?? []);
248
+ else {
249
+ if (pathIfPromise !== void 0) throw new TypeError("RpcPromise constructor expected one argument, received two.");
250
+ let kind = typeForRpc(hook);
251
+ if (kind === "rpc-promise") {
252
+ let raw = unwrapStubAndPath(hook);
253
+ if (raw.pathIfPromise.length > 0) super(raw.hook, raw.pathIfPromise);
254
+ else {
255
+ let adopted = raw.hook;
256
+ raw.hook = DISPOSED_HOOK;
257
+ super(adopted, []);
258
+ }
259
+ } else if (kind === "rpc-thenable") super(TargetStubHook.create(hook, void 0), []);
260
+ else {
261
+ let promiseHook = new PromiseStubHook(Promise.resolve(hook).then((value) => new PayloadStubHook(RpcPayload.fromAppReturn(value))));
262
+ promiseHook.ignoreUnhandledRejections();
263
+ super(promiseHook, []);
264
+ }
265
+ }
226
266
  }
227
267
  then(onfulfilled, onrejected) {
228
268
  return pullPromise(this).then(...arguments);
@@ -378,6 +418,7 @@ var RpcPayload = class RpcPayload {
378
418
  case "date":
379
419
  case "bytes":
380
420
  case "blob":
421
+ case "url":
381
422
  case "error":
382
423
  case "undefined": return value;
383
424
  case "array": {
@@ -511,6 +552,30 @@ var RpcPayload = class RpcPayload {
511
552
  this.dispose();
512
553
  }
513
554
  }
555
+ async deliverStreamWrite(writer) {
556
+ try {
557
+ let promises = [];
558
+ this.deliverTo(this, "value", promises);
559
+ if (promises.length > 0) await Promise.all(promises);
560
+ let chunk = this.value[0];
561
+ if ((this.hooks.length > 0 || this.promises.length > 0) && chunk instanceof Object) {
562
+ if (!(Symbol.dispose in chunk)) Object.defineProperty(chunk, Symbol.dispose, {
563
+ value: () => this.dispose(),
564
+ writable: true,
565
+ enumerable: false,
566
+ configurable: true
567
+ });
568
+ await writer.write(chunk);
569
+ return RpcPayload.fromAppReturn(void 0);
570
+ }
571
+ await writer.write(chunk);
572
+ this.dispose();
573
+ return RpcPayload.fromAppReturn(void 0);
574
+ } catch (err) {
575
+ this.dispose();
576
+ throw err;
577
+ }
578
+ }
514
579
  async deliverResolve() {
515
580
  try {
516
581
  let promises = [];
@@ -551,6 +616,7 @@ var RpcPayload = class RpcPayload {
551
616
  case "bytes":
552
617
  case "blob":
553
618
  case "date":
619
+ case "url":
554
620
  case "error":
555
621
  case "undefined": return;
556
622
  case "array": {
@@ -633,6 +699,7 @@ var RpcPayload = class RpcPayload {
633
699
  case "rpc-target":
634
700
  case "writable":
635
701
  case "readable":
702
+ case "url":
636
703
  case "headers":
637
704
  case "request":
638
705
  case "response": return;
@@ -702,6 +769,7 @@ function followPath(value, parent, path, owner) {
702
769
  case "blob":
703
770
  case "date":
704
771
  case "error":
772
+ case "url":
705
773
  case "headers":
706
774
  case "request":
707
775
  case "response":
@@ -734,17 +802,22 @@ function followPath(value, parent, path, owner) {
734
802
  }
735
803
  var ValueStubHook = class extends StubHook {
736
804
  call(path, args) {
805
+ let followResult;
737
806
  try {
738
807
  let { value, owner } = this.getValue();
739
- let followResult = followPath(value, void 0, path, owner);
740
- if (followResult.hook) return followResult.hook.call(followResult.remainingPath, args);
741
- if (typeof followResult.value != "function") throw new TypeError(`'${path.join(".")}' is not a function.`);
742
- return new PromiseStubHook(args.deliverCall(followResult.value, followResult.parent).then((payload) => {
743
- return new PayloadStubHook(payload);
744
- }));
808
+ followResult = followPath(value, void 0, path, owner);
745
809
  } catch (err) {
810
+ args.dispose();
746
811
  return new ErrorStubHook(err);
747
812
  }
813
+ if (followResult.hook) return followResult.hook.call(followResult.remainingPath, args);
814
+ if (typeof followResult.value != "function") {
815
+ args.dispose();
816
+ return new ErrorStubHook(/* @__PURE__ */ new TypeError(`'${path.join(".")}' is not a function.`));
817
+ }
818
+ return new PromiseStubHook(args.deliverCall(followResult.value, followResult.parent).then((payload) => {
819
+ return new PayloadStubHook(payload);
820
+ }));
748
821
  }
749
822
  map(path, captures, instructions) {
750
823
  try {
@@ -765,7 +838,10 @@ var ValueStubHook = class extends StubHook {
765
838
  get(path) {
766
839
  try {
767
840
  let { value, owner } = this.getValue();
768
- if (path.length === 0 && owner === null) throw new Error("Can't dup an RpcTarget stub as a promise.");
841
+ if (path.length === 0 && owner === null) {
842
+ if (value instanceof Object && "then" in value) return this.dup();
843
+ throw new Error("Can't dup an RpcTarget stub as a promise.");
844
+ }
769
845
  let followResult = followPath(value, void 0, path, owner);
770
846
  if (followResult.hook) return followResult.hook.get(followResult.remainingPath);
771
847
  return new PayloadStubHook(RpcPayload.deepCopyFrom(followResult.value, followResult.parent, followResult.owner));
@@ -868,7 +944,10 @@ var TargetStubHook = class TargetStubHook extends ValueStubHook {
868
944
  this.target = void 0;
869
945
  }
870
946
  }
871
- onBroken(callback) {}
947
+ onBroken(callback) {
948
+ let target = this.target;
949
+ if (target && "then" in target) Promise.resolve(target).then(() => {}, callback);
950
+ }
872
951
  };
873
952
  var PromiseStubHook = class PromiseStubHook extends StubHook {
874
953
  promise;
@@ -882,12 +961,16 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
882
961
  }
883
962
  call(path, args) {
884
963
  args.ensureDeepCopied();
885
- return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args)));
964
+ return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args), (err) => {
965
+ args.dispose();
966
+ throw err;
967
+ }));
886
968
  }
887
969
  stream(path, args) {
888
970
  args.ensureDeepCopied();
889
- return { promise: this.promise.then((hook) => {
890
- return hook.stream(path, args).promise;
971
+ return { promise: this.promise.then((hook) => hook.stream(path, args).promise, (err) => {
972
+ args.dispose();
973
+ throw err;
891
974
  }) };
892
975
  }
893
976
  map(path, captures, instructions) {
@@ -914,10 +997,7 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
914
997
  }, (err) => {});
915
998
  }
916
999
  dispose() {
917
- if (this.resolution) this.resolution.dispose();
918
- else this.promise.then((hook) => {
919
- hook.dispose();
920
- }, (err) => {});
1000
+ this.promise.then((hook) => hook.dispose(), () => {});
921
1001
  }
922
1002
  onBroken(callback) {
923
1003
  if (this.resolution) this.resolution.onBroken(callback);
@@ -935,6 +1015,71 @@ const DEFAULT_LIMITS = {
935
1015
  maxDepth: 256,
936
1016
  maxMessageSize: 32 * 1024 * 1024
937
1017
  };
1018
+ const NATIVE_LITTLE_ENDIAN = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
1019
+ const BYTE_CONTAINER_TYPE_NAMES = [
1020
+ "ArrayBuffer",
1021
+ "DataView",
1022
+ "Int8Array",
1023
+ "Uint8Array",
1024
+ "Uint8ClampedArray",
1025
+ "Int16Array",
1026
+ "Uint16Array",
1027
+ "Int32Array",
1028
+ "Uint32Array",
1029
+ "BigInt64Array",
1030
+ "BigUint64Array",
1031
+ "Float32Array",
1032
+ "Float64Array"
1033
+ ];
1034
+ function isValidByteContainerName(value) {
1035
+ return BYTE_CONTAINER_TYPE_NAMES.includes(value);
1036
+ }
1037
+ const TYPED_ARRAY_ELEMENT_SIZE = {
1038
+ ArrayBuffer: void 0,
1039
+ DataView: void 0,
1040
+ Int8Array: void 0,
1041
+ Uint8Array: void 0,
1042
+ Uint8ClampedArray: void 0,
1043
+ Int16Array: 2,
1044
+ Uint16Array: 2,
1045
+ Int32Array: 4,
1046
+ Uint32Array: 4,
1047
+ BigInt64Array: 8,
1048
+ BigUint64Array: 8,
1049
+ Float32Array: 4,
1050
+ Float64Array: 8
1051
+ };
1052
+ const BYTE_CONTAINER_PROTOTYPES = {
1053
+ ArrayBuffer: ArrayBuffer.prototype,
1054
+ DataView: DataView.prototype,
1055
+ Int8Array: Int8Array.prototype,
1056
+ Uint8ClampedArray: Uint8ClampedArray.prototype,
1057
+ Int16Array: Int16Array.prototype,
1058
+ Uint16Array: Uint16Array.prototype,
1059
+ Int32Array: Int32Array.prototype,
1060
+ Uint32Array: Uint32Array.prototype,
1061
+ BigInt64Array: BigInt64Array.prototype,
1062
+ BigUint64Array: BigUint64Array.prototype,
1063
+ Float32Array: Float32Array.prototype,
1064
+ Float64Array: Float64Array.prototype
1065
+ };
1066
+ const BYTE_CONTAINER_TYPE_BY_PROTOTYPE = /* @__PURE__ */ new Map();
1067
+ for (let type of Object.keys(BYTE_CONTAINER_PROTOTYPES)) BYTE_CONTAINER_TYPE_BY_PROTOTYPE.set(BYTE_CONTAINER_PROTOTYPES[type], type);
1068
+ function swapByteOrder(bytes, elementSize) {
1069
+ if (elementSize !== 2 && elementSize !== 4 && elementSize !== 8) throw new RangeError(`Unsupported element size: ${elementSize}`);
1070
+ let view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
1071
+ for (let offset = 0; offset < bytes.byteLength; offset += elementSize) switch (elementSize) {
1072
+ case 2:
1073
+ view.setUint16(offset, view.getUint16(offset, false), true);
1074
+ break;
1075
+ case 4:
1076
+ view.setUint32(offset, view.getUint32(offset, false), true);
1077
+ break;
1078
+ case 8:
1079
+ view.setBigUint64(offset, view.getBigUint64(offset, false), true);
1080
+ break;
1081
+ }
1082
+ }
938
1083
  var NullExporter = class {
939
1084
  exportStub(stub) {
940
1085
  throw new Error("Cannot serialize RPC stubs without an RPC session.");
@@ -954,7 +1099,8 @@ async function streamToBlob(stream, type) {
954
1099
  let b = await new Response(stream).blob();
955
1100
  return b.type === type ? b : b.slice(0, b.size, type);
956
1101
  }
957
- const ERROR_TYPES = Object.assign(Object.create(null), {
1102
+ const ERROR_TYPES = {
1103
+ __proto__: null,
958
1104
  Error,
959
1105
  EvalError,
960
1106
  RangeError,
@@ -963,7 +1109,7 @@ const ERROR_TYPES = Object.assign(Object.create(null), {
963
1109
  TypeError,
964
1110
  URIError,
965
1111
  AggregateError
966
- });
1112
+ };
967
1113
  var Devaluator = class Devaluator {
968
1114
  exporter;
969
1115
  source;
@@ -1025,18 +1171,40 @@ var Devaluator = class Devaluator {
1025
1171
  return ["date", Number.isNaN(time) ? null : time];
1026
1172
  }
1027
1173
  case "bytes": {
1028
- let bytes = value;
1029
- if (this.encodingLevel === "structuredClonable" || this.encodingLevel === "jsonCompatibleWithBytes") return ["bytes", bytes];
1030
- if (bytes.toBase64) return ["bytes", bytes.toBase64({ omitPadding: true })];
1174
+ let alternateTypeName = BYTE_CONTAINER_TYPE_BY_PROTOTYPE.get(Object.getPrototypeOf(value));
1175
+ let bytes;
1176
+ if (alternateTypeName === "ArrayBuffer") bytes = new Uint8Array(value);
1177
+ else if (alternateTypeName === void 0) bytes = value;
1178
+ else {
1179
+ let view = value;
1180
+ bytes = new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
1181
+ let elementSize = TYPED_ARRAY_ELEMENT_SIZE[alternateTypeName];
1182
+ if (!NATIVE_LITTLE_ENDIAN && elementSize) {
1183
+ bytes = bytes.slice();
1184
+ swapByteOrder(bytes, elementSize);
1185
+ }
1186
+ }
1187
+ if (this.encodingLevel === "structuredClonable" || this.encodingLevel === "jsonCompatibleWithBytes") return alternateTypeName === void 0 ? ["bytes", bytes] : [
1188
+ "bytes",
1189
+ bytes,
1190
+ alternateTypeName
1191
+ ];
1031
1192
  let b64;
1032
- if (typeof Buffer !== "undefined") b64 = (bytes instanceof Buffer ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)).toString("base64");
1193
+ if (bytes.toBase64) b64 = bytes.toBase64({ omitPadding: true });
1194
+ else if (typeof Buffer !== "undefined") b64 = (bytes instanceof Buffer ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)).toString("base64");
1033
1195
  else {
1034
1196
  let binary = "";
1035
1197
  for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]);
1036
1198
  b64 = btoa(binary);
1037
1199
  }
1038
- return ["bytes", b64.replace(/=+$/, "")];
1200
+ b64 = b64.replace(/=+$/, "");
1201
+ return alternateTypeName === void 0 ? ["bytes", b64] : [
1202
+ "bytes",
1203
+ b64,
1204
+ alternateTypeName
1205
+ ];
1039
1206
  }
1207
+ case "url": return ["url", value.href];
1040
1208
  case "headers": return ["headers", [...value]];
1041
1209
  case "request": {
1042
1210
  let req = value;
@@ -1288,18 +1456,46 @@ var Evaluator = class Evaluator {
1288
1456
  if (value[1] === null) return /* @__PURE__ */ new Date(NaN);
1289
1457
  if (typeof value[1] == "number") return new Date(value[1]);
1290
1458
  break;
1291
- case "bytes":
1292
- if (value[1] instanceof Uint8Array) return value[1];
1293
- if (typeof value[1] == "string") if (typeof Buffer !== "undefined") return Buffer.from(value[1], "base64");
1294
- else if (Uint8Array.fromBase64) return Uint8Array.fromBase64(value[1]);
1459
+ case "bytes": {
1460
+ let bytes;
1461
+ if (value[1] instanceof Uint8Array) bytes = value[1];
1462
+ else if (typeof value[1] == "string") if (typeof Buffer !== "undefined") bytes = Buffer.from(value[1], "base64");
1463
+ else if (Uint8Array.fromBase64) bytes = Uint8Array.fromBase64(value[1]);
1295
1464
  else {
1296
1465
  let bs = atob(value[1]);
1297
1466
  let len = bs.length;
1298
- let bytes = new Uint8Array(len);
1467
+ bytes = new Uint8Array(len);
1299
1468
  for (let i = 0; i < len; i++) bytes[i] = bs.charCodeAt(i);
1300
- return bytes;
1301
1469
  }
1302
- break;
1470
+ else break;
1471
+ if (value.length === 2) return bytes;
1472
+ if (typeof value[2] !== "string") throw new TypeError(`Unknown bytes type marker type: ${typeof value[2]}`);
1473
+ if (!isValidByteContainerName(value[2])) {
1474
+ let marker = value[2].slice(0, 64);
1475
+ throw new TypeError(`Unknown bytes type marker: ${marker}`);
1476
+ }
1477
+ let marker = value[2];
1478
+ let elementSize = TYPED_ARRAY_ELEMENT_SIZE[marker];
1479
+ if (elementSize !== void 0 && bytes.byteLength % elementSize !== 0) throw new TypeError(`Invalid byte length ${bytes.byteLength} for ${marker}; expected a multiple of ${elementSize}`);
1480
+ let buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
1481
+ if (!NATIVE_LITTLE_ENDIAN && elementSize !== void 0) swapByteOrder(new Uint8Array(buffer), elementSize);
1482
+ switch (marker) {
1483
+ case "ArrayBuffer": return buffer;
1484
+ case "DataView": return new DataView(buffer);
1485
+ case "Int8Array": return new Int8Array(buffer);
1486
+ case "Uint8Array": return new Uint8Array(buffer);
1487
+ case "Uint8ClampedArray": return new Uint8ClampedArray(buffer);
1488
+ case "Int16Array": return new Int16Array(buffer);
1489
+ case "Uint16Array": return new Uint16Array(buffer);
1490
+ case "Int32Array": return new Int32Array(buffer);
1491
+ case "Uint32Array": return new Uint32Array(buffer);
1492
+ case "BigInt64Array": return new BigInt64Array(buffer);
1493
+ case "BigUint64Array": return new BigUint64Array(buffer);
1494
+ case "Float32Array": return new Float32Array(buffer);
1495
+ case "Float64Array": return new Float64Array(buffer);
1496
+ default:
1497
+ }
1498
+ }
1303
1499
  case "error":
1304
1500
  if (value.length >= 3 && typeof value[1] === "string" && typeof value[2] === "string") {
1305
1501
  let cls = ERROR_TYPES[value[1]] || Error;
@@ -1328,6 +1524,9 @@ var Evaluator = class Evaluator {
1328
1524
  case "inf": return Infinity;
1329
1525
  case "-inf": return -Infinity;
1330
1526
  case "nan": return NaN;
1527
+ case "url":
1528
+ if (value.length === 2 && typeof value[1] === "string") return new URL(value[1]);
1529
+ break;
1331
1530
  case "headers":
1332
1531
  if (value.length === 2 && value[1] instanceof Array) return new Headers(value[1]);
1333
1532
  break;
@@ -1549,10 +1748,6 @@ var ImportTableEntry = class {
1549
1748
  resolution;
1550
1749
  onBrokenRegistrations;
1551
1750
  resolve(resolution) {
1552
- if (this.resolution) {
1553
- resolution.dispose();
1554
- return;
1555
- }
1556
1751
  if (this.localRefcount == 0) {
1557
1752
  resolution.dispose();
1558
1753
  return;
@@ -1631,24 +1826,28 @@ var RpcImportHook = class RpcImportHook extends StubHook {
1631
1826
  if (this.entry) return this.entry;
1632
1827
  else throw new Error("This RpcImportHook was already disposed.");
1633
1828
  }
1829
+ getEntryTakingOwnership(disposeOwned) {
1830
+ try {
1831
+ return this.getEntry();
1832
+ } catch (err) {
1833
+ disposeOwned();
1834
+ throw err;
1835
+ }
1836
+ }
1634
1837
  call(path, args) {
1635
- let entry = this.getEntry();
1838
+ let entry = this.getEntryTakingOwnership(() => args.dispose());
1636
1839
  if (entry.resolution) return entry.resolution.call(path, args);
1637
1840
  else return entry.session.sendCall(entry.importId, path, args);
1638
1841
  }
1639
1842
  stream(path, args) {
1640
- let entry = this.getEntry();
1843
+ let entry = this.getEntryTakingOwnership(() => args.dispose());
1641
1844
  if (entry.resolution) return entry.resolution.stream(path, args);
1642
1845
  else return entry.session.sendStream(entry.importId, path, args);
1643
1846
  }
1644
1847
  map(path, captures, instructions) {
1645
- let entry;
1646
- try {
1647
- entry = this.getEntry();
1648
- } catch (err) {
1848
+ let entry = this.getEntryTakingOwnership(() => {
1649
1849
  for (let cap of captures) cap.dispose();
1650
- throw err;
1651
- }
1850
+ });
1652
1851
  if (entry.resolution) return entry.resolution.map(path, captures, instructions);
1653
1852
  else return entry.session.sendMap(entry.importId, path, captures, instructions);
1654
1853
  }
@@ -1909,14 +2108,23 @@ var RpcSessionImpl = class {
1909
2108
  }
1910
2109
  }
1911
2110
  sendCall(id, path, args) {
1912
- if (this.abortReason) throw this.abortReason;
2111
+ if (this.abortReason) {
2112
+ args?.dispose();
2113
+ throw this.abortReason;
2114
+ }
1913
2115
  let value = [
1914
2116
  "pipeline",
1915
2117
  id,
1916
2118
  path
1917
2119
  ];
1918
2120
  if (args) {
1919
- let devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
2121
+ let devalue;
2122
+ try {
2123
+ devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
2124
+ } catch (err) {
2125
+ args.dispose();
2126
+ throw err;
2127
+ }
1920
2128
  value.push(devalue[0]);
1921
2129
  }
1922
2130
  this.send(["push", value]);
@@ -1925,13 +2133,22 @@ var RpcSessionImpl = class {
1925
2133
  return new RpcImportHook(true, entry);
1926
2134
  }
1927
2135
  sendStream(id, path, args) {
1928
- if (this.abortReason) throw this.abortReason;
2136
+ if (this.abortReason) {
2137
+ args.dispose();
2138
+ throw this.abortReason;
2139
+ }
1929
2140
  let value = [
1930
2141
  "pipeline",
1931
2142
  id,
1932
2143
  path
1933
2144
  ];
1934
- let devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
2145
+ let devalue;
2146
+ try {
2147
+ devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
2148
+ } catch (err) {
2149
+ args.dispose();
2150
+ throw err;
2151
+ }
1935
2152
  value.push(devalue[0]);
1936
2153
  let msg = ["stream", value];
1937
2154
  let size = this.send(msg);
@@ -2152,6 +2369,8 @@ var RpcSession$1 = class {
2152
2369
 
2153
2370
  //#endregion
2154
2371
  //#region src/websocket.ts
2372
+ /** Close-frame reason max UTF-8 bytes: 125 payload - 2-byte status (RFC 6455 section 5.5). */
2373
+ const MAX_CLOSE_REASON_BYTES = 123;
2155
2374
  function newWebSocketRpcSession$1(webSocket, localMain, options) {
2156
2375
  if (typeof webSocket === "string") webSocket = new WebSocket(webSocket);
2157
2376
  return new RpcSession$1(new WebSocketTransport(webSocket), localMain, options).getRemoteMain();
@@ -2246,6 +2465,7 @@ var WebSocketTransport = class {
2246
2465
 
2247
2466
  //#endregion
2248
2467
  //#region src/batch.ts
2468
+ const yieldToMacrotask = typeof setImmediate === "function" ? () => new Promise((resolve) => setImmediate(resolve)) : () => new Promise((resolve) => setTimeout(resolve, 0));
2249
2469
  var BatchClientTransport = class {
2250
2470
  constructor(sendBatch) {
2251
2471
  this.#promise = this.#scheduleBatch(sendBatch);
@@ -2267,7 +2487,7 @@ var BatchClientTransport = class {
2267
2487
  this.#aborted = reason;
2268
2488
  }
2269
2489
  async #scheduleBatch(sendBatch) {
2270
- await new Promise((resolve) => setTimeout(resolve, 0));
2490
+ await yieldToMacrotask();
2271
2491
  if (this.#aborted !== void 0) throw this.#aborted;
2272
2492
  let batch = this.#batchToSend;
2273
2493
  this.#batchToSend = null;
@@ -2559,9 +2779,11 @@ var MapVariableHook = class extends StubHook {
2559
2779
  else throwMapperBuilderUseError();
2560
2780
  }
2561
2781
  call(path, args) {
2782
+ args.dispose();
2562
2783
  throwMapperBuilderUseError();
2563
2784
  }
2564
2785
  map(path, captures, instructions) {
2786
+ for (let cap of captures) cap.dispose();
2565
2787
  throwMapperBuilderUseError();
2566
2788
  }
2567
2789
  pull() {
@@ -2672,14 +2894,11 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
2672
2894
  let state = this.getState();
2673
2895
  if (path.length !== 1 || typeof path[0] !== "string") throw new Error("WritableStream stub only supports direct method calls");
2674
2896
  const method = path[0];
2675
- if (method !== "write" && method !== "close" && method !== "abort") {
2676
- args.dispose();
2677
- throw new Error(`Unknown WritableStream method: ${method}`);
2678
- }
2897
+ if (method !== "write" && method !== "close" && method !== "abort") throw new Error(`Unknown WritableStream method: ${method}`);
2679
2898
  if (method === "close" || method === "abort") state.closed = true;
2680
- let func = state.writer[method];
2681
- return new PromiseStubHook(args.deliverCall(func, state.writer).then((payload) => new PayloadStubHook(payload)));
2899
+ return new PromiseStubHook((method === "write" ? args.deliverStreamWrite(state.writer) : args.deliverCall(state.writer[method], state.writer)).then((payload) => new PayloadStubHook(payload)));
2682
2900
  } catch (err) {
2901
+ args.dispose();
2683
2902
  return new ErrorStubHook(err);
2684
2903
  }
2685
2904
  }