fix bunfile bug

This commit is contained in:
Dax Raad
2025-06-10 18:23:19 -04:00
parent 49110f7412
commit ca3c22dc12
6 changed files with 18 additions and 16 deletions

View File

@@ -6,7 +6,7 @@ import fs from "fs/promises"
export namespace AuthAnthropic {
const CLIENT_ID = "9d1c250a-e61b-44d9-88ed-5944d1962f5e"
const file = Bun.file(path.join(Global.Path.data, "auth", "anthropic.json"))
const filepath = path.join(Global.Path.data, "auth", "anthropic.json")
export async function authorize() {
const pkce = await generatePKCE()
@@ -48,15 +48,15 @@ export namespace AuthAnthropic {
}),
})
if (!result.ok) throw new ExchangeFailed()
const file = Bun.file(filepath)
await Bun.write(file, result)
await fs.chmod(file.name!, 0o600)
}
export const exists = file.exists
export async function access() {
if (!(await file.exists())) return
const result = await file.json()
const file = Bun.file(filepath)
const result = await file.json().catch(() => ({}))
if (!result) return
const refresh = result.refresh_token
const response = await fetch(
"https://console.anthropic.com/v1/oauth/token",

View File

@@ -3,9 +3,10 @@ import { Global } from "../global"
import fs from "fs/promises"
export namespace AuthKeys {
const file = Bun.file(path.join(Global.Path.data, "auth", "keys.json"))
const filepath = path.join(Global.Path.data, "auth", "keys.json")
export async function get() {
const file = Bun.file(filepath)
return file
.json()
.catch(() => ({}))
@@ -13,6 +14,7 @@ export namespace AuthKeys {
}
export async function set(key: string, value: string) {
const file = Bun.file(filepath)
const env = await get()
await Bun.write(file, JSON.stringify({ ...env, [key]: value }))
await fs.chmod(file.name!, 0o600)