<commit_before>import SandboxBase from './base';
import { processScript } from '../../processing/script';

export default class ElectronSandbox extends SandboxBase {
    static _createFnWrapper (vm, nativeFn) {
        return (...args) => {
            if (typeof args[0] === 'string')
                args[0] = processScript(args[0]);

            return nativeFn.apply(vm, args);
        };
    }

    attach (window) {
        super.attach(window);

        if (!window.require)
            return;

        var vm = window.require('vm');

        if (!vm)
            return;

        var nativeMethods = this.nativeMethods;

        if (nativeMethods.refreshElectronMeths(vm)) {
            vm.createScript      = ElectronSandbox._createFnWrapper(vm, nativeMethods.createScript);
            vm.runInDebugContext = ElectronSandbox._createFnWrapper(vm, nativeMethods.runInDebugContext);
            vm.runInContext      = ElectronSandbox._createFnWrapper(vm, nativeMethods.runInContext);
            vm.runInNewContext   = ElectronSandbox._createFnWrapper(vm, nativeMethods.runInNewContext);
            vm.runInThisContext  = ElectronSandbox._createFnWrapper(vm, nativeMethods.runInThisContext);
        }
    }
}
<commit_msg>Fix require() on Electron clients (closes #1038) (#1039)<commit_after>import SandboxBase from './base';
import { processScript } from '../../processing/script';
import * as destinationLocation from '../utils/destination-location';

export default class ElectronSandbox extends SandboxBase {
    static _createFnWrapper (vm, nativeFn) {
        return (...args) => {
            if (typeof args[0] === 'string')
                args[0] = processScript(args[0]);

            return nativeFn.apply(vm, args);
        };
    }

    static _overrideElectronModulePaths (window) {
        // NOTE: Need this to avoid Webmake require
        /* eslint-disable no-eval */
        var electronRequire = eval('require');
        /* eslint-enable no-eval */

        var path        = electronRequire('path');
        var destination = destinationLocation.getParsed();

        if (destination.protocol !== 'file:')
            return;

        var pathname = window.process.platform === 'win32' && destination.pathname[0] === '/' ? destination.pathname.substr(1) : destination.pathname;

        window.__filename = path.normalize(decodeURIComponent(pathname));
        window.__dirname = path.dirname(window.__filename);

        window.module.filename = window.__filename;

        window.module.paths = window.module.paths.concat(electronRequire('module')._nodeModulePaths(window.__dirname));
    }

    attach (window) {
        super.attach(window);

        if (!window.require)
            return;

        var vm = window.require('vm');

        if (!vm)
            return;

        var nativeMethods = this.nativeMethods;

        if (nativeMethods.refreshElectronMeths(vm)) {
            vm.createScript      = ElectronSandbox._createFnWrapper(vm, nativeMethods.createScript);
            vm.runInDebugContext = ElectronSandbox._createFnWrapper(vm, nativeMethods.runInDebugContext);
            vm.runInContext      = ElectronSandbox._createFnWrapper(vm, nativeMethods.runInContext);
            vm.runInNewContext   = ElectronSandbox._createFnWrapper(vm, nativeMethods.runInNewContext);
            vm.runInThisContext  = ElectronSandbox._createFnWrapper(vm, nativeMethods.runInThisContext);

            ElectronSandbox._overrideElectronModulePaths(window);
        }
    }
}

{'fraction_non_alphanumeric': 0.11307319851894047, 'fraction_numerical': 0.005126744517231558, 'mean_word_length': 2.658333333333333, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '3805660', 'n_tokens_mistral': 977, 'n_tokens_neox': 918, 'n_words': 177}