@vastblast/capnweb 0.4.2 → 0.5.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/dist/index.d.cts CHANGED
@@ -1,226 +1,212 @@
1
1
  import { IncomingMessage, ServerResponse, OutgoingHttpHeaders, OutgoingHttpHeader } from 'node:http';
2
2
 
3
- // Copyright (c) 2025 Cloudflare, Inc.
4
- // Licensed under the MIT license found in the LICENSE.txt file or at:
5
- // https://opensource.org/license/mit
6
-
7
- // This file borrows heavily from `types/defines/rpc.d.ts` in workerd.
8
-
9
- // Branded types for identifying `WorkerEntrypoint`/`DurableObject`/`Target`s.
10
- // TypeScript uses *structural* typing meaning anything with the same shape as type `T` is a `T`.
11
- // For the classes exported by `cloudflare:workers` we want *nominal* typing (i.e. we only want to
12
- // accept `WorkerEntrypoint` from `cloudflare:workers`, not any other class with the same shape)
13
- declare const __RPC_STUB_BRAND: '__RPC_STUB_BRAND';
14
- declare const __RPC_TARGET_BRAND: '__RPC_TARGET_BRAND';
15
- interface RpcTargetBranded {
16
- [__RPC_TARGET_BRAND]: never;
17
- }
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
- type IsAny<T> = 0 extends (1 & T) ? true : false;
23
- type IsUnknown<T> = unknown extends T ? ([T] extends [unknown] ? true : false) : false;
24
-
25
- // Types that can be used through `Stub`s.
26
- // `never[]` preserves compatibility with strongly-typed function signatures without introducing
27
- // `any` into inference.
28
- type Stubable = RpcTargetBranded | ((...args: never[]) => unknown);
29
-
30
- type TypedArray =
31
- | Uint8Array
32
- | Uint8ClampedArray
33
- | Uint16Array
34
- | Uint32Array
35
- | Int8Array
36
- | Int16Array
37
- | Int32Array
38
- | BigUint64Array
39
- | BigInt64Array
40
- | Float32Array
41
- | Float64Array;
42
-
43
- // This represents all the types that can be sent as-is over an RPC boundary.
44
- type BaseType =
45
- | void
46
- | undefined
47
- | null
48
- | boolean
49
- | number
50
- | bigint
51
- | string
52
- | TypedArray
53
- | ArrayBuffer
54
- | DataView
55
- | Date
56
- | Error
57
- | RegExp
58
- | ReadableStream<Uint8Array>
59
- | WritableStream<any> // Chunk type can be any RPC-compatible type
60
- | Request
61
- | Response
62
- | Headers;
63
-
64
- // Base type for all RPC stubs, including common memory management methods.
65
- // `T` is used as a marker type for unwrapping `Stub`s later.
66
- interface StubBase<T = unknown> extends Disposable {
67
- [__RPC_STUB_BRAND]: T;
68
- dup(): this;
69
- onRpcBroken(callback: (error: any) => void): void;
70
- }
71
-
72
- // Types that can be passed over RPC.
73
- // The reason for using a generic type here is to build a serializable subset of structured
74
- // cloneable composite types. This allows types defined with the "interface" keyword to pass the
75
- // serializable check as well. Otherwise, only types defined with the "type" keyword would pass.
76
- type RpcCompatible<T, D extends unknown[] = RpcDepth> =
77
- D extends []
78
- ? unknown
79
- :
80
- // Structured cloneables
81
- | BaseType
82
- // Structured cloneable composites
83
- | Map<
84
- T extends Map<infer U, unknown> ? RpcCompatible<U, NextDepth<D>> : never,
85
- T extends Map<unknown, infer U> ? RpcCompatible<U, NextDepth<D>> : never
86
- >
87
- | Set<T extends Set<infer U> ? RpcCompatible<U, NextDepth<D>> : never>
88
- | Array<T extends Array<infer U> ? RpcCompatible<U, NextDepth<D>> : never>
89
- | ReadonlyArray<T extends ReadonlyArray<infer U> ? RpcCompatible<U, NextDepth<D>> : never>
90
- | {
91
- [K in keyof T as K extends string | number ? K : never]:
92
- RpcCompatible<T[K], NextDepth<D>>;
93
- }
94
- | Promise<T extends Promise<infer U> ? RpcCompatible<U, NextDepth<D>> : never>
95
- // Special types
96
- | Stub<Stubable>
97
- // Serialized as stubs, see `Stubify`
98
- | Stubable;
99
-
100
- type Stub<T extends RpcCompatible<T>> =
101
- T extends object ? Provider<T> & StubBase<T> : StubBase<T>;
102
-
103
- // Recursively rewrite all `Stubable` types with `Stub`s, and resolve promises.
104
- // prettier-ignore
105
- type Stubify<T, D extends unknown[] = RpcDepth> =
106
- D extends [] ? T
107
- : T extends Stubable ? Stub<T>
108
- : T extends Promise<infer U> ? Stubify<U, NextDepth<D>>
109
- : T extends StubBase<any> ? T
110
- : T extends Map<infer K, infer V> ? Map<Stubify<K, NextDepth<D>>, Stubify<V, NextDepth<D>>>
111
- : T extends Set<infer V> ? Set<Stubify<V, NextDepth<D>>>
112
- : T extends [] ? []
113
- : T extends [infer Head, ...infer Tail] ? [Stubify<Head, NextDepth<D>>, ...Stubify<Tail, NextDepth<D>>]
114
- : T extends readonly [] ? readonly []
115
- : T extends readonly [infer Head, ...infer Tail] ? readonly [Stubify<Head, NextDepth<D>>, ...Stubify<Tail, NextDepth<D>>]
116
- : T extends Array<infer V> ? Array<Stubify<V, NextDepth<D>>>
117
- : T extends ReadonlyArray<infer V> ? ReadonlyArray<Stubify<V, NextDepth<D>>>
118
- : T extends BaseType ? T
119
- // When using "unknown" instead of "any", interfaces are not stubified.
120
- : T extends { [key: string | number]: any }
121
- ? {
122
- [K in keyof T as K extends string | number ? K : never]:
123
- Stubify<T[K], NextDepth<D>>;
124
- }
125
- : T;
126
-
127
- // Recursively rewrite all `Stub<T>`s with the corresponding `T`s.
128
- // Note we use `StubBase` instead of `Stub` here to avoid circular dependencies:
129
- // `Stub` depends on `Provider`, which depends on `Unstubify`, which would depend on `Stub`.
130
- // prettier-ignore
131
- type UnstubifyInner<T, D extends unknown[] = RpcDepth> =
132
- D extends [] ? T
133
- // Preserve local RpcTarget acceptance, but avoid needless `Stub | Value` unions when the stub
134
- // is already assignable to the value type (important for callback contextual typing).
135
- : T extends StubBase<infer V> ? (T extends V ? UnstubifyInner<V, NextDepth<D>> : (T | UnstubifyInner<V, NextDepth<D>>))
136
- : T extends Promise<infer U> ? UnstubifyInner<U, NextDepth<D>>
137
- : T extends Map<infer K, infer V> ? Map<Unstubify<K, NextDepth<D>>, Unstubify<V, NextDepth<D>>>
138
- : T extends Set<infer V> ? Set<Unstubify<V, NextDepth<D>>>
139
- : T extends [] ? []
140
- : T extends [infer Head, ...infer Tail] ? [Unstubify<Head, NextDepth<D>>, ...Unstubify<Tail, NextDepth<D>>]
141
- : T extends readonly [] ? readonly []
142
- : T extends readonly [infer Head, ...infer Tail] ? readonly [Unstubify<Head, NextDepth<D>>, ...Unstubify<Tail, NextDepth<D>>]
143
- : T extends Array<infer V> ? Array<Unstubify<V, NextDepth<D>>>
144
- : T extends ReadonlyArray<infer V> ? ReadonlyArray<Unstubify<V, NextDepth<D>>>
145
- : T extends BaseType ? T
146
- : T extends { [key: string | number]: unknown }
147
- ? {
148
- [K in keyof T as K extends string | number ? K : never]:
149
- Unstubify<T[K], NextDepth<D>>;
150
- }
151
- : T;
152
-
153
- // You can put promises anywhere in the params and they'll be resolved before delivery.
154
- // (This also covers RpcPromise, because it's defined as being a Promise.)
155
- type Unstubify<T, D extends unknown[] = RpcDepth> =
156
- UnstubifyInner<T, D> | Promise<UnstubifyInner<T, D>>;
157
-
158
- type UnstubifyAll<A extends readonly unknown[], D extends unknown[] = RpcDepth> = {
159
- [I in keyof A]: Unstubify<A[I], D>;
160
- };
161
-
162
- // Utility type for adding `Disposable`s to `object` types only.
163
- // Note `unknown & T` is equivalent to `T`.
164
- type MaybeDisposable<T> = T extends object ? Disposable : unknown;
165
-
166
- // Type for method return or property on an RPC interface.
167
- // - Stubable types are replaced by stubs.
168
- // - RpcCompatible types are passed by value, with stubable types replaced by stubs
169
- // and a top-level `Disposer`.
170
- // Everything else can't be passed over RPC.
171
- // Technically, we use custom thenables here, but they quack like `Promise`s.
172
- // Intersecting with `(Maybe)Provider` allows pipelining.
173
- // prettier-ignore
174
- type UnknownResult<D extends unknown[]> = D extends []
175
- ? Promise<unknown> & StubBase<unknown>
176
- : Promise<unknown> & Provider<unknown, D> & StubBase<unknown>;
177
-
178
- // prettier-ignore
179
- type Result<R, D extends unknown[] = RpcDepth> =
180
- D extends [] ? UnknownResult<D>
181
- : IsAny<R> extends true ? UnknownResult<D>
182
- : IsUnknown<R> extends true ? UnknownResult<D>
183
- : R extends Stubable ? Promise<Stub<R>> & Provider<R, D> & StubBase<R>
184
- : R extends RpcCompatible<R, D> ? Promise<Stubify<R, D> & MaybeDisposable<R>> & Provider<R, D> & StubBase<R>
185
- : never;
186
-
187
- type HasAnyFunctionParts<P extends readonly unknown[], R> =
188
- IsAny<R> extends true ? true : IsAny<P[number]>;
189
-
190
- // Type for method or property on an RPC interface.
191
- // For methods, unwrap `Stub`s in parameters, and rewrite returns to be `Result`s.
192
- // Unwrapping `Stub`s allows calling with `Stubable` arguments.
193
- // For properties, rewrite types to be `Result`s.
194
- // In each case, unwrap `Promise`s.
195
- type MethodOrProperty<V, D extends unknown[] = RpcDepth> = V extends (...args: infer P) => infer R
196
- ? HasAnyFunctionParts<P, R> extends true
197
- ? (...args: unknown[]) => UnknownResult<NextDepth<D>>
198
- : (...args: UnstubifyAll<P, NextDepth<D>>) => Result<Awaited<R>, NextDepth<D>>
199
- : Result<Awaited<V>, NextDepth<D>>;
200
-
201
- // Type for the callable part of a `Provider` if `T` is callable.
202
- // This is intersected with methods/properties.
203
- type MaybeCallableProvider<T, D extends unknown[] = RpcDepth> = T extends (...args: any[]) => any
204
- ? MethodOrProperty<T, D>
205
- : unknown;
206
-
207
- // Base type for all other types providing RPC-like interfaces.
208
- // Rewrites all methods/properties to be `MethodOrProperty`s, while preserving callable types.
209
- type Provider<T, D extends unknown[] = RpcDepth> = MaybeCallableProvider<T, D> &
210
- (T extends Array<infer U>
211
- ? {
212
- [key: number]: MethodOrProperty<U, D>;
213
- } & {
214
- map<V>(callback: (elem: Result<U, NextDepth<D>>) => V): Result<Array<V>, NextDepth<D>>;
215
- }
216
- : {
217
- [K in Exclude<
218
- keyof T,
219
- symbol | keyof StubBase<never>
220
- >]: MethodOrProperty<T[K], D>;
221
- } & {
222
- map<V>(callback: (value: Result<NonNullable<T>, NextDepth<D>>) => V):
223
- Result<Array<V>, NextDepth<D>>;
3
+ // Copyright (c) 2025 Cloudflare, Inc.
4
+ // Licensed under the MIT license found in the LICENSE.txt file or at:
5
+ // https://opensource.org/license/mit
6
+
7
+ // This file borrows heavily from `types/defines/rpc.d.ts` in workerd.
8
+
9
+ // Branded types for identifying `WorkerEntrypoint`/`DurableObject`/`Target`s.
10
+ // TypeScript uses *structural* typing meaning anything with the same shape as type `T` is a `T`.
11
+ // For the classes exported by `cloudflare:workers` we want *nominal* typing (i.e. we only want to
12
+ // accept `WorkerEntrypoint` from `cloudflare:workers`, not any other class with the same shape)
13
+ declare const __RPC_STUB_BRAND: '__RPC_STUB_BRAND';
14
+ declare const __RPC_TARGET_BRAND: '__RPC_TARGET_BRAND';
15
+ interface RpcTargetBranded {
16
+ [__RPC_TARGET_BRAND]: never;
17
+ }
18
+
19
+ type IsAny<T> = 0 extends (1 & T) ? true : false;
20
+ type IsUnknown<T> = unknown extends T ? ([T] extends [unknown] ? true : false) : false;
21
+
22
+ // Types that can be used through `Stub`s.
23
+ // `never[]` preserves compatibility with strongly-typed function signatures without introducing
24
+ // `any` into inference.
25
+ type Stubable = RpcTargetBranded | ((...args: never[]) => unknown);
26
+
27
+ type TypedArray =
28
+ | Uint8Array
29
+ | Uint8ClampedArray
30
+ | Uint16Array
31
+ | Uint32Array
32
+ | Int8Array
33
+ | Int16Array
34
+ | Int32Array
35
+ | BigUint64Array
36
+ | BigInt64Array
37
+ | Float32Array
38
+ | Float64Array;
39
+
40
+ // This represents all the types that can be sent as-is over an RPC boundary
41
+ type BaseType =
42
+ | void
43
+ | undefined
44
+ | null
45
+ | boolean
46
+ | number
47
+ | bigint
48
+ | string
49
+ | TypedArray
50
+ | ArrayBuffer
51
+ | DataView
52
+ | Date
53
+ | Error
54
+ | RegExp
55
+ | ReadableStream<Uint8Array>
56
+ | WritableStream<any> // Chunk type can be any RPC-compatible type
57
+ | Request
58
+ | Response
59
+ | Headers;
60
+
61
+ // Base type for all RPC stubs, including common memory management methods.
62
+ // `T` is used as a marker type for unwrapping `Stub`s later.
63
+ interface StubBase<T = unknown> extends Disposable {
64
+ [__RPC_STUB_BRAND]: T;
65
+ dup(): this;
66
+ onRpcBroken(callback: (error: any) => void): void;
67
+ }
68
+
69
+ // Types that can be passed over RPC.
70
+ // The reason for using a generic type here is to build a serializable subset of structured
71
+ // cloneable composite types. This allows types defined with the "interface" keyword to pass the
72
+ // serializable check as well. Otherwise, only types defined with the "type" keyword would pass.
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;
92
+
93
+ type Stub<T extends RpcCompatible<T>> =
94
+ T extends object ? Provider<T> & StubBase<T> : StubBase<T>;
95
+
96
+ // Recursively rewrite all `Stubable` types with `Stub`s, and resolve promises.
97
+ // prettier-ignore
98
+ type Stubify<T> =
99
+ T extends Stubable ? Stub<T>
100
+ : T extends Promise<infer U> ? Stubify<U>
101
+ : T extends StubBase<any> ? T
102
+ : T extends Map<infer K, infer V> ? Map<Stubify<K>, Stubify<V>>
103
+ : T extends Set<infer V> ? Set<Stubify<V>>
104
+ : T extends [] ? []
105
+ : T extends [infer Head, ...infer Tail] ? [Stubify<Head>, ...Stubify<Tail>]
106
+ : T extends readonly [] ? readonly []
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>>
110
+ : T extends BaseType ? T
111
+ // When using "unknown" instead of "any", interfaces are not stubified.
112
+ : T extends { [key: string | number]: any }
113
+ ? {
114
+ [K in keyof T as K extends string | number ? K : never]: Stubify<T[K]>;
115
+ }
116
+ : T;
117
+
118
+ // Recursively rewrite all `Stub<T>`s with the corresponding `T`s.
119
+ // Note we use `StubBase` instead of `Stub` here to avoid circular dependencies:
120
+ // `Stub` depends on `Provider`, which depends on `Unstubify`, which would depend on `Stub`.
121
+ // prettier-ignore
122
+ type UnstubifyInner<T> =
123
+ // Preserve local RpcTarget acceptance, but avoid needless `Stub | Value` unions when the stub
124
+ // is already assignable to the value type (important for callback contextual typing).
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>>
129
+ : T extends [] ? []
130
+ : T extends [infer Head, ...infer Tail] ? [Unstubify<Head>, ...Unstubify<Tail>]
131
+ : T extends readonly [] ? readonly []
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>>
135
+ : T extends BaseType ? T
136
+ : T extends { [key: string | number]: unknown }
137
+ ? {
138
+ [K in keyof T as K extends string | number ? K : never]: Unstubify<T[K]>;
139
+ }
140
+ : T;
141
+
142
+ // You can put promises anywhere in the params and they'll be resolved before delivery.
143
+ // (This also covers RpcPromise, because it's defined as being a Promise.)
144
+ type Unstubify<T> = UnstubifyInner<T> | Promise<UnstubifyInner<T>>;
145
+
146
+ type UnstubifyAll<A extends readonly unknown[]> = { [I in keyof A]: Unstubify<A[I]> };
147
+
148
+ // Utility type for adding `Disposable`s to `object` types only.
149
+ // Note `unknown & T` is equivalent to `T`.
150
+ type MaybeDisposable<T> = T extends object ? Disposable : unknown;
151
+
152
+ // Type for method return or property on an RPC interface.
153
+ // - Stubable types are replaced by stubs.
154
+ // - RpcCompatible types are passed by value, with stubable types replaced by stubs
155
+ // and a top-level `Disposer`.
156
+ // Everything else can't be passed over RPC.
157
+ // Technically, we use custom thenables here, but they quack like `Promise`s.
158
+ // Intersecting with `(Maybe)Provider` allows pipelining.
159
+ type UnknownResult = Promise<unknown> & Provider<unknown> & StubBase<unknown>;
160
+
161
+ // prettier-ignore
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>
167
+ : never;
168
+
169
+ // Type for method or property on an RPC interface.
170
+ // For methods, unwrap `Stub`s in parameters, and rewrite returns to be `Result`s.
171
+ // Unwrapping `Stub`s allows calling with `Stubable` arguments.
172
+ // For properties, rewrite types to be `Result`s.
173
+ // In each case, unwrap `Promise`s.
174
+ type MethodOrProperty<V> = V extends (...args: infer P) => infer R
175
+ ? (...args: UnstubifyAll<P>) => (IsAny<R> extends true ? UnknownResult : Result<Awaited<R>>)
176
+ : Result<Awaited<V>>;
177
+
178
+ // Type for the callable part of an `Provider` if `T` is callable.
179
+ // This is intersected with methods/properties.
180
+ type MaybeCallableProvider<T> = T extends (...args: any[]) => any
181
+ ? MethodOrProperty<T>
182
+ : unknown;
183
+
184
+ type TupleIndexKeys<T extends ReadonlyArray<unknown>> =
185
+ Extract<keyof T, `${number}`>;
186
+ type MapCallbackValue<T> =
187
+ // `Omit` removes call signatures, so re-intersect callable provider behavior.
188
+ T extends unknown ? Omit<Result<T>, keyof Promise<unknown>> & MaybeCallableProvider<T> : never;
189
+ type ArrayProvider<E> = {
190
+ [K in number]: MethodOrProperty<E>;
191
+ } & {
192
+ map<V>(callback: (elem: MapCallbackValue<E>) => V): Result<Array<V>>;
193
+ };
194
+ type TupleProvider<T extends ReadonlyArray<unknown>> = {
195
+ [K in TupleIndexKeys<T>]: MethodOrProperty<T[K]>;
196
+ } & ArrayProvider<T[number]>;
197
+
198
+ // Base type for all other types providing RPC-like interfaces.
199
+ // Rewrites all methods/properties to be `MethodOrProperty`s, while preserving callable types.
200
+ type Provider<T> = MaybeCallableProvider<T> &
201
+ (T extends ReadonlyArray<unknown>
202
+ ? number extends T["length"] ? ArrayProvider<T[number]> : TupleProvider<T>
203
+ : {
204
+ [K in Exclude<
205
+ keyof T,
206
+ symbol | keyof StubBase<never>
207
+ >]: MethodOrProperty<T[K]>;
208
+ } & {
209
+ map<V>(callback: (value: MapCallbackValue<NonNullable<T>>) => V): Result<Array<V>>;
224
210
  });
225
211
 
226
212
  /**