From 9863e78cc9e80661f03b4332754f9f1bd026b5fd Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Wed, 23 Jun 2021 19:09:03 +0200 Subject: Refactors migration and enhances tests --- app/imap.js | 30 ++++++++++++++++-------------- test/test.js | 39 ++++++++++++++++++++++++++------------- 2 files changed, 42 insertions(+), 27 deletions(-) diff --git a/app/imap.js b/app/imap.js index f2e8cc2..181d9cf 100644 --- a/app/imap.js +++ b/app/imap.js @@ -20,6 +20,16 @@ async function connect (options) { return client; } +ImapFlow.prototype.fetchArray = async function (range, query, options = {}) { + const msgsGenerator = await this.fetch(range, query, options); + const msgs = []; + for await (const msg of msgsGenerator) { + msgs.push(msg); + } + + return msgs; +}; + async function listTreeFrom (event, options) { const client = await connect(options); @@ -47,26 +57,18 @@ async function migrate (event, { from, to }) { const fromMailbox = await fromClient.getMailboxLock(fromFolder.path); const toMailbox = await toClient.getMailboxLock(fromFolder.path); try { - const toMsgsGenerator = await toClient.fetch('1:*', { flags: true, envelope: true, source: true }); - const toMsgs = []; - for await (const toMsg of toMsgsGenerator) { - toMsgs.push(toMsg); - } - const fromMsgsGenerator = await fromClient.fetch('1:*', { flags: true, envelope: true, source: true }); + const toMsgs = await toClient.fetchArray('1:*', { flags: true, envelope: true, source: true }); + const fromMsgs = await fromClient.fetchArray('1:*', { flags: true, envelope: true, source: true }); - for await (const fromMsg of fromMsgsGenerator) { - if (toMsgs.some((toMsg) => Buffer.compare(toMsg.source, fromMsg.source) === 0)) { - continue; - } + const msgs = fromMsgs.filter((fromMsg) => !toMsgs.some((toMsg) => Buffer.compare(toMsg.source, fromMsg.source) === 0)); - await toClient.append(fromFolder.path, fromMsg.source, Array.from(fromMsg.flags), fromMsg.envelope.date); + await toClient.noop(); + for (const msg of msgs) { + toClient.append(fromFolder.path, msg.source, Array.from(msg.flags), msg.envelope.date); } } finally { fromMailbox.release(); toMailbox.release(); } } - - await fromClient.logout(); - await toClient.logout(); } diff --git a/test/test.js b/test/test.js index dad4b9f..eab0ca0 100644 --- a/test/test.js +++ b/test/test.js @@ -1,6 +1,6 @@ const { ImapFlow } = require('imapflow'); -const clientFrom = new ImapFlow({ +const fromClient = new ImapFlow({ host: 'localhost', port: 3143, auth: { @@ -9,7 +9,7 @@ const clientFrom = new ImapFlow({ }, }); -const clientTo = new ImapFlow({ +const toClient = new ImapFlow({ host: 'localhost', port: 31432, auth: { @@ -19,12 +19,13 @@ const clientTo = new ImapFlow({ }); (async () => { - await clientFrom.connect(); - await clientTo.connect(); + await fromClient.connect(); + await toClient.connect(); const fromMailboxes = [ 'From Test Parent.From Test Child', 'From Test Parent.From Test Child 2.From Test Child Inner', + '10000 Mails', ]; const toMailboxes = [ 'To Test Parent.To Test Child', @@ -32,51 +33,63 @@ const clientTo = new ImapFlow({ ]; for (const mailbox of fromMailboxes) { try { - await clientFrom.mailboxDelete(mailbox); + await fromClient.mailboxDelete(mailbox); } catch (e) {} try { - await clientTo.mailboxDelete(mailbox); + await toClient.mailboxDelete(mailbox); } catch (e) {} try { - await clientFrom.mailboxCreate(mailbox); + await fromClient.mailboxCreate(mailbox); } catch (e) {} } for (const mailbox of toMailboxes) { try { - await clientTo.mailboxDelete(mailbox); + await toClient.mailboxDelete(mailbox); } catch (e) {} try { - await clientTo.mailboxCreate(mailbox); + await toClient.mailboxCreate(mailbox); } catch (e) {} } - await clientFrom.append('INBOX', + fromClient.append('INBOX', 'From: test@example.org\r\n' + 'To: from@example.org\r\n' + 'Subject: FROM INBOX MESSAGE SUBJECT\r\n' + '\r\n' + 'FROM INBOX MESSAGE BODY', [], new Date()); - await clientFrom.append('From Test Parent.From Test Child', + fromClient.append('From Test Parent.From Test Child', 'From: test@example.org\r\n' + 'To: from@example.org\r\n' + 'Subject: FROM TEST CHILD MESSAGE FLAGGED SUBJECT\r\n' + '\r\n' + 'FROM TEST CHILD MESSAGE FLAGGED BODY', ['\\FLAGGED'], new Date()); - await clientFrom.append('From Test Parent.From Test Child', + fromClient.append('From Test Parent.From Test Child', 'From: test@example.org\r\n' + 'To: from@example.org\r\n' + 'Subject: FROM TEST CHILD MESSAGE DRAFT SUBJECT\r\n' + '\r\n' + 'FROM TEST CHILD MESSAGE DRAFT BODY', ['\\DRAFT'], new Date()); - await clientFrom.append('From Test Parent.From Test Child 2.From Test Child Inner', + fromClient.append('From Test Parent.From Test Child 2.From Test Child Inner', 'From: test@example.org\r\n' + 'To: from@example.org\r\n' + 'Subject: FROM TEST CHILD 2 INNER MESSAGE SEEN SUBJECT\r\n' + '\r\n' + 'FROM TEST CHILD 2 INNER MESSAGE SEEN BODY', ['\\SEEN'], new Date()); + + for (let i = 0; i < 10000; i++) { + const flag = ['\\SEEN', '\\FLAGGED', ''][Math.floor(Math.random() * 3)]; + const now = new Date(); + + fromClient.append('10000 Mails', + 'From: test@example.org\r\n' + + 'To: from@example.org\r\n' + + `Subject: ${i}. MAIL\r\n` + + '\r\n' + + `MANY MAILS NUMBER ${i}`, [flag], new Date(now.getTime() - i * 60000)); + } })(); -- cgit v1.2.3