From 4950b2cffeff9b4b4a8dbfb1be658d6e65dd0921 Mon Sep 17 00:00:00 2001 From: Leandro Facchinetti Date: Mon, 6 Apr 2020 09:48:34 -0400 Subject: [PATCH] Extract URLs --- package.json | 4 +++- src/index.tsx | 19 +++++++++++-------- src/test.ts | 28 ++++++++++++++-------------- 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index 7a2ee92..2110b22 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,9 @@ "env": { "NODE_ENV": "production", "WEB_PORT": "8000", - "EMAIL_PORT": "25" + "EMAIL_PORT": "25", + "BASE_URL": "https://www.kill-the-newsletter.com", + "EMAIL_DOMAIN": "kill-the-newsletter.com" } }, { diff --git a/src/index.tsx b/src/index.tsx index df6e432..14d8cf1 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -10,6 +10,9 @@ import cryptoRandomString from "crypto-random-string"; export const WEB_PORT = process.env.WEB_PORT ?? 8000; export const EMAIL_PORT = process.env.EMAIL_PORT ?? 2525; +export const BASE_URL = process.env.BASE_URL ?? "http://localhost:8000"; +export const EMAIL_DOMAIN = + process.env.EMAIL_DOMAIN ?? "kill-the-newsletter.com"; export const webServer = express() .use(express.static("static")) @@ -49,7 +52,7 @@ export const webServer = express() next(error); }); }) - .get("/entry", (req, res) => + .get("/alternate", (req, res) => res.send( renderHTML( @@ -66,7 +69,7 @@ export const webServer = express()

- + Create an Inbox

@@ -89,7 +92,7 @@ export const emailServer = new SMTPServer({ typeof email.html === "string" ? email.html : email.textAsHtml ?? "", }); for (const { address } of session.envelope.rcptTo) { - const match = address.match(/^(\w+)@kill-the-newsletter.com$/); + const match = address.match(new RegExp(`^(\\w+)@${EMAIL_DOMAIN}$`)); if (match === null) continue; const identifier = match[1]; const path = feedPath(identifier); @@ -213,7 +216,7 @@ function Created({ identifier }: { identifier: string }) {

Enjoy your readings!

- + Create Another Inbox

@@ -234,7 +237,7 @@ function Feed({ name, identifier }: { name: string; identifier: string }) { { "@rel": "alternate", "@type": "text/html", - "@href": "https://www.kill-the-newsletter.com/", + "@href": `${BASE_URL}/alternate`, }, ], id: urn(identifier), @@ -273,7 +276,7 @@ function Entry({ link: { "@rel": "alternate", "@type": "text/html", - "@href": "https://www.kill-the-newsletter.com/entry", + "@href": `${BASE_URL}/alternate`, }, content: { "@type": "html", "#": content }, }, @@ -296,11 +299,11 @@ function feedPath(identifier: string): string { } function feedURL(identifier: string): string { - return `https://www.kill-the-newsletter.com/feeds/${identifier}.xml`; + return `${BASE_URL}/feeds/${identifier}.xml`; } function feedEmail(identifier: string): string { - return `${identifier}@kill-the-newsletter.com`; + return `${identifier}@${EMAIL_DOMAIN}`; } function urn(identifier: string): string { diff --git a/src/test.ts b/src/test.ts index 7d78f55..cdc8331 100644 --- a/src/test.ts +++ b/src/test.ts @@ -1,4 +1,4 @@ -import { webServer, emailServer, WEB_PORT, EMAIL_PORT } from "."; +import { webServer, emailServer, WEB_PORT, EMAIL_PORT, EMAIL_DOMAIN } from "."; import nodemailer from "nodemailer"; import axios from "axios"; import qs from "qs"; @@ -15,7 +15,7 @@ describe("receive email", () => { const before = await getFeed(identifier); await emailClient.sendMail({ from: "publisher@example.com", - to: `${identifier}@kill-the-newsletter.com`, + to: `${identifier}@${EMAIL_DOMAIN}`, subject: "New Message", html: "

HTML content

", }); @@ -29,7 +29,7 @@ describe("receive email", () => { const identifier = await createFeed(); await emailClient.sendMail({ from: "publisher@example.com", - to: `${identifier}@kill-the-newsletter.com`, + to: `${identifier}@${EMAIL_DOMAIN}`, subject: "New Message", html: "

HTML content

", }); @@ -43,7 +43,7 @@ describe("receive email", () => { const identifier = await createFeed(); await emailClient.sendMail({ from: "publisher@example.com", - to: `${identifier}@kill-the-newsletter.com`, + to: `${identifier}@${EMAIL_DOMAIN}`, subject: "New Message", text: "TEXT content", }); @@ -55,20 +55,20 @@ describe("receive email", () => { const identifier = await createFeed(); await emailClient.sendMail({ from: "publisher@example.com", - to: `${identifier}@kill-the-newsletter.com`, + to: `${identifier}@${EMAIL_DOMAIN}`, subject: "New Message", - text: "TEXT content\n\nhttps://www.kill-the-newsletter.com\n\nMore text", + text: "TEXT content\n\nhttps://www.leafac.com\n\nMore text", }); const feed = await getFeed(identifier); expect(feed).toMatch("TEXT content"); - expect(feed).toMatch(`href="https://www.kill-the-newsletter.com"`); + expect(feed).toMatch(`href="https://www.leafac.com"`); }); test("invalid XML character in HTML", async () => { const identifier = await createFeed(); await emailClient.sendMail({ from: "publisher@example.com", - to: `${identifier}@kill-the-newsletter.com`, + to: `${identifier}@${EMAIL_DOMAIN}`, subject: "New Message", html: "

Invalid XML character (backspace): ‘\b’

", }); @@ -80,7 +80,7 @@ describe("receive email", () => { const identifier = await createFeed(); await emailClient.sendMail({ from: "publisher@example.com", - to: `${identifier}@kill-the-newsletter.com`, + to: `${identifier}@${EMAIL_DOMAIN}`, subject: "New Message", text: "Invalid XML character (backspace): ‘\b’", }); @@ -94,7 +94,7 @@ describe("receive email", () => { const identifier = await createFeed(); await emailClient.sendMail({ from: "publisher@example.com", - to: `${identifier}@kill-the-newsletter.com`, + to: `${identifier}@${EMAIL_DOMAIN}`, subject: "New Message", }); const feed = await getFeed(identifier); @@ -105,7 +105,7 @@ describe("receive email", () => { const identifier = await createFeed(); await emailClient.sendMail({ from: "publisher@example.com", - to: `${identifier}@kill-the-newsletter.com`, + to: `${identifier}@${EMAIL_DOMAIN}`, html: "

HTML content

", }); const feed = await getFeed(identifier); @@ -117,7 +117,7 @@ describe("receive email", () => { for (const repetition of [...new Array(4).keys()]) await emailClient.sendMail({ from: "publisher@example.com", - to: `${identifier}@kill-the-newsletter.com`, + to: `${identifier}@${EMAIL_DOMAIN}`, subject: "New Message", text: `REPETITION ${repetition} `.repeat(10_000), }); @@ -129,7 +129,7 @@ describe("receive email", () => { test("nonexistent address", async () => { await emailClient.sendMail({ from: "publisher@example.com", - to: "nonexistent@kill-the-newsletter.com", + to: `nonexistent@${EMAIL_DOMAIN}`, subject: "New Message", html: "

HTML content

", }); @@ -138,7 +138,7 @@ describe("receive email", () => { test("missing from", async () => { const identifier = await createFeed(); await emailClient.sendMail({ - to: `${identifier}@kill-the-newsletter.com`, + to: `${identifier}@${EMAIL_DOMAIN}`, subject: "New Message", html: "

HTML content

", });