From 4256a55bab38b257822babf79880b4c9dcc8d802 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E9=B3=B3?= Date: Sat, 25 Jul 2015 16:11:52 +0800 Subject: [PATCH] Fix UTF8 mojibake when importing Thanks to @yhsiang for pointing out this bug. The original stringification fails when a chunk falls between two utf-8 octets in the same character. --- lib/hackpad.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/hackpad.js b/lib/hackpad.js index 08308b1..f98936b 100644 --- a/lib/hackpad.js +++ b/lib/hackpad.js @@ -44,11 +44,12 @@ var Hackpad = (function() { console.log(request); var req = oauth.request(request, function(responseObj) { - var response = ''; - responseObj.on('data', function(chunk) { response += chunk; }); + var response = []; + responseObj.on('data', function(chunk) { response.push(chunk); }); responseObj.on('end', function () { + var result = Buffer.concat(response).toString('utf-8'); if(responseObj.headers['content-type'].indexOf('application/json') !== -1) { - var data = JSON.parse(response); + var data = JSON.parse(result); if(data.error) { callback(data.error); @@ -56,7 +57,7 @@ var Hackpad = (function() { callback(null, data); } } else { - callback(null, response); + callback(null, result); } }); });