diff options
| author | Dax Raad <[email protected]> | 2025-05-18 14:13:04 -0400 |
|---|---|---|
| committer | Dax Raad <[email protected]> | 2025-05-26 12:40:17 -0400 |
| commit | 0e303e6508edb4374213d1f98ec383b266339774 (patch) | |
| tree | f7dc146eb58126f55f470ef135b66c678bf16898 /js/src/storage | |
| parent | bcd2fd68b7fa00af055f558049994c2975d9515d (diff) | |
| download | opencode-0e303e6508edb4374213d1f98ec383b266339774.tar.gz opencode-0e303e6508edb4374213d1f98ec383b266339774.zip | |
sync
Diffstat (limited to 'js/src/storage')
| -rw-r--r-- | js/src/storage/storage.ts | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/js/src/storage/storage.ts b/js/src/storage/storage.ts index 19e8dc06f..89c8b4d17 100644 --- a/js/src/storage/storage.ts +++ b/js/src/storage/storage.ts @@ -4,10 +4,19 @@ import fs from "fs/promises"; import { Log } from "../util/log"; import { App } from "../app"; import { AppPath } from "../app/path"; +import { Bus } from "../bus"; +import z from "zod/v4"; export namespace Storage { const log = Log.create({ service: "storage" }); + export const Event = { + Write: Bus.event( + "storage.write", + z.object({ key: z.string(), body: z.any() }), + ), + }; + const state = App.state("storage", async () => { const app = await App.use(); const storageDir = AppPath.storage(app.root); @@ -36,4 +45,15 @@ export namespace Storage { export const read = expose("read"); export const list = expose("list"); export const readToString = expose("readToString"); + + export async function readJSON<T>(key: string) { + const data = await readToString(key + ".json"); + return JSON.parse(data) as T; + } + + export async function writeJSON<T>(key: string, data: T) { + Bus.publish(Event.Write, { key, body: data }); + const json = JSON.stringify(data); + await write(key + ".json", json); + } } |
