Skip to content

Commit

Permalink
fix: throw error when the URI is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
pr-Mais committed Nov 24, 2023
1 parent 94c00f7 commit fb2db19
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
41 changes: 23 additions & 18 deletions firestore-send-email/functions/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { createTransport } from "nodemailer";
import { createTransport, Transporter } from "nodemailer";
import { URL } from "url";
import { invalidURI } from "./logs";
import { Config } from "./types";
import { logger } from "firebase-functions/v1";

function compileUrl($: string): URL | null {
try {
Expand All @@ -17,9 +18,23 @@ function checkMicrosoftServer($: string): boolean {
);
}

export const setSmtpCredentials = (config: Config) => {
export function parseTlsOptions(tlsOptions: string) {
let tls = { rejectUnauthorized: false };

try {
tls = JSON.parse(tlsOptions);
} catch (ex) {
logger.warn(
"Invalid TLS options provided, using default TLS options instead: `{ rejectUnauthorized: false }`"
);
}

return tls;
}

export function setSmtpCredentials(config: Config): Transporter {
let url: URL;
let transport;
let transport: Transporter;

const { smtpConnectionUri, smtpPassword } = config;

Expand All @@ -28,9 +43,10 @@ export const setSmtpCredentials = (config: Config) => {

/** return null if invalid url */
if (!url) {
invalidURI(smtpConnectionUri);

return null;
invalidURI();
throw new Error(
`Invalid URI: please reconfigure with a valid SMTP connection URI`
);
}

/** encode uri password if exists */
Expand Down Expand Up @@ -59,15 +75,4 @@ export const setSmtpCredentials = (config: Config) => {
}

return transport;
};

export const parseTlsOptions = (tlsOptions: string) => {
let tls = { rejectUnauthorized: false };
try {
tls = JSON.parse(tlsOptions);
} catch (ex) {
console.log("Error parsing tls options, invalid JSON: " + ex);
}

return tls;
};
}
5 changes: 1 addition & 4 deletions firestore-send-email/functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,13 @@ async function initialize() {
events.setupEventChannel();
}

/** Extract JSON tsl options */
const tls = parseTlsOptions(config.tls);

async function transportLayer() {
if (config.testing) {
return nodemailer.createTransport({
host: "127.0.0.1",
port: 8132,
secure: false,
tls,
tls: parseTlsOptions(config.tls),
});
}

Expand Down
4 changes: 2 additions & 2 deletions firestore-send-email/functions/src/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ export function foundMissingTemplate(name) {
logger.log(`template '${name}' has been found`);
}

export function invalidURI(uri) {
export function invalidURI() {
logger.warn(
`invalid url: '${uri}' , please reconfigure with a valid SMTP connection URI`
`Invalid URI: please reconfigure with a valid SMTP connection URI`
);
}

0 comments on commit fb2db19

Please sign in to comment.