From 6c8c95a9a59632051e58c7bbc28cb82ebc9b2a33 Mon Sep 17 00:00:00 2001 From: Valeri Buchinski Date: Fri, 9 Feb 2024 11:52:23 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A8=20Lint=20examples=20with=20eslint:?= =?UTF-8?q?recommended?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since we're targetting Node 12 (should we go up?), the target ES spec is 2019. --- packages/examples/.eslintrc | 10 +++ packages/examples/package.json | 5 +- packages/examples/src/javascript.js | 70 +++++++++---------- .../examples/src/pdfkit010-with-visual.js | 4 +- packages/examples/src/pdfkit010.js | 4 +- packages/examples/src/webcrypto-external.js | 15 ++-- packages/examples/src/webcrypto.js | 15 ++-- 7 files changed, 68 insertions(+), 55 deletions(-) create mode 100644 packages/examples/.eslintrc diff --git a/packages/examples/.eslintrc b/packages/examples/.eslintrc new file mode 100644 index 00000000..c01ce84c --- /dev/null +++ b/packages/examples/.eslintrc @@ -0,0 +1,10 @@ +{ + "extends": [ + "eslint:recommended" + ], + "env": { + "node": true, + "browser": false, + "es2019": true + } +} \ No newline at end of file diff --git a/packages/examples/package.json b/packages/examples/package.json index 246de974..74e8866f 100644 --- a/packages/examples/package.json +++ b/packages/examples/package.json @@ -5,8 +5,9 @@ "description": "", "scripts": { "js": "node src/javascript.js", - "ts": "ts-node src/typescript.ts" - }, + "ts": "ts-node src/typescript.ts", + "lint": "eslint -c .eslintrc --ignore-path ../../.eslintignore ./" +}, "engines": { "node": ">=12" }, diff --git a/packages/examples/src/javascript.js b/packages/examples/src/javascript.js index a87f4712..a755cfa3 100644 --- a/packages/examples/src/javascript.js +++ b/packages/examples/src/javascript.js @@ -1,36 +1,36 @@ -var fs = require('fs'); -var path = require('path'); -var plainAddPlaceholder = require('@signpdf/placeholder-plain').plainAddPlaceholder -var signpdf = require('@signpdf/signpdf').default; -var P12Signer = require('@signpdf/signer-p12').P12Signer; - -function work() { - // contributing.pdf is the file that is going to be signed - var sourcePath = path.join(__dirname, '/../../../resources/contributing.pdf'); - var pdfBuffer = fs.readFileSync(sourcePath); - - // certificate.p12 is the certificate that is going to be used to sign - var certificatePath = path.join(__dirname, '/../../../resources/certificate.p12'); - var certificateBuffer = fs.readFileSync(certificatePath); - var signer = new P12Signer(certificateBuffer); - - // The PDF needs to have a placeholder for a signature to be signed. - var pdfWithPlaceholder = plainAddPlaceholder({ - pdfBuffer: pdfBuffer, - reason: 'The user is decalaring consent through JavaScript.', - contactInfo: 'signpdf@example.com', - name: 'John Doe', - location: 'Free Text Str., Free World', - }); - - // pdfWithPlaceholder is now a modified buffer that is ready to be signed. - signpdf - .sign(pdfWithPlaceholder, signer) - .then(function (signedPdf) { - // signedPdf is a Buffer of an electronically signed PDF. Store it. - var targetPath = path.join(__dirname, '/../output/javascript.pdf'); - fs.writeFileSync(targetPath, signedPdf); - }) -} - +var fs = require('fs'); +var path = require('path'); +var plainAddPlaceholder = require('@signpdf/placeholder-plain').plainAddPlaceholder; +var signpdf = require('@signpdf/signpdf').default; +var P12Signer = require('@signpdf/signer-p12').P12Signer; + +function work() { + // contributing.pdf is the file that is going to be signed + var sourcePath = path.join(__dirname, '/../../../resources/contributing.pdf'); + var pdfBuffer = fs.readFileSync(sourcePath); + + // certificate.p12 is the certificate that is going to be used to sign + var certificatePath = path.join(__dirname, '/../../../resources/certificate.p12'); + var certificateBuffer = fs.readFileSync(certificatePath); + var signer = new P12Signer(certificateBuffer); + + // The PDF needs to have a placeholder for a signature to be signed. + var pdfWithPlaceholder = plainAddPlaceholder({ + pdfBuffer: pdfBuffer, + reason: 'The user is decalaring consent through JavaScript.', + contactInfo: 'signpdf@example.com', + name: 'John Doe', + location: 'Free Text Str., Free World', + }); + + // pdfWithPlaceholder is now a modified buffer that is ready to be signed. + signpdf + .sign(pdfWithPlaceholder, signer) + .then(function (signedPdf) { + // signedPdf is a Buffer of an electronically signed PDF. Store it. + var targetPath = path.join(__dirname, '/../output/javascript.pdf'); + fs.writeFileSync(targetPath, signedPdf); + }) +} + work(); \ No newline at end of file diff --git a/packages/examples/src/pdfkit010-with-visual.js b/packages/examples/src/pdfkit010-with-visual.js index 0dd46ba7..2f7c6b2b 100644 --- a/packages/examples/src/pdfkit010-with-visual.js +++ b/packages/examples/src/pdfkit010-with-visual.js @@ -51,7 +51,7 @@ function work() { size: 'A4', layout: 'portrait', bufferPages: true, - });; + }); pdf.info.CreationDate = ''; // At the end we want to convert the PDFKit to a string/Buffer and store it in a file. @@ -125,6 +125,6 @@ function work() { // Finally end the PDFDocument stream. pdf.end(); // This has just triggered the `pdfReady` Promise to be resolved. -}; +} work(); \ No newline at end of file diff --git a/packages/examples/src/pdfkit010.js b/packages/examples/src/pdfkit010.js index 7f62eebc..6ef0b130 100644 --- a/packages/examples/src/pdfkit010.js +++ b/packages/examples/src/pdfkit010.js @@ -12,7 +12,7 @@ function work() { size: 'A4', layout: 'portrait', bufferPages: true, - });; + }); pdf.info.CreationDate = ''; // At the end we want to convert the PDFKit to a string/Buffer and store it in a file. @@ -74,6 +74,6 @@ function work() { // Finally end the PDFDocument stream. pdf.end(); // This has just triggered the `pdfReady` Promise to be resolved. -}; +} work(); \ No newline at end of file diff --git a/packages/examples/src/webcrypto-external.js b/packages/examples/src/webcrypto-external.js index ba795662..d3a29368 100644 --- a/packages/examples/src/webcrypto-external.js +++ b/packages/examples/src/webcrypto-external.js @@ -11,21 +11,22 @@ var createCertificate = require('./utils').createCertificate; // WebCrypto signing can also be implemented more easily by subclassing the Signer abstract // class directly, as is done in the `webcrypto.js` example script. class CryptoSigner extends ExternalSigner { - // 'SHA-256', 'SHA-384' or 'SHA-512' are supported by webcrypto - supportedHashAlgorithms = ['SHA-256', 'SHA-384', 'SHA-512']; - - // 'RSASSA-PKCS1-v1_5', 'RSA-PSS' or 'ECDSA' are supported by webcrypto - supportedSignAlgorithms = ['RSASSA-PKCS1-v1_5', 'RSA-PSS', 'ECDSA']; constructor(signAlgorithm = 'ECDSA', hashAlgorithm = 'SHA-512') { super(); + // 'SHA-256', 'SHA-384' or 'SHA-512' are supported by webcrypto + var supportedHashAlgorithms = ['SHA-256', 'SHA-384', 'SHA-512']; + + // 'RSASSA-PKCS1-v1_5', 'RSA-PSS' or 'ECDSA' are supported by webcrypto + var supportedSignAlgorithms = ['RSASSA-PKCS1-v1_5', 'RSA-PSS', 'ECDSA']; + // Verify and set signature and hash algorithms - if (!this.supportedSignAlgorithms.includes(signAlgorithm)) { + if (!supportedSignAlgorithms.includes(signAlgorithm)) { throw new Error(`Signature algorithm ${signAlgorithm} is not supported by WebCrypto.`); } this.signAlgorithm = signAlgorithm; - if (!this.supportedHashAlgorithms.includes(hashAlgorithm)) { + if (!supportedHashAlgorithms.includes(hashAlgorithm)) { throw new Error(`Hash algorithm ${hashAlgorithm} is not supported by WebCrypto.`); } this.hashAlgorithm = hashAlgorithm; diff --git a/packages/examples/src/webcrypto.js b/packages/examples/src/webcrypto.js index 8d4a089c..8f2ff7af 100644 --- a/packages/examples/src/webcrypto.js +++ b/packages/examples/src/webcrypto.js @@ -7,21 +7,22 @@ var createCertificate = require('./utils').createCertificate; // Signer implementation using the WebCrypto API class CryptoSigner extends Signer { - // 'SHA-256', 'SHA-384' or 'SHA-512' are supported by webcrypto - supportedHashAlgorithms = ['SHA-256', 'SHA-384', 'SHA-512']; - - // 'RSASSA-PKCS1-v1_5', 'RSA-PSS' or 'ECDSA' are supported by webcrypto - supportedSignAlgorithms = ['RSASSA-PKCS1-v1_5', 'RSA-PSS', 'ECDSA']; constructor(signAlgorithm = 'RSA-PSS', hashAlgorithm = 'SHA-512') { super(); + // 'SHA-256', 'SHA-384' or 'SHA-512' are supported by webcrypto + var supportedHashAlgorithms = ['SHA-256', 'SHA-384', 'SHA-512']; + + // 'RSASSA-PKCS1-v1_5', 'RSA-PSS' or 'ECDSA' are supported by webcrypto + var supportedSignAlgorithms = ['RSASSA-PKCS1-v1_5', 'RSA-PSS', 'ECDSA']; + // Verify and set signature and hash algorithms - if (!this.supportedSignAlgorithms.includes(signAlgorithm)) { + if (!supportedSignAlgorithms.includes(signAlgorithm)) { throw new Error(`Signature algorithm ${signAlgorithm} is not supported by WebCrypto.`); } this.signAlgorithm = signAlgorithm; - if (!this.supportedHashAlgorithms.includes(hashAlgorithm)) { + if (!supportedHashAlgorithms.includes(hashAlgorithm)) { throw new Error(`Hash algorithm ${hashAlgorithm} is not supported by WebCrypto.`); } this.hashAlgorithm = hashAlgorithm;