Skip to content

Commit

Permalink
fixup! refactor: tighten up cloudflare detection
Browse files Browse the repository at this point in the history
  • Loading branch information
petebacondarwin committed Mar 14, 2024
1 parent 079a259 commit 43f1069
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 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) {
if (process.release && process.release.name === 'node') {
const net = require('net')
return new net.Socket()
} else {
if (isCloudflareRuntime) {
const { CloudflareSocket } = require('pg-cloudflare')
return new CloudflareSocket(ssl)
} else {
const net = require('net')
return new net.Socket()
}
}

Expand All @@ -18,11 +18,20 @@ module.exports.getStream = function getStream(ssl) {
* @returns {Duplex}
*/
module.exports.getSecureStream = function getSecureStream(options) {
if (process.release && process.release.name === 'node') {
var tls = require('tls')
return tls.connect(options)
} else {
if (isCloudflareRuntime) {
options.socket.startTls(options)
return options.socket
} else {
var tls = require('tls')
return tls.connect(options)
}
}

/**
* Are we running in a Cloudflare Worker?
*

Check failure on line 32 in packages/pg/lib/stream.js

View workflow job for this annotation

GitHub Actions / lint

Delete `·`

Check failure on line 32 in packages/pg/lib/stream.js

View workflow job for this annotation

GitHub Actions / lint

Delete `·`
* @returns true if the code is currently running inside a Cloudflare Worker.
*/
function isCloudflareRuntime() {
return Boolean(process.release && process.release.name !== 'node' && Response && new Response(null, { cf: { thing: true } }).cf.thing === true);

Check failure on line 36 in packages/pg/lib/stream.js

View workflow job for this annotation

GitHub Actions / lint

Replace `process.release·&&·process.release.name·!==·'node'·&&·Response·&&·new·Response(null,·{·cf:·{·thing:·true·}·}).cf.thing·===·true);` with `⏎····process.release·&&⏎······process.release.name·!==·'node'·&&⏎······Response·&&⏎······new·Response(null,·{·cf:·{·thing:·true·}·}).cf.thing·===·true⏎··)`

Check failure on line 36 in packages/pg/lib/stream.js

View workflow job for this annotation

GitHub Actions / lint

Replace `process.release·&&·process.release.name·!==·'node'·&&·Response·&&·new·Response(null,·{·cf:·{·thing:·true·}·}).cf.thing·===·true);` with `⏎····process.release·&&⏎······process.release.name·!==·'node'·&&⏎······Response·&&⏎······new·Response(null,·{·cf:·{·thing:·true·}·}).cf.thing·===·true⏎··)`
}

Check failure on line 37 in packages/pg/lib/stream.js

View workflow job for this annotation

GitHub Actions / lint

Insert `⏎`

Check failure on line 37 in packages/pg/lib/stream.js

View workflow job for this annotation

GitHub Actions / lint

Insert `⏎`

0 comments on commit 43f1069

Please sign in to comment.