Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: resolve relative urls that start with file:// #108

Merged
merged 1 commit into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/npa.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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