<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// @flow
import { oneLine } from 'common-tags';

export function uploadBinaryProfileData(): * {
  const xhr = new XMLHttpRequest();
  let isAborted = false;

  return {
    abortFunction: (): void => {
      isAborted = true;
      xhr.abort();
    },
    startUpload: (
      data: $TypedArray,
      progressChangeCallback?: number => mixed
    ): Promise<string> =>
      new Promise((resolve, reject) => {
        if (isAborted) {
          reject(new Error('The request was already aborted.'));
          return;
        }

        xhr.onload = () => {
          if (xhr.status === 200) {
            resolve(xhr.responseText);
          } else {
            reject(
              new Error(
                `xhr onload with status != 200, xhr.statusText: ${
                  xhr.statusText
                }`
              )
            );
          }
        };

        xhr.onerror = () => {
          reject(
            new Error(
              // The profile store does not give useful responses.
              // See: https://github.com/firefox-devtools/profiler/issues/998
              xhr.statusText
                ? oneLine`
                    There was an issue uploading the profile to the server. This is often
                    caused by the profile file size being too large. The error<commit_msg>Add a console.error to make debugging failed XHR requests easier<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// @flow
import { oneLine } from 'common-tags';

export function uploadBinaryProfileData(): * {
  const xhr = new XMLHttpRequest();
  let isAborted = false;

  return {
    abortFunction: (): void => {
      isAborted = true;
      xhr.abort();
    },
    startUpload: (
      data: $TypedArray,
      progressChangeCallback?: number => mixed
    ): Promise<string> =>
      new Promise((resolve, reject) => {
        if (isAborted) {
          reject(new Error('The request was already aborted.'));
          return;
        }

        xhr.onload = () => {
          if (xhr.status === 200) {
            resolve(xhr.responseText);
          } else {
            reject(
              new Error(
                `xhr onload with status != 200, xhr.statusText: ${
                  xhr.statusText
                }`
              )
            );
          }
        };

        xhr.onerror = () => {
          console.error('XHR error', xhr);
          reject(
            new Error(
              // The profile store does not give useful responses.
              // See: https://github.com/firefox-devtools/profiler/issues/998
              xhr.statusText
                ? oneLine`
                    There was an issue uploading the profile to the server. This is often
                    caused by the profile file size being too large. The error
{'fraction_non_alphanumeric': 0.10440251572327044, 'fraction_numerical': 0.008176100628930818, 'mean_word_length': 1.6052416052416052, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 2, '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': '5610047', 'n_tokens_mistral': 881, 'n_tokens_neox': 845, 'n_words': 273}