UNPKG

@adguard/agtree

Version:
62 lines (59 loc) 2.13 kB
/* * AGTree v3.2.2 (build date: Tue, 08 Jul 2025 13:39:47 GMT) * (c) 2025 Adguard Software Ltd. * Released under the MIT license * https://github.com/AdguardTeam/tsurlfilter/tree/master/packages/agtree#readme */ import { isNull } from '../../utils/type-guards.js'; import { modifiersCompatibilityTable } from '../modifiers.js'; import { ResourceType } from '../schemas/resource-type.js'; /** * Map of resource types to their corresponding adblock modifier names. * * @note Record type is used to ensure that all resource types are present in the map. */ const RESOURCE_TYPE_MODIFIER_MAP = Object.freeze({ [ResourceType.MainFrame]: 'document', [ResourceType.SubFrame]: 'subdocument', [ResourceType.Stylesheet]: 'stylesheet', [ResourceType.Script]: 'script', [ResourceType.Image]: 'image', [ResourceType.Font]: 'font', [ResourceType.Object]: 'object', [ResourceType.XmlHttpRequest]: 'xmlhttprequest', [ResourceType.Ping]: 'ping', [ResourceType.Media]: 'media', [ResourceType.WebSocket]: 'websocket', [ResourceType.Other]: 'other', }); /** * Gets the adblock modifier name for the given resource type. * * @param resourceType Resource type to get the modifier name for. * @param platform Platform to get the modifier for. * * @returns A string containing the adblock modifier name for the given resource type * or `null` if the modifier could not be found. */ const getResourceTypeModifier = (resourceType, platform) => { const modifierName = RESOURCE_TYPE_MODIFIER_MAP[resourceType]; if (!modifierName) { return null; } const modifierData = modifiersCompatibilityTable.getFirst(modifierName, platform); if (isNull(modifierData)) { return null; } return modifierData.name; }; /** * Checks if the given resource type is valid. * * @param resourceType Resource type to check. * * @returns `true` if the resource type is valid, `false` otherwise. */ const isValidResourceType = (resourceType) => { return Object.values(ResourceType).includes(resourceType); }; export { getResourceTypeModifier, isValidResourceType };