From 4a8c8ba6e52dd7538e620b9e7aed7afb473e91fc Mon Sep 17 00:00:00 2001 From: Marcus Gartner Date: Mon, 19 Jun 2017 11:34:36 -0700 Subject: [PATCH] Add support for buffers --- .eslintrc | 5 ++++- .gitignore | 3 +++ lib/index.js | 4 ++-- test/index.test.js | 44 ++++++++++++++++++++++++++++++++++++-------- test/setup.test.js | 6 ++++-- 5 files changed, 49 insertions(+), 13 deletions(-) diff --git a/.eslintrc b/.eslintrc index 8e267dd..cc6049f 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,3 +1,6 @@ { - extends: "eslint-config-lob" + extends: "lob", + globals: { + "expect": false + } } diff --git a/.gitignore b/.gitignore index 59d842b..ea7593d 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,6 @@ node_modules # Users Environment Variables .lock-wscript + +# Project-specific .vimrc +.vimrc diff --git a/lib/index.js b/lib/index.js index a2b3ae2..f61061c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -8,10 +8,10 @@ exports.detect = function (buffer) { return utils.ascii(buffer, 0, 4) === '%PDF'; } -exports.measure = function (path) { +exports.measure = function (pathOrBuffer) { return new Promise(function (resolve, reject) { try { - var doc = new PopplerDocument(path); + var doc = new PopplerDocument(pathOrBuffer); if (doc.pageCount < 1) { return reject(new Error('Invalid PDF')); diff --git a/test/index.test.js b/test/index.test.js index e292498..bf51e5d 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1,13 +1,13 @@ 'use strict'; -var fs = require('fs'); -var path = require('path'); -var expect = require('chai').expect; -var pdf = require('../lib/index'); +var fs = require('fs'); +var path = require('path'); +var pdf = require('../lib/index'); describe('pdf', function () { describe('detect', function () { + it('should return true for a PDF', function () { var pdfPath = path.resolve(__dirname, 'fixtures/pdf/123x456.1.pdf'); var result = pdf.detect(fs.readFileSync(pdfPath)); @@ -19,6 +19,7 @@ describe('pdf', function () { var result = pdf.detect(fs.readFileSync(pngPath)); expect(result).to.eql(false); }); + }); describe('measure', function () { @@ -40,10 +41,10 @@ describe('pdf', function () { expectedOutput.pages[i] = { width: width, height: height }; } - it('should return the correct dimensions for ' + file, function () { + it('should return the correct dimensions for a path to ' + file, function () { var pdfPath = path.resolve(fixtures, file); + return pdf.measure(pdfPath) - .bind({}) .then(function (result) { for (var i = 0; i < result.pages.length; i++) { result.pages[i].width = Math.round(result.pages[i].width); @@ -52,18 +53,45 @@ describe('pdf', function () { expect(result).to.eql(expectedOutput); }); }); + + it('should return the correct dimensions for a buffer of ' + file, function () { + var pdfPath = path.resolve(fixtures, file); + var buffer = fs.readFileSync(pdfPath); + + return pdf.measure(buffer) + .then(function (result) { + for (var i = 0; i < result.pages.length; i++) { + result.pages[i].width = Math.round(result.pages[i].width); + result.pages[i].height = Math.round(result.pages[i].height); + } + expect(result).to.eql(expectedOutput); + }); + }); + }); - it('should error with a corrupt PDF', function () { + it('should error with a path to a corrupt PDF', function () { var pdfPath = path.resolve(__dirname, 'fixtures/corrupt/corrupt.pdf'); return expect(pdf.measure(pdfPath)).to.be.rejectedWith(Error); }); - it('should error with a PDF with no pages', function () { + it('should error with a buffer of a corrupt PDF', function () { + var pdfPath = path.resolve(__dirname, 'fixtures/corrupt/corrupt.pdf'); + var buffer = fs.readFileSync(pdfPath); + return expect(pdf.measure(buffer)).to.be.rejectedWith(Error); + }); + + it('should error with a path to a PDF with no pages', function () { var pdfPath = path.resolve(__dirname, 'fixtures/corrupt/no_pages.pdf'); return expect(pdf.measure(pdfPath)).to.be.rejectedWith(Error); }); + it('should error with a buffer of a PDF with no pages', function () { + var pdfPath = path.resolve(__dirname, 'fixtures/corrupt/no_pages.pdf'); + var buffer = fs.readFileSync(pdfPath); + return expect(pdf.measure(buffer)).to.be.rejectedWith(Error); + }); + }); }); diff --git a/test/setup.test.js b/test/setup.test.js index 1635bf9..32985bb 100644 --- a/test/setup.test.js +++ b/test/setup.test.js @@ -1,5 +1,7 @@ 'use strict'; -var chai = require('chai'); +var Chai = require('chai'); -chai.use(require('chai-as-promised')); +Chai.use(require('chai-as-promised')); + +global.expect = Chai.expect;