mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-07-19 12:03:37 +00:00
fix: http支持json5
This commit is contained in:
@@ -8,6 +8,7 @@ import cors from 'cors';
|
|||||||
import { HttpServerConfig } from '@/onebot/config/config';
|
import { HttpServerConfig } from '@/onebot/config/config';
|
||||||
import { NapCatOneBot11Adapter } from "@/onebot";
|
import { NapCatOneBot11Adapter } from "@/onebot";
|
||||||
import { IOB11NetworkAdapter } from "@/onebot/network/adapter";
|
import { IOB11NetworkAdapter } from "@/onebot/network/adapter";
|
||||||
|
import json5 from 'json5';
|
||||||
|
|
||||||
export class OB11HttpServerAdapter extends IOB11NetworkAdapter<HttpServerConfig> {
|
export class OB11HttpServerAdapter extends IOB11NetworkAdapter<HttpServerConfig> {
|
||||||
private app: Express | undefined;
|
private app: Express | undefined;
|
||||||
@@ -52,12 +53,17 @@ export class OB11HttpServerAdapter extends IOB11NetworkAdapter<HttpServerConfig>
|
|||||||
this.app.use((req, res, next) => {
|
this.app.use((req, res, next) => {
|
||||||
// 兼容处理没有带content-type的请求
|
// 兼容处理没有带content-type的请求
|
||||||
req.headers['content-type'] = 'application/json';
|
req.headers['content-type'] = 'application/json';
|
||||||
const originalJson = express.json({ limit: '5000mb' });
|
let rawData = '';
|
||||||
originalJson(req, res, (err) => {
|
req.on('data', (chunk) => {
|
||||||
if (err) {
|
rawData += chunk;
|
||||||
|
});
|
||||||
|
req.on('end', () => {
|
||||||
|
try {
|
||||||
|
req.body = json5.parse(rawData);
|
||||||
|
next();
|
||||||
|
} catch (err) {
|
||||||
return res.status(400).send('Invalid JSON');
|
return res.status(400).send('Invalid JSON');
|
||||||
}
|
}
|
||||||
next();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user