nats
Version:
Node.js client for NATS, a lightweight, high-performance cloud native messaging system
1,594 lines (1,593 loc) • 80.7 kB
TypeScript
import { NatsError } from "./error";
export { NatsError } from "./error";
import type { MsgHdrs } from "./headers";
export type { MsgHdrs } from "./headers";
import type { Authenticator } from "./authenticator";
export type { Authenticator } from "./authenticator";
import type { ServiceClient } from "./serviceclient";
export type { ServiceClient } from "./serviceclient";
import type { TypedSubscriptionOptions } from "./typedsub";
export type { TypedSubscriptionOptions } from "./typedsub";
import { QueuedIterator } from "./queued_iterator";
import { Service, ServiceConfig } from "./service";
export declare const Empty: Uint8Array;
/**
* Events reported by the {@link NatsConnection#status} iterator.
*/
export declare enum Events {
/** Client disconnected */
Disconnect = "disconnect",
/** Client reconnected */
Reconnect = "reconnect",
/** Client received a cluster update */
Update = "update",
/** Client received a signal telling it that the server is transitioning to Lame Duck Mode */
LDM = "ldm",
/** Client received an async error from the server */
Error = "error"
}
export interface Status {
type: Events | DebugEvents;
data: string | ServersChanged | number;
permissionContext?: {
operation: string;
subject: string;
};
}
/**
* Other events that can be reported by the {@link NatsConnection#status} iterator.
* These can usually be safely ignored, as higher-order functionality of the client
* will handle them.
*/
export declare enum DebugEvents {
Reconnecting = "reconnecting",
PingTimer = "pingTimer",
StaleConnection = "staleConnection"
}
export declare const DEFAULT_PORT = 4222;
export declare const DEFAULT_HOST = "127.0.0.1";
export declare const DEFAULT_RECONNECT_TIME_WAIT: number;
export declare const DEFAULT_MAX_RECONNECT_ATTEMPTS = 10;
export declare const DEFAULT_JITTER = 100;
export declare const DEFAULT_JITTER_TLS = 1000;
export declare const DEFAULT_PING_INTERVAL: number;
export declare const DEFAULT_MAX_PING_OUT = 2;
export interface ConnectFn {
(opts: ConnectionOptions): Promise<NatsConnection>;
}
export interface NatsConnection {
/**
* ServerInfo to the currently connected server or undefined
*/
info?: ServerInfo;
/**
* Returns a promise that can be used to monitor if the client closes.
* The promise can resolve an Error if the reason for the close was
* an error. Note that the promise doesn't reject, but rather resolves
* to the error if there was one.
*/
closed(): Promise<void | Error>;
/**
* Close will close the connection to the server. This call will terminate
* all pending requests and subscriptions. The returned promise resolves when
* the connection closes.
*/
close(): Promise<void>;
/**
* Publishes the specified data to the specified subject.
* @param subject
* @param data
* @param options
*/
publish(subject: string, data?: Uint8Array, options?: PublishOptions): void;
/**
* Subscribe expresses interest in the specified subject. The subject may
* have wildcards. Messages are delivered to the {@link SubOpts#callback |SubscriptionOptions callback}
* if specified. Otherwise, the subscription is an async iterator for {@link Msg}.
*
* @param subject
* @param opts
*/
subscribe(subject: string, opts?: SubscriptionOptions): Subscription;
/**
* Publishes a request with specified data in the specified subject expecting a
* response before {@link RequestOptions#timeout} milliseconds. The api returns a
* Promise that resolves when the first response to the request is received. If
* there are no responders (a subscription) listening on the request subject,
* the request will fail as soon as the server processes it.
*
* @param subject
* @param data
* @param opts
*/
request(subject: string, data?: Uint8Array, opts?: RequestOptions): Promise<Msg>;
/**
* Publishes a request expecting multiple responses back. Several strategies
* to determine when the request should stop gathering responses.
* @param subject
* @param data
* @param opts
*/
requestMany(subject: string, data: Uint8Array, opts: Partial<RequestManyOptions>): Promise<QueuedIterator<Msg>>;
/**
* Returns a Promise that resolves when the client receives a reply from
* the server. Use of this API is not necessary by clients.
*/
flush(): Promise<void>;
/**
* Initiates a drain on the connection and returns a promise that resolves when the
* drain completes and the connection closes.
*
* Drain is an ordered shutdown of the client. Instead of abruptly closing the client,
* subscriptions are drained, that is messages not yet processed by a subscription are
* handled before the subscription is closed. After subscriptions are drained it is not
* possible to create a new subscription. Then all pending outbound messages are
* sent to the server. Finally, the connection is closed.
*/
drain(): Promise<void>;
/**
* Returns true if the client is closed.
*/
isClosed(): boolean;
/**
* Returns true if the client is draining.
*/
isDraining(): boolean;
/**
* Returns the hostport of the server the client is connected to.
*/
getServer(): string;
/**
* Returns an async iterator of {@link Status} that may be
* useful in understanding when the client looses a connection, or
* reconnects, or receives an update from the cluster among other things.
*
* @return an AsyncIterable<Status>
*/
status(): AsyncIterable<Status>;
/**
* Returns some metrics such as the number of messages and bytes
* sent and recieved by the client.
*/
stats(): Stats;
/**
* Returns a Promise to a {@link JetStreamManager} which allows the client
* to access Streams and Consumers information.
*
* @param opts
*/
jetstreamManager(opts?: JetStreamOptions): Promise<JetStreamManager>;
/**
* Returns a {@link JetStreamClient} which allows publishing messages to
* JetStream or consuming messages from streams.
*
* @param opts
*/
jetstream(opts?: JetStreamOptions): JetStreamClient;
/**
* @return the number of milliseconds it took for a {@link flush}.
*/
rtt(): Promise<number>;
services: ServicesAPI;
}
export interface ConnectionOptions {
/**
* When the server requires authentication, set an {@link Authenticator}.
* An authenticator is created automatically for username/password and token
* authentication configurations
* if {@link user} and {@link pass} or the {@link token} options are set.
*/
authenticator?: Authenticator;
/**
* When set to `true` the client will print protocol messages that it receives
* or sends to the server.
*/
debug?: boolean;
/**
* Sets the maximum count of ping commands that can be awaiting a response
* before rasing a stale connection status {@link DebugEvents#StaleConnection }
* notification {@link NatsConnection#status} and initiating a reconnect.
*
* @see pingInterval
*/
maxPingOut?: number;
/**
* Sets the maximum count of per-server reconnect attempts before giving up.
* Set to `-1` to never give up.
*
* @default 10
*/
maxReconnectAttempts?: number;
/**
* Sets the client name. When set, the server monitoring pages will display
* this name when referring to this client.
*/
name?: string;
/**
* When set to true, messages published by this client will not match
* this client's subscriptions, so the client is guaranteed to never
* receive self-published messages on a subject that it is listening on.
*/
noEcho?: boolean;
/**
* If set to true, the client will not randomize its server connection list.
*/
noRandomize?: boolean;
/**
* Sets the password for a client connection. Requires that the {@link user}
* option be set. See {@link authenticator}.
*/
pass?: string;
/**
* When set to true, the server may perform additional checks on protocol
* message requests. This option is only useful for NATS client development
* and shouldn't be used, as it affects server performance.
*/
pedantic?: boolean;
/**
* Sets the number of milliseconds between client initiated ping commands.
* See {@link maxPingOut}.
* @default 2 minutes.
*/
pingInterval?: number;
/**
* Sets the port number on the localhost (127.0.0.1) where the nats-server is running.
* This option is mutually exclusive with {@link servers}.
*/
port?: number;
/**
* When set to true, the server will attempt to reconnect so long as
* {@link maxReconnectAttempts} doesn't prevent it.
* @default true
*/
reconnect?: boolean;
/**
* Set a function that dynamically determines the number of milliseconds
* that the client should wait for the next reconnect attempt.
*/
reconnectDelayHandler?: () => number;
/**
* Set the upper bound for a random delay in milliseconds added to
* {@link reconnectTimeWait}.
*
* @default 100 millis
*/
reconnectJitter?: number;
/**
* Set the upper bound for a random delay in milliseconds added to
* {@link reconnectTimeWait}. This only affects TLS connections
*
* @default 1000 millis
*/
reconnectJitterTLS?: number;
/**
* Set the number of millisecods between reconnect attempts.
*
* @default 2000 millis
*/
reconnectTimeWait?: number;
/**
* Set the hostport(s) where the client should attempt to connect.
* This option is mutually exclusive with {@link port}.
*
* @default 127.0.0.1:4222
*/
servers?: Array<string> | string;
/**
* Sets the number of milliseconds the client should wait for a server
* handshake to be established.
*
* @default 20000 millis
*/
timeout?: number;
/**
* When set (can be an empty object), the client requires a secure connection.
* TlsOptions honored depend on the runtime. Consult the specific NATS javascript
* client GitHub repo/documentation. When set to null, the client should fail
* should not connect using TLS. In the case where TLS is available on the server
* a standard connection will be used. If TLS is required, the connection will fail.
*/
tls?: TlsOptions | null;
/**
* Set to a client authentication token. Note that these tokens are
* a specific authentication strategy on the nats-server. This option
* is mutually exclusive of {@link user} and {@link pass}. See {@link authenticator}.
*/
token?: string;
/**
* Sets the username for a client connection. Requires that the {@link pass}
* option be set. See {@link authenticator}.
*/
user?: string;
/**
* When set to true, the server will send response to all server commands.
* This option is only useful for NATS client development and shouldn't
* be used, as it affects server performance.
*/
verbose?: boolean;
/**
* When set to true {@link maxReconnectAttempts} will not trigger until the client
* has established one connection.
*/
waitOnFirstConnect?: boolean;
/**
* When set to true, cluster information gossiped by the nats-server will
* not augment the lists of server(s) known by the client.
*/
ignoreClusterUpdates?: boolean;
/**
* A string prefix (must be a valid subject prefix) prepended to inboxes
* generated by client. This allows a client with limited subject permissions
* to specify a subject where requests can deliver responses.
*/
inboxPrefix?: string;
/**
* By default, NATS clients will abort reconnect if they fail authentication
* twice in a row with the same error, regardless of the reconnect policy.
* This option should be used with care as it will disable this behaviour when true
*/
ignoreAuthErrorAbort?: boolean;
}
/**
* TlsOptions that can be specified to a client. Note that
* the options are typically runtime specific, so some clients won't support
* them at all. In other cases they will match to the runtime's TLS options.
*
* If no options are specified, but the argument for TlsOptions is an object,
* the client is requesting to only use connections that are secured by TLS.
*/
export interface TlsOptions {
certFile?: string;
cert?: string;
caFile?: string;
ca?: string;
keyFile?: string;
key?: string;
}
/**
* Represents a message delivered by NATS. This interface is used by
* Subscribers.
*/
export interface Msg {
/**
* The subject the message was sent to
*/
subject: string;
/**
* The subscription ID where the message was dispatched.
*/
sid: number;
/**
* A possible subject where the recipient may publish a reply (in the cases
* where the message represents a request).
*/
reply?: string;
/**
* The message's data (or payload)
*/
data: Uint8Array;
/**
* Possible headers that may have been set by the server or the publisher.
*/
headers?: MsgHdrs;
/**
* Convenience to publish a response to the {@link reply} subject in the
* message - this is the same as doing `nc.publish(msg.reply, ...)`.
* @param data
* @param opts
*/
respond(data?: Uint8Array, opts?: PublishOptions): boolean;
}
/**
* Subscription Options
*/
export interface SubOpts<T> {
/**
* Optional queue name (subscriptions on the same subject that use queues
* are horizontally load balanced when part of the same queue).
*/
queue?: string;
/**
* Optional maximum number of messages to deliver to the subscription
* before it is auto-unsubscribed.
*/
max?: number;
/**
* Optional maximum number of milliseconds before a timer raises an error. This
* useful to monitor a subscription that is expected to yield messages.
* The timer is cancelled when the first message is received by the subscription.
*/
timeout?: number;
/**
* An optional function that will handle messages. Typically, messages
* are processed via an async iterator on the subscription. If this
* option is provided, messages are processed by the specified function.
* @param err
* @param msg
*/
callback?: (err: NatsError | null, msg: T) => void;
}
/**
* Subscription Options
*/
export type SubscriptionOptions = SubOpts<Msg>;
export interface Base {
subject: string;
callback: (error: NatsError | null, msg: Msg) => void;
received: number;
timeout?: number | null;
max?: number | undefined;
draining: boolean;
}
/**
* ServerInfo represents information from the connected server
*/
export interface ServerInfo {
/**
* True if the server requires authentication
*/
"auth_required"?: boolean;
/**
* Server-assigned client_id
*/
"client_id": number;
/**
* The client's IP as seen by the server
*/
"client_ip"?: string;
/**
* The name or ID of the cluster
*/
cluster?: string;
/**
* Other servers available on the connected cluster
*/
"connect_urls"?: string[];
/**
* Git commit information on the built server binary
*/
"git_commit"?: string;
/**
* Version information on the Go stack used to build the server binary
*/
go: string;
/**
* True if the server supports headers
*/
headers?: boolean;
/**
* Hostname of the connected server
*/
host: string;
/**
* True if the server supports JetStream
*/
jetstream?: boolean;
/**
* True if the server is in Lame Duck Mode
*/
ldm?: boolean;
/**
* Max number of bytes in message that can be sent to the server
*/
"max_payload": number;
/**
* If the server required nkey or JWT authentication the nonce used during authentication.
*/
nonce?: string;
/**
* The port where the server is listening
*/
port: number;
/**
* Version number of the NATS protocol
*/
proto: number;
/**
* The ID of the server
*/
"server_id": string;
/**
* The name of the server
*/
"server_name": string;
/**
* True if TLS is available
*/
"tls_available"?: boolean;
/**
* True if TLS connections are required
*/
"tls_required"?: boolean;
/**
* True if TLS client certificate verification is required
*/
"tls_verify"?: boolean;
/**
* The nats-server version
*/
version: string;
}
export interface Server {
hostname: string;
port: number;
listen: string;
src: string;
tlsName: string;
resolve(opts: Partial<{
fn: DnsResolveFn;
randomize: boolean;
debug?: boolean;
}>): Promise<Server[]>;
}
/**
* ServerChanged records servers in the cluster that were added or deleted.
*/
export interface ServersChanged {
/** list of added servers */
readonly added: string[];
/** list of deleted servers */
readonly deleted: string[];
}
/**
* Basic interface to a Subscription type
*/
export interface Sub<T> extends AsyncIterable<T> {
/** A promise that resolves when the subscription closes */
closed: Promise<void>;
/**
* Stop the subscription from receiving messages. You can optionally
* specify that the subscription should stop after the specified number
* of messages have been received. Note this count is since the lifetime
* of the subscription.
* @param max
*/
unsubscribe(max?: number): void;
/**
* Drain the subscription, closing it after processing all messages
* currently in flight for the client. Returns a promise that resolves
* when the subscription finished draining.
*/
drain(): Promise<void>;
/**
* Returns true if the subscription is draining.
*/
isDraining(): boolean;
/**
* Returns true if the subscription is closed.
*/
isClosed(): boolean;
/**
* @ignore
*/
callback(err: NatsError | null, msg: Msg): void;
/**
* Returns the subject that was used to create the subscription.
*/
getSubject(): string;
/**
* Returns the number of messages received by the subscription.
*/
getReceived(): number;
/**
* Returns the number of messages that have been processed by the subscription.
*/
getProcessed(): number;
/**
* Returns the number of messages that are pending processing. Note that this
* is method is only valid for iterators.
*/
getPending(): number;
/** @ignore */
getID(): number;
/**
* Return the max number of messages before the subscription will unsubscribe.
*/
getMax(): number | undefined;
}
/**
* Type alias for NATS core subscriptions
*/
export type Subscription = Sub<Msg>;
export interface RequestOptions {
/**
* number of milliseconds before the request will timeout.
*/
timeout: number;
/**
* MsgHdrs to include with the request.
*/
headers?: MsgHdrs;
/**
* If true, the request API will create a regular NATS subscription
* to process the response. Otherwise a shared muxed subscriptions is
* used. Requires {@link reply}
*/
noMux?: boolean;
/**
* The subject where the response should be sent to. Requires {@link noMux}
*/
reply?: string;
}
export declare enum RequestStrategy {
Timer = "timer",
Count = "count",
JitterTimer = "jitterTimer",
SentinelMsg = "sentinelMsg"
}
export interface RequestManyOptions {
strategy: RequestStrategy;
maxWait: number;
headers?: MsgHdrs;
maxMessages?: number;
noMux?: boolean;
jitter?: number;
}
export interface PublishOptions {
/**
* An optional subject where a response should be sent.
* Note you must have a subscription listening on this subject
* to receive the response.
*/
reply?: string;
/**
* Optional headers to include with the message.
*/
headers?: MsgHdrs;
}
export interface Stats {
/**
* Number of bytes received by the client.
*/
inBytes: number;
/**
* Number of bytes sent by the client.
*/
outBytes: number;
/**
* Number of messages received by the client.
*/
inMsgs: number;
/**
* Number of messages sent by the client.
*/
outMsgs: number;
}
export interface URLParseFn {
(u: string): string;
}
export interface DnsResolveFn {
(h: string): Promise<string[]>;
}
/**
* Options to a JetStream options applied to all JetStream or JetStreamManager requests.
*/
export interface JetStreamOptions {
/**
* Prefix required to interact with JetStream. Must match
* server configuration.
*/
apiPrefix?: string;
/**
* Number of milliseconds to wait for a JetStream API request.
* @default ConnectionOptions.timeout
* @see ConnectionOptions.timeout
*/
timeout?: number;
/**
* Name of the JetStream domain. This value automatically modifies
* the default JetStream apiPrefix.
*/
domain?: string;
}
/**
* The API for interacting with JetStream resources
*/
export interface JetStreamManager {
/**
* JetStream API to interact with Consumers
*/
consumers: ConsumerAPI;
/**
* JetStream API to interact with Streams
*/
streams: StreamAPI;
/**
* Returns JetStreamAccountStats for the current client account.
*/
getAccountInfo(): Promise<JetStreamAccountStats>;
/**
* Returns an async iteartor
*/
advisories(): AsyncIterable<Advisory>;
}
/**
* Options for a JetStream pull subscription which define how long
* the pull request will remain open and limits the amount of data
* that the server could return.
*/
export interface PullOptions {
/**
* Max number of messages to retrieve in a pull.
*/
batch: number;
/**
* If true, the request for messages will end when received by the server
*/
"no_wait": boolean;
/**
* If set, the number of milliseconds to wait for the number of messages
* specified in {@link batch}
*/
expires: number;
/**
* If set, the max number of bytes to receive. The server will limit the
* number of messages in the batch to fit within this setting.
*/
"max_bytes": number;
"idle_heartbeat": number;
}
/**
* The response returned by the JetStream server when a message is added to a stream.
*/
export interface PubAck {
/**
* The name of the stream
*/
stream: string;
/**
* The domain of the JetStream
*/
domain?: string;
/**
* The sequence number of the message as stored in JetStream
*/
seq: number;
/**
* True if the message is a duplicate
*/
duplicate: boolean;
/**
* Acknowledges the PubAck back to the server
*/
ack(): void;
}
/**
* Options for messages published to JetStream
*/
export interface JetStreamPublishOptions {
/**
* A string identifier used to detect duplicate published messages.
* If the msgID is reused within the streams's `duplicate_window`,
* the message will be rejected by the stream, and the {@link PubAck} will
* mark it as a `duplicate`.
*/
msgID: string;
/**
* The number of milliseconds to wait for the PubAck
*/
timeout: number;
/**
* Headers associated with the message. You can create an instance of
* MsgHdrs with the headers() function.
*/
headers: MsgHdrs;
/**
* Set of constraints that when specified are verified by the server.
* If the constraint(s) doesn't match, the server will reject the message.
* These settings allow you to implement deduplication and consistency
* strategies.
*/
expect: Partial<{
/**
* The expected last msgID of the last message received by the stream.
*/
lastMsgID: string;
/**
* The expected stream capturing the message
*/
streamName: string;
/**
* The expected last sequence on the stream.
*/
lastSequence: number;
/**
* The expected last sequence on the stream for a message with this subject
*/
lastSubjectSequence: number;
}>;
}
/**
* A JetStream interface that allows you to request the ConsumerInfo on the backing object.
*/
export interface ConsumerInfoable {
/** The consumer info for the consumer */
consumerInfo(): Promise<ConsumerInfo>;
}
/**
* An interface that reports via a promise when an object such as a connection
* or subscription closes.
*/
export interface Closed {
/**
* A promise that when resolves, indicates that the object is closed.
*/
closed: Promise<void>;
}
/**
* The JetStream Subscription object
*/
export type JetStreamSubscription = Sub<JsMsg> & Destroyable & Closed & ConsumerInfoable;
export type JetStreamSubscriptionOptions = TypedSubscriptionOptions<JsMsg>;
export interface Pullable {
/**
* Sends a request from the client requesting the server for more messages.
* @param opts
*/
pull(opts?: Partial<PullOptions>): void;
}
export interface Destroyable {
/**
* Destroys a resource on the server. Returns a promise that resolves to true
* whene the operation has been completed
*/
destroy(): Promise<void>;
}
/**
* The JetStream pull subscription object.
*/
export type JetStreamPullSubscription = JetStreamSubscription & Pullable;
/**
* The signature a message handler for a JetStream subscription.
*/
export type JsMsgCallback = (err: NatsError | null, msg: JsMsg | null) => void;
/**
* The interface for creating instances of different JetStream materialized views.
*/
export interface Views {
/**
* Gets or creates a JetStream KV store
* @param name - name for the KV
* @param opts - optional options to configure the KV and stream backing
*/
kv: (name: string, opts?: Partial<KvOptions>) => Promise<KV>;
os: (name: string, opts?: Partial<ObjectStoreOptions>) => Promise<ObjectStore>;
}
/**
* Interface for interacting with JetStream data
*/
export interface JetStreamClient {
/**
* Publishes a message to a stream. If not stream is configured to store the message, the
* request will fail with {@link ErrorCode.NoResponders} error.
*
* @param subj - the subject for the message
* @param data - the message's data
* @param options - the optional message
*/
publish(subj: string, data?: Uint8Array, options?: Partial<JetStreamPublishOptions>): Promise<PubAck>;
/**
* Retrieves a single message from JetStream
* @param stream - the name of the stream
* @param consumer - the consumer's durable name (if durable) or name if ephemeral
* @param expires - the number of milliseconds to wait for a message
*/
pull(stream: string, consumer: string, expires?: number): Promise<JsMsg>;
/**
* Similar to pull, but able to configure the number of messages, etc via PullOptions.
* @param stream - the name of the stream
* @param durable - the consumer's durable name (if durable) or name if ephemeral
* @param opts
*/
fetch(stream: string, durable: string, opts?: Partial<PullOptions>): QueuedIterator<JsMsg>;
/**
* Creates a pull subscription. A pull subscription relies on the client to request more
* messages from the server. If the consumer doesn't exist, it will be created matching
* the consumer options provided.
*
* It is recommended that a consumer be created first using JetStreamManager APIs and then
* use the bind option to simply attach to the created consumer.
*
* If the filter subject is not specified in the options, the filter will be set to match
* the specified subject.
*
* It is more efficient than {@link fetch} or {@link pull} because
* a single subscription is used between invocations.
*
* @param subject - a subject used to locate the stream
* @param opts
*/
pullSubscribe(subject: string, opts: ConsumerOptsBuilder | Partial<ConsumerOpts>): Promise<JetStreamPullSubscription>;
/**
* Creates a push subscription. The JetStream server feeds messages to this subscription
* without the client having to request them. The rate at which messages are provided can
* be tuned by the consumer by specifying {@link ConsumerConfig#rate_limit_bps | ConsumerConfig.rate_limit_bps} and/or
* {@link ConsumerOpts | maxAckPending}.
*
* It is recommended that a consumer be created first using JetStreamManager APIs and then
* use the bind option to simply attach to the created consumer.
*
* If the filter subject is not specified in the options, the filter will be set to match
* the specified subject.
*
* @param subject - a subject used to locate the stream
* @param opts
*/
subscribe(subject: string, opts: ConsumerOptsBuilder | Partial<ConsumerOpts>): Promise<JetStreamSubscription>;
/**
* Accessor for the JetStream materialized views API
*/
views: Views;
/**
* Returns the JS API prefix as processed from the JetStream Options
*/
apiPrefix: string;
}
export interface ConsumerOpts {
/**
* The consumer configuration
*/
config: Partial<ConsumerConfig>;
/**
* Enable manual ack. When set to true, the client is responsible to ack messages.
*/
mack: boolean;
/**
* The name of the stream
*/
stream: string;
/**
* An optional callback to process messages - note that iterators are the preferred
* way of processing messages.
*/
callbackFn?: JsMsgCallback;
/**
* The consumer name
*/
name?: string;
/**
* Only applicable to push consumers. When set to true, the consumer will be an ordered
* consumer.
*/
ordered: boolean;
/**
* Standard option for all subscriptions. Defines the maximum number of messages dispatched
* by the server before stopping the subscription. For JetStream this may not be accurate as
* JetStream can add additional protocol messages that could count towards this limit.
*/
max?: number;
/**
* Only applicable to push consumers, allows the pull subscriber to horizontally load balance.
*/
queue?: string;
/**
* If true, the client will only attempt to bind to the specified consumer name/durable on
* the specified stream. If the consumer is not found, the subscribe will fail
*/
isBind?: boolean;
}
/**
* A builder API that creates a ConsumerOpt
*/
export interface ConsumerOptsBuilder {
/**
* User description of this consumer
*/
description(description: string): this;
/**
* DeliverTo sets the subject where a push consumer receives messages
* @param subject
*/
deliverTo(subject: string): this;
/**
* Sets the durable name, when not set an ephemeral consumer is created
* @param name
*/
durable(name: string): this;
/**
* The consumer will start at the message with the specified sequence
* @param seq
*/
startSequence(seq: number): this;
/**
* consumer will start with messages received on the specified time/date
* @param time
*/
startTime(time: Date): this;
/**
* Consumer will start at first available message on the stream
*/
deliverAll(): this;
/**
* Consumer will deliver all the last per messages per subject
*/
deliverLastPerSubject(): this;
/**
* Consumer will start at the last message
*/
deliverLast(): this;
/**
* Consumer will start with new messages (not yet in the stream)
*/
deliverNew(): this;
/**
* Start delivering at the at a past point in time
* @param millis
*/
startAtTimeDelta(millis: number): this;
/**
* Messages delivered to the consumer will not have a payload. Instead
* they will have the header `Nats-Msg-Size` indicating the number of bytes
* in the message as stored by JetStream.
*/
headersOnly(): this;
/**
* Consumer will not track ack for messages
*/
ackNone(): this;
/**
* Ack'ing a message implicitly acks all messages with a lower sequence
*/
ackAll(): this;
/**
* Consumer will ack all messages - not that unless {@link manualAck} is set
* the client will auto ack messages after processing via its callback or when
* the iterator continues processing.
*/
ackExplicit(): this;
/**
* Sets the time a delivered message might remain unacknowledged before a redelivery is attempted
* @param millis
*/
ackWait(millis: number): this;
/**
* Max number of re-delivery attempts for a particular message
* @param max
*/
maxDeliver(max: number): this;
/**
* Consumer should filter the messages to those that match the specified filter.
* @param s
*/
filterSubject(s: string): this;
/**
* Replay messages as fast as possible
*/
replayInstantly(): this;
/**
* Replay at the rate received
*/
replayOriginal(): this;
/**
* Sample a subset of messages expressed as a percentage(0-100)
* @param n
*/
sample(n: number): this;
/**
* Limit message delivery to the specified rate in bits per second.
* @param bps
*/
limit(bps: number): this;
/**
* Pull subscriber option only. Limits the maximum outstanding messages scheduled
* via batch pulls as pulls are additive.
* @param max
*/
maxWaiting(max: number): this;
/**
* Max number of outstanding acks before the server stops sending new messages
* @param max
*/
maxAckPending(max: number): this;
/**
* Push consumer only option - Enables idle heartbeats from the server. If the number of
* specified millis is reached and no messages are available on the server, the server will
* send a heartbeat (status code 100 message) indicating that the JetStream consumer is alive.
* @param millis
*/
idleHeartbeat(millis: number): this;
/**
* Push consumer flow control - the server sends a status code 100 and uses the delay on the
* response to throttle inbound messages for a client and prevent slow consumer.
*/
flowControl(): this;
/**
* Push consumer only option - Sets the name of the queue group - same as queue
* @param name
*/
deliverGroup(name: string): this;
/**
* Prevents the consumer implementation from auto-acking messages. Message callbacks
* and iterators must explicitly ack messages.
*/
manualAck(): this;
/**
* Standard NATS subscription option which automatically closes the subscription after the specified
* number of messages (actual stream or flow control) are seen by the client.
* @param max
*/
maxMessages(max: number): this;
/**
* Push consumer only option - Standard NATS queue group option, same as {@link deliverGroup}
* @param n
*/
queue(n: string): this;
/**
* Use a callback to process messages. If not specified, you process messages by iterating
* on the returned subscription object.
* @param fn
*/
callback(fn: JsMsgCallback): this;
/**
* Push consumer only - creates an ordered consumer - ordered consumers cannot be a pull consumer
* nor specify durable, deliverTo, specify an ack policy, maxDeliver, or flow control.
*/
orderedConsumer(): this;
/**
* Bind to the specified durable (or consumer name if ephemeral) on the specified stream.
* If the consumer doesn't exist, the subscribe will fail. Bind the recommended way
* of subscribing to a stream, as it requires the consumer to exist already.
* @param stream
* @param durable
*/
bind(stream: string, durable: string): this;
/**
* Specify the name of the stream, avoiding a lookup where the stream is located by
* searching for a subject.
* @param stream
*/
bindStream(stream: string): this;
/**
* Pull consumer only - Sets the max number of messages that can be pulled in a batch
* that can be requested by a client during a pull.
* @param n
*/
maxPullBatch(n: number): this;
/**
* Pull consumer only - Sets the max amount of time before a pull request expires that
* may be requested by a client during a pull.
* @param millis
*/
maxPullRequestExpires(millis: number): this;
/**
* Pull consumer only - Sets the max amount of time that an ephemeral consumer will be
* allowed to live on the server. If the client doesn't perform any requests during the
* specified interval the server will discard the consumer.
* @param millis
*/
inactiveEphemeralThreshold(millis: number): this;
/**
* Force the consumer state to be kept in memory rather than inherit the setting from
* the Stream
*/
memory(): this;
/**
* When set do not inherit the replica count from the stream but specifically set it to this amount
*/
numReplicas(n: number): this;
}
/**
* An interface for listing. Returns a promise with typed list.
*/
export interface Lister<T> {
[Symbol.asyncIterator](): AsyncIterator<T>;
next(): Promise<T[]>;
}
export interface ConsumerAPI {
/**
* Returns the ConsumerInfo for the specified consumer in the specified stream.
* @param stream
* @param consumer
*/
info(stream: string, consumer: string): Promise<ConsumerInfo>;
/**
* Adds a new consumer to the specified stream with the specified consumer options.
* @param stream
* @param cfg
*/
add(stream: string, cfg: Partial<ConsumerConfig>): Promise<ConsumerInfo>;
/**
* Updates the consumer configuration for the specified consumer on the specified
* stream that has the specified durable name.
* @param stream
* @param durable
* @param cfg
*/
update(stream: string, durable: string, cfg: Partial<ConsumerUpdateConfig>): Promise<ConsumerInfo>;
/**
* Deletes the specified consumer name/durable from the specified stream.
* @param stream
* @param consumer
*/
delete(stream: string, consumer: string): Promise<boolean>;
/**
* Lists all the consumers on the specfied streams
* @param stream
*/
list(stream: string): Lister<ConsumerInfo>;
}
/**
* Options for StreamAPI info requests
*/
export type StreamInfoRequestOptions = {
/**
* Include info on deleted subjects.
*/
"deleted_details": boolean;
/**
* Only include information matching the specified subject filter
*/
"subjects_filter": string;
} & ApiPagedRequest;
export interface StreamAPI {
/**
* Returns the information about the specified stream
* @param stream
* @param opts
*/
info(stream: string, opts?: Partial<StreamInfoRequestOptions>): Promise<StreamInfo>;
/**
* Adds a new stream with the specified stream configuration.
* @param cfg
*/
add(cfg: Partial<StreamConfig>): Promise<StreamInfo>;
/**
* Updates the stream configuration for the specified stream.
* @param name
* @param cfg
*/
update(name: string, cfg: Partial<StreamUpdateConfig>): Promise<StreamInfo>;
/**
* Purges messages from a stream that match the specified purge options.
* @param stream
* @param opts
*/
purge(stream: string, opts?: PurgeOpts): Promise<PurgeResponse>;
/**
* Deletes the specified stream
* @param stream
*/
delete(stream: string): Promise<boolean>;
/**
* Lists all streams stored by JetStream
* @param subject - only return streams that include the specified subject
*/
list(subject?: string): Lister<StreamInfo>;
/**
* Deletes the specified message sequence from the stream
* @param stream
* @param seq
* @param erase - erase the message - by default true
*/
deleteMessage(stream: string, seq: number, erase?: boolean): Promise<boolean>;
/**
* Retrieves the message matching the specified query. Messages can be
* retrieved by sequence number or by last sequence matching a subject.
* @param stream
* @param query
*/
getMessage(stream: string, query: MsgRequest): Promise<StoredMsg>;
/**
* Find the stream that stores the specified subject.
* @param subject
*/
find(subject: string): Promise<string>;
/**
* Returns a list of KvStatus for all streams that are identified as
* being a KV (that is having names that have the prefix `KV_`)
*/
listKvs(): Lister<KvStatus>;
/**
* Returns a list of ObjectStoreInfo for all streams that are identified as
* being a ObjectStore (that is having names that have the prefix `OBJ_`)
*/
listObjectStores(): Lister<ObjectStoreStatus>;
/**
* Return a Lister of stream names
* @param subject - if specified, the results are filtered to streams that contain the
* subject (can be wildcarded)
*/
names(subject?: string): Lister<string>;
}
/**
* Request the next stream message by sequence for the specified subject.
*/
export type NextMsgRequest = {
/**
* The seq to start looking. If the message under the specified sequence
* matches, it will be returned.
*/
seq: number;
/**
* The subject to look for
*/
next_by_subj: string;
};
export type DirectMsgRequest = SeqMsgRequest | LastForMsgRequest | NextMsgRequest;
/**
* The Direct stream API is a bit more performant for retrieving messages,
* but requires the stream to have enabled direct access.
* See {@link StreamConfig.allow_direct}.
*/
export interface DirectStreamAPI {
/**
* Retrieves the message matching the specified query. Messages can be
* retrieved by sequence number or by last sequence matching a subject, or
* by looking for the next message sequence that matches a subject.
* @param stream
* @param query
*/
getMessage(stream: string, query: DirectMsgRequest): Promise<StoredMsg>;
}
/**
* Represents a message stored in JetStream
*/
export interface JsMsg {
/**
* True if the message was redelivered
*/
redelivered: boolean;
/**
* The delivery info for the message
*/
info: DeliveryInfo;
/**
* The sequence number for the message
*/
seq: number;
/**
* Any headers associated with the message
*/
headers: MsgHdrs | undefined;
/**
* The message's data
*/
data: Uint8Array;
/**
* The subject on which the message was published
*/
subject: string;
/**
* @ignore
*/
sid: number;
/**
* Indicate to the JetStream server that the message was processed
* successfully.
*/
ack(): void;
/**
* Indicate to the JetStream server that processing of the message
* failed, and that it should be resent after the spefied number of
* milliseconds.
* @param millis
*/
nak(millis?: number): void;
/**
* Indicate to the JetStream server that processing of the message
* is on going, and that the ack wait timer for the message should be
* reset preventing a redelivery.
*/
working(): void;
/**
* !! this is an experimental feature - and could be removed
*
* next() combines ack() and pull(), requires the subject for a
* subscription processing to process a message is provided
* (can be the same) however, because the ability to specify
* how long to keep the request open can be specified, this
* functionality doesn't work well with iterators, as an error
* (408s) are expected and needed to re-trigger a pull in case
* there was a timeout. In an iterator, the error will close
* the iterator, requiring a subscription to be reset.
*/
next(subj: string, ro?: Partial<PullOptions>): void;
/**
* Indicate to the JetStream server that processing of the message
* failed and that the message should not be sent to the consumer again.
*/
term(): void;
/**
* Indicate to the JetStream server that the message was processed
* successfully and that the JetStream server should acknowledge back
* that the acknowledgement was received.
*/
ackAck(): Promise<boolean>;
}
export interface DeliveryInfo {
/**
* JetStream domain of the message if applicable.
*/
domain: string;
/**
* The hash of the sending account if applicable.
*/
"account_hash"?: string;
/**
* The stream where the message came from
*/
stream: string;
/**
* The intended consumer for the message.
*/
consumer: string;
/**
* The number of times the message has been redelivered.
*/
redeliveryCount: number;
/**
* The sequence number of the message in the stream
*/
streamSequence: number;
/**
* The client delivery sequence for the message
*/
deliverySequence: number;
/**
* The timestamp for the message in nanoseconds. Convert with `millis(n)`,
*/
timestampNanos: number;
/**
* The number of pending messages for the consumer at the time the
* message was delivered.
*/
pending: number;
/**
* True if the message has been redelivered.
*/
redelivered: boolean;
}
/**
* An interface representing a message that retrieved directly from JetStream.
*/
export interface StoredMsg {
/**
* The subject the message was originally received on
*/
subject: string;
/**
* The sequence number of the message in the Stream
*/
seq: number;
/**
* Headers for the message
*/
header: MsgHdrs;
/**
* The payload of the message body
*/
data: Uint8Array;
/**
* The time the message was received
*/
time: Date;
}
export interface DirectMsg extends StoredMsg {
/**
* The name of the Stream storing message
*/
stream: string;
}
/**
* An advisory is an interesting event in the JetStream server
*/
export interface Advisory {
/**
* The type of the advisory
*/
kind: AdvisoryKind;
/**
* Payload associated with the advisory
*/
data: unknown;
}
/**
* The different kinds of Advisories
*/
export declare enum AdvisoryKind {
API = "api_audit",
StreamAction = "stream_action",
ConsumerAction = "consumer_action",
SnapshotCreate = "snapshot_create",
SnapshotComplete = "snapshot_complete",
RestoreCreate = "restore_create",
RestoreComplete = "restore_complete",
MaxDeliver = "max_deliver",
Terminated = "terminated",
Ack = "consumer_ack",
StreamLeaderElected = "stream_leader_elected",
StreamQuorumLost = "stream_quorum_lost",
ConsumerLeaderElected = "consumer_leader_elected",
ConsumerQuorumLost = "consumer_quorum_lost"
}
/**
* Value expressed as Nanoseconds - use the `nanos(millis)` function
* to convert millis to nanoseconds. Note that in some environments this
* could overflow.
*/
export type Nanos = number;
export interface ApiError {
/**
* HTTP like error code in the 300 to 500 range
*/
code: number;
/**
* A human friendly description of the error
*/
description: string;
/**
* The NATS error code unique to each kind of error
*/
err_code?: number;
}
export interface ApiResponse {
type: string;
error?: ApiError;
}
export interface ApiPaged {
total: number;
offset: number;
limit: number;
}
export interface ApiPagedRequest {
offset: number;
}
/**
* An alternate location to read mirrored data
*/
export interface StreamAlternate {
/**
* The mirror Stream name
*/
name: string;
/**
* The name of the cluster holding the Stream
*/
cluster: string;
/**
* The domain holding the Stream
*/
domain: string;
}
/**
* Stream configuration info
*/
export interface StreamInfo extends ApiPaged {
/**
* The active configuration for the Stream
*/
config: StreamConfig;
/**
* Timestamp when the stream was created
*/
created: Nanos;
/**
* Detail about the current State of the Stream
*/
state: StreamState;
/**
* Cluster information for the stream if applicable
*/
cluster?: ClusterInfo;
/**
* Information about an upstream stream source in a mirror
*/
mirror?: StreamSourceInfo;
/**
* Sources for the Stream if applicable
*/
sources?: StreamSourceInfo[];
/**
* Alternates for a stream if applicable. Alternates are listed
* in order of TTL. With streams at the start of the Array potentially
* closer and faster to access.
*/
alternates?: StreamAlternate[];
}
export interface StreamConfig extends StreamUpdateConfig {
/**
* A unique name for the Stream
*/
name: string;
/**
* How messages are retained in the Stream, once this is exceeded old messages are removed.