GHHG10's picture
download
raw
1.13 kB
import path from "path"
import { appendFile, mkdir, rename, rm } from "fs/promises"
export function readText(filePath: string) {
return Bun.file(filePath).text()
}
export function readJson<T>(filePath: string) {
return Bun.file(filePath).json() as Promise<T>
}
export async function writeText(filePath: string, content: string) {
await mkdir(path.dirname(filePath), { recursive: true })
await Bun.write(filePath, content)
}
export async function appendText(filePath: string, content: string) {
await mkdir(path.dirname(filePath), { recursive: true })
await appendFile(filePath, content)
}
export async function writeJsonAtomic(filePath: string, value: unknown) {
await mkdir(path.dirname(filePath), { recursive: true })
const temporary = `${filePath}.${process.pid}.${crypto.randomUUID()}.tmp`
await Bun.write(temporary, JSON.stringify(value)).catch(async (error) => {
await rm(temporary, { force: true }).catch(() => undefined)
throw error
})
await rename(temporary, filePath).catch(async (error) => {
await rm(temporary, { force: true }).catch(() => undefined)
throw error
})
}

Xet Storage Details

Size:
1.13 kB
·
Xet hash:
c933a13dacf34d69a740041da3efa98d3b4cd32550f34c6b1d7d059e4d9a5431

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.