<commit_before>import {getSVGFilterMatrixValue} from '../../generators/svg-filter';
import {bgFetch} from './network';
import {loadAsDataURL} from '../../utils/network';
import type {FilterConfig} from '../../definitions';
import {logInfo, logWarn} from '../../utils/log';
import AsyncQueue from '../../utils/async-queue';

export interface ImageDetails {
    src: string;
    dataURL: string;
    width: number;
    height: number;
    isDark: boolean;
    isLight: boolean;
    isTransparent: boolean;
    isLarge: boolean;
    isTooLarge: boolean;
}

const imageManager = new AsyncQueue();

export async function getImageDetails(url: string) {
    return new Promise<ImageDetails>(async (resolve) => {
        let dataURL: string;
        if (url.startsWith('data:')) {
            dataURL = url;
        } else {
            dataURL = await getImageDataURL(url);
        }

        const image = await urlToImage(dataURL);
        imageManager.addToQueue(() => {
            resolve({
                src: url,
                dataURL,
                width: image.naturalWidth,
                height: image.naturalHeight,
                ...analyzeImage(image),
            });
        });
    });
}

async function getImageDataURL(url: string) {
    const parsedURL = new URL(url);
    if (parsedURL.origin === location.origin) {
        return await loadAsDataURL(url);
    }
    return await bgFetch({url, responseType: 'data-url'});
}<commit_msg>Fix: catch and re-throw error in getImageDetails (#6696)<commit_after>import {getSVGFilterMatrixValue} from '../../generators/svg-filter';
import {bgFetch} from './network';
import {loadAsDataURL} from '../../utils/network';
import type {FilterConfig} from '../../definitions';
import {logInfo, logWarn} from '../../utils/log';
import AsyncQueue from '../../utils/async-queue';

export interface ImageDetails {
    src: string;
    dataURL: string;
    width: number;
    height: number;
    isDark: boolean;
    isLight: boolean;
    isTransparent: boolean;
    isLarge: boolean;
    isTooLarge: boolean;
}

const imageManager = new AsyncQueue();

export async function getImageDetails(url: string) {
    return new Promise<ImageDetails>(async (resolve, reject) => {
        let dataURL: string;
        if (url.startsWith('data:')) {
            dataURL = url;
        } else {
            dataURL = await getImageDataURL(url);
        }

        try {
            const image = await urlToImage(dataURL);
            imageManager.addToQueue(() => {
                resolve({
                    src: url,
                    dataURL,
                    width: image.naturalWidth,
                    height: image.naturalHeight,
                    ...analyzeImage(image),
                });
            });
        } catch (error) {
            reject(error);
        }
    });
}

async function getImageDataURL(url: string) {
    const parsedURL = new URL(url);
    if (parsedURL.origin === location.origin) {
        return await loadAsDataURL(url);
    }
    return await bgFetch({url, responseType: 'data-url'});
}
{'fraction_non_alphanumeric': 0.1314935064935065, 'fraction_numerical': 0.0012987012987012987, 'mean_word_length': 2.209375, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4492445', 'n_tokens_mistral': 852, 'n_tokens_neox': 850, 'n_words': 220}