From 557dac4838227770dec6ff0bc586191966cf41c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Tue, 2 Sep 2025 09:54:42 +0800 Subject: [PATCH] Add timeout parameter to file and packet API methods Introduces an optional timeout parameter (defaulting to 20000ms) to various file and packet API methods for improved control over request duration. Updates all relevant method calls and internal usages to support the new timeout argument, including OneBot message API calls with a shorter timeout for file, video, and ptt URL retrieval. --- src/core/apis/file.ts | 18 ++++++------- src/core/packet/client/baseClient.ts | 8 +++--- src/core/packet/context/clientContext.ts | 4 +-- src/core/packet/context/operationContext.ts | 28 ++++++++++----------- src/onebot/api/msg.ts | 6 ++--- 5 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/core/apis/file.ts b/src/core/apis/file.ts index 7029508e9..14bf8b874 100644 --- a/src/core/apis/file.ts +++ b/src/core/apis/file.ts @@ -64,13 +64,13 @@ export class NTQQFileApi { } } - async getFileUrl(chatType: ChatType, peer: string, fileUUID?: string, file10MMd5?: string | undefined) { + async getFileUrl(chatType: ChatType, peer: string, fileUUID?: string, file10MMd5?: string | undefined,timeout: number = 20000) { if (this.core.apis.PacketApi.packetStatus) { try { if (chatType === ChatType.KCHATTYPEGROUP && fileUUID) { - return this.core.apis.PacketApi.pkt.operation.GetGroupFileUrl(+peer, fileUUID); + return this.core.apis.PacketApi.pkt.operation.GetGroupFileUrl(+peer, fileUUID, timeout); } else if (file10MMd5 && fileUUID) { - return this.core.apis.PacketApi.pkt.operation.GetPrivateFileUrl(peer, fileUUID, file10MMd5); + return this.core.apis.PacketApi.pkt.operation.GetPrivateFileUrl(peer, fileUUID, file10MMd5, timeout); } } catch (error) { this.context.logger.logError('获取文件URL失败', (error as Error).message); @@ -79,7 +79,7 @@ export class NTQQFileApi { throw new Error('fileUUID or file10MMd5 is undefined'); } - async getPttUrl(peer: string, fileUUID?: string) { + async getPttUrl(peer: string, fileUUID?: string,timeout: number = 20000) { if (this.core.apis.PacketApi.packetStatus && fileUUID) { let appid = new NapProtoMsg(FileId).decode(Buffer.from(fileUUID.replaceAll('-', '+').replaceAll('_', '/'), 'base64')).appid; try { @@ -90,7 +90,7 @@ export class NTQQFileApi { uploadTime: 0, ttl: 0, subType: 0, - }); + }, timeout); } else if (fileUUID) { return this.core.apis.PacketApi.pkt.operation.GetPttUrl(peer, { fileUuid: fileUUID, @@ -98,7 +98,7 @@ export class NTQQFileApi { uploadTime: 0, ttl: 0, subType: 0, - }); + }, timeout); } } catch (error) { this.context.logger.logError('获取文件URL失败', (error as Error).message); @@ -107,7 +107,7 @@ export class NTQQFileApi { throw new Error('packet cant get ptt url'); } - async getVideoUrlPacket(peer: string, fileUUID?: string) { + async getVideoUrlPacket(peer: string, fileUUID?: string,timeout: number = 20000) { if (this.core.apis.PacketApi.packetStatus && fileUUID) { let appid = new NapProtoMsg(FileId).decode(Buffer.from(fileUUID.replaceAll('-', '+').replaceAll('_', '/'), 'base64')).appid; try { @@ -118,7 +118,7 @@ export class NTQQFileApi { uploadTime: 0, ttl: 0, subType: 0, - }); + }, timeout); } else if (fileUUID) { return this.core.apis.PacketApi.pkt.operation.GetVideoUrl(peer, { fileUuid: fileUUID, @@ -126,7 +126,7 @@ export class NTQQFileApi { uploadTime: 0, ttl: 0, subType: 0, - }); + }, timeout); } } catch (error) { this.context.logger.logError('获取文件URL失败', (error as Error).message); diff --git a/src/core/packet/client/baseClient.ts b/src/core/packet/client/baseClient.ts index df5e15dd2..9fd46496d 100644 --- a/src/core/packet/client/baseClient.ts +++ b/src/core/packet/client/baseClient.ts @@ -80,15 +80,15 @@ export abstract class IPacketClient { }); } - async sendPacket(cmd: string, data: PacketHexStr, rsp = false): Promise { + async sendPacket(cmd: string, data: PacketHexStr, rsp = false, timeout = 20000): Promise { const md5 = crypto.createHash('md5').update(data).digest('hex'); const trace_id = (randText(4) + md5 + data).slice(0, data.length / 2); - return this.sendCommand(cmd, data, trace_id, rsp, 20000, async () => { + return this.sendCommand(cmd, data, trace_id, rsp, timeout, async () => { await this.napcore.sendSsoCmdReqByContend(cmd, trace_id); }); } - async sendOidbPacket(pkt: OidbPacket, rsp = false): Promise { - return this.sendPacket(pkt.cmd, pkt.data, rsp); + async sendOidbPacket(pkt: OidbPacket, rsp = false, timeout = 20000): Promise { + return this.sendPacket(pkt.cmd, pkt.data, rsp, timeout); } } diff --git a/src/core/packet/context/clientContext.ts b/src/core/packet/context/clientContext.ts index e4419139c..585e4a77a 100644 --- a/src/core/packet/context/clientContext.ts +++ b/src/core/packet/context/clientContext.ts @@ -73,8 +73,8 @@ export class PacketClientContext { await this._client.init(pid, recv, send); } - async sendOidbPacket(pkt: OidbPacket, rsp?: T): Promise { - const raw = await this._client.sendOidbPacket(pkt, rsp); + async sendOidbPacket(pkt: OidbPacket, rsp?: T, timeout?: number): Promise { + const raw = await this._client.sendOidbPacket(pkt, rsp, timeout); return (rsp ? Buffer.from(raw.hex_data, 'hex') : undefined) as T extends true ? Buffer : void; } diff --git a/src/core/packet/context/operationContext.ts b/src/core/packet/context/operationContext.ts index 9554b3d19..d957f0e5b 100644 --- a/src/core/packet/context/operationContext.ts +++ b/src/core/packet/context/operationContext.ts @@ -122,37 +122,37 @@ export class PacketOperationContext { return `https://${res.download.info.domain}${res.download.info.urlPath}${res.download.rKeyParam}`; } - async GetPttUrl(selfUid: string, node: NapProtoEncodeStructType) { + async GetPttUrl(selfUid: string, node: NapProtoEncodeStructType,timeout: number = 20000) { const req = trans.DownloadPtt.build(selfUid, node); - const resp = await this.context.client.sendOidbPacket(req, true); + const resp = await this.context.client.sendOidbPacket(req, true, timeout); const res = trans.DownloadPtt.parse(resp); return `https://${res.download.info.domain}${res.download.info.urlPath}${res.download.rKeyParam}`; } - async GetVideoUrl(selfUid: string, node: NapProtoEncodeStructType) { + async GetVideoUrl(selfUid: string, node: NapProtoEncodeStructType, timeout: number = 20000) { const req = trans.DownloadVideo.build(selfUid, node); - const resp = await this.context.client.sendOidbPacket(req, true); + const resp = await this.context.client.sendOidbPacket(req, true, timeout); const res = trans.DownloadVideo.parse(resp); return `https://${res.download.info.domain}${res.download.info.urlPath}${res.download.rKeyParam}`; } - async GetGroupImageUrl(groupUin: number, node: NapProtoEncodeStructType) { + async GetGroupImageUrl(groupUin: number, node: NapProtoEncodeStructType, timeout: number = 20000) { const req = trans.DownloadGroupImage.build(groupUin, node); - const resp = await this.context.client.sendOidbPacket(req, true); + const resp = await this.context.client.sendOidbPacket(req, true, timeout); const res = trans.DownloadImage.parse(resp); return `https://${res.download.info.domain}${res.download.info.urlPath}${res.download.rKeyParam}`; } - async GetGroupPttUrl(groupUin: number, node: NapProtoEncodeStructType) { + async GetGroupPttUrl(groupUin: number, node: NapProtoEncodeStructType, timeout: number = 20000) { const req = trans.DownloadGroupPtt.build(groupUin, node); - const resp = await this.context.client.sendOidbPacket(req, true); + const resp = await this.context.client.sendOidbPacket(req, true, timeout); const res = trans.DownloadImage.parse(resp); return `https://${res.download.info.domain}${res.download.info.urlPath}${res.download.rKeyParam}`; } - async GetGroupVideoUrl(groupUin: number, node: NapProtoEncodeStructType) { + async GetGroupVideoUrl(groupUin: number, node: NapProtoEncodeStructType, timeout: number = 20000) { const req = trans.DownloadGroupVideo.build(groupUin, node); - const resp = await this.context.client.sendOidbPacket(req, true); + const resp = await this.context.client.sendOidbPacket(req, true, timeout); const res = trans.DownloadImage.parse(resp); return `https://${res.download.info.domain}${res.download.info.urlPath}${res.download.rKeyParam}`; } @@ -243,16 +243,16 @@ export class PacketOperationContext { return res.rename.retCode; } - async GetGroupFileUrl(groupUin: number, fileUUID: string) { + async GetGroupFileUrl(groupUin: number, fileUUID: string,timeout: number = 20000) { const req = trans.DownloadGroupFile.build(groupUin, fileUUID); - const resp = await this.context.client.sendOidbPacket(req, true); + const resp = await this.context.client.sendOidbPacket(req, true, timeout); const res = trans.DownloadGroupFile.parse(resp); return `https://${res.download.downloadDns}/ftn_handler/${Buffer.from(res.download.downloadUrl).toString('hex')}/?fname=`; } - async GetPrivateFileUrl(self_id: string, fileUUID: string, md5: string) { + async GetPrivateFileUrl(self_id: string, fileUUID: string, md5: string, timeout: number = 20000) { const req = trans.DownloadPrivateFile.build(self_id, fileUUID, md5); - const resp = await this.context.client.sendOidbPacket(req, true); + const resp = await this.context.client.sendOidbPacket(req, true, timeout); const res = trans.DownloadPrivateFile.parse(resp); return `http://${res.body?.result?.server}:${res.body?.result?.port}${res.body?.result?.url?.slice(8)}&isthumb=0`; } diff --git a/src/onebot/api/msg.ts b/src/onebot/api/msg.ts index 9be190aea..8a329f06f 100644 --- a/src/onebot/api/msg.ts +++ b/src/onebot/api/msg.ts @@ -150,7 +150,7 @@ export class OneBotMsgApi { if (this.core.apis.PacketApi.packetStatus) { let url; try { - url = await this.core.apis.FileApi.getFileUrl(msg.chatType, msg.peerUid, element.fileUuid, element.file10MMd5) + url = await this.core.apis.FileApi.getFileUrl(msg.chatType, msg.peerUid, element.fileUuid, element.file10MMd5, 1500) } catch (error) { url = ''; } @@ -393,7 +393,7 @@ export class OneBotMsgApi { if (!videoDownUrl) { if (this.core.apis.PacketApi.packetStatus) { try { - videoDownUrl = await this.core.apis.FileApi.getVideoUrlPacket(msg.peerUid, element.fileUuid); + videoDownUrl = await this.core.apis.FileApi.getVideoUrlPacket(msg.peerUid, element.fileUuid, 1500); } catch (e) { this.core.context.logger.logError('获取视频url失败', (e as Error).stack); videoDownUrl = element.filePath; @@ -424,7 +424,7 @@ export class OneBotMsgApi { let pttUrl = ''; if (this.core.apis.PacketApi.packetStatus) { try { - pttUrl = await this.core.apis.FileApi.getPttUrl(msg.peerUid, element.fileUuid); + pttUrl = await this.core.apis.FileApi.getPttUrl(msg.peerUid, element.fileUuid, 1500); } catch (e) { this.core.context.logger.logError('获取语音url失败', (e as Error).stack); pttUrl = element.filePath;