refactor(core): Use an IoC container to manage singleton classes [Part-1] (no-changelog) (#5509)

* add typedi

* convert ActiveWorkflowRunner into an injectable service

* convert ExternalHooks into an injectable service

* convert InternalHooks into an injectable service

* convert LoadNodesAndCredentials into an injectable service

* convert NodeTypes and CredentialTypes into an injectable service

* convert ActiveExecutions into an injectable service

* convert WaitTracker into an injectable service

* convert Push into an injectable service

* convert ActiveWebhooks and  TestWebhooks into an injectable services

* handle circular references, and log errors when a circular dependency is found
This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2023-02-21 19:21:56 +01:00
committed by GitHub
parent aca94bb995
commit 52f740b9e8
79 changed files with 594 additions and 634 deletions

View File

@@ -8,6 +8,7 @@ import config from '@/config';
import { NodeTypes } from '@/NodeTypes';
import * as ResponseHelper from '@/ResponseHelper';
import { getNodeTranslationPath } from '@/TranslationHelpers';
import { Container } from 'typedi';
export const nodeTypesController = express.Router();
@@ -21,7 +22,7 @@ nodeTypesController.post(
if (defaultLocale === 'en') {
return nodeInfos.reduce<INodeTypeDescription[]>((acc, { name, version }) => {
const { description } = NodeTypes().getByNameAndVersion(name, version);
const { description } = Container.get(NodeTypes).getByNameAndVersion(name, version);
acc.push(description);
return acc;
}, []);
@@ -32,7 +33,7 @@ nodeTypesController.post(
version: number,
nodeTypes: INodeTypeDescription[],
) {
const { description, sourcePath } = NodeTypes().getWithSourcePath(name, version);
const { description, sourcePath } = Container.get(NodeTypes).getWithSourcePath(name, version);
const translationPath = await getNodeTranslationPath({
nodeSourcePath: sourcePath,
longNodeType: description.name,

View File

@@ -2,7 +2,6 @@ import express from 'express';
import type { PublicInstalledPackage } from 'n8n-workflow';
import config from '@/config';
import { InternalHooksManager } from '@/InternalHooksManager';
import { LoadNodesAndCredentials } from '@/LoadNodesAndCredentials';
import * as ResponseHelper from '@/ResponseHelper';
@@ -33,7 +32,9 @@ import { isAuthenticatedRequest } from '@/UserManagement/UserManagementHelper';
import type { InstalledPackages } from '@db/entities/InstalledPackages';
import type { CommunityPackages } from '@/Interfaces';
import type { NodeRequest } from '@/requests';
import { getPushInstance } from '@/push';
import { Push } from '@/push';
import { Container } from 'typedi';
import { InternalHooks } from '@/InternalHooks';
const { PACKAGE_NOT_INSTALLED, PACKAGE_NAME_NOT_PROVIDED } = RESPONSE_ERROR_MESSAGES;
@@ -116,14 +117,14 @@ nodesController.post(
let installedPackage: InstalledPackages;
try {
installedPackage = await LoadNodesAndCredentials().loadNpmModule(
installedPackage = await Container.get(LoadNodesAndCredentials).loadNpmModule(
parsed.packageName,
parsed.version,
);
} catch (error) {
const errorMessage = error instanceof Error ? error.message : UNKNOWN_FAILURE_REASON;
void InternalHooksManager.getInstance().onCommunityPackageInstallFinished({
void Container.get(InternalHooks).onCommunityPackageInstallFinished({
user: req.user,
input_string: name,
package_name: parsed.packageName,
@@ -141,7 +142,7 @@ nodesController.post(
if (!hasLoaded) removePackageFromMissingList(name);
const pushInstance = getPushInstance();
const pushInstance = Container.get(Push);
// broadcast to connected frontends that node list has been updated
installedPackage.installedNodes.forEach((node) => {
@@ -151,7 +152,7 @@ nodesController.post(
});
});
void InternalHooksManager.getInstance().onCommunityPackageInstallFinished({
void Container.get(InternalHooks).onCommunityPackageInstallFinished({
user: req.user,
input_string: name,
package_name: parsed.packageName,
@@ -238,7 +239,7 @@ nodesController.delete(
}
try {
await LoadNodesAndCredentials().removeNpmModule(name, installedPackage);
await Container.get(LoadNodesAndCredentials).removeNpmModule(name, installedPackage);
} catch (error) {
const message = [
`Error removing package "${name}"`,
@@ -248,7 +249,7 @@ nodesController.delete(
throw new ResponseHelper.InternalServerError(message);
}
const pushInstance = getPushInstance();
const pushInstance = Container.get(Push);
// broadcast to connected frontends that node list has been updated
installedPackage.installedNodes.forEach((node) => {
@@ -258,7 +259,7 @@ nodesController.delete(
});
});
void InternalHooksManager.getInstance().onCommunityPackageDeleteFinished({
void Container.get(InternalHooks).onCommunityPackageDeleteFinished({
user: req.user,
package_name: name,
package_version: installedPackage.installedVersion,
@@ -290,12 +291,12 @@ nodesController.patch(
}
try {
const newInstalledPackage = await LoadNodesAndCredentials().updateNpmModule(
const newInstalledPackage = await Container.get(LoadNodesAndCredentials).updateNpmModule(
parseNpmPackageName(name).packageName,
previouslyInstalledPackage,
);
const pushInstance = getPushInstance();
const pushInstance = Container.get(Push);
// broadcast to connected frontends that node list has been updated
previouslyInstalledPackage.installedNodes.forEach((node) => {
@@ -312,7 +313,7 @@ nodesController.patch(
});
});
void InternalHooksManager.getInstance().onCommunityPackageUpdateFinished({
void Container.get(InternalHooks).onCommunityPackageUpdateFinished({
user: req.user,
package_name: name,
package_version_current: previouslyInstalledPackage.installedVersion,
@@ -325,7 +326,7 @@ nodesController.patch(
return newInstalledPackage;
} catch (error) {
previouslyInstalledPackage.installedNodes.forEach((node) => {
const pushInstance = getPushInstance();
const pushInstance = Container.get(Push);
pushInstance.send('removeNodeType', {
name: node.type,
version: node.latestVersion,

View File

@@ -9,15 +9,14 @@ import express from 'express';
import * as Db from '@/Db';
import { ExternalHooks } from '@/ExternalHooks';
import type { IExternalHooksClass, ITagWithCountDb } from '@/Interfaces';
import type { ITagWithCountDb } from '@/Interfaces';
import * as ResponseHelper from '@/ResponseHelper';
import config from '@/config';
import * as TagHelpers from '@/TagHelpers';
import { validateEntity } from '@/GenericHelpers';
import { TagEntity } from '@db/entities/TagEntity';
import type { TagsRequest } from '@/requests';
export const externalHooks: IExternalHooksClass = ExternalHooks();
import { Container } from 'typedi';
export const tagsController = express.Router();
@@ -50,12 +49,12 @@ tagsController.post(
const newTag = new TagEntity();
newTag.name = req.body.name.trim();
await externalHooks.run('tag.beforeCreate', [newTag]);
await Container.get(ExternalHooks).run('tag.beforeCreate', [newTag]);
await validateEntity(newTag);
const tag = await Db.collections.Tag.save(newTag);
await externalHooks.run('tag.afterCreate', [tag]);
await Container.get(ExternalHooks).run('tag.afterCreate', [tag]);
return tag;
}),
@@ -74,12 +73,12 @@ tagsController.patch(
newTag.id = id;
newTag.name = name.trim();
await externalHooks.run('tag.beforeUpdate', [newTag]);
await Container.get(ExternalHooks).run('tag.beforeUpdate', [newTag]);
await validateEntity(newTag);
const tag = await Db.collections.Tag.save(newTag);
await externalHooks.run('tag.afterUpdate', [tag]);
await Container.get(ExternalHooks).run('tag.afterUpdate', [tag]);
return tag;
}),
@@ -100,11 +99,11 @@ tagsController.delete(
}
const id = req.params.id;
await externalHooks.run('tag.beforeDelete', [id]);
await Container.get(ExternalHooks).run('tag.beforeDelete', [id]);
await Db.collections.Tag.delete({ id });
await externalHooks.run('tag.afterDelete', [id]);
await Container.get(ExternalHooks).run('tag.afterDelete', [id]);
return true;
}),