package-json
Advanced tools
Comparing version 5.0.0 to 6.0.0
96
index.js
'use strict'; | ||
const url = require('url'); | ||
const {URL} = require('url'); | ||
const got = require('got'); | ||
@@ -8,12 +8,27 @@ const registryUrl = require('registry-url'); | ||
module.exports = (name, options) => { | ||
class PackageNotFoundError extends Error { | ||
constructor(packageName) { | ||
super(`Package \`${packageName}\` could not be found`); | ||
this.name = 'PackageNotFoundError'; | ||
} | ||
} | ||
class VersionNotFoundError extends Error { | ||
constructor(packageName, version) { | ||
super(`Version \`${version}\` for package \`${packageName}\` could not be found`); | ||
this.name = 'VersionNotFoundError'; | ||
} | ||
} | ||
module.exports = async (name, options) => { | ||
options = { | ||
version: 'latest', | ||
...options | ||
}; | ||
const scope = name.split('/')[0]; | ||
const regUrl = registryUrl(scope); | ||
const pkgUrl = url.resolve(regUrl, encodeURIComponent(name).replace(/^%40/, '@')); | ||
const authInfo = registryAuthToken(regUrl, {recursive: true}); | ||
const pkgUrl = new URL(encodeURIComponent(name).replace(/^%40/, '@'), regUrl); | ||
const authInfo = registryAuthToken(regUrl.toString(), {recursive: true}); | ||
options = Object.assign({ | ||
version: 'latest' | ||
}, options); | ||
const headers = { | ||
@@ -31,39 +46,46 @@ accept: 'application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*' | ||
return got(pkgUrl, {json: true, headers}) | ||
.then(res => { | ||
let data = res.body; | ||
let {version} = options; | ||
let response; | ||
try { | ||
response = await got(pkgUrl, {json: true, headers}); | ||
} catch (error) { | ||
if (error.statusCode === 404) { | ||
throw new PackageNotFoundError(name); | ||
} | ||
if (options.allVersions) { | ||
return data; | ||
} | ||
throw error; | ||
} | ||
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); | ||
let data = response.body; | ||
if (!version) { | ||
throw new Error('Version doesn\'t exist'); | ||
} | ||
} | ||
if (options.allVersions) { | ||
return data; | ||
} | ||
data = data.versions[version]; | ||
let {version} = options; | ||
const versionError = new VersionNotFoundError(name, version); | ||
if (!data) { | ||
throw new Error('Version doesn\'t exist'); | ||
} | ||
} | ||
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); | ||
return data; | ||
}) | ||
.catch(err => { | ||
if (err.statusCode === 404) { | ||
throw new Error(`Package \`${name}\` doesn't exist`); | ||
if (!version) { | ||
throw versionError; | ||
} | ||
} | ||
throw err; | ||
}); | ||
data = data.versions[version]; | ||
if (!data) { | ||
throw versionError; | ||
} | ||
} | ||
return data; | ||
}; | ||
module.exports.PackageNotFoundError = PackageNotFoundError; | ||
module.exports.VersionNotFoundError = VersionNotFoundError; |
{ | ||
"name": "package-json", | ||
"version": "5.0.0", | ||
"version": "6.0.0", | ||
"description": "Get metadata of a package from the npm registry", | ||
@@ -13,3 +13,3 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=6" | ||
"node": ">=8" | ||
}, | ||
@@ -34,12 +34,12 @@ "scripts": { | ||
"dependencies": { | ||
"got": "^8.3.1", | ||
"got": "^9.6.0", | ||
"registry-auth-token": "^3.3.2", | ||
"registry-url": "^3.1.0", | ||
"semver": "^5.5.0" | ||
"registry-url": "^4.0.0", | ||
"semver": "^5.6.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"ava": "^1.2.0", | ||
"mock-private-registry": "^1.1.2", | ||
"xo": "*" | ||
"xo": "^0.24.0" | ||
} | ||
} |
@@ -70,3 +70,11 @@ # package-json [![Build Status](https://travis-ci.org/sindresorhus/package-json.svg?branch=master)](https://travis-ci.org/sindresorhus/package-json) | ||
### packageJson.PackageNotFoundError | ||
The error thrown when the given package name cannot be found. | ||
### packageJson.VersionNotFoundError | ||
The error thrown when the given package version cannot be found. | ||
## Authentication | ||
@@ -73,0 +81,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6427
70
97
+ Added@sindresorhus/is@0.14.0(transitive)
+ Added@szmarczak/http-timer@1.1.2(transitive)
+ Addedcacheable-request@6.1.0(transitive)
+ Addedclone-response@1.0.3(transitive)
+ Addeddefer-to-connect@1.1.3(transitive)
+ Addedend-of-stream@1.4.4(transitive)
+ Addedget-stream@4.1.05.2.0(transitive)
+ Addedgot@9.6.0(transitive)
+ Addedhttp-cache-semantics@4.1.1(transitive)
+ Addedkeyv@3.1.0(transitive)
+ Addedlowercase-keys@2.0.0(transitive)
+ Addednormalize-url@4.5.1(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedp-cancelable@1.1.0(transitive)
+ Addedpump@3.0.2(transitive)
+ Addedregistry-url@4.0.0(transitive)
+ Addedto-readable-stream@1.0.0(transitive)
+ Addedwrappy@1.0.2(transitive)
- Removed@sindresorhus/is@0.7.0(transitive)
- Removedcacheable-request@2.1.4(transitive)
- Removedclone-response@1.0.2(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removeddecode-uri-component@0.2.2(transitive)
- Removedfrom2@2.3.0(transitive)
- Removedget-stream@3.0.0(transitive)
- Removedgot@8.3.2(transitive)
- Removedhas-symbol-support-x@1.4.2(transitive)
- Removedhas-to-string-tag-x@1.4.1(transitive)
- Removedhttp-cache-semantics@3.8.1(transitive)
- Removedinherits@2.0.4(transitive)
- Removedinto-stream@3.1.0(transitive)
- Removedis-object@1.0.2(transitive)
- Removedis-plain-obj@1.1.0(transitive)
- Removedis-retry-allowed@1.2.0(transitive)
- Removedisarray@1.0.0(transitive)
- Removedisurl@1.0.0(transitive)
- Removedkeyv@3.0.0(transitive)
- Removedlowercase-keys@1.0.0(transitive)
- Removednormalize-url@2.0.1(transitive)
- Removedobject-assign@4.1.1(transitive)
- Removedp-cancelable@0.4.1(transitive)
- Removedp-finally@1.0.0(transitive)
- Removedp-is-promise@1.1.0(transitive)
- Removedp-timeout@2.0.1(transitive)
- Removedpify@3.0.0(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedquery-string@5.1.1(transitive)
- Removedreadable-stream@2.3.8(transitive)
- Removedregistry-url@3.1.0(transitive)
- Removedsafe-buffer@5.1.2(transitive)
- Removedsort-keys@2.0.0(transitive)
- Removedstrict-uri-encode@1.1.0(transitive)
- Removedstring_decoder@1.1.1(transitive)
- Removedtimed-out@4.0.1(transitive)
- Removedurl-to-options@1.0.1(transitive)
- Removedutil-deprecate@1.0.2(transitive)
Updatedgot@^9.6.0
Updatedregistry-url@^4.0.0
Updatedsemver@^5.6.0