Skip to content

Commit ad0ee3f

Browse files
authored
distinguish browser/webworker usage (#13)
1 parent b2be4c2 commit ad0ee3f

File tree

5 files changed

+66
-14
lines changed

5 files changed

+66
-14
lines changed

rollup.config.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ export default [
1616
plugins: [
1717
typescript({
1818
compilerOptions: {
19+
"lib": [
20+
"DOM",
21+
"WebWorker",
22+
"ESNext"
23+
],
24+
"skipDefaultLibCheck": true,
1925
"module": "ESNext",
2026
"moduleResolution": "Node",
2127
"target": "ES2022",

src/AzureAppConfigurationImpl.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import { JsonKeyValueAdapter } from "./JsonKeyValueAdapter";
99
import { KeyFilter } from "./KeyFilter";
1010
import { LabelFilter } from "./LabelFilter";
1111
import { AzureKeyVaultKeyValueAdapter } from "./keyvault/AzureKeyVaultKeyValueAdapter";
12-
import { CorrelationContextHeaderName, RequestTracingDisabledEnvironmentVariable } from "./requestTracing/constants";
13-
import { createCorrelationContextHeader } from "./requestTracing/utils";
12+
import { CorrelationContextHeaderName } from "./requestTracing/constants";
13+
import { createCorrelationContextHeader, requestTracingEnabled } from "./requestTracing/utils";
1414

1515
export class AzureAppConfigurationImpl extends Map<string, unknown> implements AzureAppConfiguration {
1616
private adapters: IKeyValueAdapter[] = [];
@@ -28,11 +28,8 @@ export class AzureAppConfigurationImpl extends Map<string, unknown> implements A
2828
) {
2929
super();
3030
// Enable request tracing if not opt-out
31-
const requestTracingDisabledEnv = process.env[RequestTracingDisabledEnvironmentVariable];
32-
if (requestTracingDisabledEnv && requestTracingDisabledEnv.toLowerCase() === "true") {
33-
this.requestTracingEnabled = false;
34-
} else {
35-
this.requestTracingEnabled = true;
31+
this.requestTracingEnabled = requestTracingEnabled();
32+
if (this.requestTracingEnabled) {
3633
this.enableRequestTracing();
3734
}
3835

src/requestTracing/constants.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ export enum HostType {
2424
AzureWebApp = "AzureWebApp",
2525
ContainerApp = "ContainerApp",
2626
Kubernetes = "Kubernetes",
27-
ServiceFabric = "ServiceFabric"
27+
ServiceFabric = "ServiceFabric",
28+
// Client-side
29+
Browser = "Web",
30+
WebWorker = "WebWorker"
2831
}
2932

3033
// Environment variables to identify Host type.

src/requestTracing/utils.ts

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
5672
function 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

7292
function 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
}

tsconfig.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
{
22
"compilerOptions": {
3+
"lib": [
4+
"DOM",
5+
"WebWorker",
6+
"ESNext"
7+
],
8+
"skipDefaultLibCheck": true,
39
"module": "ESNext",
410
"moduleResolution": "Node",
511
"target": "ES2022",

0 commit comments

Comments
 (0)