2020-05-04 16:23:54 -07:00
|
|
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
2023-02-21 10:21:56 -08:00
|
|
|
import { Service } from 'typedi';
|
2023-12-27 02:50:43 -08:00
|
|
|
import type { IExternalHooksFileData, IExternalHooksFunctions } from '@/Interfaces';
|
2022-11-09 06:25:00 -08:00
|
|
|
import config from '@/config';
|
2023-11-10 06:04:26 -08:00
|
|
|
import { UserRepository } from '@db/repositories/user.repository';
|
|
|
|
import { CredentialsRepository } from '@db/repositories/credentials.repository';
|
|
|
|
import { SettingsRepository } from '@db/repositories/settings.repository';
|
|
|
|
import { WorkflowRepository } from '@db/repositories/workflow.repository';
|
2023-11-29 03:25:10 -08:00
|
|
|
import { ApplicationError } from 'n8n-workflow';
|
2020-05-03 23:56:01 -07:00
|
|
|
|
2023-02-21 10:21:56 -08:00
|
|
|
@Service()
|
2023-12-27 02:50:43 -08:00
|
|
|
export class ExternalHooks {
|
2020-05-03 23:56:01 -07:00
|
|
|
externalHooks: {
|
|
|
|
[key: string]: Array<() => {}>;
|
|
|
|
} = {};
|
|
|
|
|
2023-11-10 06:04:26 -08:00
|
|
|
private initDidRun = false;
|
|
|
|
|
|
|
|
private dbCollections: IExternalHooksFunctions['dbCollections'];
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
userRepository: UserRepository,
|
|
|
|
settingsRepository: SettingsRepository,
|
|
|
|
credentialsRepository: CredentialsRepository,
|
|
|
|
workflowRepository: WorkflowRepository,
|
|
|
|
) {
|
|
|
|
/* eslint-disable @typescript-eslint/naming-convention */
|
|
|
|
this.dbCollections = {
|
|
|
|
User: userRepository,
|
|
|
|
Settings: settingsRepository,
|
|
|
|
Credentials: credentialsRepository,
|
|
|
|
Workflow: workflowRepository,
|
|
|
|
};
|
|
|
|
/* eslint-enable @typescript-eslint/naming-convention */
|
|
|
|
}
|
2020-05-03 23:56:01 -07:00
|
|
|
|
|
|
|
async init(): Promise<void> {
|
2020-05-05 15:59:58 -07:00
|
|
|
if (this.initDidRun) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-25 06:42:33 -08:00
|
|
|
await this.loadHooksFiles();
|
|
|
|
|
|
|
|
this.initDidRun = true;
|
|
|
|
}
|
|
|
|
|
2020-12-30 02:45:29 -08:00
|
|
|
async reload(externalHooks?: IExternalHooksFileData) {
|
2020-11-25 06:42:33 -08:00
|
|
|
this.externalHooks = {};
|
2020-12-30 02:45:29 -08:00
|
|
|
|
|
|
|
if (externalHooks === undefined) {
|
|
|
|
await this.loadHooksFiles(true);
|
|
|
|
} else {
|
|
|
|
this.loadHooks(externalHooks);
|
|
|
|
}
|
2020-11-25 06:42:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
async loadHooksFiles(reload = false) {
|
2022-04-08 10:37:27 -07:00
|
|
|
const externalHookFiles = config.getEnv('externalHookFiles').split(':');
|
2020-05-03 23:56:01 -07:00
|
|
|
|
2020-05-04 16:23:54 -07:00
|
|
|
// Load all the provided hook-files
|
2020-05-03 23:56:01 -07:00
|
|
|
for (let hookFilePath of externalHookFiles) {
|
|
|
|
hookFilePath = hookFilePath.trim();
|
|
|
|
if (hookFilePath !== '') {
|
2020-05-04 16:23:54 -07:00
|
|
|
try {
|
2020-11-25 06:42:33 -08:00
|
|
|
if (reload) {
|
|
|
|
delete require.cache[require.resolve(hookFilePath)];
|
|
|
|
}
|
|
|
|
|
2020-12-30 02:45:29 -08:00
|
|
|
const hookFile = require(hookFilePath) as IExternalHooksFileData;
|
|
|
|
this.loadHooks(hookFile);
|
2023-11-29 03:25:10 -08:00
|
|
|
} catch (e) {
|
|
|
|
const error = e instanceof Error ? e : new Error(`${e}`);
|
|
|
|
|
|
|
|
throw new ApplicationError('Problem loading external hook file', {
|
|
|
|
extra: { errorMessage: error.message, hookFilePath },
|
|
|
|
cause: error,
|
|
|
|
});
|
2020-05-03 23:56:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-30 02:45:29 -08:00
|
|
|
loadHooks(hookFileData: IExternalHooksFileData) {
|
|
|
|
for (const resource of Object.keys(hookFileData)) {
|
|
|
|
for (const operation of Object.keys(hookFileData[resource])) {
|
|
|
|
// Save all the hook functions directly under their string
|
|
|
|
// format in an array
|
|
|
|
const hookString = `${resource}.${operation}`;
|
|
|
|
if (this.externalHooks[hookString] === undefined) {
|
|
|
|
this.externalHooks[hookString] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line prefer-spread
|
|
|
|
this.externalHooks[hookString].push.apply(
|
|
|
|
this.externalHooks[hookString],
|
|
|
|
hookFileData[resource][operation],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-04 16:23:54 -07:00
|
|
|
async run(hookName: string, hookParameters?: any[]): Promise<void> {
|
2020-05-03 23:56:01 -07:00
|
|
|
if (this.externalHooks[hookName] === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-10 06:04:26 -08:00
|
|
|
const externalHookFunctions: IExternalHooksFunctions = {
|
|
|
|
dbCollections: this.dbCollections,
|
|
|
|
};
|
|
|
|
|
2020-05-03 23:56:01 -07:00
|
|
|
for (const externalHookFunction of this.externalHooks[hookName]) {
|
2022-09-29 02:33:02 -07:00
|
|
|
await externalHookFunction.apply(externalHookFunctions, hookParameters);
|
2020-05-03 23:56:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 10:01:40 -07:00
|
|
|
exists(hookName: string): boolean {
|
|
|
|
return !!this.externalHooks[hookName];
|
|
|
|
}
|
2020-05-03 23:56:01 -07:00
|
|
|
}
|