Skip to content

PURISTA API


PURISTA API / @purista/dapr-sdk / DaprEventBridge

Class: DaprEventBridge

Defined in: dapr-sdk/src/DaprEventBridge/DaprEventBridge.impl.ts:42

The DaprEventBridge connects to the Dapr sidecar container. It provides endpoints for invoking commands, triggering subscriptions and emitting event messages. The sidecar container invokes commands and subscriptions of the service connected to the event bridge. A DaprClient (http fetch) is used for communication from the service/event bridge to the sidecar container.

Names for services, commands, subscriptions and events are converted to kebab-case. If the event bridge is configured to expose REST endpoints defined in command builder, the endpoints are generated as defined in the command builder.

The event bridge is using Hono under the hood. You need to provide a serve function. Depending on your runtime (Node, Bun, Deno) an adapter might be needed.

See

Hono website

Example

typescript
import { DaprConfigStore, DaprEventBridge, DaprSecretStore, DaprStateStore } from '@purista/dapr-sdk'

const eventBridge = new DaprEventBridge({
   serve,
 })

// start the services first ...

await eventBridge.start()

Extends

Implements

Constructors

Constructor

new DaprEventBridge(config): DaprEventBridge

Defined in: dapr-sdk/src/DaprEventBridge/DaprEventBridge.impl.ts:45

Parameters

config
apiPrefix?

string

the prefix to be used if the command is configured as REST api endpoint according to the OpenAPI definition needs to enableRestApiExpose set to true

Default

ts
/api
clientConfig?

DaprClientConfig

commandPayloadAsCloudEvent?

boolean

command invocations are wrapped in CloudEvent

CloudEvents specification v1.0: https://github.com/cloudevents/spec/tree/v1.0

Default

ts
false
defaultCommandTimeout?

number

Overwrite the hardcoded default timeout of command invocations

enableHttpCompression?

boolean

enable HTTP compression in web server

Default

ts
true
enableRestApiExpose?

boolean

expose commands as regular REST endpoints when they are configured as endpoints

Default

ts
true
instanceId?

string

The instance id of the event bridge. If not set, a id will generated each time a instance is created. Use this if there is a need to always have the same instance id.

logger?

Logger

logLevel?

LogLevelName

metrics?

PuristaMetricsRuntimeOptions

metricsRecorder?

PuristaMetricsRecorderInterface

name?

string

pathPrefix?

string

the prefix to be used for exposing commands as endpoints expecting a event bus message

Default

ts
purista
serve

(options) => Server<typeof IncomingMessage, typeof ServerResponse> | Http2Server<typeof IncomingMessage, typeof ServerResponse, typeof Http2ServerRequest, typeof Http2ServerResponse> | Http2SecureServer<typeof IncomingMessage, typeof ServerResponse, typeof Http2ServerRequest, typeof Http2ServerResponse>

The serve function is depending on the runtime.

  • Bun: Bun.serve
  • Node.js: serve function from additional package @hono/hono-node-server
  • Deno: serve function from package https://deno.land/std/http/server.ts

See

https://hono.dev

serverHost?

string

Host of the server.

Default

ts
127.0.0.1
serverPort?

number

Port of the server.

Default

ts
8080
spanProcessor?

SpanProcessor

subscriptionPayloadAsCloudEvent?

boolean

subscription invocations are wrapped in CloudEvent

CloudEvents specification v1.0: https://github.com/cloudevents/spec/tree/v1.0

Default

ts
false

Returns

DaprEventBridge

Overrides

HttpEventBridge.constructor

Properties

app

app: Hono

Defined in: base-http-bridge/dist/HttpEventBridge/HttpEventBridge.impl.d.ts:25

Inherited from

DaprEventBridge.app


capabilities

capabilities: EventBridgeCapabilities

Defined in: core/dist/core/EventBridge/EventBridgeBaseClass.impl.d.ts:28

Implementation of

EventBridge.capabilities

Inherited from

HttpEventBridge.capabilities


client

client: HttpEventBridgeClient

Defined in: base-http-bridge/dist/HttpEventBridge/HttpEventBridge.impl.d.ts:28

Inherited from

HttpEventBridge.client


config

config: Complete<EventBridgeConfig<ConfigType>>

Defined in: core/dist/core/EventBridge/EventBridgeBaseClass.impl.d.ts:26

Inherited from

HttpEventBridge.config


defaultCommandTimeout

defaultCommandTimeout: number

Defined in: core/dist/core/EventBridge/EventBridgeBaseClass.impl.d.ts:30

The default time until when a command invocation automatically returns a time out error

Implementation of

EventBridge.defaultCommandTimeout

Inherited from

HttpEventBridge.defaultCommandTimeout


inFlightExecutions

protected readonly inFlightExecutions: InFlightExecutionTracker

Defined in: core/dist/core/EventBridge/EventBridgeBaseClass.impl.d.ts:31

Inherited from

HttpEventBridge.inFlightExecutions


instanceId

instanceId: string

Defined in: core/dist/core/EventBridge/EventBridgeBaseClass.impl.d.ts:29

Implementation of

EventBridge.instanceId

Inherited from

HttpEventBridge.instanceId


isShuttingDown

isShuttingDown: boolean

Defined in: base-http-bridge/dist/HttpEventBridge/HttpEventBridge.impl.d.ts:26

Inherited from

HttpEventBridge.isShuttingDown


isStarted

isStarted: boolean

Defined in: base-http-bridge/dist/HttpEventBridge/HttpEventBridge.impl.d.ts:27

Inherited from

HttpEventBridge.isStarted


logger

logger: Logger

Defined in: core/dist/core/EventBridge/EventBridgeBaseClass.impl.d.ts:23

Inherited from

HttpEventBridge.logger


metricsRecorder

protected metricsRecorder: PuristaMetricsRecorderInterface

Defined in: core/dist/core/EventBridge/EventBridgeBaseClass.impl.d.ts:25

Inherited from

HttpEventBridge.metricsRecorder


name

name: string

Defined in: core/dist/core/EventBridge/EventBridgeBaseClass.impl.d.ts:27

Implementation of

EventBridge.name

Inherited from

HttpEventBridge.name


server

server: Server<typeof IncomingMessage, typeof ServerResponse> | Http2Server<typeof IncomingMessage, typeof ServerResponse, typeof Http2ServerRequest, typeof Http2ServerResponse> | Http2SecureServer<typeof IncomingMessage, typeof ServerResponse, typeof Http2ServerRequest, typeof Http2ServerResponse> | undefined

Defined in: base-http-bridge/dist/HttpEventBridge/HttpEventBridge.impl.d.ts:24

Inherited from

HttpEventBridge.server


traceProvider

traceProvider: NodeTracerProvider

Defined in: core/dist/core/EventBridge/EventBridgeBaseClass.impl.d.ts:24

Inherited from

HttpEventBridge.traceProvider

Methods

destroy()

destroy(): Promise<void>

Defined in: base-http-bridge/dist/HttpEventBridge/HttpEventBridge.impl.d.ts:42

Shut down event bridge as gracefully as possible

Returns

Promise<void>

Implementation of

EventBridge.destroy

Inherited from

HttpEventBridge.destroy


emitMessage()

emitMessage<T>(message): Promise<Readonly<EBMessage>>

Defined in: base-http-bridge/dist/HttpEventBridge/HttpEventBridge.impl.d.ts:31

Emit a message to the eventbridge without awaiting a result

Type Parameters

T

T extends EBMessage

Parameters

message

Omit<EBMessage, "id" | "timestamp" | "correlationId">

the message

Returns

Promise<Readonly<EBMessage>>

Implementation of

EventBridge.emitMessage

Inherited from

HttpEventBridge.emitMessage


getInFlightExecutionCount()

getInFlightExecutionCount(): number

Defined in: core/dist/core/EventBridge/EventBridgeBaseClass.impl.d.ts:69

Number of currently running handlers across all work kinds.

Returns

number

Implementation of

EventBridge.getInFlightExecutionCount

Inherited from

HttpEventBridge.getInFlightExecutionCount


getInFlightExecutionCounts()

getInFlightExecutionCounts(): InFlightExecutionCounts

Defined in: core/dist/core/EventBridge/EventBridgeBaseClass.impl.d.ts:70

Number of currently running handlers grouped by work kind.

Returns

InFlightExecutionCounts

Implementation of

EventBridge.getInFlightExecutionCounts

Inherited from

HttpEventBridge.getInFlightExecutionCounts


getPausedSubscriptionConsumers()

getPausedSubscriptionConsumers(): PausedSubscriptionConsumersByRegistrationKey

Defined in: core/dist/core/EventBridge/EventBridgeBaseClass.impl.d.ts:71

Returns paused subscription consumer states keyed by adapter registration key.

Returns

PausedSubscriptionConsumersByRegistrationKey

Implementation of

EventBridge.getPausedSubscriptionConsumers

Inherited from

HttpEventBridge.getPausedSubscriptionConsumers


getTracer()

getTracer(): Tracer

Defined in: core/dist/core/EventBridge/EventBridgeBaseClass.impl.d.ts:38

Returns open telemetry tracer of this service

Returns

Tracer

Tracer

Inherited from

HttpEventBridge.getTracer


invoke()

invoke<T>(input, ttl?): Promise<T>

Defined in: base-http-bridge/dist/HttpEventBridge/HttpEventBridge.impl.d.ts:32

Call a command of a service and return the result of this command

Type Parameters

T

T

Parameters

input

Omit<Command, "id" | "messageType" | "timestamp" | "correlationId">

a partial command message

ttl?

number

the time to live (timeout) of the invocation

Returns

Promise<T>

Implementation of

EventBridge.invoke

Inherited from

HttpEventBridge.invoke


isHealthy()

isHealthy(): Promise<boolean>

Defined in: base-http-bridge/dist/HttpEventBridge/HttpEventBridge.impl.d.ts:38

Indicates if the eventbridge is running and works correctly

Returns

Promise<boolean>

Implementation of

EventBridge.isHealthy

Inherited from

HttpEventBridge.isHealthy


isReady()

isReady(): Promise<boolean>

Defined in: base-http-bridge/dist/HttpEventBridge/HttpEventBridge.impl.d.ts:37

Indicates if the eventbridge has been started and is connected to underlaying message broker

Returns

Promise<boolean>

Implementation of

EventBridge.isReady

Inherited from

HttpEventBridge.isReady


openStream()

openStream<Chunk, Final>(_input, _ttl?): Promise<StreamHandle<Chunk, Final>>

Defined in: core/dist/core/EventBridge/EventBridgeBaseClass.impl.d.ts:73

Open a stream invocation. The returned handle can be consumed via async iteration and can be cancelled by caller.

Type Parameters

Chunk

Chunk = unknown

Final

Final = unknown

Parameters

_input

Omit<StreamOpenRequest, "id" | "messageType" | "timestamp" | "correlationId">

_ttl?

number

Returns

Promise<StreamHandle<Chunk, Final>>

Implementation of

EventBridge.openStream

Inherited from

HttpEventBridge.openStream


registerCommand()

registerCommand(address, cb, metadata, eventBridgeConfig): Promise<string>

Defined in: base-http-bridge/dist/HttpEventBridge/HttpEventBridge.impl.d.ts:33

Parameters

address

EBMessageAddress

the address of the service command (service name, version and command name)

cb

(message) => Promise<Readonly<Omit<{ contentEncoding: string; contentType: string; correlationId: string; eventName?: string; id: string; messageType: CommandSuccessResponse; otp?: string; payload: unknown; principalId?: string; receiver: { instanceId: string; serviceName: string; serviceTarget: string; serviceVersion: string; }; sender: { instanceId: string; serviceName: string; serviceTarget: string; serviceVersion: string; }; tenantId?: string; timestamp: number; traceId?: string; }, "instanceId">> | Readonly<Omit<{ contentEncoding: "utf-8"; contentType: "application/json"; correlationId: string; eventName?: string; id: string; isHandledError: boolean; messageType: CommandErrorResponse; otp?: string; payload: { data?: unknown; message: string; status: StatusCode; }; principalId?: string; receiver: { instanceId: string; serviceName: string; serviceTarget: string; serviceVersion: string; }; sender: { instanceId: string; serviceName: string; serviceTarget: string; serviceVersion: string; }; tenantId?: string; timestamp: number; traceId?: string; }, "instanceId">>>

the function to be called if a matching command arrives

metadata
expose

object & object

eventBridgeConfig

DefinitionEventBridgeConfig

Returns

Promise<string>

Implementation of

EventBridge.registerCommand

Inherited from

HttpEventBridge.registerCommand


registerStream()

registerStream(_address, _cb, _metadata, _eventBridgeConfig): Promise<string>

Defined in: core/dist/core/EventBridge/EventBridgeBaseClass.impl.d.ts:74

Register a service stream.

Parameters

_address

EBMessageAddress

_cb

(message) => Promise<void>

_metadata

StreamDefinitionMetadataBase

_eventBridgeConfig

DefinitionEventBridgeConfig

Returns

Promise<string>

Implementation of

EventBridge.registerStream

Inherited from

HttpEventBridge.registerStream


registerSubscription()

registerSubscription(subscription, cb): Promise<string>

Defined in: dapr-sdk/src/DaprEventBridge/DaprEventBridge.impl.ts:97

Register a new subscription

Parameters

subscription

Subscription

the subscription definition

cb

(message) => Promise<Omit<{ contentEncoding: string; contentType: string; correlationId?: string; eventName: string; id: string; messageType: CustomMessage; otp?: string; payload?: unknown; principalId?: string; receiver?: EBMessageAddress; sender: { instanceId: string; serviceName: string; serviceTarget: string; serviceVersion: string; }; tenantId?: string; timestamp: number; traceId?: string; }, "id" | "timestamp"> | undefined>

the function to be called if a matching message arrives

Returns

Promise<string>

Implementation of

EventBridge.registerSubscription

Overrides

HttpEventBridge.registerSubscription


resumeSubscriptionConsumer()

resumeSubscriptionConsumer(_registrationKey): Promise<void>

Defined in: core/dist/core/EventBridge/EventBridgeBaseClass.impl.d.ts:72

Resumes a paused subscription consumer by registration key.

Parameters

_registrationKey

string

Returns

Promise<void>

Implementation of

EventBridge.resumeSubscriptionConsumer

Inherited from

HttpEventBridge.resumeSubscriptionConsumer


runInFlight()

runInFlight<T>(fn, kind?): Promise<T>

Defined in: core/dist/core/EventBridge/EventBridgeBaseClass.impl.d.ts:67

Type Parameters

T

T

Parameters

fn

() => Promise<T>

kind?

"command" | "subscription" | "stream" | "generic"

Returns

Promise<T>

Inherited from

HttpEventBridge.runInFlight


start()

start(): Promise<void>

Defined in: dapr-sdk/src/DaprEventBridge/DaprEventBridge.impl.ts:80

Start the eventbridge and connect to the underlaying message broker

Returns

Promise<void>

Implementation of

EventBridge.start

Overrides

HttpEventBridge.start


startActiveSpan()

startActiveSpan<F>(name, opts, context, fn): Promise<F>

Defined in: core/dist/core/EventBridge/EventBridgeBaseClass.impl.d.ts:47

Start a child span for opentelemetry tracking

Type Parameters

F

F

Parameters

name

string

name of span

opts

SpanOptions

span options

context

Context | undefined

optional context

fn

(span) => Promise<F>

function to be executed within the span

Returns

Promise<F>

return value of fn

Inherited from

HttpEventBridge.startActiveSpan


unregisterCommand()

unregisterCommand(address): Promise<void>

Defined in: base-http-bridge/dist/HttpEventBridge/HttpEventBridge.impl.d.ts:34

Unregister a service command

Parameters

address

EBMessageAddress

The address (service name, version and command name) of the command to be de-registered

Returns

Promise<void>

Implementation of

EventBridge.unregisterCommand

Inherited from

HttpEventBridge.unregisterCommand


unregisterStream()

unregisterStream(_address): Promise<void>

Defined in: core/dist/core/EventBridge/EventBridgeBaseClass.impl.d.ts:75

Unregister a service stream

Parameters

_address

EBMessageAddress

Returns

Promise<void>

Implementation of

EventBridge.unregisterStream

Inherited from

HttpEventBridge.unregisterStream


unregisterSubscription()

unregisterSubscription(address): Promise<void>

Defined in: base-http-bridge/dist/HttpEventBridge/HttpEventBridge.impl.d.ts:36

Parameters

address

EBMessageAddress

Returns

Promise<void>

Implementation of

EventBridge.unregisterSubscription

Inherited from

HttpEventBridge.unregisterSubscription


waitForInFlightDrain()

waitForInFlightDrain(timeoutMs?): Promise<boolean>

Defined in: core/dist/core/EventBridge/EventBridgeBaseClass.impl.d.ts:68

Parameters

timeoutMs?

number

Returns

Promise<boolean>

Inherited from

HttpEventBridge.waitForInFlightDrain


wrapInSpan()

wrapInSpan<F>(name, opts, fn, context?): Promise<F>

Defined in: core/dist/core/EventBridge/EventBridgeBaseClass.impl.d.ts:64

Start span for opentelemetry tracking on same level. The created span will not become the "active" span within opentelemetry!

This means during logging and similar the spanId of parent span is logged.

Use wrapInSpan for marking points in flow of one bigger function, but not to trace the program flow itself

Type Parameters

F

F

Parameters

name

string

name of span

opts

SpanOptions

span options

fn

(span) => Promise<F>

function te be executed in the span

context?

Context

span context

Returns

Promise<F>

return value of fn

Inherited from

HttpEventBridge.wrapInSpan