You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sims-hq/apps/hq/src/server.ts

172 lines
7.8 KiB
TypeScript

import express from 'express'
import fs from 'node:fs'
import type http from 'node:http'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { apiRouter } from './api'
import { maybePullAwsCosts } from './aws-costs'
import { pollBounces } from './bounces'
import { openDb, type DB } from './db'
import { renderPdf } from './pdf'
import { getClient } from './repos-clients'
import { validateShare } from './repos-shares'
import { runDailyScan } from './scheduler'
import { seedIfEmpty } from './seed'
import { documentHtml } from './templates'
// dist/server.cjs (esbuild CJS bundle) has __dirname; vitest/tsx run this file as ESM.
// Both src/ and dist/ sit one level under apps/hq, so ../../hq-web/dist works from either.
const moduleDir = typeof __dirname === 'undefined'
? path.dirname(fileURLToPath(import.meta.url))
: __dirname
function schedulerDeps(db: DB) {
const company = (): Record<string, string> =>
Object.fromEntries((db.prepare(`SELECT key, value FROM setting WHERE key LIKE 'company.%'`)
.all() as { key: string; value: string }[]).map((r) => [r.key, r.value]))
const gmail = {
f: fetch,
clientId: process.env['GOOGLE_CLIENT_ID'] ?? '',
clientSecret: process.env['GOOGLE_CLIENT_SECRET'] ?? '',
keyHex: process.env['HQ_SECRET_KEY'] ?? '',
}
return { gmail, renderPdf, company }
}
/** Boot the scan + bounce poll now and every 6h. unref() so it never blocks exit. */
export function startScheduler(db: DB): NodeJS.Timeout {
const deps = schedulerDeps(db)
const awsCreds = {
accessKeyId: process.env['AWS_ACCESS_KEY_ID'] ?? '',
secretAccessKey: process.env['AWS_SECRET_ACCESS_KEY'] ?? '',
}
const today = (): string => new Date().toISOString().slice(0, 10)
const tick = (): void => {
void runDailyScan(db, deps, today()).catch((e: unknown) => console.error('[scheduler] scan failed', e))
void pollBounces(db, deps).catch((e: unknown) => console.error('[scheduler] bounce poll failed', e))
// AWS cost pull only when credentials are configured — env-gated, once per month.
if (awsCreds.accessKeyId !== '' && awsCreds.secretAccessKey !== '') {
void maybePullAwsCosts(db, { f: fetch, creds: awsCreds }, today())
.catch((e: unknown) => console.error('[scheduler] aws cost pull failed', e))
}
}
tick()
const handle = setInterval(tick, 6 * 60 * 60 * 1000)
handle.unref()
return handle
}
/** The letterhead settings map documentHtml reads — company.* identity + template.* text. */
function companySettings(db: DB): Record<string, string> {
const rows = db.prepare(
`SELECT key, value FROM setting WHERE key LIKE 'company.%' OR key LIKE 'template.%'`,
).all() as { key: string; value: string }[]
return Object.fromEntries(rows.map((row) => [row.key, row.value]))
}
/**
* A dependency-free fixed-window rate limiter keyed by an opaque string (the caller
* passes the client IP). Returns true while the key is under `limit` in the current
* `windowMs` window, false once it is exhausted. Expired buckets are swept lazily so
* the map cannot grow without bound under IP rotation.
*/
export function makeRateLimiter(limit: number, windowMs: number): (key: string, now?: number) => boolean {
const buckets = new Map<string, { count: number; resetAt: number }>()
return (key: string, now = Date.now()): boolean => {
if (buckets.size > 5000) for (const [k, b] of buckets) if (now >= b.resetAt) buckets.delete(k)
const bucket = buckets.get(key)
if (bucket === undefined || now >= bucket.resetAt) {
buckets.set(key, { count: 1, resetAt: now + windowMs })
return true
}
if (bucket.count >= limit) return false
bucket.count += 1
return true
}
}
/** Minimal, self-contained "this link is gone" page. Carries no document data. */
function shareGonePage(message: string): string {
return `<!doctype html>
<html lang="en"><head><meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Link unavailable</title>
<style>
body{ font-family:'Segoe UI','Inter',Arial,sans-serif; background:#eceef1; color:#1f2937;
display:flex; min-height:100vh; align-items:center; justify-content:center; margin:0; }
.card{ background:#fff; max-width:420px; padding:36px 40px; border-radius:10px;
box-shadow:0 1px 4px rgba(0,0,0,.18); text-align:center; }
h1{ font-size:18px; margin:0 0 8px; color:#111827; }
p{ margin:0; color:#6b7280; font-size:13.5px; line-height:1.5; }
</style></head>
<body><div class="card"><h1>Link unavailable</h1><p>${message}</p></div></body></html>`
}
export interface PublicShareDeps {
/** HTML → PDF renderer; the real puppeteer one in production, a fake in tests. */
renderPdf: (html: string) => Promise<Buffer>
/** Per-IP lookup cap; default 30 lookups per 60s to blunt token brute-forcing. */
rateLimit?: { limit: number; windowMs: number }
}
/**
* Mount the ONE unauthenticated read route: GET /share/:token. It lives OUTSIDE the
* /api auth router and serves nothing but the single document a live token points at —
* rendered inline as application/pdf through the same documentHtml → renderPdf path the
* authenticated /pdf route uses. Unknown/expired/revoked tokens → a plain 404 page with
* no document data. Lookups are rate-limited per client IP. The token is a secret: it is
* never logged (validateShare does the lookup; nothing writes it out).
*/
export function mountPublicShare(app: express.Express, db: DB, deps: PublicShareDeps): void {
const limit = deps.rateLimit?.limit ?? 30
const windowMs = deps.rateLimit?.windowMs ?? 60_000
const allow = makeRateLimiter(limit, windowMs)
const gone = (res: express.Response, status: number, message: string): void => {
res.status(status).type('html').send(shareGonePage(message))
}
app.get('/share/:token', (req, res) => {
const ip = req.ip ?? req.socket.remoteAddress ?? 'unknown'
if (!allow(ip)) { gone(res, 429, 'Too many requests. Please try again in a minute.'); return }
const token = String(req.params['token'] ?? '')
const doc = validateShare(db, token) // null for unknown / revoked / expired — a public read, no audit
if (doc === null) { gone(res, 404, 'This link has expired or is invalid.'); return }
const client = getClient(db, doc.clientId)
if (client === null) { gone(res, 404, 'This link has expired or is invalid.'); return }
const company = companySettings(db)
void (async () => {
try {
const pdf = await deps.renderPdf(documentHtml(doc, client, company))
res.setHeader('Content-Type', 'application/pdf')
// docNo carries '/' (QT/26-27-0001) which is not a legal filename char.
const filename = (doc.docNo ?? 'document').replaceAll('/', '-')
res.setHeader('Content-Disposition', `inline; filename="${filename}.pdf"`)
res.send(pdf)
} catch {
gone(res, 500, 'This document could not be rendered right now.')
}
})()
})
}
export function startServer(port: number, opts: { scheduler?: boolean } = {}): http.Server {
const app = express()
const db = openDb(process.env['HQ_DATA_DIR'])
seedIfEmpty(db) // first boot prints the owner password once
app.locals['db'] = db
app.use(express.json({ limit: '2mb' }))
// Public, unauthenticated document view — mounted OUTSIDE /api, before static.
mountPublicShare(app, db, { renderPdf })
app.use('/api', apiRouter(db))
const webDist = path.resolve(moduleDir, '../../hq-web/dist')
if (fs.existsSync(webDist)) app.use('/', express.static(webDist))
if (opts.scheduler === true) startScheduler(db)
return app.listen(port)
}
// Direct launch (bundled dist/server.cjs); vitest imports startServer instead.
if (process.argv[1]?.endsWith('server.cjs') || process.argv[1]?.endsWith('server.ts')) {
const PORT = Number(process.env['HQ_PORT'] ?? 5182)
startServer(PORT, { scheduler: true })
console.log(`SiMS HQ console on http://localhost:${PORT}`)
}