From b54f0f5ebf5dc6b371ded8d8f6d19becd55b5703 Mon Sep 17 00:00:00 2001 From: Ben Hsieh Date: Thu, 19 May 2016 16:41:06 +0800 Subject: [PATCH] Fix #5 Fix #6 replace atob with base-64 library. --- .gitignore | 2 ++ README.md | 15 +++++++++++---- index.js | 43 +++++++------------------------------------ package.json | 5 ++++- 4 files changed, 24 insertions(+), 41 deletions(-) diff --git a/.gitignore b/.gitignore index 01171ecd1..92115f384 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +node_modules/ + # Xcode # # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore diff --git a/README.md b/README.md index 2e8268781..93b7f3554 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ If you're dealing with image or file server that requires an `Authorization` tok See [[fetch] Does fetch with blob() marshal data across the bridge?]([fetch] Does fetch with blob() marshal data across the bridge?). -This module enables you upload/download binary data in js, see [Examples](#user-content-usage) bellow. +This module enables you upload/download binary data in js, see [Examples](#user-content-usage) bellow. 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. @@ -131,6 +131,15 @@ Body of the HTTP request, body can either be a BASE64 string, or an array contai 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`. +#### `base64` + +A helper object simply uses [base-64](https://github.com/mathiasbynens/base64) for decode and encode BASE64 data. + +```js +RNFetchBlob.base64.encode(data) +RNFetchBlob.base64.decode(data) +``` + ### FetchBlobResponse 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. returns json parsed object (done in js context) #### text():string returns decoded base64 string (done in js context) -#### blob():Blob - returns Blob object (one in js context) + ### TODO * Save file to storage * Custom MIME type in form data - diff --git a/index.js b/index.js index 67c6ce1d5..c0aa0eb01 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,8 @@ * @version 0.3.3 */ -import { NativeModules } from 'react-native'; +import { NativeModules } from 'react-native' +import base64 from 'base-64' const RNFetchBlob = NativeModules.RNFetchBlob @@ -51,21 +52,22 @@ class FetchBlobResponse { * @return {blob} Return Blob object. */ this.blob = (contentType, sliceSize) => { - return b64toBlob(this.data, contentType, sliceSize) + console.warn('FetchBlobResponse.blob() is deprecated and has no funtionality.') + return null } /** * Convert result to text. * @return {string} Decoded base64 string. */ this.text = () => { - return atob(this.data) + return base64.decode(this.data) } /** * Convert result to JSON object. * @return {object} Parsed javascript object. */ this.json = () => { - return JSON.parse(atob(this.data)) + return JSON.parse(base64.decode(this.data)) } /** * Return BASE64 string directly. @@ -79,37 +81,6 @@ class FetchBlobResponse { } -/** - * Convert base64 string to blob, source {@link http://stackoverflow.com/questions/16245767/creating-a-blob-from-a-base64-string-in-javascript} - * @param {string} b64Data Base64 string of data. - * @param {string} contentType MIME type of data. - * @param {number} sliceSize Slice size, default to 512. - * @return {blob} Return Blob object. - */ -function b64toBlob(b64Data, contentType, sliceSize) { - contentType = contentType || ''; - sliceSize = sliceSize || 512; - - var byteCharacters = atob(b64Data); - var byteArrays = []; - - for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) { - var slice = byteCharacters.slice(offset, offset + sliceSize); - - var byteNumbers = new Array(slice.length); - for (var i = 0; i < slice.length; i++) { - byteNumbers[i] = slice.charCodeAt(i); - } - - var byteArray = new Uint8Array(byteNumbers); - - byteArrays.push(byteArray); - } - - var blob = new Blob(byteArrays, {type: contentType}); - return blob; -} - export default { - fetch, FetchBlobResponse + fetch, FetchBlobResponse, base64 } diff --git a/package.json b/package.json index 8d64b931c..7f43ae2fb 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,14 @@ { "name": "react-native-fetch-blob", - "version": "0.3.3", + "version": "0.4.0", "description": "A react-native plugin for fetch blob data via HTTP.", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, + "dependencies": { + "base-64": "0.1.0" + }, "keywords": [ "react-native", "fetch",