From c0e4f69fe6fb994a671f74ad65912abc4d74bed4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Tue, 11 Jun 2024 10:53:16 +0200 Subject: [PATCH] refactor(core): Remove more dead code from event bus (no-changelog) (#9697) --- .../eventbus/EventMessageClasses/Helpers.ts | 102 ------------------ .../MessageEventBusLogWriter.ts | 31 +++++- 2 files changed, 27 insertions(+), 106 deletions(-) delete mode 100644 packages/cli/src/eventbus/EventMessageClasses/Helpers.ts diff --git a/packages/cli/src/eventbus/EventMessageClasses/Helpers.ts b/packages/cli/src/eventbus/EventMessageClasses/Helpers.ts deleted file mode 100644 index 31f3e4d106c22..0000000000000 --- a/packages/cli/src/eventbus/EventMessageClasses/Helpers.ts +++ /dev/null @@ -1,102 +0,0 @@ -import type { EventMessageTypes } from '.'; -import type { EventMessageGenericOptions } from './EventMessageGeneric'; -import { EventMessageGeneric } from './EventMessageGeneric'; -import type { AbstractEventMessageOptions } from './AbstractEventMessageOptions'; -import type { EventMessageWorkflowOptions } from './EventMessageWorkflow'; -import { EventMessageWorkflow } from './EventMessageWorkflow'; -import { EventMessageTypeNames } from 'n8n-workflow'; -import type { EventMessageAuditOptions } from './EventMessageAudit'; -import { EventMessageAudit } from './EventMessageAudit'; -import type { EventMessageNodeOptions } from './EventMessageNode'; -import { EventMessageNode } from './EventMessageNode'; - -export const getEventMessageObjectByType = ( - message: AbstractEventMessageOptions, -): EventMessageTypes | null => { - switch (message.__type as EventMessageTypeNames) { - case EventMessageTypeNames.generic: - return new EventMessageGeneric(message as EventMessageGenericOptions); - case EventMessageTypeNames.workflow: - return new EventMessageWorkflow(message as EventMessageWorkflowOptions); - case EventMessageTypeNames.audit: - return new EventMessageAudit(message as EventMessageAuditOptions); - case EventMessageTypeNames.node: - return new EventMessageNode(message as EventMessageNodeOptions); - default: - return null; - } -}; - -interface StringIndexedObject { - [key: string]: StringIndexedObject | string; -} - -export function eventGroupFromEventName(eventName: string): string | undefined { - const matches = eventName.match(/^[\w\s]+\.[\w\s]+/); - if (matches && matches?.length > 0) { - return matches[0]; - } - return; -} - -function dotsToObject2(dottedString: string, o?: StringIndexedObject): StringIndexedObject { - const rootObject: StringIndexedObject = o ?? {}; - if (!dottedString) return rootObject; - - const parts = dottedString.split('.'); /*?*/ - - let part: string | undefined; - let obj: StringIndexedObject = rootObject; - while ((part = parts.shift())) { - if (typeof obj[part] !== 'object') { - obj[part] = { - __name: part, - }; - } - obj = obj[part] as StringIndexedObject; - } - return rootObject; -} - -export function eventListToObject(dottedList: string[]): object { - const result = {}; - dottedList.forEach((e) => { - dotsToObject2(e, result); - }); - return result; -} - -interface StringIndexedChild { - name: string; - children: StringIndexedChild[]; -} - -export function eventListToObjectTree(dottedList: string[]): StringIndexedChild { - const x: StringIndexedChild = { - name: 'eventTree', - children: [] as unknown as StringIndexedChild[], - }; - dottedList.forEach((dottedString: string) => { - const parts = dottedString.split('.'); - - let part: string | undefined; - let children = x.children; - while ((part = parts.shift())) { - if (part) { - // eslint-disable-next-line @typescript-eslint/no-loop-func - const foundChild = children.find((e) => e.name === part); - if (foundChild) { - children = foundChild.children; - } else { - const newChild: StringIndexedChild = { - name: part, - children: [], - }; - children.push(newChild); - children = newChild.children; - } - } - } - }); - return x; -} diff --git a/packages/cli/src/eventbus/MessageEventBusWriter/MessageEventBusLogWriter.ts b/packages/cli/src/eventbus/MessageEventBusWriter/MessageEventBusLogWriter.ts index 7565cb481482b..6c59b2e24e9cc 100644 --- a/packages/cli/src/eventbus/MessageEventBusWriter/MessageEventBusLogWriter.ts +++ b/packages/cli/src/eventbus/MessageEventBusWriter/MessageEventBusLogWriter.ts @@ -6,10 +6,18 @@ import path, { parse } from 'path'; import { Worker } from 'worker_threads'; import { createReadStream, existsSync, rmSync } from 'fs'; import readline from 'readline'; -import { jsonParse } from 'n8n-workflow'; import remove from 'lodash/remove'; import config from '@/config'; -import { getEventMessageObjectByType } from '../EventMessageClasses/Helpers'; +import type { EventMessageGenericOptions } from '../EventMessageClasses/EventMessageGeneric'; +import { EventMessageGeneric } from '../EventMessageClasses/EventMessageGeneric'; +import type { AbstractEventMessageOptions } from '../EventMessageClasses/AbstractEventMessageOptions'; +import type { EventMessageWorkflowOptions } from '../EventMessageClasses/EventMessageWorkflow'; +import { EventMessageWorkflow } from '../EventMessageClasses/EventMessageWorkflow'; +import { EventMessageTypeNames, jsonParse } from 'n8n-workflow'; +import type { EventMessageAuditOptions } from '../EventMessageClasses/EventMessageAudit'; +import { EventMessageAudit } from '../EventMessageClasses/EventMessageAudit'; +import type { EventMessageNodeOptions } from '../EventMessageClasses/EventMessageNode'; +import { EventMessageNode } from '../EventMessageClasses/EventMessageNode'; import type { EventMessageReturnMode } from '../MessageEventBus/MessageEventBus'; import type { EventMessageTypes } from '../EventMessageClasses'; import type { EventMessageConfirmSource } from '../EventMessageClasses/EventMessageConfirm'; @@ -200,7 +208,7 @@ export class MessageEventBusLogWriter { try { const json = jsonParse(line); if (isEventMessageOptions(json) && json.__type !== undefined) { - const msg = getEventMessageObjectByType(json); + const msg = this.getEventMessageObjectByType(json); if (msg !== null) results.loggedMessages.push(msg); if (msg?.eventName && msg.payload?.executionId) { const executionId = msg.payload.executionId as string; @@ -302,7 +310,7 @@ export class MessageEventBusLogWriter { json.__type !== undefined && json.payload?.executionId === executionId ) { - const msg = getEventMessageObjectByType(json); + const msg = this.getEventMessageObjectByType(json); if (msg !== null) messages.push(msg); } } catch { @@ -346,4 +354,19 @@ export class MessageEventBusLogWriter { unfinishedExecutions: result.unfinishedExecutions, }; } + + getEventMessageObjectByType(message: AbstractEventMessageOptions): EventMessageTypes | null { + switch (message.__type as EventMessageTypeNames) { + case EventMessageTypeNames.generic: + return new EventMessageGeneric(message as EventMessageGenericOptions); + case EventMessageTypeNames.workflow: + return new EventMessageWorkflow(message as EventMessageWorkflowOptions); + case EventMessageTypeNames.audit: + return new EventMessageAudit(message as EventMessageAuditOptions); + case EventMessageTypeNames.node: + return new EventMessageNode(message as EventMessageNodeOptions); + default: + return null; + } + } }