capnweb 0.0.0-2b292e4 → 0.0.0-2de5871

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.
@@ -41,7 +41,7 @@ type Stub<T extends RpcCompatible<T>> = T extends object ? Provider<T> & StubBas
41
41
  type TypedArray = Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array | Int8Array | Int16Array | Int32Array | BigUint64Array | BigInt64Array | Float32Array | Float64Array; // This represents all the types that can be sent as-is over an RPC boundary
42
42
  type BaseType = void | undefined | null | boolean | number | bigint | string | TypedArray | ArrayBuffer | DataView | Date | Error | RegExp | Blob | ReadableStream<any> // Chunk type can be any RPC-compatible type
43
43
  | WritableStream<any> // Chunk type can be any RPC-compatible type
44
- | Request | Response | Headers; // Recursively rewrite all `Stubable` types with `Stub`s, and resolve promises.
44
+ | URL | Request | Response | Headers; // Recursively rewrite all `Stubable` types with `Stub`s, and resolve promises.
45
45
  // prettier-ignore
46
46
  type Stubify<T> = T extends Stubable ? Stub<T> : T extends Promise<infer U> ? Stubify<U> : T extends StubBase<any> ? T : T extends Map<infer K, infer V> ? Map<Stubify<K>, Stubify<V>> : T extends Set<infer V> ? Set<Stubify<V>> : T extends [] ? [] : T extends [infer Head, ...infer Tail] ? [Stubify<Head>, ...Stubify<Tail>] : T extends readonly [] ? readonly [] : T extends readonly [infer Head, ...infer Tail] ? readonly [Stubify<Head>, ...Stubify<Tail>] : T extends Array<infer V> ? Array<Stubify<V>> : T extends ReadonlyArray<infer V> ? ReadonlyArray<Stubify<V>> : T extends BaseType ? T // When using "unknown" instead of "any", interfaces are not stubified.
47
47
  : T extends {
@@ -70,7 +70,13 @@ interface MapValuePlaceholder<T> {
70
70
  }
71
71
  type NonStubMembers<T> = Exclude<T, StubBase<any>>; // Utility type for adding `Disposable`s to `object` types only.
72
72
  // Note `unknown & T` is equivalent to `T`.
73
- type MaybeDisposable<T> = T extends object ? Disposable : unknown; // Type for method return or property on an RPC interface.
73
+ type MaybeDisposable<T> = T extends object ? Disposable : unknown; // The type behind the public `RpcPromise` (re-exported with its `RpcCompatible` constraint from
74
+ // index.ts). Defined here so that `Result`'s arms below resolve to the exact same alias
75
+ // instantiation: when both sides of an assignability check are the same alias applied to the
76
+ // same type arguments, the checker short-circuits on identity instead of recursing through the
77
+ // full structural comparison of these mutually-recursive types.
78
+ type RpcPromise$1<T> = T extends Stubable ? Promise<Stub<T>> & Provider<T> & StubBase<T> : Promise<Stubify<T> & MaybeDisposable<T>> & Provider<T> & StubBase<T>;
79
+ // Type for method return or property on an RPC interface.
74
80
  // - Stubable types are replaced by stubs.
75
81
  // - RpcCompatible types are passed by value, with stubable types replaced by stubs
76
82
  // and a top-level `Disposer`.
@@ -78,7 +84,7 @@ type MaybeDisposable<T> = T extends object ? Disposable : unknown; // Type for m
78
84
  // Technically, we use custom thenables here, but they quack like `Promise`s.
79
85
  // Intersecting with `(Maybe)Provider` allows pipelining.
80
86
  // prettier-ignore
81
- type Result<R> = IsAny<R> extends true ? UnknownResult : IsUnknown<R> extends true ? UnknownResult : R extends Stubable ? Promise<Stub<R>> & Provider<R> & StubBase<R> : R extends RpcCompatible<R> ? Promise<Stubify<R> & MaybeDisposable<R>> & Provider<R> & StubBase<R> : never;
87
+ type Result<R> = IsAny<R> extends true ? UnknownResult : IsUnknown<R> extends true ? UnknownResult : R extends Stubable ? RpcPromise$1<R> : R extends RpcCompatible<R> ? RpcPromise$1<R> : never;
82
88
  type IsAny<T> = 0 extends (1 & T) ? true : false;
83
89
  type UnknownResult = Promise<unknown> & Provider<unknown> & StubBase<unknown>; // Type for method or property on an RPC interface.
84
90
  // For methods, unwrap `Stub`s in parameters, and rewrite returns to be `Result`s.
@@ -322,7 +328,7 @@ declare const RpcStub: {
322
328
  * if you only intend to use the promise for pipelining and you never await it, then there's no
323
329
  * need to transmit the resolution!
324
330
  */
325
- type RpcPromise<T extends RpcCompatible<T>> = Stub<T> & Promise<Stubify<T>>;
331
+ type RpcPromise<T extends RpcCompatible<T>> = RpcPromise$1<T>;
326
332
  declare const RpcPromise: {};
327
333
  /**
328
334
  * Use to construct an `RpcSession` on top of a custom `RpcTransport`.
@@ -41,7 +41,7 @@ type Stub<T extends RpcCompatible<T>> = T extends object ? Provider<T> & StubBas
41
41
  type TypedArray = Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array | Int8Array | Int16Array | Int32Array | BigUint64Array | BigInt64Array | Float32Array | Float64Array; // This represents all the types that can be sent as-is over an RPC boundary
42
42
  type BaseType = void | undefined | null | boolean | number | bigint | string | TypedArray | ArrayBuffer | DataView | Date | Error | RegExp | Blob | ReadableStream<any> // Chunk type can be any RPC-compatible type
43
43
  | WritableStream<any> // Chunk type can be any RPC-compatible type
44
- | Request | Response | Headers; // Recursively rewrite all `Stubable` types with `Stub`s, and resolve promises.
44
+ | URL | Request | Response | Headers; // Recursively rewrite all `Stubable` types with `Stub`s, and resolve promises.
45
45
  // prettier-ignore
46
46
  type Stubify<T> = T extends Stubable ? Stub<T> : T extends Promise<infer U> ? Stubify<U> : T extends StubBase<any> ? T : T extends Map<infer K, infer V> ? Map<Stubify<K>, Stubify<V>> : T extends Set<infer V> ? Set<Stubify<V>> : T extends [] ? [] : T extends [infer Head, ...infer Tail] ? [Stubify<Head>, ...Stubify<Tail>] : T extends readonly [] ? readonly [] : T extends readonly [infer Head, ...infer Tail] ? readonly [Stubify<Head>, ...Stubify<Tail>] : T extends Array<infer V> ? Array<Stubify<V>> : T extends ReadonlyArray<infer V> ? ReadonlyArray<Stubify<V>> : T extends BaseType ? T // When using "unknown" instead of "any", interfaces are not stubified.
47
47
  : T extends {
@@ -70,7 +70,13 @@ interface MapValuePlaceholder<T> {
70
70
  }
71
71
  type NonStubMembers<T> = Exclude<T, StubBase<any>>; // Utility type for adding `Disposable`s to `object` types only.
72
72
  // Note `unknown & T` is equivalent to `T`.
73
- type MaybeDisposable<T> = T extends object ? Disposable : unknown; // Type for method return or property on an RPC interface.
73
+ type MaybeDisposable<T> = T extends object ? Disposable : unknown; // The type behind the public `RpcPromise` (re-exported with its `RpcCompatible` constraint from
74
+ // index.ts). Defined here so that `Result`'s arms below resolve to the exact same alias
75
+ // instantiation: when both sides of an assignability check are the same alias applied to the
76
+ // same type arguments, the checker short-circuits on identity instead of recursing through the
77
+ // full structural comparison of these mutually-recursive types.
78
+ type RpcPromise$1<T> = T extends Stubable ? Promise<Stub<T>> & Provider<T> & StubBase<T> : Promise<Stubify<T> & MaybeDisposable<T>> & Provider<T> & StubBase<T>;
79
+ // Type for method return or property on an RPC interface.
74
80
  // - Stubable types are replaced by stubs.
75
81
  // - RpcCompatible types are passed by value, with stubable types replaced by stubs
76
82
  // and a top-level `Disposer`.
@@ -78,7 +84,7 @@ type MaybeDisposable<T> = T extends object ? Disposable : unknown; // Type for m
78
84
  // Technically, we use custom thenables here, but they quack like `Promise`s.
79
85
  // Intersecting with `(Maybe)Provider` allows pipelining.
80
86
  // prettier-ignore
81
- type Result<R> = IsAny<R> extends true ? UnknownResult : IsUnknown<R> extends true ? UnknownResult : R extends Stubable ? Promise<Stub<R>> & Provider<R> & StubBase<R> : R extends RpcCompatible<R> ? Promise<Stubify<R> & MaybeDisposable<R>> & Provider<R> & StubBase<R> : never;
87
+ type Result<R> = IsAny<R> extends true ? UnknownResult : IsUnknown<R> extends true ? UnknownResult : R extends Stubable ? RpcPromise$1<R> : R extends RpcCompatible<R> ? RpcPromise$1<R> : never;
82
88
  type IsAny<T> = 0 extends (1 & T) ? true : false;
83
89
  type UnknownResult = Promise<unknown> & Provider<unknown> & StubBase<unknown>; // Type for method or property on an RPC interface.
84
90
  // For methods, unwrap `Stub`s in parameters, and rewrite returns to be `Result`s.
@@ -322,7 +328,7 @@ declare const RpcStub: {
322
328
  * if you only intend to use the promise for pipelining and you never await it, then there's no
323
329
  * need to transmit the resolution!
324
330
  */
325
- type RpcPromise<T extends RpcCompatible<T>> = Stub<T> & Promise<Stubify<T>>;
331
+ type RpcPromise<T extends RpcCompatible<T>> = RpcPromise$1<T>;
326
332
  declare const RpcPromise: {};
327
333
  /**
328
334
  * Use to construct an `RpcSession` on top of a custom `RpcTransport`.
@@ -62,6 +62,7 @@ function typeForRpc(value) {
62
62
  case Float64Array.prototype: return "bytes";
63
63
  case WritableStream.prototype: return "writable";
64
64
  case ReadableStream.prototype: return "readable";
65
+ case URL.prototype: return "url";
65
66
  case Headers.prototype: return "headers";
66
67
  case Request.prototype: return "request";
67
68
  case Response.prototype: return "response";
@@ -95,7 +96,14 @@ let streamImpl = {
95
96
  };
96
97
  var StubHook = class {
97
98
  stream(path, args) {
98
- 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
+ }
99
107
  let promise;
100
108
  if (pulled instanceof Promise) promise = pulled.then((p) => {
101
109
  p.dispose();
@@ -114,9 +122,11 @@ var ErrorStubHook = class extends StubHook {
114
122
  this.error = error;
115
123
  }
116
124
  call(path, args) {
125
+ args.dispose();
117
126
  return this;
118
127
  }
119
128
  map(path, captures, instructions) {
129
+ for (let cap of captures) cap.dispose();
120
130
  return this;
121
131
  }
122
132
  get(path) {
@@ -396,6 +406,7 @@ var RpcPayload = class RpcPayload {
396
406
  case "date":
397
407
  case "bytes":
398
408
  case "blob":
409
+ case "url":
399
410
  case "error":
400
411
  case "undefined": return value;
401
412
  case "array": {
@@ -593,6 +604,7 @@ var RpcPayload = class RpcPayload {
593
604
  case "bytes":
594
605
  case "blob":
595
606
  case "date":
607
+ case "url":
596
608
  case "error":
597
609
  case "undefined": return;
598
610
  case "array": {
@@ -675,6 +687,7 @@ var RpcPayload = class RpcPayload {
675
687
  case "rpc-target":
676
688
  case "writable":
677
689
  case "readable":
690
+ case "url":
678
691
  case "headers":
679
692
  case "request":
680
693
  case "response": return;
@@ -744,6 +757,7 @@ function followPath(value, parent, path, owner) {
744
757
  case "blob":
745
758
  case "date":
746
759
  case "error":
760
+ case "url":
747
761
  case "headers":
748
762
  case "request":
749
763
  case "response":
@@ -776,17 +790,22 @@ function followPath(value, parent, path, owner) {
776
790
  }
777
791
  var ValueStubHook = class extends StubHook {
778
792
  call(path, args) {
793
+ let followResult;
779
794
  try {
780
795
  let { value, owner } = this.getValue();
781
- let followResult = followPath(value, void 0, path, owner);
782
- if (followResult.hook) return followResult.hook.call(followResult.remainingPath, args);
783
- if (typeof followResult.value != "function") throw new TypeError(`'${path.join(".")}' is not a function.`);
784
- return new PromiseStubHook(args.deliverCall(followResult.value, followResult.parent).then((payload) => {
785
- return new PayloadStubHook(payload);
786
- }));
796
+ followResult = followPath(value, void 0, path, owner);
787
797
  } catch (err) {
798
+ args.dispose();
788
799
  return new ErrorStubHook(err);
789
800
  }
801
+ if (followResult.hook) return followResult.hook.call(followResult.remainingPath, args);
802
+ if (typeof followResult.value != "function") {
803
+ args.dispose();
804
+ return new ErrorStubHook(/* @__PURE__ */ new TypeError(`'${path.join(".")}' is not a function.`));
805
+ }
806
+ return new PromiseStubHook(args.deliverCall(followResult.value, followResult.parent).then((payload) => {
807
+ return new PayloadStubHook(payload);
808
+ }));
790
809
  }
791
810
  map(path, captures, instructions) {
792
811
  try {
@@ -924,12 +943,16 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
924
943
  }
925
944
  call(path, args) {
926
945
  args.ensureDeepCopied();
927
- return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args)));
946
+ return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args), (err) => {
947
+ args.dispose();
948
+ throw err;
949
+ }));
928
950
  }
929
951
  stream(path, args) {
930
952
  args.ensureDeepCopied();
931
- return { promise: this.promise.then((hook) => {
932
- return hook.stream(path, args).promise;
953
+ return { promise: this.promise.then((hook) => hook.stream(path, args).promise, (err) => {
954
+ args.dispose();
955
+ throw err;
933
956
  }) };
934
957
  }
935
958
  map(path, captures, instructions) {
@@ -956,10 +979,7 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
956
979
  }, (err) => {});
957
980
  }
958
981
  dispose() {
959
- if (this.resolution) this.resolution.dispose();
960
- else this.promise.then((hook) => {
961
- hook.dispose();
962
- }, (err) => {});
982
+ this.promise.then((hook) => hook.dispose(), () => {});
963
983
  }
964
984
  onBroken(callback) {
965
985
  if (this.resolution) this.resolution.onBroken(callback);
@@ -1166,6 +1186,7 @@ var Devaluator = class Devaluator {
1166
1186
  alternateTypeName
1167
1187
  ];
1168
1188
  }
1189
+ case "url": return ["url", value.href];
1169
1190
  case "headers": return ["headers", [...value]];
1170
1191
  case "request": {
1171
1192
  let req = value;
@@ -1485,6 +1506,9 @@ var Evaluator = class Evaluator {
1485
1506
  case "inf": return Infinity;
1486
1507
  case "-inf": return -Infinity;
1487
1508
  case "nan": return NaN;
1509
+ case "url":
1510
+ if (value.length === 2 && typeof value[1] === "string") return new URL(value[1]);
1511
+ break;
1488
1512
  case "headers":
1489
1513
  if (value.length === 2 && value[1] instanceof Array) return new Headers(value[1]);
1490
1514
  break;
@@ -1784,24 +1808,28 @@ var RpcImportHook = class RpcImportHook extends StubHook {
1784
1808
  if (this.entry) return this.entry;
1785
1809
  else throw new Error("This RpcImportHook was already disposed.");
1786
1810
  }
1811
+ getEntryTakingOwnership(disposeOwned) {
1812
+ try {
1813
+ return this.getEntry();
1814
+ } catch (err) {
1815
+ disposeOwned();
1816
+ throw err;
1817
+ }
1818
+ }
1787
1819
  call(path, args) {
1788
- let entry = this.getEntry();
1820
+ let entry = this.getEntryTakingOwnership(() => args.dispose());
1789
1821
  if (entry.resolution) return entry.resolution.call(path, args);
1790
1822
  else return entry.session.sendCall(entry.importId, path, args);
1791
1823
  }
1792
1824
  stream(path, args) {
1793
- let entry = this.getEntry();
1825
+ let entry = this.getEntryTakingOwnership(() => args.dispose());
1794
1826
  if (entry.resolution) return entry.resolution.stream(path, args);
1795
1827
  else return entry.session.sendStream(entry.importId, path, args);
1796
1828
  }
1797
1829
  map(path, captures, instructions) {
1798
- let entry;
1799
- try {
1800
- entry = this.getEntry();
1801
- } catch (err) {
1830
+ let entry = this.getEntryTakingOwnership(() => {
1802
1831
  for (let cap of captures) cap.dispose();
1803
- throw err;
1804
- }
1832
+ });
1805
1833
  if (entry.resolution) return entry.resolution.map(path, captures, instructions);
1806
1834
  else return entry.session.sendMap(entry.importId, path, captures, instructions);
1807
1835
  }
@@ -2062,14 +2090,23 @@ var RpcSessionImpl = class {
2062
2090
  }
2063
2091
  }
2064
2092
  sendCall(id, path, args) {
2065
- if (this.abortReason) throw this.abortReason;
2093
+ if (this.abortReason) {
2094
+ args?.dispose();
2095
+ throw this.abortReason;
2096
+ }
2066
2097
  let value = [
2067
2098
  "pipeline",
2068
2099
  id,
2069
2100
  path
2070
2101
  ];
2071
2102
  if (args) {
2072
- let devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
2103
+ let devalue;
2104
+ try {
2105
+ devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
2106
+ } catch (err) {
2107
+ args.dispose();
2108
+ throw err;
2109
+ }
2073
2110
  value.push(devalue[0]);
2074
2111
  }
2075
2112
  this.send(["push", value]);
@@ -2078,13 +2115,22 @@ var RpcSessionImpl = class {
2078
2115
  return new RpcImportHook(true, entry);
2079
2116
  }
2080
2117
  sendStream(id, path, args) {
2081
- if (this.abortReason) throw this.abortReason;
2118
+ if (this.abortReason) {
2119
+ args.dispose();
2120
+ throw this.abortReason;
2121
+ }
2082
2122
  let value = [
2083
2123
  "pipeline",
2084
2124
  id,
2085
2125
  path
2086
2126
  ];
2087
- 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
+ }
2088
2134
  value.push(devalue[0]);
2089
2135
  let msg = ["stream", value];
2090
2136
  let size = this.send(msg);
@@ -2305,7 +2351,7 @@ var RpcSession$1 = class {
2305
2351
 
2306
2352
  //#endregion
2307
2353
  //#region src/websocket.ts
2308
- /** Close-frame reason max UTF-8 bytes: 125 payload − 2-byte status (RFC 6455 §5.5). */
2354
+ /** Close-frame reason max UTF-8 bytes: 125 payload - 2-byte status (RFC 6455 section 5.5). */
2309
2355
  const MAX_CLOSE_REASON_BYTES = 123;
2310
2356
  function newWebSocketRpcSession$1(webSocket, localMain, options) {
2311
2357
  if (typeof webSocket === "string") webSocket = new WebSocket(webSocket);
@@ -2715,9 +2761,11 @@ var MapVariableHook = class extends StubHook {
2715
2761
  else throwMapperBuilderUseError();
2716
2762
  }
2717
2763
  call(path, args) {
2764
+ args.dispose();
2718
2765
  throwMapperBuilderUseError();
2719
2766
  }
2720
2767
  map(path, captures, instructions) {
2768
+ for (let cap of captures) cap.dispose();
2721
2769
  throwMapperBuilderUseError();
2722
2770
  }
2723
2771
  pull() {
@@ -2828,13 +2876,11 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
2828
2876
  let state = this.getState();
2829
2877
  if (path.length !== 1 || typeof path[0] !== "string") throw new Error("WritableStream stub only supports direct method calls");
2830
2878
  const method = path[0];
2831
- if (method !== "write" && method !== "close" && method !== "abort") {
2832
- args.dispose();
2833
- throw new Error(`Unknown WritableStream method: ${method}`);
2834
- }
2879
+ if (method !== "write" && method !== "close" && method !== "abort") throw new Error(`Unknown WritableStream method: ${method}`);
2835
2880
  if (method === "close" || method === "abort") state.closed = true;
2836
2881
  return new PromiseStubHook((method === "write" ? args.deliverStreamWrite(state.writer) : args.deliverCall(state.writer[method], state.writer)).then((payload) => new PayloadStubHook(payload)));
2837
2882
  } catch (err) {
2883
+ args.dispose();
2838
2884
  return new ErrorStubHook(err);
2839
2885
  }
2840
2886
  }