diff --git a/rotator.js b/rotator.js index 6304926..f930ee6 100644 --- a/rotator.js +++ b/rotator.js @@ -18,7 +18,17 @@ const DAY = 86400000; const J = { db: null, FILE: () => path.join(DATA_DIR, 'rotator.json'), - load() { try { this.db = JSON.parse(fs.readFileSync(this.FILE(), 'utf8')); } catch (e) { this.db = { v: 1, rotations: [], hits: [], conv: [] }; } if (!this.db.conv) this.db.conv = []; }, + // a missing file starts empty; a file that is there but will not parse is NEVER replaced with an empty list (2026-11-13: that + // fallback wiped 294 spin links when a deploy read a half-written file). Retry a moment, then stop the app so it restarts. + load() { + let last = null; + for (let i = 0; i < 10; i++) { + try { this.db = JSON.parse(fs.readFileSync(this.FILE(), 'utf8')); last = null; break; } + catch (e) { last = e; if (e.code === 'ENOENT') { this.db = { v: 1, rotations: [], hits: [], conv: [] }; last = null; break; } Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 500); } + } + if (last) throw new Error('rotator.json exists but cannot be read (' + last.message + '); refusing to start with no spin links'); + if (!this.db.conv) this.db.conv = []; + }, save() { try { fs.writeFileSync(this.FILE(), JSON.stringify(this.db)); } catch (e) {} } }; function init(opts) { DATA_DIR = opts.dataDir; geo = opts.geo || null; OWN_HOSTS = opts.ownHosts || (() => []); SOURCES = opts.sources || null; if (opts.wantsClickId) WANTS_CLICK_ID = opts.wantsClickId; if (opts.isNetworkHost) IS_NETWORK = opts.isNetworkHost; if (opts.goalRows) GOAL_ROWS = opts.goalRows; J.load(); } diff --git a/safefs.js b/safefs.js new file mode 100644 index 0000000..e838d8b --- /dev/null +++ b/safefs.js @@ -0,0 +1,27 @@ +// Atomic JSON data files (2026-11-13 incident). Every store here saves with fs.writeFileSync(file.json, ...), which truncates +// the file and then writes it. During a deploy the old and new app copies share the volume for a few seconds; the new copy read +// rotator.json while the old one was half way through writing it, JSON.parse failed, the store fell back to EMPTY and its next +// save wrote that empty list over 294 spin links. This makes every .json write inside DATA_DIR atomic (write a temp file in the +// same folder, then rename it over the real one), so a reader only ever sees the old file or the new one, never half of one. +// The SAME file ships in InstantAdPay and LinkSpin; server.js installs it right after DATA_DIR is known. +'use strict'; +const fs = require('fs'); +const path = require('path'); + +let installed = false; +function install(dataDir) { + if (installed) return; installed = true; + const root = path.resolve(dataDir) + path.sep; + const orig = fs.writeFileSync; + fs.writeFileSync = function (file, data, opts) { + const flag = opts && typeof opts === 'object' ? opts.flag : null; + if (typeof file === 'string' && /\.json$/i.test(file) && (!flag || flag === 'w') && path.resolve(file).startsWith(root)) { + const tmp = file + '.tmp-' + process.pid + '-' + Math.random().toString(36).slice(2, 8); + try { orig.call(fs, tmp, data, opts); fs.renameSync(tmp, file); } + catch (e) { try { fs.unlinkSync(tmp); } catch (x) {} throw e; } + return; + } + return orig.apply(fs, arguments); + }; +} +module.exports = { install }; diff --git a/server.js b/server.js index a26c997..da7ef08 100644 --- a/server.js +++ b/server.js @@ -100,6 +100,7 @@ const PORT = Number(process.env.PORT || 3000); const ROOT = __dirname; const PUBLIC_DIR = path.join(ROOT, 'public'); const DATA_DIR = process.env.DATA_DIR || path.join(ROOT, 'data'); +require('./safefs').install(DATA_DIR); // every .json data file is written atomically (2026-11-13 rotator.json loss) const ADMIN_PASSWORD = process.env.ADMIN_PASSWORD || 'changeme'; const ADMIN_EMAIL = String(process.env.ADMIN_EMAIL || '').trim().toLowerCase(); const { SITE_HOST, SITE_URL } = require('./host');