UNPKG

@timshel_npm/maildev

Version:

SMTP Server with async API and Web Interface for viewing and testing emails during development

157 lines (156 loc) 8.98 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.appendOptions = appendOptions; exports.cliOptions = cliOptions; const program = require("commander").program; const pkg = require("../../package.json"); const fs = require("fs"); const options = [ // General config ["-v, --verbose"], ["--silent"], ["--log-mail-contents", "Log a JSON representation of each incoming email"], // SMTP server parameters ["-s, --smtp <port>", "MAILDEV_SMTP_PORT", "SMTP port to catch emails", 1025, (x) => parseInt(x)], ["--ip <ip address>", "MAILDEV_IP", "IP Address to bind SMTP service to", "0.0.0.0"], ["--mail-directory <path>", "MAILDEV_MAIL_DIRECTORY", "Directory for persisting mails"], ["--incoming-user <user>", "MAILDEV_INCOMING_USER", "SMTP user for incoming emails"], ["--incoming-pass <pass>", "MAILDEV_INCOMING_PASS", "SMTP password for incoming emails"], ["--incoming-secure", "MAILDEV_INCOMING_SECURE", "Use SMTP SSL for incoming emails", false], ["--incoming-cert <path>", "MAILDEV_INCOMING_CERT", "Cert file location for incoming SSL"], ["--incoming-key <path>", "MAILDEV_INCOMING_KEY", "Key file location for incoming SSL"], [ "--hide-extensions <extensions>", "MAILDEV_HIDE_EXTENSIONS", "Comma separated list of SMTP extensions to NOT advertise (SMTPUTF8, PIPELINING, 8BITMIME)", [], (val) => val.split(","), ], // Outgoing parameters [ "--auto-relay [email]", "MAILDEV_AUTO_RELAY", "Use auto-relay mode. Optional relay email address", ], ["--outgoing-host <host>", "MAILDEV_OUTGOING_HOST", "SMTP host for outgoing emails"], [ "--outgoing-port <port>", "MAILDEV_OUTGOING_PORT", "SMTP port for outgoing emails", (x) => parseInt(x), ], ["--outgoing-user <user>", "MAILDEV_OUTGOING_USER", "SMTP user for outgoing emails"], ["--outgoing-pass <password>", "MAILDEV_OUTGOING_PASS", "SMTP password for outgoing emails"], ["--outgoing-secure", "MAILDEV_OUTGOING_SECURE", "Use SMTP SSL for outgoing emails", false], ["--auto-relay-rules <file>", "MAILDEV_AUTO_RELAY_RULES", "Filter rules for auto relay mode"], // Web app config [ "--disable-web", "MAILDEV_DISABLE_WEB", "Disable the use of the web interface. Useful for unit testing", false, ], ["-w, --web <port>", "MAILDEV_WEB_PORT", "Port to run the Web GUI", 1080, (x) => parseInt(x)], [ "--web-ip <ip address>", "MAILDEV_WEB_IP", "IP Address to bind HTTP service to, defaults to --ip", ], ["--web-user <user>", "MAILDEV_WEB_USER", "HTTP user for GUI"], ["--web-pass <password>", "MAILDEV_WEB_PASS", "HTTP password for GUI"], [ "--web-domain <path>", "MAILDEV_WEB_DOMAIN", 'External domain name (used for socket CORS, "*" otherwise)', ], ["--base-pathname <path>", "MAILDEV_BASE_PATHNAME", "Base path for URLs"], ["--https", "MAILDEV_HTTPS", "Switch from http to https protocol", false], ["--https-key <file>", "MAILDEV_HTTPS_KEY", "The file path to the ssl private key"], ["--https-cert <file>", "MAILDEV_HTTPS_CERT", "The file path to the ssl cert file"], ]; function appendOptions(program, options) { return options.reduce(function (chain, option) { var _a; const flag = option[0]; const envVariable = typeof option[1] === "string" ? option[1] : undefined; const description = typeof option[2] === "string" ? option[2] : undefined; const defaultValue = envVariable ? ((_a = process.env[envVariable]) !== null && _a !== void 0 ? _a : option[3]) : option[3]; const fn = option[4]; return fn ? chain.option(flag, description, fn, defaultValue) : chain.option(flag, description, defaultValue); }, program); } function cliOptions() { var _a, _b, _c; const config = appendOptions(program.version(pkg.version).allowUnknownOption(true), options) .parse(process.argv) .opts(); let web; if (!(config === null || config === void 0 ? void 0 : config.disableWeb)) { var secure; if (config === null || config === void 0 ? void 0 : config.https) { if (!(config === null || config === void 0 ? void 0 : config.httpsKey) || fs.existsSync(config === null || config === void 0 ? void 0 : config.httpsKey) === false) { throw new Error("Unable to find https secure key. Please specify key file via -https-key argument"); } if (!(config === null || config === void 0 ? void 0 : config.httpsCert) || fs.existsSync(config === null || config === void 0 ? void 0 : config.httpsCert) === false) { throw new Error("Unable to find https secure cert. Please specify cert file via -https-cert argument"); } secure = { cert: config === null || config === void 0 ? void 0 : config.httpsCert, key: config === null || config === void 0 ? void 0 : config.httpsKey, }; } web = { disabled: false, port: config === null || config === void 0 ? void 0 : config.web, host: config === null || config === void 0 ? void 0 : config.webIp, domain: config === null || config === void 0 ? void 0 : config.webDomain, basePathname: config === null || config === void 0 ? void 0 : config.basePathname, auth: (config === null || config === void 0 ? void 0 : config.webUser) && (config === null || config === void 0 ? void 0 : config.webPass) ? { user: config === null || config === void 0 ? void 0 : config.webUser, pass: config === null || config === void 0 ? void 0 : config.webPass } : undefined, ssl: secure, }; } else { web = { disabled: true }; } return { verbose: config === null || config === void 0 ? void 0 : config.verbose, silent: config === null || config === void 0 ? void 0 : config.silent, logMailContents: config === null || config === void 0 ? void 0 : config.logMailContents, port: config === null || config === void 0 ? void 0 : config.smtp, host: config === null || config === void 0 ? void 0 : config.ip, mailDir: config === null || config === void 0 ? void 0 : config.mailDirectory, auth: (config === null || config === void 0 ? void 0 : config.incomingUser) && (config === null || config === void 0 ? void 0 : config.incomingPass) ? { user: config === null || config === void 0 ? void 0 : config.incomingUser, pass: config === null || config === void 0 ? void 0 : config.incomingPass, } : undefined, isSecure: config === null || config === void 0 ? void 0 : config.incomingSecure, ssl: (config === null || config === void 0 ? void 0 : config.incomingCert) && (config === null || config === void 0 ? void 0 : config.incomingKey) ? { certPath: config === null || config === void 0 ? void 0 : config.incomingCert, keyPath: config === null || config === void 0 ? void 0 : config.incomingKey, } : undefined, hide8BITMIME: ((_a = config === null || config === void 0 ? void 0 : config.hideExtensions) !== null && _a !== void 0 ? _a : []).includes("8BITMIME"), hidePIPELINING: ((_b = config === null || config === void 0 ? void 0 : config.hideExtensions) !== null && _b !== void 0 ? _b : []).includes("PIPELINING"), hideSMTPUTF8: ((_c = config === null || config === void 0 ? void 0 : config.hideExtensions) !== null && _c !== void 0 ? _c : []).includes("SMTPUTF8"), outgoing: (config === null || config === void 0 ? void 0 : config.autoRelay) || (config === null || config === void 0 ? void 0 : config.autoRelayRules) || (config === null || config === void 0 ? void 0 : config.outgoingHost) ? { host: config === null || config === void 0 ? void 0 : config.outgoingHost, port: config === null || config === void 0 ? void 0 : config.outgoingPort, secure: config === null || config === void 0 ? void 0 : config.outgoingSecure, auth: (config === null || config === void 0 ? void 0 : config.outgoingUser) && (config === null || config === void 0 ? void 0 : config.outgoingPass) ? { user: config === null || config === void 0 ? void 0 : config.outgoingUser, pass: config === null || config === void 0 ? void 0 : config.outgoingPass } : undefined, autoRelayAddress: config === null || config === void 0 ? void 0 : config.autoRelay, autoRelayRules: config === null || config === void 0 ? void 0 : config.autoRelayRules, } : undefined, web, }; }