@timshel_npm/maildev
Version:
SMTP Server with async API and Web Interface for viewing and testing emails during development
97 lines (96 loc) • 4.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Web = void 0;
const routes_1 = require("./routes");
const express = require("express");
const cors = require("cors");
const fs = require("fs");
const http = require("http");
const https = require("https");
const socketio = require("socket.io");
const auth = require("./auth");
const logger = require("./logger");
const path = require("path");
class Web {
constructor(mailserver, options) {
var _a, _b, _c, _d, _e, _f;
/**
* Keep record of all connections to close them on shutdown
*/
this.connections = {};
const app = express();
this.protocol = (options === null || options === void 0 ? void 0 : options.ssl) ? "https" : "http";
this.port = (_a = options === null || options === void 0 ? void 0 : options.port) !== null && _a !== void 0 ? _a : 1080;
this.host = (_b = options === null || options === void 0 ? void 0 : options.host) !== null && _b !== void 0 ? _b : "0.0.0.0";
this.basePathname = (_c = options === null || options === void 0 ? void 0 : options.basePathname) !== null && _c !== void 0 ? _c : "/";
this.server = (options === null || options === void 0 ? void 0 : options.ssl)
? https.createServer({
key: fs.readFileSync(options === null || options === void 0 ? void 0 : options.ssl.key),
cert: fs.readFileSync(options === null || options === void 0 ? void 0 : options.ssl.cert),
}, app)
: http.createServer(app);
if (options === null || options === void 0 ? void 0 : options.auth) {
app.use(auth((_d = options === null || options === void 0 ? void 0 : options.auth) === null || _d === void 0 ? void 0 : _d.user, (_e = options === null || options === void 0 ? void 0 : options.auth) === null || _e === void 0 ? void 0 : _e.pass));
}
this.io = socketio({
path: path.posix.join(this.basePathname, "/socket.io"),
cors: {
origin: (_f = options === null || options === void 0 ? void 0 : options.domain) !== null && _f !== void 0 ? _f : "*",
methods: ["GET", "POST"],
},
});
app.use(this.basePathname, express.static(path.join(__dirname, "../app")));
app.use(cors());
(0, routes_1.routes)(app, mailserver, this.basePathname);
this.io.attach(this.server);
this.io.on("connection", webSocketConnection(mailserver));
}
listen() {
const self = this;
this.server.on("connection", (socket) => {
const key = `${socket.remoteAddress}:${socket.remotePort}`;
self.connections[key] = socket;
socket.on("close", function () {
delete self.connections[key];
});
});
return new Promise((resolve, reject) => {
self.server.listen(self.port, self.host, () => {
logger.info(`MailDev webapp running at ${self.protocol}://${self.host}:${self.port}${self.basePathname}`);
resolve();
});
});
}
close() {
const self = this;
closeConnections(this.connections);
return new Promise((resolve, reject) => {
self.io.close(resolve);
});
}
}
exports.Web = Web;
function closeConnections(connections) {
for (const key in connections) {
connections[key].destroy();
}
}
/**
* WebSockets
*/
function webSocketConnection(mailserver) {
return function onConnection(socket) {
const newHandler = (mail) => {
socket.emit("newMail", mail);
};
const deleteHandler = (mail) => {
socket.emit("deleteMail", mail);
};
mailserver.on("new", newHandler);
mailserver.on("delete", deleteHandler);
socket.on("disconnect", () => {
mailserver.removeListener("new", newHandler);
mailserver.removeListener("delete", deleteHandler);
});
};
}