From a0ebc0722468c6422572ddf5792205de3cec5cd8 Mon Sep 17 00:00:00 2001 From: Peter Bacon Darwin Date: Thu, 14 Mar 2024 12:12:29 +0000 Subject: [PATCH] fixup! refactor: tighten up cloudflare detection --- packages/pg/lib/stream.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/pg/lib/stream.js b/packages/pg/lib/stream.js index 219956c3f..8bbffd6ca 100644 --- a/packages/pg/lib/stream.js +++ b/packages/pg/lib/stream.js @@ -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); -} \ No newline at end of file + // 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 +}