@@ -23,7 +23,8 @@ export class ResponseProxyHandler<T extends Response> implements ProxyHandler<T>
23
23
}
24
24
25
25
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 ) ;
27
28
switch ( key ) {
28
29
case 'arrayBuffer' :
29
30
case 'blob' :
@@ -32,33 +33,41 @@ export class ResponseProxyHandler<T extends Response> implements ProxyHandler<T>
32
33
case 'text' :
33
34
return ( ) => {
34
35
this . item . responseType = < any > key . toLowerCase ( ) ;
35
- return target [ key ] ( ) . then ( ( resp ) => {
36
+ return value . apply ( target ) . then ( ( resp ) => {
36
37
this . item . response = Helper . genResonseByResponseType ( this . item . responseType , resp ) ;
37
38
this . onUpdateCallback ( this . item ) ;
38
39
return resp ;
39
40
} ) ;
40
41
} ;
41
42
}
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 ) ;
46
45
} else {
47
- return Reflect . get ( target , key ) ;
46
+ return value ;
48
47
}
49
48
}
50
49
51
50
protected mockReader ( ) {
52
51
let readerReceivedValue : Uint8Array ;
53
52
const _getReader = this . resp . body . getReader ;
54
53
this . resp . body . getReader = ( ) => {
54
+ // console.log('[Fetch.proxy] getReader');
55
55
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
+
56
64
const _read = reader . read ;
57
65
const _cancel = reader . cancel ;
58
66
this . item . responseType = 'arraybuffer' ;
59
67
60
68
reader . read = ( ) => {
61
69
return ( < ReturnType < typeof _read > > _read . apply ( reader ) ) . then ( ( result ) => {
70
+ // console.log('[Fetch.proxy] read', result.done);
62
71
if ( ! readerReceivedValue ) {
63
72
readerReceivedValue = new Uint8Array ( result . value ) ;
64
73
} else {
@@ -142,7 +151,7 @@ export class FetchProxyHandler<T extends typeof fetch> implements ProxyHandler<T
142
151
item . statusText = 'Pending' ;
143
152
item . readyState = 1 ;
144
153
if ( ! item . startTime ) { // UNSENT
145
- item . startTime = ( + new Date ( ) ) ;
154
+ item . startTime = Date . now ( ) ;
146
155
}
147
156
148
157
if ( Object . prototype . toString . call ( requestHeader ) === '[object Headers]' ) {
@@ -183,6 +192,7 @@ export class FetchProxyHandler<T extends typeof fetch> implements ProxyHandler<T
183
192
item . header [ key ] = value ;
184
193
isChunked = value . toLowerCase ( ) . indexOf ( 'chunked' ) > - 1 ? true : isChunked ;
185
194
}
195
+ // console.log('[Fetch.proxy] afterFetch', 'isChunked:', isChunked, resp.status);
186
196
187
197
if ( isChunked ) {
188
198
// when `transfer-encoding` is chunked, the response is a stream which is under loading,
0 commit comments