@vltpkg/query 1.5.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 (210) hide show
  1. package/package.json +10 -12
  2. package/skills/dss-query/REFERENCE.md +0 -125
  3. package/skills/dss-query/SKILL.md +0 -190
  4. package/skills/dss-query/evals/README.md +0 -81
  5. package/skills/dss-query/evals/evals.json +0 -89
  6. package/skills/dss-query/evals/grade.mjs +0 -481
  7. package/src/attribute.ts +0 -190
  8. package/src/combinator.ts +0 -135
  9. package/src/error.ts +0 -78
  10. package/src/id.ts +0 -46
  11. package/src/index.ts +0 -670
  12. package/src/parser.ts +0 -122
  13. package/src/pseudo/abandoned.ts +0 -9
  14. package/src/pseudo/attr.ts +0 -92
  15. package/src/pseudo/built.ts +0 -19
  16. package/src/pseudo/confused.ts +0 -29
  17. package/src/pseudo/cve.ts +0 -93
  18. package/src/pseudo/cwe.ts +0 -97
  19. package/src/pseudo/debug.ts +0 -9
  20. package/src/pseudo/deprecated.ts +0 -9
  21. package/src/pseudo/dev.ts +0 -18
  22. package/src/pseudo/diff.ts +0 -90
  23. package/src/pseudo/dist.ts +0 -148
  24. package/src/pseudo/dynamic.ts +0 -9
  25. package/src/pseudo/empty.ts +0 -15
  26. package/src/pseudo/entropic.ts +0 -9
  27. package/src/pseudo/env.ts +0 -6
  28. package/src/pseudo/eval.ts +0 -9
  29. package/src/pseudo/fs.ts +0 -9
  30. package/src/pseudo/helpers.ts +0 -106
  31. package/src/pseudo/host.ts +0 -111
  32. package/src/pseudo/hostname.ts +0 -156
  33. package/src/pseudo/license.ts +0 -134
  34. package/src/pseudo/link.ts +0 -30
  35. package/src/pseudo/malware.ts +0 -41
  36. package/src/pseudo/minified.ts +0 -9
  37. package/src/pseudo/missing.ts +0 -16
  38. package/src/pseudo/native.ts +0 -9
  39. package/src/pseudo/network.ts +0 -9
  40. package/src/pseudo/obfuscated.ts +0 -9
  41. package/src/pseudo/optional.ts +0 -18
  42. package/src/pseudo/outdated.ts +0 -345
  43. package/src/pseudo/overridden.ts +0 -20
  44. package/src/pseudo/path.ts +0 -146
  45. package/src/pseudo/peer.ts +0 -18
  46. package/src/pseudo/prerelease.ts +0 -47
  47. package/src/pseudo/private.ts +0 -19
  48. package/src/pseudo/prod.ts +0 -18
  49. package/src/pseudo/published.ts +0 -250
  50. package/src/pseudo/registry.ts +0 -29
  51. package/src/pseudo/root.ts +0 -19
  52. package/src/pseudo/scanned.ts +0 -20
  53. package/src/pseudo/score.ts +0 -186
  54. package/src/pseudo/scripts.ts +0 -61
  55. package/src/pseudo/semver.ts +0 -320
  56. package/src/pseudo/severity.ts +0 -231
  57. package/src/pseudo/shell.ts +0 -9
  58. package/src/pseudo/shrinkwrap.ts +0 -9
  59. package/src/pseudo/skill.ts +0 -59
  60. package/src/pseudo/skills.ts +0 -29
  61. package/src/pseudo/spec.ts +0 -131
  62. package/src/pseudo/squat.ts +0 -225
  63. package/src/pseudo/suspicious.ts +0 -9
  64. package/src/pseudo/tracker.ts +0 -9
  65. package/src/pseudo/trivial.ts +0 -9
  66. package/src/pseudo/type.ts +0 -26
  67. package/src/pseudo/undesirable.ts +0 -9
  68. package/src/pseudo/unknown.ts +0 -9
  69. package/src/pseudo/unmaintained.ts +0 -9
  70. package/src/pseudo/unpopular.ts +0 -9
  71. package/src/pseudo/unstable.ts +0 -9
  72. package/src/pseudo/vuln.ts +0 -273
  73. package/src/pseudo/workspace.ts +0 -22
  74. package/src/pseudo.ts +0 -440
  75. package/src/types.ts +0 -206
  76. package/tap-snapshots/test/attribute.ts.test.cjs +0 -304
  77. package/tap-snapshots/test/combinator.ts.test.cjs +0 -333
  78. package/tap-snapshots/test/id.ts.test.cjs +0 -118
  79. package/tap-snapshots/test/parser.ts.test.cjs +0 -344
  80. package/tap-snapshots/test/pseudo/abandoned.ts.test.cjs +0 -18
  81. package/tap-snapshots/test/pseudo/attr.ts.test.cjs +0 -161
  82. package/tap-snapshots/test/pseudo/built.ts.test.cjs +0 -70
  83. package/tap-snapshots/test/pseudo/confused.ts.test.cjs +0 -32
  84. package/tap-snapshots/test/pseudo/cve.ts.test.cjs +0 -49
  85. package/tap-snapshots/test/pseudo/cwe.ts.test.cjs +0 -49
  86. package/tap-snapshots/test/pseudo/debug.ts.test.cjs +0 -18
  87. package/tap-snapshots/test/pseudo/deprecated.ts.test.cjs +0 -18
  88. package/tap-snapshots/test/pseudo/dist.ts.test.cjs +0 -30
  89. package/tap-snapshots/test/pseudo/dynamic.ts.test.cjs +0 -18
  90. package/tap-snapshots/test/pseudo/empty.ts.test.cjs +0 -50
  91. package/tap-snapshots/test/pseudo/entropic.ts.test.cjs +0 -18
  92. package/tap-snapshots/test/pseudo/env.ts.test.cjs +0 -18
  93. package/tap-snapshots/test/pseudo/eval.ts.test.cjs +0 -18
  94. package/tap-snapshots/test/pseudo/fs.ts.test.cjs +0 -18
  95. package/tap-snapshots/test/pseudo/helpers.ts.test.cjs +0 -18
  96. package/tap-snapshots/test/pseudo/hostname.ts.test.cjs +0 -148
  97. package/tap-snapshots/test/pseudo/license.ts.test.cjs +0 -41
  98. package/tap-snapshots/test/pseudo/link.ts.test.cjs +0 -36
  99. package/tap-snapshots/test/pseudo/malware.ts.test.cjs +0 -22
  100. package/tap-snapshots/test/pseudo/minified.ts.test.cjs +0 -18
  101. package/tap-snapshots/test/pseudo/missing.ts.test.cjs +0 -30
  102. package/tap-snapshots/test/pseudo/native.ts.test.cjs +0 -18
  103. package/tap-snapshots/test/pseudo/network.ts.test.cjs +0 -18
  104. package/tap-snapshots/test/pseudo/obfuscated.ts.test.cjs +0 -18
  105. package/tap-snapshots/test/pseudo/outdated.ts.test.cjs +0 -132
  106. package/tap-snapshots/test/pseudo/overridden.ts.test.cjs +0 -75
  107. package/tap-snapshots/test/pseudo/prerelease.ts.test.cjs +0 -101
  108. package/tap-snapshots/test/pseudo/private.ts.test.cjs +0 -38
  109. package/tap-snapshots/test/pseudo/published.ts.test.cjs +0 -171
  110. package/tap-snapshots/test/pseudo/registry.ts.test.cjs +0 -68
  111. package/tap-snapshots/test/pseudo/root.ts.test.cjs +0 -24
  112. package/tap-snapshots/test/pseudo/scanned.ts.test.cjs +0 -18
  113. package/tap-snapshots/test/pseudo/score.ts.test.cjs +0 -239
  114. package/tap-snapshots/test/pseudo/scripts.ts.test.cjs +0 -62
  115. package/tap-snapshots/test/pseudo/semver.ts.test.cjs +0 -575
  116. package/tap-snapshots/test/pseudo/severity.ts.test.cjs +0 -122
  117. package/tap-snapshots/test/pseudo/shell.ts.test.cjs +0 -18
  118. package/tap-snapshots/test/pseudo/shrinkwrap.ts.test.cjs +0 -18
  119. package/tap-snapshots/test/pseudo/skills.ts.test.cjs +0 -19
  120. package/tap-snapshots/test/pseudo/spec.ts.test.cjs +0 -148
  121. package/tap-snapshots/test/pseudo/squat.ts.test.cjs +0 -129
  122. package/tap-snapshots/test/pseudo/suspicious.ts.test.cjs +0 -18
  123. package/tap-snapshots/test/pseudo/tracker.ts.test.cjs +0 -18
  124. package/tap-snapshots/test/pseudo/trivial.ts.test.cjs +0 -18
  125. package/tap-snapshots/test/pseudo/type.ts.test.cjs +0 -51
  126. package/tap-snapshots/test/pseudo/undesirable.ts.test.cjs +0 -18
  127. package/tap-snapshots/test/pseudo/unknown.ts.test.cjs +0 -18
  128. package/tap-snapshots/test/pseudo/unmaintained.ts.test.cjs +0 -18
  129. package/tap-snapshots/test/pseudo/unpopular.ts.test.cjs +0 -18
  130. package/tap-snapshots/test/pseudo/unstable.ts.test.cjs +0 -18
  131. package/tap-snapshots/test/pseudo/vuln.ts.test.cjs +0 -208
  132. package/tap-snapshots/test/pseudo/vulnerable.ts.test.cjs +0 -20
  133. package/tap-snapshots/test/pseudo/workspace.ts.test.cjs +0 -19
  134. package/tap-snapshots/test/pseudo.ts.test.cjs +0 -1247
  135. package/test/attribute.ts +0 -273
  136. package/test/combinator.ts +0 -175
  137. package/test/error.ts +0 -60
  138. package/test/fixtures/graph.ts +0 -999
  139. package/test/fixtures/selector.ts +0 -103
  140. package/test/fixtures/types.ts +0 -7
  141. package/test/id.ts +0 -102
  142. package/test/index.ts +0 -1148
  143. package/test/parser.ts +0 -117
  144. package/test/pseudo/abandoned.ts +0 -98
  145. package/test/pseudo/attr.ts +0 -298
  146. package/test/pseudo/built.ts +0 -214
  147. package/test/pseudo/confused.ts +0 -160
  148. package/test/pseudo/cve.ts +0 -249
  149. package/test/pseudo/cwe.ts +0 -255
  150. package/test/pseudo/debug.ts +0 -98
  151. package/test/pseudo/deprecated.ts +0 -98
  152. package/test/pseudo/dev.ts +0 -61
  153. package/test/pseudo/diff.ts +0 -417
  154. package/test/pseudo/dist.ts +0 -215
  155. package/test/pseudo/dynamic.ts +0 -98
  156. package/test/pseudo/empty.ts +0 -108
  157. package/test/pseudo/entropic.ts +0 -101
  158. package/test/pseudo/env.ts +0 -98
  159. package/test/pseudo/eval.ts +0 -98
  160. package/test/pseudo/fs.ts +0 -98
  161. package/test/pseudo/helpers.ts +0 -370
  162. package/test/pseudo/host-context.ts +0 -276
  163. package/test/pseudo/hostname.ts +0 -314
  164. package/test/pseudo/license.ts +0 -265
  165. package/test/pseudo/link.ts +0 -94
  166. package/test/pseudo/malware.ts +0 -184
  167. package/test/pseudo/minified.ts +0 -98
  168. package/test/pseudo/missing.ts +0 -111
  169. package/test/pseudo/native.ts +0 -98
  170. package/test/pseudo/network.ts +0 -98
  171. package/test/pseudo/obfuscated.ts +0 -98
  172. package/test/pseudo/optional.ts +0 -73
  173. package/test/pseudo/outdated.ts +0 -461
  174. package/test/pseudo/overridden.ts +0 -317
  175. package/test/pseudo/path.ts +0 -680
  176. package/test/pseudo/peer.ts +0 -101
  177. package/test/pseudo/prerelease.ts +0 -279
  178. package/test/pseudo/private.ts +0 -108
  179. package/test/pseudo/prod.ts +0 -61
  180. package/test/pseudo/published.ts +0 -557
  181. package/test/pseudo/registry.ts +0 -122
  182. package/test/pseudo/root.ts +0 -66
  183. package/test/pseudo/scanned.ts +0 -144
  184. package/test/pseudo/score.ts +0 -591
  185. package/test/pseudo/scripts.ts +0 -324
  186. package/test/pseudo/semver.ts +0 -822
  187. package/test/pseudo/severity.ts +0 -342
  188. package/test/pseudo/shell.ts +0 -98
  189. package/test/pseudo/shrinkwrap.ts +0 -98
  190. package/test/pseudo/skill.ts +0 -189
  191. package/test/pseudo/skills.ts +0 -90
  192. package/test/pseudo/spec.ts +0 -525
  193. package/test/pseudo/squat.ts +0 -565
  194. package/test/pseudo/suspicious.ts +0 -98
  195. package/test/pseudo/tracker.ts +0 -98
  196. package/test/pseudo/trivial.ts +0 -98
  197. package/test/pseudo/type.ts +0 -105
  198. package/test/pseudo/undesirable.ts +0 -98
  199. package/test/pseudo/unknown.ts +0 -98
  200. package/test/pseudo/unmaintained.ts +0 -98
  201. package/test/pseudo/unpopular.ts +0 -98
  202. package/test/pseudo/unstable.ts +0 -98
  203. package/test/pseudo/vuln.ts +0 -625
  204. package/test/pseudo/vulnerable.ts +0 -137
  205. package/test/pseudo/workspace.ts +0 -111
  206. package/test/pseudo.ts +0 -584
  207. package/tsconfig.json +0 -3
  208. package/tsconfig.publish.json +0 -19
  209. package/tsconfig.publish.tsbuildinfo +0 -1
  210. 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
- }