UNPKG

@graphql-hive/pubsub

Version:
60 lines (57 loc) 2.85 kB
import { JetStreamClient } from '@nats-io/jetstream'; import { DisposableSymbols } from '@whatwg-node/disposablestack'; import { MaybePromise } from '@whatwg-node/promise-helpers'; import { T as TopicDataMap, P as PubSub, a as PubSubListener } from './pubsub-VtDb_TZC.js'; /** * Envelope yielded by {@link NATSJetStreamPubSub} subscriptions, pairing the published * {@link TopicDataMap data} with an opaque replay `cursor`. */ type JetStreamTopicDataMap<M extends TopicDataMap> = { [Topic in keyof M]: { data: M[Topic]; cursor: string; }; }; /** Subscribe options for {@link NATSJetStreamPubSub}, omit to only receive new messages. */ type JetStreamSubscribeOptions = { /** * Opaque cursor returned by a previous subscription's message, replay resumes right after it. * Pass `undefined` to only receive messages published after the subscription starts. */ cursor: string | undefined; }; interface NATSJetStreamPubSubOptions { /** * Prefix for NATS publish subjects to avoid conflicts. * Intentionally no default because we don't want to accidentally share channels between different services. */ subjectPrefix: string; /** * Name of the JetStream stream to publish and subscribe through. * * The stream is not created nor configured by this pub/sub, it must already exist and be * configured to capture the subjects used by this pub/sub (i.e. `${subjectPrefix}:${topic}`). */ stream: string; } /** * {@link PubSub Hive PubSub} implementation using [NATS JetStream](https://docs.nats.io/nats-concepts/jetstream) * persisted streams. * * Subscribe normally to receive published data like any other {@link PubSub}. Pass subscribe * options to receive the data alongside an opaque `cursor`. Passing that cursor back on a later * subscription resumes delivery right after it, allowing subscribers to recover events missed * while disconnected. */ declare class NATSJetStreamPubSub<M extends TopicDataMap = TopicDataMap> implements PubSub<M, JetStreamSubscribeOptions> { #private; constructor(js: JetStreamClient, options: NATSJetStreamPubSubOptions); subscribedTopics(): Promise<(keyof M)[]>; publish<Topic extends keyof M>(topic: Topic, data: M[Topic]): Promise<undefined>; subscribe<Topic extends keyof M>(topic: Topic): AsyncIterable<M[Topic]>; subscribe<Topic extends keyof M>(topic: Topic, options: JetStreamSubscribeOptions): AsyncIterable<JetStreamTopicDataMap<M>[Topic]>; subscribe<Topic extends keyof M>(topic: Topic, listener: PubSubListener<M, Topic>): MaybePromise<() => MaybePromise<void>>; dispose(): Promise<void>; [DisposableSymbols.asyncDispose](): Promise<void>; } export { type JetStreamSubscribeOptions, type JetStreamTopicDataMap, NATSJetStreamPubSub, type NATSJetStreamPubSubOptions };