From f29564b69b91aed03ea59039bdf81ce64919cbbf Mon Sep 17 00:00:00 2001 From: Abdirahim Musse <33973272+abmusse@users.noreply.github.com> Date: Thu, 23 Jul 2020 16:49:55 -0500 Subject: [PATCH 1/2] refactor: Handle optional dependencies better --- .eslintrc.js | 3 +++ lib/transports/idbTransport.js | 31 +++++++++++++++++-------------- lib/transports/odbcTransport.js | 15 +++++++++++---- lib/transports/sshTransport.js | 6 ++---- 4 files changed, 33 insertions(+), 22 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 6c58a645..626868ca 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -10,6 +10,9 @@ module.exports = { // Tweak rules set by airbnb config // We need to allow use of console.log for verbose mode 'no-console': 'off', + // airbnb config forbids optionalDependencies + // https://github.com/airbnb/javascript/blob/c5bee75b1b358a3749f1a6d38ee6fad73de28e29/packages/eslint-config-airbnb-base/rules/imports.js#L95 + "import/no-extraneous-dependencies": [ "error", { "optionalDependencies": true }] }, overrides: [ { diff --git a/lib/transports/idbTransport.js b/lib/transports/idbTransport.js index 979fec24..398e6e84 100644 --- a/lib/transports/idbTransport.js +++ b/lib/transports/idbTransport.js @@ -15,15 +15,18 @@ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -const idbCall = (config, xmlInput, cb) => { - const { - dbconn, dbstmt, IN, CLOB, CHAR, SQL_ATTR_DBC_SYS_NAMING, SQL_FALSE, - // idb-connector is an optional dependency, since users may not use this transport - // thus we can't globally require it - // eslint-disable-next-line max-len - // eslint-disable-next-line global-require, import/no-extraneous-dependencies, import/no-unresolved - } = require('idb-connector'); +let idb = null; +try { + // eslint-disable-next-line global-require + idb = require('idb-connector'); +} catch (e) { + if (e.code !== 'MODULE_NOT_FOUND') { + throw e; + } +} + +const idbCall = (config, xmlInput, cb) => { const { database = '*LOCAL', username = null, @@ -37,9 +40,9 @@ const idbCall = (config, xmlInput, cb) => { let xmlOutput = ''; const sql = `call ${xslib}.iPLUGR512K(?,?,?)`; // eslint-disable-next-line new-cap - const conn = new dbconn(); + const conn = new idb.dbconn(); - conn.setConnAttr(SQL_ATTR_DBC_SYS_NAMING, SQL_FALSE); + conn.setConnAttr(idb.SQL_ATTR_DBC_SYS_NAMING, idb.SQL_FALSE); if (typeof verbose === 'boolean') { conn.debug(verbose); @@ -56,12 +59,12 @@ const idbCall = (config, xmlInput, cb) => { return; } // eslint-disable-next-line new-cap - const stmt = new dbstmt(conn); + const stmt = new idb.dbstmt(conn); const parameters = [ - [ipc, IN, CHAR], - [ctl, IN, CHAR], - [xmlInput, IN, CLOB], + [ipc, idb.IN, idb.CHAR], + [ctl, idb.IN, idb.CHAR], + [xmlInput, idb.IN, idb.CLOB], ]; // Before returning to caller, we must clean up diff --git a/lib/transports/odbcTransport.js b/lib/transports/odbcTransport.js index b5592727..7786a9f4 100644 --- a/lib/transports/odbcTransport.js +++ b/lib/transports/odbcTransport.js @@ -15,11 +15,18 @@ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -function odbcCall(config, xmlInput, done) { - // odbc is an optional dependency, since users may not use this transport - // eslint-disable-next-line global-require, import/no-extraneous-dependencies - const odbc = require('odbc'); +let odbc = null; +try { + // eslint-disable-next-line global-require + odbc = require('odbc'); +} catch (e) { + if (e.code !== 'MODULE_NOT_FOUND') { + throw e; + } +} + +function odbcCall(config, xmlInput, done) { const { host = 'localhost', username = null, diff --git a/lib/transports/sshTransport.js b/lib/transports/sshTransport.js index 243e264e..450feaf1 100644 --- a/lib/transports/sshTransport.js +++ b/lib/transports/sshTransport.js @@ -15,11 +15,9 @@ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -function sshCall(config, xmlIn, done) { - // ssh2 is an optional dependency, since users may not use this transport - // eslint-disable-next-line global-require, import/no-extraneous-dependencies - const { Client } = require('ssh2'); +const { Client } = require('ssh2'); +function sshCall(config, xmlIn, done) { const { verbose = false, } = config; From eba84b6fffcd87b3afe4f35dbce8c44251287f33 Mon Sep 17 00:00:00 2001 From: Abdirahim Musse <33973272+abmusse@users.noreply.github.com> Date: Tue, 8 Dec 2020 10:05:26 -0600 Subject: [PATCH 2/2] Remove arrow function syntax for transport calls --- lib/transports/httpTransport.js | 4 ++-- lib/transports/idbTransport.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/transports/httpTransport.js b/lib/transports/httpTransport.js index 07fdaf95..72cc4d21 100644 --- a/lib/transports/httpTransport.js +++ b/lib/transports/httpTransport.js @@ -17,7 +17,7 @@ const http = require('http'); -const httpCall = (config, xmlInput, done) => { +function httpCall(config, xmlInput, done) { const { database = '*LOCAL', username = null, @@ -85,6 +85,6 @@ const httpCall = (config, xmlInput, done) => { }); request.write(queryString); request.end(); -}; +} exports.httpCall = httpCall; diff --git a/lib/transports/idbTransport.js b/lib/transports/idbTransport.js index 398e6e84..e421a458 100644 --- a/lib/transports/idbTransport.js +++ b/lib/transports/idbTransport.js @@ -26,7 +26,7 @@ try { } } -const idbCall = (config, xmlInput, cb) => { +function idbCall(config, xmlInput, cb) { const { database = '*LOCAL', username = null, @@ -112,6 +112,6 @@ const idbCall = (config, xmlInput, cb) => { }); }); }); -}; +} exports.idbCall = idbCall;