Skip to content

Commit

Permalink
Upgraded dwn-sdk-js dependency to support Resumable Task Store (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
thehenrytsai authored Jun 18, 2024
1 parent 24c768c commit 4eb860a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 26 deletions.
32 changes: 16 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"url": "https://github.com/TBD54566975/dwn-server/issues"
},
"dependencies": {
"@tbd54566975/dwn-sdk-js": "0.3.5",
"@tbd54566975/dwn-sql-store": "0.4.5",
"@tbd54566975/dwn-sdk-js": "0.3.7",
"@tbd54566975/dwn-sql-store": "0.5.1",
"better-sqlite3": "^8.5.0",
"body-parser": "^1.20.2",
"bytes": "3.1.2",
Expand Down
28 changes: 20 additions & 8 deletions src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import {
DataStoreLevel,
EventLogLevel,
MessageStoreLevel,
ResumableTaskStoreLevel,
} from '@tbd54566975/dwn-sdk-js';
import type {
DataStore,
DwnConfig,
EventLog,
EventStream,
MessageStore,
ResumableTaskStore,
TenantGate,
} from '@tbd54566975/dwn-sdk-js';
import type { Dialect } from '@tbd54566975/dwn-sql-store';
Expand All @@ -20,6 +22,7 @@ import {
MessageStoreSql,
MysqlDialect,
PostgresDialect,
ResumableTaskStoreSql,
SqliteDialect,
} from '@tbd54566975/dwn-sql-store';

Expand All @@ -34,6 +37,7 @@ export enum EStoreType {
DataStore,
MessageStore,
EventLog,
ResumableTaskStore,
}

export enum BackendTypes {
Expand All @@ -43,7 +47,7 @@ export enum BackendTypes {
POSTGRES = 'postgres',
}

export type StoreType = DataStore | EventLog | MessageStore;
export type StoreType = DataStore | EventLog | MessageStore | ResumableTaskStore;

export function getDWNConfig(
config : DwnServerConfig,
Expand All @@ -55,18 +59,16 @@ export function getDWNConfig(
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 messageStore: MessageStore = getStore(config.messageStore, EStoreType.MessageStore);
const resumableTaskStore: ResumableTaskStore = getStore(config.messageStore, EStoreType.ResumableTaskStore);

return { eventStream, eventLog, dataStore, messageStore, tenantGate };
return { eventStream, eventLog, dataStore, messageStore, resumableTaskStore, tenantGate };
}

function getLevelStore(
storeURI: URL,
storeType: EStoreType,
): DataStore | MessageStore | EventLog {
): DataStore | MessageStore | EventLog | ResumableTaskStore {
switch (storeType) {
case EStoreType.DataStore:
return new DataStoreLevel({
Expand All @@ -81,6 +83,10 @@ function getLevelStore(
return new EventLogLevel({
location: storeURI.host + storeURI.pathname + '/EVENTLOG',
});
case EStoreType.ResumableTaskStore:
return new ResumableTaskStoreLevel({
location: storeURI.host + storeURI.pathname + '/RESUMABLE-TASK-STORE',
});
default:
throw new Error('Unexpected level store type');
}
Expand All @@ -89,14 +95,16 @@ function getLevelStore(
function getDBStore(
db: Dialect,
storeType: EStoreType,
): DataStore | MessageStore | EventLog {
): DataStore | MessageStore | EventLog | ResumableTaskStore {
switch (storeType) {
case EStoreType.DataStore:
return new DataStoreSql(db);
case EStoreType.MessageStore:
return new MessageStoreSql(db);
case EStoreType.EventLog:
return new EventLogSql(db);
case EStoreType.ResumableTaskStore:
return new ResumableTaskStoreSql(db);
default:
throw new Error('Unexpected db store type');
}
Expand All @@ -114,6 +122,10 @@ function getStore(
storeString: string,
storeType: EStoreType.MessageStore,
): MessageStore;
function getStore(
storeString: string,
storeType: EStoreType.ResumableTaskStore,
): ResumableTaskStore;
function getStore(storeString: string, storeType: EStoreType): StoreType {
const storeURI = new URL(storeString);

Expand Down
3 changes: 3 additions & 0 deletions tests/test-dwn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
DataStoreSql,
EventLogSql,
MessageStoreSql,
ResumableTaskStoreSql,
} from '@tbd54566975/dwn-sql-store';

import { getDialectFromURI } from '../src/storage.js';
Expand All @@ -18,6 +19,7 @@ export async function getTestDwn(options: {
const dataStore = new DataStoreSql(db);
const eventLog = new EventLogSql(db);
const messageStore = new MessageStoreSql(db);
const resumableTaskStore = new ResumableTaskStoreSql(db);
const eventStream = withEvents ? new EventEmitterStream() : undefined;

// NOTE: no resolver cache used here to avoid locking LevelDB
Expand All @@ -31,6 +33,7 @@ export async function getTestDwn(options: {
eventLog,
dataStore,
messageStore,
resumableTaskStore,
eventStream,
tenantGate,
didResolver
Expand Down

0 comments on commit 4eb860a

Please sign in to comment.