capnweb 0.0.0-2cb51eb → 0.0.0-2de5871
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 +45 -2
- package/dist/index-bun.cjs +448 -106
- package/dist/index-bun.cjs.map +1 -1
- package/dist/index-bun.d.cts +115 -12
- package/dist/index-bun.d.ts +115 -12
- package/dist/index-bun.js +446 -107
- package/dist/index-bun.js.map +1 -1
- package/dist/index-workers.cjs +448 -106
- package/dist/index-workers.cjs.map +1 -1
- package/dist/index-workers.d.cts +112 -12
- package/dist/index-workers.d.ts +112 -12
- package/dist/index-workers.js +446 -107
- package/dist/index-workers.js.map +1 -1
- package/dist/index.cjs +448 -106
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +112 -12
- package/dist/index.d.ts +112 -12
- package/dist/index.js +446 -107
- package/dist/index.js.map +1 -1
- package/package.json +6 -5
package/dist/index.d.cts
CHANGED
|
@@ -39,8 +39,9 @@ interface StubBase<T = unknown> extends Disposable {
|
|
|
39
39
|
}
|
|
40
40
|
type Stub<T extends RpcCompatible<T>> = T extends object ? Provider<T> & StubBase<T> : StubBase<T>;
|
|
41
41
|
type TypedArray = Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array | Int8Array | Int16Array | Int32Array | BigUint64Array | BigInt64Array | Float32Array | Float64Array; // This represents all the types that can be sent as-is over an RPC boundary
|
|
42
|
-
type BaseType = void | undefined | null | boolean | number | bigint | string | TypedArray | ArrayBuffer | DataView | Date | Error | RegExp | Blob | ReadableStream<
|
|
43
|
-
|
|
|
42
|
+
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; // Recursively rewrite all `Stubable` types with `Stub`s, and resolve promises.
|
|
44
45
|
// prettier-ignore
|
|
45
46
|
type Stubify<T> = T extends Stubable ? Stub<T> : T extends Promise<infer U> ? Stubify<U> : T extends StubBase<any> ? 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 // When using "unknown" instead of "any", interfaces are not stubified.
|
|
46
47
|
: T extends {
|
|
@@ -69,7 +70,13 @@ interface MapValuePlaceholder<T> {
|
|
|
69
70
|
}
|
|
70
71
|
type NonStubMembers<T> = Exclude<T, StubBase<any>>; // Utility type for adding `Disposable`s to `object` types only.
|
|
71
72
|
// Note `unknown & T` is equivalent to `T`.
|
|
72
|
-
type MaybeDisposable<T> = T extends object ? Disposable : unknown; //
|
|
73
|
+
type MaybeDisposable<T> = T extends object ? Disposable : unknown; // The type behind the public `RpcPromise` (re-exported with its `RpcCompatible` constraint from
|
|
74
|
+
// index.ts). Defined here so that `Result`'s arms below resolve to the exact same alias
|
|
75
|
+
// instantiation: when both sides of an assignability check are the same alias applied to the
|
|
76
|
+
// same type arguments, the checker short-circuits on identity instead of recursing through the
|
|
77
|
+
// full structural comparison of these mutually-recursive types.
|
|
78
|
+
type RpcPromise$1<T> = T extends Stubable ? Promise<Stub<T>> & Provider<T> & StubBase<T> : Promise<Stubify<T> & MaybeDisposable<T>> & Provider<T> & StubBase<T>;
|
|
79
|
+
// Type for method return or property on an RPC interface.
|
|
73
80
|
// - Stubable types are replaced by stubs.
|
|
74
81
|
// - RpcCompatible types are passed by value, with stubable types replaced by stubs
|
|
75
82
|
// and a top-level `Disposer`.
|
|
@@ -77,7 +84,7 @@ type MaybeDisposable<T> = T extends object ? Disposable : unknown; // Type for m
|
|
|
77
84
|
// Technically, we use custom thenables here, but they quack like `Promise`s.
|
|
78
85
|
// Intersecting with `(Maybe)Provider` allows pipelining.
|
|
79
86
|
// prettier-ignore
|
|
80
|
-
type Result<R> = IsAny<R> extends true ? UnknownResult : IsUnknown<R> extends true ? UnknownResult : R extends Stubable ?
|
|
87
|
+
type Result<R> = IsAny<R> extends true ? UnknownResult : IsUnknown<R> extends true ? UnknownResult : R extends Stubable ? RpcPromise$1<R> : R extends RpcCompatible<R> ? RpcPromise$1<R> : never;
|
|
81
88
|
type IsAny<T> = 0 extends (1 & T) ? true : false;
|
|
82
89
|
type UnknownResult = Promise<unknown> & Provider<unknown> & StubBase<unknown>; // Type for method or property on an RPC interface.
|
|
83
90
|
// For methods, unwrap `Stub`s in parameters, and rewrite returns to be `Result`s.
|
|
@@ -107,6 +114,32 @@ type Provider<T> = MaybeCallableProvider<T> & (T extends ReadonlyArray<unknown>
|
|
|
107
114
|
});
|
|
108
115
|
//#endregion
|
|
109
116
|
//#region src/serialize.d.ts
|
|
117
|
+
/**
|
|
118
|
+
* Encoding levels determine what representation the RPC system hands to the transport.
|
|
119
|
+
* Each level names what the transport can assume about message values.
|
|
120
|
+
*
|
|
121
|
+
* - `"string"`: JSON string. Default, used by HTTP batch and WebSocket transports.
|
|
122
|
+
* - `"jsonCompatible"`: JSON-compatible JS value tree. For custom encoders.
|
|
123
|
+
* - `"jsonCompatibleWithBytes"`: Like `"jsonCompatible"` but Uint8Array stays raw.
|
|
124
|
+
* - `"structuredClonable"`: Structured-clonable native values pass through where possible.
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```ts
|
|
128
|
+
* // What happens to Uint8Array([1, 2, 3]) at each level:
|
|
129
|
+
* "string" → '["bytes","AQID"]' // JSON string with base64
|
|
130
|
+
* "jsonCompatible" → ["bytes", "AQID"] // JS array with base64
|
|
131
|
+
* "jsonCompatibleWithBytes" → ["bytes", Uint8Array] // JS array with raw bytes
|
|
132
|
+
* "structuredClonable" → ["bytes", Uint8Array] // + Date, BigInt stay native
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
type EncodingLevel = "string" | "jsonCompatible" | "jsonCompatibleWithBytes" | "structuredClonable";
|
|
136
|
+
interface RpcLimits {
|
|
137
|
+
maxBigIntDigits: number;
|
|
138
|
+
maxDepth: number;
|
|
139
|
+
maxMessageSize: number;
|
|
140
|
+
}
|
|
141
|
+
declare const DEFAULT_MAX_DEPTH = 256;
|
|
142
|
+
declare const DEFAULT_LIMITS: RpcLimits;
|
|
110
143
|
/**
|
|
111
144
|
* Serialize a value, using Cap'n Web's underlying serialization. This won't be able to serialize
|
|
112
145
|
* RPC stubs, but it will support basic data types.
|
|
@@ -119,14 +152,21 @@ declare function deserialize(value: string): unknown;
|
|
|
119
152
|
//#endregion
|
|
120
153
|
//#region src/rpc.d.ts
|
|
121
154
|
/**
|
|
122
|
-
* Interface for
|
|
123
|
-
*
|
|
155
|
+
* Interface for a string-based RPC transport. This is the default transport type — no
|
|
156
|
+
* `encodingLevel` field is needed. Messages are JSON strings. Implement this interface if the
|
|
157
|
+
* built-in transports (e.g. for HTTP batch and WebSocket) don't meet your needs.
|
|
124
158
|
*/
|
|
125
159
|
interface RpcTransport {
|
|
126
160
|
/**
|
|
127
|
-
*
|
|
161
|
+
* The encoding level this transport works with. For this interface it is always "string";
|
|
162
|
+
* it may be omitted. (See `RpcTransportWithCustomEncoding` for the other levels.)
|
|
128
163
|
*/
|
|
129
|
-
|
|
164
|
+
readonly encodingLevel?: "string";
|
|
165
|
+
/**
|
|
166
|
+
* Sends a message to the other end. May optionally return a promise; if the promise rejects,
|
|
167
|
+
* the session is aborted.
|
|
168
|
+
*/
|
|
169
|
+
send(message: string): void | Promise<void>;
|
|
130
170
|
/**
|
|
131
171
|
* Receives a message sent by the other end.
|
|
132
172
|
*
|
|
@@ -145,6 +185,45 @@ interface RpcTransport {
|
|
|
145
185
|
*/
|
|
146
186
|
abort?(reason: any): void;
|
|
147
187
|
}
|
|
188
|
+
/**
|
|
189
|
+
* Interface for a transport that receives partially encoded JS values instead of JSON strings.
|
|
190
|
+
* The selected `encodingLevel` describes what the transport can assume about message values.
|
|
191
|
+
*/
|
|
192
|
+
interface RpcTransportWithCustomEncoding {
|
|
193
|
+
/**
|
|
194
|
+
* The encoding level this transport works with.
|
|
195
|
+
*
|
|
196
|
+
* - "jsonCompatible": JSON-compatible JS value tree; transport handles final serialization.
|
|
197
|
+
* - "jsonCompatibleWithBytes": Like "jsonCompatible" but Uint8Array values are left raw.
|
|
198
|
+
* - "structuredClonable": Structured-clonable native values pass through where possible.
|
|
199
|
+
*/
|
|
200
|
+
readonly encodingLevel: "jsonCompatible" | "jsonCompatibleWithBytes" | "structuredClonable";
|
|
201
|
+
/**
|
|
202
|
+
* Encodes and sends a message to the other end. Returns the encoded byte size if known.
|
|
203
|
+
* If the size is unavailable, return void; Cap'n Web will estimate stream message sizes for
|
|
204
|
+
* flow control. Send errors should be propagated via `receive()` rejecting.
|
|
205
|
+
*/
|
|
206
|
+
send(message: unknown): number | void;
|
|
207
|
+
/**
|
|
208
|
+
* Receives and decodes a message sent by the other end.
|
|
209
|
+
*
|
|
210
|
+
* If and when the transport becomes disconnected, this will reject. The thrown error will be
|
|
211
|
+
* propagated to all outstanding calls and future calls on any stubs associated with the session.
|
|
212
|
+
* If there are no outstanding calls (and none are made in the future), then the error does not
|
|
213
|
+
* propagate anywhere -- this is considered a "clean" shutdown.
|
|
214
|
+
*/
|
|
215
|
+
receive(): Promise<unknown>;
|
|
216
|
+
/**
|
|
217
|
+
* Indicates that the RPC system has suffered an error that prevents the session from continuing.
|
|
218
|
+
* The transport should ideally try to send any queued messages if it can, and then close the
|
|
219
|
+
* connection. (It's not strictly necessary to deliver queued messages, but the last message sent
|
|
220
|
+
* before abort() is called is often an "abort" message, which communicates the error to the
|
|
221
|
+
* peer, so if that is dropped, the peer may have less information about what happened.)
|
|
222
|
+
*/
|
|
223
|
+
abort?(reason: any): void;
|
|
224
|
+
}
|
|
225
|
+
/** Any supported transport type. */
|
|
226
|
+
type AnyRpcTransport = RpcTransport | RpcTransportWithCustomEncoding;
|
|
148
227
|
/**
|
|
149
228
|
* Options to customize behavior of an RPC session. All functions which start a session should
|
|
150
229
|
* optionally accept this.
|
|
@@ -162,6 +241,16 @@ type RpcSessionOptions = {
|
|
|
162
241
|
* to serialize the error with the stack omitted.
|
|
163
242
|
*/
|
|
164
243
|
onSendError?: (error: Error) => Error | void;
|
|
244
|
+
/**
|
|
245
|
+
* Overrides for the resource limits enforced while deserializing messages from the peer. Any
|
|
246
|
+
* field left unset falls back to `DEFAULT_LIMITS`. These guard against resource-exhaustion
|
|
247
|
+
* attacks from untrusted peers; see `RpcLimits` for the meaning and defaults of each field.
|
|
248
|
+
*
|
|
249
|
+
* Limits are a purely local, receiver-side decision -- the protocol has no negotiation step, so
|
|
250
|
+
* the peer never learns these values. A message that exceeds a limit is rejected, aborting the
|
|
251
|
+
* session.
|
|
252
|
+
*/
|
|
253
|
+
limits?: Partial<RpcLimits>;
|
|
165
254
|
};
|
|
166
255
|
//#endregion
|
|
167
256
|
//#region src/websocket.d.ts
|
|
@@ -170,6 +259,17 @@ type RpcSessionOptions = {
|
|
|
170
259
|
* with the given `localMain`.
|
|
171
260
|
*/
|
|
172
261
|
declare function newWorkersWebSocketRpcResponse(request: Request, localMain?: any, options?: RpcSessionOptions): Response;
|
|
262
|
+
/**
|
|
263
|
+
* Generic WebSocket transport. Default `T = string` is backward-compatible and satisfies
|
|
264
|
+
* `RpcTransport`. Use `T = ArrayBuffer` as a building block for binary transports.
|
|
265
|
+
*/
|
|
266
|
+
declare class WebSocketTransport<T extends string | ArrayBuffer = string> {
|
|
267
|
+
#private;
|
|
268
|
+
constructor(webSocket: WebSocket);
|
|
269
|
+
send(message: T): void;
|
|
270
|
+
receive(): Promise<T>;
|
|
271
|
+
abort(reason: any): void;
|
|
272
|
+
}
|
|
173
273
|
//#endregion
|
|
174
274
|
//#region src/batch.d.ts
|
|
175
275
|
/**
|
|
@@ -228,7 +328,7 @@ declare const RpcStub: {
|
|
|
228
328
|
* if you only intend to use the promise for pipelining and you never await it, then there's no
|
|
229
329
|
* need to transmit the resolution!
|
|
230
330
|
*/
|
|
231
|
-
type RpcPromise<T extends RpcCompatible<T>> =
|
|
331
|
+
type RpcPromise<T extends RpcCompatible<T>> = RpcPromise$1<T>;
|
|
232
332
|
declare const RpcPromise: {};
|
|
233
333
|
/**
|
|
234
334
|
* Use to construct an `RpcSession` on top of a custom `RpcTransport`.
|
|
@@ -244,7 +344,7 @@ interface RpcSession<T extends RpcCompatible<T> = undefined> {
|
|
|
244
344
|
drain(): Promise<void>;
|
|
245
345
|
}
|
|
246
346
|
declare const RpcSession: {
|
|
247
|
-
new <T extends RpcCompatible<T> = undefined>(transport:
|
|
347
|
+
new <T extends RpcCompatible<T> = undefined>(transport: AnyRpcTransport, localMain?: any, options?: RpcSessionOptions): RpcSession<T>;
|
|
248
348
|
};
|
|
249
349
|
/**
|
|
250
350
|
* Classes which are intended to be passed by reference and called over RPC must extend
|
|
@@ -297,7 +397,7 @@ declare let newMessagePortRpcSession: <T extends RpcCompatible<T> = Empty>(port:
|
|
|
297
397
|
* credentials as parameters and returns the authorized API), then cross-origin requests should
|
|
298
398
|
* be safe.
|
|
299
399
|
*/
|
|
300
|
-
declare function newWorkersRpcResponse(request: Request, localMain: any): Promise<Response>;
|
|
400
|
+
declare function newWorkersRpcResponse(request: Request, localMain: any, options?: RpcSessionOptions): Promise<Response>;
|
|
301
401
|
//#endregion
|
|
302
|
-
export { type RpcCompatible, RpcPromise, RpcSession, type RpcSessionOptions, RpcStub, RpcTarget, type RpcTransport, deserialize, newHttpBatchRpcResponse, newHttpBatchRpcSession, newMessagePortRpcSession, newWebSocketRpcSession, newWorkersRpcResponse, newWorkersWebSocketRpcResponse, nodeHttpBatchRpcResponse, serialize };
|
|
402
|
+
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 };
|
|
303
403
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.ts
CHANGED
|
@@ -39,8 +39,9 @@ interface StubBase<T = unknown> extends Disposable {
|
|
|
39
39
|
}
|
|
40
40
|
type Stub<T extends RpcCompatible<T>> = T extends object ? Provider<T> & StubBase<T> : StubBase<T>;
|
|
41
41
|
type TypedArray = Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array | Int8Array | Int16Array | Int32Array | BigUint64Array | BigInt64Array | Float32Array | Float64Array; // This represents all the types that can be sent as-is over an RPC boundary
|
|
42
|
-
type BaseType = void | undefined | null | boolean | number | bigint | string | TypedArray | ArrayBuffer | DataView | Date | Error | RegExp | Blob | ReadableStream<
|
|
43
|
-
|
|
|
42
|
+
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; // Recursively rewrite all `Stubable` types with `Stub`s, and resolve promises.
|
|
44
45
|
// prettier-ignore
|
|
45
46
|
type Stubify<T> = T extends Stubable ? Stub<T> : T extends Promise<infer U> ? Stubify<U> : T extends StubBase<any> ? 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 // When using "unknown" instead of "any", interfaces are not stubified.
|
|
46
47
|
: T extends {
|
|
@@ -69,7 +70,13 @@ interface MapValuePlaceholder<T> {
|
|
|
69
70
|
}
|
|
70
71
|
type NonStubMembers<T> = Exclude<T, StubBase<any>>; // Utility type for adding `Disposable`s to `object` types only.
|
|
71
72
|
// Note `unknown & T` is equivalent to `T`.
|
|
72
|
-
type MaybeDisposable<T> = T extends object ? Disposable : unknown; //
|
|
73
|
+
type MaybeDisposable<T> = T extends object ? Disposable : unknown; // The type behind the public `RpcPromise` (re-exported with its `RpcCompatible` constraint from
|
|
74
|
+
// index.ts). Defined here so that `Result`'s arms below resolve to the exact same alias
|
|
75
|
+
// instantiation: when both sides of an assignability check are the same alias applied to the
|
|
76
|
+
// same type arguments, the checker short-circuits on identity instead of recursing through the
|
|
77
|
+
// full structural comparison of these mutually-recursive types.
|
|
78
|
+
type RpcPromise$1<T> = T extends Stubable ? Promise<Stub<T>> & Provider<T> & StubBase<T> : Promise<Stubify<T> & MaybeDisposable<T>> & Provider<T> & StubBase<T>;
|
|
79
|
+
// Type for method return or property on an RPC interface.
|
|
73
80
|
// - Stubable types are replaced by stubs.
|
|
74
81
|
// - RpcCompatible types are passed by value, with stubable types replaced by stubs
|
|
75
82
|
// and a top-level `Disposer`.
|
|
@@ -77,7 +84,7 @@ type MaybeDisposable<T> = T extends object ? Disposable : unknown; // Type for m
|
|
|
77
84
|
// Technically, we use custom thenables here, but they quack like `Promise`s.
|
|
78
85
|
// Intersecting with `(Maybe)Provider` allows pipelining.
|
|
79
86
|
// prettier-ignore
|
|
80
|
-
type Result<R> = IsAny<R> extends true ? UnknownResult : IsUnknown<R> extends true ? UnknownResult : R extends Stubable ?
|
|
87
|
+
type Result<R> = IsAny<R> extends true ? UnknownResult : IsUnknown<R> extends true ? UnknownResult : R extends Stubable ? RpcPromise$1<R> : R extends RpcCompatible<R> ? RpcPromise$1<R> : never;
|
|
81
88
|
type IsAny<T> = 0 extends (1 & T) ? true : false;
|
|
82
89
|
type UnknownResult = Promise<unknown> & Provider<unknown> & StubBase<unknown>; // Type for method or property on an RPC interface.
|
|
83
90
|
// For methods, unwrap `Stub`s in parameters, and rewrite returns to be `Result`s.
|
|
@@ -107,6 +114,32 @@ type Provider<T> = MaybeCallableProvider<T> & (T extends ReadonlyArray<unknown>
|
|
|
107
114
|
});
|
|
108
115
|
//#endregion
|
|
109
116
|
//#region src/serialize.d.ts
|
|
117
|
+
/**
|
|
118
|
+
* Encoding levels determine what representation the RPC system hands to the transport.
|
|
119
|
+
* Each level names what the transport can assume about message values.
|
|
120
|
+
*
|
|
121
|
+
* - `"string"`: JSON string. Default, used by HTTP batch and WebSocket transports.
|
|
122
|
+
* - `"jsonCompatible"`: JSON-compatible JS value tree. For custom encoders.
|
|
123
|
+
* - `"jsonCompatibleWithBytes"`: Like `"jsonCompatible"` but Uint8Array stays raw.
|
|
124
|
+
* - `"structuredClonable"`: Structured-clonable native values pass through where possible.
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```ts
|
|
128
|
+
* // What happens to Uint8Array([1, 2, 3]) at each level:
|
|
129
|
+
* "string" → '["bytes","AQID"]' // JSON string with base64
|
|
130
|
+
* "jsonCompatible" → ["bytes", "AQID"] // JS array with base64
|
|
131
|
+
* "jsonCompatibleWithBytes" → ["bytes", Uint8Array] // JS array with raw bytes
|
|
132
|
+
* "structuredClonable" → ["bytes", Uint8Array] // + Date, BigInt stay native
|
|
133
|
+
* ```
|
|
134
|
+
*/
|
|
135
|
+
type EncodingLevel = "string" | "jsonCompatible" | "jsonCompatibleWithBytes" | "structuredClonable";
|
|
136
|
+
interface RpcLimits {
|
|
137
|
+
maxBigIntDigits: number;
|
|
138
|
+
maxDepth: number;
|
|
139
|
+
maxMessageSize: number;
|
|
140
|
+
}
|
|
141
|
+
declare const DEFAULT_MAX_DEPTH = 256;
|
|
142
|
+
declare const DEFAULT_LIMITS: RpcLimits;
|
|
110
143
|
/**
|
|
111
144
|
* Serialize a value, using Cap'n Web's underlying serialization. This won't be able to serialize
|
|
112
145
|
* RPC stubs, but it will support basic data types.
|
|
@@ -119,14 +152,21 @@ declare function deserialize(value: string): unknown;
|
|
|
119
152
|
//#endregion
|
|
120
153
|
//#region src/rpc.d.ts
|
|
121
154
|
/**
|
|
122
|
-
* Interface for
|
|
123
|
-
*
|
|
155
|
+
* Interface for a string-based RPC transport. This is the default transport type — no
|
|
156
|
+
* `encodingLevel` field is needed. Messages are JSON strings. Implement this interface if the
|
|
157
|
+
* built-in transports (e.g. for HTTP batch and WebSocket) don't meet your needs.
|
|
124
158
|
*/
|
|
125
159
|
interface RpcTransport {
|
|
126
160
|
/**
|
|
127
|
-
*
|
|
161
|
+
* The encoding level this transport works with. For this interface it is always "string";
|
|
162
|
+
* it may be omitted. (See `RpcTransportWithCustomEncoding` for the other levels.)
|
|
128
163
|
*/
|
|
129
|
-
|
|
164
|
+
readonly encodingLevel?: "string";
|
|
165
|
+
/**
|
|
166
|
+
* Sends a message to the other end. May optionally return a promise; if the promise rejects,
|
|
167
|
+
* the session is aborted.
|
|
168
|
+
*/
|
|
169
|
+
send(message: string): void | Promise<void>;
|
|
130
170
|
/**
|
|
131
171
|
* Receives a message sent by the other end.
|
|
132
172
|
*
|
|
@@ -145,6 +185,45 @@ interface RpcTransport {
|
|
|
145
185
|
*/
|
|
146
186
|
abort?(reason: any): void;
|
|
147
187
|
}
|
|
188
|
+
/**
|
|
189
|
+
* Interface for a transport that receives partially encoded JS values instead of JSON strings.
|
|
190
|
+
* The selected `encodingLevel` describes what the transport can assume about message values.
|
|
191
|
+
*/
|
|
192
|
+
interface RpcTransportWithCustomEncoding {
|
|
193
|
+
/**
|
|
194
|
+
* The encoding level this transport works with.
|
|
195
|
+
*
|
|
196
|
+
* - "jsonCompatible": JSON-compatible JS value tree; transport handles final serialization.
|
|
197
|
+
* - "jsonCompatibleWithBytes": Like "jsonCompatible" but Uint8Array values are left raw.
|
|
198
|
+
* - "structuredClonable": Structured-clonable native values pass through where possible.
|
|
199
|
+
*/
|
|
200
|
+
readonly encodingLevel: "jsonCompatible" | "jsonCompatibleWithBytes" | "structuredClonable";
|
|
201
|
+
/**
|
|
202
|
+
* Encodes and sends a message to the other end. Returns the encoded byte size if known.
|
|
203
|
+
* If the size is unavailable, return void; Cap'n Web will estimate stream message sizes for
|
|
204
|
+
* flow control. Send errors should be propagated via `receive()` rejecting.
|
|
205
|
+
*/
|
|
206
|
+
send(message: unknown): number | void;
|
|
207
|
+
/**
|
|
208
|
+
* Receives and decodes a message sent by the other end.
|
|
209
|
+
*
|
|
210
|
+
* If and when the transport becomes disconnected, this will reject. The thrown error will be
|
|
211
|
+
* propagated to all outstanding calls and future calls on any stubs associated with the session.
|
|
212
|
+
* If there are no outstanding calls (and none are made in the future), then the error does not
|
|
213
|
+
* propagate anywhere -- this is considered a "clean" shutdown.
|
|
214
|
+
*/
|
|
215
|
+
receive(): Promise<unknown>;
|
|
216
|
+
/**
|
|
217
|
+
* Indicates that the RPC system has suffered an error that prevents the session from continuing.
|
|
218
|
+
* The transport should ideally try to send any queued messages if it can, and then close the
|
|
219
|
+
* connection. (It's not strictly necessary to deliver queued messages, but the last message sent
|
|
220
|
+
* before abort() is called is often an "abort" message, which communicates the error to the
|
|
221
|
+
* peer, so if that is dropped, the peer may have less information about what happened.)
|
|
222
|
+
*/
|
|
223
|
+
abort?(reason: any): void;
|
|
224
|
+
}
|
|
225
|
+
/** Any supported transport type. */
|
|
226
|
+
type AnyRpcTransport = RpcTransport | RpcTransportWithCustomEncoding;
|
|
148
227
|
/**
|
|
149
228
|
* Options to customize behavior of an RPC session. All functions which start a session should
|
|
150
229
|
* optionally accept this.
|
|
@@ -162,6 +241,16 @@ type RpcSessionOptions = {
|
|
|
162
241
|
* to serialize the error with the stack omitted.
|
|
163
242
|
*/
|
|
164
243
|
onSendError?: (error: Error) => Error | void;
|
|
244
|
+
/**
|
|
245
|
+
* Overrides for the resource limits enforced while deserializing messages from the peer. Any
|
|
246
|
+
* field left unset falls back to `DEFAULT_LIMITS`. These guard against resource-exhaustion
|
|
247
|
+
* attacks from untrusted peers; see `RpcLimits` for the meaning and defaults of each field.
|
|
248
|
+
*
|
|
249
|
+
* Limits are a purely local, receiver-side decision -- the protocol has no negotiation step, so
|
|
250
|
+
* the peer never learns these values. A message that exceeds a limit is rejected, aborting the
|
|
251
|
+
* session.
|
|
252
|
+
*/
|
|
253
|
+
limits?: Partial<RpcLimits>;
|
|
165
254
|
};
|
|
166
255
|
//#endregion
|
|
167
256
|
//#region src/websocket.d.ts
|
|
@@ -170,6 +259,17 @@ type RpcSessionOptions = {
|
|
|
170
259
|
* with the given `localMain`.
|
|
171
260
|
*/
|
|
172
261
|
declare function newWorkersWebSocketRpcResponse(request: Request, localMain?: any, options?: RpcSessionOptions): Response;
|
|
262
|
+
/**
|
|
263
|
+
* Generic WebSocket transport. Default `T = string` is backward-compatible and satisfies
|
|
264
|
+
* `RpcTransport`. Use `T = ArrayBuffer` as a building block for binary transports.
|
|
265
|
+
*/
|
|
266
|
+
declare class WebSocketTransport<T extends string | ArrayBuffer = string> {
|
|
267
|
+
#private;
|
|
268
|
+
constructor(webSocket: WebSocket);
|
|
269
|
+
send(message: T): void;
|
|
270
|
+
receive(): Promise<T>;
|
|
271
|
+
abort(reason: any): void;
|
|
272
|
+
}
|
|
173
273
|
//#endregion
|
|
174
274
|
//#region src/batch.d.ts
|
|
175
275
|
/**
|
|
@@ -228,7 +328,7 @@ declare const RpcStub: {
|
|
|
228
328
|
* if you only intend to use the promise for pipelining and you never await it, then there's no
|
|
229
329
|
* need to transmit the resolution!
|
|
230
330
|
*/
|
|
231
|
-
type RpcPromise<T extends RpcCompatible<T>> =
|
|
331
|
+
type RpcPromise<T extends RpcCompatible<T>> = RpcPromise$1<T>;
|
|
232
332
|
declare const RpcPromise: {};
|
|
233
333
|
/**
|
|
234
334
|
* Use to construct an `RpcSession` on top of a custom `RpcTransport`.
|
|
@@ -244,7 +344,7 @@ interface RpcSession<T extends RpcCompatible<T> = undefined> {
|
|
|
244
344
|
drain(): Promise<void>;
|
|
245
345
|
}
|
|
246
346
|
declare const RpcSession: {
|
|
247
|
-
new <T extends RpcCompatible<T> = undefined>(transport:
|
|
347
|
+
new <T extends RpcCompatible<T> = undefined>(transport: AnyRpcTransport, localMain?: any, options?: RpcSessionOptions): RpcSession<T>;
|
|
248
348
|
};
|
|
249
349
|
/**
|
|
250
350
|
* Classes which are intended to be passed by reference and called over RPC must extend
|
|
@@ -297,7 +397,7 @@ declare let newMessagePortRpcSession: <T extends RpcCompatible<T> = Empty>(port:
|
|
|
297
397
|
* credentials as parameters and returns the authorized API), then cross-origin requests should
|
|
298
398
|
* be safe.
|
|
299
399
|
*/
|
|
300
|
-
declare function newWorkersRpcResponse(request: Request, localMain: any): Promise<Response>;
|
|
400
|
+
declare function newWorkersRpcResponse(request: Request, localMain: any, options?: RpcSessionOptions): Promise<Response>;
|
|
301
401
|
//#endregion
|
|
302
|
-
export { type RpcCompatible, RpcPromise, RpcSession, type RpcSessionOptions, RpcStub, RpcTarget, type RpcTransport, deserialize, newHttpBatchRpcResponse, newHttpBatchRpcSession, newMessagePortRpcSession, newWebSocketRpcSession, newWorkersRpcResponse, newWorkersWebSocketRpcResponse, nodeHttpBatchRpcResponse, serialize };
|
|
402
|
+
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 };
|
|
303
403
|
//# sourceMappingURL=index.d.ts.map
|