Linux + iPhone Continuity / iMessage / SMS
#!/usr/bin/env node
// Regenerates the shared Public Suffix List data used by the JS extension and
// the C++ daemon from the canonical PSL file.
//
// Usage:
// node scripts/generate-psl.mjs [path-or-url]
//
// Defaults to /tmp/public_suffix_list.dat. Pull the list from
// https://publicsuffix.org/list/public_suffix_list.dat (and only from there).
import { readFileSync, writeFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
const here = dirname(fileURLToPath(import.meta.url));
const root = join(here, '..');
const source = process.argv[2] || '/tmp/public_suffix_list.dat';
const data = readFileSync(source, 'utf8');
// The PSL ships some IDN rules in Unicode (e.g. 公司.cn). The reference
// implementation compares punycoded names, so convert those to ASCII here; the
// runtime inputs (browser hostname, DKIM d=) are already punycoded ASCII.
const toAscii = (rule) => {
let prefix = '';
let body = rule;
if (rule.startsWith('*.')) {
prefix = '*.';
body = rule.slice(2);
} else if (rule.startsWith('!')) {
prefix = '!';
body = rule.slice(1);
}
if (/[^\x00-\x7F]/.test(body)) {
try {
body = new URL('http://' + body).hostname;
} catch {
// Leave the rule as-is if it cannot be parsed as a hostname.
}
}
return prefix + body;
};
const rules = [];
const seen = new Set();
let version = '';
for (const raw of data.split('\n')) {
const line = raw.trim();
if (!line) continue;
if (line.startsWith('//')) {
if (line.startsWith('// VERSION:')) version = line.slice('// VERSION:'.length).trim();
continue;
}
const rule = toAscii(line);
if (!seen.has(rule)) {
seen.add(rule);
rules.push(rule);
}
}
// Rules are domain labels (possibly prefixed with `*.` or `!`); none of these
// characters appear, but escape defensively for the JS template literal.
const escapeJs = (s) =>
s.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/\$\{/g, '\\${');
const label = version || 'unknown version';
const js = `// Generated by scripts/generate-psl.mjs from the Public Suffix List.
// ${label}
// https://publicsuffix.org/list/
// Rules are newline-separated: '*.' prefix = wildcard rule, '!' prefix = exception.
export const PSL_DATA = \`${escapeJs(rules.join('\n'))}\`;
`;
const cpp = `// Generated by scripts/generate-psl.mjs from the Public Suffix List.
// ${label}
// https://publicsuffix.org/list/
// Rules are newline-separated: '*.' prefix = wildcard rule, '!' prefix = exception.
#pragma once
#include <string_view>
namespace tether {
inline constexpr std::string_view kPslData = R"PSL(
${rules.join('\n')}
)PSL";
} // namespace tether
`;
writeFileSync(join(root, 'extension', 'src', 'shared', 'psl.js'), js);
writeFileSync(join(root, 'src', 'core', 'src', 'psl_data.hpp'), cpp);
console.log(`wrote ${rules.length} PSL rules (${label})`);