Skip to content

Commit

Permalink
fix(npm): bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Airkro committed Feb 28, 2024
1 parent 9bacc8e commit 3cd0b80
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/npm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bring-it/npm",
"version": "0.5.1",
"version": "0.5.2",
"description": "Publish npm packages when needed",
"license": "MIT",
"author": {
Expand Down
27 changes: 24 additions & 3 deletions packages/npm/src/lib/scan.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,37 @@ function readPkg(file) {
.catch(() => false);
}

function Fetch(url, options) {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort('timeout'), 5000);

return fetch(url, {
...options,
signal: controller.signal,
})
.then((response) => {
clearTimeout(timeout);

return response;
})
.catch((error) => {
clearTimeout(timeout);
throw error;
});
}

function getVersions(name, registry, token = '') {
return fetch(new URL(name, registry).href, {
return Fetch(new URL(name, registry).href, {
headers: token ? { Authorization: `Bearer ${token}` } : undefined,
})
.then(async (response) => {
if (!response.ok) {
if (response.status === 401) {
const NPM_TOKEN = await readNpmToken();
const NPM_TOKEN = await readNpmToken(registry);

return getVersions(name, registry, NPM_TOKEN);
if (NPM_TOKEN) {
return getVersions(name, registry, NPM_TOKEN);
}
}

return [];
Expand Down
4 changes: 2 additions & 2 deletions packages/npm/src/lib/token.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export function readNpmToken(registry = 'https://registry.npmjs.org/') {
...localConfig.value,
}))
.then((data) => {
const domain = new URL(registry).host;
const { host, pathname } = new URL(registry);

return data[`//${domain}/:_authToken`];
return data[`//${host + pathname}:_authToken`];
})
.catch(() => {});
}

0 comments on commit 3cd0b80

Please sign in to comment.