diff --git a/packages/pg-connection-string/index.js b/packages/pg-connection-string/index.js index a5a0fe193..e02567525 100644 --- a/packages/pg-connection-string/index.js +++ b/packages/pg-connection-string/index.js @@ -68,23 +68,19 @@ function parse(str) { config.ssl = {} } - try { - if (config.sslcert) { - const fs = require('fs') - config.ssl.cert = fs.readFileSync(config.sslcert).toString() - } + // Only try to load fs if we expect to read from the disk + const fs = config.sslcert || config.sslkey || config.sslrootcert ? require('fs') : null - if (config.sslkey) { - const fs = require('fs') - config.ssl.key = fs.readFileSync(config.sslkey).toString() - } + if (config.sslcert) { + config.ssl.cert = fs.readFileSync(config.sslcert).toString() + } - if (config.sslrootcert) { - const fs = require('fs') - config.ssl.ca = fs.readFileSync(config.sslrootcert).toString() - } - } catch (e) { - throw new Error('filesystem not supported') + if (config.sslkey) { + config.ssl.key = fs.readFileSync(config.sslkey).toString() + } + + if (config.sslrootcert) { + config.ssl.ca = fs.readFileSync(config.sslrootcert).toString() } switch (config.sslmode) { diff --git a/packages/pg-connection-string/test/parse.js b/packages/pg-connection-string/test/parse.js index 92dfcddda..772a8a0df 100644 --- a/packages/pg-connection-string/test/parse.js +++ b/packages/pg-connection-string/test/parse.js @@ -1,7 +1,6 @@ 'use strict' var chai = require('chai') -var fs = require('fs') var expect = chai.expect chai.should() @@ -318,19 +317,4 @@ describe('parse', function () { var subject = parse(connectionString) subject.keepalives.should.equal('0') }) - - it('should fail gracefully if there is no filesystem', function () { - const og_readFileSync = fs.readFileSync - fs.readFileSync = null - let error - try { - var connectionString = 'pg:///?sslcert=' + __dirname + '/example.cert' - parse(connectionString) - } catch (e) { - error = e - } finally { - fs.readFileSync = og_readFileSync - } - error.message.should.equal('filesystem not supported') - }) })