Browse Source

#44 wip commit

Ben Hsieh 8 years ago
parent
commit
43563cdff9

+ 6
- 0
src/index.js View File

134
       }
134
       }
135
     })
135
     })
136
 
136
 
137
+    // When the request body comes from Blob polyfill, we should use special its ref
138
+    // as the request body
139
+    if( body instanceof Blob && body.isRNFetchBlobPolyfill) {
140
+      body = body.getRNFetchBlobRef()
141
+    }
142
+
137
     let req = RNFetchBlob[nativeMethodName]
143
     let req = RNFetchBlob[nativeMethodName]
138
     req(options, taskId, method, url, headers || {}, body, (err, data) => {
144
     req(options, taskId, method, url, headers || {}, body, (err, data) => {
139
 
145
 

+ 10
- 10
src/ios/RNFetchBlob/RNFetchBlob.m View File

80
                   headers:(NSDictionary *)headers
80
                   headers:(NSDictionary *)headers
81
                   body:(NSString *)body callback:(RCTResponseSenderBlock)callback)
81
                   body:(NSString *)body callback:(RCTResponseSenderBlock)callback)
82
 {
82
 {
83
-	NSString *cType = [headers valueForKey:"content-type"]
84
-	if (cType != nil && cType == @"application/x-www-form-urlencoded") {
85
-		[RNFetchBlobReqBuilder buildEncodedRequest:options taskId:taskId method:method url:url headers:headers body:body onComplete:^(NSURLRequest *req, long bodyLength) {
86
-	        // send HTTP request
87
-	        RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
88
-	        [utils sendRequest:options contentLength:bodyLength bridge:self.bridge taskId:taskId withRequest:req callback:callback];
89
-	        utils = nil;
90
-	    }];
91
-	} else {
83
+    NSString *cType = [headers valueForKey:@"content-type"];
84
+//	if (cType != nil && cType == @"application/x-www-form-urlencoded") {
85
+//		[RNFetchBlobReqBuilder buildEncodedRequest:options taskId:taskId method:method url:url headers:headers body:body onComplete:^(NSURLRequest *req, long bodyLength) {
86
+//	        // send HTTP request
87
+//	        RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
88
+//	        [utils sendRequest:options contentLength:bodyLength bridge:self.bridge taskId:taskId withRequest:req callback:callback];
89
+//	        utils = nil;
90
+//	    }];
91
+//	} else {
92
 		[RNFetchBlobReqBuilder buildOctetRequest:options taskId:taskId method:method url:url headers:headers body:body onComplete:^(NSURLRequest *req, long bodyLength) {
92
 		[RNFetchBlobReqBuilder buildOctetRequest:options taskId:taskId method:method url:url headers:headers body:body onComplete:^(NSURLRequest *req, long bodyLength) {
93
 	        // send HTTP request
93
 	        // send HTTP request
94
 	        RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
94
 	        RNFetchBlobNetwork * utils = [[RNFetchBlobNetwork alloc] init];
95
 	        [utils sendRequest:options contentLength:bodyLength bridge:self.bridge taskId:taskId withRequest:req callback:callback];
95
 	        [utils sendRequest:options contentLength:bodyLength bridge:self.bridge taskId:taskId withRequest:req callback:callback];
96
 	        utils = nil;
96
 	        utils = nil;
97
 	    }];
97
 	    }];
98
-	}
98
+//	}
99
 }
99
 }
100
 
100
 
101
 RCT_EXPORT_METHOD(createFile:(NSString *)path data:(NSString *)data encoding:(NSString *)encoding callback:(RCTResponseSenderBlock)callback) {
101
 RCT_EXPORT_METHOD(createFile:(NSString *)path data:(NSString *)data encoding:(NSString *)encoding callback:(RCTResponseSenderBlock)callback) {

+ 1
- 1
src/ios/RNFetchBlobConst.m View File

19
 extern NSString *const CONFIG_FILE_EXT = @"appendExt";
19
 extern NSString *const CONFIG_FILE_EXT = @"appendExt";
20
 extern NSString *const CONFIG_TRUSTY = @"trusty";
20
 extern NSString *const CONFIG_TRUSTY = @"trusty";
21
 extern NSString *const CONFIG_INDICATOR = @"indicator";
21
 extern NSString *const CONFIG_INDICATOR = @"indicator";
22
-extern NSString *const CONFIG_KEY = "@key";
22
+extern NSString *const CONFIG_KEY = @"key";
23
 
23
 
24
 extern NSString *const EVENT_STATE_CHANGE = @"RNFetchBlobState";
24
 extern NSString *const EVENT_STATE_CHANGE = @"RNFetchBlobState";
25
 extern NSString *const MSG_EVENT = @"RNFetchBlobMessage";
25
 extern NSString *const MSG_EVENT = @"RNFetchBlobMessage";

+ 4
- 4
src/ios/RNFetchBlobNetwork.m View File

74
     return mheaders;
74
     return mheaders;
75
 }
75
 }
76
 
76
 
77
-- (NSString *)md5 {
78
-    const char* str = [self UTF8String];
77
+- (NSString *)md5:(NSString *)input {
78
+    const char* str = [input UTF8String];
79
     unsigned char result[CC_MD5_DIGEST_LENGTH];
79
     unsigned char result[CC_MD5_DIGEST_LENGTH];
80
     CC_MD5(str, (CC_LONG)strlen(str), result);
80
     CC_MD5(str, (CC_LONG)strlen(str), result);
81
 
81
 
120
 
120
 
121
 		NSString* cacheKey = taskId;
121
 		NSString* cacheKey = taskId;
122
 		if (key != nil) {
122
 		if (key != nil) {
123
-			cacheKey = [key md5];
123
+            cacheKey = [self md5:key];
124
 			if (cacheKey == nil) {
124
 			if (cacheKey == nil) {
125
 				cacheKey = taskId;
125
 				cacheKey = taskId;
126
 			}
126
 			}
127
 
127
 
128
 			destPath = [RNFetchBlobFS getTempPath:cacheKey withExtension:[self.options valueForKey:CONFIG_FILE_EXT]];
128
 			destPath = [RNFetchBlobFS getTempPath:cacheKey withExtension:[self.options valueForKey:CONFIG_FILE_EXT]];
129
             if ([[NSFileManager defaultManager] fileExistsAtPath:destPath]) {
129
             if ([[NSFileManager defaultManager] fileExistsAtPath:destPath]) {
130
-				callback([NSNull null], destPath]);
130
+				callback(@[[NSNull null], destPath]);
131
                 return;
131
                 return;
132
             }
132
             }
133
 		}
133
 		}

+ 13
- 12
src/ios/RNFetchBlobReqBuilder.m View File

138
     });
138
     });
139
 }
139
 }
140
 
140
 
141
-+(void) buildEncodedRequest:(NSDictionary *)options
142
-                   taskId:(NSString *)taskId
143
-                   method:(NSString *)method
144
-                      url:(NSString *)url
145
-                  headers:(NSDictionary *)headers
146
-                     body:(NSString *)body
147
-               onComplete:(void(^)(NSURLRequest * req, long bodyLength))onComplete
148
-{
149
-	NSMutableData * formData = [[NSMutableData alloc] init];
150
-	[formData appendData:[[NSString stringWithFormat:@"%@", body] dataUsingEncoding:NSUTF8StringEncoding]];
151
-	onComplete(formData);
152
-}
141
+//+(void) buildEncodedRequest:(NSDictionary *)options
142
+//                   taskId:(NSString *)taskId
143
+//                   method:(NSString *)method
144
+//                      url:(NSString *)url
145
+//                  headers:(NSDictionary *)headers
146
+//                     body:(NSString *)body
147
+//               onComplete:(void(^)(NSURLRequest * req, long bodyLength))onComplete
148
+//{
149
+//	NSMutableData * formData = [[NSMutableData alloc] init];
150
+//    
151
+//	[formData appendData:[[NSString stringWithFormat:@"%@", body] dataUsingEncoding:NSUTF8StringEncoding]];
152
+//	onComplete(formData);
153
+//}
153
 
154
 
154
 
155
 
155
 +(void) buildFormBody:(NSArray *)form boundary:(NSString *)boundary onComplete:(void(^)(NSData * formData))onComplete
156
 +(void) buildFormBody:(NSArray *)form boundary:(NSString *)boundary onComplete:(void(^)(NSData * formData))onComplete

+ 50
- 13
src/polyfill/Blob.js View File

5
 import RNFetchBlob from '../index.js'
5
 import RNFetchBlob from '../index.js'
6
 import fs from '../fs.js'
6
 import fs from '../fs.js'
7
 import getUUID from '../utils/uuid'
7
 import getUUID from '../utils/uuid'
8
+import Log from '../utils/log.js'
8
 
9
 
10
+const log = new Log('Blob')
9
 const blobCacheDir = fs.dirs.DocumentDir + '/RNFetchBlob-blob/'
11
 const blobCacheDir = fs.dirs.DocumentDir + '/RNFetchBlob-blob/'
10
 
12
 
13
+log.level(3)
14
+
11
 /**
15
 /**
12
  * A RNFetchBlob style Blob polyfill class, this is a Blob which compatible to
16
  * A RNFetchBlob style Blob polyfill class, this is a Blob which compatible to
13
  * Response object attain fron RNFetchBlob.fetch.
17
  * Response object attain fron RNFetchBlob.fetch.
17
   cacheName:string;
21
   cacheName:string;
18
   type:string;
22
   type:string;
19
   size:number;
23
   size:number;
24
+  isRNFetchBlobPolyfill:boolean = true;
20
 
25
 
21
   _ref:string = null;
26
   _ref:string = null;
22
   _blobCreated:boolean = false;
27
   _blobCreated:boolean = false;
26
 
31
 
27
   // legacy constructor
32
   // legacy constructor
28
   constructor(data:any, mime:?string) {
33
   constructor(data:any, mime:?string) {
34
+
29
     this.cacheName = getBlobName()
35
     this.cacheName = getBlobName()
36
+    this.isRNFetchBlobPolyfill = true
30
     this.type = mime
37
     this.type = mime
38
+    log.verbose('Blob constructor called' , data, 'mime', mime)
39
+
31
     if(typeof data === 'string') {
40
     if(typeof data === 'string') {
32
       // content from file
41
       // content from file
33
       if(data.startsWith('RNFetchBlob-file://')) {
42
       if(data.startsWith('RNFetchBlob-file://')) {
36
       }
45
       }
37
       // content from variable need create file
46
       // content from variable need create file
38
       else {
47
       else {
48
+        log.verbose('create Blob cache file ..')
39
         this._ref = RNFetchBlob.wrap(blobCacheDir + this.cacheName)
49
         this._ref = RNFetchBlob.wrap(blobCacheDir + this.cacheName)
40
         let encoding = 'utf8'
50
         let encoding = 'utf8'
41
         if(typeof data === 'string' && String(mime).match('application/octet') )
51
         if(typeof data === 'string' && String(mime).match('application/octet') )
45
 
55
 
46
         this.init(data, encoding)
56
         this.init(data, encoding)
47
           .then(() => {
57
           .then(() => {
58
+            log.verbose('init executed ')
48
             if(typeof this._onCreated === 'function')
59
             if(typeof this._onCreated === 'function')
49
-              this._onCreated()
50
-            _blobCreated = true
60
+              this._onCreated(this)
51
           })
61
           })
52
           .catch((err) => {
62
           .catch((err) => {
53
-            console.log('RNFetchBlob cannot create Blob', err)
63
+            log.error('RNFetchBlob cannot create Blob', err)
54
           })
64
           })
55
       }
65
       }
66
+    }
67
+    // TODO : handle mixed blob array
68
+    else if(Array.isArray(data)) {
69
+
56
     }
70
     }
57
     else {
71
     else {
58
-      console.log('TODO')
72
+      log.verbose('TODO: else')
59
     }
73
     }
60
   }
74
   }
61
 
75
 
62
   onCreated(fn:() => void) {
76
   onCreated(fn:() => void) {
63
-    console.log('register blob onCreated', fn)
77
+    log.verbose('register blob onCreated')
64
     if(this._blobCreated)
78
     if(this._blobCreated)
65
       fn()
79
       fn()
66
     else
80
     else
75
    * @return {Promise}
89
    * @return {Promise}
76
    */
90
    */
77
   init(data, encoding):Promise {
91
   init(data, encoding):Promise {
78
-    console.log('blob init called')
79
-    return fs.exists(blobCacheDir).then((exist) => {
80
-      if(!exist)
81
-        return fs.mkdir(blobCacheDir).then(() => fs.createFile(this._ref, data, encoding))
82
-      else
83
-        return fs.createFile(this._ref, data, encoding)
92
+    return new Promise((resolve, reject) => {
93
+      fs.exists(blobCacheDir)
94
+        .then((exist) => {
95
+          log.verbose('blob cache folder exist', blobCacheDir, exist)
96
+          let path = String(this._ref).replace('RNFetchBlob-file://', '')
97
+          log.verbose('create cache file', path)
98
+          if(!exist)
99
+            return fs.mkdir(blobCacheDir)
100
+                     .then(() => fs.createFile(path, data, encoding))
101
+          else
102
+            return fs.createFile(path, data, encoding)
103
+        })
104
+        .then(() => {
105
+          this._blobCreated = true
106
+          resolve()
107
+        })
108
+        .catch((err) => {
109
+          reject(err)
110
+        })
84
     })
111
     })
112
+
85
   }
113
   }
86
 
114
 
87
   /**
115
   /**
100
    * @return {Blob}
128
    * @return {Blob}
101
    */
129
    */
102
   slice(start:?number, end:?number, encoding:?string):Blob {
130
   slice(start:?number, end:?number, encoding:?string):Blob {
103
-    console.log('slice called')
131
+    log.verbose('slice called')
104
     // return fs.slice(this.cacheName, getBlobName(), contentType, start, end)
132
     // return fs.slice(this.cacheName, getBlobName(), contentType, start, end)
105
   }
133
   }
106
 
134
 
113
     return fs.unlink(this._ref)
141
     return fs.unlink(this._ref)
114
   }
142
   }
115
 
143
 
116
-
117
 }
144
 }
118
 
145
 
119
 /**
146
 /**
123
 function getBlobName() {
150
 function getBlobName() {
124
   return 'blob-' + getUUID()
151
   return 'blob-' + getUUID()
125
 }
152
 }
153
+
154
+/**
155
+ * Create a file according to given array. The element in array can be a number,
156
+ * Blob, string.
157
+ * @param  {Array} dataArray An array contains different types of data.
158
+ * @return {string}      The blob file reference
159
+ */
160
+function createMixedBlobData(dataArray) {
161
+  // TODO : mixed blob data creator
162
+}

+ 7
- 0
src/polyfill/FormData.js View File

1
+export default class FormData {
2
+
3
+  constructor() {
4
+    console.log('FormData constructor called')
5
+  }
6
+
7
+}

+ 41
- 29
src/polyfill/XMLHttpRequest.js View File

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

+ 2
- 0
src/polyfill/XMLHttpRequestEventTarget.js View File

6
 
6
 
7
 const log = new Log('XMLHttpRequestEventTarget')
7
 const log = new Log('XMLHttpRequestEventTarget')
8
 
8
 
9
+log.disable()
10
+
9
 export default class XMLHttpRequestEventTarget extends EventTarget {
11
 export default class XMLHttpRequestEventTarget extends EventTarget {
10
 
12
 
11
   _onabort : (e:Event) => void;
13
   _onabort : (e:Event) => void;

+ 2
- 1
src/polyfill/index.js View File

1
 import Blob from './Blob.js'
1
 import Blob from './Blob.js'
2
 import File from './File.js'
2
 import File from './File.js'
3
 import XMLHttpRequest from './XMLHttpRequest.js'
3
 import XMLHttpRequest from './XMLHttpRequest.js'
4
+import FormData from './FormData.js'
4
 
5
 
5
 export default {
6
 export default {
6
-  Blob, File, XMLHttpRequest
7
+  Blob, File, XMLHttpRequest, FormData
7
 }
8
 }

+ 22
- 4
src/utils/log.js View File

1
 export default class Log {
1
 export default class Log {
2
 
2
 
3
   _name:string;
3
   _name:string;
4
+  _isEnable:boolean = true
5
+  _level:number = 0
4
 
6
 
5
   constructor(name:string) {
7
   constructor(name:string) {
6
     this._name = name
8
     this._name = name
7
   }
9
   }
8
 
10
 
9
-  info(...args) {
10
-    console.log(this._name, '-info:', ...args)
11
+  level(val:number) {
12
+    this._level = val
13
+  }
14
+
15
+  enable() {
16
+    this._isEnable = true
17
+  }
18
+
19
+  disable() {
20
+    this._isEnable = false
21
+  }
22
+
23
+  verbose(...args) {
24
+    this._isEnable && this._level > 2 && console.log(this._name, 'verbose:', ...args)
11
   }
25
   }
12
 
26
 
13
   debug(...args) {
27
   debug(...args) {
14
-    console.log(this._name, '-debug:', ...args)
28
+    this._isEnable && this._level > 1 && console.log(this._name, 'debug:', ...args)
29
+  }
30
+
31
+  info(...args) {
32
+    this._isEnable && this._level > 0 && console.log(this._name, 'info:', ...args)
15
   }
33
   }
16
 
34
 
17
   error(...args) {
35
   error(...args) {
18
-    console.log(this._name, '-error:', ...args)
36
+    this._isEnable && this._level > -1 && console.log(this._name, 'error:', ...args)
19
   }
37
   }
20
 
38
 
21
 }
39
 }

+ 1
- 2
test/test-firebase.js View File

20
 
20
 
21
 window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest
21
 window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest
22
 window.Blob = Blob
22
 window.Blob = Blob
23
+window.FormData = RNFetchBlob.polyfill.FormData
23
 
24
 
24
 const { Assert, Comparer, Info, prop } = RNTest
25
 const { Assert, Comparer, Info, prop } = RNTest
25
 const describe = RNTest.config({
26
 const describe = RNTest.config({
61
   })
62
   })
62
 })
63
 })
63
 
64
 
64
-
65
 describe('upload file to firebase', (report, done) => {
65
 describe('upload file to firebase', (report, done) => {
66
 
66
 
67
   try {
67
   try {
68
     let blob = new Blob(RNTest.prop('image'), 'application/octet-binary')
68
     let blob = new Blob(RNTest.prop('image'), 'application/octet-binary')
69
     blob.onCreated(() => {
69
     blob.onCreated(() => {
70
-      console.log('BINGO')
71
       let storage = firebase.storage().ref()
70
       let storage = firebase.storage().ref()
72
       let task = storage
71
       let task = storage
73
         .child(`testdata/firebase-test-${Platform.OS}.png`)
72
         .child(`testdata/firebase-test-${Platform.OS}.png`)

+ 2
- 2
test/test-init.js View File

18
 // test environment variables
18
 // test environment variables
19
 
19
 
20
 prop('FILENAME', `${Platform.OS}-0.7.0-${Date.now()}.png`)
20
 prop('FILENAME', `${Platform.OS}-0.7.0-${Date.now()}.png`)
21
-prop('TEST_SERVER_URL', 'http://192.168.16.70:8123')
22
-prop('TEST_SERVER_URL_SSL', 'https://192.168.16.70:8124')
21
+prop('TEST_SERVER_URL', 'http://192.168.0.11:8123')
22
+prop('TEST_SERVER_URL_SSL', 'https://192.168.0.11:8124')
23
 prop('DROPBOX_TOKEN', 'fsXcpmKPrHgAAAAAAAAAoXZhcXYWdgLpQMan6Tb_bzJ237DXhgQSev12hA-gUXt4')
23
 prop('DROPBOX_TOKEN', 'fsXcpmKPrHgAAAAAAAAAoXZhcXYWdgLpQMan6Tb_bzJ237DXhgQSev12hA-gUXt4')
24
 prop('styles', {
24
 prop('styles', {
25
   image : {
25
   image : {