|
| 1 | +import { getBytesText } from '../lib/tool'; |
1 | 2 | import * as Helper from './helper'; |
2 | 3 | import { VConsoleNetworkRequestItem } from './requestItem'; |
3 | | - |
4 | | -type IOnUpdateCallback = (item: VConsoleNetworkRequestItem) => void; |
| 4 | +import type { IOnUpdateCallback } from './helper'; |
5 | 5 |
|
6 | 6 | export class XHRProxyHandler<T extends XMLHttpRequest> implements ProxyHandler<T> { |
7 | 7 | public XMLReq: XMLHttpRequest; |
@@ -68,7 +68,7 @@ export class XHRProxyHandler<T extends XMLHttpRequest> implements ProxyHandler<T |
68 | 68 | this.item.costTime = this.item.endTime - this.item.startTime; |
69 | 69 |
|
70 | 70 | // update data by readyState |
71 | | - Helper.updateItemByReadyState(this.item, this.XMLReq); |
| 71 | + this.updateItemByReadyState(); |
72 | 72 |
|
73 | 73 | // update response by responseType |
74 | 74 | this.item.response = Helper.genResonseByResponseType(this.item.responseType, this.item.response); |
@@ -139,6 +139,73 @@ export class XHRProxyHandler<T extends XMLHttpRequest> implements ProxyHandler<T |
139 | 139 | value.apply(target, args); |
140 | 140 | }); |
141 | 141 | } |
| 142 | + |
| 143 | + /** |
| 144 | + * Update item's properties according to readyState. |
| 145 | + */ |
| 146 | + protected updateItemByReadyState() { |
| 147 | + switch (this.XMLReq.readyState) { |
| 148 | + case 0: // UNSENT |
| 149 | + this.item.status = 0; |
| 150 | + this.item.statusText = 'Pending'; |
| 151 | + if (!this.item.startTime) { |
| 152 | + this.item.startTime = Date.now(); |
| 153 | + } |
| 154 | + break; |
| 155 | + |
| 156 | + case 1: // OPENED |
| 157 | + this.item.status = 0; |
| 158 | + this.item.statusText = 'Pending'; |
| 159 | + if (!this.item.startTime) { |
| 160 | + this.item.startTime = Date.now(); |
| 161 | + } |
| 162 | + break; |
| 163 | + |
| 164 | + case 2: // HEADERS_RECEIVED |
| 165 | + this.item.status = this.XMLReq.status; |
| 166 | + this.item.statusText = 'Loading'; |
| 167 | + this.item.header = {}; |
| 168 | + const header = this.XMLReq.getAllResponseHeaders() || '', |
| 169 | + headerArr = header.split('\n'); |
| 170 | + // extract plain text to key-value format |
| 171 | + for (let i = 0; i < headerArr.length; i++) { |
| 172 | + const line = headerArr[i]; |
| 173 | + if (!line) { continue; } |
| 174 | + const arr = line.split(': '); |
| 175 | + const key = arr[0]; |
| 176 | + const value = arr.slice(1).join(': '); |
| 177 | + this.item.header[key] = value; |
| 178 | + } |
| 179 | + break; |
| 180 | + |
| 181 | + case 3: // LOADING |
| 182 | + this.item.status = this.XMLReq.status; |
| 183 | + this.item.statusText = 'Loading'; |
| 184 | + if (typeof this.XMLReq.response === 'object' && this.XMLReq.response.length) { |
| 185 | + this.item.responseSize = this.XMLReq.response.length; |
| 186 | + this.item.responseSizeText = getBytesText(this.item.responseSize); |
| 187 | + } |
| 188 | + break; |
| 189 | + |
| 190 | + case 4: // DONE |
| 191 | + // `XMLReq.abort()` will change `status` from 200 to 0, so use previous value in this case |
| 192 | + this.item.status = this.XMLReq.status || this.item.status || 0; |
| 193 | + this.item.statusText = String(this.item.status); // show status code when request completed |
| 194 | + this.item.endTime = Date.now(), |
| 195 | + this.item.costTime = this.item.endTime - (this.item.startTime || this.item.endTime); |
| 196 | + this.item.response = this.XMLReq.response; |
| 197 | + if (typeof this.XMLReq.response === 'object' && this.XMLReq.response.length) { |
| 198 | + this.item.responseSize = this.XMLReq.response.length; |
| 199 | + this.item.responseSizeText = getBytesText(this.item.responseSize); |
| 200 | + } |
| 201 | + break; |
| 202 | + |
| 203 | + default: |
| 204 | + this.item.status = this.XMLReq.status; |
| 205 | + this.item.statusText = 'Unknown'; |
| 206 | + break; |
| 207 | + } |
| 208 | + } |
142 | 209 | } |
143 | 210 |
|
144 | 211 | export class XHRProxy { |
|
0 commit comments