Skip to content

Commit

Permalink
fix LegalRequestProp, improve :any typings, tweak hit() and remove 'h…
Browse files Browse the repository at this point in the history
…it' prop from Source
  • Loading branch information
stanislav-atr committed May 7, 2023
1 parent 7ef8e59 commit 27765da
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 49 deletions.
3 changes: 0 additions & 3 deletions src/helpers/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ export function passSourceAndProps(
code: string,
redirect = false,
): string {
if (source.hit) {
source.hit = source.hit.toString();
}
const sourceString = JSON.stringify(source);
const argsString = source.args ? `[${source.args.map((arg) => JSON.stringify(arg))}]` : undefined;
const params = argsString ? `${sourceString}, ${argsString}` : sourceString;
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/object-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export const getObjectEntries = (object: Record<string, unknown>): [string, any]
* @param entries - array of pairs
* @returns result object
*/
export const getObjectFromEntries = (entries: [string, any][]): { [key: string]: any } => {
const initValue: { [key: string]: any } = {};
export const getObjectFromEntries = <T>(entries: [string, T][]): { [key: string]: T } => {
const initValue: { [key: string]: T } = {};
const output = entries
.reduce((acc, el) => {
const key = el[0];
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/prevent-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const isValidCallback = (callback: unknown): boolean => {
* @param delay native method delay arg
* @returns number as parsed delay or any input type if `delay` is not parsable
*/
export const parseRawDelay = (delay: any): any => {
export const parseRawDelay = <T>(delay: T): number | T => {
const parsedDelay = Math.floor(parseInt(delay as string, 10));
return typeof parsedDelay === 'number' && !nativeIsNaN(parsedDelay) ? parsedDelay : delay;
};
Expand Down
64 changes: 28 additions & 36 deletions src/helpers/request-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,6 @@ import { getObjectFromEntries } from './object-utils';

type FetchResource = Request | URL | string;

/**
* Fetch and xhr.open options that are valid props
* to match for (trusted-)prevent-(fetch|xhr) scriptlets
*/
export enum LegalRequestProp {
Url = 'url',
Method = 'method',
Headers = 'headers',
Body = 'body',
Credentials = 'credentials',
Cache = 'cache',
Redirect = 'redirect',
Referrer = 'referrer',
ReferrerPolicy = 'referrerPolicy',
Integrity = 'integrity',
Keepalive = 'keepalive',
Signal = 'signal',
Mode = 'mode',
}

/**
* Aggregates fetch and XMLHttpRequest.open arguments
* to operate on arbitrary request data objects
Expand All @@ -42,26 +22,38 @@ type ParsedMatchProps = SharedRequestData<string>;
*/
export type MatchPropsData = SharedRequestData<RegExp>;

/**
* Fetch and xhr.open options that are valid props
* to match for (trusted-)prevent-(fetch|xhr) scriptlets
*
* This type is being derived from getRequestProps return type
* as enums would be lost at build time disregarding 'const'
*/
export type LegalRequestProp = ReturnType<typeof getRequestProps>[number];

/**
* Returns array of request props that are supported by fetch/xhr scriptlets.
* Includes common 'url' and 'method' props and all other fetch-specific props
*
* @returns list of request props
*/
export const getRequestProps = (): Array<LegalRequestProp> => [
LegalRequestProp.Url,
LegalRequestProp.Method,
LegalRequestProp.Headers,
LegalRequestProp.Body,
LegalRequestProp.Credentials,
LegalRequestProp.Cache,
LegalRequestProp.Redirect,
LegalRequestProp.Referrer,
LegalRequestProp.ReferrerPolicy,
LegalRequestProp.Integrity,
LegalRequestProp.Keepalive,
LegalRequestProp.Signal,
];
export const getRequestProps = () => {
return [
'url',
'method',
'headers',
'body',
'credentials',
'cache',
'redirect',
'referrer',
'referrerPolicy',
'integrity',
'keepalive',
'signal',
'mode',
] as const;
};

/**
* Collects Request options to object
Expand All @@ -71,10 +63,10 @@ export const getRequestProps = (): Array<LegalRequestProp> => [
*/
export const getRequestData = (request: Request): Partial<Request> => {
const requestInitOptions = getRequestProps();
const entries: [string, any][] = requestInitOptions
const entries: [keyof Request, any][] = requestInitOptions
.map((key) => {
// if request has no such option, value will be undefined
const value = request[key as keyof typeof request];
const value = request[key];
return [key, value];
});
return getObjectFromEntries(entries);
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/shadow-dom-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const hijackAttachShadow = (
hostSelector: string,
callback: AttachShadowCallback,
): void => {
const handlerWrapper = (target: AttachShadow, thisArg: any, args: unknown[]): ShadowRoot => {
const handlerWrapper = (target: AttachShadow, thisArg: Element, args: unknown[]): ShadowRoot => {
const shadowRoot: ShadowRoot = Reflect.apply(target, thisArg, args);

if (thisArg && thisArg.matches(hostSelector || '*')) {
Expand Down
8 changes: 4 additions & 4 deletions src/helpers/string-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ export const parseDelayArg = (delay: string): DelayData => {
* @param obj data object
* @returns object's string representation
*/
export const objectToString = (obj: Record<string, unknown>): string | StringConstructor => {
export const objectToString = (obj: Record<string, unknown>): string => {
// In case if the type of passed obj is different than Object
// https://github.com/AdguardTeam/Scriptlets/issues/282
if (!obj || typeof obj !== 'object') {
Expand Down Expand Up @@ -304,15 +304,15 @@ export const objectToString = (obj: Record<string, unknown>): string | StringCon
* @param value input value type
* @returns type's string representation
*/
export const convertTypeToString = (value: any): string => {
export const convertTypeToString = (value: unknown): string => {
let output;
if (typeof value === 'undefined') {
output = 'undefined';
} else if (typeof value === 'object') {
if (value === null) {
output = 'null';
} else {
output = objectToString(value);
output = objectToString(value as Record<string, unknown>);
}
} else {
output = value.toString();
Expand Down Expand Up @@ -392,7 +392,7 @@ export function generateRandomResponse(customResponseText: string): string | nul
* @returns converted value
* @throws an error on unexpected input
*/
export function inferValue(value: string): any {
export function inferValue(value: string): unknown {
if (value === 'undefined') {
return undefined;
} if (value === 'false') {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ const REDIRECT_RULE_TYPES: Record<RedirectRuleType, RedirectsData> = {
* @param rule rule text
* @returns list of rule modifiers
*/
const parseModifiers = (rule: string): string[] => substringAfter(rule, '$')?.split(',');
const parseModifiers = (rule: string): string[] => substringAfter(rule, '$').split(',');

/**
* Gets redirect resource name
Expand Down
1 change: 0 additions & 1 deletion types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ interface Source {
verbose: boolean;
ruleText: string;
domainName?: string;
hit?: string;
}

type ValueOf<T> = T[keyof T];
Expand Down

0 comments on commit 27765da

Please sign in to comment.