Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | 1x 1x 1x 1x 1x 88x 88x 88x 88x 88x 88x 88x 88x 88x 86x 86x 86x 84x 82x 88x 88x 86x 86x 86x 86x 88x 1x 84x 84x 84x 83x 83x 83x 83x 83x 83x 83x 83x 84x 1x 89x 89x 89x 89x 89x 89x 89x 89x 88x 88x 89x 89x 3x 3x 87x 86x 86x 86x 81x 81x 81x 81x 81x 87x 18x 89x 87x 86x 86x 89x | import { constants } from "node:fs"
import { lstat, open, link, rename, unlink } from "node:fs/promises"
import { dirname } from "node:path"
import { randomUUID } from "node:crypto"
export async function readPrivateJson(
path: string,
maxBytes: number
): Promise<unknown> {
const handle = await open(
path,
constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK
)
try {
const stat = await handle.stat()
if (
!stat.isFile() ||
stat.size > maxBytes ||
(process.platform !== "win32" && (stat.mode & 0o077) !== 0)
)
throw new Error("State must be a private regular file.")
return JSON.parse(await handle.readFile("utf8"))
} finally {
await handle.close()
}
}
export async function syncPrivateDirectory(path: string): Promise<void> {
if (process.platform === "win32") return
const parent = await open(
dirname(path),
constants.O_RDONLY | constants.O_NOFOLLOW
)
try {
await parent.sync()
} finally {
await parent.close()
}
}
export async function writePrivateJson(
path: string,
value: unknown,
overwrite = true
): Promise<void> {
const directory = await lstat(dirname(path))
if (
!directory.isDirectory() ||
directory.isSymbolicLink() ||
(process.platform !== "win32" && (directory.mode & 0o077) !== 0)
)
throw new Error(
"State directory must be private and must not be a symlink."
)
const temporary = `${path}.${randomUUID()}.tmp`
try {
const handle = await open(temporary, "wx", 0o600)
try {
await handle.writeFile(JSON.stringify(value))
await handle.sync()
} finally {
await handle.close()
}
if (overwrite) await rename(temporary, path)
else await link(temporary, path)
await syncPrivateDirectory(path)
} finally {
await unlink(temporary).catch(() => undefined)
}
}
|