rearrage_stuff

This commit is contained in:
Kim Ravn Hansen
2025-09-16 11:26:40 +02:00
parent 40e8c5e0ab
commit 3f11ebe6dc
4937 changed files with 1146031 additions and 134 deletions

View File

@@ -0,0 +1,63 @@
import type { DescriptorProto_ExtensionRange, FieldDescriptorProto_Label, FieldDescriptorProto_Type, FieldOptions_OptionRetention, FieldOptions_OptionTargetType, FieldOptions_EditionDefault, EnumValueDescriptorProto, FileDescriptorProto } from "../wkt/gen/google/protobuf/descriptor_pb.js";
import type { DescFile } from "../descriptors.js";
/**
* Hydrate a file descriptor for google/protobuf/descriptor.proto from a plain
* object.
*
* See createFileDescriptorProtoBoot() for details.
*
* @private
*/
export declare function boot(boot: FileDescriptorProtoBoot): DescFile;
/**
* An object literal for initializing the message google.protobuf.FileDescriptorProto
* for google/protobuf/descriptor.proto.
*
* See createFileDescriptorProtoBoot() for details.
*
* @private
*/
export type FileDescriptorProtoBoot = {
name: "google/protobuf/descriptor.proto";
package: "google.protobuf";
messageType: DescriptorProtoBoot[];
enumType: EnumDescriptorProtoBoot[];
};
export type DescriptorProtoBoot = {
name: string;
field?: FieldDescriptorProtoBoot[];
nestedType?: DescriptorProtoBoot[];
enumType?: EnumDescriptorProtoBoot[];
extensionRange?: Pick<DescriptorProto_ExtensionRange, "start" | "end">[];
};
export type FieldDescriptorProtoBoot = {
name: string;
number: number;
label?: FieldDescriptorProto_Label;
type: FieldDescriptorProto_Type;
typeName?: string;
extendee?: string;
defaultValue?: string;
options?: FieldOptionsBoot;
};
export type FieldOptionsBoot = {
packed?: boolean;
deprecated?: boolean;
retention?: FieldOptions_OptionRetention;
targets?: FieldOptions_OptionTargetType[];
editionDefaults?: FieldOptions_EditionDefaultBoot[];
};
export type FieldOptions_EditionDefaultBoot = Pick<FieldOptions_EditionDefault, "edition" | "value">;
export type EnumDescriptorProtoBoot = {
name: string;
value: EnumValueDescriptorProtoBoot[];
};
export type EnumValueDescriptorProtoBoot = Pick<EnumValueDescriptorProto, "name" | "number">;
/**
* Creates the message google.protobuf.FileDescriptorProto from an object literal.
*
* See createFileDescriptorProtoBoot() for details.
*
* @private
*/
export declare function bootFileDescriptorProto(init: FileDescriptorProtoBoot): FileDescriptorProto;

View File

@@ -0,0 +1,105 @@
"use strict";
// Copyright 2021-2025 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.boot = boot;
exports.bootFileDescriptorProto = bootFileDescriptorProto;
const restore_json_names_js_1 = require("./restore-json-names.js");
const registry_js_1 = require("../registry.js");
/**
* Hydrate a file descriptor for google/protobuf/descriptor.proto from a plain
* object.
*
* See createFileDescriptorProtoBoot() for details.
*
* @private
*/
function boot(boot) {
const root = bootFileDescriptorProto(boot);
root.messageType.forEach(restore_json_names_js_1.restoreJsonNames);
const reg = (0, registry_js_1.createFileRegistry)(root, () => undefined);
// biome-ignore lint/style/noNonNullAssertion: non-null assertion because we just created the registry from the file we look up
return reg.getFile(root.name);
}
/**
* Creates the message google.protobuf.FileDescriptorProto from an object literal.
*
* See createFileDescriptorProtoBoot() for details.
*
* @private
*/
function bootFileDescriptorProto(init) {
const proto = Object.create({
syntax: "",
edition: 0,
});
return Object.assign(proto, Object.assign(Object.assign({ $typeName: "google.protobuf.FileDescriptorProto", dependency: [], publicDependency: [], weakDependency: [], optionDependency: [], service: [], extension: [] }, init), { messageType: init.messageType.map(bootDescriptorProto), enumType: init.enumType.map(bootEnumDescriptorProto) }));
}
function bootDescriptorProto(init) {
var _a, _b, _c, _d, _e, _f, _g, _h;
const proto = Object.create({
visibility: 0,
});
return Object.assign(proto, {
$typeName: "google.protobuf.DescriptorProto",
name: init.name,
field: (_b = (_a = init.field) === null || _a === void 0 ? void 0 : _a.map(bootFieldDescriptorProto)) !== null && _b !== void 0 ? _b : [],
extension: [],
nestedType: (_d = (_c = init.nestedType) === null || _c === void 0 ? void 0 : _c.map(bootDescriptorProto)) !== null && _d !== void 0 ? _d : [],
enumType: (_f = (_e = init.enumType) === null || _e === void 0 ? void 0 : _e.map(bootEnumDescriptorProto)) !== null && _f !== void 0 ? _f : [],
extensionRange: (_h = (_g = init.extensionRange) === null || _g === void 0 ? void 0 : _g.map((e) => (Object.assign({ $typeName: "google.protobuf.DescriptorProto.ExtensionRange" }, e)))) !== null && _h !== void 0 ? _h : [],
oneofDecl: [],
reservedRange: [],
reservedName: [],
});
}
function bootFieldDescriptorProto(init) {
const proto = Object.create({
label: 1,
typeName: "",
extendee: "",
defaultValue: "",
oneofIndex: 0,
jsonName: "",
proto3Optional: false,
});
return Object.assign(proto, Object.assign(Object.assign({ $typeName: "google.protobuf.FieldDescriptorProto" }, init), { options: init.options ? bootFieldOptions(init.options) : undefined }));
}
function bootFieldOptions(init) {
var _a, _b, _c;
const proto = Object.create({
ctype: 0,
packed: false,
jstype: 0,
lazy: false,
unverifiedLazy: false,
deprecated: false,
weak: false,
debugRedact: false,
retention: 0,
});
return Object.assign(proto, Object.assign(Object.assign({ $typeName: "google.protobuf.FieldOptions" }, init), { targets: (_a = init.targets) !== null && _a !== void 0 ? _a : [], editionDefaults: (_c = (_b = init.editionDefaults) === null || _b === void 0 ? void 0 : _b.map((e) => (Object.assign({ $typeName: "google.protobuf.FieldOptions.EditionDefault" }, e)))) !== null && _c !== void 0 ? _c : [], uninterpretedOption: [] }));
}
function bootEnumDescriptorProto(init) {
const proto = Object.create({
visibility: 0,
});
return Object.assign(proto, {
$typeName: "google.protobuf.EnumDescriptorProto",
name: init.name,
reservedName: [],
reservedRange: [],
value: init.value.map((e) => (Object.assign({ $typeName: "google.protobuf.EnumValueDescriptorProto" }, e))),
});
}

View File

@@ -0,0 +1,43 @@
import type { DescEnum, DescExtension, DescMessage, DescService } from "../descriptors.js";
import { type FileDescriptorProto } from "../wkt/gen/google/protobuf/descriptor_pb.js";
import type { FileDescriptorProtoBoot } from "./boot.js";
type EmbedUnknown = {
bootable: false;
proto(): FileDescriptorProto;
base64(): string;
};
type EmbedDescriptorProto = Omit<EmbedUnknown, "bootable"> & {
bootable: true;
boot(): FileDescriptorProtoBoot;
};
/**
* Create necessary information to embed a file descriptor in
* generated code.
*
* @private
*/
export declare function embedFileDesc(file: FileDescriptorProto): EmbedUnknown | EmbedDescriptorProto;
/**
* Compute the path to a message, enumeration, extension, or service in a
* file descriptor.
*
* @private
*/
export declare function pathInFileDesc(desc: DescMessage | DescEnum | DescExtension | DescService): number[];
/**
* The file descriptor for google/protobuf/descriptor.proto cannot be embedded
* in serialized form, since it is required to parse itself.
*
* This function takes an instance of the message, and returns a plain object
* that can be hydrated to the message again via bootFileDescriptorProto().
*
* This function only works with a message google.protobuf.FileDescriptorProto
* for google/protobuf/descriptor.proto, and only supports features that are
* relevant for the specific use case. For example, it discards file options,
* reserved ranges and reserved names, and field options that are unused in
* descriptor.proto.
*
* @private
*/
export declare function createFileDescriptorProtoBoot(proto: FileDescriptorProto): FileDescriptorProtoBoot;
export {};

View File

@@ -0,0 +1,244 @@
"use strict";
// Copyright 2021-2025 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.embedFileDesc = embedFileDesc;
exports.pathInFileDesc = pathInFileDesc;
exports.createFileDescriptorProtoBoot = createFileDescriptorProtoBoot;
const names_js_1 = require("../reflect/names.js");
const fields_js_1 = require("../fields.js");
const base64_encoding_js_1 = require("../wire/base64-encoding.js");
const to_binary_js_1 = require("../to-binary.js");
const clone_js_1 = require("../clone.js");
const descriptor_pb_js_1 = require("../wkt/gen/google/protobuf/descriptor_pb.js");
/**
* Create necessary information to embed a file descriptor in
* generated code.
*
* @private
*/
function embedFileDesc(file) {
const embed = {
bootable: false,
proto() {
const stripped = (0, clone_js_1.clone)(descriptor_pb_js_1.FileDescriptorProtoSchema, file);
(0, fields_js_1.clearField)(stripped, descriptor_pb_js_1.FileDescriptorProtoSchema.field.dependency);
(0, fields_js_1.clearField)(stripped, descriptor_pb_js_1.FileDescriptorProtoSchema.field.sourceCodeInfo);
stripped.messageType.map(stripJsonNames);
return stripped;
},
base64() {
const bytes = (0, to_binary_js_1.toBinary)(descriptor_pb_js_1.FileDescriptorProtoSchema, this.proto());
return (0, base64_encoding_js_1.base64Encode)(bytes, "std_raw");
},
};
return file.name == "google/protobuf/descriptor.proto"
? Object.assign(Object.assign({}, embed), { bootable: true, boot() {
return createFileDescriptorProtoBoot(this.proto());
} }) : embed;
}
function stripJsonNames(d) {
for (const f of d.field) {
if (f.jsonName === (0, names_js_1.protoCamelCase)(f.name)) {
(0, fields_js_1.clearField)(f, descriptor_pb_js_1.FieldDescriptorProtoSchema.field.jsonName);
}
}
for (const n of d.nestedType) {
stripJsonNames(n);
}
}
/**
* Compute the path to a message, enumeration, extension, or service in a
* file descriptor.
*
* @private
*/
function pathInFileDesc(desc) {
if (desc.kind == "service") {
return [desc.file.services.indexOf(desc)];
}
const parent = desc.parent;
if (parent == undefined) {
switch (desc.kind) {
case "enum":
return [desc.file.enums.indexOf(desc)];
case "message":
return [desc.file.messages.indexOf(desc)];
case "extension":
return [desc.file.extensions.indexOf(desc)];
}
}
function findPath(cur) {
const nested = [];
for (let parent = cur.parent; parent;) {
const idx = parent.nestedMessages.indexOf(cur);
nested.unshift(idx);
cur = parent;
parent = cur.parent;
}
nested.unshift(cur.file.messages.indexOf(cur));
return nested;
}
const path = findPath(parent);
switch (desc.kind) {
case "extension":
return [...path, parent.nestedExtensions.indexOf(desc)];
case "message":
return [...path, parent.nestedMessages.indexOf(desc)];
case "enum":
return [...path, parent.nestedEnums.indexOf(desc)];
}
}
/**
* The file descriptor for google/protobuf/descriptor.proto cannot be embedded
* in serialized form, since it is required to parse itself.
*
* This function takes an instance of the message, and returns a plain object
* that can be hydrated to the message again via bootFileDescriptorProto().
*
* This function only works with a message google.protobuf.FileDescriptorProto
* for google/protobuf/descriptor.proto, and only supports features that are
* relevant for the specific use case. For example, it discards file options,
* reserved ranges and reserved names, and field options that are unused in
* descriptor.proto.
*
* @private
*/
function createFileDescriptorProtoBoot(proto) {
var _a;
assert(proto.name == "google/protobuf/descriptor.proto");
assert(proto.package == "google.protobuf");
assert(!proto.dependency.length);
assert(!proto.publicDependency.length);
assert(!proto.weakDependency.length);
assert(!proto.optionDependency.length);
assert(!proto.service.length);
assert(!proto.extension.length);
assert(proto.sourceCodeInfo === undefined);
assert(proto.syntax == "" || proto.syntax == "proto2");
assert(!((_a = proto.options) === null || _a === void 0 ? void 0 : _a.features)); // we're dropping file options
assert(proto.edition === descriptor_pb_js_1.Edition.EDITION_UNKNOWN);
return {
name: proto.name,
package: proto.package,
messageType: proto.messageType.map(createDescriptorBoot),
enumType: proto.enumType.map(createEnumDescriptorBoot),
};
}
function createDescriptorBoot(proto) {
assert(proto.extension.length == 0);
assert(!proto.oneofDecl.length);
assert(!proto.options);
assert(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.DescriptorProtoSchema.field.visibility));
const b = {
name: proto.name,
};
if (proto.field.length) {
b.field = proto.field.map(createFieldDescriptorBoot);
}
if (proto.nestedType.length) {
b.nestedType = proto.nestedType.map(createDescriptorBoot);
}
if (proto.enumType.length) {
b.enumType = proto.enumType.map(createEnumDescriptorBoot);
}
if (proto.extensionRange.length) {
b.extensionRange = proto.extensionRange.map((r) => {
assert(!r.options);
return { start: r.start, end: r.end };
});
}
return b;
}
function createFieldDescriptorBoot(proto) {
assert((0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldDescriptorProtoSchema.field.name));
assert((0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldDescriptorProtoSchema.field.number));
assert((0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldDescriptorProtoSchema.field.type));
assert(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldDescriptorProtoSchema.field.oneofIndex));
assert(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldDescriptorProtoSchema.field.jsonName) ||
proto.jsonName === (0, names_js_1.protoCamelCase)(proto.name));
const b = {
name: proto.name,
number: proto.number,
type: proto.type,
};
if ((0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldDescriptorProtoSchema.field.label)) {
b.label = proto.label;
}
if ((0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldDescriptorProtoSchema.field.typeName)) {
b.typeName = proto.typeName;
}
if ((0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldDescriptorProtoSchema.field.extendee)) {
b.extendee = proto.extendee;
}
if ((0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldDescriptorProtoSchema.field.defaultValue)) {
b.defaultValue = proto.defaultValue;
}
if (proto.options) {
b.options = createFieldOptionsBoot(proto.options);
}
return b;
}
function createFieldOptionsBoot(proto) {
const b = {};
assert(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldOptionsSchema.field.ctype));
if ((0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldOptionsSchema.field.packed)) {
b.packed = proto.packed;
}
assert(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldOptionsSchema.field.jstype));
assert(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldOptionsSchema.field.lazy));
assert(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldOptionsSchema.field.unverifiedLazy));
if ((0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldOptionsSchema.field.deprecated)) {
b.deprecated = proto.deprecated;
}
assert(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldOptionsSchema.field.weak));
assert(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldOptionsSchema.field.debugRedact));
if ((0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldOptionsSchema.field.retention)) {
b.retention = proto.retention;
}
if (proto.targets.length) {
b.targets = proto.targets;
}
if (proto.editionDefaults.length) {
b.editionDefaults = proto.editionDefaults.map((d) => ({
value: d.value,
edition: d.edition,
}));
}
assert(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldOptionsSchema.field.features));
assert(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.FieldOptionsSchema.field.uninterpretedOption));
return b;
}
function createEnumDescriptorBoot(proto) {
assert(!proto.options);
assert(!(0, fields_js_1.isFieldSet)(proto, descriptor_pb_js_1.EnumDescriptorProtoSchema.field.visibility));
return {
name: proto.name,
value: proto.value.map((v) => {
assert(!v.options);
return {
name: v.name,
number: v.number,
};
}),
};
}
/**
* Assert that condition is truthy or throw error.
*/
function assert(condition) {
if (!condition) {
throw new Error();
}
}

View File

@@ -0,0 +1,18 @@
import type { DescEnum, DescFile } from "../descriptors.js";
import type { GenEnum } from "./types.js";
import type { JsonValue } from "../json-value.js";
/**
* Hydrate an enum descriptor.
*
* @private
*/
export declare function enumDesc<Shape extends number, JsonType extends JsonValue = JsonValue>(file: DescFile, path: number, ...paths: number[]): GenEnum<Shape, JsonType>;
/**
* Construct a TypeScript enum object at runtime from a descriptor.
*/
export declare function tsEnum(desc: DescEnum): enumObject;
type enumObject = {
[key: number]: string;
[k: string]: number | string;
};
export {};

View File

@@ -0,0 +1,40 @@
"use strict";
// Copyright 2021-2025 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.enumDesc = enumDesc;
exports.tsEnum = tsEnum;
/**
* Hydrate an enum descriptor.
*
* @private
*/
function enumDesc(file, path, ...paths) {
if (paths.length == 0) {
return file.enums[path];
}
const e = paths.pop(); // we checked length above
return paths.reduce((acc, cur) => acc.nestedMessages[cur], file.messages[path]).nestedEnums[e];
}
/**
* Construct a TypeScript enum object at runtime from a descriptor.
*/
function tsEnum(desc) {
const enumObject = {};
for (const value of desc.values) {
enumObject[value.localName] = value.number;
enumObject[value.number] = value.localName;
}
return enumObject;
}

View File

@@ -0,0 +1,9 @@
import type { Message } from "../types.js";
import type { DescFile } from "../descriptors.js";
import type { GenExtension } from "./types.js";
/**
* Hydrate an extension descriptor.
*
* @private
*/
export declare function extDesc<Extendee extends Message, Value>(file: DescFile, path: number, ...paths: number[]): GenExtension<Extendee, Value>;

View File

@@ -0,0 +1,28 @@
"use strict";
// Copyright 2021-2025 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.extDesc = extDesc;
/**
* Hydrate an extension descriptor.
*
* @private
*/
function extDesc(file, path, ...paths) {
if (paths.length == 0) {
return file.extensions[path];
}
const e = paths.pop(); // we checked length above
return paths.reduce((acc, cur) => acc.nestedMessages[cur], file.messages[path]).nestedExtensions[e];
}

View File

@@ -0,0 +1,7 @@
import type { DescFile } from "../descriptors.js";
/**
* Hydrate a file descriptor.
*
* @private
*/
export declare function fileDesc(b64: string, imports?: DescFile[]): DescFile;

View File

@@ -0,0 +1,35 @@
"use strict";
// Copyright 2021-2025 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.fileDesc = fileDesc;
const base64_encoding_js_1 = require("../wire/base64-encoding.js");
const descriptor_pb_js_1 = require("../wkt/gen/google/protobuf/descriptor_pb.js");
const registry_js_1 = require("../registry.js");
const restore_json_names_js_1 = require("./restore-json-names.js");
const from_binary_js_1 = require("../from-binary.js");
/**
* Hydrate a file descriptor.
*
* @private
*/
function fileDesc(b64, imports) {
var _a;
const root = (0, from_binary_js_1.fromBinary)(descriptor_pb_js_1.FileDescriptorProtoSchema, (0, base64_encoding_js_1.base64Decode)(b64));
root.messageType.forEach(restore_json_names_js_1.restoreJsonNames);
root.dependency = (_a = imports === null || imports === void 0 ? void 0 : imports.map((f) => f.proto.name)) !== null && _a !== void 0 ? _a : [];
const reg = (0, registry_js_1.createFileRegistry)(root, (protoFileName) => imports === null || imports === void 0 ? void 0 : imports.find((f) => f.proto.name === protoFileName));
// biome-ignore lint/style/noNonNullAssertion: non-null assertion because we just created the registry from the file we look up
return reg.getFile(root.name);
}

View File

@@ -0,0 +1,10 @@
export * from "./boot.js";
export * from "./embed.js";
export * from "./enum.js";
export * from "./extension.js";
export * from "./file.js";
export * from "./message.js";
export * from "./service.js";
export * from "./symbols.js";
export * from "./scalar.js";
export * from "./types.js";

View File

@@ -0,0 +1,39 @@
"use strict";
// Copyright 2021-2025 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
__exportStar(require("./boot.js"), exports);
__exportStar(require("./embed.js"), exports);
__exportStar(require("./enum.js"), exports);
__exportStar(require("./extension.js"), exports);
__exportStar(require("./file.js"), exports);
__exportStar(require("./message.js"), exports);
__exportStar(require("./service.js"), exports);
__exportStar(require("./symbols.js"), exports);
__exportStar(require("./scalar.js"), exports);
__exportStar(require("./types.js"), exports);

View File

@@ -0,0 +1,15 @@
import type { Message } from "../types.js";
import type { DescFile } from "../descriptors.js";
import type { GenMessage } from "./types.js";
/**
* Hydrate a message descriptor.
*
* @private
*/
export declare function messageDesc<Shape extends Message, Opt extends {
jsonType?: unknown;
validType?: unknown;
} = {
jsonType: undefined;
validType: undefined;
}>(file: DescFile, path: number, ...paths: number[]): GenMessage<Shape, Opt>;

View File

@@ -0,0 +1,24 @@
"use strict";
// Copyright 2021-2025 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.messageDesc = messageDesc;
/**
* Hydrate a message descriptor.
*
* @private
*/
function messageDesc(file, path, ...paths) {
return paths.reduce((acc, cur) => acc.nestedMessages[cur], file.messages[path]);
}

View File

@@ -0,0 +1,5 @@
import type { DescriptorProto } from "../wkt/gen/google/protobuf/descriptor_pb.js";
/**
* @private
*/
export declare function restoreJsonNames(message: DescriptorProto): void;

View File

@@ -0,0 +1,29 @@
"use strict";
// Copyright 2021-2025 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.restoreJsonNames = restoreJsonNames;
const names_js_1 = require("../reflect/names.js");
const unsafe_js_1 = require("../reflect/unsafe.js");
/**
* @private
*/
function restoreJsonNames(message) {
for (const f of message.field) {
if (!(0, unsafe_js_1.unsafeIsSetExplicit)(f, "jsonName")) {
f.jsonName = (0, names_js_1.protoCamelCase)(f.name);
}
}
message.nestedType.forEach(restoreJsonNames);
}

View File

@@ -0,0 +1,9 @@
import { ScalarType } from "../descriptors.js";
/**
* Return the TypeScript type (as a string) for the given scalar type.
*/
export declare function scalarTypeScriptType(scalar: ScalarType, longAsString: boolean): "string" | "boolean" | "bigint" | "bigint | string" | "Uint8Array" | "number";
/**
* Return the JSON type (as a string) for the given scalar type.
*/
export declare function scalarJsonType(scalar: ScalarType): "string" | "boolean" | "number" | `number | "NaN" | "Infinity" | "-Infinity"`;

View File

@@ -0,0 +1,67 @@
"use strict";
// Copyright 2021-2025 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.scalarTypeScriptType = scalarTypeScriptType;
exports.scalarJsonType = scalarJsonType;
const descriptors_js_1 = require("../descriptors.js");
/**
* Return the TypeScript type (as a string) for the given scalar type.
*/
function scalarTypeScriptType(scalar, longAsString) {
switch (scalar) {
case descriptors_js_1.ScalarType.STRING:
return "string";
case descriptors_js_1.ScalarType.BOOL:
return "boolean";
case descriptors_js_1.ScalarType.UINT64:
case descriptors_js_1.ScalarType.SFIXED64:
case descriptors_js_1.ScalarType.FIXED64:
case descriptors_js_1.ScalarType.SINT64:
case descriptors_js_1.ScalarType.INT64:
return longAsString ? "string" : "bigint";
case descriptors_js_1.ScalarType.BYTES:
return "Uint8Array";
default:
return "number";
}
}
/**
* Return the JSON type (as a string) for the given scalar type.
*/
function scalarJsonType(scalar) {
switch (scalar) {
case descriptors_js_1.ScalarType.DOUBLE:
case descriptors_js_1.ScalarType.FLOAT:
return `number | "NaN" | "Infinity" | "-Infinity"`;
case descriptors_js_1.ScalarType.UINT64:
case descriptors_js_1.ScalarType.SFIXED64:
case descriptors_js_1.ScalarType.FIXED64:
case descriptors_js_1.ScalarType.SINT64:
case descriptors_js_1.ScalarType.INT64:
return "string";
case descriptors_js_1.ScalarType.INT32:
case descriptors_js_1.ScalarType.FIXED32:
case descriptors_js_1.ScalarType.UINT32:
case descriptors_js_1.ScalarType.SFIXED32:
case descriptors_js_1.ScalarType.SINT32:
return "number";
case descriptors_js_1.ScalarType.STRING:
return "string";
case descriptors_js_1.ScalarType.BOOL:
return "boolean";
case descriptors_js_1.ScalarType.BYTES:
return "string";
}
}

View File

@@ -0,0 +1,8 @@
import type { GenService, GenServiceMethods } from "./types.js";
import type { DescFile } from "../descriptors.js";
/**
* Hydrate a service descriptor.
*
* @private
*/
export declare function serviceDesc<T extends GenServiceMethods>(file: DescFile, path: number, ...paths: number[]): GenService<T>;

View File

@@ -0,0 +1,27 @@
"use strict";
// Copyright 2021-2025 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.serviceDesc = serviceDesc;
/**
* Hydrate a service descriptor.
*
* @private
*/
function serviceDesc(file, path, ...paths) {
if (paths.length > 0) {
throw new Error();
}
return file.services[path];
}

View File

@@ -0,0 +1,135 @@
/**
* @private
*/
export declare const packageName = "@bufbuild/protobuf";
/**
* @private
*/
export declare const wktPublicImportPaths: Readonly<Record<string, string>>;
/**
* @private
*/
export declare const symbols: {
readonly isMessage: {
readonly typeOnly: false;
readonly bootstrapWktFrom: "../../is-message.js";
readonly from: "@bufbuild/protobuf";
};
readonly Message: {
readonly typeOnly: true;
readonly bootstrapWktFrom: "../../types.js";
readonly from: "@bufbuild/protobuf";
};
readonly create: {
readonly typeOnly: false;
readonly bootstrapWktFrom: "../../create.js";
readonly from: "@bufbuild/protobuf";
};
readonly fromJson: {
readonly typeOnly: false;
readonly bootstrapWktFrom: "../../from-json.js";
readonly from: "@bufbuild/protobuf";
};
readonly fromJsonString: {
readonly typeOnly: false;
readonly bootstrapWktFrom: "../../from-json.js";
readonly from: "@bufbuild/protobuf";
};
readonly fromBinary: {
readonly typeOnly: false;
readonly bootstrapWktFrom: "../../from-binary.js";
readonly from: "@bufbuild/protobuf";
};
readonly toBinary: {
readonly typeOnly: false;
readonly bootstrapWktFrom: "../../to-binary.js";
readonly from: "@bufbuild/protobuf";
};
readonly toJson: {
readonly typeOnly: false;
readonly bootstrapWktFrom: "../../to-json.js";
readonly from: "@bufbuild/protobuf";
};
readonly toJsonString: {
readonly typeOnly: false;
readonly bootstrapWktFrom: "../../to-json.js";
readonly from: "@bufbuild/protobuf";
};
readonly protoInt64: {
readonly typeOnly: false;
readonly bootstrapWktFrom: "../../proto-int64.js";
readonly from: "@bufbuild/protobuf";
};
readonly JsonValue: {
readonly typeOnly: true;
readonly bootstrapWktFrom: "../../json-value.js";
readonly from: "@bufbuild/protobuf";
};
readonly JsonObject: {
readonly typeOnly: true;
readonly bootstrapWktFrom: "../../json-value.js";
readonly from: "@bufbuild/protobuf";
};
readonly codegen: {
readonly boot: {
readonly typeOnly: false;
readonly bootstrapWktFrom: "../../codegenv2/boot.js";
readonly from: string;
};
readonly fileDesc: {
readonly typeOnly: false;
readonly bootstrapWktFrom: "../../codegenv2/file.js";
readonly from: string;
};
readonly enumDesc: {
readonly typeOnly: false;
readonly bootstrapWktFrom: "../../codegenv2/enum.js";
readonly from: string;
};
readonly extDesc: {
readonly typeOnly: false;
readonly bootstrapWktFrom: "../../codegenv2/extension.js";
readonly from: string;
};
readonly messageDesc: {
readonly typeOnly: false;
readonly bootstrapWktFrom: "../../codegenv2/message.js";
readonly from: string;
};
readonly serviceDesc: {
readonly typeOnly: false;
readonly bootstrapWktFrom: "../../codegenv2/service.js";
readonly from: string;
};
readonly tsEnum: {
readonly typeOnly: false;
readonly bootstrapWktFrom: "../../codegenv2/enum.js";
readonly from: string;
};
readonly GenFile: {
readonly typeOnly: true;
readonly bootstrapWktFrom: "../../codegenv2/types.js";
readonly from: string;
};
readonly GenEnum: {
readonly typeOnly: true;
readonly bootstrapWktFrom: "../../codegenv2/types.js";
readonly from: string;
};
readonly GenExtension: {
readonly typeOnly: true;
readonly bootstrapWktFrom: "../../codegenv2/types.js";
readonly from: string;
};
readonly GenMessage: {
readonly typeOnly: true;
readonly bootstrapWktFrom: "../../codegenv2/types.js";
readonly from: string;
};
readonly GenService: {
readonly typeOnly: true;
readonly bootstrapWktFrom: "../../codegenv2/types.js";
readonly from: string;
};
};
};

View File

@@ -0,0 +1,72 @@
"use strict";
// Copyright 2021-2025 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.symbols = exports.wktPublicImportPaths = exports.packageName = void 0;
/**
* @private
*/
exports.packageName = "@bufbuild/protobuf";
/**
* @private
*/
exports.wktPublicImportPaths = {
"google/protobuf/compiler/plugin.proto": exports.packageName + "/wkt",
"google/protobuf/any.proto": exports.packageName + "/wkt",
"google/protobuf/api.proto": exports.packageName + "/wkt",
"google/protobuf/cpp_features.proto": exports.packageName + "/wkt",
"google/protobuf/descriptor.proto": exports.packageName + "/wkt",
"google/protobuf/duration.proto": exports.packageName + "/wkt",
"google/protobuf/empty.proto": exports.packageName + "/wkt",
"google/protobuf/field_mask.proto": exports.packageName + "/wkt",
"google/protobuf/go_features.proto": exports.packageName + "/wkt",
"google/protobuf/java_features.proto": exports.packageName + "/wkt",
"google/protobuf/source_context.proto": exports.packageName + "/wkt",
"google/protobuf/struct.proto": exports.packageName + "/wkt",
"google/protobuf/timestamp.proto": exports.packageName + "/wkt",
"google/protobuf/type.proto": exports.packageName + "/wkt",
"google/protobuf/wrappers.proto": exports.packageName + "/wkt",
};
/**
* @private
*/
// biome-ignore format: want this to read well
exports.symbols = {
isMessage: { typeOnly: false, bootstrapWktFrom: "../../is-message.js", from: exports.packageName },
Message: { typeOnly: true, bootstrapWktFrom: "../../types.js", from: exports.packageName },
create: { typeOnly: false, bootstrapWktFrom: "../../create.js", from: exports.packageName },
fromJson: { typeOnly: false, bootstrapWktFrom: "../../from-json.js", from: exports.packageName },
fromJsonString: { typeOnly: false, bootstrapWktFrom: "../../from-json.js", from: exports.packageName },
fromBinary: { typeOnly: false, bootstrapWktFrom: "../../from-binary.js", from: exports.packageName },
toBinary: { typeOnly: false, bootstrapWktFrom: "../../to-binary.js", from: exports.packageName },
toJson: { typeOnly: false, bootstrapWktFrom: "../../to-json.js", from: exports.packageName },
toJsonString: { typeOnly: false, bootstrapWktFrom: "../../to-json.js", from: exports.packageName },
protoInt64: { typeOnly: false, bootstrapWktFrom: "../../proto-int64.js", from: exports.packageName },
JsonValue: { typeOnly: true, bootstrapWktFrom: "../../json-value.js", from: exports.packageName },
JsonObject: { typeOnly: true, bootstrapWktFrom: "../../json-value.js", from: exports.packageName },
codegen: {
boot: { typeOnly: false, bootstrapWktFrom: "../../codegenv2/boot.js", from: exports.packageName + "/codegenv2" },
fileDesc: { typeOnly: false, bootstrapWktFrom: "../../codegenv2/file.js", from: exports.packageName + "/codegenv2" },
enumDesc: { typeOnly: false, bootstrapWktFrom: "../../codegenv2/enum.js", from: exports.packageName + "/codegenv2" },
extDesc: { typeOnly: false, bootstrapWktFrom: "../../codegenv2/extension.js", from: exports.packageName + "/codegenv2" },
messageDesc: { typeOnly: false, bootstrapWktFrom: "../../codegenv2/message.js", from: exports.packageName + "/codegenv2" },
serviceDesc: { typeOnly: false, bootstrapWktFrom: "../../codegenv2/service.js", from: exports.packageName + "/codegenv2" },
tsEnum: { typeOnly: false, bootstrapWktFrom: "../../codegenv2/enum.js", from: exports.packageName + "/codegenv2" },
GenFile: { typeOnly: true, bootstrapWktFrom: "../../codegenv2/types.js", from: exports.packageName + "/codegenv2" },
GenEnum: { typeOnly: true, bootstrapWktFrom: "../../codegenv2/types.js", from: exports.packageName + "/codegenv2" },
GenExtension: { typeOnly: true, bootstrapWktFrom: "../../codegenv2/types.js", from: exports.packageName + "/codegenv2" },
GenMessage: { typeOnly: true, bootstrapWktFrom: "../../codegenv2/types.js", from: exports.packageName + "/codegenv2" },
GenService: { typeOnly: true, bootstrapWktFrom: "../../codegenv2/types.js", from: exports.packageName + "/codegenv2" },
},
};

View File

@@ -0,0 +1,81 @@
import type { Message } from "../types.js";
import type { DescEnum, DescEnumValue, DescExtension, DescField, DescFile, DescMessage, DescMethod, DescService } from "../descriptors.js";
import type { JsonValue } from "../json-value.js";
/**
* Describes a protobuf source file.
*
* @private
*/
export type GenFile = DescFile;
/**
* Describes a message declaration in a protobuf source file.
*
* This type is identical to DescMessage, but carries additional type
* information.
*
* @private
*/
export type GenMessage<RuntimeShape extends Message, Opt extends {
jsonType?: unknown;
validType?: unknown;
} = {
jsonType?: unknown;
validType?: unknown;
}> = Omit<DescMessage, "field" | "typeName"> & {
field: Record<MessageFieldNames<RuntimeShape>, DescField>;
typeName: RuntimeShape["$typeName"];
} & brandv2<RuntimeShape, Opt>;
/**
* Describes an enumeration in a protobuf source file.
*
* This type is identical to DescEnum, but carries additional type
* information.
*
* @private
*/
export type GenEnum<RuntimeShape extends number, JsonType extends JsonValue = JsonValue> = Omit<DescEnum, "value"> & {
value: Record<RuntimeShape, DescEnumValue>;
} & brandv2<RuntimeShape, JsonType>;
/**
* Describes an extension in a protobuf source file.
*
* This type is identical to DescExtension, but carries additional type
* information.
*
* @private
*/
export type GenExtension<Extendee extends Message = Message, RuntimeShape = unknown> = DescExtension & brandv2<Extendee, RuntimeShape>;
/**
* Describes a service declaration in a protobuf source file.
*
* This type is identical to DescService, but carries additional type
* information.
*
* @private
*/
export type GenService<RuntimeShape extends GenServiceMethods> = Omit<DescService, "method"> & {
method: {
[K in keyof RuntimeShape]: RuntimeShape[K] & DescMethod;
};
};
/**
* @private
*/
export type GenServiceMethods = Record<string, Pick<DescMethod, "input" | "output" | "methodKind">>;
declare class brandv2<A, B = unknown> {
protected v: "codegenv2";
protected a: A | boolean;
protected b: B | boolean;
}
/**
* Union of the property names of all fields, including oneof members.
* For an anonymous message (no generated message shape), it's simply a string.
*/
type MessageFieldNames<T extends Message> = Message extends T ? string : Exclude<keyof {
[P in keyof T as P extends ("$typeName" | "$unknown") ? never : T[P] extends Oneof<infer K> ? K : P]-?: true;
}, number | symbol>;
type Oneof<K extends string> = {
case: K | undefined;
value?: unknown;
};
export {};

View File

@@ -0,0 +1,22 @@
"use strict";
// Copyright 2021-2025 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Object.defineProperty(exports, "__esModule", { value: true });
class brandv2 {
constructor() {
this.v = "codegenv2";
this.a = false;
this.b = false;
}
}