From 97b24410bf5cb495e498ba6fedd360a6c2b47cad Mon Sep 17 00:00:00 2001 From: Luke Karrys Date: Wed, 28 Sep 2022 14:01:27 -0700 Subject: [PATCH] fix: resolve relative urls that start with file:// --- lib/npa.js | 6 ++++-- test/basic.js | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/lib/npa.js b/lib/npa.js index 61fee07..cd1932b 100644 --- a/lib/npa.js +++ b/lib/npa.js @@ -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:/, '') diff --git a/test/basic.js b/test/basic.js index 404c2e8..b543c5e 100644 --- a/test/basic.js +++ b/test/basic.js @@ -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://.', },