Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/core/src/browser/cookie.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { display } from '../tools/display'
import { ONE_MINUTE, ONE_SECOND } from '../tools/utils/timeUtils'
import { findCommaSeparatedValue, findCommaSeparatedValues, generateUUID } from '../tools/utils/stringUtils'
import { isBrowserEnvironment } from '../tools/globalObject'

export interface CookieOptions {
secure?: boolean
Expand Down Expand Up @@ -46,7 +47,7 @@ export function deleteCookie(name: string, options?: CookieOptions) {
}

export function areCookiesAuthorized(options: CookieOptions): boolean {
if (document.cookie === undefined || document.cookie === null) {
if (!isBrowserEnvironment || document.cookie === undefined || document.cookie === null) {
return false
}
try {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/domain/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type { SessionPersistence } from '../session/sessionConstants'
import type { MatchOption } from '../../tools/matchOption'
import { isAllowedTrackingOrigins } from '../allowedTrackingOrigins'
import type { Site } from '../intakeSites'
import { isWorkerEnvironment } from '../../tools/globalObject'
import { isBrowserEnvironment, isWorkerEnvironment } from '../../tools/globalObject'
import type { TransportConfiguration } from './transportConfiguration'
import { computeTransportConfiguration } from './transportConfiguration'

Expand Down Expand Up @@ -397,7 +397,7 @@ export function validateAndBuildConfiguration(initConfiguration: InitConfigurati
return {
beforeSend:
initConfiguration.beforeSend && catchUserErrors(initConfiguration.beforeSend, 'beforeSend threw an error:'),
sessionStoreStrategyType: isWorkerEnvironment ? undefined : selectSessionStoreStrategyType(initConfiguration),
sessionStoreStrategyType: !isBrowserEnvironment ? undefined : selectSessionStoreStrategyType(initConfiguration),
sessionSampleRate: initConfiguration.sessionSampleRate ?? 100,
telemetrySampleRate: initConfiguration.telemetrySampleRate ?? 20,
telemetryConfigurationSampleRate: initConfiguration.telemetryConfigurationSampleRate ?? 5,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getInitCookie } from '../../browser/cookie'
import { globalObject, isWorkerEnvironment } from '../../tools/globalObject'
import { globalObject, isBrowserEnvironment } from '../../tools/globalObject'

export const SYNTHETICS_TEST_ID_COOKIE_NAME = 'datadog-synthetics-public-id'
export const SYNTHETICS_RESULT_ID_COOKIE_NAME = 'datadog-synthetics-result-id'
Expand All @@ -12,7 +12,7 @@ export interface BrowserWindow extends Window {
}

export function willSyntheticsInjectRum(): boolean {
if (isWorkerEnvironment) {
if (!isBrowserEnvironment) {
// We don't expect to run synthetics tests in a worker environment
return false
}
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/tools/globalObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ export function getGlobalObject<T = typeof globalThis>(): T {
// eslint-disable-next-line local-rules/disallow-side-effects
export const globalObject = getGlobalObject<GlobalObject>()

export const isWorkerEnvironment = !('document' in globalObject)
export const isBrowserEnvironment = 'document' in globalObject
export const isWorkerEnvironment = 'WorkerGlobalScope' in globalObject
export const isNodeEnvironment = !isBrowserEnvironment && !isWorkerEnvironment
2 changes: 1 addition & 1 deletion packages/core/src/tools/utils/urlPolyfill.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { globalObject } from '../globalObject'

export function normalizeUrl(url: string) {
return buildUrl(url, location.href).href
return buildUrl(url, globalObject.location?.href).href
}

export function isValidUrl(url: string) {
Expand Down
8 changes: 7 additions & 1 deletion packages/logs/src/boot/preStartLogs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { TrackingConsentState } from '@datadog/browser-core'
import {
isBrowserEnvironment,
createBoundedBuffer,
canUseEventBridge,
display,
Expand Down Expand Up @@ -43,7 +44,12 @@ export function createPreStartStrategy(
const trackingConsentStateSubscription = trackingConsentState.observable.subscribe(tryStartLogs)

function tryStartLogs() {
if (!cachedConfiguration || !cachedInitConfiguration || !trackingConsentState.isGranted()) {
if (
!isBrowserEnvironment ||
!cachedConfiguration ||
!cachedInitConfiguration ||
!trackingConsentState.isGranted()
) {
return
}

Expand Down
4 changes: 2 additions & 2 deletions packages/logs/src/boot/startLogs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { TrackingConsentState, BufferedObservable, BufferedData, PageMayExitEvent } from '@datadog/browser-core'
import {
isBrowserEnvironment,
Observable,
sendToExtension,
createPageMayExitObservable,
Expand All @@ -11,7 +12,6 @@ import {
TelemetryService,
createIdentityEncoder,
startUserContext,
isWorkerEnvironment,
} from '@datadog/browser-core'
import { startLogsSessionManager, startLogsSessionManagerStub } from '../domain/logsSessionManager'
import type { LogsConfiguration } from '../domain/configuration'
Expand Down Expand Up @@ -55,7 +55,7 @@ export function startLogs(

const reportError = startReportError(lifeCycle)
// Page exit is not observable in worker environments (no window/document events)
const pageMayExitObservable = isWorkerEnvironment
const pageMayExitObservable = !isBrowserEnvironment
? new Observable<PageMayExitEvent>()
: createPageMayExitObservable(configuration)

Expand Down
4 changes: 2 additions & 2 deletions packages/logs/src/domain/contexts/commonContext.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { isWorkerEnvironment } from '@datadog/browser-core'
import { isBrowserEnvironment } from '@datadog/browser-core'
import type { CommonContext } from '../../rawLogsEvent.types'

export function buildCommonContext(): CommonContext {
if (isWorkerEnvironment) {
if (!isBrowserEnvironment) {
return {}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { FetchResolveContext, XhrCompleteContext } from '@datadog/browser-core'
import {
isWorkerEnvironment,
isBrowserEnvironment,
Observable,
ErrorSource,
initXhrObservable,
Expand Down Expand Up @@ -28,7 +28,7 @@ export function startNetworkErrorCollection(configuration: LogsConfiguration, li

// XHR is not available in web workers, so we use an empty observable that never emits
const xhrSubscription = (
isWorkerEnvironment ? new Observable<XhrCompleteContext>() : initXhrObservable(configuration)
!isBrowserEnvironment ? new Observable<XhrCompleteContext>() : initXhrObservable(configuration)
).subscribe((context) => {
if (context.state === 'complete') {
handleResponse(RequestType.XHR, context)
Expand Down