Debian Codegraph @

node/@bufbuild/protobuf/dist/commonjs/extensions.js

raw
1 "use strict";
2 // Copyright 2021-2026 Buf Technologies, Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 Object.defineProperty(exports, "__esModule", { value: true });
16 exports.getExtension = getExtension;
17 exports.setExtension = setExtension;
18 exports.clearExtension = clearExtension;
19 exports.hasExtension = hasExtension;
20 exports.hasOption = hasOption;
21 exports.getOption = getOption;
22 exports.createExtensionContainer = createExtensionContainer;
23 const create_js_1 = require("./create.js");
24 const from_binary_js_1 = require("./from-binary.js");
25 const reflect_js_1 = require("./reflect/reflect.js");
26 const scalar_js_1 = require("./reflect/scalar.js");
27 const to_binary_js_1 = require("./to-binary.js");
28 const binary_encoding_js_1 = require("./wire/binary-encoding.js");
29 const wrappers_js_1 = require("./wkt/wrappers.js");
30 /**
31 * Retrieve an extension value from a message.
32 *
33 * The function never returns undefined. Use hasExtension() to check whether an
34 * extension is set. If the extension is not set, this function returns the
35 * default value (if one was specified in the protobuf source), or the zero value
36 * (for example `0` for numeric types, `[]` for repeated extension fields, and
37 * an empty message instance for message fields).
38 *
39 * Extensions are stored as unknown fields on a message. To mutate an extension
40 * value, make sure to store the new value with setExtension() after mutating.
41 *
42 * If the extension does not extend the given message, an error is raised.
43 */
44 function getExtension(message, extension, options) {
45 assertExtendee(extension, message);
46 const ufs = filterUnknownFields(message.$unknown, extension);
47 const [container, field, get] = createExtensionContainer(extension);
48 const ctx = (0, from_binary_js_1.makeReadContext)(options);
49 for (const uf of ufs) {
50 (0, from_binary_js_1.readField)(container, new binary_encoding_js_1.BinaryReader(uf.data), field, uf.wireType, ctx);
51 }
52 return get();
53 }
54 /**
55 * Set an extension value on a message. If the message already has a value for
56 * this extension, the value is replaced.
57 *
58 * If the extension does not extend the given message, an error is raised.
59 */
60 function setExtension(message, extension, value) {
61 var _a;
62 assertExtendee(extension, message);
63 const ufs = ((_a = message.$unknown) !== null && _a !== void 0 ? _a : []).filter((uf) => uf.no !== extension.number);
64 const [container, field] = createExtensionContainer(extension, value);
65 const writer = new binary_encoding_js_1.BinaryWriter();
66 (0, to_binary_js_1.writeField)(writer, { writeUnknownFields: true }, container, field);
67 const reader = new binary_encoding_js_1.BinaryReader(writer.finish());
68 while (reader.pos < reader.len) {
69 const [no, wireType] = reader.tag();
70 const data = reader.skip(wireType, no);
71 ufs.push({ no, wireType, data });
72 }
73 message.$unknown = ufs;
74 }
75 /**
76 * Remove an extension value from a message.
77 *
78 * If the extension does not extend the given message, an error is raised.
79 */
80 function clearExtension(message, extension) {
81 assertExtendee(extension, message);
82 if (message.$unknown === undefined) {
83 return;
84 }
85 message.$unknown = message.$unknown.filter((uf) => uf.no !== extension.number);
86 }
87 /**
88 * Check whether an extension is set on a message.
89 */
90 function hasExtension(message, extension) {
91 var _a;
92 return (extension.extendee.typeName === message.$typeName &&
93 !!((_a = message.$unknown) === null || _a === void 0 ? void 0 : _a.find((uf) => uf.no === extension.number)));
94 }
95 /**
96 * Check whether an option is set on a descriptor.
97 *
98 * Options are extensions to the `google.protobuf.*Options` messages defined in
99 * google/protobuf/descriptor.proto. This function gets the option message from
100 * the descriptor, and calls hasExtension().
101 */
102 function hasOption(element, option) {
103 const message = element.proto.options;
104 if (!message) {
105 return false;
106 }
107 return hasExtension(message, option);
108 }
109 /**
110 * Retrieve an option value from a descriptor.
111 *
112 * Options are extensions to the `google.protobuf.*Options` messages defined in
113 * google/protobuf/descriptor.proto. This function gets the option message from
114 * the descriptor, and calls getExtension(). Same as getExtension(), this
115 * function never returns undefined.
116 */
117 function getOption(element, option) {
118 const message = element.proto.options;
119 if (!message) {
120 const [, , get] = createExtensionContainer(option);
121 return get();
122 }
123 return getExtension(message, option);
124 }
125 function filterUnknownFields(unknownFields, extension) {
126 if (unknownFields === undefined)
127 return [];
128 if (extension.fieldKind === "enum" || extension.fieldKind === "scalar") {
129 // singular scalar fields do not merge, we pick the last
130 for (let i = unknownFields.length - 1; i >= 0; --i) {
131 if (unknownFields[i].no == extension.number) {
132 return [unknownFields[i]];
133 }
134 }
135 return [];
136 }
137 return unknownFields.filter((uf) => uf.no === extension.number);
138 }
139 /**
140 * @private
141 */
142 function createExtensionContainer(extension, value) {
143 const localName = extension.typeName;
144 const field = Object.assign(Object.assign({}, extension), { kind: "field", parent: extension.extendee, localName });
145 const desc = Object.assign(Object.assign({}, extension.extendee), { fields: [field], members: [field], oneofs: [] });
146 const container = (0, create_js_1.create)(desc, value !== undefined ? { [localName]: value } : undefined);
147 return [
148 (0, reflect_js_1.reflect)(desc, container),
149 field,
150 () => {
151 const value = container[localName];
152 if (value === undefined) {
153 // biome-ignore lint/style/noNonNullAssertion: Only message fields are undefined, rest will have a zero value.
154 const desc = extension.message;
155 if ((0, wrappers_js_1.isWrapperDesc)(desc)) {
156 return (0, scalar_js_1.scalarZeroValue)(desc.fields[0].scalar, desc.fields[0].longAsString);
157 }
158 return (0, create_js_1.create)(desc);
159 }
160 return value;
161 },
162 ];
163 }
164 function assertExtendee(extension, message) {
165 if (extension.extendee.typeName != message.$typeName) {
166 throw new Error(`extension ${extension.typeName} can only be applied to message ${extension.extendee.typeName}`);
167 }
168 }