memorio 5.1.2 → 5.1.4
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/AGENTS.md +1 -3
- package/CHANGELOG.md +83 -0
- package/README.md +32 -14
- package/SUMMARY.md +2 -2
- package/examples/sqlite-batched-writes.ts +60 -57
- package/global.cjs +454 -242
- package/global.js +431 -218
- package/index.cjs +454 -242
- package/index.js +431 -218
- package/llms.txt +17 -2
- package/markdown/MEMORY.md +27 -0
- package/markdown/SQLITE.md +48 -22
- package/markdown/STORE.md +12 -12
- package/markdown/SYNC.md +18 -15
- package/modules/redux.cjs +29 -12
- package/modules/redux.cjs.map +1 -1
- package/modules/redux.js +29 -12
- package/modules/redux.js.map +1 -1
- package/package.json +7 -2
- package/types/sqlite.d.ts +58 -35
- package/types/state.d.ts +19 -5
- package/vsix/memorio.vsix +0 -0
package/index.cjs
CHANGED
|
@@ -1095,6 +1095,123 @@ var init_store = __esm({
|
|
|
1095
1095
|
}
|
|
1096
1096
|
});
|
|
1097
1097
|
|
|
1098
|
+
// core/hlc.ts
|
|
1099
|
+
function getNodeId() {
|
|
1100
|
+
if (typeof globalThis === "undefined") return "node";
|
|
1101
|
+
try {
|
|
1102
|
+
if (typeof sessionStorage !== "undefined" && sessionStorage) {
|
|
1103
|
+
let id = sessionStorage.getItem(NODE_ID_KEY);
|
|
1104
|
+
if (!id) {
|
|
1105
|
+
id = generateNodeId();
|
|
1106
|
+
sessionStorage.setItem(NODE_ID_KEY, id);
|
|
1107
|
+
}
|
|
1108
|
+
return id;
|
|
1109
|
+
}
|
|
1110
|
+
} catch {
|
|
1111
|
+
}
|
|
1112
|
+
if (!globalThis.__memorioHlcNodeId) {
|
|
1113
|
+
globalThis.__memorioHlcNodeId = generateNodeId();
|
|
1114
|
+
}
|
|
1115
|
+
return globalThis.__memorioHlcNodeId;
|
|
1116
|
+
}
|
|
1117
|
+
function generateNodeId() {
|
|
1118
|
+
if (typeof crypto !== "undefined" && crypto.randomUUID) {
|
|
1119
|
+
return crypto.randomUUID().slice(0, 8);
|
|
1120
|
+
}
|
|
1121
|
+
return Math.random().toString(36).slice(2, 10);
|
|
1122
|
+
}
|
|
1123
|
+
function tick(_incoming) {
|
|
1124
|
+
const now2 = Date.now();
|
|
1125
|
+
const base = local.physical;
|
|
1126
|
+
let physical;
|
|
1127
|
+
let logical;
|
|
1128
|
+
if (now2 > base) {
|
|
1129
|
+
physical = now2;
|
|
1130
|
+
logical = 0;
|
|
1131
|
+
} else if (now2 < base) {
|
|
1132
|
+
physical = base;
|
|
1133
|
+
logical = local.logical + 1;
|
|
1134
|
+
} else {
|
|
1135
|
+
physical = base;
|
|
1136
|
+
logical = local.logical + 1;
|
|
1137
|
+
}
|
|
1138
|
+
local.physical = physical;
|
|
1139
|
+
local.logical = logical;
|
|
1140
|
+
return { physical, logical, node: local.node };
|
|
1141
|
+
}
|
|
1142
|
+
function receive(remote) {
|
|
1143
|
+
const now2 = Date.now();
|
|
1144
|
+
if (remote.physical > local.physical) {
|
|
1145
|
+
local.physical = remote.physical > now2 ? remote.physical : now2;
|
|
1146
|
+
local.logical = 1;
|
|
1147
|
+
} else if (remote.physical === local.physical) {
|
|
1148
|
+
local.logical = Math.max(local.logical, remote.logical) + 1;
|
|
1149
|
+
}
|
|
1150
|
+
local.node = getNodeId();
|
|
1151
|
+
return { ...local };
|
|
1152
|
+
}
|
|
1153
|
+
function compare(a, b) {
|
|
1154
|
+
if (a.physical !== b.physical) {
|
|
1155
|
+
return a.physical < b.physical ? -1 : 1;
|
|
1156
|
+
}
|
|
1157
|
+
if (a.logical !== b.logical) {
|
|
1158
|
+
return a.logical < b.logical ? -1 : 1;
|
|
1159
|
+
}
|
|
1160
|
+
if (a.node !== b.node) {
|
|
1161
|
+
return a.node < b.node ? -1 : 1;
|
|
1162
|
+
}
|
|
1163
|
+
return 0;
|
|
1164
|
+
}
|
|
1165
|
+
function happenedBefore(a, b) {
|
|
1166
|
+
return compare(a, b) < 0;
|
|
1167
|
+
}
|
|
1168
|
+
function serialize(hlc2) {
|
|
1169
|
+
return `hlc:${hlc2.physical}:${hlc2.logical}:${hlc2.node}`;
|
|
1170
|
+
}
|
|
1171
|
+
function parse(serialized) {
|
|
1172
|
+
if (!serialized.startsWith("hlc:")) return null;
|
|
1173
|
+
const parts = serialized.split(":");
|
|
1174
|
+
if (parts.length !== 4) return null;
|
|
1175
|
+
const physical = parseInt(parts[1], 10);
|
|
1176
|
+
const logical = parseInt(parts[2], 10);
|
|
1177
|
+
const node = parts[3];
|
|
1178
|
+
if (isNaN(physical) || isNaN(logical)) return null;
|
|
1179
|
+
return { physical, logical, node };
|
|
1180
|
+
}
|
|
1181
|
+
var NODE_ID_KEY, HLC_KEY, local; exports.hlc = void 0;
|
|
1182
|
+
var init_hlc = __esm({
|
|
1183
|
+
"core/hlc.ts"() {
|
|
1184
|
+
init_cjs_shims();
|
|
1185
|
+
NODE_ID_KEY = "__memorio_hlc_node";
|
|
1186
|
+
__name(getNodeId, "getNodeId");
|
|
1187
|
+
__name(generateNodeId, "generateNodeId");
|
|
1188
|
+
HLC_KEY = "__memorio_hlc_local";
|
|
1189
|
+
local = globalThis[HLC_KEY] || {
|
|
1190
|
+
physical: 0,
|
|
1191
|
+
logical: 0,
|
|
1192
|
+
node: getNodeId()
|
|
1193
|
+
};
|
|
1194
|
+
if (!globalThis[HLC_KEY]) {
|
|
1195
|
+
globalThis[HLC_KEY] = local;
|
|
1196
|
+
}
|
|
1197
|
+
__name(tick, "tick");
|
|
1198
|
+
__name(receive, "receive");
|
|
1199
|
+
__name(compare, "compare");
|
|
1200
|
+
__name(happenedBefore, "happenedBefore");
|
|
1201
|
+
__name(serialize, "serialize");
|
|
1202
|
+
__name(parse, "parse");
|
|
1203
|
+
exports.hlc = {
|
|
1204
|
+
tick,
|
|
1205
|
+
receive,
|
|
1206
|
+
compare,
|
|
1207
|
+
happenedBefore,
|
|
1208
|
+
serialize,
|
|
1209
|
+
parse,
|
|
1210
|
+
getNodeId
|
|
1211
|
+
};
|
|
1212
|
+
}
|
|
1213
|
+
});
|
|
1214
|
+
|
|
1098
1215
|
// functions/idb/tools/db.create.ts
|
|
1099
1216
|
var db_create_exports = {};
|
|
1100
1217
|
var init_db_create = __esm({
|
|
@@ -1733,13 +1850,41 @@ var init_db_support2 = __esm({
|
|
|
1733
1850
|
});
|
|
1734
1851
|
|
|
1735
1852
|
// functions/sqlite/tools/persist.ts
|
|
1853
|
+
var persist_exports = {};
|
|
1854
|
+
__export(persist_exports, {
|
|
1855
|
+
CHECKPOINT_INTERVAL: () => CHECKPOINT_INTERVAL,
|
|
1856
|
+
DEBOUNCE_MS: () => DEBOUNCE_MS,
|
|
1857
|
+
_attachJournal: () => _attachJournal,
|
|
1858
|
+
_checkpoint: () => _checkpoint,
|
|
1859
|
+
_clearJournal: () => _clearJournal,
|
|
1860
|
+
_journalAppend: () => _journalAppend,
|
|
1861
|
+
_journalCount: () => _journalCount,
|
|
1862
|
+
_journalEntries: () => _journalEntries,
|
|
1863
|
+
_journalReplay: () => _journalReplay,
|
|
1864
|
+
_loadCheckpoint: () => _loadCheckpoint,
|
|
1865
|
+
_removeCheckpoint: () => _removeCheckpoint,
|
|
1866
|
+
_saveCheckpoint: () => _saveCheckpoint,
|
|
1867
|
+
_scheduleCheckpoint: () => _scheduleCheckpoint
|
|
1868
|
+
});
|
|
1736
1869
|
function _ns() {
|
|
1737
1870
|
const cfg = g.sqliteConfig || {};
|
|
1738
1871
|
return cfg.namespace ? `ns:${cfg.namespace}` : "default";
|
|
1739
1872
|
}
|
|
1740
|
-
function
|
|
1873
|
+
function _dbKey(name) {
|
|
1741
1874
|
return `memorio:sqlite:db:${_ns()}:${name}`;
|
|
1742
1875
|
}
|
|
1876
|
+
function _journalKey(name, seq) {
|
|
1877
|
+
return `memorio:sqlite:journal:${_ns()}:${name}:${seq}`;
|
|
1878
|
+
}
|
|
1879
|
+
function _journalIndexKey(name) {
|
|
1880
|
+
return `memorio:sqlite:journal:${_ns()}:${name}:index`;
|
|
1881
|
+
}
|
|
1882
|
+
function _journalSeqKey(name) {
|
|
1883
|
+
return `memorio:sqlite:journal:${_ns()}:${name}:seq`;
|
|
1884
|
+
}
|
|
1885
|
+
function _opCounterKey(name) {
|
|
1886
|
+
return `memorio:sqlite:journal:${_ns()}:${name}:ops`;
|
|
1887
|
+
}
|
|
1743
1888
|
function _b64encode2(buf) {
|
|
1744
1889
|
const bytes = buf instanceof Uint8Array ? buf : new Uint8Array(buf);
|
|
1745
1890
|
let binary = "";
|
|
@@ -1752,35 +1897,81 @@ function _b64decode2(b64) {
|
|
|
1752
1897
|
if (typeof Buffer !== "undefined") {
|
|
1753
1898
|
return new Uint8Array(Buffer.from(b64, "base64"));
|
|
1754
1899
|
}
|
|
1755
|
-
if (typeof atob
|
|
1756
|
-
|
|
1900
|
+
if (typeof atob === "function") {
|
|
1901
|
+
const binary = atob(b64);
|
|
1902
|
+
const len = binary.length;
|
|
1903
|
+
const out = new Uint8Array(len);
|
|
1904
|
+
for (let i = 0; i < len; i++) out[i] = binary.charCodeAt(i);
|
|
1905
|
+
return out;
|
|
1757
1906
|
}
|
|
1758
|
-
|
|
1759
|
-
const len = binary.length;
|
|
1760
|
-
const out = new Uint8Array(len);
|
|
1761
|
-
for (let i = 0; i < len; i++) out[i] = binary.charCodeAt(i);
|
|
1762
|
-
return out;
|
|
1907
|
+
throw new Error("memorio sqlite: no base64 decoder available");
|
|
1763
1908
|
}
|
|
1764
|
-
function
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1909
|
+
function _nextSeq(name) {
|
|
1910
|
+
const seq = (exports.store.get(_journalSeqKey(name)) || 0) + 1;
|
|
1911
|
+
exports.store.set(_journalSeqKey(name), seq);
|
|
1912
|
+
return seq;
|
|
1913
|
+
}
|
|
1914
|
+
function _journalAppend(name, sql, params) {
|
|
1915
|
+
const seq = _nextSeq(name);
|
|
1916
|
+
const entry = {
|
|
1917
|
+
hlc: exports.hlc.serialize(exports.hlc.tick()),
|
|
1918
|
+
sql,
|
|
1919
|
+
params: params || [],
|
|
1920
|
+
seq,
|
|
1921
|
+
ts: Date.now()
|
|
1922
|
+
};
|
|
1923
|
+
exports.store.set(_journalKey(name, seq), entry);
|
|
1924
|
+
const idx = exports.store.get(_journalIndexKey(name)) || [];
|
|
1925
|
+
idx.push(seq);
|
|
1926
|
+
exports.store.set(_journalIndexKey(name), idx);
|
|
1927
|
+
return seq;
|
|
1928
|
+
}
|
|
1929
|
+
function _journalEntries(name) {
|
|
1930
|
+
const idx = exports.store.get(_journalIndexKey(name)) || [];
|
|
1931
|
+
const entries = [];
|
|
1932
|
+
for (const seq of idx) {
|
|
1933
|
+
const raw = exports.store.get(_journalKey(name, seq));
|
|
1934
|
+
if (raw && typeof raw === "object" && raw.hlc) entries.push(raw);
|
|
1935
|
+
}
|
|
1936
|
+
entries.sort((a, b) => {
|
|
1937
|
+
const ha = exports.hlc.parse(a.hlc);
|
|
1938
|
+
const hb = exports.hlc.parse(b.hlc);
|
|
1939
|
+
if (ha && hb) return exports.hlc.compare(ha, hb);
|
|
1940
|
+
return a.seq - b.seq;
|
|
1780
1941
|
});
|
|
1942
|
+
return entries;
|
|
1943
|
+
}
|
|
1944
|
+
function _journalCount(name) {
|
|
1945
|
+
const idx = exports.store.get(_journalIndexKey(name)) || [];
|
|
1946
|
+
return idx.length;
|
|
1781
1947
|
}
|
|
1782
|
-
function
|
|
1783
|
-
const
|
|
1948
|
+
function _clearJournal(name) {
|
|
1949
|
+
const idx = exports.store.get(_journalIndexKey(name)) || [];
|
|
1950
|
+
for (const seq of idx) {
|
|
1951
|
+
exports.store.remove(_journalKey(name, seq));
|
|
1952
|
+
}
|
|
1953
|
+
exports.store.remove(_journalIndexKey(name));
|
|
1954
|
+
exports.store.remove(_journalSeqKey(name));
|
|
1955
|
+
exports.store.remove(_opCounterKey(name));
|
|
1956
|
+
}
|
|
1957
|
+
async function _saveCheckpoint(name) {
|
|
1958
|
+
const db = g.__sqliteDbs && g.__sqliteDbs[name];
|
|
1959
|
+
if (!db || typeof db.export !== "function") return false;
|
|
1960
|
+
try {
|
|
1961
|
+
const blob = db.export();
|
|
1962
|
+
exports.store.set(_dbKey(name), _b64encode2(blob));
|
|
1963
|
+
message_default(`Memorio SQLite: checkpoint saved for '${name}'`);
|
|
1964
|
+
return true;
|
|
1965
|
+
} catch (err) {
|
|
1966
|
+
if (db && db.version === "bun:sqlite") {
|
|
1967
|
+
return false;
|
|
1968
|
+
}
|
|
1969
|
+
console.warn("Memorio SQLite: checkpoint failed", err);
|
|
1970
|
+
return false;
|
|
1971
|
+
}
|
|
1972
|
+
}
|
|
1973
|
+
function _loadCheckpoint(name) {
|
|
1974
|
+
const b64 = exports.store.get(_dbKey(name));
|
|
1784
1975
|
if (!b64) return null;
|
|
1785
1976
|
try {
|
|
1786
1977
|
return _b64decode2(b64);
|
|
@@ -1788,48 +1979,108 @@ function loadSnapshot(name) {
|
|
|
1788
1979
|
return null;
|
|
1789
1980
|
}
|
|
1790
1981
|
}
|
|
1791
|
-
function
|
|
1792
|
-
exports.store.remove(
|
|
1982
|
+
function _removeCheckpoint(name) {
|
|
1983
|
+
exports.store.remove(_dbKey(name));
|
|
1793
1984
|
}
|
|
1794
|
-
function
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
}
|
|
1804
|
-
}
|
|
1805
|
-
}
|
|
1806
|
-
|
|
1985
|
+
async function _journalReplay(name, db) {
|
|
1986
|
+
const entries = _journalEntries(name);
|
|
1987
|
+
if (!entries.length) return 0;
|
|
1988
|
+
let replayed = 0;
|
|
1989
|
+
for (const entry of entries) {
|
|
1990
|
+
try {
|
|
1991
|
+
db.run(entry.sql, entry.params || []);
|
|
1992
|
+
replayed++;
|
|
1993
|
+
} catch (err) {
|
|
1994
|
+
console.warn(`Memorio SQLite: failed to replay journal entry ${entry.seq} for '${name}'`, err);
|
|
1995
|
+
}
|
|
1996
|
+
}
|
|
1997
|
+
message_default(`Memorio SQLite: replayed ${replayed} journal entries for '${name}'`);
|
|
1998
|
+
return replayed;
|
|
1999
|
+
}
|
|
2000
|
+
function _scheduleCheckpoint(name) {
|
|
2001
|
+
if (_checkpointTimers[name]) clearTimeout(_checkpointTimers[name]);
|
|
2002
|
+
_checkpointTimers[name] = setTimeout(() => {
|
|
2003
|
+
_checkpointTimers[name] = null;
|
|
2004
|
+
void _checkpoint(name);
|
|
2005
|
+
}, DEBOUNCE_MS);
|
|
2006
|
+
}
|
|
2007
|
+
async function _checkpoint(name) {
|
|
2008
|
+
if (_checkpointTimers[name]) {
|
|
2009
|
+
clearTimeout(_checkpointTimers[name]);
|
|
2010
|
+
_checkpointTimers[name] = null;
|
|
2011
|
+
}
|
|
2012
|
+
const saved = await _saveCheckpoint(name);
|
|
2013
|
+
if (saved) _clearJournal(name);
|
|
2014
|
+
return saved;
|
|
2015
|
+
}
|
|
2016
|
+
function _attachJournal(name, db) {
|
|
2017
|
+
if (!db || typeof db.run !== "function") return;
|
|
2018
|
+
if (db.__memorioJournalAttached) return;
|
|
2019
|
+
db.__memorioJournalAttached = true;
|
|
2020
|
+
const originalRun = db.run.bind(db);
|
|
2021
|
+
db.run = function(sql, params) {
|
|
2022
|
+
const paramArr = Array.isArray(params) ? params : params !== void 0 ? [params] : [];
|
|
2023
|
+
_journalAppend(name, sql, paramArr);
|
|
2024
|
+
const ops = (exports.store.get(_opCounterKey(name)) || 0) + 1;
|
|
2025
|
+
exports.store.set(_opCounterKey(name), ops);
|
|
2026
|
+
if (ops >= CHECKPOINT_INTERVAL) {
|
|
2027
|
+
exports.store.set(_opCounterKey(name), 0);
|
|
2028
|
+
void _checkpoint(name);
|
|
2029
|
+
} else {
|
|
2030
|
+
_scheduleCheckpoint(name);
|
|
2031
|
+
}
|
|
2032
|
+
return originalRun(sql, params);
|
|
2033
|
+
};
|
|
2034
|
+
if (typeof db.updateHook === "function") {
|
|
2035
|
+
try {
|
|
2036
|
+
db.updateHook(() => {
|
|
2037
|
+
_scheduleCheckpoint(name);
|
|
2038
|
+
});
|
|
2039
|
+
} catch (err) {
|
|
2040
|
+
console.warn("Memorio SQLite: could not attach updateHook", err);
|
|
2041
|
+
}
|
|
1807
2042
|
}
|
|
1808
2043
|
}
|
|
1809
|
-
var g,
|
|
2044
|
+
var g, CHECKPOINT_INTERVAL, DEBOUNCE_MS, _checkpointTimers;
|
|
1810
2045
|
var init_persist = __esm({
|
|
1811
2046
|
"functions/sqlite/tools/persist.ts"() {
|
|
1812
2047
|
init_cjs_shims();
|
|
1813
2048
|
init_sqlite();
|
|
1814
2049
|
init_store();
|
|
2050
|
+
init_hlc();
|
|
1815
2051
|
init_message();
|
|
1816
2052
|
g = globalThis;
|
|
1817
|
-
|
|
1818
|
-
|
|
2053
|
+
CHECKPOINT_INTERVAL = 50;
|
|
2054
|
+
DEBOUNCE_MS = 250;
|
|
2055
|
+
_checkpointTimers = {};
|
|
1819
2056
|
__name(_ns, "_ns");
|
|
1820
|
-
__name(
|
|
2057
|
+
__name(_dbKey, "_dbKey");
|
|
2058
|
+
__name(_journalKey, "_journalKey");
|
|
2059
|
+
__name(_journalIndexKey, "_journalIndexKey");
|
|
2060
|
+
__name(_journalSeqKey, "_journalSeqKey");
|
|
2061
|
+
__name(_opCounterKey, "_opCounterKey");
|
|
1821
2062
|
__name(_b64encode2, "_b64encode");
|
|
1822
2063
|
__name(_b64decode2, "_b64decode");
|
|
1823
|
-
__name(
|
|
1824
|
-
__name(
|
|
1825
|
-
__name(
|
|
1826
|
-
__name(
|
|
2064
|
+
__name(_nextSeq, "_nextSeq");
|
|
2065
|
+
__name(_journalAppend, "_journalAppend");
|
|
2066
|
+
__name(_journalEntries, "_journalEntries");
|
|
2067
|
+
__name(_journalCount, "_journalCount");
|
|
2068
|
+
__name(_clearJournal, "_clearJournal");
|
|
2069
|
+
__name(_saveCheckpoint, "_saveCheckpoint");
|
|
2070
|
+
__name(_loadCheckpoint, "_loadCheckpoint");
|
|
2071
|
+
__name(_removeCheckpoint, "_removeCheckpoint");
|
|
2072
|
+
__name(_journalReplay, "_journalReplay");
|
|
2073
|
+
__name(_scheduleCheckpoint, "_scheduleCheckpoint");
|
|
2074
|
+
__name(_checkpoint, "_checkpoint");
|
|
2075
|
+
__name(_attachJournal, "_attachJournal");
|
|
1827
2076
|
exports.sqlite.db.persist = async (name = "memorio") => {
|
|
1828
|
-
return
|
|
2077
|
+
return _checkpoint(name);
|
|
2078
|
+
};
|
|
2079
|
+
exports.sqlite.db.flush = async (name = "memorio") => {
|
|
2080
|
+
return _checkpoint(name);
|
|
1829
2081
|
};
|
|
1830
2082
|
exports.sqlite.db.download = async (name = "memorio", filename = `${name}.sqlite`) => {
|
|
1831
|
-
const
|
|
1832
|
-
const db = g3.__sqliteDbs && g3.__sqliteDbs[name];
|
|
2083
|
+
const db = g.__sqliteDbs && g.__sqliteDbs[name];
|
|
1833
2084
|
if (!db) {
|
|
1834
2085
|
console.warn(`Memorio SQLite: cannot download '${name}' - database not found. Create it with sqlite.db.create('${name}').`);
|
|
1835
2086
|
return false;
|
|
@@ -1842,18 +2093,18 @@ var init_persist = __esm({
|
|
|
1842
2093
|
return false;
|
|
1843
2094
|
}
|
|
1844
2095
|
if (!blob) return false;
|
|
1845
|
-
if (typeof
|
|
2096
|
+
if (typeof g.document === "undefined" || typeof g.Blob === "undefined") {
|
|
1846
2097
|
console.warn("Memorio SQLite: sqlite.db.download() requires a browser environment.");
|
|
1847
2098
|
return false;
|
|
1848
2099
|
}
|
|
1849
|
-
const URL2 =
|
|
2100
|
+
const URL2 = g.URL || g.webkitURL;
|
|
1850
2101
|
if (!URL2 || typeof URL2.createObjectURL !== "function") return false;
|
|
1851
2102
|
const objectUrl = URL2.createObjectURL(new Blob([blob], { type: "application/x-sqlite3" }));
|
|
1852
|
-
const a =
|
|
2103
|
+
const a = g.document.createElement("a");
|
|
1853
2104
|
a.style.display = "none";
|
|
1854
2105
|
a.href = objectUrl;
|
|
1855
2106
|
a.download = filename;
|
|
1856
|
-
|
|
2107
|
+
g.document.body.appendChild(a);
|
|
1857
2108
|
a.click();
|
|
1858
2109
|
setTimeout(() => {
|
|
1859
2110
|
try {
|
|
@@ -1868,7 +2119,7 @@ var init_persist = __esm({
|
|
|
1868
2119
|
const db = g.__sqliteDbs && g.__sqliteDbs[name];
|
|
1869
2120
|
if (db && typeof db.close === "function") {
|
|
1870
2121
|
try {
|
|
1871
|
-
await
|
|
2122
|
+
await _checkpoint(name);
|
|
1872
2123
|
} catch (_) {
|
|
1873
2124
|
}
|
|
1874
2125
|
try {
|
|
@@ -1882,15 +2133,22 @@ var init_persist = __esm({
|
|
|
1882
2133
|
const idx = g.sqlbases.findIndex((x) => x && x.name === name);
|
|
1883
2134
|
if (idx > -1) g.sqlbases.splice(idx, 1);
|
|
1884
2135
|
}
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
_exportTimers[name] = null;
|
|
2136
|
+
if (_checkpointTimers[name]) {
|
|
2137
|
+
clearTimeout(_checkpointTimers[name]);
|
|
2138
|
+
_checkpointTimers[name] = null;
|
|
1889
2139
|
}
|
|
1890
|
-
removeSnapshot(name);
|
|
1891
2140
|
};
|
|
1892
|
-
exports.sqlite.db.loadSnapshot =
|
|
1893
|
-
exports.sqlite.db.saveSnapshot =
|
|
2141
|
+
exports.sqlite.db.loadSnapshot = _loadCheckpoint;
|
|
2142
|
+
exports.sqlite.db.saveSnapshot = _saveCheckpoint;
|
|
2143
|
+
exports.sqlite.db.removeSnapshot = _removeCheckpoint;
|
|
2144
|
+
exports.sqlite.db.journal = {
|
|
2145
|
+
append: _journalAppend,
|
|
2146
|
+
entries: _journalEntries,
|
|
2147
|
+
count: _journalCount,
|
|
2148
|
+
clear: _clearJournal,
|
|
2149
|
+
replay: _journalReplay,
|
|
2150
|
+
checkpoint: _checkpoint
|
|
2151
|
+
};
|
|
1894
2152
|
}
|
|
1895
2153
|
});
|
|
1896
2154
|
|
|
@@ -1908,7 +2166,7 @@ var init_db_create2 = __esm({
|
|
|
1908
2166
|
const persistence = !!(opts && opts.persistence) || !!(g3.sqliteConfig && g3.sqliteConfig.persistence);
|
|
1909
2167
|
let data;
|
|
1910
2168
|
if (persistence && !opts.data) {
|
|
1911
|
-
const restored =
|
|
2169
|
+
const restored = _loadCheckpoint(name);
|
|
1912
2170
|
if (restored) data = restored;
|
|
1913
2171
|
}
|
|
1914
2172
|
const useData = opts && opts.data || data;
|
|
@@ -1930,7 +2188,13 @@ var init_db_create2 = __esm({
|
|
|
1930
2188
|
} else {
|
|
1931
2189
|
g3.sqlbases.push({ name, db, persistence: createOpts.persistence });
|
|
1932
2190
|
}
|
|
1933
|
-
if (persistence
|
|
2191
|
+
if (persistence && !opts.data) {
|
|
2192
|
+
_attachJournal(name, db);
|
|
2193
|
+
const replayed = await _journalReplay(name, db);
|
|
2194
|
+
if (replayed > 0) {
|
|
2195
|
+
message_default(`Memorio SQLite: replayed ${replayed} journal entries for '${name}'`);
|
|
2196
|
+
}
|
|
2197
|
+
}
|
|
1934
2198
|
return db;
|
|
1935
2199
|
};
|
|
1936
2200
|
}
|
|
@@ -1961,9 +2225,14 @@ var init_db_delete2 = __esm({
|
|
|
1961
2225
|
init_cjs_shims();
|
|
1962
2226
|
init_sqlite();
|
|
1963
2227
|
init_message();
|
|
1964
|
-
|
|
2228
|
+
init_persist();
|
|
2229
|
+
exports.sqlite.db.delete = async (name) => {
|
|
1965
2230
|
const dbs = globalThis.__sqliteDbs;
|
|
1966
2231
|
if (!dbs || !dbs[name]) return false;
|
|
2232
|
+
try {
|
|
2233
|
+
await _checkpoint(name);
|
|
2234
|
+
} catch (_) {
|
|
2235
|
+
}
|
|
1967
2236
|
try {
|
|
1968
2237
|
dbs[name].close();
|
|
1969
2238
|
} catch (_) {
|
|
@@ -1971,6 +2240,7 @@ var init_db_delete2 = __esm({
|
|
|
1971
2240
|
delete dbs[name];
|
|
1972
2241
|
const g3 = globalThis;
|
|
1973
2242
|
if (g3.sqlbases) g3.sqlbases = g3.sqlbases.filter((x) => x && x.name !== name);
|
|
2243
|
+
_removeCheckpoint(name);
|
|
1974
2244
|
message_default(`Memorio SQLite: database '${name}' deleted`);
|
|
1975
2245
|
return true;
|
|
1976
2246
|
};
|
|
@@ -2115,7 +2385,7 @@ var init_data_get2 = __esm({
|
|
|
2115
2385
|
});
|
|
2116
2386
|
|
|
2117
2387
|
// functions/sqlite/index.ts
|
|
2118
|
-
var _hasSqlJs, _hasBunSqlite; exports.sqlite = void 0;
|
|
2388
|
+
var _hasSqlJs, _hasBunSqlite; exports.sqlite = void 0; var _toolsLoaded;
|
|
2119
2389
|
var init_sqlite = __esm({
|
|
2120
2390
|
"functions/sqlite/index.ts"() {
|
|
2121
2391
|
init_cjs_shims();
|
|
@@ -2128,6 +2398,7 @@ var init_sqlite = __esm({
|
|
|
2128
2398
|
query: {},
|
|
2129
2399
|
data: {}
|
|
2130
2400
|
};
|
|
2401
|
+
_toolsLoaded = Promise.resolve();
|
|
2131
2402
|
exports.sqlite.config = (opts) => {
|
|
2132
2403
|
const g3 = globalThis;
|
|
2133
2404
|
g3.sqliteConfig = { ...g3.sqliteConfig, ...opts };
|
|
@@ -2142,15 +2413,19 @@ var init_sqlite = __esm({
|
|
|
2142
2413
|
get: /* @__PURE__ */ __name(() => {
|
|
2143
2414
|
const g3 = globalThis;
|
|
2144
2415
|
if (!g3.sqliteReady) {
|
|
2145
|
-
g3.sqliteReady =
|
|
2416
|
+
g3.sqliteReady = _toolsLoaded.then(() => {
|
|
2417
|
+
if (typeof exports.sqlite.db.getSQL !== "function") return;
|
|
2418
|
+
return exports.sqlite.db.getSQL().then(() => {
|
|
2419
|
+
}).catch(() => {
|
|
2420
|
+
});
|
|
2146
2421
|
}).catch(() => {
|
|
2147
|
-
})
|
|
2422
|
+
});
|
|
2148
2423
|
}
|
|
2149
2424
|
return g3.sqliteReady;
|
|
2150
2425
|
}, "get")
|
|
2151
2426
|
});
|
|
2152
2427
|
if (_hasSqlJs || _hasBunSqlite) {
|
|
2153
|
-
(async () => {
|
|
2428
|
+
_toolsLoaded = (async () => {
|
|
2154
2429
|
Object.defineProperty(exports.sqlite, "db", { value: {}, writable: false, configurable: true, enumerable: true });
|
|
2155
2430
|
Object.defineProperty(exports.sqlite, "query", { value: {}, writable: false, configurable: true, enumerable: true });
|
|
2156
2431
|
Object.defineProperty(exports.sqlite, "data", { value: {}, writable: false, configurable: true, enumerable: true });
|
|
@@ -2162,6 +2437,7 @@ var init_sqlite = __esm({
|
|
|
2162
2437
|
});
|
|
2163
2438
|
await Promise.all([
|
|
2164
2439
|
Promise.resolve().then(() => (init_db_support2(), db_support_exports2)),
|
|
2440
|
+
Promise.resolve().then(() => (init_persist(), persist_exports)),
|
|
2165
2441
|
Promise.resolve().then(() => (init_db_create2(), db_create_exports2)),
|
|
2166
2442
|
Promise.resolve().then(() => (init_db_get(), db_get_exports)),
|
|
2167
2443
|
Promise.resolve().then(() => (init_db_delete2(), db_delete_exports2)),
|
|
@@ -2382,133 +2658,23 @@ init_env();
|
|
|
2382
2658
|
// core/mutation/engine.ts
|
|
2383
2659
|
init_cjs_shims();
|
|
2384
2660
|
init_internal();
|
|
2385
|
-
|
|
2386
|
-
// core/hlc.ts
|
|
2387
|
-
init_cjs_shims();
|
|
2388
|
-
var NODE_ID_KEY = "__memorio_hlc_node";
|
|
2389
|
-
function getNodeId() {
|
|
2390
|
-
if (typeof globalThis === "undefined") return "node";
|
|
2391
|
-
try {
|
|
2392
|
-
if (typeof sessionStorage !== "undefined" && sessionStorage) {
|
|
2393
|
-
let id = sessionStorage.getItem(NODE_ID_KEY);
|
|
2394
|
-
if (!id) {
|
|
2395
|
-
id = generateNodeId();
|
|
2396
|
-
sessionStorage.setItem(NODE_ID_KEY, id);
|
|
2397
|
-
}
|
|
2398
|
-
return id;
|
|
2399
|
-
}
|
|
2400
|
-
} catch {
|
|
2401
|
-
}
|
|
2402
|
-
if (!globalThis.__memorioHlcNodeId) {
|
|
2403
|
-
globalThis.__memorioHlcNodeId = generateNodeId();
|
|
2404
|
-
}
|
|
2405
|
-
return globalThis.__memorioHlcNodeId;
|
|
2406
|
-
}
|
|
2407
|
-
__name(getNodeId, "getNodeId");
|
|
2408
|
-
function generateNodeId() {
|
|
2409
|
-
if (typeof crypto !== "undefined" && crypto.randomUUID) {
|
|
2410
|
-
return crypto.randomUUID().slice(0, 8);
|
|
2411
|
-
}
|
|
2412
|
-
return Math.random().toString(36).slice(2, 10);
|
|
2413
|
-
}
|
|
2414
|
-
__name(generateNodeId, "generateNodeId");
|
|
2415
|
-
var HLC_KEY = "__memorio_hlc_local";
|
|
2416
|
-
var local = globalThis[HLC_KEY] || {
|
|
2417
|
-
physical: 0,
|
|
2418
|
-
logical: 0,
|
|
2419
|
-
node: getNodeId()
|
|
2420
|
-
};
|
|
2421
|
-
if (!globalThis[HLC_KEY]) {
|
|
2422
|
-
globalThis[HLC_KEY] = local;
|
|
2423
|
-
}
|
|
2424
|
-
function tick(_incoming) {
|
|
2425
|
-
const now2 = Date.now();
|
|
2426
|
-
const base = local.physical;
|
|
2427
|
-
let physical;
|
|
2428
|
-
let logical;
|
|
2429
|
-
if (now2 > base) {
|
|
2430
|
-
physical = now2;
|
|
2431
|
-
logical = 0;
|
|
2432
|
-
} else if (now2 < base) {
|
|
2433
|
-
physical = base;
|
|
2434
|
-
logical = local.logical + 1;
|
|
2435
|
-
} else {
|
|
2436
|
-
physical = base;
|
|
2437
|
-
logical = local.logical + 1;
|
|
2438
|
-
}
|
|
2439
|
-
local.physical = physical;
|
|
2440
|
-
local.logical = logical;
|
|
2441
|
-
return { physical, logical, node: local.node };
|
|
2442
|
-
}
|
|
2443
|
-
__name(tick, "tick");
|
|
2444
|
-
function receive(remote) {
|
|
2445
|
-
const now2 = Date.now();
|
|
2446
|
-
if (remote.physical > local.physical) {
|
|
2447
|
-
local.physical = remote.physical > now2 ? remote.physical : now2;
|
|
2448
|
-
local.logical = 1;
|
|
2449
|
-
} else if (remote.physical === local.physical) {
|
|
2450
|
-
local.logical = Math.max(local.logical, remote.logical) + 1;
|
|
2451
|
-
}
|
|
2452
|
-
local.node = getNodeId();
|
|
2453
|
-
return { ...local };
|
|
2454
|
-
}
|
|
2455
|
-
__name(receive, "receive");
|
|
2456
|
-
function compare(a, b) {
|
|
2457
|
-
if (a.physical !== b.physical) {
|
|
2458
|
-
return a.physical < b.physical ? -1 : 1;
|
|
2459
|
-
}
|
|
2460
|
-
if (a.logical !== b.logical) {
|
|
2461
|
-
return a.logical < b.logical ? -1 : 1;
|
|
2462
|
-
}
|
|
2463
|
-
if (a.node !== b.node) {
|
|
2464
|
-
return a.node < b.node ? -1 : 1;
|
|
2465
|
-
}
|
|
2466
|
-
return 0;
|
|
2467
|
-
}
|
|
2468
|
-
__name(compare, "compare");
|
|
2469
|
-
function happenedBefore(a, b) {
|
|
2470
|
-
return compare(a, b) < 0;
|
|
2471
|
-
}
|
|
2472
|
-
__name(happenedBefore, "happenedBefore");
|
|
2473
|
-
function serialize(hlc2) {
|
|
2474
|
-
return `hlc:${hlc2.physical}:${hlc2.logical}:${hlc2.node}`;
|
|
2475
|
-
}
|
|
2476
|
-
__name(serialize, "serialize");
|
|
2477
|
-
function parse(serialized) {
|
|
2478
|
-
if (!serialized.startsWith("hlc:")) return null;
|
|
2479
|
-
const parts = serialized.split(":");
|
|
2480
|
-
if (parts.length !== 4) return null;
|
|
2481
|
-
const physical = parseInt(parts[1], 10);
|
|
2482
|
-
const logical = parseInt(parts[2], 10);
|
|
2483
|
-
const node = parts[3];
|
|
2484
|
-
if (isNaN(physical) || isNaN(logical)) return null;
|
|
2485
|
-
return { physical, logical, node };
|
|
2486
|
-
}
|
|
2487
|
-
__name(parse, "parse");
|
|
2488
|
-
var hlc = {
|
|
2489
|
-
tick,
|
|
2490
|
-
receive,
|
|
2491
|
-
compare,
|
|
2492
|
-
happenedBefore,
|
|
2493
|
-
serialize,
|
|
2494
|
-
parse,
|
|
2495
|
-
getNodeId
|
|
2496
|
-
};
|
|
2661
|
+
init_hlc();
|
|
2497
2662
|
|
|
2498
2663
|
// core/mutation/id.ts
|
|
2499
2664
|
init_cjs_shims();
|
|
2665
|
+
init_hlc();
|
|
2500
2666
|
function generateMutationId() {
|
|
2501
|
-
const now2 = hlc.tick();
|
|
2667
|
+
const now2 = exports.hlc.tick();
|
|
2502
2668
|
return "m_" + encodeHLC(now2);
|
|
2503
2669
|
}
|
|
2504
2670
|
__name(generateMutationId, "generateMutationId");
|
|
2505
2671
|
function generateTransactionId() {
|
|
2506
|
-
const now2 = hlc.tick();
|
|
2672
|
+
const now2 = exports.hlc.tick();
|
|
2507
2673
|
return "tx_" + encodeHLC(now2);
|
|
2508
2674
|
}
|
|
2509
2675
|
__name(generateTransactionId, "generateTransactionId");
|
|
2510
2676
|
function generateCommitId() {
|
|
2511
|
-
const now2 = hlc.tick();
|
|
2677
|
+
const now2 = exports.hlc.tick();
|
|
2512
2678
|
return "c_" + encodeHLC(now2);
|
|
2513
2679
|
}
|
|
2514
2680
|
__name(generateCommitId, "generateCommitId");
|
|
@@ -2588,6 +2754,7 @@ __name(mergePatches, "mergePatches");
|
|
|
2588
2754
|
// core/mutation/transaction.ts
|
|
2589
2755
|
init_cjs_shims();
|
|
2590
2756
|
init_internal();
|
|
2757
|
+
init_hlc();
|
|
2591
2758
|
var TX_REGISTRY_KEY = "__memorio_transaction_registry__";
|
|
2592
2759
|
var registry = globalThis[TX_REGISTRY_KEY] || /* @__PURE__ */ new Map();
|
|
2593
2760
|
if (!globalThis[TX_REGISTRY_KEY]) {
|
|
@@ -2614,7 +2781,7 @@ function beginTransaction(source, message2, metadata) {
|
|
|
2614
2781
|
const id = generateTransactionId();
|
|
2615
2782
|
const ctx = {
|
|
2616
2783
|
id,
|
|
2617
|
-
startedAt: hlc.serialize(hlc.tick()),
|
|
2784
|
+
startedAt: exports.hlc.serialize(exports.hlc.tick()),
|
|
2618
2785
|
source,
|
|
2619
2786
|
message: message2,
|
|
2620
2787
|
metadata,
|
|
@@ -2642,7 +2809,7 @@ function commitTransaction() {
|
|
|
2642
2809
|
const ctx = registry.get(txId);
|
|
2643
2810
|
if (!ctx || !ctx.active) return null;
|
|
2644
2811
|
ctx.active = false;
|
|
2645
|
-
ctx.committedAt = hlc.serialize(hlc.tick());
|
|
2812
|
+
ctx.committedAt = exports.hlc.serialize(exports.hlc.tick());
|
|
2646
2813
|
const tx = {
|
|
2647
2814
|
id: ctx.id,
|
|
2648
2815
|
mutations: ctx.mutations,
|
|
@@ -2722,7 +2889,7 @@ function abortTransaction() {
|
|
|
2722
2889
|
}
|
|
2723
2890
|
}
|
|
2724
2891
|
ctx.active = false;
|
|
2725
|
-
ctx.committedAt = hlc.serialize(hlc.tick());
|
|
2892
|
+
ctx.committedAt = exports.hlc.serialize(exports.hlc.tick());
|
|
2726
2893
|
const tx = {
|
|
2727
2894
|
id: ctx.id,
|
|
2728
2895
|
mutations: ctx.mutations,
|
|
@@ -2780,11 +2947,11 @@ __name(registerMutationInTransaction, "registerMutationInTransaction");
|
|
|
2780
2947
|
|
|
2781
2948
|
// core/mutation/engine.ts
|
|
2782
2949
|
function currentTimestamp() {
|
|
2783
|
-
return hlc.serialize(hlc.tick());
|
|
2950
|
+
return exports.hlc.serialize(exports.hlc.tick());
|
|
2784
2951
|
}
|
|
2785
2952
|
__name(currentTimestamp, "currentTimestamp");
|
|
2786
2953
|
function createMutation(path, operation, before2, after2, options) {
|
|
2787
|
-
const now2 = hlc.tick();
|
|
2954
|
+
const now2 = exports.hlc.tick();
|
|
2788
2955
|
const mutation = {
|
|
2789
2956
|
id: generateMutationId(),
|
|
2790
2957
|
path,
|
|
@@ -2792,7 +2959,7 @@ function createMutation(path, operation, before2, after2, options) {
|
|
|
2792
2959
|
after: after2,
|
|
2793
2960
|
before: before2,
|
|
2794
2961
|
timestamp: now2.physical,
|
|
2795
|
-
hlc: hlc.serialize(now2),
|
|
2962
|
+
hlc: exports.hlc.serialize(now2),
|
|
2796
2963
|
context: options?.context || exports.internal.currentContext || void 0,
|
|
2797
2964
|
source: options?.source,
|
|
2798
2965
|
transactionId: options?.transactionId || (operation !== "length" ? tagForTransaction() : void 0),
|
|
@@ -3157,19 +3324,39 @@ if (globalThis[STATE_INSTANCE_KEY]) {
|
|
|
3157
3324
|
const initial = exports.store.get(key);
|
|
3158
3325
|
if (initial !== void 0 && initial !== null) {
|
|
3159
3326
|
let acc = exports.state;
|
|
3327
|
+
let seedOk = true;
|
|
3160
3328
|
for (let i = 0; i < segments.length - 1; i++) {
|
|
3161
3329
|
const seg = segments[i];
|
|
3162
|
-
|
|
3163
|
-
|
|
3330
|
+
const existing = acc[seg];
|
|
3331
|
+
if (existing === void 0 || existing === null) {
|
|
3332
|
+
acc[seg] = {};
|
|
3333
|
+
acc = acc[seg];
|
|
3334
|
+
} else if (typeof existing === "object") {
|
|
3335
|
+
acc = existing;
|
|
3336
|
+
} else {
|
|
3337
|
+
if (exports.DEV) {
|
|
3338
|
+
console.warn(
|
|
3339
|
+
`[memorio state] state.persist('${normalized}') aborted seeding: '${seg}' already holds a primitive value (${typeof existing}), cannot nest '${segments.slice(i + 1).join(".")}' under it.`
|
|
3340
|
+
);
|
|
3341
|
+
}
|
|
3342
|
+
seedOk = false;
|
|
3343
|
+
break;
|
|
3344
|
+
}
|
|
3345
|
+
}
|
|
3346
|
+
if (seedOk && acc) {
|
|
3347
|
+
acc[segments[segments.length - 1]] = deepRaw(initial);
|
|
3164
3348
|
}
|
|
3165
|
-
if (acc) acc[segments[segments.length - 1]] = deepRaw(initial);
|
|
3166
3349
|
}
|
|
3167
3350
|
const handler = /* @__PURE__ */ __name(() => {
|
|
3168
3351
|
let current = exports.state;
|
|
3169
3352
|
for (const seg of segments) {
|
|
3170
3353
|
current = current === void 0 || current === null ? void 0 : current[seg];
|
|
3171
3354
|
}
|
|
3172
|
-
|
|
3355
|
+
if (current === void 0) {
|
|
3356
|
+
exports.store.remove(key);
|
|
3357
|
+
} else {
|
|
3358
|
+
exports.store.set(key, deepRaw(current));
|
|
3359
|
+
}
|
|
3173
3360
|
}, "handler");
|
|
3174
3361
|
const off = dispatch.listen(normalized, handler);
|
|
3175
3362
|
return typeof off === "function" ? off : () => {
|
|
@@ -3589,8 +3776,7 @@ var observerFunction = /* @__PURE__ */ __name((s, cb = null, option = true) => {
|
|
|
3589
3776
|
}
|
|
3590
3777
|
if (s && cb) {
|
|
3591
3778
|
const resolvedPath = resolvePath(s);
|
|
3592
|
-
if (!resolvedPath)
|
|
3593
|
-
if (typeof s !== "string" && !(s && typeof s === "object")) {
|
|
3779
|
+
if (!resolvedPath) {
|
|
3594
3780
|
console.error(`Observer Error: name of state need to be a 'string' like 'state.test' or a direct state value`);
|
|
3595
3781
|
return;
|
|
3596
3782
|
}
|
|
@@ -3640,7 +3826,7 @@ Object.defineProperties(
|
|
|
3640
3826
|
* @returns void
|
|
3641
3827
|
*/
|
|
3642
3828
|
removeAll: {
|
|
3643
|
-
|
|
3829
|
+
value: /* @__PURE__ */ __name(() => {
|
|
3644
3830
|
const obs = globalThis.observer;
|
|
3645
3831
|
const list = obs?.list;
|
|
3646
3832
|
if (list) {
|
|
@@ -3649,7 +3835,7 @@ Object.defineProperties(
|
|
|
3649
3835
|
});
|
|
3650
3836
|
}
|
|
3651
3837
|
return true;
|
|
3652
|
-
}, "
|
|
3838
|
+
}, "value")
|
|
3653
3839
|
},
|
|
3654
3840
|
///
|
|
3655
3841
|
/**
|
|
@@ -3679,40 +3865,58 @@ var useObserver = /* @__PURE__ */ __name(((func, deps = []) => {
|
|
|
3679
3865
|
const unsubs = [];
|
|
3680
3866
|
const depList = Array.isArray(deps) && !deps.__path ? deps : [deps];
|
|
3681
3867
|
try {
|
|
3682
|
-
depList.
|
|
3683
|
-
|
|
3684
|
-
|
|
3685
|
-
|
|
3686
|
-
|
|
3687
|
-
}
|
|
3688
|
-
t = dep.__path || (typeof dep.toString === "function" && dep.toString() !== "[object Object]" ? dep.toString() : "");
|
|
3868
|
+
if (depList.length === 0) {
|
|
3869
|
+
exports.internal.clearTrackedPaths();
|
|
3870
|
+
exports.internal.tracking = true;
|
|
3871
|
+
try {
|
|
3872
|
+
func();
|
|
3873
|
+
} catch (_) {
|
|
3689
3874
|
}
|
|
3690
|
-
|
|
3691
|
-
|
|
3692
|
-
const path = t.startsWith("store.") || t.startsWith("state.") ? t : "state." + t;
|
|
3875
|
+
exports.internal.tracking = false;
|
|
3876
|
+
exports.internal.trackedPaths.forEach((path) => {
|
|
3693
3877
|
if (!handlers.includes(path)) {
|
|
3878
|
+
message_default(`useObserver: [Auto-Discovery] -> "${path}"`);
|
|
3694
3879
|
handlers.push(path);
|
|
3695
3880
|
const off = dispatch.listen(path, () => func());
|
|
3696
3881
|
if (typeof off === "function") unsubs.push({ path, off });
|
|
3697
3882
|
}
|
|
3698
|
-
}
|
|
3699
|
-
|
|
3700
|
-
|
|
3701
|
-
|
|
3702
|
-
|
|
3703
|
-
|
|
3883
|
+
});
|
|
3884
|
+
} else {
|
|
3885
|
+
depList.forEach((dep) => {
|
|
3886
|
+
let t = "";
|
|
3887
|
+
if (typeof dep === "function") {
|
|
3888
|
+
const result = exports.propertyName(dep);
|
|
3889
|
+
if (result) t = result;
|
|
3890
|
+
} else if (typeof dep === "object" && dep !== null) {
|
|
3891
|
+
t = dep.__path || (typeof dep.toString === "function" && dep.toString() !== "[object Object]" ? dep.toString() : "");
|
|
3704
3892
|
}
|
|
3705
|
-
|
|
3706
|
-
|
|
3893
|
+
if (t) {
|
|
3894
|
+
t = t.replace(/^(\(\)\s*=>\s*)/, "").replace(/^globalThis\./, "").replace(/^state\./, "");
|
|
3895
|
+
const path = t.startsWith("store.") || t.startsWith("state.") ? t : "state." + t;
|
|
3707
3896
|
if (!handlers.includes(path)) {
|
|
3708
|
-
message_default(`useObserver: [Auto-Discovery] -> "${path}"`);
|
|
3709
3897
|
handlers.push(path);
|
|
3710
3898
|
const off = dispatch.listen(path, () => func());
|
|
3711
3899
|
if (typeof off === "function") unsubs.push({ path, off });
|
|
3712
3900
|
}
|
|
3713
|
-
}
|
|
3714
|
-
|
|
3715
|
-
|
|
3901
|
+
} else {
|
|
3902
|
+
exports.internal.clearTrackedPaths();
|
|
3903
|
+
exports.internal.tracking = true;
|
|
3904
|
+
try {
|
|
3905
|
+
func();
|
|
3906
|
+
} catch (_) {
|
|
3907
|
+
}
|
|
3908
|
+
exports.internal.tracking = false;
|
|
3909
|
+
exports.internal.trackedPaths.forEach((path) => {
|
|
3910
|
+
if (!handlers.includes(path)) {
|
|
3911
|
+
message_default(`useObserver: [Auto-Discovery] -> "${path}"`);
|
|
3912
|
+
handlers.push(path);
|
|
3913
|
+
const off = dispatch.listen(path, () => func());
|
|
3914
|
+
if (typeof off === "function") unsubs.push({ path, off });
|
|
3915
|
+
}
|
|
3916
|
+
});
|
|
3917
|
+
}
|
|
3918
|
+
});
|
|
3919
|
+
}
|
|
3716
3920
|
if (handlers.length > 0) {
|
|
3717
3921
|
message_default(`useObserver: Monitoring ${handlers.length} paths.`);
|
|
3718
3922
|
} else {
|
|
@@ -4199,8 +4403,8 @@ function isAuditActive() {
|
|
|
4199
4403
|
return _auditLogger !== null;
|
|
4200
4404
|
}
|
|
4201
4405
|
__name(isAuditActive, "isAuditActive");
|
|
4202
|
-
function setAuditLogger(
|
|
4203
|
-
_auditLogger =
|
|
4406
|
+
function setAuditLogger(logger2) {
|
|
4407
|
+
_auditLogger = logger2;
|
|
4204
4408
|
}
|
|
4205
4409
|
__name(setAuditLogger, "setAuditLogger");
|
|
4206
4410
|
function logAudit(entry) {
|
|
@@ -4443,6 +4647,9 @@ var logger = {
|
|
|
4443
4647
|
exportLogs
|
|
4444
4648
|
};
|
|
4445
4649
|
|
|
4650
|
+
// index.ts
|
|
4651
|
+
init_hlc();
|
|
4652
|
+
|
|
4446
4653
|
// core/fractional.ts
|
|
4447
4654
|
init_cjs_shims();
|
|
4448
4655
|
function generateBetween(left, right, minChars = 2) {
|
|
@@ -4635,7 +4842,7 @@ init_internal();
|
|
|
4635
4842
|
|
|
4636
4843
|
// package.json
|
|
4637
4844
|
var package_default = {
|
|
4638
|
-
version: "5.1.
|
|
4845
|
+
version: "5.1.4"};
|
|
4639
4846
|
|
|
4640
4847
|
// core/global.ts
|
|
4641
4848
|
init_env();
|
|
@@ -4656,10 +4863,12 @@ __name(typed, "typed");
|
|
|
4656
4863
|
init_cjs_shims();
|
|
4657
4864
|
init_store();
|
|
4658
4865
|
init_idb();
|
|
4866
|
+
init_hlc();
|
|
4659
4867
|
init_env();
|
|
4660
4868
|
|
|
4661
4869
|
// functions/memory/journal.ts
|
|
4662
4870
|
init_cjs_shims();
|
|
4871
|
+
init_hlc();
|
|
4663
4872
|
init_env();
|
|
4664
4873
|
init_store();
|
|
4665
4874
|
var g2 = globalThis;
|
|
@@ -4697,7 +4906,7 @@ var journal = {
|
|
|
4697
4906
|
updatedAt: entry.updatedAt || Date.now(),
|
|
4698
4907
|
version: entry.version || 1,
|
|
4699
4908
|
sync: "pending",
|
|
4700
|
-
hlc: entry.hlc || hlc.serialize(hlc.tick())
|
|
4909
|
+
hlc: entry.hlc || exports.hlc.serialize(exports.hlc.tick())
|
|
4701
4910
|
};
|
|
4702
4911
|
const key = `${STORE_PREFIX(ns)}${row.id}`;
|
|
4703
4912
|
exports.store.set(key, row);
|
|
@@ -4719,9 +4928,9 @@ var journal = {
|
|
|
4719
4928
|
}
|
|
4720
4929
|
out.sort((a, b) => {
|
|
4721
4930
|
if (a.hlc && b.hlc) {
|
|
4722
|
-
const ha = hlc.parse(a.hlc);
|
|
4723
|
-
const hb = hlc.parse(b.hlc);
|
|
4724
|
-
if (ha && hb) return hlc.compare(ha, hb);
|
|
4931
|
+
const ha = exports.hlc.parse(a.hlc);
|
|
4932
|
+
const hb = exports.hlc.parse(b.hlc);
|
|
4933
|
+
if (ha && hb) return exports.hlc.compare(ha, hb);
|
|
4725
4934
|
}
|
|
4726
4935
|
return (a.updatedAt || 0) - (b.updatedAt || 0);
|
|
4727
4936
|
});
|
|
@@ -4797,15 +5006,15 @@ var journal = {
|
|
|
4797
5006
|
const remote = await provider.pull(since);
|
|
4798
5007
|
for (const e of remote || []) {
|
|
4799
5008
|
if (e.hlc) {
|
|
4800
|
-
const parsed = hlc.parse(e.hlc);
|
|
4801
|
-
if (parsed) hlc.receive(parsed);
|
|
5009
|
+
const parsed = exports.hlc.parse(e.hlc);
|
|
5010
|
+
if (parsed) exports.hlc.receive(parsed);
|
|
4802
5011
|
}
|
|
4803
5012
|
const localEntry = await this.get(e.id);
|
|
4804
5013
|
if (localEntry && e.hlc && localEntry.hlc) {
|
|
4805
|
-
const ha = hlc.parse(localEntry.hlc);
|
|
4806
|
-
const hb = hlc.parse(e.hlc);
|
|
5014
|
+
const ha = exports.hlc.parse(localEntry.hlc);
|
|
5015
|
+
const hb = exports.hlc.parse(e.hlc);
|
|
4807
5016
|
if (ha && hb) {
|
|
4808
|
-
const cmp = hlc.compare(ha, hb);
|
|
5017
|
+
const cmp = exports.hlc.compare(ha, hb);
|
|
4809
5018
|
if (cmp === 0) {
|
|
4810
5019
|
const hasResolver = typeof provider.resolveConflict === "function";
|
|
4811
5020
|
const resolution = hasResolver ? await provider.resolveConflict(localEntry, e) : "remote";
|
|
@@ -5147,7 +5356,7 @@ var memory = {
|
|
|
5147
5356
|
version: existing ? existing.version ? existing.version + 1 : 1 : 1,
|
|
5148
5357
|
sync: isSyncEnabled2() ? "pending" : void 0,
|
|
5149
5358
|
supersededId: existing ? existing.id : null,
|
|
5150
|
-
hlc: hlc.serialize(hlc.tick())
|
|
5359
|
+
hlc: exports.hlc.serialize(exports.hlc.tick())
|
|
5151
5360
|
};
|
|
5152
5361
|
await saveToScope(entry);
|
|
5153
5362
|
await recordJournal(entry, "remember");
|
|
@@ -5200,7 +5409,7 @@ var memory = {
|
|
|
5200
5409
|
updatedAt: now(),
|
|
5201
5410
|
version: 1,
|
|
5202
5411
|
sync: isSyncEnabled2() ? "pending" : void 0,
|
|
5203
|
-
hlc: hlc.serialize(hlc.tick())
|
|
5412
|
+
hlc: exports.hlc.serialize(exports.hlc.tick())
|
|
5204
5413
|
};
|
|
5205
5414
|
await recordJournal(tombstone2, "delete");
|
|
5206
5415
|
return;
|
|
@@ -5214,7 +5423,7 @@ var memory = {
|
|
|
5214
5423
|
updatedAt: now(),
|
|
5215
5424
|
version: (entry.version ? entry.version : 0) + 1,
|
|
5216
5425
|
sync: isSyncEnabled2() ? "pending" : void 0,
|
|
5217
|
-
hlc: hlc.serialize(hlc.tick())
|
|
5426
|
+
hlc: exports.hlc.serialize(exports.hlc.tick())
|
|
5218
5427
|
};
|
|
5219
5428
|
await recordJournal(tombstone, "delete");
|
|
5220
5429
|
dispatch.set("memorio:memory:forgotten", { key });
|
|
@@ -5243,7 +5452,7 @@ var memory = {
|
|
|
5243
5452
|
updatedAt: now(),
|
|
5244
5453
|
version: (existing.version ? existing.version : 0) + 1,
|
|
5245
5454
|
sync: isSyncEnabled2() ? "pending" : void 0,
|
|
5246
|
-
hlc: hlc.serialize(hlc.tick()),
|
|
5455
|
+
hlc: exports.hlc.serialize(exports.hlc.tick()),
|
|
5247
5456
|
...opts.scope ? { scope: opts.scope } : {},
|
|
5248
5457
|
...opts.ttl !== void 0 ? { ttl: opts.ttl } : {},
|
|
5249
5458
|
...opts.type ? { type: opts.type } : {},
|
|
@@ -5309,24 +5518,27 @@ var memory = {
|
|
|
5309
5518
|
async patch(key, patches) {
|
|
5310
5519
|
const entry = await this._getEntry(key);
|
|
5311
5520
|
if (!entry) return;
|
|
5521
|
+
const patchedValue = JSON.parse(JSON.stringify(entry.value ?? {}));
|
|
5312
5522
|
let patched = false;
|
|
5313
5523
|
for (const p of patches) {
|
|
5524
|
+
const relativePath = p.path.replace(/^[^.]+\./, "");
|
|
5314
5525
|
if (p.op === "set") {
|
|
5315
|
-
setNestedPath(
|
|
5526
|
+
setNestedPath(patchedValue, relativePath, p.value);
|
|
5316
5527
|
patched = true;
|
|
5317
5528
|
} else if (p.op === "delete") {
|
|
5318
|
-
deleteNestedPath(
|
|
5529
|
+
deleteNestedPath(patchedValue, relativePath);
|
|
5319
5530
|
patched = true;
|
|
5320
5531
|
}
|
|
5321
5532
|
}
|
|
5322
5533
|
if (!patched) return;
|
|
5323
5534
|
const updated = {
|
|
5324
5535
|
...entry,
|
|
5536
|
+
value: patchedValue,
|
|
5325
5537
|
updatedAt: now(),
|
|
5326
5538
|
lastConfirmedAt: now(),
|
|
5327
5539
|
version: (entry.version ? entry.version : 0) + 1,
|
|
5328
5540
|
sync: isSyncEnabled2() ? "pending" : void 0,
|
|
5329
|
-
hlc: hlc.serialize(hlc.tick()),
|
|
5541
|
+
hlc: exports.hlc.serialize(exports.hlc.tick()),
|
|
5330
5542
|
patches,
|
|
5331
5543
|
path: patches[0]?.path
|
|
5332
5544
|
};
|
|
@@ -5572,6 +5784,7 @@ if (!("memorio" in globalThis)) {
|
|
|
5572
5784
|
);
|
|
5573
5785
|
memorio.memory = memory;
|
|
5574
5786
|
memorio.encryption = exports.encryption;
|
|
5787
|
+
memorio.sqlite = exports.sqlite;
|
|
5575
5788
|
initializePlatform();
|
|
5576
5789
|
memorio.isBrowser = isBrowser;
|
|
5577
5790
|
memorio.isNode = isNode;
|
|
@@ -5690,7 +5903,6 @@ exports.generateTransactionId = generateTransactionId;
|
|
|
5690
5903
|
exports.getCapabilities = getCapabilities;
|
|
5691
5904
|
exports.getContext = getContext;
|
|
5692
5905
|
exports.help = help;
|
|
5693
|
-
exports.hlc = hlc;
|
|
5694
5906
|
exports.isBrowser = isBrowser;
|
|
5695
5907
|
exports.isBun = isBun;
|
|
5696
5908
|
exports.isDeno = isDeno;
|