@crvouga/sqlite-mem 1.4.0 → 1.5.0

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/unstable.js CHANGED
@@ -1758,6 +1758,21 @@ function applyModifier(date, modifier, flags) {
1758
1758
  }
1759
1759
  return result;
1760
1760
  }
1761
+ function applyModifiers(date, modifiers) {
1762
+ const flags = { subsec: false };
1763
+ let current = date;
1764
+ for (const modifier of modifiers) {
1765
+ if (typeof modifier !== "string") return null;
1766
+ const next = applyModifier(current, modifier, flags);
1767
+ if (!next) return null;
1768
+ current = next;
1769
+ }
1770
+ return { date: current, subsec: flags.subsec };
1771
+ }
1772
+ function fromUnixSeconds(seconds) {
1773
+ const date = new Date(seconds * 1e3);
1774
+ return Number.isNaN(date.getTime()) ? null : date;
1775
+ }
1761
1776
  function resolveDate(args, context) {
1762
1777
  let source = args[0];
1763
1778
  let modifiers = args.slice(1);
@@ -1765,23 +1780,26 @@ function resolveDate(args, context) {
1765
1780
  if (modifiers[0] === "unixepoch") {
1766
1781
  const seconds = coerceToNumber(source);
1767
1782
  if (seconds === null) return null;
1768
- source = seconds / 86400 + JULIAN_UNIX_EPOCH;
1769
- modifiers = modifiers.slice(1);
1770
- } else if (typeof modifiers[0] === "string" && modifiers[0].trim().toLowerCase() === "auto") {
1783
+ const date2 = fromUnixSeconds(seconds);
1784
+ if (!date2) return null;
1785
+ return applyModifiers(date2, modifiers.slice(1));
1786
+ }
1787
+ if (typeof modifiers[0] === "string" && modifiers[0].trim().toLowerCase() === "auto") {
1771
1788
  const number = coerceToNumber(source);
1772
- if (number === null) return null;
1773
- source = number >= 0 && number < 53734845e-1 ? number : number / 86400 + JULIAN_UNIX_EPOCH;
1774
- modifiers = modifiers.slice(1);
1789
+ if (number === null) {
1790
+ modifiers = modifiers.slice(1);
1791
+ } else if (number >= 0 && number < 53734845e-1) {
1792
+ source = number;
1793
+ modifiers = modifiers.slice(1);
1794
+ } else {
1795
+ const date2 = fromUnixSeconds(number);
1796
+ if (!date2) return null;
1797
+ return applyModifiers(date2, modifiers.slice(1));
1798
+ }
1775
1799
  }
1776
- let date = parseDate(source, context);
1800
+ const date = parseDate(source, context);
1777
1801
  if (!date) return null;
1778
- const flags = { subsec: false };
1779
- for (const modifier of modifiers) {
1780
- if (typeof modifier !== "string") return null;
1781
- date = applyModifier(date, modifier, flags);
1782
- if (!date) return null;
1783
- }
1784
- return { date, subsec: flags.subsec };
1802
+ return applyModifiers(date, modifiers);
1785
1803
  }
1786
1804
  function pad(value, length = 2) {
1787
1805
  return String(value).padStart(length, "0");
@@ -8736,7 +8754,11 @@ var Reader = class {
8736
8754
  return utf8Decode(this.raw(this.u32()));
8737
8755
  }
8738
8756
  json() {
8739
- return JSON.parse(this.text(), jsonReviver);
8757
+ try {
8758
+ return JSON.parse(this.text(), jsonReviver);
8759
+ } catch {
8760
+ throw new SqliteError("invalid or truncated sqlite-mem snapshot", "other");
8761
+ }
8740
8762
  }
8741
8763
  value() {
8742
8764
  const tag = this.u8();
@@ -8814,6 +8836,14 @@ function encodeDatabaseState(state, runtime) {
8814
8836
  return writer.finish();
8815
8837
  }
8816
8838
  function decodeDatabaseState(snapshot) {
8839
+ try {
8840
+ return decodeDatabaseStateInner(snapshot);
8841
+ } catch (error) {
8842
+ if (error instanceof SqliteError) throw error;
8843
+ throw new SqliteError("invalid or truncated sqlite-mem snapshot", "other");
8844
+ }
8845
+ }
8846
+ function decodeDatabaseStateInner(snapshot) {
8817
8847
  const reader = new Reader(snapshot);
8818
8848
  const magic = reader.raw(4);
8819
8849
  if (!magic.every((byte, index) => byte === MAGIC[index]))