<commit_before>// @flow

import {performance, isResourceTimingAvailable} from '../performance';
import type {BeaconWithResourceTiming} from '../types';
import {serializeEntry} from './timingSerializer';
import {urlMaxLength} from './consts';
import {info, debug} from '../debug';
import {createTrie} from '../trie';
import {win} from '../browser';
import vars from '../vars';

// See https://w3c.github.io/resource-timing/
// See https://www.w3.org/TR/hr-time/

export function addResourceTimings(beacon: BeaconWithResourceTiming, minStartTime: ?number) {
  if (isResourceTimingAvailable && win.JSON) {
    const entries = getEntriesTransferFormat(performance.getEntriesByType('resource'), minStartTime);
    beacon['res'] = win.JSON.stringify(entries);

    if (vars.autoClearResourceTimings && performance.clearResourceTimings) {
      if (DEBUG) {
        debug('Automatically clearing resource timing buffer.');
      }
      performance.clearResourceTimings();
    }
  } else if (DEBUG) {
    info('Resource timing not supported.');
  }
}


function getEntriesTransferFormat(performanceEntries: Array<Object>, minStartTime: ?number) {
  const trie = createTrie();

  for (let i = 0, len = performanceEntries.length; i < len; i++) {
    const entry = performanceEntries[i];
    if (minStartTime != null &&
        (entry['startTime'] - vars.highResTimestampReference + vars.referenceTimestamp) < minStartTime) {
      continue;
    }

    let url = entry.name;
    const lowerCaseUrl = url.toLowerCase();
    if (lowerCaseUrl === 'about:blank' || lowerCaseUrl.indexOf('javascript:') === 0) {
      continue;
    }

    if (url.length > urlMaxLength) {
      url = url.substring(0, urlMaxLength);
    }

    // We provide more detailed XHR insights via our XHR instrumentation.
    // The XHR instrumentation is available once the initialization was executed
    // (which is completely synchronous).
    if (entry['initiatorType'] !== 'xmlhttprequest' || entry['startTime'] < vars.highResTimestampReference) {
      trie.addItem(url, serializeEntry(entry));
    }
  }

  return trie.toJs();
}<commit_msg>Do not report image based data transmissions as resources<commit_after>// @flow

import {performance, isResourceTimingAvailable} from '../performance';
import type {BeaconWithResourceTiming} from '../types';
import {serializeEntry} from './timingSerializer';
import {urlMaxLength} from './consts';
import {info, debug} from '../debug';
import {createTrie} from '../trie';
import {win} from '../browser';
import vars from '../vars';

// See https://w3c.github.io/resource-timing/
// See https://www.w3.org/TR/hr-time/

export function addResourceTimings(beacon: BeaconWithResourceTiming, minStartTime: ?number) {
  if (isResourceTimingAvailable && win.JSON) {
    const entries = getEntriesTransferFormat(performance.getEntriesByType('resource'), minStartTime);
    beacon['res'] = win.JSON.stringify(entries);

    if (vars.autoClearResourceTimings && performance.clearResourceTimings) {
      if (DEBUG) {
        debug('Automatically clearing resource timing buffer.');
      }
      performance.clearResourceTimings();
    }
  } else if (DEBUG) {
    info('Resource timing not supported.');
  }
}


function getEntriesTransferFormat(performanceEntries: Array<Object>, minStartTime: ?number) {
  const trie = createTrie();
  const lowerCaseReportingUrl = vars.reportingUrl != null ? vars.reportingUrl.toLowerCase() : null;

  for (let i = 0, len = performanceEntries.length; i < len; i++) {
    const entry = performanceEntries[i];
    if (minStartTime != null &&
        (entry['startTime'] - vars.highResTimestampReference + vars.referenceTimestamp) < minStartTime) {
      continue;
    }

    let url = entry.name;
    const lowerCaseUrl = url.toLowerCase();
    const initiatorType = entry['initiatorType'];
    if (
        // some iframe cases
        lowerCaseUrl === 'about:blank' || lowerCaseUrl.indexOf('javascript:') === 0 ||
        // The image based data transmission can be visible as a resource. Do not report it.
        (lowerCaseReportingUrl != null && initiatorType === 'img' && lowerCaseUrl.indexOf(lowerCaseReportingUrl) === 0)) {
      continue;
    }

    if (url.length > urlMaxLength) {
      url = url.substring(0, urlMaxLength);
    }

    // We provide more detailed XHR insights via our XHR instrumentation.
    // The XHR instrumentation is available once the initialization was executed
    // (which is completely synchronous).
    if (initiatorType !== 'xmlhttprequest' || entry['startTime'] < vars.highResTimestampReference) {
      trie.addItem(url, serializeEntry(entry));
    }
  }

  return trie.toJs();
}
{'fraction_non_alphanumeric': 0.13361976369495165, 'fraction_numerical': 0.002363050483351235, 'mean_word_length': 3.7901234567901234, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '5494322', 'n_tokens_mistral': 1340, 'n_tokens_neox': 1308, 'n_words': 347}