ts-proto 1.158.1 → 1.159.1

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
@@ -289,6 +289,10 @@ Generated code will be placed in the Gradle build directory.
289
289
 
290
290
  ### Supported options
291
291
 
292
+ - With `--ts_proto_opt=globalThisPolyfill=true`, ts-proto will include a polyfill for globalThis.
293
+
294
+ Defaults to `false`, i.e. we assume `globalThis` is available.
295
+
292
296
  - With `--ts_proto_opt=context=true`, the services will have a Go-style `ctx` parameter, which is useful for tracing/logging/etc. if you're not using node's `async_hooks` api due to performance reasons.
293
297
 
294
298
  - 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).
package/build/main.js CHANGED
@@ -273,7 +273,7 @@ function generateFile(ctx, fileDesc) {
273
273
  exports.generateFile = generateFile;
274
274
  /** These are runtime utility methods used by the generated code. */
275
275
  function makeUtils(options) {
276
- const bytes = makeByteUtils();
276
+ const bytes = makeByteUtils(options);
277
277
  const longs = makeLongUtils(options, bytes);
278
278
  return {
279
279
  ...bytes,
@@ -354,12 +354,12 @@ function makeLongUtils(options, bytes) {
354
354
  `);
355
355
  return { numberToLong, longToNumber, longToString, longToBigint, Long };
356
356
  }
357
- function makeByteUtils() {
358
- const globalThis = (0, ts_poet_1.conditionalOutput)("tsProtoGlobalThis", (0, ts_poet_1.code) `
357
+ function makeByteUtils(options) {
358
+ const globalThisPolyfill = (0, ts_poet_1.conditionalOutput)("gt", (0, ts_poet_1.code) `
359
359
  declare const self: any | undefined;
360
360
  declare const window: any | undefined;
361
361
  declare const global: any | undefined;
362
- const tsProtoGlobalThis: any = (() => {
362
+ const gt: any = (() => {
363
363
  if (typeof globalThis !== "undefined") return globalThis;
364
364
  if (typeof self !== "undefined") return self;
365
365
  if (typeof window !== "undefined") return window;
@@ -367,6 +367,7 @@ function makeByteUtils() {
367
367
  throw "Unable to locate global object";
368
368
  })();
369
369
  `);
370
+ const globalThis = options.globalThisPolyfill ? globalThisPolyfill : (0, ts_poet_1.conditionalOutput)("globalThis", (0, ts_poet_1.code) ``);
370
371
  const bytesFromBase64 = (0, ts_poet_1.conditionalOutput)("bytesFromBase64", (0, ts_poet_1.code) `
371
372
  function bytesFromBase64(b64: string): Uint8Array {
372
373
  if (${globalThis}.Buffer) {
@@ -1994,6 +1995,9 @@ function convertFromObjectKey(ctx, messageDesc, field, variableName) {
1994
1995
  return (0, ts_poet_1.code) `${ctx.utils.globalThis}.Number(${variableName})`;
1995
1996
  }
1996
1997
  }
1998
+ else if (keyField.type === ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_BOOL) {
1999
+ return (0, ts_poet_1.code) `${ctx.utils.globalThis}.Boolean(${variableName})`;
2000
+ }
1997
2001
  else {
1998
2002
  return (0, ts_poet_1.code) `${ctx.utils.globalThis}.Number(${variableName})`;
1999
2003
  }
@@ -2014,6 +2018,9 @@ function convertToObjectKey(ctx, messageDesc, field, variableName) {
2014
2018
  return (0, ts_poet_1.code) `${variableName}`;
2015
2019
  }
2016
2020
  }
2021
+ else if (keyField.type === ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_BOOL) {
2022
+ return (0, ts_poet_1.code) `${ctx.utils.globalThis}.String(${variableName})`;
2023
+ }
2017
2024
  else {
2018
2025
  return (0, ts_poet_1.code) `${variableName}`;
2019
2026
  }
package/build/options.js CHANGED
@@ -38,6 +38,7 @@ function defaultOptions() {
38
38
  context: false,
39
39
  snakeToCamel: ["json", "keys"],
40
40
  emitDefaultValues: [],
41
+ globalThisPolyfill: false,
41
42
  forceLong: LongOption.NUMBER,
42
43
  useOptionals: "none",
43
44
  useDate: DateOption.DATE,
package/build/types.js CHANGED
@@ -642,7 +642,8 @@ function shouldGenerateJSMapType(ctx, message, field) {
642
642
  if (!mapType) {
643
643
  return false;
644
644
  }
645
- return isLong(mapType.keyField) && ctx.options.forceLong === options_1.LongOption.LONG;
645
+ return (mapType.keyField.type === ts_proto_descriptors_1.FieldDescriptorProto_Type.TYPE_BOOL ||
646
+ (isLong(mapType.keyField) && ctx.options.forceLong === options_1.LongOption.LONG));
646
647
  }
647
648
  exports.shouldGenerateJSMapType = shouldGenerateJSMapType;
648
649
  function detectMapType(ctx, messageDesc, fieldDesc) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "1.158.1",
3
+ "version": "1.159.1",
4
4
  "description": "",
5
5
  "main": "build/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",