Skip to content

Commit

Permalink
optional tenant gate and event stream in options
Browse files Browse the repository at this point in the history
  • Loading branch information
LiranCohen committed Feb 12, 2024
1 parent c93180a commit 8d20cd6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
18 changes: 14 additions & 4 deletions src/dwn-server.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
Expand All @@ -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...'));
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import * as fs from 'fs';

import {
DataStoreLevel,
EventEmitterStream,
EventLogLevel,
MessageStoreLevel,
} from '@tbd54566975/dwn-sdk-js';
import type {
DataStore,
DwnConfig,
EventLog,
EventStream,
MessageStore,
TenantGate,
} from '@tbd54566975/dwn-sdk-js';
Expand Down Expand Up @@ -46,17 +46,20 @@ 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(
config.messageStore,
EStoreType.MessageStore,
);

const eventStream = config.webSocketServerEnabled ? new EventEmitterStream() : undefined;
return { eventStream, eventLog, dataStore, messageStore, tenantGate };
}

Expand Down
3 changes: 2 additions & 1 deletion src/ws-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,9 @@ export class WsApi {
});
}

start(): WebSocketServer {
start(callback?: () => void): WebSocketServer {
this.#setupWebSocket();
callback?.();
return this.#wsServer;
}

Expand Down

0 comments on commit 8d20cd6

Please sign in to comment.