ts-proto 2.6.0 → 2.7.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
@@ -530,7 +530,7 @@ Generated code will be placed in the Gradle build directory.
530
530
 
531
531
  - With `--ts_proto_opt=useNumericEnumForJson=true`, the JSON converter (`toJSON`) will encode enum values as int, rather than a string literal.
532
532
 
533
- - With `--ts_proto_opt=initializeFieldsAsUndefined=false`, all optional field initializers will be omitted from the generated base instances.
533
+ - With `--ts_proto_opt=initializeFieldsAsUndefined=false`, all optional field initializers will be omitted from the generated base instances. This can be useful if you're using Next.js and `getServerSideProps`, see [#492](https://github.com/stephenh/ts-proto/issues/492). The default is `true` to ideally get the best v8 performance by avoiding "changing shapes" as fields are incrementally set.
534
534
 
535
535
  - With `--ts_proto_opt=disableProto2Optionals=true`, all optional fields on proto2 files will not be set to be optional. Please note that this flag is primarily for preserving ts-proto's legacy handling of proto2 files, to avoid breaking changes, and as a result, it is not intended to be used moving forward.
536
536
 
@@ -645,6 +645,7 @@ export interface User {
645
645
 
646
646
  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.
647
647
 
648
+ - With `--ts_proto_opt=useDuration=string`, the generated code will use the `string` type for `google.protobuf.Duration` instead of `{seconds: number, nano: number}`.
648
649
 
649
650
  ### NestJS Support
650
651
 
@@ -701,8 +702,8 @@ const sendRequest: RpcImpl = (service, method, data) => {
701
702
  return argument;
702
703
  }
703
704
 
704
- // Using passThrough as the serialize and deserialize functions
705
- conn.makeUnaryRequest(path, passThrough, passThrough, data, resultCallback);
705
+ // Using passThrough as the deserialize functions
706
+ conn.makeUnaryRequest(path, d => Buffer.from(d), passThrough, data, resultCallback);
706
707
  });
707
708
  };
708
709
 
@@ -794,7 +795,6 @@ ts-protoc --ts_proto_out=./output -I=./protos ./protoc/*.proto
794
795
 
795
796
  # Todo
796
797
 
797
- - Support the string-based encoding of duration in `fromJSON`/`toJSON`
798
798
  - Make `oneof=unions-value` the default behavior in 2.0
799
799
  - Probably change `forceLong` default in 2.0, should default to `forceLong=long`
800
800
  - Make `esModuleInterop=true` the default in 2.0
package/build/src/main.js CHANGED
@@ -1737,6 +1737,19 @@ function generateExtension(ctx, message, extension) {
1737
1737
  function generateFromJson(ctx, fullName, fullTypeName, messageDesc) {
1738
1738
  const { options, utils, currentFile } = ctx;
1739
1739
  const chunks = [];
1740
+ // Handle Duration string format as a special case
1741
+ if (fullTypeName === "google.protobuf.Duration" && ctx.options.useDuration === options_1.DurationOption.STRING) {
1742
+ return (0, ts_poet_1.code) `
1743
+ fromJSON(object: string): ${fullName} {
1744
+ const match = object.match(/^(-?[0-9.]+)s$/);
1745
+ if (!match) throw new Error('Invalid duration string');
1746
+ const seconds = Number(match[1]);
1747
+ const wholeSeconds = Math.trunc(seconds);
1748
+ const nanos = Math.round((seconds - wholeSeconds) * 1e9);
1749
+ return { seconds: wholeSeconds, nanos };
1750
+ }
1751
+ `;
1752
+ }
1740
1753
  // create the basic function declaration
1741
1754
  chunks.push((0, ts_poet_1.code) `
1742
1755
  fromJSON(${messageDesc.field.length > 0 ? "object" : "_"}: any): ${fullName} {
@@ -1984,7 +1997,7 @@ function generateFromJson(ctx, fullName, fullTypeName, messageDesc) {
1984
1997
  chunks.push((0, ts_poet_1.code) `}`);
1985
1998
  return (0, ts_poet_1.joinCode)(chunks, { on: "\n" });
1986
1999
  }
1987
- function generateCanonicalToJson(fullName, fullProtobufTypeName, { useOptionals, useNullAsOptional }) {
2000
+ function generateCanonicalToJson(fullName, fullProtobufTypeName, { useOptionals, useNullAsOptional, useDuration }) {
1988
2001
  if ((0, types_1.isFieldMaskTypeName)(fullProtobufTypeName)) {
1989
2002
  const returnType = useOptionals === "all" ? `string | ${(0, utils_1.nullOrUndefined)({ useNullAsOptional })}` : "string";
1990
2003
  const pathModifier = useOptionals === "all" ? "?" : "";
@@ -1993,6 +2006,15 @@ function generateCanonicalToJson(fullName, fullProtobufTypeName, { useOptionals,
1993
2006
  return message.paths${pathModifier}.join(',');
1994
2007
  }
1995
2008
  `;
2009
+ }
2010
+ if (fullProtobufTypeName === "google.protobuf.Duration" && useDuration === options_1.DurationOption.STRING) {
2011
+ return (0, ts_poet_1.code) `
2012
+ toJSON(message: ${fullName}): string {
2013
+ const secs = Number(message.seconds);
2014
+ const nanos = message.nanos;
2015
+ return \`\${secs + nanos/1e9}s\`;
2016
+ }
2017
+ `;
1996
2018
  }
1997
2019
  return undefined;
1998
2020
  }
@@ -15,6 +15,10 @@ export declare enum JsonTimestampOption {
15
15
  RFC3339 = "rfc3339",
16
16
  RAW = "raw"
17
17
  }
18
+ export declare enum DurationOption {
19
+ DURATION = "duration",
20
+ STRING = "string"
21
+ }
18
22
  export declare enum EnvOption {
19
23
  NODE = "node",
20
24
  BROWSER = "browser",
@@ -108,6 +112,7 @@ export type Options = {
108
112
  bigIntLiteral: boolean;
109
113
  typePrefix: string;
110
114
  typeSuffix: string;
115
+ useDuration: DurationOption;
111
116
  };
112
117
  export declare function defaultOptions(): Options;
113
118
  export declare function optionsFromParameter(parameter: string | undefined): Options;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OutputSchemaOption = exports.ServiceOption = exports.OneofOption = exports.EnvOption = exports.JsonTimestampOption = exports.DateOption = exports.LongOption = void 0;
3
+ exports.OutputSchemaOption = exports.ServiceOption = exports.OneofOption = exports.EnvOption = exports.DurationOption = exports.JsonTimestampOption = exports.DateOption = exports.LongOption = void 0;
4
4
  exports.defaultOptions = defaultOptions;
5
5
  exports.optionsFromParameter = optionsFromParameter;
6
6
  exports.getTsPoetOpts = getTsPoetOpts;
@@ -24,6 +24,11 @@ var JsonTimestampOption;
24
24
  JsonTimestampOption["RFC3339"] = "rfc3339";
25
25
  JsonTimestampOption["RAW"] = "raw";
26
26
  })(JsonTimestampOption || (exports.JsonTimestampOption = JsonTimestampOption = {}));
27
+ var DurationOption;
28
+ (function (DurationOption) {
29
+ DurationOption["DURATION"] = "duration";
30
+ DurationOption["STRING"] = "string";
31
+ })(DurationOption || (exports.DurationOption = DurationOption = {}));
27
32
  var EnvOption;
28
33
  (function (EnvOption) {
29
34
  EnvOption["NODE"] = "node";
@@ -120,6 +125,7 @@ function defaultOptions() {
120
125
  bigIntLiteral: true,
121
126
  typePrefix: "",
122
127
  typeSuffix: "",
128
+ useDuration: DurationOption.DURATION,
123
129
  };
124
130
  }
125
131
  const nestJsOptions = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "2.6.0",
3
+ "version": "2.7.0",
4
4
  "description": "",
5
5
  "main": "build/src/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",