capnweb 0.11.1 → 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 -738
- package/dist/index-bun.cjs +370 -141
- package/dist/index-bun.cjs.map +1 -1
- package/dist/index-bun.d.cts +101 -32
- package/dist/index-bun.d.ts +101 -32
- package/dist/index-bun.js +370 -140
- package/dist/index-bun.js.map +1 -1
- package/dist/index-workers.cjs +371 -141
- package/dist/index-workers.cjs.map +1 -1
- package/dist/index-workers.d.cts +96 -30
- package/dist/index-workers.d.ts +96 -30
- package/dist/index-workers.js +370 -140
- package/dist/index-workers.js.map +1 -1
- package/dist/index.cjs +370 -141
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +96 -30
- package/dist/index.d.ts +96 -30
- package/dist/index.js +370 -140
- package/dist/index.js.map +1 -1
- package/package.json +22 -19
package/dist/index.d.cts
CHANGED
|
@@ -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:
|
|
@@ -20,16 +19,23 @@ interface RpcTargetBranded {
|
|
|
20
19
|
// `never[]` preserves compatibility with strongly-typed function signatures without introducing
|
|
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).
|
|
23
|
+
type IsUnknown<T> = unknown extends T ? true : false;
|
|
24
|
+
// Types that can be passed over RPC
|
|
24
25
|
// The reason for using a generic type here is to build the serializable subset of RPC-compatible
|
|
25
26
|
// composite types. This allows types defined with the "interface" keyword to pass the
|
|
26
27
|
// serializable check as well. Otherwise, only types defined with the "type" keyword would pass.
|
|
27
|
-
type RpcCompatible<T> =
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
|
32
|
-
|
|
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;
|
|
33
39
|
// Base type for all RPC stubs, including common memory management methods.
|
|
34
40
|
// `T` is used as a marker type for unwrapping `Stub`s later.
|
|
35
41
|
interface StubBase<T = unknown> extends Disposable {
|
|
@@ -38,24 +44,35 @@ interface StubBase<T = unknown> extends Disposable {
|
|
|
38
44
|
onRpcBroken(callback: (error: any) => void): void;
|
|
39
45
|
}
|
|
40
46
|
type Stub<T extends RpcCompatible<T>> = T extends object ? Provider<T> & StubBase<T> : StubBase<T>;
|
|
41
|
-
type TypedArray = Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array | Int8Array | Int16Array | Int32Array | BigUint64Array | BigInt64Array | Float32Array | Float64Array;
|
|
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
|
|
42
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
|
|
43
|
-
| WritableStream<any> // Chunk type can be any RPC-compatible type
|
|
44
|
-
| URL | Request | Response | Headers;
|
|
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.
|
|
53
|
+
// Arm ordering matters here:
|
|
54
|
+
// - `Promise` must come before `StubBase`: `RpcPromise<T>` matches both, and must resolve
|
|
55
|
+
// through the Promise arm rather than pass through as-is.
|
|
56
|
+
// - `StubBase` must come before `Stubable`: `Stub<T>` of a callable `T` is itself callable, so
|
|
57
|
+
// it matches `Stubable`. Checking `StubBase` first keeps existing stubs as-is instead of
|
|
58
|
+
// double-wrapping them as `Stub<Stub<T>>`.
|
|
45
59
|
// prettier-ignore
|
|
46
|
-
type Stubify<T> = T extends
|
|
47
|
-
|
|
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 {
|
|
48
63
|
[key: string | number]: any;
|
|
49
|
-
} ? { [K in keyof T as K extends string | number ? K : never]: Stubify<T[K]
|
|
64
|
+
} ? { [K in keyof T as K extends string | number ? K : never]: Stubify<T[K]>; } : T;
|
|
50
65
|
// Recursively rewrite all `Stub<T>`s with the corresponding `T`s.
|
|
51
66
|
// Note we use `StubBase` instead of `Stub` here to avoid circular dependencies:
|
|
52
67
|
// `Stub` depends on `Provider`, which depends on `Unstubify`, which would depend on `Stub`.
|
|
53
68
|
// prettier-ignore
|
|
54
|
-
type UnstubifyInner<T> =
|
|
69
|
+
type UnstubifyInner<T> =
|
|
70
|
+
// Preserve local RpcTarget acceptance, but avoid needless `Stub | Value` unions when the stub
|
|
55
71
|
// is already assignable to the value type (important for callback contextual typing).
|
|
56
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 {
|
|
57
73
|
[key: string | number]: unknown;
|
|
58
|
-
} ? { [K in keyof T as K extends string | number ? K : never]: Unstubify<T[K]
|
|
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.
|
|
59
76
|
// (This also covers RpcPromise, because it's defined as being a Promise.)
|
|
60
77
|
// Map placeholders are also allowed so primitive map callback inputs can be forwarded directly
|
|
61
78
|
// into RPC params.
|
|
@@ -64,18 +81,38 @@ T extends StubBase<infer V> ? (T extends V ? UnstubifyInner<V> : (T | UnstubifyI
|
|
|
64
81
|
// Remove stub members from mixed unions so callback params don’t get both stub and unstubbed signatures.
|
|
65
82
|
// Marker carried by map() callback inputs. This lets primitive placeholders flow through params.
|
|
66
83
|
type Unstubify<T> = NonStubMembers<T> | UnstubifyInner<T> | Promise<UnstubifyInner<T>> | MapValuePlaceholder<UnstubifyInner<T>>;
|
|
67
|
-
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]>; };
|
|
68
85
|
interface MapValuePlaceholder<T> {
|
|
69
86
|
[__RPC_MAP_VALUE_BRAND]: T;
|
|
70
87
|
}
|
|
71
|
-
type NonStubMembers<T> = Exclude<T, StubBase<any>>;
|
|
88
|
+
type NonStubMembers<T> = Exclude<T, StubBase<any>>;
|
|
89
|
+
// Utility type for adding `Disposable`s to `object` types only.
|
|
72
90
|
// Note `unknown & T` is equivalent to `T`.
|
|
73
|
-
type MaybeDisposable<T> = T extends object ? Disposable : unknown;
|
|
91
|
+
type MaybeDisposable<T> = T extends object ? Disposable : unknown;
|
|
92
|
+
// The type behind the public `RpcPromise` (re-exported with its `RpcCompatible` constraint from
|
|
74
93
|
// index.ts). Defined here so that `Result`'s arms below resolve to the exact same alias
|
|
75
94
|
// instantiation: when both sides of an assignability check are the same alias applied to the
|
|
76
95
|
// same type arguments, the checker short-circuits on identity instead of recursing through the
|
|
77
96
|
// full structural comparison of these mutually-recursive types.
|
|
78
97
|
type RpcPromise$1<T> = T extends Stubable ? Promise<Stub<T>> & Provider<T> & StubBase<T> : Promise<Stubify<T> & MaybeDisposable<T>> & Provider<T> & StubBase<T>;
|
|
98
|
+
// The elision `Result` applies to a bare stub type: unwrap `Stub<T>` back to `T` when the
|
|
99
|
+
// payload is `Stubable`. Such stubs await back to a stub either way, so eliding keeps a
|
|
100
|
+
// declared `Promise<RpcStub<T>>` return interchangeable with a `Promise<T>` return.
|
|
101
|
+
// Plain-interface stubs are NOT elided: `RpcPromise<U>` only awaits to `Stub<U>` when
|
|
102
|
+
// `U extends Stubable`, so eliding those would change the awaited type from a stub to a
|
|
103
|
+
// stubified record. The payload check is deliberately non-distributive (`[U] extends [...]`).
|
|
104
|
+
// `Stub<any>` is not elided either: `[any] extends [Stubable]` is true, so without the `IsAny`
|
|
105
|
+
// guard an `any`-payload stub would lose its stub surface.
|
|
106
|
+
// Also used by the `RpcPromise` constructor signature (index.ts), which applies exactly this
|
|
107
|
+
// transformation so constructing from a promised stub matches the method-return type.
|
|
108
|
+
type ElideStub<T> = T extends StubBase<infer U> ? (IsAny<U> extends true ? T : [U] extends [Stubable] ? U : T) : T;
|
|
109
|
+
// What the promise given to `new RpcPromise<T>(...)` may resolve to: the payload itself, or —
|
|
110
|
+
// for stubable payloads — a stub of it. `NoInfer` keeps the stub arm out of inference, so an
|
|
111
|
+
// inferred `T` is always the promise's own resolution type; the arm only matters when `T` is
|
|
112
|
+
// explicitly annotated (`new RpcPromise<Counter>(promiseOfStub)`). Stubs of non-stubable
|
|
113
|
+
// payloads are deliberately rejected: `ElideStub` wouldn't elide those, so accepting one would
|
|
114
|
+
// claim the promise awaits to a stubified record while the runtime resolves to a stub.
|
|
115
|
+
type PayloadOrStub<T> = T | NoInfer<Stub<Extract<T, Stubable>>>;
|
|
79
116
|
// Type for method return or property on an RPC interface.
|
|
80
117
|
// - Stubable types are replaced by stubs.
|
|
81
118
|
// - RpcCompatible types are passed by value, with stubable types replaced by stubs
|
|
@@ -84,32 +121,49 @@ type RpcPromise$1<T> = T extends Stubable ? Promise<Stub<T>> & Provider<T> & Stu
|
|
|
84
121
|
// Technically, we use custom thenables here, but they quack like `Promise`s.
|
|
85
122
|
// Intersecting with `(Maybe)Provider` allows pipelining.
|
|
86
123
|
// prettier-ignore
|
|
87
|
-
type Result<R> = IsAny<R> extends true ?
|
|
124
|
+
type Result<R> = IsAny<R> extends true ? RpcPromise$1<unknown> :
|
|
125
|
+
// `RpcPromise<U>`: always safe to normalize — `Result` is idempotent — so a declared
|
|
126
|
+
// `RpcPromise<U>` return produces the same type as a `Promise<U>` return.
|
|
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
|
|
129
|
+
// here: anything matching our `StubBase` was produced by machinery that already enforced
|
|
130
|
+
// the constraint, and re-evaluating `RpcCompatible<R>` in this arm recurses through its
|
|
131
|
+
// `Stub<Stubable>` member back into `Result`, tripping TS2615 circularity errors in mapped
|
|
132
|
+
// types.
|
|
133
|
+
R extends StubBase<any> ? RpcPromise$1<ElideStub<R>> : R extends RpcCompatible<R> ? RpcPromise$1<R> : never;
|
|
88
134
|
type IsAny<T> = 0 extends (1 & T) ? true : false;
|
|
89
|
-
|
|
135
|
+
// Type for method or property on an RPC interface.
|
|
90
136
|
// For methods, unwrap `Stub`s in parameters, and rewrite returns to be `Result`s.
|
|
91
137
|
// Unwrapping `Stub`s allows calling with `Stubable` arguments.
|
|
92
138
|
// For properties, rewrite types to be `Result`s.
|
|
93
139
|
// In each case, unwrap `Promise`s.
|
|
94
|
-
type MethodOrProperty<V> = V extends ((...args: infer P) => infer R) ? (...args: UnstubifyAll<P>) =>
|
|
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.
|
|
95
142
|
// This is intersected with methods/properties.
|
|
96
143
|
type MaybeCallableProvider<T> = T extends ((...args: any[]) => any) ? MethodOrProperty<T> : unknown;
|
|
97
144
|
type TupleIndexKeys<T extends ReadonlyArray<unknown>> = Extract<keyof T, `${number}`>;
|
|
98
|
-
type MapCallbackValue<T> =
|
|
145
|
+
type MapCallbackValue<T> =
|
|
146
|
+
// `Omit` removes call signatures, so re-intersect callable provider behavior.
|
|
99
147
|
T extends unknown ? Omit<Result<T>, keyof Promise<unknown>> & MaybeCallableProvider<T> & MapValuePlaceholder<T> : never;
|
|
100
148
|
type InvalidNativePromiseInMapResult<T, Seen = never> = T extends unknown ? InvalidNativePromiseInMapResultImpl<T, Seen> : never;
|
|
101
|
-
type InvalidNativePromiseInMapResultImpl<T, Seen> = [T] extends [Seen] ? never
|
|
102
|
-
|
|
103
|
-
|
|
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 {
|
|
104
154
|
[key: string | number]: unknown;
|
|
105
155
|
} ? InvalidNativePromiseInMapResult<T[Extract<keyof T, string | number>], Seen | T> : never;
|
|
106
156
|
type MapCallbackReturn<T> = InvalidNativePromiseInMapResult<T> extends never ? T : never;
|
|
107
|
-
type ArrayProvider<E> = { [K in number]: MethodOrProperty<E
|
|
157
|
+
type ArrayProvider<E> = { [K in number]: MethodOrProperty<E>; } & {
|
|
108
158
|
map<V>(callback: (elem: MapCallbackValue<E>) => MapCallbackReturn<V>): Result<Array<V>>;
|
|
109
159
|
};
|
|
110
|
-
type TupleProvider<T extends ReadonlyArray<unknown>> = { [K in TupleIndexKeys<T>]: MethodOrProperty<T[K]
|
|
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.
|
|
111
162
|
// Rewrites all methods/properties to be `MethodOrProperty`s, while preserving callable types.
|
|
112
|
-
|
|
163
|
+
// `__RPC_TARGET_BRAND` deliberately flows through the key mapping (as a `never` property) so
|
|
164
|
+
// stubs of branded targets stay assignable to workers-types' `Stubable`. Such stubs match our
|
|
165
|
+
// `Stubable` too — harmless, since all machinery checks `StubBase` before `Stubable`.
|
|
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>>> & {
|
|
113
167
|
map<V>(callback: (value: MapCallbackValue<NonNullable<T>>) => MapCallbackReturn<V>): Result<Array<V>>;
|
|
114
168
|
});
|
|
115
169
|
//#endregion
|
|
@@ -327,9 +381,21 @@ declare const RpcStub: {
|
|
|
327
381
|
* until you actually `await` the promise (or call `then()`, etc. on it). This is an optimization:
|
|
328
382
|
* if you only intend to use the promise for pipelining and you never await it, then there's no
|
|
329
383
|
* need to transmit the resolution!
|
|
384
|
+
*
|
|
385
|
+
* You may also construct an `RpcPromise` yourself from a regular `Promise`, using
|
|
386
|
+
* `new RpcPromise(promise)`, allowing you to perform promise pipelining on a local promise. This
|
|
387
|
+
* is semantically identical to creating a local-loopback RPC that returns the promise, and then
|
|
388
|
+
* invoking it: pipelined calls wait until the promise resolves, then are delivered, in order, to
|
|
389
|
+
* the resolution. This is useful when you plan to obtain some stub in the future, but want to
|
|
390
|
+
* allow code to start queuing calls on it immediately. Note that the `RpcPromise` takes
|
|
391
|
+
* ownership of the resolution: disposing it disposes the resolution, so resolve the promise
|
|
392
|
+
* with a `dup()` if you also intend to keep the stub.
|
|
330
393
|
*/
|
|
331
394
|
type RpcPromise<T extends RpcCompatible<T>> = RpcPromise$1<T>;
|
|
332
|
-
declare const RpcPromise: {
|
|
395
|
+
declare const RpcPromise: {
|
|
396
|
+
new <T extends RpcCompatible<T>>(value: Promise<T>): RpcPromise$1<ElideStub<T>>;
|
|
397
|
+
new <T extends RpcCompatible<T>>(value: Promise<PayloadOrStub<T>>): RpcPromise$1<ElideStub<T>>;
|
|
398
|
+
};
|
|
333
399
|
/**
|
|
334
400
|
* Use to construct an `RpcSession` on top of a custom `RpcTransport`.
|
|
335
401
|
*
|
package/dist/index.d.ts
CHANGED
|
@@ -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:
|
|
@@ -20,16 +19,23 @@ interface RpcTargetBranded {
|
|
|
20
19
|
// `never[]` preserves compatibility with strongly-typed function signatures without introducing
|
|
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).
|
|
23
|
+
type IsUnknown<T> = unknown extends T ? true : false;
|
|
24
|
+
// Types that can be passed over RPC
|
|
24
25
|
// The reason for using a generic type here is to build the serializable subset of RPC-compatible
|
|
25
26
|
// composite types. This allows types defined with the "interface" keyword to pass the
|
|
26
27
|
// serializable check as well. Otherwise, only types defined with the "type" keyword would pass.
|
|
27
|
-
type RpcCompatible<T> =
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
|
32
|
-
|
|
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;
|
|
33
39
|
// Base type for all RPC stubs, including common memory management methods.
|
|
34
40
|
// `T` is used as a marker type for unwrapping `Stub`s later.
|
|
35
41
|
interface StubBase<T = unknown> extends Disposable {
|
|
@@ -38,24 +44,35 @@ interface StubBase<T = unknown> extends Disposable {
|
|
|
38
44
|
onRpcBroken(callback: (error: any) => void): void;
|
|
39
45
|
}
|
|
40
46
|
type Stub<T extends RpcCompatible<T>> = T extends object ? Provider<T> & StubBase<T> : StubBase<T>;
|
|
41
|
-
type TypedArray = Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array | Int8Array | Int16Array | Int32Array | BigUint64Array | BigInt64Array | Float32Array | Float64Array;
|
|
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
|
|
42
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
|
|
43
|
-
| WritableStream<any> // Chunk type can be any RPC-compatible type
|
|
44
|
-
| URL | Request | Response | Headers;
|
|
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.
|
|
53
|
+
// Arm ordering matters here:
|
|
54
|
+
// - `Promise` must come before `StubBase`: `RpcPromise<T>` matches both, and must resolve
|
|
55
|
+
// through the Promise arm rather than pass through as-is.
|
|
56
|
+
// - `StubBase` must come before `Stubable`: `Stub<T>` of a callable `T` is itself callable, so
|
|
57
|
+
// it matches `Stubable`. Checking `StubBase` first keeps existing stubs as-is instead of
|
|
58
|
+
// double-wrapping them as `Stub<Stub<T>>`.
|
|
45
59
|
// prettier-ignore
|
|
46
|
-
type Stubify<T> = T extends
|
|
47
|
-
|
|
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 {
|
|
48
63
|
[key: string | number]: any;
|
|
49
|
-
} ? { [K in keyof T as K extends string | number ? K : never]: Stubify<T[K]
|
|
64
|
+
} ? { [K in keyof T as K extends string | number ? K : never]: Stubify<T[K]>; } : T;
|
|
50
65
|
// Recursively rewrite all `Stub<T>`s with the corresponding `T`s.
|
|
51
66
|
// Note we use `StubBase` instead of `Stub` here to avoid circular dependencies:
|
|
52
67
|
// `Stub` depends on `Provider`, which depends on `Unstubify`, which would depend on `Stub`.
|
|
53
68
|
// prettier-ignore
|
|
54
|
-
type UnstubifyInner<T> =
|
|
69
|
+
type UnstubifyInner<T> =
|
|
70
|
+
// Preserve local RpcTarget acceptance, but avoid needless `Stub | Value` unions when the stub
|
|
55
71
|
// is already assignable to the value type (important for callback contextual typing).
|
|
56
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 {
|
|
57
73
|
[key: string | number]: unknown;
|
|
58
|
-
} ? { [K in keyof T as K extends string | number ? K : never]: Unstubify<T[K]
|
|
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.
|
|
59
76
|
// (This also covers RpcPromise, because it's defined as being a Promise.)
|
|
60
77
|
// Map placeholders are also allowed so primitive map callback inputs can be forwarded directly
|
|
61
78
|
// into RPC params.
|
|
@@ -64,18 +81,38 @@ T extends StubBase<infer V> ? (T extends V ? UnstubifyInner<V> : (T | UnstubifyI
|
|
|
64
81
|
// Remove stub members from mixed unions so callback params don’t get both stub and unstubbed signatures.
|
|
65
82
|
// Marker carried by map() callback inputs. This lets primitive placeholders flow through params.
|
|
66
83
|
type Unstubify<T> = NonStubMembers<T> | UnstubifyInner<T> | Promise<UnstubifyInner<T>> | MapValuePlaceholder<UnstubifyInner<T>>;
|
|
67
|
-
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]>; };
|
|
68
85
|
interface MapValuePlaceholder<T> {
|
|
69
86
|
[__RPC_MAP_VALUE_BRAND]: T;
|
|
70
87
|
}
|
|
71
|
-
type NonStubMembers<T> = Exclude<T, StubBase<any>>;
|
|
88
|
+
type NonStubMembers<T> = Exclude<T, StubBase<any>>;
|
|
89
|
+
// Utility type for adding `Disposable`s to `object` types only.
|
|
72
90
|
// Note `unknown & T` is equivalent to `T`.
|
|
73
|
-
type MaybeDisposable<T> = T extends object ? Disposable : unknown;
|
|
91
|
+
type MaybeDisposable<T> = T extends object ? Disposable : unknown;
|
|
92
|
+
// The type behind the public `RpcPromise` (re-exported with its `RpcCompatible` constraint from
|
|
74
93
|
// index.ts). Defined here so that `Result`'s arms below resolve to the exact same alias
|
|
75
94
|
// instantiation: when both sides of an assignability check are the same alias applied to the
|
|
76
95
|
// same type arguments, the checker short-circuits on identity instead of recursing through the
|
|
77
96
|
// full structural comparison of these mutually-recursive types.
|
|
78
97
|
type RpcPromise$1<T> = T extends Stubable ? Promise<Stub<T>> & Provider<T> & StubBase<T> : Promise<Stubify<T> & MaybeDisposable<T>> & Provider<T> & StubBase<T>;
|
|
98
|
+
// The elision `Result` applies to a bare stub type: unwrap `Stub<T>` back to `T` when the
|
|
99
|
+
// payload is `Stubable`. Such stubs await back to a stub either way, so eliding keeps a
|
|
100
|
+
// declared `Promise<RpcStub<T>>` return interchangeable with a `Promise<T>` return.
|
|
101
|
+
// Plain-interface stubs are NOT elided: `RpcPromise<U>` only awaits to `Stub<U>` when
|
|
102
|
+
// `U extends Stubable`, so eliding those would change the awaited type from a stub to a
|
|
103
|
+
// stubified record. The payload check is deliberately non-distributive (`[U] extends [...]`).
|
|
104
|
+
// `Stub<any>` is not elided either: `[any] extends [Stubable]` is true, so without the `IsAny`
|
|
105
|
+
// guard an `any`-payload stub would lose its stub surface.
|
|
106
|
+
// Also used by the `RpcPromise` constructor signature (index.ts), which applies exactly this
|
|
107
|
+
// transformation so constructing from a promised stub matches the method-return type.
|
|
108
|
+
type ElideStub<T> = T extends StubBase<infer U> ? (IsAny<U> extends true ? T : [U] extends [Stubable] ? U : T) : T;
|
|
109
|
+
// What the promise given to `new RpcPromise<T>(...)` may resolve to: the payload itself, or —
|
|
110
|
+
// for stubable payloads — a stub of it. `NoInfer` keeps the stub arm out of inference, so an
|
|
111
|
+
// inferred `T` is always the promise's own resolution type; the arm only matters when `T` is
|
|
112
|
+
// explicitly annotated (`new RpcPromise<Counter>(promiseOfStub)`). Stubs of non-stubable
|
|
113
|
+
// payloads are deliberately rejected: `ElideStub` wouldn't elide those, so accepting one would
|
|
114
|
+
// claim the promise awaits to a stubified record while the runtime resolves to a stub.
|
|
115
|
+
type PayloadOrStub<T> = T | NoInfer<Stub<Extract<T, Stubable>>>;
|
|
79
116
|
// Type for method return or property on an RPC interface.
|
|
80
117
|
// - Stubable types are replaced by stubs.
|
|
81
118
|
// - RpcCompatible types are passed by value, with stubable types replaced by stubs
|
|
@@ -84,32 +121,49 @@ type RpcPromise$1<T> = T extends Stubable ? Promise<Stub<T>> & Provider<T> & Stu
|
|
|
84
121
|
// Technically, we use custom thenables here, but they quack like `Promise`s.
|
|
85
122
|
// Intersecting with `(Maybe)Provider` allows pipelining.
|
|
86
123
|
// prettier-ignore
|
|
87
|
-
type Result<R> = IsAny<R> extends true ?
|
|
124
|
+
type Result<R> = IsAny<R> extends true ? RpcPromise$1<unknown> :
|
|
125
|
+
// `RpcPromise<U>`: always safe to normalize — `Result` is idempotent — so a declared
|
|
126
|
+
// `RpcPromise<U>` return produces the same type as a `Promise<U>` return.
|
|
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
|
|
129
|
+
// here: anything matching our `StubBase` was produced by machinery that already enforced
|
|
130
|
+
// the constraint, and re-evaluating `RpcCompatible<R>` in this arm recurses through its
|
|
131
|
+
// `Stub<Stubable>` member back into `Result`, tripping TS2615 circularity errors in mapped
|
|
132
|
+
// types.
|
|
133
|
+
R extends StubBase<any> ? RpcPromise$1<ElideStub<R>> : R extends RpcCompatible<R> ? RpcPromise$1<R> : never;
|
|
88
134
|
type IsAny<T> = 0 extends (1 & T) ? true : false;
|
|
89
|
-
|
|
135
|
+
// Type for method or property on an RPC interface.
|
|
90
136
|
// For methods, unwrap `Stub`s in parameters, and rewrite returns to be `Result`s.
|
|
91
137
|
// Unwrapping `Stub`s allows calling with `Stubable` arguments.
|
|
92
138
|
// For properties, rewrite types to be `Result`s.
|
|
93
139
|
// In each case, unwrap `Promise`s.
|
|
94
|
-
type MethodOrProperty<V> = V extends ((...args: infer P) => infer R) ? (...args: UnstubifyAll<P>) =>
|
|
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.
|
|
95
142
|
// This is intersected with methods/properties.
|
|
96
143
|
type MaybeCallableProvider<T> = T extends ((...args: any[]) => any) ? MethodOrProperty<T> : unknown;
|
|
97
144
|
type TupleIndexKeys<T extends ReadonlyArray<unknown>> = Extract<keyof T, `${number}`>;
|
|
98
|
-
type MapCallbackValue<T> =
|
|
145
|
+
type MapCallbackValue<T> =
|
|
146
|
+
// `Omit` removes call signatures, so re-intersect callable provider behavior.
|
|
99
147
|
T extends unknown ? Omit<Result<T>, keyof Promise<unknown>> & MaybeCallableProvider<T> & MapValuePlaceholder<T> : never;
|
|
100
148
|
type InvalidNativePromiseInMapResult<T, Seen = never> = T extends unknown ? InvalidNativePromiseInMapResultImpl<T, Seen> : never;
|
|
101
|
-
type InvalidNativePromiseInMapResultImpl<T, Seen> = [T] extends [Seen] ? never
|
|
102
|
-
|
|
103
|
-
|
|
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 {
|
|
104
154
|
[key: string | number]: unknown;
|
|
105
155
|
} ? InvalidNativePromiseInMapResult<T[Extract<keyof T, string | number>], Seen | T> : never;
|
|
106
156
|
type MapCallbackReturn<T> = InvalidNativePromiseInMapResult<T> extends never ? T : never;
|
|
107
|
-
type ArrayProvider<E> = { [K in number]: MethodOrProperty<E
|
|
157
|
+
type ArrayProvider<E> = { [K in number]: MethodOrProperty<E>; } & {
|
|
108
158
|
map<V>(callback: (elem: MapCallbackValue<E>) => MapCallbackReturn<V>): Result<Array<V>>;
|
|
109
159
|
};
|
|
110
|
-
type TupleProvider<T extends ReadonlyArray<unknown>> = { [K in TupleIndexKeys<T>]: MethodOrProperty<T[K]
|
|
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.
|
|
111
162
|
// Rewrites all methods/properties to be `MethodOrProperty`s, while preserving callable types.
|
|
112
|
-
|
|
163
|
+
// `__RPC_TARGET_BRAND` deliberately flows through the key mapping (as a `never` property) so
|
|
164
|
+
// stubs of branded targets stay assignable to workers-types' `Stubable`. Such stubs match our
|
|
165
|
+
// `Stubable` too — harmless, since all machinery checks `StubBase` before `Stubable`.
|
|
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>>> & {
|
|
113
167
|
map<V>(callback: (value: MapCallbackValue<NonNullable<T>>) => MapCallbackReturn<V>): Result<Array<V>>;
|
|
114
168
|
});
|
|
115
169
|
//#endregion
|
|
@@ -327,9 +381,21 @@ declare const RpcStub: {
|
|
|
327
381
|
* until you actually `await` the promise (or call `then()`, etc. on it). This is an optimization:
|
|
328
382
|
* if you only intend to use the promise for pipelining and you never await it, then there's no
|
|
329
383
|
* need to transmit the resolution!
|
|
384
|
+
*
|
|
385
|
+
* You may also construct an `RpcPromise` yourself from a regular `Promise`, using
|
|
386
|
+
* `new RpcPromise(promise)`, allowing you to perform promise pipelining on a local promise. This
|
|
387
|
+
* is semantically identical to creating a local-loopback RPC that returns the promise, and then
|
|
388
|
+
* invoking it: pipelined calls wait until the promise resolves, then are delivered, in order, to
|
|
389
|
+
* the resolution. This is useful when you plan to obtain some stub in the future, but want to
|
|
390
|
+
* allow code to start queuing calls on it immediately. Note that the `RpcPromise` takes
|
|
391
|
+
* ownership of the resolution: disposing it disposes the resolution, so resolve the promise
|
|
392
|
+
* with a `dup()` if you also intend to keep the stub.
|
|
330
393
|
*/
|
|
331
394
|
type RpcPromise<T extends RpcCompatible<T>> = RpcPromise$1<T>;
|
|
332
|
-
declare const RpcPromise: {
|
|
395
|
+
declare const RpcPromise: {
|
|
396
|
+
new <T extends RpcCompatible<T>>(value: Promise<T>): RpcPromise$1<ElideStub<T>>;
|
|
397
|
+
new <T extends RpcCompatible<T>>(value: Promise<PayloadOrStub<T>>): RpcPromise$1<ElideStub<T>>;
|
|
398
|
+
};
|
|
333
399
|
/**
|
|
334
400
|
* Use to construct an `RpcSession` on top of a custom `RpcTransport`.
|
|
335
401
|
*
|