From 0e303e6508edb4374213d1f98ec383b266339774 Mon Sep 17 00:00:00 2001 From: Dax Raad Date: Sun, 18 May 2025 14:13:04 -0400 Subject: sync --- js/src/storage/storage.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'js/src/storage') 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(key: string) { + const data = await readToString(key + ".json"); + return JSON.parse(data) as T; + } + + export async function writeJSON(key: string, data: T) { + Bus.publish(Event.Write, { key, body: data }); + const json = JSON.stringify(data); + await write(key + ".json", json); + } } -- cgit v1.2.3