<commit_before>import uuid from 'node-uuid';
import {Observable, Disposable, TimeoutError} from 'rx';

const requestChannel = 'execute-javascript-request';
const responseChannel = 'execute-javascript-response';

let isBrowser = (process.type === 'browser');
let ipc = require('electron')[isBrowser ? 'ipcMain' : 'ipcRenderer'];

const d = require(isBrowser ? 'debug' : 'debug/browser')('electron-remote:execute-js-func');
const BrowserWindow = isBrowser ?
  require('electron').BrowserWindow :
  require('electron').remote.BrowserWindow;

/**
 * Determines the identifier for the current process (i.e. the thing we can use
 * to route messages to it)
 *
 * @return {object} An object with either a `guestInstanceId` or a `browserWindowId`
 */
export function getSenderIdentifier() {
  if (isBrowser) return {};

  if (process.guestInstanceId) {
    return { guestInstanceId: process.guestInstanceId };
  }

  return {
    browserWindowId: require('electron').remote.getCurrentWindow().id
  };
}

/**
 * Determines a way to send a reply back from an incoming eval request.
 *
 * @param  {Object} request   An object returned from {getSenderIdentifier}
 *
 * @return {Function}         A function that act like ipc.send, but to a
 *                            particular process.
 *
 * @private
 */
function getReplyMethod(request) {
  let target = findTargetFromParentInfo(request);

  if (target) {
    return (...a) => {
      let crashedTarget = 'webContents' in target ? target.webContents : target;
      if (crashedTarget.isCrashed() || crashedTarget.isDestroyed()) {
        console.log(`Tried to send ${JSON.stringify(a)} as reply but it is destroyed`);
        return;
      }

      target.send(...a);
    };
  } else {
    d("Using reply to main process");
    return (...a) => ipc.send(...a);
  }
}

/**
 * Turns an IPC channel into an Observable
 *
 * @param  {String} channel     The IPC channel to listen to via `ipc.on`
 *
 * @return {Observable<Array>}  An Observable which sends IPC args via `onNext`
 *
 * @private
 */
function listenToIpc(channel) {
  return Observable.create((subj) => {
    let listener = (event, ...args) => {
      d(`Got an event for ${channel}: ${JSON.stringify(args)}`);
      subj.onNext(args);
    };

    d(`Setting up listener! ${channel}`);
    ipc.on(channel, listener);
<commit_msg>Revert "Grumble"<commit_after>import uuid from 'node-uuid';
import {Observable, Disposable, TimeoutError} from 'rx';

const requestChannel = 'execute-javascript-request';
const responseChannel = 'execute-javascript-response';

let isBrowser = (process.type === 'browser');
let ipc = require('electron')[isBrowser ? 'ipcMain' : 'ipcRenderer'];

const d = require(isBrowser ? 'debug' : 'debug/browser')('electron-remote:execute-js-func');
const BrowserWindow = isBrowser ?
  require('electron').BrowserWindow :
  require('electron').remote.BrowserWindow;

/**
 * Determines the identifier for the current process (i.e. the thing we can use
 * to route messages to it)
 *
 * @return {object} An object with either a `guestInstanceId` or a `browserWindowId`
 */
export function getSenderIdentifier() {
  if (isBrowser) return {};

  if (process.guestInstanceId) {
    return { guestInstanceId: process.guestInstanceId };
  }

  return {
    browserWindowId: require('electron').remote.getCurrentWindow().id
  };
}

/**
 * Determines a way to send a reply back from an incoming eval request.
 *
 * @param  {Object} request   An object returned from {getSenderIdentifier}
 *
 * @return {Function}         A function that act like ipc.send, but to a
 *                            particular process.
 *
 * @private
 */
function getReplyMethod(request) {
  let target = findTargetFromParentInfo(request);

  if (target) {
    return (...a) => {
      if (target.isCrashed() || target.isDestroyed()) {
        console.log(`Tried to send ${JSON.stringify(a)} as reply but it is destroyed`);
        return;
      }
      
      target.send(...a);
    };
  } else {
    d("Using reply to main process");
    return (...a) => ipc.send(...a);
  }
}

/**
 * Turns an IPC channel into an Observable
 *
 * @param  {String} channel     The IPC channel to listen to via `ipc.on`
 *
 * @return {Observable<Array>}  An Observable which sends IPC args via `onNext`
 *
 * @private
 */
function listenToIpc(channel) {
  return Observable.create((subj) => {
    let listener = (event, ...args) => {
      d(`Got an event for ${channel}: ${JSON.stringify(args)}`);
      subj.onNext(args);
    };

    d(`Setting up listener! ${channel}`);
    ipc.on(channel, listener);

{'fraction_non_alphanumeric': 0.14089121887287026, 'fraction_numerical': 0.0, 'mean_word_length': 3.415621986499518, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '1269995', 'n_tokens_mistral': 1394, 'n_tokens_neox': 1374, 'n_words': 400}