Skip to content

Commit 9344167

Browse files
wraithgarlukekarrys
authored andcommitted
fix: remove strict 8909 mode
BREAKING CHANGE: the strict RFC 8909 mode has been removed
1 parent d2ab7ba commit 9344167

File tree

2 files changed

+26
-62
lines changed

2 files changed

+26
-62
lines changed

lib/npa.js

+15-32
Original file line numberDiff line numberDiff line change
@@ -257,40 +257,23 @@ function fromFile (res, where) {
257257
})
258258
}
259259

260-
// environment switch for testing
261-
if (process.env.NPM_PACKAGE_ARG_8909_STRICT !== '1') {
262-
// XXX backwards compatibility lack of compliance with 8909
263-
// Remove when we want a breaking change to come into RFC compliance.
264-
if (resolvedUrl.host && resolvedUrl.host !== 'localhost') {
265-
const rawSpec = res.rawSpec.replace(/^file:\/\//, 'file:///')
266-
resolvedUrl = new url.URL(rawSpec, `file://${path.resolve(where)}/`)
267-
specUrl = new url.URL(rawSpec)
268-
rawNoPrefix = rawSpec.replace(/^file:/, '')
269-
}
270-
// turn file:/../foo into file:../foo
271-
// for 1, 2 or 3 leading slashes since we attempted
272-
// in the previous step to make it a file protocol url with a leading slash
273-
if (/^\/{1,3}\.\.?(\/|$)/.test(rawNoPrefix)) {
274-
const rawSpec = res.rawSpec.replace(/^file:\/{1,3}/, 'file:')
275-
resolvedUrl = new url.URL(rawSpec, `file://${path.resolve(where)}/`)
276-
specUrl = new url.URL(rawSpec)
277-
rawNoPrefix = rawSpec.replace(/^file:/, '')
278-
}
279-
// XXX end 8909 violation backwards compatibility section
280-
}
281-
282-
// file:foo - relative url to ./foo
283-
// file:/foo - absolute path /foo
284-
// file:///foo - absolute path to /foo, no authority host
285-
// file://localhost/foo - absolute path to /foo, on localhost
286-
// file://foo - absolute path to / on foo host (error!)
260+
// XXX backwards compatibility lack of compliance with RFC 8909
287261
if (resolvedUrl.host && resolvedUrl.host !== 'localhost') {
288-
const msg = `Invalid file: URL, must be absolute if // present`
289-
throw Object.assign(new Error(msg), {
290-
raw: res.rawSpec,
291-
parsed: resolvedUrl,
292-
})
262+
const rawSpec = res.rawSpec.replace(/^file:\/\//, 'file:///')
263+
resolvedUrl = new url.URL(rawSpec, `file://${path.resolve(where)}/`)
264+
specUrl = new url.URL(rawSpec)
265+
rawNoPrefix = rawSpec.replace(/^file:/, '')
266+
}
267+
// turn file:/../foo into file:../foo
268+
// for 1, 2 or 3 leading slashes since we attempted
269+
// in the previous step to make it a file protocol url with a leading slash
270+
if (/^\/{1,3}\.\.?(\/|$)/.test(rawNoPrefix)) {
271+
const rawSpec = res.rawSpec.replace(/^file:\/{1,3}/, 'file:')
272+
resolvedUrl = new url.URL(rawSpec, `file://${path.resolve(where)}/`)
273+
specUrl = new url.URL(rawSpec)
274+
rawNoPrefix = rawSpec.replace(/^file:/, '')
293275
}
276+
// XXX end RFC 8909 violation backwards compatibility section
294277

295278
// turn /C:/blah into just C:/blah on windows
296279
let specPath = decodeURIComponent(specUrl.pathname)

test/basic.js

+11-30
Original file line numberDiff line numberDiff line change
@@ -731,38 +731,19 @@ t.test('basic', function (t) {
731731
t.end()
732732
})
733733

734-
t.test('strict 8909 compliance mode', t => {
735-
t.teardown(() => process.env.NPM_PACKAGE_ARG_8909_STRICT = '0')
736-
process.env.NPM_PACKAGE_ARG_8909_STRICT = '1'
737-
738-
t.throws(() => npa('file://.'), {
739-
message: 'Invalid file: URL, must be absolute if // present',
740-
raw: 'file://.',
741-
})
742-
743-
t.throws(() => npa('file://some/relative/path'), {
744-
message: 'Invalid file: URL, must be absolute if // present',
745-
raw: 'file://some/relative/path',
746-
})
747-
748-
// I cannot for the life of me figure out how to make new URL('file:...')
749-
// actually fail to parse. it seems like it accepts any garbage you can
750-
// throw at it. However, because it theoretically CAN throw, here's a test.
751-
t.throws(() => {
752-
const broken = t.mock('..', {
753-
url: {
754-
URL: class {
755-
constructor () {
756-
throw new Error('thansk i haet it')
757-
}
758-
},
734+
t.test('invalid url', t => {
735+
const broken = t.mock('..', {
736+
url: {
737+
URL: class {
738+
constructor () {
739+
throw new Error('something went wrong')
740+
}
759741
},
760-
})
761-
broken('file:thansk i haet it')
762-
}, {
763-
message: 'Invalid file: URL, must comply with RFC 8909',
742+
},
764743
})
765-
744+
t.throws(() => broken('file:./test'),
745+
{ message: 'Invalid file: URL' }
746+
)
766747
t.end()
767748
})
768749

0 commit comments

Comments
 (0)