| 1 |
"use strict"; |
| 2 |
// Copyright 2021-2026 Buf Technologies, Inc. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 16 |
exports.InvalidPathError = void 0; |
| 17 |
exports.buildPath = buildPath; |
| 18 |
exports.parsePath = parsePath; |
| 19 |
exports.pathToString = pathToString; |
| 20 |
const descriptors_js_1 = require("../descriptors.js"); |
| 21 |
/** |
| 22 |
* Create a PathBuilder. |
| 23 |
*/ |
| 24 |
function buildPath(schema) { |
| 25 |
return new PathBuilderImpl(schema, schema, []); |
| 26 |
} |
| 27 |
/** |
| 28 |
* Parse a Path from a string. |
| 29 |
* |
| 30 |
* Throws an InvalidPathError if the path is invalid. |
| 31 |
* |
| 32 |
* Note that a Registry must be provided via the options argument to parse |
| 33 |
* paths that refer to an extension. |
| 34 |
*/ |
| 35 |
function parsePath(schema, path, options) { |
| 36 |
var _a, _b; |
| 37 |
const builder = new PathBuilderImpl(schema, schema, []); |
| 38 |
const err = (message, i) => new InvalidPathError(schema, message + " at column " + (i + 1), path); |
| 39 |
for (let i = 0; i < path.length;) { |
| 40 |
const token = nextToken(i, path); |
| 41 |
const left = builder.getLeft(); |
| 42 |
let right = undefined; |
| 43 |
if ("field" in token) { |
| 44 |
right = |
| 45 |
(left === null || left === void 0 ? void 0 : left.kind) != "message" |
| 46 |
? undefined |
| 47 |
: ((_a = left.fields.find((field) => field.name === token.field)) !== null && _a !== void 0 ? _a : left.oneofs.find((oneof) => oneof.name === token.field)); |
| 48 |
if (!right) { |
| 49 |
throw err(`Unknown field "${token.field}"`, i); |
| 50 |
} |
| 51 |
} |
| 52 |
else if ("ext" in token) { |
| 53 |
right = (_b = options === null || options === void 0 ? void 0 : options.registry) === null || _b === void 0 ? void 0 : _b.getExtension(token.ext); |
| 54 |
if (!right) { |
| 55 |
throw err(`Unknown extension "${token.ext}"`, i); |
| 56 |
} |
| 57 |
} |
| 58 |
else if ("val" in token) { |
| 59 |
// list or map |
| 60 |
right = |
| 61 |
(left === null || left === void 0 ? void 0 : left.kind) == "field" && |
| 62 |
left.fieldKind == "list" && |
| 63 |
typeof token.val == "bigint" |
| 64 |
? { kind: "list_sub", index: Number(token.val) } |
| 65 |
: { kind: "map_sub", key: token.val }; |
| 66 |
} |
| 67 |
else if ("err" in token) { |
| 68 |
throw err(token.err, token.i); |
| 69 |
} |
| 70 |
if (right) { |
| 71 |
try { |
| 72 |
builder.add([right]); |
| 73 |
} |
| 74 |
catch (e) { |
| 75 |
throw err(e instanceof InvalidPathError ? e.message : String(e), i); |
| 76 |
} |
| 77 |
} |
| 78 |
i = token.i; |
| 79 |
} |
| 80 |
return builder.toPath(); |
| 81 |
} |
| 82 |
/** |
| 83 |
* Stringify a path. |
| 84 |
*/ |
| 85 |
function pathToString(path) { |
| 86 |
const str = []; |
| 87 |
for (const ele of path) { |
| 88 |
switch (ele.kind) { |
| 89 |
case "field": |
| 90 |
case "oneof": |
| 91 |
if (str.length > 0) { |
| 92 |
str.push("."); |
| 93 |
} |
| 94 |
str.push(ele.name); |
| 95 |
break; |
| 96 |
case "extension": |
| 97 |
str.push("[", ele.typeName, "]"); |
| 98 |
break; |
| 99 |
case "list_sub": |
| 100 |
str.push("[", ele.index, "]"); |
| 101 |
break; |
| 102 |
case "map_sub": |
| 103 |
if (typeof ele.key == "string") { |
| 104 |
str.push('["', ele.key |
| 105 |
.split("\\") |
| 106 |
.join("\\\\") |
| 107 |
.split('"') |
| 108 |
.join('\\"') |
| 109 |
.split("\r") |
| 110 |
.join("\\r") |
| 111 |
.split("\n") |
| 112 |
.join("\\n"), '"]'); |
| 113 |
} |
| 114 |
else { |
| 115 |
str.push("[", ele.key, "]"); |
| 116 |
} |
| 117 |
break; |
| 118 |
} |
| 119 |
} |
| 120 |
return str.join(""); |
| 121 |
} |
| 122 |
/** |
| 123 |
* InvalidPathError is thrown for invalid Paths, for example during parsing from |
| 124 |
* a string, or when a new Path is built. |
| 125 |
*/ |
| 126 |
class InvalidPathError extends Error { |
| 127 |
constructor(schema, message, path) { |
| 128 |
super(message); |
| 129 |
this.name = "InvalidPathError"; |
| 130 |
this.schema = schema; |
| 131 |
this.path = path; |
| 132 |
// see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html#example |
| 133 |
Object.setPrototypeOf(this, new.target.prototype); |
| 134 |
} |
| 135 |
} |
| 136 |
exports.InvalidPathError = InvalidPathError; |
| 137 |
class PathBuilderImpl { |
| 138 |
constructor(schema, left, path) { |
| 139 |
this.schema = schema; |
| 140 |
this.left = left; |
| 141 |
this.path = path; |
| 142 |
} |
| 143 |
getLeft() { |
| 144 |
return this.left; |
| 145 |
} |
| 146 |
field(field) { |
| 147 |
return this.push(field); |
| 148 |
} |
| 149 |
oneof(oneof) { |
| 150 |
return this.push(oneof); |
| 151 |
} |
| 152 |
extension(extension) { |
| 153 |
return this.push(extension); |
| 154 |
} |
| 155 |
list(index) { |
| 156 |
return this.push({ kind: "list_sub", index }); |
| 157 |
} |
| 158 |
map(key) { |
| 159 |
return this.push({ kind: "map_sub", key }); |
| 160 |
} |
| 161 |
add(pathOrBuilder) { |
| 162 |
const path = Array.isArray(pathOrBuilder) |
| 163 |
? pathOrBuilder |
| 164 |
: pathOrBuilder.toPath(); |
| 165 |
const l = this.path.length; |
| 166 |
try { |
| 167 |
for (const ele of path) { |
| 168 |
this.push(ele); |
| 169 |
} |
| 170 |
} |
| 171 |
catch (e) { |
| 172 |
// undo pushes |
| 173 |
this.path.splice(l); |
| 174 |
throw e; |
| 175 |
} |
| 176 |
return this; |
| 177 |
} |
| 178 |
toPath() { |
| 179 |
return this.path.concat(); |
| 180 |
} |
| 181 |
clone() { |
| 182 |
return new PathBuilderImpl(this.schema, this.left, this.path.concat()); |
| 183 |
} |
| 184 |
push(ele) { |
| 185 |
switch (ele.kind) { |
| 186 |
case "field": |
| 187 |
if (!this.left || |
| 188 |
this.left.kind != "message" || |
| 189 |
this.left.typeName != ele.parent.typeName) { |
| 190 |
throw this.err("field access"); |
| 191 |
} |
| 192 |
this.path.push(ele); |
| 193 |
this.left = |
| 194 |
ele.fieldKind == "message" |
| 195 |
? ele.message |
| 196 |
: ele.fieldKind == "list" || ele.fieldKind == "map" |
| 197 |
? ele |
| 198 |
: undefined; |
| 199 |
return this; |
| 200 |
case "oneof": |
| 201 |
if (!this.left || |
| 202 |
this.left.kind != "message" || |
| 203 |
this.left.typeName != ele.parent.typeName) { |
| 204 |
throw this.err("oneof access"); |
| 205 |
} |
| 206 |
this.path.push(ele); |
| 207 |
this.left = undefined; |
| 208 |
return this; |
| 209 |
case "extension": |
| 210 |
if (!this.left || |
| 211 |
this.left.kind != "message" || |
| 212 |
this.left.typeName != ele.extendee.typeName) { |
| 213 |
throw this.err("extension access"); |
| 214 |
} |
| 215 |
this.path.push(ele); |
| 216 |
this.left = ele.fieldKind == "message" ? ele.message : undefined; |
| 217 |
return this; |
| 218 |
case "list_sub": |
| 219 |
if (!this.left || |
| 220 |
this.left.kind != "field" || |
| 221 |
this.left.fieldKind != "list") { |
| 222 |
throw this.err("list access"); |
| 223 |
} |
| 224 |
if (ele.index < 0 || !Number.isInteger(ele.index)) { |
| 225 |
throw this.err("list index"); |
| 226 |
} |
| 227 |
this.path.push(ele); |
| 228 |
this.left = |
| 229 |
this.left.listKind == "message" ? this.left.message : undefined; |
| 230 |
return this; |
| 231 |
case "map_sub": |
| 232 |
if (!this.left || |
| 233 |
this.left.kind != "field" || |
| 234 |
this.left.fieldKind != "map") { |
| 235 |
throw this.err("map access"); |
| 236 |
} |
| 237 |
if (!checkKeyType(ele.key, this.left.mapKey)) { |
| 238 |
throw this.err("map key"); |
| 239 |
} |
| 240 |
this.path.push(ele); |
| 241 |
this.left = |
| 242 |
this.left.mapKind == "message" ? this.left.message : undefined; |
| 243 |
return this; |
| 244 |
} |
| 245 |
} |
| 246 |
err(what) { |
| 247 |
return new InvalidPathError(this.schema, "Invalid " + what, this.path); |
| 248 |
} |
| 249 |
} |
| 250 |
function checkKeyType(key, type) { |
| 251 |
switch (type) { |
| 252 |
case descriptors_js_1.ScalarType.STRING: |
| 253 |
return typeof key == "string"; |
| 254 |
case descriptors_js_1.ScalarType.INT32: |
| 255 |
case descriptors_js_1.ScalarType.UINT32: |
| 256 |
case descriptors_js_1.ScalarType.SINT32: |
| 257 |
case descriptors_js_1.ScalarType.SFIXED32: |
| 258 |
case descriptors_js_1.ScalarType.FIXED32: |
| 259 |
return typeof key == "number"; |
| 260 |
case descriptors_js_1.ScalarType.UINT64: |
| 261 |
case descriptors_js_1.ScalarType.INT64: |
| 262 |
case descriptors_js_1.ScalarType.FIXED64: |
| 263 |
case descriptors_js_1.ScalarType.SFIXED64: |
| 264 |
case descriptors_js_1.ScalarType.SINT64: |
| 265 |
return typeof key == "bigint"; |
| 266 |
case descriptors_js_1.ScalarType.BOOL: |
| 267 |
return typeof key == "boolean"; |
| 268 |
} |
| 269 |
} |
| 270 |
function nextToken(i, path) { |
| 271 |
const re_extension = /^[A-Za-z_][A-Za-z_0-9]*(?:\.[A-Za-z_][A-Za-z_0-9]*)*$/; |
| 272 |
const re_field = /^[A-Za-z_][A-Za-z_0-9]*$/; |
| 273 |
if (path[i] == "[") { |
| 274 |
i++; |
| 275 |
while (path[i] == " ") { |
| 276 |
// skip leading whitespace |
| 277 |
i++; |
| 278 |
} |
| 279 |
if (i >= path.length) { |
| 280 |
return { err: "Premature end", i: path.length - 1 }; |
| 281 |
} |
| 282 |
let token; |
| 283 |
if (path[i] == `"`) { |
| 284 |
// string literal |
| 285 |
i++; |
| 286 |
let val = ""; |
| 287 |
for (;;) { |
| 288 |
if (path[i] == `"`) { |
| 289 |
// end of string literal |
| 290 |
i++; |
| 291 |
break; |
| 292 |
} |
| 293 |
if (path[i] == "\\") { |
| 294 |
switch (path[i + 1]) { |
| 295 |
case `"`: |
| 296 |
case "\\": |
| 297 |
val += path[i + 1]; |
| 298 |
break; |
| 299 |
case "r": |
| 300 |
val += "\r"; |
| 301 |
break; |
| 302 |
case "n": |
| 303 |
val += "\n"; |
| 304 |
break; |
| 305 |
default: |
| 306 |
return { err: "Invalid escape sequence", i }; |
| 307 |
} |
| 308 |
i++; |
| 309 |
} |
| 310 |
else { |
| 311 |
val += path[i]; |
| 312 |
} |
| 313 |
if (i >= path.length) { |
| 314 |
return { err: "Premature end of string", i: path.length - 1 }; |
| 315 |
} |
| 316 |
i++; |
| 317 |
} |
| 318 |
token = { val }; |
| 319 |
} |
| 320 |
else if (path[i].match(/\d/)) { |
| 321 |
// integer literal |
| 322 |
const start = i; |
| 323 |
while (i < path.length && /\d/.test(path[i])) { |
| 324 |
i++; |
| 325 |
} |
| 326 |
token = { val: BigInt(path.substring(start, i)) }; |
| 327 |
} |
| 328 |
else if (path[i] == "]") { |
| 329 |
return { err: "Premature ]", i }; |
| 330 |
} |
| 331 |
else { |
| 332 |
// extension identifier or bool literal |
| 333 |
const start = i; |
| 334 |
while (i < path.length && path[i] != " " && path[i] != "]") { |
| 335 |
i++; |
| 336 |
} |
| 337 |
const name = path.substring(start, i); |
| 338 |
if (name === "true") { |
| 339 |
token = { val: true }; |
| 340 |
} |
| 341 |
else if (name === "false") { |
| 342 |
token = { val: false }; |
| 343 |
} |
| 344 |
else if (re_extension.test(name)) { |
| 345 |
token = { ext: name }; |
| 346 |
} |
| 347 |
else { |
| 348 |
return { err: "Invalid ident", i: start }; |
| 349 |
} |
| 350 |
} |
| 351 |
while (path[i] == " ") { |
| 352 |
// skip trailing whitespace |
| 353 |
i++; |
| 354 |
} |
| 355 |
if (path[i] != "]") { |
| 356 |
return { err: "Missing ]", i }; |
| 357 |
} |
| 358 |
i++; |
| 359 |
return Object.assign(Object.assign({}, token), { i }); |
| 360 |
} |
| 361 |
// field identifier |
| 362 |
if (i > 0) { |
| 363 |
if (path[i] != ".") { |
| 364 |
return { err: `Expected "."`, i }; |
| 365 |
} |
| 366 |
i++; |
| 367 |
} |
| 368 |
const start = i; |
| 369 |
while (i < path.length && path[i] != "." && path[i] != "[") { |
| 370 |
i++; |
| 371 |
} |
| 372 |
const field = path.substring(start, i); |
| 373 |
return re_field.test(field) |
| 374 |
? { field, i } |
| 375 |
: { err: "Invalid ident", i: start }; |
| 376 |
} |