Skip to content

Commit 468550f

Browse files
author
Michael Smith
committed
fix: refactor #failureNode, adjust tests and safety
(cherry picked from commit c91ac75)
1 parent cabe249 commit 468550f

3 files changed

Lines changed: 38 additions & 35 deletions

File tree

‎test/lib/commands/install.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ t.test('exec commands', async t => {
242242
const { npm } = await loadMockNpm(t, {
243243
config: {
244244
'allow-git': 'none',
245+
audit: false,
245246
},
246247
})
247248
await t.rejects(
@@ -258,6 +259,7 @@ t.test('exec commands', async t => {
258259
const { npm } = await loadMockNpm(t, {
259260
config: {
260261
'allow-git': 'root',
262+
audit: false,
261263
},
262264
prefixDir: {
263265
'package.json': JSON.stringify({ name: '@npmcli/test-package', version: '1.0.0' }),
@@ -276,6 +278,7 @@ t.test('exec commands', async t => {
276278
const { npm } = await loadMockNpm(t, {
277279
config: {
278280
'allow-directory': 'none',
281+
audit: false,
279282
},
280283
prefixDir: {
281284
'package.json': JSON.stringify({
@@ -301,6 +304,7 @@ t.test('exec commands', async t => {
301304
const { npm } = await loadMockNpm(t, {
302305
config: {
303306
'allow-directory': 'root',
307+
audit: false,
304308
},
305309
prefixDir: {
306310
'package.json': JSON.stringify({
@@ -322,6 +326,7 @@ t.test('exec commands', async t => {
322326
const { npm } = await loadMockNpm(t, {
323327
config: {
324328
'allow-git': 'root',
329+
audit: false,
325330
},
326331
prefixDir: {
327332
'package.json': JSON.stringify({ name: '@npmcli/test-package', version: '1.0.0' }),
@@ -374,6 +379,7 @@ t.test('exec commands', async t => {
374379
const { npm } = await loadMockNpm(t, {
375380
config: {
376381
'allow-remote': 'none',
382+
audit: false,
377383
},
378384
prefixDir: {
379385
'package.json': JSON.stringify({

‎workspaces/arborist/lib/arborist/build-ideal-tree.js‎

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ const Shrinkwrap = require('../shrinkwrap.js')
2828
const { defaultLockfileVersion } = Shrinkwrap
2929
const Node = require('../node.js')
3030
const Link = require('../link.js')
31+
32+
// Maps a parsed spec.type to the corresponding allow-* arborist option name.
33+
// Hoisted to module scope so #checkAllow doesn't re-allocate it per call.
34+
const ALLOW_OPTION_FOR_TYPE = {
35+
git: 'allowGit',
36+
remote: 'allowRemote',
37+
file: 'allowFile',
38+
directory: 'allowDirectory',
39+
}
3140
const addRmPkgDeps = require('../add-rm-pkg-deps.js')
3241
const optionalSet = require('../optional-set.js')
3342
const { checkEngine, checkPlatform } = require('npm-install-checks')
@@ -653,13 +662,7 @@ module.exports = cls => class IdealTreeBuilder extends cls {
653662
// Pacote also enforces these inside FetcherBase.get() as defense-in-depth, but the symlink branch never reaches pacote, and the manifest cache here would bypass pacote on a cached hit.
654663
// Throws the same { code: EALLOW${TYPE} } shape pacote uses, so callers and downstream consumers stay consistent.
655664
#checkAllow (spec, edge) {
656-
const optionFor = {
657-
git: 'allowGit',
658-
remote: 'allowRemote',
659-
file: 'allowFile',
660-
directory: 'allowDirectory',
661-
}
662-
const optName = optionFor[spec.type]
665+
const optName = ALLOW_OPTION_FOR_TYPE[spec.type]
663666
if (!optName) {
664667
return
665668
}
@@ -680,6 +683,20 @@ module.exports = cls => class IdealTreeBuilder extends cls {
680683
)
681684
}
682685

686+
// Builds a Node representing a spec we failed to load (allow-* gate, network failure, ENOTARGET, etc.) and records it in #loadFailures so #pruneFailedOptional can later decide whether the failure is fatal or silently dropped for optional deps.
687+
#failureNode (name, parent, error, edge) {
688+
error.requiredBy = edge?.from?.location || '.'
689+
const n = new Node({
690+
name,
691+
parent,
692+
error,
693+
installLinks: this.installLinks,
694+
legacyPeerDeps: this.legacyPeerDeps,
695+
})
696+
this.#loadFailures.add(n)
697+
return n
698+
}
699+
683700
#queueNamedUpdates () {
684701
// ignore top nodes, since they are not loaded the same way, and
685702
// probably have their own project associated with them.
@@ -1291,16 +1308,7 @@ This is a one-time fix-up, please be patient...
12911308
try {
12921309
this.#checkAllow(spec, edge)
12931310
} catch (error) {
1294-
error.requiredBy = edge?.from?.location || '.'
1295-
const n = new Node({
1296-
name,
1297-
parent,
1298-
error,
1299-
installLinks: this.installLinks,
1300-
legacyPeerDeps: this.legacyPeerDeps,
1301-
})
1302-
this.#loadFailures.add(n)
1303-
return n
1311+
return this.#failureNode(name, parent, error, edge)
13041312
}
13051313

13061314
// pacote will slap integrity on its options, so we have to clone the object so it doesn't get mutated.
@@ -1358,22 +1366,10 @@ This is a one-time fix-up, please be patient...
13581366
// spec isn't a directory, and either isn't a workspace or the workspace we have
13591367
// doesn't satisfy the edge. try to fetch a manifest and build a node from that.
13601368
return this.#fetchManifest(spec, parent, edge)
1361-
.then(pkg => new Node({ name, pkg, parent, installLinks, legacyPeerDeps }), error => {
1362-
error.requiredBy = edge.from.location || '.'
1363-
1364-
// failed to load the spec, either because of enotarget or
1365-
// fetch failure of some other sort. save it so we can verify
1366-
// later that it's optional; otherwise, the error is fatal.
1367-
const n = new Node({
1368-
name,
1369-
parent,
1370-
error,
1371-
installLinks,
1372-
legacyPeerDeps,
1373-
})
1374-
this.#loadFailures.add(n)
1375-
return n
1376-
})
1369+
.then(
1370+
pkg => new Node({ name, pkg, parent, installLinks, legacyPeerDeps }),
1371+
error => this.#failureNode(name, parent, error, edge)
1372+
)
13771373
}
13781374

13791375
// load all peer deps and meta-peer deps into the node's parent

‎workspaces/arborist/lib/arborist/reify.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -881,9 +881,10 @@ module.exports = cls => class Reifier extends cls {
881881
return false
882882
}
883883
try {
884-
const resolvedHost = new URL(node.resolved).hostname
884+
// Hostnames are case-insensitive; lowercase both sides for safety even though WHATWG URL already normalizes.
885+
const resolvedHost = new URL(node.resolved).hostname.toLowerCase()
885886
// pickRegistry only consults spec.scope, so a bare-name (tag) parse is sufficient and avoids a node.version dependency.
886-
const registryHost = new URL(pickRegistry(npa(node.name), this.options)).hostname
887+
const registryHost = new URL(pickRegistry(npa(node.name), this.options)).hostname.toLowerCase()
887888
return resolvedHost === registryHost
888889
} catch {
889890
return false

0 commit comments

Comments
 (0)