Skip to content

Commit dd4e37c

Browse files
feat: Add NPM proxy support (#3695)
Add a `useNpmProxy` flag to the NPM location options. When enabled, this proxy URL is used instead of the main NPM registry URL. The implementation assumes that the proxy returns identical content and respects the same headers. This flag is enabled only for updating preinstalled Snaps. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Adds a `useNpmProxy` option to NPM locations and routes automatic preinstalled Snap updates through the MetaMask NPM proxy. > > - **Snaps Controller** (`packages/snaps-controllers/src/snaps/SnapController.ts`): > - Use `detectSnapLocation(..., { useNpmProxy: true })` when auto-updating preinstalled Snaps during `updateRegistry`. > - **NPM Location** (`packages/snaps-controllers/src/snaps/location/npm.ts`): > - Add `NPM_REGISTRY_PROXY` and new `NpmOptions.useNpmProxy` flag. > - Default registry selection now uses proxy when `useNpmProxy` is true. > - Update `isNPM` to treat proxy URL as NPM. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 4dda778. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 0b412ce commit dd4e37c

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

packages/snaps-controllers/src/snaps/SnapController.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,6 +1516,7 @@ export class SnapController extends BaseController<
15161516
versionRange: resolvedVersion,
15171517
fetch: this.#fetchFunction,
15181518
allowLocal: false,
1519+
useNpmProxy: true,
15191520
});
15201521

15211522
await this.#updateSnap({

packages/snaps-controllers/src/snaps/location/npm.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import type { DetectSnapLocationOptions, SnapLocation } from './location';
2828

2929
export const DEFAULT_NPM_REGISTRY = new URL('https://registry.npmjs.org');
3030

31+
export const NPM_REGISTRY_PROXY = new URL('https://npm-ota.api.cx.metamask.io');
32+
3133
type NpmMeta = {
3234
registry: URL;
3335
packageName: string;
@@ -48,6 +50,12 @@ export type NpmOptions = {
4850
* @default false
4951
*/
5052
allowCustomRegistries?: boolean;
53+
/**
54+
* Whether to use a MetaMask-owned proxy when sending requests to NPM.
55+
*
56+
* @default false
57+
*/
58+
useNpmProxy?: boolean;
5159
};
5260

5361
// Base class for NPM implementation, useful for extending with custom NPM fetching logic
@@ -74,7 +82,7 @@ export abstract class BaseNpmLocation implements SnapLocation {
7482
url.username === '' &&
7583
url.password === ''
7684
) {
77-
registry = DEFAULT_NPM_REGISTRY;
85+
registry = opts.useNpmProxy ? NPM_REGISTRY_PROXY : DEFAULT_NPM_REGISTRY;
7886
} else {
7987
registry = 'https://';
8088
if (url.username) {
@@ -341,7 +349,11 @@ export function getNpmCanonicalBasePath(registryUrl: URL, packageName: string) {
341349
* @returns True if the registry is the NPM registry, otherwise false.
342350
*/
343351
function isNPM(registryUrl: URL) {
344-
return registryUrl.toString() === DEFAULT_NPM_REGISTRY.toString();
352+
const registryUrlString = registryUrl.toString();
353+
return (
354+
registryUrlString === DEFAULT_NPM_REGISTRY.toString() ||
355+
registryUrlString === NPM_REGISTRY_PROXY.toString()
356+
);
345357
}
346358

347359
/**

0 commit comments

Comments
 (0)