15 lines
489 B
TypeScript
15 lines
489 B
TypeScript
import { closeSync, fchmodSync, mkdirSync, openSync, writeFileSync } from "node:fs"
|
|
import { dirname } from "node:path"
|
|
|
|
/** Tighten an existing credential file before writing any new secret bytes. */
|
|
export function writeSecureFile(path: string, content: string | Uint8Array): void {
|
|
mkdirSync(dirname(path), { recursive: true, mode: 0o700 })
|
|
const fd = openSync(path, "w", 0o600)
|
|
try {
|
|
fchmodSync(fd, 0o600)
|
|
writeFileSync(fd, content)
|
|
} finally {
|
|
closeSync(fd)
|
|
}
|
|
}
|