@sv443-network/coreutils 2.0.3 → 3.0.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,44 @@
1
1
  # @sv443-network/coreutils
2
2
 
3
+ ## 3.0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - d6843f9: Fixed underlying `fetch()` props in `fetchAdvanced()` in stricter environments.
8
+ - 27e60dd: Made the type `DataStoreData` much less constrained. This way it won't require an index signature anymore.
9
+
10
+ ## 3.0.0
11
+
12
+ ### Major Changes
13
+
14
+ - 7718855: **Changed `DataStoreEngine` instances to allow storing original values instead of only strings.**
15
+
16
+ The previous behavior of explicit serialization was prone to errors and made it hard to implement certain features, such as data migration from UserUtils.
17
+ If you have created a custom `DataStoreEngine`, please ensure it supports storing and retrieving all values supported by JSON, i.e. `string`, `number`, `boolean`, `null`, `object` and `array`. Values like `undefined`, `Symbol`, `Function` etc. are still not supported and will lead to errors.
18
+
19
+ - 46570f4: **`NanoEmitter` multi methods' `oneOf` and `allOf` properties now behave like an AND condition instead of an OR.**
20
+
21
+ ### Minor Changes
22
+
23
+ - 1124d2e: Added DataStoreSerializer property `remapIds` to support deserializing from stores with outdated IDs.
24
+ - 240f83e: Added DataStore prop `memoryCache` to turn off the memory cache in data-intensive, non-latency-sensitive scenarios.
25
+ - 7718855: Made DataStore able to migrate data from [UserUtils <=v9 DataStore](https://github.com/Sv443-Network/UserUtils/blob/v9.4.4/docs.md#datastore) instances.
26
+
27
+ In order to trigger the migration:
28
+
29
+ 1. Switch the DataStore import from UserUtils to CoreUtils and keep the same DataStore ID.
30
+ 2. Update the options object in the DataStore constructor. (You may also want to refer to the [new DataStore documentation](https://github.com/Sv443-Network/CoreUtils/blob/main/docs.md#class-datastore).)
31
+ - The constructor now needs an `engine` property that is an instance of a [UserUtils `GMStorageEngine`.](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#class-gmstorageengine)
32
+ - Encoding with `deflate-raw` will now be enabled by default. Set `compressionFormat: null` to disable compression if it wasn't enabled in the UserUtils DataStore.
33
+ - Added shorthand property `compressionFormat` as an alternative to the properties `encodeData` and `decodeData`
34
+ - `encodeData` and `decodeData` are now a tuple array, consisting of a format identifier string and the function which was previously the only value of these properties.
35
+ 3. The next call to `loadData()` will then migrate the data automatically and transparently.
36
+
37
+ ### Patch Changes
38
+
39
+ - 99a797f: `secsToTimeStr()` now supports negative time and will only throw if the number is `NaN` or not finite.
40
+ - 38e7813: Implemented `DatedError`, `CustomError` and new classes `ScriptContextError` and `NetworkError` throughout the library.
41
+
3
42
  ## 2.0.3
4
43
 
5
44
  ### Patch Changes
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  <div align="center" style="text-align: center;">
2
2
 
3
3
  # CoreUtils
4
- Cross-platform, general-purpose, JavaScript core library for Node, Deno and the browser.
4
+ Cross-platform, general-purpose, JavaScript core library for Node, Deno and the browser with tons of various utility functions and classes.
5
5
  Intended to be used in conjunction with [`@sv443-network/userutils`](https://github.com/Sv443-Network/UserUtils) and [`@sv443-network/djsutils`](https://github.com/Sv443-Network/DJSUtils), but can be used independently as well.
6
6
 
7
7
  ### [Documentation](./docs.md#readme) &bull; [Features](#features) &bull; [Installation](#installation) &bull; [License](./LICENSE.txt) &bull; [Changelog](./CHANGELOG.md)
@@ -56,6 +56,7 @@ Intended to be used in conjunction with [`@sv443-network/userutils`](https://git
56
56
  - [**Errors:**](./docs.md#errors)
57
57
  - 🟧 [`class DatedError`](./docs.md#class-datederror) - Base error class with a `date` property
58
58
  - 🟧 [`class ChecksumMismatchError`](./docs.md#class-checksummismatcherror) - Error thrown when two checksums don't match
59
+ - 🟧 [`class CustomError`](./docs.md#class-customerror) - Custom error with a configurable name for one-off situations
59
60
  - 🟧 [`class MigrationError`](./docs.md#class-migrationerror) - Error thrown in a failed data migration
60
61
  - 🟧 [`class ValidationError`](./docs.md#class-validationerror) - Error while validating data
61
62
  - [**Math:**](./docs.md#math)
@@ -90,7 +91,7 @@ Intended to be used in conjunction with [`@sv443-network/userutils`](https://git
90
91
  - 🟣 [`function autoPlural()`](./docs.md#function-autoplural) - Turns the given term into its plural form, depending on the given number or list length
91
92
  - 🟣 [`function capitalize()`](./docs.md#function-capitalize) - Capitalizes the first letter of the given string
92
93
  - 🟣 [`function createProgressBar()`](./docs.md#function-createprogressbar) - Creates a progress bar string with the given percentage and length
93
- - 🟩 [`const defaultPbChars`](./docs.md#const-defaultpbchars) - Default characters for the progress bar
94
+ - [`const defaultPbChars`](./docs.md#const-defaultpbchars) - Default characters for the progress bar
94
95
  - 🔷 [`type ProgressBarChars`](./docs.md#type-progressbarchars) - Type for the progress bar characters object
95
96
  - 🟣 [`function joinArrayReadable()`](./docs.md#function-joinarrayreadable) - Joins the given array into a string, using the given separators and last separator
96
97
  - 🟣 [`function secsToTimeStr()`](./docs.md#function-secstotimestr) - Turns the given number of seconds into a string in the format `(hh:)mm:ss` with intelligent zero-padding
@@ -125,7 +126,7 @@ Intended to be used in conjunction with [`@sv443-network/userutils`](https://git
125
126
  > 🟣 = function
126
127
  > 🟧 = class
127
128
  > 🔷 = type
128
- > 🟩 = const
129
+ > = const
129
130
 
130
131
  <br>
131
132
 
@@ -32,6 +32,7 @@ var lib_exports = {};
32
32
  __export(lib_exports, {
33
33
  BrowserStorageEngine: () => BrowserStorageEngine,
34
34
  ChecksumMismatchError: () => ChecksumMismatchError,
35
+ CustomError: () => CustomError,
35
36
  DataStore: () => DataStore,
36
37
  DataStoreEngine: () => DataStoreEngine,
37
38
  DataStoreSerializer: () => DataStoreSerializer,
@@ -40,6 +41,8 @@ __export(lib_exports, {
40
41
  FileStorageEngine: () => FileStorageEngine,
41
42
  MigrationError: () => MigrationError,
42
43
  NanoEmitter: () => NanoEmitter,
44
+ NetworkError: () => NetworkError,
45
+ ScriptContextError: () => ScriptContextError,
43
46
  ValidationError: () => ValidationError,
44
47
  abtoa: () => abtoa,
45
48
  atoab: () => atoab,
@@ -59,6 +62,7 @@ __export(lib_exports, {
59
62
  digitCount: () => digitCount,
60
63
  fetchAdvanced: () => fetchAdvanced,
61
64
  formatNumber: () => formatNumber,
65
+ getCallStack: () => getCallStack,
62
66
  getListLength: () => getListLength,
63
67
  hexToRgb: () => hexToRgb,
64
68
  insertValues: () => insertValues,
@@ -175,7 +179,7 @@ function roundFixed(num, fractionDigits) {
175
179
  const scale = 10 ** fractionDigits;
176
180
  return Math.round(num * scale) / scale;
177
181
  }
178
- function valsWithin(a, b, dec = 10, withinRange = 0.5) {
182
+ function valsWithin(a, b, dec = 1, withinRange = 0.5) {
179
183
  return Math.abs(roundFixed(a, dec) - roundFixed(b, dec)) <= withinRange;
180
184
  }
181
185
 
@@ -325,6 +329,52 @@ function randomId(length = 16, radix = 16, enhancedEntropy = false, randomCase =
325
329
  return arr.map((v) => caseArr[randRange(0, caseArr.length - 1, enhancedEntropy)] === 1 ? v.toUpperCase() : v).join("");
326
330
  }
327
331
 
332
+ // lib/Errors.ts
333
+ var DatedError = class extends Error {
334
+ date;
335
+ constructor(message, options) {
336
+ super(message, options);
337
+ this.name = this.constructor.name;
338
+ this.date = /* @__PURE__ */ new Date();
339
+ }
340
+ };
341
+ var ChecksumMismatchError = class extends DatedError {
342
+ constructor(message, options) {
343
+ super(message, options);
344
+ this.name = "ChecksumMismatchError";
345
+ }
346
+ };
347
+ var CustomError = class extends DatedError {
348
+ constructor(name, message, options) {
349
+ super(message, options);
350
+ this.name = name;
351
+ }
352
+ };
353
+ var MigrationError = class extends DatedError {
354
+ constructor(message, options) {
355
+ super(message, options);
356
+ this.name = "MigrationError";
357
+ }
358
+ };
359
+ var ValidationError = class extends DatedError {
360
+ constructor(message, options) {
361
+ super(message, options);
362
+ this.name = "ValidationError";
363
+ }
364
+ };
365
+ var ScriptContextError = class extends DatedError {
366
+ constructor(message, options) {
367
+ super(message, options);
368
+ this.name = "ScriptContextError";
369
+ }
370
+ };
371
+ var NetworkError = class extends DatedError {
372
+ constructor(message, options) {
373
+ super(message, options);
374
+ this.name = "NetworkError";
375
+ }
376
+ };
377
+
328
378
  // lib/misc.ts
329
379
  async function consumeGen(valGen) {
330
380
  return await (typeof valGen === "function" ? valGen() : valGen);
@@ -335,9 +385,8 @@ async function consumeStringGen(strGen) {
335
385
  );
336
386
  }
337
387
  async function fetchAdvanced(input, options = {}) {
338
- const { timeout = 1e4 } = options;
388
+ const { timeout = 1e4, signal, ...restOpts } = options;
339
389
  const ctl = new AbortController();
340
- const { signal, ...restOpts } = options;
341
390
  signal == null ? void 0 : signal.addEventListener("abort", () => ctl.abort());
342
391
  let sigOpts = {}, id = void 0;
343
392
  if (timeout >= 0) {
@@ -353,7 +402,7 @@ async function fetchAdvanced(input, options = {}) {
353
402
  return res;
354
403
  } catch (err) {
355
404
  typeof id !== "undefined" && clearTimeout(id);
356
- throw new Error("Error while calling fetch", { cause: err });
405
+ throw new NetworkError("Error while calling fetch", { cause: err });
357
406
  }
358
407
  }
359
408
  function getListLength(listLike, zeroOnInvalid = true) {
@@ -364,7 +413,7 @@ function pauseFor(time, signal, rejectOnAbort = false) {
364
413
  const timeout = setTimeout(() => res(), time);
365
414
  signal == null ? void 0 : signal.addEventListener("abort", () => {
366
415
  clearTimeout(timeout);
367
- rejectOnAbort ? rej(new Error("The pause was aborted")) : res();
416
+ rejectOnAbort ? rej(new CustomError("AbortError", "The pause was aborted")) : res();
368
417
  });
369
418
  });
370
419
  }
@@ -399,14 +448,24 @@ function scheduleExit(code = 0, timeout = 0) {
399
448
  if (timeout < 0)
400
449
  throw new TypeError("Timeout must be a non-negative number");
401
450
  let exit;
402
- if (typeof process !== "undefined" && "exit" in process)
451
+ if (typeof process !== "undefined" && "exit" in process && typeof process.exit === "function")
403
452
  exit = () => process.exit(code);
404
- else if (typeof Deno !== "undefined" && "exit" in Deno)
453
+ else if (typeof Deno !== "undefined" && "exit" in Deno && typeof Deno.exit === "function")
405
454
  exit = () => Deno.exit(code);
406
455
  else
407
- throw new Error("Cannot exit the process, no exit method available");
456
+ throw new ScriptContextError("Cannot exit the process, no exit method available");
408
457
  setTimeout(exit, timeout);
409
458
  }
459
+ function getCallStack(asArray, lines = Infinity) {
460
+ if (typeof lines !== "number" || isNaN(lines) || lines < 0)
461
+ throw new TypeError("lines parameter must be a non-negative number");
462
+ try {
463
+ throw new Error("This is to capture a stack trace with CoreUtils.getCallStack(). (If you see this somewhere, you can safely ignore it.)");
464
+ } catch (err) {
465
+ const stack = (err.stack ?? "").split("\n").map((line) => line.trim()).slice(2, lines + 2);
466
+ return asArray !== false ? stack : stack.join("\n");
467
+ }
468
+ }
410
469
 
411
470
  // lib/text.ts
412
471
  function autoPlural(term, num, pluralType = "auto") {
@@ -476,16 +535,18 @@ function joinArrayReadable(array, separators = ", ", lastSeparator = " and ") {
476
535
  return arr.join(separators) + lastItm;
477
536
  }
478
537
  function secsToTimeStr(seconds) {
479
- if (seconds < 0)
480
- throw new TypeError("Seconds must be a positive number");
481
- const hours = Math.floor(seconds / 3600);
482
- const minutes = Math.floor(seconds % 3600 / 60);
483
- const secs = Math.floor(seconds % 60);
484
- return [
485
- hours ? hours + ":" : "",
486
- String(minutes).padStart(minutes > 0 || hours > 0 ? 2 : 1, "0"),
538
+ const isNegative = seconds < 0;
539
+ const s = Math.abs(seconds);
540
+ if (isNaN(s) || !isFinite(s))
541
+ throw new TypeError("The seconds argument must be a valid number");
542
+ const hrs = Math.floor(s / 3600);
543
+ const mins = Math.floor(s % 3600 / 60);
544
+ const secs = Math.floor(s % 60);
545
+ return (isNegative ? "-" : "") + [
546
+ hrs ? hrs + ":" : "",
547
+ String(mins).padStart(mins > 0 || hrs > 0 ? 2 : 1, "0"),
487
548
  ":",
488
- String(secs).padStart(secs > 0 || minutes > 0 || hours > 0 ? 2 : 1, "0")
549
+ String(secs).padStart(secs > 0 || mins > 0 || hrs > 0 || seconds === 0 ? 2 : 1, "0")
489
550
  ].join("");
490
551
  }
491
552
  function truncStr(input, length, endStr = "...") {
@@ -494,34 +555,6 @@ function truncStr(input, length, endStr = "...") {
494
555
  return finalStr.length > length ? finalStr.substring(0, length) : finalStr;
495
556
  }
496
557
 
497
- // lib/Errors.ts
498
- var DatedError = class extends Error {
499
- date;
500
- constructor(message, options) {
501
- super(message, options);
502
- this.name = this.constructor.name;
503
- this.date = /* @__PURE__ */ new Date();
504
- }
505
- };
506
- var ChecksumMismatchError = class extends DatedError {
507
- constructor(message, options) {
508
- super(message, options);
509
- this.name = "ChecksumMismatchError";
510
- }
511
- };
512
- var MigrationError = class extends DatedError {
513
- constructor(message, options) {
514
- super(message, options);
515
- this.name = "MigrationError";
516
- }
517
- };
518
- var ValidationError = class extends DatedError {
519
- constructor(message, options) {
520
- super(message, options);
521
- this.name = "ValidationError";
522
- }
523
- };
524
-
525
558
  // lib/DataStore.ts
526
559
  var dsFmtVer = 1;
527
560
  var DataStore = class {
@@ -531,6 +564,7 @@ var DataStore = class {
531
564
  encodeData;
532
565
  decodeData;
533
566
  compressionFormat = "deflate-raw";
567
+ memoryCache = true;
534
568
  engine;
535
569
  options;
536
570
  /**
@@ -543,6 +577,7 @@ var DataStore = class {
543
577
  cachedData;
544
578
  migrations;
545
579
  migrateIds = [];
580
+ //#region constructor
546
581
  /**
547
582
  * Creates an instance of DataStore to manage a sync & async database that is cached in memory and persistently saved across sessions.
548
583
  * Supports migrating data from older versions to newer ones and populating the cache with default data if no persistent data is found.
@@ -557,7 +592,8 @@ var DataStore = class {
557
592
  this.id = opts.id;
558
593
  this.formatVersion = opts.formatVersion;
559
594
  this.defaultData = opts.defaultData;
560
- this.cachedData = opts.defaultData;
595
+ this.memoryCache = Boolean(opts.memoryCache ?? true);
596
+ this.cachedData = this.memoryCache ? opts.defaultData : {};
561
597
  this.migrations = opts.migrations;
562
598
  if (opts.migrateIds)
563
599
  this.migrateIds = Array.isArray(opts.migrateIds) ? opts.migrateIds : [opts.migrateIds];
@@ -582,7 +618,7 @@ var DataStore = class {
582
618
  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.");
583
619
  this.engine.setDataStoreOptions(opts);
584
620
  }
585
- //#region public
621
+ //#region loadData
586
622
  /**
587
623
  * Loads the data saved in persistent storage into the in-memory cache and also returns a copy of it.
588
624
  * Automatically populates persistent storage with default data if it doesn't contain any data yet.
@@ -593,28 +629,28 @@ var DataStore = class {
593
629
  if (this.firstInit) {
594
630
  this.firstInit = false;
595
631
  const dsVer = Number(await this.engine.getValue("__ds_fmt_ver", 0));
596
- if (isNaN(dsVer) || dsVer < 1) {
597
- const oldData = await this.engine.getValue(`_uucfg-${this.id}`, null);
598
- if (oldData) {
599
- const oldVer = Number(await this.engine.getValue(`_uucfgver-${this.id}`, NaN));
600
- const oldEnc = await this.engine.getValue(`_uucfgenc-${this.id}`, null);
601
- const promises = [];
602
- const migrateFmt = (oldKey, newKey, value) => {
603
- promises.push(this.engine.setValue(newKey, value));
604
- promises.push(this.engine.deleteValue(oldKey));
605
- };
606
- if (oldData)
607
- migrateFmt(`_uucfg-${this.id}`, `__ds-${this.id}-dat`, oldData);
608
- if (!isNaN(oldVer))
609
- migrateFmt(`_uucfgver-${this.id}`, `__ds-${this.id}-ver`, oldVer);
610
- if (typeof oldEnc === "boolean")
611
- migrateFmt(`_uucfgenc-${this.id}`, `__ds-${this.id}-enf`, oldEnc === true ? Boolean(this.compressionFormat) || null : null);
612
- else
613
- promises.push(this.engine.setValue(`__ds-${this.id}-enf`, this.compressionFormat));
614
- await Promise.allSettled(promises);
632
+ const oldData = await this.engine.getValue(`_uucfg-${this.id}`, null);
633
+ if (oldData) {
634
+ const oldVer = Number(await this.engine.getValue(`_uucfgver-${this.id}`, NaN));
635
+ const oldEnc = await this.engine.getValue(`_uucfgenc-${this.id}`, null);
636
+ const promises = [];
637
+ const migrateFmt = (oldKey, newKey, value) => {
638
+ promises.push(this.engine.setValue(newKey, value));
639
+ promises.push(this.engine.deleteValue(oldKey));
640
+ };
641
+ migrateFmt(`_uucfg-${this.id}`, `__ds-${this.id}-dat`, oldData);
642
+ if (!isNaN(oldVer))
643
+ migrateFmt(`_uucfgver-${this.id}`, `__ds-${this.id}-ver`, oldVer);
644
+ if (typeof oldEnc === "boolean")
645
+ migrateFmt(`_uucfgenc-${this.id}`, `__ds-${this.id}-enf`, oldEnc === true ? this.compressionFormat ?? null : null);
646
+ else {
647
+ promises.push(this.engine.setValue(`__ds-${this.id}-enf`, this.compressionFormat));
648
+ promises.push(this.engine.deleteValue(`_uucfgenc-${this.id}`));
615
649
  }
616
- await this.engine.setValue("__ds_fmt_ver", dsFmtVer);
650
+ await Promise.allSettled(promises);
617
651
  }
652
+ if (isNaN(dsVer) || dsVer < dsFmtVer)
653
+ await this.engine.setValue("__ds_fmt_ver", dsFmtVer);
618
654
  }
619
655
  if (this.migrateIds.length > 0) {
620
656
  await this.migrateId(this.migrateIds);
@@ -624,7 +660,7 @@ var DataStore = class {
624
660
  let storedFmtVer = Number(await this.engine.getValue(`__ds-${this.id}-ver`, NaN));
625
661
  if (typeof storedDataRaw !== "string") {
626
662
  await this.saveDefaultData();
627
- return { ...this.defaultData };
663
+ return this.engine.deepCopy(this.defaultData);
628
664
  }
629
665
  const storedData = storedDataRaw ?? JSON.stringify(this.defaultData);
630
666
  const encodingFmt = String(await this.engine.getValue(`__ds-${this.id}-enf`, null));
@@ -639,23 +675,32 @@ var DataStore = class {
639
675
  parsed = await this.runMigrations(parsed, storedFmtVer);
640
676
  if (saveData)
641
677
  await this.setData(parsed);
642
- return this.cachedData = this.engine.deepCopy(parsed);
678
+ if (this.memoryCache)
679
+ return this.cachedData = this.engine.deepCopy(parsed);
680
+ else
681
+ return this.engine.deepCopy(parsed);
643
682
  } catch (err) {
644
683
  console.warn("Error while parsing JSON data, resetting it to the default value.", err);
645
684
  await this.saveDefaultData();
646
685
  return this.defaultData;
647
686
  }
648
687
  }
688
+ //#region getData
649
689
  /**
650
690
  * Returns a copy of the data from the in-memory cache.
651
- * Use {@linkcode loadData()} to get fresh data from persistent storage (usually not necessary since the cache should always exactly reflect persistent storage).
691
+ * Use {@linkcode loadData()} to get fresh data from persistent storage (usually not necessary since the cache should always exactly reflect persistent storage).
692
+ * ⚠️ If `memoryCache` was set to `false` in the constructor options, this method will throw an error.
652
693
  */
653
694
  getData() {
695
+ if (!this.memoryCache)
696
+ throw new DatedError("In-memory cache is disabled for this DataStore instance, so getData() can't be used. Please use loadData() instead.");
654
697
  return this.engine.deepCopy(this.cachedData);
655
698
  }
699
+ //#region setData
656
700
  /** Saves the data synchronously to the in-memory cache and asynchronously to the persistent storage */
657
701
  setData(data) {
658
- this.cachedData = data;
702
+ if (this.memoryCache)
703
+ this.cachedData = data;
659
704
  return new Promise(async (resolve) => {
660
705
  await Promise.allSettled([
661
706
  this.engine.setValue(`__ds-${this.id}-dat`, await this.engine.serializeData(data, this.encodingEnabled())),
@@ -665,15 +710,18 @@ var DataStore = class {
665
710
  resolve();
666
711
  });
667
712
  }
713
+ //#region saveDefaultData
668
714
  /** Saves the default data passed in the constructor synchronously to the in-memory cache and asynchronously to persistent storage */
669
715
  async saveDefaultData() {
670
- this.cachedData = this.defaultData;
716
+ if (this.memoryCache)
717
+ this.cachedData = this.defaultData;
671
718
  await Promise.allSettled([
672
719
  this.engine.setValue(`__ds-${this.id}-dat`, await this.engine.serializeData(this.defaultData, this.encodingEnabled())),
673
720
  this.engine.setValue(`__ds-${this.id}-ver`, this.formatVersion),
674
721
  this.engine.setValue(`__ds-${this.id}-enf`, this.compressionFormat)
675
722
  ]);
676
723
  }
724
+ //#region deleteData
677
725
  /**
678
726
  * Call this method to clear all persistently stored data associated with this DataStore instance, including the storage container (if supported by the DataStoreEngine).
679
727
  * The in-memory cache will be left untouched, so you may still access the data with {@linkcode getData()}
@@ -688,11 +736,12 @@ var DataStore = class {
688
736
  ]);
689
737
  await ((_b = (_a = this.engine).deleteStorage) == null ? void 0 : _b.call(_a));
690
738
  }
739
+ //#region encodingEnabled
691
740
  /** Returns whether encoding and decoding are enabled for this DataStore instance */
692
741
  encodingEnabled() {
693
742
  return Boolean(this.encodeData && this.decodeData) && this.compressionFormat !== null || Boolean(this.compressionFormat);
694
743
  }
695
- //#region migrations
744
+ //#region runMigrations
696
745
  /**
697
746
  * Runs all necessary migration functions consecutively and saves the result to the in-memory cache and persistent storage and also returns it.
698
747
  * This method is automatically called by {@linkcode loadData()} if the data format has changed since the last time the data was saved.
@@ -726,8 +775,12 @@ var DataStore = class {
726
775
  this.engine.setValue(`__ds-${this.id}-ver`, lastFmtVer),
727
776
  this.engine.setValue(`__ds-${this.id}-enf`, this.compressionFormat)
728
777
  ]);
729
- return this.cachedData = { ...newData };
778
+ if (this.memoryCache)
779
+ return this.cachedData = this.engine.deepCopy(newData);
780
+ else
781
+ return this.engine.deepCopy(newData);
730
782
  }
783
+ //#region migrateId
731
784
  /**
732
785
  * Tries to migrate the currently saved persistent data from one or more old IDs to the ID set in the constructor.
733
786
  * If no data exist for the old ID(s), nothing will be done, but some time may still pass trying to fetch the non-existent data.
@@ -771,7 +824,7 @@ var DataStoreEngine = class {
771
824
  this.dataStoreOptions = dataStoreOptions;
772
825
  }
773
826
  //#region serialization api
774
- /** Serializes the given object to a string, optionally encoded with `options.encodeData` if {@linkcode useEncoding} is set to true */
827
+ /** Serializes the given object to a string, optionally encoded with `options.encodeData` if {@linkcode useEncoding} is not set to false and the `encodeData` and `decodeData` options are set */
775
828
  async serializeData(data, useEncoding) {
776
829
  var _a, _b, _c, _d, _e;
777
830
  this.ensureDataStoreOptions();
@@ -819,7 +872,7 @@ var BrowserStorageEngine = class extends DataStoreEngine {
819
872
  * Creates an instance of `BrowserStorageEngine`.
820
873
  *
821
874
  * - ⚠️ Requires a DOM environment
822
- * - ⚠️ Don't reuse engines across multiple {@linkcode DataStore} instances
875
+ * - ⚠️ Don't reuse engine instances, always create a new one for each {@linkcode DataStore} instance
823
876
  */
824
877
  constructor(options) {
825
878
  super(options == null ? void 0 : options.dataStoreOptions);
@@ -857,7 +910,7 @@ var FileStorageEngine = class extends DataStoreEngine {
857
910
  * Creates an instance of `FileStorageEngine`.
858
911
  *
859
912
  * - ⚠️ Requires Node.js or Deno with Node compatibility (v1.31+)
860
- * - ⚠️ Don't reuse engines across multiple {@linkcode DataStore} instances
913
+ * - ⚠️ Don't reuse engine instances, always create a new one for each {@linkcode DataStore} instance
861
914
  */
862
915
  constructor(options) {
863
916
  super(options == null ? void 0 : options.dataStoreOptions);
@@ -875,7 +928,7 @@ var FileStorageEngine = class extends DataStoreEngine {
875
928
  if (!fs)
876
929
  fs = (_a = await import("fs/promises")) == null ? void 0 : _a.default;
877
930
  if (!fs)
878
- throw new DatedError("FileStorageEngine requires Node.js or Deno with Node compatibility (v1.31+)", { cause: new Error("'node:fs/promises' module not available") });
931
+ throw new ScriptContextError("FileStorageEngine requires Node.js or Deno with Node compatibility (v1.31+)", { cause: new DatedError("'node:fs/promises' module not available") });
879
932
  const path = typeof this.options.filePath === "string" ? this.options.filePath : this.options.filePath(this.dataStoreOptions.id);
880
933
  const data = await fs.readFile(path, "utf-8");
881
934
  return data ? JSON.parse(await ((_d = (_c = (_b = this.dataStoreOptions) == null ? void 0 : _b.decodeData) == null ? void 0 : _c[1]) == null ? void 0 : _d.call(_c, data)) ?? data) : void 0;
@@ -891,7 +944,7 @@ var FileStorageEngine = class extends DataStoreEngine {
891
944
  if (!fs)
892
945
  fs = (_a = await import("fs/promises")) == null ? void 0 : _a.default;
893
946
  if (!fs)
894
- throw new DatedError("FileStorageEngine requires Node.js or Deno with Node compatibility (v1.31+)", { cause: new Error("'node:fs/promises' module not available") });
947
+ throw new ScriptContextError("FileStorageEngine requires Node.js or Deno with Node compatibility (v1.31+)", { cause: new DatedError("'node:fs/promises' module not available") });
895
948
  const path = typeof this.options.filePath === "string" ? this.options.filePath : this.options.filePath(this.dataStoreOptions.id);
896
949
  await fs.mkdir(path.slice(0, path.lastIndexOf(path.includes("/") ? "/" : "\\")), { recursive: true });
897
950
  await fs.writeFile(path, await ((_d = (_c = (_b = this.dataStoreOptions) == null ? void 0 : _b.encodeData) == null ? void 0 : _c[1]) == null ? void 0 : _d.call(_c, JSON.stringify(data))) ?? JSON.stringify(data, void 0, 2), "utf-8");
@@ -950,9 +1003,9 @@ var FileStorageEngine = class extends DataStoreEngine {
950
1003
  if (!fs)
951
1004
  fs = (_a = await import("fs/promises")) == null ? void 0 : _a.default;
952
1005
  if (!fs)
953
- throw new DatedError("FileStorageEngine requires Node.js or Deno with Node compatibility (v1.31+)", { cause: new Error("'node:fs/promises' module not available") });
1006
+ throw new ScriptContextError("FileStorageEngine requires Node.js or Deno with Node compatibility (v1.31+)", { cause: new DatedError("'node:fs/promises' module not available") });
954
1007
  const path = typeof this.options.filePath === "string" ? this.options.filePath : this.options.filePath(this.dataStoreOptions.id);
955
- await fs.unlink(path);
1008
+ return await fs.unlink(path);
956
1009
  } catch (err) {
957
1010
  console.error("Error deleting file:", err);
958
1011
  }
@@ -965,11 +1018,12 @@ var DataStoreSerializer = class _DataStoreSerializer {
965
1018
  options;
966
1019
  constructor(stores, options = {}) {
967
1020
  if (!crypto || !crypto.subtle)
968
- throw new Error("DataStoreSerializer has to run in a secure context (HTTPS) or in another environment that implements the subtleCrypto API!");
1021
+ throw new ScriptContextError("DataStoreSerializer has to run in a secure context (HTTPS) or in another environment that implements the subtleCrypto API!");
969
1022
  this.stores = stores;
970
1023
  this.options = {
971
1024
  addChecksum: true,
972
1025
  ensureIntegrity: true,
1026
+ remapIds: {},
973
1027
  ...options
974
1028
  };
975
1029
  }
@@ -986,9 +1040,11 @@ var DataStoreSerializer = class _DataStoreSerializer {
986
1040
  async serializePartial(stores, useEncoding = true, stringified = true) {
987
1041
  var _a;
988
1042
  const serData = [];
989
- for (const storeInst of this.stores.filter((s) => typeof stores === "function" ? stores(s.id) : stores.includes(s.id))) {
1043
+ const filteredStores = this.stores.filter((s) => typeof stores === "function" ? stores(s.id) : stores.includes(s.id));
1044
+ for (const storeInst of filteredStores) {
990
1045
  const encoded = Boolean(useEncoding && storeInst.encodingEnabled() && ((_a = storeInst.encodeData) == null ? void 0 : _a[1]));
991
- const data = encoded ? await storeInst.encodeData[1](JSON.stringify(storeInst.getData())) : JSON.stringify(storeInst.getData());
1046
+ const rawData = storeInst.memoryCache ? storeInst.getData() : await storeInst.loadData();
1047
+ const data = encoded ? await storeInst.encodeData[1](JSON.stringify(rawData)) : JSON.stringify(rawData);
992
1048
  serData.push({
993
1049
  id: storeInst.id,
994
1050
  data,
@@ -1015,10 +1071,18 @@ var DataStoreSerializer = class _DataStoreSerializer {
1015
1071
  const deserStores = typeof data === "string" ? JSON.parse(data) : data;
1016
1072
  if (!Array.isArray(deserStores) || !deserStores.every(_DataStoreSerializer.isSerializedDataStoreObj))
1017
1073
  throw new TypeError("Invalid serialized data format! Expected an array of SerializedDataStore objects.");
1018
- for (const storeData of deserStores.filter((s) => typeof stores === "function" ? stores(s.id) : stores.includes(s.id))) {
1019
- const storeInst = this.stores.find((s) => s.id === storeData.id);
1074
+ const resolveStoreId = (id) => {
1075
+ var _a;
1076
+ return ((_a = Object.entries(this.options.remapIds).find(([, v]) => v.includes(id))) == null ? void 0 : _a[0]) ?? id;
1077
+ };
1078
+ const matchesFilter = (id) => typeof stores === "function" ? stores(id) : stores.includes(id);
1079
+ for (const storeData of deserStores) {
1080
+ const effectiveId = resolveStoreId(storeData.id);
1081
+ if (!matchesFilter(effectiveId))
1082
+ continue;
1083
+ const storeInst = this.stores.find((s) => s.id === effectiveId);
1020
1084
  if (!storeInst)
1021
- throw new Error(`DataStore instance with ID "${storeData.id}" not found! Make sure to provide it in the DataStoreSerializer constructor.`);
1085
+ throw new DatedError(`Can't deserialize data because no DataStore instance with the ID "${effectiveId}" was found! Make sure to provide it in the DataStoreSerializer constructor.`);
1022
1086
  if (this.options.ensureIntegrity && typeof storeData.checksum === "string") {
1023
1087
  const checksum = await this.calcChecksum(storeData.data);
1024
1088
  if (checksum !== storeData.checksum)
@@ -1189,11 +1253,12 @@ var NanoEmitter = class {
1189
1253
  * `callback` (required) is the function that will be called when the conditions are met.
1190
1254
  *
1191
1255
  * Set `once` to true to call the callback only once for the first event (or set of events) that match the criteria, then stop listening.
1192
- * If `signal` is provided, the subscription will be aborted when the given signal is aborted.
1256
+ * If `signal` is provided, the subscription will be canceled when the given signal is aborted.
1193
1257
  *
1194
1258
  * If `oneOf` is used, the callback will be called when any of the matching events are emitted.
1195
1259
  * If `allOf` is used, the callback will be called after all of the matching events are emitted at least once, then any time any of them are emitted.
1196
- * You may use a combination of the above two options, but at least one of them must be provided.
1260
+ * If both `oneOf` and `allOf` are used together, the callback will be called when any of the `oneOf` events are emitted AND all of the `allOf` events have been emitted at least once.
1261
+ * At least one of `oneOf` or `allOf` must be provided.
1197
1262
  *
1198
1263
  * @returns Returns a function that can be called to unsubscribe all listeners created by this call. Alternatively, pass an `AbortSignal` to all options objects to achieve the same effect or for finer control.
1199
1264
  */
@@ -1221,6 +1286,8 @@ var NanoEmitter = class {
1221
1286
  } = optsWithDefaults;
1222
1287
  if (signal == null ? void 0 : signal.aborted)
1223
1288
  return unsubAll;
1289
+ if (oneOf.length === 0 && allOf.length === 0)
1290
+ throw new TypeError("NanoEmitter.onMulti(): Either `oneOf` or `allOf` or both must be provided in the options");
1224
1291
  const curEvtUnsubs = [];
1225
1292
  const checkUnsubAllEvt = (force = false) => {
1226
1293
  if (!(signal == null ? void 0 : signal.aborted) && !force)
@@ -1230,34 +1297,31 @@ var NanoEmitter = class {
1230
1297
  curEvtUnsubs.splice(0, curEvtUnsubs.length);
1231
1298
  this.eventUnsubscribes = this.eventUnsubscribes.filter((u) => !curEvtUnsubs.includes(u));
1232
1299
  };
1300
+ const allOfEmitted = /* @__PURE__ */ new Set();
1301
+ const allOfConditionMet = () => allOf.length === 0 || allOfEmitted.size === allOf.length;
1233
1302
  for (const event of oneOf) {
1234
1303
  const unsub = this.events.on(event, ((...args) => {
1235
1304
  checkUnsubAllEvt();
1236
- callback(event, ...args);
1237
- if (once)
1238
- checkUnsubAllEvt(true);
1305
+ if (allOfConditionMet()) {
1306
+ callback(event, ...args);
1307
+ if (once)
1308
+ checkUnsubAllEvt(true);
1309
+ }
1239
1310
  }));
1240
1311
  curEvtUnsubs.push(unsub);
1241
1312
  }
1242
- const allOfEmitted = /* @__PURE__ */ new Set();
1243
- const checkAllOf = (event, ...args) => {
1244
- checkUnsubAllEvt();
1245
- allOfEmitted.add(event);
1246
- if (allOfEmitted.size === allOf.length) {
1247
- callback(event, ...args);
1248
- if (once)
1249
- checkUnsubAllEvt(true);
1250
- }
1251
- };
1252
1313
  for (const event of allOf) {
1253
1314
  const unsub = this.events.on(event, ((...args) => {
1254
1315
  checkUnsubAllEvt();
1255
- checkAllOf(event, ...args);
1316
+ allOfEmitted.add(event);
1317
+ if (allOfConditionMet() && (oneOf.length === 0 || oneOf.includes(event))) {
1318
+ callback(event, ...args);
1319
+ if (once)
1320
+ checkUnsubAllEvt(true);
1321
+ }
1256
1322
  }));
1257
1323
  curEvtUnsubs.push(unsub);
1258
1324
  }
1259
- if (oneOf.length === 0 && allOf.length === 0)
1260
- throw new TypeError("NanoEmitter.onMulti(): Either `oneOf` or `allOf` or both must be provided in the options");
1261
1325
  allUnsubs.push(() => checkUnsubAllEvt(true));
1262
1326
  }
1263
1327
  return unsubAll;