From 6f45298d3d61b33e762f520129f4775e216707c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=A4=95=E0=A4=BE=E0=A4=B0=E0=A4=A4=E0=A5=8B=E0=A4=AB?= =?UTF-8?q?=E0=A5=8D=E0=A4=AB=E0=A5=87=E0=A4=B2=E0=A4=B8=E0=A5=8D=E0=A4=95?= =?UTF-8?q?=E0=A5=8D=E0=A4=B0=E0=A4=BF=E0=A4=AA=E0=A5=8D=E0=A4=9F=E2=84=A2?= Date: Wed, 25 Oct 2023 13:59:38 +0200 Subject: [PATCH] fix(core): Ensure nodes post-processors run in the correct order (#7500) Fixes #7497 --- packages/cli/src/Server.ts | 66 +++++++++---------- packages/cli/src/WorkflowRunnerProcess.ts | 4 +- packages/cli/src/commands/BaseCommand.ts | 2 +- packages/cli/src/services/frontend.service.ts | 3 + 4 files changed, 38 insertions(+), 37 deletions(-) diff --git a/packages/cli/src/Server.ts b/packages/cli/src/Server.ts index 91cdaffbaf..cfe52d906a 100644 --- a/packages/cli/src/Server.ts +++ b/packages/cli/src/Server.ts @@ -123,7 +123,7 @@ import { toHttpNodeParameters } from '@/CurlConverterHelper'; import { EventBusController } from '@/eventbus/eventBus.controller'; import { EventBusControllerEE } from '@/eventbus/eventBus.controller.ee'; import { licenseController } from './license/license.controller'; -import { Push, setupPushServer, setupPushHandler } from '@/push'; +import { setupPushServer, setupPushHandler } from '@/push'; import { setupAuthMiddlewares } from './middlewares'; import { handleLdapInit, isLdapEnabled } from './Ldap/helpers'; import { AbstractServer } from './AbstractServer'; @@ -171,12 +171,10 @@ export class Server extends AbstractServer { private credentialTypes: ICredentialTypes; - private frontendService: FrontendService; + private frontendService?: FrontendService; private postHog: PostHogClient; - private push: Push; - constructor() { super('main'); @@ -194,13 +192,8 @@ export class Server extends AbstractServer { this.nodeTypes = Container.get(NodeTypes); if (!config.getEnv('endpoints.disableUi')) { - // eslint-disable-next-line @typescript-eslint/naming-convention - const { FrontendService } = await import('@/services/frontend.service'); - this.frontendService = Container.get(FrontendService); - this.loadNodesAndCredentials.addPostProcessor(async () => - this.frontendService.generateTypes(), - ); - await this.frontendService.generateTypes(); + // eslint-disable-next-line @typescript-eslint/no-var-requires + this.frontendService = Container.get(require('@/services/frontend.service').FrontendService); } this.activeExecutionsInstance = Container.get(ActiveExecutions); @@ -210,8 +203,6 @@ export class Server extends AbstractServer { this.presetCredentialsLoaded = false; this.endpointPresetCredentials = config.getEnv('credentials.overwrite.endpoint'); - this.push = Container.get(Push); - await super.start(); LoggerProxy.debug(`Server ID: ${this.uniqueInstanceId}`); @@ -372,14 +363,17 @@ export class Server extends AbstractServer { await Container.get(MetricsService).configureMetrics(this.app); } - this.frontendService.addToSettings({ - isNpmAvailable: await exec('npm --version') - .then(() => true) - .catch(() => false), - versionCli: N8N_VERSION, - }); + const { frontendService } = this; + if (frontendService) { + frontendService.addToSettings({ + isNpmAvailable: await exec('npm --version') + .then(() => true) + .catch(() => false), + versionCli: N8N_VERSION, + }); - await this.externalHooks.run('frontend.settings', [this.frontendService.getSettings()]); + await this.externalHooks.run('frontend.settings', [frontendService.getSettings()]); + } await this.postHog.init(); @@ -408,7 +402,9 @@ export class Server extends AbstractServer { if (isApiEnabled()) { const { apiRouters, apiLatestVersion } = await loadPublicApiVersions(publicApiEndpoint); this.app.use(...apiRouters); - this.frontendService.settings.publicApi.latestVersion = apiLatestVersion; + if (frontendService) { + frontendService.settings.publicApi.latestVersion = apiLatestVersion; + } } // Parse cookies for easier access this.app.use(cookieParser()); @@ -1187,17 +1183,21 @@ export class Server extends AbstractServer { // Settings // ---------------------------------------- - // Returns the current settings for the UI - this.app.get( - `/${this.restEndpoint}/settings`, - ResponseHelper.send( - async (req: express.Request, res: express.Response): Promise => { - void Container.get(InternalHooks).onFrontendSettingsAPI(req.headers.sessionid as string); + if (frontendService) { + // Returns the current settings for the UI + this.app.get( + `/${this.restEndpoint}/settings`, + ResponseHelper.send( + async (req: express.Request, res: express.Response): Promise => { + void Container.get(InternalHooks).onFrontendSettingsAPI( + req.headers.sessionid as string, + ); - return this.frontendService.getSettings(); - }, - ), - ); + return frontendService.getSettings(); + }, + ), + ); + } // ---------------------------------------- // EventBus Setup @@ -1227,7 +1227,7 @@ export class Server extends AbstractServer { Container.get(CredentialsOverwrites).setData(body); - await this.frontendService?.generateTypes(); + await frontendService?.generateTypes(); this.presetCredentialsLoaded = true; @@ -1239,7 +1239,7 @@ export class Server extends AbstractServer { ); } - if (!config.getEnv('endpoints.disableUi')) { + if (frontendService) { const staticOptions: ServeStaticOptions = { cacheControl: false, setHeaders: (res: express.Response, path: string) => { diff --git a/packages/cli/src/WorkflowRunnerProcess.ts b/packages/cli/src/WorkflowRunnerProcess.ts index 3c8385f8e3..133b729c2f 100644 --- a/packages/cli/src/WorkflowRunnerProcess.ts +++ b/packages/cli/src/WorkflowRunnerProcess.ts @@ -107,10 +107,8 @@ class WorkflowRunnerProcess { // Init db since we need to read the license. await Db.init(); - const loadNodesAndCredentials = Container.get(LoadNodesAndCredentials); - await loadNodesAndCredentials.init(); - const nodeTypes = Container.get(NodeTypes); + await Container.get(LoadNodesAndCredentials).init(); // Load all external hooks const externalHooks = Container.get(ExternalHooks); diff --git a/packages/cli/src/commands/BaseCommand.ts b/packages/cli/src/commands/BaseCommand.ts index 0c9f3ae2a4..6ebbee8319 100644 --- a/packages/cli/src/commands/BaseCommand.ts +++ b/packages/cli/src/commands/BaseCommand.ts @@ -47,8 +47,8 @@ export abstract class BaseCommand extends Command { // Make sure the settings exist this.instanceSettings = Container.get(InstanceSettings); - await Container.get(LoadNodesAndCredentials).init(); this.nodeTypes = Container.get(NodeTypes); + await Container.get(LoadNodesAndCredentials).init(); await Db.init().catch(async (error: Error) => this.exitWithCrash('There was an error initializing DB', error), diff --git a/packages/cli/src/services/frontend.service.ts b/packages/cli/src/services/frontend.service.ts index e3c4cfef12..89c57f6b82 100644 --- a/packages/cli/src/services/frontend.service.ts +++ b/packages/cli/src/services/frontend.service.ts @@ -46,6 +46,9 @@ export class FrontendService { private readonly mailer: UserManagementMailer, private readonly instanceSettings: InstanceSettings, ) { + loadNodesAndCredentials.addPostProcessor(async () => this.generateTypes()); + void this.generateTypes(); + this.initSettings(); if (config.getEnv('nodes.communityPackages.enabled')) {