HEX
Server: Apache
System: Linux webm006.cluster111.gra.hosting.ovh.net 6.18.42-ovh-vps-grsec-zfs+ #1 SMP PREEMPT_DYNAMIC Wed Aug 5 15:59:48 CEST 2026 x86_64
User: perditaeyz (838589)
PHP: 7.3.33
Disabled: _dyuweyrj4,_dyuweyrj4r,dl
Upload Files
File: /home/perditaeyz/www/wp-content/plugins/better-wp-security/core/packages/preload/src/index.js
const cache = {};

/**
 * Given a path, returns a normalized path where equal query parameter values
 * will be treated as identical, regardless of order they appear in the original
 * text.
 *
 * @param {string} path Original path.
 *
 * @return {string} Normalized path.
 */
export function getStablePath( path ) {
	const splitted = path.split( '?' );
	const query = splitted[ 1 ];
	const base = splitted[ 0 ];
	if ( ! query ) {
		return base;
	}

	// 'b=1&c=2&a=5'
	return base + '?' + query
		// [ 'b=1', 'c=2', 'a=5' ]
		.split( '&' )
		// [ [ 'b, '1' ], [ 'c', '2' ], [ 'a', '5' ] ]
		.map( function( entry ) {
			return entry.split( '=' );
		} )
		// [ [ 'a', '5' ], [ 'b, '1' ], [ 'c', '2' ] ]
		.sort( function( a, b ) {
			return a[ 0 ].localeCompare( b[ 0 ] );
		} )
		// [ 'a=5', 'b=1', 'c=2' ]
		.map( function( pair ) {
			return pair.join( '=' );
		} )
		// 'a=5&b=1&c=2'
		.join( '&' );
}

export function createMiddleware( preloadedData ) {
	Object.keys( preloadedData ).reduce( ( result, path ) => {
		result[ getStablePath( path ) ] = preloadedData[ path ];
		return result;
	}, cache );

	return ( options, next ) => {
		const { parse = true } = options;
		if ( typeof options.path === 'string' ) {
			const method = options.method || 'GET';
			const path = getStablePath( options.path );

			if ( parse && 'GET' === method && cache[ path ] ) {
				return Promise.resolve( cache[ path ].body );
			} else if (
				'OPTIONS' === method &&
				cache[ method ] &&
				cache[ method ][ path ]
			) {
				return Promise.resolve( cache[ method ][ path ] );
			}
		}

		return next( options );
	};
}

export function invalidatePreload( { path } ) {
	delete cache[ getStablePath( path ) ];
}

export const CONTROL = 'INVALIDATE_API_FETCH_PRELOAD';

export function invalidatePreloadControl( path ) {
	return {
		type: CONTROL,
		path,
	};
}