Skip to content

Commit

Permalink
avoid accessing Node specific requires when not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
petebacondarwin committed May 15, 2023
1 parent f305419 commit 2b469d0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
20 changes: 12 additions & 8 deletions packages/pg/lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)
}
}
}

Expand Down Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion packages/pg/lib/native/client.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down

0 comments on commit 2b469d0

Please sign in to comment.