Merge pull request #30 from boennemann/feat-private-modules
feat(npm-info): support private modules
This commit is contained in:
		
						commit
						1db531b511
					
				| @ -1,28 +1,45 @@ | |||||||
| 'use strict' | 'use strict' | ||||||
| 
 | 
 | ||||||
|  | var async = require('async') | ||||||
|  | var npmconf = require('npmconf') | ||||||
| var request = require('request') | var request = require('request') | ||||||
| 
 | 
 | ||||||
| var efh = require('./error').efh |  | ||||||
| 
 |  | ||||||
| module.exports = function (pkgName, cb) { | module.exports = function (pkgName, cb) { | ||||||
|   var encodedPkgName = pkgName.replace(/\//g, '%2F') |   var registry = process.env.npm_config_registry | ||||||
|   request(process.env.npm_config_registry + encodedPkgName, efh(cb)(function (response, body) { |   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 = { |       var res = { | ||||||
|         version: null, |         version: null, | ||||||
|         gitHead: null, |         gitHead: null, | ||||||
|         pkg: null |         pkg: null | ||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|     if (response.statusCode === 404 || !body) return cb(null, res) |       if (response.statusCode === 404 || !body) return callback(null, res) | ||||||
| 
 | 
 | ||||||
|       var pkg = JSON.parse(body) |       var pkg = JSON.parse(body) | ||||||
| 
 | 
 | ||||||
|     if (pkg.error) return cb(pkg.error) |       if (pkg.error) return callback(pkg.error) | ||||||
| 
 | 
 | ||||||
|       res.version = pkg['dist-tags'].latest |       res.version = pkg['dist-tags'].latest | ||||||
|       res.gitHead = pkg.versions[res.version].gitHead |       res.gitHead = pkg.versions[res.version].gitHead | ||||||
|       res.pkg = pkg |       res.pkg = pkg | ||||||
| 
 | 
 | ||||||
|     cb(null, res) |       callback(null, res) | ||||||
|   })) |     } | ||||||
|  |   ], cb) | ||||||
| } | } | ||||||
|  | |||||||
| @ -9,6 +9,7 @@ | |||||||
|   }, |   }, | ||||||
|   "dependencies": { |   "dependencies": { | ||||||
|     "abbrev": "^1.0.5", |     "abbrev": "^1.0.5", | ||||||
|  |     "async": "^1.0.0", | ||||||
|     "conventional-changelog": "0.0.17", |     "conventional-changelog": "0.0.17", | ||||||
|     "error-first-handler": "^1.0.1", |     "error-first-handler": "^1.0.1", | ||||||
|     "git-head": "^1.2.1", |     "git-head": "^1.2.1", | ||||||
| @ -16,6 +17,7 @@ | |||||||
|     "github-url-from-git": "^1.4.0", |     "github-url-from-git": "^1.4.0", | ||||||
|     "ini": "^1.3.2", |     "ini": "^1.3.2", | ||||||
|     "minimist": "^1.1.0", |     "minimist": "^1.1.0", | ||||||
|  |     "npmconf": "^2.1.2", | ||||||
|     "parse-github-repo-url": "^1.0.0", |     "parse-github-repo-url": "^1.0.0", | ||||||
|     "request": "^2.53.0", |     "request": "^2.53.0", | ||||||
|     "semver": "^4.3.3" |     "semver": "^4.3.3" | ||||||
| @ -26,8 +28,9 @@ | |||||||
|     "lodash.defaults": "^3.0.0", |     "lodash.defaults": "^3.0.0", | ||||||
|     "nano-uid": "^0.2.0", |     "nano-uid": "^0.2.0", | ||||||
|     "nixt": "^0.4.1", |     "nixt": "^0.4.1", | ||||||
|  |     "nock": "^2.2.0", | ||||||
|     "sinopia": "^1.0.0", |     "sinopia": "^1.0.0", | ||||||
|     "standard": "^3.2.1", |     "standard": "^3.11.1", | ||||||
|     "tap-spec": "^3.0.0", |     "tap-spec": "^3.0.0", | ||||||
|     "tape": "^4.0.0" |     "tape": "^4.0.0" | ||||||
|   }, |   }, | ||||||
|  | |||||||
| @ -1,3 +1,4 @@ | |||||||
| _auth=dGVzdDpmb28= | _auth=dGVzdDpmb28= | ||||||
| email=test@example.com | email=test@example.com | ||||||
| registry=http://127.0.0.1:4873/ | registry=http://127.0.0.1:4873/ | ||||||
|  | //registry.npmjs.org/:_authToken=testtoken | ||||||
|  | |||||||
| @ -4,6 +4,7 @@ var test = require('tape') | |||||||
| 
 | 
 | ||||||
| var createModule = require('./lib/create-module') | var createModule = require('./lib/create-module') | ||||||
| 
 | 
 | ||||||
|  | require('./tap/npm-info')(test) | ||||||
| require('./scenarios/custom-analyzer')(test, createModule) | require('./scenarios/custom-analyzer')(test, createModule) | ||||||
| require('./scenarios/custom-verification')(test, createModule) | require('./scenarios/custom-verification')(test, createModule) | ||||||
| require('./scenarios/ignore')(test, createModule) | require('./scenarios/ignore')(test, createModule) | ||||||
|  | |||||||
							
								
								
									
										52
									
								
								tests/tap/npm-info.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								tests/tap/npm-info.js
									
									
									
									
									
										Normal file
									
								
							| @ -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() | ||||||
|  |       }) | ||||||
|  |     }) | ||||||
|  |   }) | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user