capnweb-better-types 0.1.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/LICENSE +21 -0
- package/README.md +57 -0
- package/capnweb.d.ts +440 -0
- package/noop.js +1 -0
- package/package.json +51 -0
- package/tsconfig.paths.json +9 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 VastBlast
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# capnweb-better-types
|
|
2
|
+
|
|
3
|
+
Temporary package that ships a copy of `dist/index.d.ts` from:
|
|
4
|
+
https://github.com/cloudflare/capnweb/pull/142
|
|
5
|
+
|
|
6
|
+
Use this only until that PR is merged and released in `capnweb`.
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm i capnweb
|
|
12
|
+
npm i -D capnweb-better-types
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Use
|
|
16
|
+
|
|
17
|
+
### Option 1: Import utility types directly
|
|
18
|
+
|
|
19
|
+
Import types directly:
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import type { RpcCompatible, Stub, Stubify, Provider } from "capnweb-better-types";
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Runtime imports still come from `capnweb`.
|
|
26
|
+
|
|
27
|
+
### Option 2: Override `capnweb` type inference (recommended)
|
|
28
|
+
|
|
29
|
+
Add this to your project `tsconfig.json`:
|
|
30
|
+
|
|
31
|
+
```json
|
|
32
|
+
{
|
|
33
|
+
"compilerOptions": {
|
|
34
|
+
"paths": {
|
|
35
|
+
"capnweb": ["./node_modules/capnweb-better-types/capnweb"]
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
This replaces only TypeScript's module declaration resolution for `"capnweb"`.
|
|
42
|
+
Runtime still resolves to the real `capnweb` package.
|
|
43
|
+
|
|
44
|
+
If you use `skipLibCheck: false`, current upstream build output may hit TypeScript `TS2574`.
|
|
45
|
+
Temporary workaround: set `skipLibCheck: true` until upstream release.
|
|
46
|
+
|
|
47
|
+
## How this file is obtained (future updates)
|
|
48
|
+
|
|
49
|
+
When you want to refresh this package with newer upstream types:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
# from the capnweb repo root
|
|
53
|
+
npm run build
|
|
54
|
+
cp dist/index.d.ts capnweb-better-types/capnweb.d.ts
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Then bump `capnweb-better-types` version and publish.
|
package/capnweb.d.ts
ADDED
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
import { IncomingMessage, ServerResponse, OutgoingHttpHeaders, OutgoingHttpHeader } from 'node:http';
|
|
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
|
+
// Distinguishes mapper placeholders from regular values so param unwrapping can accept them.
|
|
16
|
+
declare const __RPC_MAP_VALUE_BRAND: unique symbol;
|
|
17
|
+
interface RpcTargetBranded {
|
|
18
|
+
[__RPC_TARGET_BRAND]: never;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
type IsAny<T> = 0 extends (1 & T) ? true : false;
|
|
22
|
+
type IsUnknown<T> = unknown extends T ? ([T] extends [unknown] ? true : false) : false;
|
|
23
|
+
|
|
24
|
+
// Types that can be used through `Stub`s.
|
|
25
|
+
// `never[]` preserves compatibility with strongly-typed function signatures without introducing
|
|
26
|
+
// `any` into inference.
|
|
27
|
+
type Stubable = RpcTargetBranded | ((...args: never[]) => unknown);
|
|
28
|
+
|
|
29
|
+
type TypedArray =
|
|
30
|
+
| Uint8Array
|
|
31
|
+
| Uint8ClampedArray
|
|
32
|
+
| Uint16Array
|
|
33
|
+
| Uint32Array
|
|
34
|
+
| Int8Array
|
|
35
|
+
| Int16Array
|
|
36
|
+
| Int32Array
|
|
37
|
+
| BigUint64Array
|
|
38
|
+
| BigInt64Array
|
|
39
|
+
| Float32Array
|
|
40
|
+
| Float64Array;
|
|
41
|
+
|
|
42
|
+
// This represents all the types that can be sent as-is over an RPC boundary
|
|
43
|
+
type BaseType =
|
|
44
|
+
| void
|
|
45
|
+
| undefined
|
|
46
|
+
| null
|
|
47
|
+
| boolean
|
|
48
|
+
| number
|
|
49
|
+
| bigint
|
|
50
|
+
| string
|
|
51
|
+
| TypedArray
|
|
52
|
+
| ArrayBuffer
|
|
53
|
+
| DataView
|
|
54
|
+
| Date
|
|
55
|
+
| Error
|
|
56
|
+
| RegExp
|
|
57
|
+
| ReadableStream<Uint8Array>
|
|
58
|
+
| WritableStream<any> // Chunk type can be any RPC-compatible type
|
|
59
|
+
| Request
|
|
60
|
+
| Response
|
|
61
|
+
| Headers;
|
|
62
|
+
|
|
63
|
+
// Base type for all RPC stubs, including common memory management methods.
|
|
64
|
+
// `T` is used as a marker type for unwrapping `Stub`s later.
|
|
65
|
+
interface StubBase<T = unknown> extends Disposable {
|
|
66
|
+
[__RPC_STUB_BRAND]: T;
|
|
67
|
+
dup(): this;
|
|
68
|
+
onRpcBroken(callback: (error: any) => void): void;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Marker carried by map() callback inputs. This lets primitive placeholders flow through params.
|
|
72
|
+
interface MapValuePlaceholder<T> {
|
|
73
|
+
[__RPC_MAP_VALUE_BRAND]: T;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Types that can be passed over RPC.
|
|
77
|
+
// The reason for using a generic type here is to build a serializable subset of structured
|
|
78
|
+
// cloneable composite types. This allows types defined with the "interface" keyword to pass the
|
|
79
|
+
// serializable check as well. Otherwise, only types defined with the "type" keyword would pass.
|
|
80
|
+
type RpcCompatible<T> =
|
|
81
|
+
// Allow `unknown` as a leaf so records/interfaces with `unknown` fields remain compatible.
|
|
82
|
+
| (IsUnknown<T> extends true ? unknown : never)
|
|
83
|
+
// Structured cloneables
|
|
84
|
+
| BaseType
|
|
85
|
+
// Structured cloneable composites
|
|
86
|
+
| Map<
|
|
87
|
+
T extends Map<infer U, unknown> ? RpcCompatible<U> : never,
|
|
88
|
+
T extends Map<unknown, infer U> ? RpcCompatible<U> : never
|
|
89
|
+
>
|
|
90
|
+
| Set<T extends Set<infer U> ? RpcCompatible<U> : never>
|
|
91
|
+
| Array<T extends Array<infer U> ? RpcCompatible<U> : never>
|
|
92
|
+
| ReadonlyArray<T extends ReadonlyArray<infer U> ? RpcCompatible<U> : never>
|
|
93
|
+
| {
|
|
94
|
+
[K in keyof T as K extends string | number ? K : never]: RpcCompatible<T[K]>;
|
|
95
|
+
}
|
|
96
|
+
| Promise<T extends Promise<infer U> ? RpcCompatible<U> : never>
|
|
97
|
+
// Special types
|
|
98
|
+
| Stub<Stubable>
|
|
99
|
+
// Serialized as stubs, see `Stubify`
|
|
100
|
+
| Stubable;
|
|
101
|
+
|
|
102
|
+
type Stub<T extends RpcCompatible<T>> =
|
|
103
|
+
T extends object ? Provider<T> & StubBase<T> : StubBase<T>;
|
|
104
|
+
|
|
105
|
+
// Recursively rewrite all `Stubable` types with `Stub`s, and resolve promises.
|
|
106
|
+
// prettier-ignore
|
|
107
|
+
type Stubify<T> =
|
|
108
|
+
T extends Stubable ? Stub<T>
|
|
109
|
+
: T extends Promise<infer U> ? Stubify<U>
|
|
110
|
+
: T extends StubBase<any> ? T
|
|
111
|
+
: T extends Map<infer K, infer V> ? Map<Stubify<K>, Stubify<V>>
|
|
112
|
+
: T extends Set<infer V> ? Set<Stubify<V>>
|
|
113
|
+
: T extends [] ? []
|
|
114
|
+
: T extends [infer Head, ...infer Tail] ? [Stubify<Head>, ...Stubify<Tail>]
|
|
115
|
+
: T extends readonly [] ? readonly []
|
|
116
|
+
: T extends readonly [infer Head, ...infer Tail] ? readonly [Stubify<Head>, ...Stubify<Tail>]
|
|
117
|
+
: T extends Array<infer V> ? Array<Stubify<V>>
|
|
118
|
+
: T extends ReadonlyArray<infer V> ? ReadonlyArray<Stubify<V>>
|
|
119
|
+
: T extends BaseType ? T
|
|
120
|
+
// When using "unknown" instead of "any", interfaces are not stubified.
|
|
121
|
+
: T extends { [key: string | number]: any } ? { [K in keyof T as K extends string | number ? K : never]: Stubify<T[K]> }
|
|
122
|
+
: T;
|
|
123
|
+
|
|
124
|
+
// Recursively rewrite all `Stub<T>`s with the corresponding `T`s.
|
|
125
|
+
// Note we use `StubBase` instead of `Stub` here to avoid circular dependencies:
|
|
126
|
+
// `Stub` depends on `Provider`, which depends on `Unstubify`, which would depend on `Stub`.
|
|
127
|
+
// prettier-ignore
|
|
128
|
+
type UnstubifyInner<T> =
|
|
129
|
+
// Preserve local RpcTarget acceptance, but avoid needless `Stub | Value` unions when the stub
|
|
130
|
+
// is already assignable to the value type (important for callback contextual typing).
|
|
131
|
+
T extends StubBase<infer V> ? (T extends V ? UnstubifyInner<V> : (T | UnstubifyInner<V>))
|
|
132
|
+
: T extends Promise<infer U> ? UnstubifyInner<U>
|
|
133
|
+
: T extends Map<infer K, infer V> ? Map<Unstubify<K>, Unstubify<V>>
|
|
134
|
+
: T extends Set<infer V> ? Set<Unstubify<V>>
|
|
135
|
+
: T extends [] ? []
|
|
136
|
+
: T extends [infer Head, ...infer Tail] ? [Unstubify<Head>, ...Unstubify<Tail>]
|
|
137
|
+
: T extends readonly [] ? readonly []
|
|
138
|
+
: T extends readonly [infer Head, ...infer Tail] ? readonly [Unstubify<Head>, ...Unstubify<Tail>]
|
|
139
|
+
: T extends Array<infer V> ? Array<Unstubify<V>>
|
|
140
|
+
: T extends ReadonlyArray<infer V> ? ReadonlyArray<Unstubify<V>>
|
|
141
|
+
: T extends BaseType ? T
|
|
142
|
+
: T extends { [key: string | number]: unknown } ? { [K in keyof T as K extends string | number ? K : never]: Unstubify<T[K]> }
|
|
143
|
+
: T;
|
|
144
|
+
|
|
145
|
+
// You can put promises anywhere in the params and they'll be resolved before delivery.
|
|
146
|
+
// (This also covers RpcPromise, because it's defined as being a Promise.)
|
|
147
|
+
// Map placeholders are also allowed so primitive map callback inputs can be forwarded directly
|
|
148
|
+
// into RPC params.
|
|
149
|
+
type Unstubify<T> =
|
|
150
|
+
| UnstubifyInner<T>
|
|
151
|
+
| Promise<UnstubifyInner<T>>
|
|
152
|
+
| MapValuePlaceholder<UnstubifyInner<T>>;
|
|
153
|
+
|
|
154
|
+
type UnstubifyAll<A extends readonly unknown[]> = { [I in keyof A]: Unstubify<A[I]> };
|
|
155
|
+
|
|
156
|
+
// Utility type for adding `Disposable`s to `object` types only.
|
|
157
|
+
// Note `unknown & T` is equivalent to `T`.
|
|
158
|
+
type MaybeDisposable<T> = T extends object ? Disposable : unknown;
|
|
159
|
+
|
|
160
|
+
// Type for method return or property on an RPC interface.
|
|
161
|
+
// - Stubable types are replaced by stubs.
|
|
162
|
+
// - RpcCompatible types are passed by value, with stubable types replaced by stubs
|
|
163
|
+
// and a top-level `Disposer`.
|
|
164
|
+
// Everything else can't be passed over RPC.
|
|
165
|
+
// Technically, we use custom thenables here, but they quack like `Promise`s.
|
|
166
|
+
// Intersecting with `(Maybe)Provider` allows pipelining.
|
|
167
|
+
type UnknownResult = Promise<unknown> & Provider<unknown> & StubBase<unknown>;
|
|
168
|
+
|
|
169
|
+
// prettier-ignore
|
|
170
|
+
type Result<R> =
|
|
171
|
+
IsAny<R> extends true ? UnknownResult
|
|
172
|
+
: IsUnknown<R> extends true ? UnknownResult
|
|
173
|
+
: R extends Stubable ? Promise<Stub<R>> & Provider<R> & StubBase<R>
|
|
174
|
+
: R extends RpcCompatible<R> ? Promise<Stubify<R> & MaybeDisposable<R>> & Provider<R> & StubBase<R>
|
|
175
|
+
: never;
|
|
176
|
+
|
|
177
|
+
// Type for method or property on an RPC interface.
|
|
178
|
+
// For methods, unwrap `Stub`s in parameters, and rewrite returns to be `Result`s.
|
|
179
|
+
// Unwrapping `Stub`s allows calling with `Stubable` arguments.
|
|
180
|
+
// For properties, rewrite types to be `Result`s.
|
|
181
|
+
// In each case, unwrap `Promise`s.
|
|
182
|
+
type MethodOrProperty<V> = V extends (...args: infer P) => infer R
|
|
183
|
+
? (...args: UnstubifyAll<P>) => IsAny<R> extends true ? UnknownResult : Result<Awaited<R>>
|
|
184
|
+
: Result<Awaited<V>>;
|
|
185
|
+
|
|
186
|
+
// Type for the callable part of an `Provider` if `T` is callable.
|
|
187
|
+
// This is intersected with methods/properties.
|
|
188
|
+
type MaybeCallableProvider<T> = T extends (...args: any[]) => any
|
|
189
|
+
? MethodOrProperty<T>
|
|
190
|
+
: unknown;
|
|
191
|
+
|
|
192
|
+
type TupleIndexKeys<T extends ReadonlyArray<unknown>> = Extract<keyof T, `${number}`>;
|
|
193
|
+
type MapCallbackValue<T> =
|
|
194
|
+
// `Omit` removes call signatures, so re-intersect callable provider behavior.
|
|
195
|
+
T extends unknown
|
|
196
|
+
? Omit<Result<T>, keyof Promise<unknown>> &
|
|
197
|
+
MaybeCallableProvider<T> &
|
|
198
|
+
MapValuePlaceholder<T>
|
|
199
|
+
: never;
|
|
200
|
+
type InvalidNativePromiseInMapResult<T, Seen = never> =
|
|
201
|
+
T extends unknown ? InvalidNativePromiseInMapResultImpl<T, Seen> : never;
|
|
202
|
+
type InvalidNativePromiseInMapResultImpl<T, Seen> =
|
|
203
|
+
[T] extends [Seen] ? never
|
|
204
|
+
// RpcPromise is modeled as Promise & StubBase, so allow promise-like stub values.
|
|
205
|
+
: T extends StubBase<any> ? never
|
|
206
|
+
// Native thenables cannot be represented in map recordings, even when typed as PromiseLike.
|
|
207
|
+
: T extends PromiseLike<unknown> ? T
|
|
208
|
+
: T extends Map<infer K, infer V>
|
|
209
|
+
? InvalidNativePromiseInMapResult<K, Seen | T> |
|
|
210
|
+
InvalidNativePromiseInMapResult<V, Seen | T>
|
|
211
|
+
: T extends Set<infer V> ? InvalidNativePromiseInMapResult<V, Seen | T>
|
|
212
|
+
: T extends readonly [] ? never
|
|
213
|
+
: T extends readonly [infer Head, ...infer Tail]
|
|
214
|
+
? InvalidNativePromiseInMapResult<Head, Seen | T> |
|
|
215
|
+
InvalidNativePromiseInMapResult<Tail[number], Seen | T>
|
|
216
|
+
: T extends ReadonlyArray<infer V> ? InvalidNativePromiseInMapResult<V, Seen | T>
|
|
217
|
+
: T extends { [key: string | number]: unknown }
|
|
218
|
+
? InvalidNativePromiseInMapResult<
|
|
219
|
+
T[Extract<keyof T, string | number>],
|
|
220
|
+
Seen | T
|
|
221
|
+
>
|
|
222
|
+
: never;
|
|
223
|
+
type MapCallbackReturn<T> =
|
|
224
|
+
InvalidNativePromiseInMapResult<T> extends never ? T : never;
|
|
225
|
+
type ArrayProvider<E> = {
|
|
226
|
+
[K in number]: MethodOrProperty<E>;
|
|
227
|
+
} & {
|
|
228
|
+
map<V>(callback: (elem: MapCallbackValue<E>) => MapCallbackReturn<V>): Result<Array<V>>;
|
|
229
|
+
};
|
|
230
|
+
type TupleProvider<T extends ReadonlyArray<unknown>> = {
|
|
231
|
+
[K in TupleIndexKeys<T>]: MethodOrProperty<T[K]>;
|
|
232
|
+
} & ArrayProvider<T[number]>;
|
|
233
|
+
|
|
234
|
+
// Base type for all other types providing RPC-like interfaces.
|
|
235
|
+
// Rewrites all methods/properties to be `MethodOrProperty`s, while preserving callable types.
|
|
236
|
+
type Provider<T> = MaybeCallableProvider<T> &
|
|
237
|
+
(T extends ReadonlyArray<unknown>
|
|
238
|
+
? number extends T["length"] ? ArrayProvider<T[number]> : TupleProvider<T>
|
|
239
|
+
: {
|
|
240
|
+
[K in Exclude<
|
|
241
|
+
keyof T,
|
|
242
|
+
symbol | keyof StubBase<never>
|
|
243
|
+
>]: MethodOrProperty<T[K]>;
|
|
244
|
+
} & {
|
|
245
|
+
map<V>(
|
|
246
|
+
callback: (value: MapCallbackValue<NonNullable<T>>) => MapCallbackReturn<V>
|
|
247
|
+
): Result<Array<V>>;
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Serialize a value, using Cap'n Web's underlying serialization. This won't be able to serialize
|
|
252
|
+
* RPC stubs, but it will support basic data types.
|
|
253
|
+
*/
|
|
254
|
+
declare function serialize(value: unknown): string;
|
|
255
|
+
/**
|
|
256
|
+
* Deserialize a value serialized using serialize().
|
|
257
|
+
*/
|
|
258
|
+
declare function deserialize(value: string): unknown;
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Interface for an RPC transport, which is a simple bidirectional message stream. Implement this
|
|
262
|
+
* interface if the built-in transports (e.g. for HTTP batch and WebSocket) don't meet your needs.
|
|
263
|
+
*/
|
|
264
|
+
interface RpcTransport {
|
|
265
|
+
/**
|
|
266
|
+
* Sends a message to the other end.
|
|
267
|
+
*/
|
|
268
|
+
send(message: string): Promise<void>;
|
|
269
|
+
/**
|
|
270
|
+
* Receives a message sent by the other end.
|
|
271
|
+
*
|
|
272
|
+
* If and when the transport becomes disconnected, this will reject. The thrown error will be
|
|
273
|
+
* propagated to all outstanding calls and future calls on any stubs associated with the session.
|
|
274
|
+
* If there are no outstanding calls (and none are made in the future), then the error does not
|
|
275
|
+
* propagate anywhere -- this is considered a "clean" shutdown.
|
|
276
|
+
*/
|
|
277
|
+
receive(): Promise<string>;
|
|
278
|
+
/**
|
|
279
|
+
* Indicates that the RPC system has suffered an error that prevents the session from continuing.
|
|
280
|
+
* The transport should ideally try to send any queued messages if it can, and then close the
|
|
281
|
+
* connection. (It's not strictly necessary to deliver queued messages, but the last message sent
|
|
282
|
+
* before abort() is called is often an "abort" message, which communicates the error to the
|
|
283
|
+
* peer, so if that is dropped, the peer may have less information about what happened.)
|
|
284
|
+
*/
|
|
285
|
+
abort?(reason: any): void;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Options to customize behavior of an RPC session. All functions which start a session should
|
|
289
|
+
* optionally accept this.
|
|
290
|
+
*/
|
|
291
|
+
type RpcSessionOptions = {
|
|
292
|
+
/**
|
|
293
|
+
* If provided, this function will be called whenever an `Error` object is serialized (for any
|
|
294
|
+
* reason, not just because it was thrown). This can be used to log errors, and also to redact
|
|
295
|
+
* them.
|
|
296
|
+
*
|
|
297
|
+
* If `onSendError` returns an Error object, than object will be substituted in place of the
|
|
298
|
+
* original. If it has a stack property, the stack will be sent to the client.
|
|
299
|
+
*
|
|
300
|
+
* If `onSendError` doesn't return anything (or is not provided at all), the default behavior is
|
|
301
|
+
* to serialize the error with the stack omitted.
|
|
302
|
+
*/
|
|
303
|
+
onSendError?: (error: Error) => Error | void;
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* For use in Cloudflare Workers: Construct an HTTP response that starts a WebSocket RPC session
|
|
308
|
+
* with the given `localMain`.
|
|
309
|
+
*/
|
|
310
|
+
declare function newWorkersWebSocketRpcResponse(request: Request, localMain?: any, options?: RpcSessionOptions): Response;
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Implements the server end of an HTTP batch session, using standard Fetch API types to represent
|
|
314
|
+
* HTTP requests and responses.
|
|
315
|
+
*
|
|
316
|
+
* @param request The request received from the client initiating the session.
|
|
317
|
+
* @param localMain The main stub or RpcTarget which the server wishes to expose to the client.
|
|
318
|
+
* @param options Optional RPC session options.
|
|
319
|
+
* @returns The HTTP response to return to the client. Note that the returned object has mutable
|
|
320
|
+
* headers, so you can modify them using e.g. `response.headers.set("Foo", "bar")`.
|
|
321
|
+
*/
|
|
322
|
+
declare function newHttpBatchRpcResponse(request: Request, localMain: any, options?: RpcSessionOptions): Promise<Response>;
|
|
323
|
+
/**
|
|
324
|
+
* Implements the server end of an HTTP batch session using traditional Node.js HTTP APIs.
|
|
325
|
+
*
|
|
326
|
+
* @param request The request received from the client initiating the session.
|
|
327
|
+
* @param response The response object, to which the response should be written.
|
|
328
|
+
* @param localMain The main stub or RpcTarget which the server wishes to expose to the client.
|
|
329
|
+
* @param options Optional RPC session options. You can also pass headers to set on the response.
|
|
330
|
+
*/
|
|
331
|
+
declare function nodeHttpBatchRpcResponse(request: IncomingMessage, response: ServerResponse, localMain: any, options?: RpcSessionOptions & {
|
|
332
|
+
headers?: OutgoingHttpHeaders | OutgoingHttpHeader[];
|
|
333
|
+
}): Promise<void>;
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Represents a reference to a remote object, on which methods may be remotely invoked via RPC.
|
|
337
|
+
*
|
|
338
|
+
* `RpcStub` can represent any interface (when using TypeScript, you pass the specific interface
|
|
339
|
+
* type as `T`, but this isn't known at runtime). The way this works is, `RpcStub` is actually a
|
|
340
|
+
* `Proxy`. It makes itself appear as if every possible method / property name is defined. You can
|
|
341
|
+
* invoke any method name, and the invocation will be sent to the server. If it turns out that no
|
|
342
|
+
* such method exists on the remote object, an exception is thrown back. But the client does not
|
|
343
|
+
* actually know, until that point, what methods exist.
|
|
344
|
+
*/
|
|
345
|
+
type RpcStub<T extends RpcCompatible<T>> = Stub<T>;
|
|
346
|
+
declare const RpcStub: {
|
|
347
|
+
new <T extends RpcCompatible<T>>(value: T): RpcStub<T>;
|
|
348
|
+
};
|
|
349
|
+
/**
|
|
350
|
+
* Represents the result of an RPC call.
|
|
351
|
+
*
|
|
352
|
+
* Also used to represent properties. That is, `stub.foo` evaluates to an `RpcPromise` for the
|
|
353
|
+
* value of `foo`.
|
|
354
|
+
*
|
|
355
|
+
* This isn't actually a JavaScript `Promise`. It does, however, have `then()`, `catch()`, and
|
|
356
|
+
* `finally()` methods, like `Promise` does, and because it has a `then()` method, JavaScript will
|
|
357
|
+
* allow you to treat it like a promise, e.g. you can `await` it.
|
|
358
|
+
*
|
|
359
|
+
* An `RpcPromise` is also a proxy, just like `RpcStub`, where calling methods or awaiting
|
|
360
|
+
* properties will make a pipelined network request.
|
|
361
|
+
*
|
|
362
|
+
* Note that and `RpcPromise` is "lazy": the actual final result is not requested from the server
|
|
363
|
+
* until you actually `await` the promise (or call `then()`, etc. on it). This is an optimization:
|
|
364
|
+
* if you only intend to use the promise for pipelining and you never await it, then there's no
|
|
365
|
+
* need to transmit the resolution!
|
|
366
|
+
*/
|
|
367
|
+
type RpcPromise<T extends RpcCompatible<T>> = Stub<T> & Promise<Stubify<T>>;
|
|
368
|
+
declare const RpcPromise: {};
|
|
369
|
+
/**
|
|
370
|
+
* Use to construct an `RpcSession` on top of a custom `RpcTransport`.
|
|
371
|
+
*
|
|
372
|
+
* Most people won't use this. You only need it if you've implemented your own `RpcTransport`.
|
|
373
|
+
*/
|
|
374
|
+
interface RpcSession<T extends RpcCompatible<T> = undefined> {
|
|
375
|
+
getRemoteMain(): RpcStub<T>;
|
|
376
|
+
getStats(): {
|
|
377
|
+
imports: number;
|
|
378
|
+
exports: number;
|
|
379
|
+
};
|
|
380
|
+
drain(): Promise<void>;
|
|
381
|
+
}
|
|
382
|
+
declare const RpcSession: {
|
|
383
|
+
new <T extends RpcCompatible<T> = undefined>(transport: RpcTransport, localMain?: any, options?: RpcSessionOptions): RpcSession<T>;
|
|
384
|
+
};
|
|
385
|
+
/**
|
|
386
|
+
* Classes which are intended to be passed by reference and called over RPC must extend
|
|
387
|
+
* `RpcTarget`. A class which does not extend `RpcTarget` (and which doesn't have built-in support
|
|
388
|
+
* from the RPC system) cannot be passed in an RPC message at all; an exception will be thrown.
|
|
389
|
+
*
|
|
390
|
+
* Note that on Cloudflare Workers, this `RpcTarget` is an alias for the one exported from the
|
|
391
|
+
* "cloudflare:workers" module, so they can be used interchangably.
|
|
392
|
+
*/
|
|
393
|
+
interface RpcTarget extends RpcTargetBranded {
|
|
394
|
+
}
|
|
395
|
+
declare const RpcTarget: {
|
|
396
|
+
new (): RpcTarget;
|
|
397
|
+
};
|
|
398
|
+
/**
|
|
399
|
+
* Empty interface used as default type parameter for sessions where the other side doesn't
|
|
400
|
+
* necessarily export a main interface.
|
|
401
|
+
*/
|
|
402
|
+
interface Empty {
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* Start a WebSocket session given either an already-open WebSocket or a URL.
|
|
406
|
+
*
|
|
407
|
+
* @param webSocket Either the `wss://` URL to connect to, or an already-open WebSocket object to
|
|
408
|
+
* use.
|
|
409
|
+
* @param localMain The main RPC interface to expose to the peer. Returns a stub for the main
|
|
410
|
+
* interface exposed from the peer.
|
|
411
|
+
*/
|
|
412
|
+
declare let newWebSocketRpcSession: <T extends RpcCompatible<T> = Empty>(webSocket: WebSocket | string, localMain?: any, options?: RpcSessionOptions) => RpcStub<T>;
|
|
413
|
+
/**
|
|
414
|
+
* Initiate an HTTP batch session from the client side.
|
|
415
|
+
*
|
|
416
|
+
* The parameters to this method have exactly the same signature as `fetch()`, but the return
|
|
417
|
+
* value is an RpcStub. You can customize anything about the request except for the method
|
|
418
|
+
* (it will always be set to POST) and the body (which the RPC system will fill in).
|
|
419
|
+
*/
|
|
420
|
+
declare let newHttpBatchRpcSession: <T extends RpcCompatible<T>>(urlOrRequest: string | Request, options?: RpcSessionOptions) => RpcStub<T>;
|
|
421
|
+
/**
|
|
422
|
+
* Initiate an RPC session over a MessagePort, which is particularly useful for communicating
|
|
423
|
+
* between an iframe and its parent frame in a browser context. Each side should call this function
|
|
424
|
+
* on its own end of the MessageChannel.
|
|
425
|
+
*/
|
|
426
|
+
declare let newMessagePortRpcSession: <T extends RpcCompatible<T> = Empty>(port: MessagePort, localMain?: any, options?: RpcSessionOptions) => RpcStub<T>;
|
|
427
|
+
/**
|
|
428
|
+
* Implements unified handling of HTTP-batch and WebSocket responses for the Cloudflare Workers
|
|
429
|
+
* Runtime.
|
|
430
|
+
*
|
|
431
|
+
* SECURITY WARNING: This function accepts cross-origin requests. If you do not want this, you
|
|
432
|
+
* should validate the `Origin` header before calling this, or use `newHttpBatchRpcSession()` and
|
|
433
|
+
* `newWebSocketRpcSession()` directly with appropriate security measures for each type of request.
|
|
434
|
+
* But if your API uses in-band authorization (i.e. it has an RPC method that takes the user's
|
|
435
|
+
* credentials as parameters and returns the authorized API), then cross-origin requests should
|
|
436
|
+
* be safe.
|
|
437
|
+
*/
|
|
438
|
+
declare function newWorkersRpcResponse(request: Request, localMain: any): Promise<Response>;
|
|
439
|
+
|
|
440
|
+
export { type RpcCompatible, RpcPromise, RpcSession, type RpcSessionOptions, RpcStub, RpcTarget, type RpcTransport, deserialize, newHttpBatchRpcResponse, newHttpBatchRpcSession, newMessagePortRpcSession, newWebSocketRpcSession, newWorkersRpcResponse, newWorkersWebSocketRpcResponse, nodeHttpBatchRpcResponse, serialize };
|
package/noop.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "capnweb-better-types",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Temporary copy of capnweb dist/index.d.ts (PR #142).",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"types": "./capnweb.d.ts",
|
|
9
|
+
"files": [
|
|
10
|
+
"capnweb.d.ts",
|
|
11
|
+
"noop.js",
|
|
12
|
+
"tsconfig.paths.json",
|
|
13
|
+
"README.md",
|
|
14
|
+
"LICENSE.txt"
|
|
15
|
+
],
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./capnweb.d.ts",
|
|
19
|
+
"default": "./noop.js"
|
|
20
|
+
},
|
|
21
|
+
"./capnweb": {
|
|
22
|
+
"types": "./capnweb.d.ts",
|
|
23
|
+
"default": "./noop.js"
|
|
24
|
+
},
|
|
25
|
+
"./tsconfig": "./tsconfig.paths.json",
|
|
26
|
+
"./package.json": "./package.json"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"capnweb": "^0.5.0"
|
|
30
|
+
},
|
|
31
|
+
"peerDependenciesMeta": {
|
|
32
|
+
"capnweb": {
|
|
33
|
+
"optional": false
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=18"
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"keywords": [
|
|
43
|
+
"capnweb",
|
|
44
|
+
"typescript",
|
|
45
|
+
"types",
|
|
46
|
+
"rpc"
|
|
47
|
+
],
|
|
48
|
+
"scripts": {
|
|
49
|
+
"pack:dry": "npm pack --dry-run"
|
|
50
|
+
}
|
|
51
|
+
}
|