Skip to content

Commit

Permalink
Merge pull request #30 from boennemann/feat-private-modules
Browse files Browse the repository at this point in the history
feat(npm-info): support private modules
  • Loading branch information
christophwitzko committed Jun 11, 2015
2 parents db39c79 + 2582896 commit 1db531b
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 22 deletions.
59 changes: 38 additions & 21 deletions lib/npm-info.js
@@ -1,28 +1,45 @@
'use strict'

var async = require('async')
var npmconf = require('npmconf')
var request = require('request')

var efh = require('./error').efh

module.exports = function (pkgName, cb) {
var encodedPkgName = pkgName.replace(/\//g, '%2F')
request(process.env.npm_config_registry + encodedPkgName, efh(cb)(function (response, body) {
var res = {
version: null,
gitHead: null,
pkg: null
var registry = process.env.npm_config_registry
async.waterfall([
npmconf.load,
function (conf, callback) {
var cred = conf.getCredentialsByURI(registry)
var reqopts = {
url: registry + pkgName.replace(/\//g, '%2F'),
headers: {}
}
if (cred.token) {
reqopts.headers.Authorization = 'Bearer ' + cred.token
} else if (cred.auth) {
reqopts.headers.Authorization = 'Basic ' + cred.auth
}
callback(null, reqopts)
},
request,
function (response, body, callback) {
var res = {
version: null,
gitHead: null,
pkg: null
}

if (response.statusCode === 404 || !body) return callback(null, res)

var pkg = JSON.parse(body)

if (pkg.error) return callback(pkg.error)

res.version = pkg['dist-tags'].latest
res.gitHead = pkg.versions[res.version].gitHead
res.pkg = pkg

callback(null, res)
}

if (response.statusCode === 404 || !body) return cb(null, res)

var pkg = JSON.parse(body)

if (pkg.error) return cb(pkg.error)

res.version = pkg['dist-tags'].latest
res.gitHead = pkg.versions[res.version].gitHead
res.pkg = pkg

cb(null, res)
}))
], cb)
}
5 changes: 4 additions & 1 deletion package.json
Expand Up @@ -9,13 +9,15 @@
},
"dependencies": {
"abbrev": "^1.0.5",
"async": "^1.0.0",
"conventional-changelog": "0.0.17",
"error-first-handler": "^1.0.1",
"git-head": "^1.2.1",
"github": "0.2.4",
"github-url-from-git": "^1.4.0",
"ini": "^1.3.2",
"minimist": "^1.1.0",
"npmconf": "^2.1.2",
"parse-github-repo-url": "^1.0.0",
"request": "^2.53.0",
"semver": "^4.3.3"
Expand All @@ -26,8 +28,9 @@
"lodash.defaults": "^3.0.0",
"nano-uid": "^0.2.0",
"nixt": "^0.4.1",
"nock": "^2.2.0",
"sinopia": "^1.0.0",
"standard": "^3.2.1",
"standard": "^3.11.1",
"tap-spec": "^3.0.0",
"tape": "^4.0.0"
},
Expand Down
1 change: 1 addition & 0 deletions tests/.npmrc
@@ -1,3 +1,4 @@
_auth=dGVzdDpmb28=
email=test@example.com
registry=http://127.0.0.1:4873/
//registry.npmjs.org/:_authToken=testtoken
1 change: 1 addition & 0 deletions tests/index.js
Expand Up @@ -4,6 +4,7 @@ var test = require('tape')

var createModule = require('./lib/create-module')

require('./tap/npm-info')(test)
require('./scenarios/custom-analyzer')(test, createModule)
require('./scenarios/custom-verification')(test, createModule)
require('./scenarios/ignore')(test, createModule)
Expand Down
52 changes: 52 additions & 0 deletions tests/tap/npm-info.js
@@ -0,0 +1,52 @@
'use strict'

var nock = require('nock')

var npmInfo = require('../../lib/npm-info.js')

var registry = 'http://registry.npmjs.org/'

var defaultModule = {
'dist-tags': {
latest: '1.0.0'
},
versions: {
'1.0.0': {
gitHead: 'HEAD'
}
}
}

process.env.npm_config_registry = registry

module.exports = function (test) {
test('npm-info', function (t) {
var regMock = nock(registry, {
reqheaders: {
'authorization': 'Bearer testtoken'
}
})
.get('/express')
.reply(200, defaultModule)
.get('/@user%2Fmodule')
.reply(200, defaultModule)

t.test('get unscoped module', function (t) {
t.plan(3)
npmInfo('express', function (err, info) {
t.error(err, 'error')
t.is(info.version, '1.0.0', 'version')
t.is(info.gitHead, 'HEAD', 'gitHead')
})
})
t.test('get scoped module', function (t) {
t.plan(3)
npmInfo('@user/module', function (err, info) {
t.error(err, 'error')
t.is(info.version, '1.0.0', 'version')
t.is(info.gitHead, 'HEAD', 'gitHead')
regMock.done()
})
})
})
}

0 comments on commit 1db531b

Please sign in to comment.