chore: update version to 0.0.51, add auto.ts to files, and update dependencies
feat: enhance parseXml function to handle Bun requests and refactor XML parsing logic
This commit is contained in:
10
src/route.ts
10
src/route.ts
@@ -576,10 +576,16 @@ export class QueryRouter {
|
||||
* -- .on
|
||||
* -- .send
|
||||
*/
|
||||
wait(params?: { path?: string; key?: string; payload?: any }, opts?: { emitter?: any, timeout?: number, getList?: boolean }) {
|
||||
wait(params?: { path?: string; key?: string; payload?: any }, opts?: {
|
||||
emitter?: any,
|
||||
timeout?: number,
|
||||
getList?: boolean
|
||||
force?: boolean
|
||||
filter?: (route: Route) => boolean
|
||||
}) {
|
||||
const getList = opts?.getList ?? true;
|
||||
if (getList) {
|
||||
this.createRouteList();
|
||||
this.createRouteList(opts?.force ?? false, opts?.filter);
|
||||
}
|
||||
return listenProcess({ app: this, params, ...opts });
|
||||
}
|
||||
|
||||
@@ -1,6 +1,34 @@
|
||||
import xml2js from 'xml2js';
|
||||
|
||||
export const parseXml = async (req: any): Promise<any> => {
|
||||
import { isBun } from '../utils/is-engine.ts';
|
||||
import http from 'http';
|
||||
export const xms2jsParser = async (data: string): Promise<any> => {
|
||||
try {
|
||||
// 使用xml2js解析XML
|
||||
const xml = await xml2js.parseStringPromise(data);
|
||||
return xml;
|
||||
} catch (error) {
|
||||
console.error('XML解析错误:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
export const parseXml = async (req: http.IncomingMessage): Promise<any> => {
|
||||
if (isBun) {
|
||||
// @ts-ignore
|
||||
const body = req.body;
|
||||
let xmlString = '';
|
||||
if (body) {
|
||||
xmlString = body;
|
||||
}
|
||||
if (!xmlString) {
|
||||
// @ts-ignore
|
||||
xmlString = await req.bun?.request?.text?.();
|
||||
}
|
||||
if (xmlString) {
|
||||
return await xms2jsParser(xmlString)
|
||||
}
|
||||
console.error('没有读取到请求体');
|
||||
return null;
|
||||
}
|
||||
return await new Promise((resolve) => {
|
||||
// 读取请求数据
|
||||
let data = '';
|
||||
@@ -12,15 +40,8 @@ export const parseXml = async (req: any): Promise<any> => {
|
||||
// 当请求结束时处理数据
|
||||
req.on('end', () => {
|
||||
try {
|
||||
// 使用xml2js解析XML
|
||||
xml2js.parseString(data, function (err, result) {
|
||||
if (err) {
|
||||
console.error('XML解析错误:', err);
|
||||
resolve(null);
|
||||
} else {
|
||||
const jsonString = JSON.stringify(result);
|
||||
resolve(jsonString);
|
||||
}
|
||||
xms2jsParser(data).then((result) => {
|
||||
resolve(result);
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('处理请求时出错:', error);
|
||||
|
||||
Reference in New Issue
Block a user