UNPKG

@mastra/core

Version:
79 lines • 3.31 kB
import EventEmitter from 'node:events'; import type { IMastraLogger } from '../../logger/index.js'; import { PubSub } from '../pubsub.js'; import type { LeaseProvider, PubSubDeliveryMode } from '../pubsub.js'; import type { Event, EventCallback, SubscribeOptions } from '../types.js'; export interface EventEmitterPubSubOptions { /** * Optional logger for surfacing batched-delivery errors. Falls back to * `console.error` when not provided. */ logger?: IMastraLogger; } /** * The default pubsub: delivers events inside the process that published them. * * Several Mastra instances that share one storage but each use their own * `EventEmitterPubSub` are not supported for evented workflows: every instance * treats the others' runs as its own to recover. A multi-instance evented * deployment needs a broker such as Redis Streams, Valkey Streams or Google * Cloud Pub/Sub. */ export declare class EventEmitterPubSub extends PubSub implements LeaseProvider { get supportedModes(): ReadonlyArray<PubSubDeliveryMode>; /** * `EventEmitterPubSub` is strictly in-process, so the `AckHandleBuffer` * queue it uses for batching shares the same lifetime as everything * else here. Nothing more durable is promised, and nothing less is * needed. */ get supportsNativeBatching(): boolean; /** * Ack and nack are no-ops here: a message lives only in the process that * published it, so nothing redelivers it once that process stops. */ get redeliversUnacked(): boolean; private emitter; private groups; private groupCounters; private groupListeners; private pendingNacks; private deliveryAttempts; private fanoutWrappers; private batchBuffers; private readonly logger?; constructor(existingEmitter?: EventEmitter, options?: EventEmitterPubSubOptions); /** * Debug-hostile silent failures are the default for emitter listeners. * Surface buffer-side errors on a single channel so they're at least visible. */ /** * Subscribers may return a promise now that they can acknowledge deliveries. * This transport is push-only and has nothing to redeliver, so a rejection is * logged rather than becoming an unhandled rejection. */ private logSubscriberError; private logBufferError; publish(topic: string, event: Omit<Event, 'id' | 'createdAt'>, _options?: { localOnly?: boolean; }): Promise<void>; subscribe(topic: string, cb: EventCallback, options?: SubscribeOptions): Promise<void>; unsubscribe(topic: string, cb: EventCallback): Promise<void>; flush(): Promise<void>; /** * Clean up all listeners during graceful shutdown. */ close(): Promise<void>; private subscribeWithGroup; private deliverToGroup; private leases; acquireLease(key: string, owner: string, ttlMs: number): Promise<{ acquired: boolean; owner?: string; }>; getLeaseOwner(key: string): Promise<string | undefined>; releaseLease(key: string, owner: string): Promise<void>; renewLease(key: string, owner: string, ttlMs: number): Promise<boolean>; transferLease(key: string, fromOwner: string, toOwner: string, ttlMs: number): Promise<boolean>; } //# sourceMappingURL=index.d.ts.map