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 43f1069 commit a0ebc07
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions packages/pg/lib/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,21 @@ module.exports.getSecureStream = function getSecureStream(options) {

/**
* Are we running in a Cloudflare Worker?
*
*
* @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);
}
// Since 2022-03-21 the `global_navigator` compatibility flag is on for Cloudflare Workers
// which means that `navigator.userAgent` will be defined.
if (typeof navigator === 'object' && navigator !== null && typeof navigator.userAgent === 'string') {
return navigator.userAgent === 'Cloudflare Workers'
}
// In case `navigator` or `navigator.userAgent` is not defined then try a more sneaky approach
if (typeof Response === 'function') {
const resp = new Response(null, { cf: { thing: true } })
if (typeof resp.cf === 'object' && resp.cf !== null && resp.cf.thing) {
return true
}
}
return false
}

0 comments on commit a0ebc07

Please sign in to comment.