| 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.maximumEdition = exports.minimumEdition = void 0; |
| 17 |
exports.createRegistry = createRegistry; |
| 18 |
exports.createMutableRegistry = createMutableRegistry; |
| 19 |
exports.createFileRegistry = createFileRegistry; |
| 20 |
const descriptors_js_1 = require("./descriptors.js"); |
| 21 |
const text_format_js_1 = require("./wire/text-format.js"); |
| 22 |
const nested_types_js_1 = require("./reflect/nested-types.js"); |
| 23 |
const unsafe_js_1 = require("./reflect/unsafe.js"); |
| 24 |
const names_js_1 = require("./reflect/names.js"); |
| 25 |
/** |
| 26 |
* Create a registry from the given inputs. |
| 27 |
* |
| 28 |
* An input can be: |
| 29 |
* - Any message, enum, service, or extension descriptor, which adds just the |
| 30 |
* descriptor for this type. |
| 31 |
* - A file descriptor, which adds all typed defined in this file. |
| 32 |
* - A registry, which adds all types from the registry. |
| 33 |
* |
| 34 |
* For duplicate descriptors (same type name), the one given last wins. |
| 35 |
*/ |
| 36 |
function createRegistry(...input) { |
| 37 |
return initBaseRegistry(input); |
| 38 |
} |
| 39 |
/** |
| 40 |
* Create a registry that allows adding and removing descriptors. |
| 41 |
*/ |
| 42 |
function createMutableRegistry(...input) { |
| 43 |
const reg = initBaseRegistry(input); |
| 44 |
return Object.assign(Object.assign({}, reg), { remove(desc) { |
| 45 |
var _a; |
| 46 |
if (desc.kind == "extension") { |
| 47 |
(_a = reg.extendees.get(desc.extendee.typeName)) === null || _a === void 0 ? void 0 : _a.delete(desc.number); |
| 48 |
} |
| 49 |
reg.types.delete(desc.typeName); |
| 50 |
} }); |
| 51 |
} |
| 52 |
function createFileRegistry(...args) { |
| 53 |
const registry = createBaseRegistry(); |
| 54 |
if (!args.length) { |
| 55 |
return registry; |
| 56 |
} |
| 57 |
if ("$typeName" in args[0] && |
| 58 |
args[0].$typeName == "google.protobuf.FileDescriptorSet") { |
| 59 |
for (const file of args[0].file) { |
| 60 |
addFile(file, registry); |
| 61 |
} |
| 62 |
return registry; |
| 63 |
} |
| 64 |
if ("$typeName" in args[0]) { |
| 65 |
const input = args[0]; |
| 66 |
const resolve = args[1]; |
| 67 |
const seen = new Set(); |
| 68 |
function recurseDeps(file) { |
| 69 |
const deps = []; |
| 70 |
for (const protoFileName of file.dependency) { |
| 71 |
if (registry.getFile(protoFileName) != undefined) { |
| 72 |
continue; |
| 73 |
} |
| 74 |
if (seen.has(protoFileName)) { |
| 75 |
continue; |
| 76 |
} |
| 77 |
const dep = resolve(protoFileName); |
| 78 |
if (!dep) { |
| 79 |
throw new Error(`Unable to resolve ${protoFileName}, imported by ${file.name}`); |
| 80 |
} |
| 81 |
if ("kind" in dep) { |
| 82 |
registry.addFile(dep, false, true); |
| 83 |
} |
| 84 |
else { |
| 85 |
seen.add(dep.name); |
| 86 |
deps.push(dep); |
| 87 |
} |
| 88 |
} |
| 89 |
return deps.concat(...deps.map(recurseDeps)); |
| 90 |
} |
| 91 |
for (const file of [input, ...recurseDeps(input)].reverse()) { |
| 92 |
addFile(file, registry); |
| 93 |
} |
| 94 |
} |
| 95 |
else { |
| 96 |
for (const fileReg of args) { |
| 97 |
for (const file of fileReg.files) { |
| 98 |
registry.addFile(file); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
return registry; |
| 103 |
} |
| 104 |
/** |
| 105 |
* @private |
| 106 |
*/ |
| 107 |
function createBaseRegistry() { |
| 108 |
const types = new Map(); |
| 109 |
const extendees = new Map(); |
| 110 |
const files = new Map(); |
| 111 |
return { |
| 112 |
kind: "registry", |
| 113 |
types, |
| 114 |
extendees, |
| 115 |
[Symbol.iterator]() { |
| 116 |
return types.values(); |
| 117 |
}, |
| 118 |
get files() { |
| 119 |
return files.values(); |
| 120 |
}, |
| 121 |
addFile(file, skipTypes, withDeps) { |
| 122 |
files.set(file.proto.name, file); |
| 123 |
if (!skipTypes) { |
| 124 |
for (const type of (0, nested_types_js_1.nestedTypes)(file)) { |
| 125 |
this.add(type); |
| 126 |
} |
| 127 |
} |
| 128 |
if (withDeps) { |
| 129 |
for (const f of file.dependencies) { |
| 130 |
this.addFile(f, skipTypes, withDeps); |
| 131 |
} |
| 132 |
} |
| 133 |
}, |
| 134 |
add(desc) { |
| 135 |
if (desc.kind == "extension") { |
| 136 |
let numberToExt = extendees.get(desc.extendee.typeName); |
| 137 |
if (!numberToExt) { |
| 138 |
extendees.set(desc.extendee.typeName, |
| 139 |
// biome-ignore lint/suspicious/noAssignInExpressions: no |
| 140 |
(numberToExt = new Map())); |
| 141 |
} |
| 142 |
numberToExt.set(desc.number, desc); |
| 143 |
} |
| 144 |
types.set(desc.typeName, desc); |
| 145 |
}, |
| 146 |
get(typeName) { |
| 147 |
return types.get(typeName); |
| 148 |
}, |
| 149 |
getFile(fileName) { |
| 150 |
return files.get(fileName); |
| 151 |
}, |
| 152 |
getMessage(typeName) { |
| 153 |
const t = types.get(typeName); |
| 154 |
return (t === null || t === void 0 ? void 0 : t.kind) == "message" ? t : undefined; |
| 155 |
}, |
| 156 |
getEnum(typeName) { |
| 157 |
const t = types.get(typeName); |
| 158 |
return (t === null || t === void 0 ? void 0 : t.kind) == "enum" ? t : undefined; |
| 159 |
}, |
| 160 |
getExtension(typeName) { |
| 161 |
const t = types.get(typeName); |
| 162 |
return (t === null || t === void 0 ? void 0 : t.kind) == "extension" ? t : undefined; |
| 163 |
}, |
| 164 |
getExtensionFor(extendee, no) { |
| 165 |
var _a; |
| 166 |
return (_a = extendees.get(extendee.typeName)) === null || _a === void 0 ? void 0 : _a.get(no); |
| 167 |
}, |
| 168 |
getService(typeName) { |
| 169 |
const t = types.get(typeName); |
| 170 |
return (t === null || t === void 0 ? void 0 : t.kind) == "service" ? t : undefined; |
| 171 |
}, |
| 172 |
}; |
| 173 |
} |
| 174 |
/** |
| 175 |
* @private |
| 176 |
*/ |
| 177 |
function initBaseRegistry(inputs) { |
| 178 |
const registry = createBaseRegistry(); |
| 179 |
for (const input of inputs) { |
| 180 |
switch (input.kind) { |
| 181 |
case "registry": |
| 182 |
for (const n of input) { |
| 183 |
registry.add(n); |
| 184 |
} |
| 185 |
break; |
| 186 |
case "file": |
| 187 |
registry.addFile(input); |
| 188 |
break; |
| 189 |
default: |
| 190 |
registry.add(input); |
| 191 |
break; |
| 192 |
} |
| 193 |
} |
| 194 |
return registry; |
| 195 |
} |
| 196 |
// bootstrap-inject google.protobuf.Edition.EDITION_PROTO2: const $name = $number; |
| 197 |
const EDITION_PROTO2 = 998; |
| 198 |
// bootstrap-inject google.protobuf.Edition.EDITION_PROTO3: const $name = $number; |
| 199 |
const EDITION_PROTO3 = 999; |
| 200 |
// bootstrap-inject google.protobuf.Edition.EDITION_UNSTABLE: const $name = $number; |
| 201 |
const EDITION_UNSTABLE = 9999; |
| 202 |
// bootstrap-inject google.protobuf.FieldDescriptorProto.Type.TYPE_STRING: const $name = $number; |
| 203 |
const TYPE_STRING = 9; |
| 204 |
// bootstrap-inject google.protobuf.FieldDescriptorProto.Type.TYPE_GROUP: const $name = $number; |
| 205 |
const TYPE_GROUP = 10; |
| 206 |
// bootstrap-inject google.protobuf.FieldDescriptorProto.Type.TYPE_MESSAGE: const $name = $number; |
| 207 |
const TYPE_MESSAGE = 11; |
| 208 |
// bootstrap-inject google.protobuf.FieldDescriptorProto.Type.TYPE_BYTES: const $name = $number; |
| 209 |
const TYPE_BYTES = 12; |
| 210 |
// bootstrap-inject google.protobuf.FieldDescriptorProto.Type.TYPE_ENUM: const $name = $number; |
| 211 |
const TYPE_ENUM = 14; |
| 212 |
// bootstrap-inject google.protobuf.FieldDescriptorProto.Label.LABEL_REPEATED: const $name = $number; |
| 213 |
const LABEL_REPEATED = 3; |
| 214 |
// bootstrap-inject google.protobuf.FieldDescriptorProto.Label.LABEL_REQUIRED: const $name = $number; |
| 215 |
const LABEL_REQUIRED = 2; |
| 216 |
// bootstrap-inject google.protobuf.FieldOptions.JSType.JS_STRING: const $name = $number; |
| 217 |
const JS_STRING = 1; |
| 218 |
// bootstrap-inject google.protobuf.MethodOptions.IdempotencyLevel.IDEMPOTENCY_UNKNOWN: const $name = $number; |
| 219 |
const IDEMPOTENCY_UNKNOWN = 0; |
| 220 |
// bootstrap-inject google.protobuf.FeatureSet.FieldPresence.EXPLICIT: const $name = $number; |
| 221 |
const EXPLICIT = 1; |
| 222 |
// bootstrap-inject google.protobuf.FeatureSet.FieldPresence.IMPLICIT: const $name = $number; |
| 223 |
const IMPLICIT = 2; |
| 224 |
// bootstrap-inject google.protobuf.FeatureSet.FieldPresence.LEGACY_REQUIRED: const $name = $number; |
| 225 |
const LEGACY_REQUIRED = 3; |
| 226 |
// bootstrap-inject google.protobuf.FeatureSet.RepeatedFieldEncoding.PACKED: const $name = $number; |
| 227 |
const PACKED = 1; |
| 228 |
// bootstrap-inject google.protobuf.FeatureSet.MessageEncoding.DELIMITED: const $name = $number; |
| 229 |
const DELIMITED = 2; |
| 230 |
// bootstrap-inject google.protobuf.FeatureSet.EnumType.OPEN: const $name = $number; |
| 231 |
const OPEN = 1; |
| 232 |
// bootstrap-inject google.protobuf.FeatureSet.Utf8Validation.VERIFY: const $name = $number; |
| 233 |
const VERIFY = 2; |
| 234 |
// biome-ignore format: want this to read well |
| 235 |
// bootstrap-inject defaults: EDITION_PROTO2 to EDITION_2024: export const minimumEdition: SupportedEdition = $minimumEdition, maximumEdition: SupportedEdition = $maximumEdition; |
| 236 |
// generated from protoc v34.1 |
| 237 |
exports.minimumEdition = 998, exports.maximumEdition = 1001; |
| 238 |
const featureDefaults = { |
| 239 |
// EDITION_PROTO2 |
| 240 |
998: { |
| 241 |
fieldPresence: 1, // EXPLICIT, |
| 242 |
enumType: 2, // CLOSED, |
| 243 |
repeatedFieldEncoding: 2, // EXPANDED, |
| 244 |
utf8Validation: 3, // NONE, |
| 245 |
messageEncoding: 1, // LENGTH_PREFIXED, |
| 246 |
jsonFormat: 2, // LEGACY_BEST_EFFORT, |
| 247 |
enforceNamingStyle: 2, // STYLE_LEGACY, |
| 248 |
defaultSymbolVisibility: 1, // EXPORT_ALL, |
| 249 |
}, |
| 250 |
// EDITION_PROTO3 |
| 251 |
999: { |
| 252 |
fieldPresence: 2, // IMPLICIT, |
| 253 |
enumType: 1, // OPEN, |
| 254 |
repeatedFieldEncoding: 1, // PACKED, |
| 255 |
utf8Validation: 2, // VERIFY, |
| 256 |
messageEncoding: 1, // LENGTH_PREFIXED, |
| 257 |
jsonFormat: 1, // ALLOW, |
| 258 |
enforceNamingStyle: 2, // STYLE_LEGACY, |
| 259 |
defaultSymbolVisibility: 1, // EXPORT_ALL, |
| 260 |
}, |
| 261 |
// EDITION_2023 |
| 262 |
1000: { |
| 263 |
fieldPresence: 1, // EXPLICIT, |
| 264 |
enumType: 1, // OPEN, |
| 265 |
repeatedFieldEncoding: 1, // PACKED, |
| 266 |
utf8Validation: 2, // VERIFY, |
| 267 |
messageEncoding: 1, // LENGTH_PREFIXED, |
| 268 |
jsonFormat: 1, // ALLOW, |
| 269 |
enforceNamingStyle: 2, // STYLE_LEGACY, |
| 270 |
defaultSymbolVisibility: 1, // EXPORT_ALL, |
| 271 |
}, |
| 272 |
// EDITION_2024 |
| 273 |
1001: { |
| 274 |
fieldPresence: 1, // EXPLICIT, |
| 275 |
enumType: 1, // OPEN, |
| 276 |
repeatedFieldEncoding: 1, // PACKED, |
| 277 |
utf8Validation: 2, // VERIFY, |
| 278 |
messageEncoding: 1, // LENGTH_PREFIXED, |
| 279 |
jsonFormat: 1, // ALLOW, |
| 280 |
enforceNamingStyle: 1, // STYLE2024, |
| 281 |
defaultSymbolVisibility: 2, // EXPORT_TOP_LEVEL, |
| 282 |
}, |
| 283 |
}; |
| 284 |
/** |
| 285 |
* Create a descriptor for a file, add it to the registry. |
| 286 |
*/ |
| 287 |
function addFile(proto, reg) { |
| 288 |
var _a, _b; |
| 289 |
const file = { |
| 290 |
kind: "file", |
| 291 |
proto, |
| 292 |
deprecated: (_b = (_a = proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false, |
| 293 |
edition: getFileEdition(proto), |
| 294 |
name: proto.name.replace(/\.proto$/, ""), |
| 295 |
dependencies: findFileDependencies(proto, reg), |
| 296 |
enums: [], |
| 297 |
messages: [], |
| 298 |
extensions: [], |
| 299 |
services: [], |
| 300 |
toString() { |
| 301 |
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- we asserted above |
| 302 |
return `file ${proto.name}`; |
| 303 |
}, |
| 304 |
}; |
| 305 |
const mapEntriesStore = new Map(); |
| 306 |
const mapEntries = { |
| 307 |
get(typeName) { |
| 308 |
return mapEntriesStore.get(typeName); |
| 309 |
}, |
| 310 |
add(desc) { |
| 311 |
var _a; |
| 312 |
assert(((_a = desc.proto.options) === null || _a === void 0 ? void 0 : _a.mapEntry) === true); |
| 313 |
mapEntriesStore.set(desc.typeName, desc); |
| 314 |
}, |
| 315 |
}; |
| 316 |
for (const enumProto of proto.enumType) { |
| 317 |
addEnum(enumProto, file, undefined, reg); |
| 318 |
} |
| 319 |
for (const messageProto of proto.messageType) { |
| 320 |
addMessage(messageProto, file, undefined, reg, mapEntries); |
| 321 |
} |
| 322 |
for (const serviceProto of proto.service) { |
| 323 |
addService(serviceProto, file, reg); |
| 324 |
} |
| 325 |
addExtensions(file, reg); |
| 326 |
for (const mapEntry of mapEntriesStore.values()) { |
| 327 |
// to create a map field, we need access to the map entry's fields |
| 328 |
addFields(mapEntry, reg, mapEntries); |
| 329 |
} |
| 330 |
for (const message of file.messages) { |
| 331 |
addFields(message, reg, mapEntries); |
| 332 |
addExtensions(message, reg); |
| 333 |
} |
| 334 |
reg.addFile(file, true); |
| 335 |
} |
| 336 |
/** |
| 337 |
* Create descriptors for extensions, and add them to the message / file, |
| 338 |
* and to our cart. |
| 339 |
* Recurses into nested types. |
| 340 |
*/ |
| 341 |
function addExtensions(desc, reg) { |
| 342 |
switch (desc.kind) { |
| 343 |
case "file": |
| 344 |
for (const proto of desc.proto.extension) { |
| 345 |
const ext = newField(proto, desc, reg); |
| 346 |
desc.extensions.push(ext); |
| 347 |
reg.add(ext); |
| 348 |
} |
| 349 |
break; |
| 350 |
case "message": |
| 351 |
for (const proto of desc.proto.extension) { |
| 352 |
const ext = newField(proto, desc, reg); |
| 353 |
desc.nestedExtensions.push(ext); |
| 354 |
reg.add(ext); |
| 355 |
} |
| 356 |
for (const message of desc.nestedMessages) { |
| 357 |
addExtensions(message, reg); |
| 358 |
} |
| 359 |
break; |
| 360 |
} |
| 361 |
} |
| 362 |
/** |
| 363 |
* Create descriptors for fields and oneof groups, and add them to the message. |
| 364 |
* Recurses into nested types. |
| 365 |
*/ |
| 366 |
function addFields(message, reg, mapEntries) { |
| 367 |
const allOneofs = message.proto.oneofDecl.map((proto) => newOneof(proto, message)); |
| 368 |
const oneofsSeen = new Set(); |
| 369 |
for (const proto of message.proto.field) { |
| 370 |
const oneof = findOneof(proto, allOneofs); |
| 371 |
const field = newField(proto, message, reg, oneof, mapEntries); |
| 372 |
message.fields.push(field); |
| 373 |
message.field[field.localName] = field; |
| 374 |
if (oneof === undefined) { |
| 375 |
message.members.push(field); |
| 376 |
} |
| 377 |
else { |
| 378 |
oneof.fields.push(field); |
| 379 |
if (!oneofsSeen.has(oneof)) { |
| 380 |
oneofsSeen.add(oneof); |
| 381 |
message.members.push(oneof); |
| 382 |
} |
| 383 |
} |
| 384 |
} |
| 385 |
for (const oneof of allOneofs.filter((o) => oneofsSeen.has(o))) { |
| 386 |
message.oneofs.push(oneof); |
| 387 |
} |
| 388 |
for (const child of message.nestedMessages) { |
| 389 |
addFields(child, reg, mapEntries); |
| 390 |
} |
| 391 |
} |
| 392 |
/** |
| 393 |
* Create a descriptor for an enumeration, and add it our cart and to the |
| 394 |
* parent type, if any. |
| 395 |
*/ |
| 396 |
function addEnum(proto, file, parent, reg) { |
| 397 |
var _a, _b, _c, _d, _e; |
| 398 |
const sharedPrefix = findEnumSharedPrefix(proto.name, proto.value); |
| 399 |
const desc = { |
| 400 |
kind: "enum", |
| 401 |
proto, |
| 402 |
deprecated: (_b = (_a = proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false, |
| 403 |
file, |
| 404 |
parent, |
| 405 |
open: true, |
| 406 |
name: proto.name, |
| 407 |
typeName: makeTypeName(proto, parent, file), |
| 408 |
value: {}, |
| 409 |
values: [], |
| 410 |
sharedPrefix, |
| 411 |
toString() { |
| 412 |
return `enum ${this.typeName}`; |
| 413 |
}, |
| 414 |
}; |
| 415 |
desc.open = isEnumOpen(desc); |
| 416 |
reg.add(desc); |
| 417 |
for (const p of proto.value) { |
| 418 |
const name = p.name; |
| 419 |
desc.values.push( |
| 420 |
// biome-ignore lint/suspicious/noAssignInExpressions: no |
| 421 |
(desc.value[p.number] = { |
| 422 |
kind: "enum_value", |
| 423 |
proto: p, |
| 424 |
deprecated: (_d = (_c = p.options) === null || _c === void 0 ? void 0 : _c.deprecated) !== null && _d !== void 0 ? _d : false, |
| 425 |
parent: desc, |
| 426 |
name, |
| 427 |
localName: (0, names_js_1.safeObjectProperty)(sharedPrefix == undefined |
| 428 |
? name |
| 429 |
: name.substring(sharedPrefix.length)), |
| 430 |
number: p.number, |
| 431 |
toString() { |
| 432 |
return `enum value ${desc.typeName}.${name}`; |
| 433 |
}, |
| 434 |
})); |
| 435 |
} |
| 436 |
((_e = parent === null || parent === void 0 ? void 0 : parent.nestedEnums) !== null && _e !== void 0 ? _e : file.enums).push(desc); |
| 437 |
} |
| 438 |
/** |
| 439 |
* Create a descriptor for a message, including nested types, and add it to our |
| 440 |
* cart. Note that this does not create descriptors fields. |
| 441 |
*/ |
| 442 |
function addMessage(proto, file, parent, reg, mapEntries) { |
| 443 |
var _a, _b, _c, _d; |
| 444 |
const desc = { |
| 445 |
kind: "message", |
| 446 |
proto, |
| 447 |
deprecated: (_b = (_a = proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false, |
| 448 |
file, |
| 449 |
parent, |
| 450 |
name: proto.name, |
| 451 |
typeName: makeTypeName(proto, parent, file), |
| 452 |
fields: [], |
| 453 |
field: {}, |
| 454 |
oneofs: [], |
| 455 |
members: [], |
| 456 |
nestedEnums: [], |
| 457 |
nestedMessages: [], |
| 458 |
nestedExtensions: [], |
| 459 |
toString() { |
| 460 |
return `message ${this.typeName}`; |
| 461 |
}, |
| 462 |
}; |
| 463 |
if (((_c = proto.options) === null || _c === void 0 ? void 0 : _c.mapEntry) === true) { |
| 464 |
mapEntries.add(desc); |
| 465 |
} |
| 466 |
else { |
| 467 |
((_d = parent === null || parent === void 0 ? void 0 : parent.nestedMessages) !== null && _d !== void 0 ? _d : file.messages).push(desc); |
| 468 |
reg.add(desc); |
| 469 |
} |
| 470 |
for (const enumProto of proto.enumType) { |
| 471 |
addEnum(enumProto, file, desc, reg); |
| 472 |
} |
| 473 |
for (const messageProto of proto.nestedType) { |
| 474 |
addMessage(messageProto, file, desc, reg, mapEntries); |
| 475 |
} |
| 476 |
} |
| 477 |
/** |
| 478 |
* Create a descriptor for a service, including methods, and add it to our |
| 479 |
* cart. |
| 480 |
*/ |
| 481 |
function addService(proto, file, reg) { |
| 482 |
var _a, _b; |
| 483 |
const desc = { |
| 484 |
kind: "service", |
| 485 |
proto, |
| 486 |
deprecated: (_b = (_a = proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false, |
| 487 |
file, |
| 488 |
name: proto.name, |
| 489 |
typeName: makeTypeName(proto, undefined, file), |
| 490 |
methods: [], |
| 491 |
method: {}, |
| 492 |
toString() { |
| 493 |
return `service ${this.typeName}`; |
| 494 |
}, |
| 495 |
}; |
| 496 |
file.services.push(desc); |
| 497 |
reg.add(desc); |
| 498 |
for (const methodProto of proto.method) { |
| 499 |
const method = newMethod(methodProto, desc, reg); |
| 500 |
desc.methods.push(method); |
| 501 |
desc.method[method.localName] = method; |
| 502 |
} |
| 503 |
} |
| 504 |
/** |
| 505 |
* Create a descriptor for a method. |
| 506 |
*/ |
| 507 |
function newMethod(proto, parent, reg) { |
| 508 |
var _a, _b, _c, _d; |
| 509 |
let methodKind; |
| 510 |
if (proto.clientStreaming && proto.serverStreaming) { |
| 511 |
methodKind = "bidi_streaming"; |
| 512 |
} |
| 513 |
else if (proto.clientStreaming) { |
| 514 |
methodKind = "client_streaming"; |
| 515 |
} |
| 516 |
else if (proto.serverStreaming) { |
| 517 |
methodKind = "server_streaming"; |
| 518 |
} |
| 519 |
else { |
| 520 |
methodKind = "unary"; |
| 521 |
} |
| 522 |
const input = reg.getMessage(trimLeadingDot(proto.inputType)); |
| 523 |
const output = reg.getMessage(trimLeadingDot(proto.outputType)); |
| 524 |
assert(input, `invalid MethodDescriptorProto: input_type ${proto.inputType} not found`); |
| 525 |
assert(output, `invalid MethodDescriptorProto: output_type ${proto.inputType} not found`); |
| 526 |
const name = proto.name; |
| 527 |
return { |
| 528 |
kind: "rpc", |
| 529 |
proto, |
| 530 |
deprecated: (_b = (_a = proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false, |
| 531 |
parent, |
| 532 |
name, |
| 533 |
localName: (0, names_js_1.safeObjectProperty)(name.length |
| 534 |
? (0, names_js_1.safeObjectProperty)(name[0].toLowerCase() + name.substring(1)) |
| 535 |
: name), |
| 536 |
methodKind, |
| 537 |
input, |
| 538 |
output, |
| 539 |
idempotency: (_d = (_c = proto.options) === null || _c === void 0 ? void 0 : _c.idempotencyLevel) !== null && _d !== void 0 ? _d : IDEMPOTENCY_UNKNOWN, |
| 540 |
toString() { |
| 541 |
return `rpc ${parent.typeName}.${name}`; |
| 542 |
}, |
| 543 |
}; |
| 544 |
} |
| 545 |
/** |
| 546 |
* Create a descriptor for a oneof group. |
| 547 |
*/ |
| 548 |
function newOneof(proto, parent) { |
| 549 |
return { |
| 550 |
kind: "oneof", |
| 551 |
proto, |
| 552 |
deprecated: false, |
| 553 |
parent, |
| 554 |
fields: [], |
| 555 |
name: proto.name, |
| 556 |
localName: (0, names_js_1.safeObjectProperty)((0, names_js_1.protoCamelCase)(proto.name)), |
| 557 |
toString() { |
| 558 |
return `oneof ${parent.typeName}.${this.name}`; |
| 559 |
}, |
| 560 |
}; |
| 561 |
} |
| 562 |
function newField(proto, parentOrFile, reg, oneof, mapEntries) { |
| 563 |
var _a, _b, _c; |
| 564 |
const isExtension = mapEntries === undefined; |
| 565 |
const field = { |
| 566 |
kind: "field", |
| 567 |
proto, |
| 568 |
deprecated: (_b = (_a = proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false, |
| 569 |
name: proto.name, |
| 570 |
number: proto.number, |
| 571 |
scalar: undefined, |
| 572 |
message: undefined, |
| 573 |
enum: undefined, |
| 574 |
presence: getFieldPresence(proto, oneof, isExtension, parentOrFile), |
| 575 |
utf8Validation: isUtf8Validated(proto, parentOrFile), |
| 576 |
listKind: undefined, |
| 577 |
mapKind: undefined, |
| 578 |
mapKey: undefined, |
| 579 |
delimitedEncoding: undefined, |
| 580 |
packed: undefined, |
| 581 |
longAsString: false, |
| 582 |
getDefaultValue: undefined, |
| 583 |
}; |
| 584 |
let toStr; |
| 585 |
if (isExtension) { |
| 586 |
// extension field |
| 587 |
const file = parentOrFile.kind == "file" ? parentOrFile : parentOrFile.file; |
| 588 |
const parent = parentOrFile.kind == "file" ? undefined : parentOrFile; |
| 589 |
const typeName = makeTypeName(proto, parent, file); |
| 590 |
field.kind = "extension"; |
| 591 |
field.file = file; |
| 592 |
field.parent = parent; |
| 593 |
field.oneof = undefined; |
| 594 |
field.typeName = typeName; |
| 595 |
field.jsonName = `[${typeName}]`; // option json_name is not allowed on extension fields |
| 596 |
toStr = () => `extension ${typeName}`; |
| 597 |
const extendee = reg.getMessage(trimLeadingDot(proto.extendee)); |
| 598 |
assert(extendee, `invalid FieldDescriptorProto: extendee ${proto.extendee} not found`); |
| 599 |
field.extendee = extendee; |
| 600 |
} |
| 601 |
else { |
| 602 |
// regular field |
| 603 |
const parent = parentOrFile; |
| 604 |
assert(parent.kind == "message"); |
| 605 |
field.parent = parent; |
| 606 |
field.oneof = oneof; |
| 607 |
field.localName = oneof |
| 608 |
? (0, names_js_1.protoCamelCase)(proto.name) |
| 609 |
: (0, names_js_1.safeObjectProperty)((0, names_js_1.protoCamelCase)(proto.name)); |
| 610 |
field.jsonName = proto.jsonName; |
| 611 |
toStr = () => `field ${parent.typeName}.${proto.name}`; |
| 612 |
} |
| 613 |
// A plain assignment throws where built-in prototypes are frozen. The |
| 614 |
// attributes match what an assignment produces. |
| 615 |
Object.defineProperty(field, "toString", { |
| 616 |
value: toStr, |
| 617 |
writable: true, |
| 618 |
enumerable: true, |
| 619 |
configurable: true, |
| 620 |
}); |
| 621 |
const label = proto.label; |
| 622 |
const type = proto.type; |
| 623 |
const jstype = (_c = proto.options) === null || _c === void 0 ? void 0 : _c.jstype; |
| 624 |
if (label === LABEL_REPEATED) { |
| 625 |
// list or map field |
| 626 |
const mapEntry = type == TYPE_MESSAGE |
| 627 |
? mapEntries === null || mapEntries === void 0 ? void 0 : mapEntries.get(trimLeadingDot(proto.typeName)) |
| 628 |
: undefined; |
| 629 |
if (mapEntry) { |
| 630 |
// map field |
| 631 |
field.fieldKind = "map"; |
| 632 |
const { key, value } = findMapEntryFields(mapEntry); |
| 633 |
field.mapKey = key.scalar; |
| 634 |
field.mapKind = value.fieldKind; |
| 635 |
field.message = value.message; |
| 636 |
field.delimitedEncoding = false; // map fields are always LENGTH_PREFIXED |
| 637 |
field.enum = value.enum; |
| 638 |
field.scalar = value.scalar; |
| 639 |
return field; |
| 640 |
} |
| 641 |
// list field |
| 642 |
field.fieldKind = "list"; |
| 643 |
switch (type) { |
| 644 |
case TYPE_MESSAGE: |
| 645 |
case TYPE_GROUP: |
| 646 |
field.listKind = "message"; |
| 647 |
field.message = reg.getMessage(trimLeadingDot(proto.typeName)); |
| 648 |
assert(field.message); |
| 649 |
field.delimitedEncoding = isDelimitedEncoding(proto, parentOrFile); |
| 650 |
break; |
| 651 |
case TYPE_ENUM: |
| 652 |
field.listKind = "enum"; |
| 653 |
field.enum = reg.getEnum(trimLeadingDot(proto.typeName)); |
| 654 |
assert(field.enum); |
| 655 |
break; |
| 656 |
default: |
| 657 |
field.listKind = "scalar"; |
| 658 |
field.scalar = type; |
| 659 |
field.longAsString = jstype == JS_STRING; |
| 660 |
break; |
| 661 |
} |
| 662 |
field.packed = isPackedField(proto, parentOrFile); |
| 663 |
return field; |
| 664 |
} |
| 665 |
// singular |
| 666 |
switch (type) { |
| 667 |
case TYPE_MESSAGE: |
| 668 |
case TYPE_GROUP: |
| 669 |
field.fieldKind = "message"; |
| 670 |
field.message = reg.getMessage(trimLeadingDot(proto.typeName)); |
| 671 |
assert(field.message, `invalid FieldDescriptorProto: type_name ${proto.typeName} not found`); |
| 672 |
field.delimitedEncoding = isDelimitedEncoding(proto, parentOrFile); |
| 673 |
field.getDefaultValue = () => undefined; |
| 674 |
break; |
| 675 |
case TYPE_ENUM: { |
| 676 |
const enumeration = reg.getEnum(trimLeadingDot(proto.typeName)); |
| 677 |
assert(enumeration !== undefined, `invalid FieldDescriptorProto: type_name ${proto.typeName} not found`); |
| 678 |
field.fieldKind = "enum"; |
| 679 |
field.enum = reg.getEnum(trimLeadingDot(proto.typeName)); |
| 680 |
field.getDefaultValue = () => { |
| 681 |
return (0, unsafe_js_1.unsafeIsSetExplicit)(proto, "defaultValue") |
| 682 |
? (0, text_format_js_1.parseTextFormatEnumValue)(enumeration, proto.defaultValue) |
| 683 |
: undefined; |
| 684 |
}; |
| 685 |
break; |
| 686 |
} |
| 687 |
default: { |
| 688 |
field.fieldKind = "scalar"; |
| 689 |
field.scalar = type; |
| 690 |
field.longAsString = jstype == JS_STRING; |
| 691 |
field.getDefaultValue = () => { |
| 692 |
return (0, unsafe_js_1.unsafeIsSetExplicit)(proto, "defaultValue") |
| 693 |
? (0, text_format_js_1.parseTextFormatScalarValue)(type, proto.defaultValue) |
| 694 |
: undefined; |
| 695 |
}; |
| 696 |
break; |
| 697 |
} |
| 698 |
} |
| 699 |
return field; |
| 700 |
} |
| 701 |
/** |
| 702 |
* Parse the "syntax" and "edition" fields, returning one of the supported |
| 703 |
* editions. |
| 704 |
*/ |
| 705 |
function getFileEdition(proto) { |
| 706 |
switch (proto.syntax) { |
| 707 |
case "": |
| 708 |
case "proto2": |
| 709 |
return EDITION_PROTO2; |
| 710 |
case "proto3": |
| 711 |
return EDITION_PROTO3; |
| 712 |
case "editions": |
| 713 |
// EDITION_UNSTABLE is a sandbox for in-development features. Collapse |
| 714 |
// it to maximumEdition so SupportedEdition and feature resolution do |
| 715 |
// not leak the test-only edition to users. |
| 716 |
if (proto.edition === EDITION_UNSTABLE) { |
| 717 |
return exports.maximumEdition; |
| 718 |
} |
| 719 |
if (proto.edition in featureDefaults) { |
| 720 |
return proto.edition; |
| 721 |
} |
| 722 |
throw new Error(`${proto.name}: unsupported edition`); |
| 723 |
default: |
| 724 |
throw new Error(`${proto.name}: unsupported syntax "${proto.syntax}"`); |
| 725 |
} |
| 726 |
} |
| 727 |
/** |
| 728 |
* Resolve dependencies of FileDescriptorProto to DescFile. |
| 729 |
*/ |
| 730 |
function findFileDependencies(proto, reg) { |
| 731 |
return proto.dependency.map((wantName) => { |
| 732 |
const dep = reg.getFile(wantName); |
| 733 |
if (!dep) { |
| 734 |
throw new Error(`Cannot find ${wantName}, imported by ${proto.name}`); |
| 735 |
} |
| 736 |
return dep; |
| 737 |
}); |
| 738 |
} |
| 739 |
/** |
| 740 |
* Finds a prefix shared by enum values, for example `my_enum_` for |
| 741 |
* `enum MyEnum {MY_ENUM_A=0; MY_ENUM_B=1;}`. |
| 742 |
*/ |
| 743 |
function findEnumSharedPrefix(enumName, values) { |
| 744 |
const prefix = camelToSnakeCase(enumName) + "_"; |
| 745 |
for (const value of values) { |
| 746 |
if (!value.name.toLowerCase().startsWith(prefix)) { |
| 747 |
return undefined; |
| 748 |
} |
| 749 |
const shortName = value.name.substring(prefix.length); |
| 750 |
if (shortName.length == 0) { |
| 751 |
return undefined; |
| 752 |
} |
| 753 |
if (/^\d/.test(shortName)) { |
| 754 |
// identifiers must not start with numbers |
| 755 |
return undefined; |
| 756 |
} |
| 757 |
} |
| 758 |
return prefix; |
| 759 |
} |
| 760 |
/** |
| 761 |
* Converts lowerCamelCase or UpperCamelCase into lower_snake_case. |
| 762 |
* This is used to find shared prefixes in an enum. |
| 763 |
*/ |
| 764 |
function camelToSnakeCase(camel) { |
| 765 |
return (camel.substring(0, 1) + camel.substring(1).replace(/[A-Z]/g, (c) => "_" + c)).toLowerCase(); |
| 766 |
} |
| 767 |
/** |
| 768 |
* Create a fully qualified name for a protobuf type or extension field. |
| 769 |
* |
| 770 |
* The fully qualified name for messages, enumerations, and services is |
| 771 |
* constructed by concatenating the package name (if present), parent |
| 772 |
* message names (for nested types), and the type name. We omit the leading |
| 773 |
* dot added by protobuf compilers. Examples: |
| 774 |
* - mypackage.MyMessage |
| 775 |
* - mypackage.MyMessage.NestedMessage |
| 776 |
* |
| 777 |
* The fully qualified name for extension fields is constructed by |
| 778 |
* concatenating the package name (if present), parent message names (for |
| 779 |
* extensions declared within a message), and the field name. Examples: |
| 780 |
* - mypackage.extfield |
| 781 |
* - mypackage.MyMessage.extfield |
| 782 |
*/ |
| 783 |
function makeTypeName(proto, parent, file) { |
| 784 |
let typeName; |
| 785 |
if (parent) { |
| 786 |
typeName = `${parent.typeName}.${proto.name}`; |
| 787 |
} |
| 788 |
else if (file.proto.package.length > 0) { |
| 789 |
typeName = `${file.proto.package}.${proto.name}`; |
| 790 |
} |
| 791 |
else { |
| 792 |
typeName = `${proto.name}`; |
| 793 |
} |
| 794 |
return typeName; |
| 795 |
} |
| 796 |
/** |
| 797 |
* Remove the leading dot from a fully qualified type name. |
| 798 |
*/ |
| 799 |
function trimLeadingDot(typeName) { |
| 800 |
return typeName.startsWith(".") ? typeName.substring(1) : typeName; |
| 801 |
} |
| 802 |
/** |
| 803 |
* Did the user put the field in a oneof group? |
| 804 |
* Synthetic oneofs for proto3 optionals are ignored. |
| 805 |
*/ |
| 806 |
function findOneof(proto, allOneofs) { |
| 807 |
if (!(0, unsafe_js_1.unsafeIsSetExplicit)(proto, "oneofIndex")) { |
| 808 |
return undefined; |
| 809 |
} |
| 810 |
if (proto.proto3Optional) { |
| 811 |
return undefined; |
| 812 |
} |
| 813 |
const oneof = allOneofs[proto.oneofIndex]; |
| 814 |
assert(oneof, `invalid FieldDescriptorProto: oneof #${proto.oneofIndex} for field #${proto.number} not found`); |
| 815 |
return oneof; |
| 816 |
} |
| 817 |
/** |
| 818 |
* Presence of the field. |
| 819 |
* See https://protobuf.dev/programming-guides/field_presence/ |
| 820 |
*/ |
| 821 |
function getFieldPresence(proto, oneof, isExtension, parent) { |
| 822 |
if (proto.label == LABEL_REQUIRED) { |
| 823 |
// proto2 required is LEGACY_REQUIRED |
| 824 |
return LEGACY_REQUIRED; |
| 825 |
} |
| 826 |
if (proto.label == LABEL_REPEATED) { |
| 827 |
// repeated fields (including maps) do not track presence |
| 828 |
return IMPLICIT; |
| 829 |
} |
| 830 |
if (!!oneof || proto.proto3Optional) { |
| 831 |
// oneof is always explicit |
| 832 |
return EXPLICIT; |
| 833 |
} |
| 834 |
if (isExtension) { |
| 835 |
// extensions always track presence |
| 836 |
return EXPLICIT; |
| 837 |
} |
| 838 |
const resolved = resolveFeature("fieldPresence", { proto, parent }); |
| 839 |
if (resolved == IMPLICIT && |
| 840 |
(proto.type == TYPE_MESSAGE || proto.type == TYPE_GROUP)) { |
| 841 |
// singular message field cannot be implicit |
| 842 |
return EXPLICIT; |
| 843 |
} |
| 844 |
return resolved; |
| 845 |
} |
| 846 |
/** |
| 847 |
* Pack this repeated field? |
| 848 |
*/ |
| 849 |
function isPackedField(proto, parent) { |
| 850 |
if (proto.label != LABEL_REPEATED) { |
| 851 |
return false; |
| 852 |
} |
| 853 |
switch (proto.type) { |
| 854 |
case TYPE_STRING: |
| 855 |
case TYPE_BYTES: |
| 856 |
case TYPE_GROUP: |
| 857 |
case TYPE_MESSAGE: |
| 858 |
// length-delimited types cannot be packed |
| 859 |
return false; |
| 860 |
} |
| 861 |
const o = proto.options; |
| 862 |
if (o && (0, unsafe_js_1.unsafeIsSetExplicit)(o, "packed")) { |
| 863 |
// prefer the field option over edition features |
| 864 |
return o.packed; |
| 865 |
} |
| 866 |
return (PACKED == |
| 867 |
resolveFeature("repeatedFieldEncoding", { |
| 868 |
proto, |
| 869 |
parent, |
| 870 |
})); |
| 871 |
} |
| 872 |
/** |
| 873 |
* Find the key and value fields of a synthetic map entry message. |
| 874 |
*/ |
| 875 |
function findMapEntryFields(mapEntry) { |
| 876 |
const key = mapEntry.fields.find((f) => f.number === 1); |
| 877 |
const value = mapEntry.fields.find((f) => f.number === 2); |
| 878 |
assert(key && |
| 879 |
key.fieldKind == "scalar" && |
| 880 |
key.scalar != descriptors_js_1.ScalarType.BYTES && |
| 881 |
key.scalar != descriptors_js_1.ScalarType.FLOAT && |
| 882 |
key.scalar != descriptors_js_1.ScalarType.DOUBLE && |
| 883 |
value && |
| 884 |
value.fieldKind != "list" && |
| 885 |
value.fieldKind != "map"); |
| 886 |
return { key, value }; |
| 887 |
} |
| 888 |
/** |
| 889 |
* Enumerations can be open or closed. |
| 890 |
* See https://protobuf.dev/programming-guides/enum/ |
| 891 |
*/ |
| 892 |
function isEnumOpen(desc) { |
| 893 |
var _a; |
| 894 |
return (OPEN == |
| 895 |
resolveFeature("enumType", { |
| 896 |
proto: desc.proto, |
| 897 |
parent: (_a = desc.parent) !== null && _a !== void 0 ? _a : desc.file, |
| 898 |
})); |
| 899 |
} |
| 900 |
/** |
| 901 |
* Encode the message delimited (a.k.a. proto2 group encoding), or |
| 902 |
* length-prefixed? |
| 903 |
*/ |
| 904 |
function isDelimitedEncoding(proto, parent) { |
| 905 |
if (proto.type == TYPE_GROUP) { |
| 906 |
return true; |
| 907 |
} |
| 908 |
return (DELIMITED == |
| 909 |
resolveFeature("messageEncoding", { |
| 910 |
proto, |
| 911 |
parent, |
| 912 |
})); |
| 913 |
} |
| 914 |
/** |
| 915 |
* Reject invalid UTF-8 when reading string fields from the binary wire format? |
| 916 |
* Driven by the resolved `utf8_validation` feature: VERIFY (proto3 / editions |
| 917 |
* 2023+ default) enforces; NONE (proto2 default) does not. |
| 918 |
*/ |
| 919 |
function isUtf8Validated(proto, parent) { |
| 920 |
return (VERIFY == |
| 921 |
resolveFeature("utf8Validation", { |
| 922 |
proto, |
| 923 |
parent, |
| 924 |
})); |
| 925 |
} |
| 926 |
function resolveFeature(name, ref) { |
| 927 |
var _a, _b; |
| 928 |
const featureSet = (_a = ref.proto.options) === null || _a === void 0 ? void 0 : _a.features; |
| 929 |
if (featureSet) { |
| 930 |
const val = featureSet[name]; |
| 931 |
if (val != 0) { |
| 932 |
return val; |
| 933 |
} |
| 934 |
} |
| 935 |
if ("kind" in ref) { |
| 936 |
if (ref.kind == "message") { |
| 937 |
return resolveFeature(name, (_b = ref.parent) !== null && _b !== void 0 ? _b : ref.file); |
| 938 |
} |
| 939 |
const editionDefaults = featureDefaults[ref.edition]; |
| 940 |
if (!editionDefaults) { |
| 941 |
throw new Error(`feature default for edition ${ref.edition} not found`); |
| 942 |
} |
| 943 |
return editionDefaults[name]; |
| 944 |
} |
| 945 |
return resolveFeature(name, ref.parent); |
| 946 |
} |
| 947 |
/** |
| 948 |
* Assert that condition is truthy or throw error (with message) |
| 949 |
*/ |
| 950 |
function assert(condition, msg) { |
| 951 |
if (!condition) { |
| 952 |
throw new Error(msg); |
| 953 |
} |
| 954 |
} |