Skip to content

Commit

Permalink
refactor: reduce diff
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz committed Feb 7, 2024
1 parent 159607e commit aef175f
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
// eslint-disable-next-line @typescript-eslint/naming-convention
'no-restricted-globals': [`error`, {
name: `fetch`,
message: `Use fetch from sources/fetchUtils.ts`,
message: `Use fetch from sources/httpUtils.ts`,
}],
},
};
10 changes: 5 additions & 5 deletions sources/corepackUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import {Readable} from 'stream';

import * as engine from './Engine';
import * as debugUtils from './debugUtils';
import {fetch} from './fetchUtils';
import * as folderUtils from './folderUtils';
import * as fsUtils from './fsUtils';
import * as httpUtils from './httpUtils';
import * as nodeUtils from './nodeUtils';
import * as npmRegistryUtils from './npmRegistryUtils';
import {RegistrySpec, Descriptor, Locator, PackageManagerSpec} from './types';
Expand All @@ -30,7 +30,7 @@ export async function fetchLatestStableVersion(spec: RegistrySpec): Promise<stri
return await npmRegistryUtils.fetchLatestStableVersion(spec.package);
}
case `url`: {
const response = await fetch(spec.url);
const response = await httpUtils.fetch(spec.url);
const data: any = await response.json();
return data[spec.fields.tags].stable;
}
Expand All @@ -46,7 +46,7 @@ export async function fetchAvailableTags(spec: RegistrySpec): Promise<Record<str
return await npmRegistryUtils.fetchAvailableTags(spec.package);
}
case `url`: {
const response = await fetch(spec.url);
const response = await httpUtils.fetch(spec.url);
const data: any = await response.json();
return data[spec.fields.tags];
}
Expand All @@ -62,7 +62,7 @@ export async function fetchAvailableVersions(spec: RegistrySpec): Promise<Array<
return await npmRegistryUtils.fetchAvailableVersions(spec.package);
}
case `url`: {
const response = await fetch(spec.url);
const response = await httpUtils.fetch(spec.url);
const data: any = await response.json();
const field = data[spec.fields.versions];
return Array.isArray(field) ? field : Object.keys(field);
Expand Down Expand Up @@ -145,7 +145,7 @@ export async function installVersion(installTarget: string, locator: Locator, {s
const tmpFolder = folderUtils.getTemporaryFolder(installTarget);
debugUtils.log(`Installing ${locator.name}@${version} from ${url} to ${tmpFolder}`);
const parsedUrl = new URL(url);
const response = await fetch(parsedUrl);
const response = await httpUtils.fetch(parsedUrl);
const webStream = response.body;
assert(webStream, `Expected stream to be set`);
const stream = Readable.fromWeb(webStream);
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions sources/npmRegistryUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {UsageError} from 'clipanion';
import {UsageError} from 'clipanion';

import {fetch} from './fetchUtils';
import * as httpUtils from './httpUtils';

// load abbreviated metadata as that's all we need for these calls
// see: https://github.com/npm/registry/blob/cfe04736f34db9274a780184d1cdb2fb3e4ead2a/docs/responses/package-metadata.md
Expand All @@ -25,7 +25,7 @@ export async function fetchAsJson(packageName: string) {
headers.authorization = `Basic ${encodedCreds}`;
}

const response = await fetch(`${npmRegistryUrl}/${packageName}`, {headers});
const response = await httpUtils.fetch(`${npmRegistryUrl}/${packageName}`, {headers});
const data: any = await response.json();
return data;
}
Expand Down
5 changes: 2 additions & 3 deletions tests/recordRequests.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ const v8 = require(`node:v8`);
*/
let mocks = new Map();

function getNockFile() {
return path.join(
const getNockFile = () =>
path.join(
__dirname,
`nock`,
`${process.env.NOCK_FILE_NAME}-${process.env.RUN_CLI_ID}.dat`,
);
}

if (process.env.NOCK_ENV === `record`) {
const realFetch = globalThis.fetch;
Expand Down

0 comments on commit aef175f

Please sign in to comment.