Skip to content

Commit

Permalink
Added buffer support
Browse files Browse the repository at this point in the history
  • Loading branch information
rkrol authored and netroy committed Oct 11, 2013
1 parent 97f9cfb commit 934a113
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ function syncFileToBuffer (filepath, buffer) {
* @params callback - optional function for async detection
*/
module.exports = function (input, callback) {

// Handle buffer input
if (input instanceof Buffer) {
return lookup(input);
}

// input should be a string at this point
if (typeof input !== 'string') {
throw new TypeError('invalid invocation');
Expand Down
21 changes: 21 additions & 0 deletions specs/all.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var expect = require('expect.js');
var glob = require('glob');
var path = require('path');

var libpath = process.env.TEST_COV ? '../lib-cov/' : '../lib/';
var imageSize = require(libpath);
Expand Down Expand Up @@ -34,6 +35,25 @@ describe('Valid images', function () {
expect(dimensions.height).to.be(456);
});
});

describe(file + ' as buffer', function () {

var dimensions;
var bufferSize = 8192;
beforeEach(function() {
var buffer = new Buffer(bufferSize);
var filepath = path.resolve(file);
var descriptor = fs.openSync(filepath, 'r');
fs.readSync(descriptor, buffer, 0, bufferSize, 0);
dimensions = imageSize(buffer);
});


it('should return correct size for ' + file + ' buffer', function() {
expect(dimensions.width).to.be(123);
expect(dimensions.height).to.be(456);
});
});
});
});

Expand Down Expand Up @@ -76,6 +96,7 @@ describe('Unsupported Images', function () {
});
});

// If something other than a buffer or filepath is passed
describe('Invalid invocation', function () {

describe('invalid type', function () {
Expand Down

0 comments on commit 934a113

Please sign in to comment.