Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for buffers #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
extends: "eslint-config-lob"
extends: "lob",
globals: {
"expect": false
}
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ node_modules

# Users Environment Variables
.lock-wscript

# Project-specific .vimrc
.vimrc
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
44 changes: 36 additions & 8 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -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));
Expand All @@ -19,6 +19,7 @@ describe('pdf', function () {
var result = pdf.detect(fs.readFileSync(pngPath));
expect(result).to.eql(false);
});

});

describe('measure', function () {
Expand All @@ -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);
Expand All @@ -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);
});

});

});
6 changes: 4 additions & 2 deletions test/setup.test.js
Original file line number Diff line number Diff line change
@@ -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;