@bufbuild/protobuf 2.2.5 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/clone.js +4 -6
- package/dist/cjs/codegenv1/boot.js +1 -1
- package/dist/cjs/codegenv1/embed.js +0 -1
- package/dist/cjs/codegenv1/file.js +1 -1
- package/dist/cjs/codegenv1/symbols.js +1 -1
- package/dist/cjs/create.js +0 -1
- package/dist/cjs/equals.js +2 -1
- package/dist/cjs/extensions.js +1 -1
- package/dist/cjs/from-binary.js +5 -3
- package/dist/cjs/from-json.js +8 -10
- package/dist/cjs/proto-int64.js +4 -1
- package/dist/cjs/reflect/guard.js +2 -1
- package/dist/cjs/reflect/index.d.ts +1 -0
- package/dist/cjs/reflect/index.js +1 -0
- package/dist/cjs/reflect/names.d.ts +8 -0
- package/dist/cjs/reflect/names.js +28 -0
- package/dist/cjs/reflect/path.d.ts +107 -0
- package/dist/cjs/reflect/path.js +376 -0
- package/dist/cjs/reflect/reflect-check.js +3 -3
- package/dist/cjs/reflect/reflect-types.d.ts +1 -1
- package/dist/cjs/reflect/reflect.js +9 -7
- package/dist/cjs/reflect/scalar.js +4 -5
- package/dist/cjs/reflect/unsafe.d.ts +3 -6
- package/dist/cjs/reflect/unsafe.js +11 -10
- package/dist/cjs/registry.js +17 -25
- package/dist/cjs/to-binary.js +1 -1
- package/dist/cjs/to-json.d.ts +6 -0
- package/dist/cjs/to-json.js +16 -18
- package/dist/cjs/wire/base64-encoding.js +4 -3
- package/dist/cjs/wire/binary-encoding.js +7 -6
- package/dist/cjs/wire/text-encoding.js +1 -1
- package/dist/cjs/wire/text-format.js +3 -4
- package/dist/cjs/wire/varint.js +0 -1
- package/dist/esm/clone.js +4 -6
- package/dist/esm/codegenv1/boot.js +1 -1
- package/dist/esm/codegenv1/embed.js +0 -1
- package/dist/esm/codegenv1/file.js +1 -1
- package/dist/esm/codegenv1/symbols.js +1 -1
- package/dist/esm/create.js +0 -2
- package/dist/esm/equals.js +2 -1
- package/dist/esm/extensions.js +1 -1
- package/dist/esm/from-binary.js +5 -3
- package/dist/esm/from-json.js +8 -10
- package/dist/esm/proto-int64.js +4 -1
- package/dist/esm/reflect/guard.js +2 -1
- package/dist/esm/reflect/index.d.ts +1 -0
- package/dist/esm/reflect/index.js +1 -0
- package/dist/esm/reflect/names.d.ts +8 -0
- package/dist/esm/reflect/names.js +27 -0
- package/dist/esm/reflect/path.d.ts +107 -0
- package/dist/esm/reflect/path.js +369 -0
- package/dist/esm/reflect/reflect-check.js +3 -3
- package/dist/esm/reflect/reflect-types.d.ts +1 -1
- package/dist/esm/reflect/reflect-types.js +0 -1
- package/dist/esm/reflect/reflect.js +9 -7
- package/dist/esm/reflect/scalar.js +4 -5
- package/dist/esm/reflect/unsafe.d.ts +3 -6
- package/dist/esm/reflect/unsafe.js +11 -10
- package/dist/esm/registry.js +17 -25
- package/dist/esm/to-binary.js +1 -1
- package/dist/esm/to-json.d.ts +6 -0
- package/dist/esm/to-json.js +16 -18
- package/dist/esm/wire/base64-encoding.js +4 -3
- package/dist/esm/wire/binary-encoding.js +7 -6
- package/dist/esm/wire/text-encoding.js +1 -1
- package/dist/esm/wire/text-format.js +3 -4
- package/dist/esm/wire/varint.js +0 -1
- package/package.json +9 -24
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
import type { AnyDesc } from "../descriptors.js";
|
|
2
|
+
/**
|
|
3
|
+
* Return a fully-qualified name for a Protobuf descriptor.
|
|
4
|
+
* For a file descriptor, return the original file path.
|
|
5
|
+
*
|
|
6
|
+
* See https://protobuf.com/docs/language-spec#fully-qualified-names
|
|
7
|
+
*/
|
|
8
|
+
export declare function qualifiedName(desc: AnyDesc): string;
|
|
1
9
|
/**
|
|
2
10
|
* Converts snake_case to protoCamelCase according to the convention
|
|
3
11
|
* used by protoc to convert a field name to a JSON name.
|
|
@@ -11,6 +11,33 @@
|
|
|
11
11
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
12
|
// See the License for the specific language governing permissions and
|
|
13
13
|
// limitations under the License.
|
|
14
|
+
/**
|
|
15
|
+
* Return a fully-qualified name for a Protobuf descriptor.
|
|
16
|
+
* For a file descriptor, return the original file path.
|
|
17
|
+
*
|
|
18
|
+
* See https://protobuf.com/docs/language-spec#fully-qualified-names
|
|
19
|
+
*/
|
|
20
|
+
export function qualifiedName(desc) {
|
|
21
|
+
switch (desc.kind) {
|
|
22
|
+
case "field":
|
|
23
|
+
case "oneof":
|
|
24
|
+
case "rpc":
|
|
25
|
+
return desc.parent.typeName + "." + desc.name;
|
|
26
|
+
case "enum_value": {
|
|
27
|
+
const p = desc.parent.parent
|
|
28
|
+
? desc.parent.parent.typeName
|
|
29
|
+
: desc.parent.file.proto.package;
|
|
30
|
+
return p + (p.length > 0 ? "." : "") + desc.name;
|
|
31
|
+
}
|
|
32
|
+
case "service":
|
|
33
|
+
case "message":
|
|
34
|
+
case "enum":
|
|
35
|
+
case "extension":
|
|
36
|
+
return desc.typeName;
|
|
37
|
+
case "file":
|
|
38
|
+
return desc.proto.name;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
14
41
|
/**
|
|
15
42
|
* Converts snake_case to protoCamelCase according to the convention
|
|
16
43
|
* used by protoc to convert a field name to a JSON name.
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { type DescExtension, type DescField, type DescMessage, type DescOneof } from "../descriptors.js";
|
|
2
|
+
import type { Registry } from "../registry.js";
|
|
3
|
+
/**
|
|
4
|
+
* A path to a (nested) member of a Protobuf message, such as a field, oneof,
|
|
5
|
+
* extension, list element, or map entry.
|
|
6
|
+
*
|
|
7
|
+
* Note that we may add additional types to this union in the future to support
|
|
8
|
+
* more use cases.
|
|
9
|
+
*/
|
|
10
|
+
export type Path = (DescField | DescExtension | DescOneof | {
|
|
11
|
+
kind: "list_sub";
|
|
12
|
+
index: number;
|
|
13
|
+
} | {
|
|
14
|
+
kind: "map_sub";
|
|
15
|
+
key: string | number | bigint | boolean;
|
|
16
|
+
})[];
|
|
17
|
+
/**
|
|
18
|
+
* Builds a Path.
|
|
19
|
+
*/
|
|
20
|
+
export type PathBuilder = {
|
|
21
|
+
/**
|
|
22
|
+
* The root message of the path.
|
|
23
|
+
*/
|
|
24
|
+
readonly schema: DescMessage;
|
|
25
|
+
/**
|
|
26
|
+
* Add field access.
|
|
27
|
+
*
|
|
28
|
+
* Throws an InvalidPathError if the field cannot be added to the path.
|
|
29
|
+
*/
|
|
30
|
+
field(field: DescField): PathBuilder;
|
|
31
|
+
/**
|
|
32
|
+
* Access a oneof.
|
|
33
|
+
*
|
|
34
|
+
* Throws an InvalidPathError if the oneof cannot be added to the path.
|
|
35
|
+
*
|
|
36
|
+
*/
|
|
37
|
+
oneof(oneof: DescOneof): PathBuilder;
|
|
38
|
+
/**
|
|
39
|
+
* Access an extension.
|
|
40
|
+
*
|
|
41
|
+
* Throws an InvalidPathError if the extension cannot be added to the path.
|
|
42
|
+
*/
|
|
43
|
+
extension(extension: DescExtension): PathBuilder;
|
|
44
|
+
/**
|
|
45
|
+
* Access a list field by index.
|
|
46
|
+
*
|
|
47
|
+
* Throws an InvalidPathError if the list access cannot be added to the path.
|
|
48
|
+
*/
|
|
49
|
+
list(index: number): PathBuilder;
|
|
50
|
+
/**
|
|
51
|
+
* Access a map field by key.
|
|
52
|
+
*
|
|
53
|
+
* Throws an InvalidPathError if the map access cannot be added to the path.
|
|
54
|
+
*/
|
|
55
|
+
map(key: string | number | bigint | boolean): PathBuilder;
|
|
56
|
+
/**
|
|
57
|
+
* Append a path.
|
|
58
|
+
*
|
|
59
|
+
* Throws an InvalidPathError if the path cannot be added.
|
|
60
|
+
*/
|
|
61
|
+
add(path: Path | PathBuilder): PathBuilder;
|
|
62
|
+
/**
|
|
63
|
+
* Return the path.
|
|
64
|
+
*/
|
|
65
|
+
toPath(): Path;
|
|
66
|
+
/**
|
|
67
|
+
* Create a copy of this builder.
|
|
68
|
+
*/
|
|
69
|
+
clone(): PathBuilder;
|
|
70
|
+
/**
|
|
71
|
+
* Get the current container - a list, map, or message.
|
|
72
|
+
*/
|
|
73
|
+
getLeft(): DescMessage | (DescField & {
|
|
74
|
+
fieldKind: "list";
|
|
75
|
+
}) | (DescField & {
|
|
76
|
+
fieldKind: "map";
|
|
77
|
+
}) | undefined;
|
|
78
|
+
};
|
|
79
|
+
/**
|
|
80
|
+
* Create a PathBuilder.
|
|
81
|
+
*/
|
|
82
|
+
export declare function buildPath(schema: DescMessage): PathBuilder;
|
|
83
|
+
/**
|
|
84
|
+
* Parse a Path from a string.
|
|
85
|
+
*
|
|
86
|
+
* Throws an InvalidPathError if the path is invalid.
|
|
87
|
+
*
|
|
88
|
+
* Note that a Registry must be provided via the options argument to parse
|
|
89
|
+
* paths that refer to an extension.
|
|
90
|
+
*/
|
|
91
|
+
export declare function parsePath(schema: DescMessage, path: string, options?: {
|
|
92
|
+
registry?: Registry;
|
|
93
|
+
}): Path;
|
|
94
|
+
/**
|
|
95
|
+
* Stringify a path.
|
|
96
|
+
*/
|
|
97
|
+
export declare function pathToString(path: Path): string;
|
|
98
|
+
/**
|
|
99
|
+
* InvalidPathError is thrown for invalid Paths, for example during parsing from
|
|
100
|
+
* a string, or when a new Path is built.
|
|
101
|
+
*/
|
|
102
|
+
export declare class InvalidPathError extends Error {
|
|
103
|
+
name: string;
|
|
104
|
+
readonly schema: DescMessage;
|
|
105
|
+
readonly path: Path | string;
|
|
106
|
+
constructor(schema: DescMessage, message: string, path: string | Path);
|
|
107
|
+
}
|
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
// Copyright 2021-2025 Buf Technologies, Inc.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
import { ScalarType, } from "../descriptors.js";
|
|
15
|
+
/**
|
|
16
|
+
* Create a PathBuilder.
|
|
17
|
+
*/
|
|
18
|
+
export function buildPath(schema) {
|
|
19
|
+
return new PathBuilderImpl(schema, schema, []);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Parse a Path from a string.
|
|
23
|
+
*
|
|
24
|
+
* Throws an InvalidPathError if the path is invalid.
|
|
25
|
+
*
|
|
26
|
+
* Note that a Registry must be provided via the options argument to parse
|
|
27
|
+
* paths that refer to an extension.
|
|
28
|
+
*/
|
|
29
|
+
export function parsePath(schema, path, options) {
|
|
30
|
+
var _a, _b;
|
|
31
|
+
const builder = new PathBuilderImpl(schema, schema, []);
|
|
32
|
+
const err = (message, i) => new InvalidPathError(schema, message + " at column " + (i + 1), path);
|
|
33
|
+
for (let i = 0; i < path.length;) {
|
|
34
|
+
const token = nextToken(i, path);
|
|
35
|
+
const left = builder.getLeft();
|
|
36
|
+
let right = undefined;
|
|
37
|
+
if ("field" in token) {
|
|
38
|
+
right =
|
|
39
|
+
(left === null || left === void 0 ? void 0 : left.kind) != "message"
|
|
40
|
+
? undefined
|
|
41
|
+
: ((_a = left.fields.find((field) => field.name === token.field)) !== null && _a !== void 0 ? _a : left.oneofs.find((oneof) => oneof.name === token.field));
|
|
42
|
+
if (!right) {
|
|
43
|
+
throw err(`Unknown field "${token.field}"`, i);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
else if ("ext" in token) {
|
|
47
|
+
right = (_b = options === null || options === void 0 ? void 0 : options.registry) === null || _b === void 0 ? void 0 : _b.getExtension(token.ext);
|
|
48
|
+
if (!right) {
|
|
49
|
+
throw err(`Unknown extension "${token.ext}"`, i);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
else if ("val" in token) {
|
|
53
|
+
// list or map
|
|
54
|
+
right =
|
|
55
|
+
(left === null || left === void 0 ? void 0 : left.kind) == "field" &&
|
|
56
|
+
left.fieldKind == "list" &&
|
|
57
|
+
typeof token.val == "bigint"
|
|
58
|
+
? { kind: "list_sub", index: Number(token.val) }
|
|
59
|
+
: { kind: "map_sub", key: token.val };
|
|
60
|
+
}
|
|
61
|
+
else if ("err" in token) {
|
|
62
|
+
throw err(token.err, token.i);
|
|
63
|
+
}
|
|
64
|
+
if (right) {
|
|
65
|
+
try {
|
|
66
|
+
builder.add([right]);
|
|
67
|
+
}
|
|
68
|
+
catch (e) {
|
|
69
|
+
throw err(e instanceof InvalidPathError ? e.message : String(e), i);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
i = token.i;
|
|
73
|
+
}
|
|
74
|
+
return builder.toPath();
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Stringify a path.
|
|
78
|
+
*/
|
|
79
|
+
export function pathToString(path) {
|
|
80
|
+
const str = [];
|
|
81
|
+
for (const ele of path) {
|
|
82
|
+
switch (ele.kind) {
|
|
83
|
+
case "field":
|
|
84
|
+
case "oneof":
|
|
85
|
+
if (str.length > 0) {
|
|
86
|
+
str.push(".");
|
|
87
|
+
}
|
|
88
|
+
str.push(ele.name);
|
|
89
|
+
break;
|
|
90
|
+
case "extension":
|
|
91
|
+
str.push("[", ele.typeName, "]");
|
|
92
|
+
break;
|
|
93
|
+
case "list_sub":
|
|
94
|
+
str.push("[", ele.index, "]");
|
|
95
|
+
break;
|
|
96
|
+
case "map_sub":
|
|
97
|
+
if (typeof ele.key == "string") {
|
|
98
|
+
str.push('["', ele.key
|
|
99
|
+
.split("\\")
|
|
100
|
+
.join("\\\\")
|
|
101
|
+
.split('"')
|
|
102
|
+
.join('\\"')
|
|
103
|
+
.split("\r")
|
|
104
|
+
.join("\\r")
|
|
105
|
+
.split("\n")
|
|
106
|
+
.join("\\n"), '"]');
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
str.push("[", ele.key, "]");
|
|
110
|
+
}
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return str.join("");
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* InvalidPathError is thrown for invalid Paths, for example during parsing from
|
|
118
|
+
* a string, or when a new Path is built.
|
|
119
|
+
*/
|
|
120
|
+
export class InvalidPathError extends Error {
|
|
121
|
+
constructor(schema, message, path) {
|
|
122
|
+
super(message);
|
|
123
|
+
this.name = "InvalidPathError";
|
|
124
|
+
this.schema = schema;
|
|
125
|
+
this.path = path;
|
|
126
|
+
// see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html#example
|
|
127
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
class PathBuilderImpl {
|
|
131
|
+
constructor(schema, left, path) {
|
|
132
|
+
this.schema = schema;
|
|
133
|
+
this.left = left;
|
|
134
|
+
this.path = path;
|
|
135
|
+
}
|
|
136
|
+
getLeft() {
|
|
137
|
+
return this.left;
|
|
138
|
+
}
|
|
139
|
+
field(field) {
|
|
140
|
+
return this.push(field);
|
|
141
|
+
}
|
|
142
|
+
oneof(oneof) {
|
|
143
|
+
return this.push(oneof);
|
|
144
|
+
}
|
|
145
|
+
extension(extension) {
|
|
146
|
+
return this.push(extension);
|
|
147
|
+
}
|
|
148
|
+
list(index) {
|
|
149
|
+
return this.push({ kind: "list_sub", index });
|
|
150
|
+
}
|
|
151
|
+
map(key) {
|
|
152
|
+
return this.push({ kind: "map_sub", key });
|
|
153
|
+
}
|
|
154
|
+
add(pathOrBuilder) {
|
|
155
|
+
const path = Array.isArray(pathOrBuilder)
|
|
156
|
+
? pathOrBuilder
|
|
157
|
+
: pathOrBuilder.toPath();
|
|
158
|
+
const l = this.path.length;
|
|
159
|
+
try {
|
|
160
|
+
for (const ele of path) {
|
|
161
|
+
this.push(ele);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
catch (e) {
|
|
165
|
+
// undo pushes
|
|
166
|
+
this.path.splice(l);
|
|
167
|
+
throw e;
|
|
168
|
+
}
|
|
169
|
+
return this;
|
|
170
|
+
}
|
|
171
|
+
toPath() {
|
|
172
|
+
return this.path.concat();
|
|
173
|
+
}
|
|
174
|
+
clone() {
|
|
175
|
+
return new PathBuilderImpl(this.schema, this.left, this.path.concat());
|
|
176
|
+
}
|
|
177
|
+
push(ele) {
|
|
178
|
+
switch (ele.kind) {
|
|
179
|
+
case "field":
|
|
180
|
+
if (!this.left ||
|
|
181
|
+
this.left.kind != "message" ||
|
|
182
|
+
this.left.typeName != ele.parent.typeName) {
|
|
183
|
+
throw this.err("field access");
|
|
184
|
+
}
|
|
185
|
+
this.path.push(ele);
|
|
186
|
+
this.left =
|
|
187
|
+
ele.fieldKind == "message"
|
|
188
|
+
? ele.message
|
|
189
|
+
: ele.fieldKind == "list" || ele.fieldKind == "map"
|
|
190
|
+
? ele
|
|
191
|
+
: undefined;
|
|
192
|
+
return this;
|
|
193
|
+
case "oneof":
|
|
194
|
+
if (!this.left ||
|
|
195
|
+
this.left.kind != "message" ||
|
|
196
|
+
this.left.typeName != ele.parent.typeName) {
|
|
197
|
+
throw this.err("oneof access");
|
|
198
|
+
}
|
|
199
|
+
this.path.push(ele);
|
|
200
|
+
this.left = undefined;
|
|
201
|
+
return this;
|
|
202
|
+
case "extension":
|
|
203
|
+
if (!this.left ||
|
|
204
|
+
this.left.kind != "message" ||
|
|
205
|
+
this.left.typeName != ele.extendee.typeName) {
|
|
206
|
+
throw this.err("extension access");
|
|
207
|
+
}
|
|
208
|
+
this.path.push(ele);
|
|
209
|
+
this.left = ele.fieldKind == "message" ? ele.message : undefined;
|
|
210
|
+
return this;
|
|
211
|
+
case "list_sub":
|
|
212
|
+
if (!this.left ||
|
|
213
|
+
this.left.kind != "field" ||
|
|
214
|
+
this.left.fieldKind != "list") {
|
|
215
|
+
throw this.err("list access");
|
|
216
|
+
}
|
|
217
|
+
if (ele.index < 0 || !Number.isInteger(ele.index)) {
|
|
218
|
+
throw this.err("list index");
|
|
219
|
+
}
|
|
220
|
+
this.path.push(ele);
|
|
221
|
+
this.left =
|
|
222
|
+
this.left.listKind == "message" ? this.left.message : undefined;
|
|
223
|
+
return this;
|
|
224
|
+
case "map_sub":
|
|
225
|
+
if (!this.left ||
|
|
226
|
+
this.left.kind != "field" ||
|
|
227
|
+
this.left.fieldKind != "map") {
|
|
228
|
+
throw this.err("map access");
|
|
229
|
+
}
|
|
230
|
+
if (!checkKeyType(ele.key, this.left.mapKey)) {
|
|
231
|
+
throw this.err("map key");
|
|
232
|
+
}
|
|
233
|
+
this.path.push(ele);
|
|
234
|
+
this.left =
|
|
235
|
+
this.left.mapKind == "message" ? this.left.message : undefined;
|
|
236
|
+
return this;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
err(what) {
|
|
240
|
+
return new InvalidPathError(this.schema, "Invalid " + what, this.path);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
function checkKeyType(key, type) {
|
|
244
|
+
switch (type) {
|
|
245
|
+
case ScalarType.STRING:
|
|
246
|
+
return typeof key == "string";
|
|
247
|
+
case ScalarType.INT32:
|
|
248
|
+
case ScalarType.UINT32:
|
|
249
|
+
case ScalarType.SINT32:
|
|
250
|
+
case ScalarType.SFIXED32:
|
|
251
|
+
case ScalarType.FIXED32:
|
|
252
|
+
return typeof key == "number";
|
|
253
|
+
case ScalarType.UINT64:
|
|
254
|
+
case ScalarType.INT64:
|
|
255
|
+
case ScalarType.FIXED64:
|
|
256
|
+
case ScalarType.SFIXED64:
|
|
257
|
+
case ScalarType.SINT64:
|
|
258
|
+
return typeof key == "bigint";
|
|
259
|
+
case ScalarType.BOOL:
|
|
260
|
+
return typeof key == "boolean";
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
function nextToken(i, path) {
|
|
264
|
+
const re_extension = /^[A-Za-z_][A-Za-z_0-9]*(?:\.[A-Za-z_][A-Za-z_0-9]*)*$/;
|
|
265
|
+
const re_field = /^[A-Za-z_][A-Za-z_0-9]*$/;
|
|
266
|
+
if (path[i] == "[") {
|
|
267
|
+
i++;
|
|
268
|
+
while (path[i] == " ") {
|
|
269
|
+
// skip leading whitespace
|
|
270
|
+
i++;
|
|
271
|
+
}
|
|
272
|
+
if (i >= path.length) {
|
|
273
|
+
return { err: "Premature end", i: path.length - 1 };
|
|
274
|
+
}
|
|
275
|
+
let token;
|
|
276
|
+
if (path[i] == `"`) {
|
|
277
|
+
// string literal
|
|
278
|
+
i++;
|
|
279
|
+
let val = "";
|
|
280
|
+
for (;;) {
|
|
281
|
+
if (path[i] == `"`) {
|
|
282
|
+
// end of string literal
|
|
283
|
+
i++;
|
|
284
|
+
break;
|
|
285
|
+
}
|
|
286
|
+
if (path[i] == "\\") {
|
|
287
|
+
switch (path[i + 1]) {
|
|
288
|
+
case `"`:
|
|
289
|
+
case "\\":
|
|
290
|
+
val += path[i + 1];
|
|
291
|
+
break;
|
|
292
|
+
case "r":
|
|
293
|
+
val += "\r";
|
|
294
|
+
break;
|
|
295
|
+
case "n":
|
|
296
|
+
val += "\n";
|
|
297
|
+
break;
|
|
298
|
+
default:
|
|
299
|
+
return { err: "Invalid escape sequence", i };
|
|
300
|
+
}
|
|
301
|
+
i++;
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
val += path[i];
|
|
305
|
+
}
|
|
306
|
+
if (i >= path.length) {
|
|
307
|
+
return { err: "Premature end of string", i: path.length - 1 };
|
|
308
|
+
}
|
|
309
|
+
i++;
|
|
310
|
+
}
|
|
311
|
+
token = { val };
|
|
312
|
+
}
|
|
313
|
+
else if (path[i].match(/\d/)) {
|
|
314
|
+
// integer literal
|
|
315
|
+
const start = i;
|
|
316
|
+
while (i < path.length && /\d/.test(path[i])) {
|
|
317
|
+
i++;
|
|
318
|
+
}
|
|
319
|
+
token = { val: BigInt(path.substring(start, i)) };
|
|
320
|
+
}
|
|
321
|
+
else if (path[i] == "]") {
|
|
322
|
+
return { err: "Premature ]", i };
|
|
323
|
+
}
|
|
324
|
+
else {
|
|
325
|
+
// extension identifier or bool literal
|
|
326
|
+
const start = i;
|
|
327
|
+
while (i < path.length && path[i] != " " && path[i] != "]") {
|
|
328
|
+
i++;
|
|
329
|
+
}
|
|
330
|
+
const name = path.substring(start, i);
|
|
331
|
+
if (name === "true") {
|
|
332
|
+
token = { val: true };
|
|
333
|
+
}
|
|
334
|
+
else if (name === "false") {
|
|
335
|
+
token = { val: false };
|
|
336
|
+
}
|
|
337
|
+
else if (re_extension.test(name)) {
|
|
338
|
+
token = { ext: name };
|
|
339
|
+
}
|
|
340
|
+
else {
|
|
341
|
+
return { err: "Invalid ident", i: start };
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
while (path[i] == " ") {
|
|
345
|
+
// skip trailing whitespace
|
|
346
|
+
i++;
|
|
347
|
+
}
|
|
348
|
+
if (path[i] != "]") {
|
|
349
|
+
return { err: "Missing ]", i };
|
|
350
|
+
}
|
|
351
|
+
i++;
|
|
352
|
+
return Object.assign(Object.assign({}, token), { i });
|
|
353
|
+
}
|
|
354
|
+
// field identifier
|
|
355
|
+
if (i > 0) {
|
|
356
|
+
if (path[i] != ".") {
|
|
357
|
+
return { err: `Expected "."`, i };
|
|
358
|
+
}
|
|
359
|
+
i++;
|
|
360
|
+
}
|
|
361
|
+
const start = i;
|
|
362
|
+
while (i < path.length && path[i] != "." && path[i] != "[") {
|
|
363
|
+
i++;
|
|
364
|
+
}
|
|
365
|
+
const field = path.substring(start, i);
|
|
366
|
+
return re_field.test(field)
|
|
367
|
+
? { field, i }
|
|
368
|
+
: { err: "Invalid ident", i: start };
|
|
369
|
+
}
|
|
@@ -136,7 +136,7 @@ function checkScalarValue(value, scalar) {
|
|
|
136
136
|
protoInt64.parse(value);
|
|
137
137
|
return true;
|
|
138
138
|
}
|
|
139
|
-
catch (
|
|
139
|
+
catch (_) {
|
|
140
140
|
return `${value} out of range`;
|
|
141
141
|
}
|
|
142
142
|
}
|
|
@@ -151,7 +151,7 @@ function checkScalarValue(value, scalar) {
|
|
|
151
151
|
protoInt64.uParse(value);
|
|
152
152
|
return true;
|
|
153
153
|
}
|
|
154
|
-
catch (
|
|
154
|
+
catch (_) {
|
|
155
155
|
return `${value} out of range`;
|
|
156
156
|
}
|
|
157
157
|
}
|
|
@@ -164,7 +164,7 @@ function reasonSingular(field, val, details) {
|
|
|
164
164
|
if (field.scalar !== undefined) {
|
|
165
165
|
return `expected ${scalarTypeDescription(field.scalar)}` + details;
|
|
166
166
|
}
|
|
167
|
-
|
|
167
|
+
if (field.enum !== undefined) {
|
|
168
168
|
return `expected ${field.enum.toString()}` + details;
|
|
169
169
|
}
|
|
170
170
|
return `expected ${formatReflectMessage(field.message)}` + details;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { DescField, DescMessage, DescOneof } from "../descriptors.js";
|
|
2
2
|
import { unsafeLocal } from "./unsafe.js";
|
|
3
3
|
import type { Message, UnknownField } from "../types.js";
|
|
4
4
|
import type { ScalarValue } from "./scalar.js";
|
|
@@ -11,5 +11,4 @@
|
|
|
11
11
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
12
|
// See the License for the specific language governing permissions and
|
|
13
13
|
// limitations under the License.
|
|
14
|
-
import {} from "../descriptors.js";
|
|
15
14
|
import { unsafeLocal } from "./unsafe.js";
|
|
@@ -37,7 +37,9 @@ check = true) {
|
|
|
37
37
|
class ReflectMessageImpl {
|
|
38
38
|
get sortedFields() {
|
|
39
39
|
var _a;
|
|
40
|
-
return ((_a = this._sortedFields) !== null && _a !== void 0 ? _a :
|
|
40
|
+
return ((_a = this._sortedFields) !== null && _a !== void 0 ? _a :
|
|
41
|
+
// biome-ignore lint/suspicious/noAssignInExpressions: no
|
|
42
|
+
(this._sortedFields = this.desc.fields
|
|
41
43
|
.concat()
|
|
42
44
|
.sort((a, b) => a.number - b.number)));
|
|
43
45
|
}
|
|
@@ -77,14 +79,17 @@ class ReflectMessageImpl {
|
|
|
77
79
|
// eslint-disable-next-line no-case-declarations
|
|
78
80
|
let list = this.lists.get(field);
|
|
79
81
|
if (!list || list[unsafeLocal] !== value) {
|
|
80
|
-
this.lists.set(field,
|
|
82
|
+
this.lists.set(field,
|
|
83
|
+
// biome-ignore lint/suspicious/noAssignInExpressions: no
|
|
84
|
+
(list = new ReflectListImpl(field, value, this.check)));
|
|
81
85
|
}
|
|
82
86
|
return list;
|
|
83
87
|
case "map":
|
|
84
|
-
// eslint-disable-next-line no-case-declarations
|
|
85
88
|
let map = this.maps.get(field);
|
|
86
89
|
if (!map || map[unsafeLocal] !== value) {
|
|
87
|
-
this.maps.set(field,
|
|
90
|
+
this.maps.set(field,
|
|
91
|
+
// biome-ignore lint/suspicious/noAssignInExpressions: no
|
|
92
|
+
(map = new ReflectMapImpl(field, value, this.check)));
|
|
88
93
|
}
|
|
89
94
|
return map;
|
|
90
95
|
case "message":
|
|
@@ -407,7 +412,6 @@ function mapKeyToReflect(key, type) {
|
|
|
407
412
|
return key;
|
|
408
413
|
}
|
|
409
414
|
function longToReflect(field, value) {
|
|
410
|
-
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
|
|
411
415
|
switch (field.scalar) {
|
|
412
416
|
case ScalarType.INT64:
|
|
413
417
|
case ScalarType.SFIXED64:
|
|
@@ -430,7 +434,6 @@ function longToReflect(field, value) {
|
|
|
430
434
|
return value;
|
|
431
435
|
}
|
|
432
436
|
function longToLocal(field, value) {
|
|
433
|
-
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
|
|
434
437
|
switch (field.scalar) {
|
|
435
438
|
case ScalarType.INT64:
|
|
436
439
|
case ScalarType.SFIXED64:
|
|
@@ -491,7 +494,6 @@ function wktValueToReflect(json) {
|
|
|
491
494
|
$typeName: "google.protobuf.Value",
|
|
492
495
|
kind: { case: undefined },
|
|
493
496
|
};
|
|
494
|
-
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check -- invalid input is unselected kind
|
|
495
497
|
switch (typeof json) {
|
|
496
498
|
case "number":
|
|
497
499
|
value.kind = { case: "numberValue", value: json };
|
|
@@ -37,7 +37,6 @@ export function scalarEquals(type, a, b) {
|
|
|
37
37
|
return true;
|
|
38
38
|
}
|
|
39
39
|
// Special case 64-bit integers - we support number, string and bigint representation.
|
|
40
|
-
// eslint-disable-next-line @typescript-eslint/switch-exhaustiveness-check
|
|
41
40
|
switch (type) {
|
|
42
41
|
case ScalarType.UINT64:
|
|
43
42
|
case ScalarType.FIXED64:
|
|
@@ -60,10 +59,6 @@ export function scalarZeroValue(type, longAsString) {
|
|
|
60
59
|
return "";
|
|
61
60
|
case ScalarType.BOOL:
|
|
62
61
|
return false;
|
|
63
|
-
default:
|
|
64
|
-
// Handles INT32, UINT32, SINT32, FIXED32, SFIXED32.
|
|
65
|
-
// We do not use individual cases to save a few bytes code size.
|
|
66
|
-
return 0;
|
|
67
62
|
case ScalarType.DOUBLE:
|
|
68
63
|
case ScalarType.FLOAT:
|
|
69
64
|
return 0.0;
|
|
@@ -75,6 +70,10 @@ export function scalarZeroValue(type, longAsString) {
|
|
|
75
70
|
return (longAsString ? "0" : protoInt64.zero);
|
|
76
71
|
case ScalarType.BYTES:
|
|
77
72
|
return new Uint8Array(0);
|
|
73
|
+
default:
|
|
74
|
+
// Handles INT32, UINT32, SINT32, FIXED32, SFIXED32.
|
|
75
|
+
// We do not use individual cases to save a few bytes code size.
|
|
76
|
+
return 0;
|
|
78
77
|
}
|
|
79
78
|
}
|
|
80
79
|
/**
|
|
@@ -5,15 +5,13 @@ export declare const unsafeLocal: unique symbol;
|
|
|
5
5
|
*
|
|
6
6
|
* @private
|
|
7
7
|
*/
|
|
8
|
-
export declare function unsafeOneofCase(target: Record<string, any>,
|
|
9
|
-
oneof: DescOneof): DescField | undefined;
|
|
8
|
+
export declare function unsafeOneofCase(target: Record<string, any>, oneof: DescOneof): DescField | undefined;
|
|
10
9
|
/**
|
|
11
10
|
* Returns true if the field is set.
|
|
12
11
|
*
|
|
13
12
|
* @private
|
|
14
13
|
*/
|
|
15
|
-
export declare function unsafeIsSet(target: Record<string, any>,
|
|
16
|
-
field: DescField): boolean;
|
|
14
|
+
export declare function unsafeIsSet(target: Record<string, any>, field: DescField): boolean;
|
|
17
15
|
/**
|
|
18
16
|
* Returns true if the field is set, but only for singular fields with explicit
|
|
19
17
|
* presence (proto2).
|
|
@@ -38,5 +36,4 @@ export declare function unsafeSet(target: Record<string, unknown>, field: DescFi
|
|
|
38
36
|
*
|
|
39
37
|
* @private
|
|
40
38
|
*/
|
|
41
|
-
export declare function unsafeClear(target: Record<string, any>,
|
|
42
|
-
field: DescField): void;
|
|
39
|
+
export declare function unsafeClear(target: Record<string, any>, field: DescField): void;
|