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.
@@ -42,7 +42,7 @@ type Stub<T extends RpcCompatible<T>> = T extends object ? Provider<T> & StubBas
42
42
  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
43
43
  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
44
44
  | WritableStream<any> // Chunk type can be any RPC-compatible type
45
- | Request | Response | Headers; // Recursively rewrite all `Stubable` types with `Stub`s, and resolve promises.
45
+ | URL | Request | Response | Headers; // Recursively rewrite all `Stubable` types with `Stub`s, and resolve promises.
46
46
  // prettier-ignore
47
47
  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.
48
48
  : T extends {
@@ -71,7 +71,13 @@ interface MapValuePlaceholder<T> {
71
71
  }
72
72
  type NonStubMembers<T> = Exclude<T, StubBase<any>>; // Utility type for adding `Disposable`s to `object` types only.
73
73
  // Note `unknown & T` is equivalent to `T`.
74
- type MaybeDisposable<T> = T extends object ? Disposable : unknown; // Type for method return or property on an RPC interface.
74
+ type MaybeDisposable<T> = T extends object ? Disposable : unknown; // The type behind the public `RpcPromise` (re-exported with its `RpcCompatible` constraint from
75
+ // index.ts). Defined here so that `Result`'s arms below resolve to the exact same alias
76
+ // instantiation: when both sides of an assignability check are the same alias applied to the
77
+ // same type arguments, the checker short-circuits on identity instead of recursing through the
78
+ // full structural comparison of these mutually-recursive types.
79
+ type RpcPromise$2<T> = T extends Stubable ? Promise<Stub<T>> & Provider<T> & StubBase<T> : Promise<Stubify<T> & MaybeDisposable<T>> & Provider<T> & StubBase<T>;
80
+ // Type for method return or property on an RPC interface.
75
81
  // - Stubable types are replaced by stubs.
76
82
  // - RpcCompatible types are passed by value, with stubable types replaced by stubs
77
83
  // and a top-level `Disposer`.
@@ -79,7 +85,7 @@ type MaybeDisposable<T> = T extends object ? Disposable : unknown; // Type for m
79
85
  // Technically, we use custom thenables here, but they quack like `Promise`s.
80
86
  // Intersecting with `(Maybe)Provider` allows pipelining.
81
87
  // prettier-ignore
82
- 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;
88
+ type Result<R> = IsAny<R> extends true ? UnknownResult : IsUnknown<R> extends true ? UnknownResult : R extends Stubable ? RpcPromise$2<R> : R extends RpcCompatible<R> ? RpcPromise$2<R> : never;
83
89
  type IsAny<T> = 0 extends (1 & T) ? true : false;
84
90
  type UnknownResult = Promise<unknown> & Provider<unknown> & StubBase<unknown>; // Type for method or property on an RPC interface.
85
91
  // For methods, unwrap `Stub`s in parameters, and rewrite returns to be `Result`s.
@@ -397,7 +403,7 @@ declare const RpcStub: {
397
403
  * if you only intend to use the promise for pipelining and you never await it, then there's no
398
404
  * need to transmit the resolution!
399
405
  */
400
- type RpcPromise<T extends RpcCompatible<T>> = Stub<T> & Promise<Stubify<T>>;
406
+ type RpcPromise<T extends RpcCompatible<T>> = RpcPromise$2<T>;
401
407
  declare const RpcPromise: {};
402
408
  /**
403
409
  * Use to construct an `RpcSession` on top of a custom `RpcTransport`.
@@ -42,7 +42,7 @@ type Stub<T extends RpcCompatible<T>> = T extends object ? Provider<T> & StubBas
42
42
  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
43
43
  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
44
44
  | WritableStream<any> // Chunk type can be any RPC-compatible type
45
- | Request | Response | Headers; // Recursively rewrite all `Stubable` types with `Stub`s, and resolve promises.
45
+ | URL | Request | Response | Headers; // Recursively rewrite all `Stubable` types with `Stub`s, and resolve promises.
46
46
  // prettier-ignore
47
47
  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.
48
48
  : T extends {
@@ -71,7 +71,13 @@ interface MapValuePlaceholder<T> {
71
71
  }
72
72
  type NonStubMembers<T> = Exclude<T, StubBase<any>>; // Utility type for adding `Disposable`s to `object` types only.
73
73
  // Note `unknown & T` is equivalent to `T`.
74
- type MaybeDisposable<T> = T extends object ? Disposable : unknown; // Type for method return or property on an RPC interface.
74
+ type MaybeDisposable<T> = T extends object ? Disposable : unknown; // The type behind the public `RpcPromise` (re-exported with its `RpcCompatible` constraint from
75
+ // index.ts). Defined here so that `Result`'s arms below resolve to the exact same alias
76
+ // instantiation: when both sides of an assignability check are the same alias applied to the
77
+ // same type arguments, the checker short-circuits on identity instead of recursing through the
78
+ // full structural comparison of these mutually-recursive types.
79
+ type RpcPromise$2<T> = T extends Stubable ? Promise<Stub<T>> & Provider<T> & StubBase<T> : Promise<Stubify<T> & MaybeDisposable<T>> & Provider<T> & StubBase<T>;
80
+ // Type for method return or property on an RPC interface.
75
81
  // - Stubable types are replaced by stubs.
76
82
  // - RpcCompatible types are passed by value, with stubable types replaced by stubs
77
83
  // and a top-level `Disposer`.
@@ -79,7 +85,7 @@ type MaybeDisposable<T> = T extends object ? Disposable : unknown; // Type for m
79
85
  // Technically, we use custom thenables here, but they quack like `Promise`s.
80
86
  // Intersecting with `(Maybe)Provider` allows pipelining.
81
87
  // prettier-ignore
82
- 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;
88
+ type Result<R> = IsAny<R> extends true ? UnknownResult : IsUnknown<R> extends true ? UnknownResult : R extends Stubable ? RpcPromise$2<R> : R extends RpcCompatible<R> ? RpcPromise$2<R> : never;
83
89
  type IsAny<T> = 0 extends (1 & T) ? true : false;
84
90
  type UnknownResult = Promise<unknown> & Provider<unknown> & StubBase<unknown>; // Type for method or property on an RPC interface.
85
91
  // For methods, unwrap `Stub`s in parameters, and rewrite returns to be `Result`s.
@@ -397,7 +403,7 @@ declare const RpcStub: {
397
403
  * if you only intend to use the promise for pipelining and you never await it, then there's no
398
404
  * need to transmit the resolution!
399
405
  */
400
- type RpcPromise<T extends RpcCompatible<T>> = Stub<T> & Promise<Stubify<T>>;
406
+ type RpcPromise<T extends RpcCompatible<T>> = RpcPromise$2<T>;
401
407
  declare const RpcPromise: {};
402
408
  /**
403
409
  * Use to construct an `RpcSession` on top of a custom `RpcTransport`.
package/dist/index-bun.js CHANGED
@@ -56,6 +56,7 @@ function typeForRpc(value) {
56
56
  case Float64Array.prototype: return "bytes";
57
57
  case WritableStream.prototype: return "writable";
58
58
  case ReadableStream.prototype: return "readable";
59
+ case URL.prototype: return "url";
59
60
  case Headers.prototype: return "headers";
60
61
  case Request.prototype: return "request";
61
62
  case Response.prototype: return "response";
@@ -89,7 +90,14 @@ let streamImpl = {
89
90
  };
90
91
  var StubHook = class {
91
92
  stream(path, args) {
92
- 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
+ }
93
101
  let promise;
94
102
  if (pulled instanceof Promise) promise = pulled.then((p) => {
95
103
  p.dispose();
@@ -108,9 +116,11 @@ var ErrorStubHook = class extends StubHook {
108
116
  this.error = error;
109
117
  }
110
118
  call(path, args) {
119
+ args.dispose();
111
120
  return this;
112
121
  }
113
122
  map(path, captures, instructions) {
123
+ for (let cap of captures) cap.dispose();
114
124
  return this;
115
125
  }
116
126
  get(path) {
@@ -390,6 +400,7 @@ var RpcPayload = class RpcPayload {
390
400
  case "date":
391
401
  case "bytes":
392
402
  case "blob":
403
+ case "url":
393
404
  case "error":
394
405
  case "undefined": return value;
395
406
  case "array": {
@@ -587,6 +598,7 @@ var RpcPayload = class RpcPayload {
587
598
  case "bytes":
588
599
  case "blob":
589
600
  case "date":
601
+ case "url":
590
602
  case "error":
591
603
  case "undefined": return;
592
604
  case "array": {
@@ -669,6 +681,7 @@ var RpcPayload = class RpcPayload {
669
681
  case "rpc-target":
670
682
  case "writable":
671
683
  case "readable":
684
+ case "url":
672
685
  case "headers":
673
686
  case "request":
674
687
  case "response": return;
@@ -738,6 +751,7 @@ function followPath(value, parent, path, owner) {
738
751
  case "blob":
739
752
  case "date":
740
753
  case "error":
754
+ case "url":
741
755
  case "headers":
742
756
  case "request":
743
757
  case "response":
@@ -770,17 +784,22 @@ function followPath(value, parent, path, owner) {
770
784
  }
771
785
  var ValueStubHook = class extends StubHook {
772
786
  call(path, args) {
787
+ let followResult;
773
788
  try {
774
789
  let { value, owner } = this.getValue();
775
- let followResult = followPath(value, void 0, path, owner);
776
- if (followResult.hook) return followResult.hook.call(followResult.remainingPath, args);
777
- if (typeof followResult.value != "function") throw new TypeError(`'${path.join(".")}' is not a function.`);
778
- return new PromiseStubHook(args.deliverCall(followResult.value, followResult.parent).then((payload) => {
779
- return new PayloadStubHook(payload);
780
- }));
790
+ followResult = followPath(value, void 0, path, owner);
781
791
  } catch (err) {
792
+ args.dispose();
782
793
  return new ErrorStubHook(err);
783
794
  }
795
+ if (followResult.hook) return followResult.hook.call(followResult.remainingPath, args);
796
+ if (typeof followResult.value != "function") {
797
+ args.dispose();
798
+ return new ErrorStubHook(/* @__PURE__ */ new TypeError(`'${path.join(".")}' is not a function.`));
799
+ }
800
+ return new PromiseStubHook(args.deliverCall(followResult.value, followResult.parent).then((payload) => {
801
+ return new PayloadStubHook(payload);
802
+ }));
784
803
  }
785
804
  map(path, captures, instructions) {
786
805
  try {
@@ -918,12 +937,16 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
918
937
  }
919
938
  call(path, args) {
920
939
  args.ensureDeepCopied();
921
- return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args)));
940
+ return new PromiseStubHook(this.promise.then((hook) => hook.call(path, args), (err) => {
941
+ args.dispose();
942
+ throw err;
943
+ }));
922
944
  }
923
945
  stream(path, args) {
924
946
  args.ensureDeepCopied();
925
- return { promise: this.promise.then((hook) => {
926
- return hook.stream(path, args).promise;
947
+ return { promise: this.promise.then((hook) => hook.stream(path, args).promise, (err) => {
948
+ args.dispose();
949
+ throw err;
927
950
  }) };
928
951
  }
929
952
  map(path, captures, instructions) {
@@ -950,10 +973,7 @@ var PromiseStubHook = class PromiseStubHook extends StubHook {
950
973
  }, (err) => {});
951
974
  }
952
975
  dispose() {
953
- if (this.resolution) this.resolution.dispose();
954
- else this.promise.then((hook) => {
955
- hook.dispose();
956
- }, (err) => {});
976
+ this.promise.then((hook) => hook.dispose(), () => {});
957
977
  }
958
978
  onBroken(callback) {
959
979
  if (this.resolution) this.resolution.onBroken(callback);
@@ -1160,6 +1180,7 @@ var Devaluator = class Devaluator {
1160
1180
  alternateTypeName
1161
1181
  ];
1162
1182
  }
1183
+ case "url": return ["url", value.href];
1163
1184
  case "headers": return ["headers", [...value]];
1164
1185
  case "request": {
1165
1186
  let req = value;
@@ -1479,6 +1500,9 @@ var Evaluator = class Evaluator {
1479
1500
  case "inf": return Infinity;
1480
1501
  case "-inf": return -Infinity;
1481
1502
  case "nan": return NaN;
1503
+ case "url":
1504
+ if (value.length === 2 && typeof value[1] === "string") return new URL(value[1]);
1505
+ break;
1482
1506
  case "headers":
1483
1507
  if (value.length === 2 && value[1] instanceof Array) return new Headers(value[1]);
1484
1508
  break;
@@ -1778,24 +1802,28 @@ var RpcImportHook = class RpcImportHook extends StubHook {
1778
1802
  if (this.entry) return this.entry;
1779
1803
  else throw new Error("This RpcImportHook was already disposed.");
1780
1804
  }
1805
+ getEntryTakingOwnership(disposeOwned) {
1806
+ try {
1807
+ return this.getEntry();
1808
+ } catch (err) {
1809
+ disposeOwned();
1810
+ throw err;
1811
+ }
1812
+ }
1781
1813
  call(path, args) {
1782
- let entry = this.getEntry();
1814
+ let entry = this.getEntryTakingOwnership(() => args.dispose());
1783
1815
  if (entry.resolution) return entry.resolution.call(path, args);
1784
1816
  else return entry.session.sendCall(entry.importId, path, args);
1785
1817
  }
1786
1818
  stream(path, args) {
1787
- let entry = this.getEntry();
1819
+ let entry = this.getEntryTakingOwnership(() => args.dispose());
1788
1820
  if (entry.resolution) return entry.resolution.stream(path, args);
1789
1821
  else return entry.session.sendStream(entry.importId, path, args);
1790
1822
  }
1791
1823
  map(path, captures, instructions) {
1792
- let entry;
1793
- try {
1794
- entry = this.getEntry();
1795
- } catch (err) {
1824
+ let entry = this.getEntryTakingOwnership(() => {
1796
1825
  for (let cap of captures) cap.dispose();
1797
- throw err;
1798
- }
1826
+ });
1799
1827
  if (entry.resolution) return entry.resolution.map(path, captures, instructions);
1800
1828
  else return entry.session.sendMap(entry.importId, path, captures, instructions);
1801
1829
  }
@@ -2056,14 +2084,23 @@ var RpcSessionImpl = class {
2056
2084
  }
2057
2085
  }
2058
2086
  sendCall(id, path, args) {
2059
- if (this.abortReason) throw this.abortReason;
2087
+ if (this.abortReason) {
2088
+ args?.dispose();
2089
+ throw this.abortReason;
2090
+ }
2060
2091
  let value = [
2061
2092
  "pipeline",
2062
2093
  id,
2063
2094
  path
2064
2095
  ];
2065
2096
  if (args) {
2066
- let devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
2097
+ let devalue;
2098
+ try {
2099
+ devalue = Devaluator.devaluate(args.value, void 0, this, args, this.encodingLevel);
2100
+ } catch (err) {
2101
+ args.dispose();
2102
+ throw err;
2103
+ }
2067
2104
  value.push(devalue[0]);
2068
2105
  }
2069
2106
  this.send(["push", value]);
@@ -2072,13 +2109,22 @@ var RpcSessionImpl = class {
2072
2109
  return new RpcImportHook(true, entry);
2073
2110
  }
2074
2111
  sendStream(id, path, args) {
2075
- if (this.abortReason) throw this.abortReason;
2112
+ if (this.abortReason) {
2113
+ args.dispose();
2114
+ throw this.abortReason;
2115
+ }
2076
2116
  let value = [
2077
2117
  "pipeline",
2078
2118
  id,
2079
2119
  path
2080
2120
  ];
2081
- 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
+ }
2082
2128
  value.push(devalue[0]);
2083
2129
  let msg = ["stream", value];
2084
2130
  let size = this.send(msg);
@@ -2299,7 +2345,7 @@ var RpcSession$1 = class {
2299
2345
 
2300
2346
  //#endregion
2301
2347
  //#region src/websocket.ts
2302
- /** Close-frame reason max UTF-8 bytes: 125 payload − 2-byte status (RFC 6455 §5.5). */
2348
+ /** Close-frame reason max UTF-8 bytes: 125 payload - 2-byte status (RFC 6455 section 5.5). */
2303
2349
  const MAX_CLOSE_REASON_BYTES = 123;
2304
2350
  function newWebSocketRpcSession$1(webSocket, localMain, options) {
2305
2351
  if (typeof webSocket === "string") webSocket = new WebSocket(webSocket);
@@ -2709,9 +2755,11 @@ var MapVariableHook = class extends StubHook {
2709
2755
  else throwMapperBuilderUseError();
2710
2756
  }
2711
2757
  call(path, args) {
2758
+ args.dispose();
2712
2759
  throwMapperBuilderUseError();
2713
2760
  }
2714
2761
  map(path, captures, instructions) {
2762
+ for (let cap of captures) cap.dispose();
2715
2763
  throwMapperBuilderUseError();
2716
2764
  }
2717
2765
  pull() {
@@ -2822,13 +2870,11 @@ var WritableStreamStubHook = class WritableStreamStubHook extends StubHook {
2822
2870
  let state = this.getState();
2823
2871
  if (path.length !== 1 || typeof path[0] !== "string") throw new Error("WritableStream stub only supports direct method calls");
2824
2872
  const method = path[0];
2825
- if (method !== "write" && method !== "close" && method !== "abort") {
2826
- args.dispose();
2827
- throw new Error(`Unknown WritableStream method: ${method}`);
2828
- }
2873
+ if (method !== "write" && method !== "close" && method !== "abort") throw new Error(`Unknown WritableStream method: ${method}`);
2829
2874
  if (method === "close" || method === "abort") state.closed = true;
2830
2875
  return new PromiseStubHook((method === "write" ? args.deliverStreamWrite(state.writer) : args.deliverCall(state.writer[method], state.writer)).then((payload) => new PayloadStubHook(payload)));
2831
2876
  } catch (err) {
2877
+ args.dispose();
2832
2878
  return new ErrorStubHook(err);
2833
2879
  }
2834
2880
  }