Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ICANN boolean to parse result (bis) #338

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import rules from './data/rules.js';
// Parse rules from file.
//
const rulesByPunySuffix = rules.reduce(
(map, rule) => {
(map, [rule, icann]) => {
const suffix = rule.replace(/^(\*\.|\!)/, '');
const punySuffix = punycode.toASCII(suffix);
const firstChar = rule.charAt(0);
Expand All @@ -19,7 +19,8 @@ const rulesByPunySuffix = rules.reduce(
suffix,
punySuffix,
wildcard: firstChar === '*',
exception: firstChar === '!'
exception: firstChar === '!',
icann,
});

return map;
Expand Down Expand Up @@ -151,7 +152,8 @@ export const parse = (input) => {
sld: null,
domain: null,
subdomain: null,
listed: false
listed: false,
icann: false
};

const domainParts = domain.split('.');
Expand Down Expand Up @@ -193,6 +195,7 @@ export const parse = (input) => {

// At this point we know the public suffix is listed.
parsed.listed = true;
parsed.icann = rule.icann;

const tldParts = rule.suffix.split('.');
const privateParts = domainParts.slice(0, domainParts.length - tldParts.length);
Expand Down
8 changes: 7 additions & 1 deletion scripts/update-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { writeFile } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

const END_ICANN_REGION = '// ===END ICANN DOMAINS===';
const __dirname = path.dirname(fileURLToPath(import.meta.url));

//
Expand All @@ -15,6 +16,8 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
const src = 'https://publicsuffix.org/list/effective_tld_names.dat';
const dest = path.join(__dirname, '../data/rules.js');

global.inIcannRegion = true;

//
// Parse line (trim and ignore empty lines and comments).
//
Expand All @@ -23,12 +26,15 @@ const parseLine = (line) => {

// Ignore empty lines and comments.
if (!trimmed || (trimmed.charAt(0) === '/' && trimmed.charAt(1) === '/')) {
if (trimmed === END_ICANN_REGION) {
global.inIcannRegion = false;
}
return;
}

// Only read up to first whitespace char.
const rule = trimmed.split(' ')[0];
return rule;
return [rule, (global.inIcannRegion) ? true : false];

// const item = [rule];
//
Expand Down
Loading