Debian Codegraph @

node/@bufbuild/protobuf/dist/esm/codegenv2/embed.js

raw
1 // Copyright 2021-2026 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 { protoCamelCase } from "../reflect/names.js";
15 import { isFieldSet, clearField } from "../fields.js";
16 import { base64Encode } from "../wire/base64-encoding.js";
17 import { toBinary } from "../to-binary.js";
18 import { clone } from "../clone.js";
19 import { Edition, FieldDescriptorProtoSchema, FieldOptionsSchema, FileDescriptorProtoSchema, DescriptorProtoSchema, EnumDescriptorProtoSchema, } from "../wkt/gen/google/protobuf/descriptor_pb.js";
20 /**
21 * Create necessary information to embed a file descriptor in
22 * generated code.
23 *
24 * @private
25 */
26 export function embedFileDesc(file) {
27 const embed = {
28 bootable: false,
29 proto() {
30 const stripped = clone(FileDescriptorProtoSchema, file);
31 clearField(stripped, FileDescriptorProtoSchema.field.dependency);
32 clearField(stripped, FileDescriptorProtoSchema.field.sourceCodeInfo);
33 stripped.messageType.map(stripJsonNames);
34 return stripped;
35 },
36 base64() {
37 const bytes = toBinary(FileDescriptorProtoSchema, this.proto());
38 return base64Encode(bytes, "std_raw");
39 },
40 };
41 return file.name == "google/protobuf/descriptor.proto"
42 ? Object.assign(Object.assign({}, embed), { bootable: true, boot() {
43 return createFileDescriptorProtoBoot(this.proto());
44 } }) : embed;
45 }
46 function stripJsonNames(d) {
47 for (const f of d.field) {
48 if (f.jsonName === protoCamelCase(f.name)) {
49 clearField(f, FieldDescriptorProtoSchema.field.jsonName);
50 }
51 }
52 for (const n of d.nestedType) {
53 stripJsonNames(n);
54 }
55 }
56 /**
57 * Compute the path to a message, enumeration, extension, or service in a
58 * file descriptor.
59 *
60 * @private
61 */
62 export function pathInFileDesc(desc) {
63 if (desc.kind == "service") {
64 return [desc.file.services.indexOf(desc)];
65 }
66 const parent = desc.parent;
67 if (parent == undefined) {
68 switch (desc.kind) {
69 case "enum":
70 return [desc.file.enums.indexOf(desc)];
71 case "message":
72 return [desc.file.messages.indexOf(desc)];
73 case "extension":
74 return [desc.file.extensions.indexOf(desc)];
75 }
76 }
77 function findPath(cur) {
78 const nested = [];
79 for (let parent = cur.parent; parent;) {
80 const idx = parent.nestedMessages.indexOf(cur);
81 nested.unshift(idx);
82 cur = parent;
83 parent = cur.parent;
84 }
85 nested.unshift(cur.file.messages.indexOf(cur));
86 return nested;
87 }
88 const path = findPath(parent);
89 switch (desc.kind) {
90 case "extension":
91 return [...path, parent.nestedExtensions.indexOf(desc)];
92 case "message":
93 return [...path, parent.nestedMessages.indexOf(desc)];
94 case "enum":
95 return [...path, parent.nestedEnums.indexOf(desc)];
96 }
97 }
98 /**
99 * The file descriptor for google/protobuf/descriptor.proto cannot be embedded
100 * in serialized form, since it is required to parse itself.
101 *
102 * This function takes an instance of the message, and returns a plain object
103 * that can be hydrated to the message again via bootFileDescriptorProto().
104 *
105 * This function only works with a message google.protobuf.FileDescriptorProto
106 * for google/protobuf/descriptor.proto, and only supports features that are
107 * relevant for the specific use case. For example, it discards file options,
108 * reserved ranges and reserved names, and field options that are unused in
109 * descriptor.proto.
110 *
111 * @private
112 */
113 export function createFileDescriptorProtoBoot(proto) {
114 var _a;
115 assert(proto.name == "google/protobuf/descriptor.proto");
116 assert(proto.package == "google.protobuf");
117 assert(!proto.dependency.length);
118 assert(!proto.publicDependency.length);
119 assert(!proto.weakDependency.length);
120 assert(!proto.optionDependency.length);
121 assert(!proto.service.length);
122 assert(!proto.extension.length);
123 assert(proto.sourceCodeInfo === undefined);
124 assert(proto.syntax == "" || proto.syntax == "proto2");
125 assert(!((_a = proto.options) === null || _a === void 0 ? void 0 : _a.features)); // we're dropping file options
126 assert(proto.edition === Edition.EDITION_UNKNOWN);
127 return {
128 name: proto.name,
129 package: proto.package,
130 messageType: proto.messageType.map(createDescriptorBoot),
131 enumType: proto.enumType.map(createEnumDescriptorBoot),
132 };
133 }
134 function createDescriptorBoot(proto) {
135 assert(proto.extension.length == 0);
136 assert(!proto.oneofDecl.length);
137 assert(!proto.options);
138 assert(!isFieldSet(proto, DescriptorProtoSchema.field.visibility));
139 const b = {
140 name: proto.name,
141 };
142 if (proto.field.length) {
143 b.field = proto.field.map(createFieldDescriptorBoot);
144 }
145 if (proto.nestedType.length) {
146 b.nestedType = proto.nestedType.map(createDescriptorBoot);
147 }
148 if (proto.enumType.length) {
149 b.enumType = proto.enumType.map(createEnumDescriptorBoot);
150 }
151 if (proto.extensionRange.length) {
152 b.extensionRange = proto.extensionRange.map((r) => {
153 assert(!r.options);
154 return { start: r.start, end: r.end };
155 });
156 }
157 return b;
158 }
159 function createFieldDescriptorBoot(proto) {
160 assert(isFieldSet(proto, FieldDescriptorProtoSchema.field.name));
161 assert(isFieldSet(proto, FieldDescriptorProtoSchema.field.number));
162 assert(isFieldSet(proto, FieldDescriptorProtoSchema.field.type));
163 assert(!isFieldSet(proto, FieldDescriptorProtoSchema.field.oneofIndex));
164 assert(!isFieldSet(proto, FieldDescriptorProtoSchema.field.jsonName) ||
165 proto.jsonName === protoCamelCase(proto.name));
166 const b = {
167 name: proto.name,
168 number: proto.number,
169 type: proto.type,
170 };
171 if (isFieldSet(proto, FieldDescriptorProtoSchema.field.label)) {
172 b.label = proto.label;
173 }
174 if (isFieldSet(proto, FieldDescriptorProtoSchema.field.typeName)) {
175 b.typeName = proto.typeName;
176 }
177 if (isFieldSet(proto, FieldDescriptorProtoSchema.field.extendee)) {
178 b.extendee = proto.extendee;
179 }
180 if (isFieldSet(proto, FieldDescriptorProtoSchema.field.defaultValue)) {
181 b.defaultValue = proto.defaultValue;
182 }
183 if (proto.options) {
184 b.options = createFieldOptionsBoot(proto.options);
185 }
186 return b;
187 }
188 function createFieldOptionsBoot(proto) {
189 const b = {};
190 assert(!isFieldSet(proto, FieldOptionsSchema.field.ctype));
191 if (isFieldSet(proto, FieldOptionsSchema.field.packed)) {
192 b.packed = proto.packed;
193 }
194 assert(!isFieldSet(proto, FieldOptionsSchema.field.jstype));
195 assert(!isFieldSet(proto, FieldOptionsSchema.field.lazy));
196 assert(!isFieldSet(proto, FieldOptionsSchema.field.unverifiedLazy));
197 if (isFieldSet(proto, FieldOptionsSchema.field.deprecated)) {
198 b.deprecated = proto.deprecated;
199 }
200 assert(!isFieldSet(proto, FieldOptionsSchema.field.weak));
201 assert(!isFieldSet(proto, FieldOptionsSchema.field.debugRedact));
202 if (isFieldSet(proto, FieldOptionsSchema.field.retention)) {
203 b.retention = proto.retention;
204 }
205 if (proto.targets.length) {
206 b.targets = proto.targets;
207 }
208 if (proto.editionDefaults.length) {
209 b.editionDefaults = proto.editionDefaults.map((d) => ({
210 value: d.value,
211 edition: d.edition,
212 }));
213 }
214 assert(!isFieldSet(proto, FieldOptionsSchema.field.features));
215 assert(!isFieldSet(proto, FieldOptionsSchema.field.uninterpretedOption));
216 return b;
217 }
218 function createEnumDescriptorBoot(proto) {
219 assert(!proto.options);
220 assert(!isFieldSet(proto, EnumDescriptorProtoSchema.field.visibility));
221 return {
222 name: proto.name,
223 value: proto.value.map((v) => {
224 assert(!v.options);
225 return {
226 name: v.name,
227 number: v.number,
228 };
229 }),
230 };
231 }
232 /**
233 * Assert that condition is truthy or throw error.
234 */
235 function assert(condition) {
236 if (!condition) {
237 throw new Error();
238 }
239 }