diff options
author | Kat Marchán <kzm@sykosomatic.org> | 2017-10-19 04:21:06 +0300 |
---|---|---|
committer | Kat Marchán <kzm@sykosomatic.org> | 2017-10-19 04:21:06 +0300 |
commit | 91d5dca96772bc5c45511ddcbeeb2685c7ea68e8 (patch) | |
tree | eaa59ff6f8bc752896945ba17ed81b361fb54177 /node_modules/npm-profile/index.js | |
parent | d7ef6a20b44e968cb92babab1beb51f99110781d (diff) |
npm-profile@2.0.5
Diffstat (limited to 'node_modules/npm-profile/index.js')
-rw-r--r-- | node_modules/npm-profile/index.js | 114 |
1 files changed, 82 insertions, 32 deletions
diff --git a/node_modules/npm-profile/index.js b/node_modules/npm-profile/index.js index 838bb12af..611200c5f 100644 --- a/node_modules/npm-profile/index.js +++ b/node_modules/npm-profile/index.js @@ -126,32 +126,58 @@ function createToken (password, readonly, cidrs, conf) { return fetchJSON(Object.assign({target: target, method: 'POST', body: props}, conf)) } -function AuthOTP (res) { - Error.call(this) - this.message = 'OTP required for authentication' - Error.captureStackTrace(this, AuthOTP) - this.headers = res.headers.raw() - this.code = 'EOTP' +function FetchError (err, method, target) { + err.method = method + err.href = target + return err } -AuthOTP.prototype = Error.prototype - -function AuthIPAddress (res) { - Error.call(this) - this.message = 'Login is not allowed from your IP address' - Error.captureStackTrace(this, AuthIPAddress) - this.headers = res.headers.raw() - this.code = 'EAUTHIP' + +class HttpErrorBase extends Error { + constructor (method, target, res, body) { + super() + this.headers = res.headers.raw() + this.statusCode = res.status + this.code = 'E' + res.status + this.method = method + this.target = target + this.body = body + this.pkgid = packageName(target) + } } -AuthIPAddress.prototype = Error.prototype - -function AuthUnknown (res) { - Error.call(this) - this.message = 'Unable to authenticate, need: ' + res.headers.get('www-authenticate') - Error.captureStackTrace(this, AuthUnknown) - this.headers = res.headers.raw() - this.code = 'E401' + +class General extends HttpErrorBase { + constructor (method, target, res, body) { + super(method, target, res, body) + this.message = `Registry returned ${this.statusCode} for ${this.method} on ${this.href}` + } +} + +class AuthOTP extends HttpErrorBase { + constructor (method, target, res, body) { + super(method, target, res, body) + this.message = 'OTP required for authentication' + this.code = 'EOTP' + Error.captureStackTrace(this, AuthOTP) + } +} + +class AuthIPAddress extends HttpErrorBase { + constructor (res, body) { + super(method, target, res, body) + this.message = 'Login is not allowed from your IP address' + this.code = 'EAUTHIP' + Error.captureStackTrace(this, AuthIPAddress) + } +} + +class AuthUnknown extends HttpErrorBase { + constructor (method, target, res, body) { + super(method, target, res, body) + this.message = 'Unable to authenticate, need: ' + res.headers.get('www-authenticate') + this.code = 'EAUTHIP' + Error.captureStackTrace(this, AuthIPAddress) + } } -AuthUnknown.prototype = Error.prototype function authHeaders (auth) { const headers = {} @@ -175,7 +201,10 @@ function fetchJSON (conf) { fetchOpts.headers['Content-Type'] = 'application/json' fetchOpts.body = JSON.stringify(conf.body) } - return fetch.defaults(conf.opts || {})(conf.target, fetchOpts).then(res => { + process.emit('log', 'http', 'request', '→',conf.method || 'GET', conf.target) + return fetch.defaults(conf.opts || {})(conf.target, fetchOpts).catch(err => { + throw new FetchError(err, conf.method, conf.target) + }).then(res => { if (res.headers.get('content-type') === 'application/json') { return res.json().then(content => [res, content]) } else { @@ -190,21 +219,42 @@ function fetchJSON (conf) { }).then(result => { const res = result[0] const content = result[1] + process.emit('log', 'http', res.status, `← ${res.statusText} (${conf.target})`) if (res.status === 401 && res.headers.get('www-authenticate')) { const auth = res.headers.get('www-authenticate').split(/,\s*/).map(s => s.toLowerCase()) if (auth.indexOf('ipaddress') !== -1) { - throw new AuthIPAddress(res) + throw new AuthIPAddress(conf.method, conf.target, res, content) } else if (auth.indexOf('otp') !== -1) { - throw new AuthOTP(res) + throw new AuthOTP(conf.method, conf.target, res, content) } else { - throw new AuthUnknown(res) + throw new AuthUnknown(conf.method, conf.target, res, content) } } else if (res.status < 200 || res.status >= 300) { - const err = new Error(res.statusText) - err.code = 'E' + res.status - err.headers = res.headers.raw() - throw err + if (typeof content === 'object' && content.error) { + return content + } else { + throw new General(conf.method, conf.target, res, content) + } + } else { + return content } - return content }) } + +function packageName (href) { + try { + let basePath = url.parse(href).pathname.substr(1) + if (!basePath.match(/^-/)) { + basePath = basePath.split('/') + var index = basePath.indexOf('_rewrite') + if (index === -1) { + index = basePath.length - 1 + } else { + index++ + } + return decodeURIComponent(basePath[index]) + } + } catch (_) { + // this is ok + } +}
\ No newline at end of file |