@bufbuild/protobuf 1.6.0 → 1.7.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 (151) 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 +1 -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 +3 -3
  47. package/dist/cjs/private/feature-set.js +27 -32
  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.js +202 -156
  53. package/dist/cjs/private/json-format-proto2.js +2 -1
  54. package/dist/cjs/private/json-format-proto3.js +7 -5
  55. package/dist/cjs/private/message-type.js +1 -1
  56. package/dist/cjs/private/names.d.ts +2 -2
  57. package/dist/cjs/private/names.js +3 -2
  58. package/dist/cjs/private/options-map.js +1 -1
  59. package/dist/cjs/private/proto-runtime.d.ts +6 -0
  60. package/dist/cjs/private/proto-runtime.js +5 -1
  61. package/dist/cjs/private/reify-wkt.js +1 -1
  62. package/dist/cjs/private/scalars.js +1 -1
  63. package/dist/cjs/private/text-format.js +1 -1
  64. package/dist/cjs/private/util-common.js +1 -1
  65. package/dist/cjs/private/util.js +1 -1
  66. package/dist/cjs/proto-base64.js +1 -1
  67. package/dist/cjs/proto-delimited.js +1 -1
  68. package/dist/cjs/proto-double.js +1 -1
  69. package/dist/cjs/proto-int64.js +1 -1
  70. package/dist/cjs/proto2.js +1 -1
  71. package/dist/cjs/proto3.js +1 -1
  72. package/dist/cjs/service-type.js +1 -1
  73. package/dist/cjs/to-plain-message.js +1 -1
  74. package/dist/cjs/type-registry.d.ts +14 -0
  75. package/dist/cjs/type-registry.js +1 -1
  76. package/dist/esm/binary-encoding.js +2 -3
  77. package/dist/esm/binary-format.d.ts +17 -0
  78. package/dist/esm/binary-format.js +1 -1
  79. package/dist/esm/codegen-info.d.ts +4 -4
  80. package/dist/esm/codegen-info.js +2 -1
  81. package/dist/esm/create-descriptor-set.d.ts +1 -1
  82. package/dist/esm/create-descriptor-set.js +56 -50
  83. package/dist/esm/create-registry-from-desc.d.ts +2 -2
  84. package/dist/esm/create-registry-from-desc.js +53 -9
  85. package/dist/esm/create-registry.d.ts +3 -2
  86. package/dist/esm/create-registry.js +56 -37
  87. package/dist/esm/descriptor-set.js +1 -1
  88. package/dist/esm/enum.js +1 -1
  89. package/dist/esm/extension-accessor.d.ts +35 -0
  90. package/dist/esm/extension-accessor.js +101 -0
  91. package/dist/esm/extension.d.ts +24 -0
  92. package/dist/esm/extension.js +14 -0
  93. package/dist/esm/field-list.js +1 -1
  94. package/dist/esm/field.js +1 -1
  95. package/dist/esm/google/protobuf/any_pb.js +1 -1
  96. package/dist/esm/google/protobuf/api_pb.js +1 -1
  97. package/dist/esm/google/protobuf/compiler/plugin_pb.js +1 -1
  98. package/dist/esm/google/protobuf/descriptor_pb.js +2 -2
  99. package/dist/esm/google/protobuf/duration_pb.js +1 -1
  100. package/dist/esm/google/protobuf/empty_pb.js +1 -1
  101. package/dist/esm/google/protobuf/field_mask_pb.js +1 -1
  102. package/dist/esm/google/protobuf/source_context_pb.js +1 -1
  103. package/dist/esm/google/protobuf/struct_pb.js +2 -2
  104. package/dist/esm/google/protobuf/timestamp_pb.js +1 -1
  105. package/dist/esm/google/protobuf/type_pb.js +2 -2
  106. package/dist/esm/google/protobuf/wrappers_pb.js +1 -1
  107. package/dist/esm/index.d.ts +3 -1
  108. package/dist/esm/index.js +2 -1
  109. package/dist/esm/json-format.d.ts +5 -5
  110. package/dist/esm/json-format.js +1 -1
  111. package/dist/esm/message-type.js +1 -1
  112. package/dist/esm/message.js +1 -1
  113. package/dist/esm/private/assert.js +1 -1
  114. package/dist/esm/private/binary-format-common.d.ts +2 -3
  115. package/dist/esm/private/binary-format-common.js +80 -77
  116. package/dist/esm/private/binary-format-proto2.js +59 -64
  117. package/dist/esm/private/binary-format-proto3.js +44 -40
  118. package/dist/esm/private/enum.js +1 -1
  119. package/dist/esm/private/extensions.d.ts +34 -0
  120. package/dist/esm/private/extensions.js +81 -0
  121. package/dist/esm/private/feature-set.d.ts +3 -3
  122. package/dist/esm/private/feature-set.js +27 -32
  123. package/dist/esm/private/field-list.js +1 -1
  124. package/dist/esm/private/field-wrapper.d.ts +2 -2
  125. package/dist/esm/private/field-wrapper.js +1 -1
  126. package/dist/esm/private/field.js +1 -1
  127. package/dist/esm/private/json-format-common.js +202 -156
  128. package/dist/esm/private/json-format-proto2.js +2 -1
  129. package/dist/esm/private/json-format-proto3.js +7 -5
  130. package/dist/esm/private/message-type.js +1 -1
  131. package/dist/esm/private/names.d.ts +2 -2
  132. package/dist/esm/private/names.js +3 -2
  133. package/dist/esm/private/options-map.js +1 -1
  134. package/dist/esm/private/proto-runtime.d.ts +6 -0
  135. package/dist/esm/private/proto-runtime.js +5 -1
  136. package/dist/esm/private/reify-wkt.js +1 -1
  137. package/dist/esm/private/scalars.js +1 -1
  138. package/dist/esm/private/text-format.js +1 -1
  139. package/dist/esm/private/util-common.js +1 -1
  140. package/dist/esm/private/util.js +1 -1
  141. package/dist/esm/proto-base64.js +1 -1
  142. package/dist/esm/proto-delimited.js +1 -1
  143. package/dist/esm/proto-double.js +1 -1
  144. package/dist/esm/proto-int64.js +1 -1
  145. package/dist/esm/proto2.js +1 -1
  146. package/dist/esm/proto3.js +1 -1
  147. package/dist/esm/service-type.js +1 -1
  148. package/dist/esm/to-plain-message.js +1 -1
  149. package/dist/esm/type-registry.d.ts +14 -0
  150. package/dist/esm/type-registry.js +1 -1
  151. package/package.json +1 -1
@@ -1,4 +1,4 @@
1
- import type { IEnumTypeRegistry, IMessageTypeRegistry, IServiceTypeRegistry } from "./type-registry.js";
1
+ import type { IEnumTypeRegistry, IExtensionRegistry, IMessageTypeRegistry, IServiceTypeRegistry } from "./type-registry.js";
2
2
  import { FileDescriptorSet } from "./google/protobuf/descriptor_pb.js";
3
3
  import type { DescriptorSet } from "./descriptor-set.js";
4
4
  /**
@@ -12,4 +12,4 @@ import type { DescriptorSet } from "./descriptor-set.js";
12
12
  * By default, all well-known types with a specialized JSON representation
13
13
  * are replaced with their generated counterpart in this package.
14
14
  */
15
- export declare function createRegistryFromDescriptors(input: DescriptorSet | FileDescriptorSet | Uint8Array, replaceWkt?: boolean): IMessageTypeRegistry & IEnumTypeRegistry & IServiceTypeRegistry;
15
+ export declare function createRegistryFromDescriptors(input: DescriptorSet | FileDescriptorSet | Uint8Array, replaceWkt?: boolean): IMessageTypeRegistry & IEnumTypeRegistry & IExtensionRegistry & IServiceTypeRegistry;
@@ -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.
@@ -64,15 +64,17 @@ export function createRegistryFromDescriptors(input, replaceWkt = true) {
64
64
  const set = input instanceof Uint8Array || input instanceof FileDescriptorSet
65
65
  ? createDescriptorSet(input)
66
66
  : input;
67
- const enums = {};
68
- const messages = {};
67
+ const enums = new Map();
68
+ const messages = new Map();
69
+ const extensions = new Map();
70
+ const extensionsByExtendee = new Map();
69
71
  const services = {};
70
72
  if (replaceWkt) {
71
73
  for (const mt of wkMessages) {
72
- messages[mt.typeName] = mt;
74
+ messages.set(mt.typeName, mt);
73
75
  }
74
76
  for (const et of wkEnums) {
75
- enums[et.typeName] = et;
77
+ enums.set(et.typeName, et);
76
78
  }
77
79
  }
78
80
  return {
@@ -80,7 +82,7 @@ export function createRegistryFromDescriptors(input, replaceWkt = true) {
80
82
  * May raise an error on invalid descriptors.
81
83
  */
82
84
  findEnum(typeName) {
83
- const existing = enums[typeName];
85
+ const existing = enums.get(typeName);
84
86
  if (existing) {
85
87
  return existing;
86
88
  }
@@ -94,14 +96,14 @@ export function createRegistryFromDescriptors(input, replaceWkt = true) {
94
96
  name: u.name,
95
97
  localName: localName(u),
96
98
  })), {});
97
- enums[typeName] = type;
99
+ enums.set(typeName, type);
98
100
  return type;
99
101
  },
100
102
  /**
101
103
  * May raise an error on invalid descriptors.
102
104
  */
103
105
  findMessage(typeName) {
104
- const existing = messages[typeName];
106
+ const existing = messages.get(typeName);
105
107
  if (existing) {
106
108
  return existing;
107
109
  }
@@ -114,7 +116,7 @@ export function createRegistryFromDescriptors(input, replaceWkt = true) {
114
116
  const type = runtime.makeMessageType(typeName, () => fields, {
115
117
  localName: localName(desc),
116
118
  });
117
- messages[typeName] = type;
119
+ messages.set(typeName, type);
118
120
  for (const field of desc.fields) {
119
121
  const fieldInfo = makeFieldInfo(field, this);
120
122
  fields.push(fieldInfo);
@@ -154,11 +156,53 @@ export function createRegistryFromDescriptors(input, replaceWkt = true) {
154
156
  methods,
155
157
  });
156
158
  },
159
+ /**
160
+ * May raise an error on invalid descriptors.
161
+ */
162
+ findExtensionFor(typeName, no) {
163
+ var _a;
164
+ if (!set.messages.has(typeName)) {
165
+ return undefined;
166
+ }
167
+ let extensionsByNo = extensionsByExtendee.get(typeName);
168
+ if (!extensionsByNo) {
169
+ // maintain a lookup for extension desc by number
170
+ extensionsByNo = new Map();
171
+ extensionsByExtendee.set(typeName, extensionsByNo);
172
+ for (const desc of set.extensions.values()) {
173
+ if (desc.extendee.typeName == typeName) {
174
+ extensionsByNo.set(desc.number, desc);
175
+ }
176
+ }
177
+ }
178
+ const desc = (_a = extensionsByExtendee.get(typeName)) === null || _a === void 0 ? void 0 : _a.get(no);
179
+ return desc ? this.findExtension(desc.typeName) : undefined;
180
+ },
181
+ /**
182
+ * May raise an error on invalid descriptors.
183
+ */
184
+ findExtension(typeName) {
185
+ const existing = extensions.get(typeName);
186
+ if (existing) {
187
+ return existing;
188
+ }
189
+ const desc = set.extensions.get(typeName);
190
+ if (!desc) {
191
+ return undefined;
192
+ }
193
+ const extendee = this.findMessage(desc.extendee.typeName);
194
+ assert(extendee, `message "${desc.extendee.typeName}" for ${desc.toString()} not found`);
195
+ const runtime = desc.file.syntax == "proto3" ? proto3 : proto2;
196
+ const ext = runtime.makeExtension(typeName, extendee, makeFieldInfo(desc, this));
197
+ extensions.set(typeName, ext);
198
+ return ext;
199
+ },
157
200
  };
158
201
  }
159
202
  function makeFieldInfo(desc, resolver) {
160
203
  switch (desc.fieldKind) {
161
204
  case "map":
205
+ assert(desc.kind == "field"); // maps are not allowed for extensions
162
206
  return makeMapFieldInfo(desc, resolver);
163
207
  case "message":
164
208
  return makeMessageFieldInfo(desc, resolver);
@@ -1,8 +1,9 @@
1
1
  import type { MessageType } from "./message-type.js";
2
2
  import type { EnumType } from "./enum.js";
3
3
  import type { ServiceType } from "./service-type.js";
4
- import type { IMessageTypeRegistry, IEnumTypeRegistry, IServiceTypeRegistry } from "./type-registry.js";
4
+ import type { IEnumTypeRegistry, IExtensionRegistry, IMessageTypeRegistry, IServiceTypeRegistry } from "./type-registry.js";
5
+ import type { Extension } from "./extension.js";
5
6
  /**
6
7
  * Create a new registry from the given types.
7
8
  */
8
- export declare function createRegistry(...types: Array<MessageType | EnumType | ServiceType>): IMessageTypeRegistry & IEnumTypeRegistry & IServiceTypeRegistry;
9
+ export declare function createRegistry(...types: Array<MessageType | EnumType | ServiceType | Extension>): IMessageTypeRegistry & IEnumTypeRegistry & IExtensionRegistry & IServiceTypeRegistry;
@@ -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.
@@ -18,42 +18,9 @@ export function createRegistry(...types) {
18
18
  const messages = {};
19
19
  const enums = {};
20
20
  const services = {};
21
+ const extensionsByName = new Map();
22
+ const extensionsByExtendee = new Map();
21
23
  const registry = {
22
- /**
23
- * Add a type to the registry. For messages, the types used in message
24
- * fields are added recursively. For services, the message types used
25
- * for requests and responses are added recursively.
26
- */
27
- add(type) {
28
- if ("fields" in type) {
29
- if (!this.findMessage(type.typeName)) {
30
- messages[type.typeName] = type;
31
- for (const field of type.fields.list()) {
32
- if (field.kind == "message") {
33
- this.add(field.T);
34
- }
35
- else if (field.kind == "map" && field.V.kind == "message") {
36
- this.add(field.V.T);
37
- }
38
- else if (field.kind == "enum") {
39
- this.add(field.T);
40
- }
41
- }
42
- }
43
- }
44
- else if ("methods" in type) {
45
- if (!this.findService(type.typeName)) {
46
- services[type.typeName] = type;
47
- for (const method of Object.values(type.methods)) {
48
- this.add(method.I);
49
- this.add(method.O);
50
- }
51
- }
52
- }
53
- else {
54
- enums[type.typeName] = type;
55
- }
56
- },
57
24
  findMessage(typeName) {
58
25
  return messages[typeName];
59
26
  },
@@ -63,9 +30,61 @@ export function createRegistry(...types) {
63
30
  findService(typeName) {
64
31
  return services[typeName];
65
32
  },
33
+ findExtensionFor(typeName, no) {
34
+ var _a, _b;
35
+ return (_b = (_a = extensionsByExtendee.get(typeName)) === null || _a === void 0 ? void 0 : _a.get(no)) !== null && _b !== void 0 ? _b : undefined;
36
+ },
37
+ findExtension(typeName) {
38
+ var _a;
39
+ return (_a = extensionsByName.get(typeName)) !== null && _a !== void 0 ? _a : undefined;
40
+ },
66
41
  };
42
+ function addType(type) {
43
+ var _a;
44
+ if ("fields" in type) {
45
+ if (!registry.findMessage(type.typeName)) {
46
+ messages[type.typeName] = type;
47
+ type.fields.list().forEach(addField);
48
+ }
49
+ }
50
+ else if ("methods" in type) {
51
+ if (!registry.findService(type.typeName)) {
52
+ services[type.typeName] = type;
53
+ for (const method of Object.values(type.methods)) {
54
+ addType(method.I);
55
+ addType(method.O);
56
+ }
57
+ }
58
+ }
59
+ else if ("extendee" in type) {
60
+ if (!extensionsByName.has(type.typeName)) {
61
+ extensionsByName.set(type.typeName, type);
62
+ const extendeeName = type.extendee.typeName;
63
+ if (!extensionsByExtendee.has(extendeeName)) {
64
+ extensionsByExtendee.set(extendeeName, new Map());
65
+ }
66
+ (_a = extensionsByExtendee.get(extendeeName)) === null || _a === void 0 ? void 0 : _a.set(type.field.no, type);
67
+ addType(type.extendee);
68
+ addField(type.field);
69
+ }
70
+ }
71
+ else {
72
+ enums[type.typeName] = type;
73
+ }
74
+ }
75
+ function addField(field) {
76
+ if (field.kind == "message") {
77
+ addType(field.T);
78
+ }
79
+ else if (field.kind == "map" && field.V.kind == "message") {
80
+ addType(field.V.T);
81
+ }
82
+ else if (field.kind == "enum") {
83
+ addType(field.T);
84
+ }
85
+ }
67
86
  for (const type of types) {
68
- registry.add(type);
87
+ addType(type);
69
88
  }
70
89
  return registry;
71
90
  }
@@ -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.
package/dist/esm/enum.js CHANGED
@@ -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,35 @@
1
+ import type { Message } from "./message.js";
2
+ import type { BinaryReadOptions, BinaryWriteOptions } from "./binary-format.js";
3
+ import type { Extension } from "./extension.js";
4
+ /**
5
+ * Retrieve an extension value from a message.
6
+ *
7
+ * The function never returns undefined. Use hasExtension() to check whether an
8
+ * extension is set. If the extension is not set, this function returns the
9
+ * default value (if one was specified in the protobuf source), or the zero value
10
+ * (for example `0` for numeric types, `[]` for repeated extension fields, and
11
+ * an empty message instance for message fields).
12
+ *
13
+ * Extensions are stored as unknown fields on a message. To mutate an extension
14
+ * value, make sure to store the new value with setExtension() after mutating.
15
+ *
16
+ * If the extension does not extend the given message, an error is raised.
17
+ */
18
+ export declare function getExtension<E extends Message<E>, V>(message: E, extension: Extension<E, V>, options?: Partial<BinaryReadOptions>): V;
19
+ /**
20
+ * Set an extension value on a message. If the message already has a value for
21
+ * this extension, the value is replaced.
22
+ *
23
+ * If the extension does not extend the given message, an error is raised.
24
+ */
25
+ export declare function setExtension<E extends Message<E>, V>(message: E, extension: Extension<E, V>, value: V, options?: Partial<BinaryReadOptions & BinaryWriteOptions>): void;
26
+ /**
27
+ * Remove an extension value from a message.
28
+ *
29
+ * If the extension does not extend the given message, an error is raised.
30
+ */
31
+ export declare function clearExtension<E extends Message<E>, V>(message: E, extension: Extension<E, V>): void;
32
+ /**
33
+ * Check whether an extension is set on a message.
34
+ */
35
+ export declare function hasExtension<E extends Message<E>, V>(message: E, extension: Extension<E, V>): boolean;
@@ -0,0 +1,101 @@
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 { assert } from "./private/assert.js";
15
+ import { createExtensionContainer, filterUnknownFields, } from "./private/extensions.js";
16
+ /**
17
+ * Retrieve an extension value from a message.
18
+ *
19
+ * The function never returns undefined. Use hasExtension() to check whether an
20
+ * extension is set. If the extension is not set, this function returns the
21
+ * default value (if one was specified in the protobuf source), or the zero value
22
+ * (for example `0` for numeric types, `[]` for repeated extension fields, and
23
+ * an empty message instance for message fields).
24
+ *
25
+ * Extensions are stored as unknown fields on a message. To mutate an extension
26
+ * value, make sure to store the new value with setExtension() after mutating.
27
+ *
28
+ * If the extension does not extend the given message, an error is raised.
29
+ */
30
+ export function getExtension(message, extension, options) {
31
+ assertExtendee(extension, message);
32
+ const opt = extension.runtime.bin.makeReadOptions(options);
33
+ const ufs = filterUnknownFields(message.getType().runtime.bin.listUnknownFields(message), extension.field);
34
+ const [container, get] = createExtensionContainer(extension);
35
+ for (const uf of ufs) {
36
+ extension.runtime.bin.readField(container, opt.readerFactory(uf.data), extension.field, uf.wireType, opt);
37
+ }
38
+ return get();
39
+ }
40
+ /**
41
+ * Set an extension value on a message. If the message already has a value for
42
+ * this extension, the value is replaced.
43
+ *
44
+ * If the extension does not extend the given message, an error is raised.
45
+ */
46
+ export function setExtension(message, extension, value, options) {
47
+ assertExtendee(extension, message);
48
+ const readOpt = extension.runtime.bin.makeReadOptions(options);
49
+ const writeOpt = extension.runtime.bin.makeWriteOptions(options);
50
+ if (hasExtension(message, extension)) {
51
+ const ufs = message
52
+ .getType()
53
+ .runtime.bin.listUnknownFields(message)
54
+ .filter((uf) => uf.no != extension.field.no);
55
+ message.getType().runtime.bin.discardUnknownFields(message);
56
+ for (const uf of ufs) {
57
+ message
58
+ .getType()
59
+ .runtime.bin.onUnknownField(message, uf.no, uf.wireType, uf.data);
60
+ }
61
+ }
62
+ const writer = writeOpt.writerFactory();
63
+ extension.runtime.bin.writeField(extension.field, value, writer, writeOpt);
64
+ const reader = readOpt.readerFactory(writer.finish());
65
+ while (reader.pos < reader.len) {
66
+ const [no, wireType] = reader.tag();
67
+ const data = reader.skip(wireType);
68
+ message.getType().runtime.bin.onUnknownField(message, no, wireType, data);
69
+ }
70
+ }
71
+ /**
72
+ * Remove an extension value from a message.
73
+ *
74
+ * If the extension does not extend the given message, an error is raised.
75
+ */
76
+ export function clearExtension(message, extension) {
77
+ assertExtendee(extension, message);
78
+ if (hasExtension(message, extension)) {
79
+ const bin = message.getType().runtime.bin;
80
+ const ufs = bin
81
+ .listUnknownFields(message)
82
+ .filter((uf) => uf.no != extension.field.no);
83
+ bin.discardUnknownFields(message);
84
+ for (const uf of ufs) {
85
+ bin.onUnknownField(message, uf.no, uf.wireType, uf.data);
86
+ }
87
+ }
88
+ }
89
+ /**
90
+ * Check whether an extension is set on a message.
91
+ */
92
+ export function hasExtension(message, extension) {
93
+ const messageType = message.getType();
94
+ return (extension.extendee.typeName === messageType.typeName &&
95
+ !!messageType.runtime.bin
96
+ .listUnknownFields(message)
97
+ .find((uf) => uf.no == extension.field.no));
98
+ }
99
+ function assertExtendee(extension, message) {
100
+ assert(extension.extendee.typeName == message.getType().typeName, `extension ${extension.typeName} can only be applied to message ${extension.extendee.typeName}`);
101
+ }
@@ -0,0 +1,24 @@
1
+ import type { FieldInfo } from "./field.js";
2
+ import type { AnyMessage, Message } from "./message.js";
3
+ import type { MessageType } from "./message-type.js";
4
+ import type { ProtoRuntime } from "./private/proto-runtime.js";
5
+ export interface Extension<E extends Message<E> = AnyMessage, V = unknown> {
6
+ /**
7
+ * The fully qualified name of the extension.
8
+ */
9
+ readonly typeName: string;
10
+ /**
11
+ * The message extended by this extension.
12
+ */
13
+ readonly extendee: MessageType<E>;
14
+ /**
15
+ * Field information for this extension. Note that required fields, maps,
16
+ * oneof are not allowed in extensions. Behavior of "localName" property is
17
+ * undefined and must not be relied upon.
18
+ */
19
+ readonly field: FieldInfo;
20
+ /**
21
+ * Provides serialization and other functionality.
22
+ */
23
+ readonly runtime: ProtoRuntime;
24
+ }
@@ -0,0 +1,14 @@
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
+ export {};
@@ -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.
package/dist/esm/field.js CHANGED
@@ -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.
@@ -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.
@@ -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.
@@ -18,7 +18,7 @@
18
18
  // The messages in this file describe the definitions found in .proto files.
19
19
  // A valid .proto file can be translated directly to a FileDescriptorProto
20
20
  // without any other information (e.g. without reading its imports).
21
- // @generated by protoc-gen-es v1.6.0 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
21
+ // @generated by protoc-gen-es v1.7.0 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
22
22
  // @generated from file google/protobuf/descriptor.proto (package google.protobuf, syntax proto2)
23
23
  /* eslint-disable */
24
24
  import { proto2 } from "../../proto2.js";
@@ -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.
@@ -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.
@@ -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.
@@ -11,7 +11,7 @@
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
- // @generated by protoc-gen-es v1.6.0 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
14
+ // @generated by protoc-gen-es v1.7.0 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
15
15
  // @generated from file google/protobuf/struct.proto (package google.protobuf, syntax proto3)
16
16
  /* eslint-disable */
17
17
  import { proto3 } from "../../proto3.js";
@@ -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.
@@ -11,7 +11,7 @@
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
- // @generated by protoc-gen-es v1.6.0 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
14
+ // @generated by protoc-gen-es v1.7.0 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts"
15
15
  // @generated from file google/protobuf/type.proto (package google.protobuf, syntax proto3)
16
16
  /* eslint-disable */
17
17
  import { proto3 } from "../../proto3.js";
@@ -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.
@@ -12,6 +12,8 @@ export type { FieldList } from "./field-list.js";
12
12
  export { ScalarType, LongType } from "./field.js";
13
13
  export type { MessageType } from "./message-type.js";
14
14
  export type { EnumType, EnumValueInfo } from "./enum.js";
15
+ export type { Extension } from "./extension.js";
16
+ export { getExtension, setExtension, hasExtension, clearExtension, } from "./extension-accessor.js";
15
17
  export type { ServiceType, MethodInfo, MethodInfoUnary, MethodInfoServerStreaming, MethodInfoClientStreaming, MethodInfoBiDiStreaming, } from "./service-type.js";
16
18
  export { MethodKind, MethodIdempotency } from "./service-type.js";
17
19
  export { WireType, BinaryWriter, BinaryReader } from "./binary-encoding.js";
@@ -20,7 +22,7 @@ export type { BinaryFormat, BinaryWriteOptions, BinaryReadOptions, } from "./bin
20
22
  export type { JsonFormat, JsonObject, JsonValue, JsonReadOptions, JsonWriteOptions, JsonWriteStringOptions, } from "./json-format.js";
21
23
  export type { DescriptorSet, AnyDesc, DescFile, DescEnum, DescEnumValue, DescMessage, DescOneof, DescField, DescService, DescMethod, DescExtension, DescComments, } from "./descriptor-set.js";
22
24
  export { createDescriptorSet } from "./create-descriptor-set.js";
23
- export type { IMessageTypeRegistry } from "./type-registry.js";
25
+ export type { IMessageTypeRegistry, IExtensionRegistry, } from "./type-registry.js";
24
26
  export { createRegistry } from "./create-registry.js";
25
27
  export { createRegistryFromDescriptors } from "./create-registry-from-desc.js";
26
28
  export { toPlainMessage } from "./to-plain-message.js";
package/dist/esm/index.js CHANGED
@@ -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.
@@ -20,6 +20,7 @@ export { protoDelimited } from "./proto-delimited.js";
20
20
  export { codegenInfo } from "./codegen-info.js";
21
21
  export { Message } from "./message.js";
22
22
  export { ScalarType, LongType } from "./field.js";
23
+ export { getExtension, setExtension, hasExtension, clearExtension, } from "./extension-accessor.js";
23
24
  export { MethodKind, MethodIdempotency } from "./service-type.js";
24
25
  export { WireType, BinaryWriter, BinaryReader } from "./binary-encoding.js";
25
26
  export { createDescriptorSet } from "./create-descriptor-set.js";
@@ -2,7 +2,7 @@ import type { Message } from "./message.js";
2
2
  import type { MessageType } from "./message-type.js";
3
3
  import type { ScalarType } from "./field.js";
4
4
  import { LongType } from "./field.js";
5
- import type { IMessageTypeRegistry } from "./type-registry.js";
5
+ import type { IExtensionRegistry, IMessageTypeRegistry } from "./type-registry.js";
6
6
  /**
7
7
  * JsonFormat is the contract for serializing messages to and from JSON.
8
8
  * Implementations may be specific to a proto syntax, and can be reflection
@@ -52,10 +52,10 @@ export interface JsonReadOptions {
52
52
  */
53
53
  ignoreUnknownFields: boolean;
54
54
  /**
55
- * This option is required to read `google.protobuf.Any`
55
+ * This option is required to read `google.protobuf.Any` and extensions
56
56
  * from JSON format.
57
57
  */
58
- typeRegistry?: IMessageTypeRegistry;
58
+ typeRegistry?: IMessageTypeRegistry & Partial<IExtensionRegistry>;
59
59
  }
60
60
  /**
61
61
  * Options for serializing to JSON.
@@ -83,10 +83,10 @@ export interface JsonWriteOptions {
83
83
  */
84
84
  useProtoFieldName: boolean;
85
85
  /**
86
- * This option is required to write `google.protobuf.Any`
86
+ * This option is required to write `google.protobuf.Any` and extensions
87
87
  * to JSON format.
88
88
  */
89
- typeRegistry?: IMessageTypeRegistry;
89
+ typeRegistry?: IMessageTypeRegistry & Partial<IExtensionRegistry>;
90
90
  }
91
91
  /**
92
92
  * Options for serializing to JSON.
@@ -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.