function parseSimpleXml(raw) { const result = {}; const reg = /<([A-Za-z0-9_]+)>(?:|([\s\S]*?))<\/\1>/g; let match; while ((match = reg.exec(raw)) !== null) { const key = match[1]; if (key === 'xml') continue; result[key] = match[2] !== undefined ? match[2] : String(match[3] || '').trim(); } return result; } function readRawBody(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); }); } export default function xmlBodyParser() { return async (ctx, next) => { const contentType = String(ctx.request.headers['content-type'] || '').toLowerCase(); if (!contentType.includes('xml')) { await next(); return; } const rawBody = await readRawBody(ctx.req); ctx.request.body = { xml: parseSimpleXml(rawBody), rawBody, }; await next(); }; }