Repository navigation
Expand file tree
/
Copy pathcodex-storage.js
More file actions
42 lines (39 loc) · 1.49 KB
/
Copy pathcodex-storage.js
File metadata and controls
42 lines (39 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// @ts-check
import { createConfigStore } from "./config-store.js";
import { createGlobalStateStore } from "./global-state-store.js";
import { createSessionStore } from "./session-store.js";
import { createStateDbStore } from "./state-db-store.js";
/** @typedef {Record<string, Function>} StoragePort */
/**
* @typedef {{
* config: StoragePort,
* sessions: StoragePort,
* stateDb: StoragePort,
* globalState: StoragePort
* }} CodexStorage
*/
/**
* Compose the four low-level Codex storage ports without adding orchestration
* or business rules. Concrete adapters are injected by the application host.
*
* @template {CodexStorage} T
* @param {T} ports
* @returns {Readonly<{config: Readonly<T['config']>, sessions: Readonly<T['sessions']>, stateDb: Readonly<T['stateDb']>, globalState: Readonly<T['globalState']>}>}
*/
export function createCodexStorage(ports) {
if (!ports || typeof ports !== "object") {
throw new TypeError("CodexStorage requires four storage ports.");
}
for (const name of ["config", "sessions", "stateDb", "globalState"]) {
const port = ports[/** @type {keyof CodexStorage} */ (name)];
if (!port || typeof port !== "object" || Array.isArray(port)) {
throw new TypeError(`CodexStorage ${name} port is missing.`);
}
}
return Object.freeze({
config: createConfigStore(ports.config),
sessions: createSessionStore(ports.sessions),
stateDb: createStateDbStore(ports.stateDb),
globalState: createGlobalStateStore(ports.globalState)
});
}