import type { PubSubClientLike } from "./options.js"; export interface PublishRequest { type: string; data: T; /** * Overrides the generated event id. Supply one when the id has to survive a republish: a * transactional outbox mints it inside the ingest transaction and stores it on the row, so a * relay that crashes between publishing and marking republishes under the same id and consumer * de-duplication collapses the two into one. Left to generate, that republish is a second event. * * CloudEvents 1.0 requires this to be unique per `source`. Nothing here enforces it, and nothing * could: a reused id is indistinguishable from a redelivery to every consumer downstream. * * Sent verbatim, whitespace and all — only the emptiness check trims. Consumers de-duplicate on * the literal value, so a republish of the same stored id still collapses into one event. */ id?: string; subject?: string; /** Overrides the configured source. Rarely needed. */ source?: string; /** Overrides the key derived from `subject`. */ orderingKey?: string; /** Explicit occurrence time. Defaults to now. */ time?: Date; /** * When the platform learned of the event, if that is not now. A relay publishes what its ingest * transaction committed earlier, so defaulting this to publish time folds the queueing delay away * and leaves ingest lag and relay lag indistinguishable in a per-stage lead-time SLI. */ ingestiontime?: Date; dataschema?: string; extensions?: Record; } export interface PublishOptions { /** Bypasses the topic resolver. */ topic?: string; } export interface EventPublisher { publish(request: PublishRequest, options?: PublishOptions): Promise; /** * Publishes every request, in order. Throws {@link PartialPublishError} if any failed — the * successes are already on the topic by then, so the error names them rather than leaving the * caller to guess. * * Within the batch, a request whose predecessor on the same ordering key failed before anything * reached the SDK is not attempted: it fails with {@link OrderingKeyBlockedError} so a later * message can never overtake one that was never sent. That hold is scoped to the batch — across * calls, re-sending a failed message before the key's later ones is the caller's job (the * README's ordering caveat), which a single outbox relay satisfies by construction. */ publishBatch(requests: Array>, options?: PublishOptions): Promise; } /** A request that made it, identified by its position in the batch. */ export interface PublishSuccess { readonly index: number; readonly messageId: string; } /** A request that did not, identified by its position in the batch. */ export interface PublishFailure { readonly index: number; readonly type: string; /** * Why. Usually whatever the failing stage threw; an {@link OrderingKeyBlockedError} when this * request was never attempted because an earlier failure blocked its ordering key. Narrow with * `instanceof` to tell the two apart — only the first kind has anything to fix. */ readonly cause: unknown; } /** * Raised when part of a batch published and part did not. * * Pub/Sub has no multi-message transaction, so a partly-failed batch leaves the successful messages * published and impossible to unsend. A bare throw would tell the caller nothing about which ones, * making the only safe recovery — retry just the failures — impossible: retrying the whole batch * would duplicate everything that already went out. */ export declare class PartialPublishError extends Error { readonly published: readonly PublishSuccess[]; readonly failures: readonly PublishFailure[]; constructor(published: readonly PublishSuccess[], failures: readonly PublishFailure[]); } /** * Raised for a request that `publishBatch` declined to attempt, because an earlier request in the * same batch failed on the same ordering key before anything reached the SDK. * * A failure the SDK observes suspends the key, and the SDK then fails everything queued behind it — * per-key order holds by itself. A failure *before* the SDK — an unresolvable topic, an empty id, a * throwing `encode`, a datacontenttype that is not one — queues nothing and suspends nothing, so * without this hold the batch's next request on the key would publish straight past the failure: * for a state machine per entity, a `cleared` whose `breached` never went out, and nothing * downstream errors. Held-back requests land in {@link PartialPublishError.failures} with this as * the cause, so a caller that retries failures in index order re-sends the key's stream in order * without special-casing anything. */ export declare class OrderingKeyBlockedError extends Error { readonly orderingKey: string; /** Index and event type of the earlier request whose failure blocked the key. */ readonly blockedBy: { readonly index: number; readonly type: string; }; constructor(orderingKey: string, /** Index and event type of the earlier request whose failure blocked the key. */ blockedBy: { readonly index: number; readonly type: string; }); } /** * What an `encode` function hands back: the bytes, and optionally the media type they are in. * * Bare bytes mean `application/json`, which keeps the built-in `JSON.stringify` path and existing * encoders working unchanged. */ export type EncodedPayload = Buffer | { readonly data: Buffer; readonly datacontenttype: string; }; export interface EventPublisherOptions { /** This service's `ce-source`. */ source: string; client: PubSubClientLike; /** Maps an event type to a topic name. Returning undefined fails the publish. */ topicResolver: (type: string) => string | undefined; /** * Encodes the payload. Without one, bodies are plain JSON — which Pub/Sub rejects on a topic * with a schema attached, since Pub/Sub's JSON encoding is Avro JSON rather than plain JSON. * * Return bare bytes and the event declares `application/json`. Return `{ data, datacontenttype }` * to say what the bytes actually are, which is what protobuf, CBOR, binary Avro or a compressed * body needs — otherwise a standards-aware consumer picks its decoder from a lie. */ encode?: (type: string, data: unknown) => EncodedPayload; /** Derives the ordering key from `subject` and enables ordering on the topic. */ ordering?: boolean; /** Development-only resource scoping — see `WerkenTransportOptions.resourcePrefix`. */ resourcePrefix?: string; allowUnsafeResourcePrefix?: boolean; now?: () => Date; } /** * Publishes CloudEvents to Pub/Sub. * * Deliberately not built on Nest's `ClientProxy`: its request/response semantics are a poor fit for * fire-and-forget events, and its default serializer wraps the payload in a Nest-shaped envelope — * putting a framework detail on a contract consumed company-wide. */ export declare function createEventPublisher(options: EventPublisherOptions): EventPublisher; //# sourceMappingURL=publisher.d.ts.map