Skip to content

Commit

Permalink
🚨 Lint examples with eslint:recommended
Browse files Browse the repository at this point in the history
Since we're targetting Node 12 (should we go up?), the target ES spec is 2019.
  • Loading branch information
Valeri Buchinski committed Feb 9, 2024
1 parent 1ef1957 commit 6c8c95a
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 55 deletions.
10 changes: 10 additions & 0 deletions packages/examples/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": [
"eslint:recommended"
],
"env": {
"node": true,
"browser": false,
"es2019": true
}
}
5 changes: 3 additions & 2 deletions packages/examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
70 changes: 35 additions & 35 deletions packages/examples/src/javascript.js
Original file line number Diff line number Diff line change
@@ -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();
4 changes: 2 additions & 2 deletions packages/examples/src/pdfkit010-with-visual.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -125,6 +125,6 @@ function work() {
// Finally end the PDFDocument stream.
pdf.end();
// This has just triggered the `pdfReady` Promise to be resolved.
};
}

work();
4 changes: 2 additions & 2 deletions packages/examples/src/pdfkit010.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -74,6 +74,6 @@ function work() {
// Finally end the PDFDocument stream.
pdf.end();
// This has just triggered the `pdfReady` Promise to be resolved.
};
}

work();
15 changes: 8 additions & 7 deletions packages/examples/src/webcrypto-external.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 8 additions & 7 deletions packages/examples/src/webcrypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 6c8c95a

Please sign in to comment.