ts-proto 1.122.0 → 1.124.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.
- package/CHANGELOG.md +21 -0
- package/README.markdown +4 -1
- package/build/case.js +4 -4
- package/build/encode.js +21 -21
- package/build/enums.js +11 -11
- package/build/generate-generic-service-definition.js +2 -2
- package/build/generate-grpc-js.js +20 -20
- package/build/generate-grpc-web.js +11 -11
- package/build/generate-nestjs.js +15 -15
- package/build/generate-nice-grpc.js +5 -5
- package/build/generate-services.js +27 -27
- package/build/generate-type-registry.js +4 -4
- package/build/main.js +103 -103
- package/build/options.js +16 -12
- package/build/plugin.js +6 -6
- package/build/schema.js +37 -37
- package/build/sourceInfo.js +3 -3
- package/build/types.js +71 -71
- package/build/utils.js +14 -14
- package/build/visit.js +5 -5
- package/package.json +6 -6
package/build/options.js
CHANGED
|
@@ -35,15 +35,15 @@ var ServiceOption;
|
|
|
35
35
|
function defaultOptions() {
|
|
36
36
|
return {
|
|
37
37
|
context: false,
|
|
38
|
-
snakeToCamel: [
|
|
38
|
+
snakeToCamel: ["json", "keys"],
|
|
39
39
|
forceLong: LongOption.NUMBER,
|
|
40
|
-
useOptionals:
|
|
40
|
+
useOptionals: "none",
|
|
41
41
|
useDate: DateOption.DATE,
|
|
42
42
|
useMongoObjectId: false,
|
|
43
43
|
oneof: OneofOption.PROPERTIES,
|
|
44
44
|
esModuleInterop: false,
|
|
45
|
-
fileSuffix:
|
|
46
|
-
importSuffix:
|
|
45
|
+
fileSuffix: "",
|
|
46
|
+
importSuffix: "",
|
|
47
47
|
lowerCaseServiceMethods: false,
|
|
48
48
|
outputEncodeMethods: true,
|
|
49
49
|
outputJsonMethods: true,
|
|
@@ -113,7 +113,7 @@ function optionsFromParameter(parameter) {
|
|
|
113
113
|
options.outputServices = [ServiceOption.NONE];
|
|
114
114
|
}
|
|
115
115
|
// Existing type-coercion inside parseParameter leaves a little to be desired.
|
|
116
|
-
if (typeof options.outputServices ==
|
|
116
|
+
if (typeof options.outputServices == "string") {
|
|
117
117
|
options.outputServices = [options.outputServices];
|
|
118
118
|
}
|
|
119
119
|
if (options.outputServices.length == 0) {
|
|
@@ -131,10 +131,10 @@ function optionsFromParameter(parameter) {
|
|
|
131
131
|
options.snakeToCamel = [];
|
|
132
132
|
}
|
|
133
133
|
else if (options.snakeToCamel === true) {
|
|
134
|
-
options.snakeToCamel = [
|
|
134
|
+
options.snakeToCamel = ["keys", "json"];
|
|
135
135
|
}
|
|
136
|
-
else if (typeof options.snakeToCamel ===
|
|
137
|
-
options.snakeToCamel = options.snakeToCamel.split(
|
|
136
|
+
else if (typeof options.snakeToCamel === "string") {
|
|
137
|
+
options.snakeToCamel = options.snakeToCamel.split("_");
|
|
138
138
|
}
|
|
139
139
|
if (options.useJsonWireFormat) {
|
|
140
140
|
if (!options.onlyTypes) {
|
|
@@ -153,9 +153,9 @@ exports.optionsFromParameter = optionsFromParameter;
|
|
|
153
153
|
// A very naive parse function, eventually could/should use iots/runtypes
|
|
154
154
|
function parseParameter(parameter) {
|
|
155
155
|
const options = {};
|
|
156
|
-
const pairs = parameter.split(
|
|
156
|
+
const pairs = parameter.split(",").map((s) => s.split("="));
|
|
157
157
|
pairs.forEach(([key, _value]) => {
|
|
158
|
-
const value = _value ===
|
|
158
|
+
const value = _value === "true" ? true : _value === "false" ? false : _value;
|
|
159
159
|
if (options[key]) {
|
|
160
160
|
options[key] = [options[key], value];
|
|
161
161
|
}
|
|
@@ -166,7 +166,11 @@ function parseParameter(parameter) {
|
|
|
166
166
|
return options;
|
|
167
167
|
}
|
|
168
168
|
function getTsPoetOpts(_options) {
|
|
169
|
-
const imports = [
|
|
170
|
-
return
|
|
169
|
+
const imports = ["protobufjs/minimal" + _options.importSuffix];
|
|
170
|
+
return {
|
|
171
|
+
prefix: `/* eslint-disable */`,
|
|
172
|
+
dprintOptions: { preferSingleLine: true, lineWidth: 120 },
|
|
173
|
+
...(_options.esModuleInterop ? { forceDefaultImport: imports } : { forceModuleImport: imports }),
|
|
174
|
+
};
|
|
171
175
|
}
|
|
172
176
|
exports.getTsPoetOpts = getTsPoetOpts;
|
package/build/plugin.js
CHANGED
|
@@ -20,16 +20,16 @@ async function main() {
|
|
|
20
20
|
const filesToGenerate = options.emitImportedFiles ? request.protoFile : (0, utils_1.protoFilesToGenerate)(request);
|
|
21
21
|
const files = await Promise.all(filesToGenerate.map(async (file) => {
|
|
22
22
|
const [path, code] = (0, main_1.generateFile)(ctx, file);
|
|
23
|
-
const
|
|
24
|
-
return { name: path, content
|
|
23
|
+
const content = code.toString({ ...(0, options_1.getTsPoetOpts)(options), path });
|
|
24
|
+
return { name: path, content };
|
|
25
25
|
}));
|
|
26
26
|
if (options.outputTypeRegistry) {
|
|
27
27
|
const utils = (0, main_1.makeUtils)(options);
|
|
28
28
|
const ctx = { options, typeMap, utils };
|
|
29
|
-
const path =
|
|
29
|
+
const path = "typeRegistry.ts";
|
|
30
30
|
const code = (0, generate_type_registry_1.generateTypeRegistry)(ctx);
|
|
31
|
-
const
|
|
32
|
-
files.push({ name: path, content
|
|
31
|
+
const content = code.toString({ ...(0, options_1.getTsPoetOpts)(options), path });
|
|
32
|
+
files.push({ name: path, content });
|
|
33
33
|
}
|
|
34
34
|
const response = ts_proto_descriptors_1.CodeGeneratorResponse.fromPartial({
|
|
35
35
|
file: files,
|
|
@@ -44,7 +44,7 @@ main()
|
|
|
44
44
|
process.exit(0);
|
|
45
45
|
})
|
|
46
46
|
.catch((e) => {
|
|
47
|
-
process.stderr.write(
|
|
47
|
+
process.stderr.write("FAILED!");
|
|
48
48
|
process.stderr.write(e.message);
|
|
49
49
|
process.stderr.write(e.stack);
|
|
50
50
|
process.exit(1);
|
package/build/schema.js
CHANGED
|
@@ -7,7 +7,7 @@ const visit_1 = require("./visit");
|
|
|
7
7
|
const utils_1 = require("./utils");
|
|
8
8
|
const types_1 = require("./types");
|
|
9
9
|
const minimal_1 = require("protobufjs/minimal");
|
|
10
|
-
const fileDescriptorProto = (0, ts_poet_1.imp)(
|
|
10
|
+
const fileDescriptorProto = (0, ts_poet_1.imp)("FileDescriptorProto@ts-proto-descriptors");
|
|
11
11
|
const extensionCache = {};
|
|
12
12
|
function generateSchema(ctx, fileDesc, sourceInfo) {
|
|
13
13
|
var _a;
|
|
@@ -53,7 +53,7 @@ function generateSchema(ctx, fileDesc, sourceInfo) {
|
|
|
53
53
|
`);
|
|
54
54
|
const references = [];
|
|
55
55
|
function addReference(localName, symbol) {
|
|
56
|
-
references.push((0, ts_poet_1.code) `'.${(0, utils_1.maybePrefixPackage)(fileDesc, localName.replace(/_/g,
|
|
56
|
+
references.push((0, ts_poet_1.code) `'.${(0, utils_1.maybePrefixPackage)(fileDesc, localName.replace(/_/g, "."))}': ${symbol}`);
|
|
57
57
|
}
|
|
58
58
|
(0, visit_1.visit)(fileDesc, sourceInfo, (fullName) => {
|
|
59
59
|
if (options.outputEncodeMethods) {
|
|
@@ -68,18 +68,18 @@ function generateSchema(ctx, fileDesc, sourceInfo) {
|
|
|
68
68
|
}
|
|
69
69
|
});
|
|
70
70
|
const dependencies = fileDesc.dependency.map((dep) => {
|
|
71
|
-
return (0, ts_poet_1.code) `${(0, utils_1.impFile)(options, `protoMetadata@./${dep.replace(
|
|
71
|
+
return (0, ts_poet_1.code) `${(0, utils_1.impFile)(options, `protoMetadata@./${dep.replace(".proto", "")}`)}`;
|
|
72
72
|
});
|
|
73
73
|
// Use toObject so that we get enums as numbers (instead of the default toJSON behavior)
|
|
74
74
|
const descriptor = ts_proto_descriptors_1.FileDescriptorProto.fromPartial(fileDesc);
|
|
75
75
|
// Only keep locations that include comments
|
|
76
76
|
descriptor.sourceCodeInfo = {
|
|
77
|
-
location: ((_a = descriptor.sourceCodeInfo) === null || _a === void 0 ? void 0 : _a.location.filter((loc) => loc[
|
|
77
|
+
location: ((_a = descriptor.sourceCodeInfo) === null || _a === void 0 ? void 0 : _a.location.filter((loc) => loc["leadingComments"] || loc["trailingComments"])) || [],
|
|
78
78
|
};
|
|
79
79
|
let fileOptions;
|
|
80
80
|
if (fileDesc.options) {
|
|
81
|
-
fileOptions = encodedOptionsToOptions(ctx,
|
|
82
|
-
delete fileDesc.options[
|
|
81
|
+
fileOptions = encodedOptionsToOptions(ctx, ".google.protobuf.FileOptions", fileDesc.options["_unknownFields"]);
|
|
82
|
+
delete fileDesc.options["_unknownFields"];
|
|
83
83
|
}
|
|
84
84
|
const messagesOptions = [];
|
|
85
85
|
(fileDesc.messageType || []).forEach((message) => {
|
|
@@ -93,8 +93,8 @@ function generateSchema(ctx, fileDesc, sourceInfo) {
|
|
|
93
93
|
const methodsOptions = [];
|
|
94
94
|
service.method.forEach((method) => {
|
|
95
95
|
if (method.options) {
|
|
96
|
-
const methodOptions = encodedOptionsToOptions(ctx,
|
|
97
|
-
delete method.options[
|
|
96
|
+
const methodOptions = encodedOptionsToOptions(ctx, ".google.protobuf.MethodOptions", method.options["_unknownFields"]);
|
|
97
|
+
delete method.options["_unknownFields"];
|
|
98
98
|
if (methodOptions) {
|
|
99
99
|
methodsOptions.push((0, ts_poet_1.code) `'${method.name}': ${methodOptions}`);
|
|
100
100
|
}
|
|
@@ -102,14 +102,14 @@ function generateSchema(ctx, fileDesc, sourceInfo) {
|
|
|
102
102
|
});
|
|
103
103
|
let serviceOptions;
|
|
104
104
|
if (service.options) {
|
|
105
|
-
serviceOptions = encodedOptionsToOptions(ctx,
|
|
106
|
-
delete service.options[
|
|
105
|
+
serviceOptions = encodedOptionsToOptions(ctx, ".google.protobuf.ServiceOptions", service.options["_unknownFields"]);
|
|
106
|
+
delete service.options["_unknownFields"];
|
|
107
107
|
}
|
|
108
108
|
if (methodsOptions.length > 0 || serviceOptions) {
|
|
109
109
|
servicesOptions.push((0, ts_poet_1.code) `
|
|
110
110
|
'${service.name}': {
|
|
111
111
|
options: ${serviceOptions},
|
|
112
|
-
methods: {${(0, ts_poet_1.joinCode)(methodsOptions, { on:
|
|
112
|
+
methods: {${(0, ts_poet_1.joinCode)(methodsOptions, { on: "," })}}
|
|
113
113
|
}
|
|
114
114
|
`);
|
|
115
115
|
}
|
|
@@ -119,8 +119,8 @@ function generateSchema(ctx, fileDesc, sourceInfo) {
|
|
|
119
119
|
const valuesOptions = [];
|
|
120
120
|
Enum.value.forEach((value) => {
|
|
121
121
|
if (value.options) {
|
|
122
|
-
const valueOptions = encodedOptionsToOptions(ctx,
|
|
123
|
-
delete value.options[
|
|
122
|
+
const valueOptions = encodedOptionsToOptions(ctx, ".google.protobuf.EnumValueOptions", value.options["_unknownFields"]);
|
|
123
|
+
delete value.options["_unknownFields"];
|
|
124
124
|
if (valueOptions) {
|
|
125
125
|
valuesOptions.push((0, ts_poet_1.code) `'${value.name}': ${valueOptions}`);
|
|
126
126
|
}
|
|
@@ -128,31 +128,31 @@ function generateSchema(ctx, fileDesc, sourceInfo) {
|
|
|
128
128
|
});
|
|
129
129
|
let enumOptions;
|
|
130
130
|
if (Enum.options) {
|
|
131
|
-
enumOptions = encodedOptionsToOptions(ctx,
|
|
132
|
-
delete Enum.options[
|
|
131
|
+
enumOptions = encodedOptionsToOptions(ctx, ".google.protobuf.EnumOptions", Enum.options["_unknownFields"]);
|
|
132
|
+
delete Enum.options["_unknownFields"];
|
|
133
133
|
}
|
|
134
134
|
if (valuesOptions.length > 0 || enumOptions) {
|
|
135
135
|
enumsOptions.push((0, ts_poet_1.code) `
|
|
136
136
|
'${Enum.name}': {
|
|
137
137
|
options: ${enumOptions},
|
|
138
|
-
values: {${(0, ts_poet_1.joinCode)(valuesOptions, { on:
|
|
138
|
+
values: {${(0, ts_poet_1.joinCode)(valuesOptions, { on: "," })}}
|
|
139
139
|
}
|
|
140
140
|
`);
|
|
141
141
|
}
|
|
142
142
|
});
|
|
143
143
|
chunks.push((0, ts_poet_1.code) `
|
|
144
|
-
export const ${(0, ts_poet_1.def)(
|
|
144
|
+
export const ${(0, ts_poet_1.def)("protoMetadata")}: ProtoMetadata = {
|
|
145
145
|
fileDescriptor: ${fileDescriptorProto}.fromPartial(${descriptor}),
|
|
146
|
-
references: { ${(0, ts_poet_1.joinCode)(references, { on:
|
|
147
|
-
dependencies: [${(0, ts_poet_1.joinCode)(dependencies, { on:
|
|
146
|
+
references: { ${(0, ts_poet_1.joinCode)(references, { on: "," })} },
|
|
147
|
+
dependencies: [${(0, ts_poet_1.joinCode)(dependencies, { on: "," })}],
|
|
148
148
|
${fileOptions || messagesOptions.length > 0 || servicesOptions.length > 0 || enumsOptions.length > 0
|
|
149
149
|
? (0, ts_poet_1.code) `options: {
|
|
150
|
-
${fileOptions ? (0, ts_poet_1.code) `options: ${fileOptions},` :
|
|
151
|
-
${messagesOptions.length > 0 ? (0, ts_poet_1.code) `messages: {${(0, ts_poet_1.joinCode)(messagesOptions, { on:
|
|
152
|
-
${servicesOptions.length > 0 ? (0, ts_poet_1.code) `services: {${(0, ts_poet_1.joinCode)(servicesOptions, { on:
|
|
153
|
-
${enumsOptions.length > 0 ? (0, ts_poet_1.code) `enums: {${(0, ts_poet_1.joinCode)(enumsOptions, { on:
|
|
150
|
+
${fileOptions ? (0, ts_poet_1.code) `options: ${fileOptions},` : ""}
|
|
151
|
+
${messagesOptions.length > 0 ? (0, ts_poet_1.code) `messages: {${(0, ts_poet_1.joinCode)(messagesOptions, { on: "," })}},` : ""}
|
|
152
|
+
${servicesOptions.length > 0 ? (0, ts_poet_1.code) `services: {${(0, ts_poet_1.joinCode)(servicesOptions, { on: "," })}},` : ""}
|
|
153
|
+
${enumsOptions.length > 0 ? (0, ts_poet_1.code) `enums: {${(0, ts_poet_1.joinCode)(enumsOptions, { on: "," })}}` : ""}
|
|
154
154
|
}`
|
|
155
|
-
:
|
|
155
|
+
: ""}
|
|
156
156
|
}
|
|
157
157
|
`);
|
|
158
158
|
return chunks;
|
|
@@ -167,13 +167,13 @@ function getExtensionValue(ctx, extension, data) {
|
|
|
167
167
|
reader.uint32();
|
|
168
168
|
return reader.buf.slice(reader.pos);
|
|
169
169
|
}));
|
|
170
|
-
const result = resultBuffer.toString(
|
|
170
|
+
const result = resultBuffer.toString("base64");
|
|
171
171
|
return (0, ts_poet_1.code) `'${extension.name}': ${typeName}.decode(Buffer.from('${result}', 'base64'))`;
|
|
172
172
|
}
|
|
173
173
|
else {
|
|
174
174
|
const reader = new minimal_1.Reader(data[0]);
|
|
175
175
|
let value = reader[(0, types_1.toReaderCall)(extension)]();
|
|
176
|
-
if (typeof value ===
|
|
176
|
+
if (typeof value === "string") {
|
|
177
177
|
value = (0, ts_poet_1.code) `"${value}"`;
|
|
178
178
|
}
|
|
179
179
|
return (0, ts_poet_1.code) `'${extension.name}': ${value}`;
|
|
@@ -192,14 +192,14 @@ function encodedOptionsToOptions(ctx, extendee, encodedOptions) {
|
|
|
192
192
|
if (resultOptions.length == 0) {
|
|
193
193
|
return undefined;
|
|
194
194
|
}
|
|
195
|
-
return (0, ts_poet_1.code) `{${(0, ts_poet_1.joinCode)(resultOptions, { on:
|
|
195
|
+
return (0, ts_poet_1.code) `{${(0, ts_poet_1.joinCode)(resultOptions, { on: "," })}}`;
|
|
196
196
|
}
|
|
197
197
|
function resolveMessageOptions(ctx, message) {
|
|
198
198
|
const fieldsOptions = [];
|
|
199
199
|
message.field.forEach((field) => {
|
|
200
200
|
if (field.options) {
|
|
201
|
-
const fieldOptions = encodedOptionsToOptions(ctx,
|
|
202
|
-
delete field.options[
|
|
201
|
+
const fieldOptions = encodedOptionsToOptions(ctx, ".google.protobuf.FieldOptions", field.options["_unknownFields"]);
|
|
202
|
+
delete field.options["_unknownFields"];
|
|
203
203
|
if (fieldOptions) {
|
|
204
204
|
fieldsOptions.push((0, ts_poet_1.code) `'${field.name}': ${fieldOptions}`);
|
|
205
205
|
}
|
|
@@ -208,8 +208,8 @@ function resolveMessageOptions(ctx, message) {
|
|
|
208
208
|
const oneOfsOptions = [];
|
|
209
209
|
message.oneofDecl.forEach((oneOf) => {
|
|
210
210
|
if (oneOf.options) {
|
|
211
|
-
const oneOfOptions = encodedOptionsToOptions(ctx,
|
|
212
|
-
delete oneOf.options[
|
|
211
|
+
const oneOfOptions = encodedOptionsToOptions(ctx, ".google.protobuf.OneofOptions", oneOf.options["_unknownFields"]);
|
|
212
|
+
delete oneOf.options["_unknownFields"];
|
|
213
213
|
if (oneOfOptions) {
|
|
214
214
|
oneOfsOptions.push((0, ts_poet_1.code) `'${oneOf.name}': ${oneOfOptions}`);
|
|
215
215
|
}
|
|
@@ -226,16 +226,16 @@ function resolveMessageOptions(ctx, message) {
|
|
|
226
226
|
}
|
|
227
227
|
let messageOptions;
|
|
228
228
|
if (message.options) {
|
|
229
|
-
messageOptions = encodedOptionsToOptions(ctx,
|
|
230
|
-
delete message.options[
|
|
229
|
+
messageOptions = encodedOptionsToOptions(ctx, ".google.protobuf.MessageOptions", message.options["_unknownFields"]);
|
|
230
|
+
delete message.options["_unknownFields"];
|
|
231
231
|
}
|
|
232
232
|
if (fieldsOptions.length > 0 || oneOfsOptions.length > 0 || nestedOptions.length > 0 || messageOptions) {
|
|
233
233
|
return (0, ts_poet_1.code) `
|
|
234
234
|
'${message.name}': {
|
|
235
|
-
${messageOptions ? (0, ts_poet_1.code) `options: ${messageOptions},` :
|
|
236
|
-
${fieldsOptions.length > 0 ? (0, ts_poet_1.code) `fields: {${(0, ts_poet_1.joinCode)(fieldsOptions, { on:
|
|
237
|
-
${oneOfsOptions.length > 0 ? (0, ts_poet_1.code) `oneof: {${(0, ts_poet_1.joinCode)(oneOfsOptions, { on:
|
|
238
|
-
${nestedOptions.length > 0 ? (0, ts_poet_1.code) `nested: {${(0, ts_poet_1.joinCode)(nestedOptions, { on:
|
|
235
|
+
${messageOptions ? (0, ts_poet_1.code) `options: ${messageOptions},` : ""}
|
|
236
|
+
${fieldsOptions.length > 0 ? (0, ts_poet_1.code) `fields: {${(0, ts_poet_1.joinCode)(fieldsOptions, { on: "," })}},` : ""}
|
|
237
|
+
${oneOfsOptions.length > 0 ? (0, ts_poet_1.code) `oneof: {${(0, ts_poet_1.joinCode)(oneOfsOptions, { on: "," })}},` : ""}
|
|
238
|
+
${nestedOptions.length > 0 ? (0, ts_poet_1.code) `nested: {${(0, ts_poet_1.joinCode)(nestedOptions, { on: "," })}},` : ""}
|
|
239
239
|
}
|
|
240
240
|
`;
|
|
241
241
|
}
|
package/build/sourceInfo.js
CHANGED
|
@@ -34,8 +34,8 @@ exports.Fields = {
|
|
|
34
34
|
class EmptyDescription {
|
|
35
35
|
constructor() {
|
|
36
36
|
this.span = [];
|
|
37
|
-
this.leadingComments =
|
|
38
|
-
this.trailingComments =
|
|
37
|
+
this.leadingComments = "";
|
|
38
|
+
this.trailingComments = "";
|
|
39
39
|
this.leadingDetachedComments = [];
|
|
40
40
|
}
|
|
41
41
|
}
|
|
@@ -63,7 +63,7 @@ class SourceInfo {
|
|
|
63
63
|
let map = {};
|
|
64
64
|
if (file.sourceCodeInfo && file.sourceCodeInfo.location) {
|
|
65
65
|
file.sourceCodeInfo.location.forEach((loc) => {
|
|
66
|
-
map[loc.path.join(
|
|
66
|
+
map[loc.path.join(".")] = loc;
|
|
67
67
|
});
|
|
68
68
|
}
|
|
69
69
|
return new SourceInfo(map, new EmptyDescription());
|