From 38e91ab730e8107713e788327d8b4b76fd455ad4 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, 22 Mar 2023 14:04:15 +0100 Subject: [PATCH] refactor(core): Stop importing LoggerProxy and createDeferredPromise in nodes-base (no-changelog) (#5742) * refactor(core): Stop importing LoggerProxy in nodes-base * refactor(core): Stop importing createDeferredPromise in nodes-base --- packages/core/src/NodeExecuteFunctions.ts | 7 +++++ .../EmailReadImap/v1/EmailReadImapV1.node.ts | 23 +++++++++------- .../EmailReadImap/v2/EmailReadImapV2.node.ts | 27 ++++++++++--------- .../nodes/Google/Gmail/GmailTrigger.node.ts | 3 +-- .../nodes/RabbitMQ/RabbitMQTrigger.node.ts | 11 ++++---- .../nodes/Salesforce/GenericFunctions.ts | 6 ++--- .../nodes/Salesforce/Salesforce.node.ts | 4 +-- packages/workflow/src/DeferredPromise.ts | 2 +- packages/workflow/src/Interfaces.ts | 24 ++++++++++++++--- 9 files changed, 67 insertions(+), 40 deletions(-) diff --git a/packages/core/src/NodeExecuteFunctions.ts b/packages/core/src/NodeExecuteFunctions.ts index 70eced6ee6..44a694de8f 100644 --- a/packages/core/src/NodeExecuteFunctions.ts +++ b/packages/core/src/NodeExecuteFunctions.ts @@ -62,6 +62,7 @@ import type { FileSystemHelperFunctions, } from 'n8n-workflow'; import { + createDeferredPromise, NodeApiError, NodeHelpers, NodeOperationError, @@ -1956,6 +1957,7 @@ const getCommonWorkflowFunctions = ( node: INode, additionalData: IWorkflowExecuteAdditionalData, ): Omit => ({ + logger: Logger, getNode: () => deepCopy(node), getWorkflow: () => ({ id: workflow.id, @@ -2118,6 +2120,7 @@ export function getExecutePollFunctions( ); }, helpers: { + createDeferredPromise, ...getRequestHelperFunctions(workflow, node, additionalData), ...getBinaryHelperFunctions(additionalData), returnJsonArray, @@ -2176,6 +2179,7 @@ export function getExecuteTriggerFunctions( ); }, helpers: { + createDeferredPromise, ...getRequestHelperFunctions(workflow, node, additionalData), ...getBinaryHelperFunctions(additionalData), returnJsonArray, @@ -2351,6 +2355,7 @@ export function getExecuteFunctions( await additionalData.hooks?.executeHookFunctions('sendResponse', [response]); }, helpers: { + createDeferredPromise, ...getRequestHelperFunctions(workflow, node, additionalData), ...getFileSystemHelperFunctions(node), ...getBinaryHelperFunctions(additionalData), @@ -2492,6 +2497,7 @@ export function getExecuteSingleFunctions( return dataProxy.getDataProxy(); }, helpers: { + createDeferredPromise, ...getRequestHelperFunctions(workflow, node, additionalData), ...getBinaryHelperFunctions(additionalData), @@ -2747,6 +2753,7 @@ export function getExecuteWebhookFunctions( getWebhookName: () => webhookData.webhookDescription.name, prepareOutputData: NodeHelpers.prepareOutputData, helpers: { + createDeferredPromise, ...getRequestHelperFunctions(workflow, node, additionalData), ...getBinaryHelperFunctions(additionalData), returnJsonArray, diff --git a/packages/nodes-base/nodes/EmailReadImap/v1/EmailReadImapV1.node.ts b/packages/nodes-base/nodes/EmailReadImap/v1/EmailReadImapV1.node.ts index 547f099225..8d633f4239 100644 --- a/packages/nodes-base/nodes/EmailReadImap/v1/EmailReadImapV1.node.ts +++ b/packages/nodes-base/nodes/EmailReadImap/v1/EmailReadImapV1.node.ts @@ -7,7 +7,6 @@ import type { ICredentialsDecrypted, ICredentialTestFunctions, IDataObject, - IDeferredPromise, INodeCredentialTestResult, INodeExecutionData, INodeType, @@ -15,7 +14,7 @@ import type { INodeTypeDescription, ITriggerResponse, } from 'n8n-workflow'; -import { createDeferredPromise, LoggerProxy as Logger, NodeOperationError } from 'n8n-workflow'; +import { NodeOperationError } from 'n8n-workflow'; import type { ImapSimple, ImapSimpleOptions, Message } from 'imap-simple'; import { connect as imapConnect, getParts } from 'imap-simple'; @@ -272,7 +271,7 @@ export class EmailReadImapV1 implements INodeType { const options = this.getNodeParameter('options', {}) as IDataObject; const staticData = this.getWorkflowStaticData('node'); - Logger.debug('Loaded static data for node "EmailReadImap"', { staticData }); + this.logger.debug('Loaded static data for node "EmailReadImap"', { staticData }); let connection: ImapSimple; @@ -501,7 +500,7 @@ export class EmailReadImapV1 implements INodeType { return newEmails; }; - const returnedPromise: IDeferredPromise | undefined = await createDeferredPromise(); + const returnedPromise = await this.helpers.createDeferredPromise(); const establishConnection = async (): Promise => { let searchCriteria = ['UNSEEN'] as Array; @@ -538,7 +537,9 @@ export class EmailReadImapV1 implements INodeType { * - You can check if UIDs changed in the above example * by checking UIDValidity. */ - Logger.debug('Querying for new messages on node "EmailReadImap"', { searchCriteria }); + this.logger.debug('Querying for new messages on node "EmailReadImap"', { + searchCriteria, + }); } try { @@ -547,7 +548,7 @@ export class EmailReadImapV1 implements INodeType { this.emit([returnData]); } } catch (error) { - Logger.error('Email Read Imap node encountered an error fetching new emails', { + this.logger.error('Email Read Imap node encountered an error fetching new emails', { error, }); // Wait with resolving till the returnedPromise got resolved, else n8n will be unhappy @@ -580,17 +581,19 @@ export class EmailReadImapV1 implements INodeType { conn.on('error', async (error) => { const errorCode = error.code.toUpperCase(); if (['ECONNRESET', 'EPIPE'].includes(errorCode as string)) { - Logger.verbose(`IMAP connection was reset (${errorCode}) - reconnecting.`, { error }); + this.logger.verbose(`IMAP connection was reset (${errorCode}) - reconnecting.`, { + error, + }); try { connection = await establishConnection(); await connection.openBox(mailbox); return; } catch (e) { - Logger.error('IMAP reconnect did fail', { error: e }); + this.logger.error('IMAP reconnect did fail', { error: e }); // If something goes wrong we want to run emitError } } else { - Logger.error('Email Read Imap node encountered a connection error', { error }); + this.logger.error('Email Read Imap node encountered a connection error', { error }); this.emitError(error as Error); } }); @@ -606,7 +609,7 @@ export class EmailReadImapV1 implements INodeType { if (options.forceReconnect !== undefined) { reconnectionInterval = setInterval(async () => { - Logger.verbose('Forcing reconnection of IMAP node.'); + this.logger.verbose('Forcing reconnection of IMAP node.'); connection.end(); connection = await establishConnection(); await connection.openBox(mailbox); diff --git a/packages/nodes-base/nodes/EmailReadImap/v2/EmailReadImapV2.node.ts b/packages/nodes-base/nodes/EmailReadImap/v2/EmailReadImapV2.node.ts index 33e1c59595..97a985aa3c 100644 --- a/packages/nodes-base/nodes/EmailReadImap/v2/EmailReadImapV2.node.ts +++ b/packages/nodes-base/nodes/EmailReadImap/v2/EmailReadImapV2.node.ts @@ -6,7 +6,6 @@ import type { ICredentialsDecrypted, ICredentialTestFunctions, IDataObject, - IDeferredPromise, INodeCredentialTestResult, INodeExecutionData, INodeType, @@ -14,7 +13,7 @@ import type { INodeTypeDescription, ITriggerResponse, } from 'n8n-workflow'; -import { createDeferredPromise, LoggerProxy as Logger, NodeOperationError } from 'n8n-workflow'; +import { NodeOperationError } from 'n8n-workflow'; import type { ImapSimple, ImapSimpleOptions, Message } from 'imap-simple'; import { connect as imapConnect, getParts } from 'imap-simple'; @@ -278,7 +277,7 @@ export class EmailReadImapV2 implements INodeType { const options = this.getNodeParameter('options', {}) as IDataObject; const staticData = this.getWorkflowStaticData('node'); - Logger.debug('Loaded static data for node "EmailReadImap"', { staticData }); + this.logger.debug('Loaded static data for node "EmailReadImap"', { staticData }); let connection: ImapSimple; let closeFunctionWasCalled = false; @@ -509,7 +508,7 @@ export class EmailReadImapV2 implements INodeType { return newEmails; }; - const returnedPromise: IDeferredPromise | undefined = await createDeferredPromise(); + const returnedPromise = await this.helpers.createDeferredPromise(); const establishConnection = async (): Promise => { let searchCriteria = ['UNSEEN'] as Array; @@ -546,7 +545,9 @@ export class EmailReadImapV2 implements INodeType { * - You can check if UIDs changed in the above example * by checking UIDValidity. */ - Logger.debug('Querying for new messages on node "EmailReadImap"', { searchCriteria }); + this.logger.debug('Querying for new messages on node "EmailReadImap"', { + searchCriteria, + }); } try { @@ -555,7 +556,7 @@ export class EmailReadImapV2 implements INodeType { this.emit([returnData]); } } catch (error) { - Logger.error('Email Read Imap node encountered an error fetching new emails', { + this.logger.error('Email Read Imap node encountered an error fetching new emails', { error, }); // Wait with resolving till the returnedPromise got resolved, else n8n will be unhappy @@ -567,7 +568,7 @@ export class EmailReadImapV2 implements INodeType { } }, onupdate: async (seqno: number, info) => { - Logger.verbose(`Email Read Imap:update ${seqno}`, info as IDataObject); + this.logger.verbose(`Email Read Imap:update ${seqno}`, info as IDataObject); }, }; @@ -590,17 +591,17 @@ export class EmailReadImapV2 implements INodeType { return imapConnect(config).then(async (conn) => { conn.on('close', async (_hadError: boolean) => { if (isCurrentlyReconnecting) { - Logger.debug('Email Read Imap: Connected closed for forced reconnecting'); + this.logger.debug('Email Read Imap: Connected closed for forced reconnecting'); } else if (closeFunctionWasCalled) { - Logger.debug('Email Read Imap: Shutting down workflow - connected closed'); + this.logger.debug('Email Read Imap: Shutting down workflow - connected closed'); } else { - Logger.error('Email Read Imap: Connected closed unexpectedly'); + this.logger.error('Email Read Imap: Connected closed unexpectedly'); this.emitError(new Error('Imap connection closed unexpectedly')); } }); conn.on('error', async (error) => { const errorCode = error.code.toUpperCase(); - Logger.verbose(`IMAP connection experienced an error: (${errorCode})`, { error }); + this.logger.verbose(`IMAP connection experienced an error: (${errorCode})`, { error }); // eslint-disable-next-line @typescript-eslint/no-use-before-define await closeFunction(); this.emitError(error as Error); @@ -617,7 +618,7 @@ export class EmailReadImapV2 implements INodeType { if (options.forceReconnect !== undefined) { reconnectionInterval = setInterval(async () => { - Logger.verbose('Forcing reconnect to IMAP server'); + this.logger.verbose('Forcing reconnect to IMAP server'); try { isCurrentlyReconnecting = true; if (connection.closeBox) await connection.closeBox(false); @@ -625,7 +626,7 @@ export class EmailReadImapV2 implements INodeType { connection = await establishConnection(); await connection.openBox(mailbox); } catch (error) { - Logger.error(error as string); + this.logger.error(error as string); } finally { isCurrentlyReconnecting = false; } diff --git a/packages/nodes-base/nodes/Google/Gmail/GmailTrigger.node.ts b/packages/nodes-base/nodes/Google/Gmail/GmailTrigger.node.ts index ab9f9abb6e..ed2834d13e 100644 --- a/packages/nodes-base/nodes/Google/Gmail/GmailTrigger.node.ts +++ b/packages/nodes-base/nodes/Google/Gmail/GmailTrigger.node.ts @@ -7,7 +7,6 @@ import type { INodeType, INodeTypeDescription, } from 'n8n-workflow'; -import { LoggerProxy as Logger } from 'n8n-workflow'; import { googleApiRequest, @@ -303,7 +302,7 @@ export class GmailTrigger implements INodeType { } const workflow = this.getWorkflow(); const node = this.getNode(); - Logger.error( + this.logger.error( `There was a problem in '${node.name}' node in workflow '${workflow.id}': '${error.description}'`, { node: node.name, diff --git a/packages/nodes-base/nodes/RabbitMQ/RabbitMQTrigger.node.ts b/packages/nodes-base/nodes/RabbitMQ/RabbitMQTrigger.node.ts index 763e3c8d71..3c7541a1b4 100644 --- a/packages/nodes-base/nodes/RabbitMQ/RabbitMQTrigger.node.ts +++ b/packages/nodes-base/nodes/RabbitMQ/RabbitMQTrigger.node.ts @@ -1,6 +1,7 @@ /* eslint-disable n8n-nodes-base/node-filename-against-convention */ import type { IDataObject, + IDeferredPromise, INodeExecutionData, INodeProperties, INodeType, @@ -9,7 +10,7 @@ import type { ITriggerFunctions, ITriggerResponse, } from 'n8n-workflow'; -import { createDeferredPromise, LoggerProxy as Logger, NodeOperationError } from 'n8n-workflow'; +import { NodeOperationError } from 'n8n-workflow'; import { rabbitDefaultOptions } from './DefaultOptions'; @@ -220,9 +221,9 @@ export class RabbitMQTrigger implements INodeType { } } - let responsePromise = undefined; + let responsePromise: IDeferredPromise | undefined = undefined; if (acknowledgeMode !== 'immediately') { - responsePromise = await createDeferredPromise(); + responsePromise = await this.helpers.createDeferredPromise(); } this.emit([[item]], undefined, responsePromise); @@ -253,7 +254,7 @@ export class RabbitMQTrigger implements INodeType { messageTracker.answered(message); } - Logger.error( + this.logger.error( `There was a problem with the RabbitMQ Trigger node "${node.name}" in workflow "${workflow.id}": "${error.message}"`, { node: node.name, @@ -277,7 +278,7 @@ export class RabbitMQTrigger implements INodeType { } catch (error) { const workflow = this.getWorkflow(); const node = this.getNode(); - Logger.error( + this.logger.error( `There was a problem closing the RabbitMQ Trigger node connection "${node.name}" in workflow "${workflow.id}": "${error.message}"`, { node: node.name, diff --git a/packages/nodes-base/nodes/Salesforce/GenericFunctions.ts b/packages/nodes-base/nodes/Salesforce/GenericFunctions.ts index 7e7837fc17..86128c1c1e 100644 --- a/packages/nodes-base/nodes/Salesforce/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Salesforce/GenericFunctions.ts @@ -8,7 +8,7 @@ import type { INodePropertyOptions, JsonObject, } from 'n8n-workflow'; -import { LoggerProxy as Logger, NodeApiError } from 'n8n-workflow'; +import { NodeApiError } from 'n8n-workflow'; import moment from 'moment-timezone'; @@ -109,7 +109,7 @@ export async function salesforceApiRequest( qs, instance_url as string, ); - Logger.debug( + this.logger.debug( `Authentication for "Salesforce" node is using "jwt". Invoking URI ${options.uri}`, ); options.headers!.Authorization = `Bearer ${access_token}`; @@ -130,7 +130,7 @@ export async function salesforceApiRequest( qs, credentials.oauthTokenData.instance_url, ); - Logger.debug( + this.logger.debug( `Authentication for "Salesforce" node is using "OAuth2". Invoking URI ${options.uri}`, ); Object.assign(options, option); diff --git a/packages/nodes-base/nodes/Salesforce/Salesforce.node.ts b/packages/nodes-base/nodes/Salesforce/Salesforce.node.ts index 53d2b33c99..8015a890b1 100644 --- a/packages/nodes-base/nodes/Salesforce/Salesforce.node.ts +++ b/packages/nodes-base/nodes/Salesforce/Salesforce.node.ts @@ -8,7 +8,7 @@ import type { INodeTypeDescription, JsonObject, } from 'n8n-workflow'; -import { LoggerProxy as Logger, NodeApiError, NodeOperationError } from 'n8n-workflow'; +import { NodeApiError, NodeOperationError } from 'n8n-workflow'; import { accountFields, accountOperations } from './AccountDescription'; @@ -1065,7 +1065,7 @@ export class Salesforce implements INodeType { const resource = this.getNodeParameter('resource', 0); const operation = this.getNodeParameter('operation', 0); - Logger.debug( + this.logger.debug( `Running "Salesforce" node named "${this.getNode.name}" resource "${resource}" operation "${operation}"`, ); diff --git a/packages/workflow/src/DeferredPromise.ts b/packages/workflow/src/DeferredPromise.ts index 0f2f9fa035..80e088e1bd 100644 --- a/packages/workflow/src/DeferredPromise.ts +++ b/packages/workflow/src/DeferredPromise.ts @@ -5,7 +5,7 @@ export interface IDeferredPromise { resolve: (result: T) => void; } -export async function createDeferredPromise(): Promise> { +export async function createDeferredPromise(): Promise> { return new Promise>((resolveCreate) => { const promise = new Promise((resolve, reject) => { resolveCreate({ promise: async () => promise, resolve, reject }); diff --git a/packages/workflow/src/Interfaces.ts b/packages/workflow/src/Interfaces.ts index 1f77b08afd..b0faaa05ce 100644 --- a/packages/workflow/src/Interfaces.ts +++ b/packages/workflow/src/Interfaces.ts @@ -657,7 +657,11 @@ export interface ICredentialTestFunctions { }; } -export interface JsonHelperFunctions { +interface BaseHelperFunctions { + createDeferredPromise: () => Promise>; +} + +interface JsonHelperFunctions { returnJsonArray(jsonData: IDataObject | IDataObject[]): INodeExecutionData[]; } @@ -709,6 +713,7 @@ export interface RequestHelperFunctions { } export interface FunctionsBase { + logger: ILogger; getCredentials(type: string, itemIndex?: number): Promise; getNode(): INode; getWorkflow(): IWorkflowMetadata; @@ -749,6 +754,7 @@ export type IExecuteFunctions = ExecuteFunctions.GetNodeParameterFn & sendResponse(response: IExecuteResponsePromiseData): void; helpers: RequestHelperFunctions & + BaseHelperFunctions & BinaryHelperFunctions & FileSystemHelperFunctions & JsonHelperFunctions & { @@ -772,6 +778,7 @@ export interface IExecuteSingleFunctions extends BaseExecutionFunctions { ): NodeParameterValueType | object; helpers: RequestHelperFunctions & + BaseHelperFunctions & BinaryHelperFunctions & { assertBinaryData(propertyName: string, inputIndex?: number): IBinaryData; getBinaryDataBuffer(propertyName: string, inputIndex?: number): Promise; @@ -812,7 +819,10 @@ export interface IPollFunctions fallbackValue?: any, options?: IGetNodeParameterOptions, ): NodeParameterValueType | object; - helpers: RequestHelperFunctions & BinaryHelperFunctions & JsonHelperFunctions; + helpers: RequestHelperFunctions & + BaseHelperFunctions & + BinaryHelperFunctions & + JsonHelperFunctions; } export interface ITriggerFunctions @@ -828,7 +838,10 @@ export interface ITriggerFunctions fallbackValue?: any, options?: IGetNodeParameterOptions, ): NodeParameterValueType | object; - helpers: RequestHelperFunctions & BinaryHelperFunctions & JsonHelperFunctions; + helpers: RequestHelperFunctions & + BaseHelperFunctions & + BinaryHelperFunctions & + JsonHelperFunctions; } export interface IHookFunctions @@ -862,7 +875,10 @@ export interface IWebhookFunctions extends FunctionsBaseWithRequiredKeys<'getMod outputData: INodeExecutionData[], outputIndex?: number, ): Promise; - helpers: RequestHelperFunctions & BinaryHelperFunctions & JsonHelperFunctions; + helpers: RequestHelperFunctions & + BaseHelperFunctions & + BinaryHelperFunctions & + JsonHelperFunctions; } export interface INodeCredentialsDetails {