122 lines
3.9 KiB
TypeScript
122 lines
3.9 KiB
TypeScript
|
|
// File storage that survives power cuts. Every write goes to a temporary
|
||
|
|
// file, is flushed to disk, and only then replaces the real file; the last
|
||
|
|
// good version stays next to it as "<file>.bak". Reading falls back to the
|
||
|
|
// .bak when the file is missing or not valid JSON. Same file format as
|
||
|
|
// Nitro's built-in "fs" driver, so existing data reads unchanged.
|
||
|
|
|
||
|
|
import { existsSync } from 'node:fs'
|
||
|
|
import { mkdir, open, readdir, readFile, rename, rm, unlink } from 'node:fs/promises'
|
||
|
|
import { dirname, join, relative, resolve, sep } from 'node:path'
|
||
|
|
|
||
|
|
const TMP = '.tmp-'
|
||
|
|
|
||
|
|
function validJson(text: string): boolean {
|
||
|
|
try {
|
||
|
|
JSON.parse(text)
|
||
|
|
return true
|
||
|
|
} catch {
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async function syncDir(dir: string) {
|
||
|
|
try {
|
||
|
|
const fh = await open(dir, 'r')
|
||
|
|
try {
|
||
|
|
await fh.sync()
|
||
|
|
} finally {
|
||
|
|
await fh.close()
|
||
|
|
}
|
||
|
|
} catch {
|
||
|
|
// Not every file system can sync a directory; the file itself is synced.
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Writes `data` to `file` so that a crash at any moment leaves either the old or the new content. */
|
||
|
|
export async function atomicWrite(file: string, data: string) {
|
||
|
|
const dir = dirname(file)
|
||
|
|
await mkdir(dir, { recursive: true })
|
||
|
|
const tmp = `${file}${TMP}${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`
|
||
|
|
const fh = await open(tmp, 'w')
|
||
|
|
try {
|
||
|
|
await fh.writeFile(data, 'utf8')
|
||
|
|
await fh.sync()
|
||
|
|
} finally {
|
||
|
|
await fh.close()
|
||
|
|
}
|
||
|
|
// Keep the current version as the backup, but only if it is intact.
|
||
|
|
const current = await readFile(file, 'utf8').catch(() => null)
|
||
|
|
if (current !== null && validJson(current)) await rename(file, `${file}.bak`)
|
||
|
|
await rename(tmp, file)
|
||
|
|
await syncDir(dir)
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Reads a JSON file, falling back to its .bak when it is missing or damaged. */
|
||
|
|
export async function safeRead(file: string): Promise<string | null> {
|
||
|
|
for (const f of [file, `${file}.bak`]) {
|
||
|
|
const text = await readFile(f, 'utf8').catch(() => null)
|
||
|
|
if (text !== null && validJson(text)) return text
|
||
|
|
}
|
||
|
|
return null
|
||
|
|
}
|
||
|
|
|
||
|
|
async function walk(dir: string, out: string[]) {
|
||
|
|
const entries = await readdir(dir, { withFileTypes: true }).catch(() => [])
|
||
|
|
for (const e of entries) {
|
||
|
|
const p = join(dir, e.name)
|
||
|
|
if (e.isDirectory()) await walk(p, out)
|
||
|
|
else out.push(p)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Removes temporary files left behind by writes that a crash interrupted. */
|
||
|
|
export async function removeStaleTemp(base: string) {
|
||
|
|
const files: string[] = []
|
||
|
|
await walk(base, files)
|
||
|
|
for (const f of files) if (f.includes(TMP)) await unlink(f).catch(() => {})
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Storage driver for Nitro's useStorage(), mounted in server/plugins/storage.ts. */
|
||
|
|
export function safeFsDriver(opts: { base: string }) {
|
||
|
|
const base = resolve(opts.base)
|
||
|
|
const path = (key: string) => {
|
||
|
|
if (/(^|:)\.\.(:|$)/.test(key)) throw new Error(`Invalid storage key ${JSON.stringify(key)}`)
|
||
|
|
return join(base, key.replace(/:/g, '/'))
|
||
|
|
}
|
||
|
|
return {
|
||
|
|
name: 'safe-fs',
|
||
|
|
options: opts,
|
||
|
|
async hasItem(key: string) {
|
||
|
|
return (await safeRead(path(key))) !== null
|
||
|
|
},
|
||
|
|
getItem(key: string) {
|
||
|
|
return safeRead(path(key))
|
||
|
|
},
|
||
|
|
setItem(key: string, value: string) {
|
||
|
|
return atomicWrite(path(key), value)
|
||
|
|
},
|
||
|
|
async removeItem(key: string) {
|
||
|
|
const p = path(key)
|
||
|
|
await unlink(p).catch(() => {})
|
||
|
|
await unlink(`${p}.bak`).catch(() => {})
|
||
|
|
await syncDir(dirname(p))
|
||
|
|
},
|
||
|
|
async getKeys(prefix?: string) {
|
||
|
|
// Only walk the folder the prefix points at, not the whole store.
|
||
|
|
const dir = prefix ? join(base, prefix.replace(/:$/, '').replace(/:/g, '/')) : base
|
||
|
|
const files: string[] = []
|
||
|
|
await walk(dir, files)
|
||
|
|
const keys = new Set<string>()
|
||
|
|
for (const f of files) {
|
||
|
|
if (f.includes(TMP)) continue
|
||
|
|
const real = f.endsWith('.bak') ? f.slice(0, -4) : f
|
||
|
|
keys.add(relative(base, real).split(sep).join(':'))
|
||
|
|
}
|
||
|
|
return [...keys]
|
||
|
|
},
|
||
|
|
async clear() {
|
||
|
|
await rm(base, { recursive: true, force: true })
|
||
|
|
},
|
||
|
|
}
|
||
|
|
}
|