@bufbuild/protobuf 2.2.5 → 2.4.0

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 (88) hide show
  1. package/dist/cjs/clone.js +4 -6
  2. package/dist/cjs/codegenv1/boot.js +1 -1
  3. package/dist/cjs/codegenv1/embed.js +0 -1
  4. package/dist/cjs/codegenv1/file.js +1 -1
  5. package/dist/cjs/codegenv1/symbols.js +4 -1
  6. package/dist/cjs/create.js +0 -1
  7. package/dist/cjs/equals.js +2 -1
  8. package/dist/cjs/extensions.js +1 -1
  9. package/dist/cjs/from-binary.js +5 -3
  10. package/dist/cjs/from-json.js +8 -10
  11. package/dist/cjs/proto-int64.js +4 -1
  12. package/dist/cjs/reflect/guard.js +2 -1
  13. package/dist/cjs/reflect/index.d.ts +1 -0
  14. package/dist/cjs/reflect/index.js +1 -0
  15. package/dist/cjs/reflect/names.d.ts +8 -0
  16. package/dist/cjs/reflect/names.js +28 -0
  17. package/dist/cjs/reflect/nested-types.d.ts +23 -0
  18. package/dist/cjs/reflect/nested-types.js +40 -0
  19. package/dist/cjs/reflect/path.d.ts +107 -0
  20. package/dist/cjs/reflect/path.js +376 -0
  21. package/dist/cjs/reflect/reflect-check.js +3 -3
  22. package/dist/cjs/reflect/reflect-types.d.ts +1 -1
  23. package/dist/cjs/reflect/reflect.js +9 -7
  24. package/dist/cjs/reflect/scalar.js +4 -5
  25. package/dist/cjs/reflect/unsafe.d.ts +3 -6
  26. package/dist/cjs/reflect/unsafe.js +11 -10
  27. package/dist/cjs/registry.js +24 -30
  28. package/dist/cjs/to-binary.js +1 -1
  29. package/dist/cjs/to-json.d.ts +6 -0
  30. package/dist/cjs/to-json.js +16 -18
  31. package/dist/cjs/wire/base64-encoding.js +4 -3
  32. package/dist/cjs/wire/binary-encoding.js +7 -6
  33. package/dist/cjs/wire/text-encoding.js +1 -1
  34. package/dist/cjs/wire/text-format.js +3 -4
  35. package/dist/cjs/wire/varint.js +0 -1
  36. package/dist/cjs/wkt/gen/google/protobuf/cpp_features_pb.d.ts +89 -0
  37. package/dist/cjs/wkt/gen/google/protobuf/cpp_features_pb.js +60 -0
  38. package/dist/cjs/wkt/gen/google/protobuf/go_features_pb.d.ts +122 -0
  39. package/dist/cjs/wkt/gen/google/protobuf/go_features_pb.js +90 -0
  40. package/dist/cjs/wkt/gen/google/protobuf/java_features_pb.d.ts +113 -0
  41. package/dist/cjs/wkt/gen/google/protobuf/java_features_pb.js +67 -0
  42. package/dist/cjs/wkt/index.d.ts +4 -1
  43. package/dist/cjs/wkt/index.js +4 -1
  44. package/dist/esm/clone.js +4 -6
  45. package/dist/esm/codegenv1/boot.js +1 -1
  46. package/dist/esm/codegenv1/embed.js +0 -1
  47. package/dist/esm/codegenv1/file.js +1 -1
  48. package/dist/esm/codegenv1/symbols.js +4 -1
  49. package/dist/esm/create.js +0 -2
  50. package/dist/esm/equals.js +2 -1
  51. package/dist/esm/extensions.js +1 -1
  52. package/dist/esm/from-binary.js +5 -3
  53. package/dist/esm/from-json.js +8 -10
  54. package/dist/esm/proto-int64.js +4 -1
  55. package/dist/esm/reflect/guard.js +2 -1
  56. package/dist/esm/reflect/index.d.ts +1 -0
  57. package/dist/esm/reflect/index.js +1 -0
  58. package/dist/esm/reflect/names.d.ts +8 -0
  59. package/dist/esm/reflect/names.js +27 -0
  60. package/dist/esm/reflect/nested-types.d.ts +23 -0
  61. package/dist/esm/reflect/nested-types.js +39 -0
  62. package/dist/esm/reflect/path.d.ts +107 -0
  63. package/dist/esm/reflect/path.js +369 -0
  64. package/dist/esm/reflect/reflect-check.js +3 -3
  65. package/dist/esm/reflect/reflect-types.d.ts +1 -1
  66. package/dist/esm/reflect/reflect-types.js +0 -1
  67. package/dist/esm/reflect/reflect.js +9 -7
  68. package/dist/esm/reflect/scalar.js +4 -5
  69. package/dist/esm/reflect/unsafe.d.ts +3 -6
  70. package/dist/esm/reflect/unsafe.js +11 -10
  71. package/dist/esm/registry.js +24 -30
  72. package/dist/esm/to-binary.js +1 -1
  73. package/dist/esm/to-json.d.ts +6 -0
  74. package/dist/esm/to-json.js +16 -18
  75. package/dist/esm/wire/base64-encoding.js +4 -3
  76. package/dist/esm/wire/binary-encoding.js +7 -6
  77. package/dist/esm/wire/text-encoding.js +1 -1
  78. package/dist/esm/wire/text-format.js +3 -4
  79. package/dist/esm/wire/varint.js +0 -1
  80. package/dist/esm/wkt/gen/google/protobuf/cpp_features_pb.d.ts +89 -0
  81. package/dist/esm/wkt/gen/google/protobuf/cpp_features_pb.js +57 -0
  82. package/dist/esm/wkt/gen/google/protobuf/go_features_pb.d.ts +122 -0
  83. package/dist/esm/wkt/gen/google/protobuf/go_features_pb.js +87 -0
  84. package/dist/esm/wkt/gen/google/protobuf/java_features_pb.d.ts +113 -0
  85. package/dist/esm/wkt/gen/google/protobuf/java_features_pb.js +64 -0
  86. package/dist/esm/wkt/index.d.ts +4 -1
  87. package/dist/esm/wkt/index.js +4 -1
  88. package/package.json +9 -24
@@ -11,7 +11,6 @@
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
- /* eslint-disable no-case-declarations,@typescript-eslint/restrict-template-expressions */
15
14
  import { ScalarType, } from "./descriptors.js";
16
15
  import { protoInt64 } from "./proto-int64.js";
17
16
  import { create } from "./create.js";
@@ -96,7 +95,7 @@ export function mergeFromJson(schema, target, json, options) {
96
95
  export function enumFromJson(descEnum, json) {
97
96
  const val = readEnum(descEnum, json, false, false);
98
97
  if (val === tokenIgnoredUnknownEnum) {
99
- throw new Error(`cannot decode ${String(descEnum)} from JSON: ${formatVal(json)}`);
98
+ throw new Error(`cannot decode ${descEnum} from JSON: ${formatVal(json)}`);
100
99
  }
101
100
  return val;
102
101
  }
@@ -139,6 +138,7 @@ function readMessage(msg, json, opts) {
139
138
  let extension = undefined;
140
139
  if (jsonKey.startsWith("[") &&
141
140
  jsonKey.endsWith("]") &&
141
+ // biome-ignore lint/suspicious/noAssignInExpressions: no
142
142
  (extension = (_a = opts.registry) === null || _a === void 0 ? void 0 : _a.getExtension(jsonKey.substring(1, jsonKey.length - 1))) &&
143
143
  extension.extendee.typeName === msg.desc.typeName) {
144
144
  const [container, field, get] = createExtensionContainer(extension);
@@ -268,7 +268,6 @@ function readEnum(desc, json, ignoreUnknownFields, nullAsZeroValue) {
268
268
  }
269
269
  return nullAsZeroValue ? desc.values[0].number : tokenNull;
270
270
  }
271
- // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
272
271
  switch (typeof json) {
273
272
  case "number":
274
273
  if (Number.isInteger(json)) {
@@ -297,7 +296,6 @@ function scalarFromJson(field, json, nullAsZeroValue) {
297
296
  }
298
297
  // int64, sfixed64, sint64, fixed64, uint64: Reflect supports string and number.
299
298
  // string, bool: Supported by reflect.
300
- // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
301
299
  switch (field.scalar) {
302
300
  // float, double: JSON value will be a number or one of the special string values "NaN", "Infinity", and "-Infinity".
303
301
  // Either numbers or strings are accepted. Exponent notation is also accepted.
@@ -310,11 +308,11 @@ function scalarFromJson(field, json, nullAsZeroValue) {
310
308
  if (json === "-Infinity")
311
309
  return Number.NEGATIVE_INFINITY;
312
310
  if (typeof json == "number") {
313
- if (isNaN(json)) {
311
+ if (Number.isNaN(json)) {
314
312
  // NaN must be encoded with string constants
315
313
  throw new FieldError(field, "unexpected NaN number");
316
314
  }
317
- if (!isFinite(json)) {
315
+ if (!Number.isFinite(json)) {
318
316
  // Infinity must be encoded with string constants
319
317
  throw new FieldError(field, "unexpected infinite number");
320
318
  }
@@ -330,7 +328,7 @@ function scalarFromJson(field, json, nullAsZeroValue) {
330
328
  break;
331
329
  }
332
330
  const float = Number(json);
333
- if (!isFinite(float)) {
331
+ if (!Number.isFinite(float)) {
334
332
  // Infinity and NaN must be encoded with string constants
335
333
  break;
336
334
  }
@@ -371,7 +369,6 @@ function scalarFromJson(field, json, nullAsZeroValue) {
371
369
  function mapKeyFromJson(type, json) {
372
370
  switch (type) {
373
371
  case ScalarType.BOOL:
374
- // eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
375
372
  switch (json) {
376
373
  case "true":
377
374
  return true;
@@ -489,11 +486,12 @@ function anyFromJson(any, json, opts) {
489
486
  const msg = reflect(desc);
490
487
  if (typeName.startsWith("google.protobuf.") &&
491
488
  Object.prototype.hasOwnProperty.call(json, "value")) {
492
- const value = json["value"];
489
+ const value = json.value;
493
490
  readMessage(msg, value, opts);
494
491
  }
495
492
  else {
496
493
  const copy = Object.assign({}, json);
494
+ // biome-ignore lint/performance/noDelete: <explanation>
497
495
  delete copy["@type"];
498
496
  readMessage(msg, copy, opts);
499
497
  }
@@ -508,7 +506,7 @@ function timestampFromJson(timestamp, json) {
508
506
  throw new Error(`cannot decode message ${timestamp.$typeName} from JSON: invalid RFC 3339 string`);
509
507
  }
510
508
  const ms = Date.parse(
511
- //prettier-ignore
509
+ // biome-ignore format: want this to read well
512
510
  matches[1] + "-" + matches[2] + "-" + matches[3] + "T" + matches[4] + ":" + matches[5] + ":" + matches[6] + (matches[8] ? matches[8] : "Z"));
513
511
  if (Number.isNaN(ms)) {
514
512
  throw new Error(`cannot decode message ${timestamp.$typeName} from JSON: invalid RFC 3339 string`);
@@ -28,7 +28,10 @@ function makeInt64Support() {
28
28
  typeof process.env != "object" ||
29
29
  process.env.BUF_BIGINT_DISABLE !== "1");
30
30
  if (ok) {
31
- const MIN = BigInt("-9223372036854775808"), MAX = BigInt("9223372036854775807"), UMIN = BigInt("0"), UMAX = BigInt("18446744073709551615");
31
+ const MIN = BigInt("-9223372036854775808");
32
+ const MAX = BigInt("9223372036854775807");
33
+ const UMIN = BigInt("0");
34
+ const UMAX = BigInt("18446744073709551615");
32
35
  return {
33
36
  zero: BigInt(0),
34
37
  supported: true,
@@ -31,7 +31,8 @@ export function isReflectList(arg, field) {
31
31
  "field" in arg &&
32
32
  typeof arg.field == "function") {
33
33
  if (field !== undefined) {
34
- const a = field, b = arg.field();
34
+ const a = field;
35
+ const b = arg.field();
35
36
  return (a.listKind == b.listKind &&
36
37
  a.scalar === b.scalar &&
37
38
  ((_a = a.message) === null || _a === void 0 ? void 0 : _a.typeName) === ((_b = b.message) === null || _b === void 0 ? void 0 : _b.typeName) &&
@@ -4,4 +4,5 @@ export * from "./nested-types.js";
4
4
  export * from "./reflect.js";
5
5
  export * from "./reflect-types.js";
6
6
  export * from "./scalar.js";
7
+ export * from "./path.js";
7
8
  export { isReflectList, isReflectMap, isReflectMessage } from "./guard.js";
@@ -17,4 +17,5 @@ export * from "./nested-types.js";
17
17
  export * from "./reflect.js";
18
18
  export * from "./reflect-types.js";
19
19
  export * from "./scalar.js";
20
+ export * from "./path.js";
20
21
  export { isReflectList, isReflectMap, isReflectMessage } from "./guard.js";
@@ -1,3 +1,11 @@
1
+ import type { AnyDesc } from "../descriptors.js";
2
+ /**
3
+ * Return a fully-qualified name for a Protobuf descriptor.
4
+ * For a file descriptor, return the original file path.
5
+ *
6
+ * See https://protobuf.com/docs/language-spec#fully-qualified-names
7
+ */
8
+ export declare function qualifiedName(desc: AnyDesc): string;
1
9
  /**
2
10
  * Converts snake_case to protoCamelCase according to the convention
3
11
  * used by protoc to convert a field name to a JSON name.
@@ -11,6 +11,33 @@
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
+ /**
15
+ * Return a fully-qualified name for a Protobuf descriptor.
16
+ * For a file descriptor, return the original file path.
17
+ *
18
+ * See https://protobuf.com/docs/language-spec#fully-qualified-names
19
+ */
20
+ export function qualifiedName(desc) {
21
+ switch (desc.kind) {
22
+ case "field":
23
+ case "oneof":
24
+ case "rpc":
25
+ return desc.parent.typeName + "." + desc.name;
26
+ case "enum_value": {
27
+ const p = desc.parent.parent
28
+ ? desc.parent.parent.typeName
29
+ : desc.parent.file.proto.package;
30
+ return p + (p.length > 0 ? "." : "") + desc.name;
31
+ }
32
+ case "service":
33
+ case "message":
34
+ case "enum":
35
+ case "extension":
36
+ return desc.typeName;
37
+ case "file":
38
+ return desc.proto.name;
39
+ }
40
+ }
14
41
  /**
15
42
  * Converts snake_case to protoCamelCase according to the convention
16
43
  * used by protoc to convert a field name to a JSON name.
@@ -4,6 +4,29 @@ import type { AnyDesc, DescEnum, DescExtension, DescFile, DescMessage, DescServi
4
4
  * and enumerations, extensions and messages nested in messages.
5
5
  */
6
6
  export declare function nestedTypes(desc: DescFile | DescMessage): Iterable<DescMessage | DescEnum | DescExtension | DescService>;
7
+ /**
8
+ * Iterate over types referenced by fields of the given message.
9
+ *
10
+ * For example:
11
+ *
12
+ * ```proto
13
+ * syntax="proto3";
14
+ *
15
+ * message Example {
16
+ * Msg singular = 1;
17
+ * repeated Level list = 2;
18
+ * }
19
+ *
20
+ * message Msg {}
21
+ *
22
+ * enum Level {
23
+ * LEVEL_UNSPECIFIED = 0;
24
+ * }
25
+ * ```
26
+ *
27
+ * The message Example references the message Msg, and the enum Level.
28
+ */
29
+ export declare function usedTypes(descMessage: DescMessage): Iterable<DescMessage | DescEnum>;
7
30
  /**
8
31
  * Returns the ancestors of a given Protobuf element, up to the file.
9
32
  */
@@ -36,6 +36,45 @@ export function* nestedTypes(desc) {
36
36
  break;
37
37
  }
38
38
  }
39
+ /**
40
+ * Iterate over types referenced by fields of the given message.
41
+ *
42
+ * For example:
43
+ *
44
+ * ```proto
45
+ * syntax="proto3";
46
+ *
47
+ * message Example {
48
+ * Msg singular = 1;
49
+ * repeated Level list = 2;
50
+ * }
51
+ *
52
+ * message Msg {}
53
+ *
54
+ * enum Level {
55
+ * LEVEL_UNSPECIFIED = 0;
56
+ * }
57
+ * ```
58
+ *
59
+ * The message Example references the message Msg, and the enum Level.
60
+ */
61
+ export function usedTypes(descMessage) {
62
+ return usedTypesInternal(descMessage, new Set());
63
+ }
64
+ function* usedTypesInternal(descMessage, seen) {
65
+ var _a, _b;
66
+ for (const field of descMessage.fields) {
67
+ const ref = (_b = (_a = field.enum) !== null && _a !== void 0 ? _a : field.message) !== null && _b !== void 0 ? _b : undefined;
68
+ if (!ref || seen.has(ref.typeName)) {
69
+ continue;
70
+ }
71
+ seen.add(ref.typeName);
72
+ yield ref;
73
+ if (ref.kind == "message") {
74
+ yield* usedTypesInternal(ref, seen);
75
+ }
76
+ }
77
+ }
39
78
  /**
40
79
  * Returns the ancestors of a given Protobuf element, up to the file.
41
80
  */
@@ -0,0 +1,107 @@
1
+ import { type DescExtension, type DescField, type DescMessage, type DescOneof } from "../descriptors.js";
2
+ import type { Registry } from "../registry.js";
3
+ /**
4
+ * A path to a (nested) member of a Protobuf message, such as a field, oneof,
5
+ * extension, list element, or map entry.
6
+ *
7
+ * Note that we may add additional types to this union in the future to support
8
+ * more use cases.
9
+ */
10
+ export type Path = (DescField | DescExtension | DescOneof | {
11
+ kind: "list_sub";
12
+ index: number;
13
+ } | {
14
+ kind: "map_sub";
15
+ key: string | number | bigint | boolean;
16
+ })[];
17
+ /**
18
+ * Builds a Path.
19
+ */
20
+ export type PathBuilder = {
21
+ /**
22
+ * The root message of the path.
23
+ */
24
+ readonly schema: DescMessage;
25
+ /**
26
+ * Add field access.
27
+ *
28
+ * Throws an InvalidPathError if the field cannot be added to the path.
29
+ */
30
+ field(field: DescField): PathBuilder;
31
+ /**
32
+ * Access a oneof.
33
+ *
34
+ * Throws an InvalidPathError if the oneof cannot be added to the path.
35
+ *
36
+ */
37
+ oneof(oneof: DescOneof): PathBuilder;
38
+ /**
39
+ * Access an extension.
40
+ *
41
+ * Throws an InvalidPathError if the extension cannot be added to the path.
42
+ */
43
+ extension(extension: DescExtension): PathBuilder;
44
+ /**
45
+ * Access a list field by index.
46
+ *
47
+ * Throws an InvalidPathError if the list access cannot be added to the path.
48
+ */
49
+ list(index: number): PathBuilder;
50
+ /**
51
+ * Access a map field by key.
52
+ *
53
+ * Throws an InvalidPathError if the map access cannot be added to the path.
54
+ */
55
+ map(key: string | number | bigint | boolean): PathBuilder;
56
+ /**
57
+ * Append a path.
58
+ *
59
+ * Throws an InvalidPathError if the path cannot be added.
60
+ */
61
+ add(path: Path | PathBuilder): PathBuilder;
62
+ /**
63
+ * Return the path.
64
+ */
65
+ toPath(): Path;
66
+ /**
67
+ * Create a copy of this builder.
68
+ */
69
+ clone(): PathBuilder;
70
+ /**
71
+ * Get the current container - a list, map, or message.
72
+ */
73
+ getLeft(): DescMessage | (DescField & {
74
+ fieldKind: "list";
75
+ }) | (DescField & {
76
+ fieldKind: "map";
77
+ }) | undefined;
78
+ };
79
+ /**
80
+ * Create a PathBuilder.
81
+ */
82
+ export declare function buildPath(schema: DescMessage): PathBuilder;
83
+ /**
84
+ * Parse a Path from a string.
85
+ *
86
+ * Throws an InvalidPathError if the path is invalid.
87
+ *
88
+ * Note that a Registry must be provided via the options argument to parse
89
+ * paths that refer to an extension.
90
+ */
91
+ export declare function parsePath(schema: DescMessage, path: string, options?: {
92
+ registry?: Registry;
93
+ }): Path;
94
+ /**
95
+ * Stringify a path.
96
+ */
97
+ export declare function pathToString(path: Path): string;
98
+ /**
99
+ * InvalidPathError is thrown for invalid Paths, for example during parsing from
100
+ * a string, or when a new Path is built.
101
+ */
102
+ export declare class InvalidPathError extends Error {
103
+ name: string;
104
+ readonly schema: DescMessage;
105
+ readonly path: Path | string;
106
+ constructor(schema: DescMessage, message: string, path: string | Path);
107
+ }