Skip to content

Commit

Permalink
Add combined_original_trackers_jusdomains to config. #103
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit cbd8a3e
Merge: 23c899f 300ab77
Author: jellizaveta <[email protected]>
Date:   Tue Aug 20 00:51:02 2024 +0300

    resolve conflict

commit 23c899f
Author: jellizaveta <[email protected]>
Date:   Tue Aug 20 00:48:31 2024 +0300

    add jsDoc comments, update variable names and linter

commit 300ab77
Author: Slava Leleka <[email protected]>
Date:   Tue Aug 20 00:19:08 2024 +0300

    script/src/update-combined.js edited online with Bitbucket

commit af3fe39
Author: jellizaveta <[email protected]>
Date:   Mon Aug 19 16:56:27 2024 +0300

    update comments

commit bbc8fe9
Author: jellizaveta <[email protected]>
Date:   Mon Aug 19 16:15:50 2024 +0300

    update the function that creates the rule

commit d548361
Author: jellizaveta <[email protected]>
Date:   Mon Aug 19 15:15:43 2024 +0300

    Add combined_original_trackers_jusdomains. #103
  • Loading branch information
jellizaveta committed Aug 20, 2024
1 parent ad2d39a commit 5eefe80
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 81 deletions.
4 changes: 3 additions & 1 deletion script/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ module.exports = {
ecmaVersion: 12,
},
rules: {
indent: ['error', 4],
indent: ['error', 4, {
SwitchCase: 1,
}],
'no-console': 'off',
'import/no-extraneous-dependencies': 'off',
'max-len': ['error', {
Expand Down
32 changes: 12 additions & 20 deletions script/src/build-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const {

const {
rpzHeaderChunks,
CONST_DATA,
FORMATS,
} = require('./constants');

/**
Expand All @@ -15,22 +15,22 @@ const {
/**
* Builds rules content by type
* @param {TrackersData} trackersData
* @param {'BASE'|'HOSTS'|'RPZ'} type rules type
* @param {'BASE'|'HOSTS'|'RPZ'} ruleType rules type
* @returns {string}
*/
const buildRulesByType = (trackersData, type) => {
const buildRulesByType = (trackersData, ruleType) => {
const {
companyName,
trackersInfoItems,
} = trackersData;

const commonChunks = [
CONST_DATA[type].commentMarker,
`${CONST_DATA[type].commentMarker} Company name: ${companyName}`,
FORMATS[ruleType].commentMarker,
`${FORMATS[ruleType].commentMarker} Company name: ${companyName}`,
];

// add specific header for rpz file format
const headerRulesChunks = type === CONST_DATA.RPZ.type ? [...commonChunks, ...rpzHeaderChunks] : commonChunks;
const headerRulesChunks = ruleType === FORMATS.RPZ.type ? [...commonChunks, ...rpzHeaderChunks] : commonChunks;
headerRulesChunks.push('');
const rulesChunks = [];
// get only disguised trackers items in array
Expand All @@ -39,18 +39,10 @@ const buildRulesByType = (trackersData, type) => {
.flatMap((trackersItem) => trackersItem.disguises)
.filter(identity);

let getRule;

if (type === CONST_DATA.BASE.type) {
getRule = composeRuleWithNewline.baseRule;
} else if (type === CONST_DATA.HOSTS.type) {
getRule = composeRuleWithNewline.hostsRule;
} else if (type === CONST_DATA.RPZ.type) {
getRule = composeRuleWithNewline.rpzRule;
}
const getRule = composeRuleWithNewline[ruleType];

if (!getRule) {
throw new Error(`Unknown type: ${type}`);
throw new Error(`Unknown type: ${ruleType}`);
}

// make rule chunks by type
Expand All @@ -59,7 +51,7 @@ const buildRulesByType = (trackersData, type) => {
});

/* Ensure there is a newline at the end of RPZ files. */
if (type === CONST_DATA.RPZ.type) {
if (ruleType === FORMATS.RPZ.type) {
rulesChunks.length += 1;
}

Expand All @@ -80,9 +72,9 @@ const buildRulesByType = (trackersData, type) => {
* @returns {RulesContent} content for base and hosts rules files
*/
const buildRules = async (trackersData) => {
const baseRulesString = buildRulesByType(trackersData, CONST_DATA.BASE.type);
const hostsRulesString = buildRulesByType(trackersData, CONST_DATA.HOSTS.type);
const rpzRulesString = buildRulesByType(trackersData, CONST_DATA.RPZ.type);
const baseRulesString = buildRulesByType(trackersData, FORMATS.BASE.type);
const hostsRulesString = buildRulesByType(trackersData, FORMATS.HOSTS.type);
const rpzRulesString = buildRulesByType(trackersData, FORMATS.RPZ.type);

return { baseRulesString, hostsRulesString, rpzRulesString };
};
Expand Down
87 changes: 67 additions & 20 deletions script/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,32 @@ const RULES_FILE_EXTENSION = 'txt';
const COMBINED_RULES_FILE_NAME_BASE = 'combined_disguised';
const HOSTS_RULES_FILE_NAME_ENDING = '_justdomains';
const RPZ_RULES_FILE_NAME_ENDING = '_rpz';
const COMBINED_ORIGINALS_FILE_NAME_BASE = 'combined_original_trackers';
const COMBINED_ORIGINALS_FILE_NAME = 'combined_original_trackers';

const COMBINED_ORIGINALS_FILE_NAME = `${COMBINED_ORIGINALS_FILE_NAME_BASE}.${RULES_FILE_EXTENSION}`;
const COMBINED_JSON_FILE_NAME = `${COMBINED_RULES_FILE_NAME_BASE}.${JSON_FILE_EXTENSION}`;
const BASE_RULES_TYPE = 'BASE';
const HOSTS_RULES_TYPE = 'HOSTS';
const RPZ_RULES_TYPE = 'RPZ';

/**
* Create the file name for the `combined_original_trackers` lists
* based on the provided rule type.
*
* @param {string} ruleType - The type of rule, which determines the file name format.
* Expected values are `BASE_RULES_TYPE` or `HOSTS_RULES_TYPE`.
* @returns {string} The generated file name corresponding to the provided rule type.
*
* @throws {Error} Throws an error if an unknown rule type is provided.
*/
const getOriginalsFileName = (ruleType) => {
switch (ruleType) {
case BASE_RULES_TYPE:
return `${COMBINED_ORIGINALS_FILE_NAME}.${RULES_FILE_EXTENSION}`;
case HOSTS_RULES_TYPE:
return `${COMBINED_ORIGINALS_FILE_NAME}${HOSTS_RULES_FILE_NAME_ENDING}.${RULES_FILE_EXTENSION}`;
default:
throw new Error(`Unknown type: ${ruleType}`);
}
};

// Length of the comment line in rpz files
const RPZ_LINE_COMMENT_LENGTH = 24;
Expand Down Expand Up @@ -165,19 +187,38 @@ const getRpzRulesCombinedHeader = (type) => [
...rpzHeaderChunks,
].join('\n');

const originalsCombinedHeader = [
`${BASE_RULE_COMMENT_MARKER} ${COMBINED_ORIGINAL_TRACKERS_HEADER_TITLE}`,
`${BASE_RULE_COMMENT_MARKER} ${COMBINED_ORIGINAL_TRACKERS_HEADER_DESC}`,
`${BASE_RULE_COMMENT_MARKER} ${COMBINED_FILTER_LIST_HEADER_TIME_UPDATED}`,
`${BASE_RULE_COMMENT_MARKER} ${COMBINED_FILTER_LIST_HEADER_HOMEPAGE}`,
`${BASE_RULE_COMMENT_MARKER}`,
].join('\n');

const BASE_RULES_TYPE = 'BASE';
const HOSTS_RULES_TYPE = 'HOSTS';
const RPZ_RULES_TYPE = 'RPZ';
/**
* Creates a combined header string for `combined_original_trackers` files
* based on the provided rule type.
*
* @param {string} ruleType - The type of rule, which determines the comment marker used in the header.
* Expected values are `BASE_RULES_TYPE` or `HOSTS_RULES_TYPE`.
* @returns {string} The generated combined header as a multi-line string.
*
* @throws {Error} Throws an error if an unknown rule type is provided.
*/
const getOriginalsCombinedHeader = (ruleType) => {
let COMMENT_MARKER;
switch (ruleType) {
case BASE_RULES_TYPE:
COMMENT_MARKER = BASE_RULE_COMMENT_MARKER;
break;
case HOSTS_RULES_TYPE:
COMMENT_MARKER = HOSTS_RULE_COMMENT_MARKER;
break;
default:
throw new Error(`Unknown type: ${ruleType}`);
}
return [
`${COMMENT_MARKER} ${COMBINED_ORIGINAL_TRACKERS_HEADER_TITLE}`,
`${COMMENT_MARKER} ${COMBINED_ORIGINAL_TRACKERS_HEADER_DESC}`,
`${COMMENT_MARKER} ${COMBINED_FILTER_LIST_HEADER_TIME_UPDATED}`,
`${COMMENT_MARKER} ${COMBINED_FILTER_LIST_HEADER_HOMEPAGE}`,
`${COMMENT_MARKER}`,
].join('\n');
};

const CONST_DATA = {
const FORMATS = {
[BASE_RULES_TYPE]: {
type: BASE_RULES_TYPE,
commentMarker: BASE_RULE_COMMENT_MARKER,
Expand All @@ -191,9 +232,16 @@ const CONST_DATA = {
commentMarker: RPZ_RULE_COMMENT_MARKER,
},
ORIGINALS: {
commentMarker: BASE_RULE_COMMENT_MARKER,
combinedHeader: originalsCombinedHeader,
combinedFileName: COMBINED_ORIGINALS_FILE_NAME,
[BASE_RULES_TYPE]: {
commentMarker: BASE_RULE_COMMENT_MARKER,
combinedHeader: getOriginalsCombinedHeader(BASE_RULES_TYPE),
combinedFileName: getOriginalsFileName(BASE_RULES_TYPE),
},
[HOSTS_RULES_TYPE]: {
commentMarker: HOSTS_RULE_COMMENT_MARKER,
combinedHeader: getOriginalsCombinedHeader(HOSTS_RULES_TYPE),
combinedFileName: getOriginalsFileName(HOSTS_RULES_TYPE),
},
},
};

Expand All @@ -205,10 +253,9 @@ module.exports = {
MAX_RPZ_RULE_LENGTH,
RULES_FILE_EXTENSION,
JSON_FILE_EXTENSION,
COMBINED_JSON_FILE_NAME,
HOSTS_RULES_FILE_NAME_ENDING,
RPZ_RULES_FILE_NAME_ENDING,
CONST_DATA,
FORMATS,
rpzHeaderChunks,
getBaseRulesCombinedHeader,
getHostsRulesCombinedHeader,
Expand Down
16 changes: 10 additions & 6 deletions script/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ const fs = require('fs-extra');
const path = require('path');
const { logger } = require('./logger');

const { COMBINED_RULES_FILE_NAME_BASE, MAX_RPZ_RULE_LENGTH } = require('./constants');
const {
FORMATS,
COMBINED_RULES_FILE_NAME_BASE,
MAX_RPZ_RULE_LENGTH,
} = require('./constants');

const DATA_DIR_PATH = './../data';

Expand Down Expand Up @@ -63,7 +67,7 @@ const composeRuleWithNewline = {
* @param {string} disguise - The disguise domain to validate and compose
* @returns {string} - The composed RPZ rule if valid, otherwise an empty string
*/
rpzRule: (disguise) => {
[FORMATS.RPZ.type]: (disguise) => {
const ruleString = createRpzRule(disguise);
if (!isValidPrzString(ruleString)) {
return '';
Expand All @@ -76,19 +80,19 @@ const composeRuleWithNewline = {
* @param {string} disguise - The disguise domain to validate and compose
* @returns {string} - The composed hosts rule if valid, otherwise an empty string
*/
hostsRule: (disguise) => `${createHostsRule(disguise)}\n`,
[FORMATS.HOSTS.type]: (disguise) => `${createHostsRule(disguise)}\n`,

/**
* Composes a base rule, new line is added at the end
* @param {string} disguise - The disguise domain to validate and compose
* @returns {string} - The composed base rule if valid, otherwise an empty string
*/
baseRule: (disguise) => `${createBaseRule(disguise)}\n`,
[FORMATS.BASE.type]: (disguise) => `${createBaseRule(disguise)}\n`,
};

/**
* Writes `fileContent` to `fileName` in library root directory
* @param {string} fileName property of CONST_DATA object for required file
* @param {string} fileName property of FORMATS object for required file
* @param {object} fileContent writeable data
*/
const writeCombinedFile = async (fileName, fileContent) => fs.writeFile(
Expand All @@ -111,7 +115,7 @@ const replace = (str, replaceable, replacement) => str

/**
* Changes the company name to the correct format for file naming
* @param {string} companyName property of CONST_DATA object for required file
* @param {string} companyName property of FORMATS object for required file
* @returns {string}
*/
const formatFilename = (companyName) => {
Expand Down
Loading

0 comments on commit 5eefe80

Please sign in to comment.