From 2b469d01da682753ca4ba84b849092647147ba56 Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Thu, 4 May 2023 15:34:07 +0100 Subject: [PATCH] avoid accessing Node specific requires when not needed --- packages/pg/lib/client.js | 20 ++++++++++++-------- packages/pg/lib/native/client.js | 8 +++++++- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/packages/pg/lib/client.js b/packages/pg/lib/client.js index f2c339d3..b7143e9d 100644 --- a/packages/pg/lib/client.js +++ b/packages/pg/lib/client.js @@ -3,7 +3,6 @@ var EventEmitter = require('events').EventEmitter var utils = require('./utils') var sasl = require('./sasl') -var pgPass = require('pgpass') var TypeOverrides = require('./type-overrides') var ConnectionParameters = require('./connection-parameters') @@ -225,12 +224,17 @@ class Client extends EventEmitter { } else if (this.password !== null) { cb() } else { - pgPass(this.connectionParameters, (pass) => { - if (undefined !== pass) { - this.connectionParameters.password = this.password = pass - } - cb() - }) + try { + const pgPass = require('pgpass') + pgPass(this.connectionParameters, (pass) => { + if (undefined !== pass) { + this.connectionParameters.password = this.password = pass + } + cb() + }) + } catch (e) { + this.emit('error', e) + } } } @@ -457,7 +461,7 @@ class Client extends EventEmitter { } // escapeIdentifier and escapeLiteral moved to utility functions & exported - // on PG + // on PG // re-exported here for backwards compatibility escapeIdentifier(str) { return utils.escapeIdentifier(str) diff --git a/packages/pg/lib/native/client.js b/packages/pg/lib/native/client.js index 58fc4aea..6a8eb536 100644 --- a/packages/pg/lib/native/client.js +++ b/packages/pg/lib/native/client.js @@ -1,7 +1,13 @@ 'use strict' // eslint-disable-next-line -var Native = require('pg-native') +var Native +try { + // Wrap this `require()` in a try-catch to avoid upstream bundlers from complaining that this might not be available since it is an optional import + Native = require('pg-native') +} catch (e) { + throw e +} var TypeOverrides = require('../type-overrides') var EventEmitter = require('events').EventEmitter var util = require('util')