ts-proto 1.178.0 → 1.179.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/README.markdown +3 -1
- package/build/enums.js +2 -4
- package/build/generate-generic-service-definition.js +3 -4
- package/build/generate-grpc-js.js +4 -7
- package/build/generate-nestjs.js +3 -6
- package/build/generate-nice-grpc.js +2 -4
- package/build/generate-services.js +2 -4
- package/build/main.js +11 -18
- package/build/options.d.ts +1 -0
- package/build/options.js +1 -0
- package/build/schema.js +1 -2
- package/build/types.js +12 -19
- package/build/utils.js +1 -2
- package/package.json +1 -1
package/README.markdown
CHANGED
|
@@ -551,6 +551,8 @@ Generated code will be placed in the Gradle build directory.
|
|
|
551
551
|
|
|
552
552
|
- With `--ts_proto_opt=comments=false`, comments won't be copied from the proto files to the generated code.
|
|
553
553
|
|
|
554
|
+
- With `--ts_proto_opt=bigIntLiteral=false`, the generated code will use `BigInt("0")` instead of `0n` for BigInt literals. BigInt literals aren't supported by TypeScript when the "target" compiler option set to something older than "ES2020".
|
|
555
|
+
|
|
554
556
|
- With `--ts_proto_opt=useNullAsOptional=true`, `undefined` values will be converted to `null`, and if you use `optional` label in your `.proto` file, the field will have `undefined` type as well. for example:
|
|
555
557
|
|
|
556
558
|
```protobuf
|
|
@@ -603,7 +605,7 @@ export interface User {
|
|
|
603
605
|
}
|
|
604
606
|
```
|
|
605
607
|
|
|
606
|
-
- With `--ts_proto_opt=noDefaultsForOptionals=true`, `undefined` primitive values will not be defaulted as per the protobuf spec. Additionally unlike the standard behavior, when a field is set to it's standard default value, it *will* be encoded allowing it to be sent over the wire and distinguished from undefined values. For example if a message does not set a boolean value, ordinarily this would be defaulted to `false` which is different to it being undefined.
|
|
608
|
+
- With `--ts_proto_opt=noDefaultsForOptionals=true`, `undefined` primitive values will not be defaulted as per the protobuf spec. Additionally unlike the standard behavior, when a field is set to it's standard default value, it *will* be encoded allowing it to be sent over the wire and distinguished from undefined values. For example if a message does not set a boolean value, ordinarily this would be defaulted to `false` which is different to it being undefined.
|
|
607
609
|
|
|
608
610
|
This option allows the library to act in a compatible way with the [Wire implementation](https://square.github.io/wire/) maintained and used by Square/Block. Note: this option should only be used in combination with other client/server code generated using Wire or ts-proto with this option enabled.
|
|
609
611
|
|
package/build/enums.js
CHANGED
|
@@ -7,11 +7,10 @@ const case_1 = require("./case");
|
|
|
7
7
|
const sourceInfo_1 = require("./sourceInfo");
|
|
8
8
|
// Output the `enum { Foo, A = 0, B = 1 }`
|
|
9
9
|
function generateEnum(ctx, fullName, enumDesc, sourceInfo) {
|
|
10
|
-
var _a;
|
|
11
10
|
const { options } = ctx;
|
|
12
11
|
const chunks = [];
|
|
13
12
|
let unrecognizedEnum = { present: false };
|
|
14
|
-
(0, utils_1.maybeAddComment)(options, sourceInfo, chunks,
|
|
13
|
+
(0, utils_1.maybeAddComment)(options, sourceInfo, chunks, enumDesc.options?.deprecated);
|
|
15
14
|
if (options.enumsAsLiterals) {
|
|
16
15
|
chunks.push((0, ts_poet_1.code) `export const ${(0, ts_poet_1.def)(fullName)} = {`);
|
|
17
16
|
}
|
|
@@ -20,14 +19,13 @@ function generateEnum(ctx, fullName, enumDesc, sourceInfo) {
|
|
|
20
19
|
}
|
|
21
20
|
const delimiter = options.enumsAsLiterals ? ":" : "=";
|
|
22
21
|
enumDesc.value.forEach((valueDesc, index) => {
|
|
23
|
-
var _a;
|
|
24
22
|
const info = sourceInfo.lookup(sourceInfo_1.Fields.enum.value, index);
|
|
25
23
|
const valueName = getValueName(ctx, fullName, valueDesc);
|
|
26
24
|
const memberName = getMemberName(ctx, enumDesc, valueDesc);
|
|
27
25
|
if (valueDesc.number === options.unrecognizedEnumValue) {
|
|
28
26
|
unrecognizedEnum = { present: true, name: memberName };
|
|
29
27
|
}
|
|
30
|
-
(0, utils_1.maybeAddComment)(options, info, chunks,
|
|
28
|
+
(0, utils_1.maybeAddComment)(options, info, chunks, valueDesc.options?.deprecated, `${memberName} - `);
|
|
31
29
|
chunks.push((0, ts_poet_1.code) `${memberName} ${delimiter} ${options.stringEnums ? `"${valueName}"` : valueDesc.number.toString()},`);
|
|
32
30
|
});
|
|
33
31
|
if (options.unrecognizedEnum && !unrecognizedEnum.present) {
|
|
@@ -11,9 +11,8 @@ const utils_1 = require("./utils");
|
|
|
11
11
|
* Generates a framework-agnostic service descriptor.
|
|
12
12
|
*/
|
|
13
13
|
function generateGenericServiceDefinition(ctx, fileDesc, sourceInfo, serviceDesc) {
|
|
14
|
-
var _a, _b, _c;
|
|
15
14
|
const chunks = [];
|
|
16
|
-
(0, utils_1.maybeAddComment)(ctx.options, sourceInfo, chunks,
|
|
15
|
+
(0, utils_1.maybeAddComment)(ctx.options, sourceInfo, chunks, serviceDesc.options?.deprecated);
|
|
17
16
|
// Service definition type
|
|
18
17
|
const name = (0, ts_poet_1.def)(`${serviceDesc.name}Definition`);
|
|
19
18
|
chunks.push((0, ts_poet_1.code) `
|
|
@@ -23,7 +22,7 @@ function generateGenericServiceDefinition(ctx, fileDesc, sourceInfo, serviceDesc
|
|
|
23
22
|
chunks.push((0, ts_poet_1.code) `
|
|
24
23
|
export const ${name} = {
|
|
25
24
|
`);
|
|
26
|
-
|
|
25
|
+
serviceDesc.options?.uninterpretedOption;
|
|
27
26
|
chunks.push((0, ts_poet_1.code) `
|
|
28
27
|
name: '${serviceDesc.name}',
|
|
29
28
|
fullName: '${(0, utils_1.maybePrefixPackage)(fileDesc, serviceDesc.name)}',
|
|
@@ -31,7 +30,7 @@ function generateGenericServiceDefinition(ctx, fileDesc, sourceInfo, serviceDesc
|
|
|
31
30
|
`);
|
|
32
31
|
for (const [index, methodDesc] of serviceDesc.method.entries()) {
|
|
33
32
|
const info = sourceInfo.lookup(sourceInfo_1.Fields.service.method, index);
|
|
34
|
-
(0, utils_1.maybeAddComment)(ctx.options, info, chunks,
|
|
33
|
+
(0, utils_1.maybeAddComment)(ctx.options, info, chunks, methodDesc.options?.deprecated);
|
|
35
34
|
chunks.push((0, ts_poet_1.code) `
|
|
36
35
|
${(0, case_1.uncapitalize)(methodDesc.name)}: ${generateMethodDefinition(ctx, methodDesc)},
|
|
37
36
|
`);
|
|
@@ -39,9 +39,8 @@ function generateGrpcJsService(ctx, fileDesc, sourceInfo, serviceDesc) {
|
|
|
39
39
|
}
|
|
40
40
|
exports.generateGrpcJsService = generateGrpcJsService;
|
|
41
41
|
function generateServiceDefinition(ctx, fileDesc, sourceInfo, serviceDesc) {
|
|
42
|
-
var _a, _b;
|
|
43
42
|
const chunks = [];
|
|
44
|
-
(0, utils_1.maybeAddComment)(ctx.options, sourceInfo, chunks,
|
|
43
|
+
(0, utils_1.maybeAddComment)(ctx.options, sourceInfo, chunks, serviceDesc.options?.deprecated);
|
|
45
44
|
// Service definition type
|
|
46
45
|
const name = (0, ts_poet_1.def)(`${serviceDesc.name}Service`);
|
|
47
46
|
chunks.push((0, ts_poet_1.code) `
|
|
@@ -56,7 +55,7 @@ function generateServiceDefinition(ctx, fileDesc, sourceInfo, serviceDesc) {
|
|
|
56
55
|
const inputType = (0, types_1.messageToTypeName)(ctx, methodDesc.inputType);
|
|
57
56
|
const outputType = (0, types_1.messageToTypeName)(ctx, methodDesc.outputType);
|
|
58
57
|
const info = sourceInfo.lookup(sourceInfo_1.Fields.service.method, index);
|
|
59
|
-
(0, utils_1.maybeAddComment)(ctx.options, info, chunks,
|
|
58
|
+
(0, utils_1.maybeAddComment)(ctx.options, info, chunks, methodDesc.options?.deprecated);
|
|
60
59
|
const inputEncoder = (0, encode_1.generateEncoder)(ctx, methodDesc.inputType);
|
|
61
60
|
const outputEncoder = (0, encode_1.generateEncoder)(ctx, methodDesc.outputType);
|
|
62
61
|
const inputDecoder = (0, encode_1.generateDecoder)(ctx, methodDesc.inputType);
|
|
@@ -79,7 +78,6 @@ function generateServiceDefinition(ctx, fileDesc, sourceInfo, serviceDesc) {
|
|
|
79
78
|
return (0, ts_poet_1.joinCode)(chunks, { on: "\n" });
|
|
80
79
|
}
|
|
81
80
|
function generateServerStub(ctx, sourceInfo, serviceDesc) {
|
|
82
|
-
var _a;
|
|
83
81
|
const chunks = [];
|
|
84
82
|
chunks.push((0, ts_poet_1.code) `export interface ${(0, ts_poet_1.def)(`${serviceDesc.name}Server`)} extends ${UntypedServiceImplementation} {`);
|
|
85
83
|
for (const [index, methodDesc] of serviceDesc.method.entries()) {
|
|
@@ -87,7 +85,7 @@ function generateServerStub(ctx, sourceInfo, serviceDesc) {
|
|
|
87
85
|
const inputType = (0, types_1.messageToTypeName)(ctx, methodDesc.inputType);
|
|
88
86
|
const outputType = (0, types_1.messageToTypeName)(ctx, methodDesc.outputType);
|
|
89
87
|
const info = sourceInfo.lookup(sourceInfo_1.Fields.service.method, index);
|
|
90
|
-
(0, utils_1.maybeAddComment)(ctx.options, info, chunks,
|
|
88
|
+
(0, utils_1.maybeAddComment)(ctx.options, info, chunks, methodDesc.options?.deprecated);
|
|
91
89
|
const callType = methodDesc.clientStreaming
|
|
92
90
|
? methodDesc.serverStreaming
|
|
93
91
|
? handleBidiStreamingCall
|
|
@@ -103,7 +101,6 @@ function generateServerStub(ctx, sourceInfo, serviceDesc) {
|
|
|
103
101
|
return (0, ts_poet_1.joinCode)(chunks, { on: "\n" });
|
|
104
102
|
}
|
|
105
103
|
function generateClientStub(ctx, sourceInfo, serviceDesc) {
|
|
106
|
-
var _a;
|
|
107
104
|
const chunks = [];
|
|
108
105
|
chunks.push((0, ts_poet_1.code) `export interface ${(0, ts_poet_1.def)(`${serviceDesc.name}Client`)} extends ${Client} {`);
|
|
109
106
|
for (const [index, methodDesc] of serviceDesc.method.entries()) {
|
|
@@ -111,7 +108,7 @@ function generateClientStub(ctx, sourceInfo, serviceDesc) {
|
|
|
111
108
|
const inputType = (0, types_1.messageToTypeName)(ctx, methodDesc.inputType);
|
|
112
109
|
const outputType = (0, types_1.messageToTypeName)(ctx, methodDesc.outputType);
|
|
113
110
|
const info = sourceInfo.lookup(sourceInfo_1.Fields.service.method, index);
|
|
114
|
-
(0, utils_1.maybeAddComment)(ctx.options, info, chunks,
|
|
111
|
+
(0, utils_1.maybeAddComment)(ctx.options, info, chunks, methodDesc.options?.deprecated);
|
|
115
112
|
const responseCallback = (0, ts_poet_1.code) `(error: ${ServiceError} | null, response: ${outputType}) => void`;
|
|
116
113
|
if (methodDesc.clientStreaming) {
|
|
117
114
|
if (methodDesc.serverStreaming) {
|
package/build/generate-nestjs.js
CHANGED
|
@@ -7,20 +7,18 @@ const sourceInfo_1 = require("./sourceInfo");
|
|
|
7
7
|
const main_1 = require("./main");
|
|
8
8
|
const utils_1 = require("./utils");
|
|
9
9
|
function generateNestjsServiceController(ctx, fileDesc, sourceInfo, serviceDesc) {
|
|
10
|
-
var _a;
|
|
11
10
|
const { options } = ctx;
|
|
12
11
|
const chunks = [];
|
|
13
12
|
const Metadata = (0, ts_poet_1.imp)("Metadata@@grpc/grpc-js");
|
|
14
|
-
(0, utils_1.maybeAddComment)(options, sourceInfo, chunks,
|
|
13
|
+
(0, utils_1.maybeAddComment)(options, sourceInfo, chunks, serviceDesc.options?.deprecated);
|
|
15
14
|
const t = options.context ? `<${main_1.contextTypeVar}>` : "";
|
|
16
15
|
chunks.push((0, ts_poet_1.code) `
|
|
17
16
|
export interface ${serviceDesc.name}Controller${t} {
|
|
18
17
|
`);
|
|
19
18
|
serviceDesc.method.forEach((methodDesc, index) => {
|
|
20
|
-
var _a;
|
|
21
19
|
(0, utils_1.assertInstanceOf)(methodDesc, utils_1.FormattedMethodDescriptor);
|
|
22
20
|
const info = sourceInfo.lookup(sourceInfo_1.Fields.service.method, index);
|
|
23
|
-
(0, utils_1.maybeAddComment)(options, info, chunks,
|
|
21
|
+
(0, utils_1.maybeAddComment)(options, info, chunks, serviceDesc.options?.deprecated);
|
|
24
22
|
const params = [];
|
|
25
23
|
if (options.context) {
|
|
26
24
|
params.push((0, ts_poet_1.code) `ctx: Context`);
|
|
@@ -80,7 +78,6 @@ function generateNestjsServiceClient(ctx, fileDesc, sourceInfo, serviceDesc) {
|
|
|
80
78
|
export interface ${serviceDesc.name}Client${t} {
|
|
81
79
|
`);
|
|
82
80
|
serviceDesc.method.forEach((methodDesc, index) => {
|
|
83
|
-
var _a;
|
|
84
81
|
(0, utils_1.assertInstanceOf)(methodDesc, utils_1.FormattedMethodDescriptor);
|
|
85
82
|
const params = [];
|
|
86
83
|
if (options.context) {
|
|
@@ -98,7 +95,7 @@ function generateNestjsServiceClient(ctx, fileDesc, sourceInfo, serviceDesc) {
|
|
|
98
95
|
// Return observable since nestjs client always returns an Observable
|
|
99
96
|
const returns = (0, types_1.responseObservable)(ctx, methodDesc);
|
|
100
97
|
const info = sourceInfo.lookup(sourceInfo_1.Fields.service.method, index);
|
|
101
|
-
(0, utils_1.maybeAddComment)(options, info, chunks,
|
|
98
|
+
(0, utils_1.maybeAddComment)(options, info, chunks, methodDesc.options?.deprecated);
|
|
102
99
|
chunks.push((0, ts_poet_1.code) `
|
|
103
100
|
${methodDesc.formattedName}(
|
|
104
101
|
${(0, ts_poet_1.joinCode)(params, { on: "," })}
|
|
@@ -19,7 +19,6 @@ function generateNiceGrpcService(ctx, fileDesc, sourceInfo, serviceDesc) {
|
|
|
19
19
|
}
|
|
20
20
|
exports.generateNiceGrpcService = generateNiceGrpcService;
|
|
21
21
|
function generateServerStub(ctx, sourceInfo, serviceDesc) {
|
|
22
|
-
var _a;
|
|
23
22
|
const chunks = [];
|
|
24
23
|
const maybeSuffix = serviceDesc.name.endsWith("Service") ? "" : "Service";
|
|
25
24
|
chunks.push((0, ts_poet_1.code) `export interface ${(0, ts_poet_1.def)(`${serviceDesc.name}${maybeSuffix}Implementation`)}<CallContextExt = {}> {`);
|
|
@@ -32,7 +31,7 @@ function generateServerStub(ctx, sourceInfo, serviceDesc) {
|
|
|
32
31
|
}
|
|
33
32
|
const ServerStreamingMethodResult = ctx.utils.NiceGrpcServerStreamingMethodResult;
|
|
34
33
|
const info = sourceInfo.lookup(sourceInfo_1.Fields.service.method, index);
|
|
35
|
-
(0, utils_1.maybeAddComment)(ctx.options, info, chunks,
|
|
34
|
+
(0, utils_1.maybeAddComment)(ctx.options, info, chunks, methodDesc.options?.deprecated);
|
|
36
35
|
if (methodDesc.clientStreaming) {
|
|
37
36
|
if (methodDesc.serverStreaming) {
|
|
38
37
|
// bidi streaming
|
|
@@ -78,7 +77,6 @@ function generateServerStub(ctx, sourceInfo, serviceDesc) {
|
|
|
78
77
|
return (0, ts_poet_1.joinCode)(chunks, { on: "\n" });
|
|
79
78
|
}
|
|
80
79
|
function generateClientStub(ctx, sourceInfo, serviceDesc) {
|
|
81
|
-
var _a;
|
|
82
80
|
const chunks = [];
|
|
83
81
|
chunks.push((0, ts_poet_1.code) `export interface ${(0, ts_poet_1.def)(`${serviceDesc.name}Client`)}<CallOptionsExt = {}> {`);
|
|
84
82
|
for (const [index, methodDesc] of serviceDesc.method.entries()) {
|
|
@@ -89,7 +87,7 @@ function generateClientStub(ctx, sourceInfo, serviceDesc) {
|
|
|
89
87
|
}
|
|
90
88
|
const outputType = (0, types_1.messageToTypeName)(ctx, methodDesc.outputType, { keepValueType: true });
|
|
91
89
|
const info = sourceInfo.lookup(sourceInfo_1.Fields.service.method, index);
|
|
92
|
-
(0, utils_1.maybeAddComment)(ctx.options, info, chunks,
|
|
90
|
+
(0, utils_1.maybeAddComment)(ctx.options, info, chunks, methodDesc.options?.deprecated);
|
|
93
91
|
if (methodDesc.clientStreaming) {
|
|
94
92
|
if (methodDesc.serverStreaming) {
|
|
95
93
|
// bidi streaming
|
|
@@ -17,17 +17,15 @@ const main_1 = require("./main");
|
|
|
17
17
|
* vs. server-side code/interfaces are handled separately.
|
|
18
18
|
*/
|
|
19
19
|
function generateService(ctx, fileDesc, sourceInfo, serviceDesc) {
|
|
20
|
-
var _a;
|
|
21
20
|
const { options } = ctx;
|
|
22
21
|
const chunks = [];
|
|
23
|
-
(0, utils_1.maybeAddComment)(options, sourceInfo, chunks,
|
|
22
|
+
(0, utils_1.maybeAddComment)(options, sourceInfo, chunks, serviceDesc.options?.deprecated);
|
|
24
23
|
const maybeTypeVar = options.context ? `<${main_1.contextTypeVar}>` : "";
|
|
25
24
|
chunks.push((0, ts_poet_1.code) `export interface ${(0, ts_poet_1.def)(serviceDesc.name)}${maybeTypeVar} {`);
|
|
26
25
|
serviceDesc.method.forEach((methodDesc, index) => {
|
|
27
|
-
var _a;
|
|
28
26
|
(0, utils_1.assertInstanceOf)(methodDesc, utils_1.FormattedMethodDescriptor);
|
|
29
27
|
const info = sourceInfo.lookup(sourceInfo_1.Fields.service.method, index);
|
|
30
|
-
(0, utils_1.maybeAddComment)(options, info, chunks,
|
|
28
|
+
(0, utils_1.maybeAddComment)(options, info, chunks, methodDesc.options?.deprecated);
|
|
31
29
|
const params = [];
|
|
32
30
|
if (options.context) {
|
|
33
31
|
params.push((0, ts_poet_1.code) `ctx: Context`);
|
package/build/main.js
CHANGED
|
@@ -21,7 +21,6 @@ const types_1 = require("./types");
|
|
|
21
21
|
const utils_1 = require("./utils");
|
|
22
22
|
const visit_1 = require("./visit");
|
|
23
23
|
function generateFile(ctx, fileDesc) {
|
|
24
|
-
var _a;
|
|
25
24
|
const { options, utils } = ctx;
|
|
26
25
|
if (options.useOptionals === false) {
|
|
27
26
|
console.warn("ts-proto: Passing useOptionals as a boolean option is deprecated and will be removed in a future version. Please pass the string 'none' instead of false.");
|
|
@@ -52,7 +51,7 @@ function generateFile(ctx, fileDesc) {
|
|
|
52
51
|
// Syntax, unlike most fields, is not repeated and thus does not use an index
|
|
53
52
|
const sourceInfo = sourceInfo_1.default.fromDescriptor(fileDesc);
|
|
54
53
|
const headerComment = sourceInfo.lookup(sourceInfo_1.Fields.file.syntax, undefined);
|
|
55
|
-
(0, utils_1.maybeAddComment)(options, headerComment, chunks,
|
|
54
|
+
(0, utils_1.maybeAddComment)(options, headerComment, chunks, fileDesc.options?.deprecated);
|
|
56
55
|
// Apply formatting to methods here, so they propagate globally
|
|
57
56
|
for (let svc of fileDesc.service) {
|
|
58
57
|
for (let i = 0; i < svc.method.length; i++) {
|
|
@@ -673,10 +672,9 @@ function makeAssertionUtils(bytes) {
|
|
|
673
672
|
}
|
|
674
673
|
// Create the interface with properties
|
|
675
674
|
function generateInterfaceDeclaration(ctx, fullName, messageDesc, sourceInfo, fullTypeName) {
|
|
676
|
-
var _a;
|
|
677
675
|
const { options, currentFile } = ctx;
|
|
678
676
|
const chunks = [];
|
|
679
|
-
(0, utils_1.maybeAddComment)(options, sourceInfo, chunks,
|
|
677
|
+
(0, utils_1.maybeAddComment)(options, sourceInfo, chunks, messageDesc.options?.deprecated);
|
|
680
678
|
// interface name should be defined to avoid import collisions
|
|
681
679
|
chunks.push((0, ts_poet_1.code) `export interface ${(0, ts_poet_1.def)(fullName)} {`);
|
|
682
680
|
if ((0, options_1.addTypeToMessages)(options)) {
|
|
@@ -685,7 +683,6 @@ function generateInterfaceDeclaration(ctx, fullName, messageDesc, sourceInfo, fu
|
|
|
685
683
|
// When oneof=unions, we generate a single property with an ADT per `oneof` clause.
|
|
686
684
|
const processedOneofs = new Set();
|
|
687
685
|
messageDesc.field.forEach((fieldDesc, index) => {
|
|
688
|
-
var _a;
|
|
689
686
|
if ((0, types_1.isWithinOneOfThatShouldBeUnion)(options, fieldDesc)) {
|
|
690
687
|
const { oneofIndex } = fieldDesc;
|
|
691
688
|
if (!processedOneofs.has(oneofIndex)) {
|
|
@@ -695,7 +692,7 @@ function generateInterfaceDeclaration(ctx, fullName, messageDesc, sourceInfo, fu
|
|
|
695
692
|
return;
|
|
696
693
|
}
|
|
697
694
|
const info = sourceInfo.lookup(sourceInfo_1.Fields.message.field, index);
|
|
698
|
-
(0, utils_1.maybeAddComment)(options, info, chunks,
|
|
695
|
+
(0, utils_1.maybeAddComment)(options, info, chunks, fieldDesc.options?.deprecated);
|
|
699
696
|
const fieldKey = (0, utils_1.safeAccessor)((0, utils_1.getFieldName)(fieldDesc, options));
|
|
700
697
|
const isOptional = (0, types_1.isOptionalProperty)(fieldDesc, messageDesc.options, options, currentFile.isProto3Syntax);
|
|
701
698
|
const type = (0, types_1.toTypeName)(ctx, messageDesc, fieldDesc, isOptional);
|
|
@@ -1054,13 +1051,13 @@ function getEncodeWriteSnippet(ctx, field) {
|
|
|
1054
1051
|
case "int64":
|
|
1055
1052
|
case "sint64":
|
|
1056
1053
|
case "sfixed64":
|
|
1057
|
-
return (place, placeAlt) => (0, ts_poet_1.code) `if (BigInt.asIntN(64, ${place}) !== ${placeAlt
|
|
1054
|
+
return (place, placeAlt) => (0, ts_poet_1.code) `if (BigInt.asIntN(64, ${place}) !== ${placeAlt ?? place}) {
|
|
1058
1055
|
throw new ${utils.globalThis}.Error('value provided for field ${place} of type ${fieldType} too large');
|
|
1059
1056
|
}
|
|
1060
1057
|
writer.uint32(${tag}).${(0, types_1.toReaderCall)(field)}(${place}.toString())`;
|
|
1061
1058
|
case "uint64":
|
|
1062
1059
|
case "fixed64":
|
|
1063
|
-
return (place, placeAlt) => (0, ts_poet_1.code) `if (BigInt.asUintN(64, ${place}) !== ${placeAlt
|
|
1060
|
+
return (place, placeAlt) => (0, ts_poet_1.code) `if (BigInt.asUintN(64, ${place}) !== ${placeAlt ?? place}) {
|
|
1064
1061
|
throw new ${utils.globalThis}.Error('value provided for field ${place} of type ${fieldType} too large');
|
|
1065
1062
|
}
|
|
1066
1063
|
writer.uint32(${tag}).${(0, types_1.toReaderCall)(field)}(${place}.toString())`;
|
|
@@ -1385,11 +1382,10 @@ function generateGetExtension(ctx, fullName) {
|
|
|
1385
1382
|
`;
|
|
1386
1383
|
}
|
|
1387
1384
|
function generateExtension(ctx, message, extension) {
|
|
1388
|
-
var _a;
|
|
1389
1385
|
const type = (0, types_1.toTypeName)(ctx, message, extension);
|
|
1390
1386
|
const packedTag = (0, types_1.isRepeated)(extension) && (0, types_1.packedType)(extension.type) !== undefined ? ((extension.number << 3) | 2) >>> 0 : undefined;
|
|
1391
1387
|
const singularTag = ((extension.number << 3) | (0, types_1.basicWireType)(extension.type)) >>> 0;
|
|
1392
|
-
const tag = packedTag
|
|
1388
|
+
const tag = packedTag ?? singularTag;
|
|
1393
1389
|
const chunks = [];
|
|
1394
1390
|
chunks.push((0, ts_poet_1.code) `{`);
|
|
1395
1391
|
chunks.push((0, ts_poet_1.code) `number: ${extension.number},`);
|
|
@@ -1397,7 +1393,7 @@ function generateExtension(ctx, message, extension) {
|
|
|
1397
1393
|
if (packedTag !== undefined)
|
|
1398
1394
|
chunks.push((0, ts_poet_1.code) `singularTag: ${singularTag},`);
|
|
1399
1395
|
chunks.push((0, ts_poet_1.code) `repeated: ${extension.label == ts_proto_descriptors_1.FieldDescriptorProto_Label.LABEL_REPEATED},`);
|
|
1400
|
-
chunks.push((0, ts_poet_1.code) `packed: ${
|
|
1396
|
+
chunks.push((0, ts_poet_1.code) `packed: ${extension.options?.packed ? true : false},`);
|
|
1401
1397
|
const Reader = (0, utils_1.impFile)(ctx.options, "Reader@protobufjs/minimal");
|
|
1402
1398
|
const Writer = (0, utils_1.impFile)(ctx.options, "Writer@protobufjs/minimal");
|
|
1403
1399
|
if (ctx.options.outputEncodeMethods === true ||
|
|
@@ -1479,7 +1475,7 @@ function generateExtension(ctx, message, extension) {
|
|
|
1479
1475
|
}
|
|
1480
1476
|
else if ((0, types_1.isScalar)(extension) || (0, types_1.isEnum)(extension)) {
|
|
1481
1477
|
chunks.push((0, ts_poet_1.code) `
|
|
1482
|
-
if (${(0, types_1.notDefaultCheck)(ctx, extension, message
|
|
1478
|
+
if (${(0, types_1.notDefaultCheck)(ctx, extension, message?.options, "value")}) {
|
|
1483
1479
|
const writer = ${Writer}.create();
|
|
1484
1480
|
${writeSnippet("value")};
|
|
1485
1481
|
encoded.push(writer.finish());
|
|
@@ -1578,7 +1574,6 @@ function generateFromJson(ctx, fullName, fullTypeName, messageDesc) {
|
|
|
1578
1574
|
};
|
|
1579
1575
|
// add a check for each incoming field
|
|
1580
1576
|
messageDesc.field.forEach((field) => {
|
|
1581
|
-
var _a;
|
|
1582
1577
|
const fieldName = (0, utils_1.getFieldName)(field, options);
|
|
1583
1578
|
const fieldKey = (0, utils_1.safeAccessor)(fieldName);
|
|
1584
1579
|
const jsonName = (0, utils_1.getFieldJsonName)(field, options);
|
|
@@ -1586,7 +1581,6 @@ function generateFromJson(ctx, fullName, fullTypeName, messageDesc) {
|
|
|
1586
1581
|
const jsonPropertyOptional = (0, utils_1.getPropertyAccessor)("object", jsonName, true);
|
|
1587
1582
|
// get code that extracts value from incoming object
|
|
1588
1583
|
const readSnippet = (from) => {
|
|
1589
|
-
var _a;
|
|
1590
1584
|
if ((0, types_1.isEnum)(field)) {
|
|
1591
1585
|
const fromJson = (0, types_1.getEnumMethod)(ctx, field.typeName, "FromJSON");
|
|
1592
1586
|
return (0, ts_poet_1.code) `${fromJson}(${from})`;
|
|
@@ -1602,7 +1596,7 @@ function generateFromJson(ctx, fullName, fullTypeName, messageDesc) {
|
|
|
1602
1596
|
}
|
|
1603
1597
|
}
|
|
1604
1598
|
else if ((0, types_1.isLong)(field) && (0, types_1.isJsTypeFieldOption)(options, field)) {
|
|
1605
|
-
const fieldType = (
|
|
1599
|
+
const fieldType = (0, types_1.getFieldOptionsJsType)(field, ctx.options) ?? field.type;
|
|
1606
1600
|
const cstr = (0, case_1.capitalize)((0, types_1.basicTypeName)(ctx, { ...field, type: fieldType }, { keepValueType: true }).toCodeString([]));
|
|
1607
1601
|
return (0, ts_poet_1.code) `${utils.globalThis}.${cstr}(${from})`;
|
|
1608
1602
|
}
|
|
@@ -1715,7 +1709,7 @@ function generateFromJson(ctx, fullName, fullTypeName, messageDesc) {
|
|
|
1715
1709
|
const noDefaultValue = !options.initializeFieldsAsUndefined &&
|
|
1716
1710
|
(0, types_1.isOptionalProperty)(field, messageDesc.options, options, currentFile.isProto3Syntax);
|
|
1717
1711
|
// and then use the snippet to handle repeated fields if necessary
|
|
1718
|
-
if (
|
|
1712
|
+
if (canonicalFromJson[fullTypeName]?.[fieldName]) {
|
|
1719
1713
|
chunks.push((0, ts_poet_1.code) `${fieldName}: ${canonicalFromJson[fullTypeName][fieldName]("object")},`);
|
|
1720
1714
|
}
|
|
1721
1715
|
else if ((0, types_1.isRepeated)(field)) {
|
|
@@ -1838,7 +1832,6 @@ function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
|
|
|
1838
1832
|
const jsonProperty = (0, utils_1.getPropertyAccessor)("obj", jsonName);
|
|
1839
1833
|
const messageProperty = (0, utils_1.getPropertyAccessor)("message", fieldName);
|
|
1840
1834
|
const readSnippet = (from) => {
|
|
1841
|
-
var _a;
|
|
1842
1835
|
if ((0, types_1.isEnum)(field)) {
|
|
1843
1836
|
const toJson = (0, types_1.getEnumMethod)(ctx, field.typeName, "ToJSON");
|
|
1844
1837
|
return (0, ts_poet_1.code) `${toJson}(${from})`;
|
|
@@ -1917,7 +1910,7 @@ function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
|
|
|
1917
1910
|
return (0, ts_poet_1.code) `${utils.base64FromBytes}(${from})`;
|
|
1918
1911
|
}
|
|
1919
1912
|
else if ((0, types_1.isLong)(field) && (0, types_1.isJsTypeFieldOption)(options, field)) {
|
|
1920
|
-
const fieldType = (
|
|
1913
|
+
const fieldType = (0, types_1.getFieldOptionsJsType)(field, ctx.options) ?? field.type;
|
|
1921
1914
|
if (!fieldType) {
|
|
1922
1915
|
return (0, ts_poet_1.code) `${from}`;
|
|
1923
1916
|
}
|
package/build/options.d.ts
CHANGED
|
@@ -97,6 +97,7 @@ export type Options = {
|
|
|
97
97
|
useNullAsOptional: boolean;
|
|
98
98
|
annotateFilesWithVersion: boolean;
|
|
99
99
|
noDefaultsForOptionals: boolean;
|
|
100
|
+
bigIntLiteral: boolean;
|
|
100
101
|
};
|
|
101
102
|
export declare function defaultOptions(): Options;
|
|
102
103
|
export declare function optionsFromParameter(parameter: string | undefined): Options;
|
package/build/options.js
CHANGED
package/build/schema.js
CHANGED
|
@@ -10,7 +10,6 @@ const minimal_1 = require("protobufjs/minimal");
|
|
|
10
10
|
const fileDescriptorProto = (0, ts_poet_1.imp)("FileDescriptorProto@ts-proto-descriptors");
|
|
11
11
|
const extensionCache = {};
|
|
12
12
|
function generateSchema(ctx, fileDesc, sourceInfo) {
|
|
13
|
-
var _a;
|
|
14
13
|
const { options } = ctx;
|
|
15
14
|
const chunks = [];
|
|
16
15
|
fileDesc.extension.forEach((extension) => {
|
|
@@ -73,7 +72,7 @@ function generateSchema(ctx, fileDesc, sourceInfo) {
|
|
|
73
72
|
const descriptor = ts_proto_descriptors_1.FileDescriptorProto.fromPartial(fileDesc);
|
|
74
73
|
// Only keep locations that include comments
|
|
75
74
|
descriptor.sourceCodeInfo = {
|
|
76
|
-
location:
|
|
75
|
+
location: descriptor.sourceCodeInfo?.location.filter((loc) => loc["leadingComments"] || loc["trailingComments"]) || [],
|
|
77
76
|
};
|
|
78
77
|
let fileOptions;
|
|
79
78
|
if (fileDesc.options) {
|
package/build/types.js
CHANGED
|
@@ -61,9 +61,8 @@ function basicLongWireType(type) {
|
|
|
61
61
|
exports.basicLongWireType = basicLongWireType;
|
|
62
62
|
/** Returns the type name without any repeated/required/etc. labels. */
|
|
63
63
|
function basicTypeName(ctx, field, typeOptions = {}) {
|
|
64
|
-
var _a, _b;
|
|
65
64
|
const { options } = ctx;
|
|
66
|
-
const fieldType =
|
|
65
|
+
const fieldType = getFieldOptionsJsType(field, ctx.options) ?? field.type;
|
|
67
66
|
switch (fieldType) {
|
|
68
67
|
case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_DOUBLE:
|
|
69
68
|
case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_FLOAT:
|
|
@@ -79,7 +78,7 @@ function basicTypeName(ctx, field, typeOptions = {}) {
|
|
|
79
78
|
case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_FIXED64:
|
|
80
79
|
case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_SFIXED64:
|
|
81
80
|
return isJsTypeFieldOption(options, field)
|
|
82
|
-
?
|
|
81
|
+
? jsTypeName(field) ?? longTypeName(ctx)
|
|
83
82
|
: // this handles 2^53, Long is only needed for 2^64; this is effectively pbjs's forceNumber
|
|
84
83
|
longTypeName(ctx);
|
|
85
84
|
case ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_BOOL:
|
|
@@ -170,8 +169,7 @@ function packedType(type) {
|
|
|
170
169
|
}
|
|
171
170
|
exports.packedType = packedType;
|
|
172
171
|
function getFieldOptionsJsType(field, options) {
|
|
173
|
-
|
|
174
|
-
if (!options.useJsTypeOverride || ((_a = field.options) === null || _a === void 0 ? void 0 : _a.jstype) === undefined) {
|
|
172
|
+
if (!options.useJsTypeOverride || field.options?.jstype === undefined) {
|
|
175
173
|
return;
|
|
176
174
|
}
|
|
177
175
|
switch (field.options.jstype) {
|
|
@@ -242,7 +240,7 @@ function defaultValue(ctx, field) {
|
|
|
242
240
|
return `"${numericDefaultVal}"`;
|
|
243
241
|
}
|
|
244
242
|
else if (options.forceLong === options_1.LongOption.BIGINT) {
|
|
245
|
-
return `BigInt("${numericDefaultVal}")`;
|
|
243
|
+
return options.bigIntLiteral ? (0, ts_poet_1.code) `${numericDefaultVal}n` : (0, ts_poet_1.code) `BigInt("${numericDefaultVal}")`;
|
|
246
244
|
}
|
|
247
245
|
else {
|
|
248
246
|
return numericDefaultVal;
|
|
@@ -375,12 +373,12 @@ function isOptionalProperty(field, messageOptions, options, isProto3Syntax) {
|
|
|
375
373
|
const optionalAll = options.useOptionals === "all";
|
|
376
374
|
const deprecatedOnly = options.useOptionals === "deprecatedOnly" && field.options && field.options.deprecated;
|
|
377
375
|
return ((optionalMessages && isMessage(field) && !isRepeated(field)) ||
|
|
378
|
-
((optionalAll || deprecatedOnly) && !
|
|
376
|
+
((optionalAll || deprecatedOnly) && !messageOptions?.mapEntry) ||
|
|
379
377
|
(options.noDefaultsForOptionals && !isRepeated(field) && (isScalar(field) || isEnum(field))) ||
|
|
380
378
|
// file is proto2, we have enabled proto2 optionals, and the field itself is optional
|
|
381
379
|
(!isProto3Syntax &&
|
|
382
380
|
field.label === ts_proto_descriptors_1.FieldDescriptorProto_Label.LABEL_OPTIONAL &&
|
|
383
|
-
!
|
|
381
|
+
!messageOptions?.mapEntry &&
|
|
384
382
|
!options.disableProto2Optionals) ||
|
|
385
383
|
// don't bother verifying that oneof is not union. union oneofs generate their own properties.
|
|
386
384
|
isWithinOneOf(field) ||
|
|
@@ -573,17 +571,15 @@ function longTypeName(ctx) {
|
|
|
573
571
|
}
|
|
574
572
|
}
|
|
575
573
|
function jsTypeName(field) {
|
|
576
|
-
|
|
577
|
-
if (((_a = field.options) === null || _a === void 0 ? void 0 : _a.jstype) === ts_proto_descriptors_1.FieldOptions_JSType.JS_STRING) {
|
|
574
|
+
if (field.options?.jstype === ts_proto_descriptors_1.FieldOptions_JSType.JS_STRING) {
|
|
578
575
|
return (0, ts_poet_1.code) `string`;
|
|
579
576
|
}
|
|
580
|
-
else if (
|
|
577
|
+
else if (field.options?.jstype === ts_proto_descriptors_1.FieldOptions_JSType.JS_NUMBER) {
|
|
581
578
|
return (0, ts_poet_1.code) `number`;
|
|
582
579
|
}
|
|
583
580
|
}
|
|
584
581
|
/** Maps `.some_proto_namespace.Message` to a TypeName. */
|
|
585
582
|
function messageToTypeName(ctx, protoType, typeOptions = {}) {
|
|
586
|
-
var _a;
|
|
587
583
|
const { options, typeMap } = ctx;
|
|
588
584
|
// Watch for the wrapper types `.google.protobuf.*Value`. If we're mapping
|
|
589
585
|
// them to basic built-in types, we union the type with undefined to
|
|
@@ -591,7 +587,7 @@ function messageToTypeName(ctx, protoType, typeOptions = {}) {
|
|
|
591
587
|
// - If the field is repeated, values cannot be undefined.
|
|
592
588
|
let valueType = valueTypeName(ctx, protoType);
|
|
593
589
|
if (!typeOptions.keepValueType && valueType) {
|
|
594
|
-
if (
|
|
590
|
+
if (typeOptions.repeated ?? false) {
|
|
595
591
|
return valueType;
|
|
596
592
|
}
|
|
597
593
|
return (0, ts_poet_1.code) `${valueType} | ${(0, utils_1.nullOrUndefined)(options)}`;
|
|
@@ -624,14 +620,13 @@ function getEnumMethod(ctx, enumProtoType, methodSuffix) {
|
|
|
624
620
|
exports.getEnumMethod = getEnumMethod;
|
|
625
621
|
/** Return the TypeName for any field (primitive/message/etc.) as exposed in the interface. */
|
|
626
622
|
function toTypeName(ctx, messageDesc, field, ensureOptional = false) {
|
|
627
|
-
var _a;
|
|
628
623
|
function finalize(type, isOptional) {
|
|
629
624
|
if (isOptional) {
|
|
630
625
|
return (0, ts_poet_1.code) `${type} | ${(0, utils_1.nullOrUndefined)(ctx.options, field.proto3Optional)}`;
|
|
631
626
|
}
|
|
632
627
|
return type;
|
|
633
628
|
}
|
|
634
|
-
const fieldType =
|
|
629
|
+
const fieldType = getFieldOptionsJsType(field, ctx.options) ?? field.type;
|
|
635
630
|
const type = basicTypeName(ctx, { ...field, type: fieldType }, { keepValueType: false });
|
|
636
631
|
if (isRepeated(field)) {
|
|
637
632
|
const mapType = messageDesc ? detectMapType(ctx, messageDesc, field) : false;
|
|
@@ -697,12 +692,11 @@ function shouldGenerateJSMapType(ctx, message, field) {
|
|
|
697
692
|
}
|
|
698
693
|
exports.shouldGenerateJSMapType = shouldGenerateJSMapType;
|
|
699
694
|
function detectMapType(ctx, messageDesc, fieldDesc) {
|
|
700
|
-
var _a;
|
|
701
695
|
const { typeMap } = ctx;
|
|
702
696
|
if (fieldDesc.label === ts_proto_descriptors_1.FieldDescriptorProto_Label.LABEL_REPEATED &&
|
|
703
697
|
fieldDesc.type === ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_MESSAGE) {
|
|
704
698
|
const mapType = typeMap.get(fieldDesc.typeName)[2];
|
|
705
|
-
if (!
|
|
699
|
+
if (!mapType.options?.mapEntry)
|
|
706
700
|
return undefined;
|
|
707
701
|
const [keyField, valueField] = mapType.field;
|
|
708
702
|
const keyType = toTypeName(ctx, messageDesc, keyField);
|
|
@@ -799,8 +793,7 @@ function hasSingleRepeatedField(messageDesc) {
|
|
|
799
793
|
return messageDesc.field.length == 1 && messageDesc.field[0].label === ts_proto_descriptors_1.FieldDescriptorProto_Label.LABEL_REPEATED;
|
|
800
794
|
}
|
|
801
795
|
function isJsTypeFieldOption(options, field) {
|
|
802
|
-
var _a, _b;
|
|
803
796
|
return (options.useJsTypeOverride &&
|
|
804
|
-
(
|
|
797
|
+
(field.options?.jstype === ts_proto_descriptors_1.FieldOptions_JSType.JS_NUMBER || field.options?.jstype === ts_proto_descriptors_1.FieldOptions_JSType.JS_STRING));
|
|
805
798
|
}
|
|
806
799
|
exports.isJsTypeFieldOption = isJsTypeFieldOption;
|
package/build/utils.js
CHANGED
|
@@ -283,7 +283,6 @@ function withAndMaybeCheckIsNull(options, typeName) {
|
|
|
283
283
|
}
|
|
284
284
|
exports.withAndMaybeCheckIsNull = withAndMaybeCheckIsNull;
|
|
285
285
|
async function getVersions(request) {
|
|
286
|
-
var _a;
|
|
287
286
|
let protocVersion = "unknown";
|
|
288
287
|
if (request.compilerVersion) {
|
|
289
288
|
const { major, minor, patch } = request.compilerVersion;
|
|
@@ -291,7 +290,7 @@ async function getVersions(request) {
|
|
|
291
290
|
}
|
|
292
291
|
const path = "../package.json";
|
|
293
292
|
const packageJson = await Promise.resolve(`${path}`).then(s => require(s));
|
|
294
|
-
const tsProtoVersion = `v${
|
|
293
|
+
const tsProtoVersion = `v${packageJson?.version ?? "unknown"}`;
|
|
295
294
|
return {
|
|
296
295
|
protocVersion,
|
|
297
296
|
tsProtoVersion,
|