Skip to content

Commit

Permalink
fix: remove strict 8909 mode
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the strict RFC 8909 mode has been removed
  • Loading branch information
wraithgar authored and lukekarrys committed Aug 15, 2023
1 parent d2ab7ba commit 9344167
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 62 deletions.
47 changes: 15 additions & 32 deletions lib/npa.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
41 changes: 11 additions & 30 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

Expand Down

0 comments on commit 9344167

Please sign in to comment.