Skip to content

Commit

Permalink
refactor: Replace got with node-fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Nov 27, 2023
1 parent db167ba commit a395d0f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
20 changes: 9 additions & 11 deletions lib/private/get-metadata.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"use strict";

const memoizee = require("memoizee")
, got = require("got")
, log = require("log").get("npm-cross-link")
, npmconf = require("npm/lib/config/core")
, cache = require("./cache");
const memoizee = require("memoizee")
, nodeFetch = require("node-fetch")
, log = require("log").get("npm-cross-link")
, npmconf = require("npm/lib/config/core")
, cache = require("./cache");

const scopedNameRe = /^@(?<org>[^/]+)\/./u;

Expand Down Expand Up @@ -48,14 +48,12 @@ module.exports = memoizee(
const cached = await resolveCached(dependencyName);
const response = await (async () => {
try {
return await got(`${ registryData.url }${ dependencyName }`, {
return await nodeFetch(`${ registryData.url }${ dependencyName }`, {
headers: {
accept: "application/vnd.npm.install-v1+json",
...(registryData.authToken && {
authorization: `Bearer ${ registryData.authToken }`
}),
// We do not rely on got's cache
// as it's not effective with 'authorization' header
...(cached.etag && { "if-none-match": cached.etag })
}
});
Expand All @@ -68,11 +66,11 @@ module.exports = memoizee(
}
})();
if (!response) return response;
if (response.statusCode === 304) return cached.data;
const result = JSON.parse(response.body);
if (response.status === 304) return cached.data;
const result = await response.json();
cache.set(
`${ dependencyName }/registry.json`,
JSON.stringify({ etag: response.headers.etag, data: result })
JSON.stringify({ etag: response.headers.get("etag"), data: result })
);
return result;
},
Expand Down
13 changes: 6 additions & 7 deletions lib/private/setup-dependency/install-external/prepare/sem-ver.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ const optionalChaining = require("es5-ext/optional-chaining")
, { resolve } = require("path")
, readFile = require("fs2/read-file")
, writeFile = require("fs2/write-file")
, got = require("got")
, tar = require("tar")
, log = require("log").get("npm-cross-link")
, runProgram = require("../../../../run-program")
, resolveNpmAuthToken = require("../../resolve-npm-auth-token");
, resolveNpmAuthToken = require("../../resolve-npm-auth-token")
, nodeFetch = require("node-fetch");

module.exports = {
isApplicable: (name, version, externalContext) =>
Expand All @@ -17,12 +17,11 @@ module.exports = {
prepare: async (tmpDir, name, version, versionMetaData) => {
// Regular npm publication
const npmAuthToken = (await resolveNpmAuthToken(versionMetaData.dist.tarball)) || "";
const response = await nodeFetch(versionMetaData.dist.tarball, {
headers: { Authorization: `Bearer ${ npmAuthToken }` }
});
const stream = response.body.pipe(tar.x({ cwd: tmpDir, strip: 1 }));
await new Promise((promiseResolve, promiseReject) => {
const stream = got
.stream(versionMetaData.dist.tarball, {
headers: { Authorization: `Bearer ${ npmAuthToken }` }
})
.pipe(tar.x({ cwd: tmpDir, strip: 1 }));
stream.on("error", promiseReject);
stream.on("end", promiseResolve);
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
"essentials": "^1.2.0",
"event-emitter": "^0.3.5",
"fs2": "^0.3.9",
"got": "^11.8.6",
"log": "^6.3.1",
"log-node": "^8.0.3",
"memoizee": "^0.4.15",
"minimist": "^1.2.8",
"ncjsm": "^4.3.2",
"node-fetch": "^2.7.0",
"npm": "^6.14.18",
"semver": "^7.5.4",
"tar": "^6.1.15",
Expand Down

0 comments on commit a395d0f

Please sign in to comment.