registry-auth-token
Advanced tools
Comparing version 1.0.1 to 1.1.0
27
index.js
@@ -0,9 +1,28 @@ | ||
var path = require('path') | ||
var url = require('url') | ||
var tokenKey = ':_authToken' | ||
module.exports = function (registryUrl) { | ||
module.exports = function (registryUrl, opts) { | ||
var options = opts || {} | ||
var npmrc = require('rc')('npm', {registry: 'https://registry.npmjs.org/'}) | ||
var url = (registryUrl || npmrc.registry).replace(/^https?:/i, '') | ||
var urlAlt = url.slice(-1) === '/' ? url : url + '/' | ||
var parsed = url.parse(registryUrl || npmrc.registry, false, true) | ||
return npmrc[url + tokenKey] || npmrc[urlAlt + tokenKey] | ||
var match | ||
var pathname | ||
while (!match && pathname !== '/') { | ||
pathname = parsed.pathname || '/' | ||
var regUrl = '//' + parsed.hostname + pathname.replace(/\/$/, '') | ||
var urlAlt = regUrl.slice(-1) === '/' ? regUrl : regUrl + '/' | ||
match = npmrc[regUrl + tokenKey] || npmrc[urlAlt + tokenKey] | ||
if (!options.recursive) { | ||
return match | ||
} | ||
parsed.pathname = path.resolve(pathname, '..') | ||
} | ||
return match | ||
} |
{ | ||
"name": "registry-auth-token", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Get the auth token set for an npm registry (if any)", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -23,2 +23,8 @@ # registry-auth-token | ||
console.log(getAuthToken('//registry.foo.bar')) | ||
// Find the registry auth token for a given URL (with deep path): | ||
// If registry is at `//some.host/registry` | ||
// URL passed is `//some.host/registry/deep/path` | ||
// Will find token the closest matching path; `//some.host/registry` | ||
console.log(getAuthToken('//some.host/registry/deep/path', {recursive: true})) | ||
``` | ||
@@ -25,0 +31,0 @@ |
17
test.js
@@ -75,2 +75,19 @@ var fs = require('fs') | ||
}) | ||
it('recursively finds registries for deep url if option is set', function (done, undef) { | ||
var opts = {recursive: true} | ||
var content = [ | ||
'//registry.blah.com/foo:_authToken=whatev', | ||
'//registry.blah.eu:_authToken=yep', '' | ||
].join('\n') | ||
fs.writeFile(npmRcPath, content, function (err) { | ||
var getAuthToken = requireUncached('./index') | ||
assert(!err, err) | ||
assert.equal(getAuthToken('https://registry.blah.com/foo/bar/baz', opts), 'whatev') | ||
assert.equal(getAuthToken('http://registry.blah.eu/what/ever', opts), 'yep') | ||
assert.equal(getAuthToken('//some.registry', opts), undef) | ||
done() | ||
}) | ||
}) | ||
}) |
6823
101
38