Skip to content

Commit

Permalink
Merge pull request #524 from Tencent/dev
Browse files Browse the repository at this point in the history
v3.14.4
  • Loading branch information
Maizify committed Mar 31, 2022
2 parents 5bea445 + c6f908c commit 1dcdc22
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 91 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
English | [简体中文](./CHANGELOG_CN.md)

## 3.14.4 (2022-03-31)

- `Fix(Network)` Fix CPU high load bug when response is a large string. (issue #515)
- `Fix(Network)` Fix missing Request Headers issue in XHR. (issue #522)


## 3.14.3 (2022-03-28)

- `Fix(Network)` Fix `response.size` error.


## 3.14.2 (2022-03-25)

- `Fix(Network)` Remove debugging console.log.
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG_CN.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
[English](./CHANGELOG.md) | 简体中文

## 3.14.4 (2022-03-31)

- `Fix(Network)` 修复回包超大时导致的卡死问题。 (issue #515)
- `Fix(Network)` 修复 XHR 中缺失显示 Request Headers 的问题。 (issue #522)


## 3.14.3 (2022-03-28)

- `Fix(Network)` 修复 `response.size` 错误。


## 3.14.2 (2022-03-25)

- `Fix(Network)` 删除调试日志。
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ See [Tutorial](./doc/tutorial.md) for more usage details.

For installation, there are 2 primary ways of adding vConsole to a project:

#### Method 1: Using npm (Suggested)
#### Method 1: Using npm (Recommended)

```bash
$ npm install vconsole
Expand Down
5 changes: 5 additions & 0 deletions dev/data/massive.json

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions dev/log.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<a onclick="allDataTypes()" href="javascript:;" class="weui_btn weui_btn_default">All Data Types</a>
<a onclick="logTypes()" href="javascript:;" class="weui_btn weui_btn_default">Log/Info/Debug/Warn/Error</a>
<a onclick="differentPanelLog()" href="javascript:;" class="weui_btn weui_btn_default">Output to Different Panels</a>
<a onclick="logTime()" href="javascript:;" class="weui_btn weui_btn_default">console.time</a>
</div>

<div class="section">
Expand Down Expand Up @@ -126,6 +127,16 @@
console.log('[foobar]', 'This log should be shown in Log panel. (No "foobar" plugin.)');
}

function logTime() {
vConsole.show();
const label = 'aaa';
console.time(label);
console.log('Wait...');
setTimeout(() => {
console.timeEnd(label);
}, Math.ceil(Math.random() * 2000));
}

function formattingLog() {
console.log('%c red,%c blue, %c bold.', 'color:red', 'color:blue', 'font-weight:bold', '(Use %c format)');
console.log('%c %c Continuous styling, only blue is applied.', 'color:red', 'color:blue', '(Use %c format)');
Expand Down
45 changes: 13 additions & 32 deletions dev/network.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
<a onclick="getAjax()" href="javascript:;" class="weui_btn weui_btn_default">GET XHR</a>
<a onclick="postAjax()" href="javascript:;" class="weui_btn weui_btn_default">POST XHR (Data: Object)</a>
<a onclick="postAjax('json')" href="javascript:;" class="weui_btn weui_btn_default">POST XHR (Data: JSON String)</a>
<a onclick="postAjax('', 'massive.json')" href="javascript:;" class="weui_btn weui_btn_default">POST XHR (Massive Resp)</a>
<a onclick="optionsXHR()" href="javascript:;" class="weui_btn weui_btn_default">OPTIONS XHR</a>
<a onclick="xhrStream()" href="javascript:;" class="weui_btn weui_btn_default">XHR Stream</a>
<a onclick="xhrStream('flv')" href="javascript:;" class="weui_btn weui_btn_default">XHR Chunked: flv</a>
<a onclick="xhrStream('json')" href="javascript:;" class="weui_btn weui_btn_default">XHR Chunked: json</a>
</div>

<div class="section">
Expand All @@ -42,6 +44,7 @@

<a onclick="sendBeacon()" href="javascript:;" class="weui_btn weui_btn_default">sendBeacon</a>
<a onclick="axiosRequest('GET')" href="javascript:;" class="weui_btn weui_btn_default">Axios: GET</a>
<a onclick="axiosRequest('POST')" href="javascript:;" class="weui_btn weui_btn_default">Axios: POST</a>
<a onclick="scrolling()" href="javascript:;" class="weui_btn weui_btn_default">Scrolling</a>
<a onclick="crossDomain()" href="javascript:;" class="weui_btn weui_btn_default">Cross Domain</a>
</div>
Expand Down Expand Up @@ -70,14 +73,11 @@

<script>
const vConsole = new window.VConsole({
maxLogNumber: 1000,
// defaultPlugins: ['system', 'network'],
// defaultPlugins: ['system'],
// disableLogScrolling: true,
onReady: function() {
console.log('vConsole is ready.');
setTimeout(() => {
vConsole.showPlugin('network');
}, 100);
vConsole.showPlugin('network');
}
});

Expand All @@ -90,7 +90,8 @@
vConsole.show();
}

function postAjax(postType) {
function postAjax(postType, file = 'large.json') {
showPanel();
const xhr = new XMLHttpRequest();
xhr.onload = () => {
console.log('post XHR Response:', JSON.parse(xhr.response));
Expand All @@ -113,7 +114,7 @@
postData = JSON.stringify(postData);
}
console.log('post data:', postData);
xhr.open('POST', './data/large.json?method=fetchPost&c=中文');
xhr.open('POST', `./data/${file}?method=fetchPost&c=中文`);
xhr.send(postData);
// xhr.send(JSON.stringify({foo: 'bar', id: Math.random(), '<xss0>': '<xss1> XSS Attack!'}));
}
Expand All @@ -133,7 +134,7 @@
}

function getFetch() {
vConsole.show();
showPanel();
window.fetch('./data/success.json?method=fetchGet&id=' + Math.random(), {
method: 'GET',
headers: {
Expand Down Expand Up @@ -296,9 +297,9 @@
});
}

function xhrStream() {
function xhrStream(format = 'flv') {
vConsole.show();
const url = './data/stream.flv?id=' + Math.random();
const url = `./data/success.${format}?chunked=1&id=` + Math.random();
const xhr = new XMLHttpRequest();
xhr.timeout = 11000;
console.log('xhr type:', typeof xhr, xhr instanceof XMLHttpRequest);
Expand Down Expand Up @@ -357,26 +358,7 @@
}

function axiosRequest(method) {
console.info('axiosRequest() Start');
axios({
method,
url: './data/success.json?method=axios&r=' + Math.random(),
headers: { 'content-type': 'application/x-www-form-urlencoded' },
data: {
foo: 'bar'
}
})
.then(function(response) {
console.log('axiosRequest response:', response);
})
.catch(function(error) {
console.log('axiosRequest error:', error);
});
console.info('axiosRequest() End');
}

function axiosRequest(method) {
console.info('axiosRequest() Start');
vConsole.show();
axios({
method,
url: './data/success.json?method=axios&r=' + Math.random(),
Expand All @@ -391,7 +373,6 @@
.catch(function(error) {
console.log('axiosRequest error:', error);
});
console.info('axiosRequest() End');
}

function optionsXHR() {
Expand Down
8 changes: 5 additions & 3 deletions dev/node.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import VConsole from 'vconsole';
import * as tool from '../src/lib/tool';
const massiveData = require('./data/massive.json');
// console.log('massiveData:', massiveData);

const vconsole = new VConsole();
console.log(vconsole.version);
const ret = tool.safeJSONStringify(massiveData, { maxDepth: 10, keyMaxLen: 5000, pretty: true });
console.log(ret);
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vconsole",
"version": "3.14.3",
"version": "3.14.4",
"description": "A lightweight, extendable front-end developer tool for mobile web page.",
"homepage": "https://github.com/Tencent/vConsole",
"files": [
Expand Down
38 changes: 14 additions & 24 deletions src/lib/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,8 @@ const _safeJSONStringifyCircularFinder = () => {
const _safeJSONStringifyFlatValue = (value: any, maxLen = 0) => {
let str = '';
if (isString(value)) {
const len = value.length;
if (maxLen > 0 && len > maxLen) {
value = subString(value, maxLen) + `...(${getBytesText(getStringBytes(value))})`;
if (maxLen > 0) {
value = getStringWithinLength(value, maxLen);
}
str += '"' + getVisibleText(value) + '"';
} else if (isSymbol(value)) {
Expand Down Expand Up @@ -337,6 +336,11 @@ export function JSONStringify(value: any, replacer?: (this: any, key: string, va
return stringifyResult;
}

/**
* Get the bytes of a string.
* @example 'a' = 1
* @example '好' = 3
*/
export function getStringBytes(str: string) {
try {
return encodeURI(str).split(/%(?:u[0-9A-F]{2})?[0-9A-F]{2}|./).length - 1;
Expand All @@ -345,6 +349,9 @@ export function getStringBytes(str: string) {
}
}

/**
* Convert bytes number to 'MB' or 'KB' string.
*/
export function getBytesText(bytes: number) {
if (bytes <= 0) {
return '';
Expand All @@ -358,31 +365,14 @@ export function getBytesText(bytes: number) {
return bytes + ' B';
}

const _subStringPattern = /[^\x00-\xff]/g
export function subString(str: string, len: number) {
let m: number;

if (str.replace(_subStringPattern, '**').length > len) {
m = Math.floor(len / 2);

for (let i = m, l = str.length; i < l; i++) {
const sub = str.substring(0, i);
if (sub.replace(_subStringPattern, '**').length >= len) {
return sub;
}
}
}

return str;
}

/**
* Get a string within a limited max length.
* The byte size of the string will be appended to the string when reached the limit.
* @return 'some string...(3.1 MB)'
*/
export function getStringWithinLength(str: string, maxLen: number) {
const bytes = getStringBytes(str);
if (bytes > maxLen) {
str = subString(str, maxLen) + `...(${getBytesText(bytes)})`;
if (str.length > maxLen) {
str = str.substring(0, maxLen) + `...(${getBytesText(getStringBytes(str))})`;
}
return str;
}
Expand Down
6 changes: 3 additions & 3 deletions src/network/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ export const genResonseByResponseType = (responseType: string, response: any) =>
if (tool.isString(response)) {
try {
ret = JSON.parse(response);
ret = tool.safeJSONStringify(ret, { maxDepth: 10, keyMaxLen: 500000, pretty: true });
ret = tool.safeJSONStringify(ret, { maxDepth: 10, keyMaxLen: 10000, pretty: true });
} catch (e) {
// not a JSON string
ret = tool.getStringWithinLength(String(response), 500000);
ret = tool.getStringWithinLength(String(response), 10000);
}
} else if (tool.isObject(response) || tool.isArray(response)) {
ret = tool.safeJSONStringify(response, { maxDepth: 10, keyMaxLen: 500000, pretty: true });
ret = tool.safeJSONStringify(response, { maxDepth: 10, keyMaxLen: 10000, pretty: true });
} else if (typeof response !== 'undefined') {
ret = Object.prototype.toString.call(response);
}
Expand Down
Loading

0 comments on commit 1dcdc22

Please sign in to comment.