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

Class JsonFileStore

packages/agent-core/src/mcp/oauth/store.ts:66–130  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

64}
65
66export class JsonFileStore {
67 private readonly dir: string;
68
69 constructor(dir: string = defaultMcpCredentialsDir()) {
70 this.dir = dir;
71 }
72
73 read<T>(file: string): T | undefined {
74 const path = join(this.dir, file);
75 let raw: string;
76 try {
77 raw = readFileSync(path, 'utf-8');
78 } catch {
79 return undefined;
80 }
81 try {
82 return JSON.parse(raw) as T;
83 } catch {
84 return undefined;
85 }
86 }
87
88 write(file: string, data: unknown): void {
89 mkdirSync(this.dir, { recursive: true, mode: 0o700 });
90 try {
91 chmodSync(this.dir, 0o700);
92 } catch {
93 // best-effort; Windows / read-only FS may refuse
94 }
95 const target = join(this.dir, file);
96 const tmp = `${target}.tmp.${process.pid}.${randomBytes(4).toString('hex')}`;
97 const buf = Buffer.from(`${JSON.stringify(data, null, 2)}\n`, 'utf-8');
98 const fd = openSync(tmp, 'w', 0o600);
99 try {
100 let written = 0;
101 while (written < buf.length) {
102 written += writeSync(fd, buf, written, buf.length - written);
103 }
104 fsyncSync(fd);
105 } finally {
106 closeSync(fd);
107 }
108 try {
109 chmodSync(tmp, 0o600);
110 renameSync(tmp, target);
111 } catch (error) {
112 try {
113 unlinkSync(tmp);
114 } catch {
115 /* ignore */
116 }
117 throw error;
118 }
119 }
120
121 remove(file: string): void {
122 try {
123 unlinkSync(join(this.dir, file));

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected