diff --git a/src/common/lru-cache.ts b/src/common/lru-cache.ts index 39c879f0..ba2cef1d 100644 --- a/src/common/lru-cache.ts +++ b/src/common/lru-cache.ts @@ -30,4 +30,13 @@ export class LRUCache { } this.cache.set(key, value); } + public resetCapacity(newCapacity: number): void { + this.capacity = newCapacity; + while (this.cache.size > this.capacity) { + const firstKey = this.cache.keys().next().value; + if (firstKey !== undefined) { + this.cache.delete(firstKey); + } + } + } } \ No newline at end of file diff --git a/src/common/message-unique.ts b/src/common/message-unique.ts index db980a94..d6ca43fc 100644 --- a/src/common/message-unique.ts +++ b/src/common/message-unique.ts @@ -2,8 +2,8 @@ import { Peer } from '@/core'; import crypto from 'crypto'; export class LimitedHashTable { - private keyToValue: Map = new Map(); - private valueToKey: Map = new Map(); + private readonly keyToValue: Map = new Map(); + private readonly valueToKey: Map = new Map(); private maxSize: number; constructor(maxSize: number) { @@ -75,8 +75,8 @@ export class LimitedHashTable { } class MessageUniqueWrapper { - private msgDataMap: LimitedHashTable; - private msgIdMap: LimitedHashTable; + private readonly msgDataMap: LimitedHashTable; + private readonly msgIdMap: LimitedHashTable; constructor(maxMap: number = 1000) { this.msgIdMap = new LimitedHashTable(maxMap); diff --git a/src/common/request.ts b/src/common/request.ts index f3e1cebe..344feced 100644 --- a/src/common/request.ts +++ b/src/common/request.ts @@ -9,48 +9,47 @@ export class RequestUtil { return new Promise((resolve, reject) => { const req = client.get(url, (res) => { let cookies: { [key: string]: string } = {}; - const handleRedirect = (res: http.IncomingMessage) => { - //console.log(res.headers.location); - if (res.statusCode === 301 || res.statusCode === 302) { - if (res.headers.location) { - const redirectUrl = new URL(res.headers.location, url); - RequestUtil.HttpsGetCookies(redirectUrl.href).then((redirectCookies) => { - // 合并重定向过程中的cookies - cookies = { ...cookies, ...redirectCookies }; - resolve(cookies); - }).catch((err) => { - reject(err); - }); - } else { - resolve(cookies); - } - } else { - resolve(cookies); - } - }; - res.on('data', () => { - }); // Necessary to consume the stream + + res.on('data', () => { }); // Necessary to consume the stream res.on('end', () => { - handleRedirect(res); + this.handleRedirect(res, url, cookies) + .then(resolve) + .catch(reject); }); + if (res.headers['set-cookie']) { - //console.log(res.headers['set-cookie']); - res.headers['set-cookie'].forEach((cookie) => { - const parts = cookie.split(';')[0].split('='); - const key = parts[0]; - const value = parts[1]; - if (key && value && key.length > 0 && value.length > 0) { - cookies[key] = value; - } - }); + this.extractCookies(res.headers['set-cookie'], cookies); } }); + req.on('error', (error: any) => { reject(error); }); }); } + private static async handleRedirect(res: http.IncomingMessage, url: string, cookies: { [key: string]: string }): Promise<{ [key: string]: string }> { + if (res.statusCode === 301 || res.statusCode === 302) { + if (res.headers.location) { + const redirectUrl = new URL(res.headers.location, url); + const redirectCookies = await this.HttpsGetCookies(redirectUrl.href); + // 合并重定向过程中的cookies + return { ...cookies, ...redirectCookies }; + } + } + return cookies; + } + + private static extractCookies(setCookieHeaders: string[], cookies: { [key: string]: string }) { + setCookieHeaders.forEach((cookie) => { + const parts = cookie.split(';')[0].split('='); + const key = parts[0]; + const value = parts[1]; + if (key && value && key.length > 0 && value.length > 0) { + cookies[key] = value; + } + }); + } // 请求和回复都是JSON data传原始内容 自动编码json static async HttpGetJson(url: string, method: string = 'GET', data?: any, headers: { diff --git a/src/core/apis/file.ts b/src/core/apis/file.ts index be6a1da3..652897ff 100644 --- a/src/core/apis/file.ts +++ b/src/core/apis/file.ts @@ -357,15 +357,13 @@ export class NTQQFileApi { async getImageSize(filePath: string): Promise { return new Promise((resolve, reject) => { - imageSize(filePath, (err, dimensions) => { + imageSize(filePath, (err: Error | null, dimensions) => { if (err) { - reject(err); + reject(new Error(err.message)); + } else if (!dimensions) { + reject(new Error('获取图片尺寸失败')); } else { - if (!dimensions) { - reject(new Error('获取图片尺寸失败')); - } else { - resolve(dimensions); - } + resolve(dimensions); } }); }); diff --git a/src/core/apis/friend.ts b/src/core/apis/friend.ts index b805c729..7e852dd0 100644 --- a/src/core/apis/friend.ts +++ b/src/core/apis/friend.ts @@ -15,7 +15,7 @@ export class NTQQFriendApi { } async getBuddyV2SimpleInfoMap(refresh = false) { const buddyService = this.context.session.getBuddyService(); - const buddyListV2 = refresh ? await buddyService.getBuddyListV2('0', BuddyListReqType.KNOMAL) : await buddyService.getBuddyListV2('0', BuddyListReqType.KNOMAL); + const buddyListV2 = await buddyService.getBuddyListV2('0', BuddyListReqType.KNOMAL); const uids = buddyListV2.data.flatMap(item => item.buddyUids); return await this.core.eventWrapper.callNoListenerEvent( 'NodeIKernelProfileService/getCoreAndBaseInfo', @@ -44,7 +44,7 @@ export class NTQQFriendApi { async getBuddyV2ExWithCate(refresh = false) { const categoryMap: Map = new Map(); const buddyService = this.context.session.getBuddyService(); - const buddyListV2 = refresh ? (await buddyService.getBuddyListV2('0', BuddyListReqType.KNOMAL)).data : (await buddyService.getBuddyListV2('0', BuddyListReqType.KNOMAL)).data; + const buddyListV2 = (await buddyService.getBuddyListV2('0', BuddyListReqType.KNOMAL)).data; const uids = buddyListV2.flatMap(item => { item.buddyUids.forEach(uid => { categoryMap.set(uid, { categoryId: item.categoryId, categoryName: item.categroyName }); diff --git a/src/core/apis/group.ts b/src/core/apis/group.ts index 596d2003..0380186c 100644 --- a/src/core/apis/group.ts +++ b/src/core/apis/group.ts @@ -319,7 +319,7 @@ export class NTQQGroupApi { return undefined; } - async tryGetGroupMembersV2(modeListener = false, groupQQ: string, num = 30, timeout = 100): Promise<{ + async tryGetGroupMembersV2(groupQQ: string, modeListener = false, num = 30, timeout = 100): Promise<{ infos: Map; finish: boolean; hasNext: boolean | undefined; @@ -372,7 +372,7 @@ export class NTQQGroupApi { infos: new Map([...(resMode2?.infos ?? []), ...result.result.infos]), finish: result.result.finish, hasNext: resMode2?.hasNext, - listenerMode: resMode2?.hasNext !== undefined ? true : false + listenerMode: resMode2?.hasNext !== undefined }; } diff --git a/src/core/apis/msg.ts b/src/core/apis/msg.ts index 37041dd4..120d3351 100644 --- a/src/core/apis/msg.ts +++ b/src/core/apis/msg.ts @@ -144,7 +144,7 @@ export class NTQQMsgApi { params, ], () => true, - () => true, // Todo: 应当通过 groupFileListResult 判断 + () => true, // 应当通过 groupFileListResult 判断 1, 5000, ); @@ -194,7 +194,7 @@ export class NTQQMsgApi { async sendMsg(peer: Peer, msgElements: SendMessageElement[], waitComplete = true, timeout = 10000) { //唉?!我有个想法 if (peer.chatType === ChatType.KCHATTYPETEMPC2CFROMGROUP && peer.guildId && peer.guildId !== '') { - const member = await this.core.apis.GroupApi.getGroupMember(peer.guildId, peer.peerUid!); + const member = await this.core.apis.GroupApi.getGroupMember(peer.guildId, peer.peerUid); if (member) { await this.PrepareTempChat(peer.peerUid, peer.guildId, member.nick); }