Skip to content

Commit

Permalink
fix(core): Ensure nodes post-processors run in the correct order (n8n…
Browse files Browse the repository at this point in the history
  • Loading branch information
netroy authored Oct 25, 2023
1 parent c47d27d commit 6f45298
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 37 deletions.
66 changes: 33 additions & 33 deletions packages/cli/src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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');

Expand All @@ -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);
Expand All @@ -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}`);

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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<IN8nUISettings> => {
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<IN8nUISettings> => {
void Container.get(InternalHooks).onFrontendSettingsAPI(
req.headers.sessionid as string,
);

return this.frontendService.getSettings();
},
),
);
return frontendService.getSettings();
},
),
);
}

// ----------------------------------------
// EventBus Setup
Expand Down Expand Up @@ -1227,7 +1227,7 @@ export class Server extends AbstractServer {

Container.get(CredentialsOverwrites).setData(body);

await this.frontendService?.generateTypes();
await frontendService?.generateTypes();

this.presetCredentialsLoaded = true;

Expand All @@ -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) => {
Expand Down
4 changes: 1 addition & 3 deletions packages/cli/src/WorkflowRunnerProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/BaseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
3 changes: 3 additions & 0 deletions packages/cli/src/services/frontend.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand Down

0 comments on commit 6f45298

Please sign in to comment.