Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor NpmLocation class #2038

Merged
merged 8 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/snaps-controllers/coverage.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"branches": 90.09,
"functions": 96.35,
"lines": 97.31,
"statements": 96.99
"branches": 90.8,
"functions": 96.36,
"lines": 97.63,
"statements": 97.3
}
87 changes: 82 additions & 5 deletions packages/snaps-controllers/src/snaps/location/npm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import fetchMock from 'jest-fetch-mock';
import path from 'path';
import { Readable } from 'stream';

import { NpmLocation } from './npm';
import {
DEFAULT_NPM_REGISTRY,
NpmLocation,
getNpmCanonicalBasePath,
} from './npm';

fetchMock.enableMocks();

Expand Down Expand Up @@ -68,9 +72,17 @@ describe('NpmLocation', () => {
);

const manifest = await location.manifest();
const sourceCode = (
await location.fetch(manifest.result.source.location.npm.filePath)
).toString();
expect(manifest.path).toBe('snap.manifest.json');
expect(manifest.data.canonicalPath).toBe(
'npm://registry.npmjs.cf/@metamask/template-snap/snap.manifest.json',
);
const sourceCode = await location.fetch(
manifest.result.source.location.npm.filePath,
);
expect(sourceCode.path).toBe('dist/bundle.js');
expect(sourceCode.data.canonicalPath).toBe(
'npm://registry.npmjs.cf/@metamask/template-snap/dist/bundle.js',
);
assert(manifest.result.source.location.npm.iconPath);
const svgIcon = (
await location.fetch(manifest.result.source.location.npm.iconPath)
Expand All @@ -94,7 +106,7 @@ describe('NpmLocation', () => {
),
);

expect(sourceCode).toStrictEqual(
expect(sourceCode.toString()).toStrictEqual(
(
await readFile(
require.resolve('@metamask/template-snap/dist/bundle.js'),
Expand All @@ -105,6 +117,8 @@ describe('NpmLocation', () => {
expect(svgIcon?.startsWith('<svg') && svgIcon.endsWith('</svg>')).toBe(
true,
);

expect(location.version).toBe(templateSnapVersion);
});

it('fetches a package tarball directly without fetching the metadata when possible', async () => {
Expand Down Expand Up @@ -170,6 +184,8 @@ describe('NpmLocation', () => {
expect(svgIcon?.startsWith('<svg') && svgIcon.endsWith('</svg>')).toBe(
true,
);

expect(location.version).toBe(templateSnapVersion);
});

it('falls back to zlib if DecompressionStream is unavailable', async () => {
Expand Down Expand Up @@ -251,6 +267,50 @@ describe('NpmLocation', () => {
);
});

it('throws if fetching the NPM tarball fails', async () => {
const { version: templateSnapVersion } = JSON.parse(
(
await readFile(require.resolve('@metamask/template-snap/package.json'))
).toString('utf8'),
);

const tarballRegistry = `https://registry.npmjs.org/@metamask/template-snap/-/template-snap-${templateSnapVersion}.tgz`;

const customFetchMock = jest.fn();

customFetchMock
.mockResolvedValueOnce({
ok: true,
json: async () => ({
// eslint-disable-next-line @typescript-eslint/naming-convention
'dist-tags': {
latest: templateSnapVersion,
},
versions: {
[templateSnapVersion]: {
dist: {
// return npmjs.org registry here so that we can check overriding it with npmjs.cf works
tarball: tarballRegistry,
},
},
},
}),
} as any)
.mockResolvedValue({
ok: false,
body: null,
} as any);

const location = new NpmLocation(new URL('npm:@metamask/template-snap'), {
versionRange: templateSnapVersion,
fetch: customFetchMock as typeof fetch,
});

await expect(location.manifest()).rejects.toThrow(
'Failed to fetch tarball for package "@metamask/template-snap"',
);
});

it("can't use custom registries by default", () => {
expect(
() =>
Expand Down Expand Up @@ -288,3 +348,20 @@ describe('NpmLocation', () => {
// TODO(ritave): Requires writing tarball packing utility out of scope for a hot-fix blocking release.
it.todo('paths are normalized to remove "./" prefix');
});

describe('getNpmCanonicalBasePath', () => {
it('returns the default base path', () => {
expect(
getNpmCanonicalBasePath(DEFAULT_NPM_REGISTRY, '@metamask/example-snap'),
).toBe('npm://registry.npmjs.org/@metamask/example-snap/');
});

it('returns a path for a custom registry', () => {
expect(
getNpmCanonicalBasePath(
new URL('https://foo:[email protected]/'),
'@metamask/example-snap',
),
).toBe('npm://foo:[email protected]/@metamask/example-snap/');
});
});
Loading