/
edge.js
294 lines (248 loc) · 6.82 KB
/
edge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// An edge in the dependency graph
// Represents a dependency relationship of some kind
const util = require('util')
const npa = require('npm-package-arg')
const depValid = require('./dep-valid.js')
const _from = Symbol('_from')
const _to = Symbol('_to')
const _type = Symbol('_type')
const _spec = Symbol('_spec')
const _accept = Symbol('_accept')
const _name = Symbol('_name')
const _error = Symbol('_error')
const _loadError = Symbol('_loadError')
const _setFrom = Symbol('_setFrom')
const _explain = Symbol('_explain')
const _explanation = Symbol('_explanation')
const types = new Set([
'prod',
'dev',
'optional',
'peer',
'peerOptional',
'workspace',
])
class ArboristEdge {}
const printableEdge = (edge) => {
const edgeFrom = edge.from && edge.from.location
const edgeTo = edge.to && edge.to.location
const override = edge.overrides && edge.overrides.value
return Object.assign(new ArboristEdge(), {
name: edge.name,
spec: edge.spec,
type: edge.type,
...(edgeFrom != null ? { from: edgeFrom } : {}),
...(edgeTo ? { to: edgeTo } : {}),
...(edge.error ? { error: edge.error } : {}),
...(edge.peerConflicted ? { peerConflicted: true } : {}),
...(override ? { overridden: override } : {}),
})
}
class Edge {
constructor (options) {
const { type, name, spec, accept, from, overrides } = options
if (typeof spec !== 'string') {
throw new TypeError('must provide string spec')
}
if (type === 'workspace' && npa(spec).type !== 'directory') {
throw new TypeError('workspace edges must be a symlink')
}
this[_spec] = spec
if (overrides !== undefined) {
this.overrides = overrides
}
if (accept !== undefined) {
if (typeof accept !== 'string') {
throw new TypeError('accept field must be a string if provided')
}
this[_accept] = accept || '*'
}
if (typeof name !== 'string') {
throw new TypeError('must provide dependency name')
}
this[_name] = name
if (!types.has(type)) {
throw new TypeError(
`invalid type: ${type}\n` +
`(valid types are: ${Edge.types.join(', ')})`)
}
this[_type] = type
if (!from) {
throw new TypeError('must provide "from" node')
}
this[_setFrom](from)
this[_error] = this[_loadError]()
this.peerConflicted = false
}
satisfiedBy (node) {
if (node.name !== this.name) {
return false
}
// NOTE: this condition means we explicitly do not support overriding
// bundled or shrinkwrapped dependencies
const spec = (node.hasShrinkwrap || node.inShrinkwrap || node.inBundle)
? this.rawSpec
: this.spec
return depValid(node, spec, this.accept, this.from)
}
explain (seen = []) {
if (this[_explanation]) {
return this[_explanation]
}
return this[_explanation] = this[_explain](seen)
}
// return the edge data, and an explanation of how that edge came to be here
[_explain] (seen) {
const { error, from, bundled } = this
return {
type: this.type,
name: this.name,
spec: this.spec,
...(this.rawSpec !== this.spec ? {
rawSpec: this.rawSpec,
overridden: true,
} : {}),
...(bundled ? { bundled } : {}),
...(error ? { error } : {}),
...(from ? { from: from.explain(null, seen) } : {}),
}
}
get bundled () {
if (!this.from) {
return false
}
const { package: { bundleDependencies = [] } } = this.from
return bundleDependencies.includes(this.name)
}
get workspace () {
return this[_type] === 'workspace'
}
get prod () {
return this[_type] === 'prod'
}
get dev () {
return this[_type] === 'dev'
}
get optional () {
return this[_type] === 'optional' || this[_type] === 'peerOptional'
}
get peer () {
return this[_type] === 'peer' || this[_type] === 'peerOptional'
}
get type () {
return this[_type]
}
get name () {
return this[_name]
}
get rawSpec () {
return this[_spec]
}
get spec () {
if (this.overrides && this.overrides.value && this.overrides.name === this.name) {
if (this.overrides.value.startsWith('$')) {
const ref = this.overrides.value.slice(1)
// we may be a virtual root, if we are we want to resolve reference overrides
// from the real root, not the virtual one
const pkg = this.from.sourceReference
? this.from.sourceReference.root.package
: this.from.root.package
const overrideSpec = (pkg.devDependencies && pkg.devDependencies[ref]) ||
(pkg.optionalDependencies && pkg.optionalDependencies[ref]) ||
(pkg.dependencies && pkg.dependencies[ref]) ||
(pkg.peerDependencies && pkg.peerDependencies[ref])
if (overrideSpec) {
return overrideSpec
}
throw new Error(`Unable to resolve reference ${this.overrides.value}`)
}
return this.overrides.value
}
return this[_spec]
}
get accept () {
return this[_accept]
}
get valid () {
return !this.error
}
get missing () {
return this.error === 'MISSING'
}
get invalid () {
return this.error === 'INVALID'
}
get peerLocal () {
return this.error === 'PEER LOCAL'
}
get error () {
this[_error] = this[_error] || this[_loadError]()
return this[_error] === 'OK' ? null : this[_error]
}
[_loadError] () {
return !this[_to] ? (this.optional ? null : 'MISSING')
: this.peer && this.from === this.to.parent && !this.from.isTop ? 'PEER LOCAL'
: !this.satisfiedBy(this.to) ? 'INVALID'
: 'OK'
}
reload (hard = false) {
this[_explanation] = null
if (this[_from].overrides) {
this.overrides = this[_from].overrides.getEdgeRule(this)
} else {
delete this.overrides
}
const newTo = this[_from].resolve(this.name)
if (newTo !== this[_to]) {
if (this[_to]) {
this[_to].edgesIn.delete(this)
}
this[_to] = newTo
this[_error] = this[_loadError]()
if (this[_to]) {
this[_to].addEdgeIn(this)
}
} else if (hard) {
this[_error] = this[_loadError]()
}
}
detach () {
this[_explanation] = null
if (this[_to]) {
this[_to].edgesIn.delete(this)
}
this[_from].edgesOut.delete(this.name)
this[_to] = null
this[_error] = 'DETACHED'
this[_from] = null
}
[_setFrom] (node) {
this[_explanation] = null
this[_from] = node
if (node.edgesOut.has(this.name)) {
node.edgesOut.get(this.name).detach()
}
node.addEdgeOut(this)
this.reload()
}
get from () {
return this[_from]
}
get to () {
return this[_to]
}
toJSON () {
return printableEdge(this)
}
[util.inspect.custom] () {
return this.toJSON()
}
}
Edge.types = [...types]
Edge.errors = [
'DETACHED',
'MISSING',
'PEER LOCAL',
'INVALID',
]
module.exports = Edge