UNPKG

n8n-nodes-imap-ai

Version:

Simplified IMAP node for n8n with AI-agent support. Clean and modular email and mailbox management for automation workflows.

113 lines 4.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Imap = void 0; const imapflow_1 = require("imapflow"); const operationRegistry_1 = require("./operations/operationRegistry"); const ImapNodeProperties_1 = require("./ImapNodeProperties"); const ImapMethods_1 = require("./ImapMethods"); class Imap { constructor() { this.description = { displayName: 'IMAP', name: 'imap', icon: 'file:imap.svg', group: ['communication'], version: 1, subtitle: '={{$parameter["operation"]}}', description: 'Connect to IMAP servers to manage emails and mailboxes', defaults: { name: 'IMAP', }, inputs: ["main"], outputs: ["main"], usableAsTool: true, credentials: [ { name: 'imap', required: true, }, ], properties: ImapNodeProperties_1.ImapNodeProperties, }; this.methods = ImapMethods_1.ImapMethods; } async execute() { const items = this.getInputData(); const returnData = []; const credentials = await this.getCredentials('imap'); const client = new imapflow_1.ImapFlow({ host: credentials.host, port: credentials.port, secure: credentials.secure, auth: { user: credentials.user, pass: credentials.password, }, socketTimeout: 10 * 60 * 1000, connectionTimeout: 15 * 1000, greetingTimeout: 10 * 1000, }); try { await client.connect(); for (let i = 0; i < items.length; i++) { const operation = this.getNodeParameter('operation', i); try { const operationHandler = operationRegistry_1.OperationRegistry.getOperation(operation); console.log(`Executing operation: ${operation}`); const result = await operationHandler.execute(this, client, i); console.log('Operation result received:', typeof result, Array.isArray(result) ? 'Array' : 'Object'); if (Array.isArray(result)) { console.log('Processing array result, length:', result.length); returnData.push(...result); } else { console.log('Processing single result:', Object.keys(result).slice(0, 5)); returnData.push({ json: result }); } console.log('Total returnData items:', returnData.length); console.log('Final returnData structure:', returnData.map(item => ({ hasJson: !!item.json, jsonKeys: item.json ? Object.keys(item.json).slice(0, 5) : [] }))); } catch (error) { if (this.continueOnFail()) { returnData.push({ json: { error: error.message }, pairedItem: { item: i }, }); } else { throw error; } } } } finally { console.log('Starting client cleanup...'); try { const logoutPromise = client.logout(); const timeoutPromise = new Promise((_, reject) => { setTimeout(() => reject(new Error('Logout timeout')), 5000); }); await Promise.race([logoutPromise, timeoutPromise]); console.log('Client logout completed'); } catch (error) { console.log('Client logout error/timeout (forcing close):', error.message); try { await client.close(); console.log('Client force closed'); } catch (closeError) { console.log('Client force close error (ignored):', closeError.message); } } console.log('Finally block completed'); } console.log('Returning from execute method with items:', returnData.length); return [returnData]; } } exports.Imap = Imap; //# sourceMappingURL=Imap.node.js.map