Skip to content

Commit

Permalink
refactor: tighten up cloudflare detection
Browse files Browse the repository at this point in the history
The previous approach to detecting whether to use Cloudflare's sockets was to check for missing polyfills.
But as we improve the polyfills that Wrangler can provide these checks are no longer valid.

Now we just try to use the Cloudflare API first and fallback to Node.js if those are not available.
  • Loading branch information
petebacondarwin committed Mar 13, 2024
1 parent b4bfd63 commit c5946f3
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions packages/pg/lib/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
* @returns {Duplex}
*/
module.exports.getStream = function getStream(ssl) {
const net = require('net')
if (typeof net.Socket === 'function') {
return new net.Socket()
} else {
try {
const { CloudflareSocket } = require('pg-cloudflare')
return new CloudflareSocket(ssl)
} catch {
const net = require('net')
return new net.Socket()
}
}

Expand All @@ -18,11 +18,11 @@ module.exports.getStream = function getStream(ssl) {
* @returns {Duplex}
*/
module.exports.getSecureStream = function getSecureStream(options) {
var tls = require('tls')
if (tls.connect) {
return tls.connect(options)
} else {
try {
options.socket.startTls(options)
return options.socket
} catch {
var tls = require('tls')
return tls.connect(options)
}
}

0 comments on commit c5946f3

Please sign in to comment.