-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·69 lines (55 loc) · 1.84 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env node
const express = require("express");
const { simpleParser } = require("mailparser");
const { SMTPServer } = require("smtp-server");
const templates = require("./templates");
let mailbox = {};
const PORT = process.env.DEV_SMTP_PORT || 2500;
const API_PORT = process.env.DEV_SMTP_API_PORT || 2501;
const app = express();
const filterByTo = (to, mailbox) =>
Object.entries(mailbox)
.filter(([k, v]) => v.to.value.filter((v) => v.address === to).length > 0)
.map(([k, v]) => ({ id: k, ...v }));
const smtp = new SMTPServer({
disabledCommands: ["STARTTLS"],
onAuth(auth, session, callback) {
// if (auth.username !== "username" || auth.password !== "password")
// return callback(new Error("Invalid username or password"));
callback(null, { user: "dummyUser" }); // using a dummy user to simulate auth success
},
logger: true,
onData(stream, session, callback) {
let result = "";
stream.on("data", (chunk) => (result += chunk));
stream.on("end", () => {
simpleParser(result, {}, (err, parsed) => {
mailbox[parsed.messageId] = parsed;
callback();
});
});
},
});
app.get("/", (req, res) => {
const messages = Object.values(mailbox).reverse();
res.send(templates.home({ messages }));
});
app.get("/to/:to", (req, res) => {
console.log(JSON.stringify(mailbox, null, 2));
const { to } = req.params;
res.json(filterByTo(to, mailbox).reverse());
});
app.get("/:messageId", (req, res) => {
res.json(mailbox[req.params.messageId]);
});
app.get("/:messageId/:field", (req, res) => {
const { field, messageId } = req.params;
res.send(mailbox[messageId][field]);
});
smtp.on("error", (err) => {
console.error("Error %s", err.message);
});
smtp.listen(PORT);
app.listen(API_PORT, () => {
console.log(`[SMTP-devServer] api listening on http://localhost:${API_PORT}`);
});