@servicetitan/datadog-rum 36.4.0 → 37.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. package/dist/create-impl.d.ts.map +1 -1
  2. package/dist/create-impl.js +44 -28
  3. package/dist/create-impl.js.map +1 -1
  4. package/dist/runtime-override/constants.d.ts +2 -0
  5. package/dist/runtime-override/constants.d.ts.map +1 -0
  6. package/dist/runtime-override/constants.js +3 -0
  7. package/dist/runtime-override/constants.js.map +1 -0
  8. package/dist/runtime-override/dev-console.d.ts +12 -0
  9. package/dist/runtime-override/dev-console.d.ts.map +1 -0
  10. package/dist/runtime-override/dev-console.js +40 -0
  11. package/dist/runtime-override/dev-console.js.map +1 -0
  12. package/dist/runtime-override/index.d.ts +4 -0
  13. package/dist/runtime-override/index.d.ts.map +1 -0
  14. package/dist/runtime-override/index.js +5 -0
  15. package/dist/runtime-override/index.js.map +1 -0
  16. package/dist/runtime-override/override.d.ts +4 -0
  17. package/dist/runtime-override/override.d.ts.map +1 -0
  18. package/dist/runtime-override/override.js +35 -0
  19. package/dist/runtime-override/override.js.map +1 -0
  20. package/dist/types.d.ts +1 -0
  21. package/dist/types.d.ts.map +1 -1
  22. package/dist/types.js.map +1 -1
  23. package/package.json +3 -3
  24. package/src/__tests__/create-impl.test.ts +82 -0
  25. package/src/create-impl.ts +53 -30
  26. package/src/runtime-override/__tests__/dev-console.test.ts +158 -0
  27. package/src/runtime-override/__tests__/override.test.ts +109 -0
  28. package/src/runtime-override/constants.ts +1 -0
  29. package/src/runtime-override/dev-console.ts +56 -0
  30. package/src/runtime-override/index.ts +3 -0
  31. package/src/runtime-override/override.ts +41 -0
  32. package/src/types.ts +1 -0
@@ -1 +1 @@
1
- {"version":3,"file":"create-impl.d.ts","sourceRoot":"","sources":["../src/create-impl.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAShD;;;;GAIG;AACH,wBAAgB,UAAU,IAAI,UAAU,CAqDvC"}
1
+ {"version":3,"file":"create-impl.d.ts","sourceRoot":"","sources":["../src/create-impl.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAShD;;;;GAIG;AACH,wBAAgB,UAAU,IAAI,UAAU,CA2EvC"}
@@ -1,6 +1,7 @@
1
1
  import { datadogRum } from '@datadog/browser-rum';
2
2
  import { buildAllowedTracingUrls } from './build-allowed-tracing-urls';
3
3
  import { buildBeforeSend } from './build-before-send';
4
+ import { DATADOG_RUM_FOR_DEV, installDevConsole, isForceEnabled } from './runtime-override';
4
5
  const DEFAULTS = {
5
6
  sessionSampleRate: 100,
6
7
  sessionReplaySampleRate: 0,
@@ -15,40 +16,55 @@ const DEFAULTS = {
15
16
  const state = {
16
17
  services: new Map()
17
18
  };
19
+ function initializeSdk(service, application, enabled) {
20
+ if (initialized) {
21
+ // eslint-disable-next-line no-console
22
+ console.info('[datadog-rum] init: application config ignored; SDK already initialized.');
23
+ return false;
24
+ }
25
+ if (datadogRum.getInitConfiguration()) {
26
+ // eslint-disable-next-line no-console
27
+ console.warn('[datadog-rum] init: SDK was initialized outside the facade; service handlers will not fire.');
28
+ return false;
29
+ }
30
+ const { beforeSend: appBeforeSend, ...sdkConfigPart } = application;
31
+ if (appBeforeSend) {
32
+ state.applicationBeforeSend = appBeforeSend;
33
+ }
34
+ datadogRum.init({
35
+ ...DEFAULTS,
36
+ ...sdkConfigPart,
37
+ service: service.name,
38
+ beforeSend: buildBeforeSend(state),
39
+ allowedTracingUrls: buildAllowedTracingUrls(state)
40
+ });
41
+ if (!enabled) {
42
+ datadogRum.setGlobalContextProperty(DATADOG_RUM_FOR_DEV, true);
43
+ }
44
+ initialized = true;
45
+ return true;
46
+ }
47
+ function registerService({ name, ...reg }) {
48
+ const existing = state.services.get(name);
49
+ state.services.set(name, {
50
+ ...reg,
51
+ context: existing === null || existing === void 0 ? void 0 : existing.context
52
+ });
53
+ }
18
54
  return {
19
- init ({ service, application }) {
55
+ init ({ service, application, enabled = true }) {
20
56
  let initializedSdk = false;
21
57
  if (application) {
22
- if (!initialized && !datadogRum.getInitConfiguration()) {
23
- const { beforeSend: appBeforeSend, ...sdkConfigPart } = application;
24
- if (appBeforeSend) {
25
- state.applicationBeforeSend = appBeforeSend;
26
- }
27
- datadogRum.init({
28
- ...DEFAULTS,
29
- ...sdkConfigPart,
30
- service: service.name,
31
- beforeSend: buildBeforeSend(state),
32
- allowedTracingUrls: buildAllowedTracingUrls(state)
33
- });
34
- initialized = true;
35
- initializedSdk = true;
36
- } else if (initialized) {
37
- // eslint-disable-next-line no-console
38
- console.info('[datadog-rum] init: application config ignored; SDK already initialized.');
39
- } else {
40
- // eslint-disable-next-line no-console
41
- console.warn('[datadog-rum] init: SDK was initialized outside the facade; service handlers will not fire.');
58
+ if (!enabled) {
59
+ installDevConsole();
60
+ }
61
+ if (enabled || isForceEnabled()) {
62
+ initializedSdk = initializeSdk(service, application, enabled);
42
63
  }
43
64
  }
44
- const { name, ...reg } = service;
45
- const existing = state.services.get(name);
46
- state.services.set(name, {
47
- ...reg,
48
- context: existing === null || existing === void 0 ? void 0 : existing.context
49
- });
65
+ registerService(service);
50
66
  return {
51
- cleanup: ()=>state.services.delete(name),
67
+ cleanup: ()=>state.services.delete(service.name),
52
68
  initializedSdk
53
69
  };
54
70
  },
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/create-impl.ts"],"sourcesContent":["import { datadogRum } from '@datadog/browser-rum';\nimport { buildAllowedTracingUrls } from './build-allowed-tracing-urls';\nimport { buildBeforeSend } from './build-before-send';\nimport type { SharedImpl } from './shared-impl';\nimport type { State } from './types';\n\nconst DEFAULTS = {\n sessionSampleRate: 100,\n sessionReplaySampleRate: 0,\n defaultPrivacyLevel: 'mask-user-input',\n} as const;\n\n/**\n * Interop surface: stored on globalThis, shared across independently\n * deployed bundles. First bundle to load wins. Append-only — never\n * remove or change method signatures.\n */\nexport function createImpl(): SharedImpl {\n let initialized = false;\n const state: State = {\n services: new Map(),\n };\n\n return {\n init({ service, application }) {\n let initializedSdk = false;\n\n if (application) {\n if (!initialized && !datadogRum.getInitConfiguration()) {\n const { beforeSend: appBeforeSend, ...sdkConfigPart } = application;\n if (appBeforeSend) {\n state.applicationBeforeSend = appBeforeSend;\n }\n datadogRum.init({\n ...DEFAULTS,\n ...sdkConfigPart,\n service: service.name,\n beforeSend: buildBeforeSend(state),\n allowedTracingUrls: buildAllowedTracingUrls(state),\n });\n initialized = true;\n initializedSdk = true;\n } else if (initialized) {\n // eslint-disable-next-line no-console\n console.info(\n '[datadog-rum] init: application config ignored; SDK already initialized.'\n );\n } else {\n // eslint-disable-next-line no-console\n console.warn(\n '[datadog-rum] init: SDK was initialized outside the facade; service handlers will not fire.'\n );\n }\n }\n\n const { name, ...reg } = service;\n const existing = state.services.get(name);\n state.services.set(name, { ...reg, context: existing?.context });\n\n return {\n cleanup: () => state.services.delete(name),\n initializedSdk,\n };\n },\n\n setServiceContext(key, context) {\n const existing = state.services.get(key);\n state.services.set(key, { ...existing, context });\n },\n };\n}\n"],"names":["datadogRum","buildAllowedTracingUrls","buildBeforeSend","DEFAULTS","sessionSampleRate","sessionReplaySampleRate","defaultPrivacyLevel","createImpl","initialized","state","services","Map","init","service","application","initializedSdk","getInitConfiguration","beforeSend","appBeforeSend","sdkConfigPart","applicationBeforeSend","name","allowedTracingUrls","console","info","warn","reg","existing","get","set","context","cleanup","delete","setServiceContext","key"],"mappings":"AAAA,SAASA,UAAU,QAAQ,uBAAuB;AAClD,SAASC,uBAAuB,QAAQ,+BAA+B;AACvE,SAASC,eAAe,QAAQ,sBAAsB;AAItD,MAAMC,WAAW;IACbC,mBAAmB;IACnBC,yBAAyB;IACzBC,qBAAqB;AACzB;AAEA;;;;CAIC,GACD,OAAO,SAASC;IACZ,IAAIC,cAAc;IAClB,MAAMC,QAAe;QACjBC,UAAU,IAAIC;IAClB;IAEA,OAAO;QACHC,MAAK,EAAEC,OAAO,EAAEC,WAAW,EAAE;YACzB,IAAIC,iBAAiB;YAErB,IAAID,aAAa;gBACb,IAAI,CAACN,eAAe,CAACR,WAAWgB,oBAAoB,IAAI;oBACpD,MAAM,EAAEC,YAAYC,aAAa,EAAE,GAAGC,eAAe,GAAGL;oBACxD,IAAII,eAAe;wBACfT,MAAMW,qBAAqB,GAAGF;oBAClC;oBACAlB,WAAWY,IAAI,CAAC;wBACZ,GAAGT,QAAQ;wBACX,GAAGgB,aAAa;wBAChBN,SAASA,QAAQQ,IAAI;wBACrBJ,YAAYf,gBAAgBO;wBAC5Ba,oBAAoBrB,wBAAwBQ;oBAChD;oBACAD,cAAc;oBACdO,iBAAiB;gBACrB,OAAO,IAAIP,aAAa;oBACpB,sCAAsC;oBACtCe,QAAQC,IAAI,CACR;gBAER,OAAO;oBACH,sCAAsC;oBACtCD,QAAQE,IAAI,CACR;gBAER;YACJ;YAEA,MAAM,EAAEJ,IAAI,EAAE,GAAGK,KAAK,GAAGb;YACzB,MAAMc,WAAWlB,MAAMC,QAAQ,CAACkB,GAAG,CAACP;YACpCZ,MAAMC,QAAQ,CAACmB,GAAG,CAACR,MAAM;gBAAE,GAAGK,GAAG;gBAAEI,OAAO,EAAEH,qBAAAA,+BAAAA,SAAUG,OAAO;YAAC;YAE9D,OAAO;gBACHC,SAAS,IAAMtB,MAAMC,QAAQ,CAACsB,MAAM,CAACX;gBACrCN;YACJ;QACJ;QAEAkB,mBAAkBC,GAAG,EAAEJ,OAAO;YAC1B,MAAMH,WAAWlB,MAAMC,QAAQ,CAACkB,GAAG,CAACM;YACpCzB,MAAMC,QAAQ,CAACmB,GAAG,CAACK,KAAK;gBAAE,GAAGP,QAAQ;gBAAEG;YAAQ;QACnD;IACJ;AACJ"}
1
+ {"version":3,"sources":["../src/create-impl.ts"],"sourcesContent":["import { datadogRum } from '@datadog/browser-rum';\nimport { buildAllowedTracingUrls } from './build-allowed-tracing-urls';\nimport { buildBeforeSend } from './build-before-send';\nimport { DATADOG_RUM_FOR_DEV, installDevConsole, isForceEnabled } from './runtime-override';\nimport type { SharedImpl } from './shared-impl';\nimport type { ApplicationConfig, ServiceConfig, State } from './types';\n\nconst DEFAULTS = {\n sessionSampleRate: 100,\n sessionReplaySampleRate: 0,\n defaultPrivacyLevel: 'mask-user-input',\n} as const;\n\n/**\n * Interop surface: stored on globalThis, shared across independently\n * deployed bundles. First bundle to load wins. Append-only — never\n * remove or change method signatures.\n */\nexport function createImpl(): SharedImpl {\n let initialized = false;\n const state: State = {\n services: new Map(),\n };\n\n function initializeSdk(\n service: ServiceConfig,\n application: ApplicationConfig,\n enabled: boolean\n ): boolean {\n if (initialized) {\n // eslint-disable-next-line no-console\n console.info(\n '[datadog-rum] init: application config ignored; SDK already initialized.'\n );\n return false;\n }\n if (datadogRum.getInitConfiguration()) {\n // eslint-disable-next-line no-console\n console.warn(\n '[datadog-rum] init: SDK was initialized outside the facade; service handlers will not fire.'\n );\n return false;\n }\n\n const { beforeSend: appBeforeSend, ...sdkConfigPart } = application;\n if (appBeforeSend) {\n state.applicationBeforeSend = appBeforeSend;\n }\n datadogRum.init({\n ...DEFAULTS,\n ...sdkConfigPart,\n service: service.name,\n beforeSend: buildBeforeSend(state),\n allowedTracingUrls: buildAllowedTracingUrls(state),\n });\n if (!enabled) {\n datadogRum.setGlobalContextProperty(DATADOG_RUM_FOR_DEV, true);\n }\n initialized = true;\n return true;\n }\n\n function registerService({ name, ...reg }: ServiceConfig): void {\n const existing = state.services.get(name);\n state.services.set(name, { ...reg, context: existing?.context });\n }\n\n return {\n init({ service, application, enabled = true }) {\n let initializedSdk = false;\n\n if (application) {\n if (!enabled) {\n installDevConsole();\n }\n if (enabled || isForceEnabled()) {\n initializedSdk = initializeSdk(service, application, enabled);\n }\n }\n\n registerService(service);\n\n return {\n cleanup: () => state.services.delete(service.name),\n initializedSdk,\n };\n },\n\n setServiceContext(key, context) {\n const existing = state.services.get(key);\n state.services.set(key, { ...existing, context });\n },\n };\n}\n"],"names":["datadogRum","buildAllowedTracingUrls","buildBeforeSend","DATADOG_RUM_FOR_DEV","installDevConsole","isForceEnabled","DEFAULTS","sessionSampleRate","sessionReplaySampleRate","defaultPrivacyLevel","createImpl","initialized","state","services","Map","initializeSdk","service","application","enabled","console","info","getInitConfiguration","warn","beforeSend","appBeforeSend","sdkConfigPart","applicationBeforeSend","init","name","allowedTracingUrls","setGlobalContextProperty","registerService","reg","existing","get","set","context","initializedSdk","cleanup","delete","setServiceContext","key"],"mappings":"AAAA,SAASA,UAAU,QAAQ,uBAAuB;AAClD,SAASC,uBAAuB,QAAQ,+BAA+B;AACvE,SAASC,eAAe,QAAQ,sBAAsB;AACtD,SAASC,mBAAmB,EAAEC,iBAAiB,EAAEC,cAAc,QAAQ,qBAAqB;AAI5F,MAAMC,WAAW;IACbC,mBAAmB;IACnBC,yBAAyB;IACzBC,qBAAqB;AACzB;AAEA;;;;CAIC,GACD,OAAO,SAASC;IACZ,IAAIC,cAAc;IAClB,MAAMC,QAAe;QACjBC,UAAU,IAAIC;IAClB;IAEA,SAASC,cACLC,OAAsB,EACtBC,WAA8B,EAC9BC,OAAgB;QAEhB,IAAIP,aAAa;YACb,sCAAsC;YACtCQ,QAAQC,IAAI,CACR;YAEJ,OAAO;QACX;QACA,IAAIpB,WAAWqB,oBAAoB,IAAI;YACnC,sCAAsC;YACtCF,QAAQG,IAAI,CACR;YAEJ,OAAO;QACX;QAEA,MAAM,EAAEC,YAAYC,aAAa,EAAE,GAAGC,eAAe,GAAGR;QACxD,IAAIO,eAAe;YACfZ,MAAMc,qBAAqB,GAAGF;QAClC;QACAxB,WAAW2B,IAAI,CAAC;YACZ,GAAGrB,QAAQ;YACX,GAAGmB,aAAa;YAChBT,SAASA,QAAQY,IAAI;YACrBL,YAAYrB,gBAAgBU;YAC5BiB,oBAAoB5B,wBAAwBW;QAChD;QACA,IAAI,CAACM,SAAS;YACVlB,WAAW8B,wBAAwB,CAAC3B,qBAAqB;QAC7D;QACAQ,cAAc;QACd,OAAO;IACX;IAEA,SAASoB,gBAAgB,EAAEH,IAAI,EAAE,GAAGI,KAAoB;QACpD,MAAMC,WAAWrB,MAAMC,QAAQ,CAACqB,GAAG,CAACN;QACpChB,MAAMC,QAAQ,CAACsB,GAAG,CAACP,MAAM;YAAE,GAAGI,GAAG;YAAEI,OAAO,EAAEH,qBAAAA,+BAAAA,SAAUG,OAAO;QAAC;IAClE;IAEA,OAAO;QACHT,MAAK,EAAEX,OAAO,EAAEC,WAAW,EAAEC,UAAU,IAAI,EAAE;YACzC,IAAImB,iBAAiB;YAErB,IAAIpB,aAAa;gBACb,IAAI,CAACC,SAAS;oBACVd;gBACJ;gBACA,IAAIc,WAAWb,kBAAkB;oBAC7BgC,iBAAiBtB,cAAcC,SAASC,aAAaC;gBACzD;YACJ;YAEAa,gBAAgBf;YAEhB,OAAO;gBACHsB,SAAS,IAAM1B,MAAMC,QAAQ,CAAC0B,MAAM,CAACvB,QAAQY,IAAI;gBACjDS;YACJ;QACJ;QAEAG,mBAAkBC,GAAG,EAAEL,OAAO;YAC1B,MAAMH,WAAWrB,MAAMC,QAAQ,CAACqB,GAAG,CAACO;YACpC7B,MAAMC,QAAQ,CAACsB,GAAG,CAACM,KAAK;gBAAE,GAAGR,QAAQ;gBAAEG;YAAQ;QACnD;IACJ;AACJ"}
@@ -0,0 +1,2 @@
1
+ export declare const DATADOG_RUM_FOR_DEV = "datadogRumForDev";
2
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/runtime-override/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,mBAAmB,qBAAqB,CAAC"}
@@ -0,0 +1,3 @@
1
+ export const DATADOG_RUM_FOR_DEV = 'datadogRumForDev';
2
+
3
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/runtime-override/constants.ts"],"sourcesContent":["export const DATADOG_RUM_FOR_DEV = 'datadogRumForDev';\n"],"names":["DATADOG_RUM_FOR_DEV"],"mappings":"AAAA,OAAO,MAAMA,sBAAsB,mBAAmB"}
@@ -0,0 +1,12 @@
1
+ interface DevConsole {
2
+ start: () => void;
3
+ stop: () => void;
4
+ }
5
+ declare global {
6
+ interface Window {
7
+ datadogRumForDev?: DevConsole;
8
+ }
9
+ }
10
+ export declare function installDevConsole(): void;
11
+ export {};
12
+ //# sourceMappingURL=dev-console.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dev-console.d.ts","sourceRoot":"","sources":["../../src/runtime-override/dev-console.ts"],"names":[],"mappings":"AAIA,UAAU,UAAU;IAChB,KAAK,EAAE,MAAM,IAAI,CAAC;IAClB,IAAI,EAAE,MAAM,IAAI,CAAC;CACpB;AAMD,OAAO,CAAC,MAAM,CAAC;IACX,UAAU,MAAM;QACZ,gBAAgB,CAAC,EAAE,UAAU,CAAC;KACjC;CACJ;AA0BD,wBAAgB,iBAAiB,IAAI,IAAI,CAYxC"}
@@ -0,0 +1,40 @@
1
+ import { datadogRum } from '@datadog/browser-rum';
2
+ import { DATADOG_RUM_FOR_DEV } from './constants';
3
+ import { clearOverride, enableOverride } from './override';
4
+ /* istanbul ignore next: thin seam over location.reload, which jsdom does not implement */ function reloadPage() {
5
+ globalThis.location.reload();
6
+ }
7
+ function start() {
8
+ enableOverride();
9
+ // eslint-disable-next-line no-console
10
+ console.info(`[datadog-rum] ${DATADOG_RUM_FOR_DEV}.start(): RUM enabled for 12h; reloading.`);
11
+ reloadPage();
12
+ }
13
+ function stop() {
14
+ try {
15
+ datadogRum.stopSession();
16
+ } catch (unused) {
17
+ // ignore: SDK may not be initialized
18
+ }
19
+ clearOverride();
20
+ // eslint-disable-next-line no-console
21
+ console.info(`[datadog-rum] ${DATADOG_RUM_FOR_DEV}.stop(): override cleared; reloading.`);
22
+ reloadPage();
23
+ }
24
+ export function installDevConsole() {
25
+ const slot = globalThis;
26
+ if (slot[DATADOG_RUM_FOR_DEV]) {
27
+ return;
28
+ }
29
+ slot[DATADOG_RUM_FOR_DEV] = {
30
+ start,
31
+ stop
32
+ };
33
+ globalThis.addEventListener('storage', (event)=>{
34
+ if (event.key === DATADOG_RUM_FOR_DEV) {
35
+ reloadPage();
36
+ }
37
+ });
38
+ }
39
+
40
+ //# sourceMappingURL=dev-console.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/runtime-override/dev-console.ts"],"sourcesContent":["import { datadogRum } from '@datadog/browser-rum';\nimport { DATADOG_RUM_FOR_DEV } from './constants';\nimport { clearOverride, enableOverride } from './override';\n\ninterface DevConsole {\n start: () => void;\n stop: () => void;\n}\n\ninterface DevGlobalSlot {\n [DATADOG_RUM_FOR_DEV]?: DevConsole;\n}\n\ndeclare global {\n interface Window {\n datadogRumForDev?: DevConsole;\n }\n}\n\n/* istanbul ignore next: thin seam over location.reload, which jsdom does not implement */\nfunction reloadPage(): void {\n globalThis.location.reload();\n}\n\nfunction start(): void {\n enableOverride();\n // eslint-disable-next-line no-console\n console.info(`[datadog-rum] ${DATADOG_RUM_FOR_DEV}.start(): RUM enabled for 12h; reloading.`);\n reloadPage();\n}\n\nfunction stop(): void {\n try {\n datadogRum.stopSession();\n } catch {\n // ignore: SDK may not be initialized\n }\n clearOverride();\n // eslint-disable-next-line no-console\n console.info(`[datadog-rum] ${DATADOG_RUM_FOR_DEV}.stop(): override cleared; reloading.`);\n reloadPage();\n}\n\nexport function installDevConsole(): void {\n const slot = globalThis as unknown as DevGlobalSlot;\n if (slot[DATADOG_RUM_FOR_DEV]) {\n return;\n }\n slot[DATADOG_RUM_FOR_DEV] = { start, stop };\n\n globalThis.addEventListener('storage', event => {\n if (event.key === DATADOG_RUM_FOR_DEV) {\n reloadPage();\n }\n });\n}\n"],"names":["datadogRum","DATADOG_RUM_FOR_DEV","clearOverride","enableOverride","reloadPage","globalThis","location","reload","start","console","info","stop","stopSession","installDevConsole","slot","addEventListener","event","key"],"mappings":"AAAA,SAASA,UAAU,QAAQ,uBAAuB;AAClD,SAASC,mBAAmB,QAAQ,cAAc;AAClD,SAASC,aAAa,EAAEC,cAAc,QAAQ,aAAa;AAiB3D,wFAAwF,GACxF,SAASC;IACLC,WAAWC,QAAQ,CAACC,MAAM;AAC9B;AAEA,SAASC;IACLL;IACA,sCAAsC;IACtCM,QAAQC,IAAI,CAAC,CAAC,cAAc,EAAET,oBAAoB,yCAAyC,CAAC;IAC5FG;AACJ;AAEA,SAASO;IACL,IAAI;QACAX,WAAWY,WAAW;IAC1B,EAAE,eAAM;IACJ,qCAAqC;IACzC;IACAV;IACA,sCAAsC;IACtCO,QAAQC,IAAI,CAAC,CAAC,cAAc,EAAET,oBAAoB,qCAAqC,CAAC;IACxFG;AACJ;AAEA,OAAO,SAASS;IACZ,MAAMC,OAAOT;IACb,IAAIS,IAAI,CAACb,oBAAoB,EAAE;QAC3B;IACJ;IACAa,IAAI,CAACb,oBAAoB,GAAG;QAAEO;QAAOG;IAAK;IAE1CN,WAAWU,gBAAgB,CAAC,WAAWC,CAAAA;QACnC,IAAIA,MAAMC,GAAG,KAAKhB,qBAAqB;YACnCG;QACJ;IACJ;AACJ"}
@@ -0,0 +1,4 @@
1
+ export { DATADOG_RUM_FOR_DEV } from './constants';
2
+ export { installDevConsole } from './dev-console';
3
+ export { isForceEnabled } from './override';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/runtime-override/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC"}
@@ -0,0 +1,5 @@
1
+ export { DATADOG_RUM_FOR_DEV } from './constants';
2
+ export { installDevConsole } from './dev-console';
3
+ export { isForceEnabled } from './override';
4
+
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/runtime-override/index.ts"],"sourcesContent":["export { DATADOG_RUM_FOR_DEV } from './constants';\nexport { installDevConsole } from './dev-console';\nexport { isForceEnabled } from './override';\n"],"names":["DATADOG_RUM_FOR_DEV","installDevConsole","isForceEnabled"],"mappings":"AAAA,SAASA,mBAAmB,QAAQ,cAAc;AAClD,SAASC,iBAAiB,QAAQ,gBAAgB;AAClD,SAASC,cAAc,QAAQ,aAAa"}
@@ -0,0 +1,4 @@
1
+ export declare function isForceEnabled(): boolean;
2
+ export declare function enableOverride(): void;
3
+ export declare function clearOverride(): void;
4
+ //# sourceMappingURL=override.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"override.d.ts","sourceRoot":"","sources":["../../src/runtime-override/override.ts"],"names":[],"mappings":"AA6BA,wBAAgB,cAAc,IAAI,OAAO,CAGxC;AAED,wBAAgB,cAAc,IAAI,IAAI,CAErC;AAED,wBAAgB,aAAa,IAAI,IAAI,CAEpC"}
@@ -0,0 +1,35 @@
1
+ import { DATADOG_RUM_FOR_DEV } from './constants';
2
+ const TTL_MS = 12 * 60 * 60 * 1000;
3
+ function readOverride() {
4
+ try {
5
+ const raw = localStorage.getItem(DATADOG_RUM_FOR_DEV);
6
+ return raw ? JSON.parse(raw) : null;
7
+ } catch (unused) {
8
+ return null;
9
+ }
10
+ }
11
+ function writeOverride(record) {
12
+ try {
13
+ if (record === null) {
14
+ localStorage.removeItem(DATADOG_RUM_FOR_DEV);
15
+ } else {
16
+ localStorage.setItem(DATADOG_RUM_FOR_DEV, JSON.stringify(record));
17
+ }
18
+ } catch (unused) {
19
+ // storage unavailable
20
+ }
21
+ }
22
+ export function isForceEnabled() {
23
+ const record = readOverride();
24
+ return record !== null && typeof record.exp === 'number' && Date.now() < record.exp;
25
+ }
26
+ export function enableOverride() {
27
+ writeOverride({
28
+ exp: Date.now() + TTL_MS
29
+ });
30
+ }
31
+ export function clearOverride() {
32
+ writeOverride(null);
33
+ }
34
+
35
+ //# sourceMappingURL=override.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/runtime-override/override.ts"],"sourcesContent":["import { DATADOG_RUM_FOR_DEV } from './constants';\n\nconst TTL_MS = 12 * 60 * 60 * 1000;\n\ninterface OverrideRecord {\n exp: number;\n}\n\nfunction readOverride(): OverrideRecord | null {\n try {\n const raw = localStorage.getItem(DATADOG_RUM_FOR_DEV);\n return raw ? (JSON.parse(raw) as OverrideRecord) : null;\n } catch {\n return null;\n }\n}\n\nfunction writeOverride(record: OverrideRecord | null): void {\n try {\n if (record === null) {\n localStorage.removeItem(DATADOG_RUM_FOR_DEV);\n } else {\n localStorage.setItem(DATADOG_RUM_FOR_DEV, JSON.stringify(record));\n }\n } catch {\n // storage unavailable\n }\n}\n\nexport function isForceEnabled(): boolean {\n const record = readOverride();\n return record !== null && typeof record.exp === 'number' && Date.now() < record.exp;\n}\n\nexport function enableOverride(): void {\n writeOverride({ exp: Date.now() + TTL_MS });\n}\n\nexport function clearOverride(): void {\n writeOverride(null);\n}\n"],"names":["DATADOG_RUM_FOR_DEV","TTL_MS","readOverride","raw","localStorage","getItem","JSON","parse","writeOverride","record","removeItem","setItem","stringify","isForceEnabled","exp","Date","now","enableOverride","clearOverride"],"mappings":"AAAA,SAASA,mBAAmB,QAAQ,cAAc;AAElD,MAAMC,SAAS,KAAK,KAAK,KAAK;AAM9B,SAASC;IACL,IAAI;QACA,MAAMC,MAAMC,aAAaC,OAAO,CAACL;QACjC,OAAOG,MAAOG,KAAKC,KAAK,CAACJ,OAA0B;IACvD,EAAE,eAAM;QACJ,OAAO;IACX;AACJ;AAEA,SAASK,cAAcC,MAA6B;IAChD,IAAI;QACA,IAAIA,WAAW,MAAM;YACjBL,aAAaM,UAAU,CAACV;QAC5B,OAAO;YACHI,aAAaO,OAAO,CAACX,qBAAqBM,KAAKM,SAAS,CAACH;QAC7D;IACJ,EAAE,eAAM;IACJ,sBAAsB;IAC1B;AACJ;AAEA,OAAO,SAASI;IACZ,MAAMJ,SAASP;IACf,OAAOO,WAAW,QAAQ,OAAOA,OAAOK,GAAG,KAAK,YAAYC,KAAKC,GAAG,KAAKP,OAAOK,GAAG;AACvF;AAEA,OAAO,SAASG;IACZT,cAAc;QAAEM,KAAKC,KAAKC,GAAG,KAAKf;IAAO;AAC7C;AAEA,OAAO,SAASiB;IACZV,cAAc;AAClB"}
package/dist/types.d.ts CHANGED
@@ -33,6 +33,7 @@ export interface ServiceConfig extends ServiceShape {
33
33
  export interface InitRumOptions {
34
34
  service: ServiceConfig;
35
35
  application?: ApplicationConfig;
36
+ enabled?: boolean;
36
37
  }
37
38
  /**
38
39
  * Per-service entry — `beforeSend` and `allowedTracingUrls` come from
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AACrD,OAAO,KAAK,EAAE,WAAW,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAC9F,OAAO,KAAK,EAAE,4BAA4B,EAAE,wBAAwB,EAAE,MAAM,eAAe,CAAC;AAE5F;;;;GAIG;AACH,MAAM,MAAM,kBAAkB,GAAG,OAAO,CAAC,MAAM,CAAC,cAAc,EAAE,WAAW,EAAE,CAAC,CAAC,CAAC;AAEhF,yFAAyF;AACzF,MAAM,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC;AAEjC;;;GAGG;AACH,MAAM,WAAW,UAAU;IACvB,iBAAiB,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,CAAC;CACjD;AAED,qFAAqF;AACrF,MAAM,WAAW,sBAAuB,SAAQ,UAAU;IACtD,OAAO,EAAE,OAAO,CAAC;CACpB;AAED,MAAM,MAAM,iBAAiB,GAAG,IAAI,CAChC,oBAAoB,EACpB,YAAY,GAAG,oBAAoB,GAAG,SAAS,CAClD,GAAG;IACA,UAAU,CAAC,EAAE,4BAA4B,CAAC;CAC7C,CAAC;AAEF,UAAU,YAAY;IAClB,UAAU,CAAC,EAAE,wBAAwB,CAAC;IACtC,kBAAkB,CAAC,EAAE,kBAAkB,CAAC;CAC3C;AAED,MAAM,WAAW,aAAc,SAAQ,YAAY;IAC/C,IAAI,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,cAAc;IAC3B,OAAO,EAAE,aAAa,CAAC;IACvB,WAAW,CAAC,EAAE,iBAAiB,CAAC;CACnC;AAED;;;GAGG;AACH,MAAM,WAAW,YAAa,SAAQ,YAAY;IAC9C,OAAO,CAAC,EAAE,OAAO,CAAC;CACrB;AAED,iGAAiG;AACjG,MAAM,WAAW,KAAK;IAClB,qBAAqB,CAAC,EAAE,4BAA4B,CAAC;IACrD,QAAQ,EAAE,GAAG,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;CACvC"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AACrD,OAAO,KAAK,EAAE,WAAW,EAAE,cAAc,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAC9F,OAAO,KAAK,EAAE,4BAA4B,EAAE,wBAAwB,EAAE,MAAM,eAAe,CAAC;AAE5F;;;;GAIG;AACH,MAAM,MAAM,kBAAkB,GAAG,OAAO,CAAC,MAAM,CAAC,cAAc,EAAE,WAAW,EAAE,CAAC,CAAC,CAAC;AAEhF,yFAAyF;AACzF,MAAM,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC;AAEjC;;;GAGG;AACH,MAAM,WAAW,UAAU;IACvB,iBAAiB,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,CAAC;CACjD;AAED,qFAAqF;AACrF,MAAM,WAAW,sBAAuB,SAAQ,UAAU;IACtD,OAAO,EAAE,OAAO,CAAC;CACpB;AAED,MAAM,MAAM,iBAAiB,GAAG,IAAI,CAChC,oBAAoB,EACpB,YAAY,GAAG,oBAAoB,GAAG,SAAS,CAClD,GAAG;IACA,UAAU,CAAC,EAAE,4BAA4B,CAAC;CAC7C,CAAC;AAEF,UAAU,YAAY;IAClB,UAAU,CAAC,EAAE,wBAAwB,CAAC;IACtC,kBAAkB,CAAC,EAAE,kBAAkB,CAAC;CAC3C;AAED,MAAM,WAAW,aAAc,SAAQ,YAAY;IAC/C,IAAI,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,cAAc;IAC3B,OAAO,EAAE,aAAa,CAAC;IACvB,WAAW,CAAC,EAAE,iBAAiB,CAAC;IAChC,OAAO,CAAC,EAAE,OAAO,CAAC;CACrB;AAED;;;GAGG;AACH,MAAM,WAAW,YAAa,SAAQ,YAAY;IAC9C,OAAO,CAAC,EAAE,OAAO,CAAC;CACrB;AAED,iGAAiG;AACjG,MAAM,WAAW,KAAK;IAClB,qBAAqB,CAAC,EAAE,4BAA4B,CAAC;IACrD,QAAQ,EAAE,GAAG,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;CACvC"}
package/dist/types.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/types.ts"],"sourcesContent":["import type { Context } from '@datadog/browser-core';\nimport type { MatchOption, PropagatorType, RumInitConfiguration } from '@datadog/browser-rum';\nimport type { ApplicationBeforeSendHandler, ServiceBeforeSendHandler } from './before-send';\n\n/**\n * URL matchers grouped by the propagator type whose headers they should\n * activate. A service registers its tracing URLs as e.g.\n * `{ tracecontext: [/api\\//, 'https://...'] }`.\n */\nexport type ServiceTracingUrls = Partial<Record<PropagatorType, MatchOption[]>>;\n\n/** Tears down a service registration: handlers, tracing URLs, and persistent context. */\nexport type Cleanup = () => void;\n\n/**\n * Service handle returned by `useRumService`. `setServiceContext` is bound to the\n * service registered by the hook, so callers don't pass the service name again.\n */\nexport interface RumService {\n setServiceContext: (context: Context) => void;\n}\n\n/** Imperative service registration returned by `init`. Adds the cleanup contract. */\nexport interface RumServiceRegistration extends RumService {\n cleanup: Cleanup;\n}\n\nexport type ApplicationConfig = Omit<\n RumInitConfiguration,\n 'beforeSend' | 'allowedTracingUrls' | 'service'\n> & {\n beforeSend?: ApplicationBeforeSendHandler;\n};\n\ninterface ServiceShape {\n beforeSend?: ServiceBeforeSendHandler;\n allowedTracingUrls?: ServiceTracingUrls;\n}\n\nexport interface ServiceConfig extends ServiceShape {\n name: string;\n}\n\nexport interface InitRumOptions {\n service: ServiceConfig;\n application?: ApplicationConfig;\n}\n\n/**\n * Per-service entry — `beforeSend` and `allowedTracingUrls` come from\n * `init`; `context` comes from `setServiceContext`. One service = one entry.\n */\nexport interface ServiceEntry extends ServiceShape {\n context?: Context;\n}\n\n/** Registration state: application-level handler + per-service entries keyed by service name. */\nexport interface State {\n applicationBeforeSend?: ApplicationBeforeSendHandler;\n services: Map<string, ServiceEntry>;\n}\n"],"names":[],"mappings":"AAwDA,+FAA+F,GAC/F,WAGC"}
1
+ {"version":3,"sources":["../src/types.ts"],"sourcesContent":["import type { Context } from '@datadog/browser-core';\nimport type { MatchOption, PropagatorType, RumInitConfiguration } from '@datadog/browser-rum';\nimport type { ApplicationBeforeSendHandler, ServiceBeforeSendHandler } from './before-send';\n\n/**\n * URL matchers grouped by the propagator type whose headers they should\n * activate. A service registers its tracing URLs as e.g.\n * `{ tracecontext: [/api\\//, 'https://...'] }`.\n */\nexport type ServiceTracingUrls = Partial<Record<PropagatorType, MatchOption[]>>;\n\n/** Tears down a service registration: handlers, tracing URLs, and persistent context. */\nexport type Cleanup = () => void;\n\n/**\n * Service handle returned by `useRumService`. `setServiceContext` is bound to the\n * service registered by the hook, so callers don't pass the service name again.\n */\nexport interface RumService {\n setServiceContext: (context: Context) => void;\n}\n\n/** Imperative service registration returned by `init`. Adds the cleanup contract. */\nexport interface RumServiceRegistration extends RumService {\n cleanup: Cleanup;\n}\n\nexport type ApplicationConfig = Omit<\n RumInitConfiguration,\n 'beforeSend' | 'allowedTracingUrls' | 'service'\n> & {\n beforeSend?: ApplicationBeforeSendHandler;\n};\n\ninterface ServiceShape {\n beforeSend?: ServiceBeforeSendHandler;\n allowedTracingUrls?: ServiceTracingUrls;\n}\n\nexport interface ServiceConfig extends ServiceShape {\n name: string;\n}\n\nexport interface InitRumOptions {\n service: ServiceConfig;\n application?: ApplicationConfig;\n enabled?: boolean;\n}\n\n/**\n * Per-service entry — `beforeSend` and `allowedTracingUrls` come from\n * `init`; `context` comes from `setServiceContext`. One service = one entry.\n */\nexport interface ServiceEntry extends ServiceShape {\n context?: Context;\n}\n\n/** Registration state: application-level handler + per-service entries keyed by service name. */\nexport interface State {\n applicationBeforeSend?: ApplicationBeforeSendHandler;\n services: Map<string, ServiceEntry>;\n}\n"],"names":[],"mappings":"AAyDA,+FAA+F,GAC/F,WAGC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@servicetitan/datadog-rum",
3
- "version": "36.4.0",
3
+ "version": "37.0.1",
4
4
  "description": "DataDog RUM JS SDK configuration for standalone applications",
5
5
  "homepage": "https://docs.st.dev/docs/frontend/uikit/datadog-rum",
6
6
  "repository": {
@@ -22,7 +22,7 @@
22
22
  "src"
23
23
  ],
24
24
  "devDependencies": {
25
- "@datadog/browser-rum": "^7.2.0",
25
+ "@datadog/browser-rum": "^7.3.0",
26
26
  "@testing-library/dom": "^10.4.1",
27
27
  "@testing-library/jest-dom": "^6.9.1",
28
28
  "@testing-library/react": "^16.3.2",
@@ -40,5 +40,5 @@
40
40
  "cli": {
41
41
  "webpack": false
42
42
  },
43
- "gitHead": "8beab9ae3a70ce381e8cd53f2de8691640e96c6e"
43
+ "gitHead": "9a2e5579f88cf9895fd2948ce5d5810e976d20b0"
44
44
  }
@@ -3,12 +3,18 @@ import { minimalApplication } from '../__mocks__';
3
3
  import { buildAllowedTracingUrls } from '../build-allowed-tracing-urls';
4
4
  import { buildBeforeSend } from '../build-before-send';
5
5
  import { createImpl } from '../create-impl';
6
+ import { DATADOG_RUM_FOR_DEV, installDevConsole, isForceEnabled } from '../runtime-override';
6
7
  import type { SharedImpl } from '../shared-impl';
7
8
  import type { InitRumOptions, State } from '../types';
8
9
 
9
10
  jest.mock('@datadog/browser-rum');
10
11
  jest.mock('../build-before-send');
11
12
  jest.mock('../build-allowed-tracing-urls');
13
+ jest.mock('../runtime-override', () => ({
14
+ ...jest.requireActual('../runtime-override'),
15
+ isForceEnabled: jest.fn(),
16
+ installDevConsole: jest.fn(),
17
+ }));
12
18
 
13
19
  describe(`[datadog-rum] ${createImpl.name}`, () => {
14
20
  const mockBeforeSend = jest.fn();
@@ -18,6 +24,7 @@ describe(`[datadog-rum] ${createImpl.name}`, () => {
18
24
  beforeEach(() => {
19
25
  jest.clearAllMocks();
20
26
  jest.mocked(datadogRum.getInitConfiguration).mockReturnValue(undefined);
27
+ jest.mocked(isForceEnabled).mockReturnValue(false);
21
28
  jest.mocked(buildBeforeSend).mockReturnValue(mockBeforeSend);
22
29
  jest.mocked(buildAllowedTracingUrls).mockReturnValue(mockAllowedTracingUrls as any);
23
30
  impl = createImpl();
@@ -64,6 +71,18 @@ describe(`[datadog-rum] ${createImpl.name}`, () => {
64
71
  expect(capturedState().services.has(options.service.name)).toBe(true);
65
72
  });
66
73
 
74
+ itDoesNotInstallDevConsole();
75
+
76
+ itDoesNotTagTheSession();
77
+
78
+ describe('with a stale override present', () => {
79
+ beforeEach(() => {
80
+ jest.mocked(isForceEnabled).mockReturnValue(true);
81
+ });
82
+
83
+ itDoesNotTagTheSession();
84
+ });
85
+
67
86
  describe('when config overrides defaults', () => {
68
87
  const overrides = {
69
88
  sessionSampleRate: 25,
@@ -124,6 +143,22 @@ describe(`[datadog-rum] ${createImpl.name}`, () => {
124
143
  });
125
144
  }
126
145
 
146
+ function itDoesNotInstallDevConsole() {
147
+ test('does not install the dev console handle', () => {
148
+ subject();
149
+
150
+ expect(installDevConsole).not.toHaveBeenCalled();
151
+ });
152
+ }
153
+
154
+ function itDoesNotTagTheSession() {
155
+ test('does not tag the session', () => {
156
+ subject();
157
+
158
+ expect(datadogRum.setGlobalContextProperty).not.toHaveBeenCalled();
159
+ });
160
+ }
161
+
127
162
  describe('when already called', () => {
128
163
  beforeEach(() => {
129
164
  subject();
@@ -171,6 +206,53 @@ describe(`[datadog-rum] ${createImpl.name}`, () => {
171
206
  itDoesNotCallDatadogRumInit();
172
207
  });
173
208
 
209
+ describe('when enabled is false', () => {
210
+ beforeEach(() => {
211
+ options.enabled = false;
212
+ });
213
+
214
+ test('installs the dev console handle', () => {
215
+ subject();
216
+
217
+ expect(installDevConsole).toHaveBeenCalled();
218
+ });
219
+
220
+ itDoesNotCallDatadogRumInit();
221
+
222
+ describe('with an active override', () => {
223
+ beforeEach(() => {
224
+ jest.mocked(isForceEnabled).mockReturnValue(true);
225
+ });
226
+
227
+ test('initializes the SDK', () => {
228
+ subject();
229
+
230
+ expect(datadogRum.init).toHaveBeenCalled();
231
+ });
232
+
233
+ test('reports it initialized the SDK', () => {
234
+ expect(subject().initializedSdk).toBe(true);
235
+ });
236
+
237
+ test('tags the session as override-driven', () => {
238
+ subject();
239
+
240
+ expect(datadogRum.setGlobalContextProperty).toHaveBeenCalledWith(
241
+ DATADOG_RUM_FOR_DEV,
242
+ true
243
+ );
244
+ });
245
+ });
246
+
247
+ describe('with no application', () => {
248
+ beforeEach(() => {
249
+ delete options.application;
250
+ });
251
+
252
+ itDoesNotInstallDevConsole();
253
+ });
254
+ });
255
+
174
256
  describe('cleanup', () => {
175
257
  beforeEach(() => {
176
258
  subject().cleanup();
@@ -1,8 +1,9 @@
1
1
  import { datadogRum } from '@datadog/browser-rum';
2
2
  import { buildAllowedTracingUrls } from './build-allowed-tracing-urls';
3
3
  import { buildBeforeSend } from './build-before-send';
4
+ import { DATADOG_RUM_FOR_DEV, installDevConsole, isForceEnabled } from './runtime-override';
4
5
  import type { SharedImpl } from './shared-impl';
5
- import type { State } from './types';
6
+ import type { ApplicationConfig, ServiceConfig, State } from './types';
6
7
 
7
8
  const DEFAULTS = {
8
9
  sessionSampleRate: 100,
@@ -21,44 +22,66 @@ export function createImpl(): SharedImpl {
21
22
  services: new Map(),
22
23
  };
23
24
 
25
+ function initializeSdk(
26
+ service: ServiceConfig,
27
+ application: ApplicationConfig,
28
+ enabled: boolean
29
+ ): boolean {
30
+ if (initialized) {
31
+ // eslint-disable-next-line no-console
32
+ console.info(
33
+ '[datadog-rum] init: application config ignored; SDK already initialized.'
34
+ );
35
+ return false;
36
+ }
37
+ if (datadogRum.getInitConfiguration()) {
38
+ // eslint-disable-next-line no-console
39
+ console.warn(
40
+ '[datadog-rum] init: SDK was initialized outside the facade; service handlers will not fire.'
41
+ );
42
+ return false;
43
+ }
44
+
45
+ const { beforeSend: appBeforeSend, ...sdkConfigPart } = application;
46
+ if (appBeforeSend) {
47
+ state.applicationBeforeSend = appBeforeSend;
48
+ }
49
+ datadogRum.init({
50
+ ...DEFAULTS,
51
+ ...sdkConfigPart,
52
+ service: service.name,
53
+ beforeSend: buildBeforeSend(state),
54
+ allowedTracingUrls: buildAllowedTracingUrls(state),
55
+ });
56
+ if (!enabled) {
57
+ datadogRum.setGlobalContextProperty(DATADOG_RUM_FOR_DEV, true);
58
+ }
59
+ initialized = true;
60
+ return true;
61
+ }
62
+
63
+ function registerService({ name, ...reg }: ServiceConfig): void {
64
+ const existing = state.services.get(name);
65
+ state.services.set(name, { ...reg, context: existing?.context });
66
+ }
67
+
24
68
  return {
25
- init({ service, application }) {
69
+ init({ service, application, enabled = true }) {
26
70
  let initializedSdk = false;
27
71
 
28
72
  if (application) {
29
- if (!initialized && !datadogRum.getInitConfiguration()) {
30
- const { beforeSend: appBeforeSend, ...sdkConfigPart } = application;
31
- if (appBeforeSend) {
32
- state.applicationBeforeSend = appBeforeSend;
33
- }
34
- datadogRum.init({
35
- ...DEFAULTS,
36
- ...sdkConfigPart,
37
- service: service.name,
38
- beforeSend: buildBeforeSend(state),
39
- allowedTracingUrls: buildAllowedTracingUrls(state),
40
- });
41
- initialized = true;
42
- initializedSdk = true;
43
- } else if (initialized) {
44
- // eslint-disable-next-line no-console
45
- console.info(
46
- '[datadog-rum] init: application config ignored; SDK already initialized.'
47
- );
48
- } else {
49
- // eslint-disable-next-line no-console
50
- console.warn(
51
- '[datadog-rum] init: SDK was initialized outside the facade; service handlers will not fire.'
52
- );
73
+ if (!enabled) {
74
+ installDevConsole();
75
+ }
76
+ if (enabled || isForceEnabled()) {
77
+ initializedSdk = initializeSdk(service, application, enabled);
53
78
  }
54
79
  }
55
80
 
56
- const { name, ...reg } = service;
57
- const existing = state.services.get(name);
58
- state.services.set(name, { ...reg, context: existing?.context });
81
+ registerService(service);
59
82
 
60
83
  return {
61
- cleanup: () => state.services.delete(name),
84
+ cleanup: () => state.services.delete(service.name),
62
85
  initializedSdk,
63
86
  };
64
87
  },
@@ -0,0 +1,158 @@
1
+ import { datadogRum } from '@datadog/browser-rum';
2
+ import { DATADOG_RUM_FOR_DEV } from '../constants';
3
+ import { installDevConsole } from '../dev-console';
4
+ import { clearOverride, enableOverride } from '../override';
5
+
6
+ jest.mock('@datadog/browser-rum');
7
+ jest.mock('../override');
8
+
9
+ describe('[datadog-rum] runtime-override/dev-console', () => {
10
+ const originalLocation = window.location;
11
+ const reload = jest.fn();
12
+ const handle = () =>
13
+ (globalThis as { datadogRumForDev?: { start: () => void; stop: () => void } })
14
+ .datadogRumForDev!;
15
+ const storageCalls = () =>
16
+ jest.mocked(globalThis.addEventListener).mock.calls.filter(([type]) => type === 'storage');
17
+ let infoSpy: jest.SpyInstance;
18
+
19
+ beforeAll(() => {
20
+ delete (window as { location?: Location }).location;
21
+ (window as unknown as { location: { reload: jest.Mock } }).location = { reload };
22
+ });
23
+
24
+ afterAll(() => {
25
+ (window as unknown as { location: Location }).location = originalLocation;
26
+ });
27
+
28
+ afterEach(() => {
29
+ jest.restoreAllMocks();
30
+ });
31
+
32
+ beforeEach(() => {
33
+ jest.clearAllMocks();
34
+ delete (globalThis as { datadogRumForDev?: unknown }).datadogRumForDev;
35
+ infoSpy = jest.spyOn(console, 'info').mockImplementation(() => {});
36
+ jest.spyOn(globalThis, 'addEventListener').mockImplementation(() => {});
37
+ installDevConsole();
38
+ });
39
+
40
+ test('installs the global handle', () => {
41
+ expect(handle()).toEqual({ start: expect.any(Function), stop: expect.any(Function) });
42
+ });
43
+
44
+ test('registers a single storage listener', () => {
45
+ expect(storageCalls()).toHaveLength(1);
46
+ });
47
+
48
+ describe('when installed again', () => {
49
+ let previous: ReturnType<typeof handle>;
50
+
51
+ beforeEach(() => {
52
+ previous = handle();
53
+ installDevConsole();
54
+ });
55
+
56
+ test('keeps the same handle', () => {
57
+ expect(handle()).toBe(previous);
58
+ });
59
+
60
+ test('does not add another storage listener', () => {
61
+ expect(storageCalls()).toHaveLength(1);
62
+ });
63
+ });
64
+
65
+ describe('the storage listener', () => {
66
+ let event: { key: string };
67
+
68
+ const subject = () => (storageCalls()[0][1] as EventListener)(event as unknown as Event);
69
+
70
+ describe('when the override key changes', () => {
71
+ beforeEach(() => (event = { key: DATADOG_RUM_FOR_DEV }));
72
+
73
+ test('reloads sibling tabs', () => {
74
+ subject();
75
+
76
+ expect(reload).toHaveBeenCalled();
77
+ });
78
+ });
79
+
80
+ describe('when an unrelated key changes', () => {
81
+ beforeEach(() => (event = { key: 'other' }));
82
+
83
+ test('does not reload', () => {
84
+ subject();
85
+
86
+ expect(reload).not.toHaveBeenCalled();
87
+ });
88
+ });
89
+ });
90
+
91
+ describe('start', () => {
92
+ const subject = () => handle().start();
93
+
94
+ test('enables the override', () => {
95
+ subject();
96
+
97
+ expect(enableOverride).toHaveBeenCalled();
98
+ });
99
+
100
+ test('reloads the page', () => {
101
+ subject();
102
+
103
+ expect(reload).toHaveBeenCalled();
104
+ });
105
+
106
+ test('logs to the console', () => {
107
+ subject();
108
+
109
+ expect(infoSpy).toHaveBeenCalledWith(expect.stringContaining('start()'));
110
+ });
111
+ });
112
+
113
+ describe('stop', () => {
114
+ const subject = () => handle().stop();
115
+
116
+ test('ends the current Datadog session', () => {
117
+ subject();
118
+
119
+ expect(datadogRum.stopSession).toHaveBeenCalled();
120
+ });
121
+
122
+ test('clears the override', () => {
123
+ subject();
124
+
125
+ expect(clearOverride).toHaveBeenCalled();
126
+ });
127
+
128
+ test('reloads the page', () => {
129
+ subject();
130
+
131
+ expect(reload).toHaveBeenCalled();
132
+ });
133
+
134
+ test('logs to the console', () => {
135
+ subject();
136
+
137
+ expect(infoSpy).toHaveBeenCalledWith(expect.stringContaining('stop()'));
138
+ });
139
+
140
+ describe('when ending the session fails', () => {
141
+ beforeEach(() =>
142
+ jest.mocked(datadogRum.stopSession).mockImplementation(() => {
143
+ throw new Error('not initialized');
144
+ })
145
+ );
146
+
147
+ test('does not throw', () => {
148
+ expect(subject).not.toThrow();
149
+ });
150
+
151
+ test('still clears the override', () => {
152
+ subject();
153
+
154
+ expect(clearOverride).toHaveBeenCalled();
155
+ });
156
+ });
157
+ });
158
+ });
@@ -0,0 +1,109 @@
1
+ import { DATADOG_RUM_FOR_DEV } from '../constants';
2
+ import { clearOverride, enableOverride, isForceEnabled } from '../override';
3
+
4
+ const TTL_MS = 12 * 60 * 60 * 1000;
5
+ const NOW = 1_700_000_000_000;
6
+
7
+ describe('[datadog-rum] runtime-override/override', () => {
8
+ const record = (exp: number) =>
9
+ localStorage.setItem(DATADOG_RUM_FOR_DEV, JSON.stringify({ exp }));
10
+
11
+ beforeEach(() => {
12
+ jest.restoreAllMocks();
13
+ localStorage.clear();
14
+ jest.spyOn(Date, 'now').mockReturnValue(NOW);
15
+ });
16
+
17
+ describe('isForceEnabled', () => {
18
+ const subject = () => isForceEnabled();
19
+
20
+ const itReturns = (value: boolean) =>
21
+ test(`is ${value}`, () => {
22
+ expect(subject()).toBe(value);
23
+ });
24
+
25
+ describe('without an override', () => itReturns(false));
26
+
27
+ describe('with a non-expired override', () => {
28
+ beforeEach(() => record(NOW + 1000));
29
+
30
+ itReturns(true);
31
+ });
32
+
33
+ describe('with an expired override', () => {
34
+ beforeEach(() => record(NOW - 1));
35
+
36
+ itReturns(false);
37
+ });
38
+
39
+ describe('with a malformed override', () => {
40
+ beforeEach(() => localStorage.setItem(DATADOG_RUM_FOR_DEV, 'not json'));
41
+
42
+ itReturns(false);
43
+ });
44
+
45
+ describe('with a record missing an expiry', () => {
46
+ beforeEach(() => localStorage.setItem(DATADOG_RUM_FOR_DEV, JSON.stringify({})));
47
+
48
+ itReturns(false);
49
+ });
50
+
51
+ describe('when storage access throws', () => {
52
+ beforeEach(() =>
53
+ jest.spyOn(Storage.prototype, 'getItem').mockImplementation(() => {
54
+ throw new Error('blocked');
55
+ })
56
+ );
57
+
58
+ itReturns(false);
59
+ });
60
+ });
61
+
62
+ describe('enableOverride', () => {
63
+ const subject = () => enableOverride();
64
+
65
+ test('persists a 12h override window', () => {
66
+ subject();
67
+
68
+ expect(localStorage.getItem(DATADOG_RUM_FOR_DEV)).toBe(
69
+ JSON.stringify({ exp: NOW + TTL_MS })
70
+ );
71
+ });
72
+
73
+ describe('when the storage write fails', () => {
74
+ beforeEach(() =>
75
+ jest.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
76
+ throw new Error('blocked');
77
+ })
78
+ );
79
+
80
+ test('does not throw', () => {
81
+ expect(subject).not.toThrow();
82
+ });
83
+ });
84
+ });
85
+
86
+ describe('clearOverride', () => {
87
+ const subject = () => clearOverride();
88
+
89
+ beforeEach(() => record(NOW + 1000));
90
+
91
+ test('removes the override', () => {
92
+ subject();
93
+
94
+ expect(localStorage.getItem(DATADOG_RUM_FOR_DEV)).toBeNull();
95
+ });
96
+
97
+ describe('when the storage removal fails', () => {
98
+ beforeEach(() =>
99
+ jest.spyOn(Storage.prototype, 'removeItem').mockImplementation(() => {
100
+ throw new Error('blocked');
101
+ })
102
+ );
103
+
104
+ test('does not throw', () => {
105
+ expect(subject).not.toThrow();
106
+ });
107
+ });
108
+ });
109
+ });
@@ -0,0 +1 @@
1
+ export const DATADOG_RUM_FOR_DEV = 'datadogRumForDev';
@@ -0,0 +1,56 @@
1
+ import { datadogRum } from '@datadog/browser-rum';
2
+ import { DATADOG_RUM_FOR_DEV } from './constants';
3
+ import { clearOverride, enableOverride } from './override';
4
+
5
+ interface DevConsole {
6
+ start: () => void;
7
+ stop: () => void;
8
+ }
9
+
10
+ interface DevGlobalSlot {
11
+ [DATADOG_RUM_FOR_DEV]?: DevConsole;
12
+ }
13
+
14
+ declare global {
15
+ interface Window {
16
+ datadogRumForDev?: DevConsole;
17
+ }
18
+ }
19
+
20
+ /* istanbul ignore next: thin seam over location.reload, which jsdom does not implement */
21
+ function reloadPage(): void {
22
+ globalThis.location.reload();
23
+ }
24
+
25
+ function start(): void {
26
+ enableOverride();
27
+ // eslint-disable-next-line no-console
28
+ console.info(`[datadog-rum] ${DATADOG_RUM_FOR_DEV}.start(): RUM enabled for 12h; reloading.`);
29
+ reloadPage();
30
+ }
31
+
32
+ function stop(): void {
33
+ try {
34
+ datadogRum.stopSession();
35
+ } catch {
36
+ // ignore: SDK may not be initialized
37
+ }
38
+ clearOverride();
39
+ // eslint-disable-next-line no-console
40
+ console.info(`[datadog-rum] ${DATADOG_RUM_FOR_DEV}.stop(): override cleared; reloading.`);
41
+ reloadPage();
42
+ }
43
+
44
+ export function installDevConsole(): void {
45
+ const slot = globalThis as unknown as DevGlobalSlot;
46
+ if (slot[DATADOG_RUM_FOR_DEV]) {
47
+ return;
48
+ }
49
+ slot[DATADOG_RUM_FOR_DEV] = { start, stop };
50
+
51
+ globalThis.addEventListener('storage', event => {
52
+ if (event.key === DATADOG_RUM_FOR_DEV) {
53
+ reloadPage();
54
+ }
55
+ });
56
+ }
@@ -0,0 +1,3 @@
1
+ export { DATADOG_RUM_FOR_DEV } from './constants';
2
+ export { installDevConsole } from './dev-console';
3
+ export { isForceEnabled } from './override';
@@ -0,0 +1,41 @@
1
+ import { DATADOG_RUM_FOR_DEV } from './constants';
2
+
3
+ const TTL_MS = 12 * 60 * 60 * 1000;
4
+
5
+ interface OverrideRecord {
6
+ exp: number;
7
+ }
8
+
9
+ function readOverride(): OverrideRecord | null {
10
+ try {
11
+ const raw = localStorage.getItem(DATADOG_RUM_FOR_DEV);
12
+ return raw ? (JSON.parse(raw) as OverrideRecord) : null;
13
+ } catch {
14
+ return null;
15
+ }
16
+ }
17
+
18
+ function writeOverride(record: OverrideRecord | null): void {
19
+ try {
20
+ if (record === null) {
21
+ localStorage.removeItem(DATADOG_RUM_FOR_DEV);
22
+ } else {
23
+ localStorage.setItem(DATADOG_RUM_FOR_DEV, JSON.stringify(record));
24
+ }
25
+ } catch {
26
+ // storage unavailable
27
+ }
28
+ }
29
+
30
+ export function isForceEnabled(): boolean {
31
+ const record = readOverride();
32
+ return record !== null && typeof record.exp === 'number' && Date.now() < record.exp;
33
+ }
34
+
35
+ export function enableOverride(): void {
36
+ writeOverride({ exp: Date.now() + TTL_MS });
37
+ }
38
+
39
+ export function clearOverride(): void {
40
+ writeOverride(null);
41
+ }
package/src/types.ts CHANGED
@@ -44,6 +44,7 @@ export interface ServiceConfig extends ServiceShape {
44
44
  export interface InitRumOptions {
45
45
  service: ServiceConfig;
46
46
  application?: ApplicationConfig;
47
+ enabled?: boolean;
47
48
  }
48
49
 
49
50
  /**