Skip to content

Commit d255e1e

Browse files
Merge pull request mapbox#2 from mapbox/react-native
Support React Native; use base-64 package
2 parents ec48882 + df077b1 commit d255e1e

5 files changed

Lines changed: 18 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## HEAD
4+
5+
- Use `base-64` behind the scenes to enable React Native compatibility.
6+
37
## 0.1.0
48

5-
- Start this log.
9+
- Start this log.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Build Status](https://travis-ci.com/mapbox/parse-mapbox-token.svg?token=FB2dZNVWaGo68KZnwz9M&branch=master)](https://travis-ci.com/mapbox/parse-mapbox-token)
44

5-
Parse a Mapbox API token, **in Node or the browser**.
5+
Parse a Mapbox API token, in any JS environment, including Node, browser, and React Native.
66

77
Learn about Mapbox API tokens by reading [Mapbox's API documentation](https://www.mapbox.com/api-documentation/#tokens).
88

@@ -38,4 +38,4 @@ The following properties may or may not be present:
3838
- **client:** OAuth client for which the token was granted.
3939
- **impersonator:** ID of the user impersonating the account owner.
4040

41-
The token's payload is parsed with [`window.atob`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/atob) in the browser and with [`Buffer`](https://nodejs.org/api/buffer.html) in Node.
41+
The token's payload is parsed with [`base-64`](https://github.com/mathiasbynens/base64).

index.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
var base64 = require('base-64');
4+
35
var tokenCache = {};
46

57
function parseToken(token) {
@@ -34,18 +36,10 @@ function parseToken(token) {
3436

3537
function parsePaylod(rawPayload) {
3638
try {
37-
if (typeof window !== 'undefined' && window.atob) {
38-
return JSON.parse(atob(rawPayload));
39-
}
40-
if (typeof Buffer !== 'undefined') {
41-
return JSON.parse(new Buffer(rawPayload, 'base64').toString());
42-
}
39+
return JSON.parse(base64.decode(rawPayload));
4340
} catch (parseError) {
4441
throw new Error('Invalid token');
4542
}
46-
throw new Error(
47-
'Unable to parse in an enviornment without window.atob (browsers) or Buffer (Node)'
48-
);
4943
}
5044

5145
function has(obj, key) {

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,8 @@
2929
"browserify": "^16.2.0",
3030
"eslint": "^4.19.1",
3131
"tape": "^4.9.0"
32+
},
33+
"dependencies": {
34+
"base-64": "^0.1.0"
3235
}
3336
}

0 commit comments

Comments
 (0)