// 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 };