Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

package-json

Package Overview
Dependencies
Maintainers
2
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

package-json - npm Package Compare versions

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 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc