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