@aptre/protobuf-es-lite 0.2.7 → 0.2.9
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.
- package/.ignore +1 -0
- package/README.md +23 -24
- package/bin/protoc-gen-es-lite +2 -2
- package/dist/binary-encoding.d.ts +420 -0
- package/dist/binary-encoding.js +437 -0
- package/dist/binary.d.ts +35 -2
- package/dist/binary.js +64 -65
- package/dist/codegen-info.d.ts +43 -0
- package/dist/codegen-info.js +72 -0
- package/dist/create-descriptor-set.d.ts +36 -0
- package/dist/create-descriptor-set.js +936 -0
- package/dist/descriptor-set.d.ts +672 -0
- package/dist/{editions.js → descriptor-set.js} +0 -11
- package/dist/enum.d.ts +35 -1
- package/dist/feature-set.d.ts +19 -0
- package/dist/feature-set.js +103 -0
- package/dist/field-wrapper.d.ts +7 -3
- package/dist/field-wrapper.js +30 -1
- package/dist/field.d.ts +8 -19
- package/dist/field.js +15 -117
- package/dist/google/index.d.ts +9 -0
- package/dist/google/index.js +41 -0
- package/dist/google/protobuf/any.pb.d.ts +139 -0
- package/dist/google/protobuf/any.pb.js +42 -0
- package/dist/google/protobuf/api.pb.d.ts +252 -0
- package/dist/google/protobuf/api.pb.js +70 -0
- package/dist/google/protobuf/compiler/plugin.pb.d.ts +240 -0
- package/dist/google/protobuf/compiler/plugin.pb.js +203 -0
- package/dist/google/protobuf/descriptor.pb.d.ts +2132 -0
- package/dist/google/protobuf/descriptor.pb.js +1080 -0
- package/dist/google/protobuf/duration.pb.d.ts +92 -0
- package/dist/google/protobuf/duration.pb.js +42 -0
- package/dist/google/protobuf/empty.pb.d.ts +18 -0
- package/dist/google/protobuf/empty.pb.js +39 -0
- package/dist/google/protobuf/source_context.pb.d.ts +21 -0
- package/dist/google/protobuf/source_context.pb.js +41 -0
- package/dist/google/protobuf/struct.pb.d.ts +121 -0
- package/dist/google/protobuf/struct.pb.js +83 -0
- package/dist/google/protobuf/timestamp.pb.d.ts +121 -0
- package/dist/google/protobuf/timestamp.pb.js +42 -0
- package/dist/google/protobuf/type.pb.d.ts +464 -0
- package/dist/google/protobuf/type.pb.js +338 -0
- package/dist/google/protobuf/wrappers.pb.d.ts +183 -0
- package/dist/google/protobuf/wrappers.pb.js +107 -0
- package/dist/index.d.ts +9 -3
- package/dist/index.js +41 -8
- package/dist/is-message.d.ts +10 -2
- package/dist/is-message.js +74 -3
- package/dist/json.d.ts +68 -1
- package/dist/json.js +57 -81
- package/dist/message.d.ts +30 -61
- package/dist/message.js +61 -20
- package/dist/names.d.ts +43 -0
- package/dist/names.js +276 -0
- package/dist/partial.js +14 -15
- package/dist/proto-base64.d.ts +18 -0
- package/dist/proto-base64.js +128 -0
- package/dist/proto-double.d.ts +5 -0
- package/dist/proto-double.js +29 -0
- package/dist/proto-int64.d.ts +95 -0
- package/dist/proto-int64.js +116 -0
- package/dist/protoc-gen-es-lite/protoc-gen-es-lite-plugin.d.ts +1 -0
- package/dist/{protoc-gen-es-lite-plugin.js → protoc-gen-es-lite/protoc-gen-es-lite-plugin.js} +2 -2
- package/dist/{typescript.d.ts → protoc-gen-es-lite/typescript.d.ts} +3 -2
- package/dist/{typescript.js → protoc-gen-es-lite/typescript.js} +46 -34
- package/dist/protoplugin/create-es-plugin.d.ts +83 -0
- package/dist/protoplugin/create-es-plugin.js +119 -0
- package/dist/protoplugin/ecmascript/file-preamble.d.ts +2 -0
- package/dist/protoplugin/ecmascript/file-preamble.js +74 -0
- package/dist/protoplugin/ecmascript/generated-file.d.ts +116 -0
- package/dist/protoplugin/ecmascript/generated-file.js +407 -0
- package/dist/protoplugin/ecmascript/import-path.d.ts +65 -0
- package/dist/protoplugin/ecmascript/import-path.js +156 -0
- package/dist/protoplugin/ecmascript/import-symbol.d.ts +39 -0
- package/dist/protoplugin/ecmascript/import-symbol.js +37 -0
- package/dist/protoplugin/ecmascript/index.d.ts +7 -0
- package/dist/protoplugin/ecmascript/index.js +20 -0
- package/dist/protoplugin/ecmascript/jsdoc.d.ts +14 -0
- package/dist/protoplugin/ecmascript/jsdoc.js +93 -0
- package/dist/protoplugin/ecmascript/opaque-printables.d.ts +27 -0
- package/dist/protoplugin/ecmascript/opaque-printables.js +15 -0
- package/dist/protoplugin/ecmascript/parameter.d.ts +13 -0
- package/dist/protoplugin/ecmascript/parameter.js +161 -0
- package/dist/protoplugin/ecmascript/reify-wkt.d.ts +100 -0
- package/dist/protoplugin/ecmascript/reify-wkt.js +170 -0
- package/dist/protoplugin/ecmascript/runtime-imports.d.ts +6 -0
- package/dist/protoplugin/ecmascript/runtime-imports.js +28 -0
- package/dist/protoplugin/ecmascript/schema.d.ts +43 -0
- package/dist/protoplugin/ecmascript/schema.js +73 -0
- package/dist/protoplugin/ecmascript/target.d.ts +4 -0
- package/dist/protoplugin/ecmascript/target.js +15 -0
- package/dist/protoplugin/ecmascript/transpile.d.ts +2 -0
- package/dist/protoplugin/ecmascript/transpile.js +149 -0
- package/dist/protoplugin/error.d.ts +6 -0
- package/dist/protoplugin/error.js +43 -0
- package/dist/protoplugin/index.d.ts +8 -0
- package/dist/protoplugin/index.js +20 -0
- package/dist/protoplugin/plugin.d.ts +18 -0
- package/dist/protoplugin/plugin.js +15 -0
- package/dist/protoplugin/run-node.d.ts +12 -0
- package/dist/protoplugin/run-node.js +83 -0
- package/dist/scalar.d.ts +53 -1
- package/dist/scalar.js +87 -22
- package/dist/service-type.d.ts +95 -0
- package/dist/service-type.js +53 -0
- package/dist/text-format.d.ts +4 -0
- package/dist/text-format.js +189 -0
- package/dist/type-registry.d.ts +43 -0
- package/dist/type-registry.js +15 -0
- package/dist/unknown.d.ts +1 -1
- package/dist/util.d.ts +2 -2
- package/dist/util.js +46 -44
- package/dist/varint.d.ts +70 -0
- package/dist/varint.js +324 -0
- package/example/example.pb.ts +87 -0
- package/example/example.proto +1 -0
- package/example/tsconfig.json +8 -2
- package/package.json +19 -12
- package/tsconfig.json +4 -4
- package/dist/editions.d.ts +0 -6
- 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
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
const
|
|
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 + "
|
|
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,
|
|
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
|
|
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
|
-
(
|
|
116
|
+
checkSupportedSyntax(message.file);
|
|
116
117
|
f.print(f.jsDoc(message));
|
|
117
|
-
|
|
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), ": ",
|
|
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,
|
|
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,
|
|
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 + "
|
|
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,
|
|
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,
|
|
194
|
+
return (0, index_js_1.createImportSymbol)(name, from);
|
|
183
195
|
};
|
|
184
196
|
exports.createTypeImport = createTypeImport;
|
|
185
197
|
function getFieldInfoLiteral(field) {
|
|
@@ -193,16 +205,16 @@ function getFieldInfoLiteral(field) {
|
|
|
193
205
|
}
|
|
194
206
|
switch (field.fieldKind) {
|
|
195
207
|
case "scalar":
|
|
196
|
-
e.push(`kind: "scalar", T: `, field.scalar, ` /* ScalarType.`,
|
|
197
|
-
if (field.longType !=
|
|
198
|
-
e.push(`L: `, field.longType, ` /* 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.`,
|
|
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.`,
|
|
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
220
|
e.push(`V: {kind: "message", T: () => `, field.mapValue.message, `}, `);
|
|
@@ -214,7 +226,7 @@ function getFieldInfoLiteral(field) {
|
|
|
214
226
|
break;
|
|
215
227
|
case "message":
|
|
216
228
|
e.push(`kind: "message", T: () => `, field.message, `, `);
|
|
217
|
-
if (field.proto.type ===
|
|
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 ===
|
|
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,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 {};
|