diff options
| author | Dax Raad <[email protected]> | 2025-05-17 21:31:42 -0400 |
|---|---|---|
| committer | Dax Raad <[email protected]> | 2025-05-26 12:40:17 -0400 |
| commit | a34d020bc6b252e842f042d935c7a0e6444460cf (patch) | |
| tree | ea3484499dff80e82d421e879ab639133ae9c3b4 /js/src/storage | |
| parent | 96fbc37f0175052291f8a096d530bd4480f6cb19 (diff) | |
| download | opencode-a34d020bc6b252e842f042d935c7a0e6444460cf.tar.gz opencode-a34d020bc6b252e842f042d935c7a0e6444460cf.zip | |
sync
Diffstat (limited to 'js/src/storage')
| -rw-r--r-- | js/src/storage/storage.ts | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/js/src/storage/storage.ts b/js/src/storage/storage.ts new file mode 100644 index 000000000..b84e089cc --- /dev/null +++ b/js/src/storage/storage.ts @@ -0,0 +1,39 @@ +import { FileStorage } from "@flystorage/file-storage"; +import { LocalStorageAdapter } from "@flystorage/local-fs"; +import fs from "fs/promises"; +import { Log } from "../util/log"; +import { App } from "../app"; +import { AppPath } from "../app/path"; + +export namespace Storage { + const log = Log.create({ service: "storage" }); + + function state() { + return App.service("storage", async () => { + const app = await App.use(); + const storageDir = AppPath.storage(app.root); + await fs.mkdir(storageDir, { recursive: true }); + const storage = new FileStorage(new LocalStorageAdapter(storageDir)); + await storage.write("test", "test"); + log.info("created", { path: storageDir }); + return { + storage, + }; + }); + } + + function expose<T extends keyof FileStorage>(key: T) { + const fn = FileStorage.prototype[key]; + return async ( + ...args: Parameters<typeof fn> + ): Promise<ReturnType<typeof fn>> => { + const { storage } = await state(); + const match = storage[key]; + // @ts-ignore + return match.call(storage, ...args); + }; + } + + export const write = expose("write"); + export const read = expose("read"); +} |
