ts-proto 1.173.0 → 1.175.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 CHANGED
@@ -305,6 +305,8 @@ Generated code will be placed in the Gradle build directory.
305
305
 
306
306
  The default behavior is `forceLong=number`, which will internally still use the `long` library to encode/decode values on the wire (so you will still see a `util.Long = Long` line in your output), but will convert the `long` values to `number` automatically for you. Note that a runtime error is thrown if, while doing this conversion, a 64-bit value is larger than can be correctly stored as a `number`.
307
307
 
308
+ - With `--ts_proto_opt=useJsTypeOverride`, 64-bit numbers will be ouput as the [FieldOption.JSType](https://protobuf.dev/reference/java/api-docs/com/google/protobuf/DescriptorProtos.FieldOptions.JSType) specified on the field. This takes precendence over the `forceLong` option provided.
309
+
308
310
  - With `--ts_proto_opt=esModuleInterop=true` changes output to be `esModuleInterop` compliant.
309
311
 
310
312
  Specifically the `Long` imports will be generated as `import Long from 'long'` instead of `import * as Long from 'long'`.
@@ -430,6 +432,8 @@ Generated code will be placed in the Gradle build directory.
430
432
 
431
433
  - With `--ts_proto_opt=useMongoObjectId=true`, fields of a type called ObjectId where the message is constructed to have on field called value that is a string will be mapped to type `mongodb.ObjectId` in the generated types. This will require your project to install the mongodb npm package. See [ObjectId](#objectid) for more details.
432
434
 
435
+ - With `--ts_proto_opt=annotateFilesWithVersion=false`, the generated files will not contain the versions of `protoc` and `ts-proto` used to generate the file. This option is normally set to `true`, such that files list the versions used.
436
+
433
437
  - With `--ts_proto_opt=outputSchema=true`, meta typings will be generated that can later be used in other code generators.
434
438
 
435
439
  - With `--ts_proto_opt=outputTypeAnnotations=true`, each message will be given a `$type` field containing its fully-qualified name. You can use `--ts_proto_opt=outputTypeAnnotations=static-only` to omit it from the `interface` declaration, or `--ts_proto_opt=outputTypeAnnotations=optional` to make it an optional property on the `interface` definition. The latter option may be useful if you want to use the `$type` field for runtime type checking on responses from a server.
@@ -785,9 +789,7 @@ The following helper types may make it easier to work with the types generated f
785
789
  type OneOfCases<T> = T extends { $case: infer U extends string } ? U : never;
786
790
 
787
791
  /** Extracts a union of all the value types from a oneOf field */
788
- type OneOfValues<T> = T extends { $case: infer U extends string; [key: string]: unknown }
789
- ? T[U]
790
- : never;
792
+ type OneOfValues<T> = T extends { $case: infer U extends string; [key: string]: unknown } ? T[U] : never;
791
793
 
792
794
  /** Extracts the specific type of a oneOf case based on its field name */
793
795
  type OneOfCase<T, K extends OneOfCases<T>> = T extends {
@@ -95,8 +95,9 @@ export type Options = {
95
95
  disableProto2Optionals: boolean;
96
96
  disableProto2DefaultValues: boolean;
97
97
  useNullAsOptional: boolean;
98
+ annotateFilesWithVersion: boolean;
98
99
  };
99
100
  export declare function defaultOptions(): Options;
100
101
  export declare function optionsFromParameter(parameter: string | undefined): Options;
101
- export declare function getTsPoetOpts(options: Options): ToStringOpts;
102
+ export declare function getTsPoetOpts(options: Options, tsProtoVersion: string, protocVersion: string, fileName?: string): ToStringOpts;
102
103
  export declare function addTypeToMessages(options: Options): boolean;
package/build/options.js CHANGED
@@ -102,6 +102,7 @@ function defaultOptions() {
102
102
  disableProto2Optionals: false,
103
103
  disableProto2DefaultValues: false,
104
104
  useNullAsOptional: false,
105
+ annotateFilesWithVersion: true,
105
106
  };
106
107
  }
107
108
  exports.defaultOptions = defaultOptions;
@@ -245,11 +246,22 @@ function parseParameter(parameter) {
245
246
  function parseParamValue(value) {
246
247
  return value === "true" ? true : value === "false" ? false : value;
247
248
  }
248
- function getTsPoetOpts(options) {
249
+ function getTsPoetOpts(options, tsProtoVersion, protocVersion, fileName) {
249
250
  const { importSuffix, esModuleInterop } = options;
250
251
  const pbjs = "protobufjs/minimal" + importSuffix;
251
252
  return {
252
- prefix: `/* eslint-disable */`,
253
+ // Comment block at the top of every source file, since these comments require specific
254
+ // syntax incompatible with ts-poet, we will hard-code the string and prepend to the
255
+ // generator output.
256
+ prefix: `// Code generated by protoc-gen-ts_proto. DO NOT EDIT.${options.annotateFilesWithVersion
257
+ ? `
258
+ // versions:
259
+ // protoc-gen-ts_proto ${tsProtoVersion}
260
+ // protoc ${protocVersion}`
261
+ : ""}
262
+ ${fileName ? `// source: ${fileName}` : ""}
263
+
264
+ /* eslint-disable */`,
253
265
  dprintOptions: { preferSingleLine: true, lineWidth: 120 },
254
266
  forceRequireImport: esModuleInterop ? [] : ["long"],
255
267
  forceDefaultImport: esModuleInterop ? [pbjs] : [],
package/build/plugin.js CHANGED
@@ -14,6 +14,7 @@ async function main() {
14
14
  // const json = JSON.parse(stdin.toString());
15
15
  // const request = CodeGeneratorRequest.fromObject(json);
16
16
  const request = ts_proto_descriptors_1.CodeGeneratorRequest.decode(stdin);
17
+ const { protocVersion, tsProtoVersion } = await (0, utils_1.getVersions)(request);
17
18
  const options = (0, options_1.optionsFromParameter)(request.parameter);
18
19
  const typeMap = (0, types_1.createTypeMap)(request, options);
19
20
  const utils = (0, main_1.makeUtils)(options);
@@ -42,7 +43,7 @@ async function main() {
42
43
  }
43
44
  const files = await Promise.all(filesToGenerate.map(async (file) => {
44
45
  const [path, code] = (0, main_1.generateFile)({ ...ctx, currentFile: (0, context_1.createFileContext)(file) }, file);
45
- const content = code.toString({ ...(0, options_1.getTsPoetOpts)(options), path });
46
+ const content = code.toString({ ...(0, options_1.getTsPoetOpts)(options, tsProtoVersion, protocVersion, file.name), path });
46
47
  return { name: path, content };
47
48
  }));
48
49
  if (options.outputTypeRegistry) {
@@ -50,12 +51,12 @@ async function main() {
50
51
  const ctx = { options, typeMap, utils };
51
52
  const path = "typeRegistry.ts";
52
53
  const code = (0, generate_type_registry_1.generateTypeRegistry)(ctx);
53
- const content = code.toString({ ...(0, options_1.getTsPoetOpts)(options), path });
54
+ const content = code.toString({ ...(0, options_1.getTsPoetOpts)(options, tsProtoVersion, protocVersion), path });
54
55
  files.push({ name: path, content });
55
56
  }
56
57
  if (options.outputIndex) {
57
58
  for (const [path, code] of (0, utils_1.generateIndexFiles)(filesToGenerate, options)) {
58
- const content = code.toString({ ...(0, options_1.getTsPoetOpts)(options), path });
59
+ const content = code.toString({ ...(0, options_1.getTsPoetOpts)(options, tsProtoVersion, protocVersion), path });
59
60
  files.push({ name: path, content });
60
61
  }
61
62
  }
package/build/utils.d.ts CHANGED
@@ -14,7 +14,6 @@ export declare function lowerFirst(name: string): string;
14
14
  export declare function upperFirst(name: string): string;
15
15
  /** Removes potentially harmful characters from comments and pushes it into chunks. */
16
16
  export declare function maybeAddComment(options: Pick<Options, "comments">, desc: Partial<Pick<SourceDescription, "leadingComments" | "trailingComments">>, chunks: Code[], deprecated?: boolean, prefix?: string): void;
17
- export declare function prefixDisableLinter(spec: string): string;
18
17
  export declare function maybePrefixPackage(fileDesc: FileDescriptorProto, rest: string): string;
19
18
  /**
20
19
  * Asserts that an object is an instance of a certain class
@@ -78,3 +77,7 @@ export declare function withOrMaybeCheckIsNotNull(options: Pick<Options, "useNul
78
77
  export declare function withOrMaybeCheckIsNull(options: Pick<Options, "useNullAsOptional">, typeName: string): string;
79
78
  export declare function withAndMaybeCheckIsNotNull(options: Pick<Options, "useNullAsOptional">, typeName: string): string;
80
79
  export declare function withAndMaybeCheckIsNull(options: Pick<Options, "useNullAsOptional">, typeName: string): string;
80
+ export declare function getVersions(request: CodeGeneratorRequest): Promise<{
81
+ protocVersion: string;
82
+ tsProtoVersion: string;
83
+ }>;
package/build/utils.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.withAndMaybeCheckIsNull = exports.withAndMaybeCheckIsNotNull = exports.withOrMaybeCheckIsNull = exports.withOrMaybeCheckIsNotNull = exports.maybeCheckIsNull = exports.maybeCheckIsNotNull = exports.nullOrUndefined = exports.arrowFunction = exports.tryCatchBlock = exports.impProto = exports.impFile = exports.getPropertyAccessor = exports.safeAccessor = exports.getFieldName = exports.getFieldJsonName = exports.FormattedMethodDescriptor = exports.assertInstanceOf = exports.maybePrefixPackage = exports.prefixDisableLinter = exports.maybeAddComment = exports.upperFirst = exports.lowerFirst = exports.singular = exports.fail = exports.readToBuffer = exports.generateIndexFiles = exports.protoFilesToGenerate = void 0;
3
+ exports.getVersions = exports.withAndMaybeCheckIsNull = exports.withAndMaybeCheckIsNotNull = exports.withOrMaybeCheckIsNull = exports.withOrMaybeCheckIsNotNull = exports.maybeCheckIsNull = exports.maybeCheckIsNotNull = exports.nullOrUndefined = exports.arrowFunction = exports.tryCatchBlock = exports.impProto = exports.impFile = exports.getPropertyAccessor = exports.safeAccessor = exports.getFieldName = exports.getFieldJsonName = exports.FormattedMethodDescriptor = exports.assertInstanceOf = exports.maybePrefixPackage = exports.maybeAddComment = exports.upperFirst = exports.lowerFirst = exports.singular = exports.fail = exports.readToBuffer = exports.generateIndexFiles = exports.protoFilesToGenerate = void 0;
4
4
  const path = require("path");
5
5
  const ts_poet_1 = require("ts-poet");
6
6
  const options_1 = require("./options");
@@ -116,13 +116,6 @@ function maybeAddComment(options, desc, chunks, deprecated, prefix = "") {
116
116
  }
117
117
  }
118
118
  exports.maybeAddComment = maybeAddComment;
119
- // Comment block at the top of every source file, since these comments require specific
120
- // syntax incompatible with ts-poet, we will hard-code the string and prepend to the
121
- // generator output.
122
- function prefixDisableLinter(spec) {
123
- return `/* eslint-disable */\n${spec}`;
124
- }
125
- exports.prefixDisableLinter = prefixDisableLinter;
126
119
  function maybePrefixPackage(fileDesc, rest) {
127
120
  const prefix = fileDesc.package === "" ? "" : `${fileDesc.package}.`;
128
121
  return `${prefix}${rest}`;
@@ -289,3 +282,19 @@ function withAndMaybeCheckIsNull(options, typeName) {
289
282
  return maybeCheckIsNotNull(options, typeName, "&&");
290
283
  }
291
284
  exports.withAndMaybeCheckIsNull = withAndMaybeCheckIsNull;
285
+ async function getVersions(request) {
286
+ var _a;
287
+ let protocVersion = "unknown";
288
+ if (request.compilerVersion) {
289
+ const { major, minor, patch } = request.compilerVersion;
290
+ protocVersion = `v${major}.${minor}.${patch}`;
291
+ }
292
+ const path = "../package.json";
293
+ const packageJson = await Promise.resolve(`${path}`).then(s => require(s));
294
+ const tsProtoVersion = `v${(_a = packageJson === null || packageJson === void 0 ? void 0 : packageJson.version) !== null && _a !== void 0 ? _a : "unknown"}`;
295
+ return {
296
+ protocVersion,
297
+ tsProtoVersion,
298
+ };
299
+ }
300
+ exports.getVersions = getVersions;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.173.0",
3
+ "version": "1.175.0",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",