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

feat(core): Allow overriding npm registry for community packages #10325

Merged
merged 6 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions packages/@n8n/config/src/configs/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class CommunityPackagesConfig {
@Env('N8N_COMMUNITY_PACKAGES_ENABLED')
enabled: boolean = true;

/** NPM registry URL to pull community packages from */
@Env('N8N_COMMUNITY_PACKAGES_REGISTRY')
registry: string = 'https://registry.npmjs.org';
ivov marked this conversation as resolved.
Show resolved Hide resolved

/** Whether to reinstall any missing community packages */
@Env('N8N_REINSTALL_MISSING_PACKAGES')
reinstallMissing: boolean = false;
Expand Down
1 change: 1 addition & 0 deletions packages/@n8n/config/test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ describe('GlobalConfig', () => {
nodes: {
communityPackages: {
enabled: true,
registry: 'https://registry.npmjs.org',
reinstallMissing: false,
},
errorTriggerType: 'n8n-nodes-base.errorTrigger',
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/src/License.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@ export class License {
return this.isFeatureEnabled(LICENSE_FEATURES.PROJECT_ROLE_VIEWER);
}

isCustomNpmRegistryEnabled() {
return this.isFeatureEnabled(LICENSE_FEATURES.COMMUNITY_NODES_CUSTOM_REGISTRY);
}

getCurrentEntitlements() {
return this.manager?.getCurrentEntitlements() ?? [];
}
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export const LICENSE_FEATURES = {
PROJECT_ROLE_ADMIN: 'feat:projectRole:admin',
PROJECT_ROLE_EDITOR: 'feat:projectRole:editor',
PROJECT_ROLE_VIEWER: 'feat:projectRole:viewer',
COMMUNITY_NODES_CUSTOM_REGISTRY: 'feat:communityNodes:customRegistry',
netroy marked this conversation as resolved.
Show resolved Hide resolved
} as const;

export const LICENSE_QUOTAS = {
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/controllers/e2e.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export class E2EController {
[LICENSE_FEATURES.PROJECT_ROLE_ADMIN]: false,
[LICENSE_FEATURES.PROJECT_ROLE_EDITOR]: false,
[LICENSE_FEATURES.PROJECT_ROLE_VIEWER]: false,
[LICENSE_FEATURES.COMMUNITY_NODES_CUSTOM_REGISTRY]: false,
};

private numericFeatures: Record<NumericLicenseFeature, number> = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { InstalledNodesRepository } from '@db/repositories/installedNodes.reposi
import { InstalledPackagesRepository } from '@db/repositories/installedPackages.repository';
import { InstalledNodes } from '@db/entities/InstalledNodes';
import type { LoadNodesAndCredentials } from '@/LoadNodesAndCredentials';
import type { License } from '@/License';

import { mockInstance } from '@test/mocking';
import { COMMUNITY_NODE_VERSION, COMMUNITY_PACKAGE_VERSION } from '@test-integration/constants';
Expand All @@ -39,10 +40,12 @@ const execMock = ((...args) => {
}) as typeof exec;

describe('CommunityPackagesService', () => {
const license = mock<License>();
const globalConfig = mock<GlobalConfig>({
nodes: {
communityPackages: {
reinstallMissing: false,
registry: 'some.random.host',
},
},
});
Expand Down Expand Up @@ -74,6 +77,7 @@ describe('CommunityPackagesService', () => {
mock(),
loadNodesAndCredentials,
mock(),
license,
globalConfig,
);

Expand Down Expand Up @@ -393,6 +397,7 @@ describe('CommunityPackagesService', () => {
installedPackage.packageName = mockPackageName();

mocked(exec).mockImplementation(execMock);
license.isCustomNpmRegistryEnabled.mockReturnValue(true);
netroy marked this conversation as resolved.
Show resolved Hide resolved

//
// ACT
Expand All @@ -406,7 +411,7 @@ describe('CommunityPackagesService', () => {
expect(exec).toHaveBeenCalledTimes(1);
expect(exec).toHaveBeenNthCalledWith(
1,
`npm install ${installedPackage.packageName}@latest`,
`npm install ${installedPackage.packageName}@latest --registry=some.random.host`,
expect.any(Object),
expect.any(Function),
);
Expand Down
28 changes: 21 additions & 7 deletions packages/cli/src/services/communityPackages.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import type { CommunityPackages } from '@/Interfaces';
import { LoadNodesAndCredentials } from '@/LoadNodesAndCredentials';
import { Logger } from '@/Logger';
import { OrchestrationService } from './orchestration.service';
import { License } from '@/License';

const DEFAULT_REGISTRY = 'https://registry.npmjs.org';
netroy marked this conversation as resolved.
Show resolved Hide resolved

const {
PACKAGE_NAME_NOT_PROVIDED,
Expand Down Expand Up @@ -57,10 +60,9 @@ export class CommunityPackagesService {
private readonly installedPackageRepository: InstalledPackagesRepository,
private readonly loadNodesAndCredentials: LoadNodesAndCredentials,
private readonly orchestrationService: OrchestrationService,
globalConfig: GlobalConfig,
) {
this.reinstallMissingPackages = globalConfig.nodes.communityPackages.reinstallMissing;
}
private readonly license: License,
private readonly globalConfig: GlobalConfig,
) {}

get hasMissingPackages() {
return this.missingPackages.length > 0;
Expand Down Expand Up @@ -279,7 +281,8 @@ export class CommunityPackagesService {

if (missingPackages.size === 0) return;

if (this.reinstallMissingPackages) {
const { reinstallMissing } = this.globalConfig.nodes.communityPackages;
if (reinstallMissing) {
this.logger.info('Attempting to reinstall missing packages', { missingPackages });
try {
// Optimistic approach - stop if any installation fails
Expand Down Expand Up @@ -321,13 +324,22 @@ export class CommunityPackagesService {
await this.orchestrationService.publish('community-package-uninstall', { packageName });
}

private getNpmRegistry() {
let { registry } = this.globalConfig.nodes.communityPackages;
if (registry !== DEFAULT_REGISTRY && !this.license.isCustomNpmRegistryEnabled()) {
this.logger.warn('Not licensed to use custom NPM registry');
registry = DEFAULT_REGISTRY;
netroy marked this conversation as resolved.
Show resolved Hide resolved
}
return registry;
}

private async installOrUpdatePackage(
packageName: string,
options: { version?: string } | { installedPackage: InstalledPackages },
) {
const isUpdate = 'installedPackage' in options;
const packageVersion = isUpdate || !options.version ? 'latest' : options.version;
const command = `npm install ${packageName}@${packageVersion}`;
const command = `npm install ${packageName}@${packageVersion} --registry=${this.getNpmRegistry()}`;

try {
await this.executeNpmCommand(command);
Expand Down Expand Up @@ -379,7 +391,9 @@ export class CommunityPackagesService {
}

async installOrUpdateNpmPackage(packageName: string, packageVersion: string) {
await this.executeNpmCommand(`npm install ${packageName}@${packageVersion}`);
await this.executeNpmCommand(
`npm install ${packageName}@${packageVersion} --registry=${this.getNpmRegistry()}`,
);
await this.loadNodesAndCredentials.loadPackage(packageName);
await this.loadNodesAndCredentials.postProcessLoaders();
this.logger.info(`Community package installed: ${packageName}`);
Expand Down