@crvouga/sqlite-mem 1.4.0 → 1.6.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");
@@ -4939,10 +4957,19 @@ var Parser = class {
4939
4957
  parseFkActions() {
4940
4958
  let onDelete = null;
4941
4959
  let onUpdate = null;
4960
+ while (this.match("MATCH")) {
4961
+ this.parseIdent();
4962
+ }
4942
4963
  while (this.at("ON")) {
4943
4964
  if (this.peek().kind === "DELETE") onDelete = this.parseFkAction("DELETE");
4944
4965
  else if (this.peek().kind === "UPDATE") onUpdate = this.parseFkAction("UPDATE");
4945
4966
  else this.syntaxError("expected ON DELETE or ON UPDATE");
4967
+ while (this.match("MATCH")) {
4968
+ this.parseIdent();
4969
+ }
4970
+ }
4971
+ while (this.match("MATCH")) {
4972
+ this.parseIdent();
4946
4973
  }
4947
4974
  return { onDelete, onUpdate };
4948
4975
  }
@@ -8736,7 +8763,11 @@ var Reader = class {
8736
8763
  return utf8Decode(this.raw(this.u32()));
8737
8764
  }
8738
8765
  json() {
8739
- return JSON.parse(this.text(), jsonReviver);
8766
+ try {
8767
+ return JSON.parse(this.text(), jsonReviver);
8768
+ } catch {
8769
+ throw new SqliteError("invalid or truncated sqlite-mem snapshot", "other");
8770
+ }
8740
8771
  }
8741
8772
  value() {
8742
8773
  const tag = this.u8();
@@ -8814,6 +8845,14 @@ function encodeDatabaseState(state, runtime) {
8814
8845
  return writer.finish();
8815
8846
  }
8816
8847
  function decodeDatabaseState(snapshot) {
8848
+ try {
8849
+ return decodeDatabaseStateInner(snapshot);
8850
+ } catch (error) {
8851
+ if (error instanceof SqliteError) throw error;
8852
+ throw new SqliteError("invalid or truncated sqlite-mem snapshot", "other");
8853
+ }
8854
+ }
8855
+ function decodeDatabaseStateInner(snapshot) {
8817
8856
  const reader = new Reader(snapshot);
8818
8857
  const magic = reader.raw(4);
8819
8858
  if (!magic.every((byte, index) => byte === MAGIC[index]))