File
Blob: src/worker/services/inbox/shared.ts
| 1 | import { customAlphabet } from "nanoid"; |
| 2 | import { TEMP_MAILBOX_TTL_HOURS, type TempMailboxTtlHours } from "@/shared/contracts"; |
| 3 | import type { InboxRecord } from "@/worker/types"; |
| 4 | |
| 5 | export const createLocalPart = customAlphabet("abcdefghijklmnopqrstuvwxyz0123456789", 10); |
| 6 | export const ADMIN_SESSION_TTL_MS = 60 * 60 * 1000; |
| 7 | export const WEBSOCKET_TICKET_TTL_MS = 60 * 1000; |
| 8 | export const EXPIRED_INBOX_CLEANUP_BATCH_SIZE = 100; |
| 9 | export const RESERVED_ADDRESSES = ["admin", "postmaster", "abuse", "webmaster"] as const; |
| 10 | export const RESERVED_ADDRESS_VALUES = [...RESERVED_ADDRESSES]; |
| 11 | |
| 12 | export function hoursToMs(hours: number) { |
| 13 | return hours * 60 * 60 * 1000; |
| 14 | } |
| 15 | |
| 16 | export function normalizeDomainName(domainName: string) { |
| 17 | return domainName.trim().toLowerCase(); |
| 18 | } |
| 19 | |
| 20 | export function isAllowedTempMailboxTtl(hours: number): hours is TempMailboxTtlHours { |
| 21 | return TEMP_MAILBOX_TTL_HOURS.includes(hours as TempMailboxTtlHours); |
| 22 | } |
| 23 | |
| 24 | export function ttlHoursFromInbox(inbox: InboxRecord) { |
| 25 | if (!inbox.expiresAt) { |
| 26 | return null; |
| 27 | } |
| 28 | |
| 29 | const ttlHours = Math.round((inbox.expiresAt.getTime() - inbox.createdAt.getTime()) / hoursToMs(1)); |
| 30 | return isAllowedTempMailboxTtl(ttlHours) ? ttlHours : null; |
| 31 | } |
| 32 | |
| 33 | export function computeInboxExpiry(createdAt: Date, ttlHours: TempMailboxTtlHours) { |
| 34 | return new Date(createdAt.getTime() + hoursToMs(ttlHours)); |
| 35 | } |