| 264 | } |
| 265 | |
| 266 | async function secure() { |
| 267 | if (sslnegotiation !== 'direct') { |
| 268 | write(SSLRequest) |
| 269 | const canSSL = await new Promise(r => socket.once('data', x => r(x[0] === 83))) // S |
| 270 | |
| 271 | if (!canSSL && ssl === 'prefer') |
| 272 | return connected() |
| 273 | } |
| 274 | |
| 275 | const options = { |
| 276 | socket, |
| 277 | servername: net.isIP(socket.host) ? undefined : socket.host |
| 278 | } |
| 279 | |
| 280 | if (sslnegotiation === 'direct') |
| 281 | options.ALPNProtocols = ['postgresql'] |
| 282 | |
| 283 | if (ssl === 'require' || ssl === 'allow' || ssl === 'prefer') |
| 284 | options.rejectUnauthorized = false |
| 285 | else if (typeof ssl === 'object') |
| 286 | Object.assign(options, ssl) |
| 287 | |
| 288 | socket.removeAllListeners() |
| 289 | socket = tls.connect(options) |
| 290 | socket.on('secureConnect', connected) |
| 291 | socket.on('error', error) |
| 292 | socket.on('close', closed) |
| 293 | socket.on('drain', drain) |
| 294 | } |
| 295 | |
| 296 | /* c8 ignore next 3 */ |
| 297 | function drain() { |