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