diff --git a/src/dwn-server.ts b/src/dwn-server.ts index 6ba27e4..b2502e9 100644 --- a/src/dwn-server.ts +++ b/src/dwn-server.ts @@ -1,4 +1,5 @@ -import { Dwn } from '@tbd54566975/dwn-sdk-js'; +import type { EventStream } from '@tbd54566975/dwn-sdk-js'; +import { Dwn, EventEmitterStream } from '@tbd54566975/dwn-sdk-js'; import type { Server } from 'http'; import log from 'loglevel'; @@ -61,7 +62,17 @@ export class DwnServer { proofOfWorkInitialMaximumAllowedHash: this.config.registrationProofOfWorkInitialMaxHash, }); - this.dwn = await Dwn.create(getDWNConfig(this.config, registrationManager)); + let eventStream: EventStream | undefined; + if (this.config.webSocketServerEnabled) { + // setting `EventEmitterStream` as default the default `EventStream + // if an alternate implementation is needed instantiate a `Dwn` with a custom `EventStream` and add it to server options. + eventStream = new EventEmitterStream(); + } + + this.dwn = await Dwn.create(getDWNConfig(this.config, { + tenantGate: registrationManager, + eventStream, + })); } this.#httpApi = new HttpApi(this.config, this.dwn, registrationManager); @@ -76,8 +87,7 @@ export class DwnServer { if (this.config.webSocketServerEnabled) { this.#wsApi = new WsApi(this.#httpApi.server, this.dwn); - this.#wsApi.start(); - log.info('WebSocketServer ready...'); + this.#wsApi.start(() => log.info('WebSocketServer ready...')); } } diff --git a/src/storage.ts b/src/storage.ts index 23b8e05..a4eea8c 100644 --- a/src/storage.ts +++ b/src/storage.ts @@ -2,7 +2,6 @@ import * as fs from 'fs'; import { DataStoreLevel, - EventEmitterStream, EventLogLevel, MessageStoreLevel, } from '@tbd54566975/dwn-sdk-js'; @@ -10,6 +9,7 @@ import type { DataStore, DwnConfig, EventLog, + EventStream, MessageStore, TenantGate, } from '@tbd54566975/dwn-sdk-js'; @@ -46,9 +46,13 @@ export enum BackendTypes { export type StoreType = DataStore | EventLog | MessageStore; export function getDWNConfig( - config: DwnServerConfig, - tenantGate: TenantGate, + config : DwnServerConfig, + options : { + tenantGate? : TenantGate, + eventStream? : EventStream, + } ): DwnConfig { + const { tenantGate, eventStream } = options; const dataStore: DataStore = getStore(config.dataStore, EStoreType.DataStore); const eventLog: EventLog = getStore(config.eventLog, EStoreType.EventLog); const messageStore: MessageStore = getStore( @@ -56,7 +60,6 @@ export function getDWNConfig( EStoreType.MessageStore, ); - const eventStream = config.webSocketServerEnabled ? new EventEmitterStream() : undefined; return { eventStream, eventLog, dataStore, messageStore, tenantGate }; } diff --git a/src/ws-api.ts b/src/ws-api.ts index c41e41e..ab0cef1 100644 --- a/src/ws-api.ts +++ b/src/ws-api.ts @@ -268,8 +268,9 @@ export class WsApi { }); } - start(): WebSocketServer { + start(callback?: () => void): WebSocketServer { this.#setupWebSocket(); + callback?.(); return this.#wsServer; }