#!/usr/bin/env node // Refreshes the bundled copy of the Open Cookie Database (Apache-2.0, // https://github.com/jkwakman/Open-Cookie-Database) that the cookie scanner // looks unknown cookie names up in. Nothing is fetched at runtime — run this, // review the diff, commit. // // pnpm cookie-db:refresh # latest commit on master // pnpm cookie-db:refresh --ref // // Writes: // src/modules/consent/scanner/data/openCookieDatabase.ts (rows, source commit, date) // docs/licenses/open-cookie-database-LICENSE.txt (the upstream license, verbatim) import { mkdirSync, writeFileSync } from 'node:fs' import path from 'node:path' import { fileURLToPath } from 'node:url' const REPO = 'jkwakman/Open-Cookie-Database' const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..') const args = process.argv.slice(2) const refArg = args.indexOf('--ref') >= 0 ? args[args.indexOf('--ref') + 1] : undefined async function get(url) { const res = await fetch(url, { headers: { 'User-Agent': 'IAPL cookie-db refresh' } }) if (!res.ok) {throw new Error(`${url}: HTTP ${res.status}`)} return res } const ref = refArg ?? (await (await get(`https://api.github.com/repos/${REPO}/commits/master`)).json()).sha const raw = (file) => `https://raw.githubusercontent.com/${REPO}/${ref}/${file}` const csv = await (await get(raw('open-cookie-database.csv'))).text() const license = await (await get(raw('LICENSE'))).text() if (!/Apache License\s+Version 2\.0/.test(license)) { console.error('The upstream license is no longer Apache-2.0 — check it before bundling. Nothing written.') process.exit(1) } /** RFC 4180 CSV: quoted fields, "" escapes, newlines inside quotes. */ function parseCsv(text) { const rows = [] let row = [] let field = '' let quoted = false for (let i = 0; i < text.length; i++) { const ch = text[i] if (quoted) { if (ch === '"' && text[i + 1] === '"') {field += '"'; i++} else if (ch === '"') {quoted = false} else {field += ch} } else if (ch === '"') {quoted = true} else if (ch === ',') {row.push(field); field = ''} else if (ch === '\n' || ch === '\r') { if (ch === '\r' && text[i + 1] === '\n') {i++} row.push(field); field = '' if (row.some((f) => f !== '')) {rows.push(row)} row = [] } else {field += ch} } if (field || row.length) {row.push(field); rows.push(row)} return rows } const [header, ...body] = parseCsv(csv) const col = (name) => { const i = header.findIndex((h) => h.trim().toLowerCase() === name.toLowerCase()) if (i < 0) {throw new Error(`Column "${name}" missing — the upstream CSV changed, update this script.`)} return i } const C = { category: col('Category'), controller: col('Data Controller'), description: col('Description'), domain: col('Domain'), name: col('Cookie / Data Key name'), platform: col('Platform'), retention: col('Retention period'), wildcard: col('Wildcard match'), } const clean = (s) => (s ?? '').replace(/\s+/g, ' ').trim() const rows = body .map((r) => [ clean(r[C.name]), clean(r[C.platform]), clean(r[C.category]), clean(r[C.domain]), clean(r[C.description]), clean(r[C.retention]), clean(r[C.controller]), clean(r[C.wildcard]) === '1' ? 1 : 0, ]) .filter((r) => r[0]) const today = new Date().toISOString().slice(0, 10) const out = `/* eslint-disable */ // GENERATED by scripts/refresh-cookie-database.mjs — do not edit by hand. // // Open Cookie Database, https://github.com/${REPO} // Commit ${ref}, fetched ${today}. // Licensed under the Apache License, Version 2.0 — full text in // docs/licenses/open-cookie-database-LICENSE.txt (shipped with the package). // // Changes made by ipal-kit: converted from open-cookie-database.csv to this // module; the columns "ID" and "User Privacy & GDPR Rights Portals" were left // out and whitespace was collapsed. The data is otherwise unchanged. /** [name, platform, category, domain, description, retention, data controller, wildcard (1 = name is a prefix)] */ export type OpenCookieRow = readonly [string, string, string, string, string, string, string, 0 | 1] export const OPEN_COOKIE_DATABASE_SOURCE = { commit: '${ref}', fetchedAt: '${today}', license: 'Apache-2.0', url: 'https://github.com/${REPO}', } as const export const OPEN_COOKIE_DATABASE: readonly OpenCookieRow[] = [ ${rows.map((r) => ` ${JSON.stringify(r)},`).join('\n')} ] ` const dataFile = path.join(root, 'src/modules/consent/scanner/data/openCookieDatabase.ts') mkdirSync(path.dirname(dataFile), { recursive: true }) writeFileSync(dataFile, out) const licenseFile = path.join(root, 'docs/licenses/open-cookie-database-LICENSE.txt') mkdirSync(path.dirname(licenseFile), { recursive: true }) writeFileSync(licenseFile, license) console.log(`Open Cookie Database @ ${ref.slice(0, 7)}: ${rows.length} rows → ${path.relative(root, dataFile)}`)