From ec02a3e87ab6e82537c3ba8c1f0f423dd30f2ca5 Mon Sep 17 00:00:00 2001 From: Cristian Talau Date: Tue, 23 Jun 2015 00:23:50 +0300 Subject: [PATCH] Unicode content support. --- github.js | 20 +++++++++++--------- package.json | 6 +++--- test/test.repo.js | 14 ++++++++++++++ 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/github.js b/github.js index 88c71c0f..51af8e1c 100644 --- a/github.js +++ b/github.js @@ -16,19 +16,21 @@ // Initial Setup // ------------- - var XMLHttpRequest, _, btoa; + var XMLHttpRequest, _, b64encode; /* istanbul ignore else */ if (typeof exports !== 'undefined') { XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; _ = require('underscore'); - if (typeof btoa === 'undefined') { - btoa = require('btoa'); //jshint ignore:line - } + b64encode = require('js-base64').Base64.encode; } else { _ = window._; - btoa = window.btoa; + b64encode = function(str) { + return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) { + return String.fromCharCode('0x' + p1); + })); + }; } - + //prefer native XMLHttpRequest always /* istanbul ignore if */ if (typeof window !== 'undefined' && typeof window.XMLHttpRequest !== 'undefined'){ @@ -83,7 +85,7 @@ xhr.setRequestHeader('Content-Type','application/json;charset=UTF-8'); if ((options.token) || (options.username && options.password)) { - var authorization = options.token ? 'token ' + options.token : 'Basic ' + btoa(options.username + ':' + options.password); + var authorization = options.token ? 'token ' + options.token : 'Basic ' + b64encode(options.username + ':' + options.password); xhr.setRequestHeader('Authorization', authorization); } if (data) { @@ -417,7 +419,7 @@ }; } else { content = { - "content": btoa(String.fromCharCode.apply(null, new Uint8Array(content))), + "content": b64encode(String.fromCharCode.apply(null, new Uint8Array(content))), "encoding": "base64" }; } @@ -675,7 +677,7 @@ if (err && err.error !== 404) return cb(err); _request("PUT", repoPath + "/contents/" + encodeURI(path), { message: message, - content: btoa(content), + content: b64encode(content), branch: branch, sha: sha }, cb); diff --git a/package.json b/package.json index 6861c216..a7d15fb5 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,7 @@ "description": "A higher-level wrapper around the Github API.", "main": "github.js", "dependencies": { - "atob": "^1.1.2", - "btoa": "^1.1.2", + "js-base64": "^2.1.8", "underscore": "~1.8.3", "xmlhttprequest": "~1.7.0" }, @@ -41,7 +40,8 @@ "url": "https://github.com/michael/github/issues" }, "browser": { - "xmlhttprequest": false + "xmlhttprequest": false, + "base64": false }, "testling": { "files": "test/test.*.js", diff --git a/test/test.repo.js b/test/test.repo.js index 6196cccf..db1398fa 100644 --- a/test/test.repo.js +++ b/test/test.repo.js @@ -132,6 +132,20 @@ test('Create Repo', function(t) { q.end(); }); }); + + t.test('repo.writeUnicodeContent', function(q) { + repo.write('master', 'TEST.md', '\u2014', 'Long dash unicode', function(err) { + q.error(err); + repo.read('master', 'TEST.md', function(err, obj) { + q.error(err); + t.equals('\u2014', obj); + }); + q.end(); + }); + }); + + + clearTimeout(timeout); t.end(); });