<commit_before>/**
 *
 * stylelounge.de
 *
 * Copyright (C) SNM Style Net Media GmbH
 * MIT Licensed
 */
import * as debug from "debug";

import "isomorphic-fetch";

import { IManifest } from "../types";

const dbg: debug.IDebugger = debug("@stylelounge/http-queue:utils:sendBeacon");

const sendHttp = async (manifest: IManifest) => {
    dbg(`Sending data via "XHR".`);
    await window.fetch(new Request(manifest.url), {
        body: JSON.stringify(manifest.data || {}),
        headers: new Headers({
            "Accept": "application/json",
            "Content-Type": "application/json",
        }),
        method: manifest.verb.toUpperCase(),
    });
};

const sendBeacon = async (manifest: IManifest): Promise<boolean> => {
    const nav: any = navigator as any;

    if (nav.sendBeacon) {
        const blob = new Blob([JSON.stringify(manifest.data || {})], { type: "application/json; charset=UTF-8" });

        dbg(`Sending data via "sendBeacon" (size: ${blob.size}).`);

        return nav.sendBeacon(manifest.url, blob);
    }

    return false;
};

const send = async (manifest: IManifest) => {
    if (!(await sendBeacon(manifest))) {
        dbg(`Okay, seems like "sendBeacon" failed. Will retry with "XHR".`);

        await sendHttp(manifest);
    }
};

export default send;<commit_msg>fix: sendBeacon problem<commit_after>/**
 *
 * stylelounge.de
 *
 * Copyright (C) SNM Style Net Media GmbH
 * MIT Licensed
 */
import * as debug from "debug";

import "isomorphic-fetch";

import { IManifest } from "../types";

const dbg: debug.IDebugger = debug("@stylelounge/http-queue:utils:sendBeacon");

const sendHttp = async (manifest: IManifest) => {
    dbg(`Sending data via "XHR".`);
    await window.fetch(new Request(manifest.url), {
        body: JSON.stringify(manifest.data || {}),
        headers: new Headers({
            "Accept": "application/json",
            "Content-Type": "application/json",
        }),
        method: manifest.verb.toUpperCase(),
    });
};

const sendBeacon = async (manifest: IManifest): Promise<boolean> => {
    const nav: any = navigator as any;

    if (nav.sendBeacon) {
        const blob = new Blob([JSON.stringify(manifest.data || {})], { type: "application/json; charset=UTF-8" });

        dbg(`Sending data via "sendBeacon" (size: ${blob.size}).`);

        try {
            nav.sendBeacon(manifest.url, blob);
            return true;
        } catch (e) {
            dbg(`Sending data via "sendBeacon" failed. Reason ${e}).`);
            return false;
        }
    }

    return false;
};

const send = async (manifest: IManifest) => {
    if (!(await sendBeacon(manifest))) {
        dbg(`Okay, seems like "sendBeacon" failed. Will retry with "XHR".`);

        await sendHttp(manifest);
    }
};

export default send;
{'fraction_non_alphanumeric': 0.15770609318996415, 'fraction_numerical': 0.0007168458781362007, 'mean_word_length': 2.8443526170798896, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 11, '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': '2984115', 'n_tokens_mistral': 882, 'n_tokens_neox': 863, 'n_words': 220}