PURISTA API / Modules / @purista/dapr-sdk / DaprEventBridge
Class: DaprEventBridge
@purista/dapr-sdk.DaprEventBridge
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
Example
import { DaprConfigStore, DaprEventBridge, DaprSecretStore, DaprStateStore } from '@purista/dapr-sdk'
const eventBridge = new DaprEventBridge({
serve,
})
// start the services first ...
await eventBridge.start()
Hierarchy
HttpEventBridge
<DaprEventBridgeConfig
>↳
DaprEventBridge
Implements
Table of contents
Constructors
Properties
- app
- client
- config
- defaultCommandTimeout
- instanceId
- isShuttingDown
- isStarted
- logger
- name
- pubSubSubscriptions
- server
- traceProvider
Methods
- destroy
- emit
- emitMessage
- getTracer
- invoke
- isHealthy
- isReady
- off
- on
- registerCommand
- registerSubscription
- removeAllListeners
- start
- startActiveSpan
- unregisterCommand
- unregisterSubscription
- wrapInSpan
Constructors
constructor
• new DaprEventBridge(config
): DaprEventBridge
Parameters
Name | Type | Description |
---|---|---|
config | Object | - |
config.apiPrefix? | string | the prefix to be used if the command is configured as REST api endpoint according to the OpenAPI defintion needs to enableRestApiExpose set to true Default ts /api |
config.clientConfig? | DaprClientConfig | - |
config.commandPayloadAsCloudEvent? | boolean | command invocations are wrapped in CloudEvent Link https://github.com/cloudevents/spec/tree/v1.0 Default ts false |
config.defaultCommandTimeout? | number | Overwrite the hardcoded default timeout of command invocations |
config.enableHttpCompression? | boolean | enable HTTP compression in web server Default ts true |
config.enableRestApiExpose? | boolean | expose commands as regular REST endpoints when they are configured as endpoints Default ts true |
config.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. |
config.logLevel? | LogLevelName | If no logger instance is given, use this log level |
config.logger? | Logger | A logger instance |
config.name? | string | name of the bridge Default ts HttpEventBridge |
config.pathPrefix? | string | the prefix to be used for exposing commands as endpoints expecting a event bus message Default ts purista |
config.serve | (options : { fetch : (request : Request ) => unknown ; hostname? : string ; port? : number }) => Server <typeof IncomingMessage , typeof ServerResponse > | Http2Server | Http2SecureServer | 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 |
config.serverHost? | string | Host of the server. Default ts 127.0.0.1 |
config.serverPort? | number | Port of the server. Default ts 8080 |
config.spanProcessor? | SpanProcessor | A OpenTelemetry span processor |
config.subscriptionPayloadAsCloudEvent? | boolean | subscription invocations are wrapped in CloudEvent Link https://github.com/cloudevents/spec/tree/v1.0 Default ts false |
Returns
Overrides
Defined in
dapr-sdk/src/DaprEventBridge/DaprEventBridge.impl.ts:45
Properties
app
• app: Hono
<Env
, BlankSchema
, "/"
>
Inherited from
Defined in
base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:26
client
• client: HttpEventBridgeClient
Inherited from
Defined in
base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:29
config
• config: Complete
<{ apiPrefix?
: string
; clientConfig?
: DaprClientConfig
; commandPayloadAsCloudEvent?
: boolean
; defaultCommandTimeout?
: number
; enableHttpCompression?
: boolean
; enableRestApiExpose?
: boolean
; instanceId?
: string
; logLevel?
: LogLevelName
; logger?
: Logger
; name?
: string
; pathPrefix?
: string
; serve
: (options
: { fetch
: (request
: Request
) => unknown
; hostname?
: string
; port?
: number
}) => Server
<typeof IncomingMessage
, typeof ServerResponse
> | Http2Server
| Http2SecureServer
; serverHost?
: string
; serverPort?
: number
; spanProcessor?
: SpanProcessor
; subscriptionPayloadAsCloudEvent?
: boolean
}>
Inherited from
Defined in
core/dist/commonjs/core/EventBridge/EventBridgeBaseClass.impl.d.ts:14
defaultCommandTimeout
• defaultCommandTimeout: number
The default time until when a command invocation automatically returns a time out error
Implementation of
EventBridge.defaultCommandTimeout
Inherited from
HttpEventBridge.defaultCommandTimeout
Defined in
core/dist/commonjs/core/EventBridge/EventBridgeBaseClass.impl.d.ts:17
instanceId
• instanceId: string
Implementation of
Inherited from
Defined in
core/dist/commonjs/core/EventBridge/EventBridgeBaseClass.impl.d.ts:16
isShuttingDown
• isShuttingDown: boolean
Inherited from
HttpEventBridge.isShuttingDown
Defined in
base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:27
isStarted
• isStarted: boolean
Inherited from
Defined in
base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:28
logger
• logger: Logger
Inherited from
Defined in
core/dist/commonjs/core/EventBridge/EventBridgeBaseClass.impl.d.ts:12
name
• name: string
Implementation of
Inherited from
Defined in
core/dist/commonjs/core/EventBridge/EventBridgeBaseClass.impl.d.ts:15
pubSubSubscriptions
• Private
pubSubSubscriptions: DaprPubSubType
[] = []
Defined in
dapr-sdk/src/DaprEventBridge/DaprEventBridge.impl.ts:43
server
• server: undefined
| Server
<typeof IncomingMessage
, typeof ServerResponse
> | Http2Server
| Http2SecureServer
Inherited from
Defined in
base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:25
traceProvider
• traceProvider: NodeTracerProvider
Inherited from
Defined in
core/dist/commonjs/core/EventBridge/EventBridgeBaseClass.impl.d.ts:13
Methods
destroy
▸ destroy(): Promise
<void
>
Shut down event bridge as gracefully as possible
Returns
Promise
<void
>
Implementation of
Inherited from
Defined in
base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:43
emit
▸ emit<K
>(eventName
, parameter?
): void
Type parameters
Name | Type |
---|---|
K | extends EventKey <{ eventbridge-connected : never ; eventbridge-connection-error : unknown ; eventbridge-disconnected : never ; eventbridge-error : unknown ; eventbridge-reconnecting : never }> |
Parameters
Name | Type |
---|---|
eventName | K |
parameter? | { eventbridge-connected : never ; eventbridge-connection-error : unknown ; eventbridge-disconnected : never ; eventbridge-error : unknown ; eventbridge-reconnecting : never }[K ] |
Returns
void
Inherited from
Defined in
core/dist/commonjs/core/types/GenericEventEmitter.d.ts:13
emitMessage
▸ emitMessage<T
>(message
): Promise
<Readonly
<EBMessage
>>
Emit a message to the eventbridge without awaiting a result
Type parameters
Name | Type |
---|---|
T | extends EBMessage |
Parameters
Name | Type | Description |
---|---|---|
message | Omit <EBMessage , "id" | "timestamp" | "correlationId" > | the message |
Returns
Promise
<Readonly
<EBMessage
>>
Implementation of
Inherited from
Defined in
base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:32
getTracer
▸ getTracer(): Tracer
Returns open telemetry tracer of this service
Returns
Tracer
Tracer
Inherited from
Defined in
core/dist/commonjs/core/EventBridge/EventBridgeBaseClass.impl.d.ts:24
invoke
▸ invoke<T
>(input
, ttl?
): Promise
<T
>
Call a command of a service and return the result of this command
Type parameters
Name |
---|
T |
Parameters
Name | Type | Description |
---|---|---|
input | Omit <{ contentEncoding : string ; contentType : string ; correlationId : string ; eventName? : string ; id : string ; messageType : Command ; otp? : string ; payload : { parameter : unknown ; payload : unknown } ; principalId? : string ; receiver : EBMessageAddress ; sender : { instanceId : string ; serviceName : string ; serviceTarget : string ; serviceVersion : string } ; tenantId? : string ; timestamp : number ; traceId? : string }, "id" | "timestamp" | "correlationId" | "messageType" > | a partial command message |
ttl? | number | the time to live (timeout) of the invocation |
Returns
Promise
<T
>
Implementation of
Inherited from
Defined in
base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:33
isHealthy
▸ isHealthy(): Promise
<boolean
>
Indicates if the eventbridge is running and works correctly
Returns
Promise
<boolean
>
Implementation of
Inherited from
Defined in
base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:39
isReady
▸ isReady(): Promise
<boolean
>
Indicates if the eventbridge has been started and is connected to underlaying message broker
Returns
Promise
<boolean
>
Implementation of
Inherited from
Defined in
base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:38
off
▸ off<K
>(eventName
, fn
): void
Type parameters
Name | Type |
---|---|
K | extends EventKey <{ eventbridge-connected : never ; eventbridge-connection-error : unknown ; eventbridge-disconnected : never ; eventbridge-error : unknown ; eventbridge-reconnecting : never }> |
Parameters
Name | Type |
---|---|
eventName | K |
fn | EventReceiver <{ eventbridge-connected : never ; eventbridge-connection-error : unknown ; eventbridge-disconnected : never ; eventbridge-error : unknown ; eventbridge-reconnecting : never }[K ]> |
Returns
void
Inherited from
Defined in
core/dist/commonjs/core/types/GenericEventEmitter.d.ts:12
on
▸ on<K
>(eventName
, fn
): void
Type parameters
Name | Type |
---|---|
K | extends EventKey <{ eventbridge-connected : never ; eventbridge-connection-error : unknown ; eventbridge-disconnected : never ; eventbridge-error : unknown ; eventbridge-reconnecting : never }> |
Parameters
Name | Type |
---|---|
eventName | K |
fn | EventReceiver <{ eventbridge-connected : never ; eventbridge-connection-error : unknown ; eventbridge-disconnected : never ; eventbridge-error : unknown ; eventbridge-reconnecting : never }[K ]> |
Returns
void
Inherited from
Defined in
core/dist/commonjs/core/types/GenericEventEmitter.d.ts:11
registerCommand
▸ registerCommand(address
, cb
, metadata
, eventBridgeConfig
): Promise
<string
>
Parameters
Name | Type |
---|---|
address | EBMessageAddress |
cb | (message : { contentEncoding : string ; contentType : string ; correlationId : string ; eventName? : string ; id : string ; messageType : Command ; otp? : string ; payload : { parameter : unknown ; payload : unknown } ; principalId? : string ; receiver : EBMessageAddress ; sender : { instanceId : string ; serviceName : string ; serviceTarget : string ; serviceVersion : string } ; tenantId? : string ; timestamp : number ; traceId? : string }) => 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" >>> |
metadata | Object |
metadata.expose | { contentEncodingRequest? : string ; contentEncodingResponse? : string ; contentTypeRequest? : string ; contentTypeResponse? : string ; deprecated? : boolean ; inputPayload? : SchemaObject ; outputPayload? : SchemaObject ; parameter? : SchemaObject } & { http : { method : "POST" | "GET" | "PATCH" | "PUT" | "DELETE" ; openApi? : { additionalStatusCodes? : StatusCode [] ; description : string ; isSecure : boolean ; operationId? : string ; query? : QueryParameter <{}>[] ; summary : string ; tags? : string [] } ; path : string } } |
eventBridgeConfig | DefinitionEventBridgeConfig |
Returns
Promise
<string
>
Implementation of
Inherited from
HttpEventBridge.registerCommand
Defined in
base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:34
registerSubscription
▸ registerSubscription(subscription
, cb
): Promise
<string
>
Register a new subscription
Parameters
Name | Type | Description |
---|---|---|
subscription | Subscription | the subscription definition |
cb | (message : EBMessage ) => Promise <undefined | 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" >> | the function to be called if a matching message arrives |
Returns
Promise
<string
>
Implementation of
EventBridge.registerSubscription
Overrides
HttpEventBridge.registerSubscription
Defined in
dapr-sdk/src/DaprEventBridge/DaprEventBridge.impl.ts:97
removeAllListeners
▸ removeAllListeners(): void
Returns
void
Inherited from
HttpEventBridge.removeAllListeners
Defined in
core/dist/commonjs/core/types/GenericEventEmitter.d.ts:14
start
▸ start(): Promise
<void
>
Start the eventbridge and connect to the underlaying message broker
Returns
Promise
<void
>
Implementation of
Overrides
Defined in
dapr-sdk/src/DaprEventBridge/DaprEventBridge.impl.ts:80
startActiveSpan
▸ startActiveSpan<F
>(name
, opts
, context
, fn
): Promise
<F
>
Start a child span for opentelemetry tracking
Type parameters
Name |
---|
F |
Parameters
Name | Type | Description |
---|---|---|
name | string | name of span |
opts | SpanOptions | span options |
context | undefined | Context | optional context |
fn | (span : Span ) => Promise <F > | function to be executed within the span |
Returns
Promise
<F
>
return value of fn
Inherited from
HttpEventBridge.startActiveSpan
Defined in
core/dist/commonjs/core/EventBridge/EventBridgeBaseClass.impl.d.ts:33
unregisterCommand
▸ unregisterCommand(address
): Promise
<void
>
Unregister a service command
Parameters
Name | Type | Description |
---|---|---|
address | EBMessageAddress | The address (service name, version and command name) of the command to be de-registered |
Returns
Promise
<void
>
Implementation of
Inherited from
HttpEventBridge.unregisterCommand
Defined in
base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:35
unregisterSubscription
▸ unregisterSubscription(address
): Promise
<void
>
Parameters
Name | Type |
---|---|
address | EBMessageAddress |
Returns
Promise
<void
>
Implementation of
EventBridge.unregisterSubscription
Inherited from
HttpEventBridge.unregisterSubscription
Defined in
base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:37
wrapInSpan
▸ wrapInSpan<F
>(name
, opts
, fn
, context?
): Promise
<F
>
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
Name |
---|
F |
Parameters
Name | Type | Description |
---|---|---|
name | string | name of span |
opts | SpanOptions | span options |
fn | (span : Span ) => Promise <F > | function te be executed in the span |
context? | Context | span context |
Returns
Promise
<F
>
return value of fn
Inherited from
Defined in
core/dist/commonjs/core/EventBridge/EventBridgeBaseClass.impl.d.ts:49