emailjs-imap-client
Advanced tools
Comparing version 2.0.3 to 2.0.5
{ | ||
"name": "emailjs-imap-client", | ||
"version": "2.0.3", | ||
"version": "2.0.5", | ||
"homepage": "https://github.com/emailjs/emailjs-imap-client", | ||
@@ -5,0 +5,0 @@ "description": "JavaScript IMAP client", |
@@ -157,5 +157,12 @@ (function(root, factory) { | ||
*/ | ||
Imap.prototype.close = function() { | ||
Imap.prototype.close = function(error) { | ||
return new Promise((resolve) => { | ||
var tearDown = () => { | ||
// fulfill pending promises | ||
this._clientQueue.forEach(cmd => cmd.callback(error)); | ||
if (this._currentCommand) { | ||
this._currentCommand.callback(error); | ||
} | ||
this._clientQueue = []; | ||
@@ -207,3 +214,3 @@ this._currentCommand = false; | ||
this.socket.onclose = this.socket.onerror = () => { | ||
this.close().then(resolve).catch(reject); | ||
this.close("Client logging out").then(resolve).catch(reject); | ||
}; | ||
@@ -293,2 +300,30 @@ | ||
/** | ||
* | ||
* @param commands | ||
* @param ctx | ||
* @returns {*} | ||
*/ | ||
Imap.prototype.getPreviouslyQueued = function(commands, ctx) { | ||
const startIndex = this._clientQueue.indexOf(ctx) - 1; | ||
// search backwards for the commands and return the first found | ||
for (let i = startIndex; i >= 0; i--) { | ||
if (isMatch(this._clientQueue[i])) { | ||
return this._clientQueue[i]; | ||
} | ||
} | ||
// also check current command if no SELECT is queued | ||
if (isMatch(this._currentCommand)) { | ||
return this._currentCommand; | ||
} | ||
return false; | ||
function isMatch(data) { | ||
return data && data.request && commands.indexOf(data.request.command) >= 0; | ||
} | ||
}; | ||
/** | ||
* Send data to the TCP socket | ||
@@ -346,3 +381,3 @@ * Arms a timeout waiting for a response from the server. | ||
// always call onerror callback, no matter if close() succeeds or fails | ||
this.close().then(() => { | ||
this.close(error).then(() => { | ||
this.onerror && this.onerror(error); | ||
@@ -482,4 +517,2 @@ }, () => { | ||
this._globalAcceptUntagged[command](response); | ||
this._canSend = true; | ||
this._sendRequest(); | ||
} else if (response.tag === this._currentCommand.tag) { | ||
@@ -486,0 +519,0 @@ // tagged response |
@@ -49,3 +49,5 @@ 'use strict'; | ||
"Starred": { "special-use": "\\Flagged" }, | ||
"Trash": { "special-use": "\\Trash" } | ||
"Trash": { "special-use": "\\Trash" }, | ||
"A": { messages: [{}] }, | ||
"B": { messages: [{}] } | ||
} | ||
@@ -85,3 +87,3 @@ } | ||
it('should use STARTTLS by default', () => { | ||
it('should use STARTTLS by default', (done) => { | ||
imap = new ImapClient('127.0.0.1', port, { | ||
@@ -96,10 +98,10 @@ auth: { | ||
return imap.connect().then(() => { | ||
imap.connect().then(() => { | ||
expect(imap.client.secureMode).to.be.true; | ||
}).then(() => { | ||
return imap.close(); | ||
}); | ||
}).then(() => done()).catch(done); | ||
}); | ||
it('should ignore STARTTLS', () => { | ||
it('should ignore STARTTLS', (done) => { | ||
imap = new ImapClient('127.0.0.1', port, { | ||
@@ -119,3 +121,3 @@ auth: { | ||
return imap.close(); | ||
}); | ||
}).then(() => done()).catch(done); | ||
}); | ||
@@ -397,2 +399,34 @@ | ||
}); | ||
it('should select correct mailboxes in prechecks on concurrent calls', (done) => { | ||
imap.selectMailbox('[Gmail]/A').then(() => { | ||
return Promise.all([ | ||
imap.selectMailbox('[Gmail]/B'), | ||
imap.setFlags('[Gmail]/A', '1', ['\\Seen']) | ||
]); | ||
}).then(() => { | ||
return imap.listMessages('[Gmail]/A', '1:1', ['flags']); | ||
}).then((messages) => { | ||
expect(messages.length).to.equal(1); | ||
expect(messages[0].flags).to.deep.equal(['\\Seen']); | ||
done(); | ||
}).catch(done); | ||
}); | ||
it('should send precheck commands in correct order on concurrent calls', (done) => { | ||
Promise.all([ | ||
imap.setFlags('[Gmail]/A', '1', ['\\Seen']), | ||
imap.setFlags('[Gmail]/B', '1', ['\\Seen']) | ||
]).then(() => { | ||
return imap.listMessages('[Gmail]/A', '1:1', ['flags']); | ||
}).then((messages) => { | ||
expect(messages.length).to.equal(1); | ||
expect(messages[0].flags).to.deep.equal(['\\Seen']); | ||
}).then(() => { | ||
return imap.listMessages('[Gmail]/B', '1:1', ['flags']); | ||
}).then((messages) => { | ||
expect(messages.length).to.equal(1); | ||
expect(messages[0].flags).to.deep.equal(['\\Seen']); | ||
}).then(done).catch(done); | ||
}); | ||
}); | ||
@@ -414,11 +448,12 @@ }); | ||
return imap.connect(); | ||
return imap.connect() | ||
.then(() => { | ||
// remove the ondata event to simulate 100% packet loss and make the socket time out after 10ms | ||
imap.client.TIMEOUT_SOCKET_LOWER_BOUND = 10; | ||
imap.client.TIMEOUT_SOCKET_MULTIPLIER = 0; | ||
imap.client.socket.ondata = () => {}; | ||
}); | ||
}); | ||
it('should timeout', (done) => { | ||
// remove the ondata event to simulate 100% packet loss and make the socket time out after 10ms | ||
imap.client.TIMEOUT_SOCKET_LOWER_BOUND = 10; | ||
imap.client.TIMEOUT_SOCKET_MULTIPLIER = 0; | ||
imap.client.socket.ondata = () => {}; | ||
imap.onerror = () => { | ||
@@ -430,4 +465,26 @@ done(); | ||
}); | ||
it('should reject all pending commands on timeout', (done) => { | ||
let rejectionCount = 0; | ||
Promise.all([ | ||
imap.selectMailbox("INBOX") | ||
.catch(err => { | ||
expect(err).to.exist; | ||
rejectionCount++; | ||
}), | ||
imap.listMessages("INBOX", "1:*", ['body.peek[]']) | ||
.catch(err => { | ||
expect(err).to.exist; | ||
rejectionCount++; | ||
}), | ||
]).then(() => { | ||
expect(rejectionCount).to.equal(2); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
})); |
@@ -242,5 +242,5 @@ 'use strict'; | ||
sinon.stub(client, '_processResponse'); | ||
sinon.stub(client, '_sendRequest'); | ||
client._globalAcceptUntagged.TEST = () => {}; | ||
sinon.stub(client._globalAcceptUntagged, 'TEST'); | ||
sinon.stub(client, '_sendRequest'); | ||
@@ -255,3 +255,3 @@ client._currentCommand = { | ||
expect(client._sendRequest.callCount).to.equal(1); | ||
expect(client._sendRequest.callCount).to.equal(0); | ||
expect(client._globalAcceptUntagged.TEST.withArgs({ | ||
@@ -550,3 +550,97 @@ tag: '*', | ||
}); | ||
describe('#getPreviouslyQueued', () => { | ||
const ctx = {}; | ||
it('should return undefined with empty queue and no current command', () => { | ||
client._currentCommand = undefined; | ||
client._clientQueue = []; | ||
expect(testAndGetAttribute()).to.be.undefined; | ||
}); | ||
it('should return undefined with empty queue and non-SELECT current command', () => { | ||
client._currentCommand = createCommand('TEST'); | ||
client._clientQueue = []; | ||
expect(testAndGetAttribute()).to.be.undefined; | ||
}); | ||
it('should return current command with empty queue and SELECT current command', () => { | ||
client._currentCommand = createCommand('SELECT', 'ATTR'); | ||
client._clientQueue = []; | ||
expect(testAndGetAttribute()).to.equal('ATTR'); | ||
}); | ||
it('should return current command with non-SELECT commands in queue and SELECT current command', () => { | ||
client._currentCommand = createCommand('SELECT', 'ATTR'); | ||
client._clientQueue = [ | ||
createCommand('TEST01'), | ||
createCommand('TEST02') | ||
]; | ||
expect(testAndGetAttribute()).to.equal('ATTR'); | ||
}); | ||
it('should return last SELECT before ctx with multiple SELECT commands in queue (1)', () => { | ||
client._currentCommand = createCommand('SELECT', 'ATTR01'); | ||
client._clientQueue = [ | ||
createCommand('SELECT', 'ATTR'), | ||
createCommand('TEST'), | ||
ctx, | ||
createCommand('SELECT', 'ATTR03') | ||
]; | ||
expect(testAndGetAttribute()).to.equal('ATTR'); | ||
}); | ||
it('should return last SELECT before ctx with multiple SELECT commands in queue (2)', () => { | ||
client._clientQueue = [ | ||
createCommand('SELECT', 'ATTR02'), | ||
createCommand('SELECT', 'ATTR'), | ||
ctx, | ||
createCommand('SELECT', 'ATTR03') | ||
]; | ||
expect(testAndGetAttribute()).to.equal('ATTR'); | ||
}); | ||
it('should return last SELECT before ctx with multiple SELECT commands in queue (3)', () => { | ||
client._clientQueue = [ | ||
createCommand('SELECT', 'ATTR02'), | ||
createCommand('SELECT', 'ATTR'), | ||
createCommand('TEST'), | ||
ctx, | ||
createCommand('SELECT', 'ATTR03') | ||
]; | ||
expect(testAndGetAttribute()).to.equal('ATTR'); | ||
}); | ||
function testAndGetAttribute() { | ||
const data = client.getPreviouslyQueued(['SELECT'], ctx); | ||
if (data) { | ||
return data.request.attributes[0].value; | ||
} | ||
} | ||
function createCommand(command, attribute) { | ||
const attributes = []; | ||
const data = { | ||
request: { command, attributes } | ||
}; | ||
if (attribute) { | ||
data.request.attributes.push({ | ||
type: 'STRING', | ||
value: attribute | ||
}); | ||
} | ||
return data; | ||
} | ||
}); | ||
}); | ||
})); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
560467
13962
1