refactor: split src into domain / application / infrastructure layers

Replace the history-driven lib/ + utils/ split with DDD layers:
- domain/: aggregate, repositories, value objects, pure parsers/format
- application/: feed-service, email-processor, feed-fetcher, stats
- infrastructure/: logging, auth, KV/R2 adapters, HTTP, framework glue

Pure file relocation; imports updated mechanically. Behaviour unchanged.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Julien Herr
2026-05-24 00:46:56 +02:00
parent ab1c15e69a
commit 7bf0f71f86
45 changed files with 90 additions and 68 deletions
@@ -6,7 +6,7 @@ import {
ProcessEmailInput, ProcessEmailInput,
RawAttachment, RawAttachment,
} from "./email-processor"; } from "./email-processor";
import { getCounters } from "../utils/stats"; import { getCounters } from "../application/stats";
const iconKey = (domain: string) => `icon:${domain}`; const iconKey = (domain: string) => `icon:${domain}`;
@@ -1,17 +1,17 @@
import { EmailParser } from "../utils/email-parser"; import { EmailParser } from "../domain/email-parser";
import { AttachmentData, EmailMetadata, Env } from "../types"; import { AttachmentData, EmailMetadata, Env } from "../types";
import { notifySubscribers } from "../utils/websub"; import { notifySubscribers } from "../infrastructure/websub";
import { bumpCounters } from "../utils/stats"; import { bumpCounters } from "../application/stats";
import { import {
cacheFaviconForDomain, cacheFaviconForDomain,
extractEmailDomain, extractEmailDomain,
} from "../utils/favicon-fetcher"; } from "../infrastructure/favicon-fetcher";
import { parseOneClickUnsubscribe } from "../utils/unsubscribe"; import { parseOneClickUnsubscribe } from "../infrastructure/unsubscribe";
import { getAttachmentBucket } from "../utils/attachments"; import { getAttachmentBucket } from "../infrastructure/attachments";
import { FeedRepository } from "../domain/feed-repository"; import { FeedRepository } from "../domain/feed-repository";
import { Feed } from "../domain/feed.aggregate"; import { Feed } from "../domain/feed.aggregate";
import { FeedId } from "../domain/value-objects/feed-id"; import { FeedId } from "../domain/value-objects/feed-id";
import { logger } from "./logger"; import { logger } from "../infrastructure/logger";
import { FEED_MAX_BYTES } from "../config/constants"; import { FEED_MAX_BYTES } from "../config/constants";
export interface RawAttachment { export interface RawAttachment {
@@ -1,9 +1,9 @@
import { Context } from "hono"; import { Context } from "hono";
import { Env, FeedConfig } from "../types"; import { Env, FeedConfig } from "../types";
import { bumpCounters } from "../utils/stats"; import { bumpCounters } from "../application/stats";
import { waitUntilSafe } from "../utils/worker"; import { waitUntilSafe } from "../infrastructure/worker";
import { sendUnsubscribes } from "../utils/unsubscribe"; import { sendUnsubscribes } from "../infrastructure/unsubscribe";
import { getAttachmentBucket } from "../utils/attachments"; import { getAttachmentBucket } from "../infrastructure/attachments";
import { FeedRepository } from "../domain/feed-repository"; import { FeedRepository } from "../domain/feed-repository";
import { FeedId } from "../domain/value-objects/feed-id"; import { FeedId } from "../domain/value-objects/feed-id";
import { import {
@@ -9,7 +9,7 @@ import {
scanKvUsage, scanKvUsage,
setStorageSnapshot, setStorageSnapshot,
} from "./stats"; } from "./stats";
import { getAttachmentBucket } from "./attachments"; import { getAttachmentBucket } from "../infrastructure/attachments";
import { STATS_KEY, FEEDS_LIST_KEY } from "../config/constants"; import { STATS_KEY, FEEDS_LIST_KEY } from "../config/constants";
import { Env } from "../types"; import { Env } from "../types";
@@ -1,10 +1,10 @@
import { Counters, Env, StatsResponse } from "../types"; import { Counters, Env, StatsResponse } from "../types";
import { logger } from "../lib/logger"; import { logger } from "../infrastructure/logger";
import { FeedRepository } from "../domain/feed-repository"; import { FeedRepository } from "../domain/feed-repository";
import { CountersRepository } from "../domain/counters-repository"; import { CountersRepository } from "../domain/counters-repository";
import { WebSubSubscriptionRepository } from "../domain/websub-subscription-repository"; import { WebSubSubscriptionRepository } from "../domain/websub-subscription-repository";
import { FeedId } from "../domain/value-objects/feed-id"; import { FeedId } from "../domain/value-objects/feed-id";
import { getAttachmentBucket } from "./attachments"; import { getAttachmentBucket } from "../infrastructure/attachments";
const EMPTY_COUNTERS: Counters = { const EMPTY_COUNTERS: Counters = {
feeds_created: 0, feeds_created: 0,
+1 -1
View File
@@ -10,7 +10,7 @@ import { FEEDS_LIST_KEY } from "../config/constants";
import { feedKeys } from "./feed-keys"; import { feedKeys } from "./feed-keys";
import { Feed } from "./feed.aggregate"; import { Feed } from "./feed.aggregate";
import { FeedId } from "./value-objects/feed-id"; import { FeedId } from "./value-objects/feed-id";
import { logger } from "../lib/logger"; import { logger } from "../infrastructure/logger";
/** /**
* Single source of truth for KV access to the Feed aggregate. The key schema * Single source of truth for KV access to the Feed aggregate. The key schema
+1 -1
View File
@@ -1,6 +1,6 @@
import { Env, WebSubSubscription } from "../types"; import { Env, WebSubSubscription } from "../types";
import { feedKeys } from "./feed-keys"; import { feedKeys } from "./feed-keys";
import { logger } from "../lib/logger"; import { logger } from "../infrastructure/logger";
/** /**
* KV access for per-feed WebSub subscriber lists (`websub:subs:<feedId>`). * KV access for per-feed WebSub subscriber lists (`websub:subs:<feedId>`).
+4 -4
View File
@@ -10,9 +10,9 @@ import { handle as handleHome } from "./routes/home";
import { handle as handleFavicon, handleFeedFavicon } from "./routes/favicon"; import { handle as handleFavicon, handleFeedFavicon } from "./routes/favicon";
import { hubRouter } from "./routes/hub"; import { hubRouter } from "./routes/hub";
import { apiApp } from "./routes/api"; import { apiApp } from "./routes/api";
import { handleCloudflareEmail } from "./lib/cloudflare-email"; import { handleCloudflareEmail } from "./infrastructure/cloudflare-email";
import { Env } from "./types"; import { Env } from "./types";
import { logger } from "./lib/logger"; import { logger } from "./infrastructure/logger";
import { FeedRepository } from "./domain/feed-repository"; import { FeedRepository } from "./domain/feed-repository";
import { purgeExpiredFeeds } from "./routes/admin/helpers"; import { purgeExpiredFeeds } from "./routes/admin/helpers";
import { import {
@@ -20,8 +20,8 @@ import {
scanR2Usage, scanR2Usage,
scanKvUsage, scanKvUsage,
setStorageSnapshot, setStorageSnapshot,
} from "./utils/stats"; } from "./application/stats";
import { getAttachmentBucket } from "./utils/attachments"; import { getAttachmentBucket } from "./infrastructure/attachments";
import { FORWARD_EMAIL_IPS_CACHE_TTL_MS } from "./config/constants"; import { FORWARD_EMAIL_IPS_CACHE_TTL_MS } from "./config/constants";
type AppEnv = { Bindings: Env }; type AppEnv = { Bindings: Env };
@@ -1,7 +1,7 @@
import PostalMime from "postal-mime"; import PostalMime from "postal-mime";
import { Env } from "../types"; import { Env } from "../types";
import { processEmail, RawAttachment } from "./email-processor"; import { processEmail, RawAttachment } from "../application/email-processor";
import { normalizeCid } from "../utils/html-processor"; import { normalizeCid } from "../infrastructure/html-processor";
import { logger } from "./logger"; import { logger } from "./logger";
export async function handleCloudflareEmail( export async function handleCloudflareEmail(
@@ -6,7 +6,7 @@ import {
} from "../config/constants"; } from "../config/constants";
import { IconRepository } from "../domain/icon-repository"; import { IconRepository } from "../domain/icon-repository";
import { EmailAddress } from "../domain/value-objects/email-address"; import { EmailAddress } from "../domain/value-objects/email-address";
import { logger } from "../lib/logger"; import { logger } from "../infrastructure/logger";
interface IconRecord { interface IconRecord {
data: string | null; // base64 icon bytes, or null for a negative cache entry data: string | null; // base64 icon bytes, or null for a negative cache entry
@@ -1,7 +1,11 @@
import { EmailParser } from "../utils/email-parser"; import { EmailParser } from "../domain/email-parser";
import { Env } from "../types"; import { Env } from "../types";
import { processEmail, IngestResult, RawAttachment } from "./email-processor"; import {
import { normalizeCid } from "../utils/html-processor"; processEmail,
IngestResult,
RawAttachment,
} from "../application/email-processor";
import { normalizeCid } from "../infrastructure/html-processor";
/** Map an ingestion result to the HTTP response ForwardEmail expects. */ /** Map an ingestion result to the HTTP response ForwardEmail expects. */
export function ingestResultToResponse(result: IngestResult): Response { export function ingestResultToResponse(result: IngestResult): Response {
@@ -6,7 +6,7 @@ import {
sendOneClickUnsubscribe, sendOneClickUnsubscribe,
sendUnsubscribes, sendUnsubscribes,
} from "./unsubscribe"; } from "./unsubscribe";
import { getCounters } from "./stats"; import { getCounters } from "../application/stats";
import type { Env } from "../types"; import type { Env } from "../types";
const POST_HEADER = "List-Unsubscribe=One-Click"; const POST_HEADER = "List-Unsubscribe=One-Click";
@@ -1,7 +1,7 @@
import { Env } from "../types"; import { Env } from "../types";
import { UNSUBSCRIBE_TIMEOUT_MS } from "../config/constants"; import { UNSUBSCRIBE_TIMEOUT_MS } from "../config/constants";
import { bumpCounters } from "./stats"; import { bumpCounters } from "../application/stats";
import { logger } from "../lib/logger"; import { logger } from "../infrastructure/logger";
/** /**
* Extract a one-click unsubscribe URL from a stored email's headers per * Extract a one-click unsubscribe URL from a stored email's headers per
+1 -1
View File
@@ -3,7 +3,7 @@ import { http, HttpResponse } from "msw";
import { Hono } from "hono"; import { Hono } from "hono";
import app from "./admin"; import app from "./admin";
import { createMockEnv, server } from "../test/setup"; import { createMockEnv, server } from "../test/setup";
import { getCounters } from "../utils/stats"; import { getCounters } from "../application/stats";
import { Env } from "../types"; import { Env } from "../types";
describe("Admin Routes", () => { describe("Admin Routes", () => {
+8 -4
View File
@@ -5,12 +5,16 @@ import { z } from "zod";
import { Env } from "../types"; import { Env } from "../types";
import { csrf } from "hono/csrf"; import { csrf } from "hono/csrf";
import { ADMIN_COOKIE_MAX_AGE } from "../config/constants"; import { ADMIN_COOKIE_MAX_AGE } from "../config/constants";
import { logger } from "../lib/logger"; import { logger } from "../infrastructure/logger";
import { timingSafeEqual, checkProxyAuth } from "../lib/auth"; import { timingSafeEqual, checkProxyAuth } from "../infrastructure/auth";
import { Layout, clampText } from "./admin/ui"; import { Layout, clampText } from "./admin/ui";
import { FeedRepository } from "../domain/feed-repository"; import { FeedRepository } from "../domain/feed-repository";
import { renameFeed } from "../lib/feed-service"; import { renameFeed } from "../application/feed-service";
import { feedRssUrl, feedAtomUrl, feedEmailAddress } from "../utils/urls"; import {
feedRssUrl,
feedAtomUrl,
feedEmailAddress,
} from "../infrastructure/urls";
import { feedsRouter } from "./admin/feeds"; import { feedsRouter } from "./admin/feeds";
import { emailsRouter } from "./admin/emails"; import { emailsRouter } from "./admin/emails";
import { dashboardScript } from "../scripts/generated/dashboard"; import { dashboardScript } from "../scripts/generated/dashboard";
+7 -3
View File
@@ -1,6 +1,6 @@
import { Hono } from "hono"; import { Hono } from "hono";
import { Env, EmailMetadata } from "../../types"; import { Env, EmailMetadata } from "../../types";
import { logger } from "../../lib/logger"; import { logger } from "../../infrastructure/logger";
import { Layout, clampText } from "./ui"; import { Layout, clampText } from "./ui";
import { import {
deleteAttachmentsForEmails, deleteAttachmentsForEmails,
@@ -8,8 +8,12 @@ import {
} from "./helpers"; } from "./helpers";
import { FeedRepository } from "../../domain/feed-repository"; import { FeedRepository } from "../../domain/feed-repository";
import { FeedId } from "../../domain/value-objects/feed-id"; import { FeedId } from "../../domain/value-objects/feed-id";
import { feedRssUrl, feedAtomUrl, feedEmailAddress } from "../../utils/urls"; import {
import { formatBytes } from "../../utils/format"; feedRssUrl,
feedAtomUrl,
feedEmailAddress,
} from "../../infrastructure/urls";
import { formatBytes } from "../../domain/format";
import { EmailAddress } from "../../domain/value-objects/email-address"; import { EmailAddress } from "../../domain/value-objects/email-address";
import { emailsPageScript } from "../../scripts/generated/emails-page"; import { emailsPageScript } from "../../scripts/generated/emails-page";
+7 -7
View File
@@ -1,12 +1,12 @@
import { Hono } from "hono"; import { Hono } from "hono";
import { z } from "zod"; import { z } from "zod";
import { Env } from "../../types"; import { Env } from "../../types";
import { bumpCounters } from "../../utils/stats"; import { bumpCounters } from "../../application/stats";
import { waitUntilSafe } from "../../utils/worker"; import { waitUntilSafe } from "../../infrastructure/worker";
import { feedRssUrl, feedEmailAddress } from "../../utils/urls"; import { feedRssUrl, feedEmailAddress } from "../../infrastructure/urls";
import { logger } from "../../lib/logger"; import { logger } from "../../infrastructure/logger";
import { sendUnsubscribes } from "../../utils/unsubscribe"; import { sendUnsubscribes } from "../../infrastructure/unsubscribe";
import { getAttachmentBucket } from "../../utils/attachments"; import { getAttachmentBucket } from "../../infrastructure/attachments";
import { Layout } from "./ui"; import { Layout } from "./ui";
import { purgeFeedKeysStep, collectUnsubscribeUrls } from "./helpers"; import { purgeFeedKeysStep, collectUnsubscribeUrls } from "./helpers";
import { FeedRepository } from "../../domain/feed-repository"; import { FeedRepository } from "../../domain/feed-repository";
@@ -16,7 +16,7 @@ import {
editFeed, editFeed,
deleteFeedRecord, deleteFeedRecord,
deleteFeedFastDetailed, deleteFeedFastDetailed,
} from "../../lib/feed-service"; } from "../../application/feed-service";
type AppEnv = { Bindings: Env }; type AppEnv = { Bindings: Env };
+2 -2
View File
@@ -1,6 +1,6 @@
import { EmailData, EmailMetadata, Env } from "../../types"; import { EmailData, EmailMetadata, Env } from "../../types";
import { logger } from "../../lib/logger"; import { logger } from "../../infrastructure/logger";
import { getAttachmentBucket } from "../../utils/attachments"; import { getAttachmentBucket } from "../../infrastructure/attachments";
import { FeedRepository } from "../../domain/feed-repository"; import { FeedRepository } from "../../domain/feed-repository";
import { FeedId } from "../../domain/value-objects/feed-id"; import { FeedId } from "../../domain/value-objects/feed-id";
+8 -4
View File
@@ -2,17 +2,21 @@ import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
import { cors } from "hono/cors"; import { cors } from "hono/cors";
import { Scalar } from "@scalar/hono-api-reference"; import { Scalar } from "@scalar/hono-api-reference";
import { Env, FeedConfig } from "../../types"; import { Env, FeedConfig } from "../../types";
import { apiAuthMiddleware } from "../../lib/auth"; import { apiAuthMiddleware } from "../../infrastructure/auth";
import { import {
createFeedRecord, createFeedRecord,
editFeed, editFeed,
deleteFeedRecord, deleteFeedRecord,
} from "../../lib/feed-service"; } from "../../application/feed-service";
import { deleteAttachmentsForEmails } from "../admin/helpers"; import { deleteAttachmentsForEmails } from "../admin/helpers";
import { FeedRepository } from "../../domain/feed-repository"; import { FeedRepository } from "../../domain/feed-repository";
import { FeedId } from "../../domain/value-objects/feed-id"; import { FeedId } from "../../domain/value-objects/feed-id";
import { getStats } from "../../utils/stats"; import { getStats } from "../../application/stats";
import { feedEmailAddress, feedRssUrl, feedAtomUrl } from "../../utils/urls"; import {
feedEmailAddress,
feedRssUrl,
feedAtomUrl,
} from "../../infrastructure/urls";
import { import {
ErrorSchema, ErrorSchema,
FeedIdParam, FeedIdParam,
+3 -3
View File
@@ -1,8 +1,8 @@
import { Context } from "hono"; import { Context } from "hono";
import { Env } from "../types"; import { Env } from "../types";
import { generateAtomFeed } from "../utils/feed-generator"; import { generateAtomFeed } from "../infrastructure/feed-generator";
import { fetchFeedData } from "../utils/feed-fetcher"; import { fetchFeedData } from "../application/feed-fetcher";
import { baseUrl, feedAtomUrl } from "../utils/urls"; import { baseUrl, feedAtomUrl } from "../infrastructure/urls";
import { isExpired } from "../domain/feed"; import { isExpired } from "../domain/feed";
export async function handle(c: Context<{ Bindings: Env }>): Promise<Response> { export async function handle(c: Context<{ Bindings: Env }>): Promise<Response> {
+2 -2
View File
@@ -1,8 +1,8 @@
import { Context } from "hono"; import { Context } from "hono";
import { html, raw } from "hono/html"; import { html, raw } from "hono/html";
import { Env } from "../types"; import { Env } from "../types";
import { processEmailContent } from "../utils/html-processor"; import { processEmailContent } from "../infrastructure/html-processor";
import { formatBytes } from "../utils/format"; import { formatBytes } from "../domain/format";
import { FeedRepository } from "../domain/feed-repository"; import { FeedRepository } from "../domain/feed-repository";
import { FeedId } from "../domain/value-objects/feed-id"; import { FeedId } from "../domain/value-objects/feed-id";
import { isExpired } from "../domain/feed"; import { isExpired } from "../domain/feed";
+4 -1
View File
@@ -2,7 +2,10 @@ import { Context } from "hono";
import { Env } from "../types"; import { Env } from "../types";
import { FeedRepository } from "../domain/feed-repository"; import { FeedRepository } from "../domain/feed-repository";
import { FeedId } from "../domain/value-objects/feed-id"; import { FeedId } from "../domain/value-objects/feed-id";
import { cacheFaviconForDomain, getCachedIcon } from "../utils/favicon-fetcher"; import {
cacheFaviconForDomain,
getCachedIcon,
} from "../infrastructure/favicon-fetcher";
export const FAVICON_PATH = "/favicon.svg"; export const FAVICON_PATH = "/favicon.svg";
+1 -1
View File
@@ -1,6 +1,6 @@
import { Context } from "hono"; import { Context } from "hono";
import { Env } from "../types"; import { Env } from "../types";
import { getAttachmentBucket } from "../utils/attachments"; import { getAttachmentBucket } from "../infrastructure/attachments";
export async function handle(c: Context<{ Bindings: Env }>): Promise<Response> { export async function handle(c: Context<{ Bindings: Env }>): Promise<Response> {
const bucket = getAttachmentBucket(c.env); const bucket = getAttachmentBucket(c.env);
+2 -2
View File
@@ -1,7 +1,7 @@
import { Context } from "hono"; import { Context } from "hono";
import { Env } from "../types"; import { Env } from "../types";
import { getStats } from "../utils/stats"; import { getStats } from "../application/stats";
import { formatBytes } from "../utils/format"; import { formatBytes } from "../domain/format";
import { R2_FREE_TIER_BYTES, KV_FREE_TIER_BYTES } from "../config/constants"; import { R2_FREE_TIER_BYTES, KV_FREE_TIER_BYTES } from "../config/constants";
import { Layout } from "./admin/ui"; import { Layout } from "./admin/ui";
+3 -3
View File
@@ -3,10 +3,10 @@ import { Env } from "../types";
import { import {
verifyAndStoreSubscription, verifyAndStoreSubscription,
verifyAndDeleteSubscription, verifyAndDeleteSubscription,
} from "../utils/websub"; } from "../infrastructure/websub";
import { waitUntilSafe } from "../utils/worker"; import { waitUntilSafe } from "../infrastructure/worker";
import { DEFAULT_LEASE_SECONDS, MAX_LEASE_SECONDS } from "../config/constants"; import { DEFAULT_LEASE_SECONDS, MAX_LEASE_SECONDS } from "../config/constants";
import { feedTopicPattern } from "../utils/urls"; import { feedTopicPattern } from "../infrastructure/urls";
import { FeedRepository } from "../domain/feed-repository"; import { FeedRepository } from "../domain/feed-repository";
import { FeedId } from "../domain/value-objects/feed-id"; import { FeedId } from "../domain/value-objects/feed-id";
+1 -1
View File
@@ -3,7 +3,7 @@ import { http, HttpResponse } from "msw";
import worker from "../index"; import worker from "../index";
import { server, createMockEnv, MockR2 } from "../test/setup"; import { server, createMockEnv, MockR2 } from "../test/setup";
import type { Env } from "../types"; import type { Env } from "../types";
import type { ForwardEmailPayload } from "../lib/forwardemail"; import type { ForwardEmailPayload } from "../infrastructure/forwardemail";
const AUTHORIZED_IP = "138.197.213.185"; // first fallback IP const AUTHORIZED_IP = "138.197.213.185"; // first fallback IP
const DOMAIN = "test.getmynews.app"; const DOMAIN = "test.getmynews.app";
+4 -1
View File
@@ -1,6 +1,9 @@
import { Context } from "hono"; import { Context } from "hono";
import { Env } from "../types"; import { Env } from "../types";
import { ForwardEmailPayload, handleForwardEmail } from "../lib/forwardemail"; import {
ForwardEmailPayload,
handleForwardEmail,
} from "../infrastructure/forwardemail";
export async function handle(c: Context<{ Bindings: Env }>): Promise<Response> { export async function handle(c: Context<{ Bindings: Env }>): Promise<Response> {
try { try {
+3 -3
View File
@@ -1,8 +1,8 @@
import { Context } from "hono"; import { Context } from "hono";
import { Env } from "../types"; import { Env } from "../types";
import { generateRssFeed } from "../utils/feed-generator"; import { generateRssFeed } from "../infrastructure/feed-generator";
import { fetchFeedData } from "../utils/feed-fetcher"; import { fetchFeedData } from "../application/feed-fetcher";
import { baseUrl, feedRssUrl } from "../utils/urls"; import { baseUrl, feedRssUrl } from "../infrastructure/urls";
import { isExpired } from "../domain/feed"; import { isExpired } from "../domain/feed";
export async function handle(c: Context<{ Bindings: Env }>): Promise<Response> { export async function handle(c: Context<{ Bindings: Env }>): Promise<Response> {