@actuallab/rpc 12.2.23 → 12.3.2

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
@@ -734,29 +734,44 @@ interface RpcStreamRef {
734
734
  readonly allowReconnect: boolean;
735
735
  readonly isRealTime: boolean;
736
736
  }
737
+ /** Configuration options for creating a local (origin-side) RpcStream. */
738
+ interface RpcStreamOptions<T> {
739
+ ackPeriod?: number;
740
+ ackAdvance?: number;
741
+ allowReconnect?: boolean;
742
+ isRealTime?: boolean;
743
+ canSkipTo?: (item: T) => boolean;
744
+ }
737
745
  /**
738
746
  * Parses a stream reference string of the form "{hostId},{localId},{ackPeriod},{ackAdvance}".
739
747
  * Returns null if the value is not a valid stream reference.
740
748
  */
741
749
  declare function parseStreamRef(value: unknown): RpcStreamRef | null;
742
750
  /**
743
- * Client-side RPC stream — consumes items sent by the server via system calls
744
- * ($sys.I, $sys.B, $sys.End) and implements AsyncIterable<T> for for-await-of consumption.
751
+ * Dual-mode RPC stream — works as both local (origin-side) and remote (target-side).
745
752
  *
746
- * Wire protocol:
747
- * - Client sends Ack(0, hostId) to start the stream
748
- * - Server sends I (single item) / B (batch) messages
749
- * - Client acks every ackPeriod items for flow control
750
- * - Server sends End to signal completion (with optional error)
751
- * - Client sends AckEnd to acknowledge completion
753
+ * **Local mode**: wraps an AsyncIterable and carries streaming configuration
754
+ * (IsRealTime, CanSkipTo, etc.). Created by service methods and sent to the
755
+ * remote peer via RpcStreamSender.
756
+ *
757
+ * **Remote mode**: consumes items sent by the server via system calls
758
+ * ($sys.I, $sys.B, $sys.End) and implements AsyncIterable for for-await-of consumption.
759
+ *
760
+ * This mirrors the .NET RpcStream<T> design where the same type serves both roles.
752
761
  */
753
762
  declare class RpcStream<T> implements AsyncIterable<T>, IRpcObject {
754
763
  readonly id: RpcObjectId;
755
- readonly kind: 1;
764
+ readonly kind: RpcObjectKind;
756
765
  readonly allowReconnect: boolean;
757
- readonly peer: RpcPeer;
766
+ readonly isRealTime: boolean;
767
+ readonly canSkipTo: (item: T) => boolean;
758
768
  readonly ackPeriod: number;
759
769
  readonly ackAdvance: number;
770
+ readonly localSource?: AsyncIterable<T>;
771
+ readonly peer: RpcPeer;
772
+ private _sender?;
773
+ private _toRefValue?;
774
+ private _whenSent?;
760
775
  private _buffer;
761
776
  private _nextExpectedIndex;
762
777
  private _consumerWaiting;
@@ -766,7 +781,27 @@ declare class RpcStream<T> implements AsyncIterable<T>, IRpcObject {
766
781
  private _started;
767
782
  private _iterating;
768
783
  private _ackSentUpTo;
784
+ /** Create a local (origin-side) stream wrapping an async iterable with optional configuration. */
785
+ constructor(source: AsyncIterable<T>, options?: RpcStreamOptions<T>);
786
+ /** Create a remote (target-side) stream from a parsed stream reference and peer. */
769
787
  constructor(ref: RpcStreamRef, peer: RpcPeer);
788
+ /**
789
+ * For local streams: resolves when the sender finishes pumping (after `toRef()` was called).
790
+ * Rejects if the source throws. Throws if `toRef()` hasn't been called yet.
791
+ */
792
+ get whenSent(): Promise<void>;
793
+ /**
794
+ * For local streams: bind to a peer, create and register an `RpcStreamSender`,
795
+ * start pumping items from the local source in the background, and return the
796
+ * stream reference.
797
+ *
798
+ * For binary peers the ref is a MessagePack-compatible object
799
+ * (`{ SerializedId, AckPeriod, AckAdvance, AllowReconnect }`);
800
+ * for text peers it is a comma-separated string.
801
+ *
802
+ * Can only be called once per stream.
803
+ */
804
+ toRef(peer: RpcPeer): unknown;
770
805
  /** Called by system call handler when a single item arrives ($sys.I). */
771
806
  onItem(index: number, item: T): void;
772
807
  /** Called by system call handler when a batch arrives ($sys.B). */
@@ -804,6 +839,7 @@ declare function resolveStreamRefs(value: unknown, peer: RpcPeer): unknown;
804
839
  * - Client sends AckEnd to acknowledge completion
805
840
  */
806
841
  declare class RpcStreamSender<T> implements IRpcObject {
842
+ private static readonly _emptyGuid;
807
843
  readonly id: RpcObjectId;
808
844
  readonly kind: 0;
809
845
  readonly allowReconnect: boolean;
@@ -817,11 +853,12 @@ declare class RpcStreamSender<T> implements IRpcObject {
817
853
  private _started;
818
854
  private _lastAckedIndex;
819
855
  private _ackWaiting;
856
+ private _resetRequested;
820
857
  constructor(peer: RpcPeer, ackPeriod?: number, ackAdvance?: number, allowReconnect?: boolean, isRealTime?: boolean, canSkipTo?: (item: T) => boolean);
821
858
  /** Returns the stream reference string for the $sys.Ok response. */
822
859
  toRef(): string;
823
860
  /** Called by system call handler when $sys.Ack is received from the client. */
824
- onAck(nextIndex: number, _hostId: string): void;
861
+ onAck(nextIndex: number, hostId: string): void;
825
862
  /** Called by system call handler when $sys.AckEnd is received from the client. */
826
863
  onAckEnd(_hostId: string): void;
827
864
  /** Send a single item to the client. */
@@ -930,4 +967,4 @@ declare function xxh3_64str(input: string, seed?: bigint): bigint;
930
967
  */
931
968
  declare function computeMethodHash(methodName: string): number;
932
969
 
933
- export { ARG_DELIMITER, ENVELOPE_DELIMITER, FRAME_DELIMITER, type IRpcObject, type MethodMeta, RPC_CLOSE_CODE_UNSUPPORTED_FORMAT, type RemoteHandshake, type RpcCallOptions, RpcCallTypeId, RpcClientPeer, RpcClientPeerReconnectDelayer, type RpcConnection, type RpcConnectionUrlResolver, type RpcDeserializedMessage, type RpcDispatchContext, RpcHub, RpcInboundCall, RpcInboundCallTracker, RpcJsonSerializationFormat, type RpcMessage, RpcMessageChannelConnection, RpcMessagePackCompactSerializationFormat, RpcMessagePackSerializationFormat, type RpcMethodDef, type RpcMethodDefInput, RpcMethodRegistry, type RpcObjectId, RpcObjectKind, RpcOutboundCall, RpcOutboundCallTracker, RpcPeer, RpcPeerConnectionKind, type RpcPeerState, RpcPeerStateKind, RpcPeerStateMonitor, type RpcReceivedMessage, RpcRemoteObjectTracker, RpcSerializationFormat, RpcSerializationFormatResolver, RpcServerPeer, type RpcServiceDef, RpcServiceHost, type RpcServiceImpl, RpcSharedObjectTracker, RpcStream, type RpcStreamRef, RpcStreamSender, RpcSystemCallHandler, RpcSystemCallSender, RpcSystemCalls, RpcType, RpcWebSocketConnection, type RpcWireData, type ServiceMeta, type WebSocketLike, type WebSocketServer, WebSocketState, computeMethodHash, createBinaryEncoder, createMessageChannelPair, createRpcClient, defaultBinaryDecoder, defaultBinaryEncoder, defaultConnectionUrlResolver, defineRpcService, deserializeBinaryMessage, deserializeCompactBinaryMessage, deserializeMessage, getMethodsMeta, getServiceMeta, getStateDescription, isConnected, likelyConnected, parseStreamRef, resolveStreamRefs, rpcMethod, rpcService, serializeBinaryFrame, serializeBinaryMessage, serializeCompactBinaryMessage, serializeFrame, serializeMessage, splitBinaryFrame, splitCompactBinaryFrame, splitFrame, wireMethodName, xxh3_64, xxh3_64str };
970
+ export { ARG_DELIMITER, ENVELOPE_DELIMITER, FRAME_DELIMITER, type IRpcObject, type MethodMeta, RPC_CLOSE_CODE_UNSUPPORTED_FORMAT, type RemoteHandshake, type RpcCallOptions, RpcCallTypeId, RpcClientPeer, RpcClientPeerReconnectDelayer, type RpcConnection, type RpcConnectionUrlResolver, type RpcDeserializedMessage, type RpcDispatchContext, RpcHub, RpcInboundCall, RpcInboundCallTracker, RpcJsonSerializationFormat, type RpcMessage, RpcMessageChannelConnection, RpcMessagePackCompactSerializationFormat, RpcMessagePackSerializationFormat, type RpcMethodDef, type RpcMethodDefInput, RpcMethodRegistry, type RpcObjectId, RpcObjectKind, RpcOutboundCall, RpcOutboundCallTracker, RpcPeer, RpcPeerConnectionKind, type RpcPeerState, RpcPeerStateKind, RpcPeerStateMonitor, type RpcReceivedMessage, RpcRemoteObjectTracker, RpcSerializationFormat, RpcSerializationFormatResolver, RpcServerPeer, type RpcServiceDef, RpcServiceHost, type RpcServiceImpl, RpcSharedObjectTracker, RpcStream, type RpcStreamOptions, type RpcStreamRef, RpcStreamSender, RpcSystemCallHandler, RpcSystemCallSender, RpcSystemCalls, RpcType, RpcWebSocketConnection, type RpcWireData, type ServiceMeta, type WebSocketLike, type WebSocketServer, WebSocketState, computeMethodHash, createBinaryEncoder, createMessageChannelPair, createRpcClient, defaultBinaryDecoder, defaultBinaryEncoder, defaultConnectionUrlResolver, defineRpcService, deserializeBinaryMessage, deserializeCompactBinaryMessage, deserializeMessage, getMethodsMeta, getServiceMeta, getStateDescription, isConnected, likelyConnected, parseStreamRef, resolveStreamRefs, rpcMethod, rpcService, serializeBinaryFrame, serializeBinaryMessage, serializeCompactBinaryMessage, serializeFrame, serializeMessage, splitBinaryFrame, splitCompactBinaryFrame, splitFrame, wireMethodName, xxh3_64, xxh3_64str };
package/dist/index.d.ts CHANGED
@@ -734,29 +734,44 @@ interface RpcStreamRef {
734
734
  readonly allowReconnect: boolean;
735
735
  readonly isRealTime: boolean;
736
736
  }
737
+ /** Configuration options for creating a local (origin-side) RpcStream. */
738
+ interface RpcStreamOptions<T> {
739
+ ackPeriod?: number;
740
+ ackAdvance?: number;
741
+ allowReconnect?: boolean;
742
+ isRealTime?: boolean;
743
+ canSkipTo?: (item: T) => boolean;
744
+ }
737
745
  /**
738
746
  * Parses a stream reference string of the form "{hostId},{localId},{ackPeriod},{ackAdvance}".
739
747
  * Returns null if the value is not a valid stream reference.
740
748
  */
741
749
  declare function parseStreamRef(value: unknown): RpcStreamRef | null;
742
750
  /**
743
- * Client-side RPC stream — consumes items sent by the server via system calls
744
- * ($sys.I, $sys.B, $sys.End) and implements AsyncIterable<T> for for-await-of consumption.
751
+ * Dual-mode RPC stream — works as both local (origin-side) and remote (target-side).
745
752
  *
746
- * Wire protocol:
747
- * - Client sends Ack(0, hostId) to start the stream
748
- * - Server sends I (single item) / B (batch) messages
749
- * - Client acks every ackPeriod items for flow control
750
- * - Server sends End to signal completion (with optional error)
751
- * - Client sends AckEnd to acknowledge completion
753
+ * **Local mode**: wraps an AsyncIterable and carries streaming configuration
754
+ * (IsRealTime, CanSkipTo, etc.). Created by service methods and sent to the
755
+ * remote peer via RpcStreamSender.
756
+ *
757
+ * **Remote mode**: consumes items sent by the server via system calls
758
+ * ($sys.I, $sys.B, $sys.End) and implements AsyncIterable for for-await-of consumption.
759
+ *
760
+ * This mirrors the .NET RpcStream<T> design where the same type serves both roles.
752
761
  */
753
762
  declare class RpcStream<T> implements AsyncIterable<T>, IRpcObject {
754
763
  readonly id: RpcObjectId;
755
- readonly kind: 1;
764
+ readonly kind: RpcObjectKind;
756
765
  readonly allowReconnect: boolean;
757
- readonly peer: RpcPeer;
766
+ readonly isRealTime: boolean;
767
+ readonly canSkipTo: (item: T) => boolean;
758
768
  readonly ackPeriod: number;
759
769
  readonly ackAdvance: number;
770
+ readonly localSource?: AsyncIterable<T>;
771
+ readonly peer: RpcPeer;
772
+ private _sender?;
773
+ private _toRefValue?;
774
+ private _whenSent?;
760
775
  private _buffer;
761
776
  private _nextExpectedIndex;
762
777
  private _consumerWaiting;
@@ -766,7 +781,27 @@ declare class RpcStream<T> implements AsyncIterable<T>, IRpcObject {
766
781
  private _started;
767
782
  private _iterating;
768
783
  private _ackSentUpTo;
784
+ /** Create a local (origin-side) stream wrapping an async iterable with optional configuration. */
785
+ constructor(source: AsyncIterable<T>, options?: RpcStreamOptions<T>);
786
+ /** Create a remote (target-side) stream from a parsed stream reference and peer. */
769
787
  constructor(ref: RpcStreamRef, peer: RpcPeer);
788
+ /**
789
+ * For local streams: resolves when the sender finishes pumping (after `toRef()` was called).
790
+ * Rejects if the source throws. Throws if `toRef()` hasn't been called yet.
791
+ */
792
+ get whenSent(): Promise<void>;
793
+ /**
794
+ * For local streams: bind to a peer, create and register an `RpcStreamSender`,
795
+ * start pumping items from the local source in the background, and return the
796
+ * stream reference.
797
+ *
798
+ * For binary peers the ref is a MessagePack-compatible object
799
+ * (`{ SerializedId, AckPeriod, AckAdvance, AllowReconnect }`);
800
+ * for text peers it is a comma-separated string.
801
+ *
802
+ * Can only be called once per stream.
803
+ */
804
+ toRef(peer: RpcPeer): unknown;
770
805
  /** Called by system call handler when a single item arrives ($sys.I). */
771
806
  onItem(index: number, item: T): void;
772
807
  /** Called by system call handler when a batch arrives ($sys.B). */
@@ -804,6 +839,7 @@ declare function resolveStreamRefs(value: unknown, peer: RpcPeer): unknown;
804
839
  * - Client sends AckEnd to acknowledge completion
805
840
  */
806
841
  declare class RpcStreamSender<T> implements IRpcObject {
842
+ private static readonly _emptyGuid;
807
843
  readonly id: RpcObjectId;
808
844
  readonly kind: 0;
809
845
  readonly allowReconnect: boolean;
@@ -817,11 +853,12 @@ declare class RpcStreamSender<T> implements IRpcObject {
817
853
  private _started;
818
854
  private _lastAckedIndex;
819
855
  private _ackWaiting;
856
+ private _resetRequested;
820
857
  constructor(peer: RpcPeer, ackPeriod?: number, ackAdvance?: number, allowReconnect?: boolean, isRealTime?: boolean, canSkipTo?: (item: T) => boolean);
821
858
  /** Returns the stream reference string for the $sys.Ok response. */
822
859
  toRef(): string;
823
860
  /** Called by system call handler when $sys.Ack is received from the client. */
824
- onAck(nextIndex: number, _hostId: string): void;
861
+ onAck(nextIndex: number, hostId: string): void;
825
862
  /** Called by system call handler when $sys.AckEnd is received from the client. */
826
863
  onAckEnd(_hostId: string): void;
827
864
  /** Send a single item to the client. */
@@ -930,4 +967,4 @@ declare function xxh3_64str(input: string, seed?: bigint): bigint;
930
967
  */
931
968
  declare function computeMethodHash(methodName: string): number;
932
969
 
933
- export { ARG_DELIMITER, ENVELOPE_DELIMITER, FRAME_DELIMITER, type IRpcObject, type MethodMeta, RPC_CLOSE_CODE_UNSUPPORTED_FORMAT, type RemoteHandshake, type RpcCallOptions, RpcCallTypeId, RpcClientPeer, RpcClientPeerReconnectDelayer, type RpcConnection, type RpcConnectionUrlResolver, type RpcDeserializedMessage, type RpcDispatchContext, RpcHub, RpcInboundCall, RpcInboundCallTracker, RpcJsonSerializationFormat, type RpcMessage, RpcMessageChannelConnection, RpcMessagePackCompactSerializationFormat, RpcMessagePackSerializationFormat, type RpcMethodDef, type RpcMethodDefInput, RpcMethodRegistry, type RpcObjectId, RpcObjectKind, RpcOutboundCall, RpcOutboundCallTracker, RpcPeer, RpcPeerConnectionKind, type RpcPeerState, RpcPeerStateKind, RpcPeerStateMonitor, type RpcReceivedMessage, RpcRemoteObjectTracker, RpcSerializationFormat, RpcSerializationFormatResolver, RpcServerPeer, type RpcServiceDef, RpcServiceHost, type RpcServiceImpl, RpcSharedObjectTracker, RpcStream, type RpcStreamRef, RpcStreamSender, RpcSystemCallHandler, RpcSystemCallSender, RpcSystemCalls, RpcType, RpcWebSocketConnection, type RpcWireData, type ServiceMeta, type WebSocketLike, type WebSocketServer, WebSocketState, computeMethodHash, createBinaryEncoder, createMessageChannelPair, createRpcClient, defaultBinaryDecoder, defaultBinaryEncoder, defaultConnectionUrlResolver, defineRpcService, deserializeBinaryMessage, deserializeCompactBinaryMessage, deserializeMessage, getMethodsMeta, getServiceMeta, getStateDescription, isConnected, likelyConnected, parseStreamRef, resolveStreamRefs, rpcMethod, rpcService, serializeBinaryFrame, serializeBinaryMessage, serializeCompactBinaryMessage, serializeFrame, serializeMessage, splitBinaryFrame, splitCompactBinaryFrame, splitFrame, wireMethodName, xxh3_64, xxh3_64str };
970
+ export { ARG_DELIMITER, ENVELOPE_DELIMITER, FRAME_DELIMITER, type IRpcObject, type MethodMeta, RPC_CLOSE_CODE_UNSUPPORTED_FORMAT, type RemoteHandshake, type RpcCallOptions, RpcCallTypeId, RpcClientPeer, RpcClientPeerReconnectDelayer, type RpcConnection, type RpcConnectionUrlResolver, type RpcDeserializedMessage, type RpcDispatchContext, RpcHub, RpcInboundCall, RpcInboundCallTracker, RpcJsonSerializationFormat, type RpcMessage, RpcMessageChannelConnection, RpcMessagePackCompactSerializationFormat, RpcMessagePackSerializationFormat, type RpcMethodDef, type RpcMethodDefInput, RpcMethodRegistry, type RpcObjectId, RpcObjectKind, RpcOutboundCall, RpcOutboundCallTracker, RpcPeer, RpcPeerConnectionKind, type RpcPeerState, RpcPeerStateKind, RpcPeerStateMonitor, type RpcReceivedMessage, RpcRemoteObjectTracker, RpcSerializationFormat, RpcSerializationFormatResolver, RpcServerPeer, type RpcServiceDef, RpcServiceHost, type RpcServiceImpl, RpcSharedObjectTracker, RpcStream, type RpcStreamOptions, type RpcStreamRef, RpcStreamSender, RpcSystemCallHandler, RpcSystemCallSender, RpcSystemCalls, RpcType, RpcWebSocketConnection, type RpcWireData, type ServiceMeta, type WebSocketLike, type WebSocketServer, WebSocketState, computeMethodHash, createBinaryEncoder, createMessageChannelPair, createRpcClient, defaultBinaryDecoder, defaultBinaryEncoder, defaultConnectionUrlResolver, defineRpcService, deserializeBinaryMessage, deserializeCompactBinaryMessage, deserializeMessage, getMethodsMeta, getServiceMeta, getStateDescription, isConnected, likelyConnected, parseStreamRef, resolveStreamRefs, rpcMethod, rpcService, serializeBinaryFrame, serializeBinaryMessage, serializeCompactBinaryMessage, serializeFrame, serializeMessage, splitBinaryFrame, splitCompactBinaryFrame, splitFrame, wireMethodName, xxh3_64, xxh3_64str };