<commit_before>import Tool from '../DevTools/Tool'
import Request from './Request'
import util from '../lib/util'

export default class Network extends Tool
{
    constructor()
    {
        super();

        this._style = util.evalCss(require('./Network.scss'));

        this.name = 'network';
        this._performanceTimingData = [];
        this._performanceTiming = {};
        this._resourceTimingData = [];
        this._requests = {};
        this._tpl = require('./Network.hbs');

        let performance = this._performance = window.webkitPerformance || window.performance;
        this._hasResourceTiming = performance && util.isFn(performance.getEntries);
    }
    init($el, parent)
    {
        super.init($el);

        this._parent = parent;
        this._bindEvent();
        this._initCfg();
    }
    show()
    {
        super.show();

        this._render();
    }
    overrideXhr()
    {
        let winXhrProto = window.XMLHttpRequest.prototype;

        let origSend = this._origSend = winXhrProto.send,
            origOpen = this._origOpen = winXhrProto.open;

        let self = this;

        winXhrProto.open = function (method, url)
        {
            let xhr = this;

            let req = xhr.erudaRequest = new Request(xhr, method, url);

            req.on('send', (id, data) => self._addReq(id, data));
            req.on('update', (id, data) => self._updateReq(id, data));

            xhr.addEventListener('readystatechange', function ()
            {
                switch (xhr.readyState)
                {
                    case 2: return req.handleHeadersReceived();
                    case 4: return req.handleDone();
                }
            });

            origOpen.apply(this, arguments);
        };

        winXhrProto.send = function (data)
        {
            let req = this.erudaRequest;
            if (req) req.handleSend(data);

            origSend.apply(this, arguments);
        };
    }
    restoreXhr()
    {
        let winXhrProto = window.XMLHttpRequest.prototype;

        if (this._origOpen) winXhrProto.open = this._origOpen;
        if (this._origSend) winXhrProto.send = this._origSend;
    }
    _addReq(id, data)
    {
        util.defaults(data, {
            name: '',
            url: '',
            status: 'pending',
            type: 'unknown',
            subType: 'unknown',
            size: 0,
            data: '',
            method: 'GET',
            startTime: util.now(),
            time: 0,
            resHeaders: {},
            resTxt: '',
            xhr: {},
            done: false
        });

        this._requests[id] = data;

        this._render();
    }
    _updateReq(id, data)
    {
        let target = this._requests[id];

        if (!target) return;

        util.extend(target, data);

        target.time = target.time - target.startTime;
        target.displayTime = formatTime(target.time);

        if (target.done && (target.status < 200 || target >= 300)) target.hasErr = true;

        this._render();
    }
    _bindEvent()
    {
        let $el = this._$el,
            parent = this._parent;

        let self = this;

        $el.on('click', '.eruda-performance-timing', function ()
        {
            $el.find('.eruda-performance-timing-data').show();
        }).on('click', '.eruda-request', function ()
        {
            let id = util.$(this).data('id'),
                data = self._requests[id];

            if (!data.done) return;

            showSources('http', {
                url: data.url,
                data: data.data,
                resTxt: data.resTxt,
                type: data.type,
                subType: data.subType,
                resHeaders: data.resHeaders
            });
        }).on('click', '.eruda-entry', function ()
        {
            let idx = util.$(this).data('idx'),
                data = self._resourceTimingData[Number(idx)];

            if (data.initiatorType === 'img')
            {
                showSources('img', data.url);
            }
        }).on('click', '.eruda-clear-xhr', function ()
        {
            self._requests = {};
            self._render();
        });

        function showSources(type, data)
        {
            let sources = parent.get('sources');
            if (!sources) return;

            sources.set(type, data);

            parent.showTool('sources');
        }
    }
    _getPerformanceTimingData()
    {
        let performance = this._performance;

        if (!performance) return;

        let timing = performance.timing;

        let data = [];

        /* eslint-disable no-unused-vars */
        let {
                navigationStart,
                unloadEventStart,
                unloadEventEnd,
                redirectStart,
                redirectEnd,<commit_msg>Fix: Performance timing unsupported #40<commit_after>import Tool from '../DevTools/Tool'
import Request from './Request'
import util from '../lib/util'

export default class Network extends Tool
{
    constructor()
    {
        super();

        this._style = util.evalCss(require('./Network.scss'));

        this.name = 'network';
        this._performanceTimingData = [];
        this._performanceTiming = {};
        this._resourceTimingData = [];
        this._requests = {};
        this._tpl = require('./Network.hbs');

        let performance = this._performance = window.webkitPerformance || window.performance;
        this._hasResourceTiming = performance && util.isFn(performance.getEntries);
    }
    init($el, parent)
    {
        super.init($el);

        this._parent = parent;
        this._bindEvent();
        this._initCfg();
    }
    show()
    {
        super.show();

        this._render();
    }
    overrideXhr()
    {
        let winXhrProto = window.XMLHttpRequest.prototype;

        let origSend = this._origSend = winXhrProto.send,
            origOpen = this._origOpen = winXhrProto.open;

        let self = this;

        winXhrProto.open = function (method, url)
        {
            let xhr = this;

            let req = xhr.erudaRequest = new Request(xhr, method, url);

            req.on('send', (id, data) => self._addReq(id, data));
            req.on('update', (id, data) => self._updateReq(id, data));

            xhr.addEventListener('readystatechange', function ()
            {
                switch (xhr.readyState)
                {
                    case 2: return req.handleHeadersReceived();
                    case 4: return req.handleDone();
                }
            });

            origOpen.apply(this, arguments);
        };

        winXhrProto.send = function (data)
        {
            let req = this.erudaRequest;
            if (req) req.handleSend(data);

            origSend.apply(this, arguments);
        };
    }
    restoreXhr()
    {
        let winXhrProto = window.XMLHttpRequest.prototype;

        if (this._origOpen) winXhrProto.open = this._origOpen;
        if (this._origSend) winXhrProto.send = this._origSend;
    }
    _addReq(id, data)
    {
        util.defaults(data, {
            name: '',
            url: '',
            status: 'pending',
            type: 'unknown',
            subType: 'unknown',
            size: 0,
            data: '',
            method: 'GET',
            startTime: util.now(),
            time: 0,
            resHeaders: {},
            resTxt: '',
            xhr: {},
            done: false
        });

        this._requests[id] = data;

        this._render();
    }
    _updateReq(id, data)
    {
        let target = this._requests[id];

        if (!target) return;

        util.extend(target, data);

        target.time = target.time - target.startTime;
        target.displayTime = formatTime(target.time);

        if (target.done && (target.status < 200 || target >= 300)) target.hasErr = true;

        this._render();
    }
    _bindEvent()
    {
        let $el = this._$el,
            parent = this._parent;

        let self = this;

        $el.on('click', '.eruda-performance-timing', function ()
        {
            $el.find('.eruda-performance-timing-data').show();
        }).on('click', '.eruda-request', function ()
        {
            let id = util.$(this).data('id'),
                data = self._requests[id];

            if (!data.done) return;

            showSources('http', {
                url: data.url,
                data: data.data,
                resTxt: data.resTxt,
                type: data.type,
                subType: data.subType,
                resHeaders: data.resHeaders
            });
        }).on('click', '.eruda-entry', function ()
        {
            let idx = util.$(this).data('idx'),
                data = self._resourceTimingData[Number(idx)];

            if (data.initiatorType === 'img')
            {
                showSources('img', data.url);
            }
        }).on('click', '.eruda-clear-xhr', function ()
        {
            self._requests = {};
            self._render();
        });

        function showSources(type, data)
        {
            let sources = parent.get('sources');
            if (!sources) return;

            sources.set(type, data);

            parent.showTool('sources');
        }
    }
    _getPerformanceTimingData()
    {
        let performance = this._performance;
        if (!performance) return;

        let timing = performance.timing;
        if (!timing) return;

        let data = [];

        /* eslint-disable no-unused-vars */
        let {
                navigationStart,
                unloadEventStart,
                unloadEventEnd,
                redirectStart,
                redirectEnd,
{'fraction_non_alphanumeric': 0.13077557755775576, 'fraction_numerical': 0.002268976897689769, 'mean_word_length': 1.5024516129032257, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '733913', 'n_tokens_mistral': 2765, 'n_tokens_neox': 2611, 'n_words': 530}