@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
@@ -14,25 +14,20 @@
14
14
  // See the License for the specific language governing permissions and
15
15
  // limitations under the License.
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.getFieldInfoLiteral = exports.createTypeImport = exports.generateFieldInfo = exports.makeImportPath = exports.generateTs = void 0;
18
- const protobuf_1 = require("@bufbuild/protobuf");
19
- const ecmascript_1 = require("@bufbuild/protoplugin/ecmascript");
20
- const editions_js_1 = require("./editions.js");
21
- const util_js_1 = require("./util.js");
22
- const libPkg = "@aptre/protobuf-es-lite";
23
- const MessageImport = (0, ecmascript_1.createImportSymbol)("Message", libPkg);
24
- const MessageTypeImport = (0, ecmascript_1.createImportSymbol)("MessageType", libPkg);
25
- const CreateMessageTypeImport = (0, ecmascript_1.createImportSymbol)("createMessageType", libPkg);
26
- const PartialFieldInfo = (0, ecmascript_1.createImportSymbol)("PartialFieldInfo", libPkg);
27
- const CreateEnumTypeImport = (0, ecmascript_1.createImportSymbol)("createEnumType", libPkg);
17
+ exports.getFieldInfoLiteral = exports.createTypeImport = exports.generateFieldInfo = exports.makeImportPath = exports.checkSupportedSyntax = exports.generateTs = void 0;
18
+ const index_js_1 = require("../protoplugin/ecmascript/index.js");
19
+ const util_js_1 = require("../util.js");
20
+ const names_js_1 = require("../names.js");
21
+ const scalar_js_1 = require("../scalar.js");
22
+ const descriptor_pb_js_1 = require("../google/protobuf/descriptor.pb.js");
28
23
  function generateTs(schema) {
29
24
  for (const file of schema.files) {
30
- const f = schema.generateFile(file.name + "_pb.ts");
25
+ const f = schema.generateFile(file.name + ".pb.ts");
31
26
  f.preamble(file);
32
27
  f.print(`export const protobufPackage = "${file.proto.package}";`);
33
28
  f.print();
34
29
  for (const enumeration of file.enums) {
35
- generateEnum(f, enumeration);
30
+ generateEnum(schema, f, enumeration);
36
31
  }
37
32
  const messageTypes = [];
38
33
  const dependencies = new Map();
@@ -41,7 +36,7 @@ function generateTs(schema) {
41
36
  return;
42
37
  }
43
38
  for (const nestedEnum of message.nestedEnums) {
44
- generateEnum(f, nestedEnum);
39
+ generateEnum(schema, f, nestedEnum);
45
40
  }
46
41
  messageTypes.push(message);
47
42
  const deps = new Set();
@@ -90,7 +85,7 @@ function topologicalSort(messages, dependencies) {
90
85
  }
91
86
  return result;
92
87
  }
93
- function generateEnum(f, enumeration) {
88
+ function generateEnum(schema, f, enumeration) {
94
89
  f.print(f.jsDoc(enumeration));
95
90
  f.print(f.exportDecl("enum", enumeration), " {");
96
91
  for (const value of enumeration.values) {
@@ -98,23 +93,40 @@ function generateEnum(f, enumeration) {
98
93
  f.print();
99
94
  }
100
95
  f.print(f.jsDoc(value, " "));
101
- f.print(" ", (0, ecmascript_1.localName)(value), " = ", value.number, ",");
96
+ f.print(" ", (0, names_js_1.localName)(value), " = ", value.number, ",");
102
97
  }
103
98
  f.print("}");
104
99
  f.print();
105
100
  f.print("// ", enumeration, "_Enum is the enum type for ", enumeration, ".");
106
- f.print(f.exportDecl("const", enumeration.name + "_Enum"), " = ", CreateEnumTypeImport, "(", f.string(enumeration.typeName), ", [");
101
+ f.print(f.exportDecl("const", enumeration), "_Enum", " = ", schema.runtime.createEnumType, "(", f.string(enumeration.typeName), ", [");
107
102
  for (const value of enumeration.values) {
108
103
  f.print(" { no: ", value.number, ', name: "', value.name, '" },');
109
104
  }
110
105
  f.print("]);");
111
106
  f.print();
112
107
  }
108
+ function checkSupportedSyntax(file) {
109
+ if (file.syntax === "editions") {
110
+ throw new Error(`${file.proto.name ?? ""}: syntax "editions" is not supported`);
111
+ }
112
+ }
113
+ exports.checkSupportedSyntax = checkSupportedSyntax;
113
114
  function generateMessage(schema, f, message) {
114
115
  // check if we support this runtime
115
- (0, editions_js_1.getNonEditionRuntime)(schema, message.file);
116
+ checkSupportedSyntax(message.file);
116
117
  f.print(f.jsDoc(message));
117
- f.print(f.exportDecl("interface", message), " extends ", MessageImport, "<", message, "> {");
118
+ /*
119
+ f.print(
120
+ f.exportDecl("interface", message),
121
+ " extends ",
122
+ schema.runtime.Message,
123
+ "<",
124
+ message,
125
+ ">",
126
+ " {",
127
+ );
128
+ */
129
+ f.print(f.exportDecl("type", message), " = ", schema.runtime.Message, "<{");
118
130
  for (const field of message.fields) {
119
131
  generateField(f, field);
120
132
  }
@@ -122,16 +134,16 @@ function generateMessage(schema, f, message) {
122
134
  generateOneof(f, oneof);
123
135
  }
124
136
  f.print();
125
- f.print("}");
137
+ f.print("}>;");
126
138
  f.print();
127
- f.print(f.exportDecl("const", message), ": ", MessageTypeImport, "<", message, "> = ", CreateMessageTypeImport, "(");
139
+ f.print(f.exportDecl("const", message), ": ", schema.runtime.MessageType, "<", message, "> = ", schema.runtime.createMessageType, "(");
128
140
  f.print(" {");
129
141
  f.print(" typeName: ", f.string(message.typeName), ",");
130
142
  f.print(" fields: [");
131
143
  for (const field of message.fields) {
132
144
  generateFieldInfo(f, field);
133
145
  }
134
- f.print(" ] as readonly ", PartialFieldInfo, "[],");
146
+ f.print(" ] as readonly ", schema.runtime.PartialFieldInfo, "[],");
135
147
  f.print(" packedByDefault: ", message.file.proto.syntax === "proto3", ",");
136
148
  f.print(" },");
137
149
  f.print(");");
@@ -143,7 +155,7 @@ function generateField(f, field) {
143
155
  }
144
156
  f.print(f.jsDoc(field, " "));
145
157
  const { typing } = (0, util_js_1.getFieldTypeInfo)(field);
146
- f.print(" ", (0, ecmascript_1.localName)(field), "?: ", typing, ";");
158
+ f.print(" ", (0, names_js_1.localName)(field), "?: ", typing, ";");
147
159
  }
148
160
  function generateOneof(f, oneof) {
149
161
  f.print();
@@ -158,7 +170,7 @@ function generateOneof(f, oneof) {
158
170
  `\n value: `,
159
171
  typing,
160
172
  `;\n case: "`,
161
- (0, ecmascript_1.localName)(field),
173
+ (0, names_js_1.localName)(field),
162
174
  `";\n }`,
163
175
  ];
164
176
  })
@@ -166,7 +178,7 @@ function generateOneof(f, oneof) {
166
178
  f.print(" ", oneof.name, "?: {\n value?: undefined,\n case: undefined\n }", oneOfCases, ";");
167
179
  }
168
180
  function makeImportPath(file) {
169
- return "./" + file.name + "_pb.js";
181
+ return "./" + file.name + ".pb.js";
170
182
  }
171
183
  exports.makeImportPath = makeImportPath;
172
184
  function generateFieldInfo(f, field) {
@@ -174,12 +186,12 @@ function generateFieldInfo(f, field) {
174
186
  }
175
187
  exports.generateFieldInfo = generateFieldInfo;
176
188
  const createTypeImport = (desc) => {
177
- var name = (0, ecmascript_1.localName)(desc);
189
+ var name = (0, names_js_1.localName)(desc);
178
190
  if (desc.kind === "enum") {
179
191
  name += "_Enum";
180
192
  }
181
193
  const from = makeImportPath(desc.file);
182
- return (0, ecmascript_1.createImportSymbol)(name, from);
194
+ return (0, index_js_1.createImportSymbol)(name, from);
183
195
  };
184
196
  exports.createTypeImport = createTypeImport;
185
197
  function getFieldInfoLiteral(field) {
@@ -193,19 +205,19 @@ function getFieldInfoLiteral(field) {
193
205
  }
194
206
  switch (field.fieldKind) {
195
207
  case "scalar":
196
- e.push(`kind: "scalar", T: `, field.scalar, ` /* ScalarType.`, protobuf_1.ScalarType[field.scalar], ` */, `);
197
- if (field.longType != protobuf_1.LongType.BIGINT) {
198
- e.push(`L: `, field.longType, ` /* LongType.`, protobuf_1.LongType[field.longType], ` */, `);
208
+ e.push(`kind: "scalar", T: `, field.scalar, ` /* ScalarType.`, scalar_js_1.ScalarType[field.scalar], ` */, `);
209
+ if (field.longType != scalar_js_1.LongType.BIGINT) {
210
+ e.push(`L: `, field.longType, ` /* LongType.`, scalar_js_1.LongType[field.longType], ` */, `);
199
211
  }
200
212
  break;
201
213
  case "map":
202
- e.push(`kind: "map", K: `, field.mapKey, ` /* ScalarType.`, protobuf_1.ScalarType[field.mapKey], ` */, `);
214
+ e.push(`kind: "map", K: `, field.mapKey, ` /* ScalarType.`, scalar_js_1.ScalarType[field.mapKey], ` */, `);
203
215
  switch (field.mapValue.kind) {
204
216
  case "scalar":
205
- e.push(`V: {kind: "scalar", T: `, field.mapValue.scalar, ` /* ScalarType.`, protobuf_1.ScalarType[field.mapValue.scalar], ` */}, `);
217
+ e.push(`V: {kind: "scalar", T: `, field.mapValue.scalar, ` /* ScalarType.`, scalar_js_1.ScalarType[field.mapValue.scalar], ` */}, `);
206
218
  break;
207
219
  case "message":
208
- e.push(`V: {kind: "message", T: `, field.mapValue.message, `}, `);
220
+ e.push(`V: {kind: "message", T: () => `, field.mapValue.message, `}, `);
209
221
  break;
210
222
  case "enum":
211
223
  e.push(`V: {kind: "enum", T: `, field.mapValue.enum, `}, `);
@@ -213,8 +225,8 @@ function getFieldInfoLiteral(field) {
213
225
  }
214
226
  break;
215
227
  case "message":
216
- e.push(`kind: "message", T: `, field.message, `, `);
217
- if (field.proto.type === protobuf_1.FieldDescriptorProto_Type.GROUP) {
228
+ e.push(`kind: "message", T: () => `, field.message, `, `);
229
+ if (field.proto.type === descriptor_pb_js_1.FieldDescriptorProto_Type.GROUP) {
218
230
  e.push(`delimited: true, `);
219
231
  }
220
232
  break;
@@ -231,7 +243,7 @@ function getFieldInfoLiteral(field) {
231
243
  if (field.optional) {
232
244
  e.push(`opt: true, `);
233
245
  }
234
- else if (field.proto.label === protobuf_1.FieldDescriptorProto_Label.REQUIRED) {
246
+ else if (field.proto.label === descriptor_pb_js_1.FieldDescriptorProto_Label.REQUIRED) {
235
247
  e.push(`req: true, `);
236
248
  }
237
249
  const defaultValue = (0, util_js_1.getFieldDefaultValueExpression)(field);
@@ -0,0 +1,83 @@
1
+ import type { Schema } from "./ecmascript/schema.js";
2
+ import type { FileInfo } from "./ecmascript/generated-file.js";
3
+ import type { Plugin } from "./plugin.js";
4
+ import { FeatureSetDefaults } from "../google/protobuf/descriptor.pb.js";
5
+ interface PluginInit {
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
+ * An optional parsing function which can be used to parse your own plugin
16
+ * options.
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
+ * 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;
33
+ /**
34
+ * A function which will generate TypeScript files based on proto input.
35
+ * This function will be invoked by the plugin framework when the plugin runs.
36
+ *
37
+ * Note that this is required to be provided for plugin initialization.
38
+ */
39
+ generateTs: (schema: Schema, target: "ts") => void;
40
+ /**
41
+ * A optional function which will generate JavaScript files based on proto
42
+ * input. This function will be invoked by the plugin framework when the
43
+ * plugin runs.
44
+ *
45
+ * If this function is not provided, the plugin framework will then check if
46
+ * a transpile function is provided. If so, it will be invoked to transpile
47
+ * JavaScript files. If not, the plugin framework will transpile the files
48
+ * itself.
49
+ */
50
+ generateJs?: (schema: Schema, target: "js") => void;
51
+ /**
52
+ * A optional function which will generate TypeScript declaration files
53
+ * based on proto input. This function will be invoked by the plugin
54
+ * framework when the plugin runs.
55
+ *
56
+ * If this function is not provided, the plugin framework will then check if
57
+ * a transpile function is provided. If so, it will be invoked to transpile
58
+ * declaration files. If not, the plugin framework will transpile the files
59
+ * itself.
60
+ */
61
+ generateDts?: (schema: Schema, target: "dts") => void;
62
+ /**
63
+ * An optional function which will transpile a given set of files.
64
+ *
65
+ * This function is meant to be used in place of either generateJs,
66
+ * generateDts, or both. However, those functions will take precedence.
67
+ * This means that if generateJs, generateDts, and this transpile function
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.
74
+ */
75
+ transpile?: (files: FileInfo[], transpileJs: boolean, transpileDts: boolean, jsImportStyle: "module" | "legacy_commonjs") => FileInfo[];
76
+ }
77
+ /**
78
+ * Create a new code generator plugin for ECMAScript.
79
+ * The plugin can generate JavaScript, TypeScript, or TypeScript declaration
80
+ * files.
81
+ */
82
+ export declare function createEcmaScriptPlugin(init: PluginInit): Plugin;
83
+ export {};
@@ -0,0 +1,119 @@
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 = void 0;
17
+ const schema_js_1 = require("./ecmascript/schema.js");
18
+ const transpile_js_1 = require("./ecmascript/transpile.js");
19
+ const parameter_js_1 = require("./ecmascript/parameter.js");
20
+ const plugin_pb_js_1 = require("../google/protobuf/compiler/plugin.pb.js");
21
+ const proto_int64_js_1 = require("../proto-int64.js");
22
+ /**
23
+ * Create a new code generator plugin for ECMAScript.
24
+ * The plugin can generate JavaScript, TypeScript, or TypeScript declaration
25
+ * files.
26
+ */
27
+ function createEcmaScriptPlugin(init) {
28
+ let transpileJs = false;
29
+ let transpileDts = false;
30
+ return {
31
+ name: init.name,
32
+ version: init.version,
33
+ run(req) {
34
+ const parameter = (0, parameter_js_1.parseParameter)(req.parameter, init.parseOption);
35
+ const schema = (0, schema_js_1.createSchema)(req, parameter, init.name, init.version, init.featureSetDefaults);
36
+ const targetTs = schema.targets.includes("ts");
37
+ const targetJs = schema.targets.includes("js");
38
+ const targetDts = schema.targets.includes("dts");
39
+ // Generate TS files under the following conditions:
40
+ // - if they are explicitly specified as a target.
41
+ // - if js is specified as a target but no js generator is provided.
42
+ // - if dts is specified as a target, but no dts generator is provided.
43
+ // In the latter two cases, it is because we need the generated TS files
44
+ // to use for transpiling js and/or dts.
45
+ let tsFiles = [];
46
+ if (targetTs ||
47
+ (targetJs && !init.generateJs) ||
48
+ (targetDts && !init.generateDts)) {
49
+ schema.prepareGenerate("ts");
50
+ init.generateTs(schema, "ts");
51
+ // Save off the generated TypeScript files so that we can pass these
52
+ // to the transpilation process if necessary. We do not want to pass
53
+ // JavaScript files for a few reasons:
54
+ // 1. Our usage of allowJs in the compiler options will cause issues
55
+ // with attempting to transpile .ts and .js files to the same location.
56
+ // 2. There should be no reason to transpile JS because generateTs
57
+ // functions are required, so users would never be able to only specify
58
+ // a generateJs function and expect to transpile declarations.
59
+ // 3. Transpiling is somewhat expensive and situations with an
60
+ // extremely large amount of files could have performance impacts.
61
+ tsFiles = schema.getFileInfo();
62
+ }
63
+ if (targetJs) {
64
+ if (init.generateJs) {
65
+ schema.prepareGenerate("js");
66
+ init.generateJs(schema, "js");
67
+ }
68
+ else {
69
+ transpileJs = true;
70
+ }
71
+ }
72
+ if (targetDts) {
73
+ if (init.generateDts) {
74
+ schema.prepareGenerate("dts");
75
+ init.generateDts(schema, "dts");
76
+ }
77
+ else {
78
+ transpileDts = true;
79
+ }
80
+ }
81
+ // Get generated files. If ts was specified as a target, then we want
82
+ // all generated files. If ts was not specified, we still may have
83
+ // generated TypeScript files to assist in transpilation. If they were
84
+ // generated but not specified in the target out, we shouldn't produce
85
+ // these files in the CodeGeneratorResponse.
86
+ let files = schema.getFileInfo();
87
+ if (!targetTs && tsFiles.length > 0) {
88
+ files = files.filter((file) => !tsFiles.some((tsFile) => tsFile.name === file.name));
89
+ }
90
+ // If either boolean is true, it means it was specified in the target out
91
+ // but no generate function was provided. This also means that we will
92
+ // have generated .ts files above.
93
+ if (transpileJs || transpileDts) {
94
+ const transpileFn = init.transpile ?? transpile_js_1.transpile;
95
+ // Transpile the TypeScript files and add to the master list of files
96
+ const transpiledFiles = transpileFn(tsFiles, transpileJs, transpileDts, parameter.jsImportStyle);
97
+ files.push(...transpiledFiles);
98
+ }
99
+ return toResponse(files, init.supportsEditions ?? false);
100
+ },
101
+ };
102
+ }
103
+ exports.createEcmaScriptPlugin = createEcmaScriptPlugin;
104
+ function toResponse(files, supportsEditions) {
105
+ let supportedFeatures = plugin_pb_js_1.CodeGeneratorResponse_Feature.PROTO3_OPTIONAL;
106
+ if (supportsEditions) {
107
+ supportedFeatures =
108
+ supportedFeatures | plugin_pb_js_1.CodeGeneratorResponse_Feature.SUPPORTS_EDITIONS;
109
+ }
110
+ return plugin_pb_js_1.CodeGeneratorResponse.create({
111
+ supportedFeatures: proto_int64_js_1.protoInt64.parse(supportedFeatures),
112
+ file: files.map((f) => {
113
+ if (f.preamble !== undefined) {
114
+ f.content = f.preamble + "\n" + f.content;
115
+ }
116
+ return f;
117
+ }),
118
+ });
119
+ }
@@ -0,0 +1,2 @@
1
+ import { DescFile } from "../../descriptor-set.js";
2
+ export declare function makeFilePreamble(file: DescFile, pluginName: string, pluginVersion: string, parameter: string, tsNoCheck: boolean): string;
@@ -0,0 +1,74 @@
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.makeFilePreamble = void 0;
17
+ const descriptor_pb_js_1 = require("../../google/protobuf/descriptor.pb.js");
18
+ function makeFilePreamble(file, pluginName, pluginVersion, parameter, tsNoCheck) {
19
+ const builder = [];
20
+ const trimSuffix = (str, suffix) => str.endsWith(suffix) ? str.substring(0, str.length - suffix.length) : str;
21
+ const writeLeadingComments = (comments) => {
22
+ for (let comment of comments.leadingDetached) {
23
+ comment = trimSuffix(comment, "\n");
24
+ for (const line of comment.split("\n")) {
25
+ builder.push(`//${line}\n`);
26
+ }
27
+ builder.push("\n");
28
+ }
29
+ if (comments.leading !== undefined) {
30
+ const comment = trimSuffix(comments.leading, "\n");
31
+ for (const line of comment.split("\n")) {
32
+ builder.push(`//${line}\n`);
33
+ }
34
+ builder.push("\n");
35
+ }
36
+ };
37
+ writeLeadingComments(file.getSyntaxComments());
38
+ builder.push(`// @generated by ${pluginName} ${pluginVersion}`);
39
+ if (parameter !== "") {
40
+ builder.push(` with parameter "${parameter}"`);
41
+ }
42
+ builder.push("\n");
43
+ builder.push(`// @generated from file ${file.name}.proto (`);
44
+ if (file.proto.package !== undefined) {
45
+ builder.push(`package ${file.proto.package}, `);
46
+ }
47
+ switch (file.edition) {
48
+ case descriptor_pb_js_1.Edition.EDITION_PROTO2:
49
+ builder.push(`syntax proto2)\n`);
50
+ break;
51
+ case descriptor_pb_js_1.Edition.EDITION_PROTO3:
52
+ builder.push(`syntax proto3)\n`);
53
+ break;
54
+ default: {
55
+ const editionString = descriptor_pb_js_1.Edition[file.edition];
56
+ if (typeof editionString == "string") {
57
+ const e = editionString.replace("EDITION_", "").toLowerCase();
58
+ builder.push(`edition ${e})\n`);
59
+ }
60
+ else {
61
+ builder.push(`unknown edition\n`);
62
+ }
63
+ break;
64
+ }
65
+ }
66
+ builder.push("/* eslint-disable */\n");
67
+ if (tsNoCheck) {
68
+ builder.push("// @ts-nocheck\n");
69
+ }
70
+ builder.push("\n");
71
+ writeLeadingComments(file.getPackageComments());
72
+ return trimSuffix(builder.join(""), "\n");
73
+ }
74
+ exports.makeFilePreamble = makeFilePreamble;
@@ -0,0 +1,116 @@
1
+ import type { ImportSymbol } from "./import-symbol.js";
2
+ import type { RuntimeImports } from "./runtime-imports.js";
3
+ import type { JSDocBlock } from "./jsdoc.js";
4
+ import type { ExportDeclaration, LiteralProtoInt64, LiteralString, RefDescEnum, RefDescMessage } from "./opaque-printables.js";
5
+ import { AnyDesc, DescEnum, DescExtension, DescFile, DescMessage } from "../../descriptor-set.js";
6
+ /**
7
+ * All types that can be passed to GeneratedFile.print()
8
+ */
9
+ export type Printable = string | number | boolean | bigint | Uint8Array | ImportSymbol | ExportDeclaration | JSDocBlock | LiteralString | LiteralProtoInt64 | RefDescMessage | RefDescEnum | DescMessage | DescEnum | DescExtension | Printable[];
10
+ /**
11
+ * FileInfo represents an intermediate type using for transpiling TypeScript internally.
12
+ */
13
+ export interface FileInfo {
14
+ name: string;
15
+ content: string;
16
+ preamble?: string | undefined;
17
+ }
18
+ /**
19
+ * Represents a JavaScript, TypeScript, or TypeScript declaration file.
20
+ */
21
+ export interface GeneratedFile {
22
+ /**
23
+ * Create a standard preamble the includes comments at the top of the
24
+ * protobuf source file (like a license header), as well as information
25
+ * about the code generator and its version.
26
+ *
27
+ * The preamble is always placed at the very top of the generated file,
28
+ * above import statements.
29
+ *
30
+ * A file with a preamble but no other content is still considered empty,
31
+ * and will not be generated unless the plugin option keep_empty_files=true
32
+ * is set.
33
+ */
34
+ preamble(file: DescFile): void;
35
+ /**
36
+ * Add a line of code to the file.
37
+ *
38
+ * - string: Prints the string verbatim.
39
+ * - number or boolean: Prints a literal.
40
+ * - bigint: Prints an expression using protoInt64.parse().
41
+ * - Uint8Array: Prints an expression that re-created the array.
42
+ * - ImportSymbol: Adds an import statement and prints the name of the symbol.
43
+ * - DescMessage or DescEnum: Imports the type if necessary, and prints the name.
44
+ */
45
+ print(...printables: Printable[]): void;
46
+ /**
47
+ * Add a line of code to the file with tagged template literal and
48
+ * an optional array of Printables.
49
+ * See print(Printable[]) for behavior when printing Printable items.
50
+ */
51
+ print(fragments: TemplateStringsArray, ...printables: Printable[]): void;
52
+ /**
53
+ * @deprecated Please use createImportSymbol() from @bufbuild/protoplugin/ecmascript instead
54
+ */
55
+ export(name: string): ImportSymbol;
56
+ /**
57
+ * Create a string literal.
58
+ */
59
+ string(string: string): Printable;
60
+ /**
61
+ * Create a JSDoc comment block with the given text. Line breaks and white-space
62
+ * stay intact.
63
+ */
64
+ jsDoc(text: string, indentation?: string): JSDocBlock;
65
+ /**
66
+ * Create a JSDoc comment block for the given message, enumeration, or other
67
+ * descriptor. The comment block will contain the original comments from the
68
+ * protobuf source, and annotations such as `@generated from message MyMessage`.
69
+ */
70
+ jsDoc(desc: Exclude<AnyDesc, DescFile>, indentation?: string): JSDocBlock;
71
+ /**
72
+ * Create a printable export statement. For example:
73
+ *
74
+ * ```ts
75
+ * f.print(f.exportDecl("abstract class", "MyClass"), " {}")
76
+ * ```
77
+ *
78
+ * Will generate as:
79
+ * ```ts
80
+ * export abstract class MyClass {}
81
+ * ```
82
+ *
83
+ * Using this method is preferred over a calling print() with a literal export
84
+ * statement. If the plugin option `js_import_style=legacy_commonjs` is set,
85
+ * exports will automatically be generated for CommonJS.
86
+ */
87
+ exportDecl(declaration: string, name: string | DescMessage | DescEnum | DescExtension): Printable;
88
+ /**
89
+ * Import a message or enumeration generated by protoc-gen-es.
90
+ */
91
+ import(type: DescMessage | DescEnum | DescExtension): ImportSymbol;
92
+ /**
93
+ * Import any symbol from a file or package.
94
+ *
95
+ * The import path can point to a package, for example `@foo/bar/baz.js`, or
96
+ * to a file, for example `./bar/baz.js`.
97
+ *
98
+ * Note that while paths to a file begin with a `./`, they must be
99
+ * relative to the project root. The import path is automatically made
100
+ * relative to the current file.
101
+ */
102
+ import(name: string, from: string): ImportSymbol;
103
+ /**
104
+ * In case you need full control over exports and imports, use print() and
105
+ * formulate your own imports and exports based on this property.
106
+ */
107
+ readonly jsImportStyle: "module" | "legacy_commonjs";
108
+ }
109
+ export interface GeneratedFileController extends GeneratedFile {
110
+ getFileInfo(): FileInfo;
111
+ }
112
+ type CreateTypeImportFn = (desc: DescMessage | DescEnum | DescExtension) => ImportSymbol;
113
+ type RewriteImportPathFn = (path: string) => string;
114
+ type CreatePreambleFn = (descFile: DescFile) => string;
115
+ export declare function createGeneratedFile(name: string, importPath: string, jsImportStyle: "module" | "legacy_commonjs", rewriteImportPath: RewriteImportPathFn, createTypeImport: CreateTypeImportFn, runtimeImports: RuntimeImports, createPreamble: CreatePreambleFn): GeneratedFileController;
116
+ export {};