|
@@ -4,6 +4,12 @@
|
4
|
4
|
|
5
|
5
|
import RNFetchBlob from '../index.js'
|
6
|
6
|
import XMLHttpRequestEventTarget from './XMLHttpRequestEventTarget.js'
|
|
7
|
+import Log from '../utils/log.js'
|
|
8
|
+import Blob from './Blob.js'
|
|
9
|
+
|
|
10
|
+const log = new Log('XMLHttpRequest')
|
|
11
|
+
|
|
12
|
+log.level(3)
|
7
|
13
|
|
8
|
14
|
const UNSENT = 0
|
9
|
15
|
const OPENED = 1
|
|
@@ -61,7 +67,7 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
|
61
|
67
|
|
62
|
68
|
constructor(...args) {
|
63
|
69
|
super()
|
64
|
|
- console.log('XMLHttpRequest constructor called', args)
|
|
70
|
+ log.verbose('XMLHttpRequest constructor called', args)
|
65
|
71
|
this._config = {}
|
66
|
72
|
this._args = {}
|
67
|
73
|
this._headers = {}
|
|
@@ -69,7 +75,7 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
|
69
|
75
|
|
70
|
76
|
// XMLHttpRequest.open, always async, user and password not supported.
|
71
|
77
|
open(method:string, url:string, async:true, user:any, password:any) {
|
72
|
|
- console.log('XMLHttpRequest open ', method, url, async, user, password)
|
|
78
|
+ log.verbose('XMLHttpRequest open ', method, url, async, user, password)
|
73
|
79
|
this._method = method
|
74
|
80
|
this._url = url
|
75
|
81
|
this.readyState = XMLHttpRequest.OPENED
|
|
@@ -80,10 +86,16 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
|
80
|
86
|
* @param {any} body Body in RNfetchblob flavor
|
81
|
87
|
*/
|
82
|
88
|
send(body) {
|
83
|
|
- console.log('XMLHttpRequest send ', body)
|
|
89
|
+ log.verbose('XMLHttpRequest send ', body)
|
84
|
90
|
let {_method, _url, _headers } = this
|
85
|
|
- console.log('sending request with args', _method, _url, _headers, body)
|
|
91
|
+ log.verbose('sending request with args', _method, _url, _headers, body)
|
|
92
|
+
|
86
|
93
|
this._upload = new XMLHttpRequestEventTarget()
|
|
94
|
+ log.verbose(typeof body, body instanceof FormData)
|
|
95
|
+ if(Array.isArray(body)) {
|
|
96
|
+ // TODO
|
|
97
|
+ }
|
|
98
|
+
|
87
|
99
|
this.dispatchEvent('loadstart')
|
88
|
100
|
if(this.onloadstart)
|
89
|
101
|
this.onloadstart()
|
|
@@ -98,17 +110,17 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
|
98
|
110
|
}
|
99
|
111
|
|
100
|
112
|
overrideMimeType(mime:string) {
|
101
|
|
- console.log('XMLHttpRequest overrideMimeType', mime)
|
|
113
|
+ log.verbose('XMLHttpRequest overrideMimeType', mime)
|
102
|
114
|
this._headers['content-type'] = mime
|
103
|
115
|
}
|
104
|
116
|
|
105
|
117
|
setRequestHeader(name, value) {
|
106
|
|
- console.log('XMLHttpRequest set header', name, value)
|
|
118
|
+ log.verbose('XMLHttpRequest set header', name, value)
|
107
|
119
|
this._headers[name] = value
|
108
|
120
|
}
|
109
|
121
|
|
110
|
122
|
abort() {
|
111
|
|
- console.log('XMLHttpRequest abort ')
|
|
123
|
+ log.verbose('XMLHttpRequest abort ')
|
112
|
124
|
if(!this._task)
|
113
|
125
|
return
|
114
|
126
|
this._task.cancel((err) => {
|
|
@@ -130,7 +142,7 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
|
130
|
142
|
}
|
131
|
143
|
|
132
|
144
|
getResponseHeader(field:string):string | null {
|
133
|
|
- console.log('XMLHttpRequest get header', field)
|
|
145
|
+ log.verbose('XMLHttpRequest get header', field)
|
134
|
146
|
if(!this.responseHeaders)
|
135
|
147
|
return null
|
136
|
148
|
return this.responseHeaders[field] || null
|
|
@@ -138,7 +150,7 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
|
138
|
150
|
}
|
139
|
151
|
|
140
|
152
|
getAllResponseHeaders():string | null {
|
141
|
|
- console.log('XMLHttpRequest get all headers')
|
|
153
|
+ log.verbose('XMLHttpRequest get all headers')
|
142
|
154
|
if(!this.responseHeaders)
|
143
|
155
|
return null
|
144
|
156
|
let result = ''
|
|
@@ -150,7 +162,7 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
|
150
|
162
|
}
|
151
|
163
|
|
152
|
164
|
_headerReceived(e) {
|
153
|
|
- console.log('header received ', e)
|
|
165
|
+ log.verbose('header received ', e)
|
154
|
166
|
this.responseURL = this._url
|
155
|
167
|
if(e.state === "2") {
|
156
|
168
|
this.readyState = XMLHttpRequest.HEADERS_RECEIVED
|
|
@@ -162,7 +174,7 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
|
162
|
174
|
}
|
163
|
175
|
|
164
|
176
|
_progressEvent(send:number, total:number) {
|
165
|
|
- console.log(this.readyState)
|
|
177
|
+ log.verbose(this.readyState)
|
166
|
178
|
if(this.readyState === XMLHttpRequest.HEADERS_RECEIVED)
|
167
|
179
|
this.readyState = XMLHttpRequest.LOADING
|
168
|
180
|
let lengthComputable = false
|
|
@@ -179,7 +191,7 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
|
179
|
191
|
}
|
180
|
192
|
|
181
|
193
|
_onError(err) {
|
182
|
|
- console.log('XMLHttpRequest error', err)
|
|
194
|
+ log.verbose('XMLHttpRequest error', err)
|
183
|
195
|
this.statusText = err
|
184
|
196
|
this.status = String(err).match(/\d+/)
|
185
|
197
|
this.status = this.status ? Math.floor(this.status) : 404
|
|
@@ -199,7 +211,7 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
|
199
|
211
|
}
|
200
|
212
|
|
201
|
213
|
_onDone(resp) {
|
202
|
|
- console.log('XMLHttpRequest done', resp.text())
|
|
214
|
+ log.verbose('XMLHttpRequest done', resp.text())
|
203
|
215
|
this.statusText = '200 OK'
|
204
|
216
|
this._status = 200
|
205
|
217
|
switch(resp.type) {
|
|
@@ -228,17 +240,17 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
|
228
|
240
|
}
|
229
|
241
|
|
230
|
242
|
set onreadystatechange(fn:() => void) {
|
231
|
|
- console.log('XMLHttpRequest set onreadystatechange', fn.toString())
|
|
243
|
+ log.verbose('XMLHttpRequest set onreadystatechange', fn.toString())
|
232
|
244
|
this._onreadystatechange = fn
|
233
|
245
|
}
|
234
|
246
|
|
235
|
247
|
set readyState(val:number) {
|
236
|
248
|
|
237
|
|
- console.log('XMLHttpRequest ready state changed to ', val)
|
|
249
|
+ log.verbose('XMLHttpRequest ready state changed to ', val)
|
238
|
250
|
this._readyState = val
|
239
|
251
|
if(this._onreadystatechange) {
|
240
|
|
- console.log('trigger onreadystatechange event', this._readyState)
|
241
|
|
- console.log(this._onreadystatechange)
|
|
252
|
+ log.verbose('trigger onreadystatechange event', this._readyState)
|
|
253
|
+ log.verbose(this._onreadystatechange)
|
242
|
254
|
this.dispatchEvent('readystatechange', )
|
243
|
255
|
if(this._onreadystatechange)
|
244
|
256
|
this._onreadystatechange()
|
|
@@ -246,12 +258,12 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
|
246
|
258
|
}
|
247
|
259
|
|
248
|
260
|
get readyState() {
|
249
|
|
- console.log('get readyState', this._readyState)
|
|
261
|
+ log.verbose('get readyState', this._readyState)
|
250
|
262
|
return this._readyState
|
251
|
263
|
}
|
252
|
264
|
|
253
|
265
|
get status() {
|
254
|
|
- console.log('get status', this._status)
|
|
266
|
+ log.verbose('get status', this._status)
|
255
|
267
|
return this._status
|
256
|
268
|
}
|
257
|
269
|
|
|
@@ -260,52 +272,52 @@ export default class XMLHttpRequest extends XMLHttpRequestEventTarget{
|
260
|
272
|
}
|
261
|
273
|
|
262
|
274
|
get statusText() {
|
263
|
|
- console.log('get statusText', this._statusText)
|
|
275
|
+ log.verbose('get statusText', this._statusText)
|
264
|
276
|
return this._statusText
|
265
|
277
|
}
|
266
|
278
|
|
267
|
279
|
set response(val) {
|
268
|
|
- console.log('set response', val)
|
|
280
|
+ log.verbose('set response', val)
|
269
|
281
|
this._response = val
|
270
|
282
|
}
|
271
|
283
|
|
272
|
284
|
get response() {
|
273
|
|
- console.log('get response', this._response)
|
|
285
|
+ log.verbose('get response', this._response)
|
274
|
286
|
return this._response
|
275
|
287
|
}
|
276
|
288
|
|
277
|
289
|
get responseText() {
|
278
|
|
- console.log('get responseText', this._responseText)
|
|
290
|
+ log.verbose('get responseText', this._responseText)
|
279
|
291
|
return this._responseText
|
280
|
292
|
}
|
281
|
293
|
|
282
|
294
|
get responseURL() {
|
283
|
|
- console.log('get responseURL', this._responseURL)
|
|
295
|
+ log.verbose('get responseURL', this._responseURL)
|
284
|
296
|
return this._responseURL
|
285
|
297
|
}
|
286
|
298
|
|
287
|
299
|
get responseHeaders() {
|
288
|
|
- console.log('get responseHeaders', this._responseHeaders)
|
|
300
|
+ log.verbose('get responseHeaders', this._responseHeaders)
|
289
|
301
|
return this._responseHeaders
|
290
|
302
|
}
|
291
|
303
|
|
292
|
304
|
set timeout(val) {
|
293
|
|
- console.log('set timeout', this._timeout)
|
|
305
|
+ log.verbose('set timeout', this._timeout)
|
294
|
306
|
this._timeout = val
|
295
|
307
|
}
|
296
|
308
|
|
297
|
309
|
get timeout() {
|
298
|
|
- console.log('get timeout', this._timeout)
|
|
310
|
+ log.verbose('get timeout', this._timeout)
|
299
|
311
|
return this._timeout
|
300
|
312
|
}
|
301
|
313
|
|
302
|
314
|
get upload() {
|
303
|
|
- console.log('get upload', this._upload)
|
|
315
|
+ log.verbose('get upload', this._upload)
|
304
|
316
|
return this._upload
|
305
|
317
|
}
|
306
|
318
|
|
307
|
319
|
get responseType() {
|
308
|
|
- console.log('get response type', this._responseType)
|
|
320
|
+ log.verbose('get response type', this._responseType)
|
309
|
321
|
return this._responseType
|
310
|
322
|
}
|
311
|
323
|
|