这是indexloc提供的服务,不要输入任何密码
Skip to content

Updates #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sudo: false
language: node_js
node_js:
- "node"
- "4"
script: "npm test"
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tweetnacl-util",
"version": "0.13.5",
"version": "0.15.0",
"homepage": "https://dchest.github.io/tweetnacl-util-js",
"authors": [
"TweetNaCl.js Contributors"
Expand Down
52 changes: 41 additions & 11 deletions nacl-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@

var util = {};

function validateBase64(s) {
if (!(/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(s))) {
throw new TypeError('invalid encoding');
}
}

util.decodeUTF8 = function(s) {
if (typeof s !== 'string') throw new TypeError('expected string');
var i, d = unescape(encodeURIComponent(s)), b = new Uint8Array(d.length);
Expand All @@ -26,25 +32,49 @@
return decodeURIComponent(escape(s.join('')));
};

util.encodeBase64 = function(arr) {
if (typeof btoa === 'undefined') {
return (new Buffer(arr)).toString('base64');
if (typeof atob === 'undefined') {
// Node.js

if (typeof Buffer.from !== 'undefined') {
// Node v6 and later
util.encodeBase64 = function (arr) { // v6 and later
return Buffer.from(arr).toString('base64');
};

util.decodeBase64 = function (s) {
validateBase64(s);
return new Uint8Array(Array.prototype.slice.call(Buffer.from(s, 'base64'), 0));
};

} else {
// Node earlier than v6
util.encodeBase64 = function (arr) { // v6 and later
return (new Buffer(arr)).toString('base64');
};

util.decodeBase64 = function(s) {
validateBase64(s);
return new Uint8Array(Array.prototype.slice.call(new Buffer(s, 'base64'), 0));
};
}

} else {
// Browsers

util.encodeBase64 = function(arr) {
var i, s = [], len = arr.length;
for (i = 0; i < len; i++) s.push(String.fromCharCode(arr[i]));
return btoa(s.join(''));
}
};
};

util.decodeBase64 = function(s) {
if (typeof atob === 'undefined') {
return new Uint8Array(Array.prototype.slice.call(new Buffer(s, 'base64'), 0));
} else {
util.decodeBase64 = function(s) {
validateBase64(s);
var i, d = atob(s), b = new Uint8Array(d.length);
for (i = 0; i < d.length; i++) b[i] = d.charCodeAt(i);
return b;
}
};
};

}

return util;

Expand Down
2 changes: 1 addition & 1 deletion nacl-util.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{
"name": "tweetnacl-util",
"version": "0.14.0",
"version": "0.15.0",
"description": "String encoding utilitlies extracted from TweetNaCl.js",
"main": "nacl-util.js",
"types": "nacl-util.d.ts",
"scripts": {
"build": "uglifyjs nacl-util.js -c -m -o nacl-util.min.js"
"build": "uglifyjs nacl-util.js -c -m -o nacl-util.min.js",
"test": "tape test/*.js; yarn run build-browser-test",
"build-browser-test": "browserify test/test.js > test/browser/bundle.js"
},
"repository": {
"type": "git",
Expand All @@ -24,6 +26,8 @@
},
"homepage": "https://github.com/dchest/tweetnacl-util-js",
"devDependencies": {
"browserify": "^14.1.0",
"tape": "^4.6.3",
"uglify-js": "^2.6.1"
},
"browser": {
Expand Down
Loading