<commit_before>const wd = require('wd');
const Promise = require('bluebird');
const bl = require('bl');
const JSONStream = require('JSONStream');
const browser = require('./browser');
const normalise = require('./normalise-logs');

// augment wd with stream methods
require('./wd-streams');

const blacklist = ['UpdateLayerTree', 'Layout', 'FunctionCall', 'UpdateLayoutTree',
  'EventDispatch', 'TimerFire', 'TimerInstall', 'TimerRemove', 'ResourceSendRequest', 'ResourceReceivedData'];

function test (opts) {
  opts.driver = opts.driver || 'http://localhost:9515';
  opts.browser = wd.remote(opts.driver, 'promiseChain');
  return Promise.using(browser(opts.browser), (session) => {
    return session
      .setWindowSize(1024, 768).catch(() => {
        return session.sleep(100).setWindowSize(1024, 768).catch(() => {});
      })
      .get(opts.url)
      .then(() => {
        if (opts.scroll || opts.reporter === 'fps') {
          return session.execute('var dir=-1;function f () { window.scrollBy(0,dir*=-1); window.requestAnimationFrame(f); } window.requestAnimationFrame(f);');
        }
      })
      .then(() => {
        if (typeof opts.inject === 'function') {
          return opts.inject(session);
        }
      })
      .then(() => {
        return session.waitFor(wd.asserters.jsCondition(`document.readyState==='complete'`), 120000)
          .catch(() => {
            console.error('Page loading timed out after 120s');
            return session;
          });
      })
      .sleep(opts.sleep)
      .then(() => {
        return new Promise((resolve, reject) => {
          const stream = session.logstream('performance')
            .pipe(JSONStream.parse('value.*', (item) => {
              item.message = JSON.parse(item.message);
              item.name = item.message.message.params.name;
              item.timestamp = item.message.message.params.timestamp || (item.message.message.params.ts / 1e6);

              if (item.message.message.params && item.message.message.params.cat === '__metadata') {
                return;<commit_msg>Wrap try/catch around JSON.parse call to handle malformed json<commit_after>const wd = require('wd');
const Promise = require('bluebird');
const bl = require('bl');
const JSONStream = require('JSONStream');
const browser = require('./browser');
const normalise = require('./normalise-logs');

// augment wd with stream methods
require('./wd-streams');

const blacklist = ['UpdateLayerTree', 'Layout', 'FunctionCall', 'UpdateLayoutTree',
  'EventDispatch', 'TimerFire', 'TimerInstall', 'TimerRemove', 'ResourceSendRequest', 'ResourceReceivedData'];

function test (opts) {
  opts.driver = opts.driver || 'http://localhost:9515';
  opts.browser = wd.remote(opts.driver, 'promiseChain');
  return Promise.using(browser(opts.browser), (session) => {
    return session
      .setWindowSize(1024, 768).catch(() => {
        return session.sleep(100).setWindowSize(1024, 768).catch(() => {});
      })
      .get(opts.url)
      .then(() => {
        if (opts.scroll || opts.reporter === 'fps') {
          return session.execute('var dir=-1;function f () { window.scrollBy(0,dir*=-1); window.requestAnimationFrame(f); } window.requestAnimationFrame(f);');
        }
      })
      .then(() => {
        if (typeof opts.inject === 'function') {
          return opts.inject(session);
        }
      })
      .then(() => {
        return session.waitFor(wd.asserters.jsCondition(`document.readyState==='complete'`), 120000)
          .catch(() => {
            console.error('Page loading timed out after 120s');
            return session;
          });
      })
      .sleep(opts.sleep)
      .then(() => {
        return new Promise((resolve, reject) => {
          const stream = session.logstream('performance')
            .pipe(JSONStream.parse('value.*', (item) => {
              try {
                item.message = JSON.parse(item.message);
              } catch (e) {
                return;
              }
              item.name = item.message.message.params.name;
              item.timestamp = item.message.message.params.timestamp || (item.message.message.params.ts / 1e6);

              if (item.message.message.params && item.message.message.params.cat === '__metadata') {
                return;
{'fraction_non_alphanumeric': 0.16974083586271305, 'fraction_numerical': 0.01634368433341116, 'mean_word_length': 2.6962899050905955, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '1139188', 'n_tokens_mistral': 1251, 'n_tokens_neox': 1187, 'n_words': 235}