From 3c6c2965a8c55a0364d90d58e231d573d42e76d8 Mon Sep 17 00:00:00 2001 From: caorushizi <84996057@qq.com> Date: Thu, 26 Sep 2024 17:54:37 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20=20video=20sniffing=20logic?= =?UTF-8?q?=20optimization?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/main/package.json | 1 + packages/main/src/helper/cache.ts | 10 +++ packages/main/src/helper/index.ts | 1 + .../src/services/SniffingHelperService.ts | 63 ++++++++++--------- packages/main/src/services/WebviewService.ts | 4 +- pnpm-lock.yaml | 9 +++ 6 files changed, 57 insertions(+), 31 deletions(-) create mode 100644 packages/main/src/helper/cache.ts diff --git a/packages/main/package.json b/packages/main/package.json index becb2077..16cc1c86 100644 --- a/packages/main/package.json +++ b/packages/main/package.json @@ -72,6 +72,7 @@ "inversify": "^6.0.2", "lint-staged": "^15.2.5", "lodash": "^4.17.21", + "lru-cache": "^11.0.1", "mime-types": "^2.1.35", "nanoid": "^5.0.7", "node-fetch": "^3.3.2", diff --git a/packages/main/src/helper/cache.ts b/packages/main/src/helper/cache.ts new file mode 100644 index 00000000..7411ba68 --- /dev/null +++ b/packages/main/src/helper/cache.ts @@ -0,0 +1,10 @@ +import { LRUCache } from "lru-cache"; + +const options: LRUCache.OptionsMaxLimit = { + max: 5000, + + // how long to live in ms + ttl: 1000 * 60 * 5, +}; + +export const urlCache = new LRUCache(options); diff --git a/packages/main/src/helper/index.ts b/packages/main/src/helper/index.ts index 69120ed3..9c96a0ae 100644 --- a/packages/main/src/helper/index.ts +++ b/packages/main/src/helper/index.ts @@ -41,3 +41,4 @@ export { convertToAudio } from "./ffmpeg.ts"; export { fetchWrapper as fetch }; export { fileExists } from "./file.ts"; export { videoPattern } from "./video.ts"; +export { urlCache } from "./cache.ts"; diff --git a/packages/main/src/services/SniffingHelperService.ts b/packages/main/src/services/SniffingHelperService.ts index a2517aa4..63768f36 100644 --- a/packages/main/src/services/SniffingHelperService.ts +++ b/packages/main/src/services/SniffingHelperService.ts @@ -8,6 +8,7 @@ import { PERSIST_WEBVIEW, PRIVACY_WEBVIEW, formatHeaders, + urlCache, } from "../helper/index.ts"; export interface SourceParams { @@ -48,8 +49,7 @@ const filterList: SourceFilter[] = [ @injectable() export class SniffingHelper extends EventEmitter { private pageInfo: PageInfo = { title: "", url: "" }; - private ready = false; - private queue: SourceParams[] = []; + private readonly prepareDelay = 1000; constructor( @inject(TYPES.ElectronLogger) @@ -59,38 +59,38 @@ export class SniffingHelper extends EventEmitter { } pluginReady() { - this.ready = true; - this.queue.forEach((item) => { - this.emit("source", item); - }); + // empty } update(pageInfo: PageInfo) { this.pageInfo = pageInfo; } - reset(pageInfo: PageInfo) { - this.pageInfo = pageInfo; - this.ready = false; - this.queue = []; - - listLoop: for (const filter of filterList) { - if (filter.hosts) { - for (const host of filter.hosts) { - if (!host.test(pageInfo.url)) { - continue; + checkPageInfo() { + // 发送 page 相关的信息 + const sendPageInfo = () => { + listLoop: for (const filter of filterList) { + if (filter.hosts) { + for (const host of filter.hosts) { + if (!host.test(this.pageInfo.url)) { + continue; + } + + this.send({ + url: this.pageInfo.url, + documentURL: this.pageInfo.url, + name: this.pageInfo.title, + type: filter.type, + }); + break listLoop; } - - this.send({ - url: pageInfo.url, - documentURL: pageInfo.url, - name: pageInfo.title, - type: filter.type, - }); - break listLoop; } } - } + }; + + setTimeout(() => { + sendPageInfo(); + }, this.prepareDelay); } start(privacy: boolean = false) { @@ -100,13 +100,16 @@ export class SniffingHelper extends EventEmitter { } send = (item: SourceParams) => { + const urlCacheKey = `${item.url}_${item.name}`; + const cacheUrl = urlCache.get(urlCacheKey); + if (cacheUrl) { + return; + } + this.logger.info(`[SniffingHelper] send: ${item.url}`); - // 等待 DOM 中浮窗加载完成 - // if (this.ready) { - // } else { - // this.queue.push(item); - // } this.emit("source", item); + + urlCache.set(urlCacheKey, true); }; private onSendHeaders = (details: OnSendHeadersListenerDetails): void => { diff --git a/packages/main/src/services/WebviewService.ts b/packages/main/src/services/WebviewService.ts index b0bf7966..440a4f05 100644 --- a/packages/main/src/services/WebviewService.ts +++ b/packages/main/src/services/WebviewService.ts @@ -85,7 +85,7 @@ export default class WebviewService { onDomReady = () => { if (!this.view) return; const pageInfo = this.getPageInfo(); - this.sniffingHelper.reset(pageInfo); + this.sniffingHelper.update(pageInfo); this.window.webContents.send("webview-dom-ready", pageInfo); }; @@ -107,6 +107,8 @@ export default class WebviewService { } catch (err) { // empty } + + this.sniffingHelper.checkPageInfo(); }; onDidFailLoad = (e: Event, code: number, desc: string) => { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3a7d4c58..21ae4ecb 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -266,6 +266,9 @@ importers: lodash: specifier: ^4.17.21 version: 4.17.21 + lru-cache: + specifier: ^11.0.1 + version: 11.0.1 mime-types: specifier: ^2.1.35 version: 2.1.35 @@ -5022,6 +5025,10 @@ packages: resolution: {integrity: sha512-9hp3Vp2/hFQUiIwKo8XCeFVnrg8Pk3TYNPIR7tJADKi5YfcF7vEaK7avFHTlSy3kOKYaJQaalfEo6YuXdceBOQ==} engines: {node: 14 || >=16.14} + lru-cache@11.0.1: + resolution: {integrity: sha512-CgeuL5uom6j/ZVrg7G/+1IXqRY8JXX4Hghfy5YE0EhoYQWvndP1kufu58cmZLNIDKnRhZrXfdS9urVWx98AipQ==} + engines: {node: 20 || >=22} + lru-cache@6.0.0: resolution: {integrity: sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==} engines: {node: '>=10'} @@ -12375,6 +12382,8 @@ snapshots: lru-cache@10.2.2: {} + lru-cache@11.0.1: {} + lru-cache@6.0.0: dependencies: yallist: 4.0.0