@camada/node 0.2.0 → 0.3.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 CHANGED
@@ -46,7 +46,7 @@ function resolveEnv(env = process.env) {
46
46
  const ingestToken = key?.ingestToken ?? env.CAMADA_TOKEN;
47
47
  const snapToken = key?.snapToken ?? env.CAMADA_SNAPSHOT_TOKEN;
48
48
  if (!ingestToken || !snapToken) return null;
49
- const ingestUrl = (env.CAMADA_INGEST_URL || "https://in.camada.dev").replace(/\/$/, "");
49
+ const ingestUrl = (env.CAMADA_INGEST_URL || "https://in.camada.app").replace(/\/$/, "");
50
50
  return {
51
51
  ingestToken,
52
52
  snapToken,
@@ -97,7 +97,7 @@ function writeChallengeJson(res) {
97
97
  }
98
98
 
99
99
  // package.json
100
- var version = "0.2.0";
100
+ var version = "0.3.0";
101
101
  var name = "@camada/node";
102
102
 
103
103
  // src/version.ts
@@ -108,6 +108,7 @@ var SESSION_COOKIE = "_sfp";
108
108
  var SCRIPT_PATH = "/_cam/b.js";
109
109
  var FP_PATH = "/_cam/fp";
110
110
  var FP_MAX = 32 * 1024;
111
+ var ATTACHED = /* @__PURE__ */ Symbol.for("camada.attached");
111
112
  var headerReader = (req) => (name2) => {
112
113
  const v = req.headers[name2];
113
114
  return v == null ? null : Array.isArray(v) ? v.join(", ") : v;
@@ -120,6 +121,17 @@ var cookieValue = (cookie, name2) => {
120
121
  const j = src.indexOf(";", start);
121
122
  return src.slice(start, j === -1 ? void 0 : j);
122
123
  };
124
+ function keepSessionCookie(res, cookie) {
125
+ const set = res.setHeader;
126
+ res.setHeader = function(name2, value) {
127
+ if (typeof name2 === "string" && name2.toLowerCase() === "set-cookie") {
128
+ const list = Array.isArray(value) ? value : [String(value)];
129
+ if (!list.some((c) => String(c).trimStart().startsWith(SESSION_COOKIE + "="))) value = [...list, cookie];
130
+ }
131
+ return set.call(this, name2, value);
132
+ };
133
+ res.setHeader("set-cookie", cookie);
134
+ }
123
135
  var Camada = class {
124
136
  env;
125
137
  snap = null;
@@ -217,22 +229,27 @@ var Camada = class {
217
229
  if (!sid) {
218
230
  sid = (0, import_node_crypto2.randomUUID)();
219
231
  const https = req.socket.encrypted || req.headers["x-forwarded-proto"] === "https";
220
- res.setHeader("set-cookie", `${SESSION_COOKIE}=${sid}; Path=/; Max-Age=2592000; HttpOnly; SameSite=Lax${https ? "; Secure" : ""}`);
232
+ keepSessionCookie(res, `${SESSION_COOKIE}=${sid}; Path=/; Max-Age=2592000; HttpOnly; SameSite=Lax${https ? "; Secure" : ""}`);
221
233
  }
222
234
  req.camada = { rid, ip, sid };
223
235
  res.setHeader("x-rid", rid);
224
236
  const cfg = this.snap.config;
225
237
  const excluded = (cfg?.exclude || []).some((x) => path.startsWith(x));
226
238
  if (!excluded && Math.random() < (cfg?.sample ?? 1)) {
227
- res.on("finish", () => (0, import_core3.guarded)(() => {
228
- if (req.camadaChallenged) return;
239
+ let shipped = false;
240
+ const ship = () => (0, import_core3.guarded)(() => {
241
+ if (shipped || req.camadaChallenged) return;
242
+ shipped = true;
229
243
  const ev = this.buildEvent(req, path, query, ip, { rid, sid, newSession });
244
+ ev.ts = t0;
230
245
  ev.st = res.statusCode;
231
246
  ev.dur = Date.now() - t0;
232
247
  if (req.route?.path) ev.rt = String(req.route.path);
233
248
  if (v.warn && v.rule) ev.wrn = v.rule;
234
249
  this.queue.push(ev);
235
- }, void 0));
250
+ }, void 0);
251
+ res.once("finish", ship);
252
+ res.once("close", ship);
236
253
  }
237
254
  return false;
238
255
  }
@@ -334,6 +351,47 @@ var Camada = class {
334
351
  this.queue.push(ev);
335
352
  }, void 0));
336
353
  }
354
+ /**
355
+ * Records the upgrades (WebSocket handshakes) a node:http or node:https server hands to its
356
+ * 'upgrade' listeners: Node never routes those through the request handler the middleware runs
357
+ * in. Each one ships one event with `st: 101` once the app's listeners have run. Observe only:
358
+ * the request is never blocked or challenged, nothing is written to the socket, no session
359
+ * cookie is minted, and a server with no 'upgrade' listener of its own behaves exactly as before
360
+ * (this wraps `server.emit` rather than adding a listener, which would claim every upgrade).
361
+ * Idempotent; returns the server. A handshake the WebSocket library then rejects still ships 101.
362
+ */
363
+ attach(server) {
364
+ (0, import_core3.guarded)(() => {
365
+ const s = server;
366
+ if (this.disabled || !this.queue || s[ATTACHED]) return;
367
+ s[ATTACHED] = true;
368
+ const emit = s.emit.bind(s);
369
+ s.emit = ((event, ...args) => {
370
+ if (event !== "upgrade") return emit(event, ...args);
371
+ const t0 = Date.now();
372
+ try {
373
+ return emit(event, ...args);
374
+ } finally {
375
+ (0, import_core3.guarded)(() => this.shipUpgrade(args[0], t0), void 0);
376
+ }
377
+ });
378
+ }, void 0);
379
+ return server;
380
+ }
381
+ shipUpgrade(req, t0) {
382
+ if (this.disabled || !this.queue) return;
383
+ const rawUrl = req.url || "/";
384
+ const qi = rawUrl.indexOf("?");
385
+ const path = qi === -1 ? rawUrl : rawUrl.slice(0, qi);
386
+ const cfg = this.snap?.config;
387
+ if ((cfg?.exclude || []).some((x) => path.startsWith(x)) || Math.random() >= (cfg?.sample ?? 1)) return;
388
+ const ip = (0, import_core3.resolveClientIp)(req.socket?.remoteAddress, req.headers["x-forwarded-for"], this.trustedProxy());
389
+ const ev = this.buildEvent(req, path, qi === -1 ? "" : rawUrl.slice(qi), ip, { rid: (0, import_node_crypto2.randomUUID)(), sid: this.sessionOf(req), newSession: false });
390
+ ev.ts = t0;
391
+ ev.st = 101;
392
+ ev.dur = Date.now() - t0;
393
+ this.queue.push(ev);
394
+ }
337
395
  /** Serve the challenge for this request on demand — for a route the app wants to gate itself
338
396
  * (the example's /challenge-me). Returns false when the client already holds a valid `_cch`,
339
397
  * so the caller renders its own page. */
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 { req: IncomingMessage; res: ServerResponse; respond?: boolean }\n\nexport function camadaKoa(engine?: Camada) {\n return async (ctx: KoaishCtx, next: () => Promise<unknown>): Promise<void> => {\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, ServerResponse } from 'node:http';\nimport { randomUUID, webcrypto } from 'node:crypto'; // webcrypto handed to hashUserId: Node 18 has no global crypto.subtle\nimport {\n SnapshotClient, EventQueue, buildWireEvent, resolveClientIp, hashUserId, guarded, logRateLimited,\n challengePage, challengeCookie, safeReturnTo, wantsHtml, parseFormBody, 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, readBody, 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\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}\n\ninterface CamadaRequest extends IncomingMessage {\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}\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 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\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 kit: ChallengeKit | null = null;\n private readonly envSource: Record<string, string | undefined>;\n\n constructor(opts: CamadaOptions = {}) {\n this.envSource = opts.env ?? process.env;\n this.fetchImpl = opts.fetchImpl ?? fetch;\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.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 private beaconEnabled(): boolean { return this.snap?.config?.beacon !== false; }\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();\n\n const rawUrl = req.url || '/';\n const qi = rawUrl.indexOf('?');\n const path = qi === -1 ? rawUrl : rawUrl.slice(0, qi);\n const query = qi === -1 ? '' : rawUrl.slice(qi);\n const ip = resolveClientIp(req.socket?.remoteAddress, req.headers['x-forwarded-for'] as string | undefined, this.trustedProxy());\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' };\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.queue.push(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' && 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' && 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' && 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 res.setHeader('set-cookie', `${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 res.on('finish', () => guarded(() => {\n // serveChallenge() may have answered from inside the app, and it already shipped the\n // `blk: \"challenge\"` row — one request, one event.\n if (req.camadaChallenged) return;\n const ev = this.buildEvent(req, path, query, ip, { rid, sid, newSession });\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.queue!.push(ev);\n }, undefined));\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 private relayBeacon(req: IncomingMessage, res: ServerResponse, ip: string | null): void {\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 res.writeHead(204, { 'cache-control': 'no-store' });\n res.end();\n let parsed: unknown;\n try { parsed = JSON.parse(Buffer.concat(chunks).toString('utf8')); } catch { return; }\n if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return;\n this.queue!.push({ ...(parsed as Record<string, unknown>), sig: 1, ip, tap: TAP_NODE });\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, challengePage({ nonce: this.kit!.nonce(ip, Date.now()), action: this.challengePath, 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.queue!.push(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 readBody(req, (body) => guarded(() => {\n const form = parseFormBody(body);\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, challengePage({ nonce: this.kit!.nonce(ip, now), action: this.challengePath, 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.challengePath, '', 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.queue!.push(ev);\n }, undefined));\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 = resolveClientIp(req.socket?.remoteAddress, req.headers['x-forwarded-for'] as string | undefined, this.trustedProxy());\n if (!ip || this.challengePassed(req, ip)) return false; // unidentifiable client: fail open\n (req as CamadaRequest).camadaChallenged = true;\n this.serveChallengeInner(req as CamadaRequest, res, ip, req.url || '/');\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 return `<script src=\"${this.scriptPath}${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 void (async () => {\n const uid = data?.user ? await hashUserId(data.user, this.env!.ingestToken, (globalThis.crypto ?? webcrypto).subtle) : null;\n this.queue!.push({ tap: TAP_NODE, et: event, uid, rid: ctx?.rid ?? null, sid: ctx?.sid ?? null, ip: ctx?.ip ?? null, ts: Date.now() });\n })().catch(logRateLimited);\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 stop(): void {\n this.snap?.stop();\n this.queue?.stop();\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)\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.dev').replace(/\\/$/, ''); // PLACEHOLDER default — confirm the production ingest domain before any npm publish\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, type ChallengeKit } 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\nexport const isHttps = (req: IncomingMessage): boolean =>\n !!(req.socket as { encrypted?: boolean } | undefined)?.encrypted || req.headers['x-forwarded-proto'] === 'https';\n\nexport function writeChallengePage(res: ServerResponse, html: string): void {\n res.writeHead(403, { 'content-type': 'text/html; charset=utf-8', 'cache-control': 'no-store', 'x-camada-challenge': '1' });\n res.end(html);\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.2.0\",\n \"license\": \"MIT\",\n \"type\": \"module\",\n \"engines\": {\n \"node\": \">=18\"\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 \"types\": \"./dist/index.d.ts\",\n \"import\": \"./dist/index.js\",\n \"require\": \"./dist/index.cjs\"\n },\n \"./fastify\": {\n \"types\": \"./dist/fastify.d.ts\",\n \"import\": \"./dist/fastify.js\",\n \"require\": \"./dist/fastify.cjs\"\n },\n \"./nest\": {\n \"types\": \"./dist/nest.d.ts\",\n \"import\": \"./dist/nest.js\",\n \"require\": \"./dist/nest.cjs\"\n },\n \"./koa\": {\n \"types\": \"./dist/koa.d.ts\",\n \"import\": \"./dist/koa.js\",\n \"require\": \"./dist/koa.cjs\"\n }\n },\n \"main\": \"./dist/index.cjs\",\n \"module\": \"./dist/index.js\",\n \"types\": \"./dist/index.d.ts\",\n \"dependencies\": {\n \"@camada/browser\": \"file:../camada-browser\",\n \"@camada/core\": \"file:../camada-core\"\n },\n \"devDependencies\": {\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/node\": \"^22.10.0\",\n \"express\": \"^4.21.0\",\n \"fastify\": \"^5.2.0\",\n \"koa\": \"^2.15.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, 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 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,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,YAAM,MAAO,IAAsB;AACnC,YAAM,YAAY;AAChB,cAAM,MAAM,MAAM,OAAO,UAAM,yBAAW,KAAK,MAAM,KAAK,IAAK,cAAc,WAAW,UAAU,+BAAW,MAAM,IAAI;AACvH,aAAK,MAAO,KAAK,EAAE,KAAK,uBAAU,IAAI,OAAO,KAAK,KAAK,KAAK,OAAO,MAAM,KAAK,KAAK,OAAO,MAAM,IAAI,KAAK,MAAM,MAAM,IAAI,KAAK,IAAI,EAAE,CAAC;AAAA,MACvI,GAAG,EAAE,MAAM,2BAAc;AAAA,IAC3B,GAAG,MAAS;AAAA,EACd;AAAA;AAAA,EAGA,UAAU;AACR,WAAO,CAAC,KAAsB,KAAqB,SAAwC;AACzF,UAAI,CAAC,KAAK,OAAO,KAAK,GAAG,EAAG,MAAK;AAAA,IACnC;AAAA,EACF;AAAA,EAEA,OAAa;AACX,SAAK,MAAM,KAAK;AAChB,SAAK,OAAO,KAAK;AAAA,EACnB;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;;;AKxTA,IAAI,YAA2B;AACxB,SAAS,aAAqB;AACnC,SAAQ,cAAc,aAAa;AACrC;;;ANJO,SAAS,UAAU,QAAiB;AACzC,SAAO,OAAO,KAAgB,SAAgD;AAC5E,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 { req: IncomingMessage; res: ServerResponse; respond?: boolean }\n\nexport function camadaKoa(engine?: Camada) {\n return async (ctx: KoaishCtx, next: () => Promise<unknown>): Promise<void> => {\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 challengePage, challengeCookie, safeReturnTo, wantsHtml, parseFormBody, 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, readBody, 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}\n\ninterface CamadaRequest extends IncomingMessage {\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}\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 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 kit: ChallengeKit | null = null;\n private readonly envSource: Record<string, string | undefined>;\n\n constructor(opts: CamadaOptions = {}) {\n this.envSource = opts.env ?? process.env;\n this.fetchImpl = opts.fetchImpl ?? fetch;\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.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 private beaconEnabled(): boolean { return this.snap?.config?.beacon !== false; }\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();\n\n const rawUrl = req.url || '/';\n const qi = rawUrl.indexOf('?');\n const path = qi === -1 ? rawUrl : rawUrl.slice(0, qi);\n const query = qi === -1 ? '' : rawUrl.slice(qi);\n const ip = resolveClientIp(req.socket?.remoteAddress, req.headers['x-forwarded-for'] as string | undefined, this.trustedProxy());\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' };\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.queue.push(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' && 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' && 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' && 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 const ship = () => guarded(() => {\n // serveChallenge() may have answered from inside the app, and it already shipped the\n // `blk: \"challenge\"` row — one request, one event.\n if (shipped || req.camadaChallenged) return;\n shipped = true;\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.queue!.push(ev);\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 private relayBeacon(req: IncomingMessage, res: ServerResponse, ip: string | null): void {\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 res.writeHead(204, { 'cache-control': 'no-store' });\n res.end();\n let parsed: unknown;\n try { parsed = JSON.parse(Buffer.concat(chunks).toString('utf8')); } catch { return; }\n if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return;\n this.queue!.push({ ...(parsed as Record<string, unknown>), sig: 1, ip, tap: TAP_NODE });\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, challengePage({ nonce: this.kit!.nonce(ip, Date.now()), action: this.challengePath, 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.queue!.push(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 readBody(req, (body) => guarded(() => {\n const form = parseFormBody(body);\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, challengePage({ nonce: this.kit!.nonce(ip, now), action: this.challengePath, 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.challengePath, '', 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.queue!.push(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 rawUrl = req.url || '/';\n const qi = rawUrl.indexOf('?');\n const path = qi === -1 ? rawUrl : rawUrl.slice(0, qi);\n const cfg = this.snap?.config;\n if ((cfg?.exclude || []).some((x) => path.startsWith(x)) || Math.random() >= (cfg?.sample ?? 1)) return;\n const ip = resolveClientIp(req.socket?.remoteAddress, req.headers['x-forwarded-for'] as string | undefined, this.trustedProxy());\n const ev = this.buildEvent(req, path, qi === -1 ? '' : rawUrl.slice(qi), 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.queue.push(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 = resolveClientIp(req.socket?.remoteAddress, req.headers['x-forwarded-for'] as string | undefined, this.trustedProxy());\n if (!ip || this.challengePassed(req, ip)) return false; // unidentifiable client: fail open\n (req as CamadaRequest).camadaChallenged = true;\n this.serveChallengeInner(req as CamadaRequest, res, ip, req.url || '/');\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 return `<script src=\"${this.scriptPath}${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 void (async () => {\n const uid = data?.user ? await hashUserId(data.user, this.env!.ingestToken, (globalThis.crypto ?? webcrypto).subtle) : null;\n this.queue!.push({ tap: TAP_NODE, et: event, uid, rid: ctx?.rid ?? null, sid: ctx?.sid ?? null, ip: ctx?.ip ?? null, ts: Date.now() });\n })().catch(logRateLimited);\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 stop(): void {\n this.snap?.stop();\n this.queue?.stop();\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)\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, type ChallengeKit } 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\nexport const isHttps = (req: IncomingMessage): boolean =>\n !!(req.socket as { encrypted?: boolean } | undefined)?.encrypted || req.headers['x-forwarded-proto'] === 'https';\n\nexport function writeChallengePage(res: ServerResponse, html: string): void {\n res.writeHead(403, { 'content-type': 'text/html; charset=utf-8', 'cache-control': 'no-store', 'x-camada-challenge': '1' });\n res.end(html);\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.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 \"types\": \"./dist/index.d.ts\",\n \"import\": \"./dist/index.js\",\n \"require\": \"./dist/index.cjs\"\n },\n \"./fastify\": {\n \"types\": \"./dist/fastify.d.ts\",\n \"import\": \"./dist/fastify.js\",\n \"require\": \"./dist/fastify.cjs\"\n },\n \"./nest\": {\n \"types\": \"./dist/nest.d.ts\",\n \"import\": \"./dist/nest.js\",\n \"require\": \"./dist/nest.cjs\"\n },\n \"./koa\": {\n \"types\": \"./dist/koa.d.ts\",\n \"import\": \"./dist/koa.js\",\n \"require\": \"./dist/koa.cjs\"\n }\n },\n \"main\": \"./dist/index.cjs\",\n \"module\": \"./dist/index.js\",\n \"types\": \"./dist/index.d.ts\",\n \"dependencies\": {\n \"@camada/browser\": \"file:../camada-browser\",\n \"@camada/core\": \"file:../camada-core\"\n },\n \"devDependencies\": {\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/node\": \"^22.10.0\",\n \"express\": \"^4.21.0\",\n \"fastify\": \"^5.2.0\",\n \"koa\": \"^2.15.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;;;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UAAM,yBAAW,KAAK,MAAM,KAAK,IAAK,cAAc,WAAW,UAAU,+BAAW,MAAM,IAAI;AACvH,aAAK,MAAO,KAAK,EAAE,KAAK,uBAAU,IAAI,OAAO,KAAK,KAAK,KAAK,OAAO,MAAM,KAAK,KAAK,OAAO,MAAM,IAAI,KAAK,MAAM,MAAM,IAAI,KAAK,IAAI,EAAE,CAAC;AAAA,MACvI,GAAG,EAAE,MAAM,2BAAc;AAAA,IAC3B,GAAG,MAAS;AAAA,EACd;AAAA;AAAA,EAGA,UAAU;AACR,WAAO,CAAC,KAAsB,KAAqB,SAAwC;AACzF,UAAI,CAAC,KAAK,OAAO,KAAK,GAAG,EAAG,MAAK;AAAA,IACnC;AAAA,EACF;AAAA,EAEA,OAAa;AACX,SAAK,MAAM,KAAK;AAChB,SAAK,OAAO,KAAK;AAAA,EACnB;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;;;AK3XA,IAAI,YAA2B;AACxB,SAAS,aAAqB;AACnC,SAAQ,cAAc,aAAa;AACrC;;;ANJO,SAAS,UAAU,QAAiB;AACzC,SAAO,OAAO,KAAgB,SAAgD;AAC5E,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-Cn5SJVIG.cjs';
2
+ import { C as Camada } from './camada-Dh_9D6Sx.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-Cn5SJVIG.js';
2
+ import { C as Camada } from './camada-Dh_9D6Sx.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-J7BQLOME.js";
3
+ } from "./chunk-CR3UHMBJ.js";
4
4
 
5
5
  // src/koa.ts
6
6
  function camadaKoa(engine) {
package/dist/nest.cjs CHANGED
@@ -55,7 +55,7 @@ function resolveEnv(env = process.env) {
55
55
  const ingestToken = key?.ingestToken ?? env.CAMADA_TOKEN;
56
56
  const snapToken = key?.snapToken ?? env.CAMADA_SNAPSHOT_TOKEN;
57
57
  if (!ingestToken || !snapToken) return null;
58
- const ingestUrl = (env.CAMADA_INGEST_URL || "https://in.camada.dev").replace(/\/$/, "");
58
+ const ingestUrl = (env.CAMADA_INGEST_URL || "https://in.camada.app").replace(/\/$/, "");
59
59
  return {
60
60
  ingestToken,
61
61
  snapToken,
@@ -106,7 +106,7 @@ function writeChallengeJson(res) {
106
106
  }
107
107
 
108
108
  // package.json
109
- var version = "0.2.0";
109
+ var version = "0.3.0";
110
110
  var name = "@camada/node";
111
111
 
112
112
  // src/version.ts
@@ -117,6 +117,7 @@ var SESSION_COOKIE = "_sfp";
117
117
  var SCRIPT_PATH = "/_cam/b.js";
118
118
  var FP_PATH = "/_cam/fp";
119
119
  var FP_MAX = 32 * 1024;
120
+ var ATTACHED = /* @__PURE__ */ Symbol.for("camada.attached");
120
121
  var headerReader = (req) => (name2) => {
121
122
  const v = req.headers[name2];
122
123
  return v == null ? null : Array.isArray(v) ? v.join(", ") : v;
@@ -129,6 +130,17 @@ var cookieValue = (cookie, name2) => {
129
130
  const j = src.indexOf(";", start);
130
131
  return src.slice(start, j === -1 ? void 0 : j);
131
132
  };
133
+ function keepSessionCookie(res, cookie) {
134
+ const set = res.setHeader;
135
+ res.setHeader = function(name2, value) {
136
+ if (typeof name2 === "string" && name2.toLowerCase() === "set-cookie") {
137
+ const list = Array.isArray(value) ? value : [String(value)];
138
+ if (!list.some((c) => String(c).trimStart().startsWith(SESSION_COOKIE + "="))) value = [...list, cookie];
139
+ }
140
+ return set.call(this, name2, value);
141
+ };
142
+ res.setHeader("set-cookie", cookie);
143
+ }
132
144
  var Camada = class {
133
145
  env;
134
146
  snap = null;
@@ -226,22 +238,27 @@ var Camada = class {
226
238
  if (!sid) {
227
239
  sid = (0, import_node_crypto2.randomUUID)();
228
240
  const https = req.socket.encrypted || req.headers["x-forwarded-proto"] === "https";
229
- res.setHeader("set-cookie", `${SESSION_COOKIE}=${sid}; Path=/; Max-Age=2592000; HttpOnly; SameSite=Lax${https ? "; Secure" : ""}`);
241
+ keepSessionCookie(res, `${SESSION_COOKIE}=${sid}; Path=/; Max-Age=2592000; HttpOnly; SameSite=Lax${https ? "; Secure" : ""}`);
230
242
  }
231
243
  req.camada = { rid, ip, sid };
232
244
  res.setHeader("x-rid", rid);
233
245
  const cfg = this.snap.config;
234
246
  const excluded = (cfg?.exclude || []).some((x) => path.startsWith(x));
235
247
  if (!excluded && Math.random() < (cfg?.sample ?? 1)) {
236
- res.on("finish", () => (0, import_core3.guarded)(() => {
237
- if (req.camadaChallenged) return;
248
+ let shipped = false;
249
+ const ship = () => (0, import_core3.guarded)(() => {
250
+ if (shipped || req.camadaChallenged) return;
251
+ shipped = true;
238
252
  const ev = this.buildEvent(req, path, query, ip, { rid, sid, newSession });
253
+ ev.ts = t0;
239
254
  ev.st = res.statusCode;
240
255
  ev.dur = Date.now() - t0;
241
256
  if (req.route?.path) ev.rt = String(req.route.path);
242
257
  if (v.warn && v.rule) ev.wrn = v.rule;
243
258
  this.queue.push(ev);
244
- }, void 0));
259
+ }, void 0);
260
+ res.once("finish", ship);
261
+ res.once("close", ship);
245
262
  }
246
263
  return false;
247
264
  }
@@ -343,6 +360,47 @@ var Camada = class {
343
360
  this.queue.push(ev);
344
361
  }, void 0));
345
362
  }
363
+ /**
364
+ * Records the upgrades (WebSocket handshakes) a node:http or node:https server hands to its
365
+ * 'upgrade' listeners: Node never routes those through the request handler the middleware runs
366
+ * in. Each one ships one event with `st: 101` once the app's listeners have run. Observe only:
367
+ * the request is never blocked or challenged, nothing is written to the socket, no session
368
+ * cookie is minted, and a server with no 'upgrade' listener of its own behaves exactly as before
369
+ * (this wraps `server.emit` rather than adding a listener, which would claim every upgrade).
370
+ * Idempotent; returns the server. A handshake the WebSocket library then rejects still ships 101.
371
+ */
372
+ attach(server) {
373
+ (0, import_core3.guarded)(() => {
374
+ const s = server;
375
+ if (this.disabled || !this.queue || s[ATTACHED]) return;
376
+ s[ATTACHED] = true;
377
+ const emit = s.emit.bind(s);
378
+ s.emit = ((event, ...args) => {
379
+ if (event !== "upgrade") return emit(event, ...args);
380
+ const t0 = Date.now();
381
+ try {
382
+ return emit(event, ...args);
383
+ } finally {
384
+ (0, import_core3.guarded)(() => this.shipUpgrade(args[0], t0), void 0);
385
+ }
386
+ });
387
+ }, void 0);
388
+ return server;
389
+ }
390
+ shipUpgrade(req, t0) {
391
+ if (this.disabled || !this.queue) return;
392
+ const rawUrl = req.url || "/";
393
+ const qi = rawUrl.indexOf("?");
394
+ const path = qi === -1 ? rawUrl : rawUrl.slice(0, qi);
395
+ const cfg = this.snap?.config;
396
+ if ((cfg?.exclude || []).some((x) => path.startsWith(x)) || Math.random() >= (cfg?.sample ?? 1)) return;
397
+ const ip = (0, import_core3.resolveClientIp)(req.socket?.remoteAddress, req.headers["x-forwarded-for"], this.trustedProxy());
398
+ const ev = this.buildEvent(req, path, qi === -1 ? "" : rawUrl.slice(qi), ip, { rid: (0, import_node_crypto2.randomUUID)(), sid: this.sessionOf(req), newSession: false });
399
+ ev.ts = t0;
400
+ ev.st = 101;
401
+ ev.dur = Date.now() - t0;
402
+ this.queue.push(ev);
403
+ }
346
404
  /** Serve the challenge for this request on demand — for a route the app wants to gate itself
347
405
  * (the example's /challenge-me). Returns false when the client already holds a valid `_cch`,
348
406
  * so the caller renders its own page. */