| 189 | } |
| 190 | |
| 191 | protected afterFetch(item) { |
| 192 | const then = (resp: Response) => { |
| 193 | item.endTime = Date.now(); |
| 194 | item.costTime = item.endTime - (item.startTime || item.endTime); |
| 195 | item.status = resp.status; |
| 196 | item.statusText = String(resp.status); |
| 197 | |
| 198 | let isChunked = false; |
| 199 | item.header = {}; |
| 200 | for (const [key, value] of resp.headers) { |
| 201 | item.header[key] = value; |
| 202 | isChunked = value.toLowerCase().indexOf('chunked') > -1 ? true : isChunked; |
| 203 | } |
| 204 | // console.log('[Fetch.proxy] afterFetch', 'isChunked:', isChunked, resp.status); |
| 205 | |
| 206 | if (isChunked) { |
| 207 | // when `transfer-encoding` is chunked, the response is a stream which is under loading, |
| 208 | // so the `readyState` should be 3 (Loading), |
| 209 | // and the response should NOT be `clone()` which will affect stream reading. |
| 210 | item.readyState = 3; |
| 211 | } else { |
| 212 | // Otherwise, not chunked, the response is not a stream, |
| 213 | // so it's completed and can be `clone()` for `text()` calling. |
| 214 | item.readyState = 4; |
| 215 | |
| 216 | this.handleResponseBody(resp.clone(), item).then((responseValue: string | ArrayBuffer) => { |
| 217 | // console.log(item.responseType, responseValue) |
| 218 | item.responseSize = typeof responseValue === 'string' ? responseValue.length : responseValue.byteLength; |
| 219 | item.responseSizeText = tool.getBytesText(item.responseSize); |
| 220 | item.response = Helper.genResonseByResponseType(item.responseType, responseValue); |
| 221 | this.onUpdateCallback(item); |
| 222 | }); |
| 223 | } |
| 224 | |
| 225 | this.onUpdateCallback(item); |
| 226 | return new Proxy(resp, new ResponseProxyHandler(resp, item, this.onUpdateCallback)); |
| 227 | }; |
| 228 | return then; |
| 229 | } |
| 230 | |
| 231 | protected handleResponseBody(resp: Response, item: VConsoleNetworkRequestItem) { |
| 232 | // parse response body by Content-Type |