|
@@ -19,6 +19,7 @@ export default class XMLHttpRequest extends EventTarget{
|
19
|
19
|
readyState : number;
|
20
|
20
|
response : any;
|
21
|
21
|
responseText : any;
|
|
22
|
+ responseHeaders : any;
|
22
|
23
|
responseType : '' | 'arraybuffer' | 'blob' | 'document' | 'json' | 'text';
|
23
|
24
|
// TODO : not suppoted for now
|
24
|
25
|
responseURL : null;
|
|
@@ -38,21 +39,20 @@ export default class XMLHttpRequest extends EventTarget{
|
38
|
39
|
// `cancel` methods.
|
39
|
40
|
_task: any;
|
40
|
41
|
|
41
|
|
- constructor() {
|
|
42
|
+ constructor(...args) {
|
42
|
43
|
super()
|
43
|
|
- console.log('---------------------------------')
|
44
|
|
- console.log('XMLHttpRequest constructor called')
|
|
44
|
+ console.log('XMLHttpRequest constructor called', args)
|
45
|
45
|
this._config = {}
|
46
|
46
|
this._args = {}
|
47
|
47
|
this._headers = {}
|
48
|
48
|
this.readyState = 0
|
49
|
49
|
this.response = null
|
50
|
50
|
this.responseText = null
|
|
51
|
+ this.responseHeaders = null
|
51
|
52
|
}
|
52
|
53
|
|
53
|
54
|
// XMLHttpRequest.open, always async, user and password not supported.
|
54
|
55
|
open(method:string, url:string, async:true, user:any, password:any) {
|
55
|
|
- console.log('---------------------------------')
|
56
|
56
|
console.log('XMLHttpRequest open called', method, url, async, user, password)
|
57
|
57
|
this._method = method
|
58
|
58
|
this._url = url
|
|
@@ -61,10 +61,10 @@ export default class XMLHttpRequest extends EventTarget{
|
61
|
61
|
this.onload()
|
62
|
62
|
if(this.onloadstart)
|
63
|
63
|
this.onloadstart()
|
|
64
|
+ this._invokeOnStateChange()
|
64
|
65
|
}
|
65
|
66
|
|
66
|
67
|
addEventListener(event, listener) {
|
67
|
|
- console.log('---------------------------------')
|
68
|
68
|
console.log('XMLHttpRequest add listener', event, listener.toString())
|
69
|
69
|
this.addEventListener(event, listener)
|
70
|
70
|
}
|
|
@@ -74,13 +74,20 @@ export default class XMLHttpRequest extends EventTarget{
|
74
|
74
|
* @param {any} body Body in RNfetchblob flavor
|
75
|
75
|
*/
|
76
|
76
|
send(body) {
|
77
|
|
- console.log('---------------------------------')
|
78
|
77
|
console.log('XMLHttpRequest send called', body)
|
79
|
|
- let [_method, _url, _headers] = this
|
|
78
|
+ let {_method, _url, _headers } = this
|
80
|
79
|
console.log('sending request with args', _method, _url, _headers, body)
|
81
|
80
|
|
82
|
81
|
this._task = RNFetchBlob.fetch(_method, _url, _headers, body)
|
83
|
82
|
this._task
|
|
83
|
+ .stateChange((e) => {
|
|
84
|
+ console.log('state change')
|
|
85
|
+ if(e.state === "2") {
|
|
86
|
+ this.readyState = 2
|
|
87
|
+ this.responseHeaders = e.headers
|
|
88
|
+ }
|
|
89
|
+ this._invokeOnStateChange()
|
|
90
|
+ })
|
84
|
91
|
.uploadProgress(this._progressEvent)
|
85
|
92
|
.onProgress(this._progressEvent)
|
86
|
93
|
.then(this._onDone)
|
|
@@ -97,8 +104,9 @@ export default class XMLHttpRequest extends EventTarget{
|
97
|
104
|
}
|
98
|
105
|
|
99
|
106
|
abort() {
|
100
|
|
- console.log('---------------------------------')
|
101
|
107
|
console.log('XMLHttpRequest abort called', this._task)
|
|
108
|
+ if(!this._task)
|
|
109
|
+ return
|
102
|
110
|
this._task.cancel((err) => {
|
103
|
111
|
let e = {
|
104
|
112
|
timeStamp : Date.now(),
|
|
@@ -117,16 +125,24 @@ export default class XMLHttpRequest extends EventTarget{
|
117
|
125
|
})
|
118
|
126
|
}
|
119
|
127
|
|
120
|
|
- getResponseHeader(field:string):string | null{
|
|
128
|
+ getResponseHeader(field:string):string | null {
|
|
129
|
+
|
|
130
|
+ if(!this.responseHeaders)
|
|
131
|
+ return null
|
|
132
|
+ return this.responseHeaders[field] || null
|
121
|
133
|
|
122
|
134
|
}
|
123
|
135
|
|
124
|
136
|
getAllResponseHeaders():string | null {
|
125
|
137
|
|
126
|
|
- }
|
127
|
|
-
|
128
|
|
- set onreadystatechange(handler:() => void) {
|
129
|
|
- this.onreadystatechange = handler
|
|
138
|
+ if(!this.responseHeaders)
|
|
139
|
+ return null
|
|
140
|
+ let result = ''
|
|
141
|
+ let respHeaders = this.responseHeaders
|
|
142
|
+ for(let i in respHeaders) {
|
|
143
|
+ result += `${i}:${respHeaders[i]}\r\n`
|
|
144
|
+ }
|
|
145
|
+ return result
|
130
|
146
|
}
|
131
|
147
|
|
132
|
148
|
_progressEvent(send:number, total:number) {
|
|
@@ -143,8 +159,11 @@ export default class XMLHttpRequest extends EventTarget{
|
143
|
159
|
}
|
144
|
160
|
|
145
|
161
|
_onError(err) {
|
|
162
|
+ console.log('XMLHttpRequest error', err)
|
146
|
163
|
this.statusText = err
|
147
|
|
-
|
|
164
|
+ this.status = String(err).match(/\d+/)
|
|
165
|
+ this.status = this.status ? Math.floor(this.status) : 404
|
|
166
|
+ this.readyState = 4
|
148
|
167
|
if(String(err).match('timeout') !== null) {
|
149
|
168
|
if(this.ontimeout)
|
150
|
169
|
this.ontimeout()
|
|
@@ -155,10 +174,13 @@ export default class XMLHttpRequest extends EventTarget{
|
155
|
174
|
detail : err
|
156
|
175
|
})
|
157
|
176
|
}
|
|
177
|
+ this._invokeOnStateChange()
|
158
|
178
|
}
|
159
|
179
|
|
160
|
180
|
_onDone(resp) {
|
|
181
|
+ console.log('XMLHttpRequest done', resp.text())
|
161
|
182
|
this.statusText = '200 OK'
|
|
183
|
+ this.status = 200
|
162
|
184
|
switch(resp.type) {
|
163
|
185
|
case 'base64' :
|
164
|
186
|
this.responseType = 'text'
|
|
@@ -170,6 +192,13 @@ export default class XMLHttpRequest extends EventTarget{
|
170
|
192
|
this.response = resp.blob()
|
171
|
193
|
break;
|
172
|
194
|
}
|
|
195
|
+ this.readyState = 4
|
|
196
|
+ this._invokeOnStateChange()
|
|
197
|
+ }
|
|
198
|
+
|
|
199
|
+ _invokeOnStateChange() {
|
|
200
|
+ if(this.onreadystatechange)
|
|
201
|
+ this.onreadystatechange()
|
173
|
202
|
}
|
174
|
203
|
|
175
|
204
|
}
|