( filePath: string, data: string | Buffer, )
| 25 | } |
| 26 | |
| 27 | export async function writePrivateFile( |
| 28 | filePath: string, |
| 29 | data: string | Buffer, |
| 30 | ): Promise<void> { |
| 31 | const dir = dirname(filePath); |
| 32 | await mkdir(dir, { recursive: true, mode: 0o700 }); |
| 33 | await chmod(dir, 0o700); |
| 34 | |
| 35 | const tmp = `${filePath}.tmp.${randomBytes(8).toString('hex')}`; |
| 36 | |
| 37 | let handle: Awaited<ReturnType<typeof open>> | undefined; |
| 38 | try { |
| 39 | handle = await open(tmp, 'w', 0o600); |
| 40 | await handle.chmod(0o600); |
| 41 | await handle.writeFile(data); |
| 42 | await handle.sync(); |
| 43 | await handle.close(); |
| 44 | handle = undefined; |
| 45 | await rename(tmp, filePath); |
| 46 | } catch (err) { |
| 47 | if (handle) { |
| 48 | await handle.close().catch(() => {}); |
| 49 | } |
| 50 | await rm(tmp, { force: true }).catch(() => {}); |
| 51 | throw err; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | export async function readPrivateFile(filePath: string): Promise<Buffer> { |
| 56 | const info = await stat(filePath); |
no test coverage detected