mirror of
https://github.com/liriliri/eruda.git
synced 2026-03-24 09:48:37 +08:00
Dev: Use prettier to format code
This commit is contained in:
@@ -1,19 +1,17 @@
|
||||
import {getType, lenToUtf8Bytes} from './util';
|
||||
import { getType, lenToUtf8Bytes } from './util';
|
||||
import {
|
||||
Emitter,
|
||||
fullUrl,
|
||||
uniqId,
|
||||
isStr,
|
||||
getFileName,
|
||||
Emitter,
|
||||
fullUrl,
|
||||
uniqId,
|
||||
isStr,
|
||||
getFileName,
|
||||
now,
|
||||
toNum,
|
||||
fileSize
|
||||
} from '../lib/util';
|
||||
|
||||
export default class FetchRequest extends Emitter
|
||||
{
|
||||
constructor(url, options = {})
|
||||
{
|
||||
export default class FetchRequest extends Emitter {
|
||||
constructor(url, options = {}) {
|
||||
super();
|
||||
|
||||
if (url instanceof window.Request) url = url.url;
|
||||
@@ -23,8 +21,7 @@ export default class FetchRequest extends Emitter
|
||||
this._options = options;
|
||||
this._method = options.method || 'GET';
|
||||
}
|
||||
send(fetchResult)
|
||||
{
|
||||
send(fetchResult) {
|
||||
let options = this._options;
|
||||
|
||||
let data = isStr(options.body) ? options.body : '';
|
||||
@@ -37,14 +34,12 @@ export default class FetchRequest extends Emitter
|
||||
method: this._method
|
||||
});
|
||||
|
||||
fetchResult.then(res =>
|
||||
{
|
||||
fetchResult.then(res => {
|
||||
res = res.clone();
|
||||
|
||||
let type = getType(res.headers.get('Content-Type'));
|
||||
|
||||
res.text().then(resTxt =>
|
||||
{
|
||||
|
||||
res.text().then(resTxt => {
|
||||
this.emit('update', this._id, {
|
||||
type: type.type,
|
||||
subType: type.subType,
|
||||
@@ -62,28 +57,24 @@ export default class FetchRequest extends Emitter
|
||||
}
|
||||
}
|
||||
|
||||
function getSize(res, resTxt)
|
||||
{
|
||||
function getSize(res, resTxt) {
|
||||
let size = 0;
|
||||
|
||||
let contentLen = res.headers.get('Content-length');
|
||||
|
||||
if (contentLen)
|
||||
{
|
||||
size = toNum(contentLen);
|
||||
} else
|
||||
{
|
||||
if (contentLen) {
|
||||
size = toNum(contentLen);
|
||||
} else {
|
||||
size = lenToUtf8Bytes(resTxt);
|
||||
}
|
||||
|
||||
return `${fileSize(size)}B`;
|
||||
}
|
||||
|
||||
function getHeaders(res)
|
||||
{
|
||||
function getHeaders(res) {
|
||||
let ret = {};
|
||||
|
||||
res.headers.forEach((val, key) => ret[key] = val);
|
||||
res.headers.forEach((val, key) => (ret[key] = val));
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,12 +2,19 @@ import Tool from '../DevTools/Tool';
|
||||
import XhrRequest from './XhrRequest';
|
||||
import FetchRequest from './FetchRequest';
|
||||
import Settings from '../Settings/Settings';
|
||||
import {evalCss, isNative, defaults, now, extend, isEmpty, $, ms} from '../lib/util';
|
||||
import {
|
||||
evalCss,
|
||||
isNative,
|
||||
defaults,
|
||||
now,
|
||||
extend,
|
||||
isEmpty,
|
||||
$,
|
||||
ms
|
||||
} from '../lib/util';
|
||||
|
||||
export default class Network extends Tool
|
||||
{
|
||||
constructor()
|
||||
{
|
||||
export default class Network extends Tool {
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this._style = evalCss(require('./Network.scss'));
|
||||
@@ -18,8 +25,7 @@ export default class Network extends Tool
|
||||
this._isFetchSupported = false;
|
||||
if (window.fetch) this._isFetchSupported = isNative(window.fetch);
|
||||
}
|
||||
init($el, container)
|
||||
{
|
||||
init($el, container) {
|
||||
super.init($el);
|
||||
|
||||
this._container = container;
|
||||
@@ -27,72 +33,64 @@ export default class Network extends Tool
|
||||
this._initCfg();
|
||||
this.overrideXhr();
|
||||
}
|
||||
show()
|
||||
{
|
||||
show() {
|
||||
super.show();
|
||||
|
||||
this._render();
|
||||
}
|
||||
clear()
|
||||
{
|
||||
clear() {
|
||||
this._requests = {};
|
||||
this._render();
|
||||
}
|
||||
overrideXhr()
|
||||
{
|
||||
overrideXhr() {
|
||||
let winXhrProto = window.XMLHttpRequest.prototype;
|
||||
|
||||
let origSend = this._origSend = winXhrProto.send,
|
||||
origOpen = this._origOpen = winXhrProto.open;
|
||||
let origSend = (this._origSend = winXhrProto.send),
|
||||
origOpen = (this._origOpen = winXhrProto.open);
|
||||
|
||||
let self = this;
|
||||
|
||||
winXhrProto.open = function (method, url)
|
||||
{
|
||||
winXhrProto.open = function(method, url) {
|
||||
let xhr = this;
|
||||
|
||||
let req = xhr.erudaRequest = new XhrRequest(xhr, method, url);
|
||||
let req = (xhr.erudaRequest = new XhrRequest(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();
|
||||
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)
|
||||
{
|
||||
winXhrProto.send = function(data) {
|
||||
let req = this.erudaRequest;
|
||||
if (req) req.handleSend(data);
|
||||
|
||||
origSend.apply(this, arguments);
|
||||
};
|
||||
}
|
||||
restoreXhr()
|
||||
{
|
||||
restoreXhr() {
|
||||
let winXhrProto = window.XMLHttpRequest.prototype;
|
||||
|
||||
if (this._origOpen) winXhrProto.open = this._origOpen;
|
||||
if (this._origSend) winXhrProto.send = this._origSend;
|
||||
}
|
||||
overrideFetch()
|
||||
{
|
||||
overrideFetch() {
|
||||
if (!this._isFetchSupported) return;
|
||||
|
||||
let origFetch = this._origFetch = window.fetch;
|
||||
let origFetch = (this._origFetch = window.fetch);
|
||||
|
||||
let self = this;
|
||||
|
||||
window.fetch = function (...args)
|
||||
{
|
||||
window.fetch = function(...args) {
|
||||
let req = new FetchRequest(...args);
|
||||
req.on('send', (id, data) => self._addReq(id, data));
|
||||
req.on('update', (id, data) => self._updateReq(id, data));
|
||||
@@ -103,14 +101,12 @@ export default class Network extends Tool
|
||||
return fetchResult;
|
||||
};
|
||||
}
|
||||
restoreFetch()
|
||||
{
|
||||
restoreFetch() {
|
||||
if (!this._isFetchSupported) return;
|
||||
|
||||
if (this._origFetch) window.fetch = this._origFetch;
|
||||
}
|
||||
_addReq(id, data)
|
||||
{
|
||||
_addReq(id, data) {
|
||||
defaults(data, {
|
||||
name: '',
|
||||
url: '',
|
||||
@@ -131,8 +127,7 @@ export default class Network extends Tool
|
||||
|
||||
this._render();
|
||||
}
|
||||
_updateReq(id, data)
|
||||
{
|
||||
_updateReq(id, data) {
|
||||
let target = this._requests[id];
|
||||
|
||||
if (!target) return;
|
||||
@@ -142,19 +137,18 @@ export default class Network extends Tool
|
||||
target.time = target.time - target.startTime;
|
||||
target.displayTime = ms(target.time);
|
||||
|
||||
if (target.done && (target.status < 200 || target >= 300)) target.hasErr = true;
|
||||
if (target.done && (target.status < 200 || target >= 300))
|
||||
target.hasErr = true;
|
||||
|
||||
this._render();
|
||||
}
|
||||
_bindEvent()
|
||||
{
|
||||
_bindEvent() {
|
||||
let $el = this._$el,
|
||||
container = this._container;
|
||||
|
||||
let self = this;
|
||||
|
||||
$el.on('click', '.eruda-request', function ()
|
||||
{
|
||||
$el.on('click', '.eruda-request', function() {
|
||||
let id = $(this).data('id'),
|
||||
data = self._requests[id];
|
||||
|
||||
@@ -170,8 +164,7 @@ export default class Network extends Tool
|
||||
});
|
||||
}).on('click', '.eruda-clear-request', () => this.clear());
|
||||
|
||||
function showSources(type, data)
|
||||
{
|
||||
function showSources(type, data) {
|
||||
let sources = container.get('sources');
|
||||
if (!sources) return;
|
||||
|
||||
@@ -180,37 +173,34 @@ export default class Network extends Tool
|
||||
container.showTool('sources');
|
||||
}
|
||||
}
|
||||
destroy()
|
||||
{
|
||||
destroy() {
|
||||
super.destroy();
|
||||
|
||||
evalCss.remove(this._style);
|
||||
this.restoreXhr();
|
||||
this.restoreFetch();
|
||||
}
|
||||
_initCfg()
|
||||
{
|
||||
let cfg = this.config = Settings.createCfg('network', {
|
||||
_initCfg() {
|
||||
let cfg = (this.config = Settings.createCfg('network', {
|
||||
overrideFetch: true
|
||||
});
|
||||
}));
|
||||
|
||||
if (cfg.get('overrideFetch')) this.overrideFetch();
|
||||
|
||||
cfg.on('change', (key, val) =>
|
||||
{
|
||||
switch (key)
|
||||
{
|
||||
case 'overrideFetch': return val ? this.overrideFetch() : this.restoreFetch();
|
||||
cfg.on('change', (key, val) => {
|
||||
switch (key) {
|
||||
case 'overrideFetch':
|
||||
return val ? this.overrideFetch() : this.restoreFetch();
|
||||
}
|
||||
});
|
||||
|
||||
let settings = this._container.get('settings');
|
||||
settings.text('Network')
|
||||
.switch(cfg, 'overrideFetch', 'Catch Fetch Requests')
|
||||
.separator();
|
||||
settings
|
||||
.text('Network')
|
||||
.switch(cfg, 'overrideFetch', 'Catch Fetch Requests')
|
||||
.separator();
|
||||
}
|
||||
_render()
|
||||
{
|
||||
_render() {
|
||||
if (!this.active) return;
|
||||
|
||||
let renderData = {};
|
||||
@@ -219,8 +209,7 @@ export default class Network extends Tool
|
||||
|
||||
this._renderHtml(this._tpl(renderData));
|
||||
}
|
||||
_renderHtml(html)
|
||||
{
|
||||
_renderHtml(html) {
|
||||
if (html === this._lastHtml) return;
|
||||
this._lastHtml = html;
|
||||
this._$el.html(html);
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import {getType, lenToUtf8Bytes} from './util';
|
||||
import { getType, lenToUtf8Bytes } from './util';
|
||||
import {
|
||||
Emitter,
|
||||
fullUrl,
|
||||
uniqId,
|
||||
isStr,
|
||||
getFileName,
|
||||
Emitter,
|
||||
fullUrl,
|
||||
uniqId,
|
||||
isStr,
|
||||
getFileName,
|
||||
now,
|
||||
each,
|
||||
trim,
|
||||
@@ -13,10 +13,8 @@ import {
|
||||
fileSize
|
||||
} from '../lib/util';
|
||||
|
||||
export default class XhrRequest extends Emitter
|
||||
{
|
||||
constructor(xhr, method, url)
|
||||
{
|
||||
export default class XhrRequest extends Emitter {
|
||||
constructor(xhr, method, url) {
|
||||
super();
|
||||
|
||||
this._xhr = xhr;
|
||||
@@ -24,8 +22,7 @@ export default class XhrRequest extends Emitter
|
||||
this._url = fullUrl(url);
|
||||
this._id = uniqId('request');
|
||||
}
|
||||
handleSend(data)
|
||||
{
|
||||
handleSend(data) {
|
||||
if (!isStr(data)) data = '';
|
||||
|
||||
this.emit('send', this._id, {
|
||||
@@ -35,8 +32,7 @@ export default class XhrRequest extends Emitter
|
||||
method: this._method
|
||||
});
|
||||
}
|
||||
handleHeadersReceived()
|
||||
{
|
||||
handleHeadersReceived() {
|
||||
let xhr = this._xhr;
|
||||
|
||||
let type = getType(xhr.getResponseHeader('Content-Type'));
|
||||
@@ -49,12 +45,14 @@ export default class XhrRequest extends Emitter
|
||||
resHeaders: getHeaders(xhr)
|
||||
});
|
||||
}
|
||||
handleDone()
|
||||
{
|
||||
handleDone() {
|
||||
let xhr = this._xhr,
|
||||
resType = xhr.responseType;
|
||||
|
||||
let resTxt = (resType === '' || resType === 'text' || resType === 'json') ? xhr.responseText : '';
|
||||
let resTxt =
|
||||
resType === '' || resType === 'text' || resType === 'json'
|
||||
? xhr.responseText
|
||||
: '';
|
||||
|
||||
this.emit('update', this._id, {
|
||||
status: xhr.status,
|
||||
@@ -66,15 +64,13 @@ export default class XhrRequest extends Emitter
|
||||
}
|
||||
}
|
||||
|
||||
function getHeaders(xhr)
|
||||
{
|
||||
function getHeaders(xhr) {
|
||||
let raw = xhr.getAllResponseHeaders(),
|
||||
lines = raw.split('\n');
|
||||
|
||||
let ret = {};
|
||||
|
||||
each(lines, (line) =>
|
||||
{
|
||||
each(lines, line => {
|
||||
line = trim(line);
|
||||
|
||||
if (line === '') return;
|
||||
@@ -87,31 +83,26 @@ function getHeaders(xhr)
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
function getSize(xhr, headersOnly, url)
|
||||
{
|
||||
function getSize(xhr, headersOnly, url) {
|
||||
let size = 0;
|
||||
|
||||
function getStrSize()
|
||||
{
|
||||
if (!headersOnly)
|
||||
{
|
||||
function getStrSize() {
|
||||
if (!headersOnly) {
|
||||
let resType = xhr.responseType;
|
||||
let resTxt = (resType === '' || resType === 'text' || resType === 'json') ? xhr.responseText : '';
|
||||
let resTxt =
|
||||
resType === '' || resType === 'text' || resType === 'json'
|
||||
? xhr.responseText
|
||||
: '';
|
||||
if (resTxt) size = lenToUtf8Bytes(resTxt);
|
||||
}
|
||||
}
|
||||
|
||||
if (isCrossOrig(url))
|
||||
{
|
||||
if (isCrossOrig(url)) {
|
||||
getStrSize();
|
||||
} else
|
||||
{
|
||||
try
|
||||
{
|
||||
} else {
|
||||
try {
|
||||
size = toNum(xhr.getResponseHeader('Content-Length'));
|
||||
} catch (e)
|
||||
{
|
||||
} catch (e) {
|
||||
getStrSize();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import {last} from '../lib/util';
|
||||
import { last } from '../lib/util';
|
||||
|
||||
export function getType(contentType)
|
||||
{
|
||||
export function getType(contentType) {
|
||||
if (!contentType) return 'unknown';
|
||||
|
||||
let type = contentType.split(';')[0].split('/');
|
||||
@@ -12,9 +11,8 @@ export function getType(contentType)
|
||||
};
|
||||
}
|
||||
|
||||
export function lenToUtf8Bytes(str)
|
||||
{
|
||||
export function lenToUtf8Bytes(str) {
|
||||
let m = encodeURIComponent(str).match(/%[89ABab]/g);
|
||||
|
||||
return str.length + (m ? m.length : 0);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user