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.
@@ -125,7 +125,14 @@ let streamImpl = {
125
125
  };
126
126
  var StubHook = class {
127
127
  stream(path, args) {
128
- 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
+ }
129
136
  let promise;
130
137
  if (pulled instanceof Promise) promise = pulled.then((p) => {
131
138
  p.dispose();
@@ -144,9 +151,11 @@ var ErrorStubHook = class extends StubHook {
144
151
  this.error = error;
145
152
  }
146
153
  call(path, args) {
154
+ args.dispose();
147
155
  return this;
148
156
  }
149
157
  map(path, captures, instructions) {
158
+ for (let cap of captures) cap.dispose();
150
159
  return this;
151
160
  }
152
161
  get(path) {
@@ -270,7 +279,25 @@ var RpcStub$1 = class RpcStub$1 extends RpcTarget$1 {
270
279
  };
271
280
  var RpcPromise$1 = class extends RpcStub$1 {
272
281
  constructor(hook, pathIfPromise) {
273
- 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
+ }
274
301
  }
275
302
  then(onfulfilled, onrejected) {
276
303
  return pullPromise(this).then(...arguments);
@@ -810,17 +837,22 @@ function followPath(value, parent, path, owner) {
810
837
  }
811
838
  var ValueStubHook = class extends StubHook {
812
839
  call(path, args) {
840
+ let followResult;
813
841
  try {
814
842
  let { value, owner } = this.getValue();
815
- let followResult = followPath(value, void 0, path, owner);
816
- if (followResult.hook) return followResult.hook.call(followResult.remainingPath, args);
817
- if (typeof followResult.value != "function") throw new TypeError(`'${path.join(".")}' is not a function.`);
818
- return new PromiseStubHook(args.deliverCall(followResult.value, followResult.parent).then((payload) => {
819
- return new PayloadStubHook(payload);
820
- }));
843
+ followResult = followPath(value, void 0, path, owner);
821
844
  } catch (err) {
845
+ args.dispose();
822
846
  return new ErrorStubHook(err);
823
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
+ }));
824
856
  }
825
857
  map(path, captures, instructions) {
826
858
  try {
@@ -841,7 +873,10 @@ var ValueStubHook = class extends StubHook {
841
873
  get(path) {
842
874
  try {
843
875
  let { value, owner } = this.getValue();
844
- 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
+ }
845
880
  let followResult = followPath(value, void 0, path, owner);
846
881
  if (followResult.hook) return followResult.hook.get(followResult.remainingPath);
847
882
  return new PayloadStubHook(RpcPayload.deepCopyFrom(followResult.value, followResult.parent, followResult.owner));
@@ -944,7 +979,10 @@ var TargetStubHook = class TargetStubHook extends ValueStubHook {
944
979
  this.target = void 0;
945
980
  }
946
981
  }
947
- onBroken(callback) {}
982
+ onBroken(callback) {
983
+ let target = this.target;
984
+ if (target && "then" in target) Promise.resolve(target).then(() => {}, callback);
985
+ }
948
986
  };
949
987
  var PromiseStubHook = class PromiseStubHook extends StubHook {
950
988
  promise;
@@ -958,12 +996,16 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
958
996
  }
959
997
  call(path, args) {
960
998
  args.ensureDeepCopied();
961
- 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
+ }));
962
1003
  }
963
1004
  stream(path, args) {
964
1005
  args.ensureDeepCopied();
965
- return { promise: this.promise.then((hook) => {
966
- 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;
967
1009
  }) };
968
1010
  }
969
1011
  map(path, captures, instructions) {
@@ -990,10 +1032,7 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
990
1032
  }, (err) => {});
991
1033
  }
992
1034
  dispose() {
993
- if (this.resolution) this.resolution.dispose();
994
- else this.promise.then((hook) => {
995
- hook.dispose();
996
- }, (err) => {});
1035
+ this.promise.then((hook) => hook.dispose(), () => {});
997
1036
  }
998
1037
  onBroken(callback) {
999
1038
  if (this.resolution) this.resolution.onBroken(callback);
@@ -1822,24 +1861,28 @@ var RpcImportHook = class RpcImportHook extends StubHook {
1822
1861
  if (this.entry) return this.entry;
1823
1862
  else throw new Error("This RpcImportHook was already disposed.");
1824
1863
  }
1864
+ getEntryTakingOwnership(disposeOwned) {
1865
+ try {
1866
+ return this.getEntry();
1867
+ } catch (err) {
1868
+ disposeOwned();
1869
+ throw err;
1870
+ }
1871
+ }
1825
1872
  call(path, args) {
1826
- let entry = this.getEntry();
1873
+ let entry = this.getEntryTakingOwnership(() => args.dispose());
1827
1874
  if (entry.resolution) return entry.resolution.call(path, args);
1828
1875
  else return entry.session.sendCall(entry.importId, path, args);
1829
1876
  }
1830
1877
  stream(path, args) {
1831
- let entry = this.getEntry();
1878
+ let entry = this.getEntryTakingOwnership(() => args.dispose());
1832
1879
  if (entry.resolution) return entry.resolution.stream(path, args);
1833
1880
  else return entry.session.sendStream(entry.importId, path, args);
1834
1881
  }
1835
1882
  map(path, captures, instructions) {
1836
- let entry;
1837
- try {
1838
- entry = this.getEntry();
1839
- } catch (err) {
1883
+ let entry = this.getEntryTakingOwnership(() => {
1840
1884
  for (let cap of captures) cap.dispose();
1841
- throw err;
1842
- }
1885
+ });
1843
1886
  if (entry.resolution) return entry.resolution.map(path, captures, instructions);
1844
1887
  else return entry.session.sendMap(entry.importId, path, captures, instructions);
1845
1888
  }
@@ -2100,14 +2143,23 @@ var RpcSessionImpl = class {
2100
2143
  }
2101
2144
  }
2102
2145
  sendCall(id, path, args) {
2103
- if (this.abortReason) throw this.abortReason;
2146
+ if (this.abortReason) {
2147
+ args?.dispose();
2148
+ throw this.abortReason;
2149
+ }
2104
2150
  let value = [
2105
2151
  "pipeline",
2106
2152
  id,
2107
2153
  path
2108
2154
  ];
2109
2155
  if (args) {
2110
- 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
+ }
2111
2163
  value.push(devalue[0]);
2112
2164
  }
2113
2165
  this.send(["push", value]);
@@ -2116,13 +2168,22 @@ var RpcSessionImpl = class {
2116
2168
  return new RpcImportHook(true, entry);
2117
2169
  }
2118
2170
  sendStream(id, path, args) {
2119
- if (this.abortReason) throw this.abortReason;
2171
+ if (this.abortReason) {
2172
+ args.dispose();
2173
+ throw this.abortReason;
2174
+ }
2120
2175
  let value = [
2121
2176
  "pipeline",
2122
2177
  id,
2123
2178
  path
2124
2179
  ];
2125
- 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
+ }
2126
2187
  value.push(devalue[0]);
2127
2188
  let msg = ["stream", value];
2128
2189
  let size = this.send(msg);
@@ -2753,9 +2814,11 @@ var MapVariableHook = class extends StubHook {
2753
2814
  else throwMapperBuilderUseError();
2754
2815
  }
2755
2816
  call(path, args) {
2817
+ args.dispose();
2756
2818
  throwMapperBuilderUseError();
2757
2819
  }
2758
2820
  map(path, captures, instructions) {
2821
+ for (let cap of captures) cap.dispose();
2759
2822
  throwMapperBuilderUseError();
2760
2823
  }
2761
2824
  pull() {
@@ -2866,13 +2929,11 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
2866
2929
  let state = this.getState();
2867
2930
  if (path.length !== 1 || typeof path[0] !== "string") throw new Error("WritableStream stub only supports direct method calls");
2868
2931
  const method = path[0];
2869
- if (method !== "write" && method !== "close" && method !== "abort") {
2870
- args.dispose();
2871
- throw new Error(`Unknown WritableStream method: ${method}`);
2872
- }
2932
+ if (method !== "write" && method !== "close" && method !== "abort") throw new Error(`Unknown WritableStream method: ${method}`);
2873
2933
  if (method === "close" || method === "abort") state.closed = true;
2874
2934
  return new PromiseStubHook((method === "write" ? args.deliverStreamWrite(state.writer) : args.deliverCall(state.writer[method], state.writer)).then((payload) => new PayloadStubHook(payload)));
2875
2935
  } catch (err) {
2936
+ args.dispose();
2876
2937
  return new ErrorStubHook(err);
2877
2938
  }
2878
2939
  }