@vltpkg/query 1.4.0 → 1.6.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 (213) hide show
  1. package/dist/index.d.ts +7 -2
  2. package/dist/index.js +23 -4
  3. package/dist/pseudo/skill.d.ts +7 -0
  4. package/dist/pseudo/skill.js +52 -0
  5. package/dist/pseudo/skills.d.ts +6 -0
  6. package/dist/pseudo/skills.js +26 -0
  7. package/dist/pseudo.js +7 -0
  8. package/dist/types.d.ts +29 -1
  9. package/package.json +10 -12
  10. package/skills/dss-query/REFERENCE.md +0 -123
  11. package/skills/dss-query/SKILL.md +0 -190
  12. package/skills/dss-query/evals/README.md +0 -81
  13. package/skills/dss-query/evals/evals.json +0 -89
  14. package/skills/dss-query/evals/grade.mjs +0 -481
  15. package/src/attribute.ts +0 -190
  16. package/src/combinator.ts +0 -135
  17. package/src/error.ts +0 -78
  18. package/src/id.ts +0 -46
  19. package/src/index.ts +0 -640
  20. package/src/parser.ts +0 -122
  21. package/src/pseudo/abandoned.ts +0 -9
  22. package/src/pseudo/attr.ts +0 -92
  23. package/src/pseudo/built.ts +0 -19
  24. package/src/pseudo/confused.ts +0 -29
  25. package/src/pseudo/cve.ts +0 -93
  26. package/src/pseudo/cwe.ts +0 -97
  27. package/src/pseudo/debug.ts +0 -9
  28. package/src/pseudo/deprecated.ts +0 -9
  29. package/src/pseudo/dev.ts +0 -18
  30. package/src/pseudo/diff.ts +0 -90
  31. package/src/pseudo/dist.ts +0 -148
  32. package/src/pseudo/dynamic.ts +0 -9
  33. package/src/pseudo/empty.ts +0 -15
  34. package/src/pseudo/entropic.ts +0 -9
  35. package/src/pseudo/env.ts +0 -6
  36. package/src/pseudo/eval.ts +0 -9
  37. package/src/pseudo/fs.ts +0 -9
  38. package/src/pseudo/helpers.ts +0 -106
  39. package/src/pseudo/host.ts +0 -111
  40. package/src/pseudo/hostname.ts +0 -156
  41. package/src/pseudo/license.ts +0 -134
  42. package/src/pseudo/link.ts +0 -30
  43. package/src/pseudo/malware.ts +0 -41
  44. package/src/pseudo/minified.ts +0 -9
  45. package/src/pseudo/missing.ts +0 -16
  46. package/src/pseudo/native.ts +0 -9
  47. package/src/pseudo/network.ts +0 -9
  48. package/src/pseudo/obfuscated.ts +0 -9
  49. package/src/pseudo/optional.ts +0 -18
  50. package/src/pseudo/outdated.ts +0 -345
  51. package/src/pseudo/overridden.ts +0 -20
  52. package/src/pseudo/path.ts +0 -146
  53. package/src/pseudo/peer.ts +0 -18
  54. package/src/pseudo/prerelease.ts +0 -47
  55. package/src/pseudo/private.ts +0 -19
  56. package/src/pseudo/prod.ts +0 -18
  57. package/src/pseudo/published.ts +0 -250
  58. package/src/pseudo/registry.ts +0 -29
  59. package/src/pseudo/root.ts +0 -19
  60. package/src/pseudo/scanned.ts +0 -20
  61. package/src/pseudo/score.ts +0 -186
  62. package/src/pseudo/scripts.ts +0 -61
  63. package/src/pseudo/semver.ts +0 -320
  64. package/src/pseudo/severity.ts +0 -231
  65. package/src/pseudo/shell.ts +0 -9
  66. package/src/pseudo/shrinkwrap.ts +0 -9
  67. package/src/pseudo/spec.ts +0 -131
  68. package/src/pseudo/squat.ts +0 -225
  69. package/src/pseudo/suspicious.ts +0 -9
  70. package/src/pseudo/tracker.ts +0 -9
  71. package/src/pseudo/trivial.ts +0 -9
  72. package/src/pseudo/type.ts +0 -26
  73. package/src/pseudo/undesirable.ts +0 -9
  74. package/src/pseudo/unknown.ts +0 -9
  75. package/src/pseudo/unmaintained.ts +0 -9
  76. package/src/pseudo/unpopular.ts +0 -9
  77. package/src/pseudo/unstable.ts +0 -9
  78. package/src/pseudo/vuln.ts +0 -273
  79. package/src/pseudo/workspace.ts +0 -22
  80. package/src/pseudo.ts +0 -433
  81. package/src/types.ts +0 -176
  82. package/tap-snapshots/test/attribute.ts.test.cjs +0 -304
  83. package/tap-snapshots/test/combinator.ts.test.cjs +0 -333
  84. package/tap-snapshots/test/id.ts.test.cjs +0 -118
  85. package/tap-snapshots/test/parser.ts.test.cjs +0 -344
  86. package/tap-snapshots/test/pseudo/abandoned.ts.test.cjs +0 -18
  87. package/tap-snapshots/test/pseudo/attr.ts.test.cjs +0 -161
  88. package/tap-snapshots/test/pseudo/built.ts.test.cjs +0 -70
  89. package/tap-snapshots/test/pseudo/confused.ts.test.cjs +0 -32
  90. package/tap-snapshots/test/pseudo/cve.ts.test.cjs +0 -49
  91. package/tap-snapshots/test/pseudo/cwe.ts.test.cjs +0 -49
  92. package/tap-snapshots/test/pseudo/debug.ts.test.cjs +0 -18
  93. package/tap-snapshots/test/pseudo/deprecated.ts.test.cjs +0 -18
  94. package/tap-snapshots/test/pseudo/dist.ts.test.cjs +0 -30
  95. package/tap-snapshots/test/pseudo/dynamic.ts.test.cjs +0 -18
  96. package/tap-snapshots/test/pseudo/empty.ts.test.cjs +0 -50
  97. package/tap-snapshots/test/pseudo/entropic.ts.test.cjs +0 -18
  98. package/tap-snapshots/test/pseudo/env.ts.test.cjs +0 -18
  99. package/tap-snapshots/test/pseudo/eval.ts.test.cjs +0 -18
  100. package/tap-snapshots/test/pseudo/fs.ts.test.cjs +0 -18
  101. package/tap-snapshots/test/pseudo/helpers.ts.test.cjs +0 -18
  102. package/tap-snapshots/test/pseudo/hostname.ts.test.cjs +0 -148
  103. package/tap-snapshots/test/pseudo/license.ts.test.cjs +0 -41
  104. package/tap-snapshots/test/pseudo/link.ts.test.cjs +0 -36
  105. package/tap-snapshots/test/pseudo/malware.ts.test.cjs +0 -22
  106. package/tap-snapshots/test/pseudo/minified.ts.test.cjs +0 -18
  107. package/tap-snapshots/test/pseudo/missing.ts.test.cjs +0 -30
  108. package/tap-snapshots/test/pseudo/native.ts.test.cjs +0 -18
  109. package/tap-snapshots/test/pseudo/network.ts.test.cjs +0 -18
  110. package/tap-snapshots/test/pseudo/obfuscated.ts.test.cjs +0 -18
  111. package/tap-snapshots/test/pseudo/outdated.ts.test.cjs +0 -132
  112. package/tap-snapshots/test/pseudo/overridden.ts.test.cjs +0 -75
  113. package/tap-snapshots/test/pseudo/prerelease.ts.test.cjs +0 -101
  114. package/tap-snapshots/test/pseudo/private.ts.test.cjs +0 -38
  115. package/tap-snapshots/test/pseudo/published.ts.test.cjs +0 -171
  116. package/tap-snapshots/test/pseudo/registry.ts.test.cjs +0 -68
  117. package/tap-snapshots/test/pseudo/root.ts.test.cjs +0 -24
  118. package/tap-snapshots/test/pseudo/scanned.ts.test.cjs +0 -18
  119. package/tap-snapshots/test/pseudo/score.ts.test.cjs +0 -239
  120. package/tap-snapshots/test/pseudo/scripts.ts.test.cjs +0 -62
  121. package/tap-snapshots/test/pseudo/semver.ts.test.cjs +0 -575
  122. package/tap-snapshots/test/pseudo/severity.ts.test.cjs +0 -122
  123. package/tap-snapshots/test/pseudo/shell.ts.test.cjs +0 -18
  124. package/tap-snapshots/test/pseudo/shrinkwrap.ts.test.cjs +0 -18
  125. package/tap-snapshots/test/pseudo/spec.ts.test.cjs +0 -148
  126. package/tap-snapshots/test/pseudo/squat.ts.test.cjs +0 -129
  127. package/tap-snapshots/test/pseudo/suspicious.ts.test.cjs +0 -18
  128. package/tap-snapshots/test/pseudo/tracker.ts.test.cjs +0 -18
  129. package/tap-snapshots/test/pseudo/trivial.ts.test.cjs +0 -18
  130. package/tap-snapshots/test/pseudo/type.ts.test.cjs +0 -51
  131. package/tap-snapshots/test/pseudo/undesirable.ts.test.cjs +0 -18
  132. package/tap-snapshots/test/pseudo/unknown.ts.test.cjs +0 -18
  133. package/tap-snapshots/test/pseudo/unmaintained.ts.test.cjs +0 -18
  134. package/tap-snapshots/test/pseudo/unpopular.ts.test.cjs +0 -18
  135. package/tap-snapshots/test/pseudo/unstable.ts.test.cjs +0 -18
  136. package/tap-snapshots/test/pseudo/vuln.ts.test.cjs +0 -208
  137. package/tap-snapshots/test/pseudo/vulnerable.ts.test.cjs +0 -20
  138. package/tap-snapshots/test/pseudo/workspace.ts.test.cjs +0 -19
  139. package/tap-snapshots/test/pseudo.ts.test.cjs +0 -1247
  140. package/test/attribute.ts +0 -273
  141. package/test/combinator.ts +0 -175
  142. package/test/error.ts +0 -60
  143. package/test/fixtures/graph.ts +0 -999
  144. package/test/fixtures/selector.ts +0 -103
  145. package/test/fixtures/types.ts +0 -7
  146. package/test/id.ts +0 -102
  147. package/test/index.ts +0 -1049
  148. package/test/parser.ts +0 -117
  149. package/test/pseudo/abandoned.ts +0 -98
  150. package/test/pseudo/attr.ts +0 -298
  151. package/test/pseudo/built.ts +0 -214
  152. package/test/pseudo/confused.ts +0 -160
  153. package/test/pseudo/cve.ts +0 -249
  154. package/test/pseudo/cwe.ts +0 -255
  155. package/test/pseudo/debug.ts +0 -98
  156. package/test/pseudo/deprecated.ts +0 -98
  157. package/test/pseudo/dev.ts +0 -61
  158. package/test/pseudo/diff.ts +0 -417
  159. package/test/pseudo/dist.ts +0 -215
  160. package/test/pseudo/dynamic.ts +0 -98
  161. package/test/pseudo/empty.ts +0 -108
  162. package/test/pseudo/entropic.ts +0 -101
  163. package/test/pseudo/env.ts +0 -98
  164. package/test/pseudo/eval.ts +0 -98
  165. package/test/pseudo/fs.ts +0 -98
  166. package/test/pseudo/helpers.ts +0 -370
  167. package/test/pseudo/host-context.ts +0 -276
  168. package/test/pseudo/hostname.ts +0 -314
  169. package/test/pseudo/license.ts +0 -265
  170. package/test/pseudo/link.ts +0 -94
  171. package/test/pseudo/malware.ts +0 -184
  172. package/test/pseudo/minified.ts +0 -98
  173. package/test/pseudo/missing.ts +0 -111
  174. package/test/pseudo/native.ts +0 -98
  175. package/test/pseudo/network.ts +0 -98
  176. package/test/pseudo/obfuscated.ts +0 -98
  177. package/test/pseudo/optional.ts +0 -73
  178. package/test/pseudo/outdated.ts +0 -461
  179. package/test/pseudo/overridden.ts +0 -317
  180. package/test/pseudo/path.ts +0 -680
  181. package/test/pseudo/peer.ts +0 -101
  182. package/test/pseudo/prerelease.ts +0 -279
  183. package/test/pseudo/private.ts +0 -108
  184. package/test/pseudo/prod.ts +0 -61
  185. package/test/pseudo/published.ts +0 -557
  186. package/test/pseudo/registry.ts +0 -122
  187. package/test/pseudo/root.ts +0 -66
  188. package/test/pseudo/scanned.ts +0 -144
  189. package/test/pseudo/score.ts +0 -591
  190. package/test/pseudo/scripts.ts +0 -324
  191. package/test/pseudo/semver.ts +0 -822
  192. package/test/pseudo/severity.ts +0 -342
  193. package/test/pseudo/shell.ts +0 -98
  194. package/test/pseudo/shrinkwrap.ts +0 -98
  195. package/test/pseudo/spec.ts +0 -525
  196. package/test/pseudo/squat.ts +0 -565
  197. package/test/pseudo/suspicious.ts +0 -98
  198. package/test/pseudo/tracker.ts +0 -98
  199. package/test/pseudo/trivial.ts +0 -98
  200. package/test/pseudo/type.ts +0 -105
  201. package/test/pseudo/undesirable.ts +0 -98
  202. package/test/pseudo/unknown.ts +0 -98
  203. package/test/pseudo/unmaintained.ts +0 -98
  204. package/test/pseudo/unpopular.ts +0 -98
  205. package/test/pseudo/unstable.ts +0 -98
  206. package/test/pseudo/vuln.ts +0 -625
  207. package/test/pseudo/vulnerable.ts +0 -137
  208. package/test/pseudo/workspace.ts +0 -111
  209. package/test/pseudo.ts +0 -584
  210. package/tsconfig.json +0 -3
  211. package/tsconfig.publish.json +0 -19
  212. package/tsconfig.publish.tsbuildinfo +0 -1
  213. package/typedoc.mjs +0 -2
@@ -1,148 +0,0 @@
1
- import pRetry, { AbortError } from 'p-retry'
2
- import { hydrate, splitDepID } from '@vltpkg/dep-id/browser'
3
- import { error } from '@vltpkg/error-cause'
4
- import { userAgentHeaders } from '@vltpkg/user-agent'
5
- import {
6
- asPostcssNodeWithChildren,
7
- asTagNode,
8
- asStringNode,
9
- isTagNode,
10
- } from '@vltpkg/dss-parser'
11
- import {
12
- removeDanglingEdges,
13
- removeNode,
14
- removeQuotes,
15
- } from './helpers.ts'
16
- import type { NodeLike, Packument } from '@vltpkg/types'
17
- import type { ParserState } from '../types.ts'
18
-
19
- /**
20
- * Fetches the dist-tags of a package from the registry.
21
- */
22
- export const retrieveDistTags = async (
23
- node: NodeLike,
24
- signal?: AbortSignal,
25
- ): Promise<Record<string, string>> => {
26
- const spec = hydrate(node.id, String(node.name), node.options)
27
- if (!spec.registry || !node.name) {
28
- return {}
29
- }
30
-
31
- const url = new URL(spec.registry)
32
- url.pathname = `/${node.name}`
33
-
34
- // Corgi is safe here: raw fetch (never the RegistryClient disk cache),
35
- // and only dist-tags are read.
36
- const response = await fetch(String(url), {
37
- headers: {
38
- Accept: 'application/vnd.npm.install-v1+json',
39
- ...userAgentHeaders,
40
- },
41
- signal,
42
- })
43
- if (response.status === 404) {
44
- throw new AbortError('Missing API')
45
- }
46
- if (!response.ok) {
47
- throw error('Failed to fetch packument', {
48
- name: node.name,
49
- spec,
50
- response,
51
- })
52
- }
53
- const packument = (await response.json()) as Packument
54
- return packument['dist-tags']
55
- }
56
-
57
- /**
58
- * Checks whether a node's installed version matches the version
59
- * associated with the given dist-tag. Returns the node if it should
60
- * be removed (does NOT match), or undefined if it matches.
61
- */
62
- export const queueNode = async (
63
- state: ParserState,
64
- node: NodeLike,
65
- tagName: string,
66
- ): Promise<NodeLike | undefined> => {
67
- if (!node.name || !node.version) {
68
- return node
69
- }
70
-
71
- let distTags: Record<string, string>
72
- try {
73
- distTags = await pRetry(
74
- () => retrieveDistTags(node, state.signal),
75
- {
76
- retries: state.retries,
77
- signal: state.signal,
78
- },
79
- )
80
- } catch (err) {
81
- // eslint-disable-next-line no-console
82
- console.warn(
83
- error('Could not retrieve dist-tags', {
84
- name: node.name,
85
- cause: err,
86
- }),
87
- )
88
- return node
89
- }
90
-
91
- const tagVersion = distTags[tagName]
92
- if (tagVersion !== node.version) {
93
- return node
94
- }
95
-
96
- return undefined
97
- }
98
-
99
- /**
100
- * :dist(tag) Pseudo-Selector, matches only nodes whose installed
101
- * version corresponds to the given dist-tag in the registry.
102
- *
103
- * Examples:
104
- * - :dist(latest) — matches packages at the `latest` dist-tag version
105
- * - :dist(nightly) — matches packages at the `nightly` dist-tag version
106
- * - :not(:dist(nightly)) — excludes nightly-tagged versions
107
- */
108
- export const dist = async (state: ParserState) => {
109
- const top = asPostcssNodeWithChildren(state.current)
110
- const selector = asPostcssNodeWithChildren(top.nodes[0])
111
- const firstChild = selector.nodes[0]
112
-
113
- let tagName: string
114
- try {
115
- tagName = removeQuotes(asStringNode(firstChild).value)
116
- } catch {
117
- if (isTagNode(firstChild)) {
118
- tagName = asTagNode(firstChild).value
119
- /* c8 ignore start */
120
- } else {
121
- throw error('Failed to parse :dist selector', {
122
- found: firstChild,
123
- })
124
- }
125
- /* c8 ignore stop */
126
- }
127
-
128
- const queue = []
129
-
130
- for (const node of state.partial.nodes) {
131
- if (splitDepID(node.id)[0] !== 'registry') {
132
- removeNode(state, node)
133
- continue
134
- }
135
- queue.push(queueNode(state, node, tagName))
136
- }
137
-
138
- const removeNodeQueue = await Promise.all(queue)
139
- for (const node of removeNodeQueue) {
140
- if (node) {
141
- removeNode(state, node)
142
- }
143
- }
144
-
145
- removeDanglingEdges(state)
146
-
147
- return state
148
- }
@@ -1,9 +0,0 @@
1
- import { createSecuritySelectorFilter } from './helpers.ts'
2
-
3
- /**
4
- * Filters out any node that does not have a **dynamicRequire** report alert.
5
- */
6
- export const dynamic = createSecuritySelectorFilter(
7
- 'dynamic',
8
- 'dynamicRequire',
9
- )
@@ -1,15 +0,0 @@
1
- import type { ParserState } from '../types.ts'
2
- import { removeNode } from './helpers.ts'
3
-
4
- /**
5
- * :empty Pseudo-Selector, matches only nodes that have no children.
6
- * It filters out any node that has edges out, i.e., has dependencies.
7
- */
8
- export const empty = async (state: ParserState) => {
9
- for (const node of state.partial.nodes) {
10
- if (node.edgesOut.size > 0) {
11
- removeNode(state, node)
12
- }
13
- }
14
- return state
15
- }
@@ -1,9 +0,0 @@
1
- import { createSecuritySelectorFilter } from './helpers.ts'
2
-
3
- /**
4
- * Filters out any node that does not have a **highEntropyStrings** report alert.
5
- */
6
- export const entropic = createSecuritySelectorFilter(
7
- 'entropic',
8
- 'highEntropyStrings',
9
- )
package/src/pseudo/env.ts DELETED
@@ -1,6 +0,0 @@
1
- import { createSecuritySelectorFilter } from './helpers.ts'
2
-
3
- /**
4
- * Filters out any node that does not have a **envVars** report alert.
5
- */
6
- export const env = createSecuritySelectorFilter('env', 'envVars')
@@ -1,9 +0,0 @@
1
- import { createSecuritySelectorFilter } from './helpers.ts'
2
-
3
- /**
4
- * Filters out any node that does not have a **usesEval** report alert.
5
- */
6
- export const evalParser = createSecuritySelectorFilter(
7
- 'eval',
8
- 'usesEval',
9
- )
package/src/pseudo/fs.ts DELETED
@@ -1,9 +0,0 @@
1
- import { createSecuritySelectorFilter } from './helpers.ts'
2
-
3
- /**
4
- * Filters out any node that does not have a **filesystemAccess** report alert.
5
- */
6
- export const fs = createSecuritySelectorFilter(
7
- 'fs',
8
- 'filesystemAccess',
9
- )
@@ -1,106 +0,0 @@
1
- import { error } from '@vltpkg/error-cause'
2
- import type { EdgeLike, NodeLike } from '@vltpkg/types'
3
- import type { ParserState } from '../types.js'
4
-
5
- /**
6
- * Removes a node and its incoming edges from the results.
7
- */
8
- export const removeNode = (state: ParserState, node: NodeLike) => {
9
- for (const edge of node.edgesIn) {
10
- state.partial.edges.delete(edge)
11
- }
12
- state.partial.nodes.delete(node)
13
- }
14
-
15
- /**
16
- * Removes an edge and its outgoing node from the results.
17
- */
18
- export const removeEdge = (state: ParserState, edge: EdgeLike) => {
19
- state.partial.edges.delete(edge)
20
- if (edge.to?.edgesIn.size === 1) {
21
- state.partial.nodes.delete(edge.to)
22
- }
23
- }
24
-
25
- /**
26
- * Removes any edges that have no destination node from the results.
27
- */
28
- export const removeDanglingEdges = (state: ParserState) => {
29
- for (const edge of state.partial.edges) {
30
- if (!edge.to || !state.partial.nodes.has(edge.to)) {
31
- state.partial.edges.delete(edge)
32
- }
33
- }
34
- }
35
-
36
- /**
37
- * Removes any nodes that have no incoming edges from the results.
38
- */
39
- export const removeUnlinkedNodes = (state: ParserState) => {
40
- nodeLoop: for (const node of state.partial.nodes) {
41
- for (const edge of state.partial.edges) {
42
- if (edge.to === node) {
43
- continue nodeLoop
44
- }
45
- }
46
- state.partial.nodes.delete(node)
47
- }
48
- }
49
-
50
- /**
51
- * Removes quotes from a string value.
52
- */
53
- export const removeQuotes = (value: string) =>
54
- value.replace(/^"(.*?)"$/, '$1')
55
-
56
- /**
57
- * Asserts that the security archive is present.
58
- */
59
- export const assertSecurityArchive: (
60
- state: ParserState,
61
- name: string,
62
- ) => asserts state is ParserState & {
63
- securityArchive: NonNullable<ParserState['securityArchive']>
64
- } = (state, name) => {
65
- if (!state.securityArchive) {
66
- throw error(
67
- `Missing security archive while trying to parse the :${name} selector`,
68
- { found: state },
69
- )
70
- }
71
- }
72
-
73
- /**
74
- * Clears all nodes and edges from the results.
75
- */
76
- export const clear = (state: ParserState): ParserState => {
77
- state.partial.nodes.clear()
78
- state.partial.edges.clear()
79
- return state
80
- }
81
-
82
- /**
83
- * Reusable security selector alert filter.
84
- */
85
- export const createSecuritySelectorFilter = (
86
- name: string,
87
- type: string,
88
- ) => {
89
- return async (state: ParserState) => {
90
- assertSecurityArchive(state, name)
91
-
92
- for (const node of state.partial.nodes) {
93
- const report = state.securityArchive.get(node.id)
94
- const exclude = !report?.alerts.some(
95
- alert => alert.type === type,
96
- )
97
- if (exclude) {
98
- removeNode(state, node)
99
- }
100
- }
101
-
102
- removeDanglingEdges(state)
103
-
104
- return state
105
- }
106
- }
@@ -1,111 +0,0 @@
1
- import { error } from '@vltpkg/error-cause'
2
- import {
3
- asPostcssNodeWithChildren,
4
- asStringNode,
5
- asTagNode,
6
- isStringNode,
7
- isTagNode,
8
- } from '@vltpkg/dss-parser'
9
- import { removeQuotes } from './helpers.ts'
10
- import type { ParserState } from '../types.ts'
11
- import type { PostcssNode } from '@vltpkg/dss-parser'
12
-
13
- /**
14
- * Parses the internal parameters of the :host() pseudo selector.
15
- * Returns the context key that should be used to look up the host context function.
16
- */
17
- export const parseInternals = (nodes: PostcssNode[]): string => {
18
- let contextKey = ''
19
-
20
- if (isStringNode(asPostcssNodeWithChildren(nodes[0]).nodes[0])) {
21
- contextKey = removeQuotes(
22
- asStringNode(asPostcssNodeWithChildren(nodes[0]).nodes[0])
23
- .value,
24
- )
25
- } else if (
26
- isTagNode(asPostcssNodeWithChildren(nodes[0]).nodes[0])
27
- ) {
28
- const tagNode = asTagNode(
29
- asPostcssNodeWithChildren(nodes[0]).nodes[0],
30
- )
31
- contextKey = tagNode.value
32
- }
33
-
34
- if (!contextKey) {
35
- throw error('Expected a context key parameter for :host selector')
36
- }
37
-
38
- return contextKey
39
- }
40
-
41
- /**
42
- * :host Pseudo-Selector, switches the current graph context to a new
43
- * set of graphs loaded from a specific host context.
44
- *
45
- * This selector accepts a single parameter that specifies which host context
46
- * to use. The host context must be defined in the hostContexts map provided
47
- * to the Query constructor.
48
- *
49
- * Example:
50
- * - :host(local) - Switches to graphs loaded from the local context
51
- */
52
- export const hostContext = async (state: ParserState) => {
53
- if (!state.hostContexts) {
54
- throw error('No host contexts available for :host selector')
55
- }
56
-
57
- let contextKey: string
58
- try {
59
- contextKey = parseInternals(
60
- asPostcssNodeWithChildren(state.current).nodes,
61
- )
62
- } catch (err) {
63
- throw error('Failed to parse :host selector', {
64
- cause: err,
65
- })
66
- }
67
-
68
- const contextFunction = state.hostContexts.get(contextKey)
69
- if (!contextFunction) {
70
- throw error(`Unknown host context: ${contextKey}`, {
71
- validOptions: Array.from(state.hostContexts.keys()),
72
- })
73
- }
74
-
75
- // Get the graphs from the host context function
76
- const {
77
- initialEdges,
78
- initialNodes,
79
- edges,
80
- nodes,
81
- securityArchive,
82
- } = await contextFunction()
83
-
84
- // Clear current nodes and edges
85
- state.securityArchive = securityArchive
86
- state.initial.nodes.clear()
87
- state.initial.edges.clear()
88
- state.partial.nodes.clear()
89
- state.partial.edges.clear()
90
- state.importers.clear()
91
-
92
- // Reset the initial state
93
- for (const node of initialNodes) {
94
- state.initial.nodes.add(node)
95
- }
96
- for (const edge of initialEdges) {
97
- state.initial.edges.add(edge)
98
- }
99
-
100
- // Populate with nodes and edges from all returned graphs
101
- for (const node of nodes) {
102
- state.partial.nodes.add(node)
103
- // use the current selected nodes by the context function as importers
104
- state.importers.add(node)
105
- }
106
- for (const edge of edges) {
107
- state.partial.edges.add(edge)
108
- }
109
-
110
- return state
111
- }
@@ -1,156 +0,0 @@
1
- import { splitDepID } from '@vltpkg/dep-id/browser'
2
- import { getOptions, gitHostWebsites } from '@vltpkg/spec/browser'
3
- import { asPostcssNodeWithChildren } from '@vltpkg/dss-parser'
4
- import { error } from '@vltpkg/error-cause'
5
- import { removeDanglingEdges, removeNode } from './helpers.ts'
6
- import { parseInternals } from './spec.ts'
7
- import type { ParserState } from '../types.ts'
8
-
9
- /**
10
- * Given a named git host (e.g. "github"), resolve its hostname
11
- * by looking up `gitHostWebsites` (e.g. "github.com"), or
12
- * falling back to the `git-hosts` template URL.
13
- */
14
- const resolveGitHostname = (
15
- namedHost: string,
16
- gitHosts: Record<string, string>,
17
- ): string | undefined => {
18
- // Check gitHostWebsites first (e.g. github -> github.com)
19
- const website =
20
- gitHostWebsites[namedHost as keyof typeof gitHostWebsites]
21
- if (website) {
22
- try {
23
- return new URL(website).hostname
24
- /* c8 ignore next */
25
- } catch {}
26
- }
27
-
28
- // Fall back to parsing the git-hosts template URL
29
- const template = gitHosts[namedHost]
30
- if (template) {
31
- try {
32
- // Templates look like 'git+ssh://git@github.com:$1/$2.git'
33
- // Replace colons after the host with / for URL parsing
34
- const normalized = template
35
- .replace(/^git\+/, '')
36
- .replace(/:(\$)/, '/$1')
37
- return new URL(normalized).hostname
38
- /* c8 ignore next */
39
- } catch {}
40
- }
41
- /* c8 ignore next */
42
- return undefined
43
- }
44
-
45
- /**
46
- * Resolve the hostname for a git remote string.
47
- * Named hosts: "github:user/repo" -> extract "github" and look up.
48
- * Full URLs: "git+ssh://git@example.com/repo.git" -> parse URL.
49
- */
50
- const getGitHostname = (
51
- gitRemote: string,
52
- gitHosts: Record<string, string>,
53
- ): string | undefined => {
54
- // Check if it's a named git host like "github:user/repo"
55
- const colonIdx = gitRemote.indexOf(':')
56
- if (colonIdx > 0) {
57
- const possibleHost = gitRemote.slice(0, colonIdx)
58
- // If this is a known named git host, resolve it
59
- if (possibleHost in gitHosts) {
60
- return resolveGitHostname(possibleHost, gitHosts)
61
- }
62
- // Could be a protocol URL like git+ssh://...
63
- try {
64
- const normalized = gitRemote
65
- .replace(/^git\+/, '')
66
- .replace(/^ssh:\/\/([^@]+@)?([^:/]+)[:/]/, 'ssh://$1$2/')
67
- return new URL(normalized).hostname
68
- /* c8 ignore next */
69
- } catch {}
70
- }
71
- /* c8 ignore start - git remotes always contain a colon */
72
-
73
- // Try parsing as a plain URL
74
- try {
75
- return new URL(gitRemote).hostname
76
- } catch {}
77
-
78
- return undefined
79
- /* c8 ignore stop */
80
- }
81
-
82
- /**
83
- * :hostname(str) Pseudo-Selector, matches only nodes whose
84
- * upstream hostname matches the provided domain string.
85
- *
86
- * Examples:
87
- * - :hostname("registry.npmjs.org") — default npm registry deps
88
- * - :hostname("github.com") — github git deps
89
- * - :hostname("example.com") — custom registry deps
90
- */
91
- export const hostname = async (state: ParserState) => {
92
- let internals
93
- try {
94
- internals = parseInternals(
95
- asPostcssNodeWithChildren(state.current).nodes,
96
- )
97
- } catch (err) {
98
- throw error('Failed to parse :hostname selector', {
99
- cause: err,
100
- })
101
- }
102
-
103
- const targetHostname = internals.specValue
104
-
105
- for (const node of state.partial.nodes) {
106
- const tuple = splitDepID(node.id)
107
- const type = tuple[0]
108
- let nodeHostname: string | undefined
109
-
110
- switch (type) {
111
- case 'registry': {
112
- const registryName = tuple[1]
113
- const options = getOptions(node.options)
114
- // Look up the registry URL from registries map. There is no
115
- // default registry, so a node with an empty registry field and
116
- // no configured registry has no hostname, and never matches.
117
- const registryUrl =
118
- options.registries[registryName] ?? options.registry
119
- if (registryUrl) {
120
- try {
121
- nodeHostname = new URL(registryUrl).hostname
122
- /* c8 ignore next */
123
- } catch {}
124
- }
125
- break
126
- }
127
- case 'git': {
128
- const gitRemote = tuple[1]
129
- const options = getOptions(node.options)
130
- nodeHostname = getGitHostname(gitRemote, options['git-hosts'])
131
- break
132
- }
133
- case 'remote': {
134
- const url = tuple[1]
135
- try {
136
- nodeHostname = new URL(url).hostname
137
- /* c8 ignore next */
138
- } catch {}
139
- break
140
- }
141
- // file and workspace deps are local — no hostname
142
- default: {
143
- nodeHostname = undefined
144
- break
145
- }
146
- }
147
-
148
- if (nodeHostname !== targetHostname) {
149
- removeNode(state, node)
150
- }
151
- }
152
-
153
- removeDanglingEdges(state)
154
-
155
- return state
156
- }