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.
@@ -76,9 +76,22 @@ function typeForRpc(value) {
76
76
  case Array.prototype: return "array";
77
77
  case Date.prototype: return "date";
78
78
  case Uint8Array.prototype:
79
- case BUFFER_PROTOTYPE: return "bytes";
79
+ case BUFFER_PROTOTYPE:
80
+ case ArrayBuffer.prototype:
81
+ case DataView.prototype:
82
+ case Int8Array.prototype:
83
+ case Uint8ClampedArray.prototype:
84
+ case Int16Array.prototype:
85
+ case Uint16Array.prototype:
86
+ case Int32Array.prototype:
87
+ case Uint32Array.prototype:
88
+ case BigInt64Array.prototype:
89
+ case BigUint64Array.prototype:
90
+ case Float32Array.prototype:
91
+ case Float64Array.prototype: return "bytes";
80
92
  case WritableStream.prototype: return "writable";
81
93
  case ReadableStream.prototype: return "readable";
94
+ case URL.prototype: return "url";
82
95
  case Headers.prototype: return "headers";
83
96
  case Request.prototype: return "request";
84
97
  case Response.prototype: return "response";
@@ -112,7 +125,14 @@ let streamImpl = {
112
125
  };
113
126
  var StubHook = class {
114
127
  stream(path, args) {
115
- let pulled = this.call(path, args).pull();
128
+ let hook = this.call(path, args);
129
+ let pulled;
130
+ try {
131
+ pulled = hook.pull();
132
+ } catch (err) {
133
+ hook.dispose();
134
+ throw err;
135
+ }
116
136
  let promise;
117
137
  if (pulled instanceof Promise) promise = pulled.then((p) => {
118
138
  p.dispose();
@@ -131,9 +151,11 @@ var ErrorStubHook = class extends StubHook {
131
151
  this.error = error;
132
152
  }
133
153
  call(path, args) {
154
+ args.dispose();
134
155
  return this;
135
156
  }
136
157
  map(path, captures, instructions) {
158
+ for (let cap of captures) cap.dispose();
137
159
  return this;
138
160
  }
139
161
  get(path) {
@@ -257,7 +279,25 @@ var RpcStub$1 = class RpcStub$1 extends RpcTarget$1 {
257
279
  };
258
280
  var RpcPromise$1 = class extends RpcStub$1 {
259
281
  constructor(hook, pathIfPromise) {
260
- super(hook, pathIfPromise);
282
+ if (hook instanceof StubHook) super(hook, pathIfPromise ?? []);
283
+ else {
284
+ if (pathIfPromise !== void 0) throw new TypeError("RpcPromise constructor expected one argument, received two.");
285
+ let kind = typeForRpc(hook);
286
+ if (kind === "rpc-promise") {
287
+ let raw = unwrapStubAndPath(hook);
288
+ if (raw.pathIfPromise.length > 0) super(raw.hook, raw.pathIfPromise);
289
+ else {
290
+ let adopted = raw.hook;
291
+ raw.hook = DISPOSED_HOOK;
292
+ super(adopted, []);
293
+ }
294
+ } else if (kind === "rpc-thenable") super(TargetStubHook.create(hook, void 0), []);
295
+ else {
296
+ let promiseHook = new PromiseStubHook(Promise.resolve(hook).then((value) => new PayloadStubHook(RpcPayload.fromAppReturn(value))));
297
+ promiseHook.ignoreUnhandledRejections();
298
+ super(promiseHook, []);
299
+ }
300
+ }
261
301
  }
262
302
  then(onfulfilled, onrejected) {
263
303
  return pullPromise(this).then(...arguments);
@@ -413,6 +453,7 @@ var RpcPayload = class RpcPayload {
413
453
  case "date":
414
454
  case "bytes":
415
455
  case "blob":
456
+ case "url":
416
457
  case "error":
417
458
  case "undefined": return value;
418
459
  case "array": {
@@ -546,6 +587,30 @@ var RpcPayload = class RpcPayload {
546
587
  this.dispose();
547
588
  }
548
589
  }
590
+ async deliverStreamWrite(writer) {
591
+ try {
592
+ let promises = [];
593
+ this.deliverTo(this, "value", promises);
594
+ if (promises.length > 0) await Promise.all(promises);
595
+ let chunk = this.value[0];
596
+ if ((this.hooks.length > 0 || this.promises.length > 0) && chunk instanceof Object) {
597
+ if (!(Symbol.dispose in chunk)) Object.defineProperty(chunk, Symbol.dispose, {
598
+ value: () => this.dispose(),
599
+ writable: true,
600
+ enumerable: false,
601
+ configurable: true
602
+ });
603
+ await writer.write(chunk);
604
+ return RpcPayload.fromAppReturn(void 0);
605
+ }
606
+ await writer.write(chunk);
607
+ this.dispose();
608
+ return RpcPayload.fromAppReturn(void 0);
609
+ } catch (err) {
610
+ this.dispose();
611
+ throw err;
612
+ }
613
+ }
549
614
  async deliverResolve() {
550
615
  try {
551
616
  let promises = [];
@@ -586,6 +651,7 @@ var RpcPayload = class RpcPayload {
586
651
  case "bytes":
587
652
  case "blob":
588
653
  case "date":
654
+ case "url":
589
655
  case "error":
590
656
  case "undefined": return;
591
657
  case "array": {
@@ -668,6 +734,7 @@ var RpcPayload = class RpcPayload {
668
734
  case "rpc-target":
669
735
  case "writable":
670
736
  case "readable":
737
+ case "url":
671
738
  case "headers":
672
739
  case "request":
673
740
  case "response": return;
@@ -737,6 +804,7 @@ function followPath(value, parent, path, owner) {
737
804
  case "blob":
738
805
  case "date":
739
806
  case "error":
807
+ case "url":
740
808
  case "headers":
741
809
  case "request":
742
810
  case "response":
@@ -769,17 +837,22 @@ function followPath(value, parent, path, owner) {
769
837
  }
770
838
  var ValueStubHook = class extends StubHook {
771
839
  call(path, args) {
840
+ let followResult;
772
841
  try {
773
842
  let { value, owner } = this.getValue();
774
- let followResult = followPath(value, void 0, path, owner);
775
- if (followResult.hook) return followResult.hook.call(followResult.remainingPath, args);
776
- if (typeof followResult.value != "function") throw new TypeError(`'${path.join(".")}' is not a function.`);
777
- return new PromiseStubHook(args.deliverCall(followResult.value, followResult.parent).then((payload) => {
778
- return new PayloadStubHook(payload);
779
- }));
843
+ followResult = followPath(value, void 0, path, owner);
780
844
  } catch (err) {
845
+ args.dispose();
781
846
  return new ErrorStubHook(err);
782
847
  }
848
+ if (followResult.hook) return followResult.hook.call(followResult.remainingPath, args);
849
+ if (typeof followResult.value != "function") {
850
+ args.dispose();
851
+ return new ErrorStubHook(/* @__PURE__ */ new TypeError(`'${path.join(".")}' is not a function.`));
852
+ }
853
+ return new PromiseStubHook(args.deliverCall(followResult.value, followResult.parent).then((payload) => {
854
+ return new PayloadStubHook(payload);
855
+ }));
783
856
  }
784
857
  map(path, captures, instructions) {
785
858
  try {
@@ -800,7 +873,10 @@ var ValueStubHook = class extends StubHook {
800
873
  get(path) {
801
874
  try {
802
875
  let { value, owner } = this.getValue();
803
- if (path.length === 0 && owner === null) throw new Error("Can't dup an RpcTarget stub as a promise.");
876
+ if (path.length === 0 && owner === null) {
877
+ if (value instanceof Object && "then" in value) return this.dup();
878
+ throw new Error("Can't dup an RpcTarget stub as a promise.");
879
+ }
804
880
  let followResult = followPath(value, void 0, path, owner);
805
881
  if (followResult.hook) return followResult.hook.get(followResult.remainingPath);
806
882
  return new PayloadStubHook(RpcPayload.deepCopyFrom(followResult.value, followResult.parent, followResult.owner));
@@ -903,7 +979,10 @@ var TargetStubHook = class TargetStubHook extends ValueStubHook {
903
979
  this.target = void 0;
904
980
  }
905
981
  }
906
- onBroken(callback) {}
982
+ onBroken(callback) {
983
+ let target = this.target;
984
+ if (target && "then" in target) Promise.resolve(target).then(() => {}, callback);
985
+ }
907
986
  };
908
987
  var PromiseStubHook = class PromiseStubHook extends StubHook {
909
988
  promise;
@@ -917,12 +996,16 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
917
996
  }
918
997
  call(path, args) {
919
998
  args.ensureDeepCopied();
920
- return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args)));
999
+ return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args), (err) => {
1000
+ args.dispose();
1001
+ throw err;
1002
+ }));
921
1003
  }
922
1004
  stream(path, args) {
923
1005
  args.ensureDeepCopied();
924
- return { promise: this.promise.then((hook) => {
925
- return hook.stream(path, args).promise;
1006
+ return { promise: this.promise.then((hook) => hook.stream(path, args).promise, (err) => {
1007
+ args.dispose();
1008
+ throw err;
926
1009
  }) };
927
1010
  }
928
1011
  map(path, captures, instructions) {
@@ -949,10 +1032,7 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
949
1032
  }, (err) => {});
950
1033
  }
951
1034
  dispose() {
952
- if (this.resolution) this.resolution.dispose();
953
- else this.promise.then((hook) => {
954
- hook.dispose();
955
- }, (err) => {});
1035
+ this.promise.then((hook) => hook.dispose(), () => {});
956
1036
  }
957
1037
  onBroken(callback) {
958
1038
  if (this.resolution) this.resolution.onBroken(callback);
@@ -970,6 +1050,71 @@ const DEFAULT_LIMITS = {
970
1050
  maxDepth: 256,
971
1051
  maxMessageSize: 32 * 1024 * 1024
972
1052
  };
1053
+ const NATIVE_LITTLE_ENDIAN = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
1054
+ const BYTE_CONTAINER_TYPE_NAMES = [
1055
+ "ArrayBuffer",
1056
+ "DataView",
1057
+ "Int8Array",
1058
+ "Uint8Array",
1059
+ "Uint8ClampedArray",
1060
+ "Int16Array",
1061
+ "Uint16Array",
1062
+ "Int32Array",
1063
+ "Uint32Array",
1064
+ "BigInt64Array",
1065
+ "BigUint64Array",
1066
+ "Float32Array",
1067
+ "Float64Array"
1068
+ ];
1069
+ function isValidByteContainerName(value) {
1070
+ return BYTE_CONTAINER_TYPE_NAMES.includes(value);
1071
+ }
1072
+ const TYPED_ARRAY_ELEMENT_SIZE = {
1073
+ ArrayBuffer: void 0,
1074
+ DataView: void 0,
1075
+ Int8Array: void 0,
1076
+ Uint8Array: void 0,
1077
+ Uint8ClampedArray: void 0,
1078
+ Int16Array: 2,
1079
+ Uint16Array: 2,
1080
+ Int32Array: 4,
1081
+ Uint32Array: 4,
1082
+ BigInt64Array: 8,
1083
+ BigUint64Array: 8,
1084
+ Float32Array: 4,
1085
+ Float64Array: 8
1086
+ };
1087
+ const BYTE_CONTAINER_PROTOTYPES = {
1088
+ ArrayBuffer: ArrayBuffer.prototype,
1089
+ DataView: DataView.prototype,
1090
+ Int8Array: Int8Array.prototype,
1091
+ Uint8ClampedArray: Uint8ClampedArray.prototype,
1092
+ Int16Array: Int16Array.prototype,
1093
+ Uint16Array: Uint16Array.prototype,
1094
+ Int32Array: Int32Array.prototype,
1095
+ Uint32Array: Uint32Array.prototype,
1096
+ BigInt64Array: BigInt64Array.prototype,
1097
+ BigUint64Array: BigUint64Array.prototype,
1098
+ Float32Array: Float32Array.prototype,
1099
+ Float64Array: Float64Array.prototype
1100
+ };
1101
+ const BYTE_CONTAINER_TYPE_BY_PROTOTYPE = /* @__PURE__ */ new Map();
1102
+ for (let type of Object.keys(BYTE_CONTAINER_PROTOTYPES)) BYTE_CONTAINER_TYPE_BY_PROTOTYPE.set(BYTE_CONTAINER_PROTOTYPES[type], type);
1103
+ function swapByteOrder(bytes, elementSize) {
1104
+ if (elementSize !== 2 && elementSize !== 4 && elementSize !== 8) throw new RangeError(`Unsupported element size: ${elementSize}`);
1105
+ let view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
1106
+ for (let offset = 0; offset < bytes.byteLength; offset += elementSize) switch (elementSize) {
1107
+ case 2:
1108
+ view.setUint16(offset, view.getUint16(offset, false), true);
1109
+ break;
1110
+ case 4:
1111
+ view.setUint32(offset, view.getUint32(offset, false), true);
1112
+ break;
1113
+ case 8:
1114
+ view.setBigUint64(offset, view.getBigUint64(offset, false), true);
1115
+ break;
1116
+ }
1117
+ }
973
1118
  var NullExporter = class {
974
1119
  exportStub(stub) {
975
1120
  throw new Error("Cannot serialize RPC stubs without an RPC session.");
@@ -989,7 +1134,8 @@ async function streamToBlob(stream, type) {
989
1134
  let b = await new Response(stream).blob();
990
1135
  return b.type === type ? b : b.slice(0, b.size, type);
991
1136
  }
992
- const ERROR_TYPES = Object.assign(Object.create(null), {
1137
+ const ERROR_TYPES = {
1138
+ __proto__: null,
993
1139
  Error,
994
1140
  EvalError,
995
1141
  RangeError,
@@ -998,7 +1144,7 @@ const ERROR_TYPES = Object.assign(Object.create(null), {
998
1144
  TypeError,
999
1145
  URIError,
1000
1146
  AggregateError
1001
- });
1147
+ };
1002
1148
  var Devaluator = class Devaluator {
1003
1149
  exporter;
1004
1150
  source;
@@ -1060,18 +1206,40 @@ var Devaluator = class Devaluator {
1060
1206
  return ["date", Number.isNaN(time) ? null : time];
1061
1207
  }
1062
1208
  case "bytes": {
1063
- let bytes = value;
1064
- if (this.encodingLevel === "structuredClonable" || this.encodingLevel === "jsonCompatibleWithBytes") return ["bytes", bytes];
1065
- if (bytes.toBase64) return ["bytes", bytes.toBase64({ omitPadding: true })];
1209
+ let alternateTypeName = BYTE_CONTAINER_TYPE_BY_PROTOTYPE.get(Object.getPrototypeOf(value));
1210
+ let bytes;
1211
+ if (alternateTypeName === "ArrayBuffer") bytes = new Uint8Array(value);
1212
+ else if (alternateTypeName === void 0) bytes = value;
1213
+ else {
1214
+ let view = value;
1215
+ bytes = new Uint8Array(view.buffer, view.byteOffset, view.byteLength);
1216
+ let elementSize = TYPED_ARRAY_ELEMENT_SIZE[alternateTypeName];
1217
+ if (!NATIVE_LITTLE_ENDIAN && elementSize) {
1218
+ bytes = bytes.slice();
1219
+ swapByteOrder(bytes, elementSize);
1220
+ }
1221
+ }
1222
+ if (this.encodingLevel === "structuredClonable" || this.encodingLevel === "jsonCompatibleWithBytes") return alternateTypeName === void 0 ? ["bytes", bytes] : [
1223
+ "bytes",
1224
+ bytes,
1225
+ alternateTypeName
1226
+ ];
1066
1227
  let b64;
1067
- if (typeof Buffer !== "undefined") b64 = (bytes instanceof Buffer ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)).toString("base64");
1228
+ if (bytes.toBase64) b64 = bytes.toBase64({ omitPadding: true });
1229
+ else if (typeof Buffer !== "undefined") b64 = (bytes instanceof Buffer ? bytes : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)).toString("base64");
1068
1230
  else {
1069
1231
  let binary = "";
1070
1232
  for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]);
1071
1233
  b64 = btoa(binary);
1072
1234
  }
1073
- return ["bytes", b64.replace(/=+$/, "")];
1235
+ b64 = b64.replace(/=+$/, "");
1236
+ return alternateTypeName === void 0 ? ["bytes", b64] : [
1237
+ "bytes",
1238
+ b64,
1239
+ alternateTypeName
1240
+ ];
1074
1241
  }
1242
+ case "url": return ["url", value.href];
1075
1243
  case "headers": return ["headers", [...value]];
1076
1244
  case "request": {
1077
1245
  let req = value;
@@ -1323,18 +1491,46 @@ var Evaluator = class Evaluator {
1323
1491
  if (value[1] === null) return /* @__PURE__ */ new Date(NaN);
1324
1492
  if (typeof value[1] == "number") return new Date(value[1]);
1325
1493
  break;
1326
- case "bytes":
1327
- if (value[1] instanceof Uint8Array) return value[1];
1328
- if (typeof value[1] == "string") if (typeof Buffer !== "undefined") return Buffer.from(value[1], "base64");
1329
- else if (Uint8Array.fromBase64) return Uint8Array.fromBase64(value[1]);
1494
+ case "bytes": {
1495
+ let bytes;
1496
+ if (value[1] instanceof Uint8Array) bytes = value[1];
1497
+ else if (typeof value[1] == "string") if (typeof Buffer !== "undefined") bytes = Buffer.from(value[1], "base64");
1498
+ else if (Uint8Array.fromBase64) bytes = Uint8Array.fromBase64(value[1]);
1330
1499
  else {
1331
1500
  let bs = atob(value[1]);
1332
1501
  let len = bs.length;
1333
- let bytes = new Uint8Array(len);
1502
+ bytes = new Uint8Array(len);
1334
1503
  for (let i = 0; i < len; i++) bytes[i] = bs.charCodeAt(i);
1335
- return bytes;
1336
1504
  }
1337
- break;
1505
+ else break;
1506
+ if (value.length === 2) return bytes;
1507
+ if (typeof value[2] !== "string") throw new TypeError(`Unknown bytes type marker type: ${typeof value[2]}`);
1508
+ if (!isValidByteContainerName(value[2])) {
1509
+ let marker = value[2].slice(0, 64);
1510
+ throw new TypeError(`Unknown bytes type marker: ${marker}`);
1511
+ }
1512
+ let marker = value[2];
1513
+ let elementSize = TYPED_ARRAY_ELEMENT_SIZE[marker];
1514
+ if (elementSize !== void 0 && bytes.byteLength % elementSize !== 0) throw new TypeError(`Invalid byte length ${bytes.byteLength} for ${marker}; expected a multiple of ${elementSize}`);
1515
+ let buffer = bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
1516
+ if (!NATIVE_LITTLE_ENDIAN && elementSize !== void 0) swapByteOrder(new Uint8Array(buffer), elementSize);
1517
+ switch (marker) {
1518
+ case "ArrayBuffer": return buffer;
1519
+ case "DataView": return new DataView(buffer);
1520
+ case "Int8Array": return new Int8Array(buffer);
1521
+ case "Uint8Array": return new Uint8Array(buffer);
1522
+ case "Uint8ClampedArray": return new Uint8ClampedArray(buffer);
1523
+ case "Int16Array": return new Int16Array(buffer);
1524
+ case "Uint16Array": return new Uint16Array(buffer);
1525
+ case "Int32Array": return new Int32Array(buffer);
1526
+ case "Uint32Array": return new Uint32Array(buffer);
1527
+ case "BigInt64Array": return new BigInt64Array(buffer);
1528
+ case "BigUint64Array": return new BigUint64Array(buffer);
1529
+ case "Float32Array": return new Float32Array(buffer);
1530
+ case "Float64Array": return new Float64Array(buffer);
1531
+ default:
1532
+ }
1533
+ }
1338
1534
  case "error":
1339
1535
  if (value.length >= 3 && typeof value[1] === "string" && typeof value[2] === "string") {
1340
1536
  let cls = ERROR_TYPES[value[1]] || Error;
@@ -1363,6 +1559,9 @@ var Evaluator = class Evaluator {
1363
1559
  case "inf": return Infinity;
1364
1560
  case "-inf": return -Infinity;
1365
1561
  case "nan": return NaN;
1562
+ case "url":
1563
+ if (value.length === 2 && typeof value[1] === "string") return new URL(value[1]);
1564
+ break;
1366
1565
  case "headers":
1367
1566
  if (value.length === 2 && value[1] instanceof Array) return new Headers(value[1]);
1368
1567
  break;
@@ -1584,10 +1783,6 @@ var ImportTableEntry = class {
1584
1783
  resolution;
1585
1784
  onBrokenRegistrations;
1586
1785
  resolve(resolution) {
1587
- if (this.resolution) {
1588
- resolution.dispose();
1589
- return;
1590
- }
1591
1786
  if (this.localRefcount == 0) {
1592
1787
  resolution.dispose();
1593
1788
  return;
@@ -1666,24 +1861,28 @@ var RpcImportHook = class RpcImportHook extends StubHook {
1666
1861
  if (this.entry) return this.entry;
1667
1862
  else throw new Error("This RpcImportHook was already disposed.");
1668
1863
  }
1864
+ getEntryTakingOwnership(disposeOwned) {
1865
+ try {
1866
+ return this.getEntry();
1867
+ } catch (err) {
1868
+ disposeOwned();
1869
+ throw err;
1870
+ }
1871
+ }
1669
1872
  call(path, args) {
1670
- let entry = this.getEntry();
1873
+ let entry = this.getEntryTakingOwnership(() => args.dispose());
1671
1874
  if (entry.resolution) return entry.resolution.call(path, args);
1672
1875
  else return entry.session.sendCall(entry.importId, path, args);
1673
1876
  }
1674
1877
  stream(path, args) {
1675
- let entry = this.getEntry();
1878
+ let entry = this.getEntryTakingOwnership(() => args.dispose());
1676
1879
  if (entry.resolution) return entry.resolution.stream(path, args);
1677
1880
  else return entry.session.sendStream(entry.importId, path, args);
1678
1881
  }
1679
1882
  map(path, captures, instructions) {
1680
- let entry;
1681
- try {
1682
- entry = this.getEntry();
1683
- } catch (err) {
1883
+ let entry = this.getEntryTakingOwnership(() => {
1684
1884
  for (let cap of captures) cap.dispose();
1685
- throw err;
1686
- }
1885
+ });
1687
1886
  if (entry.resolution) return entry.resolution.map(path, captures, instructions);
1688
1887
  else return entry.session.sendMap(entry.importId, path, captures, instructions);
1689
1888
  }
@@ -1944,14 +2143,23 @@ var RpcSessionImpl = class {
1944
2143
  }
1945
2144
  }
1946
2145
  sendCall(id, path, args) {
1947
- if (this.abortReason) throw this.abortReason;
2146
+ if (this.abortReason) {
2147
+ args?.dispose();
2148
+ throw this.abortReason;
2149
+ }
1948
2150
  let value = [
1949
2151
  "pipeline",
1950
2152
  id,
1951
2153
  path
1952
2154
  ];
1953
2155
  if (args) {
1954
- let devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
2156
+ let devalue;
2157
+ try {
2158
+ devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
2159
+ } catch (err) {
2160
+ args.dispose();
2161
+ throw err;
2162
+ }
1955
2163
  value.push(devalue[0]);
1956
2164
  }
1957
2165
  this.send(["push", value]);
@@ -1960,13 +2168,22 @@ var RpcSessionImpl = class {
1960
2168
  return new RpcImportHook(true, entry);
1961
2169
  }
1962
2170
  sendStream(id, path, args) {
1963
- if (this.abortReason) throw this.abortReason;
2171
+ if (this.abortReason) {
2172
+ args.dispose();
2173
+ throw this.abortReason;
2174
+ }
1964
2175
  let value = [
1965
2176
  "pipeline",
1966
2177
  id,
1967
2178
  path
1968
2179
  ];
1969
- let devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
2180
+ let devalue;
2181
+ try {
2182
+ devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
2183
+ } catch (err) {
2184
+ args.dispose();
2185
+ throw err;
2186
+ }
1970
2187
  value.push(devalue[0]);
1971
2188
  let msg = ["stream", value];
1972
2189
  let size = this.send(msg);
@@ -2187,6 +2404,8 @@ var RpcSession$1 = class {
2187
2404
 
2188
2405
  //#endregion
2189
2406
  //#region src/websocket.ts
2407
+ /** Close-frame reason max UTF-8 bytes: 125 payload - 2-byte status (RFC 6455 section 5.5). */
2408
+ const MAX_CLOSE_REASON_BYTES = 123;
2190
2409
  function newWebSocketRpcSession$1(webSocket, localMain, options) {
2191
2410
  if (typeof webSocket === "string") webSocket = new WebSocket(webSocket);
2192
2411
  return new RpcSession$1(new WebSocketTransport(webSocket), localMain, options).getRemoteMain();
@@ -2281,6 +2500,7 @@ var WebSocketTransport = class {
2281
2500
 
2282
2501
  //#endregion
2283
2502
  //#region src/batch.ts
2503
+ const yieldToMacrotask = typeof setImmediate === "function" ? () => new Promise((resolve) => setImmediate(resolve)) : () => new Promise((resolve) => setTimeout(resolve, 0));
2284
2504
  var BatchClientTransport = class {
2285
2505
  constructor(sendBatch) {
2286
2506
  this.#promise = this.#scheduleBatch(sendBatch);
@@ -2302,7 +2522,7 @@ var BatchClientTransport = class {
2302
2522
  this.#aborted = reason;
2303
2523
  }
2304
2524
  async #scheduleBatch(sendBatch) {
2305
- await new Promise((resolve) => setTimeout(resolve, 0));
2525
+ await yieldToMacrotask();
2306
2526
  if (this.#aborted !== void 0) throw this.#aborted;
2307
2527
  let batch = this.#batchToSend;
2308
2528
  this.#batchToSend = null;
@@ -2594,9 +2814,11 @@ var MapVariableHook = class extends StubHook {
2594
2814
  else throwMapperBuilderUseError();
2595
2815
  }
2596
2816
  call(path, args) {
2817
+ args.dispose();
2597
2818
  throwMapperBuilderUseError();
2598
2819
  }
2599
2820
  map(path, captures, instructions) {
2821
+ for (let cap of captures) cap.dispose();
2600
2822
  throwMapperBuilderUseError();
2601
2823
  }
2602
2824
  pull() {
@@ -2707,14 +2929,11 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
2707
2929
  let state = this.getState();
2708
2930
  if (path.length !== 1 || typeof path[0] !== "string") throw new Error("WritableStream stub only supports direct method calls");
2709
2931
  const method = path[0];
2710
- if (method !== "write" && method !== "close" && method !== "abort") {
2711
- args.dispose();
2712
- throw new Error(`Unknown WritableStream method: ${method}`);
2713
- }
2932
+ if (method !== "write" && method !== "close" && method !== "abort") throw new Error(`Unknown WritableStream method: ${method}`);
2714
2933
  if (method === "close" || method === "abort") state.closed = true;
2715
- let func = state.writer[method];
2716
- return new PromiseStubHook(args.deliverCall(func, state.writer).then((payload) => new PayloadStubHook(payload)));
2934
+ return new PromiseStubHook((method === "write" ? args.deliverStreamWrite(state.writer) : args.deliverCall(state.writer[method], state.writer)).then((payload) => new PayloadStubHook(payload)));
2717
2935
  } catch (err) {
2936
+ args.dispose();
2718
2937
  return new ErrorStubHook(err);
2719
2938
  }
2720
2939
  }