ts-proto 1.137.0 → 1.137.2

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
@@ -5,8 +5,6 @@
5
5
 
6
6
  > `ts-proto` transforms your `.proto` files into strongly-typed, idiomatic TypeScript files!
7
7
 
8
- (Note, if you're a new user of ts-proto and using a modern TS setup with `esModuleInterop`, or want to use ts-proto in ESM / snowpack / vite, you need to also pass that as a `ts_proto_opt`.)
9
-
10
8
  ## Table of contents
11
9
 
12
10
  - [ts-proto](#ts-proto)
@@ -14,6 +12,7 @@
14
12
  - [Overview](#overview)
15
13
  - [QuickStart](#quickstart)
16
14
  - [Buf](#buf)
15
+ - [ESM](#esm)
17
16
  - [Goals](#goals)
18
17
  - [Example Types](#example-types)
19
18
  - [Highlights](#highlights)
@@ -118,6 +117,13 @@ plugins:
118
117
  - useExactTypes=...
119
118
  ```
120
119
 
120
+ ## ESM
121
+
122
+ If you're using a modern TS setup with either `esModuleInterop` or running in an ESM environment, you'll need to pass `ts_proto_opt`s of:
123
+
124
+ * `esModuleInterop=true` if using `esModuleInterop` in your `tsconfig.json`, and
125
+ * `importSuffix=.js` if executing the generated ts-proto code in an ESM environment
126
+
121
127
  # Goals
122
128
 
123
129
  In terms of the code that `ts-proto` generates, the general goals are:
@@ -283,7 +289,9 @@ Generated code will be placed in the Gradle build directory.
283
289
 
284
290
  - With `--ts_proto_opt=forceLong=long`, all 64-bit numbers will be parsed as instances of `Long` (using the [long](https://www.npmjs.com/package/long) library).
285
291
 
286
- Alternatively, if you pass `--ts_proto_opt=forceLong=string`, all 64-bit numbers will be outputted as strings.
292
+ With `--ts_proto_opt=forceLong=string`, all 64-bit numbers will be output as strings.
293
+
294
+ With `--ts_proto_opt=forceLong=bigint`, all 64-bit numbers will be output as `BigInt`s. This option still uses the `long` library to encode/decode internally within `protobuf.js`, but then converts to/from `BigInt`s in the ts-proto-generated code.
287
295
 
288
296
  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`.
289
297
 
@@ -567,7 +575,7 @@ The commands below assume you have **Docker** installed. To use a **local** copy
567
575
 
568
576
  - Check out the [repository]() for the latest code.
569
577
  - Run `yarn install` to install the dependencies.
570
- - Run `yarn build:test` or `yarn build:test:local` to generate the test files.
578
+ - Run `yarn build:test` to generate the test files.
571
579
  > _This runs the following commands:_
572
580
  >
573
581
  > - `proto2bin` — Converts integration test `.proto` files to `.bin`.
@@ -577,25 +585,24 @@ The commands below assume you have **Docker** installed. To use a **local** copy
577
585
 
578
586
  **Workflow**
579
587
 
580
- - Modifying the plugin implementation:
581
- - The most important logic is found in [src/main.ts](src/main.ts).
582
- - Run `yarn bin2ts` or `yarn bin2ts:local`.
583
- _Since the proto files were not changed, you only need to regenerate the typescript files._
584
- - Run `yarn test` to verify the typescript files are compatible with the reference implementation, and pass other tests.
585
- - Updating or adding `.proto` files in the integration directory:
586
- - Run `yarn watch` to automatically regenerate test files when proto files change.
587
- - Or run `yarn build:test` to regenerate all integration test files.
588
- - Run `yarn test` to retest.
589
-
590
- **Contributing**
591
-
592
- - Run `yarn build:test` and `yarn test` to make sure everything works.
593
- - Run `yarn format` to format the typescript files.
594
- - Commit the changes:
595
- - Also include the generated `.bin` files for the tests where you added or modified `.proto` files.
596
- > These are checked into git so that the test suite can run without having to invoke the `protoc` build chain.
597
- - Also include the generated `.ts` files.
598
- - Create a pull request
588
+ - Add/update an integration test for your use case
589
+ - Either find an existing `integration/*` test that is close enough to your use case, e.g. has a `parameters.txt` that matches the `ts_proto_opt` params necessary to reproduce your use case
590
+ - If creating a new integration test:
591
+ - Make a new `integration/your-new-test/parameters.txt` with the necessary `ts_proto_opt` params
592
+ - Create a minimal `integration/your-new-test/your-new-test.proto` schema to reproduce your use case
593
+ - After any changes to `your-new-test.proto`, or an existing `integration/*.proto` file, run `yarn proto2bin`
594
+ - You can also leave `yarn watch` running, and it should "just do the right thing"
595
+ - Add/update a `integration/your-new-test/some-test.ts` unit test, even if it's as trivial as just making sure the generated code compiles
596
+ - Modify the `ts-proto` code generation logic:
597
+ - Most important logic is found in [src/main.ts](src/main.ts).
598
+ - After any changes to `src/*.ts` files, run `yarn bin2ts` to re-codegen all integration tests
599
+ - Or `yarn bin2ts your-new-test` to re-codegen a specific test
600
+ - Again leaving `yarn watch` running should "just do the right thing"
601
+ - Run `yarn test` to verify your changes pass all existing tests
602
+ - Commit and submit a PR
603
+ - Run `yarn format` to format the typescript files.
604
+ - Make sure to `git add` all of the `*.proto`, `*.bin`, and `*.ts` files in `integration/your-new-test`
605
+ - Sometimes checking in generated code is frowned upon, but given ts-proto's main job is to generate code, seeing the codegen diffs in PRs is helpful
599
606
 
600
607
  **Dockerized Protoc**
601
608
 
package/build/encode.js CHANGED
@@ -12,7 +12,11 @@ function generateEncoder(ctx, typeName) {
12
12
  }
13
13
  if (name == "Timestamp") {
14
14
  const TimestampValue = (0, utils_1.impProto)(ctx.options, "google/protobuf/timestamp", name);
15
- return (0, ts_poet_1.code) `${TimestampValue}.encode(${ctx.utils.toTimestamp}(value)).finish()`;
15
+ let value = (0, ts_poet_1.code) `value`;
16
+ if (ctx.options.useDate === options_1.DateOption.DATE || ctx.options.useDate === options_1.DateOption.STRING) {
17
+ value = (0, ts_poet_1.code) `${ctx.utils.toTimestamp}(${value})`;
18
+ }
19
+ return (0, ts_poet_1.code) `${TimestampValue}.encode(${value}).finish()`;
16
20
  }
17
21
  if (name == "Struct") {
18
22
  const StructType = (0, utils_1.impProto)(ctx.options, "google/protobuf/struct", name);
@@ -54,7 +58,11 @@ function generateDecoder(ctx, typeName) {
54
58
  let TypeValue;
55
59
  if (name == "Timestamp") {
56
60
  TypeValue = (0, utils_1.impProto)(ctx.options, "google/protobuf/timestamp", name);
57
- return (0, ts_poet_1.code) `${TypeValue}.decode(value)`;
61
+ const decoder = (0, ts_poet_1.code) `${TypeValue}.decode(value)`;
62
+ if (ctx.options.useDate === options_1.DateOption.DATE || ctx.options.useDate === options_1.DateOption.STRING) {
63
+ return (0, ts_poet_1.code) `${ctx.utils.fromTimestamp}(${decoder})`;
64
+ }
65
+ return decoder;
58
66
  }
59
67
  if (name == "Struct" || name == "ListValue") {
60
68
  TypeValue = (0, utils_1.impProto)(ctx.options, "google/protobuf/struct", name);
package/build/main.js CHANGED
@@ -904,10 +904,11 @@ function generateEncode(ctx, fullName, messageDesc) {
904
904
  else {
905
905
  // Ideally we'd reuse `writeSnippet` but it has tagging embedded inside of it.
906
906
  const tag = ((field.number << 3) | 2) >>> 0;
907
+ const rhs = (x) => ((0, types_1.isLong)(field) && options.forceLong === options_1.LongOption.BIGINT ? `${x}.toString()` : x);
907
908
  const listWriteSnippet = (0, ts_poet_1.code) `
908
909
  writer.uint32(${tag}).fork();
909
910
  for (const v of message.${fieldName}) {
910
- writer.${(0, types_1.toReaderCall)(field)}(v);
911
+ writer.${(0, types_1.toReaderCall)(field)}(${rhs("v")});
911
912
  }
912
913
  writer.ldelim();
913
914
  `;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.137.0",
3
+ "version": "1.137.2",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",