Skip to content

Commit 9acd9dc

Browse files
committed
👷 improve node detection
1 parent e335b0d commit 9acd9dc

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

‎packages/core/src/tools/globalObject.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@ export function getGlobalObject<T = typeof globalThis>(): T {
4949
export const globalObject = getGlobalObject<GlobalObject>()
5050

5151
export const isWorkerEnvironment = 'WorkerGlobalScope' in globalObject
52-
// @see https://stackoverflow.com/questions/4224606/how-to-check-whether-a-script-is-running-under-node-js
53-
// @ts-expect-error module is undefined outside of Node.
54-
export const isNodeEnvironment = typeof module !== 'undefined' && module.exports
52+
export const isNodeEnvironment =
53+
// @ts-expect-error for Node.js-specific globals that are not present in browser environments
54+
typeof process !== 'undefined' && process.versions !== null && process.versions.node !== null

‎test/apps/vanilla/app.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,23 @@ if (typeof window !== 'undefined') {
1717
window.RUM_INIT()
1818
}
1919
} else {
20+
// Document is always generated by the SSR environment
21+
// @ts-ignore If document is mocked, the SDK executes further enough to throw an error
22+
globalThis.document = {}
23+
24+
// Check if the SDK sent any events
25+
;(globalThis as any).__ddBrowserSdkExtensionCallback = () => {
26+
throw new Error('the SDK should not send events')
27+
}
28+
2029
// compat test
21-
datadogLogs.init({ clientToken: 'xxx', beforeSend: undefined })
22-
datadogRum.init({ clientToken: 'xxx', applicationId: 'xxx', beforeSend: undefined })
30+
31+
datadogLogs.init({ clientToken: 'xxx', beforeSend: undefined, telemetrySampleRate: 100 })
32+
datadogRum.init({ clientToken: 'xxx', applicationId: 'xxx', beforeSend: undefined, telemetrySampleRate: 100 })
2333
datadogRum.setUser({ id: undefined })
34+
35+
// Check the SDK is not started
36+
if (datadogLogs.getInternalContext() || datadogRum.getInternalContext()) {
37+
throw new Error('SDK should not start on SSR environments')
38+
}
2439
}

0 commit comments

Comments
 (0)