Skip to content

Commit b0787df

Browse files
committed
Fix(Network): Do not proxy response body reader when response is done.
1 parent 2579976 commit b0787df

File tree

5 files changed

+41
-10
lines changed

5 files changed

+41
-10
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ English | [简体中文](./CHANGELOG_CN.md)
55
- `Fix(Core)` Fix unexpected error when init vConsole twice in short time. (issue #525)
66
- `Fix(Log)` Fix bug that `console.time | console.timeEnd` do not output log. (issue #523)
77
- `Fix(Element)` Fix `undefined is not an object` error when updating attributes. (issue #526)
8+
- `Fix(Network)` Do not proxy response body reader when response is done.
89

910

1011
## 3.14.4 (2022-03-31)

CHANGELOG_CN.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- `Fix(Core)` 修复极短时间内重复初始化 vConsole 导致的报错。 (issue #525)
66
- `Fix(Log)` 修复 `console.time | console.timeEnd` 不输出日志的问题。 (issue #523)
77
- `Fix(Element)` 修复更新 attributes 时引起的 `undefined is not an object` 错误。 (issue #526)
8+
- `Fix(Network)` 当请求完成后,不再代理 response body reader。
89

910

1011
## 3.14.4 (2022-03-31)

dev/network.html

+16-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
1111
<script src="https://unpkg.com/zepto/dist/zepto.min.js"></script>
12+
<script src="https://unpkg.com/wavesurfer.js"></script>
1213
<script src="../dist/vconsole.min.js"></script>
1314
</head>
1415
<body ontouchstart>
@@ -49,6 +50,7 @@
4950
<a onclick="zeptoRequest('POST')" href="javascript:;" class="weui_btn weui_btn_default">Zepto: POST</a>
5051
<a onclick="scrolling()" href="javascript:;" class="weui_btn weui_btn_default">Scrolling</a>
5152
<a onclick="crossDomain()" href="javascript:;" class="weui_btn weui_btn_default">Cross Domain</a>
53+
<a onclick="testWavesurfer()" href="javascript:;" class="weui_btn weui_btn_default">Wavesurfer.js</a>
5254
</div>
5355

5456
<div class="section">
@@ -176,7 +178,7 @@
176178
}
177179

178180
function getFetchSimple() {
179-
window.fetch('./data/large.json?type=fetchGet&id=' + Math.random()).then((data) => {
181+
window.fetch('./data/massive.json?type=fetchGet&id=' + Math.random()).then((data) => {
180182
return data.json();
181183
}).then((data) => {
182184
console.log('getFetchSimple Response:', data);
@@ -439,6 +441,19 @@
439441
console.info('optionsFetch() End');
440442
}
441443

444+
function testWavesurfer() {
445+
showPanel();
446+
const div = document.createElement('DIV');
447+
div.id = 'waveform';
448+
document.body.append(div);
449+
var wavesurfer = WaveSurfer.create({
450+
container: '#waveform',
451+
waveColor: 'violet',
452+
progressColor: 'purple'
453+
});
454+
wavesurfer.load('./data/a.wav?method=fetch');
455+
}
456+
442457
function scrolling() {
443458
vConsole.setOption('maxNetworkNumber', 60);
444459
let n = 0

src/network/fetch.proxy.ts

+18-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export class ResponseProxyHandler<T extends Response> implements ProxyHandler<T>
2323
}
2424

2525
public get(target: T, key: string) {
26-
// if (typeof key === 'string') { console.log('Proxy get:', key) }
26+
// if (typeof key === 'string') { console.log('[Fetch.proxy] get:', key) }
27+
const value = Reflect.get(target, key);
2728
switch (key) {
2829
case 'arrayBuffer':
2930
case 'blob':
@@ -32,33 +33,41 @@ export class ResponseProxyHandler<T extends Response> implements ProxyHandler<T>
3233
case 'text':
3334
return () => {
3435
this.item.responseType = <any>key.toLowerCase();
35-
return target[key]().then((resp) => {
36+
return value.apply(target).then((resp) => {
3637
this.item.response = Helper.genResonseByResponseType(this.item.responseType, resp);
3738
this.onUpdateCallback(this.item);
3839
return resp;
3940
});
4041
};
4142
}
42-
if (typeof target[key] === 'function') {
43-
return (...args) => {
44-
return target[key].apply(target, args);
45-
};
43+
if (typeof value === 'function') {
44+
return value.bind(target);
4645
} else {
47-
return Reflect.get(target, key);
46+
return value;
4847
}
4948
}
5049

5150
protected mockReader() {
5251
let readerReceivedValue: Uint8Array;
5352
const _getReader = this.resp.body.getReader;
5453
this.resp.body.getReader = () => {
54+
// console.log('[Fetch.proxy] getReader');
5555
const reader = <ReturnType<typeof _getReader>>_getReader.apply(this.resp.body);
56+
57+
// when readyState is already 4,
58+
// it's not a chunked stream, or it had already been read.
59+
// so should not update status.
60+
if (this.item.readyState === 4) {
61+
return reader;
62+
}
63+
5664
const _read = reader.read;
5765
const _cancel = reader.cancel;
5866
this.item.responseType = 'arraybuffer';
5967

6068
reader.read = () => {
6169
return (<ReturnType<typeof _read>>_read.apply(reader)).then((result) => {
70+
// console.log('[Fetch.proxy] read', result.done);
6271
if (!readerReceivedValue) {
6372
readerReceivedValue = new Uint8Array(result.value);
6473
} else {
@@ -142,7 +151,7 @@ export class FetchProxyHandler<T extends typeof fetch> implements ProxyHandler<T
142151
item.statusText = 'Pending';
143152
item.readyState = 1;
144153
if (!item.startTime) { // UNSENT
145-
item.startTime = (+new Date());
154+
item.startTime = Date.now();
146155
}
147156

148157
if (Object.prototype.toString.call(requestHeader) === '[object Headers]') {
@@ -183,6 +192,7 @@ export class FetchProxyHandler<T extends typeof fetch> implements ProxyHandler<T
183192
item.header[key] = value;
184193
isChunked = value.toLowerCase().indexOf('chunked') > -1 ? true : isChunked;
185194
}
195+
// console.log('[Fetch.proxy] afterFetch', 'isChunked:', isChunked, resp.status);
186196

187197
if (isChunked) {
188198
// when `transfer-encoding` is chunked, the response is a stream which is under loading,

webpack.serve.config.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,15 @@ module.exports = (env, argv) => {
2525
],
2626
onBeforeSetupMiddleware(devServer) {
2727
devServer.app.all('*', (req, res) => {
28+
const contentType = {
29+
'flv': 'video/x-flv',
30+
'wav': 'audio/x-wav',
31+
};
2832
const fileType = req.path.split('.').pop();
2933
// console.log('Req:::', fileType, req.path, req.query);
3034
if (fileType === 'flv') {
3135
res.set({
32-
'Content-Type': 'video/x-flv',
36+
'Content-Type': contentType[fileType],
3337
// 'Content-Type', 'application/octet-stream',
3438
'Transfer-Encoding': 'chunked',
3539
'Connection': 'keep-alive',

0 commit comments

Comments
 (0)