فهرست منبع

refactor: migrate src/storage/storage.ts from Bun.file()/Bun.write() to Filesystem module (#14122)

Dax 6 ماه پیش
والد
کامیت
a624871ccd
1فایلهای تغییر یافته به همراه31 افزوده شده و 37 حذف شده
  1. 31 37
      packages/opencode/src/storage/storage.ts

+ 31 - 37
packages/opencode/src/storage/storage.ts

@@ -39,7 +39,7 @@ export namespace Storage {
             cwd: path.join(project, projectDir),
             absolute: true,
           })) {
-            const json = await Bun.file(msgFile).json()
+            const json = await Filesystem.readJson<any>(msgFile)
             worktree = json.path?.root
             if (worktree) break
           }
@@ -60,18 +60,15 @@ export namespace Storage {
           if (!id) continue
           projectID = id
 
-          await Bun.write(
-            path.join(dir, "project", projectID + ".json"),
-            JSON.stringify({
-              id,
-              vcs: "git",
-              worktree,
-              time: {
-                created: Date.now(),
-                initialized: Date.now(),
-              },
-            }),
-          )
+          await Filesystem.writeJson(path.join(dir, "project", projectID + ".json"), {
+            id,
+            vcs: "git",
+            worktree,
+            time: {
+              created: Date.now(),
+              initialized: Date.now(),
+            },
+          })
 
           log.info(`migrating sessions for project ${projectID}`)
           for await (const sessionFile of new Bun.Glob("storage/session/info/*.json").scan({
@@ -83,8 +80,8 @@ export namespace Storage {
               sessionFile,
               dest,
             })
-            const session = await Bun.file(sessionFile).json()
-            await Bun.write(dest, JSON.stringify(session))
+            const session = await Filesystem.readJson<any>(sessionFile)
+            await Filesystem.writeJson(dest, session)
             log.info(`migrating messages for session ${session.id}`)
             for await (const msgFile of new Bun.Glob(`storage/session/message/${session.id}/*.json`).scan({
               cwd: fullProjectDir,
@@ -95,8 +92,8 @@ export namespace Storage {
                 msgFile,
                 dest,
               })
-              const message = await Bun.file(msgFile).json()
-              await Bun.write(dest, JSON.stringify(message))
+              const message = await Filesystem.readJson<any>(msgFile)
+              await Filesystem.writeJson(dest, message)
 
               log.info(`migrating parts for message ${message.id}`)
               for await (const partFile of new Bun.Glob(`storage/session/part/${session.id}/${message.id}/*.json`).scan(
@@ -123,35 +120,32 @@ export namespace Storage {
         cwd: dir,
         absolute: true,
       })) {
-        const session = await Bun.file(item).json()
+        const session = await Filesystem.readJson<any>(item)
         if (!session.projectID) continue
         if (!session.summary?.diffs) continue
         const { diffs } = session.summary
-        await Bun.file(path.join(dir, "session_diff", session.id + ".json")).write(JSON.stringify(diffs))
-        await Bun.file(path.join(dir, "session", session.projectID, session.id + ".json")).write(
-          JSON.stringify({
-            ...session,
-            summary: {
-              additions: diffs.reduce((sum: any, x: any) => sum + x.additions, 0),
-              deletions: diffs.reduce((sum: any, x: any) => sum + x.deletions, 0),
-            },
-          }),
-        )
+        await Filesystem.write(path.join(dir, "session_diff", session.id + ".json"), JSON.stringify(diffs))
+        await Filesystem.writeJson(path.join(dir, "session", session.projectID, session.id + ".json"), {
+          ...session,
+          summary: {
+            additions: diffs.reduce((sum: any, x: any) => sum + x.additions, 0),
+            deletions: diffs.reduce((sum: any, x: any) => sum + x.deletions, 0),
+          },
+        })
       }
     },
   ]
 
   const state = lazy(async () => {
     const dir = path.join(Global.Path.data, "storage")
-    const migration = await Bun.file(path.join(dir, "migration"))
-      .json()
+    const migration = await Filesystem.readJson<string>(path.join(dir, "migration"))
       .then((x) => parseInt(x))
       .catch(() => 0)
     for (let index = migration; index < MIGRATIONS.length; index++) {
       log.info("running migration", { index })
       const migration = MIGRATIONS[index]
       await migration(dir).catch(() => log.error("failed to run migration", { index }))
-      await Bun.write(path.join(dir, "migration"), (index + 1).toString())
+      await Filesystem.write(path.join(dir, "migration"), (index + 1).toString())
     }
     return {
       dir,
@@ -171,7 +165,7 @@ export namespace Storage {
     const target = path.join(dir, ...key) + ".json"
     return withErrorHandling(async () => {
       using _ = await Lock.read(target)
-      const result = await Bun.file(target).json()
+      const result = await Filesystem.readJson<T>(target)
       return result as T
     })
   }
@@ -181,10 +175,10 @@ export namespace Storage {
     const target = path.join(dir, ...key) + ".json"
     return withErrorHandling(async () => {
       using _ = await Lock.write(target)
-      const content = await Bun.file(target).json()
-      fn(content)
-      await Bun.write(target, JSON.stringify(content, null, 2))
-      return content as T
+      const content = await Filesystem.readJson<T>(target)
+      fn(content as T)
+      await Filesystem.writeJson(target, content)
+      return content
     })
   }
 
@@ -193,7 +187,7 @@ export namespace Storage {
     const target = path.join(dir, ...key) + ".json"
     return withErrorHandling(async () => {
       using _ = await Lock.write(target)
-      await Bun.write(target, JSON.stringify(content, null, 2))
+      await Filesystem.writeJson(target, content)
     })
   }