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

Commit b54f0f5

Browse files
committed
Fix #5 Fix #6 replace atob with base-64 library.
1 parent 1f3b204 commit b54f0f5

File tree

4 files changed

+24
-41
lines changed

4 files changed

+24
-41
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
node_modules/
2+
13
# Xcode
24
#
35
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ If you're dealing with image or file server that requires an `Authorization` tok
66

77
See [[fetch] Does fetch with blob() marshal data across the bridge?]([fetch] Does fetch with blob() marshal data across the bridge?).
88

9-
This module enables you upload/download binary data in js, see [Examples](#user-content-usage) bellow.
9+
This module enables you upload/download binary data in js, see [Examples](#user-content-usage) bellow.
1010

1111
The source code is very simple, just an implementation of native HTTP request, supports both Android (uses awesome native library [AsyncHttpClient](https://github.com/AsyncHttpClient/async-http-client])) and IOS.
1212

@@ -131,6 +131,15 @@ Body of the HTTP request, body can either be a BASE64 string, or an array contai
131131

132132
When body is a base64 string , this string will be converted into byte array in native code, and the request body will be sent as `application/octet-stream`.
133133

134+
#### `base64`
135+
136+
A helper object simply uses [base-64](https://github.com/mathiasbynens/base64) for decode and encode BASE64 data.
137+
138+
```js
139+
RNFetchBlob.base64.encode(data)
140+
RNFetchBlob.base64.decode(data)
141+
```
142+
134143
### FetchBlobResponse
135144

136145
When `fetch` success, it resolve a `FetchBlobResponse` object as first argument. `FetchBlobResponse` object has the following methods (these method are synchronous, so you might take quite a performance impact if the file is big)
@@ -141,11 +150,9 @@ When `fetch` success, it resolve a `FetchBlobResponse` object as first argument.
141150
returns json parsed object (done in js context)
142151
#### text():string
143152
returns decoded base64 string (done in js context)
144-
#### blob():Blob
145-
returns Blob object (one in js context)
153+
146154

147155
### TODO
148156

149157
* Save file to storage
150158
* Custom MIME type in form data
151-

index.js

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
* @version 0.3.3
44
*/
55

6-
import { NativeModules } from 'react-native';
6+
import { NativeModules } from 'react-native'
7+
import base64 from 'base-64'
78

89
const RNFetchBlob = NativeModules.RNFetchBlob
910

@@ -51,21 +52,22 @@ class FetchBlobResponse {
5152
* @return {blob} Return Blob object.
5253
*/
5354
this.blob = (contentType, sliceSize) => {
54-
return b64toBlob(this.data, contentType, sliceSize)
55+
console.warn('FetchBlobResponse.blob() is deprecated and has no funtionality.')
56+
return null
5557
}
5658
/**
5759
* Convert result to text.
5860
* @return {string} Decoded base64 string.
5961
*/
6062
this.text = () => {
61-
return atob(this.data)
63+
return base64.decode(this.data)
6264
}
6365
/**
6466
* Convert result to JSON object.
6567
* @return {object} Parsed javascript object.
6668
*/
6769
this.json = () => {
68-
return JSON.parse(atob(this.data))
70+
return JSON.parse(base64.decode(this.data))
6971
}
7072
/**
7173
* Return BASE64 string directly.
@@ -79,37 +81,6 @@ class FetchBlobResponse {
7981

8082
}
8183

82-
/**
83-
* Convert base64 string to blob, source {@link http://stackoverflow.com/questions/16245767/creating-a-blob-from-a-base64-string-in-javascript}
84-
* @param {string} b64Data Base64 string of data.
85-
* @param {string} contentType MIME type of data.
86-
* @param {number} sliceSize Slice size, default to 512.
87-
* @return {blob} Return Blob object.
88-
*/
89-
function b64toBlob(b64Data, contentType, sliceSize) {
90-
contentType = contentType || '';
91-
sliceSize = sliceSize || 512;
92-
93-
var byteCharacters = atob(b64Data);
94-
var byteArrays = [];
95-
96-
for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
97-
var slice = byteCharacters.slice(offset, offset + sliceSize);
98-
99-
var byteNumbers = new Array(slice.length);
100-
for (var i = 0; i < slice.length; i++) {
101-
byteNumbers[i] = slice.charCodeAt(i);
102-
}
103-
104-
var byteArray = new Uint8Array(byteNumbers);
105-
106-
byteArrays.push(byteArray);
107-
}
108-
109-
var blob = new Blob(byteArrays, {type: contentType});
110-
return blob;
111-
}
112-
11384
export default {
114-
fetch, FetchBlobResponse
85+
fetch, FetchBlobResponse, base64
11586
}

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
{
22
"name": "react-native-fetch-blob",
3-
"version": "0.3.3",
3+
"version": "0.4.0",
44
"description": "A react-native plugin for fetch blob data via HTTP.",
55
"main": "index.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1"
88
},
9+
"dependencies": {
10+
"base-64": "0.1.0"
11+
},
912
"keywords": [
1013
"react-native",
1114
"fetch",

0 commit comments

Comments
 (0)