const MODULE_NAME = 'CORE-CALLBACK.DUMPER.REQ'; const fs = require('fs'); const fsPromise = require('fs').promises; const path = require('path'); const config = require('komodo-sdk/config'); const logger = require('komodo-sdk/logger'); const moment = require('moment'); const baseDumpDir = 'dump'; const subBaseDumpDir = path.join(baseDumpDir, 'core-callback'); const lastDumpFileName = path.join(subBaseDumpDir, 'last-req'); if (!fs.existsSync(baseDumpDir)) { logger.verbose(`${MODULE_NAME} B5785525: Creating base dump dir`); fs.mkdirSync(baseDumpDir); } if (!fs.existsSync(subBaseDumpDir)) { logger.verbose(`${MODULE_NAME} 13BD289A: Creating dump dir`); fs.mkdirSync(subBaseDumpDir); } module.exports = async (req, res, next) => { if ( !config || !config.listener || !config.listener.core || !config.listener.core.dump ) { next(); return; } const { xid } = res.locals; const data = `-------- XID: ${xid} PID: ${process.pid} DATE: ${moment().format('YYYY-MM-DD HH:mm:ss.SSS')} REQ-CONTENT-TYPE: ${req.get('content-type')} REQ QUERY-STRING: ${JSON.stringify(req.query, null, 2)} REQ BODY: ${JSON.stringify(req.body, null, 2)} `; const dumpDir = path.join( subBaseDumpDir, moment().format('YYYY-MM-DD'), ); try { await fsPromise.stat(dumpDir); } catch (e) { await fsPromise.mkdir(dumpDir, { recursive: true }); } const dumpFileName = path.join( dumpDir, [ 'req', moment().format('YYMMDD_HHmmss_SSS'), xid, ].join('_'), ); await fsPromise.writeFile(dumpFileName, data); await fsPromise.writeFile(lastDumpFileName, data); next(); };