-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Finished re-writing main encode and decode funcs
IMPROVEMENTS: - Logic wise, no major changes - Better error handling and type checking - Cleaner code - Better documentation Complete unit tests Gulp task to generate documentation More test cases Generate docs
- Loading branch information
1 parent
7d8f72d
commit bb2b5cf
Showing
9 changed files
with
261 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# fnCallback | ||
|
||
[src/node-base64-image.js:13-13](https://github.com/riyadhalnur/node-base64-image/blob/48ccb3a785ba53841dc0821548dff8177da7bfb0/src/node-base64-image.js#L13-L13 "Source code on GitHub") | ||
|
||
Callback for encode/decode functions | ||
|
||
**Parameters** | ||
|
||
- `Error` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** object | ||
- `Response` **([string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) \| [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object))** string or Buffer object | ||
|
||
# encode | ||
|
||
[src/node-base64-image.js:27-65](https://github.com/riyadhalnur/node-base64-image/blob/48ccb3a785ba53841dc0821548dff8177da7bfb0/src/node-base64-image.js#L27-L65 "Source code on GitHub") | ||
|
||
Encodes a remote or local image to Base64 encoded string or Buffer | ||
|
||
**Parameters** | ||
|
||
- `url` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** URL of remote image or local path to image | ||
- `options` **\[[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)]** Options object for extra configuration (optional, default `{}`) | ||
- `options.string` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Returns a Base64 encoded string. Defaults to Buffer object | ||
- `options.local` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Encode a local image file instead of a remote image | ||
- `callback` **fnCallback** Callback function | ||
|
||
Returns **fnCallback** Returns the callback | ||
|
||
# result | ||
|
||
[src/node-base64-image.js:41-41](https://github.com/riyadhalnur/node-base64-image/blob/48ccb3a785ba53841dc0821548dff8177da7bfb0/src/node-base64-image.js#L41-L41 "Source code on GitHub") | ||
|
||
# result | ||
|
||
[src/node-base64-image.js:58-58](https://github.com/riyadhalnur/node-base64-image/blob/48ccb3a785ba53841dc0821548dff8177da7bfb0/src/node-base64-image.js#L58-L58 "Source code on GitHub") | ||
|
||
# decode | ||
|
||
[src/node-base64-image.js:77-89](https://github.com/riyadhalnur/node-base64-image/blob/48ccb3a785ba53841dc0821548dff8177da7bfb0/src/node-base64-image.js#L77-L89 "Source code on GitHub") | ||
|
||
Decodes an base64 encoded image buffer and saves it to disk | ||
|
||
**Parameters** | ||
|
||
- `imageBuffer` **[Buffer](https://nodejs.org/api/buffer.html)** Image Buffer object | ||
- `options` **\[[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)]** Options object for extra configuration (optional, default `{}`) | ||
- `options.filename` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Filename for the final image file | ||
- `callback` **fnCallback** Callback function | ||
|
||
Returns **fnCallback** Returns the callback |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,140 @@ | ||
import {encode, decode} from '../../src/node-base64-image.js'; | ||
|
||
describe('Base64 Image', () => { | ||
describe('Base64 Image decode/encode', () => { | ||
it('should exist', () => { | ||
should.exist(encode); | ||
should.exist(decode); | ||
}); | ||
|
||
describe('Encoder', () => { | ||
it('should return an error if callback is not a function', (done) => { | ||
let url; | ||
let options = {}; | ||
let callback; | ||
|
||
(function () { | ||
encode(url, options, callback); | ||
}).should.throw(TypeError); | ||
|
||
done(); | ||
}); | ||
|
||
it('should return an error if url is null or undefined', (done) => { | ||
let url; | ||
let options = {}; | ||
|
||
encode(url, options, (err, image) => { | ||
err.should.exist; | ||
err.message.should.equal('URL is undefined or not properly formatted'); | ||
|
||
should.not.exist(image); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should return an error if local image could not be loaded', (done) => { | ||
let url = __dirname + '/noimage.jpg'; | ||
let options = {local: true}; | ||
|
||
encode(url, options, (err, image) => { | ||
err.should.exist; | ||
|
||
should.not.exist(image); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should return an error if remote image could not be loaded', (done) => { | ||
let url = 'https://verticalaxisbd.com/noimage.jpg'; | ||
let options = {}; | ||
|
||
encode(url, options, (err, image) => { | ||
err.should.exist; | ||
err.message.should.equal('Error retrieving image - Status Code 404'); | ||
|
||
should.not.exist(image); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should download an image and return the Base64 encoded string', function (done) { | ||
let url = 'https://res.cloudinary.com/verticalaxisbd/image/upload/h_239,w_239/rg1kxkgxayhdgoqdaejz.jpg'; // eslint-disable-line | ||
let options = {string: true}; | ||
|
||
encode(url, options, (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(); | ||
}); | ||
}); | ||
|
||
it('should download an image and return the Buffer object by default', function (done) { | ||
let url = 'https://res.cloudinary.com/verticalaxisbd/image/upload/h_239,w_239/rg1kxkgxayhdgoqdaejz.jpg'; // eslint-disable-line | ||
let options = {}; | ||
|
||
encode(url, options, (err, image) => { | ||
should.not.exist(err); | ||
|
||
image.should.exist; | ||
image.should.be.an.instanceOf(Buffer); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should encode local image and return the Buffer object by default', (done) => { | ||
let path = __dirname + '/test.jpg'; | ||
let options = {local: true}; | ||
|
||
encode(path, options, (err, image) => { | ||
should.not.exist(err); | ||
|
||
image.should.exist; | ||
image.should.be.an.instanceOf(Buffer); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should encode local image and return the Base64 encoded string', (done) => { | ||
let path = __dirname + '/test.jpg'; | ||
let options = {local: true, string: true}; | ||
|
||
encode(path, options, (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(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Decoder', () => { | ||
it('should return an error if image is not a Buffer object', (done) => { | ||
let imageData; | ||
let options = {}; | ||
|
||
decode(imageData, options, (err, response) => { | ||
err.should.exist; | ||
err.message.should.equal('The image is not a Buffer object type'); | ||
|
||
should.not.exist(response); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should decode a Base64 encoded image and save it to disk', (done) => { | ||
let options = {filename: 'test'}; | ||
let imageData = new Buffer('/9j/4AAQSkZJRgABAQAAAQABAAD/2w//Z', 'base64'); | ||
|
||
decode(imageData, options, (err, response) => { | ||
should.not.exist(err); | ||
|
||
response.should.exist; | ||
response.should.equal('Image saved successfully to disk!'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.