197aa39fca
hubevents.js (new, byte-identical in VBB, DLB, ViralAdBlitz, DownlineHydra, 5xYourTeam): POST /api/hub/events, signed by the hub with HUB_SECRET (same hook InstantAdPay and LinkSpin take). member.signedout and member.blocked: the person is looked up by hub id (/v1/resolve, signed as bannerline) and every session of their main wallet or any contract wallet ends. member.erased: matched by the wallet SHA-256s the hub now sends with the event. Only sessions made before the event end (a replay never signs anyone out twice); a failed hub lookup answers 503 so the hub resends. Off with HUB_HOOK=0. Admin status: hubEvents (counts only). My Spots: "Sign out everywhere" next to Sign out (POST /api/auth/logout-everywhere -> networksso.signOutEverywhere, then every session of this wallet here ends). my.js?v=z2. Smoke section 9 (refused signatures, signedout in two browsers, replay, older event, blocked, erased by wallet hash, other events, hub down -> 503 -> resent, Sign out everywhere, admin view): 145/145 with UI. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
73 lines
4.9 KiB
JavaScript
73 lines
4.9 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(); }
|
|
// network hub events (hubevents.js, 2026-11-16): end every session `match` names that was made before `before` -> how many
|
|
function endSessions(match, before) { let n = 0; for (const [k, x] of Object.entries(S.sessions)) if (x.expires - SESSION_TTL <= before && match(x)) { delete S.sessions[k]; n++; } if (n) save(); return n; }
|
|
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, endSessions, touch, lastSeen, keeperLog, keeperEntries, SESSION_TTL };
|