better fetch() argument detection

fork
mscdex 11 years ago
parent 23463b1e8f
commit 034bd33cf3

@ -930,7 +930,8 @@ ImapConnection.prototype.fetch = function(uids, options, what, cb) {
};
ImapConnection.prototype._fetch = function(which, uids, options, what, cb) {
if (uids === undefined || uids === null
if (uids === undefined
|| uids === null
|| (Array.isArray(uids) && uids.length === 0))
throw new Error('Nothing to fetch');
@ -939,19 +940,53 @@ ImapConnection.prototype._fetch = function(which, uids, options, what, cb) {
utils.validateUIDList(uids);
var toFetch = '', prefix = ' BODY[', extensions, self = this,
parse = true, headers, key, stream,
parse, headers, key, stream,
fetchers = {};
if (typeof what === 'function') {
cb = what;
what = options;
options = {};
// argument detection!
if (cb === undefined) {
// fetch(uids, xxxx, yyyy)
if (what === undefined) {
// fetch(uids, xxxx)
if (options === undefined) {
// fetch(uids)
what = options = {};
} else if (typeof options === 'function') {
// fetch(uids, callback)
cb = options;
what = options = {};
} else if (options.struct !== undefined
|| options.size !== undefined
|| options.markSeen !== undefined) {
// fetch(uids, options)
what = {};
} else {
// fetch(uids, what)
what = options;
options = {};
}
} else if (typeof what === 'function') {
// fetch(uids, xxxx, callback)
cb = what;
if (options.struct !== undefined
|| options.size !== undefined
|| options.markSeen !== undefined) {
// fetch(uids, options, callback)
what = {};
} else {
// fetch(uids, what, callback)
what = options;
options = {};
}
}
}
if (!Array.isArray(what))
what = [what];
for (var i = 0, wp, pprefix, len = what.length; i < len; ++i) {
wp = what[i];
parse = true;
if (wp.id !== undefined && !/^(?:[\d]+[\.]{0,1})*[\d]+$/.test(''+wp.id))
throw new Error('Invalid part id: ' + wp.id);
if (( (wp.headers
@ -1153,7 +1188,7 @@ ImapConnection.prototype._fetch = function(which, uids, options, what, cb) {
for (f = 0, lenf = fetches.length; f < lenf; ++f)
fetches[f].emit('end');
}
cb(err);
cb&&cb(err);
});
this._state.requests[this._state.requests.length - 1].fetchers = fetchers;

Loading…
Cancel
Save