@fungi.computer/capnweb 0.0.0-stage → 0.12.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.txt +21 -0
- package/NOTICE +10 -0
- package/README.md +95 -2
- package/dist/index-bun.cjs +3347 -0
- package/dist/index-bun.cjs.map +1 -0
- package/dist/index-bun.d.cts +478 -0
- package/dist/index-bun.d.ts +478 -0
- package/dist/index-bun.js +3328 -0
- package/dist/index-bun.js.map +1 -0
- package/dist/index-workers.cjs +3262 -0
- package/dist/index-workers.cjs.map +1 -0
- package/dist/index-workers.d.cts +354 -0
- package/dist/index-workers.d.ts +354 -0
- package/dist/index-workers.js +3218 -0
- package/dist/index-workers.js.map +1 -0
- package/dist/index.cjs +3228 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +354 -0
- package/dist/index.d.ts +354 -0
- package/dist/index.js +3212 -0
- package/dist/index.js.map +1 -0
- package/package.json +52 -3
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
import { IncomingMessage, OutgoingHttpHeader, OutgoingHttpHeaders, ServerResponse } from "node:http";
|
|
2
|
+
//#region src/types.d.ts
|
|
3
|
+
declare const __RPC_STUB_BRAND = "__RPC_STUB_BRAND";
|
|
4
|
+
declare const __RPC_TARGET_BRAND = "__RPC_TARGET_BRAND";
|
|
5
|
+
declare const __RPC_MAP_VALUE_BRAND: unique symbol;
|
|
6
|
+
interface RpcTargetBranded {
|
|
7
|
+
[__RPC_TARGET_BRAND]: never;
|
|
8
|
+
}
|
|
9
|
+
type Stubable = RpcTargetBranded | ((...args: never[]) => unknown);
|
|
10
|
+
type IsUnknown<T> = unknown extends T ? true : false;
|
|
11
|
+
type RpcCompatible<T> = (IsUnknown<T> extends true ? unknown : never) | BaseType | 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> | Stub<Stubable> | Stubable;
|
|
12
|
+
interface StubBase<T = unknown> extends Disposable {
|
|
13
|
+
[__RPC_STUB_BRAND]: T;
|
|
14
|
+
dup(): this;
|
|
15
|
+
onRpcBroken(callback: (error: any) => void): void;
|
|
16
|
+
}
|
|
17
|
+
type Stub<T extends RpcCompatible<T>> = T extends object ? Provider<T> & StubBase<T> : StubBase<T>;
|
|
18
|
+
type TypedArray = Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array | Int8Array | Int16Array | Int32Array | BigUint64Array | BigInt64Array | Float32Array | Float64Array;
|
|
19
|
+
type BaseType = void | undefined | null | boolean | number | bigint | string | TypedArray | ArrayBuffer | DataView | Date | Error | RegExp | Blob | ReadableStream<any> | WritableStream<any> | URL | Request | Response | Headers;
|
|
20
|
+
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 : T extends {
|
|
21
|
+
[key: string | number]: any;
|
|
22
|
+
} ? { [K in keyof T as K extends string | number ? K : never]: Stubify<T[K]>; } : T;
|
|
23
|
+
type UnstubifyInner<T> = 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>, ...UnstubifyAll<Tail>] : T extends readonly [] ? readonly [] : T extends readonly [infer Head, ...infer Tail] ? readonly [Unstubify<Head>, ...UnstubifyAll<Tail>] : T extends Array<infer V> ? Array<Unstubify<V>> : T extends ReadonlyArray<infer V> ? ReadonlyArray<Unstubify<V>> : T extends BaseType ? T : T extends {
|
|
24
|
+
[key: string | number]: unknown;
|
|
25
|
+
} ? { [K in keyof T as K extends string | number ? K : never]: Unstubify<T[K]>; } : T;
|
|
26
|
+
type Unstubify<T> = NonStubMembers<T> | UnstubifyInner<T> | Promise<UnstubifyInner<T>> | MapValuePlaceholder<UnstubifyInner<T>>;
|
|
27
|
+
type UnstubifyAll<A extends readonly unknown[]> = { [I in keyof A]: Unstubify<A[I]>; };
|
|
28
|
+
interface MapValuePlaceholder<T> {
|
|
29
|
+
[__RPC_MAP_VALUE_BRAND]: T;
|
|
30
|
+
}
|
|
31
|
+
type NonStubMembers<T> = Exclude<T, StubBase<any>>;
|
|
32
|
+
type MaybeDisposable<T> = T extends object ? Disposable : unknown;
|
|
33
|
+
type RpcPromise$1<T> = T extends Stubable ? Promise<Stub<T>> & Provider<T> & StubBase<T> : Promise<Stubify<T> & MaybeDisposable<T>> & Provider<T> & StubBase<T>;
|
|
34
|
+
type ElideStub<T> = T extends StubBase<infer U> ? IsAny<U> extends true ? T : [U] extends [Stubable] ? U : T : T;
|
|
35
|
+
type PayloadOrStub<T> = T | NoInfer<Stub<Extract<T, Stubable>>>;
|
|
36
|
+
type Result<R> = IsAny<R> extends true ? RpcPromise$1<unknown> : R extends PromiseLike<unknown> & StubBase<infer U> ? Result<U> : R extends StubBase<any> ? RpcPromise$1<ElideStub<R>> : R extends RpcCompatible<R> ? RpcPromise$1<R> : never;
|
|
37
|
+
type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
38
|
+
type MethodOrProperty<V> = V extends ((...args: infer P) => infer R) ? (...args: UnstubifyAll<P>) => Result<Awaited<R>> : Result<Awaited<V>>;
|
|
39
|
+
type MaybeCallableProvider<T> = T extends ((...args: any[]) => any) ? MethodOrProperty<T> : unknown;
|
|
40
|
+
type TupleIndexKeys<T extends ReadonlyArray<unknown>> = Extract<keyof T, `${number}`>;
|
|
41
|
+
type MapCallbackValue<T> = T extends unknown ? Omit<Result<T>, keyof Promise<unknown>> & MaybeCallableProvider<T> & MapValuePlaceholder<T> : never;
|
|
42
|
+
type InvalidNativePromiseInMapResult<T, Seen = never> = T extends unknown ? InvalidNativePromiseInMapResultImpl<T, Seen> : never;
|
|
43
|
+
type InvalidNativePromiseInMapResultImpl<T, Seen> = [T] extends [Seen] ? never : T extends StubBase<any> ? never : 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 {
|
|
44
|
+
[key: string | number]: unknown;
|
|
45
|
+
} ? InvalidNativePromiseInMapResult<T[Extract<keyof T, string | number>], Seen | T> : never;
|
|
46
|
+
type MapCallbackReturn<T> = InvalidNativePromiseInMapResult<T> extends never ? T : never;
|
|
47
|
+
type ArrayProvider<E> = { [K in number]: MethodOrProperty<E>; } & {
|
|
48
|
+
map<V>(callback: (elem: MapCallbackValue<E>) => MapCallbackReturn<V>): Result<Array<V>>;
|
|
49
|
+
};
|
|
50
|
+
type TupleProvider<T extends ReadonlyArray<unknown>> = { [K in TupleIndexKeys<T>]: MethodOrProperty<T[K]>; } & ArrayProvider<T[number]>;
|
|
51
|
+
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>>]: MethodOrProperty<T[K]>; } & {
|
|
52
|
+
map<V>(callback: (value: MapCallbackValue<NonNullable<T>>) => MapCallbackReturn<V>): Result<Array<V>>;
|
|
53
|
+
});
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region src/serialize.d.ts
|
|
56
|
+
/**
|
|
57
|
+
* Encoding levels determine what representation the RPC system hands to the transport.
|
|
58
|
+
* Each level names what the transport can assume about message values.
|
|
59
|
+
*
|
|
60
|
+
* - `"string"`: JSON string. Default, used by HTTP batch and WebSocket transports.
|
|
61
|
+
* - `"jsonCompatible"`: JSON-compatible JS value tree. For custom encoders.
|
|
62
|
+
* - `"jsonCompatibleWithBytes"`: Like `"jsonCompatible"` but Uint8Array stays raw.
|
|
63
|
+
* - `"structuredClonable"`: Structured-clonable native values pass through where possible.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* // What happens to Uint8Array([1, 2, 3]) at each level:
|
|
68
|
+
* "string" → '["bytes","AQID"]' // JSON string with base64
|
|
69
|
+
* "jsonCompatible" → ["bytes", "AQID"] // JS array with base64
|
|
70
|
+
* "jsonCompatibleWithBytes" → ["bytes", Uint8Array] // JS array with raw bytes
|
|
71
|
+
* "structuredClonable" → ["bytes", Uint8Array] // + Date, BigInt stay native
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
type EncodingLevel = "string" | "jsonCompatible" | "jsonCompatibleWithBytes" | "structuredClonable";
|
|
75
|
+
interface RpcLimits {
|
|
76
|
+
maxBigIntDigits: number;
|
|
77
|
+
maxDepth: number;
|
|
78
|
+
maxMessageSize: number;
|
|
79
|
+
}
|
|
80
|
+
declare const DEFAULT_MAX_DEPTH = 256;
|
|
81
|
+
declare const DEFAULT_LIMITS: RpcLimits;
|
|
82
|
+
/**
|
|
83
|
+
* Serialize a value, using Cap'n Web's underlying serialization. This won't be able to serialize
|
|
84
|
+
* RPC stubs, but it will support basic data types.
|
|
85
|
+
*/
|
|
86
|
+
declare function serialize(value: unknown): string;
|
|
87
|
+
/**
|
|
88
|
+
* Deserialize a value serialized using serialize().
|
|
89
|
+
*/
|
|
90
|
+
declare function deserialize(value: string): unknown;
|
|
91
|
+
//#endregion
|
|
92
|
+
//#region src/rpc.d.ts
|
|
93
|
+
/**
|
|
94
|
+
* Interface for a string-based RPC transport. This is the default transport type — no
|
|
95
|
+
* `encodingLevel` field is needed. Messages are JSON strings. Implement this interface if the
|
|
96
|
+
* built-in transports (e.g. for HTTP batch and WebSocket) don't meet your needs.
|
|
97
|
+
*/
|
|
98
|
+
interface RpcTransport {
|
|
99
|
+
/**
|
|
100
|
+
* The encoding level this transport works with. For this interface it is always "string";
|
|
101
|
+
* it may be omitted. (See `RpcTransportWithCustomEncoding` for the other levels.)
|
|
102
|
+
*/
|
|
103
|
+
readonly encodingLevel?: "string";
|
|
104
|
+
/**
|
|
105
|
+
* Sends a message to the other end. May optionally return a promise; if the promise rejects,
|
|
106
|
+
* the session is aborted.
|
|
107
|
+
*/
|
|
108
|
+
send(message: string): void | Promise<void>;
|
|
109
|
+
/**
|
|
110
|
+
* Receives a message sent by the other end.
|
|
111
|
+
*
|
|
112
|
+
* If and when the transport becomes disconnected, this will reject. The thrown error will be
|
|
113
|
+
* propagated to all outstanding calls and future calls on any stubs associated with the session.
|
|
114
|
+
* If there are no outstanding calls (and none are made in the future), then the error does not
|
|
115
|
+
* propagate anywhere -- this is considered a "clean" shutdown.
|
|
116
|
+
*/
|
|
117
|
+
receive(): Promise<string>;
|
|
118
|
+
/**
|
|
119
|
+
* Indicates that the RPC system has suffered an error that prevents the session from continuing.
|
|
120
|
+
* The transport should ideally try to send any queued messages if it can, and then close the
|
|
121
|
+
* connection. (It's not strictly necessary to deliver queued messages, but the last message sent
|
|
122
|
+
* before abort() is called is often an "abort" message, which communicates the error to the
|
|
123
|
+
* peer, so if that is dropped, the peer may have less information about what happened.)
|
|
124
|
+
*/
|
|
125
|
+
abort?(reason: any): void;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Interface for a transport that receives partially encoded JS values instead of JSON strings.
|
|
129
|
+
* The selected `encodingLevel` describes what the transport can assume about message values.
|
|
130
|
+
*/
|
|
131
|
+
interface RpcTransportWithCustomEncoding {
|
|
132
|
+
/**
|
|
133
|
+
* The encoding level this transport works with.
|
|
134
|
+
*
|
|
135
|
+
* - "jsonCompatible": JSON-compatible JS value tree; transport handles final serialization.
|
|
136
|
+
* - "jsonCompatibleWithBytes": Like "jsonCompatible" but Uint8Array values are left raw.
|
|
137
|
+
* - "structuredClonable": Structured-clonable native values pass through where possible.
|
|
138
|
+
*/
|
|
139
|
+
readonly encodingLevel: "jsonCompatible" | "jsonCompatibleWithBytes" | "structuredClonable";
|
|
140
|
+
/**
|
|
141
|
+
* Encodes and sends a message to the other end. Returns the encoded byte size if known.
|
|
142
|
+
* If the size is unavailable, return void; Cap'n Web will estimate stream message sizes for
|
|
143
|
+
* flow control. Send errors should be propagated via `receive()` rejecting.
|
|
144
|
+
*/
|
|
145
|
+
send(message: unknown): number | void;
|
|
146
|
+
/**
|
|
147
|
+
* Receives and decodes a message sent by the other end.
|
|
148
|
+
*
|
|
149
|
+
* If and when the transport becomes disconnected, this will reject. The thrown error will be
|
|
150
|
+
* propagated to all outstanding calls and future calls on any stubs associated with the session.
|
|
151
|
+
* If there are no outstanding calls (and none are made in the future), then the error does not
|
|
152
|
+
* propagate anywhere -- this is considered a "clean" shutdown.
|
|
153
|
+
*/
|
|
154
|
+
receive(): Promise<unknown>;
|
|
155
|
+
/**
|
|
156
|
+
* Indicates that the RPC system has suffered an error that prevents the session from continuing.
|
|
157
|
+
* The transport should ideally try to send any queued messages if it can, and then close the
|
|
158
|
+
* connection. (It's not strictly necessary to deliver queued messages, but the last message sent
|
|
159
|
+
* before abort() is called is often an "abort" message, which communicates the error to the
|
|
160
|
+
* peer, so if that is dropped, the peer may have less information about what happened.)
|
|
161
|
+
*/
|
|
162
|
+
abort?(reason: any): void;
|
|
163
|
+
}
|
|
164
|
+
/** Any supported transport type. */
|
|
165
|
+
type AnyRpcTransport = RpcTransport | RpcTransportWithCustomEncoding;
|
|
166
|
+
/**
|
|
167
|
+
* Options to customize behavior of an RPC session. All functions which start a session should
|
|
168
|
+
* optionally accept this.
|
|
169
|
+
*/
|
|
170
|
+
type RpcSessionOptions = {
|
|
171
|
+
/**
|
|
172
|
+
* If provided, this function will be called whenever an `Error` object is serialized (for any
|
|
173
|
+
* reason, not just because it was thrown). This can be used to log errors, and also to redact
|
|
174
|
+
* them.
|
|
175
|
+
*
|
|
176
|
+
* If `onSendError` returns an Error object, than object will be substituted in place of the
|
|
177
|
+
* original. If it has a stack property, the stack will be sent to the client.
|
|
178
|
+
*
|
|
179
|
+
* If `onSendError` doesn't return anything (or is not provided at all), the default behavior is
|
|
180
|
+
* to serialize the error with the stack omitted.
|
|
181
|
+
*/
|
|
182
|
+
onSendError?: (error: Error) => Error | void;
|
|
183
|
+
/**
|
|
184
|
+
* Overrides for the resource limits enforced while deserializing messages from the peer. Any
|
|
185
|
+
* field left unset falls back to `DEFAULT_LIMITS`. These guard against resource-exhaustion
|
|
186
|
+
* attacks from untrusted peers; see `RpcLimits` for the meaning and defaults of each field.
|
|
187
|
+
*
|
|
188
|
+
* Limits are a purely local, receiver-side decision -- the protocol has no negotiation step, so
|
|
189
|
+
* the peer never learns these values. A message that exceeds a limit is rejected, aborting the
|
|
190
|
+
* session.
|
|
191
|
+
*/
|
|
192
|
+
limits?: Partial<RpcLimits>;
|
|
193
|
+
};
|
|
194
|
+
//#endregion
|
|
195
|
+
//#region src/websocket.d.ts
|
|
196
|
+
/**
|
|
197
|
+
* For use in Cloudflare Workers: Construct an HTTP response that starts a WebSocket RPC session
|
|
198
|
+
* with the given `localMain`.
|
|
199
|
+
*/
|
|
200
|
+
declare function newWorkersWebSocketRpcResponse(request: Request, localMain?: any, options?: RpcSessionOptions): Response;
|
|
201
|
+
/**
|
|
202
|
+
* Generic WebSocket transport. Default `T = string` is backward-compatible and satisfies
|
|
203
|
+
* `RpcTransport`. Use `T = ArrayBuffer` as a building block for binary transports.
|
|
204
|
+
*/
|
|
205
|
+
declare class WebSocketTransport<T extends string | ArrayBuffer = string> {
|
|
206
|
+
#private;
|
|
207
|
+
constructor(webSocket: WebSocket);
|
|
208
|
+
send(message: T): void;
|
|
209
|
+
receive(): Promise<T>;
|
|
210
|
+
abort(reason: any): void;
|
|
211
|
+
}
|
|
212
|
+
//#endregion
|
|
213
|
+
//#region src/batch.d.ts
|
|
214
|
+
/**
|
|
215
|
+
* Implements the server end of an HTTP batch session, using standard Fetch API types to represent
|
|
216
|
+
* HTTP requests and responses.
|
|
217
|
+
*
|
|
218
|
+
* @param request The request received from the client initiating the session.
|
|
219
|
+
* @param localMain The main stub or RpcTarget which the server wishes to expose to the client.
|
|
220
|
+
* @param options Optional RPC session options.
|
|
221
|
+
* @returns The HTTP response to return to the client. Note that the returned object has mutable
|
|
222
|
+
* headers, so you can modify them using e.g. `response.headers.set("Foo", "bar")`.
|
|
223
|
+
*/
|
|
224
|
+
declare function newHttpBatchRpcResponse(request: Request, localMain: any, options?: RpcSessionOptions): Promise<Response>;
|
|
225
|
+
/**
|
|
226
|
+
* Implements the server end of an HTTP batch session using traditional Node.js HTTP APIs.
|
|
227
|
+
*
|
|
228
|
+
* @param request The request received from the client initiating the session.
|
|
229
|
+
* @param response The response object, to which the response should be written.
|
|
230
|
+
* @param localMain The main stub or RpcTarget which the server wishes to expose to the client.
|
|
231
|
+
* @param options Optional RPC session options. You can also pass headers to set on the response.
|
|
232
|
+
*/
|
|
233
|
+
declare function nodeHttpBatchRpcResponse(request: IncomingMessage, response: ServerResponse, localMain: any, options?: RpcSessionOptions & {
|
|
234
|
+
headers?: OutgoingHttpHeaders | OutgoingHttpHeader[];
|
|
235
|
+
}): Promise<void>;
|
|
236
|
+
//#endregion
|
|
237
|
+
//#region src/index.d.ts
|
|
238
|
+
/**
|
|
239
|
+
* Represents a reference to a remote object, on which methods may be remotely invoked via RPC.
|
|
240
|
+
*
|
|
241
|
+
* `RpcStub` can represent any interface (when using TypeScript, you pass the specific interface
|
|
242
|
+
* type as `T`, but this isn't known at runtime). The way this works is, `RpcStub` is actually a
|
|
243
|
+
* `Proxy`. It makes itself appear as if every possible method / property name is defined. You can
|
|
244
|
+
* invoke any method name, and the invocation will be sent to the server. If it turns out that no
|
|
245
|
+
* such method exists on the remote object, an exception is thrown back. But the client does not
|
|
246
|
+
* actually know, until that point, what methods exist.
|
|
247
|
+
*/
|
|
248
|
+
type RpcStub<T extends RpcCompatible<T>> = Stub<T>;
|
|
249
|
+
declare const RpcStub: {
|
|
250
|
+
new <T extends RpcCompatible<T>>(value: T): RpcStub<T>;
|
|
251
|
+
};
|
|
252
|
+
/**
|
|
253
|
+
* Represents the result of an RPC call.
|
|
254
|
+
*
|
|
255
|
+
* Also used to represent properties. That is, `stub.foo` evaluates to an `RpcPromise` for the
|
|
256
|
+
* value of `foo`.
|
|
257
|
+
*
|
|
258
|
+
* This isn't actually a JavaScript `Promise`. It does, however, have `then()`, `catch()`, and
|
|
259
|
+
* `finally()` methods, like `Promise` does, and because it has a `then()` method, JavaScript will
|
|
260
|
+
* allow you to treat it like a promise, e.g. you can `await` it.
|
|
261
|
+
*
|
|
262
|
+
* An `RpcPromise` is also a proxy, just like `RpcStub`, where calling methods or awaiting
|
|
263
|
+
* properties will make a pipelined network request.
|
|
264
|
+
*
|
|
265
|
+
* Note that and `RpcPromise` is "lazy": the actual final result is not requested from the server
|
|
266
|
+
* until you actually `await` the promise (or call `then()`, etc. on it). This is an optimization:
|
|
267
|
+
* if you only intend to use the promise for pipelining and you never await it, then there's no
|
|
268
|
+
* need to transmit the resolution!
|
|
269
|
+
*
|
|
270
|
+
* You may also construct an `RpcPromise` yourself from a regular `Promise`, using
|
|
271
|
+
* `new RpcPromise(promise)`, allowing you to perform promise pipelining on a local promise. This
|
|
272
|
+
* is semantically identical to creating a local-loopback RPC that returns the promise, and then
|
|
273
|
+
* invoking it: pipelined calls wait until the promise resolves, then are delivered, in order, to
|
|
274
|
+
* the resolution. This is useful when you plan to obtain some stub in the future, but want to
|
|
275
|
+
* allow code to start queuing calls on it immediately. Note that the `RpcPromise` takes
|
|
276
|
+
* ownership of the resolution: disposing it disposes the resolution, so resolve the promise
|
|
277
|
+
* with a `dup()` if you also intend to keep the stub.
|
|
278
|
+
*/
|
|
279
|
+
type RpcPromise<T extends RpcCompatible<T>> = RpcPromise$1<T>;
|
|
280
|
+
declare const RpcPromise: {
|
|
281
|
+
new <T extends RpcCompatible<T>>(value: Promise<T>): RpcPromise$1<ElideStub<T>>;
|
|
282
|
+
new <T extends RpcCompatible<T>>(value: Promise<PayloadOrStub<T>>): RpcPromise$1<ElideStub<T>>;
|
|
283
|
+
};
|
|
284
|
+
/**
|
|
285
|
+
* Use to construct an `RpcSession` on top of a custom `RpcTransport`.
|
|
286
|
+
*
|
|
287
|
+
* Most people won't use this. You only need it if you've implemented your own `RpcTransport`.
|
|
288
|
+
*/
|
|
289
|
+
interface RpcSession<T extends RpcCompatible<T> = undefined> {
|
|
290
|
+
getRemoteMain(): RpcStub<T>;
|
|
291
|
+
getStats(): {
|
|
292
|
+
imports: number;
|
|
293
|
+
exports: number;
|
|
294
|
+
};
|
|
295
|
+
drain(): Promise<void>;
|
|
296
|
+
}
|
|
297
|
+
declare const RpcSession: {
|
|
298
|
+
new <T extends RpcCompatible<T> = undefined>(transport: AnyRpcTransport, localMain?: any, options?: RpcSessionOptions): RpcSession<T>;
|
|
299
|
+
};
|
|
300
|
+
/**
|
|
301
|
+
* Classes which are intended to be passed by reference and called over RPC must extend
|
|
302
|
+
* `RpcTarget`. A class which does not extend `RpcTarget` (and which doesn't have built-in support
|
|
303
|
+
* from the RPC system) cannot be passed in an RPC message at all; an exception will be thrown.
|
|
304
|
+
*
|
|
305
|
+
* Note that on Cloudflare Workers, this `RpcTarget` is an alias for the one exported from the
|
|
306
|
+
* "cloudflare:workers" module, so they can be used interchangably.
|
|
307
|
+
*/
|
|
308
|
+
interface RpcTarget extends RpcTargetBranded {}
|
|
309
|
+
declare const RpcTarget: {
|
|
310
|
+
new (): RpcTarget;
|
|
311
|
+
};
|
|
312
|
+
/**
|
|
313
|
+
* Empty interface used as default type parameter for sessions where the other side doesn't
|
|
314
|
+
* necessarily export a main interface.
|
|
315
|
+
*/
|
|
316
|
+
interface Empty {}
|
|
317
|
+
/**
|
|
318
|
+
* Start a WebSocket session given either an already-open WebSocket or a URL.
|
|
319
|
+
*
|
|
320
|
+
* @param webSocket Either the `wss://` URL to connect to, or an already-open WebSocket object to
|
|
321
|
+
* use.
|
|
322
|
+
* @param localMain The main RPC interface to expose to the peer. Returns a stub for the main
|
|
323
|
+
* interface exposed from the peer.
|
|
324
|
+
*/
|
|
325
|
+
declare let newWebSocketRpcSession: <T extends RpcCompatible<T> = Empty>(webSocket: WebSocket | string, localMain?: any, options?: RpcSessionOptions) => RpcStub<T>;
|
|
326
|
+
/**
|
|
327
|
+
* Initiate an HTTP batch session from the client side.
|
|
328
|
+
*
|
|
329
|
+
* The parameters to this method have exactly the same signature as `fetch()`, but the return
|
|
330
|
+
* value is an RpcStub. You can customize anything about the request except for the method
|
|
331
|
+
* (it will always be set to POST) and the body (which the RPC system will fill in).
|
|
332
|
+
*/
|
|
333
|
+
declare let newHttpBatchRpcSession: <T extends RpcCompatible<T>>(urlOrRequest: string | Request, options?: RpcSessionOptions) => RpcStub<T>;
|
|
334
|
+
/**
|
|
335
|
+
* Initiate an RPC session over a MessagePort, which is particularly useful for communicating
|
|
336
|
+
* between an iframe and its parent frame in a browser context. Each side should call this function
|
|
337
|
+
* on its own end of the MessageChannel.
|
|
338
|
+
*/
|
|
339
|
+
declare let newMessagePortRpcSession: <T extends RpcCompatible<T> = Empty>(port: MessagePort, localMain?: any, options?: RpcSessionOptions) => RpcStub<T>;
|
|
340
|
+
/**
|
|
341
|
+
* Implements unified handling of HTTP-batch and WebSocket responses for the Cloudflare Workers
|
|
342
|
+
* Runtime.
|
|
343
|
+
*
|
|
344
|
+
* SECURITY WARNING: This function accepts cross-origin requests. If you do not want this, you
|
|
345
|
+
* should validate the `Origin` header before calling this, or use `newHttpBatchRpcSession()` and
|
|
346
|
+
* `newWebSocketRpcSession()` directly with appropriate security measures for each type of request.
|
|
347
|
+
* But if your API uses in-band authorization (i.e. it has an RPC method that takes the user's
|
|
348
|
+
* credentials as parameters and returns the authorized API), then cross-origin requests should
|
|
349
|
+
* be safe.
|
|
350
|
+
*/
|
|
351
|
+
declare function newWorkersRpcResponse(request: Request, localMain: any, options?: RpcSessionOptions): Promise<Response>;
|
|
352
|
+
//#endregion
|
|
353
|
+
export { type AnyRpcTransport, DEFAULT_LIMITS, DEFAULT_MAX_DEPTH, type EncodingLevel, type RpcCompatible, type RpcLimits, RpcPromise, RpcSession, type RpcSessionOptions, RpcStub, RpcTarget, type RpcTransport, type RpcTransportWithCustomEncoding, WebSocketTransport, deserialize, newHttpBatchRpcResponse, newHttpBatchRpcSession, newMessagePortRpcSession, newWebSocketRpcSession, newWorkersRpcResponse, newWorkersWebSocketRpcResponse, nodeHttpBatchRpcResponse, serialize };
|
|
354
|
+
//# sourceMappingURL=index.d.ts.map
|