From a44465b661d9f497c14e3b7b58e42e9774cce52a Mon Sep 17 00:00:00 2001 From: Paul Paterson Date: Wed, 12 Jul 2023 21:59:24 -0400 Subject: [PATCH] fix: Explicitly check for browser env (#178) --- src/util/environment.ts | 73 ++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 26 deletions(-) diff --git a/src/util/environment.ts b/src/util/environment.ts index 4ac1c3be..b35fa3b5 100644 --- a/src/util/environment.ts +++ b/src/util/environment.ts @@ -19,25 +19,55 @@ export const getDriverEnv = (): string => { runtime: "unknown", }; - let isServiceWorker = false; try { - isServiceWorker = global instanceof ServiceWorkerGlobalScope; - } catch (_) { - // okay if this fails - } + /** + * Determine if we're executing in a Node environment + */ + const isNode = + typeof window === "undefined" && + typeof process !== "undefined" && + process.versions != null && + process.versions.node != null; + + /** + * Determine if we're executing in a Node environment + */ + const isBrowser = + typeof window !== "undefined" && typeof window.document !== "undefined"; + + /** + * Determine if we're executing in a Service Worker environment + */ + const isServiceWorker = + typeof self === "object" && + self.constructor && + self.constructor.name === "DedicatedWorkerGlobalScope"; + + /** + * Determine if we're executing in Vercel's Edge Runtime + * @see {@link https://vercel.com/docs/concepts/functions/edge-functions/edge-runtime#check-if-you're-running-on-the-edge-runtime} + */ + // @ts-expect-error Cannot find name 'EdgeRuntime' + const isVercelEdgeRuntime = typeof EdgeRuntime !== "string"; - if (isNode()) { - driverEnv.runtime = ["nodejs", process.version].join("-"); - driverEnv.env = getNodeRuntimeEnv(); - driverEnv.os = [os.platform(), os.release()].join("-"); - } else if (isServiceWorker) { - driverEnv.runtime = getBrowserDetails(navigator); - driverEnv.env = "Service Worker"; - driverEnv.os = getBrowserOsDetails(navigator); - } else { - driverEnv.runtime = getBrowserDetails(navigator); - driverEnv.env = "browser"; - driverEnv.os = getBrowserOsDetails(navigator); + if (isNode) { + driverEnv.runtime = ["nodejs", process.version].join("-"); + driverEnv.env = getNodeRuntimeEnv(); + driverEnv.os = [os.platform(), os.release()].join("-"); + } else if (isServiceWorker) { + driverEnv.runtime = getBrowserDetails(navigator); + driverEnv.env = "Service Worker"; + driverEnv.os = getBrowserOsDetails(navigator); + } else if (isBrowser) { + driverEnv.runtime = getBrowserDetails(navigator); + driverEnv.env = "browser"; + driverEnv.os = getBrowserOsDetails(navigator); + } else if (isVercelEdgeRuntime) { + driverEnv.runtime = "Vercel Edge Runtime"; + driverEnv.env = "edge"; + } + } catch (e) { + // ignore errors trying to report on user environment } return ( @@ -49,15 +79,6 @@ export const getDriverEnv = (): string => { ); }; -/** - * Predicate to determine if we're executing in a Node environment or not - */ -const isNode = (): boolean => - typeof window === "undefined" && - typeof process !== "undefined" && - process.versions != null && - process.versions.node != null; - /** * Get browser environment details */