diff --git a/.babelrc b/.babelrc new file mode 100644 index 000000000..1cb920f07 --- /dev/null +++ b/.babelrc @@ -0,0 +1,8 @@ +{ + "presets": [["env", {"modules": false, "targets": {"node": "6.10"}}]], + "env": { + "test": { + "presets": [["env", {"targets": {"node": "6.10"}}]] + } + } +} \ No newline at end of file diff --git a/lib/font/standard.js b/lib/font/standard.js index feaeee4dc..80eefcb20 100644 --- a/lib/font/standard.js +++ b/lib/font/standard.js @@ -4,20 +4,20 @@ import fs from 'fs'; // This insanity is so bundlers can inline the font files const STANDARD_FONTS = { - "Courier"() { return fs.readFileSync(__dirname + "/font/data/Courier.afm", 'utf8'); }, - "Courier-Bold"() { return fs.readFileSync(__dirname + "/font/data/Courier-Bold.afm", 'utf8'); }, - "Courier-Oblique"() { return fs.readFileSync(__dirname + "/font/data/Courier-Oblique.afm", 'utf8'); }, - "Courier-BoldOblique"() { return fs.readFileSync(__dirname + "/font/data/Courier-BoldOblique.afm", 'utf8'); }, - "Helvetica"() { return fs.readFileSync(__dirname + "/font/data/Helvetica.afm", 'utf8'); }, - "Helvetica-Bold"() { return fs.readFileSync(__dirname + "/font/data/Helvetica-Bold.afm", 'utf8'); }, - "Helvetica-Oblique"() { return fs.readFileSync(__dirname + "/font/data/Helvetica-Oblique.afm", 'utf8'); }, - "Helvetica-BoldOblique"() { return fs.readFileSync(__dirname + "/font/data/Helvetica-BoldOblique.afm", 'utf8'); }, - "Times-Roman"() { return fs.readFileSync(__dirname + "/font/data/Times-Roman.afm", 'utf8'); }, - "Times-Bold"() { return fs.readFileSync(__dirname + "/font/data/Times-Bold.afm", 'utf8'); }, - "Times-Italic"() { return fs.readFileSync(__dirname + "/font/data/Times-Italic.afm", 'utf8'); }, - "Times-BoldItalic"() { return fs.readFileSync(__dirname + "/font/data/Times-BoldItalic.afm", 'utf8'); }, - "Symbol"() { return fs.readFileSync(__dirname + "/font/data/Symbol.afm", 'utf8'); }, - "ZapfDingbats"() { return fs.readFileSync(__dirname + "/font/data/ZapfDingbats.afm", 'utf8'); } + "Courier"() { return fs.readFileSync(__dirname + "/data/Courier.afm", 'utf8'); }, + "Courier-Bold"() { return fs.readFileSync(__dirname + "/data/Courier-Bold.afm", 'utf8'); }, + "Courier-Oblique"() { return fs.readFileSync(__dirname + "/data/Courier-Oblique.afm", 'utf8'); }, + "Courier-BoldOblique"() { return fs.readFileSync(__dirname + "/data/Courier-BoldOblique.afm", 'utf8'); }, + "Helvetica"() { return fs.readFileSync(__dirname + "/data/Helvetica.afm", 'utf8'); }, + "Helvetica-Bold"() { return fs.readFileSync(__dirname + "/data/Helvetica-Bold.afm", 'utf8'); }, + "Helvetica-Oblique"() { return fs.readFileSync(__dirname + "/data/Helvetica-Oblique.afm", 'utf8'); }, + "Helvetica-BoldOblique"() { return fs.readFileSync(__dirname + "/data/Helvetica-BoldOblique.afm", 'utf8'); }, + "Times-Roman"() { return fs.readFileSync(__dirname + "/data/Times-Roman.afm", 'utf8'); }, + "Times-Bold"() { return fs.readFileSync(__dirname + "/data/Times-Bold.afm", 'utf8'); }, + "Times-Italic"() { return fs.readFileSync(__dirname + "/data/Times-Italic.afm", 'utf8'); }, + "Times-BoldItalic"() { return fs.readFileSync(__dirname + "/data/Times-BoldItalic.afm", 'utf8'); }, + "Symbol"() { return fs.readFileSync(__dirname + "/data/Symbol.afm", 'utf8'); }, + "ZapfDingbats"() { return fs.readFileSync(__dirname + "/data/ZapfDingbats.afm", 'utf8'); } }; class StandardFont extends PDFFont { diff --git a/package.json b/package.json index 4f232ce7e..f4f83e5b8 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "bugs": "http://github.com/devongovett/pdfkit/issues", "devDependencies": { "babel-core": "^6.26.3", + "babel-jest": "^23.6.0", "babel-plugin-external-helpers": "^6.22.0", "babel-preset-env": "^1.7.0", "blob-stream": "^0.1.2", @@ -54,6 +55,7 @@ "website": "node docs/generate_website.js", "docs": "npm run pdf-guide && npm run website && npm run browser-demo", "test": "jest -i", + "test:integration": "jest integration/ -i", "test:unit": "jest unit/ -i" }, "main": "js/pdfkit.js", diff --git a/rollup.config.js b/rollup.config.js index cb8dd6159..c2c513852 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -41,7 +41,7 @@ export default [ }), copy({ files: ['lib/font/data/*.afm'], - dest: 'js/font/data' + dest: 'js/data' }), stripFSInterop() ] diff --git a/tests/__snapshots__/fonts.spec.js.snap b/tests/integration/__snapshots__/fonts.spec.js.snap similarity index 78% rename from tests/__snapshots__/fonts.spec.js.snap rename to tests/integration/__snapshots__/fonts.spec.js.snap index 0c751752b..e4884386d 100644 Binary files a/tests/__snapshots__/fonts.spec.js.snap and b/tests/integration/__snapshots__/fonts.spec.js.snap differ diff --git a/tests/pdfmake/__snapshots__/security.spec.js.snap b/tests/integration/__snapshots__/security.spec.js.snap similarity index 74% rename from tests/pdfmake/__snapshots__/security.spec.js.snap rename to tests/integration/__snapshots__/security.spec.js.snap index a8df99fbb..acf386cd7 100644 Binary files a/tests/pdfmake/__snapshots__/security.spec.js.snap and b/tests/integration/__snapshots__/security.spec.js.snap differ diff --git a/tests/__snapshots__/text.spec.js.snap b/tests/integration/__snapshots__/text.spec.js.snap similarity index 91% rename from tests/__snapshots__/text.spec.js.snap rename to tests/integration/__snapshots__/text.spec.js.snap index b331ab7f7..1e4f0cc23 100644 Binary files a/tests/__snapshots__/text.spec.js.snap and b/tests/integration/__snapshots__/text.spec.js.snap differ diff --git a/tests/__snapshots__/vector.spec.js.snap b/tests/integration/__snapshots__/vector.spec.js.snap similarity index 76% rename from tests/__snapshots__/vector.spec.js.snap rename to tests/integration/__snapshots__/vector.spec.js.snap index 7cf40f092..174c9da6d 100644 Binary files a/tests/__snapshots__/vector.spec.js.snap and b/tests/integration/__snapshots__/vector.spec.js.snap differ diff --git a/tests/fonts.spec.js b/tests/integration/fonts.spec.js similarity index 100% rename from tests/fonts.spec.js rename to tests/integration/fonts.spec.js diff --git a/tests/helpers.js b/tests/integration/helpers.js similarity index 79% rename from tests/helpers.js rename to tests/integration/helpers.js index 7e558cc77..ced1b73f8 100644 --- a/tests/helpers.js +++ b/tests/integration/helpers.js @@ -1,7 +1,22 @@ -var PDFDocument = require('../'); +var PDFDocument = require('../../lib/document').default; +var PDFSecurity = require('../../lib/security').default; +var CryptoJS = require('crypto-js'); var path = require('path'); var fs = require('fs'); +// manual mock for PDFSecurity to ensure stored id will be the same accross different systems +PDFSecurity.generateFileID = () => { + return new Buffer('mocked-pdf-id'); +} + +PDFSecurity.generateRandomWordArray = (bytes) => { + const words = []; + for (let i = 0; i < bytes; i++) { + words.push(0x00010203); + } + return new CryptoJS.lib.WordArray.init(words, bytes); +} + function updatePdf (pdfData, testState, snapshotChanges) { const pdfDir = path.join(path.dirname(testState.testPath), '__pdfs__'); if (!fs.existsSync(pdfDir)) { diff --git a/tests/pdfmake/__snapshots__/absolute.spec.js.snap b/tests/integration/pdfmake/__snapshots__/absolute.spec.js.snap similarity index 66% rename from tests/pdfmake/__snapshots__/absolute.spec.js.snap rename to tests/integration/pdfmake/__snapshots__/absolute.spec.js.snap index 001e6f9ce..7b18943ae 100644 Binary files a/tests/pdfmake/__snapshots__/absolute.spec.js.snap and b/tests/integration/pdfmake/__snapshots__/absolute.spec.js.snap differ diff --git a/tests/pdfmake/__snapshots__/background.spec.js.snap b/tests/integration/pdfmake/__snapshots__/background.spec.js.snap similarity index 66% rename from tests/pdfmake/__snapshots__/background.spec.js.snap rename to tests/integration/pdfmake/__snapshots__/background.spec.js.snap index c600ab3cc..ca8ff312d 100644 Binary files a/tests/pdfmake/__snapshots__/background.spec.js.snap and b/tests/integration/pdfmake/__snapshots__/background.spec.js.snap differ diff --git a/tests/pdfmake/__snapshots__/basics.spec.js.snap b/tests/integration/pdfmake/__snapshots__/basics.spec.js.snap similarity index 69% rename from tests/pdfmake/__snapshots__/basics.spec.js.snap rename to tests/integration/pdfmake/__snapshots__/basics.spec.js.snap index 16074a300..40886878a 100644 Binary files a/tests/pdfmake/__snapshots__/basics.spec.js.snap and b/tests/integration/pdfmake/__snapshots__/basics.spec.js.snap differ diff --git a/tests/pdfmake/__snapshots__/columns_simple.spec.js.snap b/tests/integration/pdfmake/__snapshots__/columns_simple.spec.js.snap similarity index 80% rename from tests/pdfmake/__snapshots__/columns_simple.spec.js.snap rename to tests/integration/pdfmake/__snapshots__/columns_simple.spec.js.snap index 9cd1a461e..5d583dd1b 100644 Binary files a/tests/pdfmake/__snapshots__/columns_simple.spec.js.snap and b/tests/integration/pdfmake/__snapshots__/columns_simple.spec.js.snap differ diff --git a/tests/pdfmake/__snapshots__/images.spec.js.snap b/tests/integration/pdfmake/__snapshots__/images.spec.js.snap similarity index 71% rename from tests/pdfmake/__snapshots__/images.spec.js.snap rename to tests/integration/pdfmake/__snapshots__/images.spec.js.snap index 89415bba3..7cabe09bd 100644 Binary files a/tests/pdfmake/__snapshots__/images.spec.js.snap and b/tests/integration/pdfmake/__snapshots__/images.spec.js.snap differ diff --git a/tests/pdfmake/__snapshots__/lists.spec.js.snap b/tests/integration/pdfmake/__snapshots__/lists.spec.js.snap similarity index 75% rename from tests/pdfmake/__snapshots__/lists.spec.js.snap rename to tests/integration/pdfmake/__snapshots__/lists.spec.js.snap index b1508befa..497a49a17 100644 Binary files a/tests/pdfmake/__snapshots__/lists.spec.js.snap and b/tests/integration/pdfmake/__snapshots__/lists.spec.js.snap differ diff --git a/tests/pdfmake/__snapshots__/page_references.spec.js.snap b/tests/integration/pdfmake/__snapshots__/page_references.spec.js.snap similarity index 86% rename from tests/pdfmake/__snapshots__/page_references.spec.js.snap rename to tests/integration/pdfmake/__snapshots__/page_references.spec.js.snap index 28ca450ff..ce421ffb8 100644 Binary files a/tests/pdfmake/__snapshots__/page_references.spec.js.snap and b/tests/integration/pdfmake/__snapshots__/page_references.spec.js.snap differ diff --git a/tests/pdfmake/__snapshots__/qrcode.spec.js.snap b/tests/integration/pdfmake/__snapshots__/qrcode.spec.js.snap similarity index 76% rename from tests/pdfmake/__snapshots__/qrcode.spec.js.snap rename to tests/integration/pdfmake/__snapshots__/qrcode.spec.js.snap index e5e8bdc61..220e32418 100644 Binary files a/tests/pdfmake/__snapshots__/qrcode.spec.js.snap and b/tests/integration/pdfmake/__snapshots__/qrcode.spec.js.snap differ diff --git a/tests/pdfmake/__snapshots__/tables.spec.js.snap b/tests/integration/pdfmake/__snapshots__/tables.spec.js.snap similarity index 79% rename from tests/pdfmake/__snapshots__/tables.spec.js.snap rename to tests/integration/pdfmake/__snapshots__/tables.spec.js.snap index ff64b6281..4136dd12a 100644 Binary files a/tests/pdfmake/__snapshots__/tables.spec.js.snap and b/tests/integration/pdfmake/__snapshots__/tables.spec.js.snap differ diff --git a/tests/pdfmake/__snapshots__/text_decorations.spec.js.snap b/tests/integration/pdfmake/__snapshots__/text_decorations.spec.js.snap similarity index 74% rename from tests/pdfmake/__snapshots__/text_decorations.spec.js.snap rename to tests/integration/pdfmake/__snapshots__/text_decorations.spec.js.snap index e39cdc555..c3a82bb62 100644 Binary files a/tests/pdfmake/__snapshots__/text_decorations.spec.js.snap and b/tests/integration/pdfmake/__snapshots__/text_decorations.spec.js.snap differ diff --git a/tests/pdfmake/__snapshots__/toc.spec.js.snap b/tests/integration/pdfmake/__snapshots__/toc.spec.js.snap similarity index 85% rename from tests/pdfmake/__snapshots__/toc.spec.js.snap rename to tests/integration/pdfmake/__snapshots__/toc.spec.js.snap index 8ebe539bc..6996fe958 100644 Binary files a/tests/pdfmake/__snapshots__/toc.spec.js.snap and b/tests/integration/pdfmake/__snapshots__/toc.spec.js.snap differ diff --git a/tests/pdfmake/__snapshots__/watermark.spec.js.snap b/tests/integration/pdfmake/__snapshots__/watermark.spec.js.snap similarity index 92% rename from tests/pdfmake/__snapshots__/watermark.spec.js.snap rename to tests/integration/pdfmake/__snapshots__/watermark.spec.js.snap index 309eb130a..cf20a9d7b 100644 Binary files a/tests/pdfmake/__snapshots__/watermark.spec.js.snap and b/tests/integration/pdfmake/__snapshots__/watermark.spec.js.snap differ diff --git a/tests/pdfmake/absolute.spec.js b/tests/integration/pdfmake/absolute.spec.js similarity index 99% rename from tests/pdfmake/absolute.spec.js rename to tests/integration/pdfmake/absolute.spec.js index dc5f74fa6..16975831d 100644 --- a/tests/pdfmake/absolute.spec.js +++ b/tests/integration/pdfmake/absolute.spec.js @@ -1,5 +1,5 @@ var {runDocTest} = require('../helpers'); -var {jpeg: bee} = require('../images/bee') +var {jpeg: bee} = require('../../images/bee') describe('pdfmake', function () { // somehow the generated pdf changes at each run diff --git a/tests/pdfmake/background.spec.js b/tests/integration/pdfmake/background.spec.js similarity index 99% rename from tests/pdfmake/background.spec.js rename to tests/integration/pdfmake/background.spec.js index 095178456..966c4447f 100644 --- a/tests/pdfmake/background.spec.js +++ b/tests/integration/pdfmake/background.spec.js @@ -1,5 +1,5 @@ var {runDocTest} = require('../helpers'); -var {jpeg: bee} = require('../images/bee') +var {jpeg: bee} = require('../../images/bee') describe('pdfmake', function () { // somehow the generated pdf changes at each run diff --git a/tests/pdfmake/basics.spec.js b/tests/integration/pdfmake/basics.spec.js similarity index 100% rename from tests/pdfmake/basics.spec.js rename to tests/integration/pdfmake/basics.spec.js diff --git a/tests/pdfmake/columns_simple.spec.js b/tests/integration/pdfmake/columns_simple.spec.js similarity index 100% rename from tests/pdfmake/columns_simple.spec.js rename to tests/integration/pdfmake/columns_simple.spec.js diff --git a/tests/pdfmake/images.spec.js b/tests/integration/pdfmake/images.spec.js similarity index 99% rename from tests/pdfmake/images.spec.js rename to tests/integration/pdfmake/images.spec.js index f3ce09dcb..274742c80 100644 --- a/tests/pdfmake/images.spec.js +++ b/tests/integration/pdfmake/images.spec.js @@ -1,5 +1,5 @@ var {runDocTest} = require('../helpers'); -var {jpeg: bee} = require('../images/bee'); +var {jpeg: bee} = require('../../images/bee'); describe('pdfmake', function () { test('images', function () { diff --git a/tests/pdfmake/lists.spec.js b/tests/integration/pdfmake/lists.spec.js similarity index 100% rename from tests/pdfmake/lists.spec.js rename to tests/integration/pdfmake/lists.spec.js diff --git a/tests/pdfmake/page_references.spec.js b/tests/integration/pdfmake/page_references.spec.js similarity index 100% rename from tests/pdfmake/page_references.spec.js rename to tests/integration/pdfmake/page_references.spec.js diff --git a/tests/pdfmake/qrcode.spec.js b/tests/integration/pdfmake/qrcode.spec.js similarity index 100% rename from tests/pdfmake/qrcode.spec.js rename to tests/integration/pdfmake/qrcode.spec.js diff --git a/tests/pdfmake/tables.spec.js b/tests/integration/pdfmake/tables.spec.js similarity index 100% rename from tests/pdfmake/tables.spec.js rename to tests/integration/pdfmake/tables.spec.js diff --git a/tests/pdfmake/text_decorations.spec.js b/tests/integration/pdfmake/text_decorations.spec.js similarity index 100% rename from tests/pdfmake/text_decorations.spec.js rename to tests/integration/pdfmake/text_decorations.spec.js diff --git a/tests/pdfmake/toc.spec.js b/tests/integration/pdfmake/toc.spec.js similarity index 100% rename from tests/pdfmake/toc.spec.js rename to tests/integration/pdfmake/toc.spec.js diff --git a/tests/pdfmake/watermark.spec.js b/tests/integration/pdfmake/watermark.spec.js similarity index 100% rename from tests/pdfmake/watermark.spec.js rename to tests/integration/pdfmake/watermark.spec.js diff --git a/tests/pdfmake/security.spec.js b/tests/integration/security.spec.js similarity index 95% rename from tests/pdfmake/security.spec.js rename to tests/integration/security.spec.js index 5fb9fbde4..6a621194f 100644 --- a/tests/pdfmake/security.spec.js +++ b/tests/integration/security.spec.js @@ -1,5 +1,5 @@ -var {runDocTest} = require('../helpers'); -var PDFDocument = require('../..'); +var {runDocTest} = require('./helpers'); +var PDFDocument = require('../../lib/document').default; var CryptoJS = require('crypto-js'); describe('pdfmake', function () { diff --git a/tests/text.spec.js b/tests/integration/text.spec.js similarity index 100% rename from tests/text.spec.js rename to tests/integration/text.spec.js diff --git a/tests/vector.spec.js b/tests/integration/vector.spec.js similarity index 98% rename from tests/vector.spec.js rename to tests/integration/vector.spec.js index 62d57c47a..4a3161215 100644 --- a/tests/vector.spec.js +++ b/tests/integration/vector.spec.js @@ -1,5 +1,5 @@ var {runDocTest} = require('./helpers'); -var tiger = require('../demo/tiger') +var tiger = require('../../demo/tiger') describe('vector', function () { test('simple shapes', function () { diff --git a/tests/unit/color.spec.js b/tests/unit/color.spec.js index 6e55cf0c5..8f7cbf395 100644 --- a/tests/unit/color.spec.js +++ b/tests/unit/color.spec.js @@ -1,8 +1,8 @@ -var PDFDocument = require("../../"); +const PDFDocument = require("../../lib/document").default; describe("color", function() { test("normalize", function() { - var doc = new PDFDocument(); + const doc = new PDFDocument(); expect(doc._normalizeColor("#FFF")).toEqual([1, 1, 1]); expect(doc._normalizeColor("#FFFFFF")).toEqual([1, 1, 1]); diff --git a/tests/unit/reference.spec.js b/tests/unit/reference.spec.js index e72878d51..930533be4 100644 --- a/tests/unit/reference.spec.js +++ b/tests/unit/reference.spec.js @@ -1,4 +1,5 @@ -const PDFDocument = require('../../') +const PDFReference = require("../../lib/reference").default; +const PDFDocument = require("../../lib/document").default; const zlib = require('zlib') describe('PDFReference', () => { @@ -8,7 +9,7 @@ describe('PDFReference', () => { }) test('instantiated without data', () => { - const ref = document.ref() + const ref = new PDFReference(document, 1); expect(ref.id).toBeDefined() expect(ref.data).toBeDefined() @@ -17,20 +18,20 @@ describe('PDFReference', () => { test('instantiated with data', () => { const refData = {Pages: 0} - const ref = document.ref(refData) + const ref = new PDFReference(document, 1, refData); - expect(ref.id).toBeDefined() + expect(ref.id).toBe(1) expect(ref.data).toBe(refData) }) test('written data of empty reference', (done) => { const dataLog = [] const expected = [ - '7 0 obj', + '1 0 obj', '<<\n>>', 'endobj' ] - const ref = document.ref() + const ref = new PDFReference(document, 1); document._write = function(data) { dataLog.push(data) } @@ -45,7 +46,7 @@ describe('PDFReference', () => { const dataLog = [] const chunk = new Buffer('test') const expected = [ - '7 0 obj', + '1 0 obj', `<< /Length ${chunk.length} >>`, @@ -54,7 +55,7 @@ describe('PDFReference', () => { '\nendstream', 'endobj' ] - const ref = document.ref() + const ref = new PDFReference(document, 1); ref.compress = false ref.write(chunk) document._write = function(data) { @@ -72,7 +73,7 @@ describe('PDFReference', () => { const chunk = new Buffer('test') const compressed = zlib.deflateSync(chunk); const expected = [ - '7 0 obj', + '1 0 obj', `<< /Length ${compressed.length} /Filter /FlateDecode @@ -82,7 +83,7 @@ describe('PDFReference', () => { '\nendstream', 'endobj' ] - const ref = document.ref() + const ref = new PDFReference(document, 1); ref.write(chunk) document._write = function(data) { dataLog.push(data) diff --git a/tests/unit/trailer.spec.js b/tests/unit/trailer.spec.js index d485ebe7c..f35023879 100644 --- a/tests/unit/trailer.spec.js +++ b/tests/unit/trailer.spec.js @@ -1,4 +1,10 @@ -const PDFDocument = require('../../'); +const PDFDocument = require('../../lib/document').default; +const PDFSecurity = require('../../lib/security').default; + +// manual mock for PDFSecurity to ensure stored id will be the same accross different systems +PDFSecurity.generateFileID = () => { + return new Buffer('mocked-pdf-id'); +} describe('Document trailer', () => { let document; @@ -28,7 +34,7 @@ describe('Document trailer', () => { ], [ 'trailer', - `<<\n/Size 11\n/Root 2 0 R\n/Info 7 0 R\n/ID [<8c72cf48ff87daac57e26bf1550e6979> <8c72cf48ff87daac57e26bf1550e6979>]\n>>` + `<<\n/Size 11\n/Root 2 0 R\n/Info 7 0 R\n/ID [<6d6f636b65642d7064662d6964> <6d6f636b65642d7064662d6964>]\n>>` ] ]; document._write = function(data) {