From e2c7edefe38e9098ee3b2fc6d8a0b7751f05d6d9 Mon Sep 17 00:00:00 2001 From: Abhishek Mathur Date: Sun, 12 Dec 2021 12:43:31 +0530 Subject: [PATCH] Use truly async fs operations instead of sync event loop blocking io It does not make any sense to use sync io functions inside a Promise as it will anyway block the event loop without any benefit Async fs module functions inside Promise will help to asynchonously load keys giving better performance --- index.js | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 2ecd2e7..6a8e9eb 100644 --- a/index.js +++ b/index.js @@ -47,20 +47,14 @@ class JWTToken { * @param {string} publicCertPath - path to public certificate * @return {object} promise - */ - loadCerts(privateCertPath, publicCertPath) { - return new Promise((resolve, reject) => { - try { - if (privateCertPath) { - this.privateCert = fs.readFileSync(privateCertPath); - } - if (publicCertPath) { - this.publicCert = fs.readFileSync(publicCertPath); - } - resolve(true); - } catch (e) { - reject(e); - } - }); + async loadCerts(privateCertPath, publicCertPath) { + if (privateCertPath) { + this.privateCert = await fs.readFile(privateCertPath); + } + if (publicCertPath) { + this.publicCert = await fs.readFile(publicCertPath); + } + return true; } /**