capnweb 0.0.0-5bf633f → 0.0.0-5da3786

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.
@@ -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<Uint8Array> | WritableStream<any> // Chunk type can be any RPC-compatible type
44
- | Request | Response | Headers; // Recursively rewrite all `Stubable` types with `Stub`s, and resolve promises.
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; // Type for method return or property on an RPC interface.
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 ? Promise<Stub<R>> & Provider<R> & StubBase<R> : R extends RpcCompatible<R> ? Promise<Stubify<R> & MaybeDisposable<R>> & Provider<R> & StubBase<R> : never;
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;
@@ -198,6 +208,13 @@ declare class RpcPayload {
198
208
  * ```
199
209
  */
200
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;
201
218
  /**
202
219
  * Serialize a value, using Cap'n Web's underlying serialization. This won't be able to serialize
203
220
  * RPC stubs, but it will support basic data types.
@@ -299,6 +316,16 @@ type RpcSessionOptions = {
299
316
  * to serialize the error with the stack omitted.
300
317
  */
301
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>;
302
329
  };
303
330
  //#endregion
304
331
  //#region src/websocket.d.ts
@@ -376,7 +403,7 @@ declare const RpcStub: {
376
403
  * if you only intend to use the promise for pipelining and you never await it, then there's no
377
404
  * need to transmit the resolution!
378
405
  */
379
- type RpcPromise<T extends RpcCompatible<T>> = Stub<T> & Promise<Stubify<T>>;
406
+ type RpcPromise<T extends RpcCompatible<T>> = RpcPromise$2<T>;
380
407
  declare const RpcPromise: {};
381
408
  /**
382
409
  * Use to construct an `RpcSession` on top of a custom `RpcTransport`.
@@ -445,7 +472,7 @@ declare let newMessagePortRpcSession: <T extends RpcCompatible<T> = Empty$1>(por
445
472
  * credentials as parameters and returns the authorized API), then cross-origin requests should
446
473
  * be safe.
447
474
  */
448
- declare function newWorkersRpcResponse(request: Request, localMain: any): Promise<Response>;
475
+ declare function newWorkersRpcResponse(request: Request, localMain: any, options?: RpcSessionOptions): Promise<Response>;
449
476
  //#endregion
450
477
  //#region src/bun.d.ts
451
478
  type WsData = {
@@ -496,5 +523,5 @@ declare let newBunWebSocketRpcSession: <T extends RpcCompatible<T> = Empty, D =
496
523
  transport: BunWebSocketTransport<D>;
497
524
  };
498
525
  //#endregion
499
- export { type AnyRpcTransport, BunWebSocketTransport, type EncodingLevel, type RpcCompatible, RpcPromise, RpcSession, type RpcSessionOptions, RpcStub, RpcTarget, type RpcTransport, type RpcTransportWithCustomEncoding, WebSocketTransport, 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 };
500
527
  //# sourceMappingURL=index-bun.d.cts.map
@@ -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<Uint8Array> | WritableStream<any> // Chunk type can be any RPC-compatible type
44
- | Request | Response | Headers; // Recursively rewrite all `Stubable` types with `Stub`s, and resolve promises.
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; // Type for method return or property on an RPC interface.
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 ? Promise<Stub<R>> & Provider<R> & StubBase<R> : R extends RpcCompatible<R> ? Promise<Stubify<R> & MaybeDisposable<R>> & Provider<R> & StubBase<R> : never;
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;
@@ -198,6 +208,13 @@ declare class RpcPayload {
198
208
  * ```
199
209
  */
200
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;
201
218
  /**
202
219
  * Serialize a value, using Cap'n Web's underlying serialization. This won't be able to serialize
203
220
  * RPC stubs, but it will support basic data types.
@@ -299,6 +316,16 @@ type RpcSessionOptions = {
299
316
  * to serialize the error with the stack omitted.
300
317
  */
301
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>;
302
329
  };
303
330
  //#endregion
304
331
  //#region src/websocket.d.ts
@@ -376,7 +403,7 @@ declare const RpcStub: {
376
403
  * if you only intend to use the promise for pipelining and you never await it, then there's no
377
404
  * need to transmit the resolution!
378
405
  */
379
- type RpcPromise<T extends RpcCompatible<T>> = Stub<T> & Promise<Stubify<T>>;
406
+ type RpcPromise<T extends RpcCompatible<T>> = RpcPromise$2<T>;
380
407
  declare const RpcPromise: {};
381
408
  /**
382
409
  * Use to construct an `RpcSession` on top of a custom `RpcTransport`.
@@ -445,7 +472,7 @@ declare let newMessagePortRpcSession: <T extends RpcCompatible<T> = Empty$1>(por
445
472
  * credentials as parameters and returns the authorized API), then cross-origin requests should
446
473
  * be safe.
447
474
  */
448
- declare function newWorkersRpcResponse(request: Request, localMain: any): Promise<Response>;
475
+ declare function newWorkersRpcResponse(request: Request, localMain: any, options?: RpcSessionOptions): Promise<Response>;
449
476
  //#endregion
450
477
  //#region src/bun.d.ts
451
478
  type WsData = {
@@ -496,5 +523,5 @@ declare let newBunWebSocketRpcSession: <T extends RpcCompatible<T> = Empty, D =
496
523
  transport: BunWebSocketTransport<D>;
497
524
  };
498
525
  //#endregion
499
- export { type AnyRpcTransport, BunWebSocketTransport, type EncodingLevel, type RpcCompatible, RpcPromise, RpcSession, type RpcSessionOptions, RpcStub, RpcTarget, type RpcTransport, type RpcTransportWithCustomEncoding, WebSocketTransport, 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 };
500
527
  //# sourceMappingURL=index-bun.d.ts.map