@camada/node 0.3.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/koa.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/koa.ts","../src/camada.ts","../src/env.ts","../src/challenge.ts","../package.json","../src/version.ts","../src/index.ts"],"sourcesContent":["// Koa middleware over the raw req/res. Register first:\n// import { camadaKoa } from '@camada/node/koa';\n// app.use(camadaKoa());\nimport type { IncomingMessage, ServerResponse } from 'node:http';\nimport { Camada } from './camada.js';\nimport { getDefault } from './index.js';\n\ninterface KoaishCtx {\n req: IncomingMessage & { body?: unknown; originalUrl?: string };\n res: ServerResponse;\n request?: { body?: unknown };\n originalUrl?: string;\n respond?: boolean;\n}\n\nexport function camadaKoa(engine?: Camada) {\n return async (ctx: KoaishCtx, next: () => Promise<unknown>): Promise<void> => {\n // A body parser registered before camada (@koa/bodyparser, koa-bodyparser) leaves the form on\n // ctx.request.body, and koa-mount strips the mount from req.url: hand both to the engine.\n ctx.req.body ??= ctx.request?.body;\n ctx.req.originalUrl ??= ctx.originalUrl;\n if ((engine ?? getDefault()).handle(ctx.req, ctx.res)) { ctx.respond = false; return; } // camada owns this response\n await next();\n };\n}\n","// The engine: node:http-level request handling every adapter delegates to.\n// camada.handle(req, res) -> true when camada fully answered the request (block or beacon\n// endpoint), false when the app should proceed. Everything runs inside the fail-open\n// envelope: a camada bug must never 5xx the customer (plan.md INT-2), and CAMADA_DISABLED=1\n// bypasses the SDK entirely.\nimport type { IncomingMessage, Server, ServerResponse } from 'node:http';\nimport { randomUUID, webcrypto } from 'node:crypto'; // webcrypto handed to hashUserId explicitly, so the hash never depends on which global the host exposes\nimport {\n SnapshotClient, EventQueue, buildWireEvent, resolveClientIp, hashUserId, guarded, logRateLimited,\n challengeCookie, safeReturnTo, wantsHtml, CHALLENGE_COOKIE,\n TAP_NODE, type ChallengeKit, type SnapshotVersion, type TrustedProxyConfig, type WireEvent,\n} from '@camada/core';\nimport iife from '@camada/browser/iife-string';\nimport { resolveEnv, type ResolvedEnv } from './env.js';\nimport {\n CHALLENGE_PATH, nodeChallengeKit, readForm, parsedBody, isHttps, writeChallengePage, writeChallengeJson,\n} from './challenge.js';\nimport { SDK_ID } from './version.js';\n\nconst SESSION_COOKIE = '_sfp'; // same cookie as the edge collector: sid/ns comparable across taps\nconst SCRIPT_PATH = '/_cam/b.js';\nconst FP_PATH = '/_cam/fp';\nconst FP_MAX = 32 * 1024; // matches the server's /fp cap: never accept what ingest will 413\nconst ATTACHED = Symbol.for('camada.attached'); // one wrap per server, however often attach() is called\n\nexport interface CamadaOptions {\n env?: Record<string, string | undefined>;\n fetchImpl?: typeof fetch; // threaded into snapshot client, event queue, and the fp relay (tests)\n refreshMs?: number;\n scriptPath?: string;\n fpPath?: string;\n challenge?: boolean; // enforce `challenge` verdicts with the first-party page (default true; CAMADA_CHALLENGE=0 also switches it off)\n challengePath?: string; // where that page posts its solution (default /__camada/challenge)\n snapshotVersion?: SnapshotVersion; // 5 (default) also carries the tenant's ordered custom rules; 4 the allow/challenge sides only; 3 opts out of both\n /** With CAMADA_SERVERLESS=1: handed every per-request event flush and snapshot refresh, so the\n * platform keeps the invocation alive until they finish (e.g. `waitUntil` from `@vercel/functions`). */\n waitUntil?: (p: Promise<unknown>) => void;\n}\n\ninterface CamadaRequest extends IncomingMessage {\n originalUrl?: string; // Express/Nest: the full URL; `url` loses the mount path inside app.use('/x', …)\n body?: unknown; // what a body parser mounted before camada left behind\n camada?: { rid: string; ip: string | null; sid: string };\n route?: { path?: string }; // Express fills this after routing\n camadaChallenged?: boolean; // serveChallenge() already shipped this request's event\n camadaOpen?: boolean; // its event ships on response finish: a track() row until then rides that flush\n camadaTracks?: Promise<void>[]; // track() rows still hashing (serverless: the finish flush waits for them)\n camadaPrefix?: string; // the mount prefix seen when camada ran (scriptTag() runs later, where req.url is whole again)\n}\n\n/** The getter `header` conditions read (§D3). node:http lower-cases every incoming name and\n * the matcher always asks with a lower-cased one, so nothing has to be normalised here; a\n * header the client repeated arrives as an array and is joined the way the wire carried it. */\nconst headerReader = (req: IncomingMessage) => (name: string): string | null => {\n const v = req.headers[name];\n return v == null ? null : Array.isArray(v) ? v.join(', ') : v;\n};\n\nconst splitUrl = (raw: string): { path: string; query: string } => {\n const qi = raw.indexOf('?');\n return qi === -1 ? { path: raw, query: '' } : { path: raw.slice(0, qi), query: raw.slice(qi) };\n};\n\n/** Path and query as the client sent them: what rules, the challenge list and events see. Express\n * strips the mount path from `req.url` inside `app.use(path, mw)` (Nest mounts module middleware\n * that way, at '{*splat}' or '*'), and koa-mount does the same (the Koa adapter copies Koa's\n * `originalUrl` over), so read `originalUrl` first; plain node:http and Fastify only have `url`. */\nfunction pathAndQuery(req: CamadaRequest): { path: string; query: string } {\n return splitUrl(req.originalUrl || req.url || '/');\n}\n\n/** Whether the request is for one of camada's own endpoints (beacon script, beacon POST,\n * challenge verify). Those answer at the mount-relative path (`req.url`), so `app.use('/api',\n * camada)` serves `/api/_cam/b.js`, and at the full path, so a wildcard mount (Nest, whose\n * `req.url` is stripped to '/') still serves `/_cam/b.js`. */\nconst atEndpoint = (req: CamadaRequest, path: string, endpoint: string): boolean =>\n path === endpoint || splitUrl(req.url || '/').path === endpoint;\n\n/** The mount prefix camada runs under: the full path minus the mount-relative one, when the full\n * path ends with it (`app.use('/api', …)`, koa-mount). A relative path of `/` says nothing: it is\n * both the mount's own root and what a wildcard mount (Nest) strips every path to, so no prefix. */\nfunction mountPrefix(req: CamadaRequest): string {\n if (!req.originalUrl) return '';\n const full = splitUrl(req.originalUrl).path, rel = splitUrl(req.url || '/').path;\n return rel !== '/' && full !== rel && full.endsWith(rel) ? full.slice(0, full.length - rel.length) : '';\n}\n\n/** A unix-socket (pipe) listener: Node never has a peer address there, on any request. The\n * listening server reports its address as the socket path (a string); `_pipeName` keeps the\n * answer after server.close() has dropped the handle while connections drain. */\nfunction onUnixListener(socket: unknown): boolean {\n const srv = (socket as { server?: { address?: () => unknown; _pipeName?: unknown } } | null)?.server;\n if (!srv) return false;\n try { if (typeof srv.address?.() === 'string') return true; } catch { /* not a net.Server */ }\n return typeof srv._pipeName === 'string';\n}\n\nconst cookieValue = (cookie: string, name: string): string | null => {\n const src = '; ' + cookie;\n const i = src.indexOf('; ' + name + '=');\n if (i === -1) return null;\n const start = i + name.length + 3;\n const j = src.indexOf(';', start);\n return src.slice(start, j === -1 ? undefined : j);\n};\n\n/** Sets a first visit's `_sfp` and keeps it there: an app that replaces Set-Cookie through\n * setHeader (or writeHead, setHeaders, which route through it once any header is set) gets its\n * own cookies plus the session's, unless it set `_sfp` itself. appendHeader already keeps it. */\nfunction keepSessionCookie(res: ServerResponse, cookie: string): void {\n const set = res.setHeader;\n res.setHeader = function (this: ServerResponse, name: string, value: number | string | readonly string[]) {\n if (typeof name === 'string' && name.toLowerCase() === 'set-cookie') {\n const list = Array.isArray(value) ? value : [String(value)];\n if (!list.some((c) => String(c).trimStart().startsWith(SESSION_COOKIE + '='))) value = [...list, cookie];\n }\n return set.call(this, name, value);\n } as ServerResponse['setHeader'];\n res.setHeader('set-cookie', cookie);\n}\n\nexport class Camada {\n readonly env: ResolvedEnv | null;\n readonly snap: SnapshotClient | null = null;\n readonly queue: EventQueue | null = null;\n private readonly fetchImpl: typeof fetch;\n private readonly scriptPath: string;\n private readonly fpPath: string;\n private readonly challengeOn: boolean;\n private readonly challengePath: string;\n private readonly challengePathSet: boolean; // configured explicitly: used as given, never prefixed\n private readonly scriptPathSet: boolean;\n private readonly kit: ChallengeKit | null = null;\n private readonly envSource: Record<string, string | undefined>;\n private readonly waitUntil?: (p: Promise<unknown>) => void;\n\n constructor(opts: CamadaOptions = {}) {\n this.envSource = opts.env ?? process.env;\n this.fetchImpl = opts.fetchImpl ?? fetch;\n this.waitUntil = opts.waitUntil;\n this.scriptPath = opts.scriptPath ?? SCRIPT_PATH;\n this.fpPath = opts.fpPath ?? FP_PATH;\n this.challengeOn = opts.challenge !== false && this.envSource.CAMADA_CHALLENGE !== '0';\n this.challengePath = opts.challengePath ?? CHALLENGE_PATH;\n this.challengePathSet = opts.challengePath != null;\n this.scriptPathSet = opts.scriptPath != null;\n this.env = resolveEnv(this.envSource);\n if (!this.env) return; // unconfigured: every entry point no-ops\n if (this.envSource.CAMADA_DISABLED === '1') return; // killed at boot: no poll timer, no exit hooks, truly silent\n this.snap = new SnapshotClient({\n url: this.env.snapshotUrl, token: this.env.snapToken,\n mode: this.env.serverless ? 'lazy' : 'timer',\n refreshMs: opts.refreshMs, snapshotVersion: opts.snapshotVersion, fetchImpl: this.fetchImpl, sdk: SDK_ID,\n });\n this.queue = new EventQueue({ url: this.env.ingestUrl, token: this.env.ingestToken, fetchImpl: this.fetchImpl, sdk: SDK_ID });\n this.kit = nodeChallengeKit(this.env.secret);\n this.snap.start();\n this.queue.installNodeExitFlush();\n }\n\n get disabled(): boolean { return this.envSource.CAMADA_DISABLED === '1' || !this.env; }\n\n private trustedProxy(): TrustedProxyConfig | null {\n return this.env?.trustedProxy ?? this.snap?.config?.trusted_proxy ?? null; // explicit local override wins\n }\n\n /** The client IP per the trusted-proxy config (team contract C6 v3). No socket at all, or a\n * unix-socket listener (nginx/Passenger in front, where Node never has a peer): core's no-peer\n * X-Forwarded-For walk, which trusts the local proxy. A TCP socket with no readable peer (the\n * client reset the connection before camada ran, e.g. behind an async middleware) is\n * unidentifiable: the walk would believe whatever the client wrote. So: null, the stance every\n * ip-bound feature already takes (no ip rules, no challenge). */\n private clientIp(req: IncomingMessage): string | null {\n const peer = req.socket?.remoteAddress;\n if (!peer && req.socket && !onUnixListener(req.socket)) return null;\n return resolveClientIp(peer ?? null, req.headers['x-forwarded-for'] as string | undefined, this.trustedProxy());\n }\n\n /** Where the challenge form posts: under the mount prefix, so it reaches camada, unless the\n * path was configured explicitly. */\n private challengeAction(req: CamadaRequest): string {\n return this.challengePathSet ? this.challengePath : (req.camadaPrefix ?? mountPrefix(req)) + this.challengePath;\n }\n\n private beaconEnabled(): boolean { return this.snap?.config?.beacon !== false; }\n\n /** Queues a row. With CAMADA_SERVERLESS=1 it also starts the POST now: the platform freezes the\n * process once the response is out, so the 15 s interval would never run. Never throws. */\n private enqueue(row: unknown, req?: CamadaRequest): void {\n const q = this.queue!;\n q.push(row);\n if (!this.env?.serverless) return;\n if (req?.camadaOpen) return; // a track() row while the response is open: the finish flush carries it\n this.flushFor(req);\n }\n\n /** Serverless: start the POST now and hand it to waitUntil. Never throws. */\n private flushFor(req?: CamadaRequest): void {\n const q = this.queue!;\n guarded(() => {\n // One POST per request: the response-finish flush first waits for that request's track()\n // rows still hashing. flush() is single-in-flight, so a row queued while a POST was\n // finishing rides a second one.\n const tracks = req?.camadaTracks ?? [];\n const p = Promise.allSettled(tracks)\n .then(() => q.flush())\n .then(() => (q.size > 0 ? q.flush() : undefined));\n this.waitUntil?.(p);\n }, undefined);\n }\n\n /** True when camada fully handled the request. Synchronous; never throws. */\n handle(req: IncomingMessage, res: ServerResponse): boolean {\n return guarded(() => this.handleInner(req as CamadaRequest, res), false);\n }\n\n private handleInner(req: CamadaRequest, res: ServerResponse): boolean {\n if (this.disabled || !this.snap || !this.queue || !this.env) return false;\n const t0 = Date.now();\n this.snap.ensureFresh(this.env.serverless ? this.waitUntil : undefined);\n\n const { path, query } = pathAndQuery(req);\n const ip = this.clientIp(req);\n req.camadaPrefix = mountPrefix(req);\n\n // enforce before anything else, beacon endpoints included — fail open while cold. The custom\n // rules read the user agent and the request headers (§D3); without them every `ua` and\n // `header` condition is false.\n const v = this.snap.verdict({ ip, path, ua: req.headers['user-agent'], header: headerReader(req) });\n if (v.block) {\n const headers: Record<string, string> = { 'x-block-reason': v.reason ?? '', 'x-block-version': v.version ?? '', 'content-type': 'text/plain', 'cache-control': 'no-store' };\n if (v.rule) headers['x-block-rule'] = v.rule; // a custom rule blocked: name it, so the customer knows which row to edit\n res.writeHead(403, headers);\n res.end('Forbidden');\n const ev = this.buildEvent(req, path, query, ip, { rid: randomUUID(), sid: null, newSession: false });\n ev.st = 403; // blocked requests always ship: silent expiry makes blocks oscillate\n ev.blk = v.reason; // SDK-01: the reason rides the event so the analyst counts SDK blocks, not the app's own 403s ('rule' when a rule decided)\n if (v.rule) ev.rl = v.rule;\n this.enqueue(ev);\n return true;\n }\n // `warn` passes the request and only marks its event (below, on response-finish); a skip\n // passes with nothing stamped at all — it is the absence of enforcement.\n\n // A challenge needs a resolved client IP: the nonce and the `_cch` cookie are bound to it,\n // so without one a single solve would mint a cookie every unidentified client could\n // present. No ip -> no challenge (fail open), the same stance ip rules take.\n if (this.challengeOn && this.kit && ip) {\n // The verify endpoint answers first: a challenged client must be able to reach it.\n if (req.method === 'POST' && atEndpoint(req, path, this.challengePath)) { this.verifyChallenge(req, res, ip); return true; }\n if (v.challenge && !this.challengePassed(req, ip)) { this.serveChallengeInner(req, res, ip, path + query); return true; }\n }\n\n if (this.beaconEnabled()) {\n if (req.method === 'GET' && atEndpoint(req, path, this.scriptPath)) {\n res.writeHead(200, { 'content-type': 'application/javascript', 'cache-control': 'public, max-age=3600' });\n res.end(iife);\n return true;\n }\n if (req.method === 'POST' && atEndpoint(req, path, this.fpPath)) {\n this.relayBeacon(req, res, ip);\n return true;\n }\n }\n\n const rid = randomUUID();\n let sid = cookieValue((req.headers.cookie as string) || '', SESSION_COOKIE);\n const newSession = !sid;\n if (!sid) {\n sid = randomUUID();\n const https = (req.socket as { encrypted?: boolean }).encrypted || req.headers['x-forwarded-proto'] === 'https';\n keepSessionCookie(res, `${SESSION_COOKIE}=${sid}; Path=/; Max-Age=2592000; HttpOnly; SameSite=Lax${https ? '; Secure' : ''}`);\n }\n req.camada = { rid, ip, sid };\n res.setHeader('x-rid', rid);\n\n const cfg = this.snap.config;\n const excluded = (cfg?.exclude || []).some((x) => path.startsWith(x));\n if (!excluded && Math.random() < (cfg?.sample ?? 1)) {\n // 'finish' when the response went out whole; 'close' alone when the client left first (an\n // aborted SSE or download), which ships the status set so far and dur up to the abort — the\n // fetch adapters' cancel semantics. 'close' also follows every 'finish': one event either way.\n let shipped = false;\n req.camadaOpen = true;\n const ship = () => guarded(() => {\n req.camadaOpen = false;\n if (shipped) return;\n shipped = true;\n // serveChallenge() may have answered from inside the app, and it already shipped the\n // `blk: \"challenge\"` row — one request, one event. A track() row queued while the\n // response was open still needs this finish flush (serverless).\n if (req.camadaChallenged) { if (this.env?.serverless) this.flushFor(req); return; }\n const ev = this.buildEvent(req, path, query, ip, { rid, sid, newSession });\n ev.ts = t0; // the request start: the timeline draws [ts, ts + dur]\n ev.st = res.statusCode;\n ev.dur = Date.now() - t0;\n if (req.route?.path) ev.rt = String(req.route.path);\n if (v.warn && v.rule) ev.wrn = v.rule; // §D3: the warn rule that let this request through\n this.enqueue(ev, req);\n }, undefined);\n res.once('finish', ship);\n res.once('close', ship);\n }\n return false;\n }\n\n private buildEvent(req: CamadaRequest, path: string, query: string, ip: string | null, o: { rid: string; sid: string | null; newSession: boolean }): WireEvent {\n const raw = req.rawHeaders || [];\n const headers: Array<[string, string]> = [];\n for (let i = 0; i + 1 < raw.length; i += 2) headers.push([raw[i], raw[i + 1]]);\n return buildWireEvent(\n { method: req.method || 'GET', host: (req.headers.host as string) || '', path, query, headers, ip, httpVersion: req.httpVersion },\n { tap: TAP_NODE, rid: o.rid, sid: o.sid, newSession: o.newSession },\n );\n }\n\n /** Reads the beacon POST (≤32 KB), answers 204 immediately, and queues the beacon as a `sig: 1`\n * row with the trusted-proxy-resolved client IP: it rides the next event batch, so the analyst\n * sees one request per flush instead of one per page view. Junk bodies are dropped, never shipped.\n * A body parser mounted before camada (express.json(), always on Nest) has already drained the\n * stream: its `req.body` is used instead, and the request is answered either way. */\n private relayBeacon(req: CamadaRequest, res: ServerResponse, ip: string | null): void {\n const accept = (parsed: unknown) => {\n res.writeHead(204, { 'cache-control': 'no-store' });\n res.end();\n if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed) || Buffer.isBuffer(parsed)) return;\n this.enqueue({ ...(parsed as Record<string, unknown>), sig: 1, ip, tap: TAP_NODE });\n };\n const fromText = (text: string) => { try { return JSON.parse(text) as unknown; } catch { return null; } };\n if (req.readableEnded) {\n const b = parsedBody(req);\n if ((typeof b === 'string' ? b : b ? JSON.stringify(b) : '').length > FP_MAX) { res.writeHead(413).end(); return; }\n accept(typeof b === 'string' ? fromText(b) : b);\n return;\n }\n const chunks: Buffer[] = [];\n let size = 0, dead = false;\n req.on('data', (c: Buffer) => {\n size += c.length;\n if (size > FP_MAX) { dead = true; req.removeAllListeners('data'); res.writeHead(413).end(); return; }\n chunks.push(c);\n });\n req.on('end', () => guarded(() => {\n if (dead) return;\n accept(fromText(Buffer.concat(chunks).toString('utf8')));\n }, undefined));\n req.on('error', () => { try { res.destroy(); } catch { /* already gone */ } });\n }\n\n private challengePassed(req: IncomingMessage, ip: string | null): boolean {\n return !!this.kit?.tokenValid(ip, Date.now(), cookieValue((req.headers.cookie as string) || '', CHALLENGE_COOKIE));\n }\n\n /** 403 + the proof-of-work page (HTML navigations) or 403 JSON (everything else), plus the\n * `blk: \"challenge\"` event — a served challenge is reported like a block (contract §D2). */\n private serveChallengeInner(req: CamadaRequest, res: ServerResponse, ip: string, target: string): void {\n const to = safeReturnTo(target);\n if (wantsHtml((req.headers.accept as string) ?? null, (req.headers['sec-fetch-dest'] as string) ?? null)) {\n writeChallengePage(res, { nonce: this.kit!.nonce(ip, Date.now()), action: this.challengeAction(req), to });\n } else {\n writeChallengeJson(res);\n }\n // The response is out; telemetry must never be able to undo that (a throw here would make\n // guarded() report the request as unhandled and let the app write to an ended response).\n guarded(() => {\n const qi = target.indexOf('?');\n const ev = this.buildEvent(req, qi === -1 ? target : target.slice(0, qi), qi === -1 ? '' : target.slice(qi), ip,\n { rid: randomUUID(), sid: this.sessionOf(req), newSession: false });\n ev.st = 403;\n ev.blk = 'challenge';\n this.enqueue(ev);\n }, undefined);\n }\n\n /** The request's existing session, so challenge rows join the session that produced them. */\n private sessionOf(req: IncomingMessage): string | null {\n return cookieValue((req.headers.cookie as string) || '', SESSION_COOKIE);\n }\n\n /** POST from the challenge page: validate the nonce and the proof of work, set `_cch`, 302\n * back to the (sanitised, same-site) original URL, and ship `{ st: 200, ch: 1 }`. */\n private verifyChallenge(req: CamadaRequest, res: ServerResponse, ip: string): void {\n readForm(req, (form) => guarded(() => {\n const to = safeReturnTo(form.to);\n const now = Date.now();\n if (!this.kit!.verify(ip, now, form.nonce, form.solution)) {\n writeChallengePage(res, { nonce: this.kit!.nonce(ip, now), action: this.challengeAction(req), to });\n return;\n }\n res.writeHead(302, {\n location: to,\n 'set-cookie': challengeCookie(this.kit!.issue(ip, now), isHttps(req)),\n 'cache-control': 'no-store',\n });\n res.end();\n const ev = this.buildEvent(req, this.challengeAction(req), '', ip, { rid: randomUUID(), sid: this.sessionOf(req), newSession: false });\n ev.st = 200;\n ev.ch = 1; // challenge passed (contract §A3 ingest field)\n this.enqueue(ev);\n }, undefined));\n }\n\n /**\n * Records the upgrades (WebSocket handshakes) a node:http or node:https server hands to its\n * 'upgrade' listeners: Node never routes those through the request handler the middleware runs\n * in. Each one ships one event with `st: 101` once the app's listeners have run. Observe only:\n * the request is never blocked or challenged, nothing is written to the socket, no session\n * cookie is minted, and a server with no 'upgrade' listener of its own behaves exactly as before\n * (this wraps `server.emit` rather than adding a listener, which would claim every upgrade).\n * Idempotent; returns the server. A handshake the WebSocket library then rejects still ships 101.\n */\n attach<S extends Server>(server: S): S {\n guarded(() => {\n const s = server as S & { [ATTACHED]?: true };\n if (this.disabled || !this.queue || s[ATTACHED]) return;\n s[ATTACHED] = true;\n const emit = s.emit.bind(s) as (event: string | symbol, ...args: unknown[]) => boolean;\n s.emit = ((event: string | symbol, ...args: unknown[]) => {\n if (event !== 'upgrade') return emit(event, ...args);\n const t0 = Date.now();\n try {\n return emit(event, ...args);\n } finally {\n guarded(() => this.shipUpgrade(args[0] as CamadaRequest, t0), undefined);\n }\n }) as S['emit'];\n }, undefined);\n return server;\n }\n\n private shipUpgrade(req: CamadaRequest, t0: number): void {\n if (this.disabled || !this.queue) return;\n const { path, query } = pathAndQuery(req);\n const cfg = this.snap?.config;\n if ((cfg?.exclude || []).some((x) => path.startsWith(x)) || Math.random() >= (cfg?.sample ?? 1)) return;\n const ip = this.clientIp(req);\n const ev = this.buildEvent(req, path, query, ip, { rid: randomUUID(), sid: this.sessionOf(req), newSession: false });\n ev.ts = t0;\n ev.st = 101;\n ev.dur = Date.now() - t0;\n this.enqueue(ev);\n }\n\n /** Serve the challenge for this request on demand — for a route the app wants to gate itself\n * (the example's /challenge-me). Returns false when the client already holds a valid `_cch`,\n * so the caller renders its own page. */\n serveChallenge(req: IncomingMessage, res: ServerResponse): boolean {\n return guarded(() => {\n if (this.disabled || !this.kit || !this.queue) return false;\n const ip = this.clientIp(req);\n if (!ip || this.challengePassed(req, ip)) return false; // unidentifiable client: fail open\n (req as CamadaRequest).camadaChallenged = true;\n const { path, query } = pathAndQuery(req as CamadaRequest);\n this.serveChallengeInner(req as CamadaRequest, res, ip, path + query);\n return true;\n }, false);\n }\n\n /** For HTML templates: the first-party beacon tag with the request's rid. */\n scriptTag(req: IncomingMessage): string {\n if (this.disabled || !this.beaconEnabled()) return '';\n const rid = (req as CamadaRequest).camada?.rid;\n const r = req as CamadaRequest;\n const src = this.scriptPathSet ? this.scriptPath : (r.camadaPrefix ?? mountPrefix(r)) + this.scriptPath;\n return `<script src=\"${src}${rid ? `?r=${rid}` : ''}\" async></script>`;\n }\n\n /** App-context outcome events (login failed, signup, …). The identifier is HMAC-hashed\n * in-process; the raw value never reaches the queue. */\n track(req: IncomingMessage, event: string, data?: { user?: string }): void {\n guarded(() => {\n if (this.disabled || !this.queue || !this.env) return;\n const ctx = (req as CamadaRequest).camada;\n const r = req as CamadaRequest;\n const row = (async () => {\n const uid = data?.user ? await hashUserId(data.user, this.env!.ingestToken, (globalThis.crypto ?? webcrypto).subtle) : null;\n this.enqueue({ tap: TAP_NODE, et: event, uid, rid: ctx?.rid ?? null, sid: ctx?.sid ?? null, ip: ctx?.ip ?? null, ts: Date.now() }, r);\n })().catch(logRateLimited);\n if (r.camadaOpen) (r.camadaTracks ??= []).push(row);\n }, undefined);\n }\n\n /** Express/Connect middleware (also what NestJS on the default platform consumes). */\n express() {\n return (req: IncomingMessage, res: ServerResponse, next: (err?: unknown) => void): void => {\n if (!this.handle(req, res)) next();\n };\n }\n\n /** Stops polling and the event queue. The promise settles once rows still queued behind a drain\n * in flight or a 429 hold are sent (or counted dropped); it never rejects. */\n stop(): Promise<void> {\n this.snap?.stop();\n return this.queue?.stop() ?? Promise.resolve();\n }\n}\n\nexport function createCamada(opts: CamadaOptions = {}): Camada {\n const c = new Camada(opts);\n if (!c.env) logRateLimited(new Error('CAMADA_KEY (or CAMADA_TOKEN + CAMADA_SNAPSHOT_TOKEN) not set — camada is inactive'));\n return c;\n}\n","// Environment wiring. The two-line quickstart depends on this file doing the right thing:\n// CAMADA_KEY=<ingest_token>.<snap_token> (printed by `reconcile instructions` and seed)\n// CAMADA_INGEST_URL / CAMADA_SNAPSHOT_URL (dev: http://localhost:8787[/snapshot])\n// CAMADA_DISABLED=1 kill switch, checked per request\n// CAMADA_SERVERLESS=1 lazy snapshot mode (no interval timer), flush per request\n// CAMADA_TRUSTED_PROXY local override: none | vercel | hops:N | cidrs:a,b\nimport { parseKey, parseTrustedProxyEnv, type TrustedProxyConfig } from '@camada/core';\n\nexport { parseTrustedProxyEnv }; // re-export: this was @camada/node's public surface before it moved to core\n\nexport interface ResolvedEnv {\n ingestToken: string;\n snapToken: string;\n secret: string; // HMAC key for the challenge nonce/cookie — never leaves the process\n ingestUrl: string;\n snapshotUrl: string;\n serverless: boolean;\n trustedProxy: TrustedProxyConfig | null; // null = defer to server-delivered config\n}\n\n/** Returns null (SDK stays disabled, one log line) rather than throwing on bad config. */\nexport function resolveEnv(env: Record<string, string | undefined> = process.env): ResolvedEnv | null {\n const key = parseKey(env.CAMADA_KEY);\n const ingestToken = key?.ingestToken ?? env.CAMADA_TOKEN;\n const snapToken = key?.snapToken ?? env.CAMADA_SNAPSHOT_TOKEN;\n if (!ingestToken || !snapToken) return null;\n const ingestUrl = (env.CAMADA_INGEST_URL || 'https://in.camada.app').replace(/\\/$/, '');\n return {\n ingestToken, snapToken, ingestUrl,\n secret: env.CAMADA_KEY || `${ingestToken}.${snapToken}`,\n snapshotUrl: env.CAMADA_SNAPSHOT_URL || `${ingestUrl}/snapshot`,\n serverless: env.CAMADA_SERVERLESS === '1',\n trustedProxy: parseTrustedProxyEnv(env.CAMADA_TRUSTED_PROXY),\n };\n}\n","// The challenge for @camada/node (SDK-04). node:crypto gives us a synchronous HMAC, so the\n// cookie check costs nothing on the hot path and Camada.handle() stays a synchronous boolean.\nimport type { IncomingMessage, ServerResponse } from 'node:http';\nimport { createHash, createHmac } from 'node:crypto';\nimport { createChallenge, challengePage, challengePageCsp, parseFormBody, type ChallengeKit, type ChallengePageOptions } from '@camada/core';\n\nexport const CHALLENGE_PATH = '/__camada/challenge';\nconst BODY_MAX = 4 * 1024; // the verify form is ~120 bytes; anything larger is not ours\n\nexport function nodeChallengeKit(secret: string): ChallengeKit {\n return createChallenge({\n secret,\n hmac: (s, m) => createHmac('sha256', s).update(m).digest('hex'),\n sha: (m) => createHash('sha256').update(m).digest('hex'),\n });\n}\n\n/** Reads at most BODY_MAX bytes; hands back '' when the client sends more (or errors). */\nexport function readBody(req: IncomingMessage, done: (body: string) => void): void {\n const chunks: Buffer[] = [];\n let size = 0, dead = false;\n req.on('data', (c: Buffer) => {\n if (dead) return;\n size += c.length;\n if (size > BODY_MAX) { dead = true; chunks.length = 0; return; }\n chunks.push(c);\n });\n req.on('end', () => done(dead ? '' : Buffer.concat(chunks).toString('utf8')));\n req.on('error', () => done(''));\n}\n\nconst FORM_FIELDS = ['nonce', 'solution', 'to'] as const;\n\n/** What a body parser mounted before camada left on `req.body` once it drained the stream: the\n * text of a string/Buffer body (text/raw parsers), the object of a parsed one (json, urlencoded,\n * Koa's parsers via the adapter), or null when nothing usable is left. */\nexport function parsedBody(req: IncomingMessage & { body?: unknown }): string | Record<string, unknown> | null {\n const b = req.body;\n if (typeof b === 'string') return b;\n if (Buffer.isBuffer(b)) return b.toString('utf8');\n return b && typeof b === 'object' ? (b as Record<string, unknown>) : null;\n}\n\n/** The verify POST's form fields, always delivered (never leaves the request hanging). A body\n * parser mounted before camada (express.urlencoded(), always on Nest) has already drained the\n * stream, so its `'end'` will never fire again: use what it left behind (parsedBody). Only\n * string fields count. A drained stream with nothing usable, or an oversized one, yields {},\n * which fails verification and re-serves the page at once. */\nexport function readForm(req: IncomingMessage & { body?: unknown }, done: (form: Record<string, string>) => void): void {\n if (!req.readableEnded) { readBody(req, (body) => done(parseFormBody(body))); return; }\n const b = parsedBody(req);\n if (typeof b === 'string') { done(b.length > BODY_MAX ? {} : parseFormBody(b)); return; }\n const form: Record<string, string> = Object.create(null);\n for (const k of FORM_FIELDS) {\n const v = b?.[k];\n if (typeof v === 'string') form[k] = v;\n }\n done(form);\n}\n\nexport const isHttps = (req: IncomingMessage): boolean =>\n !!(req.socket as { encrypted?: boolean } | undefined)?.encrypted || req.headers['x-forwarded-proto'] === 'https';\n\n/** Writes `challengePage(o)` with its own CSP for the same `bits` (only its inline solver and\n * style, by hash): a policy the app set earlier (helmet before camada) would otherwise ride along\n * on this response and block the solver. */\nexport function writeChallengePage(res: ServerResponse, o: ChallengePageOptions): void {\n res.removeHeader('content-security-policy');\n res.removeHeader('content-security-policy-report-only');\n res.writeHead(403, {\n 'content-type': 'text/html; charset=utf-8', 'cache-control': 'no-store', 'x-camada-challenge': '1',\n 'content-security-policy': challengePageCsp(o.bits),\n });\n res.end(challengePage(o));\n}\n\nexport function writeChallengeJson(res: ServerResponse): void {\n res.writeHead(403, { 'content-type': 'application/json', 'cache-control': 'no-store', 'x-camada-challenge': '1' });\n res.end('{\"error\":\"challenge_required\"}');\n}\n","{\n \"version\": \"0.3.2\",\n \"license\": \"MIT\",\n \"type\": \"module\",\n \"engines\": {\n \"node\": \">=20\"\n },\n \"files\": [\n \"dist\",\n \"README.md\",\n \"LICENSE\"\n ],\n \"scripts\": {\n \"build\": \"tsup\",\n \"dev\": \"tsup --watch\",\n \"test\": \"vitest run\",\n \"check\": \"tsc --noEmit\"\n },\n \"name\": \"@camada/node\",\n \"description\": \"camada backend SDK for Node: request capture, inline blocking, first-party beacon serving; Express/Fastify/Nest/Koa adapters\",\n \"exports\": {\n \".\": {\n \"import\": {\n \"types\": \"./dist/index.d.ts\",\n \"default\": \"./dist/index.js\"\n },\n \"require\": {\n \"types\": \"./dist/index.d.cts\",\n \"default\": \"./dist/index.cjs\"\n }\n },\n \"./fastify\": {\n \"import\": {\n \"types\": \"./dist/fastify.d.ts\",\n \"default\": \"./dist/fastify.js\"\n },\n \"require\": {\n \"types\": \"./dist/fastify.d.cts\",\n \"default\": \"./dist/fastify.cjs\"\n }\n },\n \"./nest\": {\n \"import\": {\n \"types\": \"./dist/nest.d.ts\",\n \"default\": \"./dist/nest.js\"\n },\n \"require\": {\n \"types\": \"./dist/nest.d.cts\",\n \"default\": \"./dist/nest.cjs\"\n }\n },\n \"./koa\": {\n \"import\": {\n \"types\": \"./dist/koa.d.ts\",\n \"default\": \"./dist/koa.js\"\n },\n \"require\": {\n \"types\": \"./dist/koa.d.cts\",\n \"default\": \"./dist/koa.cjs\"\n }\n }\n },\n \"main\": \"./dist/index.cjs\",\n \"module\": \"./dist/index.js\",\n \"types\": \"./dist/index.d.ts\",\n \"typesVersions\": {\n \"*\": {\n \"fastify\": [\n \"./dist/fastify.d.ts\"\n ],\n \"nest\": [\n \"./dist/nest.d.ts\"\n ],\n \"koa\": [\n \"./dist/koa.d.ts\"\n ]\n }\n },\n \"dependencies\": {\n \"@camada/browser\": \"file:../camada-browser\",\n \"@camada/core\": \"file:../camada-core\"\n },\n \"devDependencies\": {\n \"@koa/bodyparser\": \"^6.1.0\",\n \"@nestjs/common\": \"^11.0.0\",\n \"@nestjs/core\": \"^11.0.0\",\n \"@nestjs/platform-express\": \"^11.0.0\",\n \"@types/express\": \"^4.17.25\",\n \"@types/koa\": \"^2.15.2\",\n \"@types/koa-mount\": \"^4.0.5\",\n \"@types/node\": \"^22.10.0\",\n \"express\": \"^4.21.0\",\n \"fastify\": \"^5.2.0\",\n \"koa\": \"^2.15.0\",\n \"koa-mount\": \"^4.2.0\",\n \"reflect-metadata\": \"^0.2.2\",\n \"rxjs\": \"^7.8.1\",\n \"tsup\": \"^8.3.5\",\n \"typescript\": \"^5.7.2\",\n \"vitest\": \"^3.0.0\"\n },\n \"peerDependencies\": {\n \"@nestjs/common\": \">=10\"\n },\n \"peerDependenciesMeta\": {\n \"@nestjs/common\": {\n \"optional\": true\n }\n }\n}\n","// The SDK's wire identity (SDK-03): x-camada-sdk: <package>/<version>. Named JSON imports are\n// inlined and tree-shaken by tsup at build, so dist carries two literals — no runtime fs read.\nimport { name, version } from '../package.json';\nexport const SDK_ID = `${name}/${version}`;\n","// Quickstart (Express / Connect / Nest-on-Express):\n// import camada from '@camada/node';\n// app.use(camada.express()); // env: CAMADA_KEY (+ CAMADA_INGEST_URL for dev)\n// The default export is a lazy singleton wired from the environment on first use.\nimport type { IncomingMessage, Server, ServerResponse } from 'node:http';\nimport { Camada, createCamada, type CamadaOptions } from './camada.js';\n\nexport { Camada, createCamada, type CamadaOptions } from './camada.js';\nexport { resolveEnv, parseTrustedProxyEnv, type ResolvedEnv } from './env.js';\n\nlet singleton: Camada | null = null;\nexport function getDefault(): Camada {\n return (singleton ??= createCamada());\n}\n/** Test/reset hook: replaces the singleton (stops the old one). */\nexport function configure(opts: CamadaOptions): Camada {\n singleton?.stop();\n singleton = createCamada(opts);\n return singleton;\n}\n\nexport default {\n express: () => (req: IncomingMessage, res: ServerResponse, next: (err?: unknown) => void) => { if (!getDefault().handle(req, res)) next(); },\n handle: (req: IncomingMessage, res: ServerResponse) => getDefault().handle(req, res),\n scriptTag: (req: IncomingMessage) => getDefault().scriptTag(req),\n serveChallenge: (req: IncomingMessage, res: ServerResponse) => getDefault().serveChallenge(req, res),\n track: (req: IncomingMessage, event: string, data?: { user?: string }) => getDefault().track(req, event, data),\n attach: <S extends Server>(server: S): S => getDefault().attach(server),\n configure,\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACMA,IAAAA,sBAAsC;AACtC,IAAAC,eAIO;AACP,yBAAiB;;;ACNjB,kBAAwE;AAejE,SAAS,WAAW,MAA0C,QAAQ,KAAyB;AACpG,QAAM,UAAM,sBAAS,IAAI,UAAU;AACnC,QAAM,cAAc,KAAK,eAAe,IAAI;AAC5C,QAAM,YAAY,KAAK,aAAa,IAAI;AACxC,MAAI,CAAC,eAAe,CAAC,UAAW,QAAO;AACvC,QAAM,aAAa,IAAI,qBAAqB,yBAAyB,QAAQ,OAAO,EAAE;AACtF,SAAO;AAAA,IACL;AAAA,IAAa;AAAA,IAAW;AAAA,IACxB,QAAQ,IAAI,cAAc,GAAG,WAAW,IAAI,SAAS;AAAA,IACrD,aAAa,IAAI,uBAAuB,GAAG,SAAS;AAAA,IACpD,YAAY,IAAI,sBAAsB;AAAA,IACtC,kBAAc,kCAAqB,IAAI,oBAAoB;AAAA,EAC7D;AACF;;;AC/BA,yBAAuC;AACvC,IAAAC,eAA8H;AAEvH,IAAM,iBAAiB;AAC9B,IAAM,WAAW,IAAI;AAEd,SAAS,iBAAiB,QAA8B;AAC7D,aAAO,8BAAgB;AAAA,IACrB;AAAA,IACA,MAAM,CAAC,GAAG,UAAM,+BAAW,UAAU,CAAC,EAAE,OAAO,CAAC,EAAE,OAAO,KAAK;AAAA,IAC9D,KAAK,CAAC,UAAM,+BAAW,QAAQ,EAAE,OAAO,CAAC,EAAE,OAAO,KAAK;AAAA,EACzD,CAAC;AACH;AAGO,SAAS,SAAS,KAAsB,MAAoC;AACjF,QAAM,SAAmB,CAAC;AAC1B,MAAI,OAAO,GAAG,OAAO;AACrB,MAAI,GAAG,QAAQ,CAAC,MAAc;AAC5B,QAAI,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F,UAAI,CAAC,KAAK,OAAO,KAAK,GAAG,EAAG,MAAK;AAAA,IACnC;AAAA,EACF;AAAA;AAAA;AAAA,EAIA,OAAsB;AACpB,SAAK,MAAM,KAAK;AAChB,WAAO,KAAK,OAAO,KAAK,KAAK,QAAQ,QAAQ;AAAA,EAC/C;AACF;AAEO,SAAS,aAAa,OAAsB,CAAC,GAAW;AAC7D,QAAM,IAAI,IAAI,OAAO,IAAI;AACzB,MAAI,CAAC,EAAE,IAAK,kCAAe,IAAI,MAAM,wFAAmF,CAAC;AACzH,SAAO;AACT;;;AK3eA,IAAI,YAA2B;AACxB,SAAS,aAAqB;AACnC,SAAQ,cAAc,aAAa;AACrC;;;ANEO,SAAS,UAAU,QAAiB;AACzC,SAAO,OAAO,KAAgB,SAAgD;AAG5E,QAAI,IAAI,SAAS,IAAI,SAAS;AAC9B,QAAI,IAAI,gBAAgB,IAAI;AAC5B,SAAK,UAAU,WAAW,GAAG,OAAO,IAAI,KAAK,IAAI,GAAG,GAAG;AAAE,UAAI,UAAU;AAAO;AAAA,IAAQ;AACtF,UAAM,KAAK;AAAA,EACb;AACF;","names":["import_node_crypto","import_core","import_core","name","iife"]}
1
+ {"version":3,"sources":["../src/koa.ts","../src/camada.ts","../src/env.ts","../src/challenge.ts","../package.json","../src/version.ts","../src/index.ts"],"sourcesContent":["// Koa middleware over the raw req/res. Register first:\n// import { camadaKoa } from '@camada/node/koa';\n// app.use(camadaKoa());\nimport type { IncomingMessage, ServerResponse } from 'node:http';\nimport { Camada } from './camada.js';\nimport { getDefault } from './index.js';\n\ninterface KoaishCtx {\n req: IncomingMessage & { body?: unknown; originalUrl?: string };\n res: ServerResponse;\n request?: { body?: unknown };\n originalUrl?: string;\n respond?: boolean;\n}\n\nexport function camadaKoa(engine?: Camada) {\n return async (ctx: KoaishCtx, next: () => Promise<unknown>): Promise<void> => {\n // A body parser registered before camada (@koa/bodyparser, koa-bodyparser) leaves the form on\n // ctx.request.body, and koa-mount strips the mount from req.url: hand both to the engine.\n ctx.req.body ??= ctx.request?.body;\n ctx.req.originalUrl ??= ctx.originalUrl;\n if ((engine ?? getDefault()).handle(ctx.req, ctx.res)) { ctx.respond = false; return; } // camada owns this response\n await next();\n };\n}\n","// The engine: node:http-level request handling every adapter delegates to.\n// camada.handle(req, res) -> true when camada fully answered the request (block or beacon\n// endpoint), false when the app should proceed. Everything runs inside the fail-open\n// envelope: a camada bug must never 5xx the customer (plan.md INT-2), and CAMADA_DISABLED=1\n// bypasses the SDK entirely.\nimport type { IncomingMessage, Server, ServerResponse } from 'node:http';\nimport { randomUUID, webcrypto } from 'node:crypto'; // webcrypto handed to hashUserId explicitly, so the hash never depends on which global the host exposes\nimport {\n SnapshotClient, EventQueue, buildWireEvent, resolveClientIp, isTrustedPeer, hashUserId, guarded, logRateLimited,\n challengeCookie, safeReturnTo, wantsHtml, CHALLENGE_COOKIE,\n TAP_NODE, REQUEST_ROW_KEYS, type ChallengeKit, type SnapshotVersion, type TrustedProxyConfig, type WireEvent,\n} from '@camada/core';\nimport { isIP } from 'node:net';\nimport iife from '@camada/browser/iife-string';\nimport { resolveEnv, parseTlsxHeader, type ResolvedEnv } from './env.js';\nimport {\n CHALLENGE_PATH, nodeChallengeKit, readForm, parsedBody, isHttps, writeChallengePage,\n} from './challenge.js';\nimport { SDK_ID } from './version.js';\n\nconst SESSION_COOKIE = '_sfp'; // same cookie as the edge collector: sid/ns comparable across taps\nconst SCRIPT_PATH = '/_cam/b.js';\nconst FP_PATH = '/_cam/fp';\nconst FP_MAX = 32 * 1024; // matches the server's /fp cap: never accept what ingest will 413\n/** True for a peer address that is loopback (127.0.0.0/8, ::1, ::ffff:127.x) or not an IP at all. Node\n * normalises req.socket.remoteAddress (dotted v4, compressed lower-case v6), and the peer is not\n * attacker-spelled, so a prefix check on that string is enough; this is not a general IP parser. */\nfunction isLoopbackPeer(peer: string): boolean {\n const k = isIP(peer);\n if (k === 4) return peer.startsWith('127.');\n return k !== 6 || peer === '::1' || /^::ffff:127\\./i.test(peer);\n}\nconst TLSX_RE = /^[A-Za-z0-9+/=_-]+$/;\nconst ATTACHED = Symbol.for('camada.attached'); // one wrap per server, however often attach() is called\n\nexport interface CamadaOptions {\n env?: Record<string, string | undefined>;\n fetchImpl?: typeof fetch; // threaded into snapshot client, event queue, and the fp relay (tests)\n refreshMs?: number;\n scriptPath?: string;\n fpPath?: string;\n challenge?: boolean; // enforce `challenge` verdicts with the first-party page (default true; CAMADA_CHALLENGE=0 also switches it off)\n tlsxHeader?: string; // header carrying the TLS fingerprint (CAMADA_TLSX_HEADER); read only from a trusted-proxy peer in cidrs mode\n challengePath?: string; // where that page posts its solution (default /__camada/challenge)\n snapshotVersion?: SnapshotVersion; // 5 (default) also carries the tenant's ordered custom rules; 4 the allow/challenge sides only; 3 opts out of both\n /** With CAMADA_SERVERLESS=1: handed every per-request event flush and snapshot refresh, so the\n * platform keeps the invocation alive until they finish (e.g. `waitUntil` from `@vercel/functions`). */\n waitUntil?: (p: Promise<unknown>) => void;\n}\n\ninterface CamadaRequest extends IncomingMessage {\n originalUrl?: string; // Express/Nest: the full URL; `url` loses the mount path inside app.use('/x', …)\n body?: unknown; // what a body parser mounted before camada left behind\n camada?: { rid: string; ip: string | null; sid: string; ns: boolean; t0: number; wrn?: string }; // ns: first visit (no session cookie came in); t0: request start\n route?: { path?: string }; // Express fills this after routing\n camadaChallenged?: boolean; // serveChallenge() already shipped this request's event\n camadaSignalled?: boolean; // a non-HTML request passed under a challenge: x-camada-challenge header + `chs: 1` on its event\n camadaOpen?: boolean; // its event ships on response finish: a track() row until then rides that flush\n camadaTracks?: Promise<void>[]; // track() rows still hashing (serverless: the finish flush waits for them)\n camadaPrefix?: string; // the mount prefix seen when camada ran (scriptTag() runs later, where req.url is whole again)\n}\n\n/** The getter `header` conditions read (§D3). node:http lower-cases every incoming name and\n * the matcher always asks with a lower-cased one, so nothing has to be normalised here; a\n * header the client repeated arrives as an array and is joined the way the wire carried it. */\nconst headerReader = (req: IncomingMessage) => (name: string): string | null => {\n const v = req.headers[name];\n return v == null ? null : Array.isArray(v) ? v.join(', ') : v;\n};\n\nconst splitUrl = (raw: string): { path: string; query: string } => {\n const qi = raw.indexOf('?');\n return qi === -1 ? { path: raw, query: '' } : { path: raw.slice(0, qi), query: raw.slice(qi) };\n};\n\nconst isPage = (req: IncomingMessage): boolean =>\n wantsHtml((req.headers.accept as string) ?? null, (req.headers['sec-fetch-dest'] as string) ?? null);\n\n/** Path and query as the client sent them: what rules, the challenge list and events see. Express\n * strips the mount path from `req.url` inside `app.use(path, mw)` (Nest mounts module middleware\n * that way, at '{*splat}' or '*'), and koa-mount does the same (the Koa adapter copies Koa's\n * `originalUrl` over), so read `originalUrl` first; plain node:http and Fastify only have `url`. */\nfunction pathAndQuery(req: CamadaRequest): { path: string; query: string } {\n return splitUrl(req.originalUrl || req.url || '/');\n}\n\n/** Whether the request is for one of camada's own endpoints (beacon script, beacon POST,\n * challenge verify). Those answer at the mount-relative path (`req.url`), so `app.use('/api',\n * camada)` serves `/api/_cam/b.js`, and at the full path, so a wildcard mount (Nest, whose\n * `req.url` is stripped to '/') still serves `/_cam/b.js`. */\nconst atEndpoint = (req: CamadaRequest, path: string, endpoint: string): boolean =>\n path === endpoint || splitUrl(req.url || '/').path === endpoint;\n\n/** The mount prefix camada runs under: the full path minus the mount-relative one, when the full\n * path ends with it (`app.use('/api', …)`, koa-mount). A relative path of `/` says nothing: it is\n * both the mount's own root and what a wildcard mount (Nest) strips every path to, so no prefix. */\nfunction mountPrefix(req: CamadaRequest): string {\n if (!req.originalUrl) return '';\n const full = splitUrl(req.originalUrl).path, rel = splitUrl(req.url || '/').path;\n return rel !== '/' && full !== rel && full.endsWith(rel) ? full.slice(0, full.length - rel.length) : '';\n}\n\n/** A unix-socket (pipe) listener: Node never has a peer address there, on any request. The\n * listening server reports its address as the socket path (a string); `_pipeName` keeps the\n * answer after server.close() has dropped the handle while connections drain. */\nfunction onUnixListener(socket: unknown): boolean {\n const srv = (socket as { server?: { address?: () => unknown; _pipeName?: unknown } } | null)?.server;\n if (!srv) return false;\n try { if (typeof srv.address?.() === 'string') return true; } catch { /* not a net.Server */ }\n return typeof srv._pipeName === 'string';\n}\n\nconst cookieValue = (cookie: string, name: string): string | null => {\n const src = '; ' + cookie;\n const i = src.indexOf('; ' + name + '=');\n if (i === -1) return null;\n const start = i + name.length + 3;\n const j = src.indexOf(';', start);\n return src.slice(start, j === -1 ? undefined : j);\n};\n\n/** Sets a first visit's `_sfp` and keeps it there: an app that replaces Set-Cookie through\n * setHeader (or writeHead, setHeaders, which route through it once any header is set) gets its\n * own cookies plus the session's, unless it set `_sfp` itself. appendHeader already keeps it. */\nfunction keepSessionCookie(res: ServerResponse, cookie: string): void {\n const set = res.setHeader;\n res.setHeader = function (this: ServerResponse, name: string, value: number | string | readonly string[]) {\n if (typeof name === 'string' && name.toLowerCase() === 'set-cookie') {\n const list = Array.isArray(value) ? value : [String(value)];\n if (!list.some((c) => String(c).trimStart().startsWith(SESSION_COOKIE + '='))) value = [...list, cookie];\n }\n return set.call(this, name, value);\n } as ServerResponse['setHeader'];\n res.setHeader('set-cookie', cookie);\n}\n\n// CDNs read their own cache header ahead of cache-control, so all of these end up no-store.\nconst NO_STORE_HEADERS = ['cache-control', 'cdn-cache-control', 'surrogate-control', 'cloudflare-cdn-cache-control', 'vercel-cdn-cache-control', 'netlify-cdn-cache-control'];\n\n/** Marks a signalled response: `x-camada-challenge: 1` and no-store on every NO_STORE_HEADERS name. The header is\n * per client, so no shared cache may keep the copy, whatever the app sets. Every flush (an explicit\n * writeHead, http1's `_implicitHeader`, http2 compat's end/write/flushHeaders) goes through\n * `this.writeHead`, so that is the one choke point: it drops those names from the headers it is\n * given (object, flat array, tuple array) and re-sets no-store with the original setHeader. */\nfunction signal(res: ServerResponse): void {\n const set = res.setHeader, writeHead = res.writeHead;\n const isNoStore = (n: unknown) => typeof n === 'string' && NO_STORE_HEADERS.includes(n.toLowerCase());\n const strip = (h: unknown): unknown => {\n if (Array.isArray(h)) {\n if (h.length && Array.isArray(h[0])) return h.filter((p) => !isNoStore(p[0]));\n const out: unknown[] = [];\n for (let i = 0; i < h.length; i += 2) if (!isNoStore(h[i])) out.push(h[i], h[i + 1]);\n return out;\n }\n if (h && typeof h === 'object') return Object.fromEntries(Object.entries(h).filter(([k]) => !isNoStore(k)));\n return h;\n };\n res.writeHead = function (this: ServerResponse, status: number, ...rest: unknown[]) {\n if (!this.headersSent) for (const h of NO_STORE_HEADERS) set.call(this, h, 'no-store');\n return (writeHead as (...a: unknown[]) => ServerResponse).call(this, status, ...rest.map(strip));\n } as ServerResponse['writeHead'];\n res.setHeader('x-camada-challenge', '1');\n for (const h of NO_STORE_HEADERS) res.setHeader(h, 'no-store');\n}\n\nexport class Camada {\n readonly env: ResolvedEnv | null;\n readonly snap: SnapshotClient | null = null;\n readonly queue: EventQueue | null = null;\n private readonly fetchImpl: typeof fetch;\n private readonly scriptPath: string;\n private readonly fpPath: string;\n private readonly challengeOn: boolean;\n private readonly challengePath: string;\n private readonly challengePathSet: boolean; // configured explicitly: used as given, never prefixed\n private readonly scriptPathSet: boolean;\n private readonly kit: ChallengeKit | null = null;\n private readonly envSource: Record<string, string | undefined>;\n private readonly tlsxHeader: string | null;\n private readonly waitUntil?: (p: Promise<unknown>) => void;\n\n constructor(opts: CamadaOptions = {}) {\n this.envSource = opts.env ?? process.env;\n this.fetchImpl = opts.fetchImpl ?? fetch;\n this.waitUntil = opts.waitUntil;\n this.scriptPath = opts.scriptPath ?? SCRIPT_PATH;\n this.fpPath = opts.fpPath ?? FP_PATH;\n this.challengeOn = opts.challenge !== false && this.envSource.CAMADA_CHALLENGE !== '0';\n this.challengePath = opts.challengePath ?? CHALLENGE_PATH;\n this.challengePathSet = opts.challengePath != null;\n this.scriptPathSet = opts.scriptPath != null;\n this.env = resolveEnv(this.envSource);\n this.tlsxHeader = parseTlsxHeader(opts.tlsxHeader) ?? this.env?.tlsxHeader ?? null;\n if (!this.env) return; // unconfigured: every entry point no-ops\n if (this.envSource.CAMADA_DISABLED === '1') return; // killed at boot: no poll timer, no exit hooks, truly silent\n this.snap = new SnapshotClient({\n url: this.env.snapshotUrl, token: this.env.snapToken,\n mode: this.env.serverless ? 'lazy' : 'timer',\n refreshMs: opts.refreshMs, snapshotVersion: opts.snapshotVersion, fetchImpl: this.fetchImpl, sdk: SDK_ID,\n });\n this.queue = new EventQueue({ url: this.env.ingestUrl, token: this.env.ingestToken, fetchImpl: this.fetchImpl, sdk: SDK_ID });\n this.kit = nodeChallengeKit(this.env.secret);\n this.snap.start();\n this.queue.installNodeExitFlush();\n }\n\n get disabled(): boolean { return this.envSource.CAMADA_DISABLED === '1' || !this.env; }\n\n private trustedProxy(): TrustedProxyConfig | null {\n return this.env?.trustedProxy ?? this.snap?.config?.trusted_proxy ?? null; // explicit local override wins\n }\n\n /** The client IP per the trusted-proxy config (team contract C6 v3). No socket at all, or a\n * unix-socket listener (nginx/Passenger in front, where Node never has a peer): core's no-peer\n * X-Forwarded-For walk, which trusts the local proxy. A TCP socket with no readable peer (the\n * client reset the connection before camada ran, e.g. behind an async middleware) is\n * unidentifiable: the walk would believe whatever the client wrote. So: null, the stance every\n * ip-bound feature already takes (no ip rules, no challenge). */\n private clientIp(req: IncomingMessage): string | null {\n const peer = req.socket?.remoteAddress;\n if (!peer && req.socket && !onUnixListener(req.socket)) return null;\n return resolveClientIp(peer ?? null, req.headers['x-forwarded-for'] as string | undefined, this.trustedProxy());\n }\n\n /** The TLS fingerprint Cloudflare (a Transform Rule) put in the opt-in header, or null. Trusted only\n * in `cidrs` mode and only when the socket peer itself is a NON-loopback address core's cidrs walk\n * trusts, i.e. Cloudflare connects to the app directly. A loopback peer (a local reverse proxy or\n * cloudflared) is never trusted and X-Forwarded-For is never consulted, so the header is not read\n * and tlsx conditions fail open. Trust is core's isTrustedPeer, the same first-hop test the cidrs\n * walk uses. No readable peer (unix listener included) -> null, since core's no-peer walk would\n * trust everyone. */\n private tlsxOf(req: IncomingMessage): string | null {\n const header = this.tlsxHeader;\n if (!header) return null;\n const v = headerReader(req)(header); // a repeated header joins with ', ', which the charset rejects\n if (v == null || v.length > 128 || !TLSX_RE.test(v)) return null;\n const cfg = this.trustedProxy();\n const peer = req.socket?.remoteAddress;\n if (cfg?.mode !== 'cidrs' || !peer) return null;\n if (isLoopbackPeer(peer)) return null;\n return isTrustedPeer(peer, cfg) ? v : null;\n }\n\n /** Where the challenge form posts: under the mount prefix, so it reaches camada, unless the\n * path was configured explicitly. */\n private challengeAction(req: CamadaRequest): string {\n return this.challengePathSet ? this.challengePath : (req.camadaPrefix ?? mountPrefix(req)) + this.challengePath;\n }\n\n private beaconEnabled(): boolean { return this.snap?.config?.beacon !== false; }\n\n /** Queues a row. With CAMADA_SERVERLESS=1 it also starts the POST now: the platform freezes the\n * process once the response is out, so the 15 s interval would never run. Never throws. */\n private enqueue(row: unknown, req?: CamadaRequest): void {\n const q = this.queue!;\n q.push(row);\n if (!this.env?.serverless) return;\n if (req?.camadaOpen) return; // a track() row while the response is open: the finish flush carries it\n this.flushFor(req);\n }\n\n /** Serverless: start the POST now and hand it to waitUntil. Never throws. */\n private flushFor(req?: CamadaRequest): void {\n const q = this.queue!;\n guarded(() => {\n // One POST per request: the response-finish flush first waits for that request's track()\n // rows still hashing. flush() is single-in-flight, so a row queued while a POST was\n // finishing rides a second one.\n const tracks = req?.camadaTracks ?? [];\n const p = Promise.allSettled(tracks)\n .then(() => q.flush())\n .then(() => (q.size > 0 ? q.flush() : undefined));\n this.waitUntil?.(p);\n }, undefined);\n }\n\n /** True when camada fully handled the request. Synchronous; never throws. */\n handle(req: IncomingMessage, res: ServerResponse): boolean {\n return guarded(() => this.handleInner(req as CamadaRequest, res), false);\n }\n\n private handleInner(req: CamadaRequest, res: ServerResponse): boolean {\n if (this.disabled || !this.snap || !this.queue || !this.env) return false;\n const t0 = Date.now();\n this.snap.ensureFresh(this.env.serverless ? this.waitUntil : undefined);\n\n const { path, query } = pathAndQuery(req);\n const ip = this.clientIp(req);\n req.camadaPrefix = mountPrefix(req);\n\n // enforce before anything else, beacon endpoints included — fail open while cold. The custom\n // rules read the user agent and the request headers (§D3); without them every `ua` and\n // `header` condition is false.\n const v = this.snap.verdict({ ip, path, ua: req.headers['user-agent'], header: headerReader(req), tlsx: this.tlsxOf(req) });\n if (v.block) {\n const headers: Record<string, string> = { 'x-block-reason': v.reason ?? '', 'x-block-version': v.version ?? '', 'content-type': 'text/plain', 'cache-control': 'no-store' };\n if (v.rule) headers['x-block-rule'] = v.rule; // a custom rule blocked: name it, so the customer knows which row to edit\n res.writeHead(403, headers);\n res.end('Forbidden');\n const ev = this.buildEvent(req, path, query, ip, { rid: randomUUID(), sid: null, newSession: false });\n ev.st = 403; // blocked requests always ship: silent expiry makes blocks oscillate\n ev.blk = v.reason; // SDK-01: the reason rides the event so the analyst counts SDK blocks, not the app's own 403s ('rule' when a rule decided)\n if (v.rule) ev.rl = v.rule;\n this.enqueue(ev);\n return true;\n }\n // `warn` passes the request and only marks its event (below, on response-finish); a skip\n // passes with nothing stamped at all — it is the absence of enforcement.\n\n // A challenge needs a resolved client IP: the nonce and the `_cch` cookie are bound to it,\n // so without one a single solve would mint a cookie every unidentified client could\n // present. No ip -> no challenge (fail open), the same stance ip rules take.\n if (this.challengeOn && this.kit && ip) {\n // The verify endpoint answers first: a challenged client must be able to reach it.\n if (req.method === 'POST' && atEndpoint(req, path, this.challengePath)) { this.verifyChallenge(req, res, ip); return true; }\n if (v.challenge && !this.challengePassed(req, ip)) {\n // A page load gets the challenge; anything else (fetch/XHR/API) passes to the app, flagged.\n if (isPage(req)) { this.serveChallengeInner(req, res, ip, path + query); return true; }\n req.camadaSignalled = true;\n }\n }\n\n if (this.beaconEnabled()) {\n if (req.method === 'GET' && atEndpoint(req, path, this.scriptPath)) {\n res.writeHead(200, { 'content-type': 'application/javascript', 'cache-control': 'public, max-age=3600' });\n res.end(iife);\n return true;\n }\n if (req.method === 'POST' && atEndpoint(req, path, this.fpPath)) {\n this.relayBeacon(req, res, ip);\n return true;\n }\n }\n\n const { rid, sid, ns: newSession } = this.begin(req, res, ip, t0, v.warn ? v.rule : undefined);\n if (req.camadaSignalled) signal(res);\n\n const cfg = this.snap.config;\n const excluded = (cfg?.exclude || []).some((x) => path.startsWith(x));\n // A signalled request ships regardless of sample and exclude, like the 403 row it replaces.\n if (req.camadaSignalled || (!excluded && Math.random() < (cfg?.sample ?? 1))) {\n this.hookCapture(req, res, path, query, ip, { rid, sid, newSession, t0 });\n }\n return false;\n }\n\n /** Mints the request's rid and session (setting the cookie on a first visit), records them on\n * `req.camada` and sets `x-rid`. handle() and serveChallenge() (when handle() did not run) share it. */\n private begin(req: CamadaRequest, res: ServerResponse, ip: string | null, t0: number, wrn?: string): NonNullable<CamadaRequest['camada']> {\n const rid = randomUUID();\n let sid = cookieValue((req.headers.cookie as string) || '', SESSION_COOKIE);\n const ns = !sid;\n if (!sid) {\n sid = randomUUID();\n const https = (req.socket as { encrypted?: boolean }).encrypted || req.headers['x-forwarded-proto'] === 'https';\n keepSessionCookie(res, `${SESSION_COOKIE}=${sid}; Path=/; Max-Age=2592000; HttpOnly; SameSite=Lax${https ? '; Secure' : ''}`);\n }\n res.setHeader('x-rid', rid);\n return (req.camada = { rid, ip, sid, ns, t0, wrn });\n }\n\n /** Installs the response-finish hook that ships the request's normal capture event. */\n private hookCapture(req: CamadaRequest, res: ServerResponse, path: string, query: string, ip: string | null,\n o: { rid: string; sid: string | null; newSession: boolean; t0: number }): void {\n // 'finish' when the response went out whole; 'close' alone when the client left first (an\n // aborted SSE or download), which ships the status set so far and dur up to the abort — the\n // fetch adapters' cancel semantics. 'close' also follows every 'finish': one event either way.\n let shipped = false;\n req.camadaOpen = true;\n const ship = () => guarded(() => {\n req.camadaOpen = false;\n if (shipped) return;\n shipped = true;\n // serveChallenge() may have answered from inside the app, and it already shipped the\n // `blk: \"challenge\"` row — one request, one event. A track() row queued while the\n // response was open still needs this finish flush (serverless).\n if (req.camadaChallenged) { if (this.env?.serverless) this.flushFor(req); return; }\n const ev = this.buildEvent(req, path, query, ip, o);\n ev.ts = o.t0; // the request start: the timeline draws [ts, ts + dur]\n ev.st = res.statusCode;\n ev.dur = Date.now() - o.t0;\n if (req.route?.path) ev.rt = String(req.route.path);\n if (req.camada?.wrn) ev.wrn = req.camada.wrn; // §D3: the warn rule that let this request through\n if (req.camadaSignalled) ev.chs = 1; // challenge signalled (camada-all-82ar)\n this.enqueue(ev, req);\n }, undefined);\n res.once('finish', ship);\n res.once('close', ship);\n }\n\n private buildEvent(req: CamadaRequest, path: string, query: string, ip: string | null, o: { rid: string; sid: string | null; newSession: boolean }): WireEvent {\n const raw = req.rawHeaders || [];\n const headers: Array<[string, string]> = [];\n for (let i = 0; i + 1 < raw.length; i += 2) headers.push([raw[i], raw[i + 1]]);\n const ev = buildWireEvent(\n { method: req.method || 'GET', host: (req.headers.host as string) || '', path, query, headers, ip, httpVersion: req.httpVersion },\n { tap: TAP_NODE, rid: o.rid, sid: o.sid, newSession: o.newSession },\n );\n const tlsx = this.tlsxOf(req); // same trusted-header gate as matching (camada-all-bcsf)\n if (tlsx) ev.tlsx = tlsx;\n return ev;\n }\n\n /** Reads the beacon POST (≤32 KB), answers 204 immediately, and queues the beacon as a `sig: 1`\n * row with the trusted-proxy-resolved client IP: it rides the next event batch, so the analyst\n * sees one request per flush instead of one per page view. Junk bodies are dropped, never shipped.\n * A body parser mounted before camada (express.json(), always on Nest) has already drained the\n * stream: its `req.body` is used instead, and the request is answered either way. */\n private relayBeacon(req: CamadaRequest, res: ServerResponse, ip: string | null): void {\n const accept = (parsed: unknown) => {\n res.writeHead(204, { 'cache-control': 'no-store' });\n res.end();\n if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed) || Buffer.isBuffer(parsed)) return;\n const row: Record<string, unknown> = { ...(parsed as Record<string, unknown>), sig: 1, ip, tap: TAP_NODE, rts: Date.now() };\n for (const k of REQUEST_ROW_KEYS) delete row[k];\n this.enqueue(row);\n };\n const fromText = (text: string) => { try { return JSON.parse(text) as unknown; } catch { return null; } };\n if (req.readableEnded) {\n const b = parsedBody(req);\n if ((typeof b === 'string' ? b : b ? JSON.stringify(b) : '').length > FP_MAX) { res.writeHead(413).end(); return; }\n accept(typeof b === 'string' ? fromText(b) : b);\n return;\n }\n const chunks: Buffer[] = [];\n let size = 0, dead = false;\n req.on('data', (c: Buffer) => {\n size += c.length;\n if (size > FP_MAX) { dead = true; req.removeAllListeners('data'); res.writeHead(413).end(); return; }\n chunks.push(c);\n });\n req.on('end', () => guarded(() => {\n if (dead) return;\n accept(fromText(Buffer.concat(chunks).toString('utf8')));\n }, undefined));\n req.on('error', () => { try { res.destroy(); } catch { /* already gone */ } });\n }\n\n private challengePassed(req: IncomingMessage, ip: string | null): boolean {\n return !!this.kit?.tokenValid(ip, Date.now(), cookieValue((req.headers.cookie as string) || '', CHALLENGE_COOKIE));\n }\n\n /** 403 + the proof-of-work page (HTML navigations only), plus the\n * `blk: \"challenge\"` event — a served challenge is reported like a block (contract §D2). */\n private serveChallengeInner(req: CamadaRequest, res: ServerResponse, ip: string, target: string): void {\n const to = safeReturnTo(target);\n writeChallengePage(res, { nonce: this.kit!.nonce(ip, Date.now()), action: this.challengeAction(req), to });\n // The response is out; telemetry must never be able to undo that (a throw here would make\n // guarded() report the request as unhandled and let the app write to an ended response).\n guarded(() => {\n const qi = target.indexOf('?');\n const ev = this.buildEvent(req, qi === -1 ? target : target.slice(0, qi), qi === -1 ? '' : target.slice(qi), ip,\n { rid: randomUUID(), sid: this.sessionOf(req), newSession: false });\n ev.st = 403;\n ev.blk = 'challenge';\n this.enqueue(ev);\n }, undefined);\n }\n\n /** The request's existing session, so challenge rows join the session that produced them. */\n private sessionOf(req: IncomingMessage): string | null {\n return cookieValue((req.headers.cookie as string) || '', SESSION_COOKIE);\n }\n\n /** POST from the challenge page: validate the nonce and the proof of work, set `_cch`, 302\n * back to the (sanitised, same-site) original URL, and ship `{ st: 200, ch: 1 }`. */\n private verifyChallenge(req: CamadaRequest, res: ServerResponse, ip: string): void {\n readForm(req, (form) => guarded(() => {\n const to = safeReturnTo(form.to);\n const now = Date.now();\n if (!this.kit!.verify(ip, now, form.nonce, form.solution)) {\n writeChallengePage(res, { nonce: this.kit!.nonce(ip, now), action: this.challengeAction(req), to });\n return;\n }\n res.writeHead(302, {\n location: to,\n 'set-cookie': challengeCookie(this.kit!.issue(ip, now), isHttps(req)),\n 'cache-control': 'no-store',\n });\n res.end();\n const ev = this.buildEvent(req, this.challengeAction(req), '', ip, { rid: randomUUID(), sid: this.sessionOf(req), newSession: false });\n ev.st = 200;\n ev.ch = 1; // challenge passed (contract §A3 ingest field)\n this.enqueue(ev);\n }, undefined));\n }\n\n /**\n * Records the upgrades (WebSocket handshakes) a node:http or node:https server hands to its\n * 'upgrade' listeners: Node never routes those through the request handler the middleware runs\n * in. Each one ships one event with `st: 101` once the app's listeners have run. Observe only:\n * the request is never blocked or challenged, nothing is written to the socket, no session\n * cookie is minted, and a server with no 'upgrade' listener of its own behaves exactly as before\n * (this wraps `server.emit` rather than adding a listener, which would claim every upgrade).\n * Idempotent; returns the server. A handshake the WebSocket library then rejects still ships 101.\n */\n attach<S extends Server>(server: S): S {\n guarded(() => {\n const s = server as S & { [ATTACHED]?: true };\n if (this.disabled || !this.queue || s[ATTACHED]) return;\n s[ATTACHED] = true;\n const emit = s.emit.bind(s) as (event: string | symbol, ...args: unknown[]) => boolean;\n s.emit = ((event: string | symbol, ...args: unknown[]) => {\n if (event !== 'upgrade') return emit(event, ...args);\n const t0 = Date.now();\n try {\n return emit(event, ...args);\n } finally {\n guarded(() => this.shipUpgrade(args[0] as CamadaRequest, t0), undefined);\n }\n }) as S['emit'];\n }, undefined);\n return server;\n }\n\n private shipUpgrade(req: CamadaRequest, t0: number): void {\n if (this.disabled || !this.queue) return;\n const { path, query } = pathAndQuery(req);\n const cfg = this.snap?.config;\n if ((cfg?.exclude || []).some((x) => path.startsWith(x)) || Math.random() >= (cfg?.sample ?? 1)) return;\n const ip = this.clientIp(req);\n const ev = this.buildEvent(req, path, query, ip, { rid: randomUUID(), sid: this.sessionOf(req), newSession: false });\n ev.ts = t0;\n ev.st = 101;\n ev.dur = Date.now() - t0;\n this.enqueue(ev);\n }\n\n /** Serve the challenge for this request on demand — for a route the app wants to gate itself\n * (the example's /challenge-me). Returns false when the client already holds a valid `_cch`,\n * so the caller renders its own page. A non-HTML request is never challenged: it gets the\n * `x-camada-challenge: 1` header and a `chs: 1` event, and false, so the app answers. */\n serveChallenge(req: IncomingMessage, res: ServerResponse): boolean {\n return guarded(() => {\n if (this.disabled || !this.kit || !this.queue) return false;\n const ip = this.clientIp(req);\n if (!ip || this.challengePassed(req, ip)) return false; // unidentifiable client: fail open\n const r = req as CamadaRequest;\n const { path, query } = pathAndQuery(r);\n if (!isPage(r)) {\n // Not a page: the app renders, flagged. Its normal event ships with chs: 1.\n if (!r.camadaSignalled) signal(res); // handle() may have done it already\n r.camadaSignalled = true;\n if (r.camadaOpen === undefined) {\n const c = r.camada ?? this.begin(r, res, ip, Date.now()); // handle() ran (sampled out): its ids; else mint them as it would\n this.hookCapture(r, res, path, query, ip, { rid: c.rid, sid: c.sid, newSession: c.ns, t0: c.t0 });\n }\n return false;\n }\n r.camadaChallenged = true;\n this.serveChallengeInner(req as CamadaRequest, res, ip, path + query);\n return true;\n }, false);\n }\n\n /** For HTML templates: the first-party beacon tag with the request's rid. */\n scriptTag(req: IncomingMessage): string {\n if (this.disabled || !this.beaconEnabled()) return '';\n const rid = (req as CamadaRequest).camada?.rid;\n const r = req as CamadaRequest;\n const src = this.scriptPathSet ? this.scriptPath : (r.camadaPrefix ?? mountPrefix(r)) + this.scriptPath;\n return `<script src=\"${src}${rid ? `?r=${rid}` : ''}\" async></script>`;\n }\n\n /** App-context outcome events (login failed, signup, …). The identifier is HMAC-hashed\n * in-process; the raw value never reaches the queue. */\n track(req: IncomingMessage, event: string, data?: { user?: string }): void {\n guarded(() => {\n if (this.disabled || !this.queue || !this.env) return;\n const ctx = (req as CamadaRequest).camada;\n const r = req as CamadaRequest;\n const row = (async () => {\n const uid = data?.user ? await hashUserId(data.user, this.env!.ingestToken, (globalThis.crypto ?? webcrypto).subtle) : null;\n this.enqueue({ tap: TAP_NODE, et: event, uid, rid: ctx?.rid ?? null, sid: ctx?.sid ?? null, ip: ctx?.ip ?? null, ts: Date.now() }, r);\n })().catch(logRateLimited);\n if (r.camadaOpen) (r.camadaTracks ??= []).push(row);\n }, undefined);\n }\n\n /** Express/Connect middleware (also what NestJS on the default platform consumes). */\n express() {\n return (req: IncomingMessage, res: ServerResponse, next: (err?: unknown) => void): void => {\n if (!this.handle(req, res)) next();\n };\n }\n\n /** Stops polling and the event queue. The promise settles once rows still queued behind a drain\n * in flight or a 429 hold are sent (or counted dropped); it never rejects. */\n stop(): Promise<void> {\n this.snap?.stop();\n return this.queue?.stop() ?? Promise.resolve();\n }\n}\n\nexport function createCamada(opts: CamadaOptions = {}): Camada {\n const c = new Camada(opts);\n if (!c.env) logRateLimited(new Error('CAMADA_KEY (or CAMADA_TOKEN + CAMADA_SNAPSHOT_TOKEN) not set — camada is inactive'));\n return c;\n}\n","// Environment wiring. The two-line quickstart depends on this file doing the right thing:\n// CAMADA_KEY=<ingest_token>.<snap_token> (printed by `reconcile instructions` and seed)\n// CAMADA_INGEST_URL / CAMADA_SNAPSHOT_URL (dev: http://localhost:8787[/snapshot])\n// CAMADA_DISABLED=1 kill switch, checked per request\n// CAMADA_SERVERLESS=1 lazy snapshot mode (no interval timer), flush per request\n// CAMADA_TLSX_HEADER opt-in: header carrying the TLS fingerprint (trusted-proxy cidrs mode only)\n// CAMADA_TRUSTED_PROXY local override: none | vercel | hops:N | cidrs:a,b\nimport { parseKey, parseTrustedProxyEnv, type TrustedProxyConfig } from '@camada/core';\n\nexport { parseTrustedProxyEnv }; // re-export: this was @camada/node's public surface before it moved to core\n\nexport interface ResolvedEnv {\n ingestToken: string;\n snapToken: string;\n secret: string; // HMAC key for the challenge nonce/cookie — never leaves the process\n ingestUrl: string;\n snapshotUrl: string;\n serverless: boolean;\n trustedProxy: TrustedProxyConfig | null; // null = defer to server-delivered config\n tlsxHeader: string | null; // lower-cased header name, null = tlsx never matches here\n}\n\n/** A usable header name (RFC 9110 token), lower-cased; anything else is \"off\". */\nexport function parseTlsxHeader(raw: string | undefined | null): string | null {\n const n = (raw ?? '').trim().toLowerCase();\n return /^[a-z0-9!#$%&'*+.^_`|~-]+$/.test(n) ? n : null;\n}\n\n/** Returns null (SDK stays disabled, one log line) rather than throwing on bad config. */\nexport function resolveEnv(env: Record<string, string | undefined> = process.env): ResolvedEnv | null {\n const key = parseKey(env.CAMADA_KEY);\n const ingestToken = key?.ingestToken ?? env.CAMADA_TOKEN;\n const snapToken = key?.snapToken ?? env.CAMADA_SNAPSHOT_TOKEN;\n if (!ingestToken || !snapToken) return null;\n const ingestUrl = (env.CAMADA_INGEST_URL || 'https://in.camada.app').replace(/\\/$/, '');\n return {\n ingestToken, snapToken, ingestUrl,\n secret: env.CAMADA_KEY || `${ingestToken}.${snapToken}`,\n snapshotUrl: env.CAMADA_SNAPSHOT_URL || `${ingestUrl}/snapshot`,\n serverless: env.CAMADA_SERVERLESS === '1',\n trustedProxy: parseTrustedProxyEnv(env.CAMADA_TRUSTED_PROXY),\n tlsxHeader: parseTlsxHeader(env.CAMADA_TLSX_HEADER),\n };\n}\n","// The challenge for @camada/node (SDK-04). node:crypto gives us a synchronous HMAC, so the\n// cookie check costs nothing on the hot path and Camada.handle() stays a synchronous boolean.\nimport type { IncomingMessage, ServerResponse } from 'node:http';\nimport { createHash, createHmac } from 'node:crypto';\nimport { createChallenge, challengePage, challengePageCsp, parseFormBody, type ChallengeKit, type ChallengePageOptions } from '@camada/core';\n\nexport const CHALLENGE_PATH = '/__camada/challenge';\nconst BODY_MAX = 4 * 1024; // the verify form is ~120 bytes; anything larger is not ours\n\nexport function nodeChallengeKit(secret: string): ChallengeKit {\n return createChallenge({\n secret,\n hmac: (s, m) => createHmac('sha256', s).update(m).digest('hex'),\n sha: (m) => createHash('sha256').update(m).digest('hex'),\n });\n}\n\n/** Reads at most BODY_MAX bytes; hands back '' when the client sends more (or errors). */\nexport function readBody(req: IncomingMessage, done: (body: string) => void): void {\n const chunks: Buffer[] = [];\n let size = 0, dead = false;\n req.on('data', (c: Buffer) => {\n if (dead) return;\n size += c.length;\n if (size > BODY_MAX) { dead = true; chunks.length = 0; return; }\n chunks.push(c);\n });\n req.on('end', () => done(dead ? '' : Buffer.concat(chunks).toString('utf8')));\n req.on('error', () => done(''));\n}\n\nconst FORM_FIELDS = ['nonce', 'solution', 'to'] as const;\n\n/** What a body parser mounted before camada left on `req.body` once it drained the stream: the\n * text of a string/Buffer body (text/raw parsers), the object of a parsed one (json, urlencoded,\n * Koa's parsers via the adapter), or null when nothing usable is left. */\nexport function parsedBody(req: IncomingMessage & { body?: unknown }): string | Record<string, unknown> | null {\n const b = req.body;\n if (typeof b === 'string') return b;\n if (Buffer.isBuffer(b)) return b.toString('utf8');\n return b && typeof b === 'object' ? (b as Record<string, unknown>) : null;\n}\n\n/** The verify POST's form fields, always delivered (never leaves the request hanging). A body\n * parser mounted before camada (express.urlencoded(), always on Nest) has already drained the\n * stream, so its `'end'` will never fire again: use what it left behind (parsedBody). Only\n * string fields count. A drained stream with nothing usable, or an oversized one, yields {},\n * which fails verification and re-serves the page at once. */\nexport function readForm(req: IncomingMessage & { body?: unknown }, done: (form: Record<string, string>) => void): void {\n if (!req.readableEnded) { readBody(req, (body) => done(parseFormBody(body))); return; }\n const b = parsedBody(req);\n if (typeof b === 'string') { done(b.length > BODY_MAX ? {} : parseFormBody(b)); return; }\n const form: Record<string, string> = Object.create(null);\n for (const k of FORM_FIELDS) {\n const v = b?.[k];\n if (typeof v === 'string') form[k] = v;\n }\n done(form);\n}\n\nexport const isHttps = (req: IncomingMessage): boolean =>\n !!(req.socket as { encrypted?: boolean } | undefined)?.encrypted || req.headers['x-forwarded-proto'] === 'https';\n\n/** Writes `challengePage(o)` with its own CSP for the same `bits` (only its inline solver and\n * style, by hash): a policy the app set earlier (helmet before camada) would otherwise ride along\n * on this response and block the solver. */\nexport function writeChallengePage(res: ServerResponse, o: ChallengePageOptions): void {\n res.removeHeader('content-security-policy');\n res.removeHeader('content-security-policy-report-only');\n res.writeHead(403, {\n 'content-type': 'text/html; charset=utf-8', 'cache-control': 'no-store', 'x-camada-challenge': '1',\n 'content-security-policy': challengePageCsp(o.bits),\n });\n res.end(challengePage(o));\n}\n","{\n \"version\": \"0.4.0\",\n \"license\": \"MIT\",\n \"type\": \"module\",\n \"engines\": {\n \"node\": \">=20\"\n },\n \"files\": [\n \"dist\",\n \"README.md\",\n \"LICENSE\"\n ],\n \"scripts\": {\n \"build\": \"tsup\",\n \"dev\": \"tsup --watch\",\n \"test\": \"vitest run\",\n \"check\": \"tsc --noEmit\"\n },\n \"name\": \"@camada/node\",\n \"description\": \"camada backend SDK for Node: request capture, inline blocking, first-party beacon serving; Express/Fastify/Nest/Koa adapters\",\n \"exports\": {\n \".\": {\n \"import\": {\n \"types\": \"./dist/index.d.ts\",\n \"default\": \"./dist/index.js\"\n },\n \"require\": {\n \"types\": \"./dist/index.d.cts\",\n \"default\": \"./dist/index.cjs\"\n }\n },\n \"./fastify\": {\n \"import\": {\n \"types\": \"./dist/fastify.d.ts\",\n \"default\": \"./dist/fastify.js\"\n },\n \"require\": {\n \"types\": \"./dist/fastify.d.cts\",\n \"default\": \"./dist/fastify.cjs\"\n }\n },\n \"./nest\": {\n \"import\": {\n \"types\": \"./dist/nest.d.ts\",\n \"default\": \"./dist/nest.js\"\n },\n \"require\": {\n \"types\": \"./dist/nest.d.cts\",\n \"default\": \"./dist/nest.cjs\"\n }\n },\n \"./koa\": {\n \"import\": {\n \"types\": \"./dist/koa.d.ts\",\n \"default\": \"./dist/koa.js\"\n },\n \"require\": {\n \"types\": \"./dist/koa.d.cts\",\n \"default\": \"./dist/koa.cjs\"\n }\n }\n },\n \"main\": \"./dist/index.cjs\",\n \"module\": \"./dist/index.js\",\n \"types\": \"./dist/index.d.ts\",\n \"typesVersions\": {\n \"*\": {\n \"fastify\": [\n \"./dist/fastify.d.ts\"\n ],\n \"nest\": [\n \"./dist/nest.d.ts\"\n ],\n \"koa\": [\n \"./dist/koa.d.ts\"\n ]\n }\n },\n \"dependencies\": {\n \"@camada/browser\": \"file:../camada-browser\",\n \"@camada/core\": \"file:../camada-core\"\n },\n \"devDependencies\": {\n \"@koa/bodyparser\": \"^6.1.0\",\n \"@nestjs/common\": \"^11.0.0\",\n \"@nestjs/core\": \"^11.0.0\",\n \"@nestjs/platform-express\": \"^11.0.0\",\n \"@types/express\": \"^4.17.25\",\n \"@types/koa\": \"^2.15.2\",\n \"@types/koa-mount\": \"^4.0.5\",\n \"@types/node\": \"^22.10.0\",\n \"express\": \"^4.21.0\",\n \"fastify\": \"^5.2.0\",\n \"koa\": \"^2.15.0\",\n \"koa-mount\": \"^4.2.0\",\n \"reflect-metadata\": \"^0.2.2\",\n \"rxjs\": \"^7.8.1\",\n \"tsup\": \"^8.3.5\",\n \"typescript\": \"^5.7.2\",\n \"vitest\": \"^3.0.0\"\n },\n \"peerDependencies\": {\n \"@nestjs/common\": \">=10\"\n },\n \"peerDependenciesMeta\": {\n \"@nestjs/common\": {\n \"optional\": true\n }\n }\n}\n","// The SDK's wire identity (SDK-03): x-camada-sdk: <package>/<version>. Named JSON imports are\n// inlined and tree-shaken by tsup at build, so dist carries two literals — no runtime fs read.\nimport { name, version } from '../package.json';\nexport const SDK_ID = `${name}/${version}`;\n","// Quickstart (Express / Connect / Nest-on-Express):\n// import camada from '@camada/node';\n// app.use(camada.express()); // env: CAMADA_KEY (+ CAMADA_INGEST_URL for dev)\n// The default export is a lazy singleton wired from the environment on first use.\nimport type { IncomingMessage, Server, ServerResponse } from 'node:http';\nimport { Camada, createCamada, type CamadaOptions } from './camada.js';\n\nexport { Camada, createCamada, type CamadaOptions } from './camada.js';\nexport { resolveEnv, parseTrustedProxyEnv, type ResolvedEnv } from './env.js';\n\nlet singleton: Camada | null = null;\nexport function getDefault(): Camada {\n return (singleton ??= createCamada());\n}\n/** Test/reset hook: replaces the singleton (stops the old one). */\nexport function configure(opts: CamadaOptions): Camada {\n singleton?.stop();\n singleton = createCamada(opts);\n return singleton;\n}\n\nexport default {\n express: () => (req: IncomingMessage, res: ServerResponse, next: (err?: unknown) => void) => { if (!getDefault().handle(req, res)) next(); },\n handle: (req: IncomingMessage, res: ServerResponse) => getDefault().handle(req, res),\n scriptTag: (req: IncomingMessage) => getDefault().scriptTag(req),\n serveChallenge: (req: IncomingMessage, res: ServerResponse) => getDefault().serveChallenge(req, res),\n track: (req: IncomingMessage, event: string, data?: { user?: string }) => getDefault().track(req, event, data),\n attach: <S extends Server>(server: S): S => getDefault().attach(server),\n configure,\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACMA,IAAAA,sBAAsC;AACtC,IAAAC,eAIO;AACP,sBAAqB;AACrB,yBAAiB;;;ACNjB,kBAAwE;AAgBjE,SAAS,gBAAgB,KAA+C;AAC7E,QAAM,KAAK,OAAO,IAAI,KAAK,EAAE,YAAY;AACzC,SAAO,6BAA6B,KAAK,CAAC,IAAI,IAAI;AACpD;AAGO,SAAS,WAAW,MAA0C,QAAQ,KAAyB;AACpG,QAAM,UAAM,sBAAS,IAAI,UAAU;AACnC,QAAM,cAAc,KAAK,eAAe,IAAI;AAC5C,QAAM,YAAY,KAAK,aAAa,IAAI;AACxC,MAAI,CAAC,eAAe,CAAC,UAAW,QAAO;AACvC,QAAM,aAAa,IAAI,qBAAqB,yBAAyB,QAAQ,OAAO,EAAE;AACtF,SAAO;AAAA,IACL;AAAA,IAAa;AAAA,IAAW;AAAA,IACxB,QAAQ,IAAI,cAAc,GAAG,WAAW,IAAI,SAAS;AAAA,IACrD,aAAa,IAAI,uBAAuB,GAAG,SAAS;AAAA,IACpD,YAAY,IAAI,sBAAsB;AAAA,IACtC,kBAAc,kCAAqB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,cAAc,aAAa;AACrC;;;ANEO,SAAS,UAAU,QAAiB;AACzC,SAAO,OAAO,KAAgB,SAAgD;AAG5E,QAAI,IAAI,SAAS,IAAI,SAAS;AAC9B,QAAI,IAAI,gBAAgB,IAAI;AAC5B,SAAK,UAAU,WAAW,GAAG,OAAO,IAAI,KAAK,IAAI,GAAG,GAAG;AAAE,UAAI,UAAU;AAAO;AAAA,IAAQ;AACtF,UAAM,KAAK;AAAA,EACb;AACF;","names":["import_node_crypto","import_core","import_core","name","iife"]}
package/dist/koa.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { IncomingMessage, ServerResponse } from 'node:http';
2
- import { C as Camada } from './camada-CFmJTruP.cjs';
2
+ import { C as Camada } from './camada-DuHQ4sLn.cjs';
3
3
  import '@camada/core';
4
4
 
5
5
  interface KoaishCtx {
package/dist/koa.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { IncomingMessage, ServerResponse } from 'node:http';
2
- import { C as Camada } from './camada-CFmJTruP.js';
2
+ import { C as Camada } from './camada-DuHQ4sLn.js';
3
3
  import '@camada/core';
4
4
 
5
5
  interface KoaishCtx {
package/dist/koa.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  getDefault
3
- } from "./chunk-V3UWO5OT.js";
3
+ } from "./chunk-44FSZJ5F.js";
4
4
 
5
5
  // src/koa.ts
6
6
  function camadaKoa(engine) {
package/dist/nest.cjs CHANGED
@@ -46,10 +46,15 @@ var import_common = require("@nestjs/common");
46
46
  // src/camada.ts
47
47
  var import_node_crypto2 = require("crypto");
48
48
  var import_core3 = require("@camada/core");
49
+ var import_node_net = require("net");
49
50
  var import_iife_string = __toESM(require("@camada/browser/iife-string"), 1);
50
51
 
51
52
  // src/env.ts
52
53
  var import_core = require("@camada/core");
54
+ function parseTlsxHeader(raw) {
55
+ const n = (raw ?? "").trim().toLowerCase();
56
+ return /^[a-z0-9!#$%&'*+.^_`|~-]+$/.test(n) ? n : null;
57
+ }
53
58
  function resolveEnv(env = process.env) {
54
59
  const key = (0, import_core.parseKey)(env.CAMADA_KEY);
55
60
  const ingestToken = key?.ingestToken ?? env.CAMADA_TOKEN;
@@ -63,7 +68,8 @@ function resolveEnv(env = process.env) {
63
68
  secret: env.CAMADA_KEY || `${ingestToken}.${snapToken}`,
64
69
  snapshotUrl: env.CAMADA_SNAPSHOT_URL || `${ingestUrl}/snapshot`,
65
70
  serverless: env.CAMADA_SERVERLESS === "1",
66
- trustedProxy: (0, import_core.parseTrustedProxyEnv)(env.CAMADA_TRUSTED_PROXY)
71
+ trustedProxy: (0, import_core.parseTrustedProxyEnv)(env.CAMADA_TRUSTED_PROXY),
72
+ tlsxHeader: parseTlsxHeader(env.CAMADA_TLSX_HEADER)
67
73
  };
68
74
  }
69
75
 
@@ -131,13 +137,9 @@ function writeChallengePage(res, o) {
131
137
  });
132
138
  res.end((0, import_core2.challengePage)(o));
133
139
  }
134
- function writeChallengeJson(res) {
135
- res.writeHead(403, { "content-type": "application/json", "cache-control": "no-store", "x-camada-challenge": "1" });
136
- res.end('{"error":"challenge_required"}');
137
- }
138
140
 
139
141
  // package.json
140
- var version = "0.3.2";
142
+ var version = "0.4.0";
141
143
  var name = "@camada/node";
142
144
 
143
145
  // src/version.ts
@@ -148,6 +150,12 @@ var SESSION_COOKIE = "_sfp";
148
150
  var SCRIPT_PATH = "/_cam/b.js";
149
151
  var FP_PATH = "/_cam/fp";
150
152
  var FP_MAX = 32 * 1024;
153
+ function isLoopbackPeer(peer) {
154
+ const k = (0, import_node_net.isIP)(peer);
155
+ if (k === 4) return peer.startsWith("127.");
156
+ return k !== 6 || peer === "::1" || /^::ffff:127\./i.test(peer);
157
+ }
158
+ var TLSX_RE = /^[A-Za-z0-9+/=_-]+$/;
151
159
  var ATTACHED = /* @__PURE__ */ Symbol.for("camada.attached");
152
160
  var headerReader = (req) => (name2) => {
153
161
  const v = req.headers[name2];
@@ -157,6 +165,7 @@ var splitUrl = (raw) => {
157
165
  const qi = raw.indexOf("?");
158
166
  return qi === -1 ? { path: raw, query: "" } : { path: raw.slice(0, qi), query: raw.slice(qi) };
159
167
  };
168
+ var isPage = (req) => (0, import_core3.wantsHtml)(req.headers.accept ?? null, req.headers["sec-fetch-dest"] ?? null);
160
169
  function pathAndQuery(req) {
161
170
  return splitUrl(req.originalUrl || req.url || "/");
162
171
  }
@@ -194,6 +203,27 @@ function keepSessionCookie(res, cookie) {
194
203
  };
195
204
  res.setHeader("set-cookie", cookie);
196
205
  }
206
+ var NO_STORE_HEADERS = ["cache-control", "cdn-cache-control", "surrogate-control", "cloudflare-cdn-cache-control", "vercel-cdn-cache-control", "netlify-cdn-cache-control"];
207
+ function signal(res) {
208
+ const set = res.setHeader, writeHead = res.writeHead;
209
+ const isNoStore = (n) => typeof n === "string" && NO_STORE_HEADERS.includes(n.toLowerCase());
210
+ const strip = (h) => {
211
+ if (Array.isArray(h)) {
212
+ if (h.length && Array.isArray(h[0])) return h.filter((p) => !isNoStore(p[0]));
213
+ const out = [];
214
+ for (let i = 0; i < h.length; i += 2) if (!isNoStore(h[i])) out.push(h[i], h[i + 1]);
215
+ return out;
216
+ }
217
+ if (h && typeof h === "object") return Object.fromEntries(Object.entries(h).filter(([k]) => !isNoStore(k)));
218
+ return h;
219
+ };
220
+ res.writeHead = function(status, ...rest) {
221
+ if (!this.headersSent) for (const h of NO_STORE_HEADERS) set.call(this, h, "no-store");
222
+ return writeHead.call(this, status, ...rest.map(strip));
223
+ };
224
+ res.setHeader("x-camada-challenge", "1");
225
+ for (const h of NO_STORE_HEADERS) res.setHeader(h, "no-store");
226
+ }
197
227
  var Camada = class {
198
228
  env;
199
229
  snap = null;
@@ -208,6 +238,7 @@ var Camada = class {
208
238
  scriptPathSet;
209
239
  kit = null;
210
240
  envSource;
241
+ tlsxHeader;
211
242
  waitUntil;
212
243
  constructor(opts = {}) {
213
244
  this.envSource = opts.env ?? process.env;
@@ -220,6 +251,7 @@ var Camada = class {
220
251
  this.challengePathSet = opts.challengePath != null;
221
252
  this.scriptPathSet = opts.scriptPath != null;
222
253
  this.env = resolveEnv(this.envSource);
254
+ this.tlsxHeader = parseTlsxHeader(opts.tlsxHeader) ?? this.env?.tlsxHeader ?? null;
223
255
  if (!this.env) return;
224
256
  if (this.envSource.CAMADA_DISABLED === "1") return;
225
257
  this.snap = new import_core3.SnapshotClient({
@@ -253,6 +285,24 @@ var Camada = class {
253
285
  if (!peer && req.socket && !onUnixListener(req.socket)) return null;
254
286
  return (0, import_core3.resolveClientIp)(peer ?? null, req.headers["x-forwarded-for"], this.trustedProxy());
255
287
  }
288
+ /** The TLS fingerprint Cloudflare (a Transform Rule) put in the opt-in header, or null. Trusted only
289
+ * in `cidrs` mode and only when the socket peer itself is a NON-loopback address core's cidrs walk
290
+ * trusts, i.e. Cloudflare connects to the app directly. A loopback peer (a local reverse proxy or
291
+ * cloudflared) is never trusted and X-Forwarded-For is never consulted, so the header is not read
292
+ * and tlsx conditions fail open. Trust is core's isTrustedPeer, the same first-hop test the cidrs
293
+ * walk uses. No readable peer (unix listener included) -> null, since core's no-peer walk would
294
+ * trust everyone. */
295
+ tlsxOf(req) {
296
+ const header = this.tlsxHeader;
297
+ if (!header) return null;
298
+ const v = headerReader(req)(header);
299
+ if (v == null || v.length > 128 || !TLSX_RE.test(v)) return null;
300
+ const cfg = this.trustedProxy();
301
+ const peer = req.socket?.remoteAddress;
302
+ if (cfg?.mode !== "cidrs" || !peer) return null;
303
+ if (isLoopbackPeer(peer)) return null;
304
+ return (0, import_core3.isTrustedPeer)(peer, cfg) ? v : null;
305
+ }
256
306
  /** Where the challenge form posts: under the mount prefix, so it reaches camada, unless the
257
307
  * path was configured explicitly. */
258
308
  challengeAction(req) {
@@ -290,7 +340,7 @@ var Camada = class {
290
340
  const { path, query } = pathAndQuery(req);
291
341
  const ip = this.clientIp(req);
292
342
  req.camadaPrefix = mountPrefix(req);
293
- const v = this.snap.verdict({ ip, path, ua: req.headers["user-agent"], header: headerReader(req) });
343
+ const v = this.snap.verdict({ ip, path, ua: req.headers["user-agent"], header: headerReader(req), tlsx: this.tlsxOf(req) });
294
344
  if (v.block) {
295
345
  const headers = { "x-block-reason": v.reason ?? "", "x-block-version": v.version ?? "", "content-type": "text/plain", "cache-control": "no-store" };
296
346
  if (v.rule) headers["x-block-rule"] = v.rule;
@@ -309,8 +359,11 @@ var Camada = class {
309
359
  return true;
310
360
  }
311
361
  if (v.challenge && !this.challengePassed(req, ip)) {
312
- this.serveChallengeInner(req, res, ip, path + query);
313
- return true;
362
+ if (isPage(req)) {
363
+ this.serveChallengeInner(req, res, ip, path + query);
364
+ return true;
365
+ }
366
+ req.camadaSignalled = true;
314
367
  }
315
368
  }
316
369
  if (this.beaconEnabled()) {
@@ -324,50 +377,64 @@ var Camada = class {
324
377
  return true;
325
378
  }
326
379
  }
380
+ const { rid, sid, ns: newSession } = this.begin(req, res, ip, t0, v.warn ? v.rule : void 0);
381
+ if (req.camadaSignalled) signal(res);
382
+ const cfg = this.snap.config;
383
+ const excluded = (cfg?.exclude || []).some((x) => path.startsWith(x));
384
+ if (req.camadaSignalled || !excluded && Math.random() < (cfg?.sample ?? 1)) {
385
+ this.hookCapture(req, res, path, query, ip, { rid, sid, newSession, t0 });
386
+ }
387
+ return false;
388
+ }
389
+ /** Mints the request's rid and session (setting the cookie on a first visit), records them on
390
+ * `req.camada` and sets `x-rid`. handle() and serveChallenge() (when handle() did not run) share it. */
391
+ begin(req, res, ip, t0, wrn) {
327
392
  const rid = (0, import_node_crypto2.randomUUID)();
328
393
  let sid = cookieValue(req.headers.cookie || "", SESSION_COOKIE);
329
- const newSession = !sid;
394
+ const ns = !sid;
330
395
  if (!sid) {
331
396
  sid = (0, import_node_crypto2.randomUUID)();
332
397
  const https = req.socket.encrypted || req.headers["x-forwarded-proto"] === "https";
333
398
  keepSessionCookie(res, `${SESSION_COOKIE}=${sid}; Path=/; Max-Age=2592000; HttpOnly; SameSite=Lax${https ? "; Secure" : ""}`);
334
399
  }
335
- req.camada = { rid, ip, sid };
336
400
  res.setHeader("x-rid", rid);
337
- const cfg = this.snap.config;
338
- const excluded = (cfg?.exclude || []).some((x) => path.startsWith(x));
339
- if (!excluded && Math.random() < (cfg?.sample ?? 1)) {
340
- let shipped = false;
341
- req.camadaOpen = true;
342
- const ship = () => (0, import_core3.guarded)(() => {
343
- req.camadaOpen = false;
344
- if (shipped) return;
345
- shipped = true;
346
- if (req.camadaChallenged) {
347
- if (this.env?.serverless) this.flushFor(req);
348
- return;
349
- }
350
- const ev = this.buildEvent(req, path, query, ip, { rid, sid, newSession });
351
- ev.ts = t0;
352
- ev.st = res.statusCode;
353
- ev.dur = Date.now() - t0;
354
- if (req.route?.path) ev.rt = String(req.route.path);
355
- if (v.warn && v.rule) ev.wrn = v.rule;
356
- this.enqueue(ev, req);
357
- }, void 0);
358
- res.once("finish", ship);
359
- res.once("close", ship);
360
- }
361
- return false;
401
+ return req.camada = { rid, ip, sid, ns, t0, wrn };
402
+ }
403
+ /** Installs the response-finish hook that ships the request's normal capture event. */
404
+ hookCapture(req, res, path, query, ip, o) {
405
+ let shipped = false;
406
+ req.camadaOpen = true;
407
+ const ship = () => (0, import_core3.guarded)(() => {
408
+ req.camadaOpen = false;
409
+ if (shipped) return;
410
+ shipped = true;
411
+ if (req.camadaChallenged) {
412
+ if (this.env?.serverless) this.flushFor(req);
413
+ return;
414
+ }
415
+ const ev = this.buildEvent(req, path, query, ip, o);
416
+ ev.ts = o.t0;
417
+ ev.st = res.statusCode;
418
+ ev.dur = Date.now() - o.t0;
419
+ if (req.route?.path) ev.rt = String(req.route.path);
420
+ if (req.camada?.wrn) ev.wrn = req.camada.wrn;
421
+ if (req.camadaSignalled) ev.chs = 1;
422
+ this.enqueue(ev, req);
423
+ }, void 0);
424
+ res.once("finish", ship);
425
+ res.once("close", ship);
362
426
  }
363
427
  buildEvent(req, path, query, ip, o) {
364
428
  const raw = req.rawHeaders || [];
365
429
  const headers = [];
366
430
  for (let i = 0; i + 1 < raw.length; i += 2) headers.push([raw[i], raw[i + 1]]);
367
- return (0, import_core3.buildWireEvent)(
431
+ const ev = (0, import_core3.buildWireEvent)(
368
432
  { method: req.method || "GET", host: req.headers.host || "", path, query, headers, ip, httpVersion: req.httpVersion },
369
433
  { tap: import_core3.TAP_NODE, rid: o.rid, sid: o.sid, newSession: o.newSession }
370
434
  );
435
+ const tlsx = this.tlsxOf(req);
436
+ if (tlsx) ev.tlsx = tlsx;
437
+ return ev;
371
438
  }
372
439
  /** Reads the beacon POST (≤32 KB), answers 204 immediately, and queues the beacon as a `sig: 1`
373
440
  * row with the trusted-proxy-resolved client IP: it rides the next event batch, so the analyst
@@ -379,7 +446,9 @@ var Camada = class {
379
446
  res.writeHead(204, { "cache-control": "no-store" });
380
447
  res.end();
381
448
  if (!parsed || typeof parsed !== "object" || Array.isArray(parsed) || Buffer.isBuffer(parsed)) return;
382
- this.enqueue({ ...parsed, sig: 1, ip, tap: import_core3.TAP_NODE });
449
+ const row = { ...parsed, sig: 1, ip, tap: import_core3.TAP_NODE, rts: Date.now() };
450
+ for (const k of import_core3.REQUEST_ROW_KEYS) delete row[k];
451
+ this.enqueue(row);
383
452
  };
384
453
  const fromText = (text) => {
385
454
  try {
@@ -423,15 +492,11 @@ var Camada = class {
423
492
  challengePassed(req, ip) {
424
493
  return !!this.kit?.tokenValid(ip, Date.now(), cookieValue(req.headers.cookie || "", import_core3.CHALLENGE_COOKIE));
425
494
  }
426
- /** 403 + the proof-of-work page (HTML navigations) or 403 JSON (everything else), plus the
495
+ /** 403 + the proof-of-work page (HTML navigations only), plus the
427
496
  * `blk: "challenge"` event — a served challenge is reported like a block (contract §D2). */
428
497
  serveChallengeInner(req, res, ip, target) {
429
498
  const to = (0, import_core3.safeReturnTo)(target);
430
- if ((0, import_core3.wantsHtml)(req.headers.accept ?? null, req.headers["sec-fetch-dest"] ?? null)) {
431
- writeChallengePage(res, { nonce: this.kit.nonce(ip, Date.now()), action: this.challengeAction(req), to });
432
- } else {
433
- writeChallengeJson(res);
434
- }
499
+ writeChallengePage(res, { nonce: this.kit.nonce(ip, Date.now()), action: this.challengeAction(req), to });
435
500
  (0, import_core3.guarded)(() => {
436
501
  const qi = target.indexOf("?");
437
502
  const ev = this.buildEvent(
@@ -513,14 +578,25 @@ var Camada = class {
513
578
  }
514
579
  /** Serve the challenge for this request on demand — for a route the app wants to gate itself
515
580
  * (the example's /challenge-me). Returns false when the client already holds a valid `_cch`,
516
- * so the caller renders its own page. */
581
+ * so the caller renders its own page. A non-HTML request is never challenged: it gets the
582
+ * `x-camada-challenge: 1` header and a `chs: 1` event, and false, so the app answers. */
517
583
  serveChallenge(req, res) {
518
584
  return (0, import_core3.guarded)(() => {
519
585
  if (this.disabled || !this.kit || !this.queue) return false;
520
586
  const ip = this.clientIp(req);
521
587
  if (!ip || this.challengePassed(req, ip)) return false;
522
- req.camadaChallenged = true;
523
- const { path, query } = pathAndQuery(req);
588
+ const r = req;
589
+ const { path, query } = pathAndQuery(r);
590
+ if (!isPage(r)) {
591
+ if (!r.camadaSignalled) signal(res);
592
+ r.camadaSignalled = true;
593
+ if (r.camadaOpen === void 0) {
594
+ const c = r.camada ?? this.begin(r, res, ip, Date.now());
595
+ this.hookCapture(r, res, path, query, ip, { rid: c.rid, sid: c.sid, newSession: c.ns, t0: c.t0 });
596
+ }
597
+ return false;
598
+ }
599
+ r.camadaChallenged = true;
524
600
  this.serveChallengeInner(req, res, ip, path + query);
525
601
  return true;
526
602
  }, false);