@sv443-network/coreutils 2.0.1 → 2.0.3

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.
@@ -478,16 +478,18 @@ var DataStore = class {
478
478
  this.engine = typeof opts.engine === "function" ? opts.engine() : opts.engine;
479
479
  this.options = opts;
480
480
  if (typeof opts.compressionFormat === "undefined")
481
- opts.compressionFormat = ((_a = opts.encodeData) == null ? void 0 : _a[0]) ?? "deflate-raw";
481
+ this.compressionFormat = opts.compressionFormat = ((_a = opts.encodeData) == null ? void 0 : _a[0]) ?? "deflate-raw";
482
482
  if (typeof opts.compressionFormat === "string") {
483
483
  this.encodeData = [opts.compressionFormat, async (data) => await compress(data, opts.compressionFormat, "string")];
484
484
  this.decodeData = [opts.compressionFormat, async (data) => await compress(data, opts.compressionFormat, "string")];
485
485
  } else if ("encodeData" in opts && "decodeData" in opts && Array.isArray(opts.encodeData) && Array.isArray(opts.decodeData)) {
486
486
  this.encodeData = [opts.encodeData[0], opts.encodeData[1]];
487
487
  this.decodeData = [opts.decodeData[0], opts.decodeData[1]];
488
+ this.compressionFormat = opts.encodeData[0] ?? null;
488
489
  } else if (opts.compressionFormat === null) {
489
490
  this.encodeData = void 0;
490
491
  this.decodeData = void 0;
492
+ this.compressionFormat = null;
491
493
  } else
492
494
  throw new TypeError("Either `compressionFormat` or `encodeData` and `decodeData` have to be set and valid, but not all three at a time. Please refer to the documentation for more info.");
493
495
  this.engine.setDataStoreOptions(opts);
@@ -530,12 +532,13 @@ var DataStore = class {
530
532
  await this.migrateId(this.migrateIds);
531
533
  this.migrateIds = [];
532
534
  }
533
- const storedData = await this.engine.getValue(`__ds-${this.id}-dat`, JSON.stringify(this.defaultData));
535
+ const storedDataRaw = await this.engine.getValue(`__ds-${this.id}-dat`, null);
534
536
  let storedFmtVer = Number(await this.engine.getValue(`__ds-${this.id}-ver`, NaN));
535
- if (typeof storedData !== "string") {
537
+ if (typeof storedDataRaw !== "string") {
536
538
  await this.saveDefaultData();
537
539
  return { ...this.defaultData };
538
540
  }
541
+ const storedData = storedDataRaw ?? JSON.stringify(this.defaultData);
539
542
  const encodingFmt = String(await this.engine.getValue(`__ds-${this.id}-enf`, null));
540
543
  const isEncoded = encodingFmt !== "null" && encodingFmt !== "false";
541
544
  let saveData = false;
@@ -740,7 +743,8 @@ var BrowserStorageEngine = class extends DataStoreEngine {
740
743
  //#region storage api
741
744
  /** Fetches a value from persistent storage */
742
745
  async getValue(name, defaultValue) {
743
- return (this.options.type === "localStorage" ? globalThis.localStorage.getItem(name) : globalThis.sessionStorage.getItem(name)) ?? defaultValue;
746
+ const val = this.options.type === "localStorage" ? globalThis.localStorage.getItem(name) : globalThis.sessionStorage.getItem(name);
747
+ return typeof val === "undefined" ? defaultValue : val;
744
748
  }
745
749
  /** Sets a value in persistent storage */
746
750
  async setValue(name, value) {
@@ -760,6 +764,7 @@ var BrowserStorageEngine = class extends DataStoreEngine {
760
764
  var fs;
761
765
  var FileStorageEngine = class extends DataStoreEngine {
762
766
  options;
767
+ fileAccessQueue = Promise.resolve();
763
768
  /**
764
769
  * Creates an instance of `FileStorageEngine`.
765
770
  *
@@ -813,27 +818,41 @@ var FileStorageEngine = class extends DataStoreEngine {
813
818
  if (!data)
814
819
  return defaultValue;
815
820
  const value = data == null ? void 0 : data[name];
816
- if (value === void 0)
821
+ if (typeof value === "undefined")
817
822
  return defaultValue;
818
823
  if (typeof value === "string")
819
824
  return value;
820
- return String(value ?? defaultValue);
825
+ return value;
821
826
  }
822
827
  /** Sets a value in persistent storage */
823
828
  async setValue(name, value) {
824
- let data = await this.readFile();
825
- if (!data)
826
- data = {};
827
- data[name] = value;
828
- await this.writeFile(data);
829
+ this.fileAccessQueue = this.fileAccessQueue.then(async () => {
830
+ let data = await this.readFile();
831
+ if (!data)
832
+ data = {};
833
+ data[name] = value;
834
+ await this.writeFile(data);
835
+ }).catch((err) => {
836
+ console.error("Error in setValue:", err);
837
+ throw err;
838
+ });
839
+ await this.fileAccessQueue.catch(() => {
840
+ });
829
841
  }
830
842
  /** Deletes a value from persistent storage */
831
843
  async deleteValue(name) {
832
- const data = await this.readFile();
833
- if (!data)
834
- return;
835
- delete data[name];
836
- await this.writeFile(data);
844
+ this.fileAccessQueue = this.fileAccessQueue.then(async () => {
845
+ const data = await this.readFile();
846
+ if (!data)
847
+ return;
848
+ delete data[name];
849
+ await this.writeFile(data);
850
+ }).catch((err) => {
851
+ console.error("Error in deleteValue:", err);
852
+ throw err;
853
+ });
854
+ await this.fileAccessQueue.catch(() => {
855
+ });
837
856
  }
838
857
  /** Deletes the file that contains the data of this DataStore. */
839
858
  async deleteStorage() {
@@ -1066,11 +1085,11 @@ var NanoEmitter = class {
1066
1085
  once(event, cb) {
1067
1086
  return new Promise((resolve) => {
1068
1087
  let unsub;
1069
- const onceProxy = (...args) => {
1088
+ const onceProxy = ((...args) => {
1070
1089
  cb == null ? void 0 : cb(...args);
1071
1090
  unsub == null ? void 0 : unsub();
1072
1091
  resolve(args);
1073
- };
1092
+ });
1074
1093
  unsub = this.events.on(event, onceProxy);
1075
1094
  this.eventUnsubscribes.push(unsub);
1076
1095
  });
@@ -1124,12 +1143,12 @@ var NanoEmitter = class {
1124
1143
  this.eventUnsubscribes = this.eventUnsubscribes.filter((u) => !curEvtUnsubs.includes(u));
1125
1144
  };
1126
1145
  for (const event of oneOf) {
1127
- const unsub = this.events.on(event, (...args) => {
1146
+ const unsub = this.events.on(event, ((...args) => {
1128
1147
  checkUnsubAllEvt();
1129
1148
  callback(event, ...args);
1130
1149
  if (once)
1131
1150
  checkUnsubAllEvt(true);
1132
- });
1151
+ }));
1133
1152
  curEvtUnsubs.push(unsub);
1134
1153
  }
1135
1154
  const allOfEmitted = /* @__PURE__ */ new Set();
@@ -1143,10 +1162,10 @@ var NanoEmitter = class {
1143
1162
  }
1144
1163
  };
1145
1164
  for (const event of allOf) {
1146
- const unsub = this.events.on(event, (...args) => {
1165
+ const unsub = this.events.on(event, ((...args) => {
1147
1166
  checkUnsubAllEvt();
1148
1167
  checkAllOf(event, ...args);
1149
- });
1168
+ }));
1150
1169
  curEvtUnsubs.push(unsub);
1151
1170
  }
1152
1171
  if (oneOf.length === 0 && allOf.length === 0)
@@ -1278,7 +1297,7 @@ var Debouncer = class extends NanoEmitter {
1278
1297
  function debounce(fn, timeout = 200, type = "immediate") {
1279
1298
  const debouncer = new Debouncer(timeout, type);
1280
1299
  debouncer.addListener(fn);
1281
- const func = (...args) => debouncer.call(...args);
1300
+ const func = ((...args) => debouncer.call(...args));
1282
1301
  func.debouncer = debouncer;
1283
1302
  return func;
1284
1303
  }