Skip to content

Commit

Permalink
Added Base64 encoder function
Browse files Browse the repository at this point in the history
ADDED:
- Download a given remote URL and return a new Buffer or a string
- Tests
  • Loading branch information
riyadhalnur committed Dec 13, 2014
1 parent 0de7374 commit ba1381e
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: node_js
node_js:
- "0.11"
- "0.10"
notifications:
email:
on_success: always
on_failure: always
35 changes: 35 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

var request = require('request');

var base64encoder = function (url, options, callback) {

options = options || {};
if (typeof callback !== 'function') {
throw new Error('Callback needs to be a function!');
}

if (url === undefined || url === null) {
throw new Error('URL cannot be empty!');
}

request({url: url, encoding: null}, function (err, res, body) {
if (err) { return callback(err); }

if (body && res.statusCode === 200) {
var image;

if (options && options.string === true) {
image = body.toString('base64');
return callback(null, image);
} else {
image = new Buffer(body, 'base64');
return callback(null, image);
}
}
});
};

module.exports = {
base64encoder: base64encoder
};
35 changes: 35 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "nkde-base64-image",
"version": "0.0.1",
"description": "Download images from remote URLs and encode/decode them to base64",
"main": "index.js",
"scripts": {
"test": "mocha test/tests.js"
},
"engines": {
"node": ">=0.10"
},
"repository": "https://github.com/riyadhalnur/node-base64-image",
"homepage": "http://riyadhalnur.github.io/node-base64-image/",
"keywords": [
"image",
"download",
"base64",
"encode",
"decode",
"javascript",
"node"
],
"bugs": {
"url": "https://github.com/riyadhalnur/node-base64-image/issuess"
},
"author": "Riyadh Al Nur <[email protected]> (http://blog.verticalaxisbd.com)",
"license": "GPLv3",
"dependencies": {
"request": "^2.51.x"
},
"devDependencies": {
"mocha": "^2.0.x",
"should": "^4.3.x"
}
}
67 changes: 67 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'use strict';

var should = require('should');
var base64Image = require('../index');

describe('Image download and encode/decode to Base64', function () {
describe('Base64 encoder', function () {
it('should throw an error if callback is not a function', function (done) {
var url, options, callback;

var helperFunc = function () {
base64Image.base64encoder(url, options, callback);
};

helperFunc.should.throw(Error);
helperFunc.should.throw('Callback needs to be a function!');

done();
});

it('should throw an error if url is null or undefined', function (done) {
var url = undefined, options;

var helperFunc = function () {
base64Image.base64encoder(url, options, function (err, image) {
err.should.exist;
image.should.not.exist;
});
};

helperFunc.should.throw(Error);
helperFunc.should.throw('URL cannot be empty!');

done();
});

it('should download an image and return base64 encoded Buffer', function (done) {
this.timeout(15000);

var url = 'http://www.pctools.com/security-news/wp-content/uploads/2011/09/code.jpg', options = {};

base64Image.base64encoder(url, options, function (err, image) {
should.not.exist(err);

image.should.exist;
image.should.be.an.instanceOf(Buffer);

done();
});
});

it('should download an image and return base64 encoded string', function (done) {
this.timeout(15000);

var url = 'http://www.pctools.com/security-news/wp-content/uploads/2011/09/code.jpg', options = {string: true};

base64Image.base64encoder(url, options, function (err, image) {
should.not.exist(err);

image.should.exist;
image.should.match(/^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/);

done();
});
});
});
});

0 comments on commit ba1381e

Please sign in to comment.