fix(service-party-app): 延迟加载 proto 定义避免启动时崩溃

将 proto 文件加载改为延迟加载模式,在 connect() 时才加载,
避免模块加载时 app.isPackaged 还未准备好导致的路径错误。

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2025-12-28 23:38:42 -08:00
parent 4794cafdaa
commit e78b6e6dcb
1 changed files with 30 additions and 21 deletions

View File

@ -4,18 +4,6 @@ import * as path from 'path';
import { EventEmitter } from 'events';
import { app } from 'electron';
// Proto 文件路径 - 在打包后需要从 app.asar.unpacked 或 resources 目录加载
function getProtoPath(): string {
// 开发环境
if (!app.isPackaged) {
return path.join(__dirname, '../../proto/message_router.proto');
}
// 生产环境 - proto 文件需要解包
return path.join(process.resourcesPath, 'proto/message_router.proto');
}
const PROTO_PATH = getProtoPath();
// 定义 proto 包结构类型
interface ProtoPackage {
mpc?: {
@ -27,14 +15,34 @@ interface ProtoPackage {
};
}
// 加载 Proto 定义
const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
});
// 延迟加载的 Proto 定义
let packageDefinition: protoLoader.PackageDefinition | null = null;
// Proto 文件路径 - 在打包后需要从 app.asar.unpacked 或 resources 目录加载
function getProtoPath(): string {
// 开发环境
if (!app.isPackaged) {
return path.join(__dirname, '../../proto/message_router.proto');
}
// 生产环境 - proto 文件需要解包
return path.join(process.resourcesPath, 'proto/message_router.proto');
}
// 延迟加载 Proto 定义
function loadProtoDefinition(): protoLoader.PackageDefinition {
if (!packageDefinition) {
const protoPath = getProtoPath();
console.log('Loading proto from:', protoPath);
packageDefinition = protoLoader.loadSync(protoPath, {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
});
}
return packageDefinition;
}
interface SessionInfo {
sessionId: string;
@ -116,7 +124,8 @@ export class GrpcClient extends EventEmitter {
*/
async connect(address: string, useTLS?: boolean): Promise<void> {
return new Promise((resolve, reject) => {
const proto = grpc.loadPackageDefinition(packageDefinition) as ProtoPackage;
const definition = loadProtoDefinition();
const proto = grpc.loadPackageDefinition(definition) as ProtoPackage;
const MessageRouter = proto.mpc?.router?.v1?.MessageRouter;
if (!MessageRouter) {