@bufbuild/protobuf 1.6.0 → 1.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (153) hide show
  1. package/dist/cjs/binary-encoding.js +2 -3
  2. package/dist/cjs/binary-format.d.ts +17 -0
  3. package/dist/cjs/binary-format.js +1 -1
  4. package/dist/cjs/codegen-info.d.ts +4 -4
  5. package/dist/cjs/codegen-info.js +2 -1
  6. package/dist/cjs/create-descriptor-set.d.ts +7 -1
  7. package/dist/cjs/create-descriptor-set.js +56 -50
  8. package/dist/cjs/create-registry-from-desc.d.ts +2 -2
  9. package/dist/cjs/create-registry-from-desc.js +53 -9
  10. package/dist/cjs/create-registry.d.ts +3 -2
  11. package/dist/cjs/create-registry.js +56 -37
  12. package/dist/cjs/descriptor-set.js +1 -1
  13. package/dist/cjs/enum.js +1 -1
  14. package/dist/cjs/extension-accessor.d.ts +35 -0
  15. package/dist/cjs/extension-accessor.js +108 -0
  16. package/dist/cjs/extension.d.ts +24 -0
  17. package/dist/cjs/extension.js +15 -0
  18. package/dist/cjs/field-list.js +1 -1
  19. package/dist/cjs/field.js +1 -1
  20. package/dist/cjs/google/protobuf/any_pb.js +1 -1
  21. package/dist/cjs/google/protobuf/api_pb.js +1 -1
  22. package/dist/cjs/google/protobuf/compiler/plugin_pb.js +1 -1
  23. package/dist/cjs/google/protobuf/descriptor_pb.js +2 -2
  24. package/dist/cjs/google/protobuf/duration_pb.js +1 -1
  25. package/dist/cjs/google/protobuf/empty_pb.js +1 -1
  26. package/dist/cjs/google/protobuf/field_mask_pb.js +1 -1
  27. package/dist/cjs/google/protobuf/source_context_pb.js +1 -1
  28. package/dist/cjs/google/protobuf/struct_pb.js +2 -2
  29. package/dist/cjs/google/protobuf/timestamp_pb.js +1 -1
  30. package/dist/cjs/google/protobuf/type_pb.js +2 -2
  31. package/dist/cjs/google/protobuf/wrappers_pb.js +1 -1
  32. package/dist/cjs/index.d.ts +3 -1
  33. package/dist/cjs/index.js +7 -2
  34. package/dist/cjs/json-format.d.ts +5 -5
  35. package/dist/cjs/json-format.js +1 -1
  36. package/dist/cjs/message-type.js +1 -1
  37. package/dist/cjs/message.js +1 -1
  38. package/dist/cjs/private/assert.js +1 -1
  39. package/dist/cjs/private/binary-format-common.d.ts +2 -3
  40. package/dist/cjs/private/binary-format-common.js +80 -78
  41. package/dist/cjs/private/binary-format-proto2.js +59 -64
  42. package/dist/cjs/private/binary-format-proto3.js +44 -40
  43. package/dist/cjs/private/enum.js +1 -1
  44. package/dist/cjs/private/extensions.d.ts +34 -0
  45. package/dist/cjs/private/extensions.js +86 -0
  46. package/dist/cjs/private/feature-set.d.ts +6 -8
  47. package/dist/cjs/private/feature-set.js +41 -42
  48. package/dist/cjs/private/field-list.js +1 -1
  49. package/dist/cjs/private/field-wrapper.d.ts +2 -2
  50. package/dist/cjs/private/field-wrapper.js +1 -1
  51. package/dist/cjs/private/field.js +1 -1
  52. package/dist/cjs/private/json-format-common.d.ts +3 -3
  53. package/dist/cjs/private/json-format-common.js +228 -173
  54. package/dist/cjs/private/json-format-proto2.js +3 -2
  55. package/dist/cjs/private/json-format-proto3.js +8 -6
  56. package/dist/cjs/private/message-type.js +1 -1
  57. package/dist/cjs/private/names.d.ts +2 -2
  58. package/dist/cjs/private/names.js +3 -2
  59. package/dist/cjs/private/options-map.js +1 -1
  60. package/dist/cjs/private/proto-runtime.d.ts +6 -0
  61. package/dist/cjs/private/proto-runtime.js +5 -1
  62. package/dist/cjs/private/reify-wkt.js +1 -1
  63. package/dist/cjs/private/scalars.js +1 -1
  64. package/dist/cjs/private/text-format.js +1 -1
  65. package/dist/cjs/private/util-common.js +1 -1
  66. package/dist/cjs/private/util.js +1 -1
  67. package/dist/cjs/proto-base64.js +1 -1
  68. package/dist/cjs/proto-delimited.js +1 -1
  69. package/dist/cjs/proto-double.js +1 -1
  70. package/dist/cjs/proto-int64.js +1 -1
  71. package/dist/cjs/proto2.js +1 -1
  72. package/dist/cjs/proto3.js +1 -1
  73. package/dist/cjs/service-type.js +1 -1
  74. package/dist/cjs/to-plain-message.js +1 -1
  75. package/dist/cjs/type-registry.d.ts +14 -0
  76. package/dist/cjs/type-registry.js +1 -1
  77. package/dist/esm/binary-encoding.js +2 -3
  78. package/dist/esm/binary-format.d.ts +17 -0
  79. package/dist/esm/binary-format.js +1 -1
  80. package/dist/esm/codegen-info.d.ts +4 -4
  81. package/dist/esm/codegen-info.js +2 -1
  82. package/dist/esm/create-descriptor-set.d.ts +7 -1
  83. package/dist/esm/create-descriptor-set.js +57 -51
  84. package/dist/esm/create-registry-from-desc.d.ts +2 -2
  85. package/dist/esm/create-registry-from-desc.js +53 -9
  86. package/dist/esm/create-registry.d.ts +3 -2
  87. package/dist/esm/create-registry.js +56 -37
  88. package/dist/esm/descriptor-set.js +1 -1
  89. package/dist/esm/enum.js +1 -1
  90. package/dist/esm/extension-accessor.d.ts +35 -0
  91. package/dist/esm/extension-accessor.js +101 -0
  92. package/dist/esm/extension.d.ts +24 -0
  93. package/dist/esm/extension.js +14 -0
  94. package/dist/esm/field-list.js +1 -1
  95. package/dist/esm/field.js +1 -1
  96. package/dist/esm/google/protobuf/any_pb.js +1 -1
  97. package/dist/esm/google/protobuf/api_pb.js +1 -1
  98. package/dist/esm/google/protobuf/compiler/plugin_pb.js +1 -1
  99. package/dist/esm/google/protobuf/descriptor_pb.js +2 -2
  100. package/dist/esm/google/protobuf/duration_pb.js +1 -1
  101. package/dist/esm/google/protobuf/empty_pb.js +1 -1
  102. package/dist/esm/google/protobuf/field_mask_pb.js +1 -1
  103. package/dist/esm/google/protobuf/source_context_pb.js +1 -1
  104. package/dist/esm/google/protobuf/struct_pb.js +2 -2
  105. package/dist/esm/google/protobuf/timestamp_pb.js +1 -1
  106. package/dist/esm/google/protobuf/type_pb.js +2 -2
  107. package/dist/esm/google/protobuf/wrappers_pb.js +1 -1
  108. package/dist/esm/index.d.ts +3 -1
  109. package/dist/esm/index.js +2 -1
  110. package/dist/esm/json-format.d.ts +5 -5
  111. package/dist/esm/json-format.js +1 -1
  112. package/dist/esm/message-type.js +1 -1
  113. package/dist/esm/message.js +1 -1
  114. package/dist/esm/private/assert.js +1 -1
  115. package/dist/esm/private/binary-format-common.d.ts +2 -3
  116. package/dist/esm/private/binary-format-common.js +80 -77
  117. package/dist/esm/private/binary-format-proto2.js +59 -64
  118. package/dist/esm/private/binary-format-proto3.js +44 -40
  119. package/dist/esm/private/enum.js +1 -1
  120. package/dist/esm/private/extensions.d.ts +34 -0
  121. package/dist/esm/private/extensions.js +81 -0
  122. package/dist/esm/private/feature-set.d.ts +6 -8
  123. package/dist/esm/private/feature-set.js +40 -41
  124. package/dist/esm/private/field-list.js +1 -1
  125. package/dist/esm/private/field-wrapper.d.ts +2 -2
  126. package/dist/esm/private/field-wrapper.js +1 -1
  127. package/dist/esm/private/field.js +1 -1
  128. package/dist/esm/private/json-format-common.d.ts +3 -3
  129. package/dist/esm/private/json-format-common.js +228 -173
  130. package/dist/esm/private/json-format-proto2.js +3 -2
  131. package/dist/esm/private/json-format-proto3.js +8 -6
  132. package/dist/esm/private/message-type.js +1 -1
  133. package/dist/esm/private/names.d.ts +2 -2
  134. package/dist/esm/private/names.js +3 -2
  135. package/dist/esm/private/options-map.js +1 -1
  136. package/dist/esm/private/proto-runtime.d.ts +6 -0
  137. package/dist/esm/private/proto-runtime.js +5 -1
  138. package/dist/esm/private/reify-wkt.js +1 -1
  139. package/dist/esm/private/scalars.js +1 -1
  140. package/dist/esm/private/text-format.js +1 -1
  141. package/dist/esm/private/util-common.js +1 -1
  142. package/dist/esm/private/util.js +1 -1
  143. package/dist/esm/proto-base64.js +1 -1
  144. package/dist/esm/proto-delimited.js +1 -1
  145. package/dist/esm/proto-double.js +1 -1
  146. package/dist/esm/proto-int64.js +1 -1
  147. package/dist/esm/proto2.js +1 -1
  148. package/dist/esm/proto3.js +1 -1
  149. package/dist/esm/service-type.js +1 -1
  150. package/dist/esm/to-plain-message.js +1 -1
  151. package/dist/esm/type-registry.d.ts +14 -0
  152. package/dist/esm/type-registry.js +1 -1
  153. package/package.json +1 -1
@@ -1,4 +1,4 @@
1
- // Copyright 2021-2023 Buf Technologies, Inc.
1
+ // Copyright 2021-2024 Buf Technologies, Inc.
2
2
  //
3
3
  // Licensed under the Apache License, Version 2.0 (the "License");
4
4
  // you may not use this file except in compliance with the License.
@@ -15,74 +15,28 @@ import { ScalarType } from "../field.js";
15
15
  import { makeBinaryFormatCommon, writeMapEntry, writeMessageField, writePacked, writeScalar, } from "./binary-format-common.js";
16
16
  /* 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 */
17
17
  export function makeBinaryFormatProto2() {
18
- return Object.assign(Object.assign({}, makeBinaryFormatCommon()), { writeMessage(message, writer, options) {
18
+ return Object.assign(Object.assign({}, makeBinaryFormatCommon()), { writeField,
19
+ writeMessage(message, writer, options) {
19
20
  const type = message.getType();
20
21
  let field;
21
- try {
22
- for (field of type.fields.byNumber()) {
23
- let value, // this will be our field value, whether it is member of a oneof or not
24
- repeated = field.repeated, localName = field.localName;
25
- if (field.oneof) {
26
- const oneof = message[field.oneof.localName];
27
- if (oneof.case !== localName) {
28
- continue; // field is not selected, skip
29
- }
30
- value = oneof.value;
22
+ for (field of type.fields.byNumber()) {
23
+ let value, localName = field.localName;
24
+ if (field.oneof) {
25
+ const oneof = message[field.oneof.localName];
26
+ if (oneof.case !== localName) {
27
+ continue; // field is not selected, skip
31
28
  }
32
- else {
33
- value = message[localName];
34
- // In contrast to proto3, we raise an error if a non-optional (proto2 required)
35
- // field is missing a value.
36
- if (value === undefined && !field.oneof && !field.opt) {
37
- throw new Error(`cannot encode field ${type.typeName}.${field.name} to binary: required field not set`);
38
- }
39
- }
40
- switch (field.kind) {
41
- case "scalar":
42
- case "enum":
43
- let scalarType = field.kind == "enum" ? ScalarType.INT32 : field.T;
44
- if (repeated) {
45
- if (field.packed) {
46
- writePacked(writer, scalarType, field.no, value);
47
- }
48
- else {
49
- for (const item of value) {
50
- writeScalar(writer, scalarType, field.no, item, true);
51
- }
52
- }
53
- }
54
- else {
55
- if (value !== undefined) {
56
- // In contrast to proto3, we do not skip intrinsic default values.
57
- // Explicit default values are not special cased either.
58
- writeScalar(writer, scalarType, field.no, value, true);
59
- }
60
- }
61
- break;
62
- case "message":
63
- if (repeated) {
64
- for (const item of value) {
65
- writeMessageField(writer, options, field, item);
66
- }
67
- }
68
- else {
69
- writeMessageField(writer, options, field, value);
70
- }
71
- break;
72
- case "map":
73
- for (const [key, val] of Object.entries(value)) {
74
- writeMapEntry(writer, options, field, key, val);
75
- }
76
- break;
29
+ value = oneof.value;
30
+ }
31
+ else {
32
+ value = message[localName];
33
+ // In contrast to proto3, we raise an error if a non-optional (proto2 required)
34
+ // field is missing a value.
35
+ if (value === undefined && !field.oneof && !field.opt) {
36
+ throw new Error(`cannot encode field ${type.typeName}.${field === null || field === void 0 ? void 0 : field.name} to binary: required field not set`);
77
37
  }
78
38
  }
79
- }
80
- catch (e) {
81
- let m = field
82
- ? `cannot encode field ${type.typeName}.${field === null || field === void 0 ? void 0 : field.name} to binary`
83
- : `cannot encode message ${type.typeName} to binary`;
84
- let r = e instanceof Error ? e.message : String(e);
85
- throw new Error(m + (r.length > 0 ? `: ${r}` : ""));
39
+ writeField(field, value, writer, options);
86
40
  }
87
41
  if (options.writeUnknownFields) {
88
42
  this.writeUnknownFields(message, writer);
@@ -90,3 +44,44 @@ export function makeBinaryFormatProto2() {
90
44
  return writer;
91
45
  } });
92
46
  }
47
+ // TODO field presence: merge this function with proto3
48
+ function writeField(field, value, // eslint-disable-line @typescript-eslint/no-explicit-any -- `any` is the best choice for dynamic access
49
+ writer, options) {
50
+ const repeated = field.repeated;
51
+ switch (field.kind) {
52
+ case "scalar":
53
+ case "enum":
54
+ let scalarType = field.kind == "enum" ? ScalarType.INT32 : field.T;
55
+ if (repeated) {
56
+ if (field.packed) {
57
+ writePacked(writer, scalarType, field.no, value);
58
+ }
59
+ else {
60
+ for (const item of value) {
61
+ writeScalar(writer, scalarType, field.no, item, true);
62
+ }
63
+ }
64
+ }
65
+ else if (value !== undefined) {
66
+ // In contrast to proto3, we do not skip intrinsic default values.
67
+ // Explicit default values are not special cased either.
68
+ writeScalar(writer, scalarType, field.no, value, true);
69
+ }
70
+ break;
71
+ case "message":
72
+ if (repeated) {
73
+ for (const item of value) {
74
+ writeMessageField(writer, options, field, item);
75
+ }
76
+ }
77
+ else if (value !== undefined) {
78
+ writeMessageField(writer, options, field, value);
79
+ }
80
+ break;
81
+ case "map":
82
+ for (const [key, val] of Object.entries(value)) {
83
+ writeMapEntry(writer, options, field, key, val);
84
+ }
85
+ break;
86
+ }
87
+ }
@@ -1,4 +1,4 @@
1
- // Copyright 2021-2023 Buf Technologies, Inc.
1
+ // Copyright 2021-2024 Buf Technologies, Inc.
2
2
  //
3
3
  // Licensed under the Apache License, Version 2.0 (the "License");
4
4
  // you may not use this file except in compliance with the License.
@@ -15,11 +15,11 @@ import { ScalarType } from "../field.js";
15
15
  import { makeBinaryFormatCommon, writeMapEntry, writeMessageField, writePacked, writeScalar, } from "./binary-format-common.js";
16
16
  /* 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 */
17
17
  export function makeBinaryFormatProto3() {
18
- return Object.assign(Object.assign({}, makeBinaryFormatCommon()), { writeMessage(message, writer, options) {
18
+ return Object.assign(Object.assign({}, makeBinaryFormatCommon()), { writeField,
19
+ writeMessage(message, writer, options) {
19
20
  const type = message.getType();
20
21
  for (const field of type.fields.byNumber()) {
21
- let value, // this will be our field value, whether it is member of a oneof or regular field
22
- repeated = field.repeated, localName = field.localName;
22
+ let value, localName = field.localName;
23
23
  if (field.oneof) {
24
24
  const oneof = message[field.oneof.localName];
25
25
  if (oneof.case !== localName) {
@@ -30,42 +30,7 @@ export function makeBinaryFormatProto3() {
30
30
  else {
31
31
  value = message[localName];
32
32
  }
33
- switch (field.kind) {
34
- case "scalar":
35
- case "enum":
36
- let scalarType = field.kind == "enum" ? ScalarType.INT32 : field.T;
37
- if (repeated) {
38
- if (field.packed) {
39
- writePacked(writer, scalarType, field.no, value);
40
- }
41
- else {
42
- for (const item of value) {
43
- writeScalar(writer, scalarType, field.no, item, true);
44
- }
45
- }
46
- }
47
- else {
48
- if (value !== undefined) {
49
- writeScalar(writer, scalarType, field.no, value, !!field.oneof || field.opt);
50
- }
51
- }
52
- break;
53
- case "message":
54
- if (repeated) {
55
- for (const item of value) {
56
- writeMessageField(writer, options, field, item);
57
- }
58
- }
59
- else {
60
- writeMessageField(writer, options, field, value);
61
- }
62
- break;
63
- case "map":
64
- for (const [key, val] of Object.entries(value)) {
65
- writeMapEntry(writer, options, field, key, val);
66
- }
67
- break;
68
- }
33
+ writeField(field, value, writer, options);
69
34
  }
70
35
  if (options.writeUnknownFields) {
71
36
  this.writeUnknownFields(message, writer);
@@ -73,3 +38,42 @@ export function makeBinaryFormatProto3() {
73
38
  return writer;
74
39
  } });
75
40
  }
41
+ // TODO field presence: merge this function with proto2
42
+ function writeField(field, value, // eslint-disable-line @typescript-eslint/no-explicit-any -- `any` is the best choice for dynamic access
43
+ writer, options) {
44
+ const repeated = field.repeated;
45
+ switch (field.kind) {
46
+ case "scalar":
47
+ case "enum":
48
+ let scalarType = field.kind == "enum" ? ScalarType.INT32 : field.T;
49
+ if (repeated) {
50
+ if (field.packed) {
51
+ writePacked(writer, scalarType, field.no, value);
52
+ }
53
+ else {
54
+ for (const item of value) {
55
+ writeScalar(writer, scalarType, field.no, item, true);
56
+ }
57
+ }
58
+ }
59
+ else if (value !== undefined) {
60
+ writeScalar(writer, scalarType, field.no, value, !!field.oneof || field.opt);
61
+ }
62
+ break;
63
+ case "message":
64
+ if (repeated) {
65
+ for (const item of value) {
66
+ writeMessageField(writer, options, field, item);
67
+ }
68
+ }
69
+ else if (value !== undefined) {
70
+ writeMessageField(writer, options, field, value);
71
+ }
72
+ break;
73
+ case "map":
74
+ for (const [key, val] of Object.entries(value)) {
75
+ writeMapEntry(writer, options, field, key, val);
76
+ }
77
+ break;
78
+ }
79
+ }
@@ -1,4 +1,4 @@
1
- // Copyright 2021-2023 Buf Technologies, Inc.
1
+ // Copyright 2021-2024 Buf Technologies, Inc.
2
2
  //
3
3
  // Licensed under the Apache License, Version 2.0 (the "License");
4
4
  // you may not use this file except in compliance with the License.
@@ -0,0 +1,34 @@
1
+ import type { Extension } from "../extension.js";
2
+ import type { AnyMessage, Message } from "../message.js";
3
+ import type { FieldInfo, OneofInfo, PartialFieldInfo } from "../field.js";
4
+ import { WireType } from "../binary-encoding.js";
5
+ import type { ProtoRuntime } from "./proto-runtime.js";
6
+ import type { MessageType } from "../message-type.js";
7
+ export type ExtensionFieldSource = extensionFieldRules<FieldInfo> | extensionFieldRules<PartialFieldInfo> | (() => extensionFieldRules<FieldInfo>) | (() => extensionFieldRules<PartialFieldInfo>);
8
+ type extensionFieldRules<T extends FieldInfo | PartialFieldInfo> = T extends {
9
+ kind: "map";
10
+ } ? never : T extends {
11
+ oneof: string;
12
+ } ? never : T extends {
13
+ oneof: OneofInfo;
14
+ } ? never : Omit<T, "name"> & Partial<Pick<T, "name">>;
15
+ /**
16
+ * Create a new extension using the given runtime.
17
+ */
18
+ export declare function makeExtension<E extends Message<E> = AnyMessage, V = unknown>(runtime: ProtoRuntime, typeName: string, extendee: MessageType<E>, field: ExtensionFieldSource): Extension<E, V>;
19
+ /**
20
+ * Create a container that allows us to read extension fields into it with the
21
+ * same logic as regular fields.
22
+ */
23
+ export declare function createExtensionContainer<E extends Message<E> = AnyMessage, V = unknown>(extension: Extension<E, V>): [Record<string, V>, () => V];
24
+ type UnknownField = {
25
+ no: number;
26
+ wireType: WireType;
27
+ data: Uint8Array;
28
+ };
29
+ type UnknownFields = ReadonlyArray<UnknownField>;
30
+ /**
31
+ * Helper to filter unknown fields, optimized based on field type.
32
+ */
33
+ export declare function filterUnknownFields(unknownFields: UnknownFields, field: Pick<FieldInfo, "no" | "kind" | "repeated">): UnknownField[];
34
+ export {};
@@ -0,0 +1,81 @@
1
+ // Copyright 2021-2024 Buf Technologies, Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ import { scalarDefaultValue } from "./scalars.js";
15
+ import { WireType } from "../binary-encoding.js";
16
+ /**
17
+ * Create a new extension using the given runtime.
18
+ */
19
+ export function makeExtension(runtime, typeName, extendee, field) {
20
+ let fi;
21
+ return {
22
+ typeName,
23
+ extendee,
24
+ get field() {
25
+ if (!fi) {
26
+ const i = (typeof field == "function" ? field() : field);
27
+ i.name = typeName.split(".").pop();
28
+ i.jsonName = `[${typeName}]`;
29
+ fi = runtime.util.newFieldList([i]).list()[0];
30
+ }
31
+ return fi;
32
+ },
33
+ runtime,
34
+ };
35
+ }
36
+ /**
37
+ * Create a container that allows us to read extension fields into it with the
38
+ * same logic as regular fields.
39
+ */
40
+ export function createExtensionContainer(extension) {
41
+ const localName = extension.field.localName;
42
+ const container = Object.create(null);
43
+ container[localName] = initExtensionField(extension);
44
+ return [container, () => container[localName]];
45
+ }
46
+ function initExtensionField(ext) {
47
+ const field = ext.field;
48
+ if (field.repeated) {
49
+ return [];
50
+ }
51
+ if (field.default !== undefined) {
52
+ return field.default;
53
+ }
54
+ switch (field.kind) {
55
+ case "enum":
56
+ return field.T.values[0].no;
57
+ case "scalar":
58
+ return scalarDefaultValue(field.T, field.L);
59
+ case "message":
60
+ // eslint-disable-next-line no-case-declarations
61
+ const T = field.T, value = new T();
62
+ return T.fieldWrapper ? T.fieldWrapper.unwrapField(value) : value;
63
+ case "map":
64
+ throw "map fields are not allowed to be extensions";
65
+ }
66
+ }
67
+ /**
68
+ * Helper to filter unknown fields, optimized based on field type.
69
+ */
70
+ export function filterUnknownFields(unknownFields, field) {
71
+ if (!field.repeated && (field.kind == "enum" || field.kind == "scalar")) {
72
+ // singular scalar fields do not merge, we pick the last
73
+ for (let i = unknownFields.length - 1; i >= 0; --i) {
74
+ if (unknownFields[i].no == field.no) {
75
+ return [unknownFields[i]];
76
+ }
77
+ }
78
+ return [];
79
+ }
80
+ return unknownFields.filter((uf) => uf.no === field.no);
81
+ }
@@ -1,21 +1,19 @@
1
1
  import { Edition, FeatureSet, FeatureSetDefaults } from "../google/protobuf/descriptor_pb.js";
2
- /**
3
- * Static edition feature defaults supported by @bufbuild/protobuf.
4
- */
5
- export declare const featureSetDefaults: FeatureSetDefaults;
2
+ import type { BinaryReadOptions, BinaryWriteOptions } from "../binary-format.js";
6
3
  /**
7
4
  * A merged google.protobuf.FeaturesSet, with all fields guaranteed to be set.
8
5
  */
9
6
  export type MergedFeatureSet = FeatureSet & Required<FeatureSet>;
10
7
  /**
11
- * A function that resolves features for the given edition.
8
+ * A function that resolves features.
12
9
  *
13
10
  * If no feature set is provided, the default feature set for the edition is
14
11
  * returned. If features are provided, they are merged into the edition default
15
12
  * features.
16
13
  */
17
- export type FeatureResolverFn = (edition: Edition, a?: FeatureSet, b?: FeatureSet) => MergedFeatureSet;
14
+ export type FeatureResolverFn = (a?: FeatureSet, b?: FeatureSet) => MergedFeatureSet;
18
15
  /**
19
- * Create an edition feature resolver with the given feature set defaults.
16
+ * Create an edition feature resolver with the given feature set defaults, or
17
+ * the feature set defaults supported by @bufbuild/protobuf.
20
18
  */
21
- export declare function createFeatureResolver(compiledFeatureSetDefaults: FeatureSetDefaults): FeatureResolverFn;
19
+ export declare function createFeatureResolver(edition: Edition, compiledFeatureSetDefaults?: FeatureSetDefaults, serializationOptions?: Partial<BinaryReadOptions & BinaryWriteOptions>): FeatureResolverFn;
@@ -1,4 +1,4 @@
1
- // Copyright 2021-2023 Buf Technologies, Inc.
1
+ // Copyright 2021-2024 Buf Technologies, Inc.
2
2
  //
3
3
  // Licensed under the Apache License, Version 2.0 (the "License");
4
4
  // you may not use this file except in compliance with the License.
@@ -14,56 +14,55 @@
14
14
  import { Edition, FeatureSet, FeatureSetDefaults, } from "../google/protobuf/descriptor_pb.js";
15
15
  import { protoBase64 } from "../proto-base64.js";
16
16
  /**
17
- * Static edition feature defaults supported by @bufbuild/protobuf.
17
+ * Return the edition feature defaults supported by @bufbuild/protobuf.
18
18
  */
19
- export const featureSetDefaults = FeatureSetDefaults.fromBinary(protoBase64.dec(
20
- /*upstream-inject-feature-defaults-start*/ "ChESDAgBEAIYAiABKAEwAhjmBwoREgwIAhABGAEgAigBMAEY5wcKERIMCAEQARgBIAIoATABGOgHIOYHKOgH" /*upstream-inject-feature-defaults-end*/));
19
+ function getFeatureSetDefaults(options) {
20
+ return FeatureSetDefaults.fromBinary(protoBase64.dec(
21
+ /*upstream-inject-feature-defaults-start*/ "ChESDAgBEAIYAiABKAEwAhjmBwoREgwIAhABGAEgAigBMAEY5wcKERIMCAEQARgBIAIoATABGOgHIOYHKOgH" /*upstream-inject-feature-defaults-end*/), options);
22
+ }
21
23
  /**
22
- * Create an edition feature resolver with the given feature set defaults.
24
+ * Create an edition feature resolver with the given feature set defaults, or
25
+ * the feature set defaults supported by @bufbuild/protobuf.
23
26
  */
24
- export function createFeatureResolver(compiledFeatureSetDefaults) {
25
- const min = compiledFeatureSetDefaults.minimumEdition;
26
- const max = compiledFeatureSetDefaults.maximumEdition;
27
+ export function createFeatureResolver(edition, compiledFeatureSetDefaults, serializationOptions) {
28
+ var _a, _b;
29
+ const fds = compiledFeatureSetDefaults !== null && compiledFeatureSetDefaults !== void 0 ? compiledFeatureSetDefaults : getFeatureSetDefaults(serializationOptions);
30
+ const min = fds.minimumEdition;
31
+ const max = fds.maximumEdition;
27
32
  if (min === undefined ||
28
33
  max === undefined ||
29
- compiledFeatureSetDefaults.defaults.some((d) => d.edition === undefined)) {
34
+ fds.defaults.some((d) => d.edition === undefined)) {
30
35
  throw new Error("Invalid FeatureSetDefaults");
31
36
  }
32
- const defaultsBinByEdition = new Map();
33
- return (edition, ...rest) => {
34
- var _a, _b;
35
- let defaultsBin = defaultsBinByEdition.get(edition);
36
- if (defaultsBin === undefined) {
37
- if (edition < min) {
38
- throw new Error(`Edition ${Edition[edition]} is earlier than the minimum supported edition ${Edition[min]}`);
39
- }
40
- if (max < edition) {
41
- throw new Error(`Edition ${Edition[edition]} is later than the maximum supported edition ${Edition[max]}`);
42
- }
43
- let highestMatch = undefined;
44
- for (const c of compiledFeatureSetDefaults.defaults) {
45
- const e = (_a = c.edition) !== null && _a !== void 0 ? _a : 0;
46
- if (e > edition) {
47
- continue;
48
- }
49
- if (highestMatch !== undefined && highestMatch.e > e) {
50
- continue;
51
- }
52
- highestMatch = {
53
- e,
54
- f: (_b = c.features) !== null && _b !== void 0 ? _b : new FeatureSet(),
55
- };
56
- }
57
- if (highestMatch === undefined) {
58
- throw new Error(`No valid default found for edition ${Edition[edition]}`);
59
- }
60
- defaultsBin = highestMatch.f.toBinary();
61
- defaultsBinByEdition.set(edition, defaultsBin);
37
+ if (edition < min) {
38
+ throw new Error(`Edition ${Edition[edition]} is earlier than the minimum supported edition ${Edition[min]}`);
39
+ }
40
+ if (max < edition) {
41
+ throw new Error(`Edition ${Edition[edition]} is later than the maximum supported edition ${Edition[max]}`);
42
+ }
43
+ let highestMatch = undefined;
44
+ for (const c of fds.defaults) {
45
+ const e = (_a = c.edition) !== null && _a !== void 0 ? _a : 0;
46
+ if (e > edition) {
47
+ continue;
48
+ }
49
+ if (highestMatch !== undefined && highestMatch.e > e) {
50
+ continue;
62
51
  }
63
- const f = FeatureSet.fromBinary(defaultsBin);
52
+ highestMatch = {
53
+ e,
54
+ f: (_b = c.features) !== null && _b !== void 0 ? _b : new FeatureSet(),
55
+ };
56
+ }
57
+ if (highestMatch === undefined) {
58
+ throw new Error(`No valid default found for edition ${Edition[edition]}`);
59
+ }
60
+ const featureSetBin = highestMatch.f.toBinary(serializationOptions);
61
+ return (...rest) => {
62
+ const f = FeatureSet.fromBinary(featureSetBin, serializationOptions);
64
63
  for (const c of rest) {
65
64
  if (c !== undefined) {
66
- f.fromBinary(c.toBinary());
65
+ f.fromBinary(c.toBinary(serializationOptions), serializationOptions);
67
66
  }
68
67
  }
69
68
  if (!validateMergedFeatures(f)) {
@@ -1,4 +1,4 @@
1
- // Copyright 2021-2023 Buf Technologies, Inc.
1
+ // Copyright 2021-2024 Buf Technologies, Inc.
2
2
  //
3
3
  // Licensed under the Apache License, Version 2.0 (the "License");
4
4
  // you may not use this file except in compliance with the License.
@@ -1,6 +1,6 @@
1
1
  import { Message } from "../message.js";
2
2
  import type { MessageType } from "../message-type.js";
3
- import type { DescField } from "../descriptor-set.js";
3
+ import type { DescExtension, DescField } from "../descriptor-set.js";
4
4
  import { ScalarType } from "../field.js";
5
5
  /**
6
6
  * A field wrapper unwraps a message to a primitive value that is more
@@ -22,4 +22,4 @@ export declare function wrapField<T extends Message<T>>(type: MessageType<T>, va
22
22
  * If the given field uses one of the well-known wrapper types, return
23
23
  * the primitive type it wraps.
24
24
  */
25
- export declare function getUnwrappedFieldType(field: DescField): ScalarType | undefined;
25
+ export declare function getUnwrappedFieldType(field: DescField | DescExtension): ScalarType | undefined;
@@ -1,4 +1,4 @@
1
- // Copyright 2021-2023 Buf Technologies, Inc.
1
+ // Copyright 2021-2024 Buf Technologies, Inc.
2
2
  //
3
3
  // Licensed under the Apache License, Version 2.0 (the "License");
4
4
  // you may not use this file except in compliance with the License.
@@ -1,4 +1,4 @@
1
- // Copyright 2021-2023 Buf Technologies, Inc.
1
+ // Copyright 2021-2024 Buf Technologies, Inc.
2
2
  //
3
3
  // Licensed under the Apache License, Version 2.0 (the "License");
4
4
  // you may not use this file except in compliance with the License.
@@ -3,7 +3,7 @@ import type { FieldInfo } from "../field.js";
3
3
  import { ScalarType } from "../field.js";
4
4
  import type { EnumType } from "../enum.js";
5
5
  type JsonFormatWriteFieldFn = (field: FieldInfo, value: any, options: JsonWriteOptions) => JsonValue | undefined;
6
- export declare function makeJsonFormatCommon(makeWriteField: (writeEnumFn: typeof writeEnum, writeScalarFn: typeof writeScalar) => JsonFormatWriteFieldFn): JsonFormat;
7
- declare function writeEnum(type: EnumType, value: number | undefined, emitIntrinsicDefault: boolean, enumAsInteger: boolean): JsonValue | undefined;
8
- declare function writeScalar(type: ScalarType, value: any, emitIntrinsicDefault: boolean): JsonValue | undefined;
6
+ export declare function makeJsonFormatCommon(nullAsZeroValue: boolean, makeWriteField: (writeEnumFn: typeof writeEnum, writeScalarFn: typeof writeScalar) => JsonFormatWriteFieldFn): JsonFormat;
7
+ declare function writeEnum(type: EnumType, value: number | undefined, emitZeroValue: boolean, enumAsInteger: boolean): JsonValue | undefined;
8
+ declare function writeScalar(type: ScalarType, value: any, emitZeroValue: boolean): JsonValue | undefined;
9
9
  export {};