|
1 | 1 | import path from "path" |
2 | 2 | import { Global } from "../global" |
| 3 | +import fs from "fs/promises" |
3 | 4 | import z from "zod" |
| 5 | +import { Lock } from "../util/lock" |
4 | 6 |
|
5 | 7 | export const OAUTH_DUMMY_KEY = "opencode-oauth-dummy-key" |
6 | 8 |
|
@@ -42,29 +44,75 @@ export namespace Auth { |
42 | 44 | } |
43 | 45 |
|
44 | 46 | export async function all(): Promise<Record<string, Info>> { |
45 | | - const file = Bun.file(filepath) |
46 | | - const data = await file.json().catch(() => ({}) as Record<string, unknown>) |
47 | | - return Object.entries(data).reduce( |
48 | | - (acc, [key, value]) => { |
49 | | - const parsed = Info.safeParse(value) |
50 | | - if (!parsed.success) return acc |
51 | | - acc[key] = parsed.data |
52 | | - return acc |
53 | | - }, |
54 | | - {} as Record<string, Info>, |
55 | | - ) |
| 47 | + const release = await Lock.read("auth") |
| 48 | + try { |
| 49 | + const file = Bun.file(filepath) |
| 50 | + |
| 51 | + if (!(await file.exists())) return {} |
| 52 | + |
| 53 | + const data = await file.json() |
| 54 | + |
| 55 | + if (typeof data !== "object" || data === null) { |
| 56 | + throw new Error("auth.json contains invalid data") |
| 57 | + } |
| 58 | + |
| 59 | + return Object.entries(data).reduce( |
| 60 | + (acc, [key, value]) => { |
| 61 | + const parsed = Info.safeParse(value) |
| 62 | + if (!parsed.success) return acc |
| 63 | + acc[key] = parsed.data |
| 64 | + return acc |
| 65 | + }, |
| 66 | + {} as Record<string, Info>, |
| 67 | + ) |
| 68 | + } finally { |
| 69 | + release[Symbol.dispose]() |
| 70 | + } |
56 | 71 | } |
57 | 72 |
|
58 | 73 | export async function set(key: string, info: Info) { |
59 | | - const file = Bun.file(filepath) |
60 | | - const data = await all() |
61 | | - await Bun.write(file, JSON.stringify({ ...data, [key]: info }, null, 2), { mode: 0o600 }) |
| 74 | + const release = await Lock.write("auth") |
| 75 | + try { |
| 76 | + const file = Bun.file(filepath) |
| 77 | + const exists = await file.exists() |
| 78 | + const rawData = exists ? await file.json() : null |
| 79 | + const data: Record<string, Info> = {} |
| 80 | + |
| 81 | + if (typeof rawData === "object" && rawData !== null) { |
| 82 | + Object.entries(rawData).forEach(([k, v]) => { |
| 83 | + const parsed = Info.safeParse(v) |
| 84 | + if (parsed.success) data[k] = parsed.data |
| 85 | + }) |
| 86 | + } |
| 87 | + |
| 88 | + data[key] = info |
| 89 | + await Bun.write(file, JSON.stringify(data, null, 2)) |
| 90 | + await fs.chmod(filepath, 0o600) |
| 91 | + } finally { |
| 92 | + release[Symbol.dispose]() |
| 93 | + } |
62 | 94 | } |
63 | 95 |
|
64 | 96 | export async function remove(key: string) { |
65 | | - const file = Bun.file(filepath) |
66 | | - const data = await all() |
67 | | - delete data[key] |
68 | | - await Bun.write(file, JSON.stringify(data, null, 2), { mode: 0o600 }) |
| 97 | + const release = await Lock.write("auth") |
| 98 | + try { |
| 99 | + const file = Bun.file(filepath) |
| 100 | + const exists = await file.exists() |
| 101 | + const rawData = exists ? await file.json() : null |
| 102 | + const data: Record<string, Info> = {} |
| 103 | + |
| 104 | + if (typeof rawData === "object" && rawData !== null) { |
| 105 | + Object.entries(rawData).forEach(([k, v]) => { |
| 106 | + const parsed = Info.safeParse(v) |
| 107 | + if (parsed.success) data[k] = parsed.data |
| 108 | + }) |
| 109 | + } |
| 110 | + |
| 111 | + delete data[key] |
| 112 | + await Bun.write(file, JSON.stringify(data, null, 2)) |
| 113 | + await fs.chmod(filepath, 0o600) |
| 114 | + } finally { |
| 115 | + release[Symbol.dispose]() |
| 116 | + } |
69 | 117 | } |
70 | 118 | } |
0 commit comments