Skip to content

Commit

Permalink
Slight (internal?) API change: the SSL handling is entirely encapsula…
Browse files Browse the repository at this point in the history
…ted in the Connection class. Kept the client.ssl prop for now, as that is technically on the public API, but undocumented.
  • Loading branch information
boromisp committed May 12, 2024
1 parent 0096856 commit 913967f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
17 changes: 1 addition & 16 deletions packages/pg/lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,8 @@ class Client extends EventEmitter {
this.binary = c.binary || defaults.binary
this.processID = null
this.secretKey = null
// TODO: remove in next major release?
this.ssl = this.connectionParameters.ssl || false
// As with Password, make SSL->Key (the private key) non-enumerable.
// It won't show up in stack traces
// or if the client is console.logged
if (this.ssl && this.ssl.key) {
Object.defineProperty(this.ssl, 'key', {
enumerable: false,
})
}

this._connectionTimeoutMillis = c.connectionTimeoutMillis || 0
}
Expand Down Expand Up @@ -115,14 +108,6 @@ class Client extends EventEmitter {

// once connection is established send startup message
con.on('connect', function () {
if (self.ssl) {
con.requestSsl()
} else {
con.startup(self.getStartupConf())
}
})

con.on('sslconnect', function () {
con.startup(self.getStartupConf())
})

Expand Down
17 changes: 15 additions & 2 deletions packages/pg/lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ class Connection extends EventEmitter {
super()
config = config || {}

// As with Password, make SSL->Key (the private key) non-enumerable.
// It won't show up in stack traces
// or if the client is console.logged
if (config.ssl && config.ssl.key) {
Object.defineProperty(config.ssl, 'key', {
enumerable: false,
})
}

this.stream = config.stream || getStream(config.ssl)
if (typeof this.stream === 'function') {
this.stream = this.stream(config)
Expand Down Expand Up @@ -47,7 +56,11 @@ class Connection extends EventEmitter {
if (self._keepAlive) {
self.stream.setKeepAlive(true, self._keepAliveInitialDelayMillis)
}
self.emit('connect')
if (self.ssl) {
self.requestSsl()
} else {
self.emit('connect')
}
})

const reportStreamError = function (error) {
Expand Down Expand Up @@ -104,7 +117,7 @@ class Connection extends EventEmitter {
self.attachListeners(self.stream)
self.stream.on('error', reportStreamError)

self.emit('sslconnect')
self.emit('connect')
})
}

Expand Down
2 changes: 1 addition & 1 deletion packages/pg/test/unit/connection/error-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var SSLNegotiationPacketTests = [
testName: 'connection does not emit ECONNRESET errors during disconnect also when using SSL',
errorMessage: null,
response: 'S',
responseType: 'sslconnect',
responseType: 'connect',
},
{
testName: 'connection emits an error when SSL is not supported',
Expand Down

0 comments on commit 913967f

Please sign in to comment.