Skip to content

Commit

Permalink
Fix for undefined btoa
Browse files Browse the repository at this point in the history
  • Loading branch information
César Guadarrama committed Aug 18, 2018
1 parent 5b2580c commit d1ed01e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
53 changes: 53 additions & 0 deletions lib/Base64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Inspired by: https://github.com/davidchambers/Base64.js/blob/master/base64.js

const chars =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='

const Base64 = {
btoa: (input = '') => {
let str = input
let output = ''

for (
let block = 0, charCode, i = 0, map = chars;
str.charAt(i | 0) || ((map = '='), i % 1);
output += map.charAt(63 & (block >> (8 - (i % 1) * 8)))
) {
charCode = str.charCodeAt((i += 3 / 4))

if (charCode > 0xff) {
throw new Error(
"'btoa' failed: The string to be encoded contains characters outside of the Latin1 range."
)
}

block = (block << 8) | charCode
}

return output
},

atob: (input = '') => {
let str = input.replace(/=+$/, '')
let output = ''

if (str.length % 4 == 1) {
throw new Error(
"'atob' failed: The string to be decoded is not correctly encoded."
)
}
for (
let bc = 0, bs = 0, buffer, i = 0;
(buffer = str.charAt(i++));
~buffer && ((bs = bc % 4 ? bs * 64 + buffer : buffer), bc++ % 4)
? (output += String.fromCharCode(255 & (bs >> ((-2 * bc) & 6))))
: 0
) {
buffer = chars.indexOf(buffer)
}

return output
},
}

export default Base64
8 changes: 6 additions & 2 deletions lib/toBase64.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
export default function b64EncodeUnicode(str) {
import Base64 from './Base64'

function b64EncodeUnicode(str) {
// first we use encodeURIComponent to get percent-encoded UTF-8,
// then we convert the percent encodings into raw bytes which
// can be fed into btoa.
return btoa(
return Base64.btoa(
encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function toSolidBytes(
match,
p1
Expand All @@ -11,3 +13,5 @@ export default function b64EncodeUnicode(str) {
})
)
}

export default b64EncodeUnicode
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "conekta-react-native",
"version": "0.1.0",
"version": "0.1.1",
"description": "React Native Conekta wrapper",
"main": "index.js",
"scripts": {
Expand All @@ -23,7 +23,7 @@
"author": "nure",
"license": "MIT",
"dependencies": {
"react-native-uuid": "^1.4.9",
"buffer": "^5.2.0"
"buffer": "^5.2.0",
"react-native-uuid": "^1.4.9"
}
}
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ buffer@^5.2.0:
base64-js "^1.0.2"
ieee754 "^1.1.4"

conekta.js@^0.5.0:
version "0.5.0"
resolved "https://registry.yarnpkg.com/conekta.js/-/conekta.js-0.5.0.tgz#2010377cf52d7cd110c994b1451961b0a80ff9ed"

ieee754@^1.1.4:
version "1.1.12"
resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.1.12.tgz#50bf24e5b9c8bb98af4964c941cdb0918da7b60b"
Expand Down

0 comments on commit d1ed01e

Please sign in to comment.