@bufbuild/protobuf 0.0.4 → 0.0.7

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.
Files changed (76) hide show
  1. package/dist/cjs/binary-encoding.js +2 -2
  2. package/dist/cjs/descriptor-registry.js +18 -60
  3. package/dist/cjs/descriptor-set.js +15 -37
  4. package/dist/cjs/google/protobuf/any_pb.js +11 -8
  5. package/dist/cjs/google/protobuf/api_pb.js +19 -18
  6. package/dist/cjs/google/protobuf/compiler/plugin_pb.js +23 -22
  7. package/dist/cjs/google/protobuf/descriptor_pb.js +149 -148
  8. package/dist/cjs/google/protobuf/duration_pb.js +14 -12
  9. package/dist/cjs/google/protobuf/empty_pb.js +7 -6
  10. package/dist/cjs/google/protobuf/field_mask_pb.js +8 -7
  11. package/dist/cjs/google/protobuf/source_context_pb.js +7 -6
  12. package/dist/cjs/google/protobuf/struct_pb.js +26 -22
  13. package/dist/cjs/google/protobuf/timestamp_pb.js +12 -10
  14. package/dist/cjs/google/protobuf/type_pb.js +37 -33
  15. package/dist/cjs/google/protobuf/wrappers_pb.js +69 -66
  16. package/dist/cjs/index.js +40 -4
  17. package/dist/cjs/message.js +2 -1
  18. package/dist/cjs/package.json +1 -0
  19. package/dist/cjs/private/binary-format-common.js +4 -3
  20. package/dist/cjs/private/binary-format-proto2.js +3 -6
  21. package/dist/cjs/private/binary-format-proto3.js +2 -5
  22. package/dist/cjs/private/enum.js +1 -1
  23. package/dist/cjs/private/json-format-common.js +6 -5
  24. package/dist/cjs/private/message-type.js +2 -1
  25. package/dist/cjs/proto-int64.js +1 -1
  26. package/dist/cjs/proto2.js +6 -8
  27. package/dist/cjs/proto3.js +9 -12
  28. package/dist/cjs/type-registry.js +58 -13
  29. package/dist/esm/binary-encoding.js +2 -2
  30. package/dist/esm/descriptor-registry.js +18 -60
  31. package/dist/esm/descriptor-set.js +15 -37
  32. package/dist/esm/google/protobuf/any_pb.js +6 -3
  33. package/dist/esm/google/protobuf/api_pb.js +2 -1
  34. package/dist/esm/google/protobuf/compiler/plugin_pb.js +2 -1
  35. package/dist/esm/google/protobuf/descriptor_pb.js +2 -1
  36. package/dist/esm/google/protobuf/duration_pb.js +4 -2
  37. package/dist/esm/google/protobuf/empty_pb.js +2 -1
  38. package/dist/esm/google/protobuf/field_mask_pb.js +2 -1
  39. package/dist/esm/google/protobuf/source_context_pb.js +2 -1
  40. package/dist/esm/google/protobuf/struct_pb.js +6 -2
  41. package/dist/esm/google/protobuf/timestamp_pb.js +3 -1
  42. package/dist/esm/google/protobuf/type_pb.js +5 -1
  43. package/dist/esm/google/protobuf/wrappers_pb.js +4 -1
  44. package/dist/esm/index.js +25 -4
  45. package/dist/esm/message.js +2 -1
  46. package/dist/esm/private/binary-format-common.js +4 -3
  47. package/dist/esm/private/binary-format-proto2.js +3 -6
  48. package/dist/esm/private/binary-format-proto3.js +2 -5
  49. package/dist/esm/private/enum.js +1 -1
  50. package/dist/esm/private/json-format-common.js +6 -5
  51. package/dist/esm/private/message-type.js +2 -1
  52. package/dist/esm/proto-int64.js +1 -1
  53. package/dist/esm/proto2.js +6 -8
  54. package/dist/esm/proto3.js +9 -12
  55. package/dist/esm/type-registry.js +58 -13
  56. package/dist/types/google/protobuf/any_pb.d.ts +6 -2
  57. package/dist/types/google/protobuf/api_pb.d.ts +5 -2
  58. package/dist/types/google/protobuf/compiler/plugin_pb.d.ts +5 -2
  59. package/dist/types/google/protobuf/descriptor_pb.d.ts +5 -2
  60. package/dist/types/google/protobuf/duration_pb.d.ts +5 -2
  61. package/dist/types/google/protobuf/empty_pb.d.ts +5 -2
  62. package/dist/types/google/protobuf/field_mask_pb.d.ts +5 -2
  63. package/dist/types/google/protobuf/source_context_pb.d.ts +5 -2
  64. package/dist/types/google/protobuf/struct_pb.d.ts +5 -2
  65. package/dist/types/google/protobuf/timestamp_pb.d.ts +5 -2
  66. package/dist/types/google/protobuf/type_pb.d.ts +5 -2
  67. package/dist/types/google/protobuf/wrappers_pb.d.ts +5 -2
  68. package/dist/types/index.d.ts +31 -2
  69. package/dist/types/type-registry.d.ts +35 -2
  70. package/package.json +3 -3
  71. package/dist/cjs/index-runtime.js +0 -42
  72. package/dist/cjs/index-wkt.js +0 -42
  73. package/dist/esm/index-runtime.js +0 -25
  74. package/dist/esm/index-wkt.js +0 -26
  75. package/dist/types/index-runtime.d.ts +0 -19
  76. package/dist/types/index-wkt.d.ts +0 -12
package/dist/cjs/index.js CHANGED
@@ -27,7 +27,43 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
27
27
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
28
28
  };
29
29
  Object.defineProperty(exports, "__esModule", { value: true });
30
- // To avoid circular imports when the wkt import from ./index.js, we
31
- // use a separate ./index-runtime.js
32
- __exportStar(require("./index-runtime.js"), exports);
33
- __exportStar(require("./index-wkt.js"), exports);
30
+ exports.BinaryReader = exports.BinaryWriter = exports.WireType = exports.DescriptorSet = exports.DescriptorRegistry = exports.TypeRegistry = exports.MethodIdempotency = exports.MethodKind = exports.ScalarType = exports.Message = exports.protoBase64 = exports.protoInt64 = exports.proto2 = exports.proto3 = void 0;
31
+ var proto3_js_1 = require("./proto3.js");
32
+ Object.defineProperty(exports, "proto3", { enumerable: true, get: function () { return proto3_js_1.proto3; } });
33
+ var proto2_js_1 = require("./proto2.js");
34
+ Object.defineProperty(exports, "proto2", { enumerable: true, get: function () { return proto2_js_1.proto2; } });
35
+ var proto_int64_js_1 = require("./proto-int64.js");
36
+ Object.defineProperty(exports, "protoInt64", { enumerable: true, get: function () { return proto_int64_js_1.protoInt64; } });
37
+ var proto_base64_js_1 = require("./proto-base64.js");
38
+ Object.defineProperty(exports, "protoBase64", { enumerable: true, get: function () { return proto_base64_js_1.protoBase64; } });
39
+ var message_js_1 = require("./message.js");
40
+ Object.defineProperty(exports, "Message", { enumerable: true, get: function () { return message_js_1.Message; } });
41
+ var field_js_1 = require("./field.js");
42
+ Object.defineProperty(exports, "ScalarType", { enumerable: true, get: function () { return field_js_1.ScalarType; } });
43
+ var service_type_js_1 = require("./service-type.js");
44
+ Object.defineProperty(exports, "MethodKind", { enumerable: true, get: function () { return service_type_js_1.MethodKind; } });
45
+ Object.defineProperty(exports, "MethodIdempotency", { enumerable: true, get: function () { return service_type_js_1.MethodIdempotency; } });
46
+ var type_registry_js_1 = require("./type-registry.js");
47
+ Object.defineProperty(exports, "TypeRegistry", { enumerable: true, get: function () { return type_registry_js_1.TypeRegistry; } });
48
+ var descriptor_registry_js_1 = require("./descriptor-registry.js");
49
+ Object.defineProperty(exports, "DescriptorRegistry", { enumerable: true, get: function () { return descriptor_registry_js_1.DescriptorRegistry; } });
50
+ var descriptor_set_js_1 = require("./descriptor-set.js");
51
+ Object.defineProperty(exports, "DescriptorSet", { enumerable: true, get: function () { return descriptor_set_js_1.DescriptorSet; } });
52
+ var binary_encoding_js_1 = require("./binary-encoding.js");
53
+ Object.defineProperty(exports, "WireType", { enumerable: true, get: function () { return binary_encoding_js_1.WireType; } });
54
+ Object.defineProperty(exports, "BinaryWriter", { enumerable: true, get: function () { return binary_encoding_js_1.BinaryWriter; } });
55
+ Object.defineProperty(exports, "BinaryReader", { enumerable: true, get: function () { return binary_encoding_js_1.BinaryReader; } });
56
+ var json_format_js_1 = require("./json-format.js");
57
+ // ideally, we would export these types with sub-path exports:
58
+ __exportStar(require("./google/protobuf/compiler/plugin_pb.js"), exports);
59
+ __exportStar(require("./google/protobuf/api_pb.js"), exports);
60
+ __exportStar(require("./google/protobuf/any_pb.js"), exports);
61
+ __exportStar(require("./google/protobuf/descriptor_pb.js"), exports);
62
+ __exportStar(require("./google/protobuf/duration_pb.js"), exports);
63
+ __exportStar(require("./google/protobuf/empty_pb.js"), exports);
64
+ __exportStar(require("./google/protobuf/field_mask_pb.js"), exports);
65
+ __exportStar(require("./google/protobuf/source_context_pb.js"), exports);
66
+ __exportStar(require("./google/protobuf/struct_pb.js"), exports);
67
+ __exportStar(require("./google/protobuf/timestamp_pb.js"), exports);
68
+ __exportStar(require("./google/protobuf/type_pb.js"), exports);
69
+ __exportStar(require("./google/protobuf/wrappers_pb.js"), exports);
@@ -84,8 +84,9 @@ class Message {
84
84
  * Serialize the message to a JSON string.
85
85
  */
86
86
  toJsonString(options) {
87
+ var _a;
87
88
  const value = this.toJson(options);
88
- return JSON.stringify(value, null, options?.prettySpaces ?? 0);
89
+ return JSON.stringify(value, null, (_a = options === null || options === void 0 ? void 0 : options.prettySpaces) !== null && _a !== void 0 ? _a : 0);
89
90
  }
90
91
  /**
91
92
  * Retrieve the MessageType of this message - a singleton that represents
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
@@ -32,17 +32,18 @@ const writeDefaults = {
32
32
  writerFactory: () => new binary_encoding_js_1.BinaryWriter(),
33
33
  };
34
34
  function makeReadOptions(options) {
35
- return options ? { ...readDefaults, ...options } : readDefaults;
35
+ return options ? Object.assign(Object.assign({}, readDefaults), options) : readDefaults;
36
36
  }
37
37
  function makeWriteOptions(options) {
38
- return options ? { ...writeDefaults, ...options } : writeDefaults;
38
+ return options ? Object.assign(Object.assign({}, writeDefaults), options) : writeDefaults;
39
39
  }
40
40
  function makeBinaryFormatCommon() {
41
41
  return {
42
42
  makeReadOptions,
43
43
  makeWriteOptions,
44
44
  listUnknownFields(message) {
45
- return message[unknownFieldsSymbol] ?? [];
45
+ var _a;
46
+ return (_a = message[unknownFieldsSymbol]) !== null && _a !== void 0 ? _a : [];
46
47
  },
47
48
  discardUnknownFields(message) {
48
49
  delete message[unknownFieldsSymbol];
@@ -18,9 +18,7 @@ const field_js_1 = require("../field.js");
18
18
  const binary_format_common_js_1 = require("./binary-format-common.js");
19
19
  /* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unnecessary-condition, @typescript-eslint/strict-boolean-expressions, no-case-declarations, prefer-const */
20
20
  function makeBinaryFormatProto2() {
21
- return {
22
- ...(0, binary_format_common_js_1.makeBinaryFormatCommon)(),
23
- writeMessage(message, writer, options) {
21
+ return Object.assign(Object.assign({}, (0, binary_format_common_js_1.makeBinaryFormatCommon)()), { writeMessage(message, writer, options) {
24
22
  const type = message.getType();
25
23
  let field;
26
24
  try {
@@ -84,7 +82,7 @@ function makeBinaryFormatProto2() {
84
82
  }
85
83
  catch (e) {
86
84
  let m = field
87
- ? `cannot encode field ${type.typeName}.${field?.name} to binary`
85
+ ? `cannot encode field ${type.typeName}.${field === null || field === void 0 ? void 0 : field.name} to binary`
88
86
  : `cannot encode message ${type.typeName} to binary`;
89
87
  let r = e instanceof Error ? e.message : String(e);
90
88
  throw new Error(m + (r.length > 0 ? `: ${r}` : ""));
@@ -93,7 +91,6 @@ function makeBinaryFormatProto2() {
93
91
  this.writeUnknownFields(message, writer);
94
92
  }
95
93
  return writer;
96
- },
97
- };
94
+ } });
98
95
  }
99
96
  exports.makeBinaryFormatProto2 = makeBinaryFormatProto2;
@@ -18,9 +18,7 @@ const field_js_1 = require("../field.js");
18
18
  const binary_format_common_js_1 = require("./binary-format-common.js");
19
19
  /* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unnecessary-condition, @typescript-eslint/strict-boolean-expressions, prefer-const, no-case-declarations */
20
20
  function makeBinaryFormatProto3() {
21
- return {
22
- ...(0, binary_format_common_js_1.makeBinaryFormatCommon)(),
23
- writeMessage(message, writer, options) {
21
+ return Object.assign(Object.assign({}, (0, binary_format_common_js_1.makeBinaryFormatCommon)()), { writeMessage(message, writer, options) {
24
22
  const type = message.getType();
25
23
  for (const field of type.fields.byNumber()) {
26
24
  let value, // this will be our field value, whether it is member of a oneof or regular field
@@ -76,7 +74,6 @@ function makeBinaryFormatProto3() {
76
74
  this.writeUnknownFields(message, writer);
77
75
  }
78
76
  return writer;
79
- },
80
- };
77
+ } });
81
78
  }
82
79
  exports.makeBinaryFormatProto3 = makeBinaryFormatProto3;
@@ -82,7 +82,7 @@ exports.makeEnumType = makeEnumType;
82
82
  function makeEnum(typeName, values, opt) {
83
83
  const enumObject = {};
84
84
  for (const value of values) {
85
- const name = makeEnumValueName(value, opt?.sharedPrefix);
85
+ const name = makeEnumValueName(value, opt === null || opt === void 0 ? void 0 : opt.sharedPrefix);
86
86
  enumObject[name] = value.no;
87
87
  enumObject[value.no] = name;
88
88
  }
@@ -32,10 +32,10 @@ const jsonWriteDefaults = {
32
32
  prettySpaces: 0,
33
33
  };
34
34
  function makeReadOptions(options) {
35
- return options ? { ...jsonReadDefaults, ...options } : jsonReadDefaults;
35
+ return options ? Object.assign(Object.assign({}, jsonReadDefaults), options) : jsonReadDefaults;
36
36
  }
37
37
  function makeWriteOptions(options) {
38
- return options ? { ...jsonWriteDefaults, ...options } : jsonWriteDefaults;
38
+ return options ? Object.assign(Object.assign({}, jsonWriteDefaults), options) : jsonWriteDefaults;
39
39
  }
40
40
  function makeJsonFormatCommon(makeWriteField) {
41
41
  const writeField = makeWriteField(writeEnum, writeScalar);
@@ -46,7 +46,7 @@ function makeJsonFormatCommon(makeWriteField) {
46
46
  if (json == null || Array.isArray(json) || typeof json != "object") {
47
47
  throw new Error(`cannot decode message ${type.typeName} from JSON: ${this.debug(json)}`);
48
48
  }
49
- message = message ?? new type();
49
+ message = message !== null && message !== void 0 ? message : new type();
50
50
  const oneofSeen = {};
51
51
  for (const [jsonKey, jsonValue] of Object.entries(json)) {
52
52
  const field = type.fields.findJsonName(jsonKey);
@@ -386,13 +386,14 @@ function readEnum(type, json, ignoreUnknownFields) {
386
386
  case "string":
387
387
  const value = type.findName(json);
388
388
  if (value || ignoreUnknownFields) {
389
- return value?.no;
389
+ return value === null || value === void 0 ? void 0 : value.no;
390
390
  }
391
391
  break;
392
392
  }
393
393
  throw new Error(`cannot decode enum ${type.typeName} from JSON: ${debugJsonValue(json)}`);
394
394
  }
395
395
  function writeEnum(type, value, emitIntrinsicDefault, enumAsInteger) {
396
+ var _a;
396
397
  if (value === undefined) {
397
398
  return value;
398
399
  }
@@ -407,7 +408,7 @@ function writeEnum(type, value, emitIntrinsicDefault, enumAsInteger) {
407
408
  return null;
408
409
  }
409
410
  const val = type.findNumber(value);
410
- return val?.name ?? value; // if we don't know the enum value, just return the number
411
+ return (_a = val === null || val === void 0 ? void 0 : val.name) !== null && _a !== void 0 ? _a : value; // if we don't know the enum value, just return the number
411
412
  }
412
413
  function writeScalar(type, value, emitIntrinsicDefault) {
413
414
  if (value === undefined) {
@@ -19,7 +19,8 @@ const message_js_1 = require("../message.js");
19
19
  * Create a new message type using the given runtime.
20
20
  */
21
21
  function makeMessageType(runtime, typeName, fields, opt) {
22
- const localName = opt?.localName ?? typeName.substring(typeName.lastIndexOf(".") + 1);
22
+ var _a;
23
+ const localName = (_a = opt === null || opt === void 0 ? void 0 : opt.localName) !== null && _a !== void 0 ? _a : typeName.substring(typeName.lastIndexOf(".") + 1);
23
24
  const type = {
24
25
  [localName]: function (data) {
25
26
  runtime.util.initFields(this);
@@ -18,7 +18,7 @@ const varint_js_1 = require("./google/varint.js");
18
18
  function makeInt64Support() {
19
19
  const dv = new DataView(new ArrayBuffer(8));
20
20
  // note that Safari 14 implements BigInt, but not the DataView methods
21
- const ok = globalThis.BigInt !== undefined && // eslint-disable-line @typescript-eslint/no-unnecessary-condition -- conditional for BigInt is very much necessary
21
+ const ok = globalThis.BigInt !== undefined &&
22
22
  typeof dv.getBigInt64 === "function" &&
23
23
  typeof dv.getBigUint64 === "function" &&
24
24
  typeof dv.setBigInt64 === "function" &&
@@ -24,9 +24,7 @@ const json_format_proto2_js_1 = require("./private/json-format-proto2.js");
24
24
  /**
25
25
  * Provides functionality for messages defined with the proto2 syntax.
26
26
  */
27
- exports.proto2 = (0, proto_runtime_js_1.makeProtoRuntime)("proto2", (0, json_format_proto2_js_1.makeJsonFormatProto2)(), (0, binary_format_proto2_js_1.makeBinaryFormatProto2)(), {
28
- ...(0, util_common_js_1.makeUtilCommon)(),
29
- newFieldList(fields) {
27
+ exports.proto2 = (0, proto_runtime_js_1.makeProtoRuntime)("proto2", (0, json_format_proto2_js_1.makeJsonFormatProto2)(), (0, binary_format_proto2_js_1.makeBinaryFormatProto2)(), Object.assign(Object.assign({}, (0, util_common_js_1.makeUtilCommon)()), { newFieldList(fields) {
30
28
  return new field_list_js_1.InternalFieldList(fields, normalizeFieldInfosProto2);
31
29
  },
32
30
  initFields(target) {
@@ -55,10 +53,10 @@ exports.proto2 = (0, proto_runtime_js_1.makeProtoRuntime)("proto2", (0, json_for
55
53
  break;
56
54
  }
57
55
  }
58
- },
59
- });
56
+ } }));
60
57
  /* eslint-disable @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-argument */
61
58
  function normalizeFieldInfosProto2(fieldInfos) {
59
+ var _a, _b, _c;
62
60
  const r = [];
63
61
  let o;
64
62
  for (const field of typeof fieldInfos == "function"
@@ -66,10 +64,10 @@ function normalizeFieldInfosProto2(fieldInfos) {
66
64
  : fieldInfos) {
67
65
  const f = field;
68
66
  f.localName = (0, names_js_1.makeFieldName)(field.name, field.oneof !== undefined);
69
- f.jsonName = field.jsonName ?? (0, names_js_1.makeJsonName)(field.name);
70
- f.repeated = field.repeated ?? false;
67
+ f.jsonName = (_a = field.jsonName) !== null && _a !== void 0 ? _a : (0, names_js_1.makeJsonName)(field.name);
68
+ f.repeated = (_b = field.repeated) !== null && _b !== void 0 ? _b : false;
71
69
  // In contrast to proto3, repeated fields are unpacked except when explicitly specified.
72
- f.packed = field.packed ?? false;
70
+ f.packed = (_c = field.packed) !== null && _c !== void 0 ? _c : false;
73
71
  // We do not surface options at this time
74
72
  // f.options = field.options ?? emptyReadonlyObject;
75
73
  if (field.oneof !== undefined) {
@@ -26,9 +26,7 @@ const names_js_1 = require("./private/names.js");
26
26
  /**
27
27
  * Provides functionality for messages defined with the proto3 syntax.
28
28
  */
29
- exports.proto3 = (0, proto_runtime_js_1.makeProtoRuntime)("proto3", (0, json_format_proto3_js_1.makeJsonFormatProto3)(), (0, binary_format_proto3_js_1.makeBinaryFormatProto3)(), {
30
- ...(0, util_common_js_1.makeUtilCommon)(),
31
- newFieldList(fields) {
29
+ exports.proto3 = (0, proto_runtime_js_1.makeProtoRuntime)("proto3", (0, json_format_proto3_js_1.makeJsonFormatProto3)(), (0, binary_format_proto3_js_1.makeBinaryFormatProto3)(), Object.assign(Object.assign({}, (0, util_common_js_1.makeUtilCommon)()), { newFieldList(fields) {
32
30
  return new field_list_js_1.InternalFieldList(fields, normalizeFieldInfosProto3);
33
31
  },
34
32
  initFields(target) {
@@ -59,10 +57,10 @@ exports.proto3 = (0, proto_runtime_js_1.makeProtoRuntime)("proto3", (0, json_for
59
57
  break;
60
58
  }
61
59
  }
62
- },
63
- });
60
+ } }));
64
61
  /* eslint-disable @typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-argument */
65
62
  function normalizeFieldInfosProto3(fieldInfos) {
63
+ var _a, _b, _c;
66
64
  const r = [];
67
65
  let o;
68
66
  for (const field of typeof fieldInfos == "function"
@@ -70,19 +68,18 @@ function normalizeFieldInfosProto3(fieldInfos) {
70
68
  : fieldInfos) {
71
69
  const f = field;
72
70
  f.localName = (0, names_js_1.makeFieldName)(field.name, field.oneof !== undefined);
73
- f.jsonName = field.jsonName ?? (0, names_js_1.makeJsonName)(field.name);
74
- f.repeated = field.repeated ?? false;
71
+ f.jsonName = (_a = field.jsonName) !== null && _a !== void 0 ? _a : (0, names_js_1.makeJsonName)(field.name);
72
+ f.repeated = (_b = field.repeated) !== null && _b !== void 0 ? _b : false;
75
73
  // From the proto3 language guide:
76
74
  // > In proto3, repeated fields of scalar numeric types are packed by default.
77
75
  // This information is incomplete - according to the conformance tests, BOOL
78
76
  // and ENUM are packed by default as well. This means only STRING and BYTES
79
77
  // are not packed by default, which makes sense because they are length-delimited.
80
78
  f.packed =
81
- field.packed ??
82
- (field.kind == "enum" ||
83
- (field.kind == "scalar" &&
84
- field.T != field_js_1.ScalarType.BYTES &&
85
- field.T != field_js_1.ScalarType.STRING));
79
+ (_c = field.packed) !== null && _c !== void 0 ? _c : (field.kind == "enum" ||
80
+ (field.kind == "scalar" &&
81
+ field.T != field_js_1.ScalarType.BYTES &&
82
+ field.T != field_js_1.ScalarType.STRING));
86
83
  // We do not surface options at this time
87
84
  // f.options = field.options ?? emptyReadonlyObject;
88
85
  if (field.oneof !== undefined) {
@@ -15,7 +15,7 @@
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.TypeRegistry = void 0;
17
17
  /**
18
- * TypeRegistry is a basic type registry
18
+ * TypeRegistry is a simple registry for all message, enum, or service types.
19
19
  */
20
20
  class TypeRegistry {
21
21
  constructor() {
@@ -23,35 +23,80 @@ class TypeRegistry {
23
23
  this.enums = {};
24
24
  this.services = {};
25
25
  }
26
+ /**
27
+ * Find a message type by its protobuf type name.
28
+ */
26
29
  findMessage(typeName) {
27
30
  return this.messages[typeName];
28
31
  }
32
+ /**
33
+ * Find an enum type by its protobuf type name.
34
+ */
29
35
  findEnum(typeName) {
30
36
  return this.enums[typeName];
31
37
  }
38
+ /**
39
+ * Find a service type by its protobuf type name.
40
+ */
32
41
  findService(typeName) {
33
42
  return this.services[typeName];
34
43
  }
44
+ /**
45
+ * Create a new TypeRegistry from the given types.
46
+ */
47
+ static from(...types) {
48
+ const registry = new TypeRegistry();
49
+ for (const type of types) {
50
+ registry.add(type);
51
+ }
52
+ return registry;
53
+ }
54
+ /**
55
+ * @deprecated use TypeRegistry.from()
56
+ */
57
+ static fromIterable(types) {
58
+ return TypeRegistry.from(...types);
59
+ }
60
+ /**
61
+ * @deprecated use TypeRegistry.from()
62
+ */
63
+ static fromTypes(...types) {
64
+ return TypeRegistry.from(...types);
65
+ }
66
+ /**
67
+ * Add a type to the registry. For messages, the types used in message
68
+ * fields are added recursively. For services, the message types used
69
+ * for requests and responses are added recursively.
70
+ */
35
71
  add(type) {
36
72
  if ("fields" in type) {
37
- this.messages[type.typeName] = type;
73
+ if (!this.findMessage(type.typeName)) {
74
+ this.messages[type.typeName] = type;
75
+ for (const field of type.fields.list()) {
76
+ if (field.kind == "message") {
77
+ this.add(field.T);
78
+ }
79
+ else if (field.kind == "map" && field.V.kind == "message") {
80
+ this.add(field.V.T);
81
+ }
82
+ else if (field.kind == "enum") {
83
+ this.add(field.T);
84
+ }
85
+ }
86
+ }
38
87
  }
39
88
  else if ("methods" in type) {
40
- this.services[type.typeName] = type;
89
+ if (!this.findService(type.typeName)) {
90
+ this.services[type.typeName] = type;
91
+ for (const method of Object.values(type.methods)) {
92
+ this.add(method.I);
93
+ this.add(method.O);
94
+ }
95
+ }
41
96
  }
42
97
  else {
43
98
  this.enums[type.typeName] = type;
44
99
  }
45
100
  }
46
- static fromIterable(types) {
47
- const r = new TypeRegistry();
48
- for (const t of types) {
49
- r.add(t);
50
- }
51
- return r;
52
- }
53
- static fromTypes(...types) {
54
- return TypeRegistry.fromIterable(types);
55
- }
56
101
  }
57
102
  exports.TypeRegistry = TypeRegistry;
@@ -64,7 +64,7 @@ export class BinaryWriter {
64
64
  * Previous fork states.
65
65
  */
66
66
  this.stack = [];
67
- this.textEncoder = textEncoder ?? new TextEncoder();
67
+ this.textEncoder = textEncoder !== null && textEncoder !== void 0 ? textEncoder : new TextEncoder();
68
68
  this.chunks = [];
69
69
  this.buf = [];
70
70
  }
@@ -279,7 +279,7 @@ export class BinaryReader {
279
279
  this.len = buf.length;
280
280
  this.pos = 0;
281
281
  this.view = new DataView(buf.buffer, buf.byteOffset, buf.byteLength);
282
- this.textDecoder = textDecoder ?? new TextDecoder();
282
+ this.textDecoder = textDecoder !== null && textDecoder !== void 0 ? textDecoder : new TextDecoder();
283
283
  }
284
284
  /**
285
285
  * Reads a tag - field number and wire type.
@@ -62,7 +62,7 @@ export class DescriptorRegistry {
62
62
  this.enums = {};
63
63
  this.messages = {};
64
64
  this.services = {};
65
- this.ds = descriptorSet ?? new DescriptorSet();
65
+ this.ds = descriptorSet !== null && descriptorSet !== void 0 ? descriptorSet : new DescriptorSet();
66
66
  if (replaceWkt) {
67
67
  for (const mt of wkMessages) {
68
68
  this.messages["." + mt.typeName] = mt;
@@ -173,8 +173,9 @@ export class DescriptorRegistry {
173
173
  }
174
174
  }
175
175
  function makeTypeLocalName(type) {
176
+ var _a;
176
177
  const typeName = type.typeName;
177
- const packagePrefix = (type.file.proto.package ?? "") + ".";
178
+ const packagePrefix = ((_a = type.file.proto.package) !== null && _a !== void 0 ? _a : "") + ".";
178
179
  if (!typeName.startsWith(packagePrefix)) {
179
180
  return type.name;
180
181
  }
@@ -203,32 +204,23 @@ function makeMapFieldInfo(field, resolver) {
203
204
  if (field.map.value.message) {
204
205
  const messageType = resolver.findMessage(field.map.value.message.typeName);
205
206
  assert(messageType, `message "${field.map.value.message.typeName}" for ${field.toString()} not found`);
206
- return {
207
- ...base,
208
- V: {
207
+ return Object.assign(Object.assign({}, base), { V: {
209
208
  kind: "message",
210
209
  T: messageType,
211
- },
212
- };
210
+ } });
213
211
  }
214
212
  if (field.map.value.enum) {
215
213
  const enumType = resolver.findEnum(field.map.value.enum.typeName);
216
214
  assert(enumType, `enum "${field.map.value.enum.typeName}" for ${field.toString()} not found`);
217
- return {
218
- ...base,
219
- V: {
215
+ return Object.assign(Object.assign({}, base), { V: {
220
216
  kind: "enum",
221
217
  T: enumType,
222
- },
223
- };
218
+ } });
224
219
  }
225
- return {
226
- ...base,
227
- V: {
220
+ return Object.assign(Object.assign({}, base), { V: {
228
221
  kind: "scalar",
229
222
  T: field.map.value.scalarType,
230
- },
231
- };
223
+ } });
232
224
  }
233
225
  function makeScalarFieldInfo(field) {
234
226
  const base = {
@@ -238,25 +230,13 @@ function makeScalarFieldInfo(field) {
238
230
  T: field.scalarType,
239
231
  };
240
232
  if (field.repeated) {
241
- return {
242
- ...base,
243
- repeated: true,
244
- packed: field.packed,
245
- oneof: undefined,
246
- T: field.scalarType,
247
- };
233
+ return Object.assign(Object.assign({}, base), { repeated: true, packed: field.packed, oneof: undefined, T: field.scalarType });
248
234
  }
249
235
  if (field.oneof) {
250
- return {
251
- ...base,
252
- oneof: field.oneof.name,
253
- };
236
+ return Object.assign(Object.assign({}, base), { oneof: field.oneof.name });
254
237
  }
255
238
  if (field.optional) {
256
- return {
257
- ...base,
258
- opt: true,
259
- };
239
+ return Object.assign(Object.assign({}, base), { opt: true });
260
240
  }
261
241
  return base;
262
242
  }
@@ -270,24 +250,13 @@ function makeMessageFieldInfo(field, resolver) {
270
250
  T: messageType,
271
251
  };
272
252
  if (field.repeated) {
273
- return {
274
- ...base,
275
- repeated: true,
276
- packed: field.packed,
277
- oneof: undefined,
278
- };
253
+ return Object.assign(Object.assign({}, base), { repeated: true, packed: field.packed, oneof: undefined });
279
254
  }
280
255
  if (field.oneof) {
281
- return {
282
- ...base,
283
- oneof: field.oneof.name,
284
- };
256
+ return Object.assign(Object.assign({}, base), { oneof: field.oneof.name });
285
257
  }
286
258
  if (field.optional) {
287
- return {
288
- ...base,
289
- opt: true,
290
- };
259
+ return Object.assign(Object.assign({}, base), { opt: true });
291
260
  }
292
261
  return base;
293
262
  }
@@ -301,24 +270,13 @@ function makeEnumFieldInfo(field, resolver) {
301
270
  T: enumType,
302
271
  };
303
272
  if (field.repeated) {
304
- return {
305
- ...base,
306
- repeated: true,
307
- packed: field.packed,
308
- oneof: undefined,
309
- };
273
+ return Object.assign(Object.assign({}, base), { repeated: true, packed: field.packed, oneof: undefined });
310
274
  }
311
275
  if (field.oneof) {
312
- return {
313
- ...base,
314
- oneof: field.oneof.name,
315
- };
276
+ return Object.assign(Object.assign({}, base), { oneof: field.oneof.name });
316
277
  }
317
278
  if (field.optional) {
318
- return {
319
- ...base,
320
- opt: true,
321
- };
279
+ return Object.assign(Object.assign({}, base), { opt: true });
322
280
  }
323
281
  return base;
324
282
  }