```javascript
#!/usr/bin/env node
// Stage 2 — Filter.
//
// Apply --include / --exclude / --origins on top of paired.jsonl. Default
// excludes scrub analytics, sourcemaps, fonts, and other static-asset noise
// that the load stage may have let through (e.g. when looksApiUrl matched).

import { readJsonl, writeJsonl, intermediatePath } from './lib/io.mjs';

const DEFAULT_EXCLUDES = [
  // Analytics / RUM / session replay
  /segment\.(io|com)/i,
  /mixpanel\.com/i,
  /google-analytics\.com/i,
  /googletagmanager\.com/i,
  /datadog(hq)?\.com/i,
  /sentry\.io/i,
  /amplitude\.com/i,
  /fullstory\.com/i,
  /hotjar\.com/i,
  /intercom\.io/i,
  /clarity\.ms/i,
  /cloudflareinsights\.com/i,
  /doubleclick\.net/i,
  /facebook\.com\/tr/i,
  // Static assets
  /\.(png|jpe?g|gif|svg|webp|ico|woff2?|ttf|eot|otf|css|map|mp4|webm|mp3|m4a)(\?|$)/i,
  // SW / metadata
  /\/sw\.js(\?|$)/i,
  /\/service-worker\.js(\?|$)/i,
  /\/manifest\.json(\?|$)/i,
  /\/robots\.txt(\?|$)/i,
  /\/favicon\.ico(\?|$)/i,
];

export function filter(outDir, opts = {}) {
  const { include = [], exclude = [], origins = [] } = opts;
  // Precedence:
  //   1. --origins gates everything; non-matching is dropped.
  //   2. User --exclude always wins (explicit user intent).
  //   3. Default excludes can be rescued by --include (REFERENCE.md contract).
  //   4. When --include is set, anything that doesn't match it is dropped.
  const userExcludeRes = exclude.map(s => new RegExp(s));
  const includeRes = include.map(s => new RegExp(s));
  const originSet = new Set(origins);

  const paired = readJsonl(intermediatePath(outDir, 'paired.jsonl'));
  const out = [];
  let droppedOrigin = 0, droppedExclude = 0, droppedInclude = 0;

  for (const row of paired) {
    if (originSet.size) {
      const host = row.origin ? new URL(row.origin).host : '';
      const matched = [...originSet].some(o => host === o || host.endsWith('.' + o));
      if (!matched) { droppedOrigin++; continue; }
    }
    if (userExcludeRes.some(re => re.test(row.url))) { droppedExclude++; continue; }

    const matchesInclude = includeRes.length > 0 && includeRes.some(re => re.test(row.url));
    const matchesDefaultExclude = DEFAULT_EXCLUDES.some(re => re.test(row.url));
    if (matchesDefaultExclude && !matchesInclude) { droppedExclude++; continue; }
    if (includeRes.length && !matchesInclude) { droppedInclude++; continue; }

    out.push(row);
  }

  writeJsonl(intermediatePath(outDir, 'filtered.jsonl'), out);
  return { kept: out.length, droppedOrigin, droppedExclude, droppedInclude };
}

if (import.meta.url === `file://${process.argv[1]}`) {
  const out = process.argv[2];
  if (!out) { console.error('usage: filter.mjs <out-dir>'); process.exit(2); }
  const stats = filter(out);
  console.log(`filter: kept ${stats.kept}, dropped ${stats.droppedExclude} (exclude) ${stats.droppedOrigin} (origin) ${stats.droppedInclude} (include)`);
}
```

---

Copyright (c) 2026 Browserbase, Inc. License files are attached. [MIT](https://knowen.md/f/2dce65ed-63fe-4376-bd9e-3ceea6640765)

### MIT notice

````text
MIT License

Copyright (c) 2026 Browserbase, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
````