'use strict'; const http = require('node:http'); const { URL } = require('node:url'); function parseArgs(argv) { const options = { host: process.env.WEBHOOK_DEBUG_HOST || '127.0.0.1', port: Number(process.env.WEBHOOK_DEBUG_PORT || 7071), defaultResponse: process.env.WEBHOOK_DEBUG_RESPONSE || '1', noticResponse: process.env.WEBHOOK_DEBUG_NOTIC_RESPONSE || '', notic2Response: process.env.WEBHOOK_DEBUG_NOTIC2_RESPONSE || '', }; for (const arg of argv) { if (!arg.startsWith('--')) { continue; } const [rawKey, ...rest] = arg.slice(2).split('='); const value = rest.join('='); switch (rawKey) { case 'host': options.host = value || options.host; break; case 'port': options.port = Number(value || options.port); break; case 'response': options.defaultResponse = value || options.defaultResponse; break; case 'notic-response': options.noticResponse = value || options.noticResponse; break; case 'notic2-response': options.notic2Response = value || options.notic2Response; break; case 'help': options.help = true; break; default: break; } } return options; } function printHelp() { console.log(`用法:\n npm run debug:webhook:receiver\n npm run debug:webhook:receiver -- --port=7071 --notic-response=1 --notic2-response=0\n\n环境变量:\n WEBHOOK_DEBUG_PORT=7071\n WEBHOOK_DEBUG_RESPONSE=1\n WEBHOOK_DEBUG_NOTIC_RESPONSE=1\n WEBHOOK_DEBUG_NOTIC2_RESPONSE=0\n`); } function readRequestBody(req) { return new Promise((resolve, reject) => { const chunks = []; req.on('data', chunk => chunks.push(chunk)); req.on('end', () => resolve(Buffer.concat(chunks).toString('utf8'))); req.on('error', reject); }); } function buildResponseBody(pathname, options) { if (pathname === '/notic2' && options.notic2Response) { return options.notic2Response; } if (pathname === '/notic' && options.noticResponse) { return options.noticResponse; } return options.defaultResponse; } async function start() { const options = parseArgs(process.argv.slice(2)); if (options.help) { printHelp(); return; } const server = http.createServer(async (req, res) => { const url = new URL(req.url, `http://${req.headers.host || `${options.host}:${options.port}`}`); const startedAt = new Date().toISOString(); const rawBody = await readRequestBody(req); let parsedBody = rawBody; try { parsedBody = rawBody ? JSON.parse(rawBody) : null; } catch (_) { // keep raw text } console.log('\n================ WEBHOOK DEBUG RECEIVER ================'); console.log(`[time] ${startedAt}`); console.log(`[route] ${req.method} ${url.pathname}`); console.log('[query] ', Object.fromEntries(url.searchParams.entries())); console.log('[header]', req.headers); console.log('[body] ', JSON.stringify(parsedBody, null, 2)); if (![ '/notic', '/notic2', '/health' ].includes(url.pathname)) { res.writeHead(404, { 'content-type': 'text/plain; charset=utf-8' }); res.end('404 Not Found'); return; } if (url.pathname === '/health') { res.writeHead(200, { 'content-type': 'application/json; charset=utf-8' }); res.end(JSON.stringify({ ok: true, service: 'webhook-debug-receiver' })); return; } const responseBody = buildResponseBody(url.pathname, options); const statusCode = String(responseBody).startsWith('{') || String(responseBody).startsWith('[') ? 200 : 200; const contentType = String(responseBody).startsWith('{') || String(responseBody).startsWith('[') ? 'application/json; charset=utf-8' : 'text/plain; charset=utf-8'; res.writeHead(statusCode, { 'content-type': contentType }); res.end(responseBody); console.log(`[reply] ${url.pathname} -> ${responseBody}`); console.log('========================================================\n'); }); server.listen(options.port, options.host, () => { console.log(`[webhook-debug-receiver] listening on http://${options.host}:${options.port}`); console.log(`[webhook-debug-receiver] endpoints:`); console.log(` - POST http://${options.host}:${options.port}/notic`); console.log(` - POST http://${options.host}:${options.port}/notic2`); console.log(` - GET http://${options.host}:${options.port}/health`); }); } start().catch(error => { console.error('[webhook-debug-receiver] failed:', error); process.exit(1); });