capnweb 0.0.0-3edfc50 → 0.0.0-405aa91

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.
@@ -1,5 +1,4 @@
1
1
  import { IncomingMessage, OutgoingHttpHeader, OutgoingHttpHeaders, ServerResponse } from "node:http";
2
-
3
2
  //#region src/types.d.ts
4
3
  // Copyright (c) 2025 Cloudflare, Inc.
5
4
  // Licensed under the MIT license found in the LICENSE.txt file or at:
@@ -21,16 +20,22 @@ interface RpcTargetBranded {
21
20
  // `any` into inference.
22
21
  type Stubable = RpcTargetBranded | ((...args: never[]) => unknown);
23
22
  // Note: also true for `any` (call sites that care check `IsAny` first).
24
- type IsUnknown<T> = unknown extends T ? true : false; // Types that can be passed over RPC
23
+ type IsUnknown<T> = unknown extends T ? true : false;
24
+ // Types that can be passed over RPC
25
25
  // The reason for using a generic type here is to build the serializable subset of RPC-compatible
26
26
  // composite types. This allows types defined with the "interface" keyword to pass the
27
27
  // serializable check as well. Otherwise, only types defined with the "type" keyword would pass.
28
- type RpcCompatible<T> = // Allow `unknown` as a leaf so records/interfaces with `unknown` fields remain compatible.
29
- (IsUnknown<T> extends true ? unknown : never) // RPC-compatible base values
30
- | BaseType // RPC-compatible composites
31
- | Map<T extends Map<infer U, unknown> ? RpcCompatible<U> : never, T extends Map<unknown, infer U> ? RpcCompatible<U> : never> | Set<T extends Set<infer U> ? RpcCompatible<U> : never> | Array<T extends Array<infer U> ? RpcCompatible<U> : never> | ReadonlyArray<T extends ReadonlyArray<infer U> ? RpcCompatible<U> : never> | { [K in keyof T as K extends string | number ? K : never]: RpcCompatible<T[K]> } | Promise<T extends Promise<infer U> ? RpcCompatible<U> : never> // Special types
32
- | Stub<Stubable> // Serialized as stubs, see `Stubify`
33
- | Stubable;
28
+ type RpcCompatible<T> =
29
+ // Allow `unknown` as a leaf so records/interfaces with `unknown` fields remain compatible.
30
+ (IsUnknown<T> extends true ? unknown : never) |
31
+ // RPC-compatible base values
32
+ BaseType |
33
+ // RPC-compatible composites
34
+ Map<T extends Map<infer U, unknown> ? RpcCompatible<U> : never, T extends Map<unknown, infer U> ? RpcCompatible<U> : never> | Set<T extends Set<infer U> ? RpcCompatible<U> : never> | Array<T extends Array<infer U> ? RpcCompatible<U> : never> | ReadonlyArray<T extends ReadonlyArray<infer U> ? RpcCompatible<U> : never> | { [K in keyof T as K extends string | number ? K : never]: RpcCompatible<T[K]>; } | Promise<T extends Promise<infer U> ? RpcCompatible<U> : never> |
35
+ // Special types
36
+ Stub<Stubable> |
37
+ // Serialized as stubs, see `Stubify`
38
+ Stubable;
34
39
  // Base type for all RPC stubs, including common memory management methods.
35
40
  // `T` is used as a marker type for unwrapping `Stub`s later.
36
41
  interface StubBase<T = unknown> extends Disposable {
@@ -39,10 +44,12 @@ interface StubBase<T = unknown> extends Disposable {
39
44
  onRpcBroken(callback: (error: any) => void): void;
40
45
  }
41
46
  type Stub<T extends RpcCompatible<T>> = T extends object ? Provider<T> & StubBase<T> : StubBase<T>;
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
47
+ type TypedArray = Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array | Int8Array | Int16Array | Int32Array | BigUint64Array | BigInt64Array | Float32Array | Float64Array;
48
+ // This represents all the types that can be sent as-is over an RPC boundary
43
49
  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
- | WritableStream<any> // Chunk type can be any RPC-compatible type
45
- | URL | Request | Response | Headers; // Recursively rewrite all `Stubable` types with `Stub`s, and resolve promises.
50
+ | WritableStream<any> // Chunk type can be any RPC-compatible type
51
+ | URL | Request | Response | Headers;
52
+ // Recursively rewrite all `Stubable` types with `Stub`s, and resolve promises.
46
53
  // Arm ordering matters here:
47
54
  // - `Promise` must come before `StubBase`: `RpcPromise<T>` matches both, and must resolve
48
55
  // through the Promise arm rather than pass through as-is.
@@ -50,19 +57,22 @@ type BaseType = void | undefined | null | boolean | number | bigint | string | T
50
57
  // it matches `Stubable`. Checking `StubBase` first keeps existing stubs as-is instead of
51
58
  // double-wrapping them as `Stub<Stub<T>>`.
52
59
  // prettier-ignore
53
- type Stubify<T> = T extends Promise<infer U> ? Stubify<U> : T extends StubBase<any> ? T : T extends Stubable ? Stub<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.
54
- : T extends {
60
+ type Stubify<T> = T extends Promise<infer U> ? Stubify<U> : T extends StubBase<any> ? T : T extends Stubable ? Stub<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 :
61
+ // When using "unknown" instead of "any", interfaces are not stubified.
62
+ T extends {
55
63
  [key: string | number]: any;
56
- } ? { [K in keyof T as K extends string | number ? K : never]: Stubify<T[K]> } : T;
64
+ } ? { [K in keyof T as K extends string | number ? K : never]: Stubify<T[K]>; } : T;
57
65
  // Recursively rewrite all `Stub<T>`s with the corresponding `T`s.
58
66
  // Note we use `StubBase` instead of `Stub` here to avoid circular dependencies:
59
67
  // `Stub` depends on `Provider`, which depends on `Unstubify`, which would depend on `Stub`.
60
68
  // prettier-ignore
61
- type UnstubifyInner<T> = // Preserve local RpcTarget acceptance, but avoid needless `Stub | Value` unions when the stub
69
+ type UnstubifyInner<T> =
70
+ // Preserve local RpcTarget acceptance, but avoid needless `Stub | Value` unions when the stub
62
71
  // is already assignable to the value type (important for callback contextual typing).
63
72
  T extends StubBase<infer V> ? (T extends V ? UnstubifyInner<V> : (T | UnstubifyInner<V>)) : T extends Promise<infer U> ? UnstubifyInner<U> : T extends Map<infer K, infer V> ? Map<Unstubify<K>, Unstubify<V>> : T extends Set<infer V> ? Set<Unstubify<V>> : T extends [] ? [] : T extends [infer Head, ...infer Tail] ? [Unstubify<Head>, ...Unstubify<Tail>] : T extends readonly [] ? readonly [] : T extends readonly [infer Head, ...infer Tail] ? readonly [Unstubify<Head>, ...Unstubify<Tail>] : T extends Array<infer V> ? Array<Unstubify<V>> : T extends ReadonlyArray<infer V> ? ReadonlyArray<Unstubify<V>> : T extends BaseType ? T : T extends {
64
73
  [key: string | number]: unknown;
65
- } ? { [K in keyof T as K extends string | number ? K : never]: Unstubify<T[K]> } : T; // You can put promises anywhere in the params and they'll be resolved before delivery.
74
+ } ? { [K in keyof T as K extends string | number ? K : never]: Unstubify<T[K]>; } : T;
75
+ // You can put promises anywhere in the params and they'll be resolved before delivery.
66
76
  // (This also covers RpcPromise, because it's defined as being a Promise.)
67
77
  // Map placeholders are also allowed so primitive map callback inputs can be forwarded directly
68
78
  // into RPC params.
@@ -71,13 +81,15 @@ T extends StubBase<infer V> ? (T extends V ? UnstubifyInner<V> : (T | UnstubifyI
71
81
  // Remove stub members from mixed unions so callback params don’t get both stub and unstubbed signatures.
72
82
  // Marker carried by map() callback inputs. This lets primitive placeholders flow through params.
73
83
  type Unstubify<T> = NonStubMembers<T> | UnstubifyInner<T> | Promise<UnstubifyInner<T>> | MapValuePlaceholder<UnstubifyInner<T>>;
74
- type UnstubifyAll<A extends readonly unknown[]> = { [I in keyof A]: Unstubify<A[I]> };
84
+ type UnstubifyAll<A extends readonly unknown[]> = { [I in keyof A]: Unstubify<A[I]>; };
75
85
  interface MapValuePlaceholder<T> {
76
86
  [__RPC_MAP_VALUE_BRAND]: T;
77
87
  }
78
- type NonStubMembers<T> = Exclude<T, StubBase<any>>; // Utility type for adding `Disposable`s to `object` types only.
88
+ type NonStubMembers<T> = Exclude<T, StubBase<any>>;
89
+ // Utility type for adding `Disposable`s to `object` types only.
79
90
  // Note `unknown & T` is equivalent to `T`.
80
- type MaybeDisposable<T> = T extends object ? Disposable : unknown; // The type behind the public `RpcPromise` (re-exported with its `RpcCompatible` constraint from
91
+ type MaybeDisposable<T> = T extends object ? Disposable : unknown;
92
+ // The type behind the public `RpcPromise` (re-exported with its `RpcCompatible` constraint from
81
93
  // index.ts). Defined here so that `Result`'s arms below resolve to the exact same alias
82
94
  // instantiation: when both sides of an assignability check are the same alias applied to the
83
95
  // same type arguments, the checker short-circuits on identity instead of recursing through the
@@ -109,41 +121,49 @@ type PayloadOrStub<T> = T | NoInfer<Stub<Extract<T, Stubable>>>;
109
121
  // Technically, we use custom thenables here, but they quack like `Promise`s.
110
122
  // Intersecting with `(Maybe)Provider` allows pipelining.
111
123
  // prettier-ignore
112
- type Result<R> = IsAny<R> extends true ? RpcPromise$1<unknown> // `RpcPromise<U>`: always safe to normalize — `Result` is idempotent — so a declared
124
+ type Result<R> = IsAny<R> extends true ? RpcPromise$1<unknown> :
125
+ // `RpcPromise<U>`: always safe to normalize — `Result` is idempotent — so a declared
113
126
  // `RpcPromise<U>` return produces the same type as a `Promise<U>` return.
114
- : R extends PromiseLike<unknown> & StubBase<infer U> ? Result<U> // Bare stubs: elide per `ElideStub` above. Note there is no `RpcCompatible<R>` re-check
127
+ R extends PromiseLike<unknown> & StubBase<infer U> ? Result<U> :
128
+ // Bare stubs: elide per `ElideStub` above. Note there is no `RpcCompatible<R>` re-check
115
129
  // here: anything matching our `StubBase` was produced by machinery that already enforced
116
130
  // the constraint, and re-evaluating `RpcCompatible<R>` in this arm recurses through its
117
131
  // `Stub<Stubable>` member back into `Result`, tripping TS2615 circularity errors in mapped
118
132
  // types.
119
- : R extends StubBase<any> ? RpcPromise$1<ElideStub<R>> : R extends RpcCompatible<R> ? RpcPromise$1<R> : never;
120
- type IsAny<T> = 0 extends (1 & T) ? true : false; // Type for method or property on an RPC interface.
133
+ R extends StubBase<any> ? RpcPromise$1<ElideStub<R>> : R extends RpcCompatible<R> ? RpcPromise$1<R> : never;
134
+ type IsAny<T> = 0 extends (1 & T) ? true : false;
135
+ // Type for method or property on an RPC interface.
121
136
  // For methods, unwrap `Stub`s in parameters, and rewrite returns to be `Result`s.
122
137
  // Unwrapping `Stub`s allows calling with `Stubable` arguments.
123
138
  // For properties, rewrite types to be `Result`s.
124
139
  // In each case, unwrap `Promise`s.
125
- type MethodOrProperty<V> = V extends ((...args: infer P) => infer R) ? (...args: UnstubifyAll<P>) => Result<Awaited<R>> : Result<Awaited<V>>; // Type for the callable part of an `Provider` if `T` is callable.
140
+ type MethodOrProperty<V> = V extends ((...args: infer P) => infer R) ? (...args: UnstubifyAll<P>) => Result<Awaited<R>> : Result<Awaited<V>>;
141
+ // Type for the callable part of an `Provider` if `T` is callable.
126
142
  // This is intersected with methods/properties.
127
143
  type MaybeCallableProvider<T> = T extends ((...args: any[]) => any) ? MethodOrProperty<T> : unknown;
128
144
  type TupleIndexKeys<T extends ReadonlyArray<unknown>> = Extract<keyof T, `${number}`>;
129
- type MapCallbackValue<T> = // `Omit` removes call signatures, so re-intersect callable provider behavior.
145
+ type MapCallbackValue<T> =
146
+ // `Omit` removes call signatures, so re-intersect callable provider behavior.
130
147
  T extends unknown ? Omit<Result<T>, keyof Promise<unknown>> & MaybeCallableProvider<T> & MapValuePlaceholder<T> : never;
131
148
  type InvalidNativePromiseInMapResult<T, Seen = never> = T extends unknown ? InvalidNativePromiseInMapResultImpl<T, Seen> : never;
132
- type InvalidNativePromiseInMapResultImpl<T, Seen> = [T] extends [Seen] ? never // RpcPromise is modeled as Promise & StubBase, so allow promise-like stub values.
133
- : T extends StubBase<any> ? never // Native thenables cannot be represented in map recordings, even when typed as PromiseLike.
134
- : T extends PromiseLike<unknown> ? T : T extends Map<infer K, infer V> ? InvalidNativePromiseInMapResult<K, Seen | T> | InvalidNativePromiseInMapResult<V, Seen | T> : T extends Set<infer V> ? InvalidNativePromiseInMapResult<V, Seen | T> : T extends readonly [] ? never : T extends readonly [infer Head, ...infer Tail] ? InvalidNativePromiseInMapResult<Head, Seen | T> | InvalidNativePromiseInMapResult<Tail[number], Seen | T> : T extends ReadonlyArray<infer V> ? InvalidNativePromiseInMapResult<V, Seen | T> : T extends {
149
+ type InvalidNativePromiseInMapResultImpl<T, Seen> = [T] extends [Seen] ? never :
150
+ // RpcPromise is modeled as Promise & StubBase, so allow promise-like stub values.
151
+ T extends StubBase<any> ? never :
152
+ // Native thenables cannot be represented in map recordings, even when typed as PromiseLike.
153
+ T extends PromiseLike<unknown> ? T : T extends Map<infer K, infer V> ? InvalidNativePromiseInMapResult<K, Seen | T> | InvalidNativePromiseInMapResult<V, Seen | T> : T extends Set<infer V> ? InvalidNativePromiseInMapResult<V, Seen | T> : T extends readonly [] ? never : T extends readonly [infer Head, ...infer Tail] ? InvalidNativePromiseInMapResult<Head, Seen | T> | InvalidNativePromiseInMapResult<Tail[number], Seen | T> : T extends ReadonlyArray<infer V> ? InvalidNativePromiseInMapResult<V, Seen | T> : T extends {
135
154
  [key: string | number]: unknown;
136
155
  } ? InvalidNativePromiseInMapResult<T[Extract<keyof T, string | number>], Seen | T> : never;
137
156
  type MapCallbackReturn<T> = InvalidNativePromiseInMapResult<T> extends never ? T : never;
138
- type ArrayProvider<E> = { [K in number]: MethodOrProperty<E> } & {
157
+ type ArrayProvider<E> = { [K in number]: MethodOrProperty<E>; } & {
139
158
  map<V>(callback: (elem: MapCallbackValue<E>) => MapCallbackReturn<V>): Result<Array<V>>;
140
159
  };
141
- type TupleProvider<T extends ReadonlyArray<unknown>> = { [K in TupleIndexKeys<T>]: MethodOrProperty<T[K]> } & ArrayProvider<T[number]>; // Base type for all other types providing RPC-like interfaces.
160
+ type TupleProvider<T extends ReadonlyArray<unknown>> = { [K in TupleIndexKeys<T>]: MethodOrProperty<T[K]>; } & ArrayProvider<T[number]>;
161
+ // Base type for all other types providing RPC-like interfaces.
142
162
  // Rewrites all methods/properties to be `MethodOrProperty`s, while preserving callable types.
143
163
  // `__RPC_TARGET_BRAND` deliberately flows through the key mapping (as a `never` property) so
144
164
  // stubs of branded targets stay assignable to workers-types' `Stubable`. Such stubs match our
145
165
  // `Stubable` too — harmless, since all machinery checks `StubBase` before `Stubable`.
146
- type Provider<T> = MaybeCallableProvider<T> & (T extends ReadonlyArray<unknown> ? number extends T["length"] ? ArrayProvider<T[number]> : TupleProvider<T> : Pick<{ [K in keyof T]: MethodOrProperty<T[K]> }, Exclude<keyof T, symbol | keyof StubBase<never>>> & {
166
+ type Provider<T> = MaybeCallableProvider<T> & (T extends ReadonlyArray<unknown> ? number extends T["length"] ? ArrayProvider<T[number]> : TupleProvider<T> : Pick<{ [K in keyof T]: MethodOrProperty<T[K]>; }, Exclude<keyof T, symbol | keyof StubBase<never>>> & {
147
167
  map<V>(callback: (value: MapCallbackValue<NonNullable<T>>) => MapCallbackReturn<V>): Result<Array<V>>;
148
168
  });
149
169
  //#endregion
@@ -1,5 +1,4 @@
1
1
  import { IncomingMessage, OutgoingHttpHeader, OutgoingHttpHeaders, ServerResponse } from "node:http";
2
-
3
2
  //#region src/types.d.ts
4
3
  // Copyright (c) 2025 Cloudflare, Inc.
5
4
  // Licensed under the MIT license found in the LICENSE.txt file or at:
@@ -21,16 +20,22 @@ interface RpcTargetBranded {
21
20
  // `any` into inference.
22
21
  type Stubable = RpcTargetBranded | ((...args: never[]) => unknown);
23
22
  // Note: also true for `any` (call sites that care check `IsAny` first).
24
- type IsUnknown<T> = unknown extends T ? true : false; // Types that can be passed over RPC
23
+ type IsUnknown<T> = unknown extends T ? true : false;
24
+ // Types that can be passed over RPC
25
25
  // The reason for using a generic type here is to build the serializable subset of RPC-compatible
26
26
  // composite types. This allows types defined with the "interface" keyword to pass the
27
27
  // serializable check as well. Otherwise, only types defined with the "type" keyword would pass.
28
- type RpcCompatible<T> = // Allow `unknown` as a leaf so records/interfaces with `unknown` fields remain compatible.
29
- (IsUnknown<T> extends true ? unknown : never) // RPC-compatible base values
30
- | BaseType // RPC-compatible composites
31
- | Map<T extends Map<infer U, unknown> ? RpcCompatible<U> : never, T extends Map<unknown, infer U> ? RpcCompatible<U> : never> | Set<T extends Set<infer U> ? RpcCompatible<U> : never> | Array<T extends Array<infer U> ? RpcCompatible<U> : never> | ReadonlyArray<T extends ReadonlyArray<infer U> ? RpcCompatible<U> : never> | { [K in keyof T as K extends string | number ? K : never]: RpcCompatible<T[K]> } | Promise<T extends Promise<infer U> ? RpcCompatible<U> : never> // Special types
32
- | Stub<Stubable> // Serialized as stubs, see `Stubify`
33
- | Stubable;
28
+ type RpcCompatible<T> =
29
+ // Allow `unknown` as a leaf so records/interfaces with `unknown` fields remain compatible.
30
+ (IsUnknown<T> extends true ? unknown : never) |
31
+ // RPC-compatible base values
32
+ BaseType |
33
+ // RPC-compatible composites
34
+ Map<T extends Map<infer U, unknown> ? RpcCompatible<U> : never, T extends Map<unknown, infer U> ? RpcCompatible<U> : never> | Set<T extends Set<infer U> ? RpcCompatible<U> : never> | Array<T extends Array<infer U> ? RpcCompatible<U> : never> | ReadonlyArray<T extends ReadonlyArray<infer U> ? RpcCompatible<U> : never> | { [K in keyof T as K extends string | number ? K : never]: RpcCompatible<T[K]>; } | Promise<T extends Promise<infer U> ? RpcCompatible<U> : never> |
35
+ // Special types
36
+ Stub<Stubable> |
37
+ // Serialized as stubs, see `Stubify`
38
+ Stubable;
34
39
  // Base type for all RPC stubs, including common memory management methods.
35
40
  // `T` is used as a marker type for unwrapping `Stub`s later.
36
41
  interface StubBase<T = unknown> extends Disposable {
@@ -39,10 +44,12 @@ interface StubBase<T = unknown> extends Disposable {
39
44
  onRpcBroken(callback: (error: any) => void): void;
40
45
  }
41
46
  type Stub<T extends RpcCompatible<T>> = T extends object ? Provider<T> & StubBase<T> : StubBase<T>;
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
47
+ type TypedArray = Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array | Int8Array | Int16Array | Int32Array | BigUint64Array | BigInt64Array | Float32Array | Float64Array;
48
+ // This represents all the types that can be sent as-is over an RPC boundary
43
49
  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
- | WritableStream<any> // Chunk type can be any RPC-compatible type
45
- | URL | Request | Response | Headers; // Recursively rewrite all `Stubable` types with `Stub`s, and resolve promises.
50
+ | WritableStream<any> // Chunk type can be any RPC-compatible type
51
+ | URL | Request | Response | Headers;
52
+ // Recursively rewrite all `Stubable` types with `Stub`s, and resolve promises.
46
53
  // Arm ordering matters here:
47
54
  // - `Promise` must come before `StubBase`: `RpcPromise<T>` matches both, and must resolve
48
55
  // through the Promise arm rather than pass through as-is.
@@ -50,19 +57,22 @@ type BaseType = void | undefined | null | boolean | number | bigint | string | T
50
57
  // it matches `Stubable`. Checking `StubBase` first keeps existing stubs as-is instead of
51
58
  // double-wrapping them as `Stub<Stub<T>>`.
52
59
  // prettier-ignore
53
- type Stubify<T> = T extends Promise<infer U> ? Stubify<U> : T extends StubBase<any> ? T : T extends Stubable ? Stub<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.
54
- : T extends {
60
+ type Stubify<T> = T extends Promise<infer U> ? Stubify<U> : T extends StubBase<any> ? T : T extends Stubable ? Stub<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 :
61
+ // When using "unknown" instead of "any", interfaces are not stubified.
62
+ T extends {
55
63
  [key: string | number]: any;
56
- } ? { [K in keyof T as K extends string | number ? K : never]: Stubify<T[K]> } : T;
64
+ } ? { [K in keyof T as K extends string | number ? K : never]: Stubify<T[K]>; } : T;
57
65
  // Recursively rewrite all `Stub<T>`s with the corresponding `T`s.
58
66
  // Note we use `StubBase` instead of `Stub` here to avoid circular dependencies:
59
67
  // `Stub` depends on `Provider`, which depends on `Unstubify`, which would depend on `Stub`.
60
68
  // prettier-ignore
61
- type UnstubifyInner<T> = // Preserve local RpcTarget acceptance, but avoid needless `Stub | Value` unions when the stub
69
+ type UnstubifyInner<T> =
70
+ // Preserve local RpcTarget acceptance, but avoid needless `Stub | Value` unions when the stub
62
71
  // is already assignable to the value type (important for callback contextual typing).
63
72
  T extends StubBase<infer V> ? (T extends V ? UnstubifyInner<V> : (T | UnstubifyInner<V>)) : T extends Promise<infer U> ? UnstubifyInner<U> : T extends Map<infer K, infer V> ? Map<Unstubify<K>, Unstubify<V>> : T extends Set<infer V> ? Set<Unstubify<V>> : T extends [] ? [] : T extends [infer Head, ...infer Tail] ? [Unstubify<Head>, ...Unstubify<Tail>] : T extends readonly [] ? readonly [] : T extends readonly [infer Head, ...infer Tail] ? readonly [Unstubify<Head>, ...Unstubify<Tail>] : T extends Array<infer V> ? Array<Unstubify<V>> : T extends ReadonlyArray<infer V> ? ReadonlyArray<Unstubify<V>> : T extends BaseType ? T : T extends {
64
73
  [key: string | number]: unknown;
65
- } ? { [K in keyof T as K extends string | number ? K : never]: Unstubify<T[K]> } : T; // You can put promises anywhere in the params and they'll be resolved before delivery.
74
+ } ? { [K in keyof T as K extends string | number ? K : never]: Unstubify<T[K]>; } : T;
75
+ // You can put promises anywhere in the params and they'll be resolved before delivery.
66
76
  // (This also covers RpcPromise, because it's defined as being a Promise.)
67
77
  // Map placeholders are also allowed so primitive map callback inputs can be forwarded directly
68
78
  // into RPC params.
@@ -71,13 +81,15 @@ T extends StubBase<infer V> ? (T extends V ? UnstubifyInner<V> : (T | UnstubifyI
71
81
  // Remove stub members from mixed unions so callback params don’t get both stub and unstubbed signatures.
72
82
  // Marker carried by map() callback inputs. This lets primitive placeholders flow through params.
73
83
  type Unstubify<T> = NonStubMembers<T> | UnstubifyInner<T> | Promise<UnstubifyInner<T>> | MapValuePlaceholder<UnstubifyInner<T>>;
74
- type UnstubifyAll<A extends readonly unknown[]> = { [I in keyof A]: Unstubify<A[I]> };
84
+ type UnstubifyAll<A extends readonly unknown[]> = { [I in keyof A]: Unstubify<A[I]>; };
75
85
  interface MapValuePlaceholder<T> {
76
86
  [__RPC_MAP_VALUE_BRAND]: T;
77
87
  }
78
- type NonStubMembers<T> = Exclude<T, StubBase<any>>; // Utility type for adding `Disposable`s to `object` types only.
88
+ type NonStubMembers<T> = Exclude<T, StubBase<any>>;
89
+ // Utility type for adding `Disposable`s to `object` types only.
79
90
  // Note `unknown & T` is equivalent to `T`.
80
- type MaybeDisposable<T> = T extends object ? Disposable : unknown; // The type behind the public `RpcPromise` (re-exported with its `RpcCompatible` constraint from
91
+ type MaybeDisposable<T> = T extends object ? Disposable : unknown;
92
+ // The type behind the public `RpcPromise` (re-exported with its `RpcCompatible` constraint from
81
93
  // index.ts). Defined here so that `Result`'s arms below resolve to the exact same alias
82
94
  // instantiation: when both sides of an assignability check are the same alias applied to the
83
95
  // same type arguments, the checker short-circuits on identity instead of recursing through the
@@ -109,41 +121,49 @@ type PayloadOrStub<T> = T | NoInfer<Stub<Extract<T, Stubable>>>;
109
121
  // Technically, we use custom thenables here, but they quack like `Promise`s.
110
122
  // Intersecting with `(Maybe)Provider` allows pipelining.
111
123
  // prettier-ignore
112
- type Result<R> = IsAny<R> extends true ? RpcPromise$1<unknown> // `RpcPromise<U>`: always safe to normalize — `Result` is idempotent — so a declared
124
+ type Result<R> = IsAny<R> extends true ? RpcPromise$1<unknown> :
125
+ // `RpcPromise<U>`: always safe to normalize — `Result` is idempotent — so a declared
113
126
  // `RpcPromise<U>` return produces the same type as a `Promise<U>` return.
114
- : R extends PromiseLike<unknown> & StubBase<infer U> ? Result<U> // Bare stubs: elide per `ElideStub` above. Note there is no `RpcCompatible<R>` re-check
127
+ R extends PromiseLike<unknown> & StubBase<infer U> ? Result<U> :
128
+ // Bare stubs: elide per `ElideStub` above. Note there is no `RpcCompatible<R>` re-check
115
129
  // here: anything matching our `StubBase` was produced by machinery that already enforced
116
130
  // the constraint, and re-evaluating `RpcCompatible<R>` in this arm recurses through its
117
131
  // `Stub<Stubable>` member back into `Result`, tripping TS2615 circularity errors in mapped
118
132
  // types.
119
- : R extends StubBase<any> ? RpcPromise$1<ElideStub<R>> : R extends RpcCompatible<R> ? RpcPromise$1<R> : never;
120
- type IsAny<T> = 0 extends (1 & T) ? true : false; // Type for method or property on an RPC interface.
133
+ R extends StubBase<any> ? RpcPromise$1<ElideStub<R>> : R extends RpcCompatible<R> ? RpcPromise$1<R> : never;
134
+ type IsAny<T> = 0 extends (1 & T) ? true : false;
135
+ // Type for method or property on an RPC interface.
121
136
  // For methods, unwrap `Stub`s in parameters, and rewrite returns to be `Result`s.
122
137
  // Unwrapping `Stub`s allows calling with `Stubable` arguments.
123
138
  // For properties, rewrite types to be `Result`s.
124
139
  // In each case, unwrap `Promise`s.
125
- type MethodOrProperty<V> = V extends ((...args: infer P) => infer R) ? (...args: UnstubifyAll<P>) => Result<Awaited<R>> : Result<Awaited<V>>; // Type for the callable part of an `Provider` if `T` is callable.
140
+ type MethodOrProperty<V> = V extends ((...args: infer P) => infer R) ? (...args: UnstubifyAll<P>) => Result<Awaited<R>> : Result<Awaited<V>>;
141
+ // Type for the callable part of an `Provider` if `T` is callable.
126
142
  // This is intersected with methods/properties.
127
143
  type MaybeCallableProvider<T> = T extends ((...args: any[]) => any) ? MethodOrProperty<T> : unknown;
128
144
  type TupleIndexKeys<T extends ReadonlyArray<unknown>> = Extract<keyof T, `${number}`>;
129
- type MapCallbackValue<T> = // `Omit` removes call signatures, so re-intersect callable provider behavior.
145
+ type MapCallbackValue<T> =
146
+ // `Omit` removes call signatures, so re-intersect callable provider behavior.
130
147
  T extends unknown ? Omit<Result<T>, keyof Promise<unknown>> & MaybeCallableProvider<T> & MapValuePlaceholder<T> : never;
131
148
  type InvalidNativePromiseInMapResult<T, Seen = never> = T extends unknown ? InvalidNativePromiseInMapResultImpl<T, Seen> : never;
132
- type InvalidNativePromiseInMapResultImpl<T, Seen> = [T] extends [Seen] ? never // RpcPromise is modeled as Promise & StubBase, so allow promise-like stub values.
133
- : T extends StubBase<any> ? never // Native thenables cannot be represented in map recordings, even when typed as PromiseLike.
134
- : T extends PromiseLike<unknown> ? T : T extends Map<infer K, infer V> ? InvalidNativePromiseInMapResult<K, Seen | T> | InvalidNativePromiseInMapResult<V, Seen | T> : T extends Set<infer V> ? InvalidNativePromiseInMapResult<V, Seen | T> : T extends readonly [] ? never : T extends readonly [infer Head, ...infer Tail] ? InvalidNativePromiseInMapResult<Head, Seen | T> | InvalidNativePromiseInMapResult<Tail[number], Seen | T> : T extends ReadonlyArray<infer V> ? InvalidNativePromiseInMapResult<V, Seen | T> : T extends {
149
+ type InvalidNativePromiseInMapResultImpl<T, Seen> = [T] extends [Seen] ? never :
150
+ // RpcPromise is modeled as Promise & StubBase, so allow promise-like stub values.
151
+ T extends StubBase<any> ? never :
152
+ // Native thenables cannot be represented in map recordings, even when typed as PromiseLike.
153
+ T extends PromiseLike<unknown> ? T : T extends Map<infer K, infer V> ? InvalidNativePromiseInMapResult<K, Seen | T> | InvalidNativePromiseInMapResult<V, Seen | T> : T extends Set<infer V> ? InvalidNativePromiseInMapResult<V, Seen | T> : T extends readonly [] ? never : T extends readonly [infer Head, ...infer Tail] ? InvalidNativePromiseInMapResult<Head, Seen | T> | InvalidNativePromiseInMapResult<Tail[number], Seen | T> : T extends ReadonlyArray<infer V> ? InvalidNativePromiseInMapResult<V, Seen | T> : T extends {
135
154
  [key: string | number]: unknown;
136
155
  } ? InvalidNativePromiseInMapResult<T[Extract<keyof T, string | number>], Seen | T> : never;
137
156
  type MapCallbackReturn<T> = InvalidNativePromiseInMapResult<T> extends never ? T : never;
138
- type ArrayProvider<E> = { [K in number]: MethodOrProperty<E> } & {
157
+ type ArrayProvider<E> = { [K in number]: MethodOrProperty<E>; } & {
139
158
  map<V>(callback: (elem: MapCallbackValue<E>) => MapCallbackReturn<V>): Result<Array<V>>;
140
159
  };
141
- type TupleProvider<T extends ReadonlyArray<unknown>> = { [K in TupleIndexKeys<T>]: MethodOrProperty<T[K]> } & ArrayProvider<T[number]>; // Base type for all other types providing RPC-like interfaces.
160
+ type TupleProvider<T extends ReadonlyArray<unknown>> = { [K in TupleIndexKeys<T>]: MethodOrProperty<T[K]>; } & ArrayProvider<T[number]>;
161
+ // Base type for all other types providing RPC-like interfaces.
142
162
  // Rewrites all methods/properties to be `MethodOrProperty`s, while preserving callable types.
143
163
  // `__RPC_TARGET_BRAND` deliberately flows through the key mapping (as a `never` property) so
144
164
  // stubs of branded targets stay assignable to workers-types' `Stubable`. Such stubs match our
145
165
  // `Stubable` too — harmless, since all machinery checks `StubBase` before `Stubable`.
146
- type Provider<T> = MaybeCallableProvider<T> & (T extends ReadonlyArray<unknown> ? number extends T["length"] ? ArrayProvider<T[number]> : TupleProvider<T> : Pick<{ [K in keyof T]: MethodOrProperty<T[K]> }, Exclude<keyof T, symbol | keyof StubBase<never>>> & {
166
+ type Provider<T> = MaybeCallableProvider<T> & (T extends ReadonlyArray<unknown> ? number extends T["length"] ? ArrayProvider<T[number]> : TupleProvider<T> : Pick<{ [K in keyof T]: MethodOrProperty<T[K]>; }, Exclude<keyof T, symbol | keyof StubBase<never>>> & {
147
167
  map<V>(callback: (value: MapCallbackValue<NonNullable<T>>) => MapCallbackReturn<V>): Result<Array<V>>;
148
168
  });
149
169
  //#endregion