@bufbuild/protobuf
Advanced tools
Comparing version 1.4.2 to 1.5.0
@@ -1,3 +0,2 @@ | ||
import type { FieldDescriptorProto } from "./google/protobuf/descriptor_pb.js"; | ||
import { FileDescriptorProto, FileDescriptorSet } from "./google/protobuf/descriptor_pb.js"; | ||
import { FeatureSetDefaults, FileDescriptorProto, FileDescriptorSet } from "./google/protobuf/descriptor_pb.js"; | ||
import type { DescriptorSet } from "./descriptor-set.js"; | ||
@@ -12,6 +11,21 @@ /** | ||
*/ | ||
export declare function createDescriptorSet(input: FileDescriptorProto[] | FileDescriptorSet | Uint8Array): DescriptorSet; | ||
export declare function createDescriptorSet(input: FileDescriptorProto[] | FileDescriptorSet | Uint8Array, options?: CreateDescriptorSetOptions): DescriptorSet; | ||
/** | ||
* Get the default `packed` state of a repeated field. | ||
* Options to createDescriptorSet() | ||
*/ | ||
export declare function isPackedFieldByDefault(proto: FieldDescriptorProto, syntax: "proto2" | "proto3"): boolean; | ||
interface CreateDescriptorSetOptions { | ||
/** | ||
* Editions support language-specific features with extensions to | ||
* google.protobuf.FeatureSet. They can define defaults, and specify on | ||
* which targets the features can be set. | ||
* | ||
* To create a DescriptorSet that provides your language-specific features, | ||
* you have to provide a google.protobuf.FeatureSetDefaults message in this | ||
* option. | ||
* | ||
* The defaults can be generated with `protoc` - see the flag | ||
* `--experimental_edition_defaults_out`. | ||
*/ | ||
featureSetDefaults?: FeatureSetDefaults; | ||
} | ||
export {}; |
@@ -16,3 +16,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isPackedFieldByDefault = exports.createDescriptorSet = void 0; | ||
exports.createDescriptorSet = void 0; | ||
const descriptor_pb_js_1 = require("./google/protobuf/descriptor_pb.js"); | ||
@@ -23,3 +23,4 @@ const assert_js_1 = require("./private/assert.js"); | ||
const names_js_1 = require("./private/names.js"); | ||
const proto_int64_js_1 = require("./proto-int64.js"); | ||
const text_format_js_1 = require("./private/text-format.js"); | ||
const feature_set_js_1 = require("./private/feature-set.js"); | ||
/** | ||
@@ -33,3 +34,4 @@ * Create a DescriptorSet, a convenient interface for working with a set of | ||
*/ | ||
function createDescriptorSet(input) { | ||
function createDescriptorSet(input, options) { | ||
var _a; | ||
const cart = { | ||
@@ -41,2 +43,3 @@ enums: new Map(), | ||
mapEntries: new Map(), | ||
resolveFeatures: (0, feature_set_js_1.createFeatureResolver)((_a = options === null || options === void 0 ? void 0 : options.featureSetDefaults) !== null && _a !== void 0 ? _a : feature_set_js_1.featureSetDefaults), | ||
}; | ||
@@ -56,16 +59,5 @@ const fileDescriptors = input instanceof descriptor_pb_js_1.FileDescriptorSet | ||
function newFile(proto, cart) { | ||
var _a, _b, _c; | ||
var _a, _b; | ||
(0, assert_js_1.assert)(proto.name, `invalid FileDescriptorProto: missing name`); | ||
(0, assert_js_1.assert)(proto.syntax === undefined || proto.syntax === "proto3", `invalid FileDescriptorProto: unsupported syntax: ${(_a = proto.syntax) !== null && _a !== void 0 ? _a : "undefined"}`); | ||
const file = { | ||
kind: "file", | ||
proto, | ||
deprecated: (_c = (_b = proto.options) === null || _b === void 0 ? void 0 : _b.deprecated) !== null && _c !== void 0 ? _c : false, | ||
syntax: proto.syntax === "proto3" ? "proto3" : "proto2", | ||
name: proto.name.replace(/\.proto/, ""), | ||
enums: [], | ||
messages: [], | ||
extensions: [], | ||
services: [], | ||
toString() { | ||
const file = Object.assign(Object.assign({ kind: "file", proto, deprecated: (_b = (_a = proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false }, parseFileSyntax(proto.syntax, proto.edition)), { name: proto.name.replace(/\.proto/, ""), enums: [], messages: [], extensions: [], services: [], toString() { | ||
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- we asserted above | ||
@@ -84,3 +76,6 @@ return `file ${this.proto.name}`; | ||
}, | ||
}; | ||
getFeatures() { | ||
var _a; | ||
return cart.resolveFeatures(this.edition, (_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.features); | ||
} }); | ||
cart.mapEntries.clear(); // map entries are local to the file, we can safely discard | ||
@@ -138,3 +133,3 @@ for (const enumProto of proto.enumType) { | ||
function addFields(message, cart) { | ||
const allOneofs = message.proto.oneofDecl.map((proto) => newOneof(proto, message)); | ||
const allOneofs = message.proto.oneofDecl.map((proto) => newOneof(proto, message, cart)); | ||
const oneofsSeen = new Set(); | ||
@@ -196,2 +191,7 @@ for (const proto of message.proto.field) { | ||
}, | ||
getFeatures() { | ||
var _a, _b, _c; | ||
const parentFeatures = (_b = (_a = this.parent) === null || _a === void 0 ? void 0 : _a.getFeatures()) !== null && _b !== void 0 ? _b : this.file.getFeatures(); | ||
return cart.resolveFeatures(this.file.edition, parentFeatures, (_c = this.proto.options) === null || _c === void 0 ? void 0 : _c.features); | ||
}, | ||
}; | ||
@@ -229,2 +229,6 @@ cart.enums.set(desc.typeName, desc); | ||
}, | ||
getFeatures() { | ||
var _a; | ||
return cart.resolveFeatures(this.parent.file.edition, this.parent.getFeatures(), (_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.features); | ||
}, | ||
}); | ||
@@ -271,2 +275,7 @@ }); | ||
}, | ||
getFeatures() { | ||
var _a, _b, _c; | ||
const parentFeatures = (_b = (_a = this.parent) === null || _a === void 0 ? void 0 : _a.getFeatures()) !== null && _b !== void 0 ? _b : this.file.getFeatures(); | ||
return cart.resolveFeatures(this.file.edition, parentFeatures, (_c = this.proto.options) === null || _c === void 0 ? void 0 : _c.features); | ||
}, | ||
}; | ||
@@ -312,2 +321,6 @@ if (((_c = proto.options) === null || _c === void 0 ? void 0 : _c.mapEntry) === true) { | ||
}, | ||
getFeatures() { | ||
var _a; | ||
return cart.resolveFeatures(this.file.edition, this.file.getFeatures(), (_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.features); | ||
}, | ||
}; | ||
@@ -380,2 +393,6 @@ file.services.push(desc); | ||
}, | ||
getFeatures() { | ||
var _a; | ||
return cart.resolveFeatures(this.parent.file.edition, this.parent.getFeatures(), (_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.features); | ||
}, | ||
}; | ||
@@ -386,3 +403,3 @@ } | ||
*/ | ||
function newOneof(proto, parent) { | ||
function newOneof(proto, parent, cart) { | ||
(0, assert_js_1.assert)(proto.name, `invalid OneofDescriptorProto: missing name`); | ||
@@ -407,2 +424,6 @@ return { | ||
}, | ||
getFeatures() { | ||
var _a; | ||
return cart.resolveFeatures(this.parent.file.edition, this.parent.getFeatures(), (_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.features); | ||
}, | ||
}; | ||
@@ -414,7 +435,6 @@ } | ||
function newField(proto, file, parent, oneof, cart) { | ||
var _a, _b, _c, _d, _e; | ||
var _a, _b, _c; | ||
(0, assert_js_1.assert)(proto.name, `invalid FieldDescriptorProto: missing name`); | ||
(0, assert_js_1.assert)(proto.number, `invalid FieldDescriptorProto: missing number`); | ||
(0, assert_js_1.assert)(proto.type, `invalid FieldDescriptorProto: missing type`); | ||
const packedByDefault = isPackedFieldByDefault(proto, file.syntax); | ||
const common = { | ||
@@ -428,4 +448,4 @@ proto, | ||
optional: isOptionalField(proto, file.syntax), | ||
packed: (_d = (_c = proto.options) === null || _c === void 0 ? void 0 : _c.packed) !== null && _d !== void 0 ? _d : packedByDefault, | ||
packedByDefault, | ||
packedByDefault: isPackedFieldByDefault(file, proto, cart.resolveFeatures), | ||
packed: isPackedField(file, parent, proto, cart.resolveFeatures), | ||
jsonName: proto.jsonName === (0, names_js_1.fieldJsonName)(proto.name) ? undefined : proto.jsonName, | ||
@@ -438,7 +458,7 @@ scalar: undefined, | ||
mapValue: undefined, | ||
declarationString, | ||
// toString, getComments, getFeatures are overridden in newExtension | ||
toString() { | ||
// note that newExtension() calls us with parent = null | ||
return `field ${this.parent.typeName}.${this.name}`; | ||
}, | ||
declarationString, | ||
getComments() { | ||
@@ -452,2 +472,6 @@ const path = [ | ||
}, | ||
getFeatures() { | ||
var _a; | ||
return cart.resolveFeatures(file.edition, this.parent.getFeatures(), (_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.features); | ||
}, | ||
}; | ||
@@ -481,3 +505,3 @@ const repeated = proto.label === descriptor_pb_js_1.FieldDescriptorProto_Label.REPEATED; | ||
repeated, | ||
scalar, longType: ((_e = proto.options) === null || _e === void 0 ? void 0 : _e.jstype) == descriptor_pb_js_1.FieldOptions_JSType.JS_STRING | ||
scalar, longType: ((_c = proto.options) === null || _c === void 0 ? void 0 : _c.jstype) == descriptor_pb_js_1.FieldOptions_JSType.JS_STRING | ||
? field_js_1.LongType.STRING | ||
@@ -500,2 +524,4 @@ : field_js_1.LongType.BIGINT }); | ||
extendee, | ||
// Must override toString, getComments, getFeatures from newField, because we | ||
// call newField with parent undefined. | ||
toString() { | ||
@@ -516,5 +542,53 @@ return `extension ${this.typeName}`; | ||
return findComments(file.proto.sourceCodeInfo, path); | ||
}, | ||
getFeatures() { | ||
var _a, _b; | ||
return cart.resolveFeatures(this.file.edition, ((_a = this.parent) !== null && _a !== void 0 ? _a : this.file).getFeatures(), (_b = this.proto.options) === null || _b === void 0 ? void 0 : _b.features); | ||
} }); | ||
} | ||
/** | ||
* Parse the "syntax" and "edition" fields, stripping test editions. | ||
*/ | ||
function parseFileSyntax(syntax, edition) { | ||
let e; | ||
let s; | ||
switch (syntax) { | ||
case undefined: | ||
case "proto2": | ||
s = "proto2"; | ||
e = descriptor_pb_js_1.Edition.EDITION_PROTO2; | ||
break; | ||
case "proto3": | ||
s = "proto3"; | ||
e = descriptor_pb_js_1.Edition.EDITION_PROTO3; | ||
break; | ||
case "editions": | ||
s = "editions"; | ||
switch (edition) { | ||
case undefined: | ||
case descriptor_pb_js_1.Edition.EDITION_1_TEST_ONLY: | ||
case descriptor_pb_js_1.Edition.EDITION_2_TEST_ONLY: | ||
case descriptor_pb_js_1.Edition.EDITION_99997_TEST_ONLY: | ||
case descriptor_pb_js_1.Edition.EDITION_99998_TEST_ONLY: | ||
case descriptor_pb_js_1.Edition.EDITION_99999_TEST_ONLY: | ||
case descriptor_pb_js_1.Edition.EDITION_UNKNOWN: | ||
e = descriptor_pb_js_1.Edition.EDITION_UNKNOWN; | ||
break; | ||
default: | ||
e = edition; | ||
break; | ||
} | ||
break; | ||
default: | ||
throw new Error(`invalid FileDescriptorProto: unsupported syntax: ${syntax}`); | ||
} | ||
if (syntax === "editions" && edition === descriptor_pb_js_1.Edition.EDITION_UNKNOWN) { | ||
throw new Error(`invalid FileDescriptorProto: syntax ${syntax} cannot have edition ${String(edition)}`); | ||
} | ||
return { | ||
syntax: s, | ||
edition: e, | ||
}; | ||
} | ||
/** | ||
* Create a fully qualified name for a protobuf type or extension field. | ||
@@ -616,39 +690,63 @@ * | ||
return proto.proto3Optional === true; | ||
case "editions": | ||
return false; | ||
} | ||
} | ||
/** | ||
* Get the default `packed` state of a repeated field. | ||
* Is this field packed by default? Only valid for repeated enum fields, and | ||
* for repeated scalar fields except BYTES and STRING. | ||
* | ||
* In proto3 syntax, fields are packed by default. In proto2 syntax, fields | ||
* are unpacked by default. With editions, the default is whatever the edition | ||
* specifies as a default. In edition 2023, fields are packed by default. | ||
*/ | ||
function isPackedFieldByDefault(proto, syntax) { | ||
(0, assert_js_1.assert)(proto.type, `invalid FieldDescriptorProto: missing type`); | ||
if (syntax === "proto3") { | ||
switch (proto.type) { | ||
case descriptor_pb_js_1.FieldDescriptorProto_Type.DOUBLE: | ||
case descriptor_pb_js_1.FieldDescriptorProto_Type.FLOAT: | ||
case descriptor_pb_js_1.FieldDescriptorProto_Type.INT64: | ||
case descriptor_pb_js_1.FieldDescriptorProto_Type.UINT64: | ||
case descriptor_pb_js_1.FieldDescriptorProto_Type.INT32: | ||
case descriptor_pb_js_1.FieldDescriptorProto_Type.FIXED64: | ||
case descriptor_pb_js_1.FieldDescriptorProto_Type.FIXED32: | ||
case descriptor_pb_js_1.FieldDescriptorProto_Type.UINT32: | ||
case descriptor_pb_js_1.FieldDescriptorProto_Type.SFIXED32: | ||
case descriptor_pb_js_1.FieldDescriptorProto_Type.SFIXED64: | ||
case descriptor_pb_js_1.FieldDescriptorProto_Type.SINT32: | ||
case descriptor_pb_js_1.FieldDescriptorProto_Type.SINT64: | ||
case descriptor_pb_js_1.FieldDescriptorProto_Type.BOOL: | ||
case descriptor_pb_js_1.FieldDescriptorProto_Type.ENUM: | ||
// From the proto3 language guide: | ||
// > In proto3, repeated fields of scalar numeric types are packed by default. | ||
// This information is incomplete - according to the conformance tests, BOOL | ||
// and ENUM are packed by default as well. This means only STRING and BYTES | ||
// are not packed by default, which makes sense because they are length-delimited. | ||
return true; | ||
default: | ||
return false; | ||
} | ||
function isPackedFieldByDefault(file, proto, resolveFeatures) { | ||
const { repeatedFieldEncoding } = resolveFeatures(file.edition); | ||
if (repeatedFieldEncoding != descriptor_pb_js_1.FeatureSet_RepeatedFieldEncoding.PACKED) { | ||
return false; | ||
} | ||
return false; | ||
// From the proto3 language guide: | ||
// > In proto3, repeated fields of scalar numeric types are packed by default. | ||
// This information is incomplete - according to the conformance tests, BOOL | ||
// and ENUM are packed by default as well. This means only STRING and BYTES | ||
// are not packed by default, which makes sense because they are length-delimited. | ||
switch (proto.type) { | ||
case descriptor_pb_js_1.FieldDescriptorProto_Type.STRING: | ||
case descriptor_pb_js_1.FieldDescriptorProto_Type.BYTES: | ||
case descriptor_pb_js_1.FieldDescriptorProto_Type.GROUP: | ||
case descriptor_pb_js_1.FieldDescriptorProto_Type.MESSAGE: | ||
return false; | ||
default: | ||
return true; | ||
} | ||
} | ||
exports.isPackedFieldByDefault = isPackedFieldByDefault; | ||
/** | ||
* Pack this repeated field? | ||
* | ||
* Respects field type, proto2/proto3 defaults and the `packed` option, or | ||
* edition defaults and the edition features.repeated_field_encoding options. | ||
*/ | ||
function isPackedField(file, parent, proto, resolveFeatures) { | ||
var _a, _b, _c, _d, _e, _f; | ||
switch (proto.type) { | ||
case descriptor_pb_js_1.FieldDescriptorProto_Type.STRING: | ||
case descriptor_pb_js_1.FieldDescriptorProto_Type.BYTES: | ||
case descriptor_pb_js_1.FieldDescriptorProto_Type.GROUP: | ||
case descriptor_pb_js_1.FieldDescriptorProto_Type.MESSAGE: | ||
// length-delimited types cannot be packed | ||
return false; | ||
default: | ||
switch (file.edition) { | ||
case descriptor_pb_js_1.Edition.EDITION_PROTO2: | ||
return (_b = (_a = proto.options) === null || _a === void 0 ? void 0 : _a.packed) !== null && _b !== void 0 ? _b : false; | ||
case descriptor_pb_js_1.Edition.EDITION_PROTO3: | ||
return (_d = (_c = proto.options) === null || _c === void 0 ? void 0 : _c.packed) !== null && _d !== void 0 ? _d : true; | ||
default: { | ||
const { repeatedFieldEncoding } = resolveFeatures(file.edition, (_e = parent === null || parent === void 0 ? void 0 : parent.getFeatures()) !== null && _e !== void 0 ? _e : file.getFeatures(), (_f = proto.options) === null || _f === void 0 ? void 0 : _f.features); | ||
return (repeatedFieldEncoding == descriptor_pb_js_1.FeatureSet_RepeatedFieldEncoding.PACKED); | ||
} | ||
} | ||
} | ||
} | ||
/** | ||
* Map from a compiler-generated field type to our ScalarType, which is a | ||
@@ -809,47 +907,6 @@ * subset of field types declared by protobuf enum google.protobuf.FieldDescriptorProto. | ||
switch (this.fieldKind) { | ||
case "enum": { | ||
const enumValue = this.enum.values.find((v) => v.name === d); | ||
(0, assert_js_1.assert)(enumValue, `cannot parse ${this.toString()} default value: ${d}`); | ||
return enumValue.number; | ||
} | ||
case "enum": | ||
return (0, text_format_js_1.parseTextFormatEnumValue)(this.enum, d); | ||
case "scalar": | ||
switch (this.scalar) { | ||
case field_js_1.ScalarType.STRING: | ||
return d; | ||
case field_js_1.ScalarType.BYTES: { | ||
const u = unescapeBytesDefaultValue(d); | ||
if (u === false) { | ||
throw new Error(`cannot parse ${this.toString()} default value: ${d}`); | ||
} | ||
return u; | ||
} | ||
case field_js_1.ScalarType.INT64: | ||
case field_js_1.ScalarType.SFIXED64: | ||
case field_js_1.ScalarType.SINT64: | ||
return proto_int64_js_1.protoInt64.parse(d); | ||
case field_js_1.ScalarType.UINT64: | ||
case field_js_1.ScalarType.FIXED64: | ||
return proto_int64_js_1.protoInt64.uParse(d); | ||
case field_js_1.ScalarType.DOUBLE: | ||
case field_js_1.ScalarType.FLOAT: | ||
switch (d) { | ||
case "inf": | ||
return Number.POSITIVE_INFINITY; | ||
case "-inf": | ||
return Number.NEGATIVE_INFINITY; | ||
case "nan": | ||
return Number.NaN; | ||
default: | ||
return parseFloat(d); | ||
} | ||
case field_js_1.ScalarType.BOOL: | ||
return d === "true"; | ||
case field_js_1.ScalarType.INT32: | ||
case field_js_1.ScalarType.UINT32: | ||
case field_js_1.ScalarType.SINT32: | ||
case field_js_1.ScalarType.FIXED32: | ||
case field_js_1.ScalarType.SFIXED32: | ||
return parseInt(d, 10); | ||
} | ||
break; | ||
return (0, text_format_js_1.parseTextFormatScalarValue)(this.scalar, d); | ||
default: | ||
@@ -859,124 +916,1 @@ return undefined; | ||
} | ||
/** | ||
* Parses a text-encoded default value (proto2) of a BYTES field. | ||
*/ | ||
function unescapeBytesDefaultValue(str) { | ||
const b = []; | ||
const input = { | ||
tail: str, | ||
c: "", | ||
next() { | ||
if (this.tail.length == 0) { | ||
return false; | ||
} | ||
this.c = this.tail[0]; | ||
this.tail = this.tail.substring(1); | ||
return true; | ||
}, | ||
take(n) { | ||
if (this.tail.length >= n) { | ||
const r = this.tail.substring(0, n); | ||
this.tail = this.tail.substring(n); | ||
return r; | ||
} | ||
return false; | ||
}, | ||
}; | ||
while (input.next()) { | ||
switch (input.c) { | ||
case "\\": | ||
if (input.next()) { | ||
switch (input.c) { | ||
case "\\": | ||
b.push(input.c.charCodeAt(0)); | ||
break; | ||
case "b": | ||
b.push(0x08); | ||
break; | ||
case "f": | ||
b.push(0x0c); | ||
break; | ||
case "n": | ||
b.push(0x0a); | ||
break; | ||
case "r": | ||
b.push(0x0d); | ||
break; | ||
case "t": | ||
b.push(0x09); | ||
break; | ||
case "v": | ||
b.push(0x0b); | ||
break; | ||
case "0": | ||
case "1": | ||
case "2": | ||
case "3": | ||
case "4": | ||
case "5": | ||
case "6": | ||
case "7": { | ||
const s = input.c; | ||
const t = input.take(2); | ||
if (t === false) { | ||
return false; | ||
} | ||
const n = parseInt(s + t, 8); | ||
if (isNaN(n)) { | ||
return false; | ||
} | ||
b.push(n); | ||
break; | ||
} | ||
case "x": { | ||
const s = input.c; | ||
const t = input.take(2); | ||
if (t === false) { | ||
return false; | ||
} | ||
const n = parseInt(s + t, 16); | ||
if (isNaN(n)) { | ||
return false; | ||
} | ||
b.push(n); | ||
break; | ||
} | ||
case "u": { | ||
const s = input.c; | ||
const t = input.take(4); | ||
if (t === false) { | ||
return false; | ||
} | ||
const n = parseInt(s + t, 16); | ||
if (isNaN(n)) { | ||
return false; | ||
} | ||
const chunk = new Uint8Array(4); | ||
const view = new DataView(chunk.buffer); | ||
view.setInt32(0, n, true); | ||
b.push(chunk[0], chunk[1], chunk[2], chunk[3]); | ||
break; | ||
} | ||
case "U": { | ||
const s = input.c; | ||
const t = input.take(8); | ||
if (t === false) { | ||
return false; | ||
} | ||
const tc = proto_int64_js_1.protoInt64.uEnc(s + t); | ||
const chunk = new Uint8Array(8); | ||
const view = new DataView(chunk.buffer); | ||
view.setInt32(0, tc.lo, true); | ||
view.setInt32(4, tc.hi, true); | ||
b.push(chunk[0], chunk[1], chunk[2], chunk[3], chunk[4], chunk[5], chunk[6], chunk[7]); | ||
break; | ||
} | ||
} | ||
} | ||
break; | ||
default: | ||
b.push(input.c.charCodeAt(0)); | ||
} | ||
} | ||
return new Uint8Array(b); | ||
} |
@@ -1,4 +0,5 @@ | ||
import type { DescriptorProto, EnumDescriptorProto, EnumValueDescriptorProto, FieldDescriptorProto, FileDescriptorProto, MethodDescriptorProto, OneofDescriptorProto, ServiceDescriptorProto } from "./google/protobuf/descriptor_pb.js"; | ||
import type { ScalarType, LongType } from "./field.js"; | ||
import type { DescriptorProto, Edition, EnumDescriptorProto, EnumValueDescriptorProto, FieldDescriptorProto, FileDescriptorProto, MethodDescriptorProto, OneofDescriptorProto, ServiceDescriptorProto } from "./google/protobuf/descriptor_pb.js"; | ||
import type { LongType, ScalarType } from "./field.js"; | ||
import type { MethodIdempotency, MethodKind } from "./service-type.js"; | ||
import type { MergedFeatureSet } from "./private/feature-set.js"; | ||
/** | ||
@@ -53,4 +54,9 @@ * DescriptorSet provides a convenient interface for working with a set | ||
*/ | ||
readonly syntax: "proto3" | "proto2"; | ||
readonly syntax: "proto3" | "proto2" | "editions"; | ||
/** | ||
* The edition of the protobuf file. Will be EDITION_PROTO2 for syntax="proto2", | ||
* EDITION_PROTO3 for syntax="proto3"; | ||
*/ | ||
readonly edition: Exclude<Edition, Edition.EDITION_1_TEST_ONLY | Edition.EDITION_2_TEST_ONLY | Edition.EDITION_99997_TEST_ONLY | Edition.EDITION_99998_TEST_ONLY | Edition.EDITION_99999_TEST_ONLY>; | ||
/** | ||
* The name of the file, excluding the .proto suffix. | ||
@@ -95,2 +101,6 @@ * For a protobuf file `foo/bar.proto`, this is `foo/bar`. | ||
getPackageComments(): DescComments; | ||
/** | ||
* Get the edition features for this protobuf element. | ||
*/ | ||
getFeatures(): MergedFeatureSet; | ||
toString(): string; | ||
@@ -140,2 +150,6 @@ } | ||
getComments(): DescComments; | ||
/** | ||
* Get the edition features for this protobuf element. | ||
*/ | ||
getFeatures(): MergedFeatureSet; | ||
toString(): string; | ||
@@ -177,2 +191,6 @@ } | ||
getComments(): DescComments; | ||
/** | ||
* Get the edition features for this protobuf element. | ||
*/ | ||
getFeatures(): MergedFeatureSet; | ||
toString(): string; | ||
@@ -241,2 +259,6 @@ } | ||
getComments(): DescComments; | ||
/** | ||
* Get the edition features for this protobuf element. | ||
*/ | ||
getFeatures(): MergedFeatureSet; | ||
toString(): string; | ||
@@ -298,6 +320,10 @@ } | ||
/** | ||
* Is this field packed by default? Only valid for enum fields, and for | ||
* scalar fields except BYTES and STRING. | ||
* Is this field packed by default? Only valid for repeated enum fields, and | ||
* for repeated scalar fields except BYTES and STRING. | ||
* | ||
* In proto3 syntax, fields are packed by default. In proto2 syntax, fields | ||
* are unpacked by default. | ||
* | ||
* With editions, the default is whatever the edition specifies as a default. | ||
* In edition 2023, fields are packed by default. | ||
*/ | ||
@@ -327,2 +353,6 @@ readonly packedByDefault: boolean; | ||
declarationString(): string; | ||
/** | ||
* Get the edition features for this protobuf element. | ||
*/ | ||
getFeatures(): MergedFeatureSet; | ||
toString(): string; | ||
@@ -544,2 +574,6 @@ } | ||
getComments(): DescComments; | ||
/** | ||
* Get the edition features for this protobuf element. | ||
*/ | ||
getFeatures(): MergedFeatureSet; | ||
toString(): string; | ||
@@ -580,2 +614,6 @@ } | ||
getComments(): DescComments; | ||
/** | ||
* Get the edition features for this protobuf element. | ||
*/ | ||
getFeatures(): MergedFeatureSet; | ||
toString(): string; | ||
@@ -624,2 +662,6 @@ } | ||
getComments(): DescComments; | ||
/** | ||
* Get the edition features for this protobuf element. | ||
*/ | ||
getFeatures(): MergedFeatureSet; | ||
toString(): string; | ||
@@ -626,0 +668,0 @@ } |
@@ -243,3 +243,3 @@ "use strict"; | ||
const slash = url.lastIndexOf("/"); | ||
const name = slash > 0 ? url.substring(slash + 1) : url; | ||
const name = slash >= 0 ? url.substring(slash + 1) : url; | ||
if (!name.length) { | ||
@@ -246,0 +246,0 @@ throw new Error(`invalid type url: ${url}`); |
@@ -17,3 +17,3 @@ "use strict"; | ||
exports.ListValue = exports.Value = exports.Struct = exports.NullValue = void 0; | ||
// @generated by protoc-gen-es v1.4.2 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts" | ||
// @generated by protoc-gen-es v1.5.0 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts" | ||
// @generated from file google/protobuf/struct.proto (package google.protobuf, syntax proto3) | ||
@@ -20,0 +20,0 @@ /* eslint-disable */ |
@@ -17,3 +17,3 @@ "use strict"; | ||
exports.Option = exports.EnumValue = exports.Enum = exports.Field_Cardinality = exports.Field_Kind = exports.Field = exports.Type = exports.Syntax = void 0; | ||
// @generated by protoc-gen-es v1.4.2 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts" | ||
// @generated by protoc-gen-es v1.5.0 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts" | ||
// @generated from file google/protobuf/type.proto (package google.protobuf, syntax proto3) | ||
@@ -20,0 +20,0 @@ /* eslint-disable */ |
@@ -1,3 +0,2 @@ | ||
import type { FieldDescriptorProto } from "./google/protobuf/descriptor_pb.js"; | ||
import { FileDescriptorProto, FileDescriptorSet } from "./google/protobuf/descriptor_pb.js"; | ||
import { FeatureSetDefaults, FileDescriptorProto, FileDescriptorSet } from "./google/protobuf/descriptor_pb.js"; | ||
import type { DescriptorSet } from "./descriptor-set.js"; | ||
@@ -12,6 +11,21 @@ /** | ||
*/ | ||
export declare function createDescriptorSet(input: FileDescriptorProto[] | FileDescriptorSet | Uint8Array): DescriptorSet; | ||
export declare function createDescriptorSet(input: FileDescriptorProto[] | FileDescriptorSet | Uint8Array, options?: CreateDescriptorSetOptions): DescriptorSet; | ||
/** | ||
* Get the default `packed` state of a repeated field. | ||
* Options to createDescriptorSet() | ||
*/ | ||
export declare function isPackedFieldByDefault(proto: FieldDescriptorProto, syntax: "proto2" | "proto3"): boolean; | ||
interface CreateDescriptorSetOptions { | ||
/** | ||
* Editions support language-specific features with extensions to | ||
* google.protobuf.FeatureSet. They can define defaults, and specify on | ||
* which targets the features can be set. | ||
* | ||
* To create a DescriptorSet that provides your language-specific features, | ||
* you have to provide a google.protobuf.FeatureSetDefaults message in this | ||
* option. | ||
* | ||
* The defaults can be generated with `protoc` - see the flag | ||
* `--experimental_edition_defaults_out`. | ||
*/ | ||
featureSetDefaults?: FeatureSetDefaults; | ||
} | ||
export {}; |
@@ -14,3 +14,3 @@ // Copyright 2021-2023 Buf Technologies, Inc. | ||
// limitations under the License. | ||
import { FieldDescriptorProto_Label, FieldDescriptorProto_Type, FieldOptions_JSType, FileDescriptorProto, FileDescriptorSet, MethodDescriptorProto, MethodOptions_IdempotencyLevel, ServiceDescriptorProto, SourceCodeInfo, } from "./google/protobuf/descriptor_pb.js"; | ||
import { Edition, FeatureSet_RepeatedFieldEncoding, FeatureSetDefaults, FieldDescriptorProto_Label, FieldDescriptorProto_Type, FieldOptions_JSType, FileDescriptorProto, FileDescriptorSet, MethodDescriptorProto, MethodOptions_IdempotencyLevel, ServiceDescriptorProto, SourceCodeInfo, } from "./google/protobuf/descriptor_pb.js"; | ||
import { assert } from "./private/assert.js"; | ||
@@ -20,3 +20,4 @@ import { LongType, ScalarType } from "./field.js"; | ||
import { fieldJsonName, findEnumSharedPrefix } from "./private/names.js"; | ||
import { protoInt64 } from "./proto-int64.js"; | ||
import { parseTextFormatEnumValue, parseTextFormatScalarValue, } from "./private/text-format.js"; | ||
import { createFeatureResolver, featureSetDefaults, } from "./private/feature-set.js"; | ||
/** | ||
@@ -30,3 +31,4 @@ * Create a DescriptorSet, a convenient interface for working with a set of | ||
*/ | ||
export function createDescriptorSet(input) { | ||
export function createDescriptorSet(input, options) { | ||
var _a; | ||
const cart = { | ||
@@ -38,2 +40,3 @@ enums: new Map(), | ||
mapEntries: new Map(), | ||
resolveFeatures: createFeatureResolver((_a = options === null || options === void 0 ? void 0 : options.featureSetDefaults) !== null && _a !== void 0 ? _a : featureSetDefaults), | ||
}; | ||
@@ -52,16 +55,5 @@ const fileDescriptors = input instanceof FileDescriptorSet | ||
function newFile(proto, cart) { | ||
var _a, _b, _c; | ||
var _a, _b; | ||
assert(proto.name, `invalid FileDescriptorProto: missing name`); | ||
assert(proto.syntax === undefined || proto.syntax === "proto3", `invalid FileDescriptorProto: unsupported syntax: ${(_a = proto.syntax) !== null && _a !== void 0 ? _a : "undefined"}`); | ||
const file = { | ||
kind: "file", | ||
proto, | ||
deprecated: (_c = (_b = proto.options) === null || _b === void 0 ? void 0 : _b.deprecated) !== null && _c !== void 0 ? _c : false, | ||
syntax: proto.syntax === "proto3" ? "proto3" : "proto2", | ||
name: proto.name.replace(/\.proto/, ""), | ||
enums: [], | ||
messages: [], | ||
extensions: [], | ||
services: [], | ||
toString() { | ||
const file = Object.assign(Object.assign({ kind: "file", proto, deprecated: (_b = (_a = proto.options) === null || _a === void 0 ? void 0 : _a.deprecated) !== null && _b !== void 0 ? _b : false }, parseFileSyntax(proto.syntax, proto.edition)), { name: proto.name.replace(/\.proto/, ""), enums: [], messages: [], extensions: [], services: [], toString() { | ||
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- we asserted above | ||
@@ -80,3 +72,6 @@ return `file ${this.proto.name}`; | ||
}, | ||
}; | ||
getFeatures() { | ||
var _a; | ||
return cart.resolveFeatures(this.edition, (_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.features); | ||
} }); | ||
cart.mapEntries.clear(); // map entries are local to the file, we can safely discard | ||
@@ -134,3 +129,3 @@ for (const enumProto of proto.enumType) { | ||
function addFields(message, cart) { | ||
const allOneofs = message.proto.oneofDecl.map((proto) => newOneof(proto, message)); | ||
const allOneofs = message.proto.oneofDecl.map((proto) => newOneof(proto, message, cart)); | ||
const oneofsSeen = new Set(); | ||
@@ -192,2 +187,7 @@ for (const proto of message.proto.field) { | ||
}, | ||
getFeatures() { | ||
var _a, _b, _c; | ||
const parentFeatures = (_b = (_a = this.parent) === null || _a === void 0 ? void 0 : _a.getFeatures()) !== null && _b !== void 0 ? _b : this.file.getFeatures(); | ||
return cart.resolveFeatures(this.file.edition, parentFeatures, (_c = this.proto.options) === null || _c === void 0 ? void 0 : _c.features); | ||
}, | ||
}; | ||
@@ -225,2 +225,6 @@ cart.enums.set(desc.typeName, desc); | ||
}, | ||
getFeatures() { | ||
var _a; | ||
return cart.resolveFeatures(this.parent.file.edition, this.parent.getFeatures(), (_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.features); | ||
}, | ||
}); | ||
@@ -267,2 +271,7 @@ }); | ||
}, | ||
getFeatures() { | ||
var _a, _b, _c; | ||
const parentFeatures = (_b = (_a = this.parent) === null || _a === void 0 ? void 0 : _a.getFeatures()) !== null && _b !== void 0 ? _b : this.file.getFeatures(); | ||
return cart.resolveFeatures(this.file.edition, parentFeatures, (_c = this.proto.options) === null || _c === void 0 ? void 0 : _c.features); | ||
}, | ||
}; | ||
@@ -308,2 +317,6 @@ if (((_c = proto.options) === null || _c === void 0 ? void 0 : _c.mapEntry) === true) { | ||
}, | ||
getFeatures() { | ||
var _a; | ||
return cart.resolveFeatures(this.file.edition, this.file.getFeatures(), (_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.features); | ||
}, | ||
}; | ||
@@ -376,2 +389,6 @@ file.services.push(desc); | ||
}, | ||
getFeatures() { | ||
var _a; | ||
return cart.resolveFeatures(this.parent.file.edition, this.parent.getFeatures(), (_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.features); | ||
}, | ||
}; | ||
@@ -382,3 +399,3 @@ } | ||
*/ | ||
function newOneof(proto, parent) { | ||
function newOneof(proto, parent, cart) { | ||
assert(proto.name, `invalid OneofDescriptorProto: missing name`); | ||
@@ -403,2 +420,6 @@ return { | ||
}, | ||
getFeatures() { | ||
var _a; | ||
return cart.resolveFeatures(this.parent.file.edition, this.parent.getFeatures(), (_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.features); | ||
}, | ||
}; | ||
@@ -410,7 +431,6 @@ } | ||
function newField(proto, file, parent, oneof, cart) { | ||
var _a, _b, _c, _d, _e; | ||
var _a, _b, _c; | ||
assert(proto.name, `invalid FieldDescriptorProto: missing name`); | ||
assert(proto.number, `invalid FieldDescriptorProto: missing number`); | ||
assert(proto.type, `invalid FieldDescriptorProto: missing type`); | ||
const packedByDefault = isPackedFieldByDefault(proto, file.syntax); | ||
const common = { | ||
@@ -424,4 +444,4 @@ proto, | ||
optional: isOptionalField(proto, file.syntax), | ||
packed: (_d = (_c = proto.options) === null || _c === void 0 ? void 0 : _c.packed) !== null && _d !== void 0 ? _d : packedByDefault, | ||
packedByDefault, | ||
packedByDefault: isPackedFieldByDefault(file, proto, cart.resolveFeatures), | ||
packed: isPackedField(file, parent, proto, cart.resolveFeatures), | ||
jsonName: proto.jsonName === fieldJsonName(proto.name) ? undefined : proto.jsonName, | ||
@@ -434,7 +454,7 @@ scalar: undefined, | ||
mapValue: undefined, | ||
declarationString, | ||
// toString, getComments, getFeatures are overridden in newExtension | ||
toString() { | ||
// note that newExtension() calls us with parent = null | ||
return `field ${this.parent.typeName}.${this.name}`; | ||
}, | ||
declarationString, | ||
getComments() { | ||
@@ -448,2 +468,6 @@ const path = [ | ||
}, | ||
getFeatures() { | ||
var _a; | ||
return cart.resolveFeatures(file.edition, this.parent.getFeatures(), (_a = this.proto.options) === null || _a === void 0 ? void 0 : _a.features); | ||
}, | ||
}; | ||
@@ -477,3 +501,3 @@ const repeated = proto.label === FieldDescriptorProto_Label.REPEATED; | ||
repeated, | ||
scalar, longType: ((_e = proto.options) === null || _e === void 0 ? void 0 : _e.jstype) == FieldOptions_JSType.JS_STRING | ||
scalar, longType: ((_c = proto.options) === null || _c === void 0 ? void 0 : _c.jstype) == FieldOptions_JSType.JS_STRING | ||
? LongType.STRING | ||
@@ -496,2 +520,4 @@ : LongType.BIGINT }); | ||
extendee, | ||
// Must override toString, getComments, getFeatures from newField, because we | ||
// call newField with parent undefined. | ||
toString() { | ||
@@ -512,5 +538,53 @@ return `extension ${this.typeName}`; | ||
return findComments(file.proto.sourceCodeInfo, path); | ||
}, | ||
getFeatures() { | ||
var _a, _b; | ||
return cart.resolveFeatures(this.file.edition, ((_a = this.parent) !== null && _a !== void 0 ? _a : this.file).getFeatures(), (_b = this.proto.options) === null || _b === void 0 ? void 0 : _b.features); | ||
} }); | ||
} | ||
/** | ||
* Parse the "syntax" and "edition" fields, stripping test editions. | ||
*/ | ||
function parseFileSyntax(syntax, edition) { | ||
let e; | ||
let s; | ||
switch (syntax) { | ||
case undefined: | ||
case "proto2": | ||
s = "proto2"; | ||
e = Edition.EDITION_PROTO2; | ||
break; | ||
case "proto3": | ||
s = "proto3"; | ||
e = Edition.EDITION_PROTO3; | ||
break; | ||
case "editions": | ||
s = "editions"; | ||
switch (edition) { | ||
case undefined: | ||
case Edition.EDITION_1_TEST_ONLY: | ||
case Edition.EDITION_2_TEST_ONLY: | ||
case Edition.EDITION_99997_TEST_ONLY: | ||
case Edition.EDITION_99998_TEST_ONLY: | ||
case Edition.EDITION_99999_TEST_ONLY: | ||
case Edition.EDITION_UNKNOWN: | ||
e = Edition.EDITION_UNKNOWN; | ||
break; | ||
default: | ||
e = edition; | ||
break; | ||
} | ||
break; | ||
default: | ||
throw new Error(`invalid FileDescriptorProto: unsupported syntax: ${syntax}`); | ||
} | ||
if (syntax === "editions" && edition === Edition.EDITION_UNKNOWN) { | ||
throw new Error(`invalid FileDescriptorProto: syntax ${syntax} cannot have edition ${String(edition)}`); | ||
} | ||
return { | ||
syntax: s, | ||
edition: e, | ||
}; | ||
} | ||
/** | ||
* Create a fully qualified name for a protobuf type or extension field. | ||
@@ -612,38 +686,63 @@ * | ||
return proto.proto3Optional === true; | ||
case "editions": | ||
return false; | ||
} | ||
} | ||
/** | ||
* Get the default `packed` state of a repeated field. | ||
* Is this field packed by default? Only valid for repeated enum fields, and | ||
* for repeated scalar fields except BYTES and STRING. | ||
* | ||
* In proto3 syntax, fields are packed by default. In proto2 syntax, fields | ||
* are unpacked by default. With editions, the default is whatever the edition | ||
* specifies as a default. In edition 2023, fields are packed by default. | ||
*/ | ||
export function isPackedFieldByDefault(proto, syntax) { | ||
assert(proto.type, `invalid FieldDescriptorProto: missing type`); | ||
if (syntax === "proto3") { | ||
switch (proto.type) { | ||
case FieldDescriptorProto_Type.DOUBLE: | ||
case FieldDescriptorProto_Type.FLOAT: | ||
case FieldDescriptorProto_Type.INT64: | ||
case FieldDescriptorProto_Type.UINT64: | ||
case FieldDescriptorProto_Type.INT32: | ||
case FieldDescriptorProto_Type.FIXED64: | ||
case FieldDescriptorProto_Type.FIXED32: | ||
case FieldDescriptorProto_Type.UINT32: | ||
case FieldDescriptorProto_Type.SFIXED32: | ||
case FieldDescriptorProto_Type.SFIXED64: | ||
case FieldDescriptorProto_Type.SINT32: | ||
case FieldDescriptorProto_Type.SINT64: | ||
case FieldDescriptorProto_Type.BOOL: | ||
case FieldDescriptorProto_Type.ENUM: | ||
// From the proto3 language guide: | ||
// > In proto3, repeated fields of scalar numeric types are packed by default. | ||
// This information is incomplete - according to the conformance tests, BOOL | ||
// and ENUM are packed by default as well. This means only STRING and BYTES | ||
// are not packed by default, which makes sense because they are length-delimited. | ||
return true; | ||
default: | ||
return false; | ||
} | ||
function isPackedFieldByDefault(file, proto, resolveFeatures) { | ||
const { repeatedFieldEncoding } = resolveFeatures(file.edition); | ||
if (repeatedFieldEncoding != FeatureSet_RepeatedFieldEncoding.PACKED) { | ||
return false; | ||
} | ||
return false; | ||
// From the proto3 language guide: | ||
// > In proto3, repeated fields of scalar numeric types are packed by default. | ||
// This information is incomplete - according to the conformance tests, BOOL | ||
// and ENUM are packed by default as well. This means only STRING and BYTES | ||
// are not packed by default, which makes sense because they are length-delimited. | ||
switch (proto.type) { | ||
case FieldDescriptorProto_Type.STRING: | ||
case FieldDescriptorProto_Type.BYTES: | ||
case FieldDescriptorProto_Type.GROUP: | ||
case FieldDescriptorProto_Type.MESSAGE: | ||
return false; | ||
default: | ||
return true; | ||
} | ||
} | ||
/** | ||
* Pack this repeated field? | ||
* | ||
* Respects field type, proto2/proto3 defaults and the `packed` option, or | ||
* edition defaults and the edition features.repeated_field_encoding options. | ||
*/ | ||
function isPackedField(file, parent, proto, resolveFeatures) { | ||
var _a, _b, _c, _d, _e, _f; | ||
switch (proto.type) { | ||
case FieldDescriptorProto_Type.STRING: | ||
case FieldDescriptorProto_Type.BYTES: | ||
case FieldDescriptorProto_Type.GROUP: | ||
case FieldDescriptorProto_Type.MESSAGE: | ||
// length-delimited types cannot be packed | ||
return false; | ||
default: | ||
switch (file.edition) { | ||
case Edition.EDITION_PROTO2: | ||
return (_b = (_a = proto.options) === null || _a === void 0 ? void 0 : _a.packed) !== null && _b !== void 0 ? _b : false; | ||
case Edition.EDITION_PROTO3: | ||
return (_d = (_c = proto.options) === null || _c === void 0 ? void 0 : _c.packed) !== null && _d !== void 0 ? _d : true; | ||
default: { | ||
const { repeatedFieldEncoding } = resolveFeatures(file.edition, (_e = parent === null || parent === void 0 ? void 0 : parent.getFeatures()) !== null && _e !== void 0 ? _e : file.getFeatures(), (_f = proto.options) === null || _f === void 0 ? void 0 : _f.features); | ||
return (repeatedFieldEncoding == FeatureSet_RepeatedFieldEncoding.PACKED); | ||
} | ||
} | ||
} | ||
} | ||
/** | ||
* Map from a compiler-generated field type to our ScalarType, which is a | ||
@@ -804,47 +903,6 @@ * subset of field types declared by protobuf enum google.protobuf.FieldDescriptorProto. | ||
switch (this.fieldKind) { | ||
case "enum": { | ||
const enumValue = this.enum.values.find((v) => v.name === d); | ||
assert(enumValue, `cannot parse ${this.toString()} default value: ${d}`); | ||
return enumValue.number; | ||
} | ||
case "enum": | ||
return parseTextFormatEnumValue(this.enum, d); | ||
case "scalar": | ||
switch (this.scalar) { | ||
case ScalarType.STRING: | ||
return d; | ||
case ScalarType.BYTES: { | ||
const u = unescapeBytesDefaultValue(d); | ||
if (u === false) { | ||
throw new Error(`cannot parse ${this.toString()} default value: ${d}`); | ||
} | ||
return u; | ||
} | ||
case ScalarType.INT64: | ||
case ScalarType.SFIXED64: | ||
case ScalarType.SINT64: | ||
return protoInt64.parse(d); | ||
case ScalarType.UINT64: | ||
case ScalarType.FIXED64: | ||
return protoInt64.uParse(d); | ||
case ScalarType.DOUBLE: | ||
case ScalarType.FLOAT: | ||
switch (d) { | ||
case "inf": | ||
return Number.POSITIVE_INFINITY; | ||
case "-inf": | ||
return Number.NEGATIVE_INFINITY; | ||
case "nan": | ||
return Number.NaN; | ||
default: | ||
return parseFloat(d); | ||
} | ||
case ScalarType.BOOL: | ||
return d === "true"; | ||
case ScalarType.INT32: | ||
case ScalarType.UINT32: | ||
case ScalarType.SINT32: | ||
case ScalarType.FIXED32: | ||
case ScalarType.SFIXED32: | ||
return parseInt(d, 10); | ||
} | ||
break; | ||
return parseTextFormatScalarValue(this.scalar, d); | ||
default: | ||
@@ -854,124 +912,1 @@ return undefined; | ||
} | ||
/** | ||
* Parses a text-encoded default value (proto2) of a BYTES field. | ||
*/ | ||
function unescapeBytesDefaultValue(str) { | ||
const b = []; | ||
const input = { | ||
tail: str, | ||
c: "", | ||
next() { | ||
if (this.tail.length == 0) { | ||
return false; | ||
} | ||
this.c = this.tail[0]; | ||
this.tail = this.tail.substring(1); | ||
return true; | ||
}, | ||
take(n) { | ||
if (this.tail.length >= n) { | ||
const r = this.tail.substring(0, n); | ||
this.tail = this.tail.substring(n); | ||
return r; | ||
} | ||
return false; | ||
}, | ||
}; | ||
while (input.next()) { | ||
switch (input.c) { | ||
case "\\": | ||
if (input.next()) { | ||
switch (input.c) { | ||
case "\\": | ||
b.push(input.c.charCodeAt(0)); | ||
break; | ||
case "b": | ||
b.push(0x08); | ||
break; | ||
case "f": | ||
b.push(0x0c); | ||
break; | ||
case "n": | ||
b.push(0x0a); | ||
break; | ||
case "r": | ||
b.push(0x0d); | ||
break; | ||
case "t": | ||
b.push(0x09); | ||
break; | ||
case "v": | ||
b.push(0x0b); | ||
break; | ||
case "0": | ||
case "1": | ||
case "2": | ||
case "3": | ||
case "4": | ||
case "5": | ||
case "6": | ||
case "7": { | ||
const s = input.c; | ||
const t = input.take(2); | ||
if (t === false) { | ||
return false; | ||
} | ||
const n = parseInt(s + t, 8); | ||
if (isNaN(n)) { | ||
return false; | ||
} | ||
b.push(n); | ||
break; | ||
} | ||
case "x": { | ||
const s = input.c; | ||
const t = input.take(2); | ||
if (t === false) { | ||
return false; | ||
} | ||
const n = parseInt(s + t, 16); | ||
if (isNaN(n)) { | ||
return false; | ||
} | ||
b.push(n); | ||
break; | ||
} | ||
case "u": { | ||
const s = input.c; | ||
const t = input.take(4); | ||
if (t === false) { | ||
return false; | ||
} | ||
const n = parseInt(s + t, 16); | ||
if (isNaN(n)) { | ||
return false; | ||
} | ||
const chunk = new Uint8Array(4); | ||
const view = new DataView(chunk.buffer); | ||
view.setInt32(0, n, true); | ||
b.push(chunk[0], chunk[1], chunk[2], chunk[3]); | ||
break; | ||
} | ||
case "U": { | ||
const s = input.c; | ||
const t = input.take(8); | ||
if (t === false) { | ||
return false; | ||
} | ||
const tc = protoInt64.uEnc(s + t); | ||
const chunk = new Uint8Array(8); | ||
const view = new DataView(chunk.buffer); | ||
view.setInt32(0, tc.lo, true); | ||
view.setInt32(4, tc.hi, true); | ||
b.push(chunk[0], chunk[1], chunk[2], chunk[3], chunk[4], chunk[5], chunk[6], chunk[7]); | ||
break; | ||
} | ||
} | ||
} | ||
break; | ||
default: | ||
b.push(input.c.charCodeAt(0)); | ||
} | ||
} | ||
return new Uint8Array(b); | ||
} |
@@ -1,4 +0,5 @@ | ||
import type { DescriptorProto, EnumDescriptorProto, EnumValueDescriptorProto, FieldDescriptorProto, FileDescriptorProto, MethodDescriptorProto, OneofDescriptorProto, ServiceDescriptorProto } from "./google/protobuf/descriptor_pb.js"; | ||
import type { ScalarType, LongType } from "./field.js"; | ||
import type { DescriptorProto, Edition, EnumDescriptorProto, EnumValueDescriptorProto, FieldDescriptorProto, FileDescriptorProto, MethodDescriptorProto, OneofDescriptorProto, ServiceDescriptorProto } from "./google/protobuf/descriptor_pb.js"; | ||
import type { LongType, ScalarType } from "./field.js"; | ||
import type { MethodIdempotency, MethodKind } from "./service-type.js"; | ||
import type { MergedFeatureSet } from "./private/feature-set.js"; | ||
/** | ||
@@ -53,4 +54,9 @@ * DescriptorSet provides a convenient interface for working with a set | ||
*/ | ||
readonly syntax: "proto3" | "proto2"; | ||
readonly syntax: "proto3" | "proto2" | "editions"; | ||
/** | ||
* The edition of the protobuf file. Will be EDITION_PROTO2 for syntax="proto2", | ||
* EDITION_PROTO3 for syntax="proto3"; | ||
*/ | ||
readonly edition: Exclude<Edition, Edition.EDITION_1_TEST_ONLY | Edition.EDITION_2_TEST_ONLY | Edition.EDITION_99997_TEST_ONLY | Edition.EDITION_99998_TEST_ONLY | Edition.EDITION_99999_TEST_ONLY>; | ||
/** | ||
* The name of the file, excluding the .proto suffix. | ||
@@ -95,2 +101,6 @@ * For a protobuf file `foo/bar.proto`, this is `foo/bar`. | ||
getPackageComments(): DescComments; | ||
/** | ||
* Get the edition features for this protobuf element. | ||
*/ | ||
getFeatures(): MergedFeatureSet; | ||
toString(): string; | ||
@@ -140,2 +150,6 @@ } | ||
getComments(): DescComments; | ||
/** | ||
* Get the edition features for this protobuf element. | ||
*/ | ||
getFeatures(): MergedFeatureSet; | ||
toString(): string; | ||
@@ -177,2 +191,6 @@ } | ||
getComments(): DescComments; | ||
/** | ||
* Get the edition features for this protobuf element. | ||
*/ | ||
getFeatures(): MergedFeatureSet; | ||
toString(): string; | ||
@@ -241,2 +259,6 @@ } | ||
getComments(): DescComments; | ||
/** | ||
* Get the edition features for this protobuf element. | ||
*/ | ||
getFeatures(): MergedFeatureSet; | ||
toString(): string; | ||
@@ -298,6 +320,10 @@ } | ||
/** | ||
* Is this field packed by default? Only valid for enum fields, and for | ||
* scalar fields except BYTES and STRING. | ||
* Is this field packed by default? Only valid for repeated enum fields, and | ||
* for repeated scalar fields except BYTES and STRING. | ||
* | ||
* In proto3 syntax, fields are packed by default. In proto2 syntax, fields | ||
* are unpacked by default. | ||
* | ||
* With editions, the default is whatever the edition specifies as a default. | ||
* In edition 2023, fields are packed by default. | ||
*/ | ||
@@ -327,2 +353,6 @@ readonly packedByDefault: boolean; | ||
declarationString(): string; | ||
/** | ||
* Get the edition features for this protobuf element. | ||
*/ | ||
getFeatures(): MergedFeatureSet; | ||
toString(): string; | ||
@@ -544,2 +574,6 @@ } | ||
getComments(): DescComments; | ||
/** | ||
* Get the edition features for this protobuf element. | ||
*/ | ||
getFeatures(): MergedFeatureSet; | ||
toString(): string; | ||
@@ -580,2 +614,6 @@ } | ||
getComments(): DescComments; | ||
/** | ||
* Get the edition features for this protobuf element. | ||
*/ | ||
getFeatures(): MergedFeatureSet; | ||
toString(): string; | ||
@@ -624,2 +662,6 @@ } | ||
getComments(): DescComments; | ||
/** | ||
* Get the edition features for this protobuf element. | ||
*/ | ||
getFeatures(): MergedFeatureSet; | ||
toString(): string; | ||
@@ -626,0 +668,0 @@ } |
@@ -240,3 +240,3 @@ // Copyright 2021-2023 Buf Technologies, Inc. | ||
const slash = url.lastIndexOf("/"); | ||
const name = slash > 0 ? url.substring(slash + 1) : url; | ||
const name = slash >= 0 ? url.substring(slash + 1) : url; | ||
if (!name.length) { | ||
@@ -243,0 +243,0 @@ throw new Error(`invalid type url: ${url}`); |
@@ -14,3 +14,3 @@ // Copyright 2021-2023 Buf Technologies, Inc. | ||
// limitations under the License. | ||
// @generated by protoc-gen-es v1.4.2 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts" | ||
// @generated by protoc-gen-es v1.5.0 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts" | ||
// @generated from file google/protobuf/struct.proto (package google.protobuf, syntax proto3) | ||
@@ -17,0 +17,0 @@ /* eslint-disable */ |
@@ -14,3 +14,3 @@ // Copyright 2021-2023 Buf Technologies, Inc. | ||
// limitations under the License. | ||
// @generated by protoc-gen-es v1.4.2 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts" | ||
// @generated by protoc-gen-es v1.5.0 with parameter "bootstrap_wkt=true,ts_nocheck=false,target=ts" | ||
// @generated from file google/protobuf/type.proto (package google.protobuf, syntax proto3) | ||
@@ -17,0 +17,0 @@ /* eslint-disable */ |
{ | ||
"name": "@bufbuild/protobuf", | ||
"version": "1.4.2", | ||
"version": "1.5.0", | ||
"license": "(Apache-2.0 AND BSD-3-Clause)", | ||
@@ -17,6 +17,6 @@ "description": "A complete implementation of Protocol Buffers in TypeScript, suitable for web browsers and Node.js.", | ||
"build:proxy": "node ../../scripts/gen-esm-proxy.mjs .", | ||
"prebootstrap": "rm -rf .tmp && mkdir -p .tmp/google/protobuf && cp -rp src/google/protobuf/* .tmp/google/protobuf", | ||
"bootstrap": "protoc --es_out=src --es_opt=bootstrap_wkt=true,ts_nocheck=false,target=ts --proto_path $(upstream-include wkt) $(upstream-files wkt)", | ||
"postbootstrap": "license-header src/google/protobuf", | ||
"bootstrap-diff": "diff >/dev/null -r .tmp/google/protobuf src/google/protobuf && cp -rp .tmp/google/protobuf/* src/google/protobuf", | ||
"bootstrap:featureset-defaults": "upstream-inject-feature-defaults src/private/feature-set.ts", | ||
"prebootstrap:wkt": "rm -rf .tmp && mkdir -p .tmp/google/protobuf && cp -rp src/google/protobuf/* .tmp/google/protobuf", | ||
"bootstrap:wkt": "protoc --es_out=src --es_opt=bootstrap_wkt=true,ts_nocheck=false,target=ts --proto_path $(upstream-include wkt) $(upstream-files wkt) && license-header src/google/protobuf", | ||
"postbootstrap:wkt": "diff >/dev/null -r .tmp/google/protobuf src/google/protobuf && cp -rp .tmp/google/protobuf/* src/google/protobuf || true", | ||
"attw": "attw --pack" | ||
@@ -23,0 +23,0 @@ }, |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found
Found 1 instance in 1 package
1404730
233
35663
3