fix(storage): migrate filesystem operations to smartfs and tighten TypeScript initialization checks

This commit is contained in:
2026-04-30 10:12:32 +00:00
parent 1c4ba7a7d0
commit 6084ffb0ce
9 changed files with 2827 additions and 3656 deletions
+17 -12
View File
@@ -26,21 +26,18 @@ export class KeyValueStore<T = any> {
name: 'syncTask',
buffered: true,
bufferMax: 1,
execDelay: 0,
taskFunction: async () => {
if (this.type !== 'ephemeral') {
const storedJson = await plugins.smartFs.file(this.filePath!).encoding('utf8').read() as string;
this.dataObject = {
...plugins.smartfile.fs.toObjectSync(this.filePath),
...plugins.smartjson.parse(storedJson || '{}'),
...this.dataObject,
};
for (const key of Object.keys(this.deletedObject) as Array<keyof T>) {
delete this.dataObject[key];
}
this.deletedObject = {};
await plugins.smartfile.memory.toFs(
plugins.smartjson.stringifyPretty(this.dataObject),
this.filePath,
);
await plugins.smartFs.file(this.filePath!).encoding('utf8').write(plugins.smartjson.stringifyPretty(this.dataObject));
}
const newStateString = plugins.smartjson.stringify(this.dataObject);
@@ -67,13 +64,16 @@ export class KeyValueStore<T = any> {
paths.cwd,
);
this.filePath = absolutePath;
if (plugins.smartfile.fs.isDirectorySync(this.filePath)) {
if (plugins.nodeFs.existsSync(this.filePath) && plugins.nodeFs.statSync(this.filePath).isDirectory()) {
this.filePath = plugins.path.join(
this.filePath,
this.identity + '.json',
);
}
plugins.smartfile.fs.ensureFileSync(this.filePath, '{}');
plugins.nodeFs.mkdirSync(plugins.path.dirname(this.filePath), { recursive: true });
if (!plugins.nodeFs.existsSync(this.filePath)) {
plugins.nodeFs.writeFileSync(this.filePath, '{}');
}
return;
}
@@ -84,8 +84,10 @@ export class KeyValueStore<T = any> {
throw new Error('kv type not supported');
}
this.filePath = plugins.path.join(baseDir, this.identity + '.json');
plugins.smartfile.fs.ensureDirSync(baseDir);
plugins.smartfile.fs.ensureFileSync(this.filePath, '{}');
plugins.nodeFs.mkdirSync(baseDir, { recursive: true });
if (!plugins.nodeFs.existsSync(this.filePath)) {
plugins.nodeFs.writeFileSync(this.filePath, '{}');
}
};
// if no custom path is provided, try to store at home directory
@@ -162,8 +164,11 @@ export class KeyValueStore<T = any> {
*/
public async wipe(): Promise<void> {
this.dataObject = {};
if (this.type !== 'ephemeral') {
await plugins.smartfile.fs.remove(this.filePath);
if (this.type !== 'ephemeral' && this.filePath) {
const file = plugins.smartFs.file(this.filePath);
if (await file.exists()) {
await file.delete();
}
}
}