Skip to content
Permalink
Browse files
fix: resolve relative urls that start with file://
  • Loading branch information
lukekarrys committed Sep 28, 2022
1 parent f0c7895 commit 8383fdc240280e8e3cb3dac8d9bb588752bd2656
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
@@ -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:/, '')
@@ -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,
@@ -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,
@@ -517,6 +553,7 @@ t.test('basic', function (t) {
escapedName: null,
type: 'directory',
saveSpec: 'file:',
fetchSpec: '/test/a/b',
raw: 'file://.',
},

0 comments on commit 8383fdc

Please sign in to comment.