Skip to content

Commit

Permalink
Switch from got to ky (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy-mitchell authored Feb 20, 2024
1 parent 7238462 commit b4ee1c7
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 45 deletions.
9 changes: 0 additions & 9 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import {Agent as HttpAgent} from 'node:http';
import {Agent as HttpsAgent} from 'node:https';
import {type Agents} from 'got';

/**
The error thrown when the given package version cannot be found.
*/
Expand Down Expand Up @@ -52,11 +48,6 @@ export type Options = {
The registry URL is by default inferred from the npm defaults and `.npmrc`. This is beneficial as `package-json` and any project using it will work just like npm. This option is*only** intended for internal tools. You should __not__ use this option in reusable packages. Prefer just using `.npmrc` whenever possible.
*/
readonly registryUrl?: string;

/**
Overwrite the `agent` option that is passed down to [`got`](https://github.com/sindresorhus/got#agent). This might be useful to add [proxy support](https://github.com/sindresorhus/got#proxies).
*/
readonly agent?: Agents;
};

export type FullMetadataOptions = {
Expand Down
30 changes: 3 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
import {Agent as HttpAgent} from 'node:http';
import {Agent as HttpsAgent} from 'node:https';
import got from 'got';
import ky from 'ky';
import registryUrl from 'registry-url';
import registryAuthToken from 'registry-auth-token';
import semver from 'semver';

// These agent options are chosen to match the npm client defaults and help with performance
// See: `npm config get maxsockets` and #50
const agentOptions = {
keepAlive: true,
maxSockets: 50,
};

const httpAgent = new HttpAgent(agentOptions);
const httpsAgent = new HttpsAgent(agentOptions);

export class PackageNotFoundError extends Error {
constructor(packageName) {
super(`Package \`${packageName}\` could not be found`);
Expand Down Expand Up @@ -52,23 +40,11 @@ export default async function packageJson(packageName, options) {
headers.authorization = `${authInfo.type} ${authInfo.token}`;
}

const gotOptions = {
headers,
agent: {
http: httpAgent,
https: httpsAgent,
},
};

if (options.agent) {
gotOptions.agent = options.agent;
}

let data;
try {
data = await got(packageUrl, gotOptions).json();
data = await ky(packageUrl, {headers, keepalive: true}).json();
} catch (error) {
if (error?.response?.statusCode === 404) {
if (error?.response?.status === 404) {
throw new PackageNotFoundError(packageName);
}

Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
"node": ">=18"
},
"scripts": {
"//test": "xo && ava && tsd",
"test": "xo && ava"
"test": "xo && ava && tsd"
},
"files": [
"index.js",
Expand All @@ -39,7 +38,7 @@
"scoped"
],
"dependencies": {
"got": "^13.0.0",
"ky": "^1.2.0",
"registry-auth-token": "^5.0.2",
"registry-url": "^6.0.1",
"semver": "^7.6.0"
Expand Down
12 changes: 6 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,6 @@ Default: Auto-detected

The registry URL is by default inferred from the npm defaults and `.npmrc`. This is beneficial as `package-json` and any project using it will work just like npm. This option is **only** intended for internal tools. You should **not** use this option in reusable packages. Prefer just using `.npmrc` whenever possible.

##### agent

Type: `object`

Overwrite the `agent` option that is passed down to [`got`](https://github.com/sindresorhus/got#agent). This might be useful to add [proxy support](https://github.com/sindresorhus/got#proxies).

### PackageNotFoundError

The error thrown when the given package name cannot be found.
Expand All @@ -87,6 +81,12 @@ The error thrown when the given package version cannot be found.

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.

## Proxies

Proxy support is not implemented in this package. If necessary, use a global agent that modifies [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API), which this package uses internally.

Support for this may come to [Node.js in the future](https://github.com/nodejs/undici/issues/1650).

## Related

- [package-json-cli](https://github.com/sindresorhus/package-json-cli) - CLI for this module
Expand Down

0 comments on commit b4ee1c7

Please sign in to comment.