@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.
- package/CHANGELOG.md +62 -0
- package/LICENSE +28 -0
- package/README.md +169 -2
- package/dist/_costs.d.ts +26 -0
- package/dist/_costs.d.ts.map +1 -0
- package/dist/_costs.js +36 -0
- package/dist/_costs.js.map +1 -0
- package/dist/_evaluate.d.ts +41 -0
- package/dist/_evaluate.d.ts.map +1 -0
- package/dist/_evaluate.js +512 -0
- package/dist/_evaluate.js.map +1 -0
- package/dist/_functions.d.ts +12 -0
- package/dist/_functions.d.ts.map +1 -0
- package/dist/_functions.js +17 -0
- package/dist/_functions.js.map +1 -0
- package/dist/_json-equal.d.ts +19 -0
- package/dist/_json-equal.d.ts.map +1 -0
- package/dist/_json-equal.js +91 -0
- package/dist/_json-equal.js.map +1 -0
- package/dist/_parse.d.ts +17 -0
- package/dist/_parse.d.ts.map +1 -0
- package/dist/_parse.js +617 -0
- package/dist/_parse.js.map +1 -0
- package/dist/ast.d.ts +133 -0
- package/dist/ast.d.ts.map +1 -0
- package/dist/ast.js +2 -0
- package/dist/ast.js.map +1 -0
- package/dist/compile-jsonpath.d.ts +108 -0
- package/dist/compile-jsonpath.d.ts.map +1 -0
- package/dist/compile-jsonpath.js +103 -0
- package/dist/compile-jsonpath.js.map +1 -0
- package/dist/errors.d.ts +64 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +34 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/limits.d.ts +108 -0
- package/dist/limits.d.ts.map +1 -0
- package/dist/limits.js +86 -0
- package/dist/limits.js.map +1 -0
- package/dist/normalized-path.d.ts +67 -0
- package/dist/normalized-path.d.ts.map +1 -0
- package/dist/normalized-path.js +269 -0
- package/dist/normalized-path.js.map +1 -0
- package/dist/parse-jsonpath.d.ts +60 -0
- package/dist/parse-jsonpath.d.ts.map +1 -0
- package/dist/parse-jsonpath.js +66 -0
- package/dist/parse-jsonpath.js.map +1 -0
- package/package.json +65 -3
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# @ac-kit/format-jsonpath
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 5152230: New package: RFC 9535 JSONPath queries over plain JSON values, safe on
|
|
8
|
+
untrusted queries and documents.
|
|
9
|
+
|
|
10
|
+
- `compileJsonPath(source, options?)` checks and compiles a query once;
|
|
11
|
+
`select(document, budget?)` returns the selected values and
|
|
12
|
+
`selectNodes(document, budget?)` pairs each with its normalized path (a
|
|
13
|
+
`PropertyPath` from `@ac-kit/core`). `selectJsonPath` and
|
|
14
|
+
`selectJsonPathNodes` do both in one call. `parseJsonPath`,
|
|
15
|
+
`tryParseJsonPath` and `isSingularJsonPath` expose the syntax tree.
|
|
16
|
+
- The whole RFC: every selector and segment, filters, comparisons, the five
|
|
17
|
+
functions, well-typedness and the I-JSON integer range. The JSONPath
|
|
18
|
+
Compliance Test Suite passes in full. Object members come in `Object.keys`
|
|
19
|
+
order; strings compare by code point; `1e400` is `Infinity`.
|
|
20
|
+
- `match()` and `search()` run RFC 9485 I-Regexp patterns on
|
|
21
|
+
`@ac-kit/format-regex`'s linear-time engine. `^` and `$` are anchors, as the
|
|
22
|
+
compliance suite expects, not RFC 9485's literal characters.
|
|
23
|
+
- `printNormalizedPath` and `parseNormalizedPath` (RFC 9535 §2.7); a lone
|
|
24
|
+
surrogate in a member name is printed as a `\udxxx` escape.
|
|
25
|
+
`printNormalizedPath(path, { maxLength })` checks the printed length before
|
|
26
|
+
building anything (16 Mi UTF-16 code units by default); overrides must be
|
|
27
|
+
positive integers at most 2²⁸ − 16 code units, `Infinity` is not allowed.
|
|
28
|
+
- `JsonPathSyntaxError` (`code`, `source`, `position`) for invalid queries.
|
|
29
|
+
`JsonPathLimits` bounds query length, nesting (at most 256) and each
|
|
30
|
+
evaluation's work (`maxWork`, 3 Mi units, about 100 ms; charged before the
|
|
31
|
+
work, weighted by real cost, including every regex step), throwing
|
|
32
|
+
`JsonPathLimitExceededError`; a caller's `WorkBudget` can be shared across
|
|
33
|
+
calls instead. `limits.regex` takes `maxProgramSize` and `maxNestingDepth`
|
|
34
|
+
only. A single `Object.keys` listing runs to completion before the budget
|
|
35
|
+
can stop it; for a hard latency bound, limit document size or object width
|
|
36
|
+
at parse time. Deep documents and deep queries never reach the native call
|
|
37
|
+
stack's limit, even under a deep caller.
|
|
38
|
+
|
|
39
|
+
### Patch Changes
|
|
40
|
+
|
|
41
|
+
- Updated dependencies [2a0fe74]
|
|
42
|
+
- Updated dependencies [5152230]
|
|
43
|
+
- Updated dependencies [5152230]
|
|
44
|
+
- Updated dependencies [2a0fe74]
|
|
45
|
+
- Updated dependencies [5152230]
|
|
46
|
+
- Updated dependencies [2a0fe74]
|
|
47
|
+
- Updated dependencies [5152230]
|
|
48
|
+
- Updated dependencies [5152230]
|
|
49
|
+
- Updated dependencies [2a0fe74]
|
|
50
|
+
- Updated dependencies [2a0fe74]
|
|
51
|
+
- Updated dependencies [5152230]
|
|
52
|
+
- Updated dependencies [5152230]
|
|
53
|
+
- Updated dependencies [5152230]
|
|
54
|
+
- Updated dependencies [5152230]
|
|
55
|
+
- Updated dependencies [5152230]
|
|
56
|
+
- Updated dependencies [5152230]
|
|
57
|
+
- Updated dependencies [5152230]
|
|
58
|
+
- Updated dependencies [5152230]
|
|
59
|
+
- Updated dependencies [5152230]
|
|
60
|
+
- @ac-kit/algo@0.4.0
|
|
61
|
+
- @ac-kit/core@0.10.0
|
|
62
|
+
- @ac-kit/format-regex@0.3.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-2026 Anthony Champagne and contributors
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
16
|
+
contributors may be used to endorse or promote products derived from
|
|
17
|
+
this software without specific prior written permission.
|
|
18
|
+
|
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,170 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @ac-kit/format-jsonpath
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
JSONPath queries ([RFC 9535](https://www.rfc-editor.org/rfc/rfc9535)) over
|
|
4
|
+
plain JSON values: parse, check, compile once and select many times, with each
|
|
5
|
+
result's normalized path when you need it.
|
|
6
|
+
|
|
7
|
+
A JSONPath query often arrives from outside the program, in a configuration
|
|
8
|
+
file or a plugin, and the document it runs over does too. So the whole RFC is
|
|
9
|
+
checked before anything runs, and every evaluation runs within a work budget
|
|
10
|
+
charged by what it actually does: a hostile query or document fails with a
|
|
11
|
+
clear error instead of hanging the process or overflowing the stack.
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { compileJsonPath, selectJsonPath } from "@ac-kit/format-jsonpath";
|
|
15
|
+
|
|
16
|
+
// Any JSON value: JSON.parse output, or a literal typed as JSON.
|
|
17
|
+
const store = JSON.parse(`{
|
|
18
|
+
"books": [
|
|
19
|
+
{ "title": "Moby Dick", "price": 8.99, "isbn": "0-553-21311-3" },
|
|
20
|
+
{ "title": "Sword of Honour", "price": 12.99 }
|
|
21
|
+
]
|
|
22
|
+
}`);
|
|
23
|
+
|
|
24
|
+
selectJsonPath(store, "$.books[?@.price < 10].title"); // ["Moby Dick"]
|
|
25
|
+
selectJsonPath(store, "$..isbn"); // ["0-553-21311-3"]
|
|
26
|
+
|
|
27
|
+
const titles = compileJsonPath("$.books[*].title");
|
|
28
|
+
titles.select(store); // ["Moby Dick", "Sword of Honour"]
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## What is supported
|
|
32
|
+
|
|
33
|
+
The whole of RFC 9535: name, wildcard, index, slice and filter selectors;
|
|
34
|
+
child and descendant segments; `&&`, `||`, `!`, parentheses and the six
|
|
35
|
+
comparison operators; the five functions `length()`, `count()`, `match()`,
|
|
36
|
+
`search()` and `value()`; and the validity rules — every index and slice bound
|
|
37
|
+
within ±(2⁵³−1), every function call well-typed (RFC 9535 §2.4.3). The
|
|
38
|
+
[JSONPath Compliance Test Suite](https://github.com/jsonpath-standard/jsonpath-compliance-test-suite)
|
|
39
|
+
runs in full in this package's tests.
|
|
40
|
+
|
|
41
|
+
Pre-RFC dialects are not: no script expressions (`[(@.length-1)]`), no
|
|
42
|
+
`@.length`, no `[?(@.a)]` forms the RFC rejects.
|
|
43
|
+
|
|
44
|
+
Details the RFC leaves to the implementation:
|
|
45
|
+
|
|
46
|
+
- **Order.** Object members come in `Object.keys` order: integer-like names
|
|
47
|
+
first, ascending, then insertion order. Descendants come in pre-order. A node
|
|
48
|
+
selected twice (`$[0,0]`) is listed twice.
|
|
49
|
+
- **Numbers.** A number literal is an IEEE 754 double; one beyond its range,
|
|
50
|
+
such as `1e400`, is `Infinity`, which the RFC allows since such numbers are
|
|
51
|
+
outside I-JSON's interoperable range. Numbers compare by value, so
|
|
52
|
+
`1 == 1.0` and `0 == -0`.
|
|
53
|
+
- **Strings** compare by Unicode code point, not by UTF-16 code unit, so
|
|
54
|
+
`'\uFFFF' < '𐀀'`; `length()` counts code points.
|
|
55
|
+
- **Function extensions** beyond the five built-ins are not supported; an
|
|
56
|
+
unknown function is a syntax error.
|
|
57
|
+
|
|
58
|
+
### Regular expressions
|
|
59
|
+
|
|
60
|
+
`match()` and `search()` take RFC 9485 I-Regexp patterns, run by
|
|
61
|
+
`@ac-kit/format-regex` in time linear in the input, whatever the pattern: no
|
|
62
|
+
backtracking, so `(a*)*b` is as cheap as `a*b`. Matching is by code point, `.`
|
|
63
|
+
excludes only `\n` and `\r`, and `\p{…}` takes the general categories of the
|
|
64
|
+
JavaScript engine's Unicode version. A pattern that is not an I-Regexp makes the
|
|
65
|
+
call false.
|
|
66
|
+
|
|
67
|
+
**Deviation:** `^` and `$` outside a class are anchors, as the compliance test
|
|
68
|
+
suite expects, rather than the literal characters RFC 9485's grammar makes of
|
|
69
|
+
them. Write a literal caret or dollar as `\\^` or `[$]` inside a query string.
|
|
70
|
+
|
|
71
|
+
## Documents
|
|
72
|
+
|
|
73
|
+
Documents are plain JSON values, as `JSON.parse` and the `@ac-kit/format-json*`
|
|
74
|
+
parsers produce them: arrays are `Array.isArray`, objects any other non-null
|
|
75
|
+
object. Members are read as own enumerable string keys only, so `$.constructor`
|
|
76
|
+
selects nothing on `{}` and a `__proto__` member parsed from JSON is selected
|
|
77
|
+
like any other. Nothing is ever written. A value outside JSON (a cycle, a
|
|
78
|
+
`Map`) gives unspecified results, but within the same limits.
|
|
79
|
+
|
|
80
|
+
## Nodes and normalized paths
|
|
81
|
+
|
|
82
|
+
`selectNodes` pairs each value with its location, an array of member names and
|
|
83
|
+
array indices that core's `getAtPath` reads directly. `printNormalizedPath`
|
|
84
|
+
spells it the RFC 9535 §2.7 way, and `parseNormalizedPath` reads it back:
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
import {
|
|
88
|
+
compileJsonPath,
|
|
89
|
+
parseNormalizedPath,
|
|
90
|
+
printNormalizedPath,
|
|
91
|
+
} from "@ac-kit/format-jsonpath";
|
|
92
|
+
|
|
93
|
+
const [node] = compileJsonPath("$..price").selectNodes({ a: [{ price: 3 }] });
|
|
94
|
+
if (node !== undefined) {
|
|
95
|
+
node.path; // ["a", 0, "price"]
|
|
96
|
+
printNormalizedPath(node.path); // "$['a'][0]['price']"
|
|
97
|
+
}
|
|
98
|
+
parseNormalizedPath("$['a'][0]['price']"); // ["a", 0, "price"]
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
A member name holding a lone surrogate, which a JSON string may carry but §2.7
|
|
102
|
+
cannot spell, is printed with a `\udxxx` escape that `parseNormalizedPath`
|
|
103
|
+
accepts back. `printNormalizedPath` computes the printed length before building
|
|
104
|
+
anything and refuses one beyond `maxLength` (16 Mi code units by default) with
|
|
105
|
+
`JsonPathLimitExceededError`. `maxLength` must be a positive integer at most
|
|
106
|
+
2²⁸ − 16 UTF-16 code units (core's `MAX_PORTABLE_STRING_LENGTH`); larger values and `Infinity` throw `RangeError`
|
|
107
|
+
before the path is read.
|
|
108
|
+
|
|
109
|
+
## Errors
|
|
110
|
+
|
|
111
|
+
A query that is not valid throws `JsonPathSyntaxError`, a `SyntaxError` with a
|
|
112
|
+
`code` (`"invalid-integer"`, `"non-singular-query"`, `"unknown-function"`, …)
|
|
113
|
+
and the `position` it gave up at. `tryParseJsonPath` answers `null` instead.
|
|
114
|
+
`parseJsonPath` returns the syntax tree, for a caller that checks a query when
|
|
115
|
+
loading it or inspects it (`isSingularJsonPath`).
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
import { parseJsonPath, tryParseJsonPath } from "@ac-kit/format-jsonpath";
|
|
119
|
+
|
|
120
|
+
tryParseJsonPath("$[01]"); // null: leading zero
|
|
121
|
+
tryParseJsonPath("$[?@.* == 1]"); // null: compared query is not singular
|
|
122
|
+
parseJsonPath("$.a[?length(@) > 2]").segments.length; // 2
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Limits and work budget
|
|
126
|
+
|
|
127
|
+
`JsonPathLimits` bounds a query's length (`maxQueryLength`, 4 Ki by default)
|
|
128
|
+
and nesting (`maxNestingDepth`, 64, at most 256), checked while parsing, and
|
|
129
|
+
each evaluation's work (`maxWork`, 3 Mi units, about 100 ms of the costliest
|
|
130
|
+
evaluations on a typical machine). Work is charged before it is done, weighted
|
|
131
|
+
by its real cost so that a unit takes about the same time whatever the query
|
|
132
|
+
does: each selector tried, node listed, visited or appended, step of a deep
|
|
133
|
+
comparison, code unit compared or measured, normalized path segment built, and
|
|
134
|
+
every regex compilation and step. Exceeding a limit throws
|
|
135
|
+
`JsonPathLimitExceededError`, naming the limit.
|
|
136
|
+
|
|
137
|
+
Neither the document's depth nor the query's nesting reaches the native call
|
|
138
|
+
stack: both are walked on explicit stacks, so a document nested a million deep
|
|
139
|
+
is fine and a deep query works even when the caller is itself deep in the
|
|
140
|
+
stack.
|
|
141
|
+
|
|
142
|
+
`maxWork` is not a hard latency bound. A single `Object.keys` listing of a wide
|
|
143
|
+
object runs to completion before the budget can stop it. Each object is listed
|
|
144
|
+
at most once per evaluation; a single listing took about 66 ms for 500,000
|
|
145
|
+
members and 158 ms for 1 million. If you need a hard latency bound, limit
|
|
146
|
+
document size or object width at parse time.
|
|
147
|
+
|
|
148
|
+
To cap several evaluations together, say everything done for one page, pass
|
|
149
|
+
one `WorkBudget` from `@ac-kit/core` to each call: it is charged instead of a
|
|
150
|
+
fresh `maxWork`, and what it throws when spent comes through unchanged.
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
import { FixedWorkBudget } from "@ac-kit/core";
|
|
154
|
+
import { compileJsonPath } from "@ac-kit/format-jsonpath";
|
|
155
|
+
|
|
156
|
+
const budget = new FixedWorkBudget(1_000_000);
|
|
157
|
+
const links = compileJsonPath("$..links[*].href", { budget });
|
|
158
|
+
for (const document of [{ links: [{ href: "/a" }] }, { links: [] }]) {
|
|
159
|
+
links.select(document, budget); // WorkBudgetExceededError once spent
|
|
160
|
+
}
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
A regex pattern too large to compile throws `RegexLimitExceededError` from
|
|
164
|
+
`@ac-kit/format-regex`, whether it is written in the query or read from the
|
|
165
|
+
document; `limits.regex` passes `maxProgramSize` and `maxNestingDepth` through.
|
|
166
|
+
It has no `maxSteps`: every regex step is charged to the JSONPath allowance.
|
|
167
|
+
|
|
168
|
+
See the
|
|
169
|
+
[generated API docs](https://anthochamp.pages.grenadine.io/node-essentials/api/format-jsonpath/)
|
|
170
|
+
for the full reference.
|
package/dist/_costs.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Work units charged for each operation of an evaluation, weighted so that one
|
|
3
|
+
* unit costs about the same time, roughly 25 ns on a typical machine, whatever
|
|
4
|
+
* the query does. Measured by `limits.test.ts` ("keeps the time per unit
|
|
5
|
+
* bounded"); regex steps are `@ac-kit/format-regex`'s own, cheaper, units.
|
|
6
|
+
*
|
|
7
|
+
* The unit is a cheap step: a selector tried, a logical operator applied, a
|
|
8
|
+
* node visited or appended to a nodelist, a segment of a path built.
|
|
9
|
+
*/
|
|
10
|
+
export declare const STEP_COST_ = 1;
|
|
11
|
+
/** Listing one node: a location record allocated and stored. */
|
|
12
|
+
export declare const NODE_COST_ = 3;
|
|
13
|
+
/** Listing one member name of an object (`Object.keys`, about 100 ns). */
|
|
14
|
+
export declare const MEMBER_COST_ = 4;
|
|
15
|
+
/**
|
|
16
|
+
* One pair of containers tracked by a deep comparison: a map and a set entry,
|
|
17
|
+
* besides the step that compares them.
|
|
18
|
+
*/
|
|
19
|
+
export declare const PAIR_COST_ = 6;
|
|
20
|
+
/** The units for comparing `length` code units for equality, rounded up. */
|
|
21
|
+
export declare function equalityScanCost_(length: number): number;
|
|
22
|
+
/** The units for ordering `length` code units by code point, rounded up. */
|
|
23
|
+
export declare function orderScanCost_(length: number): number;
|
|
24
|
+
/** The units for counting the code points of `length` code units. */
|
|
25
|
+
export declare function measureScanCost_(length: number): number;
|
|
26
|
+
//# sourceMappingURL=_costs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_costs.d.ts","sourceRoot":"","sources":["../src/_costs.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,eAAO,MAAM,UAAU,IAAI,CAAC;AAE5B,gEAAgE;AAChE,eAAO,MAAM,UAAU,IAAI,CAAC;AAE5B,0EAA0E;AAC1E,eAAO,MAAM,YAAY,IAAI,CAAC;AAE9B;;;GAGG;AACH,eAAO,MAAM,UAAU,IAAI,CAAC;AAQ5B,4EAA4E;AAC5E,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAExD;AAED,4EAA4E;AAC5E,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAErD;AAED,qEAAqE;AACrE,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAEvD"}
|
package/dist/_costs.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Work units charged for each operation of an evaluation, weighted so that one
|
|
3
|
+
* unit costs about the same time, roughly 25 ns on a typical machine, whatever
|
|
4
|
+
* the query does. Measured by `limits.test.ts` ("keeps the time per unit
|
|
5
|
+
* bounded"); regex steps are `@ac-kit/format-regex`'s own, cheaper, units.
|
|
6
|
+
*
|
|
7
|
+
* The unit is a cheap step: a selector tried, a logical operator applied, a
|
|
8
|
+
* node visited or appended to a nodelist, a segment of a path built.
|
|
9
|
+
*/
|
|
10
|
+
export const STEP_COST_ = 1;
|
|
11
|
+
/** Listing one node: a location record allocated and stored. */
|
|
12
|
+
export const NODE_COST_ = 3;
|
|
13
|
+
/** Listing one member name of an object (`Object.keys`, about 100 ns). */
|
|
14
|
+
export const MEMBER_COST_ = 4;
|
|
15
|
+
/**
|
|
16
|
+
* One pair of containers tracked by a deep comparison: a map and a set entry,
|
|
17
|
+
* besides the step that compares them.
|
|
18
|
+
*/
|
|
19
|
+
export const PAIR_COST_ = 6;
|
|
20
|
+
/** Code units compared for equality per unit: a native `memcmp`. */
|
|
21
|
+
const EQUALITY_CODE_UNITS_ = 512;
|
|
22
|
+
/** Code units ordered by code point per unit (`compareCodePoints`). */
|
|
23
|
+
const ORDER_CODE_UNITS_ = 16;
|
|
24
|
+
/** The units for comparing `length` code units for equality, rounded up. */
|
|
25
|
+
export function equalityScanCost_(length) {
|
|
26
|
+
return Math.ceil(length / EQUALITY_CODE_UNITS_);
|
|
27
|
+
}
|
|
28
|
+
/** The units for ordering `length` code units by code point, rounded up. */
|
|
29
|
+
export function orderScanCost_(length) {
|
|
30
|
+
return Math.ceil(length / ORDER_CODE_UNITS_);
|
|
31
|
+
}
|
|
32
|
+
/** The units for counting the code points of `length` code units. */
|
|
33
|
+
export function measureScanCost_(length) {
|
|
34
|
+
return length;
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=_costs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_costs.js","sourceRoot":"","sources":["../src/_costs.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,CAAC;AAE5B,gEAAgE;AAChE,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,CAAC;AAE5B,0EAA0E;AAC1E,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,CAAC;AAE9B;;;GAGG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,CAAC;AAE5B,oEAAoE;AACpE,MAAM,oBAAoB,GAAG,GAAG,CAAC;AAEjC,uEAAuE;AACvE,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAE7B,4EAA4E;AAC5E,MAAM,UAAU,iBAAiB,CAAC,MAAc;IAC/C,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,oBAAoB,CAAC,CAAC;AACjD,CAAC;AAED,4EAA4E;AAC5E,MAAM,UAAU,cAAc,CAAC,MAAc;IAC5C,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,iBAAiB,CAAC,CAAC;AAC9C,CAAC;AAED,qEAAqE;AACrE,MAAM,UAAU,gBAAgB,CAAC,MAAc;IAC9C,OAAO,MAAM,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type WorkBudget } from "@ac-kit/core";
|
|
2
|
+
import { type IRegexp, type RegexLimits } from "@ac-kit/format-regex";
|
|
3
|
+
import type { JsonPathQuery } from "./ast.js";
|
|
4
|
+
/**
|
|
5
|
+
* A node of a nodelist with its location as a chain of parents, so a path is
|
|
6
|
+
* materialised only when asked for. `children` caches the node's children once
|
|
7
|
+
* listed, so a descendant walk and the selectors applied during it list them
|
|
8
|
+
* once between them.
|
|
9
|
+
*/
|
|
10
|
+
export type Located_ = {
|
|
11
|
+
readonly value: unknown;
|
|
12
|
+
readonly parent: Located_ | null;
|
|
13
|
+
readonly key: string | number;
|
|
14
|
+
children: readonly Located_[] | undefined;
|
|
15
|
+
};
|
|
16
|
+
/** What a compiled query keeps for every evaluation. */
|
|
17
|
+
export type CompiledEvaluation_ = {
|
|
18
|
+
readonly query: JsonPathQuery;
|
|
19
|
+
/** Subtrees short and filter-free enough for plain recursion. */
|
|
20
|
+
readonly inline: ReadonlySet<object>;
|
|
21
|
+
/** Patterns compiled with the query; `null` for one that is not an I-Regexp. */
|
|
22
|
+
readonly literalPatterns: ReadonlyMap<string, IRegexp | null>;
|
|
23
|
+
readonly regexLimits: RegexLimits;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Evaluates a compiled query over `document`, charging `budget`. Iterative:
|
|
27
|
+
* neither the document's depth nor the query's nesting reaches the native call
|
|
28
|
+
* stack.
|
|
29
|
+
*/
|
|
30
|
+
export declare function evaluate_(compiled: CompiledEvaluation_, document: unknown, budget: WorkBudget): readonly Located_[];
|
|
31
|
+
/**
|
|
32
|
+
* The subtrees of `query` evaluated by plain recursion: filter-free and at most
|
|
33
|
+
* {@link INLINE_HEIGHT_} tall. Iterative, post-order. O(query size).
|
|
34
|
+
*/
|
|
35
|
+
export declare function inlineSubtreesOf_(query: JsonPathQuery): Set<object>;
|
|
36
|
+
/**
|
|
37
|
+
* The patterns of `match()`/`search()` calls whose pattern is a string literal,
|
|
38
|
+
* so a compiled query compiles each once.
|
|
39
|
+
*/
|
|
40
|
+
export declare function literalPatternsOf_(query: JsonPathQuery): string[];
|
|
41
|
+
//# sourceMappingURL=_evaluate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_evaluate.d.ts","sourceRoot":"","sources":["../src/_evaluate.ts"],"names":[],"mappings":"AACA,OAAO,EAKN,KAAK,UAAU,EACf,MAAM,cAAc,CAAC;AACtB,OAAO,EACN,KAAK,OAAO,EACZ,KAAK,WAAW,EAEhB,MAAM,sBAAsB,CAAC;AAW9B,OAAO,KAAK,EAKX,aAAa,EAIb,MAAM,UAAU,CAAC;AAElB;;;;;GAKG;AACH,MAAM,MAAM,QAAQ,GAAG;IACtB,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,QAAQ,GAAG,IAAI,CAAC;IACjC,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC;IAC9B,QAAQ,EAAE,SAAS,QAAQ,EAAE,GAAG,SAAS,CAAC;CAC1C,CAAC;AAaF,wDAAwD;AACxD,MAAM,MAAM,mBAAmB,GAAG;IACjC,QAAQ,CAAC,KAAK,EAAE,aAAa,CAAC;IAC9B,iEAAiE;IACjE,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IACrC,gFAAgF;IAChF,QAAQ,CAAC,eAAe,EAAE,WAAW,CAAC,MAAM,EAAE,OAAO,GAAG,IAAI,CAAC,CAAC;IAC9D,QAAQ,CAAC,WAAW,EAAE,WAAW,CAAC;CAClC,CAAC;AA+kBF;;;;GAIG;AACH,wBAAgB,SAAS,CACxB,QAAQ,EAAE,mBAAmB,EAC7B,QAAQ,EAAE,OAAO,EACjB,MAAM,EAAE,UAAU,GAChB,SAAS,QAAQ,EAAE,CAgBrB;AAoCD;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,aAAa,GAAG,GAAG,CAAC,MAAM,CAAC,CA6BnE;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,aAAa,GAAG,MAAM,EAAE,CAoBjE"}
|