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,6 +344,7 @@ class FetchBlobResponse {
344 344
     this.readFile = (encode: 'base64' | 'utf8' | 'ascii') => {
345 345
       if(this.type === 'path') {
346 346
         encode = encode || 'utf8'
347
+
347 348
         return readFile(this.data, encode)
348 349
       }
349 350
       else {

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

@@ -11,7 +11,8 @@ import EventTarget from './EventTarget'
11 11
 const log = new Log('Blob')
12 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 18
  * A RNFetchBlob style Blob polyfill class, this is a Blob which compatible to
@@ -45,6 +46,10 @@ export default class Blob extends EventTarget {
45 46
     })
46 47
   }
47 48
 
49
+  get blobPath() {
50
+    return this._ref
51
+  }
52
+
48 53
   /**
49 54
    * RNFetchBlob Blob polyfill, create a Blob directly from file path, BASE64
50 55
    * encoded data, and string. The conversion is done implicitly according to
@@ -131,7 +136,6 @@ export default class Blob extends EventTarget {
131 136
       log.verbose('create Blob cache file from string', 'encode', encoding)
132 137
       p = fs.writeFile(this._ref, data, encoding)
133 138
             .then((size) => {
134
-              console.log('file bytes', size)
135 139
               return Promise.resolve(size)
136 140
             })
137 141
 

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

@@ -6,8 +6,8 @@ import Blob from './Blob'
6 6
 
7 7
 const log = new Log('FetchPolyfill')
8 8
 
9
-// log.level(3)
10 9
 log.disable()
10
+// log.level(3)
11 11
 
12 12
 export default class Fetch {
13 13
 
@@ -23,27 +23,34 @@ class RNFetchBlobFetchPolyfill {
23 23
     this.build = () => (url, options = {}) => {
24 24
 
25 25
       let body = options.body
26
-      let promise = null
26
+      let promise = Promise.resolve()
27 27
       let blobCache = null
28 28
 
29 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 55
       // task is a progress reportable and cancellable Promise, however,
49 56
       // task.then is not, so we have to extend task.then with progress and
@@ -51,7 +58,7 @@ class RNFetchBlobFetchPolyfill {
51 58
       let task = promise
52 59
           .then((body) => {
53 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 64
       let statefulPromise = task.then((resp) => {
@@ -59,7 +66,6 @@ class RNFetchBlobFetchPolyfill {
59 66
         // release blob cache created when sending request
60 67
         if(blobCache !== null && blobCache instanceof Blob)
61 68
           blobCache.close()
62
-        let info = resp.info()
63 69
         return Promise.resolve(new RNFetchBlobFetchRepsonse(resp))
64 70
       })
65 71
 
@@ -128,7 +134,6 @@ function readText(resp, info):Promise<string> {
128 134
       break
129 135
     case 'path':
130 136
       return resp.readFile('utf8').then((data) => {
131
-        data = unicode(data)
132 137
         return Promise.resolve(data)
133 138
       })
134 139
       break