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

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 (143) hide show
  1. package/README.md +1 -1
  2. package/dist/cjs/clone.d.ts +1 -1
  3. package/dist/cjs/clone.js +2 -2
  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 +2 -2
  28. package/dist/cjs/from-binary.js +22 -22
  29. package/dist/cjs/from-json.d.ts +4 -4
  30. package/dist/cjs/from-json.js +200 -181
  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/index.d.ts +0 -1
  39. package/dist/cjs/reflect/index.js +0 -1
  40. package/dist/cjs/reflect/names.d.ts +1 -1
  41. package/dist/cjs/reflect/names.js +1 -1
  42. package/dist/cjs/reflect/nested-types.d.ts +1 -1
  43. package/dist/cjs/reflect/reflect-check.d.ts +10 -1
  44. package/dist/cjs/reflect/reflect-check.js +126 -9
  45. package/dist/cjs/reflect/reflect-types.d.ts +4 -4
  46. package/dist/cjs/reflect/reflect.d.ts +1 -1
  47. package/dist/cjs/reflect/reflect.js +28 -27
  48. package/dist/cjs/reflect/scalar.d.ts +3 -55
  49. package/dist/cjs/reflect/scalar.js +26 -220
  50. package/dist/cjs/reflect/unsafe.d.ts +1 -1
  51. package/dist/cjs/reflect/unsafe.js +1 -1
  52. package/dist/cjs/{reflect/registry.d.ts → registry.d.ts} +2 -2
  53. package/dist/cjs/{reflect/registry.js → registry.js} +54 -36
  54. package/dist/cjs/to-binary.d.ts +1 -1
  55. package/dist/cjs/to-binary.js +27 -27
  56. package/dist/cjs/to-json.d.ts +7 -6
  57. package/dist/cjs/to-json.js +57 -44
  58. package/dist/cjs/types.d.ts +1 -1
  59. package/dist/cjs/wire/base64-encoding.js +2 -2
  60. package/dist/cjs/wire/binary-encoding.d.ts +20 -0
  61. package/dist/cjs/wire/binary-encoding.js +54 -8
  62. package/dist/cjs/wire/size-delimited.d.ts +1 -1
  63. package/dist/cjs/wire/text-format.d.ts +1 -2
  64. package/dist/cjs/wire/text-format.js +21 -19
  65. package/dist/cjs/wkt/any.d.ts +3 -3
  66. package/dist/cjs/wkt/gen/google/protobuf/descriptor_pb.d.ts +6 -0
  67. package/dist/cjs/wkt/gen/google/protobuf/descriptor_pb.js +1 -1
  68. package/dist/cjs/wkt/wrappers.d.ts +1 -1
  69. package/dist/esm/clone.d.ts +1 -1
  70. package/dist/esm/clone.js +1 -1
  71. package/dist/esm/codegenv1/boot.d.ts +1 -1
  72. package/dist/esm/codegenv1/boot.js +3 -5
  73. package/dist/esm/codegenv1/embed.d.ts +1 -1
  74. package/dist/esm/codegenv1/embed.js +9 -4
  75. package/dist/esm/codegenv1/enum.d.ts +1 -1
  76. package/dist/esm/codegenv1/extension.d.ts +1 -1
  77. package/dist/esm/codegenv1/file.d.ts +1 -1
  78. package/dist/esm/codegenv1/file.js +4 -6
  79. package/dist/esm/codegenv1/index.d.ts +1 -0
  80. package/dist/esm/codegenv1/index.js +1 -0
  81. package/dist/esm/codegenv1/message.d.ts +1 -1
  82. package/dist/esm/codegenv1/scalar.d.ts +5 -0
  83. package/dist/esm/codegenv1/scalar.js +35 -0
  84. package/dist/esm/codegenv1/service.d.ts +3 -3
  85. package/dist/esm/codegenv1/types.d.ts +7 -9
  86. package/dist/esm/create.d.ts +1 -1
  87. package/dist/esm/create.js +4 -3
  88. package/dist/esm/{desc-types.d.ts → descriptors.d.ts} +39 -7
  89. package/dist/esm/descriptors.js +50 -0
  90. package/dist/esm/equals.d.ts +1 -1
  91. package/dist/esm/extensions.d.ts +1 -1
  92. package/dist/esm/extensions.js +4 -3
  93. package/dist/esm/fields.d.ts +1 -1
  94. package/dist/esm/from-binary.d.ts +2 -2
  95. package/dist/esm/from-binary.js +4 -4
  96. package/dist/esm/from-json.d.ts +4 -4
  97. package/dist/esm/from-json.js +193 -174
  98. package/dist/esm/index.d.ts +2 -1
  99. package/dist/esm/index.js +2 -1
  100. package/dist/esm/is-message.d.ts +1 -1
  101. package/dist/esm/proto-int64.d.ts +4 -1
  102. package/dist/esm/proto-int64.js +16 -6
  103. package/dist/esm/reflect/error.d.ts +1 -1
  104. package/dist/esm/reflect/guard.d.ts +1 -1
  105. package/dist/esm/reflect/index.d.ts +0 -1
  106. package/dist/esm/reflect/index.js +0 -1
  107. package/dist/esm/reflect/names.d.ts +1 -1
  108. package/dist/esm/reflect/names.js +1 -1
  109. package/dist/esm/reflect/nested-types.d.ts +1 -1
  110. package/dist/esm/reflect/reflect-check.d.ts +10 -1
  111. package/dist/esm/reflect/reflect-check.js +119 -2
  112. package/dist/esm/reflect/reflect-types.d.ts +4 -4
  113. package/dist/esm/reflect/reflect-types.js +1 -0
  114. package/dist/esm/reflect/reflect.d.ts +1 -1
  115. package/dist/esm/reflect/reflect.js +10 -9
  116. package/dist/esm/reflect/scalar.d.ts +3 -55
  117. package/dist/esm/reflect/scalar.js +14 -205
  118. package/dist/esm/reflect/unsafe.d.ts +1 -1
  119. package/dist/esm/reflect/unsafe.js +1 -1
  120. package/dist/esm/{reflect/registry.d.ts → registry.d.ts} +2 -2
  121. package/dist/esm/{reflect/registry.js → registry.js} +44 -26
  122. package/dist/esm/to-binary.d.ts +1 -1
  123. package/dist/esm/to-binary.js +1 -1
  124. package/dist/esm/to-json.d.ts +7 -6
  125. package/dist/esm/to-json.js +42 -29
  126. package/dist/esm/types.d.ts +1 -1
  127. package/dist/esm/wire/base64-encoding.js +2 -2
  128. package/dist/esm/wire/binary-encoding.d.ts +20 -0
  129. package/dist/esm/wire/binary-encoding.js +47 -1
  130. package/dist/esm/wire/size-delimited.d.ts +1 -1
  131. package/dist/esm/wire/text-format.d.ts +1 -2
  132. package/dist/esm/wire/text-format.js +5 -3
  133. package/dist/esm/wkt/any.d.ts +3 -3
  134. package/dist/esm/wkt/gen/google/protobuf/descriptor_pb.d.ts +6 -0
  135. package/dist/esm/wkt/gen/google/protobuf/descriptor_pb.js +1 -1
  136. package/dist/esm/wkt/wrappers.d.ts +1 -1
  137. package/package.json +1 -1
  138. package/dist/cjs/desc-types.js +0 -15
  139. package/dist/cjs/reflect/assert.d.ts +0 -16
  140. package/dist/cjs/reflect/assert.js +0 -59
  141. package/dist/esm/desc-types.js +0 -14
  142. package/dist/esm/reflect/assert.d.ts +0 -16
  143. package/dist/esm/reflect/assert.js +0 -52
@@ -12,72 +12,7 @@
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
14
  import { protoInt64 } from "../proto-int64.js";
15
- import { getTextEncoding } from "../wire/text-encoding.js";
16
- /**
17
- * Scalar value types. This is a subset of field types declared by protobuf
18
- * enum google.protobuf.FieldDescriptorProto.Type The types GROUP and MESSAGE
19
- * are omitted, but the numerical values are identical.
20
- */
21
- export var ScalarType;
22
- (function (ScalarType) {
23
- // 0 is reserved for errors.
24
- // Order is weird for historical reasons.
25
- ScalarType[ScalarType["DOUBLE"] = 1] = "DOUBLE";
26
- ScalarType[ScalarType["FLOAT"] = 2] = "FLOAT";
27
- // Not ZigZag encoded. Negative numbers take 10 bytes. Use TYPE_SINT64 if
28
- // negative values are likely.
29
- ScalarType[ScalarType["INT64"] = 3] = "INT64";
30
- ScalarType[ScalarType["UINT64"] = 4] = "UINT64";
31
- // Not ZigZag encoded. Negative numbers take 10 bytes. Use TYPE_SINT32 if
32
- // negative values are likely.
33
- ScalarType[ScalarType["INT32"] = 5] = "INT32";
34
- ScalarType[ScalarType["FIXED64"] = 6] = "FIXED64";
35
- ScalarType[ScalarType["FIXED32"] = 7] = "FIXED32";
36
- ScalarType[ScalarType["BOOL"] = 8] = "BOOL";
37
- ScalarType[ScalarType["STRING"] = 9] = "STRING";
38
- // Tag-delimited aggregate.
39
- // Group type is deprecated and not supported in proto3. However, Proto3
40
- // implementations should still be able to parse the group wire format and
41
- // treat group fields as unknown fields.
42
- // TYPE_GROUP = 10,
43
- // TYPE_MESSAGE = 11, // Length-delimited aggregate.
44
- // New in version 2.
45
- ScalarType[ScalarType["BYTES"] = 12] = "BYTES";
46
- ScalarType[ScalarType["UINT32"] = 13] = "UINT32";
47
- // TYPE_ENUM = 14,
48
- ScalarType[ScalarType["SFIXED32"] = 15] = "SFIXED32";
49
- ScalarType[ScalarType["SFIXED64"] = 16] = "SFIXED64";
50
- ScalarType[ScalarType["SINT32"] = 17] = "SINT32";
51
- ScalarType[ScalarType["SINT64"] = 18] = "SINT64";
52
- })(ScalarType || (ScalarType = {}));
53
- /**
54
- * JavaScript representation of fields with 64 bit integral types (int64, uint64,
55
- * sint64, fixed64, sfixed64).
56
- *
57
- * This is a subset of google.protobuf.FieldOptions.JSType, which defines JS_NORMAL,
58
- * JS_STRING, and JS_NUMBER. Protobuf-ES uses BigInt by default, but will use
59
- * String if `[jstype = JS_STRING]` is specified.
60
- *
61
- * ```protobuf
62
- * uint64 field_a = 1; // BigInt
63
- * uint64 field_b = 2 [jstype = JS_NORMAL]; // BigInt
64
- * uint64 field_b = 2 [jstype = JS_NUMBER]; // BigInt
65
- * uint64 field_b = 2 [jstype = JS_STRING]; // String
66
- * ```
67
- */
68
- export var LongType;
69
- (function (LongType) {
70
- /**
71
- * Use JavaScript BigInt.
72
- */
73
- LongType[LongType["BIGINT"] = 0] = "BIGINT";
74
- /**
75
- * Use JavaScript String.
76
- *
77
- * Field option `[jstype = JS_STRING]`.
78
- */
79
- LongType[LongType["STRING"] = 1] = "STRING";
80
- })(LongType || (LongType = {}));
15
+ import { ScalarType } from "../descriptors.js";
81
16
  /**
82
17
  * Returns true if both scalar values are equal.
83
18
  */
@@ -119,27 +54,27 @@ export function scalarEquals(type, a, b) {
119
54
  /**
120
55
  * Returns the zero value for the given scalar type.
121
56
  */
122
- export function scalarZeroValue(type, longType) {
57
+ export function scalarZeroValue(type, longAsString) {
123
58
  switch (type) {
59
+ case ScalarType.STRING:
60
+ return "";
124
61
  case ScalarType.BOOL:
125
62
  return false;
126
- case ScalarType.UINT64:
127
- case ScalarType.FIXED64:
128
- case ScalarType.INT64:
129
- case ScalarType.SFIXED64:
130
- case ScalarType.SINT64:
131
- return (longType == 0 ? protoInt64.zero : "0");
63
+ default:
64
+ // Handles INT32, UINT32, SINT32, FIXED32, SFIXED32.
65
+ // We do not use individual cases to save a few bytes code size.
66
+ return 0;
132
67
  case ScalarType.DOUBLE:
133
68
  case ScalarType.FLOAT:
134
69
  return 0.0;
70
+ case ScalarType.INT64:
71
+ case ScalarType.UINT64:
72
+ case ScalarType.SFIXED64:
73
+ case ScalarType.FIXED64:
74
+ case ScalarType.SINT64:
75
+ return (longAsString ? "0" : protoInt64.zero);
135
76
  case ScalarType.BYTES:
136
77
  return new Uint8Array(0);
137
- case ScalarType.STRING:
138
- return "";
139
- default:
140
- // Handles INT32, UINT32, SINT32, FIXED32, SFIXED32.
141
- // We do not use individual cases to save a few bytes code size.
142
- return 0;
143
78
  }
144
79
  }
145
80
  /**
@@ -161,129 +96,3 @@ export function isScalarZeroValue(type, value) {
161
96
  return value == 0; // Loose comparison matches 0n, 0 and "0"
162
97
  }
163
98
  }
164
- const FLOAT32_MAX = 3.4028234663852886e38, FLOAT32_MIN = -3.4028234663852886e38, UINT32_MAX = 0xffffffff, INT32_MAX = 0x7fffffff, INT32_MIN = -0x80000000;
165
- // TODO this checks UTF-8 validity
166
- export function checkScalarValue(value, scalar) {
167
- switch (scalar) {
168
- case ScalarType.DOUBLE:
169
- return typeof value == "number";
170
- case ScalarType.FLOAT:
171
- if (typeof value != "number") {
172
- return false;
173
- }
174
- if (Number.isNaN(value) || !Number.isFinite(value)) {
175
- return true;
176
- }
177
- if (value > FLOAT32_MAX || value < FLOAT32_MIN) {
178
- return `${value.toFixed()} out of range`;
179
- }
180
- return true;
181
- case ScalarType.INT32:
182
- case ScalarType.SFIXED32:
183
- case ScalarType.SINT32:
184
- // signed
185
- if (typeof value !== "number" || !Number.isInteger(value)) {
186
- return false;
187
- }
188
- if (value > INT32_MAX || value < INT32_MIN) {
189
- return `${value.toFixed()} out of range`;
190
- }
191
- return true;
192
- case ScalarType.FIXED32:
193
- case ScalarType.UINT32:
194
- // unsigned
195
- if (typeof value !== "number" || !Number.isInteger(value)) {
196
- return false;
197
- }
198
- if (value > UINT32_MAX || value < 0) {
199
- return `${value.toFixed()} out of range`;
200
- }
201
- return true;
202
- case ScalarType.BOOL:
203
- return typeof value == "boolean";
204
- case ScalarType.STRING:
205
- if (typeof value != "string") {
206
- return false;
207
- }
208
- return getTextEncoding().checkUtf8(value) || "invalid UTF8";
209
- case ScalarType.BYTES:
210
- return value instanceof Uint8Array;
211
- case ScalarType.INT64:
212
- case ScalarType.SFIXED64:
213
- case ScalarType.SINT64:
214
- // signed
215
- if (typeof value != "string" &&
216
- typeof value !== "bigint" &&
217
- typeof value !== "number") {
218
- return false;
219
- }
220
- try {
221
- protoInt64.parse(value);
222
- }
223
- catch (e) {
224
- return `${value} out of range`;
225
- }
226
- return true;
227
- case ScalarType.FIXED64:
228
- case ScalarType.UINT64:
229
- // unsigned
230
- if (typeof value != "string" &&
231
- typeof value !== "bigint" &&
232
- typeof value !== "number") {
233
- return false;
234
- }
235
- try {
236
- protoInt64.uParse(value);
237
- }
238
- catch (e) {
239
- return `${value} out of range`;
240
- }
241
- return true;
242
- }
243
- }
244
- export function scalarTypeDescription(scalar) {
245
- switch (scalar) {
246
- case ScalarType.STRING:
247
- return "string";
248
- case ScalarType.BOOL:
249
- return "boolean";
250
- case ScalarType.INT64:
251
- case ScalarType.SINT64:
252
- case ScalarType.SFIXED64:
253
- return "bigint (int64)";
254
- case ScalarType.UINT64:
255
- case ScalarType.FIXED64:
256
- return "bigint (uint64)";
257
- case ScalarType.BYTES:
258
- return "Uint8Array";
259
- case ScalarType.DOUBLE:
260
- return "number (float64)";
261
- case ScalarType.FLOAT:
262
- return "number (float32)";
263
- case ScalarType.FIXED32:
264
- case ScalarType.UINT32:
265
- return "number (uint32)";
266
- case ScalarType.INT32:
267
- case ScalarType.SFIXED32:
268
- case ScalarType.SINT32:
269
- return "number (int32)";
270
- }
271
- }
272
- export function scalarTypeScriptType(scalar, long) {
273
- switch (scalar) {
274
- case ScalarType.STRING:
275
- return "string";
276
- case ScalarType.BOOL:
277
- return "boolean";
278
- case ScalarType.UINT64:
279
- case ScalarType.SFIXED64:
280
- case ScalarType.FIXED64:
281
- case ScalarType.SINT64:
282
- case ScalarType.INT64:
283
- return long == LongType.STRING ? "string" : "bigint";
284
- case ScalarType.BYTES:
285
- return "Uint8Array";
286
- default:
287
- return "number";
288
- }
289
- }
@@ -1,4 +1,4 @@
1
- import type { DescField, DescOneof } from "../desc-types.js";
1
+ import type { DescField, DescOneof } from "../descriptors.js";
2
2
  export declare const unsafeLocal: unique symbol;
3
3
  /**
4
4
  * Return the selected field of a oneof group.
@@ -132,7 +132,7 @@ field) {
132
132
  target[name] = field.enum.values[0].number;
133
133
  break;
134
134
  case "scalar":
135
- target[name] = scalarZeroValue(field.scalar, field.longType);
135
+ target[name] = scalarZeroValue(field.scalar, field.longAsString);
136
136
  break;
137
137
  }
138
138
  }
@@ -1,5 +1,5 @@
1
- import type { FileDescriptorProto, FileDescriptorSet } from "../wkt/gen/google/protobuf/descriptor_pb.js";
2
- import type { DescEnum, DescExtension, DescFile, DescMessage, DescService, SupportedEdition } from "../desc-types.js";
1
+ import type { FileDescriptorProto, FileDescriptorSet } from "./wkt/gen/google/protobuf/descriptor_pb.js";
2
+ import { type DescEnum, type DescExtension, type DescFile, type DescMessage, type DescService, type SupportedEdition } from "./descriptors.js";
3
3
  /**
4
4
  * A set of descriptors for messages, enumerations, extensions,
5
5
  * and services.
@@ -11,12 +11,11 @@
11
11
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
- import { assert } from "./assert.js";
15
- import { parseTextFormatEnumValue, parseTextFormatScalarValue, } from "../wire/text-format.js";
16
- import { LongType, ScalarType } from "./scalar.js";
17
- import { nestedTypes } from "./nested-types.js";
18
- import { unsafeIsSetExplicit } from "./unsafe.js";
19
- import { protoCamelCase, safeObjectProperty } from "./names.js";
14
+ import { ScalarType, } from "./descriptors.js";
15
+ import { parseTextFormatEnumValue, parseTextFormatScalarValue, } from "./wire/text-format.js";
16
+ import { nestedTypes } from "./reflect/nested-types.js";
17
+ import { unsafeIsSetExplicit } from "./reflect/unsafe.js";
18
+ import { protoCamelCase, safeObjectProperty } from "./reflect/names.js";
20
19
  /**
21
20
  * Create a registry from the given inputs.
22
21
  *
@@ -221,7 +220,7 @@ const DELIMITED = 2;
221
220
  const OPEN = 1;
222
221
  // prettier-ignore
223
222
  // bootstrap-inject defaults: EDITION_PROTO2 to EDITION_2023: export const minimumEdition: SupportedEdition = $minimumEdition, maximumEdition: SupportedEdition = $maximumEdition;
224
- // generated from protoc v27.0-rc2
223
+ // generated from protoc v27.0
225
224
  export const minimumEdition = 998, maximumEdition = 1000;
226
225
  const featureDefaults = {
227
226
  // EDITION_PROTO2
@@ -455,6 +454,7 @@ function addService(proto, file, reg) {
455
454
  name: proto.name,
456
455
  typeName: makeTypeName(proto, undefined, file),
457
456
  methods: [],
457
+ method: {},
458
458
  toString() {
459
459
  return `service ${this.typeName}`;
460
460
  },
@@ -462,7 +462,9 @@ function addService(proto, file, reg) {
462
462
  file.services.push(desc);
463
463
  reg.add(desc);
464
464
  for (const methodProto of proto.method) {
465
- desc.methods.push(newMethod(methodProto, desc, reg));
465
+ const method = newMethod(methodProto, desc, reg);
466
+ desc.methods.push(method);
467
+ desc.method[method.localName] = method;
466
468
  }
467
469
  }
468
470
  /**
@@ -541,7 +543,7 @@ function newField(proto, parentOrFile, reg, oneof, mapEntries) {
541
543
  mapKey: undefined,
542
544
  delimitedEncoding: undefined,
543
545
  packed: undefined,
544
- longType: undefined,
546
+ longAsString: false,
545
547
  getDefaultValue: undefined,
546
548
  };
547
549
  if (isExtension) {
@@ -583,21 +585,13 @@ function newField(proto, parentOrFile, reg, oneof, mapEntries) {
583
585
  if (mapEntry) {
584
586
  // map field
585
587
  field.fieldKind = "map";
586
- const keyField = mapEntry.fields.find((f) => f.number === 1);
587
- assert(keyField);
588
- assert(keyField.fieldKind == "scalar");
589
- assert(keyField.scalar != ScalarType.BYTES &&
590
- keyField.scalar != ScalarType.FLOAT &&
591
- keyField.scalar != ScalarType.DOUBLE);
592
- const valueField = mapEntry.fields.find((f) => f.number === 2);
593
- assert(valueField);
594
- assert(valueField.fieldKind != "list" && valueField.fieldKind != "map");
595
- field.mapKey = keyField.scalar;
596
- field.mapKind = valueField.fieldKind;
597
- field.message = valueField.message;
588
+ const { key, value } = findMapEntryFields(mapEntry);
589
+ field.mapKey = key.scalar;
590
+ field.mapKind = value.fieldKind;
591
+ field.message = value.message;
598
592
  field.delimitedEncoding = false; // map fields are always LENGTH_PREFIXED
599
- field.enum = valueField.enum;
600
- field.scalar = valueField.scalar;
593
+ field.enum = value.enum;
594
+ field.scalar = value.scalar;
601
595
  return field;
602
596
  }
603
597
  // list field
@@ -618,8 +612,7 @@ function newField(proto, parentOrFile, reg, oneof, mapEntries) {
618
612
  default:
619
613
  field.listKind = "scalar";
620
614
  field.scalar = type;
621
- field.longType =
622
- jstype == JS_STRING ? LongType.STRING : LongType.BIGINT;
615
+ field.longAsString = jstype == JS_STRING;
623
616
  break;
624
617
  }
625
618
  field.packed = isPackedField(proto, parentOrFile);
@@ -650,7 +643,7 @@ function newField(proto, parentOrFile, reg, oneof, mapEntries) {
650
643
  default: {
651
644
  field.fieldKind = "scalar";
652
645
  field.scalar = type;
653
- field.longType = jstype == JS_STRING ? LongType.STRING : LongType.BIGINT;
646
+ field.longAsString = jstype == JS_STRING;
654
647
  field.getDefaultValue = () => {
655
648
  return unsafeIsSetExplicit(proto, "defaultValue")
656
649
  ? parseTextFormatScalarValue(type, proto.defaultValue)
@@ -825,6 +818,22 @@ function isPackedField(proto, parent) {
825
818
  parent,
826
819
  }));
827
820
  }
821
+ /**
822
+ * Find the key and value fields of a synthetic map entry message.
823
+ */
824
+ function findMapEntryFields(mapEntry) {
825
+ const key = mapEntry.fields.find((f) => f.number === 1);
826
+ const value = mapEntry.fields.find((f) => f.number === 2);
827
+ assert(key &&
828
+ key.fieldKind == "scalar" &&
829
+ key.scalar != ScalarType.BYTES &&
830
+ key.scalar != ScalarType.FLOAT &&
831
+ key.scalar != ScalarType.DOUBLE &&
832
+ value &&
833
+ value.fieldKind != "list" &&
834
+ value.fieldKind != "map");
835
+ return { key, value };
836
+ }
828
837
  /**
829
838
  * Enumerations can be open or closed.
830
839
  * See https://protobuf.dev/programming-guides/enum/
@@ -872,3 +881,12 @@ function resolveFeature(name, ref) {
872
881
  }
873
882
  return resolveFeature(name, ref.parent);
874
883
  }
884
+ /**
885
+ * Assert that condition is truthy or throw error (with message)
886
+ */
887
+ function assert(condition, msg) {
888
+ // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions -- we want the implicit conversion to boolean
889
+ if (!condition) {
890
+ throw new Error(msg);
891
+ }
892
+ }
@@ -1,6 +1,6 @@
1
1
  import type { MessageShape } from "./types.js";
2
2
  import { BinaryWriter } from "./wire/binary-encoding.js";
3
- import type { DescField, DescMessage } from "./desc-types.js";
3
+ import { type DescField, type DescMessage } from "./descriptors.js";
4
4
  import type { ReflectMessage } from "./reflect/index.js";
5
5
  /**
6
6
  * Options for serializing to binary data.
@@ -13,7 +13,7 @@
13
13
  // limitations under the License.
14
14
  import { reflect } from "./reflect/reflect.js";
15
15
  import { BinaryWriter, WireType } from "./wire/binary-encoding.js";
16
- import { ScalarType } from "./reflect/scalar.js";
16
+ import { ScalarType } from "./descriptors.js";
17
17
  // bootstrap-inject google.protobuf.FeatureSet.FieldPresence.LEGACY_REQUIRED: const $name: FeatureSet_FieldPresence.$localName = $number;
18
18
  const LEGACY_REQUIRED = 3;
19
19
  // Default options for serializing binary data.
@@ -1,17 +1,18 @@
1
- import type { DescMessage } from "./desc-types.js";
1
+ import { type DescMessage } from "./descriptors.js";
2
2
  import type { JsonValue } from "./json-value.js";
3
- import type { Registry } from "./reflect/registry.js";
3
+ import type { Registry } from "./registry.js";
4
4
  import type { MessageShape } from "./types.js";
5
5
  /**
6
6
  * Options for serializing to JSON.
7
7
  */
8
8
  export interface JsonWriteOptions {
9
9
  /**
10
- * Emit fields with default values: Fields with default values are omitted
11
- * by default in proto3 JSON output. This option overrides this behavior
12
- * and outputs fields with their default values.
10
+ * By default, fields with implicit presence are not serialized if they are
11
+ * unset. For example, an empty list field or a proto3 int32 field with 0 is
12
+ * not serialized. With this option enabled, such fields are included in the
13
+ * output.
13
14
  */
14
- emitDefaultValues: boolean;
15
+ alwaysEmitImplicit: boolean;
15
16
  /**
16
17
  * Emit enum values as integers instead of strings: The name of an enum
17
18
  * value is used by default in JSON output. An option may be provided to
@@ -11,21 +11,22 @@
11
11
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
  // See the License for the specific language governing permissions and
13
13
  // limitations under the License.
14
- import { assert } from "./reflect/assert.js";
14
+ import { ScalarType, } from "./descriptors.js";
15
15
  import { protoCamelCase } from "./reflect/names.js";
16
16
  import { reflect } from "./reflect/reflect.js";
17
- import { ScalarType } from "./reflect/scalar.js";
18
17
  import { anyUnpack } from "./wkt/index.js";
19
18
  import { isWrapperDesc } from "./wkt/wrappers.js";
20
19
  import { base64Encode } from "./wire/index.js";
21
20
  import { createExtensionContainer, getExtension } from "./extensions.js";
21
+ import { checkField, formatVal } from "./reflect/reflect-check.js";
22
+ /* eslint-disable @typescript-eslint/restrict-template-expressions */
22
23
  // bootstrap-inject google.protobuf.FeatureSet.FieldPresence.LEGACY_REQUIRED: const $name: FeatureSet_FieldPresence.$localName = $number;
23
24
  const LEGACY_REQUIRED = 3;
24
25
  // bootstrap-inject google.protobuf.FeatureSet.FieldPresence.IMPLICIT: const $name: FeatureSet_FieldPresence.$localName = $number;
25
26
  const IMPLICIT = 2;
26
27
  // Default options for serializing to JSON.
27
28
  const jsonWriteDefaults = {
28
- emitDefaultValues: false,
29
+ alwaysEmitImplicit: false,
29
30
  enumAsInteger: false,
30
31
  useProtoFieldName: false,
31
32
  };
@@ -56,9 +57,9 @@ function reflectToJson(msg, opts) {
56
57
  for (const f of msg.sortedFields) {
57
58
  if (!msg.isSet(f)) {
58
59
  if (f.presence == LEGACY_REQUIRED) {
59
- throw new Error(`cannot encode field ${msg.desc.typeName}.${f.name} to binary: required field not set`);
60
+ throw new Error(`cannot encode field ${msg.desc.typeName}.${f.name} to JSON: required field not set`);
60
61
  }
61
- if (!opts.emitDefaultValues || f.presence !== IMPLICIT) {
62
+ if (!opts.alwaysEmitImplicit || f.presence !== IMPLICIT) {
62
63
  // Fields with implicit presence omit zero values (e.g. empty string) by default
63
64
  continue;
64
65
  }
@@ -93,7 +94,7 @@ function reflectToJson(msg, opts) {
93
94
  function fieldToJson(f, val, opts) {
94
95
  switch (f.fieldKind) {
95
96
  case "scalar":
96
- return scalarToJson(f.scalar, val);
97
+ return scalarToJson(f, val);
97
98
  case "message":
98
99
  return reflectToJson(val, opts);
99
100
  case "enum":
@@ -110,7 +111,7 @@ function mapToJson(map, opts) {
110
111
  switch (f.mapKind) {
111
112
  case "scalar":
112
113
  for (const [entryKey, entryValue] of map) {
113
- jsonObj[entryKey.toString()] = scalarToJson(f.scalar, entryValue); // JSON standard allows only (double quoted) string as property key
114
+ jsonObj[entryKey.toString()] = scalarToJson(f, entryValue); // JSON standard allows only (double quoted) string as property key
114
115
  }
115
116
  break;
116
117
  case "message":
@@ -126,7 +127,7 @@ function mapToJson(map, opts) {
126
127
  }
127
128
  break;
128
129
  }
129
- return opts.emitDefaultValues || map.size > 0 ? jsonObj : undefined;
130
+ return opts.alwaysEmitImplicit || map.size > 0 ? jsonObj : undefined;
130
131
  }
131
132
  function listToJson(list, opts) {
132
133
  const f = list.field();
@@ -134,7 +135,7 @@ function listToJson(list, opts) {
134
135
  switch (f.listKind) {
135
136
  case "scalar":
136
137
  for (const item of list) {
137
- jsonArr.push(scalarToJson(f.scalar, item));
138
+ jsonArr.push(scalarToJson(f, item));
138
139
  }
139
140
  break;
140
141
  case "enum":
@@ -148,11 +149,13 @@ function listToJson(list, opts) {
148
149
  }
149
150
  break;
150
151
  }
151
- return opts.emitDefaultValues || jsonArr.length > 0 ? jsonArr : undefined;
152
+ return opts.alwaysEmitImplicit || jsonArr.length > 0 ? jsonArr : undefined;
152
153
  }
153
154
  function enumToJson(desc, value, enumAsInteger) {
154
155
  var _a;
155
- assert(typeof value == "number");
156
+ if (typeof value != "number") {
157
+ throw new Error(`cannot encode ${desc} to JSON: expected number, got ${formatVal(value)}`);
158
+ }
156
159
  if (desc.typeName == "google.protobuf.NullValue") {
157
160
  return null;
158
161
  }
@@ -162,23 +165,27 @@ function enumToJson(desc, value, enumAsInteger) {
162
165
  const val = desc.values.find((v) => v.number == value);
163
166
  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
164
167
  }
165
- function scalarToJson(type, value) {
166
- switch (type) {
168
+ function scalarToJson(field, value) {
169
+ var _a, _b, _c, _d, _e, _f;
170
+ switch (field.scalar) {
167
171
  // int32, fixed32, uint32: JSON value will be a decimal number. Either numbers or strings are accepted.
168
172
  case ScalarType.INT32:
169
173
  case ScalarType.SFIXED32:
170
174
  case ScalarType.SINT32:
171
175
  case ScalarType.FIXED32:
172
176
  case ScalarType.UINT32:
173
- assert(typeof value == "number");
177
+ if (typeof value != "number") {
178
+ throw new Error(`cannot encode ${field} to JSON: ${(_a = checkField(field, value)) === null || _a === void 0 ? void 0 : _a.message}`);
179
+ }
174
180
  return value;
175
181
  // float, double: JSON value will be a number or one of the special string values "NaN", "Infinity", and "-Infinity".
176
182
  // Either numbers or strings are accepted. Exponent notation is also accepted.
177
183
  case ScalarType.FLOAT:
178
- // assertFloat32(value);
179
184
  case ScalarType.DOUBLE: // eslint-disable-line no-fallthrough
180
- assert(typeof value == "number");
181
- if (Number.isNaN(value))
185
+ if (typeof value != "number") {
186
+ throw new Error(`cannot encode ${field} to JSON: ${(_b = checkField(field, value)) === null || _b === void 0 ? void 0 : _b.message}`);
187
+ }
188
+ if (isNaN(value))
182
189
  return "NaN";
183
190
  if (value === Number.POSITIVE_INFINITY)
184
191
  return "Infinity";
@@ -187,11 +194,15 @@ function scalarToJson(type, value) {
187
194
  return value;
188
195
  // string:
189
196
  case ScalarType.STRING:
190
- assert(typeof value == "string");
197
+ if (typeof value != "string") {
198
+ throw new Error(`cannot encode ${field} to JSON: ${(_c = checkField(field, value)) === null || _c === void 0 ? void 0 : _c.message}`);
199
+ }
191
200
  return value;
192
201
  // bool:
193
202
  case ScalarType.BOOL:
194
- assert(typeof value == "boolean");
203
+ if (typeof value != "boolean") {
204
+ throw new Error(`cannot encode ${field} to JSON: ${(_d = checkField(field, value)) === null || _d === void 0 ? void 0 : _d.message}`);
205
+ }
195
206
  return value;
196
207
  // JSON value will be a decimal string. Either numbers or strings are accepted.
197
208
  case ScalarType.UINT64:
@@ -199,15 +210,17 @@ function scalarToJson(type, value) {
199
210
  case ScalarType.INT64:
200
211
  case ScalarType.SFIXED64:
201
212
  case ScalarType.SINT64:
202
- assert(typeof value == "bigint" ||
203
- typeof value == "string" ||
204
- typeof value == "number");
213
+ if (typeof value != "bigint" && typeof value != "string") {
214
+ throw new Error(`cannot encode ${field} to JSON: ${(_e = checkField(field, value)) === null || _e === void 0 ? void 0 : _e.message}`);
215
+ }
205
216
  return value.toString();
206
217
  // bytes: JSON value will be the data encoded as a string using standard base64 encoding with paddings.
207
218
  // Either standard or URL-safe base64 encoding with/without paddings are accepted.
208
219
  case ScalarType.BYTES:
209
- assert(value instanceof Uint8Array);
210
- return base64Encode(value);
220
+ if (value instanceof Uint8Array) {
221
+ return base64Encode(value);
222
+ }
223
+ throw new Error(`cannot encode ${field} to JSON: ${(_f = checkField(field, value)) === null || _f === void 0 ? void 0 : _f.message}`);
211
224
  }
212
225
  }
213
226
  function jsonName(f, opts) {
@@ -236,7 +249,7 @@ function tryWktToJson(msg, opts) {
236
249
  default:
237
250
  if (isWrapperDesc(msg.desc)) {
238
251
  const valueField = msg.desc.fields[0];
239
- return scalarToJson(valueField.scalar, msg.get(valueField));
252
+ return scalarToJson(valueField, msg.get(valueField));
240
253
  }
241
254
  return undefined;
242
255
  }
@@ -270,7 +283,7 @@ function anyToJson(val, opts) {
270
283
  function durationToJson(val) {
271
284
  if (Number(val.seconds) > 315576000000 ||
272
285
  Number(val.seconds) < -315576000000) {
273
- throw new Error(`cannot encode ${val.$typeName} to JSON: value out of range`);
286
+ throw new Error(`cannot encode message ${val.$typeName} to JSON: value out of range`);
274
287
  }
275
288
  let text = val.seconds.toString();
276
289
  if (val.nanos !== 0) {
@@ -294,7 +307,7 @@ function fieldMaskToJson(val) {
294
307
  .map((p) => {
295
308
  // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
296
309
  if (p.match(/_[0-9]?_/g) || p.match(/[A-Z]/g)) {
297
- throw new Error(`cannot encode ${val.$typeName} to JSON: lowerCamelCase of path name "` +
310
+ throw new Error(`cannot encode message ${val.$typeName} to JSON: lowerCamelCase of path name "` +
298
311
  p +
299
312
  '" is irreversible');
300
313
  }
@@ -337,10 +350,10 @@ function timestampToJson(val) {
337
350
  const ms = Number(val.seconds) * 1000;
338
351
  if (ms < Date.parse("0001-01-01T00:00:00Z") ||
339
352
  ms > Date.parse("9999-12-31T23:59:59Z")) {
340
- throw new Error(`cannot encode ${val.$typeName} to JSON: must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive`);
353
+ throw new Error(`cannot encode message ${val.$typeName} to JSON: must be from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive`);
341
354
  }
342
355
  if (val.nanos < 0) {
343
- throw new Error(`cannot encode ${val.$typeName} to JSON: nanos must not be negative`);
356
+ throw new Error(`cannot encode message ${val.$typeName} to JSON: nanos must not be negative`);
344
357
  }
345
358
  let z = "Z";
346
359
  if (val.nanos > 0) {
@@ -1,5 +1,5 @@
1
1
  import type { GenDescEnum, GenDescExtension, GenDescMessage } from "./codegenv1/types.js";
2
- import type { DescEnum, DescExtension, DescMessage } from "./desc-types.js";
2
+ import type { DescEnum, DescExtension, DescMessage } from "./descriptors.js";
3
3
  import type { OneofADT } from "./reflect/guard.js";
4
4
  import type { WireType } from "./wire/index.js";
5
5
  /**