@@ -14,6 +14,7 @@ import {
1414 KubernetesEnvironmentVariable ,
1515 NodeJSDevEnvironmentVariableValue ,
1616 NodeJSEnvironmentVariable ,
17+ RequestTracingDisabledEnvironmentVariable ,
1718 RequestType ,
1819 RequestTypeKey ,
1920 ServiceFabricEnvironmentVariable
@@ -53,26 +54,65 @@ export function createCorrelationContextHeader(options: AzureAppConfigurationOpt
5354 return contextParts . join ( "," ) ;
5455}
5556
57+ export function requestTracingEnabled ( ) : boolean {
58+ const requestTracingDisabledEnv = getEnvironmentVariable ( RequestTracingDisabledEnvironmentVariable ) ;
59+ const disabled = requestTracingDisabledEnv ?. toLowerCase ( ) === "true" ;
60+ return ! disabled ;
61+ }
62+
63+ function getEnvironmentVariable ( name : string ) {
64+ // Make it compatible with non-Node.js runtime
65+ if ( typeof process ?. env === "object" ) {
66+ return process . env [ name ] ;
67+ } else {
68+ return undefined ;
69+ }
70+ }
71+
5672function getHostType ( ) : string | undefined {
5773 let hostType : string | undefined ;
58- if ( process . env [ AzureFunctionEnvironmentVariable ] ) {
74+ if ( getEnvironmentVariable ( AzureFunctionEnvironmentVariable ) ) {
5975 hostType = HostType . AzureFunction ;
60- } else if ( process . env [ AzureWebAppEnvironmentVariable ] ) {
76+ } else if ( getEnvironmentVariable ( AzureWebAppEnvironmentVariable ) ) {
6177 hostType = HostType . AzureWebApp ;
62- } else if ( process . env [ ContainerAppEnvironmentVariable ] ) {
78+ } else if ( getEnvironmentVariable ( ContainerAppEnvironmentVariable ) ) {
6379 hostType = HostType . ContainerApp ;
64- } else if ( process . env [ KubernetesEnvironmentVariable ] ) {
80+ } else if ( getEnvironmentVariable ( KubernetesEnvironmentVariable ) ) {
6581 hostType = HostType . Kubernetes ;
66- } else if ( process . env [ ServiceFabricEnvironmentVariable ] ) {
82+ } else if ( getEnvironmentVariable ( ServiceFabricEnvironmentVariable ) ) {
6783 hostType = HostType . ServiceFabric ;
84+ } else if ( isBrowser ( ) ) {
85+ hostType = HostType . Browser ;
86+ } else if ( isWebWorker ( ) ) {
87+ hostType = HostType . WebWorker ;
6888 }
6989 return hostType ;
7090}
7191
7292function isDevEnvironment ( ) : boolean {
73- const envType = process . env [ NodeJSEnvironmentVariable ] ;
93+ const envType = getEnvironmentVariable ( NodeJSEnvironmentVariable ) ;
7494 if ( NodeJSDevEnvironmentVariableValue === envType ?. toLowerCase ( ) ) {
7595 return true ;
7696 }
7797 return false ;
98+ }
99+
100+ function isBrowser ( ) {
101+ // https://developer.mozilla.org/en-US/docs/Web/API/Window
102+ const isWindowDefinedAsExpected = typeof window === "object" && typeof Window === "function" && window instanceof Window ;
103+ // https://developer.mozilla.org/en-US/docs/Web/API/Document
104+ const isDocumentDefinedAsExpected = typeof document === "object" && typeof Document === "function" && document instanceof Document ;
105+
106+ return isWindowDefinedAsExpected && isDocumentDefinedAsExpected ;
107+ }
108+
109+ function isWebWorker ( ) {
110+ // https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope
111+ const workerGlobalScopeDefined = typeof WorkerGlobalScope !== "undefined" ;
112+ // https://developer.mozilla.org/en-US/docs/Web/API/WorkerNavigator
113+ const isNavigatorDefinedAsExpected = typeof navigator === "object" && typeof WorkerNavigator !== "function" && navigator instanceof WorkerNavigator ;
114+ // https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers#importing_scripts_and_libraries
115+ const importScriptsAsGlobalFunction = typeof importScripts === "function" ;
116+
117+ return workerGlobalScopeDefined && importScriptsAsGlobalFunction && isNavigatorDefinedAsExpected ;
78118}
0 commit comments