@bufbuild/protobuf 2.0.0-alpha.2 → 2.0.0-alpha.4

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 (145) hide show
  1. package/README.md +1 -1
  2. package/dist/cjs/clone.d.ts +1 -1
  3. package/dist/cjs/clone.js +9 -6
  4. package/dist/cjs/codegenv1/boot.d.ts +1 -1
  5. package/dist/cjs/codegenv1/boot.js +3 -5
  6. package/dist/cjs/codegenv1/embed.d.ts +1 -1
  7. package/dist/cjs/codegenv1/embed.js +39 -34
  8. package/dist/cjs/codegenv1/enum.d.ts +1 -1
  9. package/dist/cjs/codegenv1/extension.d.ts +1 -1
  10. package/dist/cjs/codegenv1/file.d.ts +1 -1
  11. package/dist/cjs/codegenv1/file.js +5 -7
  12. package/dist/cjs/codegenv1/index.d.ts +1 -0
  13. package/dist/cjs/codegenv1/index.js +1 -0
  14. package/dist/cjs/codegenv1/message.d.ts +1 -1
  15. package/dist/cjs/codegenv1/scalar.d.ts +5 -0
  16. package/dist/cjs/codegenv1/scalar.js +39 -0
  17. package/dist/cjs/codegenv1/service.d.ts +3 -3
  18. package/dist/cjs/codegenv1/types.d.ts +7 -9
  19. package/dist/cjs/create.d.ts +1 -1
  20. package/dist/cjs/create.js +6 -5
  21. package/dist/cjs/{desc-types.d.ts → descriptors.d.ts} +39 -7
  22. package/dist/cjs/descriptors.js +53 -0
  23. package/dist/cjs/equals.d.ts +1 -1
  24. package/dist/cjs/extensions.d.ts +1 -1
  25. package/dist/cjs/extensions.js +4 -3
  26. package/dist/cjs/fields.d.ts +1 -1
  27. package/dist/cjs/from-binary.d.ts +1 -1
  28. package/dist/cjs/from-binary.js +32 -29
  29. package/dist/cjs/from-json.d.ts +2 -2
  30. package/dist/cjs/from-json.js +201 -188
  31. package/dist/cjs/index.d.ts +2 -1
  32. package/dist/cjs/index.js +2 -1
  33. package/dist/cjs/is-message.d.ts +1 -1
  34. package/dist/cjs/proto-int64.d.ts +4 -1
  35. package/dist/cjs/proto-int64.js +16 -6
  36. package/dist/cjs/reflect/error.d.ts +1 -1
  37. package/dist/cjs/reflect/guard.d.ts +1 -1
  38. package/dist/cjs/reflect/guard.js +4 -4
  39. package/dist/cjs/reflect/index.d.ts +0 -1
  40. package/dist/cjs/reflect/index.js +0 -1
  41. package/dist/cjs/reflect/names.d.ts +1 -1
  42. package/dist/cjs/reflect/names.js +1 -1
  43. package/dist/cjs/reflect/nested-types.d.ts +1 -1
  44. package/dist/cjs/reflect/reflect-check.d.ts +10 -1
  45. package/dist/cjs/reflect/reflect-check.js +126 -9
  46. package/dist/cjs/reflect/reflect-types.d.ts +3 -41
  47. package/dist/cjs/reflect/reflect.d.ts +1 -1
  48. package/dist/cjs/reflect/reflect.js +42 -60
  49. package/dist/cjs/reflect/scalar.d.ts +3 -55
  50. package/dist/cjs/reflect/scalar.js +26 -220
  51. package/dist/cjs/reflect/unsafe.d.ts +1 -17
  52. package/dist/cjs/reflect/unsafe.js +2 -20
  53. package/dist/cjs/{reflect/registry.d.ts → registry.d.ts} +2 -2
  54. package/dist/cjs/{reflect/registry.js → registry.js} +54 -36
  55. package/dist/cjs/to-binary.d.ts +1 -1
  56. package/dist/cjs/to-binary.js +27 -27
  57. package/dist/cjs/to-json.d.ts +7 -6
  58. package/dist/cjs/to-json.js +57 -44
  59. package/dist/cjs/types.d.ts +1 -1
  60. package/dist/cjs/wire/base64-encoding.js +2 -2
  61. package/dist/cjs/wire/binary-encoding.d.ts +20 -0
  62. package/dist/cjs/wire/binary-encoding.js +75 -8
  63. package/dist/cjs/wire/size-delimited.d.ts +1 -1
  64. package/dist/cjs/wire/text-format.d.ts +1 -2
  65. package/dist/cjs/wire/text-format.js +21 -19
  66. package/dist/cjs/wkt/any.d.ts +2 -2
  67. package/dist/cjs/wkt/gen/google/protobuf/descriptor_pb.d.ts +6 -0
  68. package/dist/cjs/wkt/gen/google/protobuf/descriptor_pb.js +1 -1
  69. package/dist/cjs/wkt/wrappers.d.ts +1 -1
  70. package/dist/esm/clone.d.ts +1 -1
  71. package/dist/esm/clone.js +8 -5
  72. package/dist/esm/codegenv1/boot.d.ts +1 -1
  73. package/dist/esm/codegenv1/boot.js +3 -5
  74. package/dist/esm/codegenv1/embed.d.ts +1 -1
  75. package/dist/esm/codegenv1/embed.js +9 -4
  76. package/dist/esm/codegenv1/enum.d.ts +1 -1
  77. package/dist/esm/codegenv1/extension.d.ts +1 -1
  78. package/dist/esm/codegenv1/file.d.ts +1 -1
  79. package/dist/esm/codegenv1/file.js +4 -6
  80. package/dist/esm/codegenv1/index.d.ts +1 -0
  81. package/dist/esm/codegenv1/index.js +1 -0
  82. package/dist/esm/codegenv1/message.d.ts +1 -1
  83. package/dist/esm/codegenv1/scalar.d.ts +5 -0
  84. package/dist/esm/codegenv1/scalar.js +35 -0
  85. package/dist/esm/codegenv1/service.d.ts +3 -3
  86. package/dist/esm/codegenv1/types.d.ts +7 -9
  87. package/dist/esm/create.d.ts +1 -1
  88. package/dist/esm/create.js +4 -3
  89. package/dist/esm/{desc-types.d.ts → descriptors.d.ts} +39 -7
  90. package/dist/esm/descriptors.js +50 -0
  91. package/dist/esm/equals.d.ts +1 -1
  92. package/dist/esm/extensions.d.ts +1 -1
  93. package/dist/esm/extensions.js +4 -3
  94. package/dist/esm/fields.d.ts +1 -1
  95. package/dist/esm/from-binary.d.ts +1 -1
  96. package/dist/esm/from-binary.js +14 -11
  97. package/dist/esm/from-json.d.ts +2 -2
  98. package/dist/esm/from-json.js +194 -181
  99. package/dist/esm/index.d.ts +2 -1
  100. package/dist/esm/index.js +2 -1
  101. package/dist/esm/is-message.d.ts +1 -1
  102. package/dist/esm/proto-int64.d.ts +4 -1
  103. package/dist/esm/proto-int64.js +16 -6
  104. package/dist/esm/reflect/error.d.ts +1 -1
  105. package/dist/esm/reflect/guard.d.ts +1 -1
  106. package/dist/esm/reflect/guard.js +4 -4
  107. package/dist/esm/reflect/index.d.ts +0 -1
  108. package/dist/esm/reflect/index.js +0 -1
  109. package/dist/esm/reflect/names.d.ts +1 -1
  110. package/dist/esm/reflect/names.js +1 -1
  111. package/dist/esm/reflect/nested-types.d.ts +1 -1
  112. package/dist/esm/reflect/reflect-check.d.ts +10 -1
  113. package/dist/esm/reflect/reflect-check.js +119 -2
  114. package/dist/esm/reflect/reflect-types.d.ts +3 -41
  115. package/dist/esm/reflect/reflect-types.js +1 -0
  116. package/dist/esm/reflect/reflect.d.ts +1 -1
  117. package/dist/esm/reflect/reflect.js +25 -43
  118. package/dist/esm/reflect/scalar.d.ts +3 -55
  119. package/dist/esm/reflect/scalar.js +14 -205
  120. package/dist/esm/reflect/unsafe.d.ts +1 -17
  121. package/dist/esm/reflect/unsafe.js +1 -17
  122. package/dist/esm/{reflect/registry.d.ts → registry.d.ts} +2 -2
  123. package/dist/esm/{reflect/registry.js → registry.js} +44 -26
  124. package/dist/esm/to-binary.d.ts +1 -1
  125. package/dist/esm/to-binary.js +1 -1
  126. package/dist/esm/to-json.d.ts +7 -6
  127. package/dist/esm/to-json.js +42 -29
  128. package/dist/esm/types.d.ts +1 -1
  129. package/dist/esm/wire/base64-encoding.js +2 -2
  130. package/dist/esm/wire/binary-encoding.d.ts +20 -0
  131. package/dist/esm/wire/binary-encoding.js +68 -1
  132. package/dist/esm/wire/size-delimited.d.ts +1 -1
  133. package/dist/esm/wire/text-format.d.ts +1 -2
  134. package/dist/esm/wire/text-format.js +5 -3
  135. package/dist/esm/wkt/any.d.ts +2 -2
  136. package/dist/esm/wkt/gen/google/protobuf/descriptor_pb.d.ts +6 -0
  137. package/dist/esm/wkt/gen/google/protobuf/descriptor_pb.js +1 -1
  138. package/dist/esm/wkt/wrappers.d.ts +1 -1
  139. package/package.json +1 -1
  140. package/dist/cjs/desc-types.js +0 -15
  141. package/dist/cjs/reflect/assert.d.ts +0 -16
  142. package/dist/cjs/reflect/assert.js +0 -59
  143. package/dist/esm/desc-types.js +0 -14
  144. package/dist/esm/reflect/assert.d.ts +0 -16
  145. package/dist/esm/reflect/assert.js +0 -52
package/README.md CHANGED
@@ -15,7 +15,7 @@ Some additional features that set it apart from the others:
15
15
  - ECMAScript module support
16
16
  - First-class TypeScript support
17
17
  - Generation of idiomatic JavaScript and TypeScript code.
18
- - Generation of [much smaller bundles](https://github.com/bufbuild/protobuf-es/blob/main/packages/protobuf-bench)
18
+ - Generation of [much smaller bundles](https://github.com/bufbuild/protobuf-es/blob/main/packages/bundle-size)
19
19
  - Implementation of all proto3 features, including the [canonical JSON format](https://developers.google.com/protocol-buffers/docs/proto3#json).
20
20
  - Implementation of all proto2 features, except for extensions and the text format.
21
21
  - Usage of standard JavaScript APIs instead of the [Closure Library](http://googlecode.blogspot.com/2009/11/introducing-closure-tools.html)
@@ -1,5 +1,5 @@
1
1
  import type { MessageShape } from "./types.js";
2
- import type { DescMessage } from "./desc-types.js";
2
+ import { type DescMessage } from "./descriptors.js";
3
3
  /**
4
4
  * Create a deep copy of a message, including extensions and unknown fields.
5
5
  */
package/dist/cjs/clone.js CHANGED
@@ -14,9 +14,9 @@
14
14
  // limitations under the License.
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.clone = void 0;
17
+ const descriptors_js_1 = require("./descriptors.js");
17
18
  const reflect_js_1 = require("./reflect/reflect.js");
18
19
  const guard_js_1 = require("./reflect/guard.js");
19
- const scalar_js_1 = require("./reflect/scalar.js");
20
20
  /**
21
21
  * Create a deep copy of a message, including extensions and unknown fields.
22
22
  */
@@ -40,18 +40,21 @@ function cloneReflect(i) {
40
40
  break;
41
41
  }
42
42
  case "list":
43
+ // eslint-disable-next-line no-case-declarations
44
+ const list = o.get(f);
43
45
  for (const item of i.get(f)) {
44
- // TODO fix type error
45
- // @ts-expect-error TODO
46
- const err = o.addListItem(f, cloneSingular(f, item));
46
+ const err = list.add(cloneSingular(f, item));
47
47
  if (err) {
48
48
  throw err;
49
49
  }
50
50
  }
51
51
  break;
52
52
  case "map":
53
+ // eslint-disable-next-line no-case-declarations
54
+ const map = o.get(f);
53
55
  for (const entry of i.get(f).entries()) {
54
- const err = o.setMapEntry(f, entry[0], cloneSingular(f, entry[1]));
56
+ // @ts-expect-error TODO fix type error
57
+ const err = map.set(entry[0], cloneSingular(f, entry[1]));
55
58
  if (err) {
56
59
  throw err;
57
60
  }
@@ -69,7 +72,7 @@ function cloneSingular(field, value) {
69
72
  if (field.message !== undefined && (0, guard_js_1.isReflectMessage)(value)) {
70
73
  return cloneReflect(value);
71
74
  }
72
- if (field.scalar == scalar_js_1.ScalarType.BYTES && value instanceof Uint8Array) {
75
+ if (field.scalar == descriptors_js_1.ScalarType.BYTES && value instanceof Uint8Array) {
73
76
  // @ts-expect-error T cannot extend Uint8Array in practice
74
77
  return value.slice();
75
78
  }
@@ -1,5 +1,5 @@
1
1
  import type { DescriptorProto_ExtensionRange, FieldDescriptorProto_Label, FieldDescriptorProto_Type, FieldOptions_OptionRetention, FieldOptions_OptionTargetType, FieldOptions_EditionDefault, EnumValueDescriptorProto, FileDescriptorProto } from "../wkt/gen/google/protobuf/descriptor_pb.js";
2
- import type { DescFile } from "../desc-types.js";
2
+ import type { DescFile } from "../descriptors.js";
3
3
  /**
4
4
  * Hydrate a file descriptor for google/protobuf/descriptor.proto from a plain
5
5
  * object.
@@ -15,8 +15,7 @@
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.bootFileDescriptorProto = exports.boot = void 0;
17
17
  const restore_json_names_js_1 = require("./restore-json-names.js");
18
- const registry_js_1 = require("../reflect/registry.js");
19
- const assert_js_1 = require("../reflect/assert.js");
18
+ const registry_js_1 = require("../registry.js");
20
19
  /**
21
20
  * Hydrate a file descriptor for google/protobuf/descriptor.proto from a plain
22
21
  * object.
@@ -29,9 +28,8 @@ function boot(boot) {
29
28
  const root = bootFileDescriptorProto(boot);
30
29
  root.messageType.forEach(restore_json_names_js_1.restoreJsonNames);
31
30
  const reg = (0, registry_js_1.createFileRegistry)(root, () => undefined);
32
- const file = reg.getFile(root.name);
33
- (0, assert_js_1.assert)(file);
34
- return file;
31
+ // non-null assertion because we just created the registry from the file we look up
32
+ return reg.getFile(root.name);
35
33
  }
36
34
  exports.boot = boot;
37
35
  /**
@@ -1,4 +1,4 @@
1
- import type { DescEnum, DescExtension, DescMessage, DescService } from "../desc-types.js";
1
+ import type { DescEnum, DescExtension, DescMessage, DescService } from "../descriptors.js";
2
2
  import type { FileDescriptorProto } from "../wkt/gen/google/protobuf/descriptor_pb.js";
3
3
  import type { FileDescriptorProtoBoot } from "./boot.js";
4
4
  type EmbedUnknown = {
@@ -15,7 +15,6 @@
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.createFileDescriptorProtoBoot = exports.pathInFileDesc = exports.embedFileDesc = void 0;
17
17
  const names_js_1 = require("../reflect/names.js");
18
- const assert_js_1 = require("../reflect/assert.js");
19
18
  const fields_js_1 = require("../fields.js");
20
19
  const base64_encoding_js_1 = require("../wire/base64-encoding.js");
21
20
  const to_binary_js_1 = require("../to-binary.js");
@@ -118,17 +117,17 @@ exports.pathInFileDesc = pathInFileDesc;
118
117
  */
119
118
  function createFileDescriptorProtoBoot(proto) {
120
119
  var _a;
121
- (0, assert_js_1.assert)(proto.name == "google/protobuf/descriptor.proto");
122
- (0, assert_js_1.assert)(proto.package == "google.protobuf");
123
- (0, assert_js_1.assert)(!proto.dependency.length);
124
- (0, assert_js_1.assert)(!proto.publicDependency.length);
125
- (0, assert_js_1.assert)(!proto.weakDependency.length);
126
- (0, assert_js_1.assert)(!proto.service.length);
127
- (0, assert_js_1.assert)(!proto.extension.length);
128
- (0, assert_js_1.assert)(proto.sourceCodeInfo === undefined);
129
- (0, assert_js_1.assert)(proto.syntax == "" || proto.syntax == "proto2");
130
- (0, assert_js_1.assert)(!((_a = proto.options) === null || _a === void 0 ? void 0 : _a.features)); // we're dropping file options
131
- (0, assert_js_1.assert)(proto.edition === descriptor_pb_js_1.Edition.EDITION_UNKNOWN);
120
+ assert(proto.name == "google/protobuf/descriptor.proto");
121
+ assert(proto.package == "google.protobuf");
122
+ assert(!proto.dependency.length);
123
+ assert(!proto.publicDependency.length);
124
+ assert(!proto.weakDependency.length);
125
+ assert(!proto.service.length);
126
+ assert(!proto.extension.length);
127
+ assert(proto.sourceCodeInfo === undefined);
128
+ assert(proto.syntax == "" || proto.syntax == "proto2");
129
+ assert(!((_a = proto.options) === null || _a === void 0 ? void 0 : _a.features)); // we're dropping file options
130
+ assert(proto.edition === descriptor_pb_js_1.Edition.EDITION_UNKNOWN);
132
131
  return {
133
132
  name: proto.name,
134
133
  package: proto.package,
@@ -138,10 +137,9 @@ function createFileDescriptorProtoBoot(proto) {
138
137
  }
139
138
  exports.createFileDescriptorProtoBoot = createFileDescriptorProtoBoot;
140
139
  function createDescriptorBoot(proto) {
141
- (0, assert_js_1.assert)(proto.extension.length == 0);
142
- (0, assert_js_1.assert)(!proto.oneofDecl.length);
143
- (0, assert_js_1.assert)(!proto.options);
144
- (0, assert_js_1.assert)(!proto.reservedName.length); // we're also dropping reserved_range
140
+ assert(proto.extension.length == 0);
141
+ assert(!proto.oneofDecl.length);
142
+ assert(!proto.options);
145
143
  const b = {
146
144
  name: proto.name,
147
145
  };
@@ -156,18 +154,18 @@ function createDescriptorBoot(proto) {
156
154
  }
157
155
  if (proto.extensionRange.length) {
158
156
  b.extensionRange = proto.extensionRange.map((r) => {
159
- (0, assert_js_1.assert)(!r.options);
157
+ assert(!r.options);
160
158
  return { start: r.start, end: r.end };
161
159
  });
162
160
  }
163
161
  return b;
164
162
  }
165
163
  function createFieldDescriptorBoot(proto) {
166
- (0, assert_js_1.assert)((0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldDescriptorProtoDesc.field.name));
167
- (0, assert_js_1.assert)((0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldDescriptorProtoDesc.field.number));
168
- (0, assert_js_1.assert)((0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldDescriptorProtoDesc.field.type));
169
- (0, assert_js_1.assert)(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldDescriptorProtoDesc.field.oneofIndex));
170
- (0, assert_js_1.assert)(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldDescriptorProtoDesc.field.jsonName) ||
164
+ assert((0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldDescriptorProtoDesc.field.name));
165
+ assert((0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldDescriptorProtoDesc.field.number));
166
+ assert((0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldDescriptorProtoDesc.field.type));
167
+ assert(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldDescriptorProtoDesc.field.oneofIndex));
168
+ assert(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldDescriptorProtoDesc.field.jsonName) ||
171
169
  proto.jsonName === (0, names_js_1.protoCamelCase)(proto.name));
172
170
  const b = {
173
171
  name: proto.name,
@@ -193,18 +191,18 @@ function createFieldDescriptorBoot(proto) {
193
191
  }
194
192
  function createFieldOptionsBoot(proto) {
195
193
  const b = {};
196
- (0, assert_js_1.assert)(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldOptionsDesc.field.ctype));
194
+ assert(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldOptionsDesc.field.ctype));
197
195
  if ((0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldOptionsDesc.field.packed)) {
198
196
  b.packed = proto.packed;
199
197
  }
200
- (0, assert_js_1.assert)(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldOptionsDesc.field.jstype));
201
- (0, assert_js_1.assert)(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldOptionsDesc.field.lazy));
202
- (0, assert_js_1.assert)(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldOptionsDesc.field.unverifiedLazy));
198
+ assert(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldOptionsDesc.field.jstype));
199
+ assert(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldOptionsDesc.field.lazy));
200
+ assert(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldOptionsDesc.field.unverifiedLazy));
203
201
  if ((0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldOptionsDesc.field.deprecated)) {
204
202
  b.deprecated = proto.deprecated;
205
203
  }
206
- (0, assert_js_1.assert)(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldOptionsDesc.field.weak));
207
- (0, assert_js_1.assert)(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldOptionsDesc.field.debugRedact));
204
+ assert(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldOptionsDesc.field.weak));
205
+ assert(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldOptionsDesc.field.debugRedact));
208
206
  if ((0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldOptionsDesc.field.retention)) {
209
207
  b.retention = proto.retention;
210
208
  }
@@ -217,18 +215,16 @@ function createFieldOptionsBoot(proto) {
217
215
  edition: d.edition,
218
216
  }));
219
217
  }
220
- (0, assert_js_1.assert)(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldOptionsDesc.field.features));
221
- (0, assert_js_1.assert)(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldOptionsDesc.field.uninterpretedOption));
218
+ assert(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldOptionsDesc.field.features));
219
+ assert(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldOptionsDesc.field.uninterpretedOption));
222
220
  return b;
223
221
  }
224
222
  function createEnumDescriptorBoot(proto) {
225
- (0, assert_js_1.assert)(!proto.reservedName.length);
226
- (0, assert_js_1.assert)(!proto.reservedRange.length);
227
- (0, assert_js_1.assert)(!proto.options);
223
+ assert(!proto.options);
228
224
  return {
229
225
  name: proto.name,
230
226
  value: proto.value.map((v) => {
231
- (0, assert_js_1.assert)(!v.options);
227
+ assert(!v.options);
232
228
  return {
233
229
  name: v.name,
234
230
  number: v.number,
@@ -236,3 +232,12 @@ function createEnumDescriptorBoot(proto) {
236
232
  }),
237
233
  };
238
234
  }
235
+ /**
236
+ * Assert that condition is truthy or throw error.
237
+ */
238
+ function assert(condition) {
239
+ // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions -- we want the implicit conversion to boolean
240
+ if (!condition) {
241
+ throw new Error();
242
+ }
243
+ }
@@ -1,4 +1,4 @@
1
- import type { DescEnum, DescFile } from "../desc-types.js";
1
+ import type { DescEnum, DescFile } from "../descriptors.js";
2
2
  import type { GenDescEnum } from "./types.js";
3
3
  /**
4
4
  * Hydrate an enum descriptor.
@@ -1,5 +1,5 @@
1
1
  import type { Message } from "../types.js";
2
- import type { DescFile } from "../desc-types.js";
2
+ import type { DescFile } from "../descriptors.js";
3
3
  import type { GenDescExtension } from "./types.js";
4
4
  /**
5
5
  * Hydrate an extension descriptor.
@@ -1,4 +1,4 @@
1
- import type { DescFile } from "../desc-types.js";
1
+ import type { DescFile } from "../descriptors.js";
2
2
  /**
3
3
  * Hydrate a file descriptor.
4
4
  *
@@ -14,10 +14,9 @@
14
14
  // limitations under the License.
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.fileDesc = void 0;
17
- const index_js_1 = require("../wire/index.js");
17
+ const base64_encoding_js_1 = require("../wire/base64-encoding.js");
18
18
  const descriptor_pb_js_1 = require("../wkt/gen/google/protobuf/descriptor_pb.js");
19
- const registry_js_1 = require("../reflect/registry.js");
20
- const assert_js_1 = require("../reflect/assert.js");
19
+ const registry_js_1 = require("../registry.js");
21
20
  const restore_json_names_js_1 = require("./restore-json-names.js");
22
21
  const from_binary_js_1 = require("../from-binary.js");
23
22
  /**
@@ -27,12 +26,11 @@ const from_binary_js_1 = require("../from-binary.js");
27
26
  */
28
27
  function fileDesc(b64, imports) {
29
28
  var _a;
30
- const root = (0, from_binary_js_1.fromBinary)(descriptor_pb_js_1.FileDescriptorProtoDesc, (0, index_js_1.base64Decode)(b64));
29
+ const root = (0, from_binary_js_1.fromBinary)(descriptor_pb_js_1.FileDescriptorProtoDesc, (0, base64_encoding_js_1.base64Decode)(b64));
31
30
  root.messageType.forEach(restore_json_names_js_1.restoreJsonNames);
32
31
  root.dependency = (_a = imports === null || imports === void 0 ? void 0 : imports.map((f) => f.proto.name)) !== null && _a !== void 0 ? _a : [];
33
32
  const reg = (0, registry_js_1.createFileRegistry)(root, (protoFileName) => imports === null || imports === void 0 ? void 0 : imports.find((f) => f.proto.name === protoFileName));
34
- const file = reg.getFile(root.name);
35
- (0, assert_js_1.assert)(file);
36
- return file;
33
+ // non-null assertion because we just created the registry from the file we look up
34
+ return reg.getFile(root.name);
37
35
  }
38
36
  exports.fileDesc = fileDesc;
@@ -6,4 +6,5 @@ export * from "./file.js";
6
6
  export * from "./message.js";
7
7
  export * from "./service.js";
8
8
  export * from "./symbols.js";
9
+ export * from "./scalar.js";
9
10
  export * from "./types.js";
@@ -35,4 +35,5 @@ __exportStar(require("./file.js"), exports);
35
35
  __exportStar(require("./message.js"), exports);
36
36
  __exportStar(require("./service.js"), exports);
37
37
  __exportStar(require("./symbols.js"), exports);
38
+ __exportStar(require("./scalar.js"), exports);
38
39
  __exportStar(require("./types.js"), exports);
@@ -1,5 +1,5 @@
1
1
  import type { Message } from "../types.js";
2
- import type { DescFile } from "../desc-types.js";
2
+ import type { DescFile } from "../descriptors.js";
3
3
  import type { GenDescMessage } from "./types.js";
4
4
  /**
5
5
  * Hydrate a message descriptor.
@@ -0,0 +1,5 @@
1
+ import { ScalarType } from "../descriptors.js";
2
+ /**
3
+ * Return the TypeScript type (as a string) for the given scalar type.
4
+ */
5
+ export declare function scalarTypeScriptType(scalar: ScalarType, longAsString: boolean): "string" | "boolean" | "bigint" | "bigint | string" | "Uint8Array" | "number";
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.scalarTypeScriptType = void 0;
17
+ const descriptors_js_1 = require("../descriptors.js");
18
+ /**
19
+ * Return the TypeScript type (as a string) for the given scalar type.
20
+ */
21
+ function scalarTypeScriptType(scalar, longAsString) {
22
+ switch (scalar) {
23
+ case descriptors_js_1.ScalarType.STRING:
24
+ return "string";
25
+ case descriptors_js_1.ScalarType.BOOL:
26
+ return "boolean";
27
+ case descriptors_js_1.ScalarType.UINT64:
28
+ case descriptors_js_1.ScalarType.SFIXED64:
29
+ case descriptors_js_1.ScalarType.FIXED64:
30
+ case descriptors_js_1.ScalarType.SINT64:
31
+ case descriptors_js_1.ScalarType.INT64:
32
+ return longAsString ? "string" : "bigint";
33
+ case descriptors_js_1.ScalarType.BYTES:
34
+ return "Uint8Array";
35
+ default:
36
+ return "number";
37
+ }
38
+ }
39
+ exports.scalarTypeScriptType = scalarTypeScriptType;
@@ -1,8 +1,8 @@
1
- import type { GenDescService, GenDescServiceShape } from "./types.js";
2
- import type { DescFile } from "../desc-types.js";
1
+ import type { GenDescService, GenDescServiceMethods } from "./types.js";
2
+ import type { DescFile } from "../descriptors.js";
3
3
  /**
4
4
  * Hydrate a service descriptor.
5
5
  *
6
6
  * @private
7
7
  */
8
- export declare function serviceDesc<T extends GenDescServiceShape>(file: DescFile, path: number, ...paths: number[]): GenDescService<T>;
8
+ export declare function serviceDesc<T extends GenDescServiceMethods>(file: DescFile, path: number, ...paths: number[]): GenDescService<T>;
@@ -1,5 +1,5 @@
1
1
  import type { Message } from "../types.js";
2
- import type { DescEnum, DescExtension, DescField, DescFile, DescMessage, DescService } from "../desc-types.js";
2
+ import type { DescEnum, DescExtension, DescField, DescFile, DescMessage, DescMethod, DescService } from "../descriptors.js";
3
3
  /**
4
4
  * Describes a protobuf source file.
5
5
  *
@@ -43,17 +43,15 @@ export type GenDescExtension<Extendee extends Message = Message, RuntimeShape =
43
43
  *
44
44
  * @private
45
45
  */
46
- export type GenDescService<RuntimeShape extends GenDescServiceShape> = DescService & brand<RuntimeShape>;
46
+ export type GenDescService<RuntimeShape extends GenDescServiceMethods> = Omit<DescService, "method"> & {
47
+ method: {
48
+ [K in keyof RuntimeShape]: RuntimeShape[K] & DescMethod;
49
+ };
50
+ };
47
51
  /**
48
52
  * @private
49
53
  */
50
- export type GenDescServiceShape = {
51
- [localName: string]: {
52
- kind: "unary" | "server_streaming" | "client_streaming" | "bidi_streaming";
53
- I: Message;
54
- O: Message;
55
- };
56
- };
54
+ export type GenDescServiceMethods = Record<string, Pick<DescMethod, "input" | "output" | "methodKind">>;
57
55
  declare class brand<A, B = unknown> {
58
56
  protected a: A | boolean;
59
57
  protected b: B | boolean;
@@ -1,4 +1,4 @@
1
- import type { DescMessage } from "./desc-types.js";
1
+ import { type DescMessage } from "./descriptors.js";
2
2
  import type { MessageInitShape, MessageShape } from "./types.js";
3
3
  /**
4
4
  * Create a new message instance.
@@ -15,6 +15,7 @@
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.create = void 0;
17
17
  const is_message_js_1 = require("./is-message.js");
18
+ const descriptors_js_1 = require("./descriptors.js");
18
19
  const scalar_js_1 = require("./reflect/scalar.js");
19
20
  const guard_js_1 = require("./reflect/guard.js");
20
21
  const unsafe_js_1 = require("./reflect/unsafe.js");
@@ -87,14 +88,14 @@ function initMessage(messageDesc, message, init) {
87
88
  return message;
88
89
  }
89
90
  function initScalar(field, value) {
90
- if (field.scalar == scalar_js_1.ScalarType.BYTES) {
91
+ if (field.scalar == descriptors_js_1.ScalarType.BYTES) {
91
92
  return toU8Arr(value);
92
93
  }
93
94
  return value;
94
95
  }
95
96
  function initMap(field, value) {
96
97
  if ((0, guard_js_1.isObject)(value)) {
97
- if (field.scalar == scalar_js_1.ScalarType.BYTES) {
98
+ if (field.scalar == descriptors_js_1.ScalarType.BYTES) {
98
99
  return convertObjectValues(value, toU8Arr);
99
100
  }
100
101
  if (field.mapKind == "message") {
@@ -105,7 +106,7 @@ function initMap(field, value) {
105
106
  }
106
107
  function initList(field, value) {
107
108
  if (Array.isArray(value)) {
108
- if (field.scalar == scalar_js_1.ScalarType.BYTES) {
109
+ if (field.scalar == descriptors_js_1.ScalarType.BYTES) {
109
110
  return value.map(toU8Arr);
110
111
  }
111
112
  if (field.listKind == "message") {
@@ -221,11 +222,11 @@ function createZeroField(field) {
221
222
  }
222
223
  const defaultValue = field.getDefaultValue();
223
224
  if (defaultValue !== undefined) {
224
- return field.fieldKind == "scalar" && field.longType == scalar_js_1.LongType.STRING
225
+ return field.fieldKind == "scalar" && field.longAsString
225
226
  ? defaultValue.toString()
226
227
  : defaultValue;
227
228
  }
228
229
  return field.fieldKind == "scalar"
229
- ? (0, scalar_js_1.scalarZeroValue)(field.scalar, field.longType)
230
+ ? (0, scalar_js_1.scalarZeroValue)(field.scalar, field.longAsString)
230
231
  : field.enum.values[0].number;
231
232
  }
@@ -1,7 +1,29 @@
1
1
  import type { DescriptorProto, Edition, EnumDescriptorProto, EnumValueDescriptorProto, FeatureSet_FieldPresence, FieldDescriptorProto, FileDescriptorProto, MethodDescriptorProto, MethodOptions_IdempotencyLevel, OneofDescriptorProto, ServiceDescriptorProto } from "./wkt/gen/google/protobuf/descriptor_pb.js";
2
- import type { LongType, ScalarType, ScalarValue } from "./reflect/scalar.js";
2
+ import type { ScalarValue } from "./reflect/scalar.js";
3
3
  export type SupportedEdition = Extract<Edition, Edition.EDITION_PROTO2 | Edition.EDITION_PROTO3 | Edition.EDITION_2023>;
4
4
  type SupportedFieldPresence = Extract<FeatureSet_FieldPresence, FeatureSet_FieldPresence.EXPLICIT | FeatureSet_FieldPresence.IMPLICIT | FeatureSet_FieldPresence.LEGACY_REQUIRED>;
5
+ /**
6
+ * Scalar value types. This is a subset of field types declared by protobuf
7
+ * enum google.protobuf.FieldDescriptorProto.Type The types GROUP and MESSAGE
8
+ * are omitted, but the numerical values are identical.
9
+ */
10
+ export declare enum ScalarType {
11
+ DOUBLE = 1,
12
+ FLOAT = 2,
13
+ INT64 = 3,
14
+ UINT64 = 4,
15
+ INT32 = 5,
16
+ FIXED64 = 6,
17
+ FIXED32 = 7,
18
+ BOOL = 8,
19
+ STRING = 9,
20
+ BYTES = 12,
21
+ UINT32 = 13,
22
+ SFIXED32 = 15,
23
+ SFIXED64 = 16,
24
+ SINT32 = 17,// Uses ZigZag encoding.
25
+ SINT64 = 18
26
+ }
5
27
  /**
6
28
  * A union of all descriptors, discriminated by a `kind` property.
7
29
  */
@@ -281,10 +303,13 @@ type descFieldScalar<T extends ScalarType = ScalarType> = T extends T ? {
281
303
  */
282
304
  readonly scalar: T;
283
305
  /**
284
- * JavaScript type for 64 bit integral types (int64, uint64,
285
- * sint64, fixed64, sfixed64).
306
+ * By default, 64-bit integral types (int64, uint64, sint64, fixed64,
307
+ * sfixed64) are represented with BigInt.
308
+ *
309
+ * If the field option `jstype = JS_STRING` is set, this property
310
+ * is true, and 64-bit integral types are represented with String.
286
311
  */
287
- readonly longType: LongType;
312
+ readonly longAsString: boolean;
288
313
  /**
289
314
  * The message type, if it is a message field.
290
315
  */
@@ -369,10 +394,13 @@ type descFieldListScalar<T extends ScalarType = ScalarType> = T extends T ? {
369
394
  */
370
395
  readonly scalar: T;
371
396
  /**
372
- * JavaScript type for 64 bit integral types (int64, uint64,
373
- * sint64, fixed64, sfixed64).
397
+ * By default, 64-bit integral types (int64, uint64, sint64, fixed64,
398
+ * sfixed64) are represented with BigInt.
399
+ *
400
+ * If the field option `jstype = JS_STRING` is set, this property
401
+ * is true, and 64-bit integral types are represented with String.
374
402
  */
375
- readonly longType: LongType;
403
+ readonly longAsString: boolean;
376
404
  } : never;
377
405
  type descFieldListEnum = {
378
406
  readonly listKind: "enum";
@@ -526,6 +554,10 @@ export interface DescService {
526
554
  * The RPCs this service declares.
527
555
  */
528
556
  readonly methods: DescMethod[];
557
+ /**
558
+ * All methods of this service by their "localName".
559
+ */
560
+ readonly method: Record<string, DescMethod>;
529
561
  /**
530
562
  * Marked as deprecated in the protobuf source.
531
563
  */
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ // Copyright 2021-2024 Buf Technologies, Inc.
3
+ //
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+ //
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+ //
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ Object.defineProperty(exports, "__esModule", { value: true });
16
+ exports.ScalarType = void 0;
17
+ /**
18
+ * Scalar value types. This is a subset of field types declared by protobuf
19
+ * enum google.protobuf.FieldDescriptorProto.Type The types GROUP and MESSAGE
20
+ * are omitted, but the numerical values are identical.
21
+ */
22
+ var ScalarType;
23
+ (function (ScalarType) {
24
+ // 0 is reserved for errors.
25
+ // Order is weird for historical reasons.
26
+ ScalarType[ScalarType["DOUBLE"] = 1] = "DOUBLE";
27
+ ScalarType[ScalarType["FLOAT"] = 2] = "FLOAT";
28
+ // Not ZigZag encoded. Negative numbers take 10 bytes. Use TYPE_SINT64 if
29
+ // negative values are likely.
30
+ ScalarType[ScalarType["INT64"] = 3] = "INT64";
31
+ ScalarType[ScalarType["UINT64"] = 4] = "UINT64";
32
+ // Not ZigZag encoded. Negative numbers take 10 bytes. Use TYPE_SINT32 if
33
+ // negative values are likely.
34
+ ScalarType[ScalarType["INT32"] = 5] = "INT32";
35
+ ScalarType[ScalarType["FIXED64"] = 6] = "FIXED64";
36
+ ScalarType[ScalarType["FIXED32"] = 7] = "FIXED32";
37
+ ScalarType[ScalarType["BOOL"] = 8] = "BOOL";
38
+ ScalarType[ScalarType["STRING"] = 9] = "STRING";
39
+ // Tag-delimited aggregate.
40
+ // Group type is deprecated and not supported in proto3. However, Proto3
41
+ // implementations should still be able to parse the group wire format and
42
+ // treat group fields as unknown fields.
43
+ // TYPE_GROUP = 10,
44
+ // TYPE_MESSAGE = 11, // Length-delimited aggregate.
45
+ // New in version 2.
46
+ ScalarType[ScalarType["BYTES"] = 12] = "BYTES";
47
+ ScalarType[ScalarType["UINT32"] = 13] = "UINT32";
48
+ // TYPE_ENUM = 14,
49
+ ScalarType[ScalarType["SFIXED32"] = 15] = "SFIXED32";
50
+ ScalarType[ScalarType["SFIXED64"] = 16] = "SFIXED64";
51
+ ScalarType[ScalarType["SINT32"] = 17] = "SINT32";
52
+ ScalarType[ScalarType["SINT64"] = 18] = "SINT64";
53
+ })(ScalarType || (exports.ScalarType = ScalarType = {}));
@@ -1,5 +1,5 @@
1
1
  import type { MessageShape } from "./types.js";
2
- import type { DescMessage } from "./desc-types.js";
2
+ import type { DescMessage } from "./descriptors.js";
3
3
  /**
4
4
  * Compare two messages of the same type.
5
5
  *
@@ -1,4 +1,4 @@
1
- import type { AnyDesc, DescEnum, DescEnumValue, DescExtension, DescField, DescFile, DescMessage, DescMethod, DescOneof, DescService } from "./desc-types.js";
1
+ import type { AnyDesc, DescEnum, DescEnumValue, DescExtension, DescField, DescFile, DescMessage, DescMethod, DescOneof, DescService } from "./descriptors.js";
2
2
  import type { ReflectMessage } from "./reflect/reflect-types.js";
3
3
  import type { Extendee, ExtensionValueShape } from "./types.js";
4
4
  import type { EnumOptions, EnumValueOptions, FieldOptions, FileOptions, MessageOptions, MethodOptions, OneofOptions, ServiceOptions } from "./wkt/gen/google/protobuf/descriptor_pb.js";
@@ -14,7 +14,6 @@
14
14
  // limitations under the License.
15
15
  Object.defineProperty(exports, "__esModule", { value: true });
16
16
  exports.createExtensionContainer = exports.getOption = exports.hasOption = exports.hasExtension = exports.clearExtension = exports.setExtension = exports.getExtension = void 0;
17
- const assert_js_1 = require("./reflect/assert.js");
18
17
  const create_js_1 = require("./create.js");
19
18
  const from_binary_js_1 = require("./from-binary.js");
20
19
  const reflect_js_1 = require("./reflect/reflect.js");
@@ -155,7 +154,7 @@ function createExtensionContainer(extension, value) {
155
154
  // Only message fields are undefined, rest will have a zero value.
156
155
  const desc = extension.message;
157
156
  if ((0, wrappers_js_1.isWrapperDesc)(desc)) {
158
- return (0, scalar_js_1.scalarZeroValue)(desc.fields[0].scalar, desc.fields[0].longType);
157
+ return (0, scalar_js_1.scalarZeroValue)(desc.fields[0].scalar, desc.fields[0].longAsString);
159
158
  }
160
159
  return (0, create_js_1.create)(desc);
161
160
  }
@@ -165,5 +164,7 @@ function createExtensionContainer(extension, value) {
165
164
  }
166
165
  exports.createExtensionContainer = createExtensionContainer;
167
166
  function assertExtendee(extension, message) {
168
- (0, assert_js_1.assert)(extension.extendee.typeName == message.$typeName, `extension ${extension.typeName} can only be applied to message ${extension.extendee.typeName}`);
167
+ if (extension.extendee.typeName != message.$typeName) {
168
+ throw new Error(`extension ${extension.typeName} can only be applied to message ${extension.extendee.typeName}`);
169
+ }
169
170
  }
@@ -1,5 +1,5 @@
1
1
  import type { MessageShape } from "./types.js";
2
- import type { DescField, DescMessage } from "./desc-types.js";
2
+ import type { DescField, DescMessage } from "./descriptors.js";
3
3
  /**
4
4
  * Returns true if the field is set.
5
5
  *