Skip to content

Commit

Permalink
cache nodemailer transporters
Browse files Browse the repository at this point in the history
  • Loading branch information
prostgles committed Nov 27, 2024
1 parent 3f1cf3a commit 5f91a95
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
8 changes: 6 additions & 2 deletions lib/Auth/sendEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ const transporterCache: Map<string, Transporter> = new Map();
* https://www.nodemailer.com/transports/ses/
*/
export const sendEmail = (smptConfig: SMTPConfig, email: Email) => {
const transporter = getOrSetTransporter(smptConfig);
return send(transporter, email);
}

export const getOrSetTransporter = (smptConfig: SMTPConfig) => {
const configStr = JSON.stringify(smptConfig);
const transporter = transporterCache.get(configStr) ?? getTransporter(smptConfig);
if(!transporterCache.has(configStr)){
transporterCache.set(configStr, transporter);
}

return send(transporter, email);
return transporter;
}

const getTransporter = (smptConfig: SMTPConfig) => {
Expand Down
18 changes: 17 additions & 1 deletion lib/Auth/setEmailProvider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import e from "express";
import { AUTH_ROUTES_AND_PARAMS, AuthHandler, HTTPCODES } from "./AuthHandler";
import { Email, SMTPConfig } from "./AuthTypes";
import { sendEmail } from "./sendEmail";
import { getOrSetTransporter, sendEmail } from "./sendEmail";
import { promises } from "node:dns";

export async function setEmailProvider(this: AuthHandler, app: e.Express) {
Expand All @@ -12,6 +12,14 @@ export async function setEmailProvider(this: AuthHandler, app: e.Express) {
await checkDmarc(websiteUrl);
}

if(email.signupType === "withPassword"){
if(email.emailConfirmation){
tryGetTransporter(email.emailConfirmation.smtp);
}
} else {
tryGetTransporter(email.emailMagicLink.smtp);
}

app.post(AUTH_ROUTES_AND_PARAMS.emailSignup, async (req, res) => {
const { username, password } = req.body;
let validationError = "";
Expand Down Expand Up @@ -82,4 +90,12 @@ const checkDmarc = async (websiteUrl: string) => {
} else {
console.log("DMARC set to reject")
}
}

const tryGetTransporter = (smtp: SMTPConfig) => {
try {
getOrSetTransporter(smtp);
} catch(err) {
console.error("Failed to set email transporter", err);
}
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prostgles-server",
"version": "4.2.161",
"version": "4.2.162",
"description": "",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion tests/server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5f91a95

Please sign in to comment.