├── .gitignore ├── .vscode └── launch.json ├── LICENSE ├── README.md ├── out ├── cli.js ├── index.js └── index.js.map ├── package-lock.json ├── package.json ├── pblib ├── protobuf-library.d.ts ├── protobuf-library.js └── protobuf-library.min.js ├── src └── index.ts ├── test └── protobuf │ ├── bundles │ ├── proto_bundle.js │ └── proto_bundle.min.js │ ├── dts │ └── proto_bundle.d.ts │ ├── library │ ├── protobuf-library.d.ts │ ├── protobuf-library.js │ └── protobuf-library.min.js │ ├── pbconfig.json │ └── protofile │ ├── df_1000.proto │ ├── df_1001.proto │ ├── df_1002.proto │ ├── df_1003.proto │ ├── df_1004.proto │ └── df_1005.proto └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // 使用 IntelliSense 了解相关属性。 3 | // 悬停以查看现有属性的描述。 4 | // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "type": "node", 9 | "request": "launch", 10 | "name": "启动程序", 11 | "skipFiles": [ 12 | "/**" 13 | ], 14 | "program": "${workspaceFolder}\\out\\cli.js", 15 | "args": [ 16 | "g" 17 | ], 18 | "cwd": "${workspaceFolder}\\test" 19 | } 20 | ] 21 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 AILHC 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # egf protobuf 2 | 3 | ## 仓库迁移提示 4 | 5 | 这个仓库不再更新了哦。因为我把这个工具进行优化集成到我的框架仓库中了。会持续优化更新~ 6 | 7 | 传送门: [EasyGameFramework](https://github.com/AILHC/EasyGameFrameworkOpen.git) 8 | 9 | ## 特性 10 | 11 | 12 | 1. 提供 protobuf.js 基础运行时库 13 | 2. 提供命令行脚本,将 protofile 生成 JavaScript 代码 14 | 3. 生成正确的 .d.ts 代码,以方便 TypeScript 项目使用 15 | 5. 理论上支持所有 HTML5 游戏引擎。欢迎使用 PIXI.js , Cocos2d-js , LayaAir 等其他引擎的开发者使用本库。 16 | 6. 封装protobufjs的命令行,不需另外安装protobufjs 17 | 18 | ## 原理 19 | 20 | 封装了 protobufjs 库及命令行。使用 protobufjs 6.8.4 的运行时库和命令行工具。 21 | 22 | protobufjs 自身存在着 pbts 命令,虽然也可以生成 .d.ts 文件,但是在全局模式而非 ES6 module 的情况下存在一些错误,本项目致力于解决这个问题,使 protobufjs 可以在非 ES6 模块项目中(比如白鹭引擎,LayaAir引擎,Cocoscreator引擎)中也可以使用 protobufjs 23 | 24 | protobufjs 提供了多种使用方式,由于微信小游戏禁止 eval , new Function 等动态代码形式,所以本项目只提供了生成代码的形式,不支持通过 ```protobuf.load('awesome.proto')``` 的方式(因为这种方式也无法在微信小游戏中运行)。 25 | 26 | 27 | ## 如何安装 28 | 29 | ``` 30 | npm install egf-protobuf -g 31 | 或者 32 | npm install -S egf-protobuf 33 | ``` 34 | 35 | ## 如何使用 36 | 37 | 38 | + 假设用户有个名为 project 的项目 39 | 40 | ``` 41 | cd projectRoot 42 | egf-pb init //初始化项目 43 | ``` 44 | 45 | + 将 protofile 文件放在 projectRoot/protobuf/protofile 文件夹中 46 | + 配置protobuf/pbconfig.json文件 47 | ```json 48 | { 49 | "options": { 50 | "no-create": false,//不生成用于反射兼容性的create函数。 51 | "no-verify": false,//不生成验证函数。 52 | "no-convert": true,//不生成解码函数。 53 | "no-delimited": true,//不生成带分隔符的编码/解码函数。 54 | "no-encode":false,//没有生成像from/toObject这样的转换函数吗 55 | "no-decode":false//不生成编码函数。 56 | }, 57 | 58 | "outputFileType":0,//导出文件类型 0 全部(js和.min.js)1(js) 2(.min.js) 59 | "dtsOutDir":"../",//定义文件输出目录 60 | "sourceRoot": "../priv",//proto文件目录 61 | "outFileName":"",//输出文件名 62 | "outputDir":"../assets/proto"//输出文件夹 63 | } 64 | ``` 65 | + 使用生成命令 66 | 67 | egf-pb generate 68 | 或者 69 | egf-pb g 70 | 71 | 72 | ## 更新日志 73 | 74 | ### 1.2.0 75 | 76 | 封装protobufjs的命令行,不需要另外安装protobufjs命令行 77 | 优化生成逻辑,更快了 78 | 优化文件写入逻辑,避免文件夹不存在报错 79 | 80 | ### 1.0.2 81 | 修复和确认某些windows环境 生成报错的问题 82 | 83 | ### 1.0.1 84 | 命令行命令运行使用第三方库commander; 85 | 使用方式修改 86 | 87 | 1. pb-egf g<或者generate> //生成当前项目目录下的protojs 88 | 2. pb-egf a<或者add> //拷贝proto .d.ts定义文件,以及protojs的解析库,还有pb-egf的配置文件 可以传参数,egret 就是egret项目,不传则是通用初始化 89 | 90 | 91 | ### 1.0.0 92 | 初始版本,基于pb-egret改造,更加自由,protobuf的库文件和proto文件合并,兼容cocosCreator的使用 93 | 94 | ## 已知问题 95 | 96 | proto 文件中的每一个协议一定要从属于一个 package,否则.d.ts生成会出现错误导致 ts 文件无法正确的找到这些类 97 | 98 | 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /out/cli.js: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env node 2 | 3 | var cmd = require('commander'); 4 | var script = require('./index'); 5 | cmd 6 | .command("generate") 7 | .alias('g') 8 | .description("生成protojs") 9 | .action(async option=>{ 10 | console.log("生成protojs"); 11 | await script.generate("."); 12 | }); 13 | cmd 14 | .command("init") 15 | .alias('i') 16 | .description("初始化项目") 17 | .action(async (...arg)=>{ 18 | console.log("初始化项目"); 19 | await script.initProj(".",arg[0]); 20 | }) 21 | cmd.parse(process.argv); -------------------------------------------------------------------------------- /out/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { 3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } 4 | return new (P || (P = Promise))(function (resolve, reject) { 5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } 6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } 7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } 8 | step((generator = generator.apply(thisArg, _arguments || [])).next()); 9 | }); 10 | }; 11 | var __generator = (this && this.__generator) || function (thisArg, body) { 12 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; 13 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; 14 | function verb(n) { return function (v) { return step([n, v]); }; } 15 | function step(op) { 16 | if (f) throw new TypeError("Generator is already executing."); 17 | while (_) try { 18 | if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; 19 | if (y = 0, t) op = [op[0] & 2, t.value]; 20 | switch (op[0]) { 21 | case 0: case 1: t = op; break; 22 | case 4: _.label++; return { value: op[1], done: false }; 23 | case 5: _.label++; y = op[1]; op = [0]; continue; 24 | case 7: op = _.ops.pop(); _.trys.pop(); continue; 25 | default: 26 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } 27 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } 28 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } 29 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } 30 | if (t[2]) _.ops.pop(); 31 | _.trys.pop(); continue; 32 | } 33 | op = body.call(thisArg, _); 34 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } 35 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; 36 | } 37 | }; 38 | Object.defineProperty(exports, "__esModule", { value: true }); 39 | exports.initProj = exports.generate = void 0; 40 | var child_process = require("child_process"); 41 | var fs = require("fs-extra-promise"); 42 | var path = require("path"); 43 | var UglifyJS = require("uglify-js"); 44 | var rimraf = require("rimraf"); 45 | var root = path.resolve(__filename, '../../'); 46 | var pbjs = require("protobufjs/cli/pbjs"); 47 | var pbts = require("protobufjs/cli/pbts"); 48 | function shell(command, args) { 49 | return new Promise(function (resolve, reject) { 50 | var cmd = command + " " + args.join(" "); 51 | child_process.exec(cmd, function (error, stdout, stderr) { 52 | if (error) { 53 | console.error("\u68C0\u67E5\u662F\u5426\u5B89\u88C5\u4E86protobufjs"); 54 | reject(error); 55 | } 56 | else { 57 | resolve(stdout); 58 | } 59 | }); 60 | }); 61 | } 62 | var pbconfigContent = JSON.stringify({ 63 | options: { 64 | "no-create": false, 65 | "no-verify": false, 66 | "no-convert": true, 67 | "no-delimited": false 68 | }, 69 | outputFileType: 0, 70 | dtsOutDir: "protofile", 71 | outFileName: "proto_bundle", 72 | sourceRoot: "protofile", 73 | outputDir: "bundles" 74 | }, null, '\t'); 75 | process.on('unhandledRejection', function (reason, p) { 76 | console.log('Unhandled Rejection at: Promise', p, 'reason:', reason); 77 | }); 78 | function generate(rootDir) { 79 | return __awaiter(this, void 0, void 0, function () { 80 | var pbconfigPath, pbconfigPath_1, pbconfig, tempfile, pbjsFile, dirname, protoRoot, fileList, protoList, args, pbjsResult, pbjsLib, outPbj, minjs, pbtsResult, dtsOut, isExit_dts, dtsOutDirPath, isExit_dtsOutDir; 81 | var _this = this; 82 | return __generator(this, function (_a) { 83 | switch (_a.label) { 84 | case 0: 85 | pbconfigPath = path.join(rootDir, 'pbconfig.json'); 86 | return [4 /*yield*/, fs.existsAsync(pbconfigPath)]; 87 | case 1: 88 | if (!!(_a.sent())) return [3 /*break*/, 9]; 89 | return [4 /*yield*/, fs.existsAsync(path.join(rootDir, 'protobuf'))]; 90 | case 2: 91 | if (!_a.sent()) return [3 /*break*/, 7]; 92 | pbconfigPath_1 = path.join(rootDir, 'protobuf', 'pbconfig.json'); 93 | return [4 /*yield*/, (fs.existsAsync(pbconfigPath_1))]; 94 | case 3: 95 | if (!!(_a.sent())) return [3 /*break*/, 5]; 96 | return [4 /*yield*/, fs.writeFileAsync(pbconfigPath_1, pbconfigContent, 'utf-8')]; 97 | case 4: 98 | _a.sent(); 99 | _a.label = 5; 100 | case 5: return [4 /*yield*/, generate(path.join(rootDir, 'protobuf'))]; 101 | case 6: 102 | _a.sent(); 103 | return [3 /*break*/, 8]; 104 | case 7: throw '请首先执行 pb-egf add 命令'; 105 | case 8: return [2 /*return*/]; 106 | case 9: return [4 /*yield*/, fs.readJSONAsync(path.join(rootDir, 'pbconfig.json'))]; 107 | case 10: 108 | pbconfig = _a.sent(); 109 | tempfile = path.join(rootDir, 'pbtemp.js'); 110 | return [4 /*yield*/, fs.mkdirpAsync(path.dirname(tempfile)).catch(function (res) { 111 | console.log(res); 112 | })]; 113 | case 11: 114 | _a.sent(); 115 | return [4 /*yield*/, fs.writeFileAsync(tempfile, "")]; 116 | case 12: 117 | _a.sent(); 118 | pbjsFile = path.join(rootDir, pbconfig.outputDir + "/" + pbconfig.outFileName + ".js"); 119 | dirname = path.dirname(pbjsFile); 120 | return [4 /*yield*/, new Promise(function (res, rej) { 121 | rimraf(dirname, function () { 122 | res(); 123 | }); 124 | })]; 125 | case 13: 126 | _a.sent(); 127 | return [4 /*yield*/, fs.mkdirpAsync(dirname).catch(function (res) { 128 | console.log(res); 129 | })]; 130 | case 14: 131 | _a.sent(); 132 | protoRoot = path.join(rootDir, pbconfig.sourceRoot); 133 | return [4 /*yield*/, fs.readdirAsync(protoRoot)]; 134 | case 15: 135 | fileList = _a.sent(); 136 | protoList = fileList.filter(function (item) { return path.extname(item) === '.proto'; }); 137 | if (protoList.length == 0) { 138 | throw ' protofile 文件夹中不存在 .proto 文件'; 139 | } 140 | return [4 /*yield*/, Promise.all(protoList.map(function (protofile) { return __awaiter(_this, void 0, void 0, function () { 141 | var content; 142 | return __generator(this, function (_a) { 143 | switch (_a.label) { 144 | case 0: return [4 /*yield*/, fs.readFileAsync(path.join(protoRoot, protofile), 'utf-8')]; 145 | case 1: 146 | content = _a.sent(); 147 | if (content.indexOf('package') == -1) { 148 | throw protofile + " \u4E2D\u5FC5\u987B\u5305\u542B package \u5B57\u6BB5"; 149 | } 150 | return [2 /*return*/]; 151 | } 152 | }); 153 | }); }))]; 154 | case 16: 155 | _a.sent(); 156 | args = ['-t', 'static', '--keep-case', '-p', protoRoot].concat(protoList); 157 | if (pbconfig.options['no-create']) { 158 | args.unshift('--no-create'); 159 | } 160 | if (pbconfig.options['no-verify']) { 161 | args.unshift('--no-verify'); 162 | } 163 | if (pbconfig.options['no-convert']) { 164 | args.unshift('--no-convert'); 165 | } 166 | if (pbconfig.options["no-delimited"]) { 167 | args.unshift("--no-delimited"); 168 | } 169 | console.log("[egf-protobuf]解析proto文件"); 170 | return [4 /*yield*/, new Promise(function (res) { 171 | pbjs.main(args, function (err, output) { 172 | if (err) { 173 | console.error(err); 174 | } 175 | res(output); 176 | return {}; 177 | }); 178 | })]; 179 | case 17: 180 | pbjsResult = _a.sent(); 181 | return [4 /*yield*/, fs.readFileAsync(path.join(root, 'pblib/protobuf-library.min.js')).catch(function (res) { console.log(res); })]; 182 | case 18: 183 | pbjsLib = _a.sent(); 184 | outPbj = pbjsLib + 'var $protobuf = window.protobuf;\n$protobuf.roots.default=window;\n' + pbjsResult; 185 | console.log("[egf-protobuf]解析proto文件->完成" + pbjsFile); 186 | if (!(pbconfig.outputFileType === 0 || pbconfig.outputFileType === 1)) return [3 /*break*/, 20]; 187 | console.log("[egf-protobuf]生成js文件"); 188 | return [4 /*yield*/, fs.writeFileAsync(pbjsFile, outPbj, 'utf-8').catch(function (res) { console.log(res); })]; 189 | case 19: 190 | _a.sent(); 191 | ; 192 | console.log("[egf-protobuf]生成js文件->完成"); 193 | _a.label = 20; 194 | case 20: 195 | if (!(pbconfig.outputFileType === 0 || pbconfig.outputFileType === 2)) return [3 /*break*/, 22]; 196 | console.log("[egf-protobuf]生成.min.js文件"); 197 | minjs = UglifyJS.minify(outPbj); 198 | return [4 /*yield*/, fs.writeFileAsync(pbjsFile.replace('.js', '.min.js'), minjs.code, 'utf-8')]; 199 | case 21: 200 | _a.sent(); 201 | console.log("[egf-protobuf]生成.min.js文件->完成"); 202 | _a.label = 22; 203 | case 22: 204 | console.log("[egf-protobuf]解析js文件生成.d.ts中"); 205 | return [4 /*yield*/, new Promise(function (res) { 206 | pbts.main(['--main', pbjsFile], function (err, output) { 207 | if (err) { 208 | console.error(err); 209 | } 210 | res(output); 211 | return {}; 212 | }); 213 | }) 214 | // pbtsResult = await fs.readFileAsync(tempfile, 'utf-8').catch(function (res) { console.log(res) }) as any; 215 | ]; 216 | case 23: 217 | pbtsResult = _a.sent(); 218 | // pbtsResult = await fs.readFileAsync(tempfile, 'utf-8').catch(function (res) { console.log(res) }) as any; 219 | pbtsResult = pbtsResult.replace(/\$protobuf/gi, "protobuf").replace(/export namespace/gi, 'declare namespace'); 220 | pbtsResult = 'type Long = protobuf.Long;\n' + pbtsResult; 221 | dtsOut = path.join(rootDir, pbconfig.dtsOutDir, pbconfig.outFileName + ".d.ts"); 222 | console.log("[egf-protobuf]解析js文件->完成"); 223 | return [4 /*yield*/, fs.existsAsync(dtsOut)]; 224 | case 24: 225 | isExit_dts = _a.sent(); 226 | if (!isExit_dts) return [3 /*break*/, 26]; 227 | console.log("[egf-protobuf]\u5220\u9664\u65E7.d.ts\u6587\u4EF6:" + dtsOut); 228 | return [4 /*yield*/, new Promise(function (res, rej) { 229 | rimraf(dtsOut, function () { 230 | res(); 231 | }); 232 | })]; 233 | case 25: 234 | _a.sent(); 235 | _a.label = 26; 236 | case 26: 237 | dtsOutDirPath = path.dirname(dtsOut); 238 | if (!(rootDir !== dtsOutDirPath)) return [3 /*break*/, 29]; 239 | return [4 /*yield*/, fs.existsAsync(dtsOutDirPath)]; 240 | case 27: 241 | isExit_dtsOutDir = _a.sent(); 242 | if (!!isExit_dtsOutDir) return [3 /*break*/, 29]; 243 | // 244 | console.log("[egf-protobuf]\u521B\u5EFA.d.ts \u7684\u6587\u4EF6\u5939:" + dtsOutDirPath + "->"); 245 | return [4 /*yield*/, fs.mkdirAsync(dtsOutDirPath)]; 246 | case 28: 247 | _a.sent(); 248 | _a.label = 29; 249 | case 29: 250 | console.log("[egf-protobuf]生成.d.ts文件->"); 251 | return [4 /*yield*/, fs.writeFileAsync(dtsOut, pbtsResult, 'utf-8').catch(function (res) { console.log(res); })]; 252 | case 30: 253 | _a.sent(); 254 | ; 255 | console.log("[egf-protobuf]生成.d.ts文件->完成"); 256 | return [4 /*yield*/, fs.removeAsync(tempfile).catch(function (res) { console.log(res); })]; 257 | case 31: 258 | _a.sent(); 259 | ; 260 | return [2 /*return*/]; 261 | } 262 | }); 263 | }); 264 | } 265 | exports.generate = generate; 266 | function initProj(projRoot, projType) { 267 | if (projRoot === void 0) { projRoot = "."; } 268 | return __awaiter(this, void 0, void 0, function () { 269 | var egretPropertiesPath, egretProperties, tsconfig; 270 | return __generator(this, function (_a) { 271 | switch (_a.label) { 272 | case 0: 273 | console.log('正在将 protobuf 源码拷贝至项目中...'); 274 | return [4 /*yield*/, fs.copyAsync(path.join(root, 'pblib'), path.join(projRoot, 'protobuf/library')).catch(function (res) { console.log(res); })]; 275 | case 1: 276 | _a.sent(); 277 | ; 278 | return [4 /*yield*/, fs.mkdirpSync(path.join(projRoot, 'protobuf/protofile'))]; 279 | case 2: 280 | _a.sent(); 281 | return [4 /*yield*/, fs.mkdirpSync(path.join(projRoot, 'protobuf/bundles'))]; 282 | case 3: 283 | _a.sent(); 284 | return [4 /*yield*/, fs.writeFileAsync((path.join(projRoot, 'protobuf/pbconfig.json')), pbconfigContent, 'utf-8').catch(function (res) { console.log(res); })]; 285 | case 4: 286 | _a.sent(); 287 | ; 288 | if (!(projType === "egret")) return [3 /*break*/, 11]; 289 | egretPropertiesPath = path.join(projRoot, 'egretProperties.json'); 290 | return [4 /*yield*/, fs.existsAsync(egretPropertiesPath)]; 291 | case 5: 292 | if (!_a.sent()) return [3 /*break*/, 10]; 293 | console.log('正在将 protobuf 添加到 egretProperties.json 中...'); 294 | return [4 /*yield*/, fs.readJSONAsync(egretPropertiesPath)]; 295 | case 6: 296 | egretProperties = _a.sent(); 297 | egretProperties.modules.push({ name: 'protobuf-library', path: 'protobuf/library' }); 298 | egretProperties.modules.push({ name: 'protobuf-bundles', path: 'protobuf/bundles' }); 299 | return [4 /*yield*/, fs.writeFileAsync(path.join(projRoot, 'egretProperties.json'), JSON.stringify(egretProperties, null, '\t\t'))]; 300 | case 7: 301 | _a.sent(); 302 | console.log('正在将 protobuf 添加到 tsconfig.json 中...'); 303 | return [4 /*yield*/, fs.readJSONAsync(path.join(projRoot, 'tsconfig.json'))]; 304 | case 8: 305 | tsconfig = _a.sent(); 306 | tsconfig.include.push('protobuf/**/*.d.ts'); 307 | return [4 /*yield*/, fs.writeFileAsync(path.join(projRoot, 'tsconfig.json'), JSON.stringify(tsconfig, null, '\t\t')).catch(function (res) { console.log(res); })]; 308 | case 9: 309 | _a.sent(); 310 | ; 311 | return [3 /*break*/, 11]; 312 | case 10: 313 | console.log('输入的文件夹不是白鹭引擎项目'); 314 | _a.label = 11; 315 | case 11: return [2 /*return*/]; 316 | } 317 | }); 318 | }); 319 | } 320 | exports.initProj = initProj; 321 | //# sourceMappingURL=index.js.map -------------------------------------------------------------------------------- /out/index.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"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"} -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "egf-protobuf", 3 | "version": "1.0.2", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "@protobufjs/aspromise": { 8 | "version": "1.1.2", 9 | "resolved": "https://registry.npm.taobao.org/@protobufjs/aspromise/download/@protobufjs/aspromise-1.1.2.tgz", 10 | "integrity": "sha1-m4sMxmPWaafY9vXQiToU00jzD78=" 11 | }, 12 | "@protobufjs/base64": { 13 | "version": "1.1.2", 14 | "resolved": "https://registry.npm.taobao.org/@protobufjs/base64/download/@protobufjs/base64-1.1.2.tgz", 15 | "integrity": "sha1-TIVzDlm5ofHzSQR9vyQpYDS7JzU=" 16 | }, 17 | "@protobufjs/codegen": { 18 | "version": "2.0.4", 19 | "resolved": "https://registry.npm.taobao.org/@protobufjs/codegen/download/@protobufjs/codegen-2.0.4.tgz", 20 | "integrity": "sha1-fvN/DQEPsCitGtWXIuUG2SYoFcs=" 21 | }, 22 | "@protobufjs/eventemitter": { 23 | "version": "1.1.0", 24 | "resolved": "https://registry.npm.taobao.org/@protobufjs/eventemitter/download/@protobufjs/eventemitter-1.1.0.tgz", 25 | "integrity": "sha1-NVy8mLr61ZePntCV85diHx0Ga3A=" 26 | }, 27 | "@protobufjs/fetch": { 28 | "version": "1.1.0", 29 | "resolved": "https://registry.npm.taobao.org/@protobufjs/fetch/download/@protobufjs/fetch-1.1.0.tgz", 30 | "integrity": "sha1-upn7WYYUr2VwDBYZ/wbUVLDYTEU=", 31 | "requires": { 32 | "@protobufjs/aspromise": "^1.1.1", 33 | "@protobufjs/inquire": "^1.1.0" 34 | } 35 | }, 36 | "@protobufjs/float": { 37 | "version": "1.0.2", 38 | "resolved": "https://registry.npm.taobao.org/@protobufjs/float/download/@protobufjs/float-1.0.2.tgz", 39 | "integrity": "sha1-Xp4avctz/Ap8uLKR33jIy9l7h9E=" 40 | }, 41 | "@protobufjs/inquire": { 42 | "version": "1.1.0", 43 | "resolved": "https://registry.npm.taobao.org/@protobufjs/inquire/download/@protobufjs/inquire-1.1.0.tgz", 44 | "integrity": "sha1-/yAOPnzyQp4tyvwRQIKOjMY48Ik=" 45 | }, 46 | "@protobufjs/path": { 47 | "version": "1.1.2", 48 | "resolved": "https://registry.npm.taobao.org/@protobufjs/path/download/@protobufjs/path-1.1.2.tgz", 49 | "integrity": "sha1-bMKyDFya1q0NzP0hynZz2Nf79o0=" 50 | }, 51 | "@protobufjs/pool": { 52 | "version": "1.1.0", 53 | "resolved": "https://registry.npm.taobao.org/@protobufjs/pool/download/@protobufjs/pool-1.1.0.tgz", 54 | "integrity": "sha1-Cf0V8tbTq/qbZbw2ZQbWrXhG/1Q=" 55 | }, 56 | "@protobufjs/utf8": { 57 | "version": "1.1.0", 58 | "resolved": "https://registry.npm.taobao.org/@protobufjs/utf8/download/@protobufjs/utf8-1.1.0.tgz", 59 | "integrity": "sha1-p3c2C1s5oaLlEG+OhY8v0tBgxXA=" 60 | }, 61 | "@types/bluebird": { 62 | "version": "3.5.25", 63 | "resolved": "http://registry.npm.taobao.org/@types/bluebird/download/@types/bluebird-3.5.25.tgz", 64 | "integrity": "sha1-WRiLhxIICS43dn5LPYDDs+quQ70=", 65 | "dev": true 66 | }, 67 | "@types/events": { 68 | "version": "3.0.0", 69 | "resolved": "http://registry.npm.taobao.org/@types/events/download/@types/events-3.0.0.tgz", 70 | "integrity": "sha1-KGLz9Yqaf3w+eNefEw3U1xwlwqc=" 71 | }, 72 | "@types/fs-extra": { 73 | "version": "4.0.8", 74 | "resolved": "http://registry.npm.taobao.org/@types/fs-extra/download/@types/fs-extra-4.0.8.tgz", 75 | "integrity": "sha1-aVfdr5FzGVGZy5baPbRMdHAEY9I=", 76 | "dev": true, 77 | "requires": { 78 | "@types/node": "*" 79 | } 80 | }, 81 | "@types/fs-extra-promise": { 82 | "version": "1.0.7", 83 | "resolved": "http://registry.npm.taobao.org/@types/fs-extra-promise/download/@types/fs-extra-promise-1.0.7.tgz", 84 | "integrity": "sha1-ACihHi8mpaI5GVS/GZv7sMmAah8=", 85 | "dev": true, 86 | "requires": { 87 | "@types/bluebird": "*", 88 | "@types/fs-extra": "^4", 89 | "@types/node": "*" 90 | } 91 | }, 92 | "@types/glob": { 93 | "version": "7.1.1", 94 | "resolved": "http://registry.npm.taobao.org/@types/glob/download/@types/glob-7.1.1.tgz", 95 | "integrity": "sha1-qlmhxuP7xCHgfM0xqUTDDrpSFXU=", 96 | "requires": { 97 | "@types/events": "*", 98 | "@types/minimatch": "*", 99 | "@types/node": "*" 100 | } 101 | }, 102 | "@types/long": { 103 | "version": "4.0.1", 104 | "resolved": "https://registry.npm.taobao.org/@types/long/download/@types/long-4.0.1.tgz?cache=0&sync_timestamp=1605055033937&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2F%40types%2Flong%2Fdownload%2F%40types%2Flong-4.0.1.tgz", 105 | "integrity": "sha1-RZxl+hhn2v5qjzIsTFFpVmPMVek=" 106 | }, 107 | "@types/minimatch": { 108 | "version": "3.0.3", 109 | "resolved": "http://registry.npm.taobao.org/@types/minimatch/download/@types/minimatch-3.0.3.tgz", 110 | "integrity": "sha1-PcoOPzOyAPx9ETnAzZbBJoyt/Z0=" 111 | }, 112 | "@types/node": { 113 | "version": "9.6.61", 114 | "resolved": "https://registry.npm.taobao.org/@types/node/download/@types/node-9.6.61.tgz", 115 | "integrity": "sha1-KfEk7d1BxMdCgb0LRV1okQn8Ki0=" 116 | }, 117 | "@types/rimraf": { 118 | "version": "2.0.2", 119 | "resolved": "http://registry.npm.taobao.org/@types/rimraf/download/@types/rimraf-2.0.2.tgz", 120 | "integrity": "sha1-fw/Dzw/wrSqZu3I64XZPMKyvi24=", 121 | "requires": { 122 | "@types/glob": "*", 123 | "@types/node": "*" 124 | } 125 | }, 126 | "@types/uglify-js": { 127 | "version": "2.6.32", 128 | "resolved": "http://registry.npm.taobao.org/@types/uglify-js/download/@types/uglify-js-2.6.32.tgz", 129 | "integrity": "sha1-G2CQaUb89u5M6vqBLSuG8TWNqQQ=", 130 | "dev": true, 131 | "requires": { 132 | "source-map": "^0.6.1" 133 | } 134 | }, 135 | "balanced-match": { 136 | "version": "1.0.0", 137 | "resolved": "http://registry.npm.taobao.org/balanced-match/download/balanced-match-1.0.0.tgz", 138 | "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" 139 | }, 140 | "bluebird": { 141 | "version": "3.5.2", 142 | "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.2.tgz", 143 | "integrity": "sha512-dhHTWMI7kMx5whMQntl7Vr9C6BvV10lFXDAasnqnrMYhXVCzzk6IO9Fo2L75jXHT07WrOngL1WDXOp+yYS91Yg==" 144 | }, 145 | "brace-expansion": { 146 | "version": "1.1.11", 147 | "resolved": "http://registry.npm.taobao.org/brace-expansion/download/brace-expansion-1.1.11.tgz", 148 | "integrity": "sha1-PH/L9SnYcibz0vUrlm/1Jx60Qd0=", 149 | "requires": { 150 | "balanced-match": "^1.0.0", 151 | "concat-map": "0.0.1" 152 | } 153 | }, 154 | "commander": { 155 | "version": "2.20.0", 156 | "resolved": "http://registry.npm.taobao.org/commander/download/commander-2.20.0.tgz", 157 | "integrity": "sha1-1YuytcHuj4ew00ACfp6U4iLFpCI=" 158 | }, 159 | "concat-map": { 160 | "version": "0.0.1", 161 | "resolved": "http://registry.npm.taobao.org/concat-map/download/concat-map-0.0.1.tgz", 162 | "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" 163 | }, 164 | "fs-extra": { 165 | "version": "5.0.0", 166 | "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-5.0.0.tgz", 167 | "integrity": "sha512-66Pm4RYbjzdyeuqudYqhFiNBbCIuI9kgRqLPSHIlXHidW8NIQtVdkM1yeZ4lXwuhbTETv3EUGMNHAAw6hiundQ==", 168 | "requires": { 169 | "graceful-fs": "^4.1.2", 170 | "jsonfile": "^4.0.0", 171 | "universalify": "^0.1.0" 172 | } 173 | }, 174 | "fs-extra-promise": { 175 | "version": "1.0.1", 176 | "resolved": "https://registry.npmjs.org/fs-extra-promise/-/fs-extra-promise-1.0.1.tgz", 177 | "integrity": "sha1-tu0azpexDga5X0WNBRt/BcZhPuY=", 178 | "requires": { 179 | "bluebird": "^3.5.0", 180 | "fs-extra": "^2.1.2" 181 | }, 182 | "dependencies": { 183 | "fs-extra": { 184 | "version": "2.1.2", 185 | "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-2.1.2.tgz", 186 | "integrity": "sha1-BGxwFjzvmq1GsOSn+kZ/si1x3jU=", 187 | "requires": { 188 | "graceful-fs": "^4.1.2", 189 | "jsonfile": "^2.1.0" 190 | } 191 | }, 192 | "jsonfile": { 193 | "version": "2.4.0", 194 | "resolved": "http://registry.npmjs.org/jsonfile/-/jsonfile-2.4.0.tgz", 195 | "integrity": "sha1-NzaitCi4e72gzIO1P6PWM6NcKug=", 196 | "requires": { 197 | "graceful-fs": "^4.1.6" 198 | } 199 | } 200 | } 201 | }, 202 | "fs.realpath": { 203 | "version": "1.0.0", 204 | "resolved": "http://registry.npm.taobao.org/fs.realpath/download/fs.realpath-1.0.0.tgz", 205 | "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" 206 | }, 207 | "glob": { 208 | "version": "7.1.3", 209 | "resolved": "http://registry.npm.taobao.org/glob/download/glob-7.1.3.tgz", 210 | "integrity": "sha1-OWCDLT8VdBCDQtr9OmezMsCWnfE=", 211 | "requires": { 212 | "fs.realpath": "^1.0.0", 213 | "inflight": "^1.0.4", 214 | "inherits": "2", 215 | "minimatch": "^3.0.4", 216 | "once": "^1.3.0", 217 | "path-is-absolute": "^1.0.0" 218 | } 219 | }, 220 | "graceful-fs": { 221 | "version": "4.1.11", 222 | "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.11.tgz", 223 | "integrity": "sha1-Dovf5NHduIVNZOBOp8AOKgJuVlg=" 224 | }, 225 | "inflight": { 226 | "version": "1.0.6", 227 | "resolved": "http://registry.npm.taobao.org/inflight/download/inflight-1.0.6.tgz", 228 | "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", 229 | "requires": { 230 | "once": "^1.3.0", 231 | "wrappy": "1" 232 | } 233 | }, 234 | "inherits": { 235 | "version": "2.0.3", 236 | "resolved": "http://registry.npm.taobao.org/inherits/download/inherits-2.0.3.tgz", 237 | "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" 238 | }, 239 | "jsonfile": { 240 | "version": "4.0.0", 241 | "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz", 242 | "integrity": "sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=", 243 | "requires": { 244 | "graceful-fs": "^4.1.6" 245 | } 246 | }, 247 | "long": { 248 | "version": "4.0.0", 249 | "resolved": "https://registry.npm.taobao.org/long/download/long-4.0.0.tgz", 250 | "integrity": "sha1-mntxz7fTYaGU6lVSQckvdGjVvyg=" 251 | }, 252 | "minimatch": { 253 | "version": "3.0.4", 254 | "resolved": "http://registry.npm.taobao.org/minimatch/download/minimatch-3.0.4.tgz", 255 | "integrity": "sha1-UWbihkV/AzBgZL5Ul+jbsMPTIIM=", 256 | "requires": { 257 | "brace-expansion": "^1.1.7" 258 | } 259 | }, 260 | "once": { 261 | "version": "1.4.0", 262 | "resolved": "http://registry.npm.taobao.org/once/download/once-1.4.0.tgz", 263 | "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", 264 | "requires": { 265 | "wrappy": "1" 266 | } 267 | }, 268 | "path-is-absolute": { 269 | "version": "1.0.1", 270 | "resolved": "http://registry.npm.taobao.org/path-is-absolute/download/path-is-absolute-1.0.1.tgz", 271 | "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=" 272 | }, 273 | "protobufjs": { 274 | "version": "6.8.9", 275 | "resolved": "https://registry.npm.taobao.org/protobufjs/download/protobufjs-6.8.9.tgz", 276 | "integrity": "sha1-CxrbzaqYPTacPZEIqXyBTtwDB1Q=", 277 | "requires": { 278 | "@protobufjs/aspromise": "^1.1.2", 279 | "@protobufjs/base64": "^1.1.2", 280 | "@protobufjs/codegen": "^2.0.4", 281 | "@protobufjs/eventemitter": "^1.1.0", 282 | "@protobufjs/fetch": "^1.1.0", 283 | "@protobufjs/float": "^1.0.2", 284 | "@protobufjs/inquire": "^1.1.0", 285 | "@protobufjs/path": "^1.1.2", 286 | "@protobufjs/pool": "^1.1.0", 287 | "@protobufjs/utf8": "^1.1.0", 288 | "@types/long": "^4.0.0", 289 | "@types/node": "^10.1.0", 290 | "long": "^4.0.0" 291 | }, 292 | "dependencies": { 293 | "@types/node": { 294 | "version": "10.17.50", 295 | "resolved": "https://registry.npm.taobao.org/@types/node/download/@types/node-10.17.50.tgz", 296 | "integrity": "sha1-eiCQKvWRKCqpF2uu/DfUNyExwy0=" 297 | } 298 | } 299 | }, 300 | "rimraf": { 301 | "version": "2.6.3", 302 | "resolved": "http://registry.npm.taobao.org/rimraf/download/rimraf-2.6.3.tgz", 303 | "integrity": "sha1-stEE/g2Psnz54KHNqCYt04M8bKs=", 304 | "requires": { 305 | "glob": "^7.1.3" 306 | } 307 | }, 308 | "source-map": { 309 | "version": "0.6.1", 310 | "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", 311 | "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==" 312 | }, 313 | "uglify-js": { 314 | "version": "3.4.9", 315 | "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.4.9.tgz", 316 | "integrity": "sha512-8CJsbKOtEbnJsTyv6LE6m6ZKniqMiFWmm9sRbopbkGs3gMPPfd3Fh8iIA4Ykv5MgaTbqHr4BaoGLJLZNhsrW1Q==", 317 | "requires": { 318 | "commander": "~2.17.1", 319 | "source-map": "~0.6.1" 320 | }, 321 | "dependencies": { 322 | "commander": { 323 | "version": "2.17.1", 324 | "resolved": "http://registry.npm.taobao.org/commander/download/commander-2.17.1.tgz", 325 | "integrity": "sha1-vXerfebelCBc6sxy8XFtKfIKd78=" 326 | } 327 | } 328 | }, 329 | "universalify": { 330 | "version": "0.1.2", 331 | "resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz", 332 | "integrity": "sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==" 333 | }, 334 | "wrappy": { 335 | "version": "1.0.2", 336 | "resolved": "http://registry.npm.taobao.org/wrappy/download/wrappy-1.0.2.tgz", 337 | "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" 338 | } 339 | } 340 | } 341 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "egf-protobuf", 3 | "version": "1.2.0", 4 | "description": "1. 提供 protobuf.js 基础运行时库\\r 2. 提供命令行脚本,将 protofile 生成 JavaScript 代码\\r 3. 生成正确的 .d.ts 代码,以方便 TypeScript 项目使用\\r", 5 | "main": "index.js", 6 | "bin": { 7 | "egf-pb": "out/cli.js" 8 | }, 9 | "scripts": { 10 | "test": "echo \"Error: no test specified\" && exit 1" 11 | }, 12 | "keywords": [ 13 | "typescript", 14 | "protobuf", 15 | "jscode" 16 | ], 17 | "repository": { 18 | "type": "git", 19 | "url": "https://github.com/AILHC/egf-protobuf.git" 20 | }, 21 | "author": "AILHC", 22 | "license": "MIT", 23 | "dependencies": { 24 | "@types/rimraf": "^2.0.2", 25 | "commander": "^2.20.0", 26 | "fs-extra": "^5.0.0", 27 | "fs-extra-promise": "^1.0.1", 28 | "protobufjs": "~6.8.4", 29 | "rimraf": "^2.6.3", 30 | "uglify-js": "^3.3.7" 31 | }, 32 | "devDependencies": { 33 | "@types/fs-extra-promise": "^1.0.4", 34 | "@types/node": "^9.6.61", 35 | "@types/uglify-js": "^2.6.30" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /pblib/protobuf-library.d.ts: -------------------------------------------------------------------------------- 1 | export as namespace protobuf; 2 | 3 | /** 4 | * Provides common type definitions. 5 | * Can also be used to provide additional google types or your own custom types. 6 | * @param name Short name as in `google/protobuf/[name].proto` or full file name 7 | * @param json JSON definition within `google.protobuf` if a short name, otherwise the file's root definition 8 | */ 9 | export function common(name: string, json: { [k: string]: any }): void; 10 | 11 | export namespace common { 12 | 13 | /** Properties of a google.protobuf.Any message. */ 14 | interface IAny { 15 | typeUrl?: string; 16 | bytes?: Uint8Array; 17 | } 18 | 19 | /** Properties of a google.protobuf.Duration message. */ 20 | interface IDuration { 21 | seconds?: (number | Long); 22 | nanos?: number; 23 | } 24 | 25 | /** Properties of a google.protobuf.Timestamp message. */ 26 | interface ITimestamp { 27 | seconds?: (number | Long); 28 | nanos?: number; 29 | } 30 | 31 | /** Properties of a google.protobuf.Empty message. */ 32 | interface IEmpty { 33 | } 34 | 35 | /** Properties of a google.protobuf.Struct message. */ 36 | interface IStruct { 37 | fields?: { [k: string]: IValue }; 38 | } 39 | 40 | /** Properties of a google.protobuf.Value message. */ 41 | interface IValue { 42 | kind?: string; 43 | nullValue?: 0; 44 | numberValue?: number; 45 | stringValue?: string; 46 | boolValue?: boolean; 47 | structValue?: IStruct; 48 | listValue?: IListValue; 49 | } 50 | 51 | /** Properties of a google.protobuf.ListValue message. */ 52 | interface IListValue { 53 | values?: IValue[]; 54 | } 55 | 56 | /** Properties of a google.protobuf.DoubleValue message. */ 57 | interface IDoubleValue { 58 | value?: number; 59 | } 60 | 61 | /** Properties of a google.protobuf.FloatValue message. */ 62 | interface IFloatValue { 63 | value?: number; 64 | } 65 | 66 | /** Properties of a google.protobuf.Int64Value message. */ 67 | interface IInt64Value { 68 | value?: (number | Long); 69 | } 70 | 71 | /** Properties of a google.protobuf.UInt64Value message. */ 72 | interface IUInt64Value { 73 | value?: (number | Long); 74 | } 75 | 76 | /** Properties of a google.protobuf.Int32Value message. */ 77 | interface IInt32Value { 78 | value?: number; 79 | } 80 | 81 | /** Properties of a google.protobuf.UInt32Value message. */ 82 | interface IUInt32Value { 83 | value?: number; 84 | } 85 | 86 | /** Properties of a google.protobuf.BoolValue message. */ 87 | interface IBoolValue { 88 | value?: boolean; 89 | } 90 | 91 | /** Properties of a google.protobuf.StringValue message. */ 92 | interface IStringValue { 93 | value?: string; 94 | } 95 | 96 | /** Properties of a google.protobuf.BytesValue message. */ 97 | interface IBytesValue { 98 | value?: Uint8Array; 99 | } 100 | 101 | /** 102 | * Gets the root definition of the specified common proto file. 103 | * 104 | * Bundled definitions are: 105 | * - google/protobuf/any.proto 106 | * - google/protobuf/duration.proto 107 | * - google/protobuf/empty.proto 108 | * - google/protobuf/struct.proto 109 | * - google/protobuf/timestamp.proto 110 | * - google/protobuf/wrappers.proto 111 | * 112 | * @param file Proto file name 113 | * @returns Root definition or `null` if not defined 114 | */ 115 | function get(file: string): (INamespace | null); 116 | } 117 | 118 | /** Runtime message from/to plain object converters. */ 119 | export namespace converter { 120 | 121 | /** 122 | * Generates a plain object to runtime message converter specific to the specified message type. 123 | * @param mtype Message type 124 | * @returns Codegen instance 125 | */ 126 | function fromObject(mtype: Type): Codegen; 127 | 128 | /** 129 | * Generates a runtime message to plain object converter specific to the specified message type. 130 | * @param mtype Message type 131 | * @returns Codegen instance 132 | */ 133 | function toObject(mtype: Type): Codegen; 134 | } 135 | 136 | /** 137 | * Generates a decoder specific to the specified message type. 138 | * @param mtype Message type 139 | * @returns Codegen instance 140 | */ 141 | export function decoder(mtype: Type): Codegen; 142 | 143 | /** 144 | * Generates an encoder specific to the specified message type. 145 | * @param mtype Message type 146 | * @returns Codegen instance 147 | */ 148 | export function encoder(mtype: Type): Codegen; 149 | 150 | /** Reflected enum. */ 151 | export class Enum extends ReflectionObject { 152 | 153 | /** 154 | * Constructs a new enum instance. 155 | * @param name Unique name within its namespace 156 | * @param [values] Enum values as an object, by name 157 | * @param [options] Declared options 158 | */ 159 | constructor(name: string, values?: { [k: string]: number }, options?: { [k: string]: any }); 160 | 161 | /** Enum values by id. */ 162 | public valuesById: { [k: number]: string }; 163 | 164 | /** Enum values by name. */ 165 | public values: { [k: string]: number }; 166 | 167 | /** Value comment texts, if any. */ 168 | public comments: { [k: string]: string }; 169 | 170 | /** Reserved ranges, if any. */ 171 | public reserved: (number[] | string) []; 172 | 173 | /** 174 | * Constructs an enum from an enum descriptor. 175 | * @param name Enum name 176 | * @param json Enum descriptor 177 | * @returns Created enum 178 | * @throws {TypeError} If arguments are invalid 179 | */ 180 | public static fromJSON(name: string, json: IEnum): Enum; 181 | 182 | /** 183 | * Converts this enum to an enum descriptor. 184 | * @returns Enum descriptor 185 | */ 186 | public toJSON(): IEnum; 187 | 188 | /** 189 | * Adds a value to this enum. 190 | * @param name Value name 191 | * @param id Value id 192 | * @param [comment] Comment, if any 193 | * @returns `this` 194 | * @throws {TypeError} If arguments are invalid 195 | * @throws {Error} If there is already a value with this name or id 196 | */ 197 | public add(name: string, id: number, comment?: string): Enum; 198 | 199 | /** 200 | * Removes a value from this enum 201 | * @param name Value name 202 | * @returns `this` 203 | * @throws {TypeError} If arguments are invalid 204 | * @throws {Error} If `name` is not a name of this enum 205 | */ 206 | public remove(name: string): Enum; 207 | 208 | /** 209 | * Tests if the specified id is reserved. 210 | * @param id Id to test 211 | * @returns `true` if reserved, otherwise `false` 212 | */ 213 | public isReservedId(id: number): boolean; 214 | 215 | /** 216 | * Tests if the specified name is reserved. 217 | * @param name Name to test 218 | * @returns `true` if reserved, otherwise `false` 219 | */ 220 | public isReservedName(name: string): boolean; 221 | } 222 | 223 | /** Enum descriptor. */ 224 | export interface IEnum { 225 | 226 | /** Enum values */ 227 | values: { [k: string]: number }; 228 | 229 | /** Enum options */ 230 | options?: { [k: string]: any }; 231 | } 232 | 233 | /** Reflected message field. */ 234 | export class Field extends FieldBase { 235 | 236 | /** 237 | * Constructs a new message field instance. Note that {@link MapField|map fields} have their own class. 238 | * @param name Unique name within its namespace 239 | * @param id Unique id within its namespace 240 | * @param type Value type 241 | * @param [rule="optional"] Field rule 242 | * @param [extend] Extended type if different from parent 243 | * @param [options] Declared options 244 | */ 245 | constructor(name: string, id: number, type: string, rule?: (string | { [k: string]: any }), extend?: (string | { [k: string]: any }), options?: { [k: string]: any }); 246 | 247 | /** 248 | * Constructs a field from a field descriptor. 249 | * @param name Field name 250 | * @param json Field descriptor 251 | * @returns Created field 252 | * @throws {TypeError} If arguments are invalid 253 | */ 254 | public static fromJSON(name: string, json: IField): Field; 255 | 256 | /** Determines whether this field is packed. Only relevant when repeated and working with proto2. */ 257 | public readonly packed: boolean; 258 | 259 | /** 260 | * Field decorator (TypeScript). 261 | * @param fieldId Field id 262 | * @param fieldType Field type 263 | * @param [fieldRule="optional"] Field rule 264 | * @param [defaultValue] Default value 265 | * @returns Decorator function 266 | */ 267 | public static d(fieldId: number, fieldType: ("double" | "float" | "int32" | "uint32" | "sint32" | "fixed32" | "sfixed32" | "int64" | "uint64" | "sint64" | "fixed64" | "sfixed64" | "string" | "bool" | "bytes" | object), fieldRule?: ("optional" | "required" | "repeated"), defaultValue?: T): FieldDecorator; 268 | 269 | /** 270 | * Field decorator (TypeScript). 271 | * @param fieldId Field id 272 | * @param fieldType Field type 273 | * @param [fieldRule="optional"] Field rule 274 | * @returns Decorator function 275 | */ 276 | public static d>(fieldId: number, fieldType: (Constructor | string), fieldRule?: ("optional" | "required" | "repeated")): FieldDecorator; 277 | } 278 | 279 | /** Base class of all reflected message fields. This is not an actual class but here for the sake of having consistent type definitions. */ 280 | export class FieldBase extends ReflectionObject { 281 | 282 | /** 283 | * Not an actual constructor. Use {@link Field} instead. 284 | * @param name Unique name within its namespace 285 | * @param id Unique id within its namespace 286 | * @param type Value type 287 | * @param [rule="optional"] Field rule 288 | * @param [extend] Extended type if different from parent 289 | * @param [options] Declared options 290 | */ 291 | constructor(name: string, id: number, type: string, rule?: (string | { [k: string]: any }), extend?: (string | { [k: string]: any }), options?: { [k: string]: any }); 292 | 293 | /** Field rule, if any. */ 294 | public rule?: string; 295 | 296 | /** Field type. */ 297 | public type: string; 298 | 299 | /** Unique field id. */ 300 | public id: number; 301 | 302 | /** Extended type if different from parent. */ 303 | public extend?: string; 304 | 305 | /** Whether this field is required. */ 306 | public required: boolean; 307 | 308 | /** Whether this field is optional. */ 309 | public optional: boolean; 310 | 311 | /** Whether this field is repeated. */ 312 | public repeated: boolean; 313 | 314 | /** Whether this field is a map or not. */ 315 | public map: boolean; 316 | 317 | /** Message this field belongs to. */ 318 | public message: (Type | null); 319 | 320 | /** OneOf this field belongs to, if any, */ 321 | public partOf: (OneOf | null); 322 | 323 | /** The field type's default value. */ 324 | public typeDefault: any; 325 | 326 | /** The field's default value on prototypes. */ 327 | public defaultValue: any; 328 | 329 | /** Whether this field's value should be treated as a long. */ 330 | public long: boolean; 331 | 332 | /** Whether this field's value is a buffer. */ 333 | public bytes: boolean; 334 | 335 | /** Resolved type if not a basic type. */ 336 | public resolvedType: (Type | Enum | null); 337 | 338 | /** Sister-field within the extended type if a declaring extension field. */ 339 | public extensionField: (Field | null); 340 | 341 | /** Sister-field within the declaring namespace if an extended field. */ 342 | public declaringField: (Field | null); 343 | 344 | /** 345 | * Converts this field to a field descriptor. 346 | * @returns Field descriptor 347 | */ 348 | public toJSON(): IField; 349 | 350 | /** 351 | * Resolves this field's type references. 352 | * @returns `this` 353 | * @throws {Error} If any reference cannot be resolved 354 | */ 355 | public resolve(): Field; 356 | } 357 | 358 | /** Field descriptor. */ 359 | export interface IField { 360 | 361 | /** Field rule */ 362 | rule?: string; 363 | 364 | /** Field type */ 365 | type: string; 366 | 367 | /** Field id */ 368 | id: number; 369 | 370 | /** Field options */ 371 | options?: { [k: string]: any }; 372 | } 373 | 374 | /** Extension field descriptor. */ 375 | export interface IExtensionField extends IField { 376 | 377 | /** Extended type */ 378 | extend: string; 379 | } 380 | 381 | /** 382 | * Decorator function as returned by {@link Field.d} and {@link MapField.d} (TypeScript). 383 | * @param prototype Target prototype 384 | * @param fieldName Field name 385 | */ 386 | type FieldDecorator = (prototype: object, fieldName: string) => void; 387 | 388 | /** 389 | * A node-style callback as used by {@link load} and {@link Root#load}. 390 | * @param error Error, if any, otherwise `null` 391 | * @param [root] Root, if there hasn't been an error 392 | */ 393 | type LoadCallback = (error: (Error | null), root?: Root) => void; 394 | 395 | /** 396 | * Loads one or multiple .proto or preprocessed .json files into a common root namespace and calls the callback. 397 | * @param filename One or multiple files to load 398 | * @param root Root namespace, defaults to create a new one if omitted. 399 | * @param callback Callback function 400 | * @see {@link Root#load} 401 | */ 402 | export function load(filename: (string | string[]), root: Root, callback: LoadCallback): void; 403 | 404 | /** 405 | * Loads one or multiple .proto or preprocessed .json files into a common root namespace and calls the callback. 406 | * @param filename One or multiple files to load 407 | * @param callback Callback function 408 | * @see {@link Root#load} 409 | */ 410 | export function load(filename: (string | string[]), callback: LoadCallback): void; 411 | 412 | /** 413 | * Loads one or multiple .proto or preprocessed .json files into a common root namespace and returns a promise. 414 | * @param filename One or multiple files to load 415 | * @param [root] Root namespace, defaults to create a new one if omitted. 416 | * @returns Promise 417 | * @see {@link Root#load} 418 | */ 419 | export function load(filename: (string | string[]), root?: Root): Promise; 420 | 421 | /** 422 | * Synchronously loads one or multiple .proto or preprocessed .json files into a common root namespace (node only). 423 | * @param filename One or multiple files to load 424 | * @param [root] Root namespace, defaults to create a new one if omitted. 425 | * @returns Root namespace 426 | * @throws {Error} If synchronous fetching is not supported (i.e. in browsers) or if a file's syntax is invalid 427 | * @see {@link Root#loadSync} 428 | */ 429 | export function loadSync(filename: (string | string[]), root?: Root): Root; 430 | 431 | /** Build type, one of `"full"`, `"light"` or `"minimal"`. */ 432 | export const build: string; 433 | 434 | /** Reconfigures the library according to the environment. */ 435 | export function configure(): void; 436 | 437 | /** Reflected map field. */ 438 | export class MapField extends FieldBase { 439 | 440 | /** 441 | * Constructs a new map field instance. 442 | * @param name Unique name within its namespace 443 | * @param id Unique id within its namespace 444 | * @param keyType Key type 445 | * @param type Value type 446 | * @param [options] Declared options 447 | */ 448 | constructor(name: string, id: number, keyType: string, type: string, options?: { [k: string]: any }); 449 | 450 | /** Key type. */ 451 | public keyType: string; 452 | 453 | /** Resolved key type if not a basic type. */ 454 | public resolvedKeyType: (ReflectionObject | null); 455 | 456 | /** 457 | * Constructs a map field from a map field descriptor. 458 | * @param name Field name 459 | * @param json Map field descriptor 460 | * @returns Created map field 461 | * @throws {TypeError} If arguments are invalid 462 | */ 463 | public static fromJSON(name: string, json: IMapField): MapField; 464 | 465 | /** 466 | * Converts this map field to a map field descriptor. 467 | * @returns Map field descriptor 468 | */ 469 | public toJSON(): IMapField; 470 | 471 | /** 472 | * Map field decorator (TypeScript). 473 | * @param fieldId Field id 474 | * @param fieldKeyType Field key type 475 | * @param fieldValueType Field value type 476 | * @returns Decorator function 477 | */ 478 | public static d }>(fieldId: number, fieldKeyType: ("int32" | "uint32" | "sint32" | "fixed32" | "sfixed32" | "int64" | "uint64" | "sint64" | "fixed64" | "sfixed64" | "bool" | "string"), fieldValueType: ("double" | "float" | "int32" | "uint32" | "sint32" | "fixed32" | "sfixed32" | "int64" | "uint64" | "sint64" | "fixed64" | "sfixed64" | "bool" | "string" | "bytes" | object | Constructor<{}>)): FieldDecorator; 479 | } 480 | 481 | /** Map field descriptor. */ 482 | export interface IMapField extends IField { 483 | 484 | /** Key type */ 485 | keyType: string; 486 | } 487 | 488 | /** Extension map field descriptor. */ 489 | export interface IExtensionMapField extends IMapField { 490 | 491 | /** Extended type */ 492 | extend: string; 493 | } 494 | 495 | /** Abstract runtime message. */ 496 | export class Message { 497 | 498 | /** 499 | * Constructs a new message instance. 500 | * @param [properties] Properties to set 501 | */ 502 | constructor(properties?: Properties); 503 | 504 | /** Reference to the reflected type. */ 505 | public static readonly $type: Type; 506 | 507 | /** Reference to the reflected type. */ 508 | public readonly $type: Type; 509 | 510 | /** 511 | * Creates a new message of this type using the specified properties. 512 | * @param [properties] Properties to set 513 | * @returns Message instance 514 | */ 515 | public static create>(this: Constructor, properties?: { [k: string]: any }): Message; 516 | 517 | /** 518 | * Encodes a message of this type. 519 | * @param message Message to encode 520 | * @param [writer] Writer to use 521 | * @returns Writer 522 | */ 523 | public static encode>(this: Constructor, message: (T | { [k: string]: any }), writer?: Writer): Writer; 524 | 525 | /** 526 | * Encodes a message of this type preceeded by its length as a varint. 527 | * @param message Message to encode 528 | * @param [writer] Writer to use 529 | * @returns Writer 530 | */ 531 | public static encodeDelimited>(this: Constructor, message: (T | { [k: string]: any }), writer?: Writer): Writer; 532 | 533 | /** 534 | * Decodes a message of this type. 535 | * @param reader Reader or buffer to decode 536 | * @returns Decoded message 537 | */ 538 | public static decode>(this: Constructor, reader: (Reader | Uint8Array)): T; 539 | 540 | /** 541 | * Decodes a message of this type preceeded by its length as a varint. 542 | * @param reader Reader or buffer to decode 543 | * @returns Decoded message 544 | */ 545 | public static decodeDelimited>(this: Constructor, reader: (Reader | Uint8Array)): T; 546 | 547 | /** 548 | * Verifies a message of this type. 549 | * @param message Plain object to verify 550 | * @returns `null` if valid, otherwise the reason why it is not 551 | */ 552 | public static verify(message: { [k: string]: any }): (string | null); 553 | 554 | /** 555 | * Creates a new message of this type from a plain object. Also converts values to their respective internal types. 556 | * @param object Plain object 557 | * @returns Message instance 558 | */ 559 | public static fromObject>(this: Constructor, object: { [k: string]: any }): T; 560 | 561 | /** 562 | * Creates a plain object from a message of this type. Also converts values to other types if specified. 563 | * @param message Message instance 564 | * @param [options] Conversion options 565 | * @returns Plain object 566 | */ 567 | public static toObject>(this: Constructor, message: T, options?: IConversionOptions): { [k: string]: any }; 568 | 569 | /** 570 | * Converts this message to JSON. 571 | * @returns JSON object 572 | */ 573 | public toJSON(): { [k: string]: any }; 574 | } 575 | 576 | /** Reflected service method. */ 577 | export class Method extends ReflectionObject { 578 | 579 | /** 580 | * Constructs a new service method instance. 581 | * @param name Method name 582 | * @param type Method type, usually `"rpc"` 583 | * @param requestType Request message type 584 | * @param responseType Response message type 585 | * @param [requestStream] Whether the request is streamed 586 | * @param [responseStream] Whether the response is streamed 587 | * @param [options] Declared options 588 | */ 589 | constructor(name: string, type: (string | undefined), requestType: string, responseType: string, requestStream?: (boolean | { [k: string]: any }), responseStream?: (boolean | { [k: string]: any }), options?: { [k: string]: any }); 590 | 591 | /** Method type. */ 592 | public type: string; 593 | 594 | /** Request type. */ 595 | public requestType: string; 596 | 597 | /** Whether requests are streamed or not. */ 598 | public requestStream?: boolean; 599 | 600 | /** Response type. */ 601 | public responseType: string; 602 | 603 | /** Whether responses are streamed or not. */ 604 | public responseStream?: boolean; 605 | 606 | /** Resolved request type. */ 607 | public resolvedRequestType: (Type | null); 608 | 609 | /** Resolved response type. */ 610 | public resolvedResponseType: (Type | null); 611 | 612 | /** 613 | * Constructs a method from a method descriptor. 614 | * @param name Method name 615 | * @param json Method descriptor 616 | * @returns Created method 617 | * @throws {TypeError} If arguments are invalid 618 | */ 619 | public static fromJSON(name: string, json: IMethod): Method; 620 | 621 | /** 622 | * Converts this method to a method descriptor. 623 | * @returns Method descriptor 624 | */ 625 | public toJSON(): IMethod; 626 | } 627 | 628 | /** Method descriptor. */ 629 | export interface IMethod { 630 | 631 | /** Method type */ 632 | type?: string; 633 | 634 | /** Request type */ 635 | requestType: string; 636 | 637 | /** Response type */ 638 | responseType: string; 639 | 640 | /** Whether requests are streamed */ 641 | requestStream?: boolean; 642 | 643 | /** Whether responses are streamed */ 644 | responseStream?: boolean; 645 | 646 | /** Method options */ 647 | options?: { [k: string]: any }; 648 | } 649 | 650 | /** Reflected namespace. */ 651 | export class Namespace extends NamespaceBase { 652 | 653 | /** 654 | * Constructs a new namespace instance. 655 | * @param name Namespace name 656 | * @param [options] Declared options 657 | */ 658 | constructor(name: string, options?: { [k: string]: any }); 659 | 660 | /** 661 | * Constructs a namespace from JSON. 662 | * @param name Namespace name 663 | * @param json JSON object 664 | * @returns Created namespace 665 | * @throws {TypeError} If arguments are invalid 666 | */ 667 | public static fromJSON(name: string, json: { [k: string]: any }): Namespace; 668 | 669 | /** 670 | * Converts an array of reflection objects to JSON. 671 | * @param array Object array 672 | * @returns JSON object or `undefined` when array is empty 673 | */ 674 | public static arrayToJSON(array: ReflectionObject[]): ({ [k: string]: any } | undefined); 675 | 676 | /** 677 | * Tests if the specified id is reserved. 678 | * @param reserved Array of reserved ranges and names 679 | * @param id Id to test 680 | * @returns `true` if reserved, otherwise `false` 681 | */ 682 | public static isReservedId(reserved: ((number[] | string) [] | undefined), id: number): boolean; 683 | 684 | /** 685 | * Tests if the specified name is reserved. 686 | * @param reserved Array of reserved ranges and names 687 | * @param name Name to test 688 | * @returns `true` if reserved, otherwise `false` 689 | */ 690 | public static isReservedName(reserved: ((number[] | string) [] | undefined), name: string): boolean; 691 | } 692 | 693 | /** Base class of all reflection objects containing nested objects. This is not an actual class but here for the sake of having consistent type definitions. */ 694 | export abstract class NamespaceBase extends ReflectionObject { 695 | 696 | /** Nested objects by name. */ 697 | public nested?: { [k: string]: ReflectionObject }; 698 | 699 | /** Nested objects of this namespace as an array for iteration. */ 700 | public readonly nestedArray: ReflectionObject[]; 701 | 702 | /** 703 | * Converts this namespace to a namespace descriptor. 704 | * @returns Namespace descriptor 705 | */ 706 | public toJSON(): INamespace; 707 | 708 | /** 709 | * Adds nested objects to this namespace from nested object descriptors. 710 | * @param nestedJson Any nested object descriptors 711 | * @returns `this` 712 | */ 713 | public addJSON(nestedJson: { [k: string]: AnyNestedObject }): Namespace; 714 | 715 | /** 716 | * Gets the nested object of the specified name. 717 | * @param name Nested object name 718 | * @returns The reflection object or `null` if it doesn't exist 719 | */ 720 | public get(name: string): (ReflectionObject | null); 721 | 722 | /** 723 | * Gets the values of the nested {@link Enum|enum} of the specified name. 724 | * This methods differs from {@link Namespace#get|get} in that it returns an enum's values directly and throws instead of returning `null`. 725 | * @param name Nested enum name 726 | * @returns Enum values 727 | * @throws {Error} If there is no such enum 728 | */ 729 | public getEnum(name: string): { [k: string]: number }; 730 | 731 | /** 732 | * Adds a nested object to this namespace. 733 | * @param object Nested object to add 734 | * @returns `this` 735 | * @throws {TypeError} If arguments are invalid 736 | * @throws {Error} If there is already a nested object with this name 737 | */ 738 | public add(object: ReflectionObject): Namespace; 739 | 740 | /** 741 | * Removes a nested object from this namespace. 742 | * @param object Nested object to remove 743 | * @returns `this` 744 | * @throws {TypeError} If arguments are invalid 745 | * @throws {Error} If `object` is not a member of this namespace 746 | */ 747 | public remove(object: ReflectionObject): Namespace; 748 | 749 | /** 750 | * Defines additial namespaces within this one if not yet existing. 751 | * @param path Path to create 752 | * @param [json] Nested types to create from JSON 753 | * @returns Pointer to the last namespace created or `this` if path is empty 754 | */ 755 | public define(path: (string | string[]), json?: any): Namespace; 756 | 757 | /** 758 | * Resolves this namespace's and all its nested objects' type references. Useful to validate a reflection tree, but comes at a cost. 759 | * @returns `this` 760 | */ 761 | public resolveAll(): Namespace; 762 | 763 | /** 764 | * Recursively looks up the reflection object matching the specified path in the scope of this namespace. 765 | * @param path Path to look up 766 | * @param filterTypes Filter types, any combination of the constructors of `protobuf.Type`, `protobuf.Enum`, `protobuf.Service` etc. 767 | * @param [parentAlreadyChecked=false] If known, whether the parent has already been checked 768 | * @returns Looked up object or `null` if none could be found 769 | */ 770 | public lookup(path: (string | string[]), filterTypes: (any | any[]), parentAlreadyChecked?: boolean): (ReflectionObject | null); 771 | 772 | /** 773 | * Looks up the reflection object at the specified path, relative to this namespace. 774 | * @param path Path to look up 775 | * @param [parentAlreadyChecked=false] Whether the parent has already been checked 776 | * @returns Looked up object or `null` if none could be found 777 | */ 778 | public lookup(path: (string | string[]), parentAlreadyChecked?: boolean): (ReflectionObject | null); 779 | 780 | /** 781 | * Looks up the {@link Type|type} at the specified path, relative to this namespace. 782 | * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`. 783 | * @param path Path to look up 784 | * @returns Looked up type 785 | * @throws {Error} If `path` does not point to a type 786 | */ 787 | public lookupType(path: (string | string[])): Type; 788 | 789 | /** 790 | * Looks up the values of the {@link Enum|enum} at the specified path, relative to this namespace. 791 | * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`. 792 | * @param path Path to look up 793 | * @returns Looked up enum 794 | * @throws {Error} If `path` does not point to an enum 795 | */ 796 | public lookupEnum(path: (string | string[])): Enum; 797 | 798 | /** 799 | * Looks up the {@link Type|type} or {@link Enum|enum} at the specified path, relative to this namespace. 800 | * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`. 801 | * @param path Path to look up 802 | * @returns Looked up type or enum 803 | * @throws {Error} If `path` does not point to a type or enum 804 | */ 805 | public lookupTypeOrEnum(path: (string | string[])): Type; 806 | 807 | /** 808 | * Looks up the {@link Service|service} at the specified path, relative to this namespace. 809 | * Besides its signature, this methods differs from {@link Namespace#lookup|lookup} in that it throws instead of returning `null`. 810 | * @param path Path to look up 811 | * @returns Looked up service 812 | * @throws {Error} If `path` does not point to a service 813 | */ 814 | public lookupService(path: (string | string[])): Service; 815 | } 816 | 817 | /** Namespace descriptor. */ 818 | export interface INamespace { 819 | 820 | /** Namespace options */ 821 | options?: { [k: string]: any }; 822 | 823 | /** Nested object descriptors */ 824 | nested?: { [k: string]: AnyNestedObject }; 825 | } 826 | 827 | /** Any extension field descriptor. */ 828 | type AnyExtensionField = (IExtensionField | IExtensionMapField); 829 | 830 | /** Any nested object descriptor. */ 831 | type AnyNestedObject = (IEnum | IType | IService | AnyExtensionField | INamespace); 832 | 833 | /** Base class of all reflection objects. */ 834 | export abstract class ReflectionObject { 835 | 836 | /** Options. */ 837 | public options?: { [k: string]: any }; 838 | 839 | /** Unique name within its namespace. */ 840 | public name: string; 841 | 842 | /** Parent namespace. */ 843 | public parent: (Namespace | null); 844 | 845 | /** Whether already resolved or not. */ 846 | public resolved: boolean; 847 | 848 | /** Comment text, if any. */ 849 | public comment: (string | null); 850 | 851 | /** Defining file name. */ 852 | public filename: (string | null); 853 | 854 | /** Reference to the root namespace. */ 855 | public readonly root: Root; 856 | 857 | /** Full name including leading dot. */ 858 | public readonly fullName: string; 859 | 860 | /** 861 | * Converts this reflection object to its descriptor representation. 862 | * @returns Descriptor 863 | */ 864 | public toJSON(): { [k: string]: any }; 865 | 866 | /** 867 | * Called when this object is added to a parent. 868 | * @param parent Parent added to 869 | */ 870 | public onAdd(parent: ReflectionObject): void; 871 | 872 | /** 873 | * Called when this object is removed from a parent. 874 | * @param parent Parent removed from 875 | */ 876 | public onRemove(parent: ReflectionObject): void; 877 | 878 | /** 879 | * Resolves this objects type references. 880 | * @returns `this` 881 | */ 882 | public resolve(): ReflectionObject; 883 | 884 | /** 885 | * Gets an option value. 886 | * @param name Option name 887 | * @returns Option value or `undefined` if not set 888 | */ 889 | public getOption(name: string): any; 890 | 891 | /** 892 | * Sets an option. 893 | * @param name Option name 894 | * @param value Option value 895 | * @param [ifNotSet] Sets the option only if it isn't currently set 896 | * @returns `this` 897 | */ 898 | public setOption(name: string, value: any, ifNotSet?: boolean): ReflectionObject; 899 | 900 | /** 901 | * Sets multiple options. 902 | * @param options Options to set 903 | * @param [ifNotSet] Sets an option only if it isn't currently set 904 | * @returns `this` 905 | */ 906 | public setOptions(options: { [k: string]: any }, ifNotSet?: boolean): ReflectionObject; 907 | 908 | /** 909 | * Converts this instance to its string representation. 910 | * @returns Class name[, space, full name] 911 | */ 912 | public toString(): string; 913 | } 914 | 915 | /** Reflected oneof. */ 916 | export class OneOf extends ReflectionObject { 917 | 918 | /** 919 | * Constructs a new oneof instance. 920 | * @param name Oneof name 921 | * @param [fieldNames] Field names 922 | * @param [options] Declared options 923 | */ 924 | constructor(name: string, fieldNames?: (string[] | { [k: string]: any }), options?: { [k: string]: any }); 925 | 926 | /** Field names that belong to this oneof. */ 927 | public oneof: string[]; 928 | 929 | /** Fields that belong to this oneof as an array for iteration. */ 930 | public readonly fieldsArray: Field[]; 931 | 932 | /** 933 | * Constructs a oneof from a oneof descriptor. 934 | * @param name Oneof name 935 | * @param json Oneof descriptor 936 | * @returns Created oneof 937 | * @throws {TypeError} If arguments are invalid 938 | */ 939 | public static fromJSON(name: string, json: IOneOf): OneOf; 940 | 941 | /** 942 | * Converts this oneof to a oneof descriptor. 943 | * @returns Oneof descriptor 944 | */ 945 | public toJSON(): IOneOf; 946 | 947 | /** 948 | * Adds a field to this oneof and removes it from its current parent, if any. 949 | * @param field Field to add 950 | * @returns `this` 951 | */ 952 | public add(field: Field): OneOf; 953 | 954 | /** 955 | * Removes a field from this oneof and puts it back to the oneof's parent. 956 | * @param field Field to remove 957 | * @returns `this` 958 | */ 959 | public remove(field: Field): OneOf; 960 | 961 | /** 962 | * OneOf decorator (TypeScript). 963 | * @param fieldNames Field names 964 | * @returns Decorator function 965 | */ 966 | public static d(...fieldNames: string[]): OneOfDecorator; 967 | } 968 | 969 | /** Oneof descriptor. */ 970 | export interface IOneOf { 971 | 972 | /** Oneof field names */ 973 | oneof: string[]; 974 | 975 | /** Oneof options */ 976 | options?: { [k: string]: any }; 977 | } 978 | 979 | /** 980 | * Decorator function as returned by {@link OneOf.d} (TypeScript). 981 | * @param prototype Target prototype 982 | * @param oneofName OneOf name 983 | */ 984 | type OneOfDecorator = (prototype: object, oneofName: string) => void; 985 | 986 | /** 987 | * Parses the given .proto source and returns an object with the parsed contents. 988 | * @param source Source contents 989 | * @param [options] Parse options. Defaults to {@link parse.defaults} when omitted. 990 | * @returns Parser result 991 | */ 992 | export function parse(source: string, options?: IParseOptions): IParserResult; 993 | 994 | /** Result object returned from {@link parse}. */ 995 | export interface IParserResult { 996 | 997 | /** Package name, if declared */ 998 | package: (string | undefined); 999 | 1000 | /** Imports, if any */ 1001 | imports: (string[] | undefined); 1002 | 1003 | /** Weak imports, if any */ 1004 | weakImports: (string[] | undefined); 1005 | 1006 | /** Syntax, if specified (either `"proto2"` or `"proto3"`) */ 1007 | syntax: (string | undefined); 1008 | 1009 | /** Populated root instance */ 1010 | root: Root; 1011 | } 1012 | 1013 | /** Options modifying the behavior of {@link parse}. */ 1014 | export interface IParseOptions { 1015 | 1016 | /** Keeps field casing instead of converting to camel case */ 1017 | keepCase?: boolean; 1018 | } 1019 | 1020 | /** 1021 | * Parses the given .proto source and returns an object with the parsed contents. 1022 | * @param source Source contents 1023 | * @param root Root to populate 1024 | * @param [options] Parse options. Defaults to {@link parse.defaults} when omitted. 1025 | * @returns Parser result 1026 | */ 1027 | export function parse(source: string, root: Root, options?: IParseOptions): IParserResult; 1028 | 1029 | /** Wire format reader using `Uint8Array` if available, otherwise `Array`. */ 1030 | export class Reader { 1031 | 1032 | /** 1033 | * Constructs a new reader instance using the specified buffer. 1034 | * @param buffer Buffer to read from 1035 | */ 1036 | constructor(buffer: Uint8Array); 1037 | 1038 | /** Read buffer. */ 1039 | public buf: Uint8Array; 1040 | 1041 | /** Read buffer position. */ 1042 | public pos: number; 1043 | 1044 | /** Read buffer length. */ 1045 | public len: number; 1046 | 1047 | /** 1048 | * Creates a new reader using the specified buffer. 1049 | * @param buffer Buffer to read from 1050 | * @returns A {@link BufferReader} if `buffer` is a Buffer, otherwise a {@link Reader} 1051 | * @throws {Error} If `buffer` is not a valid buffer 1052 | */ 1053 | public static create(buffer: (Uint8Array | Buffer)): (Reader | BufferReader); 1054 | 1055 | /** 1056 | * Reads a varint as an unsigned 32 bit value. 1057 | * @returns Value read 1058 | */ 1059 | public uint32(): number; 1060 | 1061 | /** 1062 | * Reads a varint as a signed 32 bit value. 1063 | * @returns Value read 1064 | */ 1065 | public int32(): number; 1066 | 1067 | /** 1068 | * Reads a zig-zag encoded varint as a signed 32 bit value. 1069 | * @returns Value read 1070 | */ 1071 | public sint32(): number; 1072 | 1073 | /** 1074 | * Reads a varint as a signed 64 bit value. 1075 | * @returns Value read 1076 | */ 1077 | public int64(): Long; 1078 | 1079 | /** 1080 | * Reads a varint as an unsigned 64 bit value. 1081 | * @returns Value read 1082 | */ 1083 | public uint64(): Long; 1084 | 1085 | /** 1086 | * Reads a zig-zag encoded varint as a signed 64 bit value. 1087 | * @returns Value read 1088 | */ 1089 | public sint64(): Long; 1090 | 1091 | /** 1092 | * Reads a varint as a boolean. 1093 | * @returns Value read 1094 | */ 1095 | public bool(): boolean; 1096 | 1097 | /** 1098 | * Reads fixed 32 bits as an unsigned 32 bit integer. 1099 | * @returns Value read 1100 | */ 1101 | public fixed32(): number; 1102 | 1103 | /** 1104 | * Reads fixed 32 bits as a signed 32 bit integer. 1105 | * @returns Value read 1106 | */ 1107 | public sfixed32(): number; 1108 | 1109 | /** 1110 | * Reads fixed 64 bits. 1111 | * @returns Value read 1112 | */ 1113 | public fixed64(): Long; 1114 | 1115 | /** 1116 | * Reads zig-zag encoded fixed 64 bits. 1117 | * @returns Value read 1118 | */ 1119 | public sfixed64(): Long; 1120 | 1121 | /** 1122 | * Reads a float (32 bit) as a number. 1123 | * @returns Value read 1124 | */ 1125 | public float(): number; 1126 | 1127 | /** 1128 | * Reads a double (64 bit float) as a number. 1129 | * @returns Value read 1130 | */ 1131 | public double(): number; 1132 | 1133 | /** 1134 | * Reads a sequence of bytes preceeded by its length as a varint. 1135 | * @returns Value read 1136 | */ 1137 | public bytes(): Uint8Array; 1138 | 1139 | /** 1140 | * Reads a string preceeded by its byte length as a varint. 1141 | * @returns Value read 1142 | */ 1143 | public string(): string; 1144 | 1145 | /** 1146 | * Skips the specified number of bytes if specified, otherwise skips a varint. 1147 | * @param [length] Length if known, otherwise a varint is assumed 1148 | * @returns `this` 1149 | */ 1150 | public skip(length?: number): Reader; 1151 | 1152 | /** 1153 | * Skips the next element of the specified wire type. 1154 | * @param wireType Wire type received 1155 | * @returns `this` 1156 | */ 1157 | public skipType(wireType: number): Reader; 1158 | } 1159 | 1160 | /** Wire format reader using node buffers. */ 1161 | export class BufferReader extends Reader { 1162 | 1163 | /** 1164 | * Constructs a new buffer reader instance. 1165 | * @param buffer Buffer to read from 1166 | */ 1167 | constructor(buffer: Buffer); 1168 | 1169 | /** 1170 | * Reads a sequence of bytes preceeded by its length as a varint. 1171 | * @returns Value read 1172 | */ 1173 | public bytes(): Buffer; 1174 | } 1175 | 1176 | /** Root namespace wrapping all types, enums, services, sub-namespaces etc. that belong together. */ 1177 | export class Root extends NamespaceBase { 1178 | 1179 | /** 1180 | * Constructs a new root namespace instance. 1181 | * @param [options] Top level options 1182 | */ 1183 | constructor(options?: { [k: string]: any }); 1184 | 1185 | /** Deferred extension fields. */ 1186 | public deferred: Field[]; 1187 | 1188 | /** Resolved file names of loaded files. */ 1189 | public files: string[]; 1190 | 1191 | /** 1192 | * Loads a namespace descriptor into a root namespace. 1193 | * @param json Nameespace descriptor 1194 | * @param [root] Root namespace, defaults to create a new one if omitted 1195 | * @returns Root namespace 1196 | */ 1197 | public static fromJSON(json: INamespace, root?: Root): Root; 1198 | 1199 | /** 1200 | * Resolves the path of an imported file, relative to the importing origin. 1201 | * This method exists so you can override it with your own logic in case your imports are scattered over multiple directories. 1202 | * @param origin The file name of the importing file 1203 | * @param target The file name being imported 1204 | * @returns Resolved path to `target` or `null` to skip the file 1205 | */ 1206 | public resolvePath(origin: string, target: string): (string | null); 1207 | 1208 | /** 1209 | * Loads one or multiple .proto or preprocessed .json files into this root namespace and calls the callback. 1210 | * @param filename Names of one or multiple files to load 1211 | * @param options Parse options 1212 | * @param callback Callback function 1213 | */ 1214 | public load(filename: (string | string[]), options: IParseOptions, callback: LoadCallback): void; 1215 | 1216 | /** 1217 | * Loads one or multiple .proto or preprocessed .json files into this root namespace and calls the callback. 1218 | * @param filename Names of one or multiple files to load 1219 | * @param callback Callback function 1220 | */ 1221 | public load(filename: (string | string[]), callback: LoadCallback): void; 1222 | 1223 | /** 1224 | * Loads one or multiple .proto or preprocessed .json files into this root namespace and returns a promise. 1225 | * @param filename Names of one or multiple files to load 1226 | * @param [options] Parse options. Defaults to {@link parse.defaults} when omitted. 1227 | * @returns Promise 1228 | */ 1229 | public load(filename: (string | string[]), options?: IParseOptions): Promise; 1230 | 1231 | /** 1232 | * Synchronously loads one or multiple .proto or preprocessed .json files into this root namespace (node only). 1233 | * @param filename Names of one or multiple files to load 1234 | * @param [options] Parse options. Defaults to {@link parse.defaults} when omitted. 1235 | * @returns Root namespace 1236 | * @throws {Error} If synchronous fetching is not supported (i.e. in browsers) or if a file's syntax is invalid 1237 | */ 1238 | public loadSync(filename: (string | string[]), options?: IParseOptions): Root; 1239 | } 1240 | 1241 | /** 1242 | * Named roots. 1243 | * This is where pbjs stores generated structures (the option `-r, --root` specifies a name). 1244 | * Can also be used manually to make roots available accross modules. 1245 | */ 1246 | export let roots: { [k: string]: Root }; 1247 | 1248 | /** Streaming RPC helpers. */ 1249 | export namespace rpc { 1250 | 1251 | /** 1252 | * A service method callback as used by {@link rpc.ServiceMethod|ServiceMethod}. 1253 | * 1254 | * Differs from {@link RPCImplCallback} in that it is an actual callback of a service method which may not return `response = null`. 1255 | * @param error Error, if any 1256 | * @param [response] Response message 1257 | */ 1258 | type ServiceMethodCallback> = (error: (Error | null), response?: TRes) => void; 1259 | 1260 | /** 1261 | * A service method part of a {@link rpc.Service} as created by {@link Service.create}. 1262 | * @param request Request message or plain object 1263 | * @param [callback] Node-style callback called with the error, if any, and the response message 1264 | * @returns Promise if `callback` has been omitted, otherwise `undefined` 1265 | */ 1266 | type ServiceMethod, TRes extends Message> = (request: (TReq | Properties), callback?: rpc.ServiceMethodCallback) => Promise>; 1267 | 1268 | /** An RPC service as returned by {@link Service#create}. */ 1269 | class Service extends util.EventEmitter { 1270 | 1271 | /** 1272 | * Constructs a new RPC service instance. 1273 | * @param rpcImpl RPC implementation 1274 | * @param [requestDelimited=false] Whether requests are length-delimited 1275 | * @param [responseDelimited=false] Whether responses are length-delimited 1276 | */ 1277 | constructor(rpcImpl: RPCImpl, requestDelimited?: boolean, responseDelimited?: boolean); 1278 | 1279 | /** RPC implementation. Becomes `null` once the service is ended. */ 1280 | public rpcImpl: (RPCImpl | null); 1281 | 1282 | /** Whether requests are length-delimited. */ 1283 | public requestDelimited: boolean; 1284 | 1285 | /** Whether responses are length-delimited. */ 1286 | public responseDelimited: boolean; 1287 | 1288 | /** 1289 | * Calls a service method through {@link rpc.Service#rpcImpl|rpcImpl}. 1290 | * @param method Reflected or static method 1291 | * @param requestCtor Request constructor 1292 | * @param responseCtor Response constructor 1293 | * @param request Request message or plain object 1294 | * @param callback Service callback 1295 | */ 1296 | public rpcCall, TRes extends Message>(method: (Method | rpc.ServiceMethod), requestCtor: Constructor, responseCtor: Constructor, request: (TReq | Properties), callback: rpc.ServiceMethodCallback): void; 1297 | 1298 | /** 1299 | * Ends this service and emits the `end` event. 1300 | * @param [endedByRPC=false] Whether the service has been ended by the RPC implementation. 1301 | * @returns `this` 1302 | */ 1303 | public end(endedByRPC?: boolean): rpc.Service; 1304 | } 1305 | } 1306 | 1307 | /** 1308 | * RPC implementation passed to {@link Service#create} performing a service request on network level, i.e. by utilizing http requests or websockets. 1309 | * @param method Reflected or static method being called 1310 | * @param requestData Request data 1311 | * @param callback Callback function 1312 | */ 1313 | type RPCImpl = (method: (Method | rpc.ServiceMethod, Message<{}>>), requestData: Uint8Array, callback: RPCImplCallback) => void; 1314 | 1315 | /** 1316 | * Node-style callback as used by {@link RPCImpl}. 1317 | * @param error Error, if any, otherwise `null` 1318 | * @param [response] Response data or `null` to signal end of stream, if there hasn't been an error 1319 | */ 1320 | type RPCImplCallback = (error: (Error | null), response?: (Uint8Array | null)) => void; 1321 | 1322 | /** Reflected service. */ 1323 | export class Service extends NamespaceBase { 1324 | 1325 | /** 1326 | * Constructs a new service instance. 1327 | * @param name Service name 1328 | * @param [options] Service options 1329 | * @throws {TypeError} If arguments are invalid 1330 | */ 1331 | constructor(name: string, options?: { [k: string]: any }); 1332 | 1333 | /** Service methods. */ 1334 | public methods: { [k: string]: Method }; 1335 | 1336 | /** 1337 | * Constructs a service from a service descriptor. 1338 | * @param name Service name 1339 | * @param json Service descriptor 1340 | * @returns Created service 1341 | * @throws {TypeError} If arguments are invalid 1342 | */ 1343 | public static fromJSON(name: string, json: IService): Service; 1344 | 1345 | /** 1346 | * Converts this service to a service descriptor. 1347 | * @returns Service descriptor 1348 | */ 1349 | public toJSON(): IService; 1350 | 1351 | /** Methods of this service as an array for iteration. */ 1352 | public readonly methodsArray: Method[]; 1353 | 1354 | /** 1355 | * Creates a runtime service using the specified rpc implementation. 1356 | * @param rpcImpl RPC implementation 1357 | * @param [requestDelimited=false] Whether requests are length-delimited 1358 | * @param [responseDelimited=false] Whether responses are length-delimited 1359 | * @returns RPC service. Useful where requests and/or responses are streamed. 1360 | */ 1361 | public create(rpcImpl: RPCImpl, requestDelimited?: boolean, responseDelimited?: boolean): rpc.Service; 1362 | } 1363 | 1364 | /** Service descriptor. */ 1365 | export interface IService extends INamespace { 1366 | 1367 | /** Method descriptors */ 1368 | methods: { [k: string]: IMethod }; 1369 | } 1370 | 1371 | /** 1372 | * Gets the next token and advances. 1373 | * @returns Next token or `null` on eof 1374 | */ 1375 | type TokenizerHandleNext = () => (string | null); 1376 | 1377 | /** 1378 | * Peeks for the next token. 1379 | * @returns Next token or `null` on eof 1380 | */ 1381 | type TokenizerHandlePeek = () => (string | null); 1382 | 1383 | /** 1384 | * Pushes a token back to the stack. 1385 | * @param token Token 1386 | */ 1387 | type TokenizerHandlePush = (token: string) => void; 1388 | 1389 | /** 1390 | * Skips the next token. 1391 | * @param expected Expected token 1392 | * @param [optional=false] If optional 1393 | * @returns Whether the token matched 1394 | * @throws {Error} If the token didn't match and is not optional 1395 | */ 1396 | type TokenizerHandleSkip = (expected: string, optional?: boolean) => boolean; 1397 | 1398 | /** 1399 | * Gets the comment on the previous line or, alternatively, the line comment on the specified line. 1400 | * @param [line] Line number 1401 | * @returns Comment text or `null` if none 1402 | */ 1403 | type TokenizerHandleCmnt = (line?: number) => (string | null); 1404 | 1405 | /** Handle object returned from {@link tokenize}. */ 1406 | export interface ITokenizerHandle { 1407 | 1408 | /** Gets the next token and advances (`null` on eof) */ 1409 | next: TokenizerHandleNext; 1410 | 1411 | /** Peeks for the next token (`null` on eof) */ 1412 | peek: TokenizerHandlePeek; 1413 | 1414 | /** Pushes a token back to the stack */ 1415 | push: TokenizerHandlePush; 1416 | 1417 | /** Skips a token, returns its presence and advances or, if non-optional and not present, throws */ 1418 | skip: TokenizerHandleSkip; 1419 | 1420 | /** Gets the comment on the previous line or the line comment on the specified line, if any */ 1421 | cmnt: TokenizerHandleCmnt; 1422 | 1423 | /** Current line number */ 1424 | line: number; 1425 | } 1426 | 1427 | /** 1428 | * Tokenizes the given .proto source and returns an object with useful utility functions. 1429 | * @param source Source contents 1430 | * @returns Tokenizer handle 1431 | */ 1432 | export function tokenize(source: string): ITokenizerHandle; 1433 | 1434 | export namespace tokenize { 1435 | 1436 | /** 1437 | * Unescapes a string. 1438 | * @param str String to unescape 1439 | * @returns Unescaped string 1440 | */ 1441 | function unescape(str: string): string; 1442 | } 1443 | 1444 | /** Reflected message type. */ 1445 | export class Type extends NamespaceBase { 1446 | 1447 | /** 1448 | * Constructs a new reflected message type instance. 1449 | * @param name Message name 1450 | * @param [options] Declared options 1451 | */ 1452 | constructor(name: string, options?: { [k: string]: any }); 1453 | 1454 | /** Message fields. */ 1455 | public fields: { [k: string]: Field }; 1456 | 1457 | /** Oneofs declared within this namespace, if any. */ 1458 | public oneofs: { [k: string]: OneOf }; 1459 | 1460 | /** Extension ranges, if any. */ 1461 | public extensions: number[][]; 1462 | 1463 | /** Reserved ranges, if any. */ 1464 | public reserved: (number[] | string) []; 1465 | 1466 | /** Message fields by id. */ 1467 | public readonly fieldsById: { [k: number]: Field }; 1468 | 1469 | /** Fields of this message as an array for iteration. */ 1470 | public readonly fieldsArray: Field[]; 1471 | 1472 | /** Oneofs of this message as an array for iteration. */ 1473 | public readonly oneofsArray: OneOf[]; 1474 | 1475 | /** 1476 | * The registered constructor, if any registered, otherwise a generic constructor. 1477 | * Assigning a function replaces the internal constructor. If the function does not extend {@link Message} yet, its prototype will be setup accordingly and static methods will be populated. If it already extends {@link Message}, it will just replace the internal constructor. 1478 | */ 1479 | public ctor: Constructor<{}>; 1480 | 1481 | /** 1482 | * Generates a constructor function for the specified type. 1483 | * @param mtype Message type 1484 | * @returns Codegen instance 1485 | */ 1486 | public static generateConstructor(mtype: Type): Codegen; 1487 | 1488 | /** 1489 | * Creates a message type from a message type descriptor. 1490 | * @param name Message name 1491 | * @param json Message type descriptor 1492 | * @returns Created message type 1493 | */ 1494 | public static fromJSON(name: string, json: IType): Type; 1495 | 1496 | /** 1497 | * Converts this message type to a message type descriptor. 1498 | * @returns Message type descriptor 1499 | */ 1500 | public toJSON(): IType; 1501 | 1502 | /** 1503 | * Adds a nested object to this type. 1504 | * @param object Nested object to add 1505 | * @returns `this` 1506 | * @throws {TypeError} If arguments are invalid 1507 | * @throws {Error} If there is already a nested object with this name or, if a field, when there is already a field with this id 1508 | */ 1509 | public add(object: ReflectionObject): Type; 1510 | 1511 | /** 1512 | * Removes a nested object from this type. 1513 | * @param object Nested object to remove 1514 | * @returns `this` 1515 | * @throws {TypeError} If arguments are invalid 1516 | * @throws {Error} If `object` is not a member of this type 1517 | */ 1518 | public remove(object: ReflectionObject): Type; 1519 | 1520 | /** 1521 | * Tests if the specified id is reserved. 1522 | * @param id Id to test 1523 | * @returns `true` if reserved, otherwise `false` 1524 | */ 1525 | public isReservedId(id: number): boolean; 1526 | 1527 | /** 1528 | * Tests if the specified name is reserved. 1529 | * @param name Name to test 1530 | * @returns `true` if reserved, otherwise `false` 1531 | */ 1532 | public isReservedName(name: string): boolean; 1533 | 1534 | /** 1535 | * Creates a new message of this type using the specified properties. 1536 | * @param [properties] Properties to set 1537 | * @returns Message instance 1538 | */ 1539 | public create(properties?: { [k: string]: any }): Message<{}>; 1540 | 1541 | /** 1542 | * Sets up {@link Type#encode|encode}, {@link Type#decode|decode} and {@link Type#verify|verify}. 1543 | * @returns `this` 1544 | */ 1545 | public setup(): Type; 1546 | 1547 | /** 1548 | * Encodes a message of this type. Does not implicitly {@link Type#verify|verify} messages. 1549 | * @param message Message instance or plain object 1550 | * @param [writer] Writer to encode to 1551 | * @returns writer 1552 | */ 1553 | public encode(message: (Message<{}> | { [k: string]: any }), writer?: Writer): Writer; 1554 | 1555 | /** 1556 | * Encodes a message of this type preceeded by its byte length as a varint. Does not implicitly {@link Type#verify|verify} messages. 1557 | * @param message Message instance or plain object 1558 | * @param [writer] Writer to encode to 1559 | * @returns writer 1560 | */ 1561 | public encodeDelimited(message: (Message<{}> | { [k: string]: any }), writer?: Writer): Writer; 1562 | 1563 | /** 1564 | * Decodes a message of this type. 1565 | * @param reader Reader or buffer to decode from 1566 | * @param [length] Length of the message, if known beforehand 1567 | * @returns Decoded message 1568 | * @throws {Error} If the payload is not a reader or valid buffer 1569 | * @throws {util.ProtocolError<{}>} If required fields are missing 1570 | */ 1571 | public decode(reader: (Reader | Uint8Array), length?: number): Message<{}>; 1572 | 1573 | /** 1574 | * Decodes a message of this type preceeded by its byte length as a varint. 1575 | * @param reader Reader or buffer to decode from 1576 | * @returns Decoded message 1577 | * @throws {Error} If the payload is not a reader or valid buffer 1578 | * @throws {util.ProtocolError} If required fields are missing 1579 | */ 1580 | public decodeDelimited(reader: (Reader | Uint8Array)): Message<{}>; 1581 | 1582 | /** 1583 | * Verifies that field values are valid and that required fields are present. 1584 | * @param message Plain object to verify 1585 | * @returns `null` if valid, otherwise the reason why it is not 1586 | */ 1587 | public verify(message: { [k: string]: any }): (null | string); 1588 | 1589 | /** 1590 | * Creates a new message of this type from a plain object. Also converts values to their respective internal types. 1591 | * @param object Plain object to convert 1592 | * @returns Message instance 1593 | */ 1594 | public fromObject(object: { [k: string]: any }): Message<{}>; 1595 | 1596 | /** 1597 | * Creates a plain object from a message of this type. Also converts values to other types if specified. 1598 | * @param message Message instance 1599 | * @param [options] Conversion options 1600 | * @returns Plain object 1601 | */ 1602 | public toObject(message: Message<{}>, options?: IConversionOptions): { [k: string]: any }; 1603 | 1604 | /** 1605 | * Type decorator (TypeScript). 1606 | * @param [typeName] Type name, defaults to the constructor's name 1607 | * @returns Decorator function 1608 | */ 1609 | public static d>(typeName?: string): TypeDecorator; 1610 | } 1611 | 1612 | /** Message type descriptor. */ 1613 | export interface IType extends INamespace { 1614 | 1615 | /** Oneof descriptors */ 1616 | oneofs?: { [k: string]: IOneOf }; 1617 | 1618 | /** Field descriptors */ 1619 | fields: { [k: string]: IField }; 1620 | 1621 | /** Extension ranges */ 1622 | extensions?: number[][]; 1623 | 1624 | /** Reserved ranges */ 1625 | reserved?: number[][]; 1626 | 1627 | /** Whether a legacy group or not */ 1628 | group?: boolean; 1629 | } 1630 | 1631 | /** Conversion options as used by {@link Type#toObject} and {@link Message.toObject}. */ 1632 | export interface IConversionOptions { 1633 | 1634 | /** 1635 | * Long conversion type. 1636 | * Valid values are `String` and `Number` (the global types). 1637 | * Defaults to copy the present value, which is a possibly unsafe number without and a {@link Long} with a long library. 1638 | */ 1639 | longs?: Function; 1640 | 1641 | /** 1642 | * Enum value conversion type. 1643 | * Only valid value is `String` (the global type). 1644 | * Defaults to copy the present value, which is the numeric id. 1645 | */ 1646 | enums?: Function; 1647 | 1648 | /** 1649 | * Bytes value conversion type. 1650 | * Valid values are `Array` and (a base64 encoded) `String` (the global types). 1651 | * Defaults to copy the present value, which usually is a Buffer under node and an Uint8Array in the browser. 1652 | */ 1653 | bytes?: Function; 1654 | 1655 | /** Also sets default values on the resulting object */ 1656 | defaults?: boolean; 1657 | 1658 | /** Sets empty arrays for missing repeated fields even if `defaults=false` */ 1659 | arrays?: boolean; 1660 | 1661 | /** Sets empty objects for missing map fields even if `defaults=false` */ 1662 | objects?: boolean; 1663 | 1664 | /** Includes virtual oneof properties set to the present field's name, if any */ 1665 | oneofs?: boolean; 1666 | 1667 | /** Performs additional JSON compatibility conversions, i.e. NaN and Infinity to strings */ 1668 | json?: boolean; 1669 | } 1670 | 1671 | /** 1672 | * Decorator function as returned by {@link Type.d} (TypeScript). 1673 | * @param target Target constructor 1674 | */ 1675 | type TypeDecorator> = (target: Constructor) => void; 1676 | 1677 | /** Common type constants. */ 1678 | export namespace types { 1679 | 1680 | /** Basic type wire types. */ 1681 | const basic: { 1682 | "double": number, 1683 | "float": number, 1684 | "int32": number, 1685 | "uint32": number, 1686 | "sint32": number, 1687 | "fixed32": number, 1688 | "sfixed32": number, 1689 | "int64": number, 1690 | "uint64": number, 1691 | "sint64": number, 1692 | "fixed64": number, 1693 | "sfixed64": number, 1694 | "bool": number, 1695 | "string": number, 1696 | "bytes": number 1697 | }; 1698 | 1699 | /** Basic type defaults. */ 1700 | const defaults: { 1701 | "double": number, 1702 | "float": number, 1703 | "int32": number, 1704 | "uint32": number, 1705 | "sint32": number, 1706 | "fixed32": number, 1707 | "sfixed32": number, 1708 | "int64": number, 1709 | "uint64": number, 1710 | "sint64": number, 1711 | "fixed64": number, 1712 | "sfixed64": number, 1713 | "bool": boolean, 1714 | "string": string, 1715 | "bytes": number[], 1716 | "message": null 1717 | }; 1718 | 1719 | /** Basic long type wire types. */ 1720 | const long: { 1721 | "int64": number, 1722 | "uint64": number, 1723 | "sint64": number, 1724 | "fixed64": number, 1725 | "sfixed64": number 1726 | }; 1727 | 1728 | /** Allowed types for map keys with their associated wire type. */ 1729 | const mapKey: { 1730 | "int32": number, 1731 | "uint32": number, 1732 | "sint32": number, 1733 | "fixed32": number, 1734 | "sfixed32": number, 1735 | "int64": number, 1736 | "uint64": number, 1737 | "sint64": number, 1738 | "fixed64": number, 1739 | "sfixed64": number, 1740 | "bool": number, 1741 | "string": number 1742 | }; 1743 | 1744 | /** Allowed types for packed repeated fields with their associated wire type. */ 1745 | const packed: { 1746 | "double": number, 1747 | "float": number, 1748 | "int32": number, 1749 | "uint32": number, 1750 | "sint32": number, 1751 | "fixed32": number, 1752 | "sfixed32": number, 1753 | "int64": number, 1754 | "uint64": number, 1755 | "sint64": number, 1756 | "fixed64": number, 1757 | "sfixed64": number, 1758 | "bool": number 1759 | }; 1760 | } 1761 | 1762 | /** Constructor type. */ 1763 | export interface Constructor extends Function { 1764 | new(...params: any[]): T; prototype: T; 1765 | } 1766 | 1767 | /** Properties type. */ 1768 | type Properties = {[P in keyof T]?: T[P]}; 1769 | 1770 | /** 1771 | * Any compatible Buffer instance. 1772 | * This is a minimal stand-alone definition of a Buffer instance. The actual type is that exported by node's typings. 1773 | */ 1774 | export interface Buffer extends Uint8Array { 1775 | } 1776 | 1777 | /** 1778 | * Any compatible Long instance. 1779 | * This is a minimal stand-alone definition of a Long instance. The actual type is that exported by long.js. 1780 | */ 1781 | export interface Long { 1782 | 1783 | /** Low bits */ 1784 | low: number; 1785 | 1786 | /** High bits */ 1787 | high: number; 1788 | 1789 | /** Whether unsigned or not */ 1790 | unsigned: boolean; 1791 | } 1792 | 1793 | /** 1794 | * A OneOf getter as returned by {@link util.oneOfGetter}. 1795 | * @returns Set field name, if any 1796 | */ 1797 | type OneOfGetter = () => (string | undefined); 1798 | 1799 | /** 1800 | * A OneOf setter as returned by {@link util.oneOfSetter}. 1801 | * @param value Field name 1802 | */ 1803 | type OneOfSetter = (value: (string | undefined)) => void; 1804 | 1805 | /** Various utility functions. */ 1806 | export namespace util { 1807 | 1808 | /** Helper class for working with the low and high bits of a 64 bit value. */ 1809 | class LongBits { 1810 | 1811 | /** 1812 | * Constructs new long bits. 1813 | * @param lo Low 32 bits, unsigned 1814 | * @param hi High 32 bits, unsigned 1815 | */ 1816 | constructor(lo: number, hi: number); 1817 | 1818 | /** Low bits. */ 1819 | public lo: number; 1820 | 1821 | /** High bits. */ 1822 | public hi: number; 1823 | 1824 | /** Zero bits. */ 1825 | public static zero: util.LongBits; 1826 | 1827 | /** Zero hash. */ 1828 | public static zeroHash: string; 1829 | 1830 | /** 1831 | * Constructs new long bits from the specified number. 1832 | * @param value Value 1833 | * @returns Instance 1834 | */ 1835 | public static fromNumber(value: number): util.LongBits; 1836 | 1837 | /** 1838 | * Constructs new long bits from a number, long or string. 1839 | * @param value Value 1840 | * @returns Instance 1841 | */ 1842 | public static from(value: (Long | number | string)): util.LongBits; 1843 | 1844 | /** 1845 | * Converts this long bits to a possibly unsafe JavaScript number. 1846 | * @param [unsigned=false] Whether unsigned or not 1847 | * @returns Possibly unsafe number 1848 | */ 1849 | public toNumber(unsigned?: boolean): number; 1850 | 1851 | /** 1852 | * Converts this long bits to a long. 1853 | * @param [unsigned=false] Whether unsigned or not 1854 | * @returns Long 1855 | */ 1856 | public toLong(unsigned?: boolean): Long; 1857 | 1858 | /** 1859 | * Constructs new long bits from the specified 8 characters long hash. 1860 | * @param hash Hash 1861 | * @returns Bits 1862 | */ 1863 | public static fromHash(hash: string): util.LongBits; 1864 | 1865 | /** 1866 | * Converts this long bits to a 8 characters long hash. 1867 | * @returns Hash 1868 | */ 1869 | public toHash(): string; 1870 | 1871 | /** 1872 | * Zig-zag encodes this long bits. 1873 | * @returns `this` 1874 | */ 1875 | public zzEncode(): util.LongBits; 1876 | 1877 | /** 1878 | * Zig-zag decodes this long bits. 1879 | * @returns `this` 1880 | */ 1881 | public zzDecode(): util.LongBits; 1882 | 1883 | /** 1884 | * Calculates the length of this longbits when encoded as a varint. 1885 | * @returns Length 1886 | */ 1887 | public length(): number; 1888 | } 1889 | 1890 | /** An immuable empty array. */ 1891 | const emptyArray: any[]; 1892 | 1893 | /** An immutable empty object. */ 1894 | const emptyObject: object; 1895 | 1896 | /** Whether running within node or not. */ 1897 | const isNode: boolean; 1898 | 1899 | /** 1900 | * Tests if the specified value is an integer. 1901 | * @param value Value to test 1902 | * @returns `true` if the value is an integer 1903 | */ 1904 | function isInteger(value: any): boolean; 1905 | 1906 | /** 1907 | * Tests if the specified value is a string. 1908 | * @param value Value to test 1909 | * @returns `true` if the value is a string 1910 | */ 1911 | function isString(value: any): boolean; 1912 | 1913 | /** 1914 | * Tests if the specified value is a non-null object. 1915 | * @param value Value to test 1916 | * @returns `true` if the value is a non-null object 1917 | */ 1918 | function isObject(value: any): boolean; 1919 | 1920 | /** 1921 | * Checks if a property on a message is considered to be present. 1922 | * This is an alias of {@link util.isSet}. 1923 | * @param obj Plain object or message instance 1924 | * @param prop Property name 1925 | * @returns `true` if considered to be present, otherwise `false` 1926 | */ 1927 | function isset(obj: object, prop: string): boolean; 1928 | 1929 | /** 1930 | * Checks if a property on a message is considered to be present. 1931 | * @param obj Plain object or message instance 1932 | * @param prop Property name 1933 | * @returns `true` if considered to be present, otherwise `false` 1934 | */ 1935 | function isSet(obj: object, prop: string): boolean; 1936 | 1937 | /** Node's Buffer class if available. */ 1938 | let Buffer: Constructor; 1939 | 1940 | /** 1941 | * Creates a new buffer of whatever type supported by the environment. 1942 | * @param [sizeOrArray=0] Buffer size or number array 1943 | * @returns Buffer 1944 | */ 1945 | function newBuffer(sizeOrArray?: (number | number[])): (Uint8Array | Buffer); 1946 | 1947 | /** Array implementation used in the browser. `Uint8Array` if supported, otherwise `Array`. */ 1948 | let Array: Constructor; 1949 | 1950 | /** Long.js's Long class if available. */ 1951 | let Long: Constructor; 1952 | 1953 | /** Regular expression used to verify 2 bit (`bool`) map keys. */ 1954 | const key2Re: RegExp; 1955 | 1956 | /** Regular expression used to verify 32 bit (`int32` etc.) map keys. */ 1957 | const key32Re: RegExp; 1958 | 1959 | /** Regular expression used to verify 64 bit (`int64` etc.) map keys. */ 1960 | const key64Re: RegExp; 1961 | 1962 | /** 1963 | * Converts a number or long to an 8 characters long hash string. 1964 | * @param value Value to convert 1965 | * @returns Hash 1966 | */ 1967 | function longToHash(value: (Long | number)): string; 1968 | 1969 | /** 1970 | * Converts an 8 characters long hash string to a long or number. 1971 | * @param hash Hash 1972 | * @param [unsigned=false] Whether unsigned or not 1973 | * @returns Original value 1974 | */ 1975 | function longFromHash(hash: string, unsigned?: boolean): (Long | number); 1976 | 1977 | /** 1978 | * Merges the properties of the source object into the destination object. 1979 | * @param dst Destination object 1980 | * @param src Source object 1981 | * @param [ifNotSet=false] Merges only if the key is not already set 1982 | * @returns Destination object 1983 | */ 1984 | function merge(dst: { [k: string]: any }, src: { [k: string]: any }, ifNotSet?: boolean): { [k: string]: any }; 1985 | 1986 | /** 1987 | * Converts the first character of a string to lower case. 1988 | * @param str String to convert 1989 | * @returns Converted string 1990 | */ 1991 | function lcFirst(str: string): string; 1992 | 1993 | /** 1994 | * Creates a custom error constructor. 1995 | * @param name Error name 1996 | * @returns Custom error constructor 1997 | */ 1998 | function newError(name: string): Constructor; 1999 | 2000 | /** Error subclass indicating a protocol specifc error. */ 2001 | class ProtocolError> extends Error { 2002 | 2003 | /** 2004 | * Constructs a new protocol error. 2005 | * @param message Error message 2006 | * @param [properties] Additional properties 2007 | */ 2008 | constructor(message: string, properties?: { [k: string]: any }); 2009 | 2010 | /** So far decoded message instance. */ 2011 | public instance: Message; 2012 | } 2013 | 2014 | /** 2015 | * Builds a getter for a oneof's present field name. 2016 | * @param fieldNames Field names 2017 | * @returns Unbound getter 2018 | */ 2019 | function oneOfGetter(fieldNames: string[]): OneOfGetter; 2020 | 2021 | /** 2022 | * Builds a setter for a oneof's present field name. 2023 | * @param fieldNames Field names 2024 | * @returns Unbound setter 2025 | */ 2026 | function oneOfSetter(fieldNames: string[]): OneOfSetter; 2027 | 2028 | /** 2029 | * Default conversion options used for {@link Message#toJSON} implementations. 2030 | * 2031 | * These options are close to proto3's JSON mapping with the exception that internal types like Any are handled just like messages. More precisely: 2032 | * 2033 | * - Longs become strings 2034 | * - Enums become string keys 2035 | * - Bytes become base64 encoded strings 2036 | * - (Sub-)Messages become plain objects 2037 | * - Maps become plain objects with all string keys 2038 | * - Repeated fields become arrays 2039 | * - NaN and Infinity for float and double fields become strings 2040 | * 2041 | * @see https://developers.google.com/protocol-buffers/docs/proto3?hl=en#json 2042 | */ 2043 | let toJSONOptions: IConversionOptions; 2044 | 2045 | /** Node's fs module if available. */ 2046 | let fs: { [k: string]: any }; 2047 | 2048 | /** 2049 | * Converts an object's values to an array. 2050 | * @param object Object to convert 2051 | * @returns Converted array 2052 | */ 2053 | function toArray(object: { [k: string]: any }): any[]; 2054 | 2055 | /** 2056 | * Converts an array of keys immediately followed by their respective value to an object, omitting undefined values. 2057 | * @param array Array to convert 2058 | * @returns Converted object 2059 | */ 2060 | function toObject(array: any[]): { [k: string]: any }; 2061 | 2062 | /** 2063 | * Tests whether the specified name is a reserved word in JS. 2064 | * @param name Name to test 2065 | * @returns `true` if reserved, otherwise `false` 2066 | */ 2067 | function isReserved(name: string): boolean; 2068 | 2069 | /** 2070 | * Returns a safe property accessor for the specified property name. 2071 | * @param prop Property name 2072 | * @returns Safe accessor 2073 | */ 2074 | function safeProp(prop: string): string; 2075 | 2076 | /** 2077 | * Converts the first character of a string to upper case. 2078 | * @param str String to convert 2079 | * @returns Converted string 2080 | */ 2081 | function ucFirst(str: string): string; 2082 | 2083 | /** 2084 | * Converts a string to camel case. 2085 | * @param str String to convert 2086 | * @returns Converted string 2087 | */ 2088 | function camelCase(str: string): string; 2089 | 2090 | /** 2091 | * Compares reflected fields by id. 2092 | * @param a First field 2093 | * @param b Second field 2094 | * @returns Comparison value 2095 | */ 2096 | function compareFieldsById(a: Field, b: Field): number; 2097 | 2098 | /** 2099 | * Decorator helper for types (TypeScript). 2100 | * @param ctor Constructor function 2101 | * @param [typeName] Type name, defaults to the constructor's name 2102 | * @returns Reflected type 2103 | */ 2104 | function decorateType>(ctor: Constructor, typeName?: string): Type; 2105 | 2106 | /** 2107 | * Decorator helper for enums (TypeScript). 2108 | * @param object Enum object 2109 | * @returns Reflected enum 2110 | */ 2111 | function decorateEnum(object: object): Enum; 2112 | 2113 | /** Decorator root (TypeScript). */ 2114 | let decorateRoot: Root; 2115 | 2116 | /** 2117 | * Returns a promise from a node-style callback function. 2118 | * @param fn Function to call 2119 | * @param ctx Function context 2120 | * @param params Function arguments 2121 | * @returns Promisified function 2122 | */ 2123 | function asPromise(fn: asPromiseCallback, ctx: any, ...params: any[]): Promise; 2124 | 2125 | /** A minimal base64 implementation for number arrays. */ 2126 | namespace base64 { 2127 | 2128 | /** 2129 | * Calculates the byte length of a base64 encoded string. 2130 | * @param string Base64 encoded string 2131 | * @returns Byte length 2132 | */ 2133 | function length(string: string): number; 2134 | 2135 | /** 2136 | * Encodes a buffer to a base64 encoded string. 2137 | * @param buffer Source buffer 2138 | * @param start Source start 2139 | * @param end Source end 2140 | * @returns Base64 encoded string 2141 | */ 2142 | function encode(buffer: Uint8Array, start: number, end: number): string; 2143 | 2144 | /** 2145 | * Decodes a base64 encoded string to a buffer. 2146 | * @param string Source string 2147 | * @param buffer Destination buffer 2148 | * @param offset Destination offset 2149 | * @returns Number of bytes written 2150 | * @throws {Error} If encoding is invalid 2151 | */ 2152 | function decode(string: string, buffer: Uint8Array, offset: number): number; 2153 | 2154 | /** 2155 | * Tests if the specified string appears to be base64 encoded. 2156 | * @param string String to test 2157 | * @returns `true` if probably base64 encoded, otherwise false 2158 | */ 2159 | function test(string: string): boolean; 2160 | } 2161 | 2162 | /** 2163 | * Begins generating a function. 2164 | * @param functionParams Function parameter names 2165 | * @param [functionName] Function name if not anonymous 2166 | * @returns Appender that appends code to the function's body 2167 | */ 2168 | function codegen(functionParams: string[], functionName?: string): Codegen; 2169 | 2170 | namespace codegen { 2171 | 2172 | /** When set to `true`, codegen will log generated code to console. Useful for debugging. */ 2173 | let verbose: boolean; 2174 | } 2175 | 2176 | /** 2177 | * Begins generating a function. 2178 | * @param [functionName] Function name if not anonymous 2179 | * @returns Appender that appends code to the function's body 2180 | */ 2181 | function codegen(functionName?: string): Codegen; 2182 | 2183 | /** A minimal event emitter. */ 2184 | class EventEmitter { 2185 | 2186 | /** Constructs a new event emitter instance. */ 2187 | constructor(); 2188 | 2189 | /** 2190 | * Registers an event listener. 2191 | * @param evt Event name 2192 | * @param fn Listener 2193 | * @param [ctx] Listener context 2194 | * @returns `this` 2195 | */ 2196 | public on(evt: string, fn: EventEmitterListener, ctx?: any): this; 2197 | 2198 | /** 2199 | * Removes an event listener or any matching listeners if arguments are omitted. 2200 | * @param [evt] Event name. Removes all listeners if omitted. 2201 | * @param [fn] Listener to remove. Removes all listeners of `evt` if omitted. 2202 | * @returns `this` 2203 | */ 2204 | public off(evt?: string, fn?: EventEmitterListener): this; 2205 | 2206 | /** 2207 | * Emits an event by calling its listeners with the specified arguments. 2208 | * @param evt Event name 2209 | * @param args Arguments 2210 | * @returns `this` 2211 | */ 2212 | public emit(evt: string, ...args: any[]): this; 2213 | } 2214 | 2215 | /** Reads / writes floats / doubles from / to buffers. */ 2216 | namespace float { 2217 | 2218 | /** 2219 | * Writes a 32 bit float to a buffer using little endian byte order. 2220 | * @param val Value to write 2221 | * @param buf Target buffer 2222 | * @param pos Target buffer offset 2223 | */ 2224 | function writeFloatLE(val: number, buf: Uint8Array, pos: number): void; 2225 | 2226 | /** 2227 | * Writes a 32 bit float to a buffer using big endian byte order. 2228 | * @param val Value to write 2229 | * @param buf Target buffer 2230 | * @param pos Target buffer offset 2231 | */ 2232 | function writeFloatBE(val: number, buf: Uint8Array, pos: number): void; 2233 | 2234 | /** 2235 | * Reads a 32 bit float from a buffer using little endian byte order. 2236 | * @param buf Source buffer 2237 | * @param pos Source buffer offset 2238 | * @returns Value read 2239 | */ 2240 | function readFloatLE(buf: Uint8Array, pos: number): number; 2241 | 2242 | /** 2243 | * Reads a 32 bit float from a buffer using big endian byte order. 2244 | * @param buf Source buffer 2245 | * @param pos Source buffer offset 2246 | * @returns Value read 2247 | */ 2248 | function readFloatBE(buf: Uint8Array, pos: number): number; 2249 | 2250 | /** 2251 | * Writes a 64 bit double to a buffer using little endian byte order. 2252 | * @param val Value to write 2253 | * @param buf Target buffer 2254 | * @param pos Target buffer offset 2255 | */ 2256 | function writeDoubleLE(val: number, buf: Uint8Array, pos: number): void; 2257 | 2258 | /** 2259 | * Writes a 64 bit double to a buffer using big endian byte order. 2260 | * @param val Value to write 2261 | * @param buf Target buffer 2262 | * @param pos Target buffer offset 2263 | */ 2264 | function writeDoubleBE(val: number, buf: Uint8Array, pos: number): void; 2265 | 2266 | /** 2267 | * Reads a 64 bit double from a buffer using little endian byte order. 2268 | * @param buf Source buffer 2269 | * @param pos Source buffer offset 2270 | * @returns Value read 2271 | */ 2272 | function readDoubleLE(buf: Uint8Array, pos: number): number; 2273 | 2274 | /** 2275 | * Reads a 64 bit double from a buffer using big endian byte order. 2276 | * @param buf Source buffer 2277 | * @param pos Source buffer offset 2278 | * @returns Value read 2279 | */ 2280 | function readDoubleBE(buf: Uint8Array, pos: number): number; 2281 | } 2282 | 2283 | /** 2284 | * Fetches the contents of a file. 2285 | * @param filename File path or url 2286 | * @param options Fetch options 2287 | * @param callback Callback function 2288 | */ 2289 | function fetch(filename: string, options: IFetchOptions, callback: FetchCallback): void; 2290 | 2291 | /** 2292 | * Fetches the contents of a file. 2293 | * @param path File path or url 2294 | * @param callback Callback function 2295 | */ 2296 | function fetch(path: string, callback: FetchCallback): void; 2297 | 2298 | /** 2299 | * Fetches the contents of a file. 2300 | * @param path File path or url 2301 | * @param [options] Fetch options 2302 | * @returns Promise 2303 | */ 2304 | function fetch(path: string, options?: IFetchOptions): Promise<(string | Uint8Array)>; 2305 | 2306 | /** 2307 | * Requires a module only if available. 2308 | * @param moduleName Module to require 2309 | * @returns Required module if available and not empty, otherwise `null` 2310 | */ 2311 | function inquire(moduleName: string): object; 2312 | 2313 | /** A minimal path module to resolve Unix, Windows and URL paths alike. */ 2314 | namespace path { 2315 | 2316 | /** 2317 | * Tests if the specified path is absolute. 2318 | * @param path Path to test 2319 | * @returns `true` if path is absolute 2320 | */ 2321 | function isAbsolute(path: string): boolean; 2322 | 2323 | /** 2324 | * Normalizes the specified path. 2325 | * @param path Path to normalize 2326 | * @returns Normalized path 2327 | */ 2328 | function normalize(path: string): string; 2329 | 2330 | /** 2331 | * Resolves the specified include path against the specified origin path. 2332 | * @param originPath Path to the origin file 2333 | * @param includePath Include path relative to origin path 2334 | * @param [alreadyNormalized=false] `true` if both paths are already known to be normalized 2335 | * @returns Path to the include file 2336 | */ 2337 | function resolve(originPath: string, includePath: string, alreadyNormalized?: boolean): string; 2338 | } 2339 | 2340 | /** 2341 | * A general purpose buffer pool. 2342 | * @param alloc Allocator 2343 | * @param slice Slicer 2344 | * @param [size=8192] Slab size 2345 | * @returns Pooled allocator 2346 | */ 2347 | function pool(alloc: PoolAllocator, slice: PoolSlicer, size?: number): PoolAllocator; 2348 | 2349 | /** A minimal UTF8 implementation for number arrays. */ 2350 | namespace utf8 { 2351 | 2352 | /** 2353 | * Calculates the UTF8 byte length of a string. 2354 | * @param string String 2355 | * @returns Byte length 2356 | */ 2357 | function length(string: string): number; 2358 | 2359 | /** 2360 | * Reads UTF8 bytes as a string. 2361 | * @param buffer Source buffer 2362 | * @param start Source start 2363 | * @param end Source end 2364 | * @returns String read 2365 | */ 2366 | function read(buffer: Uint8Array, start: number, end: number): string; 2367 | 2368 | /** 2369 | * Writes a string as UTF8 bytes. 2370 | * @param string Source string 2371 | * @param buffer Destination buffer 2372 | * @param offset Destination offset 2373 | * @returns Bytes written 2374 | */ 2375 | function write(string: string, buffer: Uint8Array, offset: number): number; 2376 | } 2377 | } 2378 | 2379 | /** 2380 | * Generates a verifier specific to the specified message type. 2381 | * @param mtype Message type 2382 | * @returns Codegen instance 2383 | */ 2384 | export function verifier(mtype: Type): Codegen; 2385 | 2386 | /** Wrappers for common types. */ 2387 | export const wrappers: { [k: string]: IWrapper }; 2388 | 2389 | /** 2390 | * From object converter part of an {@link IWrapper}. 2391 | * @param object Plain object 2392 | * @returns Message instance 2393 | */ 2394 | type WrapperFromObjectConverter = (this: Type, object: { [k: string]: any }) => Message<{}>; 2395 | 2396 | /** 2397 | * To object converter part of an {@link IWrapper}. 2398 | * @param message Message instance 2399 | * @param [options] Conversion options 2400 | * @returns Plain object 2401 | */ 2402 | type WrapperToObjectConverter = (this: Type, message: Message<{}>, options?: IConversionOptions) => { [k: string]: any }; 2403 | 2404 | /** Common type wrapper part of {@link wrappers}. */ 2405 | export interface IWrapper { 2406 | 2407 | /** From object converter */ 2408 | fromObject?: WrapperFromObjectConverter; 2409 | 2410 | /** To object converter */ 2411 | toObject?: WrapperToObjectConverter; 2412 | } 2413 | 2414 | /** Wire format writer using `Uint8Array` if available, otherwise `Array`. */ 2415 | export class Writer { 2416 | 2417 | /** Constructs a new writer instance. */ 2418 | constructor(); 2419 | 2420 | /** Current length. */ 2421 | public len: number; 2422 | 2423 | /** Operations head. */ 2424 | public head: object; 2425 | 2426 | /** Operations tail */ 2427 | public tail: object; 2428 | 2429 | /** Linked forked states. */ 2430 | public states: (object | null); 2431 | 2432 | /** 2433 | * Creates a new writer. 2434 | * @returns A {@link BufferWriter} when Buffers are supported, otherwise a {@link Writer} 2435 | */ 2436 | public static create(): (BufferWriter | Writer); 2437 | 2438 | /** 2439 | * Allocates a buffer of the specified size. 2440 | * @param size Buffer size 2441 | * @returns Buffer 2442 | */ 2443 | public static alloc(size: number): Uint8Array; 2444 | 2445 | /** 2446 | * Writes an unsigned 32 bit value as a varint. 2447 | * @param value Value to write 2448 | * @returns `this` 2449 | */ 2450 | public uint32(value: number): Writer; 2451 | 2452 | /** 2453 | * Writes a signed 32 bit value as a varint. 2454 | * @param value Value to write 2455 | * @returns `this` 2456 | */ 2457 | public int32(value: number): Writer; 2458 | 2459 | /** 2460 | * Writes a 32 bit value as a varint, zig-zag encoded. 2461 | * @param value Value to write 2462 | * @returns `this` 2463 | */ 2464 | public sint32(value: number): Writer; 2465 | 2466 | /** 2467 | * Writes an unsigned 64 bit value as a varint. 2468 | * @param value Value to write 2469 | * @returns `this` 2470 | * @throws {TypeError} If `value` is a string and no long library is present. 2471 | */ 2472 | public uint64(value: (Long | number | string)): Writer; 2473 | 2474 | /** 2475 | * Writes a signed 64 bit value as a varint. 2476 | * @param value Value to write 2477 | * @returns `this` 2478 | * @throws {TypeError} If `value` is a string and no long library is present. 2479 | */ 2480 | public int64(value: (Long | number | string)): Writer; 2481 | 2482 | /** 2483 | * Writes a signed 64 bit value as a varint, zig-zag encoded. 2484 | * @param value Value to write 2485 | * @returns `this` 2486 | * @throws {TypeError} If `value` is a string and no long library is present. 2487 | */ 2488 | public sint64(value: (Long | number | string)): Writer; 2489 | 2490 | /** 2491 | * Writes a boolish value as a varint. 2492 | * @param value Value to write 2493 | * @returns `this` 2494 | */ 2495 | public bool(value: boolean): Writer; 2496 | 2497 | /** 2498 | * Writes an unsigned 32 bit value as fixed 32 bits. 2499 | * @param value Value to write 2500 | * @returns `this` 2501 | */ 2502 | public fixed32(value: number): Writer; 2503 | 2504 | /** 2505 | * Writes a signed 32 bit value as fixed 32 bits. 2506 | * @param value Value to write 2507 | * @returns `this` 2508 | */ 2509 | public sfixed32(value: number): Writer; 2510 | 2511 | /** 2512 | * Writes an unsigned 64 bit value as fixed 64 bits. 2513 | * @param value Value to write 2514 | * @returns `this` 2515 | * @throws {TypeError} If `value` is a string and no long library is present. 2516 | */ 2517 | public fixed64(value: (Long | number | string)): Writer; 2518 | 2519 | /** 2520 | * Writes a signed 64 bit value as fixed 64 bits. 2521 | * @param value Value to write 2522 | * @returns `this` 2523 | * @throws {TypeError} If `value` is a string and no long library is present. 2524 | */ 2525 | public sfixed64(value: (Long | number | string)): Writer; 2526 | 2527 | /** 2528 | * Writes a float (32 bit). 2529 | * @param value Value to write 2530 | * @returns `this` 2531 | */ 2532 | public float(value: number): Writer; 2533 | 2534 | /** 2535 | * Writes a double (64 bit float). 2536 | * @param value Value to write 2537 | * @returns `this` 2538 | */ 2539 | public double(value: number): Writer; 2540 | 2541 | /** 2542 | * Writes a sequence of bytes. 2543 | * @param value Buffer or base64 encoded string to write 2544 | * @returns `this` 2545 | */ 2546 | public bytes(value: (Uint8Array | string)): Writer; 2547 | 2548 | /** 2549 | * Writes a string. 2550 | * @param value Value to write 2551 | * @returns `this` 2552 | */ 2553 | public string(value: string): Writer; 2554 | 2555 | /** 2556 | * Forks this writer's state by pushing it to a stack. 2557 | * Calling {@link Writer#reset|reset} or {@link Writer#ldelim|ldelim} resets the writer to the previous state. 2558 | * @returns `this` 2559 | */ 2560 | public fork(): Writer; 2561 | 2562 | /** 2563 | * Resets this instance to the last state. 2564 | * @returns `this` 2565 | */ 2566 | public reset(): Writer; 2567 | 2568 | /** 2569 | * Resets to the last state and appends the fork state's current write length as a varint followed by its operations. 2570 | * @returns `this` 2571 | */ 2572 | public ldelim(): Writer; 2573 | 2574 | /** 2575 | * Finishes the write operation. 2576 | * @returns Finished buffer 2577 | */ 2578 | public finish(): Uint8Array; 2579 | } 2580 | 2581 | /** Wire format writer using node buffers. */ 2582 | export class BufferWriter extends Writer { 2583 | 2584 | /** Constructs a new buffer writer instance. */ 2585 | constructor(); 2586 | 2587 | /** 2588 | * Finishes the write operation. 2589 | * @returns Finished buffer 2590 | */ 2591 | public finish(): Buffer; 2592 | 2593 | /** 2594 | * Allocates a buffer of the specified size. 2595 | * @param size Buffer size 2596 | * @returns Buffer 2597 | */ 2598 | public static alloc(size: number): Buffer; 2599 | } 2600 | 2601 | /** 2602 | * Callback as used by {@link util.asPromise}. 2603 | * @param error Error, if any 2604 | * @param params Additional arguments 2605 | */ 2606 | type asPromiseCallback = (error: (Error | null), ...params: any[]) => void; 2607 | 2608 | /** 2609 | * Appends code to the function's body or finishes generation. 2610 | * @param [formatStringOrScope] Format string or, to finish the function, an object of additional scope variables, if any 2611 | * @param [formatParams] Format parameters 2612 | * @returns Itself or the generated function if finished 2613 | * @throws {Error} If format parameter counts do not match 2614 | */ 2615 | type Codegen = (formatStringOrScope?: (string | { [k: string]: any }), ...formatParams: any[]) => (Codegen | Function); 2616 | 2617 | /** 2618 | * Event listener as used by {@link util.EventEmitter}. 2619 | * @param args Arguments 2620 | */ 2621 | type EventEmitterListener = (...args: any[]) => void; 2622 | 2623 | /** 2624 | * Node-style callback as used by {@link util.fetch}. 2625 | * @param error Error, if any, otherwise `null` 2626 | * @param [contents] File contents, if there hasn't been an error 2627 | */ 2628 | type FetchCallback = (error: Error, contents?: string) => void; 2629 | 2630 | /** Options as used by {@link util.fetch}. */ 2631 | export interface IFetchOptions { 2632 | 2633 | /** Whether expecting a binary response */ 2634 | binary?: boolean; 2635 | 2636 | /** If `true`, forces the use of XMLHttpRequest */ 2637 | xhr?: boolean; 2638 | } 2639 | 2640 | /** 2641 | * An allocator as used by {@link util.pool}. 2642 | * @param size Buffer size 2643 | * @returns Buffer 2644 | */ 2645 | type PoolAllocator = (size: number) => Uint8Array; 2646 | 2647 | /** 2648 | * A slicer as used by {@link util.pool}. 2649 | * @param start Start offset 2650 | * @param end End offset 2651 | * @returns Buffer slice 2652 | */ 2653 | type PoolSlicer = (this: Uint8Array, start: number, end: number) => Uint8Array; 2654 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import * as child_process from 'child_process'; 2 | import * as fs from 'fs-extra-promise'; 3 | import * as path from 'path'; 4 | import * as UglifyJS from 'uglify-js'; 5 | import * as os from 'os'; 6 | import rimraf = require('rimraf'); 7 | const root = path.resolve(__filename, '../../'); 8 | import * as pbjs from "protobufjs/cli/pbjs" 9 | import * as pbts from "protobufjs/cli/pbts" 10 | function shell(command: string, args: string[]) { 11 | return new Promise((resolve, reject) => { 12 | 13 | const cmd = command + " " + args.join(" "); 14 | child_process.exec(cmd, (error, stdout, stderr) => { 15 | if (error) { 16 | console.error(`检查是否安装了protobufjs`) 17 | reject(error); 18 | 19 | } 20 | else { 21 | resolve(stdout) 22 | } 23 | }) 24 | }) 25 | } 26 | 27 | 28 | type ProtobufConfig = { 29 | 30 | options: { 31 | "no-create": boolean, 32 | "no-verify": boolean, 33 | "no-convert": boolean, 34 | "no-delimited": boolean 35 | }, 36 | outputFileType: number, 37 | dtsOutDir: string, 38 | outFileName: string, 39 | sourceRoot: string, 40 | outputDir: string 41 | 42 | 43 | } 44 | 45 | const pbconfigContent = JSON.stringify({ 46 | options: { 47 | "no-create": false, 48 | "no-verify": false, 49 | "no-convert": true, 50 | "no-delimited": false 51 | }, 52 | outputFileType: 0, 53 | dtsOutDir: "protofile", 54 | outFileName: "proto_bundle", 55 | sourceRoot: "protofile", 56 | outputDir: "bundles" 57 | } as ProtobufConfig, null, '\t'); 58 | process.on('unhandledRejection', (reason, p) => { 59 | console.log('Unhandled Rejection at: Promise', p, 'reason:', reason); 60 | }); 61 | export async function generate(rootDir: string) { 62 | 63 | const pbconfigPath = path.join(rootDir, 'pbconfig.json'); 64 | if (!(await fs.existsAsync(pbconfigPath))) { 65 | if (await fs.existsAsync(path.join(rootDir, 'protobuf'))) { 66 | const pbconfigPath = path.join(rootDir, 'protobuf', 'pbconfig.json') 67 | if (!await (fs.existsAsync(pbconfigPath))) { 68 | await fs.writeFileAsync(pbconfigPath, pbconfigContent, 'utf-8'); 69 | } 70 | await generate(path.join(rootDir, 'protobuf')); 71 | } 72 | else { 73 | throw '请首先执行 pb-egf add 命令' 74 | } 75 | return; 76 | } 77 | const pbconfig: ProtobufConfig = await fs.readJSONAsync(path.join(rootDir, 'pbconfig.json')); 78 | const tempfile = path.join(rootDir, 'pbtemp.js'); 79 | await fs.mkdirpAsync(path.dirname(tempfile)).catch(function (res) { 80 | console.log(res); 81 | }); 82 | await fs.writeFileAsync(tempfile, ""); 83 | 84 | const pbjsFile = path.join(rootDir, pbconfig.outputDir + "/" + pbconfig.outFileName + ".js"); 85 | const dirname = path.dirname(pbjsFile); 86 | await new Promise((res, rej) => { 87 | rimraf(dirname, function () { 88 | res(); 89 | }) 90 | }) 91 | 92 | await fs.mkdirpAsync(dirname).catch(function (res) { 93 | console.log(res); 94 | }); 95 | const protoRoot = path.join(rootDir, pbconfig.sourceRoot); 96 | const fileList = await fs.readdirAsync(protoRoot); 97 | const protoList = fileList.filter(item => path.extname(item) === '.proto') 98 | if (protoList.length == 0) { 99 | throw ' protofile 文件夹中不存在 .proto 文件' 100 | } 101 | await Promise.all(protoList.map(async (protofile) => { 102 | const content = await fs.readFileAsync(path.join(protoRoot, protofile), 'utf-8') 103 | if (content.indexOf('package') == -1) { 104 | throw `${protofile} 中必须包含 package 字段` 105 | } 106 | })) 107 | 108 | const args = ['-t', 'static', '--keep-case', '-p', protoRoot].concat(protoList); 109 | if (pbconfig.options['no-create']) { 110 | args.unshift('--no-create'); 111 | } 112 | if (pbconfig.options['no-verify']) { 113 | args.unshift('--no-verify'); 114 | } 115 | if (pbconfig.options['no-convert']) { 116 | args.unshift('--no-convert') 117 | } 118 | if (pbconfig.options["no-delimited"]) { 119 | args.unshift("--no-delimited") 120 | } 121 | console.log("[egf-protobuf]解析proto文件"); 122 | // await shell('./node_modules/protobufjs/bin/pbjs', args).catch(function (res) { 123 | // console.log(res); 124 | // }); 125 | // let pbjsResult = await fs.readFileAsync(tempfile, 'utf-8').catch(function (res) { console.log(res) }); 126 | 127 | 128 | const pbjsResult = await new Promise((res) => { 129 | pbjs.main(args, (err, output) => { 130 | if (err) { 131 | console.error(err); 132 | } 133 | res(output); 134 | return {} 135 | }) 136 | }) 137 | let pbjsLib = await fs.readFileAsync(path.join(root, 'pblib/protobuf-library.min.js')).catch(function (res) { console.log(res) }); 138 | let outPbj = pbjsLib + 'var $protobuf = window.protobuf;\n$protobuf.roots.default=window;\n' + pbjsResult; 139 | console.log("[egf-protobuf]解析proto文件->完成" + pbjsFile); 140 | if (pbconfig.outputFileType === 0 || pbconfig.outputFileType === 1) { 141 | console.log("[egf-protobuf]生成js文件"); 142 | await fs.writeFileAsync(pbjsFile, outPbj, 'utf-8').catch(function (res) { console.log(res) });; 143 | console.log("[egf-protobuf]生成js文件->完成"); 144 | } 145 | if (pbconfig.outputFileType === 0 || pbconfig.outputFileType === 2) { 146 | console.log("[egf-protobuf]生成.min.js文件"); 147 | const minjs = UglifyJS.minify(outPbj); 148 | await fs.writeFileAsync(pbjsFile.replace('.js', '.min.js'), minjs.code, 'utf-8'); 149 | console.log("[egf-protobuf]生成.min.js文件->完成"); 150 | } 151 | console.log("[egf-protobuf]解析js文件生成.d.ts中"); 152 | // await shell('pbts', ['--main', output, '-o', tempfile]).catch(function (res) { 153 | // console.error(`检查是否安装了protobufjs`) 154 | // console.log(res); 155 | // }); 156 | // let pbtsResult: string = await fs.readFileAsync(tempfile, 'utf-8').catch(function (res) { console.log(res) }) as any; 157 | 158 | let pbtsResult = await new Promise((res) => { 159 | pbts.main(['--main', pbjsFile], (err, output) => { 160 | if (err) { 161 | console.error(err); 162 | } 163 | res(output); 164 | return {} 165 | }) 166 | }) 167 | // pbtsResult = await fs.readFileAsync(tempfile, 'utf-8').catch(function (res) { console.log(res) }) as any; 168 | 169 | pbtsResult = pbtsResult.replace(/\$protobuf/gi, "protobuf").replace(/export namespace/gi, 'declare namespace'); 170 | pbtsResult = 'type Long = protobuf.Long;\n' + pbtsResult; 171 | let dtsOut = path.join(rootDir, pbconfig.dtsOutDir, pbconfig.outFileName + ".d.ts"); 172 | console.log("[egf-protobuf]解析js文件->完成"); 173 | const isExit_dts = await fs.existsAsync(dtsOut); 174 | if (isExit_dts) { 175 | console.log(`[egf-protobuf]删除旧.d.ts文件:${dtsOut}`); 176 | await new Promise((res, rej) => { 177 | rimraf(dtsOut, function () { 178 | res(); 179 | }) 180 | }) 181 | } 182 | const dtsOutDirPath = path.dirname(dtsOut) 183 | if (rootDir !== dtsOutDirPath) { 184 | const isExit_dtsOutDir = await fs.existsAsync(dtsOutDirPath); 185 | if (!isExit_dtsOutDir) { 186 | // 187 | console.log(`[egf-protobuf]创建.d.ts 的文件夹:${dtsOutDirPath}->`); 188 | await fs.mkdirAsync(dtsOutDirPath); 189 | } 190 | } 191 | console.log("[egf-protobuf]生成.d.ts文件->"); 192 | await fs.writeFileAsync(dtsOut, pbtsResult, 'utf-8').catch(function (res) { console.log(res) });; 193 | console.log("[egf-protobuf]生成.d.ts文件->完成"); 194 | await fs.removeAsync(tempfile).catch(function (res) { console.log(res) });; 195 | 196 | } 197 | 198 | 199 | 200 | export async function initProj(projRoot: string = ".", projType: string) { 201 | console.log('正在将 protobuf 源码拷贝至项目中...'); 202 | await fs.copyAsync(path.join(root, 'pblib'), path.join(projRoot, 'protobuf/library')).catch(function (res) { console.log(res) });; 203 | await fs.mkdirpSync(path.join(projRoot, 'protobuf/protofile')); 204 | await fs.mkdirpSync(path.join(projRoot, 'protobuf/bundles')); 205 | await fs.writeFileAsync((path.join(projRoot, 'protobuf/pbconfig.json')), pbconfigContent, 'utf-8').catch(function (res) { console.log(res) });; 206 | if (projType === "egret") { 207 | const egretPropertiesPath = path.join(projRoot, 'egretProperties.json'); 208 | if (await fs.existsAsync(egretPropertiesPath)) { 209 | console.log('正在将 protobuf 添加到 egretProperties.json 中...'); 210 | const egretProperties = await fs.readJSONAsync(egretPropertiesPath); 211 | egretProperties.modules.push({ name: 'protobuf-library', path: 'protobuf/library' }); 212 | egretProperties.modules.push({ name: 'protobuf-bundles', path: 'protobuf/bundles' }); 213 | await fs.writeFileAsync(path.join(projRoot, 'egretProperties.json'), JSON.stringify(egretProperties, null, '\t\t')); 214 | console.log('正在将 protobuf 添加到 tsconfig.json 中...'); 215 | const tsconfig = await fs.readJSONAsync(path.join(projRoot, 'tsconfig.json')); 216 | tsconfig.include.push('protobuf/**/*.d.ts'); 217 | await fs.writeFileAsync(path.join(projRoot, 'tsconfig.json'), JSON.stringify(tsconfig, null, '\t\t')).catch(function (res) { console.log(res) });; 218 | } 219 | else { 220 | console.log('输入的文件夹不是白鹭引擎项目') 221 | } 222 | } 223 | 224 | 225 | 226 | } 227 | 228 | -------------------------------------------------------------------------------- /test/protobuf/pbconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "options": { 3 | "no-create": false, 4 | "no-verify": false, 5 | "no-convert": false, 6 | "no-delimited": true 7 | }, 8 | "outputFileType": 0, 9 | "dtsOutDir": "dts", 10 | "outFileName": "proto_bundle", 11 | "sourceRoot": "protofile", 12 | "outputDir": "bundles" 13 | } -------------------------------------------------------------------------------- /test/protobuf/protofile/df_1000.proto: -------------------------------------------------------------------------------- 1 | package doomsday_pt; 2 | 3 | message ResData{ 4 | required uint32 result = 1; // 结果值 5 | repeated string param = 2; // 消息参数 6 | } 7 | 8 | 9 | message DrawAward{ 10 | required uint32 award_type = 1; // 奖励的类型,1-货币,2-物品, 3-经验 11 | required uint32 award_id = 2; // 货币的类型, 如:1:钻石 2:金币 12 | required uint32 award_num = 3; // 获得的数量 13 | } 14 | 15 | message HeroExpMsg{ 16 | required uint32 hero_id = 1; // 英雄配置表id 17 | required uint32 grade = 2; // 当前英雄的等级 18 | required uint32 exp = 3; // 当前英雄的经验 19 | } 20 | 21 | message BattleAward { 22 | repeated DrawAward draw_award = 1; // 战斗胜利奖励的物品列表 23 | repeated HeroExpMsg hero_exp_msg = 2; // 战斗胜利奖励的物品列表 24 | } 25 | 26 | message GetGoods{ 27 | required uint32 tab_id = 1;//物品在表格中的id 28 | required uint32 number = 2;//物品的数量 29 | } 30 | 31 | 32 | 33 | message Cs_10000001{ 34 | required string mg_name = 1; // GM指令名称 35 | required uint32 id = 2; // 指令id 36 | required int32 num = 3; // 指令数量 37 | } 38 | 39 | message Sc_10000001{ 40 | required ResData res = 1; // 结果 41 | } -------------------------------------------------------------------------------- /test/protobuf/protofile/df_1001.proto: -------------------------------------------------------------------------------- 1 | import "df_1000.proto"; 2 | package doomsday_pt; 3 | 4 | // 角色数据,(暂定这些数据,以后再加) 5 | message Pt_HeroMsg{ 6 | required uint64 hero_id = 1; // 角色表的配置ID 7 | required uint32 index_id = 2; // 角色获取的的序列id 8 | required uint32 grade = 3; // 等级 9 | } 10 | 11 | message Pt_RoleInfo{ 12 | required uint64 role_id = 1; // 玩家id 13 | required string nickname = 2; // 昵称 14 | required uint64 exp_pool = 3; // 经验 15 | required uint32 vip_grade = 4; // vip等级 16 | required uint32 vip_exp = 5; // vip 经验 17 | required uint32 gold_coin = 6; // 金币 18 | required uint32 diamond = 7; // 钻石 19 | required uint32 fighting = 8; // 战斗力 20 | repeated Pt_HeroMsg hero_list = 9; // 角色列表 21 | } 22 | 23 | 24 | // 属性 25 | message Pt_Currency{ 26 | required uint32 exp_pool = 1; // 经验 27 | required uint32 vip_grade = 2; // vip等级 28 | required uint32 vip_exp = 3; // vip 经验 29 | required uint32 gold_coin = 4; // 金币 30 | required uint32 diamond = 5; // 钻石 31 | required uint32 fighting = 6; // 战斗力 32 | } 33 | 34 | 35 | 36 | // 登陆 37 | message Cs_10010001{ 38 | required uint32 account_id = 1; // 用户登陆名 39 | required string token = 2; // token 40 | } 41 | 42 | message Sc_10010001{ 43 | required ResData res = 1; 44 | optional Pt_RoleInfo role_info = 2; 45 | } 46 | 47 | 48 | 49 | // 创建角色 50 | message Cs_10010002{ 51 | required uint32 account_id = 1; // 用户登陆名 52 | required string token = 2; // token 53 | required string nickname = 3; // 昵称 54 | required uint32 hero_id = 4; // 角色Id 55 | } 56 | 57 | message Sc_10010002{ 58 | required ResData res = 1; 59 | } 60 | 61 | 62 | // 心跳包 63 | message Cs_10010003{ 64 | optional uint32 rand = 1; // 值随便发 65 | } 66 | 67 | message Sc_10010003{ 68 | required uint32 interval = 1; // 间隔时间 69 | } 70 | 71 | 72 | // 服务器推送错误码 73 | message Sc_10010004{ 74 | required ResData res = 1; 75 | } 76 | 77 | 78 | // 更新货币与等级经验 79 | message Sc_10010005{ 80 | required Pt_Currency currency = 1; 81 | } 82 | 83 | 84 | -------------------------------------------------------------------------------- /test/protobuf/protofile/df_1002.proto: -------------------------------------------------------------------------------- 1 | import "df_1000.proto"; 2 | package doomsday_pt; 3 | 4 | 5 | // 角色面板-属性列表 6 | message Pt_AttList{ 7 | required uint32 att_id = 1; // 角色序号 8 | required uint32 att_value = 2; // 属性值 9 | } 10 | 11 | // 角色面板 12 | message Pt_HeroPanel{ 13 | required uint64 power = 1; // 战力 14 | required string heroname = 2; // 角色昵称 15 | } 16 | 17 | // 经验面板-角色基本信息 18 | message Pt_HeroInfo{ 19 | required uint64 hero_id = 1; // 角色id 20 | required uint32 index_id = 2; // 角色序号 21 | required string heroname = 3; // 角色昵称 22 | required uint64 exper = 4; // 角色经验值 23 | required uint32 grade = 5; // 角色等级 24 | } 25 | 26 | 27 | 28 | 29 | // 角色详细属性 30 | message Cs_10020001{ 31 | required uint64 hero_id = 1; // 角色id 32 | } 33 | 34 | message Sc_10020001{ 35 | repeated Pt_AttList att_list = 1; // 角色属性列表 36 | } 37 | 38 | 39 | 40 | // 角色面板 41 | message Cs_10020002{ 42 | required uint64 hero_id = 1; // 角色id 43 | } 44 | 45 | 46 | message Sc_10020002{ 47 | required Pt_HeroPanel HeroPanel = 1; //角色面板 48 | repeated Pt_AttList att_list = 2; // 角色属性列表 49 | } 50 | 51 | 52 | // 经验面板 53 | message Cs_10020003{ 54 | optional uint32 rand = 1; // 值不用发 55 | } 56 | 57 | message Sc_10020003{ 58 | required uint64 exper_pool = 1; // 经验池的值 0-未开启 或已用完 59 | repeated Pt_HeroInfo hero_list = 2; // 角色列表 60 | } 61 | 62 | // 经验面板-角色升级 63 | message Cs_10020004{ 64 | required uint64 hero_id = 1; // 角色id 65 | } 66 | 67 | message Sc_10020004{ 68 | required ResData res = 1; // 错误码信息 69 | required Pt_HeroInfo hero_info = 2; // 单个角色信息 70 | } 71 | 72 | -------------------------------------------------------------------------------- /test/protobuf/protofile/df_1003.proto: -------------------------------------------------------------------------------- 1 | import "df_1000.proto"; 2 | package doomsday_pt; 3 | 4 | 5 | message Pt_GoodsMsg { 6 | required uint64 id = 1; // 唯一id 7 | required uint32 base_id = 2; // 物品配置表id 8 | required uint32 num = 3; // 数量 9 | optional uint32 valid_time = 4; // 到期时间戳 10 | } 11 | 12 | 13 | // 上线时推送背包列表和仓库列表 14 | message Sc_10030001{ 15 | required uint32 now_time = 1; // 当前时间戳 16 | repeated Pt_GoodsMsg bag_msg = 2; // 背包物品列表 17 | repeated Pt_GoodsMsg entrepot_msg = 3; // 仓库物品列表 18 | } 19 | 20 | 21 | // 当物品数据发生改变时推送的物品列表 22 | message Sc_10030002{ 23 | required uint32 location = 1; // 1-背包,2-仓库 24 | repeated Pt_GoodsMsg goods_msg = 2; // 背包物品列表 25 | } 26 | 27 | 28 | // 出售 29 | message Cs_10030003{ 30 | required uint64 id = 1; // 物品唯一id 31 | required uint32 num = 2; // 出售的数量 32 | } 33 | 34 | message Sc_10030003{ 35 | required ResData res = 1; // 出售成功后也会推送10030002协议 36 | } 37 | 38 | 39 | 40 | // 放入仓库 41 | message Cs_10030004{ 42 | required uint64 id = 1; // 物品唯一id 43 | } 44 | 45 | message Sc_10030004{ 46 | required ResData res = 1; // 出售成功后也会推送10030002协议 47 | } 48 | 49 | 50 | 51 | // 背包整理 52 | message Cs_10030005{ 53 | optional uint32 id = 1; // 不要传 54 | } 55 | 56 | message Sc_10030005{ 57 | required ResData res = 1; // 出售成功后也会推送10030002协议 58 | } 59 | 60 | 61 | // 仓库取出放入背包 62 | message Cs_10030006{ 63 | required uint64 id = 1; // 物品唯一id 64 | } 65 | 66 | message Sc_10030006{ 67 | required ResData res = 1; // 出售成功后也会推送10030002协议 68 | } 69 | 70 | 71 | // 礼包的使用 72 | message Cs_10030007{ 73 | required uint64 id = 1; // 物品唯一id 74 | required uint32 use_num = 2; // 使用的数量 75 | optional uint32 select_id = 3; // N选1时,用户选择的物品id 76 | } 77 | 78 | message Sc_10030007{ 79 | required ResData res = 1; // 出售成功后也会推送10030002协议 80 | } -------------------------------------------------------------------------------- /test/protobuf/protofile/df_1004.proto: -------------------------------------------------------------------------------- 1 | 2 | package doomsday_pt; 3 | //位置 4 | message Pt_Pos { 5 | required float x = 1; 6 | required float y = 2; 7 | } 8 | 9 | message Cs_10040001 { 10 | required uint32 id = 1; // 不要传 11 | } 12 | message Sc_10040001{ 13 | repeated uint32 id_list = 1; // 角色id列表 14 | } 15 | 16 | //请求计算站位点 17 | message Cs_10040002 { 18 | required uint32 atk_id = 1;//攻击者id 19 | required uint32 tar_id = 2;//目标id 20 | 21 | } 22 | //返回站位点 23 | message Sc_10040002 { 24 | optional Pt_Pos tar_pos = 1; 25 | } 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /test/protobuf/protofile/df_1005.proto: -------------------------------------------------------------------------------- 1 | import "df_1000.proto"; 2 | package doomsday_pt; 3 | 4 | 5 | // 道具列表 6 | message Pt_GoodsList{ 7 | required uint32 goods_id = 1; // 道具id 8 | required uint32 goods_bought = 2; // 道具已购买数量 9 | } 10 | 11 | // 刷新道具 12 | message Pt_Goodsfresh{ 13 | required uint32 goods_id = 1; // 道具id 14 | required uint32 goods_num = 2; // 拥有道具数量 15 | } 16 | 17 | // 商店信息 18 | message Cs_10050001{ 19 | required uint32 shop_type = 1 ; // 商店类型 20 | required uint32 shop_lv = 2 ; // 商店等级 21 | } 22 | 23 | message Sc_10050001{ 24 | repeated Pt_GoodsList goods_list = 1; // 道具列表 25 | required Pt_Goodsfresh goods_fresh = 2; // 刷新道具 26 | } 27 | 28 | // 购买物品 29 | message Cs_10050002{ 30 | required uint32 shop_type = 1 ; // 商店类型 31 | required uint32 shop_lv = 2 ; // 商店等级 32 | required uint32 goods_id = 3 ; // 物品id 33 | } 34 | 35 | message Sc_10050002{ 36 | required ResData res = 1; // 错误码信息 37 | required Pt_GoodsList goods_list = 2; // 购买物品的信息 38 | } 39 | 40 | 41 | // 刷新商店 42 | message Cs_10050003{ 43 | required uint32 shop_type = 1 ; // 商店类型 44 | required uint32 shop_lv = 2 ; // 商店等级 45 | required uint32 fresh_goodsid = 3 ; // 刷新工具id 46 | } 47 | 48 | message Sc_10050003{ 49 | required ResData res = 1; // 错误码信息 50 | repeated Pt_GoodsList goods_list = 2; // 道具列表 51 | } 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | /* Basic Options */ 4 | "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'. */ 5 | "module": "commonjs", /* Specify module code generation: 'commonjs', 'amd', 'system', 'umd' or 'es2015'. */ 6 | "lib": [ 7 | "es2015", 8 | "dom" 9 | ], /* Specify library files to be included in the compilation: */ 10 | // "allowJs": true, /* Allow javascript files to be compiled. */ 11 | // "checkJs": true, /* Report errors in .js files. */ 12 | // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ 13 | // "declaration": true, /* Generates corresponding '.d.ts' file. */ 14 | "sourceMap": true, /* Generates corresponding '.map' file. */ 15 | // "outFile": "./", /* Concatenate and emit output to single file. */ 16 | "outDir": "./out", /* Redirect output structure to the directory. */ 17 | // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ 18 | // "removeComments": true, /* Do not emit comments to output. */ 19 | // "noEmit": true, /* Do not emit outputs. */ 20 | // "importHelpers": true, /* Import emit helpers from 'tslib'. */ 21 | // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ 22 | // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ 23 | /* Strict Type-Checking Options */ 24 | // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ 25 | // "strictNullChecks": true, /* Enable strict null checks. */ 26 | // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ 27 | // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ 28 | /* Additional Checks */ 29 | // "noUnusedLocals": true, /* Report errors on unused locals. */ 30 | // "noUnusedParameters": true, /* Report errors on unused parameters. */ 31 | // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ 32 | // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ 33 | /* Module Resolution Options */ 34 | // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ 35 | // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ 36 | // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ 37 | // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ 38 | // "typeRoots": [], /* List of folders to include type definitions from. */ 39 | // "types": [], /* Type declaration files to be included in compilation. */ 40 | // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ 41 | /* Source Map Options */ 42 | // "sourceRoot": "./", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ 43 | // "mapRoot": "./", /* Specify the location where debugger should locate map files instead of generated locations. */ 44 | // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ 45 | // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ 46 | /* Experimental Options */ 47 | // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ 48 | // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ 49 | }, 50 | "exclude": [ 51 | "egret-project", 52 | "node_modules", 53 | "test" 54 | ] 55 | } --------------------------------------------------------------------------------