Skip to content

Commit

Permalink
fix: resolve relative urls that start with file://
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Sep 28, 2022
1 parent f0c7895 commit 3176a05
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/npa.js
Expand Up @@ -241,8 +241,10 @@ function fromFile (res, where) {
rawNoPrefix = rawSpec.replace(/^file:/, '')
}
// turn file:/../foo into file:../foo
if (/^\/\.\.?(\/|$)/.test(rawNoPrefix)) {
const rawSpec = res.rawSpec.replace(/^file:\//, 'file:')
// for 1, 2 or 3 leading slashes since we attempted
// in the previous step to make it a file protocol url with a leading slash
if (/^\/{1,3}\.\.?(\/|$)/.test(rawNoPrefix)) {
const rawSpec = res.rawSpec.replace(/^file:\/{1,3}/, 'file:')
resolvedUrl = new url.URL(rawSpec, `file://${path.resolve(where)}/`)
specUrl = new url.URL(rawSpec)
rawNoPrefix = rawSpec.replace(/^file:/, '')
Expand Down
37 changes: 37 additions & 0 deletions test/basic.js
Expand Up @@ -461,6 +461,33 @@ t.test('basic', function (t) {
raw: 'file:/.path/to/foo',
},

'file:./path/to/foo': {
name: null,
escapedName: null,
type: 'directory',
saveSpec: 'file:path/to/foo',
fetchSpec: '/test/a/b/path/to/foo',
raw: 'file:./path/to/foo',
},

'file:/./path/to/foo': {
name: null,
escapedName: null,
type: 'directory',
saveSpec: 'file:path/to/foo',
fetchSpec: '/test/a/b/path/to/foo',
raw: 'file:/./path/to/foo',
},

'file://./path/to/foo': {
name: null,
escapedName: null,
type: 'directory',
saveSpec: 'file:path/to/foo',
fetchSpec: '/test/a/b/path/to/foo',
raw: 'file://./path/to/foo',
},

'file:../path/to/foo': {
name: null,
escapedName: null,
Expand All @@ -479,6 +506,15 @@ t.test('basic', function (t) {
raw: 'file:/../path/to/foo',
},

'file://../path/to/foo': {
name: null,
escapedName: null,
type: 'directory',
saveSpec: 'file:../path/to/foo',
fetchSpec: '/test/a/path/to/foo',
raw: 'file://../path/to/foo',
},

'file:///path/to/foo': {
name: null,
escapedName: null,
Expand Down Expand Up @@ -517,6 +553,7 @@ t.test('basic', function (t) {
escapedName: null,
type: 'directory',
saveSpec: 'file:',
fetchSpec: '/test/a/b',
raw: 'file://.',
},

Expand Down

0 comments on commit 3176a05

Please sign in to comment.