Skip to content

Commit 7b9cb25

Browse files
committed
fix: resolve relative urls that start with file://
1 parent 749ccad commit 7b9cb25

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

lib/npa.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,10 @@ function fromFile (res, where) {
241241
rawNoPrefix = rawSpec.replace(/^file:/, '')
242242
}
243243
// turn file:/../foo into file:../foo
244-
if (/^\/\.\.?(\/|$)/.test(rawNoPrefix)) {
245-
const rawSpec = res.rawSpec.replace(/^file:\//, 'file:')
244+
// for 1, 2 or 3 leading slashes since we attempted
245+
// in the previous step to make it a file protocol url with a leading slash
246+
if (/^\/{1,3}\.\.?(\/|$)/.test(rawNoPrefix)) {
247+
const rawSpec = res.rawSpec.replace(/^file:\/{1,3}/, 'file:')
246248
resolvedUrl = new url.URL(rawSpec, `file://${path.resolve(where)}/`)
247249
specUrl = new url.URL(rawSpec)
248250
rawNoPrefix = rawSpec.replace(/^file:/, '')

test/basic.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,33 @@ t.test('basic', function (t) {
461461
raw: 'file:/.path/to/foo',
462462
},
463463

464+
'file:./path/to/foo': {
465+
name: null,
466+
escapedName: null,
467+
type: 'directory',
468+
saveSpec: 'file:path/to/foo',
469+
fetchSpec: '/test/a/b/path/to/foo',
470+
raw: 'file:./path/to/foo',
471+
},
472+
473+
'file:/./path/to/foo': {
474+
name: null,
475+
escapedName: null,
476+
type: 'directory',
477+
saveSpec: 'file:path/to/foo',
478+
fetchSpec: '/test/a/b/path/to/foo',
479+
raw: 'file:/./path/to/foo',
480+
},
481+
482+
'file://./path/to/foo': {
483+
name: null,
484+
escapedName: null,
485+
type: 'directory',
486+
saveSpec: 'file:path/to/foo',
487+
fetchSpec: '/test/a/b/path/to/foo',
488+
raw: 'file://./path/to/foo',
489+
},
490+
464491
'file:../path/to/foo': {
465492
name: null,
466493
escapedName: null,
@@ -479,6 +506,15 @@ t.test('basic', function (t) {
479506
raw: 'file:/../path/to/foo',
480507
},
481508

509+
'file://../path/to/foo': {
510+
name: null,
511+
escapedName: null,
512+
type: 'directory',
513+
saveSpec: 'file:../path/to/foo',
514+
fetchSpec: '/test/a/path/to/foo',
515+
raw: 'file://../path/to/foo',
516+
},
517+
482518
'file:///path/to/foo': {
483519
name: null,
484520
escapedName: null,
@@ -517,6 +553,7 @@ t.test('basic', function (t) {
517553
escapedName: null,
518554
type: 'directory',
519555
saveSpec: 'file:',
556+
fetchSpec: '/test/a/b',
520557
raw: 'file://.',
521558
},
522559

0 commit comments

Comments
 (0)