@actual-app/api 26.7.0-nightly.20260607 → 26.7.0-nightly.20260608

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -458,21 +458,53 @@ var getModifiedTime = (filepath) => {
458
458
  var getStorePath = () => (0, path.join)(getDataDir(), "global-store.json");
459
459
  var store;
460
460
  var persisted = true;
461
+ var writeCounter = 0;
462
+ var pendingSave = Promise.resolve();
461
463
  var init$3 = function({ persist = true } = {}) {
462
- if (persist) try {
463
- store = JSON.parse(fs.readFileSync(getStorePath(), "utf8"));
464
- } catch {
465
- store = {};
466
- }
464
+ if (persist) store = loadStore(getStorePath());
467
465
  else store = {};
468
466
  persisted = persist;
469
467
  };
468
+ function loadStore(storePath) {
469
+ let contents;
470
+ try {
471
+ contents = fs.readFileSync(storePath, "utf8");
472
+ } catch (err) {
473
+ if (err?.code !== "ENOENT") logger.error("Could not read global preferences, using defaults", err);
474
+ return {};
475
+ }
476
+ try {
477
+ const parsed = JSON.parse(contents);
478
+ if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) throw new Error("Global preferences are not a JSON object");
479
+ return parsed;
480
+ } catch (err) {
481
+ const backupPath = `${storePath}.corrupt`;
482
+ try {
483
+ fs.writeFileSync(backupPath, contents, "utf8");
484
+ logger.error(`Could not parse global preferences at ${storePath}; backed up the corrupt file to ${backupPath} and started with defaults`, err);
485
+ } catch (backupErr) {
486
+ logger.error(`Could not parse global preferences at ${storePath}, and failed to back up the corrupt file; starting with defaults`, backupErr);
487
+ }
488
+ return {};
489
+ }
490
+ }
470
491
  function _saveStore() {
471
- if (persisted) return new Promise(function(resolve, reject) {
472
- fs.writeFile(getStorePath(), JSON.stringify(store), "utf8", function(err) {
473
- return err ? reject(err) : resolve();
474
- });
475
- });
492
+ if (!persisted) return Promise.resolve();
493
+ pendingSave = pendingSave.then(writeStore, writeStore);
494
+ return pendingSave;
495
+ }
496
+ async function writeStore() {
497
+ const storePath = getStorePath();
498
+ const tmpPath = `${storePath}.${process.pid}.${writeCounter++}.tmp`;
499
+ try {
500
+ await fs.promises.writeFile(tmpPath, JSON.stringify(store), "utf8");
501
+ await fs.promises.rename(tmpPath, storePath);
502
+ } catch (err) {
503
+ try {
504
+ await fs.promises.rm(tmpPath, { force: true });
505
+ } catch {}
506
+ throw err;
507
+ }
476
508
  }
477
509
  var getItem = function(key) {
478
510
  return new Promise(function(resolve) {
@@ -112092,7 +112124,6 @@ handlers$1["api/get-id-by-name"] = async function({ type, name }) {
112092
112124
  return data[0].id;
112093
112125
  };
112094
112126
  handlers$1["api/get-server-version"] = async function() {
112095
- checkFileOpen();
112096
112127
  return handlers$1["get-server-version"]();
112097
112128
  };
112098
112129
  function installAPI(serverHandlers) {