@vltpkg/query 0.0.0-8 → 0.0.0-9
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/README.md +25 -6
- package/dist/esm/id.d.ts.map +1 -1
- package/dist/esm/id.js +20 -6
- package/dist/esm/id.js.map +1 -1
- package/dist/esm/index.d.ts +7 -2
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +168 -8
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/parser.d.ts +15 -0
- package/dist/esm/parser.d.ts.map +1 -0
- package/dist/esm/parser.js +92 -0
- package/dist/esm/parser.js.map +1 -0
- package/dist/esm/pseudo/attr.d.ts.map +1 -1
- package/dist/esm/pseudo/attr.js +10 -2
- package/dist/esm/pseudo/attr.js.map +1 -1
- package/dist/esm/pseudo/cve.js +1 -1
- package/dist/esm/pseudo/cve.js.map +1 -1
- package/dist/esm/pseudo/cwe.js +1 -1
- package/dist/esm/pseudo/cwe.js.map +1 -1
- package/dist/esm/pseudo/dev.d.ts +6 -0
- package/dist/esm/pseudo/dev.d.ts.map +1 -0
- package/dist/esm/pseudo/dev.js +15 -0
- package/dist/esm/pseudo/dev.js.map +1 -0
- package/dist/esm/pseudo/empty.d.ts +7 -0
- package/dist/esm/pseudo/empty.d.ts.map +1 -0
- package/dist/esm/pseudo/empty.js +14 -0
- package/dist/esm/pseudo/empty.js.map +1 -0
- package/dist/esm/pseudo/helpers.d.ts +9 -1
- package/dist/esm/pseudo/helpers.d.ts.map +1 -1
- package/dist/esm/pseudo/helpers.js +19 -0
- package/dist/esm/pseudo/helpers.js.map +1 -1
- package/dist/esm/pseudo/link.d.ts +9 -0
- package/dist/esm/pseudo/link.d.ts.map +1 -0
- package/dist/esm/pseudo/link.js +25 -0
- package/dist/esm/pseudo/link.js.map +1 -0
- package/dist/esm/pseudo/malware.d.ts +2 -0
- package/dist/esm/pseudo/malware.d.ts.map +1 -1
- package/dist/esm/pseudo/malware.js +115 -7
- package/dist/esm/pseudo/malware.js.map +1 -1
- package/dist/esm/pseudo/missing.d.ts +8 -0
- package/dist/esm/pseudo/missing.d.ts.map +1 -0
- package/dist/esm/pseudo/missing.js +15 -0
- package/dist/esm/pseudo/missing.js.map +1 -0
- package/dist/esm/pseudo/optional.d.ts +6 -0
- package/dist/esm/pseudo/optional.d.ts.map +1 -0
- package/dist/esm/pseudo/optional.js +15 -0
- package/dist/esm/pseudo/optional.js.map +1 -0
- package/dist/esm/pseudo/outdated.d.ts +1 -1
- package/dist/esm/pseudo/outdated.d.ts.map +1 -1
- package/dist/esm/pseudo/outdated.js +33 -27
- package/dist/esm/pseudo/outdated.js.map +1 -1
- package/dist/esm/pseudo/peer.d.ts +6 -0
- package/dist/esm/pseudo/peer.d.ts.map +1 -0
- package/dist/esm/pseudo/peer.js +15 -0
- package/dist/esm/pseudo/peer.js.map +1 -0
- package/dist/esm/pseudo/private.d.ts +7 -0
- package/dist/esm/pseudo/private.d.ts.map +1 -0
- package/dist/esm/pseudo/private.js +16 -0
- package/dist/esm/pseudo/private.js.map +1 -0
- package/dist/esm/pseudo/prod.d.ts +6 -0
- package/dist/esm/pseudo/prod.d.ts.map +1 -0
- package/dist/esm/pseudo/prod.js +15 -0
- package/dist/esm/pseudo/prod.js.map +1 -0
- package/dist/esm/pseudo/published.d.ts +40 -0
- package/dist/esm/pseudo/published.d.ts.map +1 -0
- package/dist/esm/pseudo/published.js +159 -0
- package/dist/esm/pseudo/published.js.map +1 -0
- package/dist/esm/pseudo/scanned.d.ts +4 -2
- package/dist/esm/pseudo/scanned.d.ts.map +1 -1
- package/dist/esm/pseudo/scanned.js +10 -5
- package/dist/esm/pseudo/scanned.js.map +1 -1
- package/dist/esm/pseudo/score.d.ts +15 -0
- package/dist/esm/pseudo/score.d.ts.map +1 -0
- package/dist/esm/pseudo/score.js +119 -0
- package/dist/esm/pseudo/score.js.map +1 -0
- package/dist/esm/pseudo/semver.d.ts.map +1 -1
- package/dist/esm/pseudo/semver.js +11 -25
- package/dist/esm/pseudo/semver.js.map +1 -1
- package/dist/esm/pseudo/severity.d.ts +2 -0
- package/dist/esm/pseudo/severity.d.ts.map +1 -1
- package/dist/esm/pseudo/severity.js +110 -7
- package/dist/esm/pseudo/severity.js.map +1 -1
- package/dist/esm/pseudo/squat.d.ts +2 -0
- package/dist/esm/pseudo/squat.d.ts.map +1 -1
- package/dist/esm/pseudo/squat.js +114 -7
- package/dist/esm/pseudo/squat.js.map +1 -1
- package/dist/esm/pseudo/workspace.d.ts +6 -0
- package/dist/esm/pseudo/workspace.d.ts.map +1 -0
- package/dist/esm/pseudo/workspace.js +15 -0
- package/dist/esm/pseudo/workspace.js.map +1 -0
- package/dist/esm/pseudo.d.ts.map +1 -1
- package/dist/esm/pseudo.js +24 -40
- package/dist/esm/pseudo.js.map +1 -1
- package/dist/esm/types.d.ts +74 -5
- package/dist/esm/types.d.ts.map +1 -1
- package/dist/esm/types.js +12 -13
- package/dist/esm/types.js.map +1 -1
- package/package.json +9 -8
- package/dist/esm/class.d.ts +0 -6
- package/dist/esm/class.d.ts.map +0 -1
- package/dist/esm/class.js +0 -131
- package/dist/esm/class.js.map +0 -1
package/README.md
CHANGED
|
@@ -121,6 +121,7 @@ e.g: `#foo` is the same as `[name=foo]`
|
|
|
121
121
|
of the same name, this selector has a forgiving behavior regarding
|
|
122
122
|
its nested selector list ignoring any usage of non-existing ids,
|
|
123
123
|
classes, combinators, operators and pseudo-selectors.
|
|
124
|
+
- `:link` Matches linked packages only.
|
|
124
125
|
- `:not(<selector-list>)` Negation pseudo-class, select packages that
|
|
125
126
|
do not match a list of selectors.
|
|
126
127
|
- `:outdated(<type>)` Matches packages that are outdated, the type
|
|
@@ -139,6 +140,17 @@ e.g: `#foo` is the same as `[name=foo]`
|
|
|
139
140
|
current one
|
|
140
141
|
- `:private` Matches packages that have the property `private` set on
|
|
141
142
|
their `package.json` file.
|
|
143
|
+
- `:published(<date>)` Matches packages based on their publication
|
|
144
|
+
date. The date parameter can be prefixed with a comparator (`>`,
|
|
145
|
+
`<`, `>=`, `<=`). If no comparator is provided, it will match exact
|
|
146
|
+
dates. Please note that the value parameter needs to be quoted if
|
|
147
|
+
using a comparator. Examples:
|
|
148
|
+
- `:published(2024-01-01)` - Matches packages published exactly on
|
|
149
|
+
January 1st, 2024
|
|
150
|
+
- `:published(">2024-01-01")` - Matches packages published after
|
|
151
|
+
January 1st, 2024
|
|
152
|
+
- `:published("<=2023-12-31")` - Matches packages published on or
|
|
153
|
+
before December 31st, 2023
|
|
142
154
|
- `:semver(<value>, <function>, <custom-attribute-selector>)` Matches
|
|
143
155
|
packages based on a semver value, e.g, to retrieve all packages that
|
|
144
156
|
have a `version` satisfied by the semver value `^1.0.0`:
|
|
@@ -228,12 +240,19 @@ security data needs to be fetched prior to a `Query` instantiation.
|
|
|
228
240
|
- `:obfuscated` Matches packages that use obfuscated files,
|
|
229
241
|
intentionally packed to hide their behavior. This could be a sign of
|
|
230
242
|
malware.
|
|
231
|
-
- `:scanned`
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
243
|
+
- `:scanned` Matches packages that have insight security metadata.
|
|
244
|
+
- `:score(<rate>, <kind>)` Matches packages based on their security
|
|
245
|
+
score. The rate parameter is required and should be a value between
|
|
246
|
+
0 and 100 (or 0 and 1). The rate parameter can be prefixed with a
|
|
247
|
+
comparator (`>`, `<`, `>=`, `<=`). If no comparator is provided, it
|
|
248
|
+
will match exact scores. The kind parameter is optional and defaults
|
|
249
|
+
to 'overall'. Valid kinds are: 'overall', 'license', 'maintenance',
|
|
250
|
+
'quality', 'supplyChain', and 'vulnerability'. Examples:
|
|
251
|
+
- `:score(80)` - Matches packages with exactly 0.8 overall score
|
|
252
|
+
- `:score(">0.8")` - Matches packages with overall score greater
|
|
253
|
+
than 0.8
|
|
254
|
+
- `:score("<=0.5", "maintenance")` - Matches packages with
|
|
255
|
+
maintenance score less than or equal to 0.5
|
|
237
256
|
- `:scripts` Matches packages that have scripts that are run when the
|
|
238
257
|
package is installed. The majority of malware in npm is hidden in
|
|
239
258
|
install scripts.
|
package/dist/esm/id.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"id.d.ts","sourceRoot":"","sources":["../../src/id.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"id.d.ts","sourceRoot":"","sources":["../../src/id.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAE7C;;GAEG;AACH,eAAO,MAAM,EAAE,UACN,WAAW,KACjB,OAAO,CAAC,WAAW,CAiCrB,CAAA"}
|
package/dist/esm/id.js
CHANGED
|
@@ -1,20 +1,34 @@
|
|
|
1
1
|
import { error } from '@vltpkg/error-cause';
|
|
2
2
|
import { asIdentifierNode } from "./types.js";
|
|
3
|
-
import { attributeSelectorsMap, filterAttributes, } from "./attribute.js";
|
|
4
3
|
/**
|
|
5
4
|
* Parse ids, e.g: `#foo`
|
|
6
5
|
*/
|
|
7
6
|
export const id = async (state) => {
|
|
8
7
|
const { value } = asIdentifierNode(state.current);
|
|
9
|
-
const comparator = attributeSelectorsMap.get('=');
|
|
10
8
|
/* c8 ignore start - should not be possible */
|
|
11
9
|
if (!value) {
|
|
12
10
|
throw error('Missing identifier name');
|
|
13
11
|
}
|
|
14
|
-
if (!comparator) {
|
|
15
|
-
throw error('Could not find attribute selector comparator');
|
|
16
|
-
}
|
|
17
12
|
/* c8 ignore stop */
|
|
18
|
-
|
|
13
|
+
// Filter out any edges and their linked
|
|
14
|
+
// nodes if they don't match the id value
|
|
15
|
+
for (const edge of state.partial.edges) {
|
|
16
|
+
if (edge.name !== value) {
|
|
17
|
+
state.partial.edges.delete(edge);
|
|
18
|
+
if (edge.to) {
|
|
19
|
+
state.partial.nodes.delete(edge.to);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
// Filter out importer nodes, this extra step is needed
|
|
24
|
+
// to filter out nodes that have no edges linking to them
|
|
25
|
+
for (const node of state.partial.nodes) {
|
|
26
|
+
if (node.edgesIn.size === 0 &&
|
|
27
|
+
node.name !== value &&
|
|
28
|
+
state.partial.nodes.has(node)) {
|
|
29
|
+
state.partial.nodes.delete(node);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return state;
|
|
19
33
|
};
|
|
20
34
|
//# sourceMappingURL=id.js.map
|
package/dist/esm/id.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"id.js","sourceRoot":"","sources":["../../src/id.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAA;AAC3C,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"id.js","sourceRoot":"","sources":["../../src/id.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAA;AAC3C,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAG7C;;GAEG;AACH,MAAM,CAAC,MAAM,EAAE,GAAG,KAAK,EACrB,KAAkB,EACI,EAAE;IACxB,MAAM,EAAE,KAAK,EAAE,GAAG,gBAAgB,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;IAEjD,8CAA8C;IAC9C,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,MAAM,KAAK,CAAC,yBAAyB,CAAC,CAAA;IACxC,CAAC;IACD,oBAAoB;IAEpB,wCAAwC;IACxC,yCAAyC;IACzC,KAAK,MAAM,IAAI,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;QACvC,IAAI,IAAI,CAAC,IAAI,KAAK,KAAK,EAAE,CAAC;YACxB,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;YAChC,IAAI,IAAI,CAAC,EAAE,EAAE,CAAC;gBACZ,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;YACrC,CAAC;QACH,CAAC;IACH,CAAC;IAED,uDAAuD;IACvD,yDAAyD;IACzD,KAAK,MAAM,IAAI,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;QACvC,IACE,IAAI,CAAC,OAAO,CAAC,IAAI,KAAK,CAAC;YACvB,IAAI,CAAC,IAAI,KAAK,KAAK;YACnB,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAC7B,CAAC;YACD,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;QAClC,CAAC;IACH,CAAC;IAED,OAAO,KAAK,CAAA;AACd,CAAC,CAAA","sourcesContent":["import { error } from '@vltpkg/error-cause'\nimport { asIdentifierNode } from './types.ts'\nimport type { ParserState } from './types.ts'\n\n/**\n * Parse ids, e.g: `#foo`\n */\nexport const id = async (\n state: ParserState,\n): Promise<ParserState> => {\n const { value } = asIdentifierNode(state.current)\n\n /* c8 ignore start - should not be possible */\n if (!value) {\n throw error('Missing identifier name')\n }\n /* c8 ignore stop */\n\n // Filter out any edges and their linked\n // nodes if they don't match the id value\n for (const edge of state.partial.edges) {\n if (edge.name !== value) {\n state.partial.edges.delete(edge)\n if (edge.to) {\n state.partial.nodes.delete(edge.to)\n }\n }\n }\n\n // Filter out importer nodes, this extra step is needed\n // to filter out nodes that have no edges linking to them\n for (const node of state.partial.nodes) {\n if (\n node.edgesIn.size === 0 &&\n node.name !== value &&\n state.partial.nodes.has(node)\n ) {\n state.partial.nodes.delete(node)\n }\n }\n\n return state\n}\n"]}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import type { GraphLike } from '@vltpkg/graph';
|
|
2
2
|
import type { SpecOptions } from '@vltpkg/spec/browser';
|
|
3
3
|
import type { SecurityArchiveLike } from '@vltpkg/security-archive';
|
|
4
|
-
import type { ParserState, QueryResponse } from './types.ts';
|
|
4
|
+
import type { ParsedSelectorToken, ParserState, QueryResponse } from './types.ts';
|
|
5
5
|
export * from './types.ts';
|
|
6
6
|
export declare const walk: (state: ParserState) => Promise<ParserState>;
|
|
7
7
|
export type QueryOptions = {
|
|
8
8
|
graph: GraphLike;
|
|
9
|
+
retries?: number;
|
|
9
10
|
specOptions: SpecOptions;
|
|
10
11
|
securityArchive: SecurityArchiveLike | undefined;
|
|
11
12
|
};
|
|
@@ -17,10 +18,14 @@ export declare class Query {
|
|
|
17
18
|
* skip hydrating the security archive if it's not needed.
|
|
18
19
|
*/
|
|
19
20
|
static hasSecuritySelectors(query: string): boolean;
|
|
20
|
-
constructor({ graph, specOptions, securityArchive }: QueryOptions);
|
|
21
|
+
constructor({ graph, retries, specOptions, securityArchive, }: QueryOptions);
|
|
21
22
|
/**
|
|
22
23
|
* Search the graph for nodes and edges that match the given query.
|
|
23
24
|
*/
|
|
24
25
|
search(query: string, signal?: AbortSignal): Promise<QueryResponse>;
|
|
26
|
+
/**
|
|
27
|
+
* Parses a query string in order to retrieve an array of tokens.
|
|
28
|
+
*/
|
|
29
|
+
static getQueryTokens(query: string): ParsedSelectorToken[];
|
|
25
30
|
}
|
|
26
31
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/esm/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAY,SAAS,EAAY,MAAM,eAAe,CAAA;AAClE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AACvD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAY,SAAS,EAAY,MAAM,eAAe,CAAA;AAClE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AACvD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAA;AAcnE,OAAO,KAAK,EAEV,mBAAmB,EAEnB,WAAW,EAEX,aAAa,EAGd,MAAM,YAAY,CAAA;AAEnB,cAAc,YAAY,CAAA;AAqC1B,eAAO,MAAM,IAAI,UACR,WAAW,KACjB,OAAO,CAAC,WAAW,CAsDrB,CAAA;AAED,MAAM,MAAM,YAAY,GAAG;IACzB,KAAK,EAAE,SAAS,CAAA;IAChB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,WAAW,EAAE,WAAW,CAAA;IACxB,eAAe,EAAE,mBAAmB,GAAG,SAAS,CAAA;CACjD,CAAA;AAiDD,qBAAa,KAAK;;IAOhB;;;;OAIG;IACH,MAAM,CAAC,oBAAoB,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO;gBASvC,EACV,KAAK,EACL,OAAO,EACP,WAAW,EACX,eAAe,GAChB,EAAE,YAAY;IAyLf;;OAEG;IACG,MAAM,CACV,KAAK,EAAE,MAAM,EACb,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC,aAAa,CAAC;IA+CzB;;OAEG;IACH,MAAM,CAAC,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,mBAAmB,EAAE;CAmE5D"}
|
package/dist/esm/index.js
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
import { error } from '@vltpkg/error-cause';
|
|
2
|
-
import
|
|
2
|
+
import { parse } from "./parser.js";
|
|
3
3
|
import { attribute } from "./attribute.js";
|
|
4
|
-
import { classFn } from "./class.js";
|
|
5
4
|
import { combinator } from "./combinator.js";
|
|
6
5
|
import { id } from "./id.js";
|
|
7
6
|
import { pseudo } from "./pseudo.js";
|
|
8
|
-
import { isPostcssNodeWithChildren, asPostcssNodeWithChildren, isSelectorNode, } from "./types.js";
|
|
7
|
+
import { isPostcssNodeWithChildren, asPostcssNodeWithChildren, isSelectorNode, isPseudoNode, isIdentifierNode, isAttributeNode, } from "./types.js";
|
|
9
8
|
export * from "./types.js";
|
|
10
9
|
const noopFn = async (state) => state;
|
|
11
10
|
const selectors = {
|
|
12
11
|
attribute,
|
|
13
|
-
|
|
12
|
+
/* c8 ignore start */
|
|
13
|
+
class: async (state) => {
|
|
14
|
+
throw error('Unsupported selector', { found: state.current });
|
|
15
|
+
},
|
|
16
|
+
/* c8 ignore end */
|
|
14
17
|
combinator,
|
|
15
18
|
comment: noopFn,
|
|
16
19
|
id,
|
|
@@ -82,6 +85,7 @@ const securitySelectors = new Set([
|
|
|
82
85
|
':abandoned',
|
|
83
86
|
':confused',
|
|
84
87
|
':cve',
|
|
88
|
+
':cwe',
|
|
85
89
|
':debug',
|
|
86
90
|
':deprecated',
|
|
87
91
|
':dynamic',
|
|
@@ -96,6 +100,7 @@ const securitySelectors = new Set([
|
|
|
96
100
|
':network',
|
|
97
101
|
':obfuscated',
|
|
98
102
|
':scanned',
|
|
103
|
+
':score',
|
|
99
104
|
':scripts',
|
|
100
105
|
':sev',
|
|
101
106
|
':severity',
|
|
@@ -111,9 +116,18 @@ const securitySelectors = new Set([
|
|
|
111
116
|
':unpopular',
|
|
112
117
|
':unstable',
|
|
113
118
|
]);
|
|
119
|
+
const setMethodToJSON = (node) => {
|
|
120
|
+
const { toJSON } = node;
|
|
121
|
+
const insights = node.insights;
|
|
122
|
+
node.toJSON = () => ({
|
|
123
|
+
...toJSON.call(node),
|
|
124
|
+
insights,
|
|
125
|
+
});
|
|
126
|
+
};
|
|
114
127
|
export class Query {
|
|
115
128
|
#cache;
|
|
116
129
|
#graph;
|
|
130
|
+
#retries;
|
|
117
131
|
#specOptions;
|
|
118
132
|
#securityArchive;
|
|
119
133
|
/**
|
|
@@ -129,12 +143,96 @@ export class Query {
|
|
|
129
143
|
}
|
|
130
144
|
return false;
|
|
131
145
|
}
|
|
132
|
-
constructor({ graph, specOptions, securityArchive }) {
|
|
146
|
+
constructor({ graph, retries, specOptions, securityArchive, }) {
|
|
133
147
|
this.#cache = new Map();
|
|
134
148
|
this.#graph = graph;
|
|
149
|
+
this.#retries = retries ?? 3;
|
|
135
150
|
this.#specOptions = specOptions;
|
|
136
151
|
this.#securityArchive = securityArchive;
|
|
137
152
|
}
|
|
153
|
+
#getQueryResponseEdges(_edges) {
|
|
154
|
+
return Array.from(_edges);
|
|
155
|
+
}
|
|
156
|
+
#getQueryResponseNodes(_nodes) {
|
|
157
|
+
const nodes = Array.from(_nodes);
|
|
158
|
+
for (const node of nodes) {
|
|
159
|
+
const securityArchiveEntry = this.#securityArchive?.get(node.id);
|
|
160
|
+
// if a security archive entry is not found then the insights object
|
|
161
|
+
// should just be empty with scanned=false
|
|
162
|
+
if (!securityArchiveEntry) {
|
|
163
|
+
node.insights = {
|
|
164
|
+
scanned: false,
|
|
165
|
+
};
|
|
166
|
+
setMethodToJSON(node);
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
// if a security archive entry is found then we can populate the insights
|
|
170
|
+
node.insights = {
|
|
171
|
+
scanned: true,
|
|
172
|
+
score: securityArchiveEntry.score,
|
|
173
|
+
abandoned: securityArchiveEntry.alerts.some(i => i.type === 'missingAuthor'),
|
|
174
|
+
confused: securityArchiveEntry.alerts.some(i => i.type === 'manifestConfusion'),
|
|
175
|
+
cve: securityArchiveEntry.alerts
|
|
176
|
+
.filter(i => i.props?.cveId)
|
|
177
|
+
// can not be undefined because of the filter above but TS doesn't know that
|
|
178
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-non-null-asserted-optional-chain
|
|
179
|
+
.map(i => i.props?.cveId),
|
|
180
|
+
cwe: Array.from(new Set(securityArchiveEntry.alerts
|
|
181
|
+
.filter(i => i.props?.cveId)
|
|
182
|
+
.flatMap(i => i.props?.cwes?.map(j => j.id)))),
|
|
183
|
+
debug: securityArchiveEntry.alerts.some(i => i.type === 'debugAccess'),
|
|
184
|
+
deprecated: securityArchiveEntry.alerts.some(i => i.type === 'deprecated'),
|
|
185
|
+
dynamic: securityArchiveEntry.alerts.some(i => i.type === 'dynamicRequire'),
|
|
186
|
+
entropic: securityArchiveEntry.alerts.some(i => i.type === 'highEntropyStrings'),
|
|
187
|
+
env: securityArchiveEntry.alerts.some(i => i.type === 'envVars'),
|
|
188
|
+
eval: securityArchiveEntry.alerts.some(i => i.type === 'usesEval'),
|
|
189
|
+
fs: securityArchiveEntry.alerts.some(i => i.type === 'filesystemAccess'),
|
|
190
|
+
license: {
|
|
191
|
+
unlicensed: securityArchiveEntry.alerts.some(i => i.type === 'explicitlyUnlicensedItem'),
|
|
192
|
+
misc: securityArchiveEntry.alerts.some(i => i.type === 'miscLicenseIssues'),
|
|
193
|
+
restricted: securityArchiveEntry.alerts.some(i => i.type === 'nonpermissiveLicense'),
|
|
194
|
+
ambiguous: securityArchiveEntry.alerts.some(i => i.type === 'ambiguousClassifier'),
|
|
195
|
+
copyleft: securityArchiveEntry.alerts.some(i => i.type === 'copyleftLicense'),
|
|
196
|
+
unknown: securityArchiveEntry.alerts.some(i => i.type === 'unidentifiedLicense'),
|
|
197
|
+
none: securityArchiveEntry.alerts.some(i => i.type === 'noLicenseFound'),
|
|
198
|
+
exception: securityArchiveEntry.alerts.some(i => i.type === 'licenseException'),
|
|
199
|
+
},
|
|
200
|
+
malware: {
|
|
201
|
+
low: securityArchiveEntry.alerts.some(i => i.type === 'gptAnomaly'),
|
|
202
|
+
medium: securityArchiveEntry.alerts.some(i => i.type === 'gptSecurity'),
|
|
203
|
+
high: securityArchiveEntry.alerts.some(i => i.type === 'gptMalware'),
|
|
204
|
+
critical: securityArchiveEntry.alerts.some(i => i.type === 'malware'),
|
|
205
|
+
},
|
|
206
|
+
minified: securityArchiveEntry.alerts.some(i => i.type === 'minifiedFile'),
|
|
207
|
+
native: securityArchiveEntry.alerts.some(i => i.type === 'hasNativeCode'),
|
|
208
|
+
network: securityArchiveEntry.alerts.some(i => i.type === 'networkAccess'),
|
|
209
|
+
obfuscated: securityArchiveEntry.alerts.some(i => i.type === 'obfuscatedFile'),
|
|
210
|
+
scripts: securityArchiveEntry.alerts.some(i => i.type === 'installScripts'),
|
|
211
|
+
severity: {
|
|
212
|
+
low: securityArchiveEntry.alerts.some(i => i.type === 'mildCVE'),
|
|
213
|
+
medium: securityArchiveEntry.alerts.some(i => i.type === 'potentialVulnerability'),
|
|
214
|
+
high: securityArchiveEntry.alerts.some(i => i.type === 'cve'),
|
|
215
|
+
critical: securityArchiveEntry.alerts.some(i => i.type === 'criticalCVE'),
|
|
216
|
+
},
|
|
217
|
+
shell: securityArchiveEntry.alerts.some(i => i.type === 'shellAccess'),
|
|
218
|
+
shrinkwrap: securityArchiveEntry.alerts.some(i => i.type === 'shrinkwrap'),
|
|
219
|
+
squat: {
|
|
220
|
+
medium: securityArchiveEntry.alerts.some(i => i.type === 'gptDidYouMean'),
|
|
221
|
+
critical: securityArchiveEntry.alerts.some(i => i.type === 'didYouMean'),
|
|
222
|
+
},
|
|
223
|
+
suspicious: securityArchiveEntry.alerts.some(i => i.type === 'suspiciousStarActivity'),
|
|
224
|
+
tracker: securityArchiveEntry.alerts.some(i => i.type === 'telemetry'),
|
|
225
|
+
trivial: securityArchiveEntry.alerts.some(i => i.type === 'trivialPackage'),
|
|
226
|
+
undesirable: securityArchiveEntry.alerts.some(i => i.type === 'troll'),
|
|
227
|
+
unknown: securityArchiveEntry.alerts.some(i => i.type === 'newAuthor'),
|
|
228
|
+
unmaintained: securityArchiveEntry.alerts.some(i => i.type === 'unmaintained'),
|
|
229
|
+
unpopular: securityArchiveEntry.alerts.some(i => i.type === 'unpopularPackage'),
|
|
230
|
+
unstable: securityArchiveEntry.alerts.some(i => i.type === 'unstableOwnership'),
|
|
231
|
+
};
|
|
232
|
+
setMethodToJSON(node);
|
|
233
|
+
}
|
|
234
|
+
return nodes;
|
|
235
|
+
}
|
|
138
236
|
/**
|
|
139
237
|
* Search the graph for nodes and edges that match the given query.
|
|
140
238
|
*/
|
|
@@ -156,7 +254,7 @@ export class Query {
|
|
|
156
254
|
});
|
|
157
255
|
signal?.throwIfAborted();
|
|
158
256
|
},
|
|
159
|
-
current:
|
|
257
|
+
current: parse(query),
|
|
160
258
|
initial: {
|
|
161
259
|
nodes: new Set(nodes),
|
|
162
260
|
edges: new Set(edges),
|
|
@@ -166,17 +264,79 @@ export class Query {
|
|
|
166
264
|
edges: new Set(),
|
|
167
265
|
},
|
|
168
266
|
partial: { nodes, edges },
|
|
267
|
+
retries: this.#retries,
|
|
169
268
|
signal,
|
|
170
269
|
securityArchive: this.#securityArchive,
|
|
171
270
|
specOptions: this.#specOptions,
|
|
172
271
|
walk,
|
|
173
272
|
});
|
|
174
273
|
const res = {
|
|
175
|
-
edges:
|
|
176
|
-
nodes:
|
|
274
|
+
edges: this.#getQueryResponseEdges(collect.edges),
|
|
275
|
+
nodes: this.#getQueryResponseNodes(collect.nodes),
|
|
177
276
|
};
|
|
178
277
|
this.#cache.set(query, res);
|
|
179
278
|
return res;
|
|
180
279
|
}
|
|
280
|
+
/**
|
|
281
|
+
* Parses a query string in order to retrieve an array of tokens.
|
|
282
|
+
*/
|
|
283
|
+
static getQueryTokens(query) {
|
|
284
|
+
if (!query)
|
|
285
|
+
return [];
|
|
286
|
+
const tokens = [];
|
|
287
|
+
const ast = (q) => {
|
|
288
|
+
try {
|
|
289
|
+
return parse(q);
|
|
290
|
+
}
|
|
291
|
+
catch (_e) {
|
|
292
|
+
return ast(q.slice(0, -1));
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
const processNode = (node) => {
|
|
296
|
+
for (const key of selectorsMap.keys()) {
|
|
297
|
+
if (node.type === key && node.type !== 'root') {
|
|
298
|
+
let token = `${node.spaces.before}${node.value}${node.spaces.after}`;
|
|
299
|
+
if (isIdentifierNode(node)) {
|
|
300
|
+
token = `${node.spaces.before}#${node.value}${node.spaces.after}`;
|
|
301
|
+
}
|
|
302
|
+
else if (isSelectorNode(node)) {
|
|
303
|
+
token = `${node.spaces.before},${node.spaces.after}`;
|
|
304
|
+
}
|
|
305
|
+
else if (isAttributeNode(node)) {
|
|
306
|
+
token = String(node.source?.start?.column &&
|
|
307
|
+
node.source.end?.column &&
|
|
308
|
+
`${node.spaces.before}${query.slice(node.source.start.column - 1, node.source.end.column)}${node.spaces.after}`);
|
|
309
|
+
}
|
|
310
|
+
if (isPostcssNodeWithChildren(node) &&
|
|
311
|
+
isPseudoNode(node) &&
|
|
312
|
+
node.nodes.length) {
|
|
313
|
+
token = String(token.split('(')[0]);
|
|
314
|
+
token += '(';
|
|
315
|
+
}
|
|
316
|
+
if (!isSelectorNode(node) ||
|
|
317
|
+
node.parent?.nodes.indexOf(node) !== 0) {
|
|
318
|
+
tokens.push({
|
|
319
|
+
...node,
|
|
320
|
+
token,
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
if (isPostcssNodeWithChildren(node)) {
|
|
326
|
+
for (const child of node.nodes) {
|
|
327
|
+
processNode(child);
|
|
328
|
+
}
|
|
329
|
+
if (isPseudoNode(node) && node.nodes.length) {
|
|
330
|
+
tokens.push({
|
|
331
|
+
...node,
|
|
332
|
+
token: ')' + node.spaces.after,
|
|
333
|
+
type: 'pseudo',
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
processNode(ast(query));
|
|
339
|
+
return tokens;
|
|
340
|
+
}
|
|
181
341
|
}
|
|
182
342
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAA;AAI3C,OAAO,qBAAqB,MAAM,yBAAyB,CAAA;AAC3D,OAAO,EAAE,SAAS,EAAE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CAAA;gBACxB,CAAC,CAAC,CAAA;gBACF,MAAM,EAAE,cAAc,EAAE,CAAA;YAC1B,CAAC;YACD,OAAO,EAAE,qBAAqB,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC;YAC/C,OAAO,EAAE;gBACP,KAAK,EAAE,IAAI,GAAG,CAAC,KAAK,CAAC;gBACrB,KAAK,EAAE,IAAI,GAAG,CAAC,KAAK,CAAC;aACtB;YACD,OAAO,EAAE;gBACP,KAAK,EAAE,IAAI,GAAG,EAAY;gBAC1B,KAAK,EAAE,IAAI,GAAG,EAAY;aAC3B;YACD,OAAO,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE;YACzB,MAAM;YACN,eAAe,EAAE,IAAI,CAAC,gBAAgB;YACtC,WAAW,EAAE,IAAI,CAAC,YAAY;YAC9B,IAAI;SACL,CAAC,CAAA;QAEF,MAAM,GAAG,GAAkB;YACzB,KAAK,EAAE,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC;YAChC,KAAK,EAAE,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC;SACjC,CAAA;QACD,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,CAAA;QAC3B,OAAO,GAAG,CAAA;IACZ,CAAC;CACF","sourcesContent":["import { error } from '@vltpkg/error-cause'\nimport type { EdgeLike, GraphLike, NodeLike } from '@vltpkg/graph'\nimport type { SpecOptions } from '@vltpkg/spec/browser'\nimport type { SecurityArchiveLike } from '@vltpkg/security-archive'\nimport postcssSelectorParser from 'postcss-selector-parser'\nimport { attribute } from './attribute.ts'\nimport { classFn } from './class.ts'\nimport { combinator } from './combinator.ts'\nimport { id } from './id.ts'\nimport { pseudo } from './pseudo.ts'\nimport {\n isPostcssNodeWithChildren,\n asPostcssNodeWithChildren,\n isSelectorNode,\n} from './types.ts'\nimport type {\n PostcssNodeWithChildren,\n ParserState,\n ParserFn,\n QueryResponse,\n} from './types.ts'\n\nexport * from './types.ts'\n\nconst noopFn = async (state: ParserState) => state\n\nconst selectors = {\n attribute,\n class: classFn,\n combinator,\n comment: noopFn,\n id,\n nesting: noopFn,\n pseudo,\n root: noopFn,\n selector: async (state: ParserState) => {\n state.partial.nodes = new Set(state.initial.nodes)\n state.partial.edges = new Set(state.initial.edges)\n return state\n },\n string: async (state: ParserState) => {\n throw error('Unsupported selector', { found: state.current })\n },\n tag: async (state: ParserState) => {\n if (state.current.value !== '{' && state.current.value !== '}') {\n throw error('Unsupported selector', { found: state.current })\n }\n return state\n },\n universal: noopFn,\n}\nconst selectorsMap = new Map<string, ParserFn>(\n Object.entries(selectors),\n)\n\nexport const walk = async (\n state: ParserState,\n): Promise<ParserState> => {\n await state.cancellable()\n\n const parserFn = selectorsMap.get(state.current.type)\n\n if (!parserFn) {\n if (state.loose) {\n return state\n }\n\n throw error(\n `Missing parser for query node: ${state.current.type}`,\n {\n found: state.current,\n },\n )\n }\n state = await parserFn(state)\n\n // pseudo selectors handle their own sub selectors\n if (\n isPostcssNodeWithChildren(state.current) &&\n state.current.type !== 'pseudo'\n ) {\n const node: PostcssNodeWithChildren = asPostcssNodeWithChildren(\n state.current,\n )\n\n if (node.nodes.length) {\n for (let i = 0; i < node.nodes.length; i++) {\n const current = node.nodes[i]\n /* c8 ignore next -- impossible but TS doesn't know that */\n if (!current) continue\n\n const childState: ParserState = {\n ...state,\n current,\n next: node.nodes[i + 1],\n prev: node.nodes[i - 1],\n }\n state = await walk(childState)\n }\n }\n\n if (isSelectorNode(node)) {\n for (const edge of state.partial.edges) {\n state.collect.edges.add(edge)\n }\n for (const node of state.partial.nodes) {\n state.collect.nodes.add(node)\n }\n }\n }\n return state\n}\n\nexport type QueryOptions = {\n graph: GraphLike\n specOptions: SpecOptions\n securityArchive: SecurityArchiveLike | undefined\n}\n\n// A list of known security selectors that rely on\n// data from the security-archive in order to work\nconst securitySelectors = new Set([\n ':abandoned',\n ':confused',\n ':cve',\n ':debug',\n ':deprecated',\n ':dynamic',\n ':entropic',\n ':env',\n ':eval',\n ':fs',\n ':license',\n ':malware',\n ':minified',\n ':native',\n ':network',\n ':obfuscated',\n ':scanned',\n ':scripts',\n ':sev',\n ':severity',\n ':shell',\n ':shrinkwrap',\n ':squat',\n ':suspicious',\n ':tracker',\n ':trivial',\n ':undesirable',\n ':unknown',\n ':unmaintained',\n ':unpopular',\n ':unstable',\n])\n\nexport class Query {\n #cache: Map<string, QueryResponse>\n #graph: GraphLike\n #specOptions: SpecOptions\n #securityArchive: SecurityArchiveLike | undefined\n\n /**\n * Helper method to determine if a given query string is using any of\n * the known security selectors. This is useful so that operations can\n * skip hydrating the security archive if it's not needed.\n */\n static hasSecuritySelectors(query: string): boolean {\n for (const selector of securitySelectors) {\n if (query.includes(selector)) {\n return true\n }\n }\n return false\n }\n\n constructor({ graph, specOptions, securityArchive }: QueryOptions) {\n this.#cache = new Map()\n this.#graph = graph\n this.#specOptions = specOptions\n this.#securityArchive = securityArchive\n }\n\n /**\n * Search the graph for nodes and edges that match the given query.\n */\n async search(\n query: string,\n signal?: AbortSignal,\n ): Promise<QueryResponse> {\n if (!query) return { edges: [], nodes: [] }\n\n const cachedResult = this.#cache.get(query)\n if (cachedResult) {\n return cachedResult\n }\n\n const nodes = new Set<NodeLike>(\n Array.from(this.#graph.nodes.values()),\n )\n const edges = new Set<EdgeLike>(Array.from(this.#graph.edges))\n\n // builds initial state and walks over it,\n // retrieving the collected result\n const { collect } = await walk({\n cancellable: async () => {\n await new Promise(resolve => {\n setTimeout(resolve, 0)\n })\n signal?.throwIfAborted()\n },\n current: postcssSelectorParser().astSync(query),\n initial: {\n nodes: new Set(nodes),\n edges: new Set(edges),\n },\n collect: {\n nodes: new Set<NodeLike>(),\n edges: new Set<EdgeLike>(),\n },\n partial: { nodes, edges },\n signal,\n securityArchive: this.#securityArchive,\n specOptions: this.#specOptions,\n walk,\n })\n\n const res: QueryResponse = {\n edges: Array.from(collect.edges),\n nodes: Array.from(collect.nodes),\n }\n this.#cache.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AI,CAAC,KAAK,CAAC,EACtC,CAAC;wBACD,MAAM,CAAC,IAAI,CAAC;4BACV,GAAG,IAAI;4BACP,KAAK;yBACiB,CAAC,CAAA;oBAC3B,CAAC;gBACH,CAAC;YACH,CAAC;YACD,IAAI,yBAAyB,CAAC,IAAI,CAAC,EAAE,CAAC;gBACpC,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;oBAC/B,WAAW,CAAC,KAAK,CAAC,CAAA;gBACpB,CAAC;gBACD,IAAI,YAAY,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC;oBAC5C,MAAM,CAAC,IAAI,CAAC;wBACV,GAAG,IAAI;wBACP,KAAK,EAAE,GAAG,GAAG,IAAI,CAAC,MAAM,CAAC,KAAK;wBAC9B,IAAI,EAAE,QAAQ;qBACQ,CAAC,CAAA;gBAC3B,CAAC;YACH,CAAC;QACH,CAAC,CAAA;QAED,WAAW,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,CAAA;QACvB,OAAO,MAAM,CAAA;IACf,CAAC;CACF","sourcesContent":["import { error } from '@vltpkg/error-cause'\nimport type { EdgeLike, GraphLike, NodeLike } from '@vltpkg/graph'\nimport type { SpecOptions } from '@vltpkg/spec/browser'\nimport type { SecurityArchiveLike } from '@vltpkg/security-archive'\nimport { parse } from './parser.ts'\nimport { attribute } from './attribute.ts'\nimport { combinator } from './combinator.ts'\nimport { id } from './id.ts'\nimport { pseudo } from './pseudo.ts'\nimport {\n isPostcssNodeWithChildren,\n asPostcssNodeWithChildren,\n isSelectorNode,\n isPseudoNode,\n isIdentifierNode,\n isAttributeNode,\n} from './types.ts'\nimport type {\n PostcssNode,\n ParsedSelectorToken,\n PostcssNodeWithChildren,\n ParserState,\n ParserFn,\n QueryResponse,\n QueryResponseNode,\n QueryResponseEdge,\n} from './types.ts'\n\nexport * from './types.ts'\n\nconst noopFn = async (state: ParserState) => state\n\nconst selectors = {\n attribute,\n /* c8 ignore start */\n class: async (state: ParserState) => {\n throw error('Unsupported selector', { found: state.current })\n },\n /* c8 ignore end */\n combinator,\n comment: noopFn,\n id,\n nesting: noopFn,\n pseudo,\n root: noopFn,\n selector: async (state: ParserState) => {\n state.partial.nodes = new Set(state.initial.nodes)\n state.partial.edges = new Set(state.initial.edges)\n return state\n },\n string: async (state: ParserState) => {\n throw error('Unsupported selector', { found: state.current })\n },\n tag: async (state: ParserState) => {\n if (state.current.value !== '{' && state.current.value !== '}') {\n throw error('Unsupported selector', { found: state.current })\n }\n return state\n },\n universal: noopFn,\n}\nconst selectorsMap = new Map<string, ParserFn>(\n Object.entries(selectors),\n)\n\nexport const walk = async (\n state: ParserState,\n): Promise<ParserState> => {\n await state.cancellable()\n\n const parserFn = selectorsMap.get(state.current.type)\n\n if (!parserFn) {\n if (state.loose) {\n return state\n }\n\n throw error(\n `Missing parser for query node: ${state.current.type}`,\n {\n found: state.current,\n },\n )\n }\n state = await parserFn(state)\n\n // pseudo selectors handle their own sub selectors\n if (\n isPostcssNodeWithChildren(state.current) &&\n state.current.type !== 'pseudo'\n ) {\n const node: PostcssNodeWithChildren = asPostcssNodeWithChildren(\n state.current,\n )\n\n if (node.nodes.length) {\n for (let i = 0; i < node.nodes.length; i++) {\n const current = node.nodes[i]\n /* c8 ignore next -- impossible but TS doesn't know that */\n if (!current) continue\n\n const childState: ParserState = {\n ...state,\n current,\n next: node.nodes[i + 1],\n prev: node.nodes[i - 1],\n }\n state = await walk(childState)\n }\n }\n\n if (isSelectorNode(node)) {\n for (const edge of state.partial.edges) {\n state.collect.edges.add(edge)\n }\n for (const node of state.partial.nodes) {\n state.collect.nodes.add(node)\n }\n }\n }\n return state\n}\n\nexport type QueryOptions = {\n graph: GraphLike\n retries?: number\n specOptions: SpecOptions\n securityArchive: SecurityArchiveLike | undefined\n}\n\n// A list of known security selectors that rely on\n// data from the security-archive in order to work\nconst securitySelectors = new Set([\n ':abandoned',\n ':confused',\n ':cve',\n ':cwe',\n ':debug',\n ':deprecated',\n ':dynamic',\n ':entropic',\n ':env',\n ':eval',\n ':fs',\n ':license',\n ':malware',\n ':minified',\n ':native',\n ':network',\n ':obfuscated',\n ':scanned',\n ':score',\n ':scripts',\n ':sev',\n ':severity',\n ':shell',\n ':shrinkwrap',\n ':squat',\n ':suspicious',\n ':tracker',\n ':trivial',\n ':undesirable',\n ':unknown',\n ':unmaintained',\n ':unpopular',\n ':unstable',\n])\n\nconst setMethodToJSON = (node: QueryResponseNode) => {\n const { toJSON } = node\n const insights = node.insights\n node.toJSON = () => ({\n ...toJSON.call(node),\n insights,\n })\n}\n\nexport class Query {\n #cache: Map<string, QueryResponse>\n #graph: GraphLike\n #retries: number\n #specOptions: SpecOptions\n #securityArchive: SecurityArchiveLike | undefined\n\n /**\n * Helper method to determine if a given query string is using any of\n * the known security selectors. This is useful so that operations can\n * skip hydrating the security archive if it's not needed.\n */\n static hasSecuritySelectors(query: string): boolean {\n for (const selector of securitySelectors) {\n if (query.includes(selector)) {\n return true\n }\n }\n return false\n }\n\n constructor({\n graph,\n retries,\n specOptions,\n securityArchive,\n }: QueryOptions) {\n this.#cache = new Map()\n this.#graph = graph\n this.#retries = retries ?? 3\n this.#specOptions = specOptions\n this.#securityArchive = securityArchive\n }\n\n #getQueryResponseEdges(_edges: Set<EdgeLike>): QueryResponseEdge[] {\n return Array.from(_edges) as QueryResponseEdge[]\n }\n\n #getQueryResponseNodes(_nodes: Set<NodeLike>): QueryResponseNode[] {\n const nodes = Array.from(_nodes) as QueryResponseNode[]\n for (const node of nodes) {\n const securityArchiveEntry = this.#securityArchive?.get(node.id)\n\n // if a security archive entry is not found then the insights object\n // should just be empty with scanned=false\n if (!securityArchiveEntry) {\n node.insights = {\n scanned: false,\n }\n\n setMethodToJSON(node)\n continue\n }\n\n // if a security archive entry is found then we can populate the insights\n node.insights = {\n scanned: true,\n score: securityArchiveEntry.score,\n abandoned: securityArchiveEntry.alerts.some(\n i => i.type === 'missingAuthor',\n ),\n confused: securityArchiveEntry.alerts.some(\n i => i.type === 'manifestConfusion',\n ),\n cve: securityArchiveEntry.alerts\n .filter(i => i.props?.cveId)\n // can not be undefined because of the filter above but TS doesn't know that\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-non-null-asserted-optional-chain\n .map(i => i.props?.cveId!),\n cwe: Array.from(\n new Set(\n securityArchiveEntry.alerts\n .filter(i => i.props?.cveId)\n .flatMap(i => i.props?.cwes?.map(j => j.id)),\n ),\n ) as `CWE-${string}`[],\n debug: securityArchiveEntry.alerts.some(\n i => i.type === 'debugAccess',\n ),\n deprecated: securityArchiveEntry.alerts.some(\n i => i.type === 'deprecated',\n ),\n dynamic: securityArchiveEntry.alerts.some(\n i => i.type === 'dynamicRequire',\n ),\n entropic: securityArchiveEntry.alerts.some(\n i => i.type === 'highEntropyStrings',\n ),\n env: securityArchiveEntry.alerts.some(\n i => i.type === 'envVars',\n ),\n eval: securityArchiveEntry.alerts.some(\n i => i.type === 'usesEval',\n ),\n fs: securityArchiveEntry.alerts.some(\n i => i.type === 'filesystemAccess',\n ),\n license: {\n unlicensed: securityArchiveEntry.alerts.some(\n i => i.type === 'explicitlyUnlicensedItem',\n ),\n misc: securityArchiveEntry.alerts.some(\n i => i.type === 'miscLicenseIssues',\n ),\n restricted: securityArchiveEntry.alerts.some(\n i => i.type === 'nonpermissiveLicense',\n ),\n ambiguous: securityArchiveEntry.alerts.some(\n i => i.type === 'ambiguousClassifier',\n ),\n copyleft: securityArchiveEntry.alerts.some(\n i => i.type === 'copyleftLicense',\n ),\n unknown: securityArchiveEntry.alerts.some(\n i => i.type === 'unidentifiedLicense',\n ),\n none: securityArchiveEntry.alerts.some(\n i => i.type === 'noLicenseFound',\n ),\n exception: securityArchiveEntry.alerts.some(\n i => i.type === 'licenseException',\n ),\n },\n malware: {\n low: securityArchiveEntry.alerts.some(\n i => i.type === 'gptAnomaly',\n ),\n medium: securityArchiveEntry.alerts.some(\n i => i.type === 'gptSecurity',\n ),\n high: securityArchiveEntry.alerts.some(\n i => i.type === 'gptMalware',\n ),\n critical: securityArchiveEntry.alerts.some(\n i => i.type === 'malware',\n ),\n },\n minified: securityArchiveEntry.alerts.some(\n i => i.type === 'minifiedFile',\n ),\n native: securityArchiveEntry.alerts.some(\n i => i.type === 'hasNativeCode',\n ),\n network: securityArchiveEntry.alerts.some(\n i => i.type === 'networkAccess',\n ),\n obfuscated: securityArchiveEntry.alerts.some(\n i => i.type === 'obfuscatedFile',\n ),\n scripts: securityArchiveEntry.alerts.some(\n i => i.type === 'installScripts',\n ),\n severity: {\n low: securityArchiveEntry.alerts.some(\n i => i.type === 'mildCVE',\n ),\n medium: securityArchiveEntry.alerts.some(\n i => i.type === 'potentialVulnerability',\n ),\n high: securityArchiveEntry.alerts.some(\n i => i.type === 'cve',\n ),\n critical: securityArchiveEntry.alerts.some(\n i => i.type === 'criticalCVE',\n ),\n },\n shell: securityArchiveEntry.alerts.some(\n i => i.type === 'shellAccess',\n ),\n shrinkwrap: securityArchiveEntry.alerts.some(\n i => i.type === 'shrinkwrap',\n ),\n squat: {\n medium: securityArchiveEntry.alerts.some(\n i => i.type === 'gptDidYouMean',\n ),\n critical: securityArchiveEntry.alerts.some(\n i => i.type === 'didYouMean',\n ),\n },\n suspicious: securityArchiveEntry.alerts.some(\n i => i.type === 'suspiciousStarActivity',\n ),\n tracker: securityArchiveEntry.alerts.some(\n i => i.type === 'telemetry',\n ),\n trivial: securityArchiveEntry.alerts.some(\n i => i.type === 'trivialPackage',\n ),\n undesirable: securityArchiveEntry.alerts.some(\n i => i.type === 'troll',\n ),\n unknown: securityArchiveEntry.alerts.some(\n i => i.type === 'newAuthor',\n ),\n unmaintained: securityArchiveEntry.alerts.some(\n i => i.type === 'unmaintained',\n ),\n unpopular: securityArchiveEntry.alerts.some(\n i => i.type === 'unpopularPackage',\n ),\n unstable: securityArchiveEntry.alerts.some(\n i => i.type === 'unstableOwnership',\n ),\n }\n\n setMethodToJSON(node)\n }\n return nodes\n }\n\n /**\n * Search the graph for nodes and edges that match the given query.\n */\n async search(\n query: string,\n signal?: AbortSignal,\n ): Promise<QueryResponse> {\n if (!query) return { edges: [], nodes: [] }\n\n const cachedResult = this.#cache.get(query)\n if (cachedResult) {\n return cachedResult\n }\n\n const nodes = new Set<NodeLike>(\n Array.from(this.#graph.nodes.values()),\n )\n const edges = new Set<EdgeLike>(Array.from(this.#graph.edges))\n\n // builds initial state and walks over it,\n // retrieving the collected result\n const { collect } = await walk({\n cancellable: async () => {\n await new Promise(resolve => {\n setTimeout(resolve, 0)\n })\n signal?.throwIfAborted()\n },\n current: parse(query),\n initial: {\n nodes: new Set(nodes),\n edges: new Set(edges),\n },\n collect: {\n nodes: new Set<NodeLike>(),\n edges: new Set<EdgeLike>(),\n },\n partial: { nodes, edges },\n retries: this.#retries,\n signal,\n securityArchive: this.#securityArchive,\n specOptions: this.#specOptions,\n walk,\n })\n\n const res: QueryResponse = {\n edges: this.#getQueryResponseEdges(collect.edges),\n nodes: this.#getQueryResponseNodes(collect.nodes),\n }\n this.#cache.set(query, res)\n return res\n }\n\n /**\n * Parses a query string in order to retrieve an array of tokens.\n */\n static getQueryTokens(query: string): ParsedSelectorToken[] {\n if (!query) return []\n\n const tokens: ParsedSelectorToken[] = []\n\n const ast = (q: string) => {\n try {\n return parse(q)\n } catch (_e) {\n return ast(q.slice(0, -1))\n }\n }\n\n const processNode = (node: PostcssNode) => {\n for (const key of selectorsMap.keys()) {\n if (node.type === key && node.type !== 'root') {\n let token = `${node.spaces.before}${node.value}${node.spaces.after}`\n\n if (isIdentifierNode(node)) {\n token = `${node.spaces.before}#${node.value}${node.spaces.after}`\n } else if (isSelectorNode(node)) {\n token = `${node.spaces.before},${node.spaces.after}`\n } else if (isAttributeNode(node)) {\n token = String(\n node.source?.start?.column &&\n node.source.end?.column &&\n `${node.spaces.before}${query.slice(node.source.start.column - 1, node.source.end.column)}${node.spaces.after}`,\n )\n }\n\n if (\n isPostcssNodeWithChildren(node) &&\n isPseudoNode(node) &&\n node.nodes.length\n ) {\n token = String(token.split('(')[0])\n token += '('\n }\n\n if (\n !isSelectorNode(node) ||\n node.parent?.nodes.indexOf(node) !== 0\n ) {\n tokens.push({\n ...node,\n token,\n } as ParsedSelectorToken)\n }\n }\n }\n if (isPostcssNodeWithChildren(node)) {\n for (const child of node.nodes) {\n processNode(child)\n }\n if (isPseudoNode(node) && node.nodes.length) {\n tokens.push({\n ...node,\n token: ')' + node.spaces.after,\n type: 'pseudo',\n } as ParsedSelectorToken)\n }\n }\n }\n\n processNode(ast(query))\n return tokens\n }\n}\n"]}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Root } from 'postcss-selector-parser';
|
|
2
|
+
/**
|
|
3
|
+
* Escapes forward slashes in specific patterns matching @scoped/name paths
|
|
4
|
+
* This will allow usage of unescaped forward slashes necessary for scoped
|
|
5
|
+
* package names in the id selector.
|
|
6
|
+
*/
|
|
7
|
+
export declare const escapeScopedNamesSlashes: (query: string) => string;
|
|
8
|
+
export declare const escapeDots: (query: string) => string;
|
|
9
|
+
export declare const unescapeDots: (query: string) => string;
|
|
10
|
+
/**
|
|
11
|
+
* Parses a CSS selector string into an AST
|
|
12
|
+
* Handles escaping of forward slashes in specific patterns
|
|
13
|
+
*/
|
|
14
|
+
export declare const parse: (query: string) => Root;
|
|
15
|
+
//# sourceMappingURL=parser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../../src/parser.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAU,IAAI,EAAE,MAAM,yBAAyB,CAAA;AAS3D;;;;GAIG;AACH,eAAO,MAAM,wBAAwB,UAAW,MAAM,KAAG,MAItD,CAAA;AAEH,eAAO,MAAM,UAAU,UAAW,MAAM,KAAG,MACb,CAAA;AAE9B,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,MACf,CAAA;AAgB9B;;;GAGG;AACH,eAAO,MAAM,KAAK,UAAW,MAAM,KAAG,IA0ErC,CAAA"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import postcssSelectorParser from 'postcss-selector-parser';
|
|
2
|
+
import { asSelectorNode, isCombinatorNode, isPseudoNode, isTagNode, } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Escapes forward slashes in specific patterns matching @scoped/name paths
|
|
5
|
+
* This will allow usage of unescaped forward slashes necessary for scoped
|
|
6
|
+
* package names in the id selector.
|
|
7
|
+
*/
|
|
8
|
+
export const escapeScopedNamesSlashes = (query) => query.replace(/(#@(\w|-|\.)+)\//gm, (_, scope) => `${scope}\\/`);
|
|
9
|
+
export const escapeDots = (query) => query.replaceAll('.', '\\.');
|
|
10
|
+
export const unescapeDots = (query) => query.replaceAll('\\.', '.');
|
|
11
|
+
const pseudoCleanUpNeeded = new Set([
|
|
12
|
+
':published',
|
|
13
|
+
':score',
|
|
14
|
+
':malware',
|
|
15
|
+
':severity',
|
|
16
|
+
':sev',
|
|
17
|
+
':squat',
|
|
18
|
+
':semver',
|
|
19
|
+
':v',
|
|
20
|
+
]);
|
|
21
|
+
const hasParamsToEscape = (node) => pseudoCleanUpNeeded.has(node.value);
|
|
22
|
+
/**
|
|
23
|
+
* Parses a CSS selector string into an AST
|
|
24
|
+
* Handles escaping of forward slashes in specific patterns
|
|
25
|
+
*/
|
|
26
|
+
export const parse = (query) => {
|
|
27
|
+
const escapedQuery = escapeDots(escapeScopedNamesSlashes(query));
|
|
28
|
+
const transformAst = (root) => {
|
|
29
|
+
root.walk((node) => {
|
|
30
|
+
// clean up the escaped dots
|
|
31
|
+
if (node.value && typeof node.value === 'string') {
|
|
32
|
+
node.value = unescapeDots(node.value);
|
|
33
|
+
}
|
|
34
|
+
if (isPseudoNode(node) && hasParamsToEscape(node)) {
|
|
35
|
+
// these are pseudo nodes that should only take strings as
|
|
36
|
+
// parameters, so in this preparse step we clean up anything
|
|
37
|
+
// that was recognized as a postcss node and transform that
|
|
38
|
+
// into something that can be most likely parsed as a string
|
|
39
|
+
for (const n of node.nodes) {
|
|
40
|
+
// the parameters have a selector node that wraps them up
|
|
41
|
+
const selector = asSelectorNode(n);
|
|
42
|
+
selector.nodes.forEach((currentNode, index, arr) => {
|
|
43
|
+
// get the next node, we'll update it later
|
|
44
|
+
const nextNode = arr[index + 1];
|
|
45
|
+
// if the current node is a combinator node, we'll need to
|
|
46
|
+
// escape it, we do so by removing the node entirely and
|
|
47
|
+
// updating the contents of the next node with its value
|
|
48
|
+
if (isCombinatorNode(currentNode) &&
|
|
49
|
+
isTagNode(nextNode)) {
|
|
50
|
+
nextNode.value = `${currentNode.spaces.before}${currentNode.value}${currentNode.spaces.after}${nextNode.value}`;
|
|
51
|
+
// make sure to also update the source position
|
|
52
|
+
// references, those are used by the syntax highlighter
|
|
53
|
+
if (nextNode.source?.start?.line &&
|
|
54
|
+
currentNode.source?.start?.line) {
|
|
55
|
+
nextNode.source.start.line =
|
|
56
|
+
currentNode.source.start.line;
|
|
57
|
+
}
|
|
58
|
+
if (nextNode.source?.start?.column &&
|
|
59
|
+
currentNode.source?.start?.column) {
|
|
60
|
+
nextNode.source.start.column =
|
|
61
|
+
currentNode.source.start.column;
|
|
62
|
+
}
|
|
63
|
+
// removes the current node from the selector node
|
|
64
|
+
arr.splice(index, 1);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
// after removing combinator nodes, if we end up with multiple
|
|
68
|
+
// tags in the selector node, we need to smush them together
|
|
69
|
+
selector.nodes.reduce((acc, currentNode) => {
|
|
70
|
+
if (currentNode === acc)
|
|
71
|
+
return acc;
|
|
72
|
+
acc.value = `${acc.value}${currentNode.spaces.before}${currentNode.value}${currentNode.spaces.after}`;
|
|
73
|
+
// make sure to also update the source position refs
|
|
74
|
+
if (currentNode.source?.end?.line &&
|
|
75
|
+
acc.source?.end?.line) {
|
|
76
|
+
acc.source.end.line = currentNode.source.end.line;
|
|
77
|
+
}
|
|
78
|
+
if (currentNode.source?.end?.column &&
|
|
79
|
+
acc.source?.end?.column) {
|
|
80
|
+
acc.source.end.column = currentNode.source.end.column;
|
|
81
|
+
}
|
|
82
|
+
return acc;
|
|
83
|
+
}, selector.first);
|
|
84
|
+
// the selector wrapper node should have a single node
|
|
85
|
+
selector.nodes.length = 1;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
return postcssSelectorParser(transformAst).astSync(escapedQuery);
|
|
91
|
+
};
|
|
92
|
+
//# sourceMappingURL=parser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parser.js","sourceRoot":"","sources":["../../src/parser.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,MAAM,yBAAyB,CAAA;AAE3D,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,YAAY,EACZ,SAAS,GACV,MAAM,YAAY,CAAA;AAGnB;;;;GAIG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG,CAAC,KAAa,EAAU,EAAE,CAChE,KAAK,CAAC,OAAO,CACX,oBAAoB,EACpB,CAAC,CAAC,EAAE,KAAa,EAAE,EAAE,CAAC,GAAG,KAAK,KAAK,CACpC,CAAA;AAEH,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,KAAa,EAAU,EAAE,CAClD,KAAK,CAAC,UAAU,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;AAE9B,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,KAAa,EAAU,EAAE,CACpD,KAAK,CAAC,UAAU,CAAC,KAAK,EAAE,GAAG,CAAC,CAAA;AAE9B,MAAM,mBAAmB,GAAG,IAAI,GAAG,CAAC;IAClC,YAAY;IACZ,QAAQ;IACR,UAAU;IACV,WAAW;IACX,MAAM;IACN,QAAQ;IACR,SAAS;IACT,IAAI;CACL,CAAC,CAAA;AAEF,MAAM,iBAAiB,GAAG,CAAC,IAAY,EAAE,EAAE,CACzC,mBAAmB,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAA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AC/B,GAAG,CAAC,MAAM,EAAE,GAAG,EAAE,MAAM,EACvB,CAAC;4BACD,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,MAAM,GAAG,WAAW,CAAC,MAAM,CAAC,GAAG,CAAC,MAAM,CAAA;wBACvD,CAAC;wBACD,OAAO,GAAG,CAAA;oBACZ,CAAC,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAA;oBAClB,sDAAsD;oBACtD,QAAQ,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAA;gBAC3B,CAAC;YACH,CAAC;QACH,CAAC,CAAC,CAAA;IACJ,CAAC,CAAA;IACD,OAAO,qBAAqB,CAAC,YAAY,CAAC,CAAC,OAAO,CAAC,YAAY,CAAC,CAAA;AAClE,CAAC,CAAA","sourcesContent":["import postcssSelectorParser from 'postcss-selector-parser'\nimport type { Pseudo, Root } from 'postcss-selector-parser'\nimport {\n asSelectorNode,\n isCombinatorNode,\n isPseudoNode,\n isTagNode,\n} from './types.ts'\nimport type { PostcssNode } from './types.ts'\n\n/**\n * Escapes forward slashes in specific patterns matching @scoped/name paths\n * This will allow usage of unescaped forward slashes necessary for scoped\n * package names in the id selector.\n */\nexport const escapeScopedNamesSlashes = (query: string): string =>\n query.replace(\n /(#@(\\w|-|\\.)+)\\//gm,\n (_, scope: string) => `${scope}\\\\/`,\n )\n\nexport const escapeDots = (query: string): string =>\n query.replaceAll('.', '\\\\.')\n\nexport const unescapeDots = (query: string): string =>\n query.replaceAll('\\\\.', '.')\n\nconst pseudoCleanUpNeeded = new Set([\n ':published',\n ':score',\n ':malware',\n ':severity',\n ':sev',\n ':squat',\n ':semver',\n ':v',\n])\n\nconst hasParamsToEscape = (node: Pseudo) =>\n pseudoCleanUpNeeded.has(node.value)\n\n/**\n * Parses a CSS selector string into an AST\n * Handles escaping of forward slashes in specific patterns\n */\nexport const parse = (query: string): Root => {\n const escapedQuery = escapeDots(escapeScopedNamesSlashes(query))\n const transformAst = (root: Root) => {\n root.walk((node: PostcssNode) => {\n // clean up the escaped dots\n if (node.value && typeof node.value === 'string') {\n node.value = unescapeDots(node.value)\n }\n if (isPseudoNode(node) && hasParamsToEscape(node)) {\n // these are pseudo nodes that should only take strings as\n // parameters, so in this preparse step we clean up anything\n // that was recognized as a postcss node and transform that\n // into something that can be most likely parsed as a string\n for (const n of node.nodes) {\n // the parameters have a selector node that wraps them up\n const selector = asSelectorNode(n)\n selector.nodes.forEach((currentNode, index, arr) => {\n // get the next node, we'll update it later\n const nextNode = arr[index + 1]\n // if the current node is a combinator node, we'll need to\n // escape it, we do so by removing the node entirely and\n // updating the contents of the next node with its value\n if (\n isCombinatorNode(currentNode) &&\n isTagNode(nextNode)\n ) {\n nextNode.value = `${currentNode.spaces.before}${currentNode.value}${currentNode.spaces.after}${nextNode.value}`\n // make sure to also update the source position\n // references, those are used by the syntax highlighter\n if (\n nextNode.source?.start?.line &&\n currentNode.source?.start?.line\n ) {\n nextNode.source.start.line =\n currentNode.source.start.line\n }\n if (\n nextNode.source?.start?.column &&\n currentNode.source?.start?.column\n ) {\n nextNode.source.start.column =\n currentNode.source.start.column\n }\n // removes the current node from the selector node\n arr.splice(index, 1)\n }\n })\n // after removing combinator nodes, if we end up with multiple\n // tags in the selector node, we need to smush them together\n selector.nodes.reduce((acc, currentNode) => {\n if (currentNode === acc) return acc\n acc.value = `${acc.value}${currentNode.spaces.before}${currentNode.value}${currentNode.spaces.after}`\n // make sure to also update the source position refs\n if (\n currentNode.source?.end?.line &&\n acc.source?.end?.line\n ) {\n acc.source.end.line = currentNode.source.end.line\n }\n if (\n currentNode.source?.end?.column &&\n acc.source?.end?.column\n ) {\n acc.source.end.column = currentNode.source.end.column\n }\n return acc\n }, selector.first)\n // the selector wrapper node should have a single node\n selector.nodes.length = 1\n }\n }\n })\n }\n return postcssSelectorParser(transformAst).astSync(escapedQuery)\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"attr.d.ts","sourceRoot":"","sources":["../../../src/pseudo/attr.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"attr.d.ts","sourceRoot":"","sources":["../../../src/pseudo/attr.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAO3D,MAAM,MAAM,aAAa,GAAG;IAC1B,SAAS,EAAE,MAAM,CAAA;IACjB,WAAW,EAAE,OAAO,CAAA;IACpB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,UAAU,EAAE,MAAM,EAAE,CAAA;CACrB,CAAA;AAED;;GAEG;AACH,eAAO,MAAM,cAAc,UAClB,WAAW,EAAE,KACnB,aA0BF,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,IAAI,UAAiB,WAAW,yBA8B5C,CAAA"}
|