neoproto 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/.github/workflows/create-release.yml +70 -0
  2. package/.nvmrc +1 -0
  3. package/dist/cli.d.ts +24 -0
  4. package/dist/cli.d.ts.map +1 -0
  5. package/dist/cli.js +99 -0
  6. package/dist/cli.js.map +1 -0
  7. package/dist/generators/serialization.d.ts +21 -0
  8. package/dist/generators/serialization.d.ts.map +1 -0
  9. package/{src/generators/serialization.ts → dist/generators/serialization.js} +53 -78
  10. package/dist/generators/serialization.js.map +1 -0
  11. package/dist/generators/test.d.ts +10 -0
  12. package/dist/generators/test.d.ts.map +1 -0
  13. package/dist/generators/test.js +170 -0
  14. package/dist/generators/test.js.map +1 -0
  15. package/dist/generators/traits.d.ts +10 -0
  16. package/dist/generators/traits.d.ts.map +1 -0
  17. package/{src/generators/traits.ts → dist/generators/traits.js} +67 -88
  18. package/dist/generators/traits.js.map +1 -0
  19. package/dist/generators/types.d.ts +9 -0
  20. package/dist/generators/types.d.ts.map +1 -0
  21. package/dist/generators/types.js +154 -0
  22. package/dist/generators/types.js.map +1 -0
  23. package/dist/generators/unwrap.d.ts +21 -0
  24. package/dist/generators/unwrap.d.ts.map +1 -0
  25. package/dist/generators/unwrap.js +253 -0
  26. package/dist/generators/unwrap.js.map +1 -0
  27. package/dist/generators/wrap.d.ts +15 -0
  28. package/dist/generators/wrap.d.ts.map +1 -0
  29. package/dist/generators/wrap.js +253 -0
  30. package/dist/generators/wrap.js.map +1 -0
  31. package/dist/index.d.ts +2 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/index.js +79 -0
  34. package/dist/index.js.map +1 -0
  35. package/dist/pbcli.d.ts +13 -0
  36. package/dist/pbcli.d.ts.map +1 -0
  37. package/dist/pbcli.js +49 -0
  38. package/dist/pbcli.js.map +1 -0
  39. package/dist/utils/array-manipulation.d.ts +7 -0
  40. package/dist/utils/array-manipulation.d.ts.map +1 -0
  41. package/dist/utils/array-manipulation.js +22 -0
  42. package/dist/utils/array-manipulation.js.map +1 -0
  43. package/dist/utils/associations.d.ts +15 -0
  44. package/dist/utils/associations.d.ts.map +1 -0
  45. package/{src/utils/associations.ts → dist/utils/associations.js} +24 -28
  46. package/dist/utils/associations.js.map +1 -0
  47. package/{src/utils/comments.ts → dist/utils/comments.d.ts} +2 -11
  48. package/dist/utils/comments.d.ts.map +1 -0
  49. package/dist/utils/comments.js +18 -0
  50. package/dist/utils/comments.js.map +1 -0
  51. package/{src/utils/logger.ts → dist/utils/logger.d.ts} +3 -12
  52. package/dist/utils/logger.d.ts.map +1 -0
  53. package/dist/utils/logger.js +25 -0
  54. package/dist/utils/logger.js.map +1 -0
  55. package/{src/utils/protobuf.ts → dist/utils/protobuf.d.ts} +7 -42
  56. package/dist/utils/protobuf.d.ts.map +1 -0
  57. package/dist/utils/protobuf.js +71 -0
  58. package/dist/utils/protobuf.js.map +1 -0
  59. package/{src/utils/string-manipulation.ts → dist/utils/string-manipulation.d.ts} +4 -11
  60. package/dist/utils/string-manipulation.d.ts.map +1 -0
  61. package/dist/utils/string-manipulation.js +30 -0
  62. package/dist/utils/string-manipulation.js.map +1 -0
  63. package/dist/utils/system.d.ts +17 -0
  64. package/dist/utils/system.d.ts.map +1 -0
  65. package/dist/utils/system.js +49 -0
  66. package/dist/utils/system.js.map +1 -0
  67. package/package.json +8 -5
  68. package/.github/workflows/npm-publish.yml +0 -23
  69. package/data/enum.proto +0 -36
  70. package/data/example.proto +0 -42
  71. package/patches/protobufjs-cli+2.0.0.patch +0 -10
  72. package/src/generators/test.ts +0 -206
  73. package/src/generators/types.ts +0 -191
  74. package/src/generators/unwrap.ts +0 -333
  75. package/src/generators/wrap.ts +0 -334
  76. package/src/index.ts +0 -249
  77. package/src/pbcli.ts +0 -49
  78. package/src/utils/array-manipulation.ts +0 -19
  79. package/src/utils/system.ts +0 -54
  80. package/tsconfig.json +0 -26
@@ -0,0 +1,70 @@
1
+ name: Create Release
2
+
3
+ on:
4
+ workflow_dispatch:
5
+ inputs:
6
+ version_type:
7
+ description: 'Level: patch, minor, or major'
8
+ required: true
9
+ default: 'patch'
10
+ type: choice
11
+ options:
12
+ - patch
13
+ - minor
14
+ - major
15
+
16
+ permissions:
17
+ id-token: write
18
+ contents: write
19
+
20
+ jobs:
21
+ create-release:
22
+ runs-on: ubuntu-latest
23
+ steps:
24
+
25
+ - name: Checkout Code
26
+ uses: actions/checkout@v4
27
+ with:
28
+ fetch-depth: 0
29
+
30
+ - name: Setup Node.js
31
+ uses: actions/setup-node@v4
32
+ with:
33
+ node-version-file: .nvmrc
34
+ registry-url: https://registry.npmjs.org/
35
+
36
+ - name: Configure Git
37
+ run: |
38
+ git config user.name "github-actions[bot]"
39
+ git config user.email "github-actions[bot]@users.noreply.github.com"
40
+
41
+ - name: Bump Version
42
+ id: bump_version
43
+ run: |
44
+ VERSION=$(npm version ${{ github.event.inputs.version_type }} -m "chore: bump version to %s")
45
+ echo "VERSION=$VERSION" >> $GITHUB_ENV
46
+
47
+ - run: npm ci
48
+
49
+ - run: npm run build
50
+
51
+ - run: npm test
52
+
53
+ - name: Push Changes
54
+ run: git push origin ${{ github.ref_name }} --follow-tags
55
+
56
+ - name: Create GitHub Release
57
+ uses: actions/github-script@v8
58
+ with:
59
+ github-token: ${{ secrets.GITHUB_TOKEN }}
60
+ script: | # javascript
61
+ const version = 'v${{ steps.bump_version.outputs.VERSION }}';
62
+ await github.rest.repos.createRelease({
63
+ owner: context.repo.owner,
64
+ repo: context.repo.repo,
65
+ tag_name: version,
66
+ name: version,
67
+ generate_release_notes: true,
68
+ });
69
+
70
+ - run: npm publish
package/.nvmrc ADDED
@@ -0,0 +1 @@
1
+ v24
package/dist/cli.d.ts ADDED
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Options needed to run the main function of the CLI tool.
3
+ */
4
+ interface Options {
5
+ /** The output directory where generated files will be saved. */
6
+ outDir: string;
7
+ /** The path to the .proto file to process. */
8
+ protoPath: string;
9
+ /** The output directory where test files will be saved. */
10
+ testDir?: string;
11
+ /** Whether to forcibly overwrite existing output directories. */
12
+ flagForce?: boolean;
13
+ /** Whether to skip formatting generated files with Prettier. */
14
+ flagNoPrettier?: boolean;
15
+ }
16
+ /**
17
+ * The main function that orchestrates the generation of serialization functions, traits, types, and tests based on a .proto file.
18
+ * @param options - An object containing the necessary options to run the function, including output directory, .proto file path, test directory, and flags for force and Prettier.
19
+ * @returns A promise that resolves when all tasks are completed, or rejects if any errors occur during the process.
20
+ * @throws If the output directory or test directory already exists and the --force flag is not provided, the function will log an error and exit the process.
21
+ */
22
+ declare function run({ outDir, protoPath, testDir, flagForce, flagNoPrettier, }: Options): Promise<void>;
23
+ export { run };
24
+ //# sourceMappingURL=cli.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":"AAmCA;;GAEG;AACH,UAAU,OAAO;IACf,gEAAgE;IAChE,MAAM,EAAE,MAAM,CAAC;IAEf,8CAA8C;IAC9C,SAAS,EAAE,MAAM,CAAC;IAElB,2DAA2D;IAC3D,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB,iEAAiE;IACjE,SAAS,CAAC,EAAE,OAAO,CAAC;IAEpB,gEAAgE;IAChE,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B;AAED;;;;;GAKG;AACH,iBAAe,GAAG,CAAC,EACjB,MAAM,EACN,SAAS,EACT,OAAO,EACP,SAAS,EACT,cAAc,GACf,EAAE,OAAO,iBAqFT;AAED,OAAO,EAAE,GAAG,EAAE,CAAC"}
package/dist/cli.js ADDED
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.run = run;
7
+ const node_fs_1 = require("node:fs");
8
+ const promises_1 = require("node:fs/promises");
9
+ const pbcli_js_1 = require("./pbcli.js");
10
+ const serialization_js_1 = require("./generators/serialization.js");
11
+ const types_js_1 = require("./generators/types.js");
12
+ const traits_js_1 = require("./generators/traits.js");
13
+ const wrap_js_1 = require("./generators/wrap.js");
14
+ const unwrap_js_1 = require("./generators/unwrap.js");
15
+ const associations_js_1 = require("./utils/associations.js");
16
+ const protobuf_js_1 = require("./utils/protobuf.js");
17
+ const logger_js_1 = require("./utils/logger.js");
18
+ const prettier_1 = __importDefault(require("prettier"));
19
+ const test_js_1 = require("./generators/test.js");
20
+ const system_js_1 = require("./utils/system.js");
21
+ const node_process_1 = __importDefault(require("node:process"));
22
+ /**
23
+ * Formats all .ts files in the specified directory using Prettier.
24
+ * @param directory - The directory containing the .ts files to format.
25
+ * @returns A promise that resolves when all files have been formatted.
26
+ */
27
+ async function runPrettier(directory) {
28
+ const tsFiles = await (0, promises_1.readdir)(directory);
29
+ const tsFilePaths = tsFiles
30
+ .filter((file) => file.endsWith(".ts"))
31
+ .map((file) => `${directory}/${file}`);
32
+ for (const filePath of tsFilePaths) {
33
+ const input = await (0, promises_1.readFile)(filePath, "utf-8");
34
+ const output = await prettier_1.default.format(input, { parser: "typescript" });
35
+ await (0, promises_1.writeFile)(filePath, output);
36
+ console.log(`Formatted ${filePath} with Prettier`);
37
+ }
38
+ }
39
+ /**
40
+ * The main function that orchestrates the generation of serialization functions, traits, types, and tests based on a .proto file.
41
+ * @param options - An object containing the necessary options to run the function, including output directory, .proto file path, test directory, and flags for force and Prettier.
42
+ * @returns A promise that resolves when all tasks are completed, or rejects if any errors occur during the process.
43
+ * @throws If the output directory or test directory already exists and the --force flag is not provided, the function will log an error and exit the process.
44
+ */
45
+ async function run({ outDir, protoPath, testDir, flagForce, flagNoPrettier, }) {
46
+ if ((0, node_fs_1.existsSync)(outDir)) {
47
+ if (!flagForce) {
48
+ (0, logger_js_1.logError)(`Output directory ${outDir} already exists. Use --force or -f to overwrite.`);
49
+ node_process_1.default.exit(1);
50
+ }
51
+ await (0, promises_1.rm)(outDir, { force: true, recursive: true });
52
+ }
53
+ await (0, promises_1.mkdir)(`${outDir}/protobuf`, { recursive: true });
54
+ if (testDir && (0, node_fs_1.existsSync)(testDir)) {
55
+ if (!flagForce) {
56
+ (0, logger_js_1.logError)(`Test directory ${testDir} already exists. Use --force or -f to overwrite.`);
57
+ node_process_1.default.exit(1);
58
+ }
59
+ await (0, promises_1.rm)(testDir, { force: true, recursive: true });
60
+ }
61
+ const { namespace } = await (0, pbcli_js_1.runProtobufjsCli)(protoPath, `${outDir}/protobuf`);
62
+ const associations = (0, associations_js_1.associateMessages)((0, protobuf_js_1.getMessages)(namespace));
63
+ await (0, promises_1.writeFile)(`${outDir}/serialization.ts`, (0, serialization_js_1.createNamespaceSerializers)(namespace));
64
+ console.log(`Wrote serialization functions to ${outDir}/serialization.ts`);
65
+ await (0, promises_1.writeFile)(`${outDir}/wrap.ts`, (0, wrap_js_1.createRootNamespaceWrappers)(namespace));
66
+ console.log(`Wrote wrapper functions to ${outDir}/wrap.ts`);
67
+ await (0, promises_1.writeFile)(`${outDir}/unwrap.ts`, (0, unwrap_js_1.createRootNamespaceUnwrappers)(namespace));
68
+ console.log(`Wrote unwrapper functions to ${outDir}/unwrap.ts`);
69
+ await (0, promises_1.writeFile)(`${outDir}/types.ts`, (0, types_js_1.createRootNamespaceTypes)(namespace));
70
+ console.log(`Wrote TypeScript interfaces to ${outDir}/types.ts`);
71
+ await (0, promises_1.writeFile)(`${outDir}/traits.ts`, (0, traits_js_1.createNamespaceTraits)(namespace, associations));
72
+ console.log(`Wrote traits to ${outDir}/traits.ts`);
73
+ await (0, promises_1.writeFile)(`${outDir}/index.ts`, `export * from "./serialization.js";
74
+ export * from "./traits.js";
75
+ export * from "./types.js";
76
+ export * from "./unwrap.js";
77
+ export * from "./wrap.js";
78
+ `);
79
+ console.log(`Wrote index file to ${outDir}/index.ts`);
80
+ if (testDir) {
81
+ await (0, promises_1.mkdir)(testDir, { recursive: true });
82
+ await (0, promises_1.writeFile)(`${testDir}/serialization.spec.ts`, (0, test_js_1.generateNamespaceTests)(namespace, (0, system_js_1.getRelativePath)(testDir, outDir)));
83
+ console.log(`Wrote unit test file to ${testDir}/serialization.spec.ts`);
84
+ if (testDir !== outDir && !flagNoPrettier)
85
+ await runPrettier(testDir);
86
+ }
87
+ if (!flagNoPrettier)
88
+ await runPrettier(outDir);
89
+ if (!node_process_1.default.argv.includes("--no-test-execution") && testDir) {
90
+ console.log("\nRunning generated tests...\n");
91
+ try {
92
+ await (0, system_js_1.executeCommand)(node_process_1.default.argv[0] || "node", ["--import tsx --test", `${testDir}/serialization.spec.ts`], node_process_1.default.cwd());
93
+ }
94
+ catch (err) {
95
+ (0, logger_js_1.logError)("Generated unit tests failed.", "");
96
+ }
97
+ }
98
+ }
99
+ //# sourceMappingURL=cli.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";;;;;AA0JS,kBAAG;AA1JZ,qCAAqC;AACrC,+CAA2E;AAC3E,yCAA8C;AAC9C,oEAA2E;AAC3E,oDAAiE;AACjE,sDAA+D;AAC/D,kDAAmE;AACnE,sDAAuE;AACvE,6DAA4D;AAC5D,qDAAkD;AAClD,iDAA6C;AAC7C,wDAAgC;AAChC,kDAA8D;AAC9D,iDAAoE;AACpE,gEAAmC;AAEnC;;;;GAIG;AACH,KAAK,UAAU,WAAW,CAAC,SAAiB;IAC1C,MAAM,OAAO,GAAG,MAAM,IAAA,kBAAO,EAAC,SAAS,CAAC,CAAC;IACzC,MAAM,WAAW,GAAG,OAAO;SACxB,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;SACtC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,GAAG,SAAS,IAAI,IAAI,EAAE,CAAC,CAAC;IAEzC,KAAK,MAAM,QAA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K,EAAC,OAAO,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QAC1C,MAAM,IAAA,oBAAS,EACb,GAAG,OAAO,wBAAwB,EAClC,IAAA,gCAAsB,EAAC,SAAS,EAAE,IAAA,2BAAe,EAAC,OAAO,EAAE,MAAM,CAAC,CAAC,CACpE,CAAC;QACF,OAAO,CAAC,GAAG,CAAC,2BAA2B,OAAO,wBAAwB,CAAC,CAAC;QAExE,IAAI,OAAO,KAAK,MAAM,IAAI,CAAC,cAAc;YAAE,MAAM,WAAW,CAAC,OAAO,CAAC,CAAC;IACxE,CAAC;IAED,IAAI,CAAC,cAAc;QAAE,MAAM,WAAW,CAAC,MAAM,CAAC,CAAC;IAE/C,IAAI,CAAC,sBAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,qBAAqB,CAAC,IAAI,OAAO,EAAE,CAAC;QAC7D,OAAO,CAAC,GAAG,CAAC,gCAAgC,CAAC,CAAC;QAC9C,IAAI,CAAC;YACH,MAAM,IAAA,0BAAc,EAClB,sBAAO,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,MAAM,EACzB,CAAC,qBAAqB,EAAE,GAAG,OAAO,wBAAwB,CAAC,EAC3D,sBAAO,CAAC,GAAG,EAAE,CACd,CAAC;QACJ,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,IAAA,oBAAQ,EAAC,8BAA8B,EAAE,EAAE,CAAC,CAAC;QAC/C,CAAC;IACH,CAAC;AACH,CAAC"}
@@ -0,0 +1,21 @@
1
+ import proto from "protobufjs";
2
+ /**
3
+ * Generates wrapper functions for all messages and enums in a protobuf namespace, including nested namespaces, and returns the content of the wrapper file as a string.
4
+ * @param namespace - The protobuf namespace to create wrapper functions for
5
+ * @returns A string containing the content of the wrapper file with all the generated wrapper functions for the protobuf namespace
6
+ */
7
+ declare function createNamespaceSerializers(namespace: proto.Namespace): string;
8
+ /**
9
+ * Generates a serializer and deserializer function name for a given protobuf message name by capitalizing the first letter of the message name and prefixing it with "serialize" for the serializer function and "deserialize" for the deserializer function. For example, if the message name is "MyMessage", the generated serializer function name would be "serializeMyMessage" and the deserializer function name would be "deserializeMyMessage".
10
+ * @param messageName - The name of the protobuf message type to generate the serializer and deserializer function names for
11
+ * @returns An object containing the generated serializer and deserializer function names for the specified protobuf message type
12
+ */
13
+ declare function getSerializerFunctionName(messageName: string): string;
14
+ /**
15
+ * Generates a deserializer function name for a given protobuf message name by capitalizing the first letter of the message name and prefixing it with "deserialize". For example, if the message name is "MyMessage", the generated deserializer function name would be "deserializeMyMessage".
16
+ * @param messageName - The name of the protobuf message type to generate the deserializer function name for
17
+ * @returns A string containing the generated deserializer function name for the specified protobuf message type
18
+ */
19
+ declare function getDeserializerFunctionName(messageName: string): string;
20
+ export { createNamespaceSerializers, getSerializerFunctionName, getDeserializerFunctionName, };
21
+ //# sourceMappingURL=serialization.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serialization.d.ts","sourceRoot":"","sources":["../../src/generators/serialization.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,YAAY,CAAC;AAQ/B;;;;GAIG;AACH,iBAAS,0BAA0B,CAAC,SAAS,EAAE,KAAK,CAAC,SAAS,GAAG,MAAM,CAwCtE;AAqDD;;;;GAIG;AACH,iBAAS,yBAAyB,CAAC,WAAW,EAAE,MAAM,UAErD;AAED;;;;GAIG;AACH,iBAAS,2BAA2B,CAAC,WAAW,EAAE,MAAM,UAEvD;AAED,OAAO,EACL,0BAA0B,EAC1B,yBAAyB,EACzB,2BAA2B,GAC5B,CAAC"}
@@ -1,48 +1,42 @@
1
- import proto from "protobufjs";
2
- import { capitalizeFirstLetter } from "../utils/string-manipulation.js";
3
- import { getWrapperFunctionName } from "./wrap.js";
4
- import { getUnwrapperFunctionName } from "./unwrap.js";
5
- import { getMessages } from "../utils/protobuf.js";
6
- import { getMessageId } from "../utils/associations.js";
7
- import { logError } from "../utils/logger.js";
8
-
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createNamespaceSerializers = createNamespaceSerializers;
4
+ exports.getSerializerFunctionName = getSerializerFunctionName;
5
+ exports.getDeserializerFunctionName = getDeserializerFunctionName;
6
+ const string_manipulation_js_1 = require("../utils/string-manipulation.js");
7
+ const wrap_js_1 = require("./wrap.js");
8
+ const unwrap_js_1 = require("./unwrap.js");
9
+ const protobuf_js_1 = require("../utils/protobuf.js");
10
+ const associations_js_1 = require("../utils/associations.js");
11
+ const logger_js_1 = require("../utils/logger.js");
9
12
  /**
10
13
  * Generates wrapper functions for all messages and enums in a protobuf namespace, including nested namespaces, and returns the content of the wrapper file as a string.
11
14
  * @param namespace - The protobuf namespace to create wrapper functions for
12
15
  * @returns A string containing the content of the wrapper file with all the generated wrapper functions for the protobuf namespace
13
16
  */
14
- function createNamespaceSerializers(namespace: proto.Namespace): string {
15
- let definitions = "";
16
- const exports: string[] = [];
17
- const typeImports: string[] = [];
18
- const wrapperImports: string[] = [];
19
- const unwrapperImports: string[] = [];
20
- const messages = getMessages(namespace);
21
- for (const message of messages) {
22
- if (!getMessageId(message)) continue;
23
- const {
24
- definitions: messageDefinitions,
25
- exports: messageExports,
26
- typeImports: messageImports,
27
- wrapperImports: messageWrapperImports,
28
- unwrapperImports: messageUnwrapperImports,
29
- } = createMessageSerializers(namespace.name, message);
30
- definitions += messageDefinitions;
31
- exports.push(...messageExports);
32
- typeImports.push(...messageImports);
33
- wrapperImports.push(...messageWrapperImports);
34
- unwrapperImports.push(...messageUnwrapperImports);
35
- }
36
-
37
- if (!definitions)
38
- logError(
39
- `No messages with a valid message ID found in namespace ${namespace.name}.`,
40
- );
41
-
42
- const dedupedTypeImports = [...new Set(typeImports)];
43
- const dedupedWrapperImports = [...new Set(wrapperImports)];
44
- const dedupedUnwrapperImports = [...new Set(unwrapperImports)];
45
- return `import type { ${dedupedTypeImports.join(", ")} } from "./types.js";
17
+ function createNamespaceSerializers(namespace) {
18
+ let definitions = "";
19
+ const exports = [];
20
+ const typeImports = [];
21
+ const wrapperImports = [];
22
+ const unwrapperImports = [];
23
+ const messages = (0, protobuf_js_1.getMessages)(namespace);
24
+ for (const message of messages) {
25
+ if (!(0, associations_js_1.getMessageId)(message))
26
+ continue;
27
+ const { definitions: messageDefinitions, exports: messageExports, typeImports: messageImports, wrapperImports: messageWrapperImports, unwrapperImports: messageUnwrapperImports, } = createMessageSerializers(namespace.name, message);
28
+ definitions += messageDefinitions;
29
+ exports.push(...messageExports);
30
+ typeImports.push(...messageImports);
31
+ wrapperImports.push(...messageWrapperImports);
32
+ unwrapperImports.push(...messageUnwrapperImports);
33
+ }
34
+ if (!definitions)
35
+ (0, logger_js_1.logError)(`No messages with a valid message ID found in namespace ${namespace.name}.`);
36
+ const dedupedTypeImports = [...new Set(typeImports)];
37
+ const dedupedWrapperImports = [...new Set(wrapperImports)];
38
+ const dedupedUnwrapperImports = [...new Set(unwrapperImports)];
39
+ return `import type { ${dedupedTypeImports.join(", ")} } from "./types.js";
46
40
  import { ${namespace.name} } from "./protobuf/${namespace.name}.js";
47
41
  import { ${dedupedWrapperImports.join(", ")} } from "./wrap.js";
48
42
  import { ${dedupedUnwrapperImports.join(", ")} } from "./unwrap.js";
@@ -52,31 +46,19 @@ export {
52
46
  };
53
47
  `;
54
48
  }
55
-
56
49
  /**
57
50
  * Generates a serializer and deserializer function for a single protobuf message type. The serializer function takes an instance of the message type and returns a Uint8Array containing the serialized message, while the deserializer function takes a Uint8Array containing the serialized message and returns an instance of the message type. The functions use the protobufjs library to perform the encoding and decoding, and they also utilize wrapper and unwrapper functions to convert between the protobuf message format and the TypeScript types defined for the message.
58
51
  * @param namespace - The name of the protobuf namespace that the message type belongs to, which is used to reference the correct message type in the protobufjs encoding and decoding functions
59
52
  * @param message - The protobuf message type to create the serializer and deserializer functions for
60
53
  * @returns An object containing the TypeScript definitions for the serializer and deserializer functions, the names of the functions to be exported, and arrays of imports required for the message types and wrapper/unwrapper functions used in the definitions
61
54
  */
62
- function createMessageSerializers(
63
- namespace: string,
64
- message: proto.Type,
65
- ): {
66
- definitions: string;
67
- exports: string[];
68
- typeImports: string[];
69
- wrapperImports: string[];
70
- unwrapperImports: string[];
71
- } {
72
- const functionSuffix = capitalizeFirstLetter(message.name);
73
-
74
- const serialize = `serialize${functionSuffix}`;
75
- const deserialize = `deserialize${functionSuffix}`;
76
- const wrap = getWrapperFunctionName(message.name);
77
- const unwrap = getUnwrapperFunctionName(message.name);
78
-
79
- const definitions = `
55
+ function createMessageSerializers(namespace, message) {
56
+ const functionSuffix = (0, string_manipulation_js_1.capitalizeFirstLetter)(message.name);
57
+ const serialize = `serialize${functionSuffix}`;
58
+ const deserialize = `deserialize${functionSuffix}`;
59
+ const wrap = (0, wrap_js_1.getWrapperFunctionName)(message.name);
60
+ const unwrap = (0, unwrap_js_1.getUnwrapperFunctionName)(message.name);
61
+ const definitions = `
80
62
  /**
81
63
  * Serializes a ${message.name} message to a Uint8Array.
82
64
  * @param input ${message.name} message to serialize
@@ -95,35 +77,28 @@ function ${deserialize}(input: Uint8Array): ${message.name} {
95
77
  return ${wrap}(${namespace}.${message.name}.decode(input));
96
78
  }
97
79
  `;
98
- return {
99
- definitions,
100
- exports: [serialize, deserialize],
101
- typeImports: [message.name],
102
- wrapperImports: [wrap],
103
- unwrapperImports: [unwrap],
104
- };
80
+ return {
81
+ definitions,
82
+ exports: [serialize, deserialize],
83
+ typeImports: [message.name],
84
+ wrapperImports: [wrap],
85
+ unwrapperImports: [unwrap],
86
+ };
105
87
  }
106
-
107
88
  /**
108
89
  * Generates a serializer and deserializer function name for a given protobuf message name by capitalizing the first letter of the message name and prefixing it with "serialize" for the serializer function and "deserialize" for the deserializer function. For example, if the message name is "MyMessage", the generated serializer function name would be "serializeMyMessage" and the deserializer function name would be "deserializeMyMessage".
109
90
  * @param messageName - The name of the protobuf message type to generate the serializer and deserializer function names for
110
91
  * @returns An object containing the generated serializer and deserializer function names for the specified protobuf message type
111
92
  */
112
- function getSerializerFunctionName(messageName: string) {
113
- return `serialize${capitalizeFirstLetter(messageName)}`;
93
+ function getSerializerFunctionName(messageName) {
94
+ return `serialize${(0, string_manipulation_js_1.capitalizeFirstLetter)(messageName)}`;
114
95
  }
115
-
116
96
  /**
117
97
  * Generates a deserializer function name for a given protobuf message name by capitalizing the first letter of the message name and prefixing it with "deserialize". For example, if the message name is "MyMessage", the generated deserializer function name would be "deserializeMyMessage".
118
98
  * @param messageName - The name of the protobuf message type to generate the deserializer function name for
119
99
  * @returns A string containing the generated deserializer function name for the specified protobuf message type
120
100
  */
121
- function getDeserializerFunctionName(messageName: string) {
122
- return `deserialize${capitalizeFirstLetter(messageName)}`;
101
+ function getDeserializerFunctionName(messageName) {
102
+ return `deserialize${(0, string_manipulation_js_1.capitalizeFirstLetter)(messageName)}`;
123
103
  }
124
-
125
- export {
126
- createNamespaceSerializers,
127
- getSerializerFunctionName,
128
- getDeserializerFunctionName,
129
- };
104
+ //# sourceMappingURL=serialization.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serialization.js","sourceRoot":"","sources":["../../src/generators/serialization.ts"],"names":[],"mappings":";;AA6HE,gEAA0B;AAC1B,8DAAyB;AACzB,kEAA2B;AA9H7B,4EAAwE;AACxE,uCAAmD;AACnD,2CAAuD;AACvD,sDAAmD;AACnD,8DAAwD;AACxD,kDAA8C;AAE9C;;;;GAIG;AACH,SAAS,0BAA0B,CAAC,SAA0B;IAC5D,IAAI,WAAW,GAAG,EAAE,CAAC;IACrB,MAAM,OAAO,GAAa,EAAE,CAAC;IAC7B,MAAM,WAAW,GAAa,EAAE,CAAC;IACjC,MAAM,cAAc,GAAa,EAAE,CAAC;IACpC,MAAM,gBAAgB,GAAa,EAAE,CAAC;IACtC,MAAM,QAAQ,GAAG,IAAA,yBAAW,EAAC,SAAS,CAAC,CAAC;IACxC,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE,CAAC;QAC/B,IAAI,CAAC,IAAA,8BAAY,EAAC,OAAO,CAAC;YAAE,SAAS;QACrC,M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C;IAE3D,MAAM,SAAS,GAAG,YAAY,cAAc,EAAE,CAAC;IAC/C,MAAM,WAAW,GAAG,cAAc,cAAc,EAAE,CAAC;IACnD,MAAM,IAAI,GAAG,IAAA,gCAAsB,EAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAClD,MAAM,MAAM,GAAG,IAAA,oCAAwB,EAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAEtD,MAAM,WAAW,GAAG;;kBAEJ,OAAO,CAAC,IAAI;kBACZ,OAAO,CAAC,IAAI;;;WAGnB,SAAS,WAAW,OAAO,CAAC,IAAI;WAChC,SAAS,IAAI,OAAO,CAAC,IAAI,WAAW,MAAM;;;;oBAIjC,OAAO,CAAC,IAAI;;cAElB,OAAO,CAAC,IAAI;;WAEf,WAAW,wBAAwB,OAAO,CAAC,IAAI;WAC/C,IAAI,IAAI,SAAS,IAAI,OAAO,CAAC,IAAI;;CAE3C,CAAC;IACA,OAAO;QACL,WAAW;QACX,OAAO,EAAE,CAAC,SAAS,EAAE,WAAW,CAAC;QACjC,WAAW,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC;QAC3B,cAAc,EAAE,CAAC,IAAI,CAAC;QACtB,gBAAgB,EAAE,CAAC,MAAM,CAAC;KAC3B,CAAC;AACJ,CAAC;AAED;;;;GAIG;AACH,SAAS,yBAAyB,CAAC,WAAmB;IACpD,OAAO,YAAY,IAAA,8CAAqB,EAAC,WAAW,CAAC,EAAE,CAAC;AAC1D,CAAC;AAED;;;;GAIG;AACH,SAAS,2BAA2B,CAAC,WAAmB;IACtD,OAAO,cAAc,IAAA,8CAAqB,EAAC,WAAW,CAAC,EAAE,CAAC;AAC5D,CAAC"}
@@ -0,0 +1,10 @@
1
+ import proto from "protobufjs";
2
+ /**
3
+ * Generates test cases for all messages in a protobuf namespace, including nested namespaces, and returns the content of the test file as a string. The generated tests include serialization and deserialization tests for each message, as well as additional tests for handling optional fields if any are present in the message definitions.
4
+ * @param namespace - The protobuf namespace to create test cases for
5
+ * @param relativePath - The relative path to the generated index file for the protobuf namespace, which is used for importing the serializer and deserializer functions in the generated test file
6
+ * @returns A string containing the content of the test file with all the generated test cases for the protobuf namespace
7
+ */
8
+ declare function generateNamespaceTests(namespace: proto.Namespace, relativePath: string): string;
9
+ export { generateNamespaceTests };
10
+ //# sourceMappingURL=test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"test.d.ts","sourceRoot":"","sources":["../../src/generators/test.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,YAAY,CAAC;AAyB/B;;;;;GAKG;AACH,iBAAS,sBAAsB,CAC7B,SAAS,EAAE,KAAK,CAAC,SAAS,EAC1B,YAAY,EAAE,MAAM,UAsBrB;AAsJD,OAAO,EAAE,sBAAsB,EAAE,CAAC"}
@@ -0,0 +1,170 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.generateNamespaceTests = generateNamespaceTests;
7
+ const protobufjs_1 = __importDefault(require("protobufjs"));
8
+ const protobuf_js_1 = require("../utils/protobuf.js");
9
+ const associations_js_1 = require("../utils/associations.js");
10
+ const serialization_js_1 = require("./serialization.js");
11
+ const logger_js_1 = require("../utils/logger.js");
12
+ /**
13
+ * Generates test cases for all messages in a protobuf namespace, including nested namespaces, and returns the content of the test file as a string. The generated tests include serialization and deserialization tests for each message, as well as additional tests for handling optional fields if any are present in the message definitions.
14
+ * @param namespace - The protobuf namespace to create test cases for
15
+ * @param relativePath - The relative path to the generated index file for the protobuf namespace, which is used for importing the serializer and deserializer functions in the generated test file
16
+ * @returns A string containing the content of the test file with all the generated test cases for the protobuf namespace
17
+ */
18
+ function generateNamespaceTests(namespace, relativePath) {
19
+ const messages = (0, protobuf_js_1.getMessages)(namespace).filter((msg) => Boolean((0, associations_js_1.getMessageId)(msg)));
20
+ return `
21
+ import { describe, it } from 'node:test';
22
+ import assert from 'node:assert';
23
+ import { ${messages
24
+ .flatMap((msg) => [
25
+ (0, serialization_js_1.getSerializerFunctionName)(msg.name),
26
+ (0, serialization_js_1.getDeserializerFunctionName)(msg.name),
27
+ `type ${msg.name}`,
28
+ ])
29
+ .join(", ")} } from "${relativePath}/index.js";
30
+
31
+ describe("${namespace.name}", () => {
32
+ ${messages.map((msg) => generateMessageTests(msg)).join("\n")}
33
+ });
34
+
35
+ `;
36
+ }
37
+ /**
38
+ * Generates test cases for a single protobuf message type, including serialization and deserialization tests, as well as additional tests for handling optional fields if any are present in the message definition. The generated tests create instances of the message with different combinations of required and optional fields to ensure that the serializer and deserializer functions handle all cases correctly.
39
+ * @param message - The protobuf message type to create test cases for
40
+ * @returns A string containing the test cases for the specified protobuf message type, including serialization and deserialization tests and optional field handling tests if applicable
41
+ */
42
+ function generateMessageTests(message) {
43
+ const serializer = (0, serialization_js_1.getSerializerFunctionName)(message.name);
44
+ const deserializer = (0, serialization_js_1.getDeserializerFunctionName)(message.name);
45
+ const optional = (0, protobuf_js_1.hasOptionalField)(message);
46
+ const optionalTest = optional
47
+ ? `it("should handle optional fields correctly", () => {
48
+ const original: ${message.name} = ${createTestInstance(message, "omit-optional")};
49
+ const defaulted: ${message.name} = ${createTestInstance(message, "default-optional")};
50
+ const serialized = ${serializer}(original);
51
+ const deserialized = ${deserializer}(serialized);
52
+ assert.deepStrictEqual(deserialized, defaulted);
53
+ });`
54
+ : "";
55
+ return `
56
+ describe("${message.name}", () => {
57
+ it("should serialize and deserialize correctly", () => {
58
+ const original: ${message.name} = ${createTestInstance(message, "all-required")};
59
+ const serialized = ${serializer}(original);
60
+ const deserialized = ${deserializer}(serialized);
61
+ assert.deepStrictEqual(deserialized, original);
62
+ });
63
+ ${optionalTest}
64
+ });
65
+ `;
66
+ }
67
+ /**
68
+ * Creates a test instance of a protobuf message type with example values for each field, handling optional fields according to the specified behavior. The function generates a string representation of a TypeScript object that can be used as a test instance for the message type in serialization and deserialization tests. For repeated fields, it generates an array of example values, and for optional fields, it either omits them or sets them to default values based on the provided optional behavior.
69
+ * @param message - The protobuf message type to create a test instance for
70
+ * @param optionalBehavior - The behavior to apply for optional fields when creating the test instance, which can be "all-required" to include all fields as required, "omit-optional" to omit optional fields from the test instance, or "default-optional" to include optional fields with default values (e.g., empty string for strings, 0 for numbers, false for booleans)
71
+ * @returns A string containing the TypeScript object representation of a test instance for the specified protobuf message type, with example values for each field and handling of optional fields according to the specified behavior
72
+ */
73
+ function createTestInstance(message, optionalBehavior) {
74
+ return `{
75
+ ${message.fieldsArray
76
+ .map((field) => createTestFieldValue(field, optionalBehavior))
77
+ .filter(Boolean)
78
+ .join("\n")}
79
+ }`;
80
+ }
81
+ /**
82
+ * Creates a test value for a single field of a protobuf message type, handling optional fields according to the specified behavior. The function generates a string representation of the field assignment that can be included in a test instance for the message type. For repeated fields, it generates an array of example values, and for optional fields, it either omits them or sets them to default values based on the provided optional behavior.
83
+ * @param field - The protobuf field to create a test value for
84
+ * @param optionalBehavior - The behavior to apply for optional fields when creating the test value, which can be "all-required" to include all fields as required, "omit-optional" to omit optional fields from the test value, or "default-optional" to include optional fields with default values (e.g., empty string for strings, 0 for numbers, false for booleans)
85
+ * @param omitFieldName - A boolean flag indicating whether to omit the field name in the generated test value, which is used for generating values for repeated fields where the field name is not included in the array elements
86
+ * @returns A string containing the TypeScript representation of the test value for the specified protobuf field, with handling of optional fields according to the specified behavior. If the field is omitted due to being optional and the optional behavior is "omit-optional", it returns null.
87
+ */
88
+ function createTestFieldValue(field, optionalBehavior, omitFieldName = false) {
89
+ const isOptional = !(0, protobuf_js_1.isRequiredField)(field);
90
+ if (optionalBehavior === "omit-optional" && isOptional && !field.repeated) {
91
+ return null; // Skip optional fields if optionalBehavior is "omit-optional"
92
+ }
93
+ const fieldName = field.name;
94
+ const fieldType = field.resolvedType ? field.resolvedType.name : field.type;
95
+ let exampleValue;
96
+ if (field.repeated) {
97
+ if (optionalBehavior === "all-required") {
98
+ const instance = createTestFieldValue({ ...field, repeated: false }, optionalBehavior,
99
+ /* omitFieldName */ true);
100
+ exampleValue = `[${instance}, ${instance}]`;
101
+ }
102
+ else {
103
+ exampleValue = "[]"; // Use empty array for repeated fields when omitting optional fields
104
+ }
105
+ }
106
+ else {
107
+ switch (fieldType) {
108
+ case "string":
109
+ exampleValue =
110
+ optionalBehavior === "default-optional" && isOptional
111
+ ? `""`
112
+ : `"example"`;
113
+ break;
114
+ case "int32":
115
+ case "uint32":
116
+ case "sint32":
117
+ case "fixed32":
118
+ case "sfixed32":
119
+ exampleValue =
120
+ optionalBehavior === "default-optional" && isOptional ? `0` : `123`;
121
+ break;
122
+ case "int64":
123
+ case "uint64":
124
+ case "sint64":
125
+ case "fixed64":
126
+ case "sfixed64":
127
+ exampleValue =
128
+ optionalBehavior === "default-optional" && isOptional ? `0n` : `123n`; // BigInt for 64-bit integers
129
+ break;
130
+ case "bool":
131
+ exampleValue =
132
+ optionalBehavior === "default-optional" && isOptional
133
+ ? `false`
134
+ : `true`;
135
+ break;
136
+ case "bytes":
137
+ exampleValue =
138
+ optionalBehavior === "default-optional" && isOptional
139
+ ? `Buffer.from([])`
140
+ : `Buffer.from([1, 2, 3])`;
141
+ break;
142
+ default:
143
+ const resolvedType = field.resolvedType;
144
+ if (resolvedType && resolvedType instanceof protobufjs_1.default.Type) {
145
+ if (optionalBehavior === "default-optional" && isOptional) {
146
+ exampleValue = "";
147
+ }
148
+ else {
149
+ exampleValue = createTestInstance(resolvedType, optionalBehavior);
150
+ }
151
+ }
152
+ else if (field.resolvedType &&
153
+ field.resolvedType instanceof protobufjs_1.default.Enum) {
154
+ const enumValues = Object.keys(field.resolvedType.values);
155
+ const middleValue = enumValues[Math.floor(enumValues.length / 2)];
156
+ exampleValue = `"${middleValue}"`;
157
+ }
158
+ else {
159
+ (0, logger_js_1.logError)(`Unsupported field type "${fieldType}" for field "${fieldName}". Using "UNKNOWN" as placeholder.`);
160
+ exampleValue = "UNKNOWN";
161
+ }
162
+ }
163
+ }
164
+ if (!exampleValue)
165
+ return null;
166
+ return omitFieldName
167
+ ? `${exampleValue}`
168
+ : ` ${fieldName}: ${exampleValue},`;
169
+ }
170
+ //# sourceMappingURL=test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"test.js","sourceRoot":"","sources":["../../src/generators/test.ts"],"names":[],"mappings":";;;;;AA6MS,wDAAsB;AA7M/B,4DAA+B;AAC/B,sDAI8B;AAC9B,8DAAwD;AACxD,yDAG4B;AAC5B,kDAA8C;AAc9C;;;;;GAKG;AACH,SAAS,sBAAsB,CAC7B,SAA0B,EAC1B,YAAoB;IAEpB,MAAM,QAAQ,GAAG,IAAA,yBAAW,EAAC,SAAS,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CACrD,OAAO,CAAC,IAAA,8BAAY,EAAC,GAAG,CAAC,CAAC,CAC3B,CAAC;IAEF,OAAO;;;WAGE,QAAQ;SACd,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC;QAChB,IAAA,4CAAyB,EAAC,GAAG,CAAC,IAAI,CAAC;QACnC,IAAA,8CAA2B,EAAC,GAAG,CAAC,IAAI,CAAC;QACrC,QAAQ,GAAG,CAAC,IAAI,EAAE;KACnB,CAAC;SACD,IAAI,CAAC,IAAI,CAAC,YAAY,YAAY;;YAE3B,SAAS,CAAC,IAAI;IACtB,QAAQ,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,oBAAoB,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC;;;CAG9D,CAAC;AACF,CAAC;AAED;;;;GAIG;AACH,SAAS,oBAAoB,CAAC,OAAmB;IAC/C,MAAM,UAAU,GAAG,IAAA,4CAAyB,EAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAC3D,MAAM,YAAY,GAAG,IAAA,8CAA2B,EAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAC/D,MAAM,QAAQ,GAAG,IAAA,8BAAgB,E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kB,IAAI,UAAU;wBACnD,CAAC,CAAC,iBAAiB;wBACnB,CAAC,CAAC,wBAAwB,CAAC;gBAC/B,MAAM;YACR;gBACE,MAAM,YAAY,GAAG,KAAK,CAAC,YAAY,CAAC;gBACxC,IAAI,YAAY,IAAI,YAAY,YAAY,oBAAK,CAAC,IAAI,EAAE,CAAC;oBACvD,IAAI,gBAAgB,KAAK,kBAAkB,IAAI,UAAU,EAAE,CAAC;wBAC1D,YAAY,GAAG,EAAE,CAAC;oBACpB,CAAC;yBAAM,CAAC;wBACN,YAAY,GAAG,kBAAkB,CAAC,YAAY,EAAE,gBAAgB,CAAC,CAAC;oBACpE,CAAC;gBACH,CAAC;qBAAM,IACL,KAAK,CAAC,YAAY;oBAClB,KAAK,CAAC,YAAY,YAAY,oBAAK,CAAC,IAAI,EACxC,CAAC;oBACD,MAAM,UAAU,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC;oBAC1D,MAAM,WAAW,GAAG,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC;oBAClE,YAAY,GAAG,IAAI,WAAW,GAAG,CAAC;gBACpC,CAAC;qBAAM,CAAC;oBACN,IAAA,oBAAQ,EACN,2BAA2B,SAAS,gBAAgB,SAAS,oCAAoC,CAClG,CAAC;oBACF,YAAY,GAAG,SAAS,CAAC;gBAC3B,CAAC;QACL,CAAC;IACH,CAAC;IAED,IAAI,CAAC,YAAY;QAAE,OAAO,IAAI,CAAC;IAE/B,OAAO,aAAa;QAClB,CAAC,CAAC,GAAG,YAAY,EAAE;QACnB,CAAC,CAAC,SAAS,SAAS,KAAK,YAAY,GAAG,CAAC;AAC7C,CAAC"}
@@ -0,0 +1,10 @@
1
+ import proto from "protobufjs";
2
+ /**
3
+ * Generates wrapper functions for all messages and enums in a protobuf namespace, including nested namespaces, and returns the content of the wrapper file as a string.
4
+ * @param namespace - The protobuf namespace to create wrapper functions for
5
+ * @param associations - An array of tuples representing request-response message associations, where each tuple contains a request message type and its corresponding response message type. These associations are used to generate additional traits for request-response pairs.
6
+ * @returns A string containing the content of the wrapper file with all the generated wrapper functions for the protobuf namespace
7
+ */
8
+ declare function createNamespaceTraits(namespace: proto.Namespace, associations: [proto.Type, proto.Type][]): string;
9
+ export { createNamespaceTraits };
10
+ //# sourceMappingURL=traits.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"traits.d.ts","sourceRoot":"","sources":["../../src/generators/traits.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,YAAY,CAAC;AAa/B;;;;;GAKG;AACH,iBAAS,qBAAqB,CAC5B,SAAS,EAAE,KAAK,CAAC,SAAS,EAC1B,YAAY,EAAE,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,EAAE,GACvC,MAAM,CAoDR;AAkED,OAAO,EAAE,qBAAqB,EAAE,CAAC"}