| 1 |
// Copyright 2021-2026 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 |
} |