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-bun.d.cts
CHANGED
|
@@ -40,8 +40,9 @@ interface StubBase<T = unknown> extends Disposable {
|
|
|
40
40
|
}
|
|
41
41
|
type Stub<T extends RpcCompatible<T>> = T extends object ? Provider<T> & StubBase<T> : StubBase<T>;
|
|
42
42
|
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
|
|
43
|
-
type BaseType = void | undefined | null | boolean | number | bigint | string | TypedArray | ArrayBuffer | DataView | Date | Error | RegExp | Blob | ReadableStream<
|
|
44
|
-
|
|
|
43
|
+
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
|
|
44
|
+
| WritableStream<any> // Chunk type can be any RPC-compatible type
|
|
45
|
+
| URL | Request | Response | Headers; // Recursively rewrite all `Stubable` types with `Stub`s, and resolve promises.
|
|
45
46
|
// prettier-ignore
|
|
46
47
|
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.
|
|
47
48
|
: T extends {
|
|
@@ -70,7 +71,13 @@ interface MapValuePlaceholder<T> {
|
|
|
70
71
|
}
|
|
71
72
|
type NonStubMembers<T> = Exclude<T, StubBase<any>>; // Utility type for adding `Disposable`s to `object` types only.
|
|
72
73
|
// Note `unknown & T` is equivalent to `T`.
|
|
73
|
-
type MaybeDisposable<T> = T extends object ? Disposable : unknown; //
|
|
74
|
+
type MaybeDisposable<T> = T extends object ? Disposable : unknown; // The type behind the public `RpcPromise` (re-exported with its `RpcCompatible` constraint from
|
|
75
|
+
// index.ts). Defined here so that `Result`'s arms below resolve to the exact same alias
|
|
76
|
+
// instantiation: when both sides of an assignability check are the same alias applied to the
|
|
77
|
+
// same type arguments, the checker short-circuits on identity instead of recursing through the
|
|
78
|
+
// full structural comparison of these mutually-recursive types.
|
|
79
|
+
type RpcPromise$2<T> = T extends Stubable ? Promise<Stub<T>> & Provider<T> & StubBase<T> : Promise<Stubify<T> & MaybeDisposable<T>> & Provider<T> & StubBase<T>;
|
|
80
|
+
// Type for method return or property on an RPC interface.
|
|
74
81
|
// - Stubable types are replaced by stubs.
|
|
75
82
|
// - RpcCompatible types are passed by value, with stubable types replaced by stubs
|
|
76
83
|
// and a top-level `Disposer`.
|
|
@@ -78,7 +85,7 @@ type MaybeDisposable<T> = T extends object ? Disposable : unknown; // Type for m
|
|
|
78
85
|
// Technically, we use custom thenables here, but they quack like `Promise`s.
|
|
79
86
|
// Intersecting with `(Maybe)Provider` allows pipelining.
|
|
80
87
|
// prettier-ignore
|
|
81
|
-
type Result<R> = IsAny<R> extends true ? UnknownResult : IsUnknown<R> extends true ? UnknownResult : R extends Stubable ?
|
|
88
|
+
type Result<R> = IsAny<R> extends true ? UnknownResult : IsUnknown<R> extends true ? UnknownResult : R extends Stubable ? RpcPromise$2<R> : R extends RpcCompatible<R> ? RpcPromise$2<R> : never;
|
|
82
89
|
type IsAny<T> = 0 extends (1 & T) ? true : false;
|
|
83
90
|
type UnknownResult = Promise<unknown> & Provider<unknown> & StubBase<unknown>; // Type for method or property on an RPC interface.
|
|
84
91
|
// For methods, unwrap `Stub`s in parameters, and rewrite returns to be `Result`s.
|
|
@@ -171,6 +178,9 @@ declare class RpcPayload {
|
|
|
171
178
|
private deliverTo;
|
|
172
179
|
private static deliverRpcPromiseTo;
|
|
173
180
|
deliverCall(func: Function, thisArg: object | undefined): Promise<RpcPayload>;
|
|
181
|
+
deliverStreamWrite(writer: {
|
|
182
|
+
write(chunk: unknown): Promise<void>;
|
|
183
|
+
}): Promise<RpcPayload>;
|
|
174
184
|
deliverResolve(): Promise<unknown>;
|
|
175
185
|
dispose(): void;
|
|
176
186
|
private disposeImpl;
|
|
@@ -179,6 +189,32 @@ declare class RpcPayload {
|
|
|
179
189
|
}
|
|
180
190
|
//#endregion
|
|
181
191
|
//#region src/serialize.d.ts
|
|
192
|
+
/**
|
|
193
|
+
* Encoding levels determine what representation the RPC system hands to the transport.
|
|
194
|
+
* Each level names what the transport can assume about message values.
|
|
195
|
+
*
|
|
196
|
+
* - `"string"`: JSON string. Default, used by HTTP batch and WebSocket transports.
|
|
197
|
+
* - `"jsonCompatible"`: JSON-compatible JS value tree. For custom encoders.
|
|
198
|
+
* - `"jsonCompatibleWithBytes"`: Like `"jsonCompatible"` but Uint8Array stays raw.
|
|
199
|
+
* - `"structuredClonable"`: Structured-clonable native values pass through where possible.
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* ```ts
|
|
203
|
+
* // What happens to Uint8Array([1, 2, 3]) at each level:
|
|
204
|
+
* "string" → '["bytes","AQID"]' // JSON string with base64
|
|
205
|
+
* "jsonCompatible" → ["bytes", "AQID"] // JS array with base64
|
|
206
|
+
* "jsonCompatibleWithBytes" → ["bytes", Uint8Array] // JS array with raw bytes
|
|
207
|
+
* "structuredClonable" → ["bytes", Uint8Array] // + Date, BigInt stay native
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
type EncodingLevel = "string" | "jsonCompatible" | "jsonCompatibleWithBytes" | "structuredClonable";
|
|
211
|
+
interface RpcLimits {
|
|
212
|
+
maxBigIntDigits: number;
|
|
213
|
+
maxDepth: number;
|
|
214
|
+
maxMessageSize: number;
|
|
215
|
+
}
|
|
216
|
+
declare const DEFAULT_MAX_DEPTH = 256;
|
|
217
|
+
declare const DEFAULT_LIMITS: RpcLimits;
|
|
182
218
|
/**
|
|
183
219
|
* Serialize a value, using Cap'n Web's underlying serialization. This won't be able to serialize
|
|
184
220
|
* RPC stubs, but it will support basic data types.
|
|
@@ -191,14 +227,21 @@ declare function deserialize(value: string): unknown;
|
|
|
191
227
|
//#endregion
|
|
192
228
|
//#region src/rpc.d.ts
|
|
193
229
|
/**
|
|
194
|
-
* Interface for
|
|
195
|
-
*
|
|
230
|
+
* Interface for a string-based RPC transport. This is the default transport type — no
|
|
231
|
+
* `encodingLevel` field is needed. Messages are JSON strings. Implement this interface if the
|
|
232
|
+
* built-in transports (e.g. for HTTP batch and WebSocket) don't meet your needs.
|
|
196
233
|
*/
|
|
197
234
|
interface RpcTransport {
|
|
198
235
|
/**
|
|
199
|
-
*
|
|
236
|
+
* The encoding level this transport works with. For this interface it is always "string";
|
|
237
|
+
* it may be omitted. (See `RpcTransportWithCustomEncoding` for the other levels.)
|
|
200
238
|
*/
|
|
201
|
-
|
|
239
|
+
readonly encodingLevel?: "string";
|
|
240
|
+
/**
|
|
241
|
+
* Sends a message to the other end. May optionally return a promise; if the promise rejects,
|
|
242
|
+
* the session is aborted.
|
|
243
|
+
*/
|
|
244
|
+
send(message: string): void | Promise<void>;
|
|
202
245
|
/**
|
|
203
246
|
* Receives a message sent by the other end.
|
|
204
247
|
*
|
|
@@ -217,6 +260,45 @@ interface RpcTransport {
|
|
|
217
260
|
*/
|
|
218
261
|
abort?(reason: any): void;
|
|
219
262
|
}
|
|
263
|
+
/**
|
|
264
|
+
* Interface for a transport that receives partially encoded JS values instead of JSON strings.
|
|
265
|
+
* The selected `encodingLevel` describes what the transport can assume about message values.
|
|
266
|
+
*/
|
|
267
|
+
interface RpcTransportWithCustomEncoding {
|
|
268
|
+
/**
|
|
269
|
+
* The encoding level this transport works with.
|
|
270
|
+
*
|
|
271
|
+
* - "jsonCompatible": JSON-compatible JS value tree; transport handles final serialization.
|
|
272
|
+
* - "jsonCompatibleWithBytes": Like "jsonCompatible" but Uint8Array values are left raw.
|
|
273
|
+
* - "structuredClonable": Structured-clonable native values pass through where possible.
|
|
274
|
+
*/
|
|
275
|
+
readonly encodingLevel: "jsonCompatible" | "jsonCompatibleWithBytes" | "structuredClonable";
|
|
276
|
+
/**
|
|
277
|
+
* Encodes and sends a message to the other end. Returns the encoded byte size if known.
|
|
278
|
+
* If the size is unavailable, return void; Cap'n Web will estimate stream message sizes for
|
|
279
|
+
* flow control. Send errors should be propagated via `receive()` rejecting.
|
|
280
|
+
*/
|
|
281
|
+
send(message: unknown): number | void;
|
|
282
|
+
/**
|
|
283
|
+
* Receives and decodes a message sent by the other end.
|
|
284
|
+
*
|
|
285
|
+
* If and when the transport becomes disconnected, this will reject. The thrown error will be
|
|
286
|
+
* propagated to all outstanding calls and future calls on any stubs associated with the session.
|
|
287
|
+
* If there are no outstanding calls (and none are made in the future), then the error does not
|
|
288
|
+
* propagate anywhere -- this is considered a "clean" shutdown.
|
|
289
|
+
*/
|
|
290
|
+
receive(): Promise<unknown>;
|
|
291
|
+
/**
|
|
292
|
+
* Indicates that the RPC system has suffered an error that prevents the session from continuing.
|
|
293
|
+
* The transport should ideally try to send any queued messages if it can, and then close the
|
|
294
|
+
* connection. (It's not strictly necessary to deliver queued messages, but the last message sent
|
|
295
|
+
* before abort() is called is often an "abort" message, which communicates the error to the
|
|
296
|
+
* peer, so if that is dropped, the peer may have less information about what happened.)
|
|
297
|
+
*/
|
|
298
|
+
abort?(reason: any): void;
|
|
299
|
+
}
|
|
300
|
+
/** Any supported transport type. */
|
|
301
|
+
type AnyRpcTransport = RpcTransport | RpcTransportWithCustomEncoding;
|
|
220
302
|
/**
|
|
221
303
|
* Options to customize behavior of an RPC session. All functions which start a session should
|
|
222
304
|
* optionally accept this.
|
|
@@ -234,6 +316,16 @@ type RpcSessionOptions = {
|
|
|
234
316
|
* to serialize the error with the stack omitted.
|
|
235
317
|
*/
|
|
236
318
|
onSendError?: (error: Error) => Error | void;
|
|
319
|
+
/**
|
|
320
|
+
* Overrides for the resource limits enforced while deserializing messages from the peer. Any
|
|
321
|
+
* field left unset falls back to `DEFAULT_LIMITS`. These guard against resource-exhaustion
|
|
322
|
+
* attacks from untrusted peers; see `RpcLimits` for the meaning and defaults of each field.
|
|
323
|
+
*
|
|
324
|
+
* Limits are a purely local, receiver-side decision -- the protocol has no negotiation step, so
|
|
325
|
+
* the peer never learns these values. A message that exceeds a limit is rejected, aborting the
|
|
326
|
+
* session.
|
|
327
|
+
*/
|
|
328
|
+
limits?: Partial<RpcLimits>;
|
|
237
329
|
};
|
|
238
330
|
//#endregion
|
|
239
331
|
//#region src/websocket.d.ts
|
|
@@ -242,6 +334,17 @@ type RpcSessionOptions = {
|
|
|
242
334
|
* with the given `localMain`.
|
|
243
335
|
*/
|
|
244
336
|
declare function newWorkersWebSocketRpcResponse(request: Request, localMain?: any, options?: RpcSessionOptions): Response;
|
|
337
|
+
/**
|
|
338
|
+
* Generic WebSocket transport. Default `T = string` is backward-compatible and satisfies
|
|
339
|
+
* `RpcTransport`. Use `T = ArrayBuffer` as a building block for binary transports.
|
|
340
|
+
*/
|
|
341
|
+
declare class WebSocketTransport<T extends string | ArrayBuffer = string> {
|
|
342
|
+
#private;
|
|
343
|
+
constructor(webSocket: WebSocket);
|
|
344
|
+
send(message: T): void;
|
|
345
|
+
receive(): Promise<T>;
|
|
346
|
+
abort(reason: any): void;
|
|
347
|
+
}
|
|
245
348
|
//#endregion
|
|
246
349
|
//#region src/batch.d.ts
|
|
247
350
|
/**
|
|
@@ -300,7 +403,7 @@ declare const RpcStub: {
|
|
|
300
403
|
* if you only intend to use the promise for pipelining and you never await it, then there's no
|
|
301
404
|
* need to transmit the resolution!
|
|
302
405
|
*/
|
|
303
|
-
type RpcPromise<T extends RpcCompatible<T>> =
|
|
406
|
+
type RpcPromise<T extends RpcCompatible<T>> = RpcPromise$2<T>;
|
|
304
407
|
declare const RpcPromise: {};
|
|
305
408
|
/**
|
|
306
409
|
* Use to construct an `RpcSession` on top of a custom `RpcTransport`.
|
|
@@ -316,7 +419,7 @@ interface RpcSession<T extends RpcCompatible<T> = undefined> {
|
|
|
316
419
|
drain(): Promise<void>;
|
|
317
420
|
}
|
|
318
421
|
declare const RpcSession: {
|
|
319
|
-
new <T extends RpcCompatible<T> = undefined>(transport:
|
|
422
|
+
new <T extends RpcCompatible<T> = undefined>(transport: AnyRpcTransport, localMain?: any, options?: RpcSessionOptions): RpcSession<T>;
|
|
320
423
|
};
|
|
321
424
|
/**
|
|
322
425
|
* Classes which are intended to be passed by reference and called over RPC must extend
|
|
@@ -369,7 +472,7 @@ declare let newMessagePortRpcSession: <T extends RpcCompatible<T> = Empty$1>(por
|
|
|
369
472
|
* credentials as parameters and returns the authorized API), then cross-origin requests should
|
|
370
473
|
* be safe.
|
|
371
474
|
*/
|
|
372
|
-
declare function newWorkersRpcResponse(request: Request, localMain: any): Promise<Response>;
|
|
475
|
+
declare function newWorkersRpcResponse(request: Request, localMain: any, options?: RpcSessionOptions): Promise<Response>;
|
|
373
476
|
//#endregion
|
|
374
477
|
//#region src/bun.d.ts
|
|
375
478
|
type WsData = {
|
|
@@ -420,5 +523,5 @@ declare let newBunWebSocketRpcSession: <T extends RpcCompatible<T> = Empty, D =
|
|
|
420
523
|
transport: BunWebSocketTransport<D>;
|
|
421
524
|
};
|
|
422
525
|
//#endregion
|
|
423
|
-
export { BunWebSocketTransport, type RpcCompatible, RpcPromise, RpcSession, type RpcSessionOptions, RpcStub, RpcTarget, type RpcTransport, deserialize, newBunWebSocketRpcHandler, newBunWebSocketRpcSession, newHttpBatchRpcResponse, newHttpBatchRpcSession, newMessagePortRpcSession, newWebSocketRpcSession, newWorkersRpcResponse, newWorkersWebSocketRpcResponse, nodeHttpBatchRpcResponse, serialize };
|
|
526
|
+
export { type AnyRpcTransport, BunWebSocketTransport, DEFAULT_LIMITS, DEFAULT_MAX_DEPTH, type EncodingLevel, type RpcCompatible, type RpcLimits, RpcPromise, RpcSession, type RpcSessionOptions, RpcStub, RpcTarget, type RpcTransport, type RpcTransportWithCustomEncoding, WebSocketTransport, deserialize, newBunWebSocketRpcHandler, newBunWebSocketRpcSession, newHttpBatchRpcResponse, newHttpBatchRpcSession, newMessagePortRpcSession, newWebSocketRpcSession, newWorkersRpcResponse, newWorkersWebSocketRpcResponse, nodeHttpBatchRpcResponse, serialize };
|
|
424
527
|
//# sourceMappingURL=index-bun.d.cts.map
|
package/dist/index-bun.d.ts
CHANGED
|
@@ -40,8 +40,9 @@ interface StubBase<T = unknown> extends Disposable {
|
|
|
40
40
|
}
|
|
41
41
|
type Stub<T extends RpcCompatible<T>> = T extends object ? Provider<T> & StubBase<T> : StubBase<T>;
|
|
42
42
|
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
|
|
43
|
-
type BaseType = void | undefined | null | boolean | number | bigint | string | TypedArray | ArrayBuffer | DataView | Date | Error | RegExp | Blob | ReadableStream<
|
|
44
|
-
|
|
|
43
|
+
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
|
|
44
|
+
| WritableStream<any> // Chunk type can be any RPC-compatible type
|
|
45
|
+
| URL | Request | Response | Headers; // Recursively rewrite all `Stubable` types with `Stub`s, and resolve promises.
|
|
45
46
|
// prettier-ignore
|
|
46
47
|
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.
|
|
47
48
|
: T extends {
|
|
@@ -70,7 +71,13 @@ interface MapValuePlaceholder<T> {
|
|
|
70
71
|
}
|
|
71
72
|
type NonStubMembers<T> = Exclude<T, StubBase<any>>; // Utility type for adding `Disposable`s to `object` types only.
|
|
72
73
|
// Note `unknown & T` is equivalent to `T`.
|
|
73
|
-
type MaybeDisposable<T> = T extends object ? Disposable : unknown; //
|
|
74
|
+
type MaybeDisposable<T> = T extends object ? Disposable : unknown; // The type behind the public `RpcPromise` (re-exported with its `RpcCompatible` constraint from
|
|
75
|
+
// index.ts). Defined here so that `Result`'s arms below resolve to the exact same alias
|
|
76
|
+
// instantiation: when both sides of an assignability check are the same alias applied to the
|
|
77
|
+
// same type arguments, the checker short-circuits on identity instead of recursing through the
|
|
78
|
+
// full structural comparison of these mutually-recursive types.
|
|
79
|
+
type RpcPromise$2<T> = T extends Stubable ? Promise<Stub<T>> & Provider<T> & StubBase<T> : Promise<Stubify<T> & MaybeDisposable<T>> & Provider<T> & StubBase<T>;
|
|
80
|
+
// Type for method return or property on an RPC interface.
|
|
74
81
|
// - Stubable types are replaced by stubs.
|
|
75
82
|
// - RpcCompatible types are passed by value, with stubable types replaced by stubs
|
|
76
83
|
// and a top-level `Disposer`.
|
|
@@ -78,7 +85,7 @@ type MaybeDisposable<T> = T extends object ? Disposable : unknown; // Type for m
|
|
|
78
85
|
// Technically, we use custom thenables here, but they quack like `Promise`s.
|
|
79
86
|
// Intersecting with `(Maybe)Provider` allows pipelining.
|
|
80
87
|
// prettier-ignore
|
|
81
|
-
type Result<R> = IsAny<R> extends true ? UnknownResult : IsUnknown<R> extends true ? UnknownResult : R extends Stubable ?
|
|
88
|
+
type Result<R> = IsAny<R> extends true ? UnknownResult : IsUnknown<R> extends true ? UnknownResult : R extends Stubable ? RpcPromise$2<R> : R extends RpcCompatible<R> ? RpcPromise$2<R> : never;
|
|
82
89
|
type IsAny<T> = 0 extends (1 & T) ? true : false;
|
|
83
90
|
type UnknownResult = Promise<unknown> & Provider<unknown> & StubBase<unknown>; // Type for method or property on an RPC interface.
|
|
84
91
|
// For methods, unwrap `Stub`s in parameters, and rewrite returns to be `Result`s.
|
|
@@ -171,6 +178,9 @@ declare class RpcPayload {
|
|
|
171
178
|
private deliverTo;
|
|
172
179
|
private static deliverRpcPromiseTo;
|
|
173
180
|
deliverCall(func: Function, thisArg: object | undefined): Promise<RpcPayload>;
|
|
181
|
+
deliverStreamWrite(writer: {
|
|
182
|
+
write(chunk: unknown): Promise<void>;
|
|
183
|
+
}): Promise<RpcPayload>;
|
|
174
184
|
deliverResolve(): Promise<unknown>;
|
|
175
185
|
dispose(): void;
|
|
176
186
|
private disposeImpl;
|
|
@@ -179,6 +189,32 @@ declare class RpcPayload {
|
|
|
179
189
|
}
|
|
180
190
|
//#endregion
|
|
181
191
|
//#region src/serialize.d.ts
|
|
192
|
+
/**
|
|
193
|
+
* Encoding levels determine what representation the RPC system hands to the transport.
|
|
194
|
+
* Each level names what the transport can assume about message values.
|
|
195
|
+
*
|
|
196
|
+
* - `"string"`: JSON string. Default, used by HTTP batch and WebSocket transports.
|
|
197
|
+
* - `"jsonCompatible"`: JSON-compatible JS value tree. For custom encoders.
|
|
198
|
+
* - `"jsonCompatibleWithBytes"`: Like `"jsonCompatible"` but Uint8Array stays raw.
|
|
199
|
+
* - `"structuredClonable"`: Structured-clonable native values pass through where possible.
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* ```ts
|
|
203
|
+
* // What happens to Uint8Array([1, 2, 3]) at each level:
|
|
204
|
+
* "string" → '["bytes","AQID"]' // JSON string with base64
|
|
205
|
+
* "jsonCompatible" → ["bytes", "AQID"] // JS array with base64
|
|
206
|
+
* "jsonCompatibleWithBytes" → ["bytes", Uint8Array] // JS array with raw bytes
|
|
207
|
+
* "structuredClonable" → ["bytes", Uint8Array] // + Date, BigInt stay native
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
type EncodingLevel = "string" | "jsonCompatible" | "jsonCompatibleWithBytes" | "structuredClonable";
|
|
211
|
+
interface RpcLimits {
|
|
212
|
+
maxBigIntDigits: number;
|
|
213
|
+
maxDepth: number;
|
|
214
|
+
maxMessageSize: number;
|
|
215
|
+
}
|
|
216
|
+
declare const DEFAULT_MAX_DEPTH = 256;
|
|
217
|
+
declare const DEFAULT_LIMITS: RpcLimits;
|
|
182
218
|
/**
|
|
183
219
|
* Serialize a value, using Cap'n Web's underlying serialization. This won't be able to serialize
|
|
184
220
|
* RPC stubs, but it will support basic data types.
|
|
@@ -191,14 +227,21 @@ declare function deserialize(value: string): unknown;
|
|
|
191
227
|
//#endregion
|
|
192
228
|
//#region src/rpc.d.ts
|
|
193
229
|
/**
|
|
194
|
-
* Interface for
|
|
195
|
-
*
|
|
230
|
+
* Interface for a string-based RPC transport. This is the default transport type — no
|
|
231
|
+
* `encodingLevel` field is needed. Messages are JSON strings. Implement this interface if the
|
|
232
|
+
* built-in transports (e.g. for HTTP batch and WebSocket) don't meet your needs.
|
|
196
233
|
*/
|
|
197
234
|
interface RpcTransport {
|
|
198
235
|
/**
|
|
199
|
-
*
|
|
236
|
+
* The encoding level this transport works with. For this interface it is always "string";
|
|
237
|
+
* it may be omitted. (See `RpcTransportWithCustomEncoding` for the other levels.)
|
|
200
238
|
*/
|
|
201
|
-
|
|
239
|
+
readonly encodingLevel?: "string";
|
|
240
|
+
/**
|
|
241
|
+
* Sends a message to the other end. May optionally return a promise; if the promise rejects,
|
|
242
|
+
* the session is aborted.
|
|
243
|
+
*/
|
|
244
|
+
send(message: string): void | Promise<void>;
|
|
202
245
|
/**
|
|
203
246
|
* Receives a message sent by the other end.
|
|
204
247
|
*
|
|
@@ -217,6 +260,45 @@ interface RpcTransport {
|
|
|
217
260
|
*/
|
|
218
261
|
abort?(reason: any): void;
|
|
219
262
|
}
|
|
263
|
+
/**
|
|
264
|
+
* Interface for a transport that receives partially encoded JS values instead of JSON strings.
|
|
265
|
+
* The selected `encodingLevel` describes what the transport can assume about message values.
|
|
266
|
+
*/
|
|
267
|
+
interface RpcTransportWithCustomEncoding {
|
|
268
|
+
/**
|
|
269
|
+
* The encoding level this transport works with.
|
|
270
|
+
*
|
|
271
|
+
* - "jsonCompatible": JSON-compatible JS value tree; transport handles final serialization.
|
|
272
|
+
* - "jsonCompatibleWithBytes": Like "jsonCompatible" but Uint8Array values are left raw.
|
|
273
|
+
* - "structuredClonable": Structured-clonable native values pass through where possible.
|
|
274
|
+
*/
|
|
275
|
+
readonly encodingLevel: "jsonCompatible" | "jsonCompatibleWithBytes" | "structuredClonable";
|
|
276
|
+
/**
|
|
277
|
+
* Encodes and sends a message to the other end. Returns the encoded byte size if known.
|
|
278
|
+
* If the size is unavailable, return void; Cap'n Web will estimate stream message sizes for
|
|
279
|
+
* flow control. Send errors should be propagated via `receive()` rejecting.
|
|
280
|
+
*/
|
|
281
|
+
send(message: unknown): number | void;
|
|
282
|
+
/**
|
|
283
|
+
* Receives and decodes a message sent by the other end.
|
|
284
|
+
*
|
|
285
|
+
* If and when the transport becomes disconnected, this will reject. The thrown error will be
|
|
286
|
+
* propagated to all outstanding calls and future calls on any stubs associated with the session.
|
|
287
|
+
* If there are no outstanding calls (and none are made in the future), then the error does not
|
|
288
|
+
* propagate anywhere -- this is considered a "clean" shutdown.
|
|
289
|
+
*/
|
|
290
|
+
receive(): Promise<unknown>;
|
|
291
|
+
/**
|
|
292
|
+
* Indicates that the RPC system has suffered an error that prevents the session from continuing.
|
|
293
|
+
* The transport should ideally try to send any queued messages if it can, and then close the
|
|
294
|
+
* connection. (It's not strictly necessary to deliver queued messages, but the last message sent
|
|
295
|
+
* before abort() is called is often an "abort" message, which communicates the error to the
|
|
296
|
+
* peer, so if that is dropped, the peer may have less information about what happened.)
|
|
297
|
+
*/
|
|
298
|
+
abort?(reason: any): void;
|
|
299
|
+
}
|
|
300
|
+
/** Any supported transport type. */
|
|
301
|
+
type AnyRpcTransport = RpcTransport | RpcTransportWithCustomEncoding;
|
|
220
302
|
/**
|
|
221
303
|
* Options to customize behavior of an RPC session. All functions which start a session should
|
|
222
304
|
* optionally accept this.
|
|
@@ -234,6 +316,16 @@ type RpcSessionOptions = {
|
|
|
234
316
|
* to serialize the error with the stack omitted.
|
|
235
317
|
*/
|
|
236
318
|
onSendError?: (error: Error) => Error | void;
|
|
319
|
+
/**
|
|
320
|
+
* Overrides for the resource limits enforced while deserializing messages from the peer. Any
|
|
321
|
+
* field left unset falls back to `DEFAULT_LIMITS`. These guard against resource-exhaustion
|
|
322
|
+
* attacks from untrusted peers; see `RpcLimits` for the meaning and defaults of each field.
|
|
323
|
+
*
|
|
324
|
+
* Limits are a purely local, receiver-side decision -- the protocol has no negotiation step, so
|
|
325
|
+
* the peer never learns these values. A message that exceeds a limit is rejected, aborting the
|
|
326
|
+
* session.
|
|
327
|
+
*/
|
|
328
|
+
limits?: Partial<RpcLimits>;
|
|
237
329
|
};
|
|
238
330
|
//#endregion
|
|
239
331
|
//#region src/websocket.d.ts
|
|
@@ -242,6 +334,17 @@ type RpcSessionOptions = {
|
|
|
242
334
|
* with the given `localMain`.
|
|
243
335
|
*/
|
|
244
336
|
declare function newWorkersWebSocketRpcResponse(request: Request, localMain?: any, options?: RpcSessionOptions): Response;
|
|
337
|
+
/**
|
|
338
|
+
* Generic WebSocket transport. Default `T = string` is backward-compatible and satisfies
|
|
339
|
+
* `RpcTransport`. Use `T = ArrayBuffer` as a building block for binary transports.
|
|
340
|
+
*/
|
|
341
|
+
declare class WebSocketTransport<T extends string | ArrayBuffer = string> {
|
|
342
|
+
#private;
|
|
343
|
+
constructor(webSocket: WebSocket);
|
|
344
|
+
send(message: T): void;
|
|
345
|
+
receive(): Promise<T>;
|
|
346
|
+
abort(reason: any): void;
|
|
347
|
+
}
|
|
245
348
|
//#endregion
|
|
246
349
|
//#region src/batch.d.ts
|
|
247
350
|
/**
|
|
@@ -300,7 +403,7 @@ declare const RpcStub: {
|
|
|
300
403
|
* if you only intend to use the promise for pipelining and you never await it, then there's no
|
|
301
404
|
* need to transmit the resolution!
|
|
302
405
|
*/
|
|
303
|
-
type RpcPromise<T extends RpcCompatible<T>> =
|
|
406
|
+
type RpcPromise<T extends RpcCompatible<T>> = RpcPromise$2<T>;
|
|
304
407
|
declare const RpcPromise: {};
|
|
305
408
|
/**
|
|
306
409
|
* Use to construct an `RpcSession` on top of a custom `RpcTransport`.
|
|
@@ -316,7 +419,7 @@ interface RpcSession<T extends RpcCompatible<T> = undefined> {
|
|
|
316
419
|
drain(): Promise<void>;
|
|
317
420
|
}
|
|
318
421
|
declare const RpcSession: {
|
|
319
|
-
new <T extends RpcCompatible<T> = undefined>(transport:
|
|
422
|
+
new <T extends RpcCompatible<T> = undefined>(transport: AnyRpcTransport, localMain?: any, options?: RpcSessionOptions): RpcSession<T>;
|
|
320
423
|
};
|
|
321
424
|
/**
|
|
322
425
|
* Classes which are intended to be passed by reference and called over RPC must extend
|
|
@@ -369,7 +472,7 @@ declare let newMessagePortRpcSession: <T extends RpcCompatible<T> = Empty$1>(por
|
|
|
369
472
|
* credentials as parameters and returns the authorized API), then cross-origin requests should
|
|
370
473
|
* be safe.
|
|
371
474
|
*/
|
|
372
|
-
declare function newWorkersRpcResponse(request: Request, localMain: any): Promise<Response>;
|
|
475
|
+
declare function newWorkersRpcResponse(request: Request, localMain: any, options?: RpcSessionOptions): Promise<Response>;
|
|
373
476
|
//#endregion
|
|
374
477
|
//#region src/bun.d.ts
|
|
375
478
|
type WsData = {
|
|
@@ -420,5 +523,5 @@ declare let newBunWebSocketRpcSession: <T extends RpcCompatible<T> = Empty, D =
|
|
|
420
523
|
transport: BunWebSocketTransport<D>;
|
|
421
524
|
};
|
|
422
525
|
//#endregion
|
|
423
|
-
export { BunWebSocketTransport, type RpcCompatible, RpcPromise, RpcSession, type RpcSessionOptions, RpcStub, RpcTarget, type RpcTransport, deserialize, newBunWebSocketRpcHandler, newBunWebSocketRpcSession, newHttpBatchRpcResponse, newHttpBatchRpcSession, newMessagePortRpcSession, newWebSocketRpcSession, newWorkersRpcResponse, newWorkersWebSocketRpcResponse, nodeHttpBatchRpcResponse, serialize };
|
|
526
|
+
export { type AnyRpcTransport, BunWebSocketTransport, DEFAULT_LIMITS, DEFAULT_MAX_DEPTH, type EncodingLevel, type RpcCompatible, type RpcLimits, RpcPromise, RpcSession, type RpcSessionOptions, RpcStub, RpcTarget, type RpcTransport, type RpcTransportWithCustomEncoding, WebSocketTransport, deserialize, newBunWebSocketRpcHandler, newBunWebSocketRpcSession, newHttpBatchRpcResponse, newHttpBatchRpcSession, newMessagePortRpcSession, newWebSocketRpcSession, newWorkersRpcResponse, newWorkersWebSocketRpcResponse, nodeHttpBatchRpcResponse, serialize };
|
|
424
527
|
//# sourceMappingURL=index-bun.d.ts.map
|