Skip to content

Commit

Permalink
Merge branch 'release/v1.7' into feature/AG-17679
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Nov 21, 2022
2 parents f28e33d + 4ad761d commit bccaff1
Show file tree
Hide file tree
Showing 59 changed files with 536 additions and 482 deletions.
3 changes: 1 addition & 2 deletions src/helpers/adjust-set-utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { nativeIsNaN, nativeIsFinite } from './number-utils';
import { getWildcardSymbol } from './constants';

export const shouldMatchAnyDelay = (delay) => delay === getWildcardSymbol();
export const shouldMatchAnyDelay = (delay) => delay === '*';

/**
* Handles input delay value
Expand Down
5 changes: 0 additions & 5 deletions src/helpers/constants.js

This file was deleted.

4 changes: 1 addition & 3 deletions src/helpers/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import {

import { isExisting } from './array-utils';

import { getWildcardSymbol } from './constants';

import validator from './validator';

import { parseRule } from './parse-rule';
Expand Down Expand Up @@ -48,7 +46,7 @@ const UBO_SET_CONSTANT_EMPTY_STRING = '\'\'';

const ADG_PREVENT_FETCH_NAME = 'prevent-fetch';
const ADG_PREVENT_FETCH_EMPTY_STRING = '';
const ADG_PREVENT_FETCH_WILDCARD = getWildcardSymbol();
const ADG_PREVENT_FETCH_WILDCARD = '*';
const UBO_NO_FETCH_IF_WILDCARD = '/^/';

const ESCAPED_COMMA_SEPARATOR = '\\,';
Expand Down
20 changes: 13 additions & 7 deletions src/helpers/cookie-utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { nativeIsNaN } from './number-utils';
import { logMessage } from './log-message';

/**
* @typedef { import('../scriptlets/index').Source } Source
*/

/**
* Checks whether the input path is supported
Expand Down Expand Up @@ -30,16 +35,16 @@ export const getCookiePath = (rawPath) => {
/**
* Combines input cookie name, value, and path into string.
*
* @param {Source} source
* @param {string} rawName
* @param {string} rawValue
* @param {string} rawPath
*
* @returns {string} string OR `null` if path is not supported
*/
export const concatCookieNameValuePath = (rawName, rawValue, rawPath) => {
const log = console.log.bind(console); // eslint-disable-line no-console
export const concatCookieNameValuePath = (source, rawName, rawValue, rawPath) => {
if (!isValidCookieRawPath(rawPath)) {
log(`Invalid cookie path: '${rawPath}'`);
logMessage(source, `Invalid cookie path: '${rawPath}'`);
return null;
}
// eslint-disable-next-line max-len
Expand All @@ -49,15 +54,16 @@ export const concatCookieNameValuePath = (rawName, rawValue, rawPath) => {
/**
* Gets supported cookie value
*
* @param {Source} source
* @param {string} value input cookie value
*
* @returns {string|null} valid cookie string if ok OR null if not
*/
export const getLimitedCookieValue = (value) => {
export const getLimitedCookieValue = (source, value) => {
if (!value) {
return null;
}
const log = console.log.bind(console); // eslint-disable-line no-console

let validValue;
if (value === 'true') {
validValue = 'true';
Expand All @@ -82,11 +88,11 @@ export const getLimitedCookieValue = (value) => {
} else if (/^\d+$/.test(value)) {
validValue = parseFloat(value);
if (nativeIsNaN(validValue)) {
log(`Invalid cookie value: '${value}'`);
logMessage(source, `Invalid cookie value: '${value}'`);
return null;
}
if (Math.abs(validValue) < 0 || Math.abs(validValue) > 15) {
log(`Invalid cookie value: '${value}'`);
logMessage(source, `Invalid cookie value: '${value}'`);
return null;
}
} else {
Expand Down
6 changes: 2 additions & 4 deletions src/helpers/get-wildcard-property-in-chain.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { getWildcardSymbol } from './constants';

/**
* @typedef Chain
* @property {Object} base
Expand All @@ -23,7 +21,7 @@ export function getWildcardPropertyInChain(base, chain, lookThrough = false, out
const pos = chain.indexOf('.');
if (pos === -1) {
// for paths like 'a.b.*' every final nested prop should be processed
if (chain === getWildcardSymbol() || chain === '[]') {
if (chain === '*' || chain === '[]') {
// eslint-disable-next-line no-restricted-syntax
for (const key in base) {
// to process each key in base except inherited ones
Expand All @@ -41,7 +39,7 @@ export function getWildcardPropertyInChain(base, chain, lookThrough = false, out
const prop = chain.slice(0, pos);

const shouldLookThrough = (prop === '[]' && Array.isArray(base))
|| (prop === getWildcardSymbol() && base instanceof Object);
|| (prop === '*' && base instanceof Object);

if (shouldLookThrough) {
const nextProp = chain.slice(pos + 1);
Expand Down
41 changes: 21 additions & 20 deletions src/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
/**
* This file must export all used dependencies
*/
export * from './constants';
export * from './random-id';
export * from './set-property-access';
export * from './get-property-in-chain';
export * from './get-wildcard-property-in-chain';
export * from './string-utils';
export * from './create-on-error-handler';
export * from './noop';
export * from './hit';
export * from './observer';
export * from './match-stack';
export * from './script-source-utils';
export * from './open-shadow-dom-utils';

export * from './add-event-listener-utils';
export * from './adjust-set-utils';
export * from './array-utils';
export * from './cookie-utils';
export * from './noop-utils';
export * from './number-utils';
export * from './adjust-set-utils';
export * from './request-utils';
export * from './object-utils';
export * from './prevent-window-open-utils';
export * from './add-event-listener-utils';
export * from './script-source-utils';
export * from './open-shadow-dom-utils';
export * from './prevent-utils';
export * from './prevent-window-open-utils';
export * from './regexp-utils';
export * from './random-response';
export * from './request-utils';
export * from './storage-utils';
export * from './string-utils';

export * from './log-message';
export * from './create-on-error-handler';
export * from './get-descriptor-addon';
export * from './parse-flags';
export * from './get-property-in-chain';
export * from './get-wildcard-property-in-chain';
export * from './hit';
export * from './match-request-props';
export * from './storage-utils';
export * from './match-stack';
export * from './observer';
export * from './parse-flags';
export * from './parse-keyword-value';
export * from './random-id';
export * from './throttle';
19 changes: 19 additions & 0 deletions src/helpers/log-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @typedef { import('../scriptlets/index').Source } Source
*/

/**
* Conditionally logs message to console.
* Convention is to log messages by source.verbose if such log
* is not a part of scriptlet's functionality, eg on invalid input,
* and use 'forced' argument otherwise.
* @param {Source} source required
* @param {string} message required, message to log
* @param {boolean} [forced=false] to log message unconditionally
*/
export const logMessage = (source, message, forced = false) => {
if (forced || source.verbose) {
// eslint-disable-next-line no-console
console.log(`${source.name}: ${message}`);
}
};
11 changes: 8 additions & 3 deletions src/helpers/match-request-props.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,22 @@ import {
validateParsedData,
parseMatchProps,
} from './request-utils';
import { logMessage } from './log-message';

/**
* @typedef { import('../scriptlets/index').Source } Source
*/

/**
* Checks if given propsToMatch string matches with given request data
* This is used by prevent-xhr, prevent-fetch, trusted-replace-xhr-response
* and trusted-replace-fetch-response scriptlets
* @param {Source} source
* @param {string} propsToMatch
* @param {Object} requestData object with standard properties of fetch/xhr like url, method etc
* @returns {boolean}
*/
export const matchRequestProps = (propsToMatch, requestData) => {
export const matchRequestProps = (source, propsToMatch, requestData) => {
if (propsToMatch === '' || propsToMatch === '*') {
return true;
}
Expand All @@ -21,8 +27,7 @@ export const matchRequestProps = (propsToMatch, requestData) => {

const parsedData = parseMatchProps(propsToMatch);
if (!validateParsedData(parsedData)) {
// eslint-disable-next-line no-console
console.log(`Invalid parameter: ${propsToMatch}`);
logMessage(source, `Invalid parameter: ${propsToMatch}`);
isMatched = false;
} else {
const matchData = getMatchPropsData(parsedData);
Expand Down
File renamed without changes.
13 changes: 13 additions & 0 deletions src/helpers/number-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,16 @@ export const getNumberFromString = (rawString) => {
: parsedDelay;
return validDelay;
};

/**
* Generate a random integer between two values, inclusive
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#getting_a_random_integer_between_two_values_inclusive
* @param {number} min
* @param {number} max
* @returns {number}
*/
export function getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1) + min);
}
16 changes: 16 additions & 0 deletions src/helpers/object-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,19 @@ export const safeGetDescriptor = (obj, prop) => {
}
return null;
};

/**
* Set getter and setter to property if it's configurable
* @param {Object} object target object with property
* @param {string} property property name
* @param {PropertyDescriptor} descriptor contains getter and setter functions
* @returns {boolean} is operation successful
*/
export function setPropertyAccess(object, property, descriptor) {
const currentDescriptor = Object.getOwnPropertyDescriptor(object, property);
if (currentDescriptor && !currentDescriptor.configurable) {
return false;
}
Object.defineProperty(object, property, descriptor);
return true;
}
31 changes: 3 additions & 28 deletions src/helpers/observer.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,11 @@
import { throttle } from './throttle';

/**
* DOM tree changes observer. Used for 'remove-attr' and 'remove-class' scriptlets
* @param {Function} callback
* @param {Boolean} observeAttrs - optional parameter - should observer check attributes changes
* @param {boolean} observeAttrs - optional parameter - should observer check attributes changes
*/
export const observeDOMChanges = (callback, observeAttrs = false, attrsToObserve = []) => {
/**
* Returns a wrapper, passing the call to 'method' at maximum once per 'delay' milliseconds.
* Those calls that fall into the "cooldown" period, are ignored
* @param {Function} method
* @param {Number} delay - milliseconds
*/
const throttle = (method, delay) => {
let wait = false;
let savedArgs;
const wrapper = (...args) => {
if (wait) {
savedArgs = args;
return;
}
method(...args);
wait = true;
setTimeout(() => {
wait = false;
if (savedArgs) {
wrapper(savedArgs);
savedArgs = null;
}
}, delay);
};
return wrapper;
};

/**
* 'delay' in milliseconds for 'throttle' method
*/
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/prevent-window-open-utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
noopFunc,
trueFunc,
} from './noop';
} from './noop-utils';
import {
startsWith,
endsWith,
Expand Down
74 changes: 0 additions & 74 deletions src/helpers/random-response.js

This file was deleted.

Loading

0 comments on commit bccaff1

Please sign in to comment.