1c203eba8a
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
71 lines
4.6 KiB
JavaScript
71 lines
4.6 KiB
JavaScript
// JSON volume store (the InstantAdPay JSON-mode pattern: one file in DATA_DIR, atomic tmp+rename, debounced).
|
|
// Holds only what the chain does not: banner + link per spot, member defaults, views/visits delivered,
|
|
// sign-in sessions, last-seen times and the keeper log. Every function is sync and small so a MySQL
|
|
// version can replace this file behind the same names later.
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const crypto = require('crypto');
|
|
const cfgMod = require('./config');
|
|
|
|
let S = null, saveT = null;
|
|
const FILE = () => path.join(cfgMod.get().dataDir, 'store.json');
|
|
function load() {
|
|
try { S = JSON.parse(fs.readFileSync(FILE(), 'utf8')); } catch (e) { S = null; }
|
|
S = Object.assign({ v: 1, spotMeta: {}, defaults: {}, views: {}, clicks: {}, seen: {}, sessions: {}, keeper: { log: [] }, daily: {} }, S || {});
|
|
const now = Date.now();
|
|
for (const [k, s] of Object.entries(S.sessions)) if (s.expires < now) delete S.sessions[k];
|
|
}
|
|
function save(now) {
|
|
if (saveT && !now) return;
|
|
const run = () => { saveT = null; try { const tmp = FILE() + '.tmp'; fs.writeFileSync(tmp, JSON.stringify(S), { mode: 0o600 }); fs.renameSync(tmp, FILE()); } catch (e) { console.error('store save failed', e.message); } };
|
|
if (now) { clearTimeout(saveT); run(); } else saveT = setTimeout(run, 1500);
|
|
}
|
|
function flush() { save(true); }
|
|
|
|
// ---- creative per spot + member default ----
|
|
function spotMeta(id) { return S.spotMeta[id] || null; }
|
|
function setSpotMeta(id, m) { S.spotMeta[id] = Object.assign({}, S.spotMeta[id] || {}, m, { updatedAt: Date.now() }); save(); return S.spotMeta[id]; }
|
|
function memberDefault(addr) { return S.defaults[String(addr).toLowerCase()] || null; }
|
|
function setMemberDefault(addr, m) { const a = String(addr).toLowerCase(); S.defaults[a] = Object.assign({}, S.defaults[a] || {}, m, { updatedAt: Date.now() }); save(); return S.defaults[a]; }
|
|
|
|
// ---- views + clicks (gate and organic kept apart) ----
|
|
// Rate limit: one counted view per visitor per spot per hour, one counted click per visitor per spot per hour.
|
|
const seenView = new Map(), seenClick = new Map();
|
|
const HOUR = 3600000;
|
|
function prune(m) { if (m.size < 50000) return; const cut = Date.now() - HOUR; for (const [k, t] of m) if (t < cut) m.delete(k); }
|
|
function addView(spotId, visitorKey, source) {
|
|
const k = visitorKey + ':' + spotId; const t = seenView.get(k);
|
|
if (t && Date.now() - t < HOUR) return false;
|
|
seenView.set(k, Date.now()); prune(seenView);
|
|
const v = S.views[spotId] || (S.views[spotId] = { organic: 0, gate: 0 });
|
|
v[source === 'gate' ? 'gate' : 'organic'] += 1; bumpDaily('views_' + (source === 'gate' ? 'gate' : 'organic')); save();
|
|
return true;
|
|
}
|
|
function views(spotId) { const v = S.views[spotId] || { organic: 0, gate: 0 }; return { organic: v.organic, gate: v.gate, total: v.organic + v.gate }; }
|
|
function addClick(spotId, visitorKey, source) {
|
|
const k = visitorKey + ':' + spotId + ':' + source; const t = seenClick.get(k);
|
|
if (t && Date.now() - t < HOUR) return false;
|
|
seenClick.set(k, Date.now()); prune(seenClick);
|
|
const c = S.clicks[spotId] || (S.clicks[spotId] = { organic: 0, gate: 0 });
|
|
c[source === 'gate' ? 'gate' : 'organic'] += 1; bumpDaily('clicks_' + (source === 'gate' ? 'gate' : 'organic')); save();
|
|
return true;
|
|
}
|
|
function clicks(spotId) { return S.clicks[spotId] || { organic: 0, gate: 0 }; }
|
|
function bumpDaily(k) { const day = new Date().toISOString().slice(0, 10); const d = S.daily[day] || (S.daily[day] = {}); d[k] = (d[k] || 0) + 1; }
|
|
function daily() { return S.daily; }
|
|
|
|
// ---- sessions + last seen ----
|
|
const SESSION_TTL = 7 * 24 * HOUR;
|
|
function mintSession(address) { const token = crypto.randomBytes(32).toString('hex'); S.sessions[token] = { address: address.toLowerCase(), expires: Date.now() + SESSION_TTL }; touch(address); save(); return token; }
|
|
function session(token) { const s = token && S.sessions[token]; return s && s.expires > Date.now() ? Object.assign({ token }, s) : null; }
|
|
function dropSession(token) { delete S.sessions[token]; save(); }
|
|
function touch(address) { S.seen[String(address).toLowerCase()] = Date.now(); save(); }
|
|
function lastSeen(address) { return S.seen[String(address).toLowerCase()] || 0; }
|
|
|
|
// ---- keeper ----
|
|
function keeperLog(entry) { S.keeper.log.push(Object.assign({ at: Date.now() }, entry)); if (S.keeper.log.length > 2000) S.keeper.log = S.keeper.log.slice(-2000); save(true); }
|
|
function keeperEntries() { return S.keeper.log.slice(); }
|
|
|
|
module.exports = { load, flush, spotMeta, setSpotMeta, memberDefault, setMemberDefault, addView, views, addClick, clicks, daily,
|
|
mintSession, session, dropSession, touch, lastSeen, keeperLog, keeperEntries, SESSION_TTL };
|