pax_global_header00006660000000000000000000000064130750662320014516gustar00rootroot0000000000000052 comment=2312d0437c16d7117d354ae76f640c5ac25fe0e0 package-json-4.0.1/000077500000000000000000000000001307506623200140625ustar00rootroot00000000000000package-json-4.0.1/.editorconfig000066400000000000000000000002761307506623200165440ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [{package.json,*.yml}] indent_style = space indent_size = 2 package-json-4.0.1/.gitattributes000066400000000000000000000000351307506623200167530ustar00rootroot00000000000000* text=auto *.js text eol=lf package-json-4.0.1/.gitignore000066400000000000000000000000151307506623200160460ustar00rootroot00000000000000node_modules package-json-4.0.1/.npmrc000066400000000000000000000004031307506623200151770ustar00rootroot00000000000000@mockscope:registry=http://localhost:63142 //localhost:63142/:_authToken=MySecretToken @mockscope2:registry=http://localhost:63143 //localhost:63143/:username=Aladdin //localhost:63143/:_password=T3BlblNlc2FtZQ== @mockscope3:registry=http://localhost:63144 package-json-4.0.1/.travis.yml000066400000000000000000000000671307506623200161760ustar00rootroot00000000000000sudo: false language: node_js node_js: - '6' - '4' package-json-4.0.1/index.js000066400000000000000000000030141307506623200155250ustar00rootroot00000000000000'use strict'; const url = require('url'); const got = require('got'); const registryUrl = require('registry-url'); const registryAuthToken = require('registry-auth-token'); const semver = require('semver'); module.exports = (name, opts) => { const scope = name.split('/')[0]; const regUrl = registryUrl(scope); const pkgUrl = url.resolve(regUrl, encodeURIComponent(name).replace(/^%40/, '@')); const authInfo = registryAuthToken(regUrl, {recursive: true}); opts = Object.assign({ version: 'latest' }, opts); const headers = { accept: 'application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*' }; if (opts.fullMetadata) { delete headers.accept; } if (authInfo) { headers.authorization = `${authInfo.type} ${authInfo.token}`; } return got(pkgUrl, {json: true, headers}) .then(res => { let data = res.body; let version = opts.version; if (opts.allVersions) { return data; } if (data['dist-tags'][version]) { data = data.versions[data['dist-tags'][version]]; } else if (version) { if (!data.versions[version]) { const versions = Object.keys(data.versions); version = semver.maxSatisfying(versions, version); if (!version) { throw new Error('Version doesn\'t exist'); } } data = data.versions[version]; if (!data) { throw new Error('Version doesn\'t exist'); } } return data; }) .catch(err => { if (err.statusCode === 404) { throw new Error(`Package \`${name}\` doesn't exist`); } throw err; }); }; package-json-4.0.1/license000066400000000000000000000021371307506623200154320ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) Sindre Sorhus (sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. package-json-4.0.1/package.json000066400000000000000000000014331307506623200163510ustar00rootroot00000000000000{ "name": "package-json", "version": "4.0.1", "description": "Get metadata of a package from the npm registry", "license": "MIT", "repository": "sindresorhus/package-json", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=4" }, "scripts": { "test": "xo && ava" }, "files": [ "index.js" ], "keywords": [ "npm", "registry", "package", "pkg", "package.json", "json", "module", "scope", "scoped" ], "dependencies": { "got": "^6.7.1", "registry-auth-token": "^3.0.1", "registry-url": "^3.0.3", "semver": "^5.1.0" }, "devDependencies": { "ava": "*", "mock-private-registry": "^1.1.0", "xo": "*" } } package-json-4.0.1/readme.md000066400000000000000000000045201307506623200156420ustar00rootroot00000000000000# package-json [![Build Status](https://travis-ci.org/sindresorhus/package-json.svg?branch=master)](https://travis-ci.org/sindresorhus/package-json) > Get metadata of a package from the npm registry ## Install ``` $ npm install --save package-json ``` ## Usage ```js const packageJson = require('package-json'); packageJson('ava').then(json => { console.log(json); //=> {name: 'ava', ...} }); // Also works with scoped packages packageJson('@sindresorhus/df').then(json => { console.log(json); //=> {name: '@sindresorhus/df', ...} }); ``` ## API ### packageJson(name, [options]) #### name Type: `string` Name of the package. #### options Type: `Object` ##### version Type: `string`
Default: `latest` Package version such as `1.0.0` or a [dist tag](https://docs.npmjs.com/cli/dist-tag) such as `latest`. The version can also be in any format supported by the [semver](https://github.com/npm/node-semver) module. For example: - `1` - get the latest `1.x.x` - `1.2` - get the latest `1.2.x` - `^1.2.3` - get the latest `1.x.x` but at least `1.2.3` - `~1.2.3` - get the latest `1.2.x` but at least `1.2.3` ##### fullMetadata Type: `boolean`
Default: `false` By default, only an abbreviated metadata object is returned for performance reasons. [Read more.](https://github.com/npm/registry/blob/master/docs/responses/package-metadata.md) ##### allVersions Type: `boolean`
Default: `false` Return the [main entry](https://registry.npmjs.org/ava) containing all versions. ## Authentication Both public and private registries are supported, for both scoped and unscoped packages, as long as the registry uses either bearer tokens or basic authentication. ## Related - [package-json-cli](https://github.com/sindresorhus/package-json-cli) - CLI for this module - [latest-version](https://github.com/sindresorhus/latest-version) - Get the latest version of an npm package - [pkg-versions](https://github.com/sindresorhus/pkg-versions) - Get the version numbers of a package from the npm registry - [npm-keyword](https://github.com/sindresorhus/npm-keyword) - Get a list of npm packages with a certain keyword - [npm-user](https://github.com/sindresorhus/npm-user) - Get user info of an npm user - [npm-email](https://github.com/sindresorhus/npm-email) - Get the email of an npm user ## License MIT © [Sindre Sorhus](https://sindresorhus.com) package-json-4.0.1/test.js000066400000000000000000000055431307506623200154060ustar00rootroot00000000000000import http from 'http'; import test from 'ava'; import privateRegistry from 'mock-private-registry/promise'; import m from '.'; test('latest version', async t => { const json = await m('ava'); t.is(json.name, 'ava'); t.falsy(json.versions); }); test('full metadata', async t => { const json = await m('pageres', { fullMetadata: true, version: '4.4.0' }); t.is(json.name, 'pageres'); t.is(json._id, 'pageres@4.4.0'); }); test('all version', async t => { const json = await m('pageres', {allVersions: true}); t.is(json.name, 'pageres'); t.is(json.versions['0.1.0'].name, 'pageres'); }); test('specific version', async t => { const json = await m('pageres', {version: '0.1.0'}); t.is(json.version, '0.1.0'); }); test('incomplete version x', async t => { const json = await m('pageres', {version: '0'}); t.is(json.version.substr(0, 2), '0.'); }); test('scoped - latest version', async t => { const json = await m('@sindresorhus/df'); t.is(json.name, '@sindresorhus/df'); }); test('scoped - full metadata', async t => { const json = await m('@sindresorhus/df', { fullMetadata: true, version: '1.0.1' }); t.is(json.name, '@sindresorhus/df'); t.is(json._id, '@sindresorhus/df@1.0.1'); }); test('scoped - all version', async t => { const json = await m('@sindresorhus/df', {allVersions: true}); t.is(json.name, '@sindresorhus/df'); t.is(json.versions['1.0.1'].name, '@sindresorhus/df'); }); test('scoped - specific version', async t => { const json = await m('@sindresorhus/df', {version: '1.0.1'}); t.is(json.version, '1.0.1'); }); test('scoped - dist tag', async t => { const json = await m('@rexxars/npmtest', {version: 'next'}); t.is(json.version, '2.0.0'); }); test('reject when version doesn\'t exist', async t => { await t.throws(m('hapi', {version: '6.6.6'}), 'Version doesn\'t exist'); }); test('reject when package doesn\'t exist', async t => { await t.throws(m('nnnope'), 'Package `nnnope` doesn\'t exist'); }); test.cb('does not send any auth token for unconfigured registries', t => { const server = http.createServer((req, res) => { res.end(JSON.stringify({headers: req.headers, 'dist-tags': {}})); }); server.listen(63144, '127.0.0.1', async () => { const json = await m('@mockscope3/foobar', {allVersions: true}); t.is(json.headers.host, 'localhost:63144'); t.is(json.headers.authorization, undefined); server.close(t.end); }); }); test('private registry (bearer token)', async t => { const server = await privateRegistry(); const json = await m('@mockscope/foobar'); t.is(json.name, '@mockscope/foobar'); server.close(); }); test('private registry (basic token)', async t => { const server = await privateRegistry({ port: 63143, pkgName: '@mockscope2/foobar', token: 'QWxhZGRpbjpPcGVuU2VzYW1l', tokenType: 'Basic' }); const json = await m('@mockscope2/foobar'); t.is(json.name, '@mockscope2/foobar'); server.close(); });