Nodejs下實現郵件的同步讀取 1

項目需求

之前兩篇文章風別對codeceptjs框架進行了基本的介紹【1】,並用codecept搭建了第一個測試框架【2】。Codeceptjs有許多優點,其中一條就是將許多異步調用包裝成了同步調用。文檔【3】中有一段描述:“ Tests are written in a synchronous way. This improves the readability and maintainability of tests. While writing tests you should not think about promises. You should focus on the test scenario.”意思是說,測試case是以同步的方式寫成的。這一方式提高了測試case的可讀性和可維護性。所以,在開發測試case的時候,你可以專注於測試scenario,而不用考慮promise。我想,作者寫這段話的意思是突出I對象的特性,而不是說在codeceptjs中完全不需要爲異步操作採取額外的步驟。例如,在我們當前的項目中,就需要到制定郵箱內讀取郵件的內容,標準的codeceptjs的I對象並沒有提供現成的方法,也就是說我們需要自己擴展codeceptjs實現這一功能。很高興的是網上已經有很多牛人提供了現成的解決方案,儘管都不能完全滿足我們的需求。站在巨人的肩膀上,我們提出了一個難稱完美卻差強人意的方案,謹在此分享,請大家斧正。

工具庫選擇imap

對於從郵箱中讀取郵件,node-imap是較爲常用的IMAP客戶端模塊【4】。和大多數nodejs核心API一樣, node-imap構建於異步事件驅動模式。在這種模式下,一種被稱爲emitter的對象會發出event,event會引起一些被稱作listener的function調用【5】。

stream.on('data', function(chunk) {
    buffer += chunk.toString('utf8');
});

類似的代碼可能會引起java程序員(比方說我)的不適,因爲這裏的一切都是異步調用。而java提供的同步順序調用【6】可能會更加適合java程序員的胃口。

    String emailHost = "email.box";
    String userEmail = "username";
    String password = "password";
    props.setProperty("mail.store.protocol", "imaps");
    props.setProperty("mail.imap.starttls.enable", "true");
    props.setProperty("mail.imap.ssl.enable", "true");

    // Get a Session object
    Session session = Session.getInstance(props, null);
    session.setDebug(true);
    Store store = session.getStore("imaps");
    store.connect(emailHost, userEmail, password);
    
    // Open the Folder
    String mailBox = "inbox";
    Folder folder = store.getDefaultFolder();
    ...
    // term for search
    String subject = "XXXX";

    SearchTerm term = new SubjectTerm(subject);

    // get msgs
    Message[] msgs = folder.search(term);
            ...

如果是javascript,要想實現同步的效果,必須將一切操作放入promise中。首先創建imap連接:

const imap = new Imap({
    user: '$user',
    password: '$password',
    port: 993,
    host: '$host',
    tls: true
});

其次,將打開imap連接的操作用promise包起來。打開連接後的操作將作爲‘ready’事件的callback函數被調用:

function connectAsync() {
    return new Promise(function (resolve, nay) {
        imap.on('ready', resolve);
        imap.connect();
    });
}

再其次,將打開mailbox的操作用promise包起來。打開mailbox後的操作將作爲打開事件的callback函數被調用:

function openBoxAsync(name, readOnly) {
    return new Promise(function (resolve, nay) {
        imap.openBox(name, readOnly, function (err, mailbox) {
            if (err) nay(err); else resolve(mailbox);
        });
    });
}

打開mailbox之後,調用imap提供的api,按照查詢條件獲得目標message集合。同樣將這一步操作放入promise:

function searchForMessages(startData) {
    return new Promise(function (resolve, nay) {
        imap.seq.search([['SINCE', startData], ['SUBJECT', '$My_subject']], function (err, result) {
            if (err) nay(err); else resolve(result);
        });
    });
}

對於按照查詢條件獲得的目標message集合中的每一封郵件,只選取我們需要的信息:

function getMailAsync(request, process) {
    return collect_events(request, 'message', 'error', 'end', process || collectEmailAsync, true);
}

這裏collect_events是一個公用函數,用於等待一系列由相同類型event觸發的並行處理的callback函數全部執行完畢,並將結果以集合的形式返回:

function collect_events(thing, good, bad, end, munch, isFetch = false) { // Collect a sequence of events, munching them as you go if you wish.
    return new Promise(function (yay, nay) {
        const ans = [];
        thing.on(good, function () {
            const args = [].slice.call(arguments);
            ans.push(munch ? munch.apply(null, args) : args);
        });
        if (bad) thing.on(bad, nay);
        thing.on(end, function () {
            Promise.all(ans).then(yay);
            if (isFetch) {
                imap.end();
            }
        });
    });
}

對於每封郵件,僅處理其body事件:

function collectEmailAsync(msg, seq) {
    return new Promise(
        function (resolve, nav) {
            const rel = collect_events(msg, 'body', 'error', 'end', collectBody)
                .then(function (x) {
                    return (x && x.length) ? x : null;
                })
            resolve(rel);
        });

}

每封郵件body事件會返回一個stream,處理其‘data’事件,並將data事件中獲得的數據拼接起來,組成字符串:

function collectBody(stream, info) {
    return new Promise(
        function (resolve, nay) {
            const body = collect_events(stream, 'data', 'error', 'end')
                .then(function (bits) {
                    return bits.map(function (c) {
                        return c.toString('utf8');
                    }).join('');
                })
            ;
            resolve(body);
        }
    );
}

最終將各個方法以promise chain的形式串起來,就能以同步的方式得到想要的結果,詳細代碼請參照github【8】:

async function f(startData = '$start_date') {
    let emailBody = await connectAsync().then(function () {
        console.log('connected');
    }).then(
        function () {
            return openBoxAsync('INBOX', true);
        }
    ).then(function () {
        return searchForMessages(startData);
    }).then(
        function (result) {
            return getMailAsync(
                imap.seq.fetch(result, {bodies: ['HEADER.FIELDS (FROM)', 'TEXT']})
                ,
                function (message) {
                    // For each e-mail:
                    return collectEmailAsync(message);
                }
            );
        }
    ).then(function (messages) {
        console.log(JSON.stringify(messages, null, 2));
        return messages[messages.length-1][1];
    })
        .catch(function (error) {
            console.error("Oops:", error.message);
            imap.end();
        })
    console.log(emailBody);
}

問題1

上面的imap能很好的同郵箱建立連接,並通過回調函數讀取符合查詢條件的郵件。但是有一個致命的問題,它是異步的,也就是說,不能直接用於codeceptjs的scenario中。做了一些調查,有一個叫做imap-promise的模塊【8】,實現了打開郵箱和讀取郵件的同步調用。但是仍然存在兩個問題。第一,該模塊是一個大而全的通用方案,能讀取郵件的所有內容,包括header,body,以及各個屬性和附件(很牛!),因此使用起來比較複雜。而第二個問題就比較致命了,它通過關閉進程的方式保障郵箱連接的關閉,難以置信!因此,需要進一步的完善。首先,簡化對郵件內容的讀取,其次,提供同步的方法關閉連接。

問題2

修改後的代碼仍然不能滿足需求,至少在兩個方面需要改進。首先是實現同步關閉連接的方法必須和讀取郵件的“配對使用”。 其次,當多次調用讀取郵件的方法時候,會引起emitter MaxListeners error。將在下一篇文章中繼續討論。

【1】淺析 codeceptjs
【2】第一個codeceptjs測試框架
【3】How it works
【4】imap
【5】events_emitter_once_eventname_listener
【6】java mail
【7】test_imap_sample.js
【8】imap-promise

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章