Browse Source

Fix Fetch replacement and Blob construction logic

Ben Hsieh 8 years ago
parent
commit
182b0d3973
3 changed files with 33 additions and 23 deletions
  1. 1
    0
      src/index.js
  2. 6
    2
      src/polyfill/Blob.js
  3. 26
    21
      src/polyfill/Fetch.js

+ 1
- 0
src/index.js View File

344
     this.readFile = (encode: 'base64' | 'utf8' | 'ascii') => {
344
     this.readFile = (encode: 'base64' | 'utf8' | 'ascii') => {
345
       if(this.type === 'path') {
345
       if(this.type === 'path') {
346
         encode = encode || 'utf8'
346
         encode = encode || 'utf8'
347
+
347
         return readFile(this.data, encode)
348
         return readFile(this.data, encode)
348
       }
349
       }
349
       else {
350
       else {

+ 6
- 2
src/polyfill/Blob.js View File

11
 const log = new Log('Blob')
11
 const log = new Log('Blob')
12
 const blobCacheDir = fs.dirs.DocumentDir + '/RNFetchBlob-blobs/'
12
 const blobCacheDir = fs.dirs.DocumentDir + '/RNFetchBlob-blobs/'
13
 
13
 
14
-log.level(3)
14
+log.disable()
15
+// log.level(3)
15
 
16
 
16
 /**
17
 /**
17
  * A RNFetchBlob style Blob polyfill class, this is a Blob which compatible to
18
  * A RNFetchBlob style Blob polyfill class, this is a Blob which compatible to
45
     })
46
     })
46
   }
47
   }
47
 
48
 
49
+  get blobPath() {
50
+    return this._ref
51
+  }
52
+
48
   /**
53
   /**
49
    * RNFetchBlob Blob polyfill, create a Blob directly from file path, BASE64
54
    * RNFetchBlob Blob polyfill, create a Blob directly from file path, BASE64
50
    * encoded data, and string. The conversion is done implicitly according to
55
    * encoded data, and string. The conversion is done implicitly according to
131
       log.verbose('create Blob cache file from string', 'encode', encoding)
136
       log.verbose('create Blob cache file from string', 'encode', encoding)
132
       p = fs.writeFile(this._ref, data, encoding)
137
       p = fs.writeFile(this._ref, data, encoding)
133
             .then((size) => {
138
             .then((size) => {
134
-              console.log('file bytes', size)
135
               return Promise.resolve(size)
139
               return Promise.resolve(size)
136
             })
140
             })
137
 
141
 

+ 26
- 21
src/polyfill/Fetch.js View File

6
 
6
 
7
 const log = new Log('FetchPolyfill')
7
 const log = new Log('FetchPolyfill')
8
 
8
 
9
-// log.level(3)
10
 log.disable()
9
 log.disable()
10
+// log.level(3)
11
 
11
 
12
 export default class Fetch {
12
 export default class Fetch {
13
 
13
 
23
     this.build = () => (url, options = {}) => {
23
     this.build = () => (url, options = {}) => {
24
 
24
 
25
       let body = options.body
25
       let body = options.body
26
-      let promise = null
26
+      let promise = Promise.resolve()
27
       let blobCache = null
27
       let blobCache = null
28
 
28
 
29
       options.headers = options.headers || {}
29
       options.headers = options.headers || {}
30
-      options['Content-Type'] = options.headers['Content-Type'] || options.headers['content-type']
31
-      options['content-type'] = options.headers['Content-Type'] || options.headers['content-type']
32
-
33
-      // When the request body is an instance of FormData, create a Blob cache
34
-      // to upload the body.
35
-      if(body instanceof FormData) {
36
-        promise = Blob.build(body).then((b) => {
37
-          blobCache = b
38
-          return Promise.resolve(b.getRNFetchBlobRef())
39
-        })
30
+      let ctype = options['Content-Type'] || options['content-type']
31
+      let ctypeH = options.headers['Content-Type'] || options.headers['content-type']
32
+      options.headers['Content-Type'] = ctype || ctypeH
33
+      options.headers['content-type'] = ctype || ctypeH
34
+      options.method = options.method || 'GET'
35
+
36
+      if(body) {
37
+        // When the request body is an instance of FormData, create a Blob cache
38
+        // to upload the body.
39
+        if(body instanceof FormData) {
40
+          log.verbose('convert FormData to blob body')
41
+          promise = Blob.build(body).then((b) => {
42
+            blobCache = b
43
+            options.headers['Content-Type'] = 'multipart/form-data;boundary=' + b.multipartBoundary
44
+            return Promise.resolve(RNFetchBlob.wrap(b._ref))
45
+          })
46
+        }
47
+        // When request body is a Blob, use file URI of the Blob as request body.
48
+        else if (body.isRNFetchBlobPolyfill)
49
+          promise = Promise.resolve(RNFetchBlob.wrap(body.blobPath))
50
+        // send it as-is, leave the native module decide how to send the body.
51
+        else
52
+          promise = Promise.resolve(body)
40
       }
53
       }
41
-      // When request body is a Blob, use file URI of the Blob as request body.
42
-      else if (body instanceof Blob)
43
-        promise = Promise.resolve(RNFetchBlob.wrap(body.getRNFetchBlobRef()))
44
-      // send it as-is, leave the native module decide how to send the body.
45
-      else
46
-        promise = Promise.resolve(body)
47
 
54
 
48
       // task is a progress reportable and cancellable Promise, however,
55
       // task is a progress reportable and cancellable Promise, however,
49
       // task.then is not, so we have to extend task.then with progress and
56
       // task.then is not, so we have to extend task.then with progress and
51
       let task = promise
58
       let task = promise
52
           .then((body) => {
59
           .then((body) => {
53
             return RNFetchBlob.config(config)
60
             return RNFetchBlob.config(config)
54
-            .fetch(options.method, url, options.headers, options.body)
61
+            .fetch(options.method, url, options.headers, body)
55
           })
62
           })
56
 
63
 
57
       let statefulPromise = task.then((resp) => {
64
       let statefulPromise = task.then((resp) => {
59
         // release blob cache created when sending request
66
         // release blob cache created when sending request
60
         if(blobCache !== null && blobCache instanceof Blob)
67
         if(blobCache !== null && blobCache instanceof Blob)
61
           blobCache.close()
68
           blobCache.close()
62
-        let info = resp.info()
63
         return Promise.resolve(new RNFetchBlobFetchRepsonse(resp))
69
         return Promise.resolve(new RNFetchBlobFetchRepsonse(resp))
64
       })
70
       })
65
 
71
 
128
       break
134
       break
129
     case 'path':
135
     case 'path':
130
       return resp.readFile('utf8').then((data) => {
136
       return resp.readFile('utf8').then((data) => {
131
-        data = unicode(data)
132
         return Promise.resolve(data)
137
         return Promise.resolve(data)
133
       })
138
       })
134
       break
139
       break