All files / util http-util.js

94.44% Statements 17/18
75% Branches 3/4
100% Functions 3/3
94.44% Lines 17/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 251x   1x 2x   1x 2x 2x   2x 2x 2x 2x 2x 2x 2x 10x   2x 2x         1x
const url = require('url');
 
const webpSupported = (req) =>
    req.headers['accept']?.indexOf('image/webp') > -1 ? 'webp' : '';
 
const identifyImage = (req) => {
    const requestUrl = req.url.toLowerCase();
    const results = requestUrl.match(/\/image\/([0-9a-zA-Z_-]{1,128})(\.(png|gif|jpg|jpeg|webp))?(\?.*)?/);
 
    Eif (results) {
        const { query } = url.parse(requestUrl, true);
        const formatInQuery = query['format']?.replace('jpg', 'jpeg').split(',');
        const formatInName = results[3];
        const formatInHeader = webpSupported(req);
        const formatDefaults = ['jpeg', 'png'];
        const formats =  [...new Set(
            [].concat.apply([], [formatInQuery, formatInName, formatInHeader, formatDefaults]).filter(i => i)
        )];
        const profile = query.profile;
        return { image: results[1], profile, formats };
    }
    throw Error(`Illegal path: ${req.url}`);
}
 
module.exports = { identifyImage }