Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit 182b0d3

Browse files
committed
Fix Fetch replacement and Blob construction logic
1 parent 5e5a461 commit 182b0d3

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ class FetchBlobResponse {
344344
this.readFile = (encode: 'base64' | 'utf8' | 'ascii') => {
345345
if(this.type === 'path') {
346346
encode = encode || 'utf8'
347+
347348
return readFile(this.data, encode)
348349
}
349350
else {

src/polyfill/Blob.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import EventTarget from './EventTarget'
1111
const log = new Log('Blob')
1212
const blobCacheDir = fs.dirs.DocumentDir + '/RNFetchBlob-blobs/'
1313

14-
log.level(3)
14+
log.disable()
15+
// log.level(3)
1516

1617
/**
1718
* A RNFetchBlob style Blob polyfill class, this is a Blob which compatible to
@@ -45,6 +46,10 @@ export default class Blob extends EventTarget {
4546
})
4647
}
4748

49+
get blobPath() {
50+
return this._ref
51+
}
52+
4853
/**
4954
* RNFetchBlob Blob polyfill, create a Blob directly from file path, BASE64
5055
* encoded data, and string. The conversion is done implicitly according to
@@ -131,7 +136,6 @@ export default class Blob extends EventTarget {
131136
log.verbose('create Blob cache file from string', 'encode', encoding)
132137
p = fs.writeFile(this._ref, data, encoding)
133138
.then((size) => {
134-
console.log('file bytes', size)
135139
return Promise.resolve(size)
136140
})
137141

src/polyfill/Fetch.js

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import Blob from './Blob'
66

77
const log = new Log('FetchPolyfill')
88

9-
// log.level(3)
109
log.disable()
10+
// log.level(3)
1111

1212
export default class Fetch {
1313

@@ -23,43 +23,49 @@ class RNFetchBlobFetchPolyfill {
2323
this.build = () => (url, options = {}) => {
2424

2525
let body = options.body
26-
let promise = null
26+
let promise = Promise.resolve()
2727
let blobCache = null
2828

2929
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)
4053
}
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)
4754

4855
// task is a progress reportable and cancellable Promise, however,
4956
// task.then is not, so we have to extend task.then with progress and
5057
// cancel function
5158
let task = promise
5259
.then((body) => {
5360
return RNFetchBlob.config(config)
54-
.fetch(options.method, url, options.headers, options.body)
61+
.fetch(options.method, url, options.headers, body)
5562
})
5663

5764
let statefulPromise = task.then((resp) => {
5865
log.verbose('response', resp)
5966
// release blob cache created when sending request
6067
if(blobCache !== null && blobCache instanceof Blob)
6168
blobCache.close()
62-
let info = resp.info()
6369
return Promise.resolve(new RNFetchBlobFetchRepsonse(resp))
6470
})
6571

@@ -128,7 +134,6 @@ function readText(resp, info):Promise<string> {
128134
break
129135
case 'path':
130136
return resp.readFile('utf8').then((data) => {
131-
data = unicode(data)
132137
return Promise.resolve(data)
133138
})
134139
break

0 commit comments

Comments
 (0)