Skip to content

Commit

Permalink
Only throw "filesystem unsupported" if the fs require fails
Browse files Browse the repository at this point in the history
  • Loading branch information
petebacondarwin committed May 5, 2023
1 parent 78bb6b6 commit 51681d9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 31 deletions.
26 changes: 11 additions & 15 deletions packages/pg-connection-string/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 0 additions & 16 deletions packages/pg-connection-string/test/parse.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

var chai = require('chai')
var fs = require('fs')
var expect = chai.expect
chai.should()

Expand Down Expand Up @@ -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')
})
})

0 comments on commit 51681d9

Please sign in to comment.