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.
@@ -179,6 +179,25 @@ declare class RpcPayload {
179
179
  }
180
180
  //#endregion
181
181
  //#region src/serialize.d.ts
182
+ /**
183
+ * Encoding levels determine what representation the RPC system hands to the transport.
184
+ * Each level names what the transport can assume about message values.
185
+ *
186
+ * - `"string"`: JSON string. Default, used by HTTP batch and WebSocket transports.
187
+ * - `"jsonCompatible"`: JSON-compatible JS value tree. For custom encoders.
188
+ * - `"jsonCompatibleWithBytes"`: Like `"jsonCompatible"` but Uint8Array stays raw.
189
+ * - `"structuredClonable"`: Structured-clonable native values pass through where possible.
190
+ *
191
+ * @example
192
+ * ```ts
193
+ * // What happens to Uint8Array([1, 2, 3]) at each level:
194
+ * "string" → '["bytes","AQID"]' // JSON string with base64
195
+ * "jsonCompatible" → ["bytes", "AQID"] // JS array with base64
196
+ * "jsonCompatibleWithBytes" → ["bytes", Uint8Array] // JS array with raw bytes
197
+ * "structuredClonable" → ["bytes", Uint8Array] // + Date, BigInt stay native
198
+ * ```
199
+ */
200
+ type EncodingLevel = "string" | "jsonCompatible" | "jsonCompatibleWithBytes" | "structuredClonable";
182
201
  /**
183
202
  * Serialize a value, using Cap'n Web's underlying serialization. This won't be able to serialize
184
203
  * RPC stubs, but it will support basic data types.
@@ -191,14 +210,21 @@ declare function deserialize(value: string): unknown;
191
210
  //#endregion
192
211
  //#region src/rpc.d.ts
193
212
  /**
194
- * Interface for an RPC transport, which is a simple bidirectional message stream. Implement this
195
- * interface if the built-in transports (e.g. for HTTP batch and WebSocket) don't meet your needs.
213
+ * Interface for a string-based RPC transport. This is the default transport type no
214
+ * `encodingLevel` field is needed. Messages are JSON strings. Implement this interface if the
215
+ * built-in transports (e.g. for HTTP batch and WebSocket) don't meet your needs.
196
216
  */
197
217
  interface RpcTransport {
198
218
  /**
199
- * Sends a message to the other end.
219
+ * The encoding level this transport works with. For this interface it is always "string";
220
+ * it may be omitted. (See `RpcTransportWithCustomEncoding` for the other levels.)
200
221
  */
201
- send(message: string): Promise<void>;
222
+ readonly encodingLevel?: "string";
223
+ /**
224
+ * Sends a message to the other end. May optionally return a promise; if the promise rejects,
225
+ * the session is aborted.
226
+ */
227
+ send(message: string): void | Promise<void>;
202
228
  /**
203
229
  * Receives a message sent by the other end.
204
230
  *
@@ -217,6 +243,45 @@ interface RpcTransport {
217
243
  */
218
244
  abort?(reason: any): void;
219
245
  }
246
+ /**
247
+ * Interface for a transport that receives partially encoded JS values instead of JSON strings.
248
+ * The selected `encodingLevel` describes what the transport can assume about message values.
249
+ */
250
+ interface RpcTransportWithCustomEncoding {
251
+ /**
252
+ * The encoding level this transport works with.
253
+ *
254
+ * - "jsonCompatible": JSON-compatible JS value tree; transport handles final serialization.
255
+ * - "jsonCompatibleWithBytes": Like "jsonCompatible" but Uint8Array values are left raw.
256
+ * - "structuredClonable": Structured-clonable native values pass through where possible.
257
+ */
258
+ readonly encodingLevel: "jsonCompatible" | "jsonCompatibleWithBytes" | "structuredClonable";
259
+ /**
260
+ * Encodes and sends a message to the other end. Returns the encoded byte size if known.
261
+ * If the size is unavailable, return void; Cap'n Web will estimate stream message sizes for
262
+ * flow control. Send errors should be propagated via `receive()` rejecting.
263
+ */
264
+ send(message: unknown): number | void;
265
+ /**
266
+ * Receives and decodes a message sent by the other end.
267
+ *
268
+ * If and when the transport becomes disconnected, this will reject. The thrown error will be
269
+ * propagated to all outstanding calls and future calls on any stubs associated with the session.
270
+ * If there are no outstanding calls (and none are made in the future), then the error does not
271
+ * propagate anywhere -- this is considered a "clean" shutdown.
272
+ */
273
+ receive(): Promise<unknown>;
274
+ /**
275
+ * Indicates that the RPC system has suffered an error that prevents the session from continuing.
276
+ * The transport should ideally try to send any queued messages if it can, and then close the
277
+ * connection. (It's not strictly necessary to deliver queued messages, but the last message sent
278
+ * before abort() is called is often an "abort" message, which communicates the error to the
279
+ * peer, so if that is dropped, the peer may have less information about what happened.)
280
+ */
281
+ abort?(reason: any): void;
282
+ }
283
+ /** Any supported transport type. */
284
+ type AnyRpcTransport = RpcTransport | RpcTransportWithCustomEncoding;
220
285
  /**
221
286
  * Options to customize behavior of an RPC session. All functions which start a session should
222
287
  * optionally accept this.
@@ -242,6 +307,17 @@ type RpcSessionOptions = {
242
307
  * with the given `localMain`.
243
308
  */
244
309
  declare function newWorkersWebSocketRpcResponse(request: Request, localMain?: any, options?: RpcSessionOptions): Response;
310
+ /**
311
+ * Generic WebSocket transport. Default `T = string` is backward-compatible and satisfies
312
+ * `RpcTransport`. Use `T = ArrayBuffer` as a building block for binary transports.
313
+ */
314
+ declare class WebSocketTransport<T extends string | ArrayBuffer = string> {
315
+ #private;
316
+ constructor(webSocket: WebSocket);
317
+ send(message: T): void;
318
+ receive(): Promise<T>;
319
+ abort(reason: any): void;
320
+ }
245
321
  //#endregion
246
322
  //#region src/batch.d.ts
247
323
  /**
@@ -316,7 +392,7 @@ interface RpcSession<T extends RpcCompatible<T> = undefined> {
316
392
  drain(): Promise<void>;
317
393
  }
318
394
  declare const RpcSession: {
319
- new <T extends RpcCompatible<T> = undefined>(transport: RpcTransport, localMain?: any, options?: RpcSessionOptions): RpcSession<T>;
395
+ new <T extends RpcCompatible<T> = undefined>(transport: AnyRpcTransport, localMain?: any, options?: RpcSessionOptions): RpcSession<T>;
320
396
  };
321
397
  /**
322
398
  * Classes which are intended to be passed by reference and called over RPC must extend
@@ -420,5 +496,5 @@ declare let newBunWebSocketRpcSession: <T extends RpcCompatible<T> = Empty, D =
420
496
  transport: BunWebSocketTransport<D>;
421
497
  };
422
498
  //#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 };
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 };
424
500
  //# sourceMappingURL=index-bun.d.cts.map
@@ -179,6 +179,25 @@ declare class RpcPayload {
179
179
  }
180
180
  //#endregion
181
181
  //#region src/serialize.d.ts
182
+ /**
183
+ * Encoding levels determine what representation the RPC system hands to the transport.
184
+ * Each level names what the transport can assume about message values.
185
+ *
186
+ * - `"string"`: JSON string. Default, used by HTTP batch and WebSocket transports.
187
+ * - `"jsonCompatible"`: JSON-compatible JS value tree. For custom encoders.
188
+ * - `"jsonCompatibleWithBytes"`: Like `"jsonCompatible"` but Uint8Array stays raw.
189
+ * - `"structuredClonable"`: Structured-clonable native values pass through where possible.
190
+ *
191
+ * @example
192
+ * ```ts
193
+ * // What happens to Uint8Array([1, 2, 3]) at each level:
194
+ * "string" → '["bytes","AQID"]' // JSON string with base64
195
+ * "jsonCompatible" → ["bytes", "AQID"] // JS array with base64
196
+ * "jsonCompatibleWithBytes" → ["bytes", Uint8Array] // JS array with raw bytes
197
+ * "structuredClonable" → ["bytes", Uint8Array] // + Date, BigInt stay native
198
+ * ```
199
+ */
200
+ type EncodingLevel = "string" | "jsonCompatible" | "jsonCompatibleWithBytes" | "structuredClonable";
182
201
  /**
183
202
  * Serialize a value, using Cap'n Web's underlying serialization. This won't be able to serialize
184
203
  * RPC stubs, but it will support basic data types.
@@ -191,14 +210,21 @@ declare function deserialize(value: string): unknown;
191
210
  //#endregion
192
211
  //#region src/rpc.d.ts
193
212
  /**
194
- * Interface for an RPC transport, which is a simple bidirectional message stream. Implement this
195
- * interface if the built-in transports (e.g. for HTTP batch and WebSocket) don't meet your needs.
213
+ * Interface for a string-based RPC transport. This is the default transport type no
214
+ * `encodingLevel` field is needed. Messages are JSON strings. Implement this interface if the
215
+ * built-in transports (e.g. for HTTP batch and WebSocket) don't meet your needs.
196
216
  */
197
217
  interface RpcTransport {
198
218
  /**
199
- * Sends a message to the other end.
219
+ * The encoding level this transport works with. For this interface it is always "string";
220
+ * it may be omitted. (See `RpcTransportWithCustomEncoding` for the other levels.)
200
221
  */
201
- send(message: string): Promise<void>;
222
+ readonly encodingLevel?: "string";
223
+ /**
224
+ * Sends a message to the other end. May optionally return a promise; if the promise rejects,
225
+ * the session is aborted.
226
+ */
227
+ send(message: string): void | Promise<void>;
202
228
  /**
203
229
  * Receives a message sent by the other end.
204
230
  *
@@ -217,6 +243,45 @@ interface RpcTransport {
217
243
  */
218
244
  abort?(reason: any): void;
219
245
  }
246
+ /**
247
+ * Interface for a transport that receives partially encoded JS values instead of JSON strings.
248
+ * The selected `encodingLevel` describes what the transport can assume about message values.
249
+ */
250
+ interface RpcTransportWithCustomEncoding {
251
+ /**
252
+ * The encoding level this transport works with.
253
+ *
254
+ * - "jsonCompatible": JSON-compatible JS value tree; transport handles final serialization.
255
+ * - "jsonCompatibleWithBytes": Like "jsonCompatible" but Uint8Array values are left raw.
256
+ * - "structuredClonable": Structured-clonable native values pass through where possible.
257
+ */
258
+ readonly encodingLevel: "jsonCompatible" | "jsonCompatibleWithBytes" | "structuredClonable";
259
+ /**
260
+ * Encodes and sends a message to the other end. Returns the encoded byte size if known.
261
+ * If the size is unavailable, return void; Cap'n Web will estimate stream message sizes for
262
+ * flow control. Send errors should be propagated via `receive()` rejecting.
263
+ */
264
+ send(message: unknown): number | void;
265
+ /**
266
+ * Receives and decodes a message sent by the other end.
267
+ *
268
+ * If and when the transport becomes disconnected, this will reject. The thrown error will be
269
+ * propagated to all outstanding calls and future calls on any stubs associated with the session.
270
+ * If there are no outstanding calls (and none are made in the future), then the error does not
271
+ * propagate anywhere -- this is considered a "clean" shutdown.
272
+ */
273
+ receive(): Promise<unknown>;
274
+ /**
275
+ * Indicates that the RPC system has suffered an error that prevents the session from continuing.
276
+ * The transport should ideally try to send any queued messages if it can, and then close the
277
+ * connection. (It's not strictly necessary to deliver queued messages, but the last message sent
278
+ * before abort() is called is often an "abort" message, which communicates the error to the
279
+ * peer, so if that is dropped, the peer may have less information about what happened.)
280
+ */
281
+ abort?(reason: any): void;
282
+ }
283
+ /** Any supported transport type. */
284
+ type AnyRpcTransport = RpcTransport | RpcTransportWithCustomEncoding;
220
285
  /**
221
286
  * Options to customize behavior of an RPC session. All functions which start a session should
222
287
  * optionally accept this.
@@ -242,6 +307,17 @@ type RpcSessionOptions = {
242
307
  * with the given `localMain`.
243
308
  */
244
309
  declare function newWorkersWebSocketRpcResponse(request: Request, localMain?: any, options?: RpcSessionOptions): Response;
310
+ /**
311
+ * Generic WebSocket transport. Default `T = string` is backward-compatible and satisfies
312
+ * `RpcTransport`. Use `T = ArrayBuffer` as a building block for binary transports.
313
+ */
314
+ declare class WebSocketTransport<T extends string | ArrayBuffer = string> {
315
+ #private;
316
+ constructor(webSocket: WebSocket);
317
+ send(message: T): void;
318
+ receive(): Promise<T>;
319
+ abort(reason: any): void;
320
+ }
245
321
  //#endregion
246
322
  //#region src/batch.d.ts
247
323
  /**
@@ -316,7 +392,7 @@ interface RpcSession<T extends RpcCompatible<T> = undefined> {
316
392
  drain(): Promise<void>;
317
393
  }
318
394
  declare const RpcSession: {
319
- new <T extends RpcCompatible<T> = undefined>(transport: RpcTransport, localMain?: any, options?: RpcSessionOptions): RpcSession<T>;
395
+ new <T extends RpcCompatible<T> = undefined>(transport: AnyRpcTransport, localMain?: any, options?: RpcSessionOptions): RpcSession<T>;
320
396
  };
321
397
  /**
322
398
  * Classes which are intended to be passed by reference and called over RPC must extend
@@ -420,5 +496,5 @@ declare let newBunWebSocketRpcSession: <T extends RpcCompatible<T> = Empty, D =
420
496
  transport: BunWebSocketTransport<D>;
421
497
  };
422
498
  //#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 };
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 };
424
500
  //# sourceMappingURL=index-bun.d.ts.map