MCPcopy Create free account
hub / github.com/MoonshotAI/kimi-code / writePrivateFile

Function writePrivateFile

packages/server/src/services/auth/privateFiles.ts:27–53  ·  view source on GitHub ↗
(
  filePath: string,
  data: string | Buffer,
)

Source from the content-addressed store, hash-verified

25}
26
27export 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
55export async function readPrivateFile(filePath: string): Promise<Buffer> {
56 const info = await stat(filePath);

Callers 2

writeServerTokenFunction · 0.90

Calls 3

toStringMethod · 0.65
closeMethod · 0.65
mkdirFunction · 0.50

Tested by

no test coverage detected