16 lines
454 B
JavaScript
16 lines
454 B
JavaScript
'use strict';
|
|||
|
|
const { fail } = require('../../ledger');
|
||
|
|
// 保持原样(含按块拼接字符串的已知问题),修正留到下一阶段。
|
||
|
|
async function readBody(req) {
|
||
|
|
let raw = '';
|
||
|
|
for await (const chunk of req) {
|
||
|
|
raw += chunk;
|
||
|
|
if (Buffer.byteLength(raw) > 300000) fail('内容过大', 413);
|
||
|
|
}
|
||
|
|
try {
|
||
|
|
return JSON.parse(raw || '{}');
|
||
|
|
} catch {
|
||
|
|
fail('请求内容不是有效JSON');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
module.exports = { readBody };
|