Skip to content

Commit

Permalink
fix: Use WhatWG URLs instead of url.parse
Browse files Browse the repository at this point in the history
The better to make standard happy.
  • Loading branch information
isaacs committed Dec 17, 2019
1 parent 510b125 commit 8ccfa8a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
7 changes: 4 additions & 3 deletions auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ function addKey (opts, obj, scope, key, objKey) {
// Called a nerf dart in the main codebase. Used as a "safe"
// key when fetching registry info from config.
function registryKey (registry) {
const parsed = url.parse(registry)
const parsed = new url.URL(registry)
const formatted = url.format({
protocol: parsed.protocol,
host: parsed.host,
pathname: parsed.pathname,
slashes: parsed.slashes
slashes: true
})
return url.resolve(formatted, '.')
return url.format(new url.URL('.', formatted)).replace(/^[^:]+:/, '')
}
2 changes: 1 addition & 1 deletion errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const url = require('url')

function packageName (href) {
try {
let basePath = url.parse(href).pathname.substr(1)
let basePath = new url.URL(href).pathname.substr(1)
if (!basePath.match(/^-/)) {
basePath = basePath.split('/')
var index = basePath.indexOf('_rewrite')
Expand Down
40 changes: 21 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ const url = require('url')
const zlib = require('minizlib')
const Minipass = require('minipass')

// WhatWG URL throws if it's not fully resolved
const urlIsValid = u => {
try {
return !!new url.URL(u)
} catch (_) {
return false
}
}

module.exports = regFetch
function regFetch (uri, opts) {
opts = config(opts)
Expand All @@ -23,13 +32,13 @@ function regFetch (uri, opts) {
'https://registry.npmjs.org/'
)

uri = url.parse(uri).protocol
? uri
: `${
if (!urlIsValid(uri)) {
uri = `${
registry.trim().replace(/\/?$/g, '')
}/${
uri.trim().replace(/^\//, '')
}`
}

const method = opts.method ||
/* istanbul ignore next: default set in figgy pudding config */
Expand Down Expand Up @@ -64,24 +73,17 @@ function regFetch (uri, opts) {
}

if (opts.query) {
let q = opts.query
if (typeof q === 'string') {
q = qs.parse(q)
}
const q = typeof opts.query === 'string'
? qs.parse(opts.query)
: opts.query

const parsed = new url.URL(uri)
Object.keys(q).forEach(key => {
if (q[key] === undefined) {
delete q[key]
if (q[key] !== undefined) {
parsed.searchParams.set(key, q[key])
}
})
if (Object.keys(q).length) {
const parsed = url.parse(uri)
parsed.search = '?' + qs.stringify(
parsed.query
? Object.assign(qs.parse(parsed.query), q)
: q
)
uri = url.format(parsed)
}
uri = url.format(parsed)
}

const doFetch = (body) => fetch(uri, {
Expand Down Expand Up @@ -185,7 +187,7 @@ function getHeaders (registry, uri, opts) {
// credentials on `alwaysAuth`
const shouldAuth = (
auth.alwaysAuth ||
url.parse(uri).host === url.parse(registry).host
new url.URL(uri).host === new url.URL(registry).host
)
if (shouldAuth && auth.token) {
headers.authorization = `Bearer ${auth.token}`
Expand Down

0 comments on commit 8ccfa8a

Please sign in to comment.