2024-04-19 21:39:35 +00:00
|
|
|
import { NCache } from '@nostrify/nostrify';
|
2024-01-04 06:07:45 +00:00
|
|
|
import { Conf } from '@/config.ts';
|
2024-01-04 03:55:53 +00:00
|
|
|
import { db } from '@/db.ts';
|
2024-01-07 21:15:20 +00:00
|
|
|
import { activeRelays, pool } from '@/pool.ts';
|
2024-01-04 03:55:53 +00:00
|
|
|
import { EventsDB } from '@/storages/events-db.ts';
|
2024-01-04 06:52:55 +00:00
|
|
|
import { Optimizer } from '@/storages/optimizer.ts';
|
2024-01-07 21:15:20 +00:00
|
|
|
import { PoolStore } from '@/storages/pool-store.ts';
|
2024-01-07 20:54:33 +00:00
|
|
|
import { Reqmeister } from '@/storages/reqmeister.ts';
|
2024-01-04 06:07:45 +00:00
|
|
|
import { SearchStore } from '@/storages/search-store.ts';
|
2024-04-25 23:28:19 +00:00
|
|
|
import { InternalRelay } from '@/storages/InternalRelay.ts';
|
2024-05-01 19:56:47 +00:00
|
|
|
import { UserStore } from '@/storages/UserStore.ts';
|
2024-01-07 20:54:33 +00:00
|
|
|
import { Time } from '@/utils/time.ts';
|
2024-01-04 03:55:53 +00:00
|
|
|
|
2024-04-25 23:28:19 +00:00
|
|
|
export class Storages {
|
2024-05-01 19:56:47 +00:00
|
|
|
private static _db: EventsDB | undefined;
|
|
|
|
private static _admin: UserStore | undefined;
|
|
|
|
private static _cache: NCache | undefined;
|
|
|
|
private static _client: PoolStore | undefined;
|
|
|
|
private static _optimizer: Optimizer | undefined;
|
|
|
|
private static _reqmeister: Reqmeister | undefined;
|
2024-04-25 23:31:16 +00:00
|
|
|
private static _pubsub: InternalRelay | undefined;
|
2024-05-01 19:56:47 +00:00
|
|
|
private static _search: SearchStore | undefined;
|
|
|
|
|
|
|
|
/** SQLite database to store events this Ditto server cares about. */
|
|
|
|
public static get db(): EventsDB {
|
|
|
|
if (!this._db) {
|
|
|
|
this._db = new EventsDB(db);
|
|
|
|
}
|
|
|
|
return this._db;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Admin user storage. */
|
|
|
|
public static get admin(): UserStore {
|
|
|
|
if (!this._admin) {
|
|
|
|
this._admin = new UserStore(Conf.pubkey, this.db);
|
|
|
|
}
|
|
|
|
return this._admin;
|
|
|
|
}
|
2024-04-25 23:28:19 +00:00
|
|
|
|
2024-05-01 19:56:47 +00:00
|
|
|
/** Internal pubsub relay between controllers and the pipeline. */
|
|
|
|
public static get pubsub(): InternalRelay {
|
2024-04-25 23:31:16 +00:00
|
|
|
if (!this._pubsub) {
|
|
|
|
this._pubsub = new InternalRelay();
|
2024-04-25 23:28:19 +00:00
|
|
|
}
|
2024-04-25 23:31:16 +00:00
|
|
|
return this._pubsub;
|
2024-04-25 23:28:19 +00:00
|
|
|
}
|
|
|
|
|
2024-05-01 19:56:47 +00:00
|
|
|
/** Relay pool storage. */
|
|
|
|
public static get client(): PoolStore {
|
|
|
|
if (!this._client) {
|
|
|
|
this._client = new PoolStore({
|
|
|
|
pool,
|
|
|
|
relays: activeRelays,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return this._client;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** In-memory data store for cached events. */
|
|
|
|
public static get cache(): NCache {
|
|
|
|
if (!this._cache) {
|
|
|
|
this._cache = new NCache({ max: 3000 });
|
|
|
|
}
|
|
|
|
return this._cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Batches requests for single events. */
|
|
|
|
public static get reqmeister(): Reqmeister {
|
|
|
|
if (!this._reqmeister) {
|
|
|
|
this._reqmeister = new Reqmeister({
|
|
|
|
client: this.client,
|
|
|
|
delay: Time.seconds(1),
|
|
|
|
timeout: Time.seconds(1),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return this._reqmeister;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Main Ditto storage adapter */
|
|
|
|
public static get optimizer(): Optimizer {
|
|
|
|
if (!this._optimizer) {
|
|
|
|
this._optimizer = new Optimizer({
|
|
|
|
db: this.db,
|
|
|
|
cache: this.cache,
|
|
|
|
client: this.reqmeister,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return this._optimizer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Storage to use for remote search. */
|
|
|
|
public static get search(): SearchStore {
|
|
|
|
if (!this._search) {
|
|
|
|
this._search = new SearchStore({
|
|
|
|
relay: Conf.searchRelay,
|
|
|
|
fallback: this.optimizer,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return this._search;
|
|
|
|
}
|
|
|
|
}
|