diff --git a/lib/npa.js b/lib/npa.js index 36bd18c..f5ede23 100644 --- a/lib/npa.js +++ b/lib/npa.js @@ -257,40 +257,23 @@ function fromFile (res, where) { }) } - // environment switch for testing - if (process.env.NPM_PACKAGE_ARG_8909_STRICT !== '1') { - // XXX backwards compatibility lack of compliance with 8909 - // Remove when we want a breaking change to come into RFC compliance. - if (resolvedUrl.host && resolvedUrl.host !== 'localhost') { - const rawSpec = res.rawSpec.replace(/^file:\/\//, 'file:///') - resolvedUrl = new url.URL(rawSpec, `file://${path.resolve(where)}/`) - specUrl = new url.URL(rawSpec) - rawNoPrefix = rawSpec.replace(/^file:/, '') - } - // turn file:/../foo into file:../foo - // 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:/, '') - } - // XXX end 8909 violation backwards compatibility section - } - - // file:foo - relative url to ./foo - // file:/foo - absolute path /foo - // file:///foo - absolute path to /foo, no authority host - // file://localhost/foo - absolute path to /foo, on localhost - // file://foo - absolute path to / on foo host (error!) + // XXX backwards compatibility lack of compliance with RFC 8909 if (resolvedUrl.host && resolvedUrl.host !== 'localhost') { - const msg = `Invalid file: URL, must be absolute if // present` - throw Object.assign(new Error(msg), { - raw: res.rawSpec, - parsed: resolvedUrl, - }) + const rawSpec = res.rawSpec.replace(/^file:\/\//, 'file:///') + resolvedUrl = new url.URL(rawSpec, `file://${path.resolve(where)}/`) + specUrl = new url.URL(rawSpec) + rawNoPrefix = rawSpec.replace(/^file:/, '') + } + // turn file:/../foo into file:../foo + // 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:/, '') } + // XXX end RFC 8909 violation backwards compatibility section // turn /C:/blah into just C:/blah on windows let specPath = decodeURIComponent(specUrl.pathname) diff --git a/test/basic.js b/test/basic.js index 13b4cce..7553e7a 100644 --- a/test/basic.js +++ b/test/basic.js @@ -731,38 +731,19 @@ t.test('basic', function (t) { t.end() }) -t.test('strict 8909 compliance mode', t => { - t.teardown(() => process.env.NPM_PACKAGE_ARG_8909_STRICT = '0') - process.env.NPM_PACKAGE_ARG_8909_STRICT = '1' - - t.throws(() => npa('file://.'), { - message: 'Invalid file: URL, must be absolute if // present', - raw: 'file://.', - }) - - t.throws(() => npa('file://some/relative/path'), { - message: 'Invalid file: URL, must be absolute if // present', - raw: 'file://some/relative/path', - }) - - // I cannot for the life of me figure out how to make new URL('file:...') - // actually fail to parse. it seems like it accepts any garbage you can - // throw at it. However, because it theoretically CAN throw, here's a test. - t.throws(() => { - const broken = t.mock('..', { - url: { - URL: class { - constructor () { - throw new Error('thansk i haet it') - } - }, +t.test('invalid url', t => { + const broken = t.mock('..', { + url: { + URL: class { + constructor () { + throw new Error('something went wrong') + } }, - }) - broken('file:thansk i haet it') - }, { - message: 'Invalid file: URL, must comply with RFC 8909', + }, }) - + t.throws(() => broken('file:./test'), + { message: 'Invalid file: URL' } + ) t.end() })