ts-proto 1.116.1 → 1.118.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 CHANGED
@@ -1,3 +1,24 @@
1
+ # [1.118.0](https://github.com/stephenh/ts-proto/compare/v1.117.1...v1.118.0) (2022-07-19)
2
+
3
+
4
+ ### Features
5
+
6
+ * add dynamic upStreamCodes option ([#618](https://github.com/stephenh/ts-proto/issues/618)) ([3091023](https://github.com/stephenh/ts-proto/commit/309102313273e09aef3a8480f4b46360ad82adaa))
7
+
8
+ ## [1.117.1](https://github.com/stephenh/ts-proto/compare/v1.117.0...v1.117.1) (2022-07-16)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * import protobufjs/minimal with importSuffix ([#616](https://github.com/stephenh/ts-proto/issues/616)) ([b86291c](https://github.com/stephenh/ts-proto/commit/b86291c58b1cb9d4064944a6371e928b564aef7f))
14
+
15
+ # [1.117.0](https://github.com/stephenh/ts-proto/compare/v1.116.1...v1.117.0) (2022-07-05)
16
+
17
+
18
+ ### Features
19
+
20
+ * add importSuffix option and remove default .js suffix ([#612](https://github.com/stephenh/ts-proto/issues/612)) ([63a8895](https://github.com/stephenh/ts-proto/commit/63a8895f5a3a38fa3d5c0868f44e950e137fb697))
21
+
1
22
  ## [1.116.1](https://github.com/stephenh/ts-proto/compare/v1.116.0...v1.116.1) (2022-07-02)
2
23
 
3
24
 
package/README.markdown CHANGED
@@ -110,6 +110,8 @@ plugins:
110
110
 
111
111
  # Goals
112
112
 
113
+ In terms of the code that `ts-proto` generates, the general goals are:
114
+
113
115
  - Idiomatic TypeScript/ES6 types
114
116
  - `ts-proto` is a clean break from either the built-in Google/Java-esque JS code of `protoc` or the "make `.d.ts` files the `*.js` comments" approach of `protobufjs`
115
117
  - (Techically the `protobufjs/minimal` package is used for actually reading/writing bytes.)
@@ -118,6 +120,17 @@ plugins:
118
120
  - As much as possible, types are just interfaces, so you can work with messages just like regular hashes/data structures.
119
121
  - Only supports codegen `*.proto`-to-`*.ts` workflow, currently no runtime reflection/loading of dynamic `.proto` files
120
122
 
123
+ ## Non-Goals
124
+
125
+ Note that ts-proto is not an out-of-the-box RPC framework; instead it's more of a swiss-army knife (as witnessed by its many config options), that lets you build _exactly_ the RPC framework you'd like on top of it (i.e. that best integrates with your company's protobuf ecosystem; for better or worse, protobuf RPC is still a somewhat fragmented ecosystem).
126
+
127
+ If you'd like an out-of-the-box RPC framework built on top of ts-proto, there are a few examples:
128
+
129
+ - [nice-grpc](https://github.com/deeplay-io/nice-grpc)
130
+ - [starpc](https://github.com/aperturerobotics/starpc)
131
+
132
+ (Note for potential contributors, if you develop other frameworks/mini-frameworks, or even blog posts/tutorials, on using `ts-proto`, we're happy to link to them.)
133
+
121
134
  # Example Types
122
135
 
123
136
  The generated types are "just data", i.e.:
@@ -388,6 +401,8 @@ Generated code will be placed in the Gradle build directory.
388
401
 
389
402
  - With `--ts_proto_opt=fileSuffix=<SUFFIX>`, ts-proto will emit generated files using the specified suffix. A `helloworld.proto` file with `fileSuffix=.pb` would be generated as `helloworld.pb.ts`. This is common behavior in other protoc plugins and provides a way to quickly glob all the generated files.
390
403
 
404
+ - With `--ts_proto_opt=importSuffix=<SUFFIX>`, ts-proto will emit file imports using the specified suffix. An import of `helloworld.ts` with `fileSuffix=.js` would generate `import "helloworld.js"`. The default is to import without a file extension. Supported by TypeScript 4.7.x and up.
405
+
391
406
  - With `--ts_proto_opt=enumsAsLiterals=true`, the generated enum types will be enum-ish object with `as const`.
392
407
 
393
408
  - With `--ts_proto_opt=useExactTypes=false`, the generated `fromPartial` method will not use Exact types.
@@ -481,6 +496,8 @@ If you need ts-proto customizations or priority support for your company, you ca
481
496
 
482
497
  # Development
483
498
 
499
+ This section describes how to contribute directly to ts-proto, i.e. it's not required for running `ts-proto` in `protoc` or using the generated TypeScript.
500
+
484
501
  **Requirements**
485
502
 
486
503
  - [Docker](https://www.docker.com) or [protoc](https://github.com/protocolbuffers/protobuf/releases) v3.19.1
@@ -170,6 +170,7 @@ function generateGrpcWebImpl(ctx, returnObservable, hasStreamingMethods) {
170
170
  ${hasStreamingMethods ? 'streamingTransport?: grpc.TransportFactory,' : ``}
171
171
  debug?: boolean,
172
172
  metadata?: grpc.Metadata,
173
+ upStreamRetryCodes?: number[],
173
174
  }
174
175
  `;
175
176
  const chunks = [];
@@ -268,8 +269,7 @@ function createInvokeMethod(ctx) {
268
269
  _request: any,
269
270
  metadata: grpc.Metadata | undefined
270
271
  ): ${(0, types_1.observableType)(ctx)}<any> {
271
- // Status Response Codes (https://developers.google.com/maps-booking/reference/grpc-api/status_codes)
272
- const upStreamCodes = [2, 4, 8, 9, 10, 13, 14, 15];
272
+ const upStreamCodes = this.options.upStreamRetryCodes || [];
273
273
  const DEFAULT_TIMEOUT_TIME: number = 3_000;
274
274
  const request = { ..._request, ...methodDesc.requestType };
275
275
  const maybeCombinedMetadata =
@@ -8,7 +8,6 @@ const sourceInfo_1 = require("./sourceInfo");
8
8
  const main_1 = require("./main");
9
9
  const hash = (0, ts_poet_1.imp)('hash*object-hash');
10
10
  const dataloader = (0, ts_poet_1.imp)('DataLoader*dataloader');
11
- const Reader = (0, ts_poet_1.imp)('Reader@protobufjs/minimal');
12
11
  /**
13
12
  * Generates an interface for `serviceDesc`.
14
13
  *
@@ -76,7 +75,7 @@ exports.generateService = generateService;
76
75
  function generateRegularRpcMethod(ctx, fileDesc, serviceDesc, methodDesc) {
77
76
  (0, utils_1.assertInstanceOf)(methodDesc, utils_1.FormattedMethodDescriptor);
78
77
  const { options, utils } = ctx;
79
- const Reader = (0, ts_poet_1.imp)('Reader@protobufjs/minimal');
78
+ const Reader = (0, utils_1.impFile)(ctx.options, 'Reader@protobufjs/minimal');
80
79
  const rawInputType = (0, types_1.rawRequestType)(ctx, methodDesc);
81
80
  const inputType = (0, types_1.requestType)(ctx, methodDesc);
82
81
  const outputType = (0, types_1.responseType)(ctx, methodDesc);
@@ -223,6 +222,7 @@ function generateCachingRpcMethod(ctx, fileDesc, serviceDesc, methodDesc) {
223
222
  const inputType = (0, types_1.requestType)(ctx, methodDesc);
224
223
  const outputType = (0, types_1.responseType)(ctx, methodDesc);
225
224
  const uniqueIdentifier = `${(0, utils_1.maybePrefixPackage)(fileDesc, serviceDesc.name)}.${methodDesc.name}`;
225
+ const Reader = (0, utils_1.impFile)(ctx.options, 'Reader@protobufjs/minimal');
226
226
  const lambda = (0, ts_poet_1.code) `
227
227
  (requests) => {
228
228
  const responses = requests.map(async request => {
@@ -2,8 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateTypeRegistry = void 0;
4
4
  const ts_poet_1 = require("ts-poet");
5
- const Writer = (0, ts_poet_1.imp)('Writer@protobufjs/minimal');
6
- const Reader = (0, ts_poet_1.imp)('Reader@protobufjs/minimal');
5
+ const utils_1 = require("./utils");
7
6
  function generateTypeRegistry(ctx) {
8
7
  const chunks = [];
9
8
  chunks.push(generateMessageType(ctx));
@@ -22,6 +21,8 @@ function generateMessageType(ctx) {
22
21
  chunks.push((0, ts_poet_1.code) `export interface MessageType<Message extends UnknownMessage = UnknownMessage> {`);
23
22
  chunks.push((0, ts_poet_1.code) `$type: Message['$type'];`);
24
23
  if (ctx.options.outputEncodeMethods) {
24
+ const Writer = (0, utils_1.impFile)(ctx.options, 'Writer@protobufjs/minimal');
25
+ const Reader = (0, utils_1.impFile)(ctx.options, 'Reader@protobufjs/minimal');
25
26
  chunks.push((0, ts_poet_1.code) `encode(message: Message, writer?: ${Writer}): ${Writer};`);
26
27
  chunks.push((0, ts_poet_1.code) `decode(input: ${Reader} | Uint8Array, length?: number): Message;`);
27
28
  }
package/build/main.js CHANGED
@@ -108,7 +108,7 @@ function generateFile(ctx, fileDesc) {
108
108
  };
109
109
  `);
110
110
  if (options.outputTypeRegistry) {
111
- const messageTypeRegistry = (0, ts_poet_1.imp)('messageTypeRegistry@./typeRegistry');
111
+ const messageTypeRegistry = (0, utils_1.impFile)(options, 'messageTypeRegistry@./typeRegistry');
112
112
  chunks.push((0, ts_poet_1.code) `
113
113
  ${messageTypeRegistry}.set(${fullName}.$type, ${fullName});
114
114
  `);
@@ -227,8 +227,8 @@ exports.makeUtils = makeUtils;
227
227
  function makeLongUtils(options, bytes) {
228
228
  // Regardless of which `forceLong` config option we're using, we always use
229
229
  // the `long` library to either represent or at least sanity-check 64-bit values
230
- const util = (0, ts_poet_1.imp)('util@protobufjs/minimal');
231
- const configure = (0, ts_poet_1.imp)('configure@protobufjs/minimal');
230
+ const util = (0, utils_1.impFile)(options, 'util@protobufjs/minimal');
231
+ const configure = (0, utils_1.impFile)(options, 'configure@protobufjs/minimal');
232
232
  // Before esModuleInterop, we had to use 'import * as Long from long` b/c long is
233
233
  // an `export =` module and exports only the Long constructor (which is callable).
234
234
  // See https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require.
@@ -572,6 +572,7 @@ function generateDecode(ctx, fullName, messageDesc) {
572
572
  if (options.usePrototypeForDefaults) {
573
573
  createBase = (0, ts_poet_1.code) `Object.create(${createBase}) as ${fullName}`;
574
574
  }
575
+ const Reader = (0, utils_1.impFile)(ctx.options, 'Reader@protobufjs/minimal');
575
576
  // create the basic function declaration
576
577
  chunks.push((0, ts_poet_1.code) `
577
578
  decode(
@@ -712,12 +713,11 @@ function generateDecode(ctx, fullName, messageDesc) {
712
713
  chunks.push((0, ts_poet_1.code) `}`);
713
714
  return (0, ts_poet_1.joinCode)(chunks, { on: '\n' });
714
715
  }
715
- const Writer = (0, ts_poet_1.imp)('Writer@protobufjs/minimal');
716
- const Reader = (0, ts_poet_1.imp)('Reader@protobufjs/minimal');
717
716
  /** Creates a function to encode a message by loop overing the tags. */
718
717
  function generateEncode(ctx, fullName, messageDesc) {
719
718
  const { options, utils, typeMap } = ctx;
720
719
  const chunks = [];
720
+ const Writer = (0, utils_1.impFile)(ctx.options, 'Writer@protobufjs/minimal');
721
721
  // create the basic function declaration
722
722
  chunks.push((0, ts_poet_1.code) `
723
723
  encode(
package/build/options.js CHANGED
@@ -43,6 +43,7 @@ function defaultOptions() {
43
43
  oneof: OneofOption.PROPERTIES,
44
44
  esModuleInterop: false,
45
45
  fileSuffix: '',
46
+ importSuffix: '',
46
47
  lowerCaseServiceMethods: false,
47
48
  outputEncodeMethods: true,
48
49
  outputJsonMethods: true,
@@ -163,7 +164,8 @@ function parseParameter(parameter) {
163
164
  });
164
165
  return options;
165
166
  }
166
- function getTsPoetOpts(options) {
167
- return { forceModuleImport: ['protobufjs/minimal'] };
167
+ function getTsPoetOpts(_options) {
168
+ const imports = ['protobufjs/minimal' + _options.importSuffix];
169
+ return _options.esModuleInterop ? { forceDefaultImport: imports } : { forceModuleImport: imports };
168
170
  }
169
171
  exports.getTsPoetOpts = getTsPoetOpts;
package/build/schema.js CHANGED
@@ -68,7 +68,7 @@ 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, ts_poet_1.imp)(`protoMetadata@./${dep.replace('.proto', '')}`)}`;
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);
package/build/utils.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.impProto = exports.getPropertyAccessor = exports.getFieldJsonName = exports.FormattedMethodDescriptor = exports.assertInstanceOf = exports.maybePrefixPackage = exports.prefixDisableLinter = exports.maybeAddComment = exports.upperFirst = exports.lowerFirst = exports.singular = exports.fail = exports.readToBuffer = exports.protoFilesToGenerate = void 0;
3
+ exports.impProto = exports.impFile = exports.getPropertyAccessor = exports.getFieldJsonName = exports.FormattedMethodDescriptor = exports.assertInstanceOf = exports.maybePrefixPackage = exports.prefixDisableLinter = exports.maybeAddComment = exports.upperFirst = exports.lowerFirst = exports.singular = exports.fail = exports.readToBuffer = exports.protoFilesToGenerate = void 0;
4
4
  const ts_poet_1 = require("ts-poet");
5
5
  const options_1 = require("./options");
6
6
  const case_1 = require("./case");
@@ -169,9 +169,12 @@ function getPropertyAccessor(objectName, propertyName, optional = false) {
169
169
  : `${objectName}${optional ? '?.' : ''}[${JSON.stringify(propertyName)}]`;
170
170
  }
171
171
  exports.getPropertyAccessor = getPropertyAccessor;
172
+ function impFile(options, spec) {
173
+ return (0, ts_poet_1.imp)(`${spec}${options.importSuffix}`);
174
+ }
175
+ exports.impFile = impFile;
172
176
  function impProto(options, module, type) {
173
- // NOTE: TypeScript resolves importing foo.js to importing foo.ts.
174
- const importString = `${type}@./${module}${options.fileSuffix}.js`;
177
+ const importString = `${type}@./${module}${options.fileSuffix}${options.importSuffix}`;
175
178
  if (options.onlyTypes) {
176
179
  return (0, ts_poet_1.imp)('t:' + importString);
177
180
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.116.1",
3
+ "version": "1.118.0",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",
@@ -68,6 +68,6 @@
68
68
  "object-hash": "^1.3.1",
69
69
  "protobufjs": "^6.11.3",
70
70
  "ts-poet": "^4.15.0",
71
- "ts-proto-descriptors": "1.7.0"
71
+ "ts-proto-descriptors": "1.7.1"
72
72
  }
73
73
  }