稳定测试版

This commit is contained in:
2025-10-28 19:40:13 +08:00
commit 183feef2ea
24 changed files with 11631 additions and 0 deletions

11
main/utils/guard.js Normal file
View File

@@ -0,0 +1,11 @@
// main/utils/guard.js
const { app } = require('electron')
function setupGuards() {
process.on('uncaughtException', (error) => console.error('uncaughtException:', error))
process.on('unhandledRejection', (reason) => console.error('unhandledRejection:', reason))
app.on('web-contents-created', (_, contents) => {
contents.on('render-process-gone', (_e, details) => console.error('渲染崩溃:', details))
})
}
module.exports = { setupGuards }

25
main/utils/mem.js Normal file
View File

@@ -0,0 +1,25 @@
// main/utils/mem.js
const { app } = require('electron')
function toMB(v) {
if (!v || v <= 0) return 0
const kbToMB = v / 1024
if (kbToMB > 1) return Number(kbToMB.toFixed(1))
return Number(v.toFixed(1))
}
function dumpAllMem() {
try {
const metrics = app.getAppMetrics()
const report = metrics.map(m => {
const mem = m.memory || {}
const workingSetMB = toMB(mem.workingSetSize ?? mem.workingSet ?? 0)
const privateMB = toMB(mem.privateBytes ?? mem.private ?? 0)
const sharedMB = toMB(mem.shared ?? 0)
return { pid: m.pid, type: m.type, workingSetMB, privateMB, sharedMB }
})
// console.log(report)
} catch (e) {
console.warn('getAppMetrics error:', e)
}
}
module.exports = { toMB, dumpAllMem }

21
main/utils/paths.js Normal file
View File

@@ -0,0 +1,21 @@
// main/utils/paths.js
const path = require('path')
const { fileURLToPath } = require('node:url')
const { app } = require('electron')
function normalizePath(targetPath, baseDir) {
if (typeof targetPath !== 'string' || !targetPath.trim()) throw new Error('无效的路径参数')
if (targetPath.startsWith('file://')) return fileURLToPath(new URL(targetPath))
if (!path.isAbsolute(targetPath)) {
const base = baseDir && typeof baseDir === 'string' ? baseDir : process.cwd()
return path.resolve(base, targetPath)
}
return path.resolve(targetPath)
}
function resolveResource(relPath) {
const base = app.isPackaged ? process.resourcesPath : path.resolve(__dirname, '..')
return path.join(base, relPath)
}
module.exports = { normalizePath, resolveResource }