Skip to content

Commit d9a02f9

Browse files
committed
refactor: packument fetch logic
Currently we use `another-npm-registry-client` for fetching packuments. This is a fork of a deprecated library. This change instead uses the currently maintained library for fetching packuments that is maintained by npm Just to modernize the dependencies a bit.
1 parent d788396 commit d9a02f9

File tree

4 files changed

+12
-31
lines changed

4 files changed

+12
-31
lines changed

src/index.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#!/usr/bin/env node
22

3-
import RegClient from "another-npm-registry-client";
43
import npmlog from "npmlog";
54
import updateNotifier from "update-notifier";
65
import pkg from "../package.json";
@@ -29,11 +28,7 @@ const debugLogToConsole: DebugLog = async function (message, context) {
2928
return log.verbose("", `${message}${contextMessage}`);
3029
};
3130

32-
const registryClient = new RegClient({ log });
33-
const fetchPackument = getRegistryPackumentUsing(
34-
registryClient,
35-
debugLogToConsole
36-
);
31+
const fetchPackument = getRegistryPackumentUsing(debugLogToConsole);
3732
const searchRegistry = searchRegistryUsing(debugLogToConsole);
3833
const fetchAllPackuments = getAllRegistryPackumentsUsing(debugLogToConsole);
3934
const getAuthToken = getAuthTokenUsing(debugLogToConsole);

src/io/registry.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type RegClient from "another-npm-registry-client";
21
import npmSearch from "libnpmsearch";
32
import { loginCouch } from "npm-profile";
43
import npmFetch from "npm-registry-fetch";
@@ -129,26 +128,19 @@ export type GetRegistryPackument = (
129128
* from a remote npm registry.
130129
*/
131130
export function getRegistryPackumentUsing(
132-
registryClient: RegClient.Instance,
133131
debugLog: DebugLog
134132
): GetRegistryPackument {
135-
return (registry, name) => {
133+
return async (registry, name) => {
136134
const url = `${registry.url}/${name}`;
137-
return new Promise((resolve, reject) => {
138-
return registryClient.get(
139-
url,
140-
{ auth: registry.auth || undefined },
141-
(error, packument) => {
142-
if (error !== null) {
143-
assertIsHttpError(error);
144-
if (error.statusCode === 404) resolve(null);
145-
else reject(error);
146-
} else resolve(packument);
147-
}
148-
);
149-
}).catch(async (error) => {
135+
try {
136+
const json = await npmFetch.json(url);
137+
// TODO: Validate object
138+
return json as UnityPackument;
139+
} catch (error) {
140+
assertIsHttpError(error);
141+
if (error.statusCode === 404) return null;
150142
await debugLog("Fetching a packument failed.", error);
151143
throw makeRegistryInteractionError(error, registry.url);
152-
});
144+
}
153145
};
154146
}

test/integration/app/add-dependencies.test.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import RegClient from "another-npm-registry-client";
21
import nock from "nock";
32
import {
43
addDependenciesUsing,
@@ -81,10 +80,7 @@ describe("add dependencies", () => {
8180
const someProjectDir = "/users/some-user/projects/SomeProject";
8281

8382
function makeDependencies() {
84-
const fetchPackument = getRegistryPackumentUsing(
85-
new RegClient(),
86-
noopLogger
87-
);
83+
const fetchPackument = getRegistryPackumentUsing(noopLogger);
8884

8985
const log = makeMockLogger();
9086

test/integration/app/resolve-dependencies.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import RegClient from "another-npm-registry-client";
21
import nock from "nock";
32
import { resolveDependenciesUsing } from "../../../src/app/resolve-dependencies";
43
import { PackumentNotFoundError } from "../../../src/domain/common-errors";
@@ -32,11 +31,10 @@ describe("dependency resolving", () => {
3231
const someVersion = SemanticVersion.parse("1.0.0");
3332
const otherVersion = SemanticVersion.parse("2.0.0");
3433

35-
const registryClient = new RegClient();
3634
const resolveDependencies = partialApply(
3735
resolveDependenciesUsing,
3836
fetchCheckUrlExists,
39-
getRegistryPackumentUsing(registryClient, noopLogger)
37+
getRegistryPackumentUsing(noopLogger)
4038
);
4139

4240
afterEach(() => {

0 commit comments

Comments
 (0)