'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');

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';
	}
}

const packageJson = async (name, options) => {
	options = {
		version: 'latest',
		...options
	};

	const scope = name.split('/')[0];
	const regUrl = registryUrl(scope);
	const pkgUrl = new URL(encodeURIComponent(name).replace(/^%40/, '@'), regUrl);
	const authInfo = registryAuthToken(regUrl.toString(), {recursive: true});

	const headers = {
		accept: 'application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*'
	};

	if (options.fullMetadata) {
		delete headers.accept;
	}

	if (authInfo) {
		headers.authorization = `${authInfo.type} ${authInfo.token}`;
	}

	let response;
	try {
		response = await got(pkgUrl, {json: true, headers});
	} catch (error) {
		if (error.statusCode === 404) {
			throw new PackageNotFoundError(name);
		}

		throw error;
	}

	let data = response.body;

	if (options.allVersions) {
		return data;
	}

	let {version} = options;
	const versionError = new VersionNotFoundError(name, version);

	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 versionError;
			}
		}

		data = data.versions[version];

		if (!data) {
			throw versionError;
		}
	}

	return data;
};

module.exports = packageJson;
module.exports.default = packageJson;

module.exports.PackageNotFoundError = PackageNotFoundError;

module.exports.VersionNotFoundError = VersionNotFoundError;

@@ -33,24 +33,114 @@
 ire('url');%0A
+const %7BAgent: HttpAgent%7D = require('http');%0Aconst %7BAgent: HttpsAgent%7D = require('https');%0A
 const got = 
@@ -293,16 +293,310 @@
 ver');%0A%0A
+// These agent options are chosen to match the npm client defaults and help with performance%0A// see: %60npm config get maxsockets%60 and #50%0Aconst agentOptions = %7B%0A%09keepAlive: true,%0A%09maxSockets: 50%0A%7D;%0Aconst httpAgent = new HttpAgent(agentOptions);%0Aconst httpsAgent = new HttpsAgent(agentOptions);%0A%0A
 class Pa
@@ -1569,16 +1569,131 @@
 %0A%09try %7B%0A
+%09%09const gotOptions = %7B%0A%09%09%09json: true,%0A%09%09%09headers,%0A%09%09%09agent: %7B%0A%09%09%09%09http: httpAgent,%0A%09%09%09%09https: httpsAgent%0A%09%09%09%7D%0A%09%09%7D;%0A
 %09%09respon
@@ -1719,29 +1719,18 @@
 rl, 
-%7Bjson: true, headers%7D
+gotOptions
 );%0A%09

Use keepAlive agent to increase performance (#50)