// Контекст вызова — пробрасывается через AsyncLocalStorage, чтобы // сервисы (ai.js, covers.js, postImages.js, articleAutoSeries.js) могли // логировать расход не получая лишних параметров. // // Заполняется middleware в index.js (на основе URL-префикса) либо // явно из воркеров/cron: aiContext.run({ service: 'zeropost-blog', userId: 0 }, fn). const { AsyncLocalStorage } = require('node:async_hooks'); const als = new AsyncLocalStorage(); function run(ctx, fn) { return als.run({ ...ctx }, fn); } function get() { return als.getStore() || { service: 'zeropost-unknown', userId: null }; } function service() { return get().service; } function userId() { return get().userId; } // Удобно для воркеров: оборачивает функцию в run() с заданным контекстом. function withContext(ctx, fn) { return (...args) => als.run({ ...ctx }, () => fn(...args)); } module.exports = { run, get, service, userId, withContext };