fileoverview import { HintContext } from 'hint/dist/src/lib/hint-context' ; import { IHint , FetchEnd } from 'hint/dist/src/lib/types' ; import { Severity } from '@hint/utils-types' ; import meta from './meta' ; import { getMessage } from './i18n.import' ; export default class NoHttpRedirectHint implements IHint { public static readonly meta = meta ; public constructor ( context ) { /** The maximum number of hops for a resource. */ const maxResourceHops = context . hintOptions && context . hintOptions [ 'max-resource-redirects' ] || 0 ; /** The maximum number of hops for the html. */ const maxHTMLHops = context . hintOptions && context . hintOptions [ 'max-html-redirects' ] || 0 ; /**
         * Returns a function that will validate if the number of hops is within the limit passed by `maxHops`.
         * If it doesn't validate, it will notify the context.
         *
         * Ex.: `validateRequestEnd(10)(fetchEnd)` will verify if the event `fetchEnd` has had less than 10 hops.
         */ const validateRequestEnd = ( fetchEnd , eventName ) => { const maxHops = eventName === 'fetch::end::html' ? maxHTMLHops : maxResourceHops ; const { request , response , element } = fetchEnd ; if ( response . hops . length > maxHops ) { const message = getMessage ( 'redirectsDectected' , context . language , maxHops . toString ( ) ) ; context . report ( request . url , message , { element , severity : Severity . warning } ) ; } } ; context . on ( 'fetch::end::*' , validateRequestEnd ) ; } }