-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathpersistence.ts
More file actions
74 lines (62 loc) · 1.93 KB
/
persistence.ts
File metadata and controls
74 lines (62 loc) · 1.93 KB
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
72
73
74
/**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import process from 'node:process';
import {logger} from '../logger.js';
export interface LocalState {
lastActive: string; // ISO 8601 UTC date string
}
const STATE_FILE_NAME = 'telemetry_state.json';
function getDataFolder(): string {
const homedir = os.homedir();
const {env} = process;
const name = 'chrome-devtools-mcp';
if (process.platform === 'darwin') {
return path.join(homedir, 'Library', 'Application Support', name);
}
if (process.platform === 'win32') {
const localAppData =
env.LOCALAPPDATA || path.join(homedir, 'AppData', 'Local');
return path.join(localAppData, name, 'Data');
}
return path.join(
env.XDG_DATA_HOME || path.join(homedir, '.local', 'share'),
name,
);
}
export interface Persistence {
loadState(): Promise<LocalState>;
saveState(state: LocalState): Promise<void>;
}
export class FilePersistence implements Persistence {
#dataFolder: string;
constructor(dataFolderOverride?: string) {
this.#dataFolder = dataFolderOverride ?? getDataFolder();
}
async loadState(): Promise<LocalState> {
try {
const filePath = path.join(this.#dataFolder, STATE_FILE_NAME);
const content = await fs.readFile(filePath, 'utf-8');
return JSON.parse(content) as LocalState;
} catch {
return {
lastActive: '',
};
}
}
async saveState(state: LocalState): Promise<void> {
const filePath = path.join(this.#dataFolder, STATE_FILE_NAME);
try {
await fs.mkdir(this.#dataFolder, {recursive: true});
await fs.writeFile(filePath, JSON.stringify(state, null, 2), 'utf-8');
} catch (error) {
// Ignore errors during state saving to avoid crashing the server
logger(`Failed to save telemetry state to ${filePath}:`, error);
}
}
}