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
Example
import { DaprConfigStore, DaprEventBridge, DaprSecretStore, DaprStateStore } from '@purista/dapr-sdk'
const eventBridge = new DaprEventBridge({
serve,
})
// start the services first ...
await eventBridge.start()
Extends
Implements
Constructors
new DaprEventBridge()
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 defintion needs to enableRestApiExpose
set to true
Default
/api
clientConfig?
commandPayloadAsCloudEvent?
boolean
command invocations are wrapped in CloudEvent
Link
https://github.com/cloudevents/spec/tree/v1.0
Default
false
defaultCommandTimeout?
number
Overwrite the hardcoded default timeout of command invocations
enableHttpCompression?
boolean
enable HTTP compression in web server
Default
true
enableRestApiExpose?
boolean
expose commands as regular REST endpoints when they are configured as endpoints
Default
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?
A logger instance
logLevel?
If no logger instance is given, use this log level
name?
string
name of the bridge
Default
HttpEventBridge
pathPrefix?
string
the prefix to be used for exposing commands as endpoints expecting a event bus message
Default
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 packagehttps://deno.land/std/http/server.ts
See
serverHost?
string
Host of the server.
Default
127.0.0.1
serverPort?
number
Port of the server.
Default
8080
spanProcessor?
SpanProcessor
A OpenTelemetry span processor
subscriptionPayloadAsCloudEvent?
boolean
subscription invocations are wrapped in CloudEvent
Link
https://github.com/cloudevents/spec/tree/v1.0
Default
false
Returns
Overrides
Properties
app
app:
Hono
Defined in: base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:24
Inherited from
client
client:
HttpEventBridgeClient
Defined in: base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:27
Inherited from
config
config:
Complete
<{apiPrefix
:string
;clientConfig
:DaprClientConfig
;commandPayloadAsCloudEvent
:boolean
;defaultCommandTimeout
:number
;enableHttpCompression
:boolean
;enableRestApiExpose
:boolean
;instanceId
:string
;logger
:Logger
;logLevel
:LogLevelName
;name
:string
;pathPrefix
:string
;serve
: (options
) =>Server
<typeofIncomingMessage
, typeofServerResponse
> |Http2Server
<typeofIncomingMessage
, typeofServerResponse
, typeofHttp2ServerRequest
, typeofHttp2ServerResponse
> |Http2SecureServer
<typeofIncomingMessage
, typeofServerResponse
, typeofHttp2ServerRequest
, typeofHttp2ServerResponse
>;serverHost
:string
;serverPort
:number
;spanProcessor
:SpanProcessor
;subscriptionPayloadAsCloudEvent
:boolean
; }>
Defined in: core/dist/commonjs/core/EventBridge/EventBridgeBaseClass.impl.d.ts:14
Inherited from
defaultCommandTimeout
defaultCommandTimeout:
number
Defined in: core/dist/commonjs/core/EventBridge/EventBridgeBaseClass.impl.d.ts:17
The default time until when a command invocation automatically returns a time out error
Implementation of
EventBridge
.defaultCommandTimeout
Inherited from
HttpEventBridge
.defaultCommandTimeout
instanceId
instanceId:
string
Defined in: core/dist/commonjs/core/EventBridge/EventBridgeBaseClass.impl.d.ts:16
Implementation of
Inherited from
isShuttingDown
isShuttingDown:
boolean
Defined in: base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:25
Inherited from
HttpEventBridge
.isShuttingDown
isStarted
isStarted:
boolean
Defined in: base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:26
Inherited from
logger
logger:
Logger
Defined in: core/dist/commonjs/core/EventBridge/EventBridgeBaseClass.impl.d.ts:12
Inherited from
name
name:
string
Defined in: core/dist/commonjs/core/EventBridge/EventBridgeBaseClass.impl.d.ts:15
Implementation of
Inherited from
server
server:
undefined
|Server
<typeofIncomingMessage
, typeofServerResponse
> |Http2Server
<typeofIncomingMessage
, typeofServerResponse
, typeofHttp2ServerRequest
, typeofHttp2ServerResponse
> |Http2SecureServer
<typeofIncomingMessage
, typeofServerResponse
, typeofHttp2ServerRequest
, typeofHttp2ServerResponse
>
Defined in: base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:23
Inherited from
traceProvider
traceProvider:
NodeTracerProvider
Defined in: core/dist/commonjs/core/EventBridge/EventBridgeBaseClass.impl.d.ts:13
Inherited from
Methods
destroy()
destroy():
Promise
<void
>
Defined in: base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:41
Shut down event bridge as gracefully as possible
Returns
Promise
<void
>
Implementation of
Inherited from
emit()
emit<
K
>(eventName
,parameter
?):void
Defined in: core/dist/commonjs/core/types/GenericEventEmitter.d.ts:13
Type Parameters
• K extends EventKey
<{ [key:
custom-${string}]
: unknown
; [key:
adapter-${string}]
: unknown
; eventbridge-connected
: never
; eventbridge-connection-error
: unknown
; eventbridge-disconnected
: never
; eventbridge-error
: unknown
; eventbridge-reconnecting
: never
; }>
Parameters
eventName
K
parameter?
object
[K
]
Returns
void
Inherited from
emitMessage()
emitMessage<
T
>(message
):Promise
<Readonly
<EBMessage
>>
Defined in: base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:30
Emit a message to the eventbridge without awaiting a result
Type Parameters
• T extends EBMessage
Parameters
message
Omit
<EBMessage
, "id"
| "timestamp"
| "correlationId"
>
the message
Returns
Promise
<Readonly
<EBMessage
>>
Implementation of
Inherited from
getTracer()
getTracer():
Tracer
Defined in: core/dist/commonjs/core/EventBridge/EventBridgeBaseClass.impl.d.ts:24
Returns open telemetry tracer of this service
Returns
Tracer
Tracer
Inherited from
invoke()
invoke<
T
>(input
,ttl
?):Promise
<T
>
Defined in: base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:31
Call a command of a service and return the result of this command
Type Parameters
• T
Parameters
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
isHealthy()
isHealthy():
Promise
<boolean
>
Defined in: base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:37
Indicates if the eventbridge is running and works correctly
Returns
Promise
<boolean
>
Implementation of
Inherited from
isReady()
isReady():
Promise
<boolean
>
Defined in: base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:36
Indicates if the eventbridge has been started and is connected to underlaying message broker
Returns
Promise
<boolean
>
Implementation of
Inherited from
off()
off<
K
>(eventName
,fn
):void
Defined in: core/dist/commonjs/core/types/GenericEventEmitter.d.ts:12
Type Parameters
• K extends EventKey
<{ [key:
custom-${string}]
: unknown
; [key:
adapter-${string}]
: unknown
; eventbridge-connected
: never
; eventbridge-connection-error
: unknown
; eventbridge-disconnected
: never
; eventbridge-error
: unknown
; eventbridge-reconnecting
: never
; }>
Parameters
eventName
K
fn
EventReceiver
<object
[K
]>
Returns
void
Inherited from
on()
on<
K
>(eventName
,fn
):void
Defined in: core/dist/commonjs/core/types/GenericEventEmitter.d.ts:11
Type Parameters
• K extends EventKey
<{ [key:
custom-${string}]
: unknown
; [key:
adapter-${string}]
: unknown
; eventbridge-connected
: never
; eventbridge-connection-error
: unknown
; eventbridge-disconnected
: never
; eventbridge-error
: unknown
; eventbridge-reconnecting
: never
; }>
Parameters
eventName
K
fn
EventReceiver
<object
[K
]>
Returns
void
Inherited from
registerCommand()
registerCommand(
address
,cb
,metadata
,eventBridgeConfig
):Promise
<string
>
Defined in: base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:32
Parameters
address
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
Returns
Promise
<string
>
Implementation of
Inherited from
HttpEventBridge
.registerCommand
registerSubscription()
registerSubscription(
subscription
,cb
):Promise
<string
>
Defined in: dapr-sdk/src/DaprEventBridge/DaprEventBridge.impl.ts:97
Register a new subscription
Parameters
subscription
the subscription definition
cb
(message
) => 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
removeAllListeners()
removeAllListeners():
void
Defined in: core/dist/commonjs/core/types/GenericEventEmitter.d.ts:14
Returns
void
Inherited from
HttpEventBridge
.removeAllListeners
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
Overrides
startActiveSpan()
startActiveSpan<
F
>(name
,opts
,context
,fn
):Promise
<F
>
Defined in: core/dist/commonjs/core/EventBridge/EventBridgeBaseClass.impl.d.ts:33
Start a child span for opentelemetry tracking
Type Parameters
• F
Parameters
name
string
name of span
opts
SpanOptions
span options
context
optional context
undefined
| 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/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:33
Unregister a service command
Parameters
address
The address (service name, version and command name) of the command to be de-registered
Returns
Promise
<void
>
Implementation of
Inherited from
HttpEventBridge
.unregisterCommand
unregisterSubscription()
unregisterSubscription(
address
):Promise
<void
>
Defined in: base-http-bridge/dist/commonjs/HttpEventBridge/HttpEventBridge.impl.d.ts:35
Parameters
address
Returns
Promise
<void
>
Implementation of
EventBridge
.unregisterSubscription
Inherited from
HttpEventBridge
.unregisterSubscription
wrapInSpan()
wrapInSpan<
F
>(name
,opts
,fn
,context
?):Promise
<F
>
Defined in: core/dist/commonjs/core/EventBridge/EventBridgeBaseClass.impl.d.ts:49
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
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