@aptre/protobuf-es-lite 0.2.6 → 0.2.8

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 (121) hide show
  1. package/.ignore +1 -0
  2. package/README.md +25 -24
  3. package/bin/protoc-gen-es-lite +3 -3
  4. package/dist/binary-encoding.d.ts +420 -0
  5. package/dist/binary-encoding.js +437 -0
  6. package/dist/binary.d.ts +35 -2
  7. package/dist/binary.js +69 -66
  8. package/dist/codegen-info.d.ts +43 -0
  9. package/dist/codegen-info.js +72 -0
  10. package/dist/create-descriptor-set.d.ts +36 -0
  11. package/dist/create-descriptor-set.js +936 -0
  12. package/dist/descriptor-set.d.ts +672 -0
  13. package/dist/{editions.js → descriptor-set.js} +0 -11
  14. package/dist/enum.d.ts +35 -1
  15. package/dist/feature-set.d.ts +19 -0
  16. package/dist/feature-set.js +103 -0
  17. package/dist/field-wrapper.d.ts +7 -3
  18. package/dist/field-wrapper.js +30 -1
  19. package/dist/field.d.ts +8 -19
  20. package/dist/field.js +15 -117
  21. package/dist/google/index.d.ts +9 -0
  22. package/dist/google/index.js +41 -0
  23. package/dist/google/protobuf/any.pb.d.ts +139 -0
  24. package/dist/google/protobuf/any.pb.js +42 -0
  25. package/dist/google/protobuf/api.pb.d.ts +252 -0
  26. package/dist/google/protobuf/api.pb.js +70 -0
  27. package/dist/google/protobuf/compiler/plugin.pb.d.ts +240 -0
  28. package/dist/google/protobuf/compiler/plugin.pb.js +203 -0
  29. package/dist/google/protobuf/descriptor.pb.d.ts +2132 -0
  30. package/dist/google/protobuf/descriptor.pb.js +1080 -0
  31. package/dist/google/protobuf/duration.pb.d.ts +92 -0
  32. package/dist/google/protobuf/duration.pb.js +42 -0
  33. package/dist/google/protobuf/empty.pb.d.ts +18 -0
  34. package/dist/google/protobuf/empty.pb.js +39 -0
  35. package/dist/google/protobuf/source_context.pb.d.ts +21 -0
  36. package/dist/google/protobuf/source_context.pb.js +41 -0
  37. package/dist/google/protobuf/struct.pb.d.ts +121 -0
  38. package/dist/google/protobuf/struct.pb.js +83 -0
  39. package/dist/google/protobuf/timestamp.pb.d.ts +121 -0
  40. package/dist/google/protobuf/timestamp.pb.js +42 -0
  41. package/dist/google/protobuf/type.pb.d.ts +464 -0
  42. package/dist/google/protobuf/type.pb.js +338 -0
  43. package/dist/google/protobuf/wrappers.pb.d.ts +183 -0
  44. package/dist/google/protobuf/wrappers.pb.js +107 -0
  45. package/dist/index.d.ts +8 -3
  46. package/dist/index.js +38 -8
  47. package/dist/is-message.d.ts +10 -2
  48. package/dist/is-message.js +76 -3
  49. package/dist/json.d.ts +68 -1
  50. package/dist/json.js +60 -81
  51. package/dist/message.d.ts +30 -61
  52. package/dist/message.js +63 -18
  53. package/dist/names.d.ts +43 -0
  54. package/dist/names.js +276 -0
  55. package/dist/partial.js +19 -16
  56. package/dist/proto-base64.d.ts +18 -0
  57. package/dist/proto-base64.js +128 -0
  58. package/dist/proto-double.d.ts +5 -0
  59. package/dist/proto-double.js +29 -0
  60. package/dist/proto-int64.d.ts +95 -0
  61. package/dist/proto-int64.js +116 -0
  62. package/dist/protoc-gen-es-lite/protoc-gen-es-lite-plugin.d.ts +1 -0
  63. package/dist/{protoc-gen-es-lite-plugin.js → protoc-gen-es-lite/protoc-gen-es-lite-plugin.js} +2 -2
  64. package/dist/{typescript.d.ts → protoc-gen-es-lite/typescript.d.ts} +3 -2
  65. package/dist/{typescript.js → protoc-gen-es-lite/typescript.js} +48 -36
  66. package/dist/protoplugin/create-es-plugin.d.ts +83 -0
  67. package/dist/protoplugin/create-es-plugin.js +119 -0
  68. package/dist/protoplugin/ecmascript/file-preamble.d.ts +2 -0
  69. package/dist/protoplugin/ecmascript/file-preamble.js +74 -0
  70. package/dist/protoplugin/ecmascript/generated-file.d.ts +116 -0
  71. package/dist/protoplugin/ecmascript/generated-file.js +407 -0
  72. package/dist/protoplugin/ecmascript/import-path.d.ts +65 -0
  73. package/dist/protoplugin/ecmascript/import-path.js +156 -0
  74. package/dist/protoplugin/ecmascript/import-symbol.d.ts +39 -0
  75. package/dist/protoplugin/ecmascript/import-symbol.js +37 -0
  76. package/dist/protoplugin/ecmascript/index.d.ts +7 -0
  77. package/dist/protoplugin/ecmascript/index.js +20 -0
  78. package/dist/protoplugin/ecmascript/jsdoc.d.ts +14 -0
  79. package/dist/protoplugin/ecmascript/jsdoc.js +93 -0
  80. package/dist/protoplugin/ecmascript/opaque-printables.d.ts +27 -0
  81. package/dist/protoplugin/ecmascript/opaque-printables.js +15 -0
  82. package/dist/protoplugin/ecmascript/parameter.d.ts +13 -0
  83. package/dist/protoplugin/ecmascript/parameter.js +161 -0
  84. package/dist/protoplugin/ecmascript/reify-wkt.d.ts +100 -0
  85. package/dist/protoplugin/ecmascript/reify-wkt.js +170 -0
  86. package/dist/protoplugin/ecmascript/runtime-imports.d.ts +6 -0
  87. package/dist/protoplugin/ecmascript/runtime-imports.js +28 -0
  88. package/dist/protoplugin/ecmascript/schema.d.ts +43 -0
  89. package/dist/protoplugin/ecmascript/schema.js +73 -0
  90. package/dist/protoplugin/ecmascript/target.d.ts +4 -0
  91. package/dist/protoplugin/ecmascript/target.js +15 -0
  92. package/dist/protoplugin/ecmascript/transpile.d.ts +2 -0
  93. package/dist/protoplugin/ecmascript/transpile.js +149 -0
  94. package/dist/protoplugin/error.d.ts +6 -0
  95. package/dist/protoplugin/error.js +43 -0
  96. package/dist/protoplugin/index.d.ts +8 -0
  97. package/dist/protoplugin/index.js +20 -0
  98. package/dist/protoplugin/plugin.d.ts +18 -0
  99. package/dist/protoplugin/plugin.js +15 -0
  100. package/dist/protoplugin/run-node.d.ts +12 -0
  101. package/dist/protoplugin/run-node.js +83 -0
  102. package/dist/scalar.d.ts +53 -1
  103. package/dist/scalar.js +87 -22
  104. package/dist/service-type.d.ts +95 -0
  105. package/dist/service-type.js +53 -0
  106. package/dist/text-format.d.ts +4 -0
  107. package/dist/text-format.js +189 -0
  108. package/dist/type-registry.d.ts +43 -0
  109. package/dist/type-registry.js +15 -0
  110. package/dist/unknown.d.ts +1 -1
  111. package/dist/util.d.ts +2 -2
  112. package/dist/util.js +46 -44
  113. package/dist/varint.d.ts +70 -0
  114. package/dist/varint.js +324 -0
  115. package/example/example.pb.ts +87 -0
  116. package/example/example.proto +1 -0
  117. package/example/tsconfig.json +8 -2
  118. package/package.json +19 -12
  119. package/tsconfig.json +4 -4
  120. package/dist/editions.d.ts +0 -6
  121. package/dist/protoc-gen-es-lite-plugin.d.ts +0 -1
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.createRuntimeImports = void 0;
17
+ const import_symbol_js_1 = require("./import-symbol.js");
18
+ const codegen_info_js_1 = require("../../codegen-info.js");
19
+ function createRuntimeImports(bootstrapWkt) {
20
+ const imports = {};
21
+ for (const [name, info] of Object.entries(codegen_info_js_1.codegenInfo.symbols)) {
22
+ const symbol = (0, import_symbol_js_1.createImportSymbol)(name, bootstrapWkt ? info.privateImportPath : info.publicImportPath);
23
+ imports[name] =
24
+ info.typeOnly ? symbol.toTypeOnly() : symbol;
25
+ }
26
+ return imports;
27
+ }
28
+ exports.createRuntimeImports = createRuntimeImports;
@@ -0,0 +1,43 @@
1
+ import type { FileInfo, GeneratedFile } from "./generated-file.js";
2
+ import type { RuntimeImports } from "./runtime-imports.js";
3
+ import type { Target } from "./target.js";
4
+ import type { ParsedParameter } from "./parameter.js";
5
+ import { DescFile } from "../../descriptor-set.js";
6
+ import { CodeGeneratorRequest } from "../../google/protobuf/compiler/plugin.pb.js";
7
+ import { FeatureSetDefaults } from "../../google/protobuf/descriptor.pb.js";
8
+ /**
9
+ * Schema describes the files and types that the plugin is requested to
10
+ * generate.
11
+ */
12
+ export interface Schema {
13
+ /**
14
+ * The files we are asked to generate.
15
+ */
16
+ readonly files: readonly DescFile[];
17
+ /**
18
+ * All files contained in the code generator request.
19
+ */
20
+ readonly allFiles: readonly DescFile[];
21
+ /**
22
+ * The plugin option `target`. A code generator should support all targets.
23
+ */
24
+ readonly targets: readonly Target[];
25
+ /**
26
+ * Provides some symbols from the runtime library.
27
+ */
28
+ readonly runtime: RuntimeImports;
29
+ /**
30
+ * Generate a new file with the given name.
31
+ */
32
+ generateFile(name: string): GeneratedFile;
33
+ /**
34
+ * The original google.protobuf.compiler.CodeGeneratorRequest.
35
+ */
36
+ readonly proto: CodeGeneratorRequest;
37
+ }
38
+ interface SchemaController extends Schema {
39
+ getFileInfo: () => FileInfo[];
40
+ prepareGenerate(target: Target): void;
41
+ }
42
+ export declare function createSchema(request: CodeGeneratorRequest, parameter: ParsedParameter, pluginName: string, pluginVersion: string, featureSetDefaults: FeatureSetDefaults | undefined): SchemaController;
43
+ export {};
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.createSchema = void 0;
17
+ const generated_file_js_1 = require("./generated-file.js");
18
+ const runtime_imports_js_1 = require("./runtime-imports.js");
19
+ const import_symbol_js_1 = require("./import-symbol.js");
20
+ const import_path_js_1 = require("./import-path.js");
21
+ const file_preamble_js_1 = require("./file-preamble.js");
22
+ const create_descriptor_set_js_1 = require("../../create-descriptor-set.js");
23
+ const codegen_info_js_1 = require("../../codegen-info.js");
24
+ function createSchema(request, parameter, pluginName, pluginVersion, featureSetDefaults) {
25
+ if (!request.protoFile) {
26
+ throw new Error("no protoFile passed to createSchema");
27
+ }
28
+ const descriptorSet = (0, create_descriptor_set_js_1.createDescriptorSet)(request.protoFile, {
29
+ featureSetDefaults,
30
+ });
31
+ const filesToGenerate = findFilesToGenerate(descriptorSet, request);
32
+ const runtime = (0, runtime_imports_js_1.createRuntimeImports)(parameter.bootstrapWkt);
33
+ const createTypeImport = (desc) => {
34
+ const name = codegen_info_js_1.codegenInfo.localName(desc);
35
+ const from = (0, import_path_js_1.makeImportPath)(desc.file, parameter.bootstrapWkt, filesToGenerate);
36
+ return (0, import_symbol_js_1.createImportSymbol)(name, from);
37
+ };
38
+ const createPreamble = (descFile) => (0, file_preamble_js_1.makeFilePreamble)(descFile, pluginName, pluginVersion, parameter.sanitizedParameter, parameter.tsNocheck);
39
+ let target;
40
+ const generatedFiles = [];
41
+ return {
42
+ targets: parameter.targets,
43
+ runtime,
44
+ proto: request,
45
+ files: filesToGenerate,
46
+ allFiles: descriptorSet.files,
47
+ generateFile(name) {
48
+ if (target === undefined) {
49
+ throw new Error("prepareGenerate() must be called before generateFile()");
50
+ }
51
+ const genFile = (0, generated_file_js_1.createGeneratedFile)(name, (0, import_path_js_1.deriveImportPath)(name), target === "js" ? parameter.jsImportStyle : "module", // ts and dts always use import/export, only js may use commonjs
52
+ (importPath) => (0, import_path_js_1.rewriteImportPath)(importPath, parameter.rewriteImports, parameter.importExtension), createTypeImport, runtime, createPreamble);
53
+ generatedFiles.push(genFile);
54
+ return genFile;
55
+ },
56
+ getFileInfo() {
57
+ return generatedFiles
58
+ .map((f) => f.getFileInfo())
59
+ .filter((fi) => parameter.keepEmptyFiles || fi.content.length > 0);
60
+ },
61
+ prepareGenerate(newTarget) {
62
+ target = newTarget;
63
+ },
64
+ };
65
+ }
66
+ exports.createSchema = createSchema;
67
+ function findFilesToGenerate(descriptorSet, request) {
68
+ const missing = request.fileToGenerate?.filter((fileToGenerate) => descriptorSet.files.every((file) => fileToGenerate !== file.name + ".proto")) ?? [];
69
+ if (missing.length) {
70
+ throw `files_to_generate missing in the request: ${missing.join(", ")}`;
71
+ }
72
+ return descriptorSet.files.filter((file) => request.fileToGenerate?.includes(file.name + ".proto") ?? false);
73
+ }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Represents possible values of the plugin option `target`.
3
+ */
4
+ export type Target = "js" | "ts" | "dts";
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ import type { FileInfo } from "./generated-file.js";
2
+ export declare function transpile(files: FileInfo[], transpileJs: boolean, transpileDts: boolean, jsImportStyle: "module" | "legacy_commonjs"): FileInfo[];
@@ -0,0 +1,149 @@
1
+ "use strict";
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
16
+ if (k2 === undefined) k2 = k;
17
+ var desc = Object.getOwnPropertyDescriptor(m, k);
18
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
19
+ desc = { enumerable: true, get: function() { return m[k]; } };
20
+ }
21
+ Object.defineProperty(o, k2, desc);
22
+ }) : (function(o, m, k, k2) {
23
+ if (k2 === undefined) k2 = k;
24
+ o[k2] = m[k];
25
+ }));
26
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
27
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
28
+ }) : function(o, v) {
29
+ o["default"] = v;
30
+ });
31
+ var __importStar = (this && this.__importStar) || function (mod) {
32
+ if (mod && mod.__esModule) return mod;
33
+ var result = {};
34
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
35
+ __setModuleDefault(result, mod);
36
+ return result;
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.transpile = void 0;
40
+ const typescript_1 = __importStar(require("typescript"));
41
+ const vfs_1 = require("@typescript/vfs");
42
+ /* eslint-disable import/no-named-as-default-member */
43
+ // The default options used to auto-transpile if needed.
44
+ const defaultOptions = {
45
+ // Type checking
46
+ strict: false,
47
+ // modules
48
+ module: typescript_1.default.ModuleKind.ES2020,
49
+ moduleResolution: typescript_1.default.ModuleResolutionKind.Node16,
50
+ noResolve: true,
51
+ resolveJsonModule: false,
52
+ // emit
53
+ emitBOM: false,
54
+ importsNotUsedAsValues: typescript_1.default.ImportsNotUsedAsValues.Preserve,
55
+ newLine: typescript_1.default.NewLineKind.LineFeed,
56
+ preserveValueImports: false,
57
+ // JavaScript Support
58
+ allowJs: true,
59
+ checkJs: false,
60
+ // Language and Environment
61
+ lib: [],
62
+ moduleDetection: typescript_1.ModuleDetectionKind.Force,
63
+ target: typescript_1.default.ScriptTarget.ES2020,
64
+ // Completeness
65
+ skipLibCheck: true,
66
+ skipDefaultLibCheck: false,
67
+ };
68
+ /**
69
+ * Create a transpiler using the given compiler options, which will compile the
70
+ * content provided in the files array.
71
+ *
72
+ * Note: this library intentionally transpiles with a pinned older version of
73
+ * TypeScript for stability. This version is denoted in this workspace's
74
+ * package.json. For the default set of compiler options, we use a lenient
75
+ * set of options because the general goal is to emit code as best as we can.
76
+ * For a list of the options used, see `defaultOptions` above.
77
+ *
78
+ * If this is not desirable for plugin authors, they are free to provide their
79
+ * own transpile function as part of the plugin initialization. If one is
80
+ * provided, it will be invoked instead and the framework's auto-transpilation
81
+ * will be bypassed.
82
+ *
83
+ * In addition, note that there is a dependency on @typescript/vfs in the
84
+ * top-level package as well as this package. This is to avoid npm hoisting
85
+ * @typescript/vfs to the top-level node_modules directory, which then causes
86
+ * type mismatches when trying to use it with this package's version of
87
+ * TypeScript. Ideally we would use something like Yarn's nohoist here, but
88
+ * npm does not support that yet.
89
+ */
90
+ function createTranspiler(options, files) {
91
+ const fsMap = (0, vfs_1.createDefaultMapFromNodeModules)({
92
+ target: options.target,
93
+ });
94
+ files.forEach((file) => {
95
+ fsMap.set(file.name, file.content);
96
+ });
97
+ const system = (0, vfs_1.createSystem)(fsMap);
98
+ const host = (0, vfs_1.createVirtualCompilerHost)(system, options, typescript_1.default);
99
+ return typescript_1.default.createProgram({
100
+ rootNames: [...fsMap.keys()],
101
+ options,
102
+ host: host.compilerHost,
103
+ });
104
+ }
105
+ function transpile(files, transpileJs, transpileDts, jsImportStyle) {
106
+ const options = {
107
+ ...defaultOptions,
108
+ declaration: transpileDts,
109
+ emitDeclarationOnly: transpileDts && !transpileJs,
110
+ };
111
+ if (jsImportStyle == "legacy_commonjs") {
112
+ options.module = typescript_1.default.ModuleKind.CommonJS;
113
+ }
114
+ // Create the transpiler (a ts.Program object)
115
+ const program = createTranspiler(options, files);
116
+ const results = [];
117
+ let err;
118
+ const result = program.emit(undefined, (fileName, data, writeByteOrderMark, onError, sourceFiles) => {
119
+ // We have to go through some hoops here because the header we add to each
120
+ // file is not part of the AST. So we find the TypeScript file we
121
+ // generated for each emitted file and add the header to each output ourselves.
122
+ if (!sourceFiles) {
123
+ err = new Error(`unable to map emitted file "${fileName}" to a source file: missing source files`);
124
+ return;
125
+ }
126
+ if (sourceFiles.length !== 1) {
127
+ err = new Error(`unable to map emitted file "${fileName}" to a source file: expected 1 source file, got ${sourceFiles.length}`);
128
+ return;
129
+ }
130
+ const file = files.find((x) => sourceFiles[0].fileName === x.name);
131
+ if (!file) {
132
+ err = new Error(`unable to map emitted file "${fileName}" to a source file: not found`);
133
+ return;
134
+ }
135
+ results.push({
136
+ name: fileName,
137
+ preamble: file.preamble,
138
+ content: data,
139
+ });
140
+ });
141
+ if (err) {
142
+ throw err;
143
+ }
144
+ if (result.emitSkipped) {
145
+ throw Error("A problem occurred during transpilation and files were not generated. Contact the plugin author for support.");
146
+ }
147
+ return results;
148
+ }
149
+ exports.transpile = transpile;
@@ -0,0 +1,6 @@
1
+ export declare class PluginOptionError extends Error {
2
+ name: string;
3
+ constructor(option: string, reason?: unknown);
4
+ }
5
+ export declare function reasonToString(reason: unknown): string;
6
+ export declare function isPluginOptionError(arg: unknown): arg is PluginOptionError;
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.isPluginOptionError = exports.reasonToString = exports.PluginOptionError = void 0;
17
+ class PluginOptionError extends Error {
18
+ constructor(option, reason) {
19
+ const detail = reason !== undefined ? reasonToString(reason) : "";
20
+ super(detail.length > 0 ?
21
+ `invalid option "${option}": ${detail}`
22
+ : `invalid option "${option}"`);
23
+ this.name = "PluginOptionError";
24
+ }
25
+ }
26
+ exports.PluginOptionError = PluginOptionError;
27
+ function reasonToString(reason) {
28
+ if (reason instanceof Error) {
29
+ return reason.message;
30
+ }
31
+ if (typeof reason === "string") {
32
+ return reason;
33
+ }
34
+ return String(reason);
35
+ }
36
+ exports.reasonToString = reasonToString;
37
+ function isPluginOptionError(arg) {
38
+ if (!(arg instanceof Error)) {
39
+ return false;
40
+ }
41
+ return arg.name === "PluginOptionError";
42
+ }
43
+ exports.isPluginOptionError = isPluginOptionError;
@@ -0,0 +1,8 @@
1
+ import type { Schema as SchemaInternal } from "./ecmascript/schema.js";
2
+ export type { Plugin } from "./plugin.js";
3
+ export { runNodeJs } from "./run-node.js";
4
+ export { createEcmaScriptPlugin } from "./create-es-plugin.js";
5
+ /**
6
+ * @deprecated Please use Schema from @bufbuild/protoplugin/ecmascript instead
7
+ */
8
+ export type Schema = SchemaInternal;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.createEcmaScriptPlugin = exports.runNodeJs = void 0;
17
+ var run_node_js_1 = require("./run-node.js");
18
+ Object.defineProperty(exports, "runNodeJs", { enumerable: true, get: function () { return run_node_js_1.runNodeJs; } });
19
+ var create_es_plugin_js_1 = require("./create-es-plugin.js");
20
+ Object.defineProperty(exports, "createEcmaScriptPlugin", { enumerable: true, get: function () { return create_es_plugin_js_1.createEcmaScriptPlugin; } });
@@ -0,0 +1,18 @@
1
+ import { CodeGeneratorRequest, CodeGeneratorResponse } from "../google/protobuf/compiler/plugin.pb.js";
2
+ /**
3
+ * Represents any code generator plugin.
4
+ */
5
+ export interface Plugin {
6
+ /**
7
+ * Name of this code generator plugin.
8
+ */
9
+ name: string;
10
+ /**
11
+ * Version of this code generator plugin.
12
+ */
13
+ version: string;
14
+ /**
15
+ * Run this plugin for the given request.
16
+ */
17
+ run(request: CodeGeneratorRequest): CodeGeneratorResponse;
18
+ }
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,12 @@
1
+ import type { Plugin } from "./plugin.js";
2
+ /**
3
+ * Run a plugin with Node.js.
4
+ *
5
+ * ```
6
+ * #!/usr/bin/env node
7
+ * const {runNodeJs} = require("@bufbuild/protoplugin");
8
+ * const {myPlugin} = require("./protoc-gen-x-plugin.js");
9
+ * runNodeJs(myPlugin);
10
+ * ```
11
+ */
12
+ export declare function runNodeJs(plugin: Plugin): void;
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.runNodeJs = void 0;
17
+ const error_js_1 = require("./error.js");
18
+ const plugin_pb_js_1 = require("../google/protobuf/compiler/plugin.pb.js");
19
+ /**
20
+ * Run a plugin with Node.js.
21
+ *
22
+ * ```
23
+ * #!/usr/bin/env node
24
+ * const {runNodeJs} = require("@bufbuild/protoplugin");
25
+ * const {myPlugin} = require("./protoc-gen-x-plugin.js");
26
+ * runNodeJs(myPlugin);
27
+ * ```
28
+ */
29
+ function runNodeJs(plugin) {
30
+ const args = process.argv.slice(2);
31
+ if ((args.length === 1 && args[0] === "-v") || args[0] === "--version") {
32
+ process.stdout.write(`${plugin.name} ${plugin.version}\n`);
33
+ process.exit(0);
34
+ }
35
+ if (args.length !== 0) {
36
+ process.stderr.write(`${plugin.name} accepts a google.protobuf.compiler.CodeGeneratorRequest on stdin and writes a CodeGeneratorResponse to stdout\n`);
37
+ process.exit(1);
38
+ }
39
+ readBytes(process.stdin)
40
+ .then((data) => {
41
+ const req = plugin_pb_js_1.CodeGeneratorRequest.fromBinary(data);
42
+ const res = plugin.run(req);
43
+ return writeBytes(process.stdout, plugin_pb_js_1.CodeGeneratorResponse.toBinary(res));
44
+ })
45
+ .then(() => process.exit(0))
46
+ .catch((reason) => {
47
+ const message = (0, error_js_1.isPluginOptionError)(reason) ? reason.message : (0, error_js_1.reasonToString)(reason);
48
+ process.stderr.write(`${plugin.name}: ${message}\n`);
49
+ process.exit(1);
50
+ return;
51
+ });
52
+ }
53
+ exports.runNodeJs = runNodeJs;
54
+ /**
55
+ * Read a stream to the end.
56
+ */
57
+ function readBytes(stream) {
58
+ return new Promise((resolve, reject) => {
59
+ const chunks = [];
60
+ stream.on("data", (chunk) => chunks.push(chunk));
61
+ stream.on("end", () => {
62
+ resolve(Buffer.concat(chunks));
63
+ });
64
+ stream.on("error", (err) => {
65
+ reject(err);
66
+ });
67
+ });
68
+ }
69
+ /**
70
+ * Write a chunk of bytes to a stream.
71
+ */
72
+ function writeBytes(stream, data) {
73
+ return new Promise((resolve, reject) => {
74
+ stream.write(data, (err) => {
75
+ if (err) {
76
+ reject(err);
77
+ }
78
+ else {
79
+ resolve();
80
+ }
81
+ });
82
+ });
83
+ }
package/dist/scalar.d.ts CHANGED
@@ -1,4 +1,56 @@
1
- import { LongType, ScalarType, ScalarValue } from "@bufbuild/protobuf";
1
+ /**
2
+ * Scalar value types. This is a subset of field types declared by protobuf
3
+ * enum google.protobuf.FieldDescriptorProto.Type The types GROUP and MESSAGE
4
+ * are omitted, but the numerical values are identical.
5
+ */
6
+ export declare enum ScalarType {
7
+ DOUBLE = 1,
8
+ FLOAT = 2,
9
+ INT64 = 3,
10
+ UINT64 = 4,
11
+ INT32 = 5,
12
+ FIXED64 = 6,
13
+ FIXED32 = 7,
14
+ BOOL = 8,
15
+ STRING = 9,
16
+ BYTES = 12,
17
+ UINT32 = 13,
18
+ SFIXED32 = 15,
19
+ SFIXED64 = 16,
20
+ SINT32 = 17,// Uses ZigZag encoding.
21
+ SINT64 = 18
22
+ }
23
+ /**
24
+ * JavaScript representation of fields with 64 bit integral types (int64, uint64,
25
+ * sint64, fixed64, sfixed64).
26
+ *
27
+ * This is a subset of google.protobuf.FieldOptions.JSType, which defines JS_NORMAL,
28
+ * JS_STRING, and JS_NUMBER. Protobuf-ES uses BigInt by default, but will use
29
+ * String if `[jstype = JS_STRING]` is specified.
30
+ *
31
+ * ```protobuf
32
+ * uint64 field_a = 1; // BigInt
33
+ * uint64 field_b = 2 [jstype = JS_NORMAL]; // BigInt
34
+ * uint64 field_b = 2 [jstype = JS_NUMBER]; // BigInt
35
+ * uint64 field_b = 2 [jstype = JS_STRING]; // String
36
+ * ```
37
+ */
38
+ export declare enum LongType {
39
+ /**
40
+ * Use JavaScript BigInt.
41
+ */
42
+ BIGINT = 0,
43
+ /**
44
+ * Use JavaScript String.
45
+ *
46
+ * Field option `[jstype = JS_STRING]`.
47
+ */
48
+ STRING = 1
49
+ }
50
+ /**
51
+ * ScalarValue maps from a scalar field type to a TypeScript value type.
52
+ */
53
+ export type ScalarValue<T = ScalarType, L extends LongType = LongType.STRING | LongType.BIGINT> = T extends ScalarType.STRING ? string : T extends ScalarType.INT32 ? number : T extends ScalarType.UINT32 ? number : T extends ScalarType.UINT32 ? number : T extends ScalarType.SINT32 ? number : T extends ScalarType.FIXED32 ? number : T extends ScalarType.SFIXED32 ? number : T extends ScalarType.FLOAT ? number : T extends ScalarType.DOUBLE ? number : T extends ScalarType.INT64 ? (L extends LongType.STRING ? string : bigint) : T extends ScalarType.SINT64 ? (L extends LongType.STRING ? string : bigint) : T extends ScalarType.SFIXED64 ? (L extends LongType.STRING ? string : bigint) : T extends ScalarType.UINT64 ? (L extends LongType.STRING ? string : bigint) : T extends ScalarType.FIXED64 ? (L extends LongType.STRING ? string : bigint) : T extends ScalarType.BOOL ? boolean : T extends ScalarType.BYTES ? Uint8Array : never;
2
54
  /**
3
55
  * Returns true if both scalar values are equal.
4
56
  */