capnweb 0.0.0-409821 → 0.0.0-47df697

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/dist/index.d.cts CHANGED
@@ -107,6 +107,25 @@ type Provider<T> = MaybeCallableProvider<T> & (T extends ReadonlyArray<unknown>
107
107
  });
108
108
  //#endregion
109
109
  //#region src/serialize.d.ts
110
+ /**
111
+ * Encoding levels determine what representation the RPC system hands to the transport.
112
+ * Each level names what the transport can assume about message values.
113
+ *
114
+ * - `"string"`: JSON string. Default, used by HTTP batch and WebSocket transports.
115
+ * - `"jsonCompatible"`: JSON-compatible JS value tree. For custom encoders.
116
+ * - `"jsonCompatibleWithBytes"`: Like `"jsonCompatible"` but Uint8Array stays raw.
117
+ * - `"structuredClonable"`: Structured-clonable native values pass through where possible.
118
+ *
119
+ * @example
120
+ * ```ts
121
+ * // What happens to Uint8Array([1, 2, 3]) at each level:
122
+ * "string" → '["bytes","AQID"]' // JSON string with base64
123
+ * "jsonCompatible" → ["bytes", "AQID"] // JS array with base64
124
+ * "jsonCompatibleWithBytes" → ["bytes", Uint8Array] // JS array with raw bytes
125
+ * "structuredClonable" → ["bytes", Uint8Array] // + Date, BigInt stay native
126
+ * ```
127
+ */
128
+ type EncodingLevel = "string" | "jsonCompatible" | "jsonCompatibleWithBytes" | "structuredClonable";
110
129
  /**
111
130
  * Serialize a value, using Cap'n Web's underlying serialization. This won't be able to serialize
112
131
  * RPC stubs, but it will support basic data types.
@@ -119,14 +138,21 @@ declare function deserialize(value: string): unknown;
119
138
  //#endregion
120
139
  //#region src/rpc.d.ts
121
140
  /**
122
- * Interface for an RPC transport, which is a simple bidirectional message stream. Implement this
123
- * interface if the built-in transports (e.g. for HTTP batch and WebSocket) don't meet your needs.
141
+ * Interface for a string-based RPC transport. This is the default transport type no
142
+ * `encodingLevel` field is needed. Messages are JSON strings. Implement this interface if the
143
+ * built-in transports (e.g. for HTTP batch and WebSocket) don't meet your needs.
124
144
  */
125
145
  interface RpcTransport {
126
146
  /**
127
- * Sends a message to the other end.
147
+ * The encoding level this transport works with. For this interface it is always "string";
148
+ * it may be omitted. (See `RpcTransportWithCustomEncoding` for the other levels.)
128
149
  */
129
- send(message: string): Promise<void>;
150
+ readonly encodingLevel?: "string";
151
+ /**
152
+ * Sends a message to the other end. May optionally return a promise; if the promise rejects,
153
+ * the session is aborted.
154
+ */
155
+ send(message: string): void | Promise<void>;
130
156
  /**
131
157
  * Receives a message sent by the other end.
132
158
  *
@@ -145,6 +171,45 @@ interface RpcTransport {
145
171
  */
146
172
  abort?(reason: any): void;
147
173
  }
174
+ /**
175
+ * Interface for a transport that receives partially encoded JS values instead of JSON strings.
176
+ * The selected `encodingLevel` describes what the transport can assume about message values.
177
+ */
178
+ interface RpcTransportWithCustomEncoding {
179
+ /**
180
+ * The encoding level this transport works with.
181
+ *
182
+ * - "jsonCompatible": JSON-compatible JS value tree; transport handles final serialization.
183
+ * - "jsonCompatibleWithBytes": Like "jsonCompatible" but Uint8Array values are left raw.
184
+ * - "structuredClonable": Structured-clonable native values pass through where possible.
185
+ */
186
+ readonly encodingLevel: "jsonCompatible" | "jsonCompatibleWithBytes" | "structuredClonable";
187
+ /**
188
+ * Encodes and sends a message to the other end. Returns the encoded byte size if known.
189
+ * If the size is unavailable, return void; Cap'n Web will estimate stream message sizes for
190
+ * flow control. Send errors should be propagated via `receive()` rejecting.
191
+ */
192
+ send(message: unknown): number | void;
193
+ /**
194
+ * Receives and decodes a message sent by the other end.
195
+ *
196
+ * If and when the transport becomes disconnected, this will reject. The thrown error will be
197
+ * propagated to all outstanding calls and future calls on any stubs associated with the session.
198
+ * If there are no outstanding calls (and none are made in the future), then the error does not
199
+ * propagate anywhere -- this is considered a "clean" shutdown.
200
+ */
201
+ receive(): Promise<unknown>;
202
+ /**
203
+ * Indicates that the RPC system has suffered an error that prevents the session from continuing.
204
+ * The transport should ideally try to send any queued messages if it can, and then close the
205
+ * connection. (It's not strictly necessary to deliver queued messages, but the last message sent
206
+ * before abort() is called is often an "abort" message, which communicates the error to the
207
+ * peer, so if that is dropped, the peer may have less information about what happened.)
208
+ */
209
+ abort?(reason: any): void;
210
+ }
211
+ /** Any supported transport type. */
212
+ type AnyRpcTransport = RpcTransport | RpcTransportWithCustomEncoding;
148
213
  /**
149
214
  * Options to customize behavior of an RPC session. All functions which start a session should
150
215
  * optionally accept this.
@@ -170,6 +235,17 @@ type RpcSessionOptions = {
170
235
  * with the given `localMain`.
171
236
  */
172
237
  declare function newWorkersWebSocketRpcResponse(request: Request, localMain?: any, options?: RpcSessionOptions): Response;
238
+ /**
239
+ * Generic WebSocket transport. Default `T = string` is backward-compatible and satisfies
240
+ * `RpcTransport`. Use `T = ArrayBuffer` as a building block for binary transports.
241
+ */
242
+ declare class WebSocketTransport<T extends string | ArrayBuffer = string> {
243
+ #private;
244
+ constructor(webSocket: WebSocket);
245
+ send(message: T): void;
246
+ receive(): Promise<T>;
247
+ abort(reason: any): void;
248
+ }
173
249
  //#endregion
174
250
  //#region src/batch.d.ts
175
251
  /**
@@ -244,7 +320,7 @@ interface RpcSession<T extends RpcCompatible<T> = undefined> {
244
320
  drain(): Promise<void>;
245
321
  }
246
322
  declare const RpcSession: {
247
- new <T extends RpcCompatible<T> = undefined>(transport: RpcTransport, localMain?: any, options?: RpcSessionOptions): RpcSession<T>;
323
+ new <T extends RpcCompatible<T> = undefined>(transport: AnyRpcTransport, localMain?: any, options?: RpcSessionOptions): RpcSession<T>;
248
324
  };
249
325
  /**
250
326
  * Classes which are intended to be passed by reference and called over RPC must extend
@@ -299,5 +375,5 @@ declare let newMessagePortRpcSession: <T extends RpcCompatible<T> = Empty>(port:
299
375
  */
300
376
  declare function newWorkersRpcResponse(request: Request, localMain: any): Promise<Response>;
301
377
  //#endregion
302
- export { type RpcCompatible, RpcPromise, RpcSession, type RpcSessionOptions, RpcStub, RpcTarget, type RpcTransport, deserialize, newHttpBatchRpcResponse, newHttpBatchRpcSession, newMessagePortRpcSession, newWebSocketRpcSession, newWorkersRpcResponse, newWorkersWebSocketRpcResponse, nodeHttpBatchRpcResponse, serialize };
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 };
303
379
  //# sourceMappingURL=index.d.cts.map
package/dist/index.d.ts CHANGED
@@ -107,6 +107,25 @@ type Provider<T> = MaybeCallableProvider<T> & (T extends ReadonlyArray<unknown>
107
107
  });
108
108
  //#endregion
109
109
  //#region src/serialize.d.ts
110
+ /**
111
+ * Encoding levels determine what representation the RPC system hands to the transport.
112
+ * Each level names what the transport can assume about message values.
113
+ *
114
+ * - `"string"`: JSON string. Default, used by HTTP batch and WebSocket transports.
115
+ * - `"jsonCompatible"`: JSON-compatible JS value tree. For custom encoders.
116
+ * - `"jsonCompatibleWithBytes"`: Like `"jsonCompatible"` but Uint8Array stays raw.
117
+ * - `"structuredClonable"`: Structured-clonable native values pass through where possible.
118
+ *
119
+ * @example
120
+ * ```ts
121
+ * // What happens to Uint8Array([1, 2, 3]) at each level:
122
+ * "string" → '["bytes","AQID"]' // JSON string with base64
123
+ * "jsonCompatible" → ["bytes", "AQID"] // JS array with base64
124
+ * "jsonCompatibleWithBytes" → ["bytes", Uint8Array] // JS array with raw bytes
125
+ * "structuredClonable" → ["bytes", Uint8Array] // + Date, BigInt stay native
126
+ * ```
127
+ */
128
+ type EncodingLevel = "string" | "jsonCompatible" | "jsonCompatibleWithBytes" | "structuredClonable";
110
129
  /**
111
130
  * Serialize a value, using Cap'n Web's underlying serialization. This won't be able to serialize
112
131
  * RPC stubs, but it will support basic data types.
@@ -119,14 +138,21 @@ declare function deserialize(value: string): unknown;
119
138
  //#endregion
120
139
  //#region src/rpc.d.ts
121
140
  /**
122
- * Interface for an RPC transport, which is a simple bidirectional message stream. Implement this
123
- * interface if the built-in transports (e.g. for HTTP batch and WebSocket) don't meet your needs.
141
+ * Interface for a string-based RPC transport. This is the default transport type no
142
+ * `encodingLevel` field is needed. Messages are JSON strings. Implement this interface if the
143
+ * built-in transports (e.g. for HTTP batch and WebSocket) don't meet your needs.
124
144
  */
125
145
  interface RpcTransport {
126
146
  /**
127
- * Sends a message to the other end.
147
+ * The encoding level this transport works with. For this interface it is always "string";
148
+ * it may be omitted. (See `RpcTransportWithCustomEncoding` for the other levels.)
128
149
  */
129
- send(message: string): Promise<void>;
150
+ readonly encodingLevel?: "string";
151
+ /**
152
+ * Sends a message to the other end. May optionally return a promise; if the promise rejects,
153
+ * the session is aborted.
154
+ */
155
+ send(message: string): void | Promise<void>;
130
156
  /**
131
157
  * Receives a message sent by the other end.
132
158
  *
@@ -145,6 +171,45 @@ interface RpcTransport {
145
171
  */
146
172
  abort?(reason: any): void;
147
173
  }
174
+ /**
175
+ * Interface for a transport that receives partially encoded JS values instead of JSON strings.
176
+ * The selected `encodingLevel` describes what the transport can assume about message values.
177
+ */
178
+ interface RpcTransportWithCustomEncoding {
179
+ /**
180
+ * The encoding level this transport works with.
181
+ *
182
+ * - "jsonCompatible": JSON-compatible JS value tree; transport handles final serialization.
183
+ * - "jsonCompatibleWithBytes": Like "jsonCompatible" but Uint8Array values are left raw.
184
+ * - "structuredClonable": Structured-clonable native values pass through where possible.
185
+ */
186
+ readonly encodingLevel: "jsonCompatible" | "jsonCompatibleWithBytes" | "structuredClonable";
187
+ /**
188
+ * Encodes and sends a message to the other end. Returns the encoded byte size if known.
189
+ * If the size is unavailable, return void; Cap'n Web will estimate stream message sizes for
190
+ * flow control. Send errors should be propagated via `receive()` rejecting.
191
+ */
192
+ send(message: unknown): number | void;
193
+ /**
194
+ * Receives and decodes a message sent by the other end.
195
+ *
196
+ * If and when the transport becomes disconnected, this will reject. The thrown error will be
197
+ * propagated to all outstanding calls and future calls on any stubs associated with the session.
198
+ * If there are no outstanding calls (and none are made in the future), then the error does not
199
+ * propagate anywhere -- this is considered a "clean" shutdown.
200
+ */
201
+ receive(): Promise<unknown>;
202
+ /**
203
+ * Indicates that the RPC system has suffered an error that prevents the session from continuing.
204
+ * The transport should ideally try to send any queued messages if it can, and then close the
205
+ * connection. (It's not strictly necessary to deliver queued messages, but the last message sent
206
+ * before abort() is called is often an "abort" message, which communicates the error to the
207
+ * peer, so if that is dropped, the peer may have less information about what happened.)
208
+ */
209
+ abort?(reason: any): void;
210
+ }
211
+ /** Any supported transport type. */
212
+ type AnyRpcTransport = RpcTransport | RpcTransportWithCustomEncoding;
148
213
  /**
149
214
  * Options to customize behavior of an RPC session. All functions which start a session should
150
215
  * optionally accept this.
@@ -170,6 +235,17 @@ type RpcSessionOptions = {
170
235
  * with the given `localMain`.
171
236
  */
172
237
  declare function newWorkersWebSocketRpcResponse(request: Request, localMain?: any, options?: RpcSessionOptions): Response;
238
+ /**
239
+ * Generic WebSocket transport. Default `T = string` is backward-compatible and satisfies
240
+ * `RpcTransport`. Use `T = ArrayBuffer` as a building block for binary transports.
241
+ */
242
+ declare class WebSocketTransport<T extends string | ArrayBuffer = string> {
243
+ #private;
244
+ constructor(webSocket: WebSocket);
245
+ send(message: T): void;
246
+ receive(): Promise<T>;
247
+ abort(reason: any): void;
248
+ }
173
249
  //#endregion
174
250
  //#region src/batch.d.ts
175
251
  /**
@@ -244,7 +320,7 @@ interface RpcSession<T extends RpcCompatible<T> = undefined> {
244
320
  drain(): Promise<void>;
245
321
  }
246
322
  declare const RpcSession: {
247
- new <T extends RpcCompatible<T> = undefined>(transport: RpcTransport, localMain?: any, options?: RpcSessionOptions): RpcSession<T>;
323
+ new <T extends RpcCompatible<T> = undefined>(transport: AnyRpcTransport, localMain?: any, options?: RpcSessionOptions): RpcSession<T>;
248
324
  };
249
325
  /**
250
326
  * Classes which are intended to be passed by reference and called over RPC must extend
@@ -299,5 +375,5 @@ declare let newMessagePortRpcSession: <T extends RpcCompatible<T> = Empty>(port:
299
375
  */
300
376
  declare function newWorkersRpcResponse(request: Request, localMain: any): Promise<Response>;
301
377
  //#endregion
302
- export { type RpcCompatible, RpcPromise, RpcSession, type RpcSessionOptions, RpcStub, RpcTarget, type RpcTransport, deserialize, newHttpBatchRpcResponse, newHttpBatchRpcSession, newMessagePortRpcSession, newWebSocketRpcSession, newWorkersRpcResponse, newWorkersWebSocketRpcResponse, nodeHttpBatchRpcResponse, serialize };
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 };
303
379
  //# sourceMappingURL=index.d.ts.map