I use the promise wrapper for my implementation and keep it async (ES2015).
Library /pfx.js
import { readFile } from 'fs' import { resolve as resolvePath } from 'path' export const CERTIFICATE_ROOT = resolvePath(__dirname, '..', 'etc', 'certificates') export const getCertificatePath = filename => resolvePath(CERTIFICATE_ROOT, filename) export function readCertificate(filename) { let certificatePath = getCertificatePath(filename) return new Promise((resolve, reject) => { readFile(certificatePath, (err, certificate) => { if (err) return reject(err) resolve(certificate) }) }) } export function readPfx(filename, passphrase) { assert.typeOf(passphrase, 'string', 'passphrase must be a string') assert.isAbove(passphrase.length, 0, 'passphrase must not be empty') return readCertificate(filename).then(pfx => ({ pfx, passphrase })) }
and use
Library /app.js
import { readPfx } from './pfx' readPfx('8ab20f7b-51b9-4c09-a2e0-1918bb9fb37f.pfx', process.env.PASSPHRASE) .then(opts => ) .catch(err => )
source share