Skip to content

Commit eb63873

Browse files
authored
fix: fallback to shasum when integrity is not defined (#542)
Some npm registries do not define an `integrity` field, in which case we can try using the `shasum` field instead.
1 parent 93a49c8 commit eb63873

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

sources/npmRegistryUtils.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export function verifySignature({signatures, integrity, packageName, version}: {
6262
export async function fetchLatestStableVersion(packageName: string) {
6363
const metadata = await fetchAsJson(packageName, `latest`);
6464

65-
const {version, dist: {integrity, signatures}} = metadata;
65+
const {version, dist: {integrity, signatures, shasum}} = metadata;
6666

6767
if (!shouldSkipIntegrityCheck()) {
6868
verifySignature({
@@ -71,7 +71,11 @@ export async function fetchLatestStableVersion(packageName: string) {
7171
});
7272
}
7373

74-
return `${version}+sha512.${Buffer.from(integrity.slice(7), `base64`).toString(`hex`)}`;
74+
return `${version}+${
75+
integrity ?
76+
`sha512.${Buffer.from(integrity.slice(7), `base64`).toString(`hex`)}` :
77+
`sha1.${shasum}`
78+
}`;
7579
}
7680

7781
export async function fetchAvailableTags(packageName: string) {

tests/_registryServer.mjs

+1-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ const registry = {
8787
function generateSignature(packageName, version) {
8888
if (privateKey == null) return undefined;
8989
const sign = createSign(`SHA256`).end(`${packageName}@${version}:${integrity}`);
90-
return {signatures: [{
90+
return {integrity, signatures: [{
9191
keyid,
9292
sig: sign.sign(privateKey, `base64`),
9393
}]};
@@ -100,10 +100,8 @@ function generateVersionMetadata(packageName, version) {
100100
[packageName]: `./bin/${packageName}.js`,
101101
},
102102
dist: {
103-
integrity,
104103
shasum,
105104
size: mockPackageTarGz.length,
106-
noattachment: false,
107105
tarball: `https://registry.npmjs.org/${packageName}/-/${packageName}-${version}.tgz`,
108106
...generateSignature(packageName, version),
109107
},

tests/main.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,30 @@ it(`should download yarn berry from custom registry`, async () => {
887887
});
888888
});
889889

890+
it(`should download latest pnpm from custom registry`, async () => {
891+
await xfs.mktempPromise(async cwd => {
892+
process.env.AUTH_TYPE = `COREPACK_NPM_TOKEN`; // See `_registryServer.mjs`
893+
process.env.COREPACK_DEFAULT_TO_LATEST = `1`;
894+
process.env.COREPACK_INTEGRITY_KEYS = `0`;
895+
896+
await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as Filename), {
897+
});
898+
899+
await expect(runCli(cwd, [`pnpm`, `--version`], true)).resolves.toMatchObject({
900+
exitCode: 0,
901+
stdout: `pnpm: Hello from custom registry\n`,
902+
stderr: /^! The local project doesn't define a 'packageManager' field\. Corepack will now add one referencing pnpm@1\.9998\.9999@sha1\./,
903+
});
904+
905+
// Should keep working with cache
906+
await expect(runCli(cwd, [`pnpm`, `--version`])).resolves.toMatchObject({
907+
exitCode: 0,
908+
stdout: `pnpm: Hello from custom registry\n`,
909+
stderr: ``,
910+
});
911+
});
912+
});
913+
890914
for (const authType of [`COREPACK_NPM_REGISTRY`, `COREPACK_NPM_TOKEN`, `COREPACK_NPM_PASSWORD`, `PROXY`]) {
891915
describe(`custom registry with auth ${authType}`, () => {
892916
beforeEach(() => {

0 commit comments

Comments
 (0)