From 03b7e9a9d0893a397071892bde99779f9e5bd2d3 Mon Sep 17 00:00:00 2001 From: Zack Schuster Date: Thu, 28 Jun 2018 20:44:54 -0700 Subject: [PATCH] strongly type functions --- smtp/client.js | 22 +++++++++++++--------- smtp/message.js | 28 +++++++++++----------------- smtp/response.js | 4 ++-- test/server.js | 4 +--- 4 files changed, 27 insertions(+), 31 deletions(-) diff --git a/smtp/client.js b/smtp/client.js index a199766..e89b600 100644 --- a/smtp/client.js +++ b/smtp/client.js @@ -16,11 +16,12 @@ class Client { * @property {string[]} [authentication] * * @typedef {Object} MessageStack - * @property {Function} [callback] - * @property {Message} message + * @property {function(Error, Message): void} [callback] + * @property {Message} [message] * @property {string} [returnPath] * @property {string} [from] - * @property {Array} [to] + * @property {string} [subject] + * @property {string|Array} [to] * @property {Array} [cc] * @property {Array} [bcc] * @property {string} [text] @@ -122,7 +123,7 @@ class Client { /** * @param {Message|MessageStack} msg msg - * @param {Function} callback callback + * @param {function(Error, MessageStack): void} callback callback * @returns {void} */ send(msg, callback) { @@ -145,7 +146,8 @@ class Client { msg instanceof Message ? msg : canMakeMessage(msg) ? create(msg) : null; if (message == null) { - callback(new Error('message is not a valid Message instance'), msg); + // prettier-ignore + callback(new Error('message is not a valid Message instance'), /**@type {MessageStack}*/(msg)); return; } @@ -178,7 +180,8 @@ class Client { this.queue.push(stack); this._poll(); } else { - callback(new Error(why), msg); + // prettier-ignore + callback(new Error(why), /**@type {MessageStack}*/(msg)); } }); } @@ -211,8 +214,8 @@ class Client { /** * @param {MessageStack} stack stack - * @param {Function} next next - * @returns {Function} callback + * @param {function(MessageStack): void} next next + * @returns {function(Error): void} callback */ _sendsmtp(stack, next) { /** @@ -245,7 +248,8 @@ class Client { * @returns {void} */ _sendrcpt(stack) { - const to = stack.to.shift().address; + //prettier-ignore + const to = /** @type{Array} */(stack.to).shift().address; this.smtp.rcpt( this._sendsmtp(stack, stack.to.length ? this._sendrcpt : this._senddata), '<' + to + '>' diff --git a/smtp/message.js b/smtp/message.js index 533333c..317b66c 100644 --- a/smtp/message.js +++ b/smtp/message.js @@ -208,7 +208,7 @@ class Message { } /** - * @param {Function} callback the function to call with the error and buffer + * @param {function(Error, string): void} callback the function to call with the error and buffer * @returns {void} */ read(callback) { @@ -298,7 +298,7 @@ class MessageStream extends Stream { * @param {string} boundary the boundary text between outputs * @param {MessageAttachment[]} list the list of potential messages to output * @param {number} index the index of the list item to output - * @param {Function} callback the function to call if index is greater than upper bound + * @param {function(): void} callback the function to call if index is greater than upper bound * @returns {void} */ const output_message = (boundary, list, index, callback) => { @@ -353,15 +353,9 @@ class MessageStream extends Stream { output(data.concat([CRLF]).join('')); }; - /** - * @callback NextFn - * @param {NodeJS.ErrnoException} err - * @returns {void} - */ - /** * @param {MessageAttachment} attachment the metadata to use as headers - * @param {NextFn} callback the function to call after output is finished + * @param {function(): void} callback the function to call after output is finished * @returns {void} */ const output_attachment = (attachment, callback) => { @@ -376,7 +370,7 @@ class MessageStream extends Stream { /** * @param {MessageAttachment} attachment the metadata to use as headers - * @param {Function} callback the function to call after output is finished + * @param {function(): void} callback the function to call after output is finished * @returns {void} */ const output_data = (attachment, callback) => { @@ -390,7 +384,7 @@ class MessageStream extends Stream { /** * @param {MessageAttachment} attachment the metadata to use as headers - * @param {NextFn} next the function to call when the file is closed + * @param {function(NodeJS.ErrnoException): void} next the function to call when the file is closed * @returns {void} */ const output_file = (attachment, next) => { @@ -399,7 +393,7 @@ class MessageStream extends Stream { const closed = fd => fs.closeSync(fd); /** - * @param {*} err the error to emit + * @param {Error} err the error to emit * @param {number} fd the file descriptor * @returns {void} */ @@ -449,7 +443,7 @@ class MessageStream extends Stream { /** * @param {MessageAttachment} attachment the metadata to use as headers - * @param {Function} callback the function to call after output is finished + * @param {function(): void} callback the function to call after output is finished * @returns {void} */ const output_stream = (attachment, callback) => { @@ -496,7 +490,7 @@ class MessageStream extends Stream { /** * @param {string} data the data to output as base64 - * @param {Function} [callback] the function to call after output is finished + * @param {function(): void} [callback] the function to call after output is finished * @returns {void} */ const output_base64 = (data, callback) => { @@ -533,7 +527,7 @@ class MessageStream extends Stream { /** * @param {Message} message the message to output - * @param {Function} callback the function to call after output is finished + * @param {function(): void} callback the function to call after output is finished * @returns {void} */ const output_alternative = (message, callback) => { @@ -561,7 +555,7 @@ class MessageStream extends Stream { /** * @param {MessageAttachment} message the message to output - * @param {Function} callback the function to call after output is finished + * @param {function(): void} callback the function to call after output is finished * @returns {void} */ const output_related = (message, callback) => { @@ -618,7 +612,7 @@ class MessageStream extends Stream { /** * @param {string} data the data to output - * @param {Function} [callback] the function + * @param {function(...args): void} [callback] the function * @param {*[]} [args] array of arguments to pass to the callback * @returns {void} */ diff --git a/smtp/response.js b/smtp/response.js index 33f85b9..c58a0e0 100644 --- a/smtp/response.js +++ b/smtp/response.js @@ -6,7 +6,7 @@ class SMTPResponse { * @constructor * @param {NodeJS.Socket | TLSSocket} stream the open socket to stream a response from * @param {number} timeout the time to wait (in milliseconds) before closing the socket - * @param {Function} onerror the function to call on error + * @param {function(Error): void} onerror the function to call on error */ constructor(stream, timeout, onerror) { let buffer = ''; @@ -132,7 +132,7 @@ exports.SMTPResponse = SMTPResponse; /** * @param {NodeJS.Socket | TLSSocket} stream the open socket to stream a response from * @param {number} timeout the time to wait (in milliseconds) before closing the socket - * @param {Function} onerror the function to call on error + * @param {function(Error): void} onerror the function to call on error * @returns {SMTPResponse} the smtp response */ exports.monitor = (stream, timeout, onerror) => diff --git a/test/server.js b/test/server.js index 588ba71..b8a402b 100644 --- a/test/server.js +++ b/test/server.js @@ -3,18 +3,16 @@ const assert = require('assert'); describe('Connect to wrong email server', function() { const emailModulePath = require.resolve(path.join(__dirname, '..', 'email')); - let email; beforeEach(function() { if (require.cache[emailModulePath]) { delete require.cache[emailModulePath]; } - email = require('../email'); }); it('Should not call callback multiple times with wrong server configuration', function(done) { this.timeout(5000); - const server = email.server.connect({ host: 'bar.baz' }); + const server = require(emailModulePath).server.connect({ host: 'bar.baz' }); server.send( { from: 'foo@bar.baz',