-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
1,104 additions
and
6,021 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ dist | |
data | ||
etc | ||
node_modules | ||
tmp | ||
tmp | ||
dwn.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Histogram, Counter } from 'prom-client'; | ||
|
||
export const requestCounter = new Counter({ | ||
name : 'dwn_requests_total', | ||
help : 'all dwn requests processed', | ||
labelNames : ['method', 'status', 'error'], | ||
}); | ||
|
||
export const responseHistogram = new Histogram({ | ||
name : 'http_response', | ||
help : 'response histogram', | ||
buckets : [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10], | ||
labelNames : ['route', 'code'], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { Config } from './config.js'; | ||
import { DataStore, EventLog, MessageStore, DwnConfig } from '@tbd54566975/dwn-sdk-js'; | ||
import { SqliteDialect, MysqlDialect, PostgresDialect, MessageStoreSql, DataStoreSql, EventLogSql, Dialect } from '@tbd54566975/dwn-sql-store'; | ||
import { DataStoreLevel, EventLogLevel, MessageStoreLevel } from '@tbd54566975/dwn-sdk-js/stores'; | ||
|
||
import Database from 'better-sqlite3'; | ||
import { createPool as MySQLCreatePool } from 'mysql2'; | ||
import pg from 'pg'; | ||
import Cursor from 'pg-cursor'; | ||
|
||
enum EStoreType { | ||
DataStore, | ||
MessageStore, | ||
EventLog, | ||
} | ||
|
||
enum BackendTypes { | ||
LEVEL = 'level', | ||
SQLITE = 'sqlite', | ||
MYSQL = 'mysql', | ||
POSTGRES = 'postgres', | ||
} | ||
|
||
type StoreType = DataStore | EventLog | MessageStore; | ||
|
||
export function getDWNConfig(config: Config): DwnConfig { | ||
let dataStore: DataStore = getStore(config.dataStore, EStoreType.DataStore); | ||
let eventLog: EventLog = getStore(config.eventLog, EStoreType.EventLog); | ||
let messageStore: MessageStore = getStore(config.messageStore, EStoreType.MessageStore); | ||
|
||
return { eventLog, dataStore, messageStore }; | ||
} | ||
|
||
function getLevelStore(storeURI: URL, storeType: EStoreType) { | ||
switch (storeType) { | ||
case EStoreType.DataStore: | ||
return new DataStoreLevel({ | ||
blockstoreLocation: storeURI.host + storeURI.pathname + '/DATASTORE', | ||
}); | ||
case EStoreType.MessageStore: | ||
return new MessageStoreLevel({ | ||
blockstoreLocation: storeURI.host + storeURI.pathname + '/DATASTORE', | ||
}); | ||
case EStoreType.EventLog: | ||
return new EventLogLevel({ | ||
location: storeURI.host + storeURI.pathname + '/EVENTLOG' | ||
}); | ||
default: | ||
throw new Error('Unexpected level store type'); | ||
} | ||
} | ||
|
||
function getDBStore(db: Dialect, storeType: EStoreType) { | ||
switch (storeType) { | ||
case EStoreType.DataStore: | ||
return new DataStoreSql(db); | ||
case EStoreType.MessageStore: | ||
return new MessageStoreSql(db); | ||
case EStoreType.EventLog: | ||
return new EventLogSql(db); | ||
default: | ||
throw new Error('Unexpected db store type'); | ||
} | ||
} | ||
|
||
function getStore(storeString: string, storeType: EStoreType.DataStore): DataStore; | ||
function getStore(storeString: string, storeType: EStoreType.EventLog): EventLog; | ||
function getStore(storeString: string, storeType: EStoreType.MessageStore): MessageStore; | ||
function getStore(storeString: string, storeType: EStoreType): StoreType { | ||
const storeURI = new URL(storeString); | ||
|
||
switch (storeURI.protocol.slice(0, -1)) { | ||
case BackendTypes.LEVEL: | ||
return getLevelStore(storeURI, storeType); | ||
|
||
case BackendTypes.SQLITE: | ||
case BackendTypes.MYSQL: | ||
case BackendTypes.POSTGRES: | ||
return getDBStore(getDBFromURI(storeURI), storeType); | ||
|
||
default: | ||
throw invalidStorageSchemeMessage(storeURI.protocol); | ||
} | ||
} | ||
|
||
function getDBFromURI(u: URL): Dialect { | ||
switch(u.protocol.slice(0, -1)) { | ||
case BackendTypes.SQLITE: | ||
return new SqliteDialect({ | ||
database: async () => new Database(u.host + u.pathname), | ||
}); | ||
case BackendTypes.MYSQL: | ||
return new MysqlDialect({ | ||
pool: async () => MySQLCreatePool(u.toString()), | ||
}); | ||
case BackendTypes.POSTGRES: | ||
return new PostgresDialect({ | ||
pool : async () => new pg.Pool({u}), | ||
cursor : Cursor, | ||
}); | ||
} | ||
} | ||
|
||
function invalidStorageSchemeMessage(protocol: string): string { | ||
let schemes = []; | ||
for (const [_, value] of Object.entries(BackendTypes)) { | ||
schemes.push(value); | ||
} | ||
return 'Unknown storage protocol ' + protocol.slice(0, 1) + '! Please use one of: ' + schemes.join(', ') + '. For details, see README'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters