Skip to content

Commit

Permalink
no atomic writeJSON with a temporary file and atomic rename to desire…
Browse files Browse the repository at this point in the history
…d file

Signed-off-by: Dmitry Maslennikov <[email protected]>
  • Loading branch information
daimor committed Aug 26, 2020
1 parent b0f1fe7 commit 74e6495
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/plugin-ext/src/main/node/plugins-key-value-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class PluginsKeyValueStorage {
return {};
}
try {
return fs.readJSONSync(pathToFile);
return await fs.readJSON(pathToFile);
} catch (error) {
console.error('Failed to parse data from "', pathToFile, '". Reason:', error);
return {};
Expand All @@ -110,7 +110,16 @@ export class PluginsKeyValueStorage {

private async writeToFile(pathToFile: string, data: KeysToKeysToAnyValue): Promise<void> {
await fs.ensureDir(path.dirname(pathToFile));
fs.writeJSONSync(pathToFile, data);
const tmpFile = pathToFile + '.tmp';
await new Promise((resolve, reject) => {
fs.writeJSON(tmpFile, data, {}, error => {
if (error) {
reject(error);
} else {
fs.rename(tmpFile, pathToFile, resolve);
}
});
});
}

}

0 comments on commit 74e6495

Please sign in to comment.