Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 20 additions & 0 deletions packages/snaps-controllers/src/snaps/location/npm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,26 @@ describe('NpmLocation', () => {
);
});

it('throws if NPM returns 404', async () => {
const customFetchMock = jest.fn().mockResolvedValue({
ok: false,
body: null,
status: 404,
} as any);

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

await expect(location.manifest()).rejects.toThrow(
'"@metamask/jsx-example-snap" was not found in the NPM registry',
);
});

it('throws if the NPM tarball URL is invalid', async () => {
const customFetchMock = jest.fn();

Expand Down
15 changes: 10 additions & 5 deletions packages/snaps-controllers/src/snaps/location/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,15 @@ export class NpmLocation extends BaseNpmLocation {
async fetchNpmTarball(tarballUrl: URL): Promise<Map<string, VirtualFile>> {
// Perform a raw fetch because we want the Response object itself.
const tarballResponse = await this.meta.fetch(tarballUrl.toString());
if (!tarballResponse.ok || !tarballResponse.body) {
throw new Error(
`Failed to fetch tarball for package "${this.meta.packageName}".`,
);
}

assert(
tarballResponse.status !== 404,
`"${this.meta.packageName}" was not found in the NPM registry`,
);
assert(
tarballResponse.ok && tarballResponse.body,
`Failed to fetch tarball for package "${this.meta.packageName}"`,
);

// We assume that NPM is a good actor and provides us with a valid `content-length` header.
const tarballSizeString = tarballResponse.headers.get('content-length');
Expand All @@ -234,6 +238,7 @@ export class NpmLocation extends BaseNpmLocation {
tarballSize <= TARBALL_SIZE_SAFETY_LIMIT,
'Snap tarball exceeds size limit',
);

return new Promise((resolve, reject) => {
const files = new Map();

Expand Down
Loading