From 1e40bd013d29f2cddcd6b67981a2dff310ebb342 Mon Sep 17 00:00:00 2001 From: MyPrototypeWhat <43230886+MyPrototypeWhat@users.noreply.github.com> Date: Thu, 15 May 2025 22:09:53 +0800 Subject: [PATCH] fix: remove undici dependency and clean up ProxyManager code (#6020) --- package.json | 2 -- src/main/ipc.ts | 19 +++++----- src/main/services/ProxyManager.ts | 35 +++++++++---------- src/main/utils/index.ts | 18 ++++++++++ .../settings/DataSettings/DataSettings.tsx | 2 +- yarn.lock | 17 ++------- 6 files changed, 48 insertions(+), 45 deletions(-) diff --git a/package.json b/package.json index 49bbcf1f89..b069265ab3 100644 --- a/package.json +++ b/package.json @@ -87,7 +87,6 @@ "fast-xml-parser": "^5.2.0", "fetch-socks": "^1.3.2", "fs-extra": "^11.2.0", - "go-get-folder-size": "^0.5.5", "got-scraping": "^4.1.1", "jsdom": "^26.0.0", "markdown-it": "^14.1.0", @@ -100,7 +99,6 @@ "tar": "^7.4.3", "turndown": "^7.2.0", "turndown-plugin-gfm": "^1.0.2", - "undici": "^7.4.0", "webdav": "^5.8.0", "ws": "^8.18.1", "zipread": "^1.3.3" diff --git a/src/main/ipc.ts b/src/main/ipc.ts index dd7a5ca3b3..665e8114b7 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -8,7 +8,6 @@ import { IpcChannel } from '@shared/IpcChannel' import { Shortcut, ThemeMode } from '@types' import { BrowserWindow, ipcMain, nativeTheme, session, shell } from 'electron' import log from 'electron-log' -import { getFolderSizeBin } from 'go-get-folder-size' import { titleBarOverlayDark, titleBarOverlayLight } from './config' import AppUpdater from './services/AppUpdater' @@ -30,7 +29,7 @@ import storeSyncService from './services/StoreSyncService' import { TrayService } from './services/TrayService' import { setOpenLinkExternal } from './services/WebviewService' import { windowService } from './services/WindowService' -import { getResourcePath } from './utils' +import { calculateDirectorySize, getResourcePath } from './utils' import { decrypt, encrypt } from './utils/aes' import { getCacheDir, getConfigDir, getFilesDir } from './utils/file' import { compress, decompress } from './utils/zip' @@ -184,14 +183,16 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) { // get cache size ipcMain.handle(IpcChannel.App_GetCacheSize, async () => { const cachePath = getCacheDir() - const size = await getFolderSizeBin(cachePath, true, { - // ignore files that we can't access - loose: true - }).catch((err) => { - log.error('Failed to get cache size:', err) - }) + log.info(`Calculating cache size for path: ${cachePath}`) - return size || '0MB' + try { + const sizeInBytes = await calculateDirectorySize(cachePath) + const sizeInMB = (sizeInBytes / (1024 * 1024)).toFixed(2) + return `${sizeInMB}` + } catch (error: any) { + log.error(`Failed to calculate cache size for ${cachePath}: ${error.message}`) + return '0' + } }) // check for update diff --git a/src/main/services/ProxyManager.ts b/src/main/services/ProxyManager.ts index 84d3f84038..3a4aa09438 100644 --- a/src/main/services/ProxyManager.ts +++ b/src/main/services/ProxyManager.ts @@ -1,8 +1,7 @@ import { ProxyConfig as _ProxyConfig, session } from 'electron' -import { socksDispatcher } from 'fetch-socks' import { getSystemProxy } from 'os-proxy-config' import { ProxyAgent as GeneralProxyAgent } from 'proxy-agent' -import { ProxyAgent, setGlobalDispatcher } from 'undici' +// import { ProxyAgent, setGlobalDispatcher } from 'undici' type ProxyMode = 'system' | 'custom' | 'none' @@ -121,22 +120,22 @@ export class ProxyManager { return this.config.url || '' } - setGlobalProxy() { - const proxyUrl = this.config.url - if (proxyUrl) { - const [protocol, address] = proxyUrl.split('://') - const [host, port] = address.split(':') - if (!protocol.includes('socks')) { - setGlobalDispatcher(new ProxyAgent(proxyUrl)) - } else { - global[Symbol.for('undici.globalDispatcher.1')] = socksDispatcher({ - port: parseInt(port), - type: protocol === 'socks5' ? 5 : 4, - host: host - }) - } - } - } + // setGlobalProxy() { + // const proxyUrl = this.config.url + // if (proxyUrl) { + // const [protocol, address] = proxyUrl.split('://') + // const [host, port] = address.split(':') + // if (!protocol.includes('socks')) { + // setGlobalDispatcher(new ProxyAgent(proxyUrl)) + // } else { + // global[Symbol.for('undici.globalDispatcher.1')] = socksDispatcher({ + // port: parseInt(port), + // type: protocol === 'socks5' ? 5 : 4, + // host: host + // }) + // } + // } + // } } export const proxyManager = new ProxyManager() diff --git a/src/main/utils/index.ts b/src/main/utils/index.ts index 4a6fde670d..a5f63fcc42 100644 --- a/src/main/utils/index.ts +++ b/src/main/utils/index.ts @@ -1,4 +1,5 @@ import fs from 'node:fs' +import fsAsync from 'node:fs/promises' import path from 'node:path' import { app } from 'electron' @@ -52,3 +53,20 @@ export function makeSureDirExists(dir: string) { fs.mkdirSync(dir, { recursive: true }) } } + +export async function calculateDirectorySize(directoryPath: string): Promise { + let totalSize = 0 + const items = await fsAsync.readdir(directoryPath) + + for (const item of items) { + const itemPath = path.join(directoryPath, item) + const stats = await fsAsync.stat(itemPath) + + if (stats.isFile()) { + totalSize += stats.size + } else if (stats.isDirectory()) { + totalSize += await calculateDirectorySize(itemPath) + } + } + return totalSize +} diff --git a/src/renderer/src/pages/settings/DataSettings/DataSettings.tsx b/src/renderer/src/pages/settings/DataSettings/DataSettings.tsx index 50f838ce37..ec31fd6ae7 100644 --- a/src/renderer/src/pages/settings/DataSettings/DataSettings.tsx +++ b/src/renderer/src/pages/settings/DataSettings/DataSettings.tsx @@ -240,7 +240,7 @@ const DataSettings: FC = () => { {t('settings.data.clear_cache.title')} - ({cacheSize}) + {cacheSize && ({cacheSize}MB)}