-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsettings-store.ts
More file actions
182 lines (169 loc) · 5.28 KB
/
Copy pathsettings-store.ts
File metadata and controls
182 lines (169 loc) · 5.28 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import fs from "node:fs";
import path from "node:path";
import { randomUUID } from "node:crypto";
const SETTINGS_FILE_NAME = "settings.json";
const SETTINGS_VERSION = 1;
const MAX_SETTINGS_KEY_LENGTH = 256;
const MAX_SETTINGS_VALUE_LENGTH = 1_000_000;
type SettingsValues = Record<string, string>;
type SettingsPayload = {
version: number;
values: SettingsValues;
};
function isValidSetting(key: unknown, value: unknown): key is string {
return (
typeof key === "string" &&
key.length > 0 &&
key.length <= MAX_SETTINGS_KEY_LENGTH &&
typeof value === "string" &&
value.length <= MAX_SETTINGS_VALUE_LENGTH
);
}
function normalizeValues(input: unknown): SettingsValues {
if (!input || typeof input !== "object" || Array.isArray(input)) return {};
const values: SettingsValues = {};
for (const [key, value] of Object.entries(input)) {
if (isValidSetting(key, value)) {
values[key] = value;
}
}
return values;
}
type SettingsFileSystem = Pick<
typeof fs,
| "readFileSync"
| "mkdirSync"
| "writeFileSync"
| "renameSync"
| "unlinkSync"
| "openSync"
| "fsyncSync"
| "closeSync"
>;
class SettingsCommitError extends Error {
readonly committed = true;
}
function readSettingsFile(filePath: string, fileSystem: SettingsFileSystem): SettingsPayload {
try {
const raw = fileSystem.readFileSync(filePath, "utf8");
const parsed = JSON.parse(raw);
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
if (parsed.values && typeof parsed.values === "object") {
return {
version: typeof parsed.version === "number" ? parsed.version : SETTINGS_VERSION,
values: normalizeValues(parsed.values),
};
}
// Backward-compatible fallback if file ever contained flat object.
return {
version: SETTINGS_VERSION,
values: normalizeValues(parsed),
};
}
} catch {
// Ignore missing or malformed file; start fresh.
}
return { version: SETTINGS_VERSION, values: {} };
}
function writeSettingsFile(
filePath: string,
payload: SettingsPayload,
fileSystem: SettingsFileSystem,
) {
const dir = path.dirname(filePath);
fileSystem.mkdirSync(dir, { recursive: true });
const tempPath = `${filePath}.tmp-${process.pid}-${randomUUID()}`;
let replaced = false;
try {
fileSystem.writeFileSync(tempPath, `${JSON.stringify(payload, null, 2)}\n`, "utf8");
const temporaryFile = fileSystem.openSync(tempPath, "r+");
try {
fileSystem.fsyncSync(temporaryFile);
} finally {
fileSystem.closeSync(temporaryFile);
}
fileSystem.renameSync(tempPath, filePath);
replaced = true;
let directory: number | undefined;
try {
directory = fileSystem.openSync(dir, "r");
fileSystem.fsyncSync(directory);
} catch (error) {
const code = (error as NodeJS.ErrnoException).code;
if (!code || !["EISDIR", "EINVAL", "ENOTSUP", "EPERM"].includes(code)) {
throw new SettingsCommitError(
`The settings file was replaced, but directory durability could not be confirmed: ${error instanceof Error ? error.message : String(error)}`,
);
}
} finally {
if (directory !== undefined) fileSystem.closeSync(directory);
}
} catch (error) {
if (!replaced) {
try {
fileSystem.unlinkSync(tempPath);
} catch {
// The temporary file may not have been created. Preserve the write error.
}
}
throw error;
}
}
function createSettingsStore(baseDir: string, fileSystem: SettingsFileSystem = fs) {
const filePath = path.join(baseDir, SETTINGS_FILE_NAME);
let state = readSettingsFile(filePath, fileSystem);
function persist(values: SettingsValues) {
const nextState = {
version: SETTINGS_VERSION,
values: normalizeValues(values),
};
try {
writeSettingsFile(filePath, nextState, fileSystem);
state = nextState;
} catch (error) {
if (error instanceof SettingsCommitError) state = nextState;
throw error;
}
}
return {
filePath,
getAll() {
return { ...state.values };
},
get(key: string) {
if (typeof key !== "string" || key.length === 0 || key.length > MAX_SETTINGS_KEY_LENGTH)
return null;
return state.values[key] ?? null;
},
set(key: string, value: string) {
if (!isValidSetting(key, value)) return false;
persist({ ...state.values, [key]: value });
return true;
},
remove(key: string) {
if (typeof key !== "string" || key.length === 0 || key.length > MAX_SETTINGS_KEY_LENGTH)
return false;
if (!(key in state.values)) return true;
const nextValues = { ...state.values };
delete nextValues[key];
persist(nextValues);
return true;
},
merge(entries: unknown) {
if (!entries || typeof entries !== "object" || Array.isArray(entries)) {
return false;
}
let changed = false;
const nextValues = { ...state.values };
for (const [key, value] of Object.entries(entries)) {
if (!isValidSetting(key, value)) continue;
if (nextValues[key] === value) continue;
nextValues[key] = value;
changed = true;
}
if (changed) persist(nextValues);
return true;
},
};
}
export { SETTINGS_FILE_NAME, SETTINGS_VERSION, createSettingsStore };