capnweb 0.9.1 → 0.11.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/README.md +38 -2
- package/dist/index-bun.cjs +236 -33
- package/dist/index-bun.cjs.map +1 -1
- package/dist/index-bun.d.cts +34 -7
- package/dist/index-bun.d.ts +34 -7
- package/dist/index-bun.js +235 -34
- package/dist/index-bun.js.map +1 -1
- package/dist/index-workers.cjs +236 -33
- package/dist/index-workers.cjs.map +1 -1
- package/dist/index-workers.d.cts +31 -7
- package/dist/index-workers.d.ts +31 -7
- package/dist/index-workers.js +235 -34
- package/dist/index-workers.js.map +1 -1
- package/dist/index.cjs +236 -33
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +31 -7
- package/dist/index.d.ts +31 -7
- package/dist/index.js +235 -34
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
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.
|
|
@@ -126,6 +133,13 @@ type Provider<T> = MaybeCallableProvider<T> & (T extends ReadonlyArray<unknown>
|
|
|
126
133
|
* ```
|
|
127
134
|
*/
|
|
128
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;
|
|
129
143
|
/**
|
|
130
144
|
* Serialize a value, using Cap'n Web's underlying serialization. This won't be able to serialize
|
|
131
145
|
* RPC stubs, but it will support basic data types.
|
|
@@ -227,6 +241,16 @@ type RpcSessionOptions = {
|
|
|
227
241
|
* to serialize the error with the stack omitted.
|
|
228
242
|
*/
|
|
229
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>;
|
|
230
254
|
};
|
|
231
255
|
//#endregion
|
|
232
256
|
//#region src/websocket.d.ts
|
|
@@ -304,7 +328,7 @@ declare const RpcStub: {
|
|
|
304
328
|
* if you only intend to use the promise for pipelining and you never await it, then there's no
|
|
305
329
|
* need to transmit the resolution!
|
|
306
330
|
*/
|
|
307
|
-
type RpcPromise<T extends RpcCompatible<T>> =
|
|
331
|
+
type RpcPromise<T extends RpcCompatible<T>> = RpcPromise$1<T>;
|
|
308
332
|
declare const RpcPromise: {};
|
|
309
333
|
/**
|
|
310
334
|
* Use to construct an `RpcSession` on top of a custom `RpcTransport`.
|
|
@@ -373,7 +397,7 @@ declare let newMessagePortRpcSession: <T extends RpcCompatible<T> = Empty>(port:
|
|
|
373
397
|
* credentials as parameters and returns the authorized API), then cross-origin requests should
|
|
374
398
|
* be safe.
|
|
375
399
|
*/
|
|
376
|
-
declare function newWorkersRpcResponse(request: Request, localMain: any): Promise<Response>;
|
|
400
|
+
declare function newWorkersRpcResponse(request: Request, localMain: any, options?: RpcSessionOptions): Promise<Response>;
|
|
377
401
|
//#endregion
|
|
378
|
-
export { type AnyRpcTransport, type EncodingLevel, type RpcCompatible, RpcPromise, RpcSession, type RpcSessionOptions, RpcStub, RpcTarget, type RpcTransport, type RpcTransportWithCustomEncoding, WebSocketTransport, 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 };
|
|
379
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.
|
|
@@ -126,6 +133,13 @@ type Provider<T> = MaybeCallableProvider<T> & (T extends ReadonlyArray<unknown>
|
|
|
126
133
|
* ```
|
|
127
134
|
*/
|
|
128
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;
|
|
129
143
|
/**
|
|
130
144
|
* Serialize a value, using Cap'n Web's underlying serialization. This won't be able to serialize
|
|
131
145
|
* RPC stubs, but it will support basic data types.
|
|
@@ -227,6 +241,16 @@ type RpcSessionOptions = {
|
|
|
227
241
|
* to serialize the error with the stack omitted.
|
|
228
242
|
*/
|
|
229
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>;
|
|
230
254
|
};
|
|
231
255
|
//#endregion
|
|
232
256
|
//#region src/websocket.d.ts
|
|
@@ -304,7 +328,7 @@ declare const RpcStub: {
|
|
|
304
328
|
* if you only intend to use the promise for pipelining and you never await it, then there's no
|
|
305
329
|
* need to transmit the resolution!
|
|
306
330
|
*/
|
|
307
|
-
type RpcPromise<T extends RpcCompatible<T>> =
|
|
331
|
+
type RpcPromise<T extends RpcCompatible<T>> = RpcPromise$1<T>;
|
|
308
332
|
declare const RpcPromise: {};
|
|
309
333
|
/**
|
|
310
334
|
* Use to construct an `RpcSession` on top of a custom `RpcTransport`.
|
|
@@ -373,7 +397,7 @@ declare let newMessagePortRpcSession: <T extends RpcCompatible<T> = Empty>(port:
|
|
|
373
397
|
* credentials as parameters and returns the authorized API), then cross-origin requests should
|
|
374
398
|
* be safe.
|
|
375
399
|
*/
|
|
376
|
-
declare function newWorkersRpcResponse(request: Request, localMain: any): Promise<Response>;
|
|
400
|
+
declare function newWorkersRpcResponse(request: Request, localMain: any, options?: RpcSessionOptions): Promise<Response>;
|
|
377
401
|
//#endregion
|
|
378
|
-
export { type AnyRpcTransport, type EncodingLevel, type RpcCompatible, RpcPromise, RpcSession, type RpcSessionOptions, RpcStub, RpcTarget, type RpcTransport, type RpcTransportWithCustomEncoding, WebSocketTransport, 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 };
|
|
379
403
|
//# sourceMappingURL=index.d.ts.map
|