接入proto

This commit is contained in:
chen wei bo
2025-08-13 17:23:54 +08:00
parent 707946304e
commit 48c712edc5
9 changed files with 2835 additions and 2 deletions
+14
View File
@@ -0,0 +1,14 @@
syntax = "proto3";
// 登录请求
message LoginReq {
string username = 1; // 用户名
string password = 2; // 密码
}
// 登录响应
message LoginRes {
bool success = 1; // 是否成功
string token = 2; // 登录成功返回的 token
string message = 3; // 失败时的错误信息
}
+22
View File
@@ -0,0 +1,22 @@
syntax = "proto3";
package user;
// 用户信息
message UserInfo {
int32 id = 1; // 用户ID
string nickname = 2; // 昵称
string email = 3; // 邮箱
}
// 获取用户信息请求
message GetUserReq {
int32 id = 1; // 要查询的用户ID
}
// 获取用户信息响应
message GetUserRes {
bool success = 1; // 是否成功
UserInfo data = 2; // 用户数据
string message = 3; // 错误信息
}
+52
View File
@@ -0,0 +1,52 @@
// Tools/build-proto.js
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const SRC = path.join(__dirname, '..', '..', 'Proto'); // .proto 源文件目录
const OUT_JS = path.join(__dirname, '..', '..', 'assets', 'Scripts', 'proto', 'proto.pb.js');
const OUT_DTS = path.join(__dirname, '..', '..', 'assets', 'Scripts', 'proto', 'proto.pb.d.ts');
// 递归收集所有 .proto 文件路径
function walk(dir) {
const files = [];
for (const name of fs.readdirSync(dir)) {
const p = path.join(dir, name);
const stat = fs.statSync(p);
if (stat.isDirectory()) files.push(...walk(p));
else if (name.endsWith('.proto')) files.push(p);
}
return files;
}
const allProtoFiles = walk(SRC);
if (allProtoFiles.length === 0) {
console.error('没有找到任何 .proto 文件!');
process.exit(1);
}
// 确保输出目录存在
fs.mkdirSync(path.dirname(OUT_JS), { recursive: true });
// 1) 生成一个 JS 文件
execSync([
'pbjs',
'-t static-module',
'-w es6', // 生成 ESM 语法
'--dependency protobufjs/minimal.js',
`-o "${OUT_JS}"`,
`-p "${SRC}"`, // 让 pbjs 识别 proto import
...allProtoFiles.map(f => `"${f}"`) // 所有 proto 文件
].join(' '), { stdio: 'inherit' });
// 2) 生成一个 TS 声明文件
execSync([
'pbts',
'--main',
`-o "${OUT_DTS}"`,
`"${OUT_JS}"`
].join(' '), { stdio: 'inherit' });
console.log(`生成完成!
JS 文件: ${OUT_JS}
TS 文件: ${OUT_DTS}`);
+15
View File
@@ -0,0 +1,15 @@
const fs = require('fs-extra');
const ps = require('path');
(async () => {
// 目标输出目录(proto 生成文件存放处)
const outDir = ps.join(__dirname, '..', '..', 'assets', 'Scripts', 'proto');
try {
await fs.emptyDir(outDir);
console.log(`已清空目录: ${outDir}`);
} catch (err) {
console.error(`清空目录失败: ${outDir}`, err);
process.exit(1);
}
})();
+34
View File
@@ -0,0 +1,34 @@
const fs = require('fs');
const path = require('path');
const DTS_DIR = path.join(__dirname, '..', '..', 'assets', 'Scripts', 'proto');
const NAMESPACE = 'proto';
function walk(dir) {
let results = [];
for (const name of fs.readdirSync(dir)) {
const p = path.join(dir, name);
const stat = fs.statSync(p);
if (stat.isDirectory()) results = results.concat(walk(p));
else if (name.endsWith('.d.ts')) results.push(p);
}
return results;
}
function wrapFile(filePath) {
const original = fs.readFileSync(filePath, 'utf8');
if (original.includes(`declare namespace ${NAMESPACE}`)) {
console.log(`跳过已包装文件: ${filePath}`);
return;
}
const wrapped = `declare namespace ${NAMESPACE} {\n${original}\n}\nexport default ${NAMESPACE};\n`;
fs.writeFileSync(filePath, wrapped, 'utf8');
console.log(`已包装文件: ${filePath}`);
}
const files = walk(DTS_DIR);
files.forEach(wrapFile);
console.log(`处理完成,共包装 ${files.length} 个 .d.ts 文件`);
+536
View File
@@ -0,0 +1,536 @@
declare namespace proto {
// DO NOT EDIT! This is a generated file. Edit the JSDoc in src/*.js instead and run 'npm run build:types'.
/** Properties of a LoginReq. */
export interface ILoginReq {
/** LoginReq username */
username?: (string|null);
/** LoginReq password */
password?: (string|null);
}
/** Represents a LoginReq. */
export class LoginReq implements ILoginReq {
/**
* Constructs a new LoginReq.
* @param [properties] Properties to set
*/
constructor(properties?: ILoginReq);
/** LoginReq username. */
public username: string;
/** LoginReq password. */
public password: string;
/**
* Creates a new LoginReq instance using the specified properties.
* @param [properties] Properties to set
* @returns LoginReq instance
*/
public static create(properties?: ILoginReq): LoginReq;
/**
* Encodes the specified LoginReq message. Does not implicitly {@link LoginReq.verify|verify} messages.
* @param message LoginReq message or plain object to encode
* @param [writer] Writer to encode to
* @returns Writer
*/
public static encode(message: ILoginReq, writer?: $protobuf.Writer): $protobuf.Writer;
/**
* Encodes the specified LoginReq message, length delimited. Does not implicitly {@link LoginReq.verify|verify} messages.
* @param message LoginReq message or plain object to encode
* @param [writer] Writer to encode to
* @returns Writer
*/
public static encodeDelimited(message: ILoginReq, writer?: $protobuf.Writer): $protobuf.Writer;
/**
* Decodes a LoginReq message from the specified reader or buffer.
* @param reader Reader or buffer to decode from
* @param [length] Message length if known beforehand
* @returns LoginReq
* @throws {Error} If the payload is not a reader or valid buffer
* @throws {$protobuf.util.ProtocolError} If required fields are missing
*/
public static decode(reader: ($protobuf.Reader|Uint8Array), length?: number): LoginReq;
/**
* Decodes a LoginReq message from the specified reader or buffer, length delimited.
* @param reader Reader or buffer to decode from
* @returns LoginReq
* @throws {Error} If the payload is not a reader or valid buffer
* @throws {$protobuf.util.ProtocolError} If required fields are missing
*/
public static decodeDelimited(reader: ($protobuf.Reader|Uint8Array)): LoginReq;
/**
* Verifies a LoginReq message.
* @param message Plain object to verify
* @returns `null` if valid, otherwise the reason why it is not
*/
public static verify(message: { [k: string]: any }): (string|null);
/**
* Creates a LoginReq message from a plain object. Also converts values to their respective internal types.
* @param object Plain object
* @returns LoginReq
*/
public static fromObject(object: { [k: string]: any }): LoginReq;
/**
* Creates a plain object from a LoginReq message. Also converts values to other types if specified.
* @param message LoginReq
* @param [options] Conversion options
* @returns Plain object
*/
public static toObject(message: LoginReq, options?: $protobuf.IConversionOptions): { [k: string]: any };
/**
* Converts this LoginReq to JSON.
* @returns JSON object
*/
public toJSON(): { [k: string]: any };
/**
* Gets the default type url for LoginReq
* @param [typeUrlPrefix] your custom typeUrlPrefix(default "type.googleapis.com")
* @returns The default type url
*/
public static getTypeUrl(typeUrlPrefix?: string): string;
}
/** Properties of a LoginRes. */
export interface ILoginRes {
/** LoginRes success */
success?: (boolean|null);
/** LoginRes token */
token?: (string|null);
/** LoginRes message */
message?: (string|null);
}
/** Represents a LoginRes. */
export class LoginRes implements ILoginRes {
/**
* Constructs a new LoginRes.
* @param [properties] Properties to set
*/
constructor(properties?: ILoginRes);
/** LoginRes success. */
public success: boolean;
/** LoginRes token. */
public token: string;
/** LoginRes message. */
public message: string;
/**
* Creates a new LoginRes instance using the specified properties.
* @param [properties] Properties to set
* @returns LoginRes instance
*/
public static create(properties?: ILoginRes): LoginRes;
/**
* Encodes the specified LoginRes message. Does not implicitly {@link LoginRes.verify|verify} messages.
* @param message LoginRes message or plain object to encode
* @param [writer] Writer to encode to
* @returns Writer
*/
public static encode(message: ILoginRes, writer?: $protobuf.Writer): $protobuf.Writer;
/**
* Encodes the specified LoginRes message, length delimited. Does not implicitly {@link LoginRes.verify|verify} messages.
* @param message LoginRes message or plain object to encode
* @param [writer] Writer to encode to
* @returns Writer
*/
public static encodeDelimited(message: ILoginRes, writer?: $protobuf.Writer): $protobuf.Writer;
/**
* Decodes a LoginRes message from the specified reader or buffer.
* @param reader Reader or buffer to decode from
* @param [length] Message length if known beforehand
* @returns LoginRes
* @throws {Error} If the payload is not a reader or valid buffer
* @throws {$protobuf.util.ProtocolError} If required fields are missing
*/
public static decode(reader: ($protobuf.Reader|Uint8Array), length?: number): LoginRes;
/**
* Decodes a LoginRes message from the specified reader or buffer, length delimited.
* @param reader Reader or buffer to decode from
* @returns LoginRes
* @throws {Error} If the payload is not a reader or valid buffer
* @throws {$protobuf.util.ProtocolError} If required fields are missing
*/
public static decodeDelimited(reader: ($protobuf.Reader|Uint8Array)): LoginRes;
/**
* Verifies a LoginRes message.
* @param message Plain object to verify
* @returns `null` if valid, otherwise the reason why it is not
*/
public static verify(message: { [k: string]: any }): (string|null);
/**
* Creates a LoginRes message from a plain object. Also converts values to their respective internal types.
* @param object Plain object
* @returns LoginRes
*/
public static fromObject(object: { [k: string]: any }): LoginRes;
/**
* Creates a plain object from a LoginRes message. Also converts values to other types if specified.
* @param message LoginRes
* @param [options] Conversion options
* @returns Plain object
*/
public static toObject(message: LoginRes, options?: $protobuf.IConversionOptions): { [k: string]: any };
/**
* Converts this LoginRes to JSON.
* @returns JSON object
*/
public toJSON(): { [k: string]: any };
/**
* Gets the default type url for LoginRes
* @param [typeUrlPrefix] your custom typeUrlPrefix(default "type.googleapis.com")
* @returns The default type url
*/
public static getTypeUrl(typeUrlPrefix?: string): string;
}
/** Namespace user. */
export namespace user {
/** Properties of a UserInfo. */
interface IUserInfo {
/** UserInfo id */
id?: (number|null);
/** UserInfo nickname */
nickname?: (string|null);
/** UserInfo email */
email?: (string|null);
}
/** Represents a UserInfo. */
class UserInfo implements IUserInfo {
/**
* Constructs a new UserInfo.
* @param [properties] Properties to set
*/
constructor(properties?: user.IUserInfo);
/** UserInfo id. */
public id: number;
/** UserInfo nickname. */
public nickname: string;
/** UserInfo email. */
public email: string;
/**
* Creates a new UserInfo instance using the specified properties.
* @param [properties] Properties to set
* @returns UserInfo instance
*/
public static create(properties?: user.IUserInfo): user.UserInfo;
/**
* Encodes the specified UserInfo message. Does not implicitly {@link user.UserInfo.verify|verify} messages.
* @param message UserInfo message or plain object to encode
* @param [writer] Writer to encode to
* @returns Writer
*/
public static encode(message: user.IUserInfo, writer?: $protobuf.Writer): $protobuf.Writer;
/**
* Encodes the specified UserInfo message, length delimited. Does not implicitly {@link user.UserInfo.verify|verify} messages.
* @param message UserInfo message or plain object to encode
* @param [writer] Writer to encode to
* @returns Writer
*/
public static encodeDelimited(message: user.IUserInfo, writer?: $protobuf.Writer): $protobuf.Writer;
/**
* Decodes a UserInfo message from the specified reader or buffer.
* @param reader Reader or buffer to decode from
* @param [length] Message length if known beforehand
* @returns UserInfo
* @throws {Error} If the payload is not a reader or valid buffer
* @throws {$protobuf.util.ProtocolError} If required fields are missing
*/
public static decode(reader: ($protobuf.Reader|Uint8Array), length?: number): user.UserInfo;
/**
* Decodes a UserInfo message from the specified reader or buffer, length delimited.
* @param reader Reader or buffer to decode from
* @returns UserInfo
* @throws {Error} If the payload is not a reader or valid buffer
* @throws {$protobuf.util.ProtocolError} If required fields are missing
*/
public static decodeDelimited(reader: ($protobuf.Reader|Uint8Array)): user.UserInfo;
/**
* Verifies a UserInfo message.
* @param message Plain object to verify
* @returns `null` if valid, otherwise the reason why it is not
*/
public static verify(message: { [k: string]: any }): (string|null);
/**
* Creates a UserInfo message from a plain object. Also converts values to their respective internal types.
* @param object Plain object
* @returns UserInfo
*/
public static fromObject(object: { [k: string]: any }): user.UserInfo;
/**
* Creates a plain object from a UserInfo message. Also converts values to other types if specified.
* @param message UserInfo
* @param [options] Conversion options
* @returns Plain object
*/
public static toObject(message: user.UserInfo, options?: $protobuf.IConversionOptions): { [k: string]: any };
/**
* Converts this UserInfo to JSON.
* @returns JSON object
*/
public toJSON(): { [k: string]: any };
/**
* Gets the default type url for UserInfo
* @param [typeUrlPrefix] your custom typeUrlPrefix(default "type.googleapis.com")
* @returns The default type url
*/
public static getTypeUrl(typeUrlPrefix?: string): string;
}
/** Properties of a GetUserReq. */
interface IGetUserReq {
/** GetUserReq id */
id?: (number|null);
}
/** Represents a GetUserReq. */
class GetUserReq implements IGetUserReq {
/**
* Constructs a new GetUserReq.
* @param [properties] Properties to set
*/
constructor(properties?: user.IGetUserReq);
/** GetUserReq id. */
public id: number;
/**
* Creates a new GetUserReq instance using the specified properties.
* @param [properties] Properties to set
* @returns GetUserReq instance
*/
public static create(properties?: user.IGetUserReq): user.GetUserReq;
/**
* Encodes the specified GetUserReq message. Does not implicitly {@link user.GetUserReq.verify|verify} messages.
* @param message GetUserReq message or plain object to encode
* @param [writer] Writer to encode to
* @returns Writer
*/
public static encode(message: user.IGetUserReq, writer?: $protobuf.Writer): $protobuf.Writer;
/**
* Encodes the specified GetUserReq message, length delimited. Does not implicitly {@link user.GetUserReq.verify|verify} messages.
* @param message GetUserReq message or plain object to encode
* @param [writer] Writer to encode to
* @returns Writer
*/
public static encodeDelimited(message: user.IGetUserReq, writer?: $protobuf.Writer): $protobuf.Writer;
/**
* Decodes a GetUserReq message from the specified reader or buffer.
* @param reader Reader or buffer to decode from
* @param [length] Message length if known beforehand
* @returns GetUserReq
* @throws {Error} If the payload is not a reader or valid buffer
* @throws {$protobuf.util.ProtocolError} If required fields are missing
*/
public static decode(reader: ($protobuf.Reader|Uint8Array), length?: number): user.GetUserReq;
/**
* Decodes a GetUserReq message from the specified reader or buffer, length delimited.
* @param reader Reader or buffer to decode from
* @returns GetUserReq
* @throws {Error} If the payload is not a reader or valid buffer
* @throws {$protobuf.util.ProtocolError} If required fields are missing
*/
public static decodeDelimited(reader: ($protobuf.Reader|Uint8Array)): user.GetUserReq;
/**
* Verifies a GetUserReq message.
* @param message Plain object to verify
* @returns `null` if valid, otherwise the reason why it is not
*/
public static verify(message: { [k: string]: any }): (string|null);
/**
* Creates a GetUserReq message from a plain object. Also converts values to their respective internal types.
* @param object Plain object
* @returns GetUserReq
*/
public static fromObject(object: { [k: string]: any }): user.GetUserReq;
/**
* Creates a plain object from a GetUserReq message. Also converts values to other types if specified.
* @param message GetUserReq
* @param [options] Conversion options
* @returns Plain object
*/
public static toObject(message: user.GetUserReq, options?: $protobuf.IConversionOptions): { [k: string]: any };
/**
* Converts this GetUserReq to JSON.
* @returns JSON object
*/
public toJSON(): { [k: string]: any };
/**
* Gets the default type url for GetUserReq
* @param [typeUrlPrefix] your custom typeUrlPrefix(default "type.googleapis.com")
* @returns The default type url
*/
public static getTypeUrl(typeUrlPrefix?: string): string;
}
/** Properties of a GetUserRes. */
interface IGetUserRes {
/** GetUserRes success */
success?: (boolean|null);
/** GetUserRes data */
data?: (user.IUserInfo|null);
/** GetUserRes message */
message?: (string|null);
}
/** Represents a GetUserRes. */
class GetUserRes implements IGetUserRes {
/**
* Constructs a new GetUserRes.
* @param [properties] Properties to set
*/
constructor(properties?: user.IGetUserRes);
/** GetUserRes success. */
public success: boolean;
/** GetUserRes data. */
public data?: (user.IUserInfo|null);
/** GetUserRes message. */
public message: string;
/**
* Creates a new GetUserRes instance using the specified properties.
* @param [properties] Properties to set
* @returns GetUserRes instance
*/
public static create(properties?: user.IGetUserRes): user.GetUserRes;
/**
* Encodes the specified GetUserRes message. Does not implicitly {@link user.GetUserRes.verify|verify} messages.
* @param message GetUserRes message or plain object to encode
* @param [writer] Writer to encode to
* @returns Writer
*/
public static encode(message: user.IGetUserRes, writer?: $protobuf.Writer): $protobuf.Writer;
/**
* Encodes the specified GetUserRes message, length delimited. Does not implicitly {@link user.GetUserRes.verify|verify} messages.
* @param message GetUserRes message or plain object to encode
* @param [writer] Writer to encode to
* @returns Writer
*/
public static encodeDelimited(message: user.IGetUserRes, writer?: $protobuf.Writer): $protobuf.Writer;
/**
* Decodes a GetUserRes message from the specified reader or buffer.
* @param reader Reader or buffer to decode from
* @param [length] Message length if known beforehand
* @returns GetUserRes
* @throws {Error} If the payload is not a reader or valid buffer
* @throws {$protobuf.util.ProtocolError} If required fields are missing
*/
public static decode(reader: ($protobuf.Reader|Uint8Array), length?: number): user.GetUserRes;
/**
* Decodes a GetUserRes message from the specified reader or buffer, length delimited.
* @param reader Reader or buffer to decode from
* @returns GetUserRes
* @throws {Error} If the payload is not a reader or valid buffer
* @throws {$protobuf.util.ProtocolError} If required fields are missing
*/
public static decodeDelimited(reader: ($protobuf.Reader|Uint8Array)): user.GetUserRes;
/**
* Verifies a GetUserRes message.
* @param message Plain object to verify
* @returns `null` if valid, otherwise the reason why it is not
*/
public static verify(message: { [k: string]: any }): (string|null);
/**
* Creates a GetUserRes message from a plain object. Also converts values to their respective internal types.
* @param object Plain object
* @returns GetUserRes
*/
public static fromObject(object: { [k: string]: any }): user.GetUserRes;
/**
* Creates a plain object from a GetUserRes message. Also converts values to other types if specified.
* @param message GetUserRes
* @param [options] Conversion options
* @returns Plain object
*/
public static toObject(message: user.GetUserRes, options?: $protobuf.IConversionOptions): { [k: string]: any };
/**
* Converts this GetUserRes to JSON.
* @returns JSON object
*/
public toJSON(): { [k: string]: any };
/**
* Gets the default type url for GetUserRes
* @param [typeUrlPrefix] your custom typeUrlPrefix(default "type.googleapis.com")
* @returns The default type url
*/
public static getTypeUrl(typeUrlPrefix?: string): string;
}
}
}
export default proto;
File diff suppressed because it is too large Load Diff
+936 -1
View File
File diff suppressed because it is too large Load Diff
+9 -1
View File
@@ -12,6 +12,14 @@
},
"dependencies": {
"@google/genai": "^1.13.0",
"buffer": "^6.0.3"
"buffer": "^6.0.3",
"protobufjs": "^7.5.3"
},
"devDependencies": {
"fs-extra": "^11.3.1",
"protobufjs-cli": "^1.1.3"
},
"scripts": {
"build-proto": "node ./Tools/Proto/clear-proto.js && node ./Tools/Proto/build-proto.js && node ./Tools/Proto/wrap-pbts-result.js"
}
}