mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-21 20:00:02 +00:00
feat(Email Trigger (IMAP) Node): Migrate from imap-simple to @n8n/imap (#8899)
Co-authored-by: Jonathan Bennetts <jonathan.bennetts@gmail.com>
This commit is contained in:
committed by
GitHub
parent
28261047c3
commit
9f87cc25a0
53
packages/@n8n/imap/src/helpers/getMessage.ts
Normal file
53
packages/@n8n/imap/src/helpers/getMessage.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import {
|
||||
parseHeader,
|
||||
type ImapMessage,
|
||||
type ImapMessageBodyInfo,
|
||||
type ImapMessageAttributes,
|
||||
} from 'imap';
|
||||
import type { Message, MessageBodyPart } from '../types';
|
||||
|
||||
/**
|
||||
* Given an 'ImapMessage' from the node-imap library, retrieves the `Message`
|
||||
*/
|
||||
export async function getMessage(
|
||||
/** an ImapMessage from the node-imap library */
|
||||
message: ImapMessage,
|
||||
): Promise<Message> {
|
||||
return await new Promise((resolve) => {
|
||||
let attributes: ImapMessageAttributes;
|
||||
const parts: MessageBodyPart[] = [];
|
||||
|
||||
const messageOnBody = (stream: NodeJS.ReadableStream, info: ImapMessageBodyInfo) => {
|
||||
let body: string = '';
|
||||
|
||||
const streamOnData = (chunk: Buffer) => {
|
||||
body += chunk.toString('utf8');
|
||||
};
|
||||
|
||||
stream.on('data', streamOnData);
|
||||
stream.once('end', () => {
|
||||
stream.removeListener('data', streamOnData);
|
||||
|
||||
parts.push({
|
||||
which: info.which,
|
||||
size: info.size,
|
||||
body: /^HEADER/g.test(info.which) ? parseHeader(body) : body,
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const messageOnAttributes = (attrs: ImapMessageAttributes) => {
|
||||
attributes = attrs;
|
||||
};
|
||||
|
||||
const messageOnEnd = () => {
|
||||
message.removeListener('body', messageOnBody);
|
||||
message.removeListener('attributes', messageOnAttributes);
|
||||
resolve({ attributes, parts });
|
||||
};
|
||||
|
||||
message.on('body', messageOnBody);
|
||||
message.once('attributes', messageOnAttributes);
|
||||
message.once('end', messageOnEnd);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user