capnweb 0.11.1 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -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);
@@ -777,17 +804,22 @@ function followPath(value, parent, path, owner) {
777
804
  }
778
805
  var ValueStubHook = class extends StubHook {
779
806
  call(path, args) {
807
+ let followResult;
780
808
  try {
781
809
  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
- }));
810
+ followResult = followPath(value, void 0, path, owner);
788
811
  } catch (err) {
812
+ args.dispose();
789
813
  return new ErrorStubHook(err);
790
814
  }
815
+ if (followResult.hook) return followResult.hook.call(followResult.remainingPath, args);
816
+ if (typeof followResult.value != "function") {
817
+ args.dispose();
818
+ return new ErrorStubHook(/* @__PURE__ */ new TypeError(`'${path.join(".")}' is not a function.`));
819
+ }
820
+ return new PromiseStubHook(args.deliverCall(followResult.value, followResult.parent).then((payload) => {
821
+ return new PayloadStubHook(payload);
822
+ }));
791
823
  }
792
824
  map(path, captures, instructions) {
793
825
  try {
@@ -808,7 +840,10 @@ var ValueStubHook = class extends StubHook {
808
840
  get(path) {
809
841
  try {
810
842
  let { value, owner } = this.getValue();
811
- if (path.length === 0 && owner === null) throw new Error("Can't dup an RpcTarget stub as a promise.");
843
+ if (path.length === 0 && owner === null) {
844
+ if (value instanceof Object && "then" in value) return this.dup();
845
+ throw new Error("Can't dup an RpcTarget stub as a promise.");
846
+ }
812
847
  let followResult = followPath(value, void 0, path, owner);
813
848
  if (followResult.hook) return followResult.hook.get(followResult.remainingPath);
814
849
  return new PayloadStubHook(RpcPayload.deepCopyFrom(followResult.value, followResult.parent, followResult.owner));
@@ -911,7 +946,10 @@ var TargetStubHook = class TargetStubHook extends ValueStubHook {
911
946
  this.target = void 0;
912
947
  }
913
948
  }
914
- onBroken(callback) {}
949
+ onBroken(callback) {
950
+ let target = this.target;
951
+ if (target && "then" in target) Promise.resolve(target).then(() => {}, callback);
952
+ }
915
953
  };
916
954
  var PromiseStubHook = class PromiseStubHook extends StubHook {
917
955
  promise;
@@ -925,12 +963,16 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
925
963
  }
926
964
  call(path, args) {
927
965
  args.ensureDeepCopied();
928
- return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args)));
966
+ return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args), (err) => {
967
+ args.dispose();
968
+ throw err;
969
+ }));
929
970
  }
930
971
  stream(path, args) {
931
972
  args.ensureDeepCopied();
932
- return { promise: this.promise.then((hook) => {
933
- return hook.stream(path, args).promise;
973
+ return { promise: this.promise.then((hook) => hook.stream(path, args).promise, (err) => {
974
+ args.dispose();
975
+ throw err;
934
976
  }) };
935
977
  }
936
978
  map(path, captures, instructions) {
@@ -957,10 +999,7 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
957
999
  }, (err) => {});
958
1000
  }
959
1001
  dispose() {
960
- if (this.resolution) this.resolution.dispose();
961
- else this.promise.then((hook) => {
962
- hook.dispose();
963
- }, (err) => {});
1002
+ this.promise.then((hook) => hook.dispose(), () => {});
964
1003
  }
965
1004
  onBroken(callback) {
966
1005
  if (this.resolution) this.resolution.onBroken(callback);
@@ -1789,24 +1828,28 @@ var RpcImportHook = class RpcImportHook extends StubHook {
1789
1828
  if (this.entry) return this.entry;
1790
1829
  else throw new Error("This RpcImportHook was already disposed.");
1791
1830
  }
1831
+ getEntryTakingOwnership(disposeOwned) {
1832
+ try {
1833
+ return this.getEntry();
1834
+ } catch (err) {
1835
+ disposeOwned();
1836
+ throw err;
1837
+ }
1838
+ }
1792
1839
  call(path, args) {
1793
- let entry = this.getEntry();
1840
+ let entry = this.getEntryTakingOwnership(() => args.dispose());
1794
1841
  if (entry.resolution) return entry.resolution.call(path, args);
1795
1842
  else return entry.session.sendCall(entry.importId, path, args);
1796
1843
  }
1797
1844
  stream(path, args) {
1798
- let entry = this.getEntry();
1845
+ let entry = this.getEntryTakingOwnership(() => args.dispose());
1799
1846
  if (entry.resolution) return entry.resolution.stream(path, args);
1800
1847
  else return entry.session.sendStream(entry.importId, path, args);
1801
1848
  }
1802
1849
  map(path, captures, instructions) {
1803
- let entry;
1804
- try {
1805
- entry = this.getEntry();
1806
- } catch (err) {
1850
+ let entry = this.getEntryTakingOwnership(() => {
1807
1851
  for (let cap of captures) cap.dispose();
1808
- throw err;
1809
- }
1852
+ });
1810
1853
  if (entry.resolution) return entry.resolution.map(path, captures, instructions);
1811
1854
  else return entry.session.sendMap(entry.importId, path, captures, instructions);
1812
1855
  }
@@ -2067,14 +2110,23 @@ var RpcSessionImpl = class {
2067
2110
  }
2068
2111
  }
2069
2112
  sendCall(id, path, args) {
2070
- if (this.abortReason) throw this.abortReason;
2113
+ if (this.abortReason) {
2114
+ args?.dispose();
2115
+ throw this.abortReason;
2116
+ }
2071
2117
  let value = [
2072
2118
  "pipeline",
2073
2119
  id,
2074
2120
  path
2075
2121
  ];
2076
2122
  if (args) {
2077
- let devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
2123
+ let devalue;
2124
+ try {
2125
+ devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
2126
+ } catch (err) {
2127
+ args.dispose();
2128
+ throw err;
2129
+ }
2078
2130
  value.push(devalue[0]);
2079
2131
  }
2080
2132
  this.send(["push", value]);
@@ -2083,13 +2135,22 @@ var RpcSessionImpl = class {
2083
2135
  return new RpcImportHook(true, entry);
2084
2136
  }
2085
2137
  sendStream(id, path, args) {
2086
- if (this.abortReason) throw this.abortReason;
2138
+ if (this.abortReason) {
2139
+ args.dispose();
2140
+ throw this.abortReason;
2141
+ }
2087
2142
  let value = [
2088
2143
  "pipeline",
2089
2144
  id,
2090
2145
  path
2091
2146
  ];
2092
- let devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
2147
+ let devalue;
2148
+ try {
2149
+ devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
2150
+ } catch (err) {
2151
+ args.dispose();
2152
+ throw err;
2153
+ }
2093
2154
  value.push(devalue[0]);
2094
2155
  let msg = ["stream", value];
2095
2156
  let size = this.send(msg);
@@ -2720,9 +2781,11 @@ var MapVariableHook = class extends StubHook {
2720
2781
  else throwMapperBuilderUseError();
2721
2782
  }
2722
2783
  call(path, args) {
2784
+ args.dispose();
2723
2785
  throwMapperBuilderUseError();
2724
2786
  }
2725
2787
  map(path, captures, instructions) {
2788
+ for (let cap of captures) cap.dispose();
2726
2789
  throwMapperBuilderUseError();
2727
2790
  }
2728
2791
  pull() {
@@ -2833,13 +2896,11 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
2833
2896
  let state = this.getState();
2834
2897
  if (path.length !== 1 || typeof path[0] !== "string") throw new Error("WritableStream stub only supports direct method calls");
2835
2898
  const method = path[0];
2836
- if (method !== "write" && method !== "close" && method !== "abort") {
2837
- args.dispose();
2838
- throw new Error(`Unknown WritableStream method: ${method}`);
2839
- }
2899
+ if (method !== "write" && method !== "close" && method !== "abort") throw new Error(`Unknown WritableStream method: ${method}`);
2840
2900
  if (method === "close" || method === "abort") state.closed = true;
2841
2901
  return new PromiseStubHook((method === "write" ? args.deliverStreamWrite(state.writer) : args.deliverCall(state.writer[method], state.writer)).then((payload) => new PayloadStubHook(payload)));
2842
2902
  } catch (err) {
2903
+ args.dispose();
2843
2904
  return new ErrorStubHook(err);
2844
2905
  }
2845
2906
  }