@ac-kit/format-jsonpath 0.0.0-stage → 0.1.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.
Files changed (52) hide show
  1. package/CHANGELOG.md +62 -0
  2. package/LICENSE +28 -0
  3. package/README.md +169 -2
  4. package/dist/_costs.d.ts +26 -0
  5. package/dist/_costs.d.ts.map +1 -0
  6. package/dist/_costs.js +36 -0
  7. package/dist/_costs.js.map +1 -0
  8. package/dist/_evaluate.d.ts +41 -0
  9. package/dist/_evaluate.d.ts.map +1 -0
  10. package/dist/_evaluate.js +512 -0
  11. package/dist/_evaluate.js.map +1 -0
  12. package/dist/_functions.d.ts +12 -0
  13. package/dist/_functions.d.ts.map +1 -0
  14. package/dist/_functions.js +17 -0
  15. package/dist/_functions.js.map +1 -0
  16. package/dist/_json-equal.d.ts +19 -0
  17. package/dist/_json-equal.d.ts.map +1 -0
  18. package/dist/_json-equal.js +91 -0
  19. package/dist/_json-equal.js.map +1 -0
  20. package/dist/_parse.d.ts +17 -0
  21. package/dist/_parse.d.ts.map +1 -0
  22. package/dist/_parse.js +617 -0
  23. package/dist/_parse.js.map +1 -0
  24. package/dist/ast.d.ts +133 -0
  25. package/dist/ast.d.ts.map +1 -0
  26. package/dist/ast.js +2 -0
  27. package/dist/ast.js.map +1 -0
  28. package/dist/compile-jsonpath.d.ts +108 -0
  29. package/dist/compile-jsonpath.d.ts.map +1 -0
  30. package/dist/compile-jsonpath.js +103 -0
  31. package/dist/compile-jsonpath.js.map +1 -0
  32. package/dist/errors.d.ts +64 -0
  33. package/dist/errors.d.ts.map +1 -0
  34. package/dist/errors.js +34 -0
  35. package/dist/errors.js.map +1 -0
  36. package/dist/index.d.ts +7 -0
  37. package/dist/index.d.ts.map +1 -0
  38. package/dist/index.js +7 -0
  39. package/dist/index.js.map +1 -0
  40. package/dist/limits.d.ts +108 -0
  41. package/dist/limits.d.ts.map +1 -0
  42. package/dist/limits.js +86 -0
  43. package/dist/limits.js.map +1 -0
  44. package/dist/normalized-path.d.ts +67 -0
  45. package/dist/normalized-path.d.ts.map +1 -0
  46. package/dist/normalized-path.js +269 -0
  47. package/dist/normalized-path.js.map +1 -0
  48. package/dist/parse-jsonpath.d.ts +60 -0
  49. package/dist/parse-jsonpath.d.ts.map +1 -0
  50. package/dist/parse-jsonpath.js +66 -0
  51. package/dist/parse-jsonpath.js.map +1 -0
  52. package/package.json +65 -3
package/dist/limits.js ADDED
@@ -0,0 +1,86 @@
1
+ import { DEFAULT_REGEX_LIMITS } from "@ac-kit/format-regex";
2
+ /**
3
+ * The highest {@link JsonPathLimits.maxNestingDepth} accepted. Parsing and
4
+ * evaluation never use the native stack for nesting, so this bounds the depth
5
+ * of the syntax tree {@link parseJsonPath} hands out, for a caller walking it
6
+ * recursively.
7
+ */
8
+ export const MAX_JSONPATH_NESTING_DEPTH = 256;
9
+ /**
10
+ * Defaults, chosen so no hand-written query over a realistic document reaches
11
+ * them: 4 Ki characters is two orders of magnitude above any query written by
12
+ * hand, 64 levels of nesting is an order above, and 3 Mi work units list every
13
+ * node of a document of half a million nodes (`$..*`).
14
+ */
15
+ export const DEFAULT_JSONPATH_LIMITS = {
16
+ maxQueryLength: 4096,
17
+ maxNestingDepth: 64,
18
+ maxWork: 3145728,
19
+ regex: {
20
+ maxProgramSize: DEFAULT_REGEX_LIMITS.maxProgramSize,
21
+ maxNestingDepth: DEFAULT_REGEX_LIMITS.maxNestingDepth,
22
+ },
23
+ };
24
+ const MESSAGES_ = {
25
+ queryLength: (limit) => `JSONPath query is longer than ${limit} characters`,
26
+ nestingDepth: (limit) => `JSONPath query nests deeper than ${limit}`,
27
+ work: (limit) => `JSONPath evaluation exceeded ${limit} work units`,
28
+ pathLength: (limit) => `Normalized path would be longer than ${limit} characters`,
29
+ };
30
+ /**
31
+ * Thrown when a query or an evaluation exceeds one of the
32
+ * {@link JsonPathLimits}.
33
+ */
34
+ export class JsonPathLimitExceededError extends Error {
35
+ kind;
36
+ limit;
37
+ /**
38
+ * @param kind The bound that was exceeded.
39
+ * @param limit Its value.
40
+ * @param options `cause` holds the `WorkBudgetExceededError` behind a
41
+ * `"work"` error.
42
+ */
43
+ constructor(kind, limit, options) {
44
+ super(MESSAGES_[kind](limit), options);
45
+ this.kind = kind;
46
+ this.limit = limit;
47
+ this.name = "JsonPathLimitExceededError";
48
+ }
49
+ }
50
+ function isSizeBound(value) {
51
+ return value === Infinity || (Number.isSafeInteger(value) && value > 0);
52
+ }
53
+ /**
54
+ * Fills in the unset bounds from {@link DEFAULT_JSONPATH_LIMITS}, including the
55
+ * nested `@ac-kit/format-regex` ones.
56
+ *
57
+ * @throws {RangeError} When `maxQueryLength` or `maxWork` is not a positive
58
+ * safe integer or `Infinity`, or `maxNestingDepth` is not an integer from 1
59
+ * to {@link MAX_JSONPATH_NESTING_DEPTH}.
60
+ */
61
+ export function jsonPathLimits(limits) {
62
+ const resolved = {
63
+ maxQueryLength: limits?.maxQueryLength ?? DEFAULT_JSONPATH_LIMITS.maxQueryLength,
64
+ maxNestingDepth: limits?.maxNestingDepth ?? DEFAULT_JSONPATH_LIMITS.maxNestingDepth,
65
+ maxWork: limits?.maxWork ?? DEFAULT_JSONPATH_LIMITS.maxWork,
66
+ regex: {
67
+ maxProgramSize: limits?.regex?.maxProgramSize ??
68
+ DEFAULT_JSONPATH_LIMITS.regex.maxProgramSize,
69
+ maxNestingDepth: limits?.regex?.maxNestingDepth ??
70
+ DEFAULT_JSONPATH_LIMITS.regex.maxNestingDepth,
71
+ },
72
+ };
73
+ if (!isSizeBound(resolved.maxQueryLength)) {
74
+ throw new RangeError("maxQueryLength must be a positive safe integer or Infinity");
75
+ }
76
+ if (!isSizeBound(resolved.maxWork)) {
77
+ throw new RangeError("maxWork must be a positive safe integer or Infinity");
78
+ }
79
+ if (!Number.isInteger(resolved.maxNestingDepth) ||
80
+ resolved.maxNestingDepth < 1 ||
81
+ resolved.maxNestingDepth > MAX_JSONPATH_NESTING_DEPTH) {
82
+ throw new RangeError(`maxNestingDepth must be an integer from 1 to ${MAX_JSONPATH_NESTING_DEPTH}`);
83
+ }
84
+ return resolved;
85
+ }
86
+ //# sourceMappingURL=limits.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"limits.js","sourceRoot":"","sources":["../src/limits.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAoB,MAAM,sBAAsB,CAAC;AA6E9E;;;;;GAKG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAG,GAAG,CAAC;AAE9C;;;;;GAKG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAA2B;IAC9D,cAAc,EAAE,IAAI;IACpB,eAAe,EAAE,EAAE;IACnB,OAAO,EAAE,OAAS;IAClB,KAAK,EAAE;QACN,cAAc,EAAE,oBAAoB,CAAC,cAAc;QACnD,eAAe,EAAE,oBAAoB,CAAC,eAAe;KACrD;CACD,CAAC;AASF,MAAM,SAAS,GAEX;IACH,WAAW,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,iCAAiC,KAAK,aAAa;IAC3E,YAAY,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,oCAAoC,KAAK,EAAE;IACpE,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,gCAAgC,KAAK,aAAa;IACnE,UAAU,EAAE,CAAC,KAAK,EAAE,EAAE,CACrB,wCAAwC,KAAK,aAAa;CAC3D,CAAC;AAEF;;;GAGG;AACH,MAAM,OAAO,0BAA2B,SAAQ,KAAK;IAQ1C,IAAI;IACJ,KAAK;IARf;;;;;OAKG;IACH,YACU,IAAuB,EACvB,KAAa,EACtB,OAAsB;QAEtB,KAAK,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,CAAC;oBAJ9B,IAAI;qBACJ,KAAK;QAId,IAAI,CAAC,IAAI,GAAG,4BAA4B,CAAC;IAC1C,CAAC;CACD;AAED,SAAS,WAAW,CAAC,KAAa;IACjC,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,CAAC,CAAC;AACzE,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,cAAc,CAC7B,MAAuB;IAEvB,MAAM,QAAQ,GAA2B;QACxC,cAAc,EACb,MAAM,EAAE,cAAc,IAAI,uBAAuB,CAAC,cAAc;QACjE,eAAe,EACd,MAAM,EAAE,eAAe,IAAI,uBAAuB,CAAC,eAAe;QACnE,OAAO,EAAE,MAAM,EAAE,OAAO,IAAI,uBAAuB,CAAC,OAAO;QAC3D,KAAK,EAAE;YACN,cAAc,EACb,MAAM,EAAE,KAAK,EAAE,cAAc;gBAC7B,uBAAuB,CAAC,KAAK,CAAC,cAAc;YAC7C,eAAe,EACd,MAAM,EAAE,KAAK,EAAE,eAAe;gBAC9B,uBAAuB,CAAC,KAAK,CAAC,eAAe;SAC9C;KACD,CAAC;IACF,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,cAAc,CAAC,EAAE,CAAC;QAC3C,MAAM,IAAI,UAAU,CACnB,4DAA4D,CAC5D,CAAC;IACH,CAAC;IACD,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;QACpC,MAAM,IAAI,UAAU,CAAC,qDAAqD,CAAC,CAAC;IAC7E,CAAC;IACD,IACC,CAAC,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,eAAe,CAAC;QAC3C,QAAQ,CAAC,eAAe,GAAG,CAAC;QAC5B,QAAQ,CAAC,eAAe,GAAG,0BAA0B,EACpD,CAAC;QACF,MAAM,IAAI,UAAU,CACnB,gDAAgD,0BAA0B,EAAE,CAC5E,CAAC;IACH,CAAC;IACD,OAAO,QAAQ,CAAC;AACjB,CAAC"}
@@ -0,0 +1,67 @@
1
+ import { type PropertyPath } from "@ac-kit/core";
2
+ /**
3
+ * A node's location in a document: a number is an array index (a non-negative
4
+ * safe integer), a string is a member name. Core's `PropertyPath`, so it feeds
5
+ * `getAtPath` and `setAtPath` from `@ac-kit/core` directly.
6
+ */
7
+ export type JsonPathNormalizedPath = PropertyPath;
8
+ /** Options of {@link printNormalizedPath}. */
9
+ export type PrintNormalizedPathOptions = {
10
+ /**
11
+ * Longest text to build, in UTF-16 code units; checked before anything is
12
+ * built. A positive integer at most 2²⁸ (256 Mi code units). Default
13
+ * {@link DEFAULT_MAX_NORMALIZED_PATH_LENGTH}.
14
+ */
15
+ readonly maxLength?: number;
16
+ };
17
+ /**
18
+ * Default {@link PrintNormalizedPathOptions.maxLength}: 16 Mi code units, far
19
+ * above any real path and far below the longest string a JavaScript engine
20
+ * builds (V8's is about 512 Mi).
21
+ */
22
+ export declare const DEFAULT_MAX_NORMALIZED_PATH_LENGTH = 16777216;
23
+ /**
24
+ * Spells a location as an RFC 9535 §2.7 normalized path, such as
25
+ * `$['store']['book'][0]`.
26
+ *
27
+ * Member names are single-quoted, with `\b`, `\t`, `\n`, `\f`, `\r`, `\'` and
28
+ * `\\` escaped and every other control character as `\u00xx`. A lone surrogate,
29
+ * which a JSON string may hold but §2.7 cannot spell, is printed as its own
30
+ * lowercase `\udxxx` escape; {@link parseNormalizedPath} reads that back.
31
+ *
32
+ * The printed length is computed first, without building anything, and checked
33
+ * against `options.maxLength`.
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * printNormalizedPath(["a", 0, "it's"]); // "$['a'][0]['it\\'s']"
38
+ * ```;
39
+ *
40
+ * @param path The location; `[]` is the root.
41
+ * @returns The canonical text. Complexity: O(total length of the path), twice.
42
+ * @throws {RangeError} If a number in `path` is not a non-negative safe
43
+ * integer, or `options.maxLength` is not a positive integer at most 2²⁸.
44
+ * Invalid length options are rejected before reading the path.
45
+ * @throws {JsonPathLimitExceededError} Of kind `"pathLength"` if the text would
46
+ * be longer than `options.maxLength`.
47
+ */
48
+ export declare function printNormalizedPath(path: JsonPathNormalizedPath, options?: PrintNormalizedPathOptions): string;
49
+ /**
50
+ * Reads an RFC 9535 §2.7 normalized path back into a location: the inverse of
51
+ * {@link printNormalizedPath}, accepting exactly the text it prints.
52
+ *
53
+ * Only the canonical spelling is accepted: no double quotes, no shorthand, no
54
+ * blank space, no leading zero, no escape other than the canonical one. It is
55
+ * checked while reading, in one pass.
56
+ *
57
+ * @example
58
+ * ```ts
59
+ * parseNormalizedPath("$['a'][0]"); // ["a", 0]
60
+ * ```;
61
+ *
62
+ * @returns The location. Complexity: O(source length).
63
+ * @throws {JsonPathSyntaxError} With code `"not-normalized"` and the position
64
+ * of the first non-canonical character.
65
+ */
66
+ export declare function parseNormalizedPath(source: string): JsonPathNormalizedPath;
67
+ //# sourceMappingURL=normalized-path.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"normalized-path.d.ts","sourceRoot":"","sources":["../src/normalized-path.ts"],"names":[],"mappings":"AAAA,OAAO,EAMN,KAAK,YAAY,EAEjB,MAAM,cAAc,CAAC;AAKtB;;;;GAIG;AACH,MAAM,MAAM,sBAAsB,GAAG,YAAY,CAAC;AAElD,8CAA8C;AAC9C,MAAM,MAAM,0BAA0B,GAAG;IACxC;;;;OAIG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;CAC5B,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,kCAAkC,WAAa,CAAC;AAmF7D;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,mBAAmB,CAClC,IAAI,EAAE,sBAAsB,EAC5B,OAAO,CAAC,EAAE,0BAA0B,GAClC,MAAM,CA6BR;AA+GD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,MAAM,GAAG,sBAAsB,CAuB1E"}
@@ -0,0 +1,269 @@
1
+ import { isAsciiDigit, isAsciiHexDigit, isAsciiUpperAlpha, isHighSurrogate, isLowSurrogate, MAX_PORTABLE_STRING_LENGTH, } from "@ac-kit/core";
2
+ import { JsonPathSyntaxError } from "./errors.js";
3
+ import { JsonPathLimitExceededError } from "./limits.js";
4
+ /**
5
+ * Default {@link PrintNormalizedPathOptions.maxLength}: 16 Mi code units, far
6
+ * above any real path and far below the longest string a JavaScript engine
7
+ * builds (V8's is about 512 Mi).
8
+ */
9
+ export const DEFAULT_MAX_NORMALIZED_PATH_LENGTH = 16777216;
10
+ const MAX_NORMALIZED_PATH_LENGTH_ = MAX_PORTABLE_STRING_LENGTH;
11
+ const SHORT_ESCAPES_ = new Map([
12
+ [0x08, "\\b"],
13
+ [0x09, "\\t"],
14
+ [0x0a, "\\n"],
15
+ [0x0c, "\\f"],
16
+ [0x0d, "\\r"],
17
+ [0x27, "\\'"],
18
+ [0x5c, "\\\\"],
19
+ ]);
20
+ const UNESCAPES_ = new Map([
21
+ ["b", 0x08],
22
+ ["t", 0x09],
23
+ ["n", 0x0a],
24
+ ["f", 0x0c],
25
+ ["r", 0x0d],
26
+ ["'", 0x27],
27
+ ["\\", 0x5c],
28
+ ]);
29
+ const HEX_ESCAPE_LENGTH_ = 6;
30
+ /** Whether the code unit at `index` is half of a well-formed surrogate pair. */
31
+ function isPaired(name, index) {
32
+ const unit = name.charCodeAt(index);
33
+ return isHighSurrogate(unit)
34
+ ? isLowSurrogate(name.charCodeAt(index + 1))
35
+ : isLowSurrogate(unit) && isHighSurrogate(name.charCodeAt(index - 1));
36
+ }
37
+ /** Whether the unit is printed as a `\uXXXX` escape. */
38
+ function needsHexEscape(name, index) {
39
+ const unit = name.charCodeAt(index);
40
+ return (!SHORT_ESCAPES_.has(unit) &&
41
+ (unit < 0x20 ||
42
+ ((isHighSurrogate(unit) || isLowSurrogate(unit)) &&
43
+ !isPaired(name, index))));
44
+ }
45
+ function printedNameLength(name) {
46
+ let length = name.length + 4;
47
+ for (let index = 0; index < name.length; index++) {
48
+ if (SHORT_ESCAPES_.has(name.charCodeAt(index))) {
49
+ length += 1;
50
+ }
51
+ else if (needsHexEscape(name, index)) {
52
+ length += HEX_ESCAPE_LENGTH_ - 1;
53
+ }
54
+ }
55
+ return length;
56
+ }
57
+ function printMemberName(name) {
58
+ let printed = "";
59
+ let runStart = 0;
60
+ for (let index = 0; index < name.length; index++) {
61
+ const unit = name.charCodeAt(index);
62
+ const escape = SHORT_ESCAPES_.get(unit) ??
63
+ (needsHexEscape(name, index)
64
+ ? `\\u${unit.toString(16).padStart(4, "0")}`
65
+ : undefined);
66
+ if (escape !== undefined) {
67
+ printed += name.slice(runStart, index) + escape;
68
+ runStart = index + 1;
69
+ }
70
+ }
71
+ return printed + name.slice(runStart);
72
+ }
73
+ function checkIndex(part) {
74
+ if (!Number.isSafeInteger(part) || part < 0 || Object.is(part, -0)) {
75
+ throw new RangeError(`A normalized path index must be a non-negative safe integer, not ${part}`);
76
+ }
77
+ }
78
+ /**
79
+ * Spells a location as an RFC 9535 §2.7 normalized path, such as
80
+ * `$['store']['book'][0]`.
81
+ *
82
+ * Member names are single-quoted, with `\b`, `\t`, `\n`, `\f`, `\r`, `\'` and
83
+ * `\\` escaped and every other control character as `\u00xx`. A lone surrogate,
84
+ * which a JSON string may hold but §2.7 cannot spell, is printed as its own
85
+ * lowercase `\udxxx` escape; {@link parseNormalizedPath} reads that back.
86
+ *
87
+ * The printed length is computed first, without building anything, and checked
88
+ * against `options.maxLength`.
89
+ *
90
+ * @example
91
+ * ```ts
92
+ * printNormalizedPath(["a", 0, "it's"]); // "$['a'][0]['it\\'s']"
93
+ * ```;
94
+ *
95
+ * @param path The location; `[]` is the root.
96
+ * @returns The canonical text. Complexity: O(total length of the path), twice.
97
+ * @throws {RangeError} If a number in `path` is not a non-negative safe
98
+ * integer, or `options.maxLength` is not a positive integer at most 2²⁸.
99
+ * Invalid length options are rejected before reading the path.
100
+ * @throws {JsonPathLimitExceededError} Of kind `"pathLength"` if the text would
101
+ * be longer than `options.maxLength`.
102
+ */
103
+ export function printNormalizedPath(path, options) {
104
+ const maxLength = options?.maxLength ?? DEFAULT_MAX_NORMALIZED_PATH_LENGTH;
105
+ if (!Number.isSafeInteger(maxLength) ||
106
+ maxLength < 1 ||
107
+ maxLength > MAX_NORMALIZED_PATH_LENGTH_) {
108
+ throw new RangeError(`maxLength must be a positive integer at most ${MAX_NORMALIZED_PATH_LENGTH_}`);
109
+ }
110
+ let length = 1;
111
+ for (const part of path) {
112
+ if (typeof part === "number") {
113
+ checkIndex(part);
114
+ length += String(part).length + 2;
115
+ }
116
+ else {
117
+ length += printedNameLength(part);
118
+ }
119
+ if (length > maxLength) {
120
+ throw new JsonPathLimitExceededError("pathLength", maxLength);
121
+ }
122
+ }
123
+ let printed = "$";
124
+ for (const part of path) {
125
+ printed +=
126
+ typeof part === "number" ? `[${part}]` : `['${printMemberName(part)}']`;
127
+ }
128
+ return printed;
129
+ }
130
+ function notNormalized(source, position) {
131
+ throw new JsonPathSyntaxError("not-normalized", source, position, "not an RFC 9535 normalized path");
132
+ }
133
+ function readLowercaseHexQuad(cursor) {
134
+ const { source, index } = cursor;
135
+ for (let offset = 0; offset < 4; offset++) {
136
+ const unit = source.charCodeAt(index + offset);
137
+ if (!isAsciiHexDigit(unit) || isAsciiUpperAlpha(unit)) {
138
+ return undefined;
139
+ }
140
+ }
141
+ return Number.parseInt(source.slice(index, index + 4), 16);
142
+ }
143
+ /** Whether `\u` followed by `unit` is the canonical spelling of `unit`. */
144
+ function isCanonicalHexEscape(unit, cursor) {
145
+ if (isHighSurrogate(unit)) {
146
+ // Canonical only when no low surrogate follows, raw or escaped.
147
+ const { source, index } = cursor;
148
+ if (source.startsWith("\\u", index + 4)) {
149
+ const next = readLowercaseHexQuad({ source, index: index + 6 });
150
+ return next === undefined || !isLowSurrogate(next);
151
+ }
152
+ return !isLowSurrogate(source.charCodeAt(index + 4));
153
+ }
154
+ if (isLowSurrogate(unit)) {
155
+ // Preceded by an escaped high surrogate is already refused there.
156
+ return true;
157
+ }
158
+ return unit < 0x20 && !SHORT_ESCAPES_.has(unit);
159
+ }
160
+ /** The member name after the opening quote, up to and past the closing one. */
161
+ function readMemberName(cursor) {
162
+ const { source } = cursor;
163
+ let name = "";
164
+ let runStart = cursor.index;
165
+ while (cursor.index < source.length) {
166
+ const unit = source.charCodeAt(cursor.index);
167
+ if (unit === 0x27) {
168
+ name += source.slice(runStart, cursor.index);
169
+ cursor.index++;
170
+ return name;
171
+ }
172
+ if (unit === 0x5c) {
173
+ name += source.slice(runStart, cursor.index);
174
+ const escapeStart = cursor.index;
175
+ const next = source[cursor.index + 1];
176
+ const unescaped = next === undefined ? undefined : UNESCAPES_.get(next);
177
+ if (unescaped !== undefined) {
178
+ name += String.fromCharCode(unescaped);
179
+ cursor.index += 2;
180
+ }
181
+ else if (next === "u") {
182
+ cursor.index += 2;
183
+ const value = readLowercaseHexQuad(cursor);
184
+ if (value === undefined || !isCanonicalHexEscape(value, cursor)) {
185
+ notNormalized(source, escapeStart);
186
+ }
187
+ name += String.fromCharCode(value);
188
+ cursor.index += 4;
189
+ }
190
+ else {
191
+ notNormalized(source, escapeStart);
192
+ }
193
+ runStart = cursor.index;
194
+ continue;
195
+ }
196
+ if (unit < 0x20) {
197
+ notNormalized(source, cursor.index);
198
+ }
199
+ if (isHighSurrogate(unit) || isLowSurrogate(unit)) {
200
+ if (!isHighSurrogate(unit) ||
201
+ !isLowSurrogate(source.charCodeAt(cursor.index + 1))) {
202
+ notNormalized(source, cursor.index);
203
+ }
204
+ cursor.index += 2;
205
+ continue;
206
+ }
207
+ cursor.index++;
208
+ }
209
+ return notNormalized(source, cursor.index);
210
+ }
211
+ function readIndex(cursor) {
212
+ const { source } = cursor;
213
+ const start = cursor.index;
214
+ while (isAsciiDigit(source.charCodeAt(cursor.index))) {
215
+ cursor.index++;
216
+ }
217
+ const digits = cursor.index - start;
218
+ if (digits === 0 || (digits > 1 && source[start] === "0")) {
219
+ notNormalized(source, start);
220
+ }
221
+ const index = Number(source.slice(start, cursor.index));
222
+ if (!Number.isSafeInteger(index)) {
223
+ notNormalized(source, start);
224
+ }
225
+ return index;
226
+ }
227
+ /**
228
+ * Reads an RFC 9535 §2.7 normalized path back into a location: the inverse of
229
+ * {@link printNormalizedPath}, accepting exactly the text it prints.
230
+ *
231
+ * Only the canonical spelling is accepted: no double quotes, no shorthand, no
232
+ * blank space, no leading zero, no escape other than the canonical one. It is
233
+ * checked while reading, in one pass.
234
+ *
235
+ * @example
236
+ * ```ts
237
+ * parseNormalizedPath("$['a'][0]"); // ["a", 0]
238
+ * ```;
239
+ *
240
+ * @returns The location. Complexity: O(source length).
241
+ * @throws {JsonPathSyntaxError} With code `"not-normalized"` and the position
242
+ * of the first non-canonical character.
243
+ */
244
+ export function parseNormalizedPath(source) {
245
+ if (source[0] !== "$") {
246
+ notNormalized(source, 0);
247
+ }
248
+ const cursor = { source, index: 1 };
249
+ const path = [];
250
+ while (cursor.index < source.length) {
251
+ if (source[cursor.index] !== "[") {
252
+ notNormalized(source, cursor.index);
253
+ }
254
+ cursor.index++;
255
+ if (source[cursor.index] === "'") {
256
+ cursor.index++;
257
+ path.push(readMemberName(cursor));
258
+ }
259
+ else {
260
+ path.push(readIndex(cursor));
261
+ }
262
+ if (source[cursor.index] !== "]") {
263
+ notNormalized(source, cursor.index);
264
+ }
265
+ cursor.index++;
266
+ }
267
+ return path;
268
+ }
269
+ //# sourceMappingURL=normalized-path.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"normalized-path.js","sourceRoot":"","sources":["../src/normalized-path.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,YAAY,EACZ,eAAe,EACf,iBAAiB,EACjB,eAAe,EACf,cAAc,EAEd,0BAA0B,GAC1B,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,EAAE,0BAA0B,EAAE,MAAM,aAAa,CAAC;AAmBzD;;;;GAIG;AACH,MAAM,CAAC,MAAM,kCAAkC,GAAG,QAAU,CAAC;AAE7D,MAAM,2BAA2B,GAAG,0BAA0B,CAAC;AAE/D,MAAM,cAAc,GAAgC,IAAI,GAAG,CAAC;IAC3D,CAAC,IA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AK,EAAE,CAAC,EAAE,CAAC;IAC7C,MAAM,IAAI,GAAwB,EAAE,CAAC;IACrC,OAAO,MAAM,CAAC,KAAK,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC;QACrC,IAAI,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,GAAG,EAAE,CAAC;YAClC,aAAa,CAAC,MAAM,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC;QACrC,CAAC;QACD,MAAM,CAAC,KAAK,EAAE,CAAC;QACf,IAAI,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,GAAG,EAAE,CAAC;YAClC,MAAM,CAAC,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,IAAI,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC,CAAC;QACnC,CAAC;aAAM,CAAC;YACP,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC;QAC9B,CAAC;QACD,IAAI,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,GAAG,EAAE,CAAC;YAClC,aAAa,CAAC,MAAM,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC;QACrC,CAAC;QACD,MAAM,CAAC,KAAK,EAAE,CAAC;IAChB,CAAC;IACD,OAAO,IAAI,CAAC;AACb,CAAC"}
@@ -0,0 +1,60 @@
1
+ import type { WorkBudget } from "@ac-kit/core";
2
+ import type { JsonPathQuery } from "./ast.js";
3
+ import { type JsonPathLimits } from "./limits.js";
4
+ /** How a query is parsed, compiled and evaluated. */
5
+ export type JsonPathOptions = {
6
+ /** Bounds on the query and on each evaluation; see {@link JsonPathLimits}. */
7
+ readonly limits?: JsonPathLimits;
8
+ /**
9
+ * A work allowance shared with the caller's other work (`@ac-kit/core`'s
10
+ * `WorkBudget`, such as a `FixedWorkBudget` for one whole page), charged
11
+ * instead of a fresh {@link JsonPathLimits.maxWork} allowance. Whatever its
12
+ * `spend` throws propagates unchanged.
13
+ */
14
+ readonly budget?: WorkBudget;
15
+ };
16
+ /**
17
+ * Parses and checks an absolute RFC 9535 query: well-formed, every integer
18
+ * within ±(2⁵³−1), every function call well-typed (RFC 9535 §2.4.3).
19
+ *
20
+ * The text must be the query alone, without blank space at either end.
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * const query = parseJsonPath("$.store.book[?@.price < 10].title");
25
+ * query.segments.length; // 4
26
+ * ```;
27
+ *
28
+ * @param source The query text.
29
+ * @param options Limits, and a budget charged one unit per character.
30
+ * @returns The query's syntax tree. Complexity: O(source length) time and
31
+ * memory.
32
+ * @throws {JsonPathSyntaxError} If `source` is not a valid query.
33
+ * @throws {JsonPathLimitExceededError} If `source` is longer than
34
+ * {@link JsonPathLimits.maxQueryLength} or nests deeper than
35
+ * {@link JsonPathLimits.maxNestingDepth}.
36
+ * @throws {RangeError} If a limit is out of its range.
37
+ */
38
+ export declare function parseJsonPath(source: string, options?: JsonPathOptions): JsonPathQuery;
39
+ /**
40
+ * {@link parseJsonPath}, answering `null` for a query that is not valid.
41
+ *
42
+ * @throws {JsonPathLimitExceededError} As {@link parseJsonPath}: outgrowing a
43
+ * limit is a fault to report, not an invalid query.
44
+ * @throws {RangeError} If a limit is out of its range.
45
+ */
46
+ export declare function tryParseJsonPath(source: string, options?: JsonPathOptions): JsonPathQuery | null;
47
+ /**
48
+ * Whether `query` is singular (RFC 9535 §2.3.5.1): only child segments with a
49
+ * single name or index selector each, so it selects at most one node.
50
+ *
51
+ * @example
52
+ * ```ts
53
+ * isSingularJsonPath(parseJsonPath("$.a[0]['b']")); // true
54
+ * isSingularJsonPath(parseJsonPath("$.a[*]")); // false
55
+ * ```;
56
+ *
57
+ * @returns Complexity: O(segments).
58
+ */
59
+ export declare function isSingularJsonPath(query: JsonPathQuery): boolean;
60
+ //# sourceMappingURL=parse-jsonpath.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-jsonpath.d.ts","sourceRoot":"","sources":["../src/parse-jsonpath.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAG/C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAE9C,OAAO,EAAE,KAAK,cAAc,EAAkB,MAAM,aAAa,CAAC;AAElE,qDAAqD;AACrD,MAAM,MAAM,eAAe,GAAG;IAC7B,8EAA8E;IAC9E,QAAQ,CAAC,MAAM,CAAC,EAAE,cAAc,CAAC;IAEjC;;;;;OAKG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,UAAU,CAAC;CAC7B,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,aAAa,CAC5B,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,eAAe,GACvB,aAAa,CAIf;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAC/B,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,eAAe,GACvB,aAAa,GAAG,IAAI,CAWtB;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,aAAa,GAAG,OAAO,CAEhE"}
@@ -0,0 +1,66 @@
1
+ import { isSingularQuery_, parseJsonPathBounded_ } from "./_parse.js";
2
+ import { JsonPathSyntaxError } from "./errors.js";
3
+ import { jsonPathLimits } from "./limits.js";
4
+ /**
5
+ * Parses and checks an absolute RFC 9535 query: well-formed, every integer
6
+ * within ±(2⁵³−1), every function call well-typed (RFC 9535 §2.4.3).
7
+ *
8
+ * The text must be the query alone, without blank space at either end.
9
+ *
10
+ * @example
11
+ * ```ts
12
+ * const query = parseJsonPath("$.store.book[?@.price < 10].title");
13
+ * query.segments.length; // 4
14
+ * ```;
15
+ *
16
+ * @param source The query text.
17
+ * @param options Limits, and a budget charged one unit per character.
18
+ * @returns The query's syntax tree. Complexity: O(source length) time and
19
+ * memory.
20
+ * @throws {JsonPathSyntaxError} If `source` is not a valid query.
21
+ * @throws {JsonPathLimitExceededError} If `source` is longer than
22
+ * {@link JsonPathLimits.maxQueryLength} or nests deeper than
23
+ * {@link JsonPathLimits.maxNestingDepth}.
24
+ * @throws {RangeError} If a limit is out of its range.
25
+ */
26
+ export function parseJsonPath(source, options) {
27
+ const limits = jsonPathLimits(options?.limits);
28
+ options?.budget?.spend(Math.min(source.length, limits.maxQueryLength) + 1);
29
+ return parseJsonPathBounded_(source, limits);
30
+ }
31
+ /**
32
+ * {@link parseJsonPath}, answering `null` for a query that is not valid.
33
+ *
34
+ * @throws {JsonPathLimitExceededError} As {@link parseJsonPath}: outgrowing a
35
+ * limit is a fault to report, not an invalid query.
36
+ * @throws {RangeError} If a limit is out of its range.
37
+ */
38
+ export function tryParseJsonPath(source, options) {
39
+ const limits = jsonPathLimits(options?.limits);
40
+ options?.budget?.spend(Math.min(source.length, limits.maxQueryLength) + 1);
41
+ try {
42
+ return parseJsonPathBounded_(source, limits);
43
+ }
44
+ catch (error) {
45
+ if (error instanceof JsonPathSyntaxError) {
46
+ return null;
47
+ }
48
+ throw error;
49
+ }
50
+ }
51
+ /**
52
+ * Whether `query` is singular (RFC 9535 §2.3.5.1): only child segments with a
53
+ * single name or index selector each, so it selects at most one node.
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * isSingularJsonPath(parseJsonPath("$.a[0]['b']")); // true
58
+ * isSingularJsonPath(parseJsonPath("$.a[*]")); // false
59
+ * ```;
60
+ *
61
+ * @returns Complexity: O(segments).
62
+ */
63
+ export function isSingularJsonPath(query) {
64
+ return isSingularQuery_(query);
65
+ }
66
+ //# sourceMappingURL=parse-jsonpath.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-jsonpath.js","sourceRoot":"","sources":["../src/parse-jsonpath.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAEtE,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,EAAuB,cAAc,EAAE,MAAM,aAAa,CAAC;AAgBlE;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,aAAa,CAC5B,MAAc,EACd,OAAyB;IAEzB,MAAM,MAAM,GAAG,cAAc,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IAC/C,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC,CAAC;IAC3E,OAAO,qBAAqB,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAC9C,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,gBAAgB,CAC/B,MAAc,EACd,OAAyB;IAEzB,MAAM,MAAM,GAAG,cAAc,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IAC/C,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC,CAAC;IAC3E,IAAI,CAAC;QACJ,OAAO,qBAAqB,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC9C,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,IAAI,KAAK,YAAY,mBAAmB,EAAE,CAAC;YAC1C,OAAO,IAAI,CAAC;QACb,CAAC;QACD,MAAM,KAAK,CAAC;IACb,CAAC;AACF,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,kBAAkB,CAAC,KAAoB;IACtD,OAAO,gBAAgB,CAAC,KAAK,CAAC,CAAC;AAChC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,68 @@
1
1
  {
2
2
  "name": "@ac-kit/format-jsonpath",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "JSONPath (RFC 9535): parse and query JSON values.",
5
+ "keywords": [
6
+ "json",
7
+ "jsonpath",
8
+ "query",
9
+ "rfc9535"
10
+ ],
11
+ "homepage": "https://anthochamp.pages.grenadine.io/node-essentials/api/format-jsonpath/",
12
+ "bugs": {
13
+ "url": "https://git.grenadine.io/anthochamp/node-essentials/issues"
14
+ },
15
+ "license": "BSD-3-Clause",
16
+ "author": "Anthony Champagne <dev@anthonychampagne.fr>",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://git.grenadine.io/anthochamp/node-essentials.git",
20
+ "directory": "packages/format-jsonpath"
21
+ },
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "type": "module",
26
+ "sideEffects": false,
27
+ "main": "./dist/index.js",
28
+ "exports": {
29
+ ".": "./dist/index.js"
30
+ },
31
+ "publishConfig": {
32
+ "main": "./dist/index.js",
33
+ "exports": {
34
+ ".": "./dist/index.js"
35
+ },
36
+ "access": "public"
37
+ },
38
+ "scripts": {
39
+ "compile": "tsc -p ./tsconfig.build.json -outDir dist",
40
+ "compile:check": "tsc -noEmit -p ./tsconfig.json",
41
+ "clean": "rimraf dist",
42
+ "build": "yarn clean && yarn compile",
43
+ "test": "yarn run -T vitest run",
44
+ "prepack": "yarn build",
45
+ "update:cts": "node ./scripts/update-cts.ts"
46
+ },
47
+ "dependencies": {
48
+ "@ac-kit/algo": "^0.4.0",
49
+ "@ac-kit/core": "^0.10.0",
50
+ "@ac-kit/format-regex": "^0.3.0",
51
+ "@types/web": "^0.0.367",
52
+ "type-fest": "^6.0.0"
53
+ },
54
+ "devDependencies": {
55
+ "@ac-kit/math-random": "0.2.0",
56
+ "@ac-kit/test-util": "0.3.0",
57
+ "@ac-kit/tsconfig": "0.1.0",
58
+ "@ac-kit/typedoc-config": "0.1.0",
59
+ "@ac-kit/vitest-config": "0.1.1",
60
+ "@typescript/native": "typescript@7.0.2",
61
+ "rimraf": "6.1.4",
62
+ "typedoc": "0.28.20",
63
+ "typescript": "@typescript/typescript6@6.0.2"
64
+ },
65
+ "engines": {
66
+ "node": ">=24"
67
+ }
6
68
  }