@vastblast/capnweb 0.4.2 → 0.4.3
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.d.cts +58 -77
- package/dist/index.d.ts +58 -77
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -16,9 +16,6 @@ interface RpcTargetBranded {
|
|
|
16
16
|
[__RPC_TARGET_BRAND]: never;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
// Recursive type transforms are bounded to avoid runaway instantiation.
|
|
20
|
-
type RpcDepth = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
|
|
21
|
-
type NextDepth<D extends unknown[]> = D extends [unknown, ...infer Rest] ? Rest : [];
|
|
22
19
|
type IsAny<T> = 0 extends (1 & T) ? true : false;
|
|
23
20
|
type IsUnknown<T> = unknown extends T ? ([T] extends [unknown] ? true : false) : false;
|
|
24
21
|
|
|
@@ -73,54 +70,48 @@ interface StubBase<T = unknown> extends Disposable {
|
|
|
73
70
|
// The reason for using a generic type here is to build a serializable subset of structured
|
|
74
71
|
// cloneable composite types. This allows types defined with the "interface" keyword to pass the
|
|
75
72
|
// serializable check as well. Otherwise, only types defined with the "type" keyword would pass.
|
|
76
|
-
type RpcCompatible<T
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
// Special types
|
|
96
|
-
| Stub<Stubable>
|
|
97
|
-
// Serialized as stubs, see `Stubify`
|
|
98
|
-
| Stubable;
|
|
73
|
+
type RpcCompatible<T> =
|
|
74
|
+
// Structured cloneables
|
|
75
|
+
| BaseType
|
|
76
|
+
// Structured cloneable composites
|
|
77
|
+
| Map<
|
|
78
|
+
T extends Map<infer U, unknown> ? RpcCompatible<U> : never,
|
|
79
|
+
T extends Map<unknown, infer U> ? RpcCompatible<U> : never
|
|
80
|
+
>
|
|
81
|
+
| Set<T extends Set<infer U> ? RpcCompatible<U> : never>
|
|
82
|
+
| Array<T extends Array<infer U> ? RpcCompatible<U> : never>
|
|
83
|
+
| ReadonlyArray<T extends ReadonlyArray<infer U> ? RpcCompatible<U> : never>
|
|
84
|
+
| {
|
|
85
|
+
[K in keyof T as K extends string | number ? K : never]: RpcCompatible<T[K]>;
|
|
86
|
+
}
|
|
87
|
+
| Promise<T extends Promise<infer U> ? RpcCompatible<U> : never>
|
|
88
|
+
// Special types
|
|
89
|
+
| Stub<Stubable>
|
|
90
|
+
// Serialized as stubs, see `Stubify`
|
|
91
|
+
| Stubable;
|
|
99
92
|
|
|
100
93
|
type Stub<T extends RpcCompatible<T>> =
|
|
101
94
|
T extends object ? Provider<T> & StubBase<T> : StubBase<T>;
|
|
102
95
|
|
|
103
96
|
// Recursively rewrite all `Stubable` types with `Stub`s, and resolve promises.
|
|
104
97
|
// prettier-ignore
|
|
105
|
-
type Stubify<T
|
|
106
|
-
|
|
107
|
-
: T extends
|
|
108
|
-
: T extends Promise<infer U> ? Stubify<U, NextDepth<D>>
|
|
98
|
+
type Stubify<T> =
|
|
99
|
+
T extends Stubable ? Stub<T>
|
|
100
|
+
: T extends Promise<infer U> ? Stubify<U>
|
|
109
101
|
: T extends StubBase<any> ? T
|
|
110
|
-
: T extends Map<infer K, infer V> ? Map<Stubify<K
|
|
111
|
-
: T extends Set<infer V> ? Set<Stubify<V
|
|
102
|
+
: T extends Map<infer K, infer V> ? Map<Stubify<K>, Stubify<V>>
|
|
103
|
+
: T extends Set<infer V> ? Set<Stubify<V>>
|
|
112
104
|
: T extends [] ? []
|
|
113
|
-
: T extends [infer Head, ...infer Tail] ? [Stubify<Head
|
|
105
|
+
: T extends [infer Head, ...infer Tail] ? [Stubify<Head>, ...Stubify<Tail>]
|
|
114
106
|
: T extends readonly [] ? readonly []
|
|
115
|
-
: T extends readonly [infer Head, ...infer Tail] ? readonly [Stubify<Head
|
|
116
|
-
: T extends Array<infer V> ? Array<Stubify<V
|
|
117
|
-
: T extends ReadonlyArray<infer V> ? ReadonlyArray<Stubify<V
|
|
107
|
+
: T extends readonly [infer Head, ...infer Tail] ? readonly [Stubify<Head>, ...Stubify<Tail>]
|
|
108
|
+
: T extends Array<infer V> ? Array<Stubify<V>>
|
|
109
|
+
: T extends ReadonlyArray<infer V> ? ReadonlyArray<Stubify<V>>
|
|
118
110
|
: T extends BaseType ? T
|
|
119
111
|
// When using "unknown" instead of "any", interfaces are not stubified.
|
|
120
112
|
: T extends { [key: string | number]: any }
|
|
121
113
|
? {
|
|
122
|
-
[K in keyof T as K extends string | number ? K : never]:
|
|
123
|
-
Stubify<T[K], NextDepth<D>>;
|
|
114
|
+
[K in keyof T as K extends string | number ? K : never]: Stubify<T[K]>;
|
|
124
115
|
}
|
|
125
116
|
: T;
|
|
126
117
|
|
|
@@ -128,36 +119,31 @@ type Stubify<T, D extends unknown[] = RpcDepth> =
|
|
|
128
119
|
// Note we use `StubBase` instead of `Stub` here to avoid circular dependencies:
|
|
129
120
|
// `Stub` depends on `Provider`, which depends on `Unstubify`, which would depend on `Stub`.
|
|
130
121
|
// prettier-ignore
|
|
131
|
-
type UnstubifyInner<T
|
|
132
|
-
D extends [] ? T
|
|
122
|
+
type UnstubifyInner<T> =
|
|
133
123
|
// Preserve local RpcTarget acceptance, but avoid needless `Stub | Value` unions when the stub
|
|
134
124
|
// is already assignable to the value type (important for callback contextual typing).
|
|
135
|
-
|
|
136
|
-
: T extends Promise<infer U> ? UnstubifyInner<U
|
|
137
|
-
: T extends Map<infer K, infer V> ? Map<Unstubify<K
|
|
138
|
-
: T extends Set<infer V> ? Set<Unstubify<V
|
|
125
|
+
T extends StubBase<infer V> ? (T extends V ? UnstubifyInner<V> : (T | UnstubifyInner<V>))
|
|
126
|
+
: T extends Promise<infer U> ? UnstubifyInner<U>
|
|
127
|
+
: T extends Map<infer K, infer V> ? Map<Unstubify<K>, Unstubify<V>>
|
|
128
|
+
: T extends Set<infer V> ? Set<Unstubify<V>>
|
|
139
129
|
: T extends [] ? []
|
|
140
|
-
: T extends [infer Head, ...infer Tail] ? [Unstubify<Head
|
|
130
|
+
: T extends [infer Head, ...infer Tail] ? [Unstubify<Head>, ...Unstubify<Tail>]
|
|
141
131
|
: T extends readonly [] ? readonly []
|
|
142
|
-
: T extends readonly [infer Head, ...infer Tail] ? readonly [Unstubify<Head
|
|
143
|
-
: T extends Array<infer V> ? Array<Unstubify<V
|
|
144
|
-
: T extends ReadonlyArray<infer V> ? ReadonlyArray<Unstubify<V
|
|
132
|
+
: T extends readonly [infer Head, ...infer Tail] ? readonly [Unstubify<Head>, ...Unstubify<Tail>]
|
|
133
|
+
: T extends Array<infer V> ? Array<Unstubify<V>>
|
|
134
|
+
: T extends ReadonlyArray<infer V> ? ReadonlyArray<Unstubify<V>>
|
|
145
135
|
: T extends BaseType ? T
|
|
146
136
|
: T extends { [key: string | number]: unknown }
|
|
147
137
|
? {
|
|
148
|
-
[K in keyof T as K extends string | number ? K : never]:
|
|
149
|
-
Unstubify<T[K], NextDepth<D>>;
|
|
138
|
+
[K in keyof T as K extends string | number ? K : never]: Unstubify<T[K]>;
|
|
150
139
|
}
|
|
151
140
|
: T;
|
|
152
141
|
|
|
153
142
|
// You can put promises anywhere in the params and they'll be resolved before delivery.
|
|
154
143
|
// (This also covers RpcPromise, because it's defined as being a Promise.)
|
|
155
|
-
type Unstubify<T
|
|
156
|
-
UnstubifyInner<T, D> | Promise<UnstubifyInner<T, D>>;
|
|
144
|
+
type Unstubify<T> = UnstubifyInner<T> | Promise<UnstubifyInner<T>>;
|
|
157
145
|
|
|
158
|
-
type UnstubifyAll<A extends readonly unknown[]
|
|
159
|
-
[I in keyof A]: Unstubify<A[I], D>;
|
|
160
|
-
};
|
|
146
|
+
type UnstubifyAll<A extends readonly unknown[]> = { [I in keyof A]: Unstubify<A[I]> };
|
|
161
147
|
|
|
162
148
|
// Utility type for adding `Disposable`s to `object` types only.
|
|
163
149
|
// Note `unknown & T` is equivalent to `T`.
|
|
@@ -170,18 +156,14 @@ type MaybeDisposable<T> = T extends object ? Disposable : unknown;
|
|
|
170
156
|
// Everything else can't be passed over RPC.
|
|
171
157
|
// Technically, we use custom thenables here, but they quack like `Promise`s.
|
|
172
158
|
// Intersecting with `(Maybe)Provider` allows pipelining.
|
|
173
|
-
|
|
174
|
-
type UnknownResult<D extends unknown[]> = D extends []
|
|
175
|
-
? Promise<unknown> & StubBase<unknown>
|
|
176
|
-
: Promise<unknown> & Provider<unknown, D> & StubBase<unknown>;
|
|
159
|
+
type UnknownResult = Promise<unknown> & Provider<unknown> & StubBase<unknown>;
|
|
177
160
|
|
|
178
161
|
// prettier-ignore
|
|
179
|
-
type Result<R
|
|
180
|
-
|
|
181
|
-
:
|
|
182
|
-
:
|
|
183
|
-
: R extends
|
|
184
|
-
: R extends RpcCompatible<R, D> ? Promise<Stubify<R, D> & MaybeDisposable<R>> & Provider<R, D> & StubBase<R>
|
|
162
|
+
type Result<R> =
|
|
163
|
+
IsAny<R> extends true ? UnknownResult
|
|
164
|
+
: IsUnknown<R> extends true ? UnknownResult
|
|
165
|
+
: R extends Stubable ? Promise<Stub<R>> & Provider<R> & StubBase<R>
|
|
166
|
+
: R extends RpcCompatible<R> ? Promise<Stubify<R> & MaybeDisposable<R>> & Provider<R> & StubBase<R>
|
|
185
167
|
: never;
|
|
186
168
|
|
|
187
169
|
type HasAnyFunctionParts<P extends readonly unknown[], R> =
|
|
@@ -192,35 +174,34 @@ type HasAnyFunctionParts<P extends readonly unknown[], R> =
|
|
|
192
174
|
// Unwrapping `Stub`s allows calling with `Stubable` arguments.
|
|
193
175
|
// For properties, rewrite types to be `Result`s.
|
|
194
176
|
// In each case, unwrap `Promise`s.
|
|
195
|
-
type MethodOrProperty<V
|
|
177
|
+
type MethodOrProperty<V> = V extends (...args: infer P) => infer R
|
|
196
178
|
? HasAnyFunctionParts<P, R> extends true
|
|
197
|
-
? (...args: unknown[]) => UnknownResult
|
|
198
|
-
: (...args: UnstubifyAll<P
|
|
199
|
-
: Result<Awaited<V
|
|
179
|
+
? (...args: unknown[]) => UnknownResult
|
|
180
|
+
: (...args: UnstubifyAll<P>) => Result<Awaited<R>>
|
|
181
|
+
: Result<Awaited<V>>;
|
|
200
182
|
|
|
201
183
|
// Type for the callable part of a `Provider` if `T` is callable.
|
|
202
184
|
// This is intersected with methods/properties.
|
|
203
|
-
type MaybeCallableProvider<T
|
|
204
|
-
? MethodOrProperty<T
|
|
185
|
+
type MaybeCallableProvider<T> = T extends (...args: any[]) => any
|
|
186
|
+
? MethodOrProperty<T>
|
|
205
187
|
: unknown;
|
|
206
188
|
|
|
207
189
|
// Base type for all other types providing RPC-like interfaces.
|
|
208
190
|
// Rewrites all methods/properties to be `MethodOrProperty`s, while preserving callable types.
|
|
209
|
-
type Provider<T
|
|
191
|
+
type Provider<T> = MaybeCallableProvider<T> &
|
|
210
192
|
(T extends Array<infer U>
|
|
211
193
|
? {
|
|
212
|
-
[key: number]: MethodOrProperty<U
|
|
194
|
+
[key: number]: MethodOrProperty<U>;
|
|
213
195
|
} & {
|
|
214
|
-
map<V>(callback: (elem: Result<U
|
|
196
|
+
map<V>(callback: (elem: Result<U>) => V): Result<Array<V>>;
|
|
215
197
|
}
|
|
216
198
|
: {
|
|
217
199
|
[K in Exclude<
|
|
218
200
|
keyof T,
|
|
219
201
|
symbol | keyof StubBase<never>
|
|
220
|
-
>]: MethodOrProperty<T[K]
|
|
202
|
+
>]: MethodOrProperty<T[K]>;
|
|
221
203
|
} & {
|
|
222
|
-
map<V>(callback: (value: Result<NonNullable<T
|
|
223
|
-
Result<Array<V>, NextDepth<D>>;
|
|
204
|
+
map<V>(callback: (value: Result<NonNullable<T>>) => V): Result<Array<V>>;
|
|
224
205
|
});
|
|
225
206
|
|
|
226
207
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -16,9 +16,6 @@ interface RpcTargetBranded {
|
|
|
16
16
|
[__RPC_TARGET_BRAND]: never;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
// Recursive type transforms are bounded to avoid runaway instantiation.
|
|
20
|
-
type RpcDepth = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
|
|
21
|
-
type NextDepth<D extends unknown[]> = D extends [unknown, ...infer Rest] ? Rest : [];
|
|
22
19
|
type IsAny<T> = 0 extends (1 & T) ? true : false;
|
|
23
20
|
type IsUnknown<T> = unknown extends T ? ([T] extends [unknown] ? true : false) : false;
|
|
24
21
|
|
|
@@ -73,54 +70,48 @@ interface StubBase<T = unknown> extends Disposable {
|
|
|
73
70
|
// The reason for using a generic type here is to build a serializable subset of structured
|
|
74
71
|
// cloneable composite types. This allows types defined with the "interface" keyword to pass the
|
|
75
72
|
// serializable check as well. Otherwise, only types defined with the "type" keyword would pass.
|
|
76
|
-
type RpcCompatible<T
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
// Special types
|
|
96
|
-
| Stub<Stubable>
|
|
97
|
-
// Serialized as stubs, see `Stubify`
|
|
98
|
-
| Stubable;
|
|
73
|
+
type RpcCompatible<T> =
|
|
74
|
+
// Structured cloneables
|
|
75
|
+
| BaseType
|
|
76
|
+
// Structured cloneable composites
|
|
77
|
+
| Map<
|
|
78
|
+
T extends Map<infer U, unknown> ? RpcCompatible<U> : never,
|
|
79
|
+
T extends Map<unknown, infer U> ? RpcCompatible<U> : never
|
|
80
|
+
>
|
|
81
|
+
| Set<T extends Set<infer U> ? RpcCompatible<U> : never>
|
|
82
|
+
| Array<T extends Array<infer U> ? RpcCompatible<U> : never>
|
|
83
|
+
| ReadonlyArray<T extends ReadonlyArray<infer U> ? RpcCompatible<U> : never>
|
|
84
|
+
| {
|
|
85
|
+
[K in keyof T as K extends string | number ? K : never]: RpcCompatible<T[K]>;
|
|
86
|
+
}
|
|
87
|
+
| Promise<T extends Promise<infer U> ? RpcCompatible<U> : never>
|
|
88
|
+
// Special types
|
|
89
|
+
| Stub<Stubable>
|
|
90
|
+
// Serialized as stubs, see `Stubify`
|
|
91
|
+
| Stubable;
|
|
99
92
|
|
|
100
93
|
type Stub<T extends RpcCompatible<T>> =
|
|
101
94
|
T extends object ? Provider<T> & StubBase<T> : StubBase<T>;
|
|
102
95
|
|
|
103
96
|
// Recursively rewrite all `Stubable` types with `Stub`s, and resolve promises.
|
|
104
97
|
// prettier-ignore
|
|
105
|
-
type Stubify<T
|
|
106
|
-
|
|
107
|
-
: T extends
|
|
108
|
-
: T extends Promise<infer U> ? Stubify<U, NextDepth<D>>
|
|
98
|
+
type Stubify<T> =
|
|
99
|
+
T extends Stubable ? Stub<T>
|
|
100
|
+
: T extends Promise<infer U> ? Stubify<U>
|
|
109
101
|
: T extends StubBase<any> ? T
|
|
110
|
-
: T extends Map<infer K, infer V> ? Map<Stubify<K
|
|
111
|
-
: T extends Set<infer V> ? Set<Stubify<V
|
|
102
|
+
: T extends Map<infer K, infer V> ? Map<Stubify<K>, Stubify<V>>
|
|
103
|
+
: T extends Set<infer V> ? Set<Stubify<V>>
|
|
112
104
|
: T extends [] ? []
|
|
113
|
-
: T extends [infer Head, ...infer Tail] ? [Stubify<Head
|
|
105
|
+
: T extends [infer Head, ...infer Tail] ? [Stubify<Head>, ...Stubify<Tail>]
|
|
114
106
|
: T extends readonly [] ? readonly []
|
|
115
|
-
: T extends readonly [infer Head, ...infer Tail] ? readonly [Stubify<Head
|
|
116
|
-
: T extends Array<infer V> ? Array<Stubify<V
|
|
117
|
-
: T extends ReadonlyArray<infer V> ? ReadonlyArray<Stubify<V
|
|
107
|
+
: T extends readonly [infer Head, ...infer Tail] ? readonly [Stubify<Head>, ...Stubify<Tail>]
|
|
108
|
+
: T extends Array<infer V> ? Array<Stubify<V>>
|
|
109
|
+
: T extends ReadonlyArray<infer V> ? ReadonlyArray<Stubify<V>>
|
|
118
110
|
: T extends BaseType ? T
|
|
119
111
|
// When using "unknown" instead of "any", interfaces are not stubified.
|
|
120
112
|
: T extends { [key: string | number]: any }
|
|
121
113
|
? {
|
|
122
|
-
[K in keyof T as K extends string | number ? K : never]:
|
|
123
|
-
Stubify<T[K], NextDepth<D>>;
|
|
114
|
+
[K in keyof T as K extends string | number ? K : never]: Stubify<T[K]>;
|
|
124
115
|
}
|
|
125
116
|
: T;
|
|
126
117
|
|
|
@@ -128,36 +119,31 @@ type Stubify<T, D extends unknown[] = RpcDepth> =
|
|
|
128
119
|
// Note we use `StubBase` instead of `Stub` here to avoid circular dependencies:
|
|
129
120
|
// `Stub` depends on `Provider`, which depends on `Unstubify`, which would depend on `Stub`.
|
|
130
121
|
// prettier-ignore
|
|
131
|
-
type UnstubifyInner<T
|
|
132
|
-
D extends [] ? T
|
|
122
|
+
type UnstubifyInner<T> =
|
|
133
123
|
// Preserve local RpcTarget acceptance, but avoid needless `Stub | Value` unions when the stub
|
|
134
124
|
// is already assignable to the value type (important for callback contextual typing).
|
|
135
|
-
|
|
136
|
-
: T extends Promise<infer U> ? UnstubifyInner<U
|
|
137
|
-
: T extends Map<infer K, infer V> ? Map<Unstubify<K
|
|
138
|
-
: T extends Set<infer V> ? Set<Unstubify<V
|
|
125
|
+
T extends StubBase<infer V> ? (T extends V ? UnstubifyInner<V> : (T | UnstubifyInner<V>))
|
|
126
|
+
: T extends Promise<infer U> ? UnstubifyInner<U>
|
|
127
|
+
: T extends Map<infer K, infer V> ? Map<Unstubify<K>, Unstubify<V>>
|
|
128
|
+
: T extends Set<infer V> ? Set<Unstubify<V>>
|
|
139
129
|
: T extends [] ? []
|
|
140
|
-
: T extends [infer Head, ...infer Tail] ? [Unstubify<Head
|
|
130
|
+
: T extends [infer Head, ...infer Tail] ? [Unstubify<Head>, ...Unstubify<Tail>]
|
|
141
131
|
: T extends readonly [] ? readonly []
|
|
142
|
-
: T extends readonly [infer Head, ...infer Tail] ? readonly [Unstubify<Head
|
|
143
|
-
: T extends Array<infer V> ? Array<Unstubify<V
|
|
144
|
-
: T extends ReadonlyArray<infer V> ? ReadonlyArray<Unstubify<V
|
|
132
|
+
: T extends readonly [infer Head, ...infer Tail] ? readonly [Unstubify<Head>, ...Unstubify<Tail>]
|
|
133
|
+
: T extends Array<infer V> ? Array<Unstubify<V>>
|
|
134
|
+
: T extends ReadonlyArray<infer V> ? ReadonlyArray<Unstubify<V>>
|
|
145
135
|
: T extends BaseType ? T
|
|
146
136
|
: T extends { [key: string | number]: unknown }
|
|
147
137
|
? {
|
|
148
|
-
[K in keyof T as K extends string | number ? K : never]:
|
|
149
|
-
Unstubify<T[K], NextDepth<D>>;
|
|
138
|
+
[K in keyof T as K extends string | number ? K : never]: Unstubify<T[K]>;
|
|
150
139
|
}
|
|
151
140
|
: T;
|
|
152
141
|
|
|
153
142
|
// You can put promises anywhere in the params and they'll be resolved before delivery.
|
|
154
143
|
// (This also covers RpcPromise, because it's defined as being a Promise.)
|
|
155
|
-
type Unstubify<T
|
|
156
|
-
UnstubifyInner<T, D> | Promise<UnstubifyInner<T, D>>;
|
|
144
|
+
type Unstubify<T> = UnstubifyInner<T> | Promise<UnstubifyInner<T>>;
|
|
157
145
|
|
|
158
|
-
type UnstubifyAll<A extends readonly unknown[]
|
|
159
|
-
[I in keyof A]: Unstubify<A[I], D>;
|
|
160
|
-
};
|
|
146
|
+
type UnstubifyAll<A extends readonly unknown[]> = { [I in keyof A]: Unstubify<A[I]> };
|
|
161
147
|
|
|
162
148
|
// Utility type for adding `Disposable`s to `object` types only.
|
|
163
149
|
// Note `unknown & T` is equivalent to `T`.
|
|
@@ -170,18 +156,14 @@ type MaybeDisposable<T> = T extends object ? Disposable : unknown;
|
|
|
170
156
|
// Everything else can't be passed over RPC.
|
|
171
157
|
// Technically, we use custom thenables here, but they quack like `Promise`s.
|
|
172
158
|
// Intersecting with `(Maybe)Provider` allows pipelining.
|
|
173
|
-
|
|
174
|
-
type UnknownResult<D extends unknown[]> = D extends []
|
|
175
|
-
? Promise<unknown> & StubBase<unknown>
|
|
176
|
-
: Promise<unknown> & Provider<unknown, D> & StubBase<unknown>;
|
|
159
|
+
type UnknownResult = Promise<unknown> & Provider<unknown> & StubBase<unknown>;
|
|
177
160
|
|
|
178
161
|
// prettier-ignore
|
|
179
|
-
type Result<R
|
|
180
|
-
|
|
181
|
-
:
|
|
182
|
-
:
|
|
183
|
-
: R extends
|
|
184
|
-
: R extends RpcCompatible<R, D> ? Promise<Stubify<R, D> & MaybeDisposable<R>> & Provider<R, D> & StubBase<R>
|
|
162
|
+
type Result<R> =
|
|
163
|
+
IsAny<R> extends true ? UnknownResult
|
|
164
|
+
: IsUnknown<R> extends true ? UnknownResult
|
|
165
|
+
: R extends Stubable ? Promise<Stub<R>> & Provider<R> & StubBase<R>
|
|
166
|
+
: R extends RpcCompatible<R> ? Promise<Stubify<R> & MaybeDisposable<R>> & Provider<R> & StubBase<R>
|
|
185
167
|
: never;
|
|
186
168
|
|
|
187
169
|
type HasAnyFunctionParts<P extends readonly unknown[], R> =
|
|
@@ -192,35 +174,34 @@ type HasAnyFunctionParts<P extends readonly unknown[], R> =
|
|
|
192
174
|
// Unwrapping `Stub`s allows calling with `Stubable` arguments.
|
|
193
175
|
// For properties, rewrite types to be `Result`s.
|
|
194
176
|
// In each case, unwrap `Promise`s.
|
|
195
|
-
type MethodOrProperty<V
|
|
177
|
+
type MethodOrProperty<V> = V extends (...args: infer P) => infer R
|
|
196
178
|
? HasAnyFunctionParts<P, R> extends true
|
|
197
|
-
? (...args: unknown[]) => UnknownResult
|
|
198
|
-
: (...args: UnstubifyAll<P
|
|
199
|
-
: Result<Awaited<V
|
|
179
|
+
? (...args: unknown[]) => UnknownResult
|
|
180
|
+
: (...args: UnstubifyAll<P>) => Result<Awaited<R>>
|
|
181
|
+
: Result<Awaited<V>>;
|
|
200
182
|
|
|
201
183
|
// Type for the callable part of a `Provider` if `T` is callable.
|
|
202
184
|
// This is intersected with methods/properties.
|
|
203
|
-
type MaybeCallableProvider<T
|
|
204
|
-
? MethodOrProperty<T
|
|
185
|
+
type MaybeCallableProvider<T> = T extends (...args: any[]) => any
|
|
186
|
+
? MethodOrProperty<T>
|
|
205
187
|
: unknown;
|
|
206
188
|
|
|
207
189
|
// Base type for all other types providing RPC-like interfaces.
|
|
208
190
|
// Rewrites all methods/properties to be `MethodOrProperty`s, while preserving callable types.
|
|
209
|
-
type Provider<T
|
|
191
|
+
type Provider<T> = MaybeCallableProvider<T> &
|
|
210
192
|
(T extends Array<infer U>
|
|
211
193
|
? {
|
|
212
|
-
[key: number]: MethodOrProperty<U
|
|
194
|
+
[key: number]: MethodOrProperty<U>;
|
|
213
195
|
} & {
|
|
214
|
-
map<V>(callback: (elem: Result<U
|
|
196
|
+
map<V>(callback: (elem: Result<U>) => V): Result<Array<V>>;
|
|
215
197
|
}
|
|
216
198
|
: {
|
|
217
199
|
[K in Exclude<
|
|
218
200
|
keyof T,
|
|
219
201
|
symbol | keyof StubBase<never>
|
|
220
|
-
>]: MethodOrProperty<T[K]
|
|
202
|
+
>]: MethodOrProperty<T[K]>;
|
|
221
203
|
} & {
|
|
222
|
-
map<V>(callback: (value: Result<NonNullable<T
|
|
223
|
-
Result<Array<V>, NextDepth<D>>;
|
|
204
|
+
map<V>(callback: (value: Result<NonNullable<T>>) => V): Result<Array<V>>;
|
|
224
205
|
});
|
|
225
206
|
|
|
226
207
|
/**
|