caelum0x bb856083ad feat(insights-edge): add session replay (rrweb), paid-tier gated
Adopts rrweb + rrweb-player (MIT, vendored as prebuilt UMD bundles via
scripts/vendor-rrweb.mjs, see NOTICE) to add cookieless session replay to
Railyard Insights, gated behind the existing Dodo paid tier.

- src/replay/config.mjs: per-site settings with privacy-first floors/defaults
  (off by default, sample rate, maskAllInputs, maskTextSelector, per-session
  byte/duration caps, monthly session cap) — every cap is clamped server-side
  regardless of what a site's stored config asks for.
- src/replay/ingest.mjs: POST /api/replay chunk ingestion — bot/DNT/GPC drop,
  paid-tier + enabled gate, hostname check, and all caps enforced again on
  the server (not just trusted from the client).
- src/replay/store.mjs: D1 replay_sessions metadata + R2 event chunks
  (<siteKey>/<sessionId>/<seq>.json); wired into the existing site-delete
  flow so deleting a site removes its sessions too.
- src/replay/tracker.mjs, routes.mjs, pages.mjs: the opt-in /replay.js tag,
  the dashboard's settings + session list + player pages (matching the
  existing Insights dashboard design), and the events API — all behind the
  same private dashboard token, so a site owner can only ever see and play
  back their own site's sessions.
- wrangler.jsonc: new R2 binding (railyard-insights-replay).

Tests: 23 new (config validation/clamping, ingestion caps, dashboard auth
scoping, a regression test for the vendored player's actual constructor
shape) + existing 52, all 75 passing. Verified end-to-end locally with
wrangler dev + a real browser: recorded a session, played it back with
rrweb-player, screenshots in /tmp/railyard-replay-*.png.

No deploy, no merge to railyard main from this commit alone (see
plans/railyard.md for the deploy/rollback steps).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 03:06:17 +03:00

62 lines
2.4 KiB
JavaScript

// Regenerates src/vendor/*.generated.mjs from the pinned rrweb / rrweb-player
// npm packages (MIT, rrweb-io/rrweb). Run `npm run vendor:rrweb` after
// bumping the versions in package.json. The Worker never reads node_modules
// at runtime; these generated files are the only thing it imports, so they
// are committed.
import { readFileSync, writeFileSync } from "node:fs";
import { createRequire } from "node:module";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
const require = createRequire(import.meta.url);
const ROOT = join(dirname(fileURLToPath(import.meta.url)), "..");
const OUT = join(ROOT, "src", "vendor");
const MODULES = join(ROOT, "node_modules");
function pkgDir(name) {
return join(MODULES, name);
}
function pkgVersion(name) {
return JSON.parse(readFileSync(join(pkgDir(name), "package.json"), "utf8")).version;
}
function readDist(name, file) {
return readFileSync(join(pkgDir(name), "dist", file), "utf8");
}
function writeGenerated(file, { source, comment, exportName, content }) {
const banner = `// GENERATED by scripts/vendor-rrweb.mjs from ${source} — do not hand-edit.\n// ${comment}\nexport const ${exportName} = ${JSON.stringify(content)};\n`;
writeFileSync(join(OUT, file), banner);
console.log(`wrote ${file} (${content.length} bytes)`);
}
const rrwebVersion = pkgVersion("rrweb");
const playerVersion = pkgVersion("rrweb-player");
writeGenerated("rrweb-recorder.generated.mjs", {
source: `rrweb@${rrwebVersion} dist/rrweb.umd.min.cjs`,
comment: "MIT, Copyright (c) rrweb-io/rrweb contributors. Exposes window.rrweb.record().",
exportName: "RRWEB_RECORDER_JS",
content: readDist("rrweb", "rrweb.umd.min.cjs"),
});
writeGenerated("rrweb-player.generated.mjs", {
source: `rrweb-player@${playerVersion} dist/rrweb-player.umd.min.cjs`,
comment: "MIT, Copyright (c) rrweb-io/rrweb contributors. Exposes window.rrwebPlayer.",
exportName: "RRWEB_PLAYER_JS",
content: readDist("rrweb-player", "rrweb-player.umd.min.cjs"),
});
writeGenerated("rrweb-player.generated.css.mjs", {
source: `rrweb-player@${playerVersion} dist/style.min.css`,
comment: "MIT, Copyright (c) rrweb-io/rrweb contributors.",
exportName: "RRWEB_PLAYER_CSS",
content: readDist("rrweb-player", "style.min.css"),
});
writeFileSync(
join(OUT, "VERSIONS.json"),
JSON.stringify({ rrweb: rrwebVersion, "rrweb-player": playerVersion, generated_at: new Date().toISOString() }, null, 2) + "\n",
);