@bufbuild/protoplugin 1.5.1 → 1.7.0

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 (64) hide show
  1. package/README.md +2 -2
  2. package/dist/cjs/create-es-plugin.d.ts +23 -3
  3. package/dist/cjs/create-es-plugin.js +26 -135
  4. package/dist/cjs/ecmascript/custom-options.d.ts +8 -0
  5. package/dist/cjs/ecmascript/custom-options.js +7 -1
  6. package/dist/cjs/ecmascript/export-declaration.d.ts +7 -0
  7. package/dist/cjs/ecmascript/export-declaration.js +24 -0
  8. package/dist/cjs/ecmascript/gencommon.d.ts +4 -5
  9. package/dist/cjs/ecmascript/gencommon.js +2 -76
  10. package/dist/cjs/ecmascript/generated-file.d.ts +48 -13
  11. package/dist/cjs/ecmascript/generated-file.js +82 -19
  12. package/dist/cjs/ecmascript/import-path.js +1 -1
  13. package/dist/cjs/ecmascript/import-symbol.js +1 -1
  14. package/dist/cjs/ecmascript/index.d.ts +22 -7
  15. package/dist/cjs/ecmascript/index.js +31 -9
  16. package/dist/cjs/ecmascript/jsdoc.d.ts +8 -0
  17. package/dist/cjs/ecmascript/jsdoc.js +93 -0
  18. package/dist/cjs/ecmascript/parameter.d.ts +13 -0
  19. package/dist/cjs/ecmascript/parameter.js +161 -0
  20. package/dist/cjs/ecmascript/reify-wkt.js +1 -1
  21. package/dist/cjs/ecmascript/runtime-imports.d.ts +1 -0
  22. package/dist/cjs/ecmascript/runtime-imports.js +2 -1
  23. package/dist/cjs/ecmascript/schema.d.ts +5 -6
  24. package/dist/cjs/ecmascript/schema.js +24 -37
  25. package/dist/cjs/ecmascript/target.js +1 -1
  26. package/dist/cjs/ecmascript/transpile.d.ts +1 -1
  27. package/dist/cjs/ecmascript/transpile.js +5 -2
  28. package/dist/cjs/error.js +1 -1
  29. package/dist/cjs/index.d.ts +5 -1
  30. package/dist/cjs/index.js +1 -1
  31. package/dist/cjs/plugin.js +1 -1
  32. package/dist/cjs/run-node.js +1 -1
  33. package/dist/esm/create-es-plugin.d.ts +23 -3
  34. package/dist/esm/create-es-plugin.js +27 -136
  35. package/dist/esm/ecmascript/custom-options.d.ts +8 -0
  36. package/dist/esm/ecmascript/custom-options.js +7 -1
  37. package/dist/esm/ecmascript/export-declaration.d.ts +7 -0
  38. package/dist/esm/ecmascript/export-declaration.js +20 -0
  39. package/dist/esm/ecmascript/gencommon.d.ts +4 -5
  40. package/dist/esm/ecmascript/gencommon.js +1 -73
  41. package/dist/esm/ecmascript/generated-file.d.ts +48 -13
  42. package/dist/esm/ecmascript/generated-file.js +83 -20
  43. package/dist/esm/ecmascript/import-path.js +1 -1
  44. package/dist/esm/ecmascript/import-symbol.js +1 -1
  45. package/dist/esm/ecmascript/index.d.ts +22 -7
  46. package/dist/esm/ecmascript/index.js +24 -3
  47. package/dist/esm/ecmascript/jsdoc.d.ts +8 -0
  48. package/dist/esm/ecmascript/jsdoc.js +89 -0
  49. package/dist/esm/ecmascript/parameter.d.ts +13 -0
  50. package/dist/esm/ecmascript/parameter.js +157 -0
  51. package/dist/esm/ecmascript/reify-wkt.js +1 -1
  52. package/dist/esm/ecmascript/runtime-imports.d.ts +1 -0
  53. package/dist/esm/ecmascript/runtime-imports.js +2 -1
  54. package/dist/esm/ecmascript/schema.d.ts +5 -6
  55. package/dist/esm/ecmascript/schema.js +24 -36
  56. package/dist/esm/ecmascript/target.js +1 -1
  57. package/dist/esm/ecmascript/transpile.d.ts +1 -1
  58. package/dist/esm/ecmascript/transpile.js +5 -2
  59. package/dist/esm/error.js +1 -1
  60. package/dist/esm/index.d.ts +5 -1
  61. package/dist/esm/index.js +1 -1
  62. package/dist/esm/plugin.js +1 -1
  63. package/dist/esm/run-node.js +1 -1
  64. package/package.json +2 -2
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- // Copyright 2021-2023 Buf Technologies, Inc.
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
3
  //
4
4
  // Licensed under the Apache License, Version 2.0 (the "License");
5
5
  // you may not use this file except in compliance with the License.
@@ -13,66 +13,53 @@
13
13
  // See the License for the specific language governing permissions and
14
14
  // limitations under the License.
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.toResponse = exports.createSchema = void 0;
16
+ exports.createSchema = void 0;
17
17
  const protobuf_1 = require("@bufbuild/protobuf");
18
18
  const generated_file_js_1 = require("./generated-file.js");
19
19
  const runtime_imports_js_1 = require("./runtime-imports.js");
20
20
  const import_symbol_js_1 = require("./import-symbol.js");
21
21
  const import_path_js_1 = require("./import-path.js");
22
- function createSchema(request, targets, tsNocheck, bootstrapWkt, rewriteImports, importExtension, keepEmptyFiles, pluginName, pluginVersion, pluginParameter) {
23
- const descriptorSet = (0, protobuf_1.createDescriptorSet)(request.protoFile);
22
+ const gencommon_1 = require("./gencommon");
23
+ function createSchema(request, parameter, pluginName, pluginVersion, featureSetDefaults) {
24
+ const descriptorSet = (0, protobuf_1.createDescriptorSet)(request.protoFile, {
25
+ featureSetDefaults,
26
+ });
24
27
  const filesToGenerate = findFilesToGenerate(descriptorSet, request);
25
- const runtime = (0, runtime_imports_js_1.createRuntimeImports)(bootstrapWkt);
28
+ const runtime = (0, runtime_imports_js_1.createRuntimeImports)(parameter.bootstrapWkt);
26
29
  const createTypeImport = (desc) => {
27
30
  const name = protobuf_1.codegenInfo.localName(desc);
28
- const from = (0, import_path_js_1.makeImportPath)(desc.file, bootstrapWkt, filesToGenerate);
31
+ const from = (0, import_path_js_1.makeImportPath)(desc.file, parameter.bootstrapWkt, filesToGenerate);
29
32
  return (0, import_symbol_js_1.createImportSymbol)(name, from);
30
33
  };
34
+ const createPreamble = (descFile) => (0, gencommon_1.makeFilePreamble)(descFile, pluginName, pluginVersion, parameter.sanitizedParameter, parameter.tsNocheck);
35
+ let target;
31
36
  const generatedFiles = [];
32
- const schema = {
33
- targets,
37
+ return {
38
+ targets: parameter.targets,
34
39
  runtime,
35
40
  proto: request,
36
41
  files: filesToGenerate,
37
42
  allFiles: descriptorSet.files,
38
43
  generateFile(name) {
39
- const genFile = (0, generated_file_js_1.createGeneratedFile)(name, (0, import_path_js_1.deriveImportPath)(name), (importPath) => (0, import_path_js_1.rewriteImportPath)(importPath, rewriteImports, importExtension), createTypeImport, runtime, {
40
- pluginName,
41
- pluginVersion,
42
- pluginParameter,
43
- tsNocheck,
44
- }, keepEmptyFiles);
44
+ if (target === undefined) {
45
+ throw new Error("prepareGenerate() must be called before generateFile()");
46
+ }
47
+ 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
48
+ (importPath) => (0, import_path_js_1.rewriteImportPath)(importPath, parameter.rewriteImports, parameter.importExtension), createTypeImport, runtime, createPreamble);
45
49
  generatedFiles.push(genFile);
46
50
  return genFile;
47
51
  },
48
- };
49
- return {
50
- schema,
51
52
  getFileInfo() {
52
- return generatedFiles.flatMap((file) => {
53
- const fileInfo = file.getFileInfo();
54
- // undefined is returned if the file has no content
55
- if (!fileInfo) {
56
- return [];
57
- }
58
- return [fileInfo];
59
- });
53
+ return generatedFiles
54
+ .map((f) => f.getFileInfo())
55
+ .filter((fi) => parameter.keepEmptyFiles || fi.content.length > 0);
56
+ },
57
+ prepareGenerate(newTarget) {
58
+ target = newTarget;
60
59
  },
61
60
  };
62
61
  }
63
62
  exports.createSchema = createSchema;
64
- function toResponse(files) {
65
- return new protobuf_1.CodeGeneratorResponse({
66
- supportedFeatures: protobuf_1.protoInt64.parse(protobuf_1.CodeGeneratorResponse_Feature.PROTO3_OPTIONAL),
67
- file: files.map((f) => {
68
- if (f.preamble !== undefined) {
69
- f.content = f.preamble + "\n" + f.content;
70
- }
71
- return f;
72
- }),
73
- });
74
- }
75
- exports.toResponse = toResponse;
76
63
  function findFilesToGenerate(descriptorSet, request) {
77
64
  const missing = request.fileToGenerate.filter((fileToGenerate) => descriptorSet.files.every((file) => fileToGenerate !== file.name + ".proto"));
78
65
  if (missing.length) {
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- // Copyright 2021-2023 Buf Technologies, Inc.
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
3
  //
4
4
  // Licensed under the Apache License, Version 2.0 (the "License");
5
5
  // you may not use this file except in compliance with the License.
@@ -1,2 +1,2 @@
1
1
  import type { FileInfo } from "./generated-file.js";
2
- export declare function transpile(files: FileInfo[], transpileJs: boolean, transpileDts: boolean): FileInfo[];
2
+ export declare function transpile(files: FileInfo[], transpileJs: boolean, transpileDts: boolean, jsImportStyle: "module" | "legacy_commonjs"): FileInfo[];
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- // Copyright 2021-2023 Buf Technologies, Inc.
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
3
  //
4
4
  // Licensed under the Apache License, Version 2.0 (the "License");
5
5
  // you may not use this file except in compliance with the License.
@@ -82,8 +82,11 @@ function createTranspiler(options, files) {
82
82
  host: host.compilerHost,
83
83
  });
84
84
  }
85
- function transpile(files, transpileJs, transpileDts) {
85
+ function transpile(files, transpileJs, transpileDts, jsImportStyle) {
86
86
  const options = Object.assign(Object.assign({}, defaultOptions), { declaration: transpileDts, emitDeclarationOnly: transpileDts && !transpileJs });
87
+ if (jsImportStyle == "legacy_commonjs") {
88
+ options.module = typescript_1.default.ModuleKind.CommonJS;
89
+ }
87
90
  // Create the transpiler (a ts.Program object)
88
91
  const program = createTranspiler(options, files);
89
92
  const results = [];
package/dist/cjs/error.js CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- // Copyright 2021-2023 Buf Technologies, Inc.
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
3
  //
4
4
  // Licensed under the Apache License, Version 2.0 (the "License");
5
5
  // you may not use this file except in compliance with the License.
@@ -1,4 +1,8 @@
1
+ import { Schema as SchemaInternal } from "./ecmascript/schema.js";
1
2
  export { Plugin } from "./plugin.js";
2
- export { Schema } from "./ecmascript/schema.js";
3
3
  export { runNodeJs } from "./run-node.js";
4
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;
package/dist/cjs/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- // Copyright 2021-2023 Buf Technologies, Inc.
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
3
  //
4
4
  // Licensed under the Apache License, Version 2.0 (the "License");
5
5
  // you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- // Copyright 2021-2023 Buf Technologies, Inc.
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
3
  //
4
4
  // Licensed under the Apache License, Version 2.0 (the "License");
5
5
  // you may not use this file except in compliance with the License.
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
- // Copyright 2021-2023 Buf Technologies, Inc.
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
3
  //
4
4
  // Licensed under the Apache License, Version 2.0 (the "License");
5
5
  // you may not use this file except in compliance with the License.
@@ -1,6 +1,7 @@
1
1
  import { Schema } from "./ecmascript/schema.js";
2
2
  import type { FileInfo } from "./ecmascript/generated-file.js";
3
3
  import type { Plugin } from "./plugin.js";
4
+ import type { FeatureSetDefaults } from "@bufbuild/protobuf";
4
5
  interface PluginInit {
5
6
  /**
6
7
  * Name of this code generator plugin.
@@ -15,6 +16,20 @@ interface PluginInit {
15
16
  * options.
16
17
  */
17
18
  parseOption?: (key: string, value: string) => void;
19
+ /**
20
+ * Whether the plugin supports editions.
21
+ */
22
+ supportsEditions?: boolean;
23
+ /**
24
+ * By default, plugins support all editions supported by createDescriptorSet()
25
+ * from @bufbuild/protobuf, if supportsEditions is enabled.
26
+ *
27
+ * This option can be used to limit support for specific editions, by providing
28
+ * your own google.protobuf.FeatureSetDefaults generated by protoc with the
29
+ * flags --experimental_edition_defaults_out, --experimental_edition_defaults_minimum,
30
+ * and --experimental_edition_defaults_maximum.
31
+ */
32
+ featureSetDefaults?: FeatureSetDefaults;
18
33
  /**
19
34
  * A function which will generate TypeScript files based on proto input.
20
35
  * This function will be invoked by the plugin framework when the plugin runs.
@@ -45,14 +60,19 @@ interface PluginInit {
45
60
  */
46
61
  generateDts?: (schema: Schema, target: "dts") => void;
47
62
  /**
48
- * A optional function which will transpile a given set of files.
63
+ * An optional function which will transpile a given set of files.
49
64
  *
50
- * This funcion is meant to be used in place of either generateJs,
65
+ * This function is meant to be used in place of either generateJs,
51
66
  * generateDts, or both. However, those functions will take precedence.
52
67
  * This means that if generateJs, generateDts, and this transpile function
53
68
  * are all provided, this transpile function will be ignored.
69
+ *
70
+ * If jsImportStyle is "module" (the standard behavior), the function is
71
+ * expected to use ECMAScript module import and export statements when
72
+ * transpiling to JS. If jsImportStyle is "legacy_commonjs", the function is
73
+ * expected to use CommonJs require() and exports when transpiling to JS.
54
74
  */
55
- transpile?: (files: FileInfo[], transpileJs: boolean, transpileDts: boolean) => FileInfo[];
75
+ transpile?: (files: FileInfo[], transpileJs: boolean, transpileDts: boolean, jsImportStyle: "module" | "legacy_commonjs") => FileInfo[];
56
76
  }
57
77
  /**
58
78
  * Create a new code generator plugin for ECMAScript.
@@ -1,4 +1,4 @@
1
- // Copyright 2021-2023 Buf Technologies, Inc.
1
+ // Copyright 2021-2024 Buf Technologies, Inc.
2
2
  //
3
3
  // Licensed under the Apache License, Version 2.0 (the "License");
4
4
  // you may not use this file except in compliance with the License.
@@ -11,9 +11,10 @@
11
11
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
- import { createSchema, toResponse } from "./ecmascript/schema.js";
14
+ import { createSchema } from "./ecmascript/schema.js";
15
15
  import { transpile } from "./ecmascript/transpile.js";
16
- import { PluginOptionError } from "./error.js";
16
+ import { parseParameter } from "./ecmascript/parameter.js";
17
+ import { CodeGeneratorResponse, CodeGeneratorResponse_Feature, protoInt64, } from "@bufbuild/protobuf";
17
18
  /**
18
19
  * Create a new code generator plugin for ECMAScript.
19
20
  * The plugin can generate JavaScript, TypeScript, or TypeScript declaration
@@ -26,9 +27,9 @@ export function createEcmaScriptPlugin(init) {
26
27
  name: init.name,
27
28
  version: init.version,
28
29
  run(req) {
29
- var _a;
30
- const { targets, tsNocheck, bootstrapWkt, rewriteImports, importExtension, keepEmptyFiles, pluginParameter, } = parseParameter(req.parameter, init.parseOption);
31
- const { schema, getFileInfo } = createSchema(req, targets, tsNocheck, bootstrapWkt, rewriteImports, importExtension, keepEmptyFiles, init.name, init.version, pluginParameter);
30
+ var _a, _b;
31
+ const parameter = parseParameter(req.parameter, init.parseOption);
32
+ const schema = createSchema(req, parameter, init.name, init.version, init.featureSetDefaults);
32
33
  const targetTs = schema.targets.includes("ts");
33
34
  const targetJs = schema.targets.includes("js");
34
35
  const targetDts = schema.targets.includes("dts");
@@ -42,6 +43,7 @@ export function createEcmaScriptPlugin(init) {
42
43
  if (targetTs ||
43
44
  (targetJs && !init.generateJs) ||
44
45
  (targetDts && !init.generateDts)) {
46
+ schema.prepareGenerate("ts");
45
47
  init.generateTs(schema, "ts");
46
48
  // Save off the generated TypeScript files so that we can pass these
47
49
  // to the transpilation process if necessary. We do not want to pass
@@ -53,10 +55,11 @@ export function createEcmaScriptPlugin(init) {
53
55
  // a generateJs function and expect to transpile declarations.
54
56
  // 3. Transpiling is somewhat expensive and situations with an
55
57
  // extremely large amount of files could have performance impacts.
56
- tsFiles = getFileInfo();
58
+ tsFiles = schema.getFileInfo();
57
59
  }
58
60
  if (targetJs) {
59
61
  if (init.generateJs) {
62
+ schema.prepareGenerate("js");
60
63
  init.generateJs(schema, "js");
61
64
  }
62
65
  else {
@@ -65,6 +68,7 @@ export function createEcmaScriptPlugin(init) {
65
68
  }
66
69
  if (targetDts) {
67
70
  if (init.generateDts) {
71
+ schema.prepareGenerate("dts");
68
72
  init.generateDts(schema, "dts");
69
73
  }
70
74
  else {
@@ -76,7 +80,7 @@ export function createEcmaScriptPlugin(init) {
76
80
  // generated TypeScript files to assist in transpilation. If they were
77
81
  // generated but not specified in the target out, we shouldn't produce
78
82
  // these files in the CodeGeneratorResponse.
79
- let files = getFileInfo();
83
+ let files = schema.getFileInfo();
80
84
  if (!targetTs && tsFiles.length > 0) {
81
85
  files = files.filter((file) => !tsFiles.some((tsFile) => tsFile.name === file.name));
82
86
  }
@@ -86,139 +90,26 @@ export function createEcmaScriptPlugin(init) {
86
90
  if (transpileJs || transpileDts) {
87
91
  const transpileFn = (_a = init.transpile) !== null && _a !== void 0 ? _a : transpile;
88
92
  // Transpile the TypeScript files and add to the master list of files
89
- const transpiledFiles = transpileFn(tsFiles, transpileJs, transpileDts);
93
+ const transpiledFiles = transpileFn(tsFiles, transpileJs, transpileDts, parameter.jsImportStyle);
90
94
  files.push(...transpiledFiles);
91
95
  }
92
- return toResponse(files);
96
+ return toResponse(files, (_b = init.supportsEditions) !== null && _b !== void 0 ? _b : false);
93
97
  },
94
98
  };
95
99
  }
96
- function parseParameter(parameter, parseOption) {
97
- let targets = ["js", "dts"];
98
- let tsNocheck = true;
99
- let bootstrapWkt = false;
100
- let keepEmptyFiles = false;
101
- const rewriteImports = [];
102
- let importExtension = ".js";
103
- const rawParameters = [];
104
- for (const { key, value, raw } of splitParameter(parameter)) {
105
- // Whether this key/value plugin parameter pair should be
106
- // printed to the generated file preamble
107
- let printToFile = true;
108
- switch (key) {
109
- case "target":
110
- targets = [];
111
- for (const rawTarget of value.split("+")) {
112
- switch (rawTarget) {
113
- case "js":
114
- case "ts":
115
- case "dts":
116
- if (targets.indexOf(rawTarget) < 0) {
117
- targets.push(rawTarget);
118
- }
119
- break;
120
- default:
121
- throw new PluginOptionError(raw);
122
- }
123
- }
124
- value.split("+");
125
- break;
126
- case "ts_nocheck":
127
- switch (value) {
128
- case "true":
129
- case "1":
130
- tsNocheck = true;
131
- break;
132
- case "false":
133
- case "0":
134
- tsNocheck = false;
135
- break;
136
- default:
137
- throw new PluginOptionError(raw);
138
- }
139
- break;
140
- case "bootstrap_wkt":
141
- switch (value) {
142
- case "true":
143
- case "1":
144
- bootstrapWkt = true;
145
- break;
146
- case "false":
147
- case "0":
148
- bootstrapWkt = false;
149
- break;
150
- default:
151
- throw new PluginOptionError(raw);
152
- }
153
- break;
154
- case "rewrite_imports": {
155
- const parts = value.split(":");
156
- if (parts.length !== 2) {
157
- throw new PluginOptionError(raw, "must be in the form of <pattern>:<target>");
158
- }
159
- const [pattern, target] = parts;
160
- rewriteImports.push({ pattern, target });
161
- // rewrite_imports can be noisy and is more of an implementation detail
162
- // so we strip it out of the preamble
163
- printToFile = false;
164
- break;
165
- }
166
- case "import_extension": {
167
- importExtension = value === "none" ? "" : value;
168
- break;
169
- }
170
- case "keep_empty_files": {
171
- switch (value) {
172
- case "true":
173
- case "1":
174
- keepEmptyFiles = true;
175
- break;
176
- case "false":
177
- case "0":
178
- keepEmptyFiles = false;
179
- break;
180
- default:
181
- throw new PluginOptionError(raw);
182
- }
183
- break;
184
- }
185
- default:
186
- if (parseOption === undefined) {
187
- throw new PluginOptionError(raw);
188
- }
189
- try {
190
- parseOption(key, value);
191
- }
192
- catch (e) {
193
- throw new PluginOptionError(raw, e);
194
- }
195
- break;
196
- }
197
- if (printToFile) {
198
- rawParameters.push(raw);
199
- }
200
- }
201
- const pluginParameter = rawParameters.join(",");
202
- return {
203
- targets,
204
- tsNocheck,
205
- bootstrapWkt,
206
- rewriteImports,
207
- importExtension,
208
- keepEmptyFiles,
209
- pluginParameter,
210
- };
211
- }
212
- function splitParameter(parameter) {
213
- if (parameter == undefined) {
214
- return [];
100
+ function toResponse(files, supportsEditions) {
101
+ let supportedFeatures = CodeGeneratorResponse_Feature.PROTO3_OPTIONAL;
102
+ if (supportsEditions) {
103
+ supportedFeatures =
104
+ supportedFeatures | CodeGeneratorResponse_Feature.SUPPORTS_EDITIONS;
215
105
  }
216
- return parameter.split(",").map((raw) => {
217
- const i = raw.indexOf("=");
218
- return {
219
- key: i === -1 ? raw : raw.substring(0, i),
220
- value: i === -1 ? "" : raw.substring(i + 1),
221
- raw,
222
- };
106
+ return new CodeGeneratorResponse({
107
+ supportedFeatures: protoInt64.parse(supportedFeatures),
108
+ file: files.map((f) => {
109
+ if (f.preamble !== undefined) {
110
+ f.content = f.preamble + "\n" + f.content;
111
+ }
112
+ return f;
113
+ }),
223
114
  });
224
115
  }
@@ -1,12 +1,16 @@
1
1
  import type { AnyDesc } from "@bufbuild/protobuf";
2
2
  import { Message, MessageType, ScalarType } from "@bufbuild/protobuf";
3
3
  /**
4
+ * @deprecated Please use extensions instead.
5
+ *
4
6
  * Returns the value of a custom option with a scalar type.
5
7
  *
6
8
  * If no option is found, returns undefined.
7
9
  */
8
10
  export declare function findCustomScalarOption<T extends ScalarType>(desc: AnyDesc, extensionNumber: number, scalarType: T): ScalarValue<T> | undefined;
9
11
  /**
12
+ * @deprecated Please use extensions instead.
13
+ *
10
14
  * Returns the value of a custom message option for the given descriptor and
11
15
  * extension number.
12
16
  * The msgType param is then used to deserialize the message for returning to
@@ -19,6 +23,8 @@ export declare function findCustomScalarOption<T extends ScalarType>(desc: AnyDe
19
23
  */
20
24
  export declare function findCustomMessageOption<T extends Message<T>>(desc: AnyDesc, extensionNumber: number, msgType: MessageType<T>): T | undefined;
21
25
  /**
26
+ * @deprecated Please use extensions instead.
27
+ *
22
28
  * Returns the value of a custom enum option for the given descriptor and
23
29
  * extension number.
24
30
  *
@@ -26,6 +32,8 @@ export declare function findCustomMessageOption<T extends Message<T>>(desc: AnyD
26
32
  */
27
33
  export declare function findCustomEnumOption(desc: AnyDesc, extensionNumber: number): number | undefined;
28
34
  /**
35
+ * @deprecated Please use extensions instead.
36
+ *
29
37
  * ScalarValue is a conditional type that pairs a ScalarType value with its concrete type.
30
38
  */
31
39
  type ScalarValue<T> = 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 ? bigint | string : T extends ScalarType.SINT64 ? bigint | string : T extends ScalarType.SFIXED64 ? bigint | string : T extends ScalarType.UINT64 ? bigint | string : T extends ScalarType.FIXED64 ? bigint | string : T extends ScalarType.BOOL ? boolean : T extends ScalarType.BYTES ? Uint8Array : never;
@@ -1,4 +1,4 @@
1
- // Copyright 2021-2023 Buf Technologies, Inc.
1
+ // Copyright 2021-2024 Buf Technologies, Inc.
2
2
  //
3
3
  // Licensed under the Apache License, Version 2.0 (the "License");
4
4
  // you may not use this file except in compliance with the License.
@@ -13,6 +13,8 @@
13
13
  // limitations under the License.
14
14
  import { proto3, BinaryReader, ScalarType, } from "@bufbuild/protobuf";
15
15
  /**
16
+ * @deprecated Please use extensions instead.
17
+ *
16
18
  * Returns the value of a custom option with a scalar type.
17
19
  *
18
20
  * If no option is found, returns undefined.
@@ -59,6 +61,8 @@ export function findCustomScalarOption(desc, extensionNumber, scalarType) {
59
61
  return undefined;
60
62
  }
61
63
  /**
64
+ * @deprecated Please use extensions instead.
65
+ *
62
66
  * Returns the value of a custom message option for the given descriptor and
63
67
  * extension number.
64
68
  * The msgType param is then used to deserialize the message for returning to
@@ -84,6 +88,8 @@ export function findCustomMessageOption(desc, extensionNumber, msgType) {
84
88
  return undefined;
85
89
  }
86
90
  /**
91
+ * @deprecated Please use extensions instead.
92
+ *
87
93
  * Returns the value of a custom enum option for the given descriptor and
88
94
  * extension number.
89
95
  *
@@ -0,0 +1,7 @@
1
+ import { DescEnum, DescExtension, DescMessage } from "@bufbuild/protobuf";
2
+ export type ExportDeclaration = {
3
+ readonly kind: "es_export_decl";
4
+ declaration: string;
5
+ name: string | DescMessage | DescEnum | DescExtension;
6
+ };
7
+ export declare function createExportDeclaration(declaration: string, name: ExportDeclaration["name"]): ExportDeclaration;
@@ -0,0 +1,20 @@
1
+ // Copyright 2021-2024 Buf Technologies, Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ export function createExportDeclaration(declaration, name) {
15
+ return {
16
+ kind: "es_export_decl",
17
+ declaration,
18
+ name,
19
+ };
20
+ }
@@ -1,20 +1,19 @@
1
- import { DescEnum, DescEnumValue, DescField, DescFile, DescMessage, DescMethod, DescOneof, DescService, LongType, ScalarType } from "@bufbuild/protobuf";
1
+ import type { DescExtension } from "@bufbuild/protobuf";
2
+ import { DescField, DescFile, LongType, ScalarType } from "@bufbuild/protobuf";
2
3
  import type { GeneratedFile, Printable } from "./generated-file.js";
3
4
  import type { ImportSymbol } from "./import-symbol.js";
4
5
  export declare function makeFilePreamble(file: DescFile, pluginName: string, pluginVersion: string, parameter: string, tsNoCheck: boolean): string;
5
- export declare function createJsDocBlock(text: string, indentation?: string): Printable;
6
- export declare function makeJsDoc(desc: DescEnum | DescEnumValue | DescMessage | DescOneof | DescField | DescService | DescMethod, indentation?: string): Printable;
7
6
  /**
8
7
  * Returns an expression for the TypeScript typing of a field,
9
8
  * and whether the property should be optional.
10
9
  */
11
- export declare function getFieldTyping(field: DescField, file: GeneratedFile): {
10
+ export declare function getFieldTyping(field: DescField | DescExtension, file: GeneratedFile): {
12
11
  typing: Printable;
13
12
  optional: boolean;
14
13
  };
15
14
  export declare function scalarTypeScriptType(type: ScalarType, longType: LongType): Printable;
16
15
  export declare function literalString(value: string): string;
17
- export declare function getFieldExplicitDefaultValue(field: DescField, protoInt64Symbol: ImportSymbol): Printable | undefined;
16
+ export declare function getFieldExplicitDefaultValue(field: DescField | DescExtension, protoInt64Symbol: ImportSymbol): Printable | undefined;
18
17
  export declare function getFieldIntrinsicDefaultValue(field: DescField): {
19
18
  defaultValue: Printable | undefined;
20
19
  typingInferrable: boolean;
@@ -1,4 +1,4 @@
1
- // Copyright 2021-2023 Buf Technologies, Inc.
1
+ // Copyright 2021-2024 Buf Technologies, Inc.
2
2
  //
3
3
  // Licensed under the Apache License, Version 2.0 (the "License");
4
4
  // you may not use this file except in compliance with the License.
@@ -69,78 +69,6 @@ export function makeFilePreamble(file, pluginName, pluginVersion, parameter, tsN
69
69
  writeLeadingComments(file.getPackageComments());
70
70
  return trimSuffix(builder.join(""), "\n");
71
71
  }
72
- export function createJsDocBlock(text, indentation = "") {
73
- if (text.trim().length == 0) {
74
- return [];
75
- }
76
- let lines = text.split("\n");
77
- if (lines.length === 0) {
78
- return [];
79
- }
80
- lines = lines.map((l) => l.split("*/").join("*\\/"));
81
- lines = lines.map((l) => (l.length > 0 ? " " + l : l));
82
- // prettier-ignore
83
- return [
84
- `${indentation}/**\n`,
85
- ...lines.map((l) => `${indentation} *${l}\n`),
86
- `${indentation} */`
87
- ];
88
- }
89
- export function makeJsDoc(desc, indentation = "") {
90
- var _a, _b;
91
- const comments = desc.getComments();
92
- let text = "";
93
- if (comments.leading !== undefined) {
94
- text += comments.leading;
95
- if (text.endsWith("\n")) {
96
- text = text.substring(0, text.length - 1);
97
- }
98
- }
99
- if (comments.trailing !== undefined) {
100
- if (text.length > 0) {
101
- text += "\n\n";
102
- }
103
- text += comments.trailing;
104
- if (text.endsWith("\n")) {
105
- text = text.substring(0, text.length - 1);
106
- }
107
- }
108
- if (text.length > 0) {
109
- text += "\n\n";
110
- }
111
- text = text
112
- .split("\n")
113
- .map((line) => (line.startsWith(" ") ? line.substring(1) : line))
114
- .join("\n");
115
- switch (desc.kind) {
116
- case "enum_value":
117
- text += `@generated from enum value: ${desc.declarationString()};`;
118
- break;
119
- case "field":
120
- text += `@generated from field: ${desc.declarationString()};`;
121
- break;
122
- default:
123
- text += `@generated from ${desc.toString()}`;
124
- break;
125
- }
126
- let deprecated = desc.deprecated;
127
- switch (desc.kind) {
128
- case "enum":
129
- case "message":
130
- case "service":
131
- deprecated = deprecated || ((_b = (_a = desc.file.proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false);
132
- break;
133
- default:
134
- break;
135
- }
136
- if (deprecated) {
137
- text += "\n@deprecated";
138
- }
139
- if (text.length > 0) {
140
- return createJsDocBlock(text, indentation);
141
- }
142
- return [];
143
- }
144
72
  /**
145
73
  * Returns an expression for the TypeScript typing of a field,
146
74
  * and whether the property should be optional.