fix: remove undici dependency and clean up ProxyManager code (#6020)

This commit is contained in:
MyPrototypeWhat 2025-05-15 22:09:53 +08:00 committed by GitHub
parent 0d7f965e29
commit 1e40bd013d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 48 additions and 45 deletions

View File

@ -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"

View File

@ -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

View File

@ -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()

View File

@ -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<number> {
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
}

View File

@ -240,7 +240,7 @@ const DataSettings: FC = () => {
<SettingRow>
<SettingRowTitle>
{t('settings.data.clear_cache.title')}
<CacheText>({cacheSize})</CacheText>
{cacheSize && <CacheText>({cacheSize}MB)</CacheText>}
</SettingRowTitle>
<HStack gap="5px">
<Button onClick={handleClearCache} danger>

View File

@ -4431,7 +4431,6 @@ __metadata:
fast-xml-parser: "npm:^5.2.0"
fetch-socks: "npm:^1.3.2"
fs-extra: "npm:^11.2.0"
go-get-folder-size: "npm:^0.5.5"
got-scraping: "npm:^4.1.1"
html-to-image: "npm:^1.11.13"
husky: "npm:^9.1.7"
@ -4484,7 +4483,6 @@ __metadata:
turndown: "npm:^7.2.0"
turndown-plugin-gfm: "npm:^1.0.2"
typescript: "npm:^5.6.2"
undici: "npm:^7.4.0"
uuid: "npm:^10.0.0"
vite: "npm:6.2.6"
vitest: "npm:^3.1.1"
@ -8945,17 +8943,6 @@ __metadata:
languageName: node
linkType: hard
"go-get-folder-size@npm:^0.5.5":
version: 0.5.5
resolution: "go-get-folder-size@npm:0.5.5"
dependencies:
std-env: "npm:^3.7.0"
bin:
go-get-folder-size: bin/cli.js
checksum: 10c0/eb69b686952218cc114dccf65763e0dff5056050fa3e9b2afa6161b933c3978500455503b104f9f28d96aab2fedd64ccb0255d6ea16d699fe020795f431ed7b8
languageName: node
linkType: hard
"google-auth-library@npm:^9.14.2":
version: 9.15.1
resolution: "google-auth-library@npm:9.15.1"
@ -15945,7 +15932,7 @@ __metadata:
languageName: node
linkType: hard
"std-env@npm:^3.7.0, std-env@npm:^3.8.1":
"std-env@npm:^3.8.1":
version: 3.9.0
resolution: "std-env@npm:3.9.0"
checksum: 10c0/4a6f9218aef3f41046c3c7ecf1f98df00b30a07f4f35c6d47b28329bc2531eef820828951c7d7b39a1c5eb19ad8a46e3ddfc7deb28f0a2f3ceebee11bab7ba50
@ -16901,7 +16888,7 @@ __metadata:
languageName: node
linkType: hard
"undici@npm:>=6, undici@npm:^7.4.0":
"undici@npm:>=6":
version: 7.8.0
resolution: "undici@npm:7.8.0"
checksum: 10c0/7141f63ea405208a88120d211d83d77bf21327b16b451d3149fb266c28884fbcf78ec370ac2d3412a0e68ba6132ab85265ba85a2f4fde24cb47dc77f5c5a158c