@servicetitan/datadog-rum 36.3.0 → 36.4.0

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 (94) hide show
  1. package/dist/before-send.d.ts +11 -1
  2. package/dist/before-send.d.ts.map +1 -1
  3. package/dist/before-send.js +2 -56
  4. package/dist/before-send.js.map +1 -1
  5. package/dist/build-allowed-tracing-urls.d.ts +9 -0
  6. package/dist/build-allowed-tracing-urls.d.ts.map +1 -0
  7. package/dist/build-allowed-tracing-urls.js +54 -0
  8. package/dist/build-allowed-tracing-urls.js.map +1 -0
  9. package/dist/build-before-send.d.ts +4 -0
  10. package/dist/build-before-send.d.ts.map +1 -0
  11. package/dist/build-before-send.js +64 -0
  12. package/dist/build-before-send.js.map +1 -0
  13. package/dist/bundle-state.d.ts +10 -0
  14. package/dist/bundle-state.d.ts.map +1 -0
  15. package/dist/bundle-state.js +10 -0
  16. package/dist/bundle-state.js.map +1 -0
  17. package/dist/create-impl.d.ts +8 -0
  18. package/dist/create-impl.d.ts.map +1 -0
  19. package/dist/create-impl.js +65 -0
  20. package/dist/create-impl.js.map +1 -0
  21. package/dist/datadog-provider.d.ts +2 -5
  22. package/dist/datadog-provider.d.ts.map +1 -1
  23. package/dist/datadog-provider.js +2 -2
  24. package/dist/datadog-provider.js.map +1 -1
  25. package/dist/enrich-event.d.ts +6 -0
  26. package/dist/enrich-event.d.ts.map +1 -0
  27. package/dist/enrich-event.js +33 -0
  28. package/dist/enrich-event.js.map +1 -0
  29. package/dist/get-shared.d.ts +10 -0
  30. package/dist/get-shared.d.ts.map +1 -0
  31. package/dist/get-shared.js +16 -0
  32. package/dist/get-shared.js.map +1 -0
  33. package/dist/handle-resource.d.ts +3 -0
  34. package/dist/handle-resource.d.ts.map +1 -0
  35. package/dist/handle-resource.js +27 -0
  36. package/dist/handle-resource.js.map +1 -0
  37. package/dist/index.d.ts +3 -0
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +3 -0
  40. package/dist/index.js.map +1 -1
  41. package/dist/init.d.ts +17 -0
  42. package/dist/init.d.ts.map +1 -0
  43. package/dist/init.js +28 -0
  44. package/dist/init.js.map +1 -0
  45. package/dist/set-user.d.ts +13 -0
  46. package/dist/set-user.d.ts.map +1 -0
  47. package/dist/set-user.js +22 -0
  48. package/dist/set-user.js.map +1 -0
  49. package/dist/shared-impl.d.ts +11 -0
  50. package/dist/shared-impl.d.ts.map +1 -0
  51. package/dist/shared-impl.js +3 -0
  52. package/dist/shared-impl.js.map +1 -0
  53. package/dist/types.d.ts +50 -0
  54. package/dist/types.d.ts.map +1 -0
  55. package/dist/types.js +3 -0
  56. package/dist/types.js.map +1 -0
  57. package/dist/use-rum-service.d.ts +12 -0
  58. package/dist/use-rum-service.d.ts.map +1 -0
  59. package/dist/use-rum-service.js +25 -0
  60. package/dist/use-rum-service.js.map +1 -0
  61. package/package.json +3 -3
  62. package/src/__mocks__/index.ts +6 -0
  63. package/src/__mocks__/init-config.ts +6 -0
  64. package/src/__mocks__/process-event.ts +20 -0
  65. package/src/__mocks__/reset-shared-impl.ts +8 -0
  66. package/src/__mocks__/tracing-urls.ts +30 -0
  67. package/src/__mocks__/types.ts +1 -0
  68. package/src/__tests__/before-send.test.ts +39 -87
  69. package/src/__tests__/build-allowed-tracing-urls.test.ts +120 -0
  70. package/src/__tests__/build-before-send.test.ts +193 -0
  71. package/src/__tests__/create-impl.test.ts +211 -0
  72. package/src/__tests__/datadog-provider.test.tsx +55 -25
  73. package/src/__tests__/enrich-event.test.ts +34 -0
  74. package/src/__tests__/get-shared.test.ts +56 -0
  75. package/src/__tests__/handle-resource.test.ts +76 -0
  76. package/src/__tests__/init.test.ts +69 -0
  77. package/src/__tests__/integration.test.ts +147 -0
  78. package/src/__tests__/set-user.test.ts +61 -0
  79. package/src/__tests__/use-rum-service.test.tsx +82 -0
  80. package/src/before-send.ts +15 -63
  81. package/src/build-allowed-tracing-urls.ts +53 -0
  82. package/src/build-before-send.ts +81 -0
  83. package/src/bundle-state.ts +9 -0
  84. package/src/create-impl.ts +71 -0
  85. package/src/datadog-provider.tsx +4 -6
  86. package/src/enrich-event.ts +38 -0
  87. package/src/get-shared.ts +24 -0
  88. package/src/handle-resource.ts +30 -0
  89. package/src/index.ts +3 -0
  90. package/src/init.ts +39 -0
  91. package/src/set-user.ts +25 -0
  92. package/src/shared-impl.ts +9 -0
  93. package/src/types.ts +61 -0
  94. package/src/use-rum-service.ts +28 -0
package/dist/init.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ import type { InitRumOptions, RumServiceRegistration } from './types';
2
+ export type { ApplicationConfig, Cleanup, InitRumOptions, RumService, RumServiceRegistration, ServiceConfig, ServiceTracingUrls, } from './types';
3
+ /**
4
+ * Registers a service and, optionally, initializes the Datadog RUM SDK.
5
+ *
6
+ * Any caller may pass `application`. The first call that includes it
7
+ * initializes the SDK; subsequent callers' `application` config is ignored
8
+ * with a warning. Callers that omit `application` register their service
9
+ * without attempting SDK init.
10
+ *
11
+ * Returns a `RumServiceRegistration` — `{ cleanup, setServiceContext }`.
12
+ * `cleanup` removes this service's `beforeSend`, `allowedTracingUrls`, and
13
+ * persistent context. `setServiceContext` attaches session-lifetime metadata
14
+ * to every event whose `event.service` matches this service's name.
15
+ */
16
+ export declare function init(opts: InitRumOptions): RumServiceRegistration;
17
+ //# sourceMappingURL=init.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"init.d.ts","sourceRoot":"","sources":["../src/init.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,sBAAsB,EAAE,MAAM,SAAS,CAAC;AAEtE,YAAY,EACR,iBAAiB,EACjB,OAAO,EACP,cAAc,EACd,UAAU,EACV,sBAAsB,EACtB,aAAa,EACb,kBAAkB,GACrB,MAAM,SAAS,CAAC;AAEjB;;;;;;;;;;;;GAYG;AACH,wBAAgB,IAAI,CAAC,IAAI,EAAE,cAAc,GAAG,sBAAsB,CAWjE"}
package/dist/init.js ADDED
@@ -0,0 +1,28 @@
1
+ import { bundleState } from './bundle-state';
2
+ import { getOrCreateSharedImpl } from './get-shared';
3
+ /**
4
+ * Registers a service and, optionally, initializes the Datadog RUM SDK.
5
+ *
6
+ * Any caller may pass `application`. The first call that includes it
7
+ * initializes the SDK; subsequent callers' `application` config is ignored
8
+ * with a warning. Callers that omit `application` register their service
9
+ * without attempting SDK init.
10
+ *
11
+ * Returns a `RumServiceRegistration` — `{ cleanup, setServiceContext }`.
12
+ * `cleanup` removes this service's `beforeSend`, `allowedTracingUrls`, and
13
+ * persistent context. `setServiceContext` attaches session-lifetime metadata
14
+ * to every event whose `event.service` matches this service's name.
15
+ */ export function init(opts) {
16
+ const impl = getOrCreateSharedImpl();
17
+ const result = impl.init(opts);
18
+ if (result.initializedSdk) {
19
+ bundleState.isSdkInitializer = true;
20
+ }
21
+ const { name } = opts.service;
22
+ return {
23
+ cleanup: result.cleanup,
24
+ setServiceContext: (context)=>impl.setServiceContext(name, context)
25
+ };
26
+ }
27
+
28
+ //# sourceMappingURL=init.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/init.ts"],"sourcesContent":["import { bundleState } from './bundle-state';\nimport { getOrCreateSharedImpl } from './get-shared';\nimport type { InitRumOptions, RumServiceRegistration } from './types';\n\nexport type {\n ApplicationConfig,\n Cleanup,\n InitRumOptions,\n RumService,\n RumServiceRegistration,\n ServiceConfig,\n ServiceTracingUrls,\n} from './types';\n\n/**\n * Registers a service and, optionally, initializes the Datadog RUM SDK.\n *\n * Any caller may pass `application`. The first call that includes it\n * initializes the SDK; subsequent callers' `application` config is ignored\n * with a warning. Callers that omit `application` register their service\n * without attempting SDK init.\n *\n * Returns a `RumServiceRegistration` — `{ cleanup, setServiceContext }`.\n * `cleanup` removes this service's `beforeSend`, `allowedTracingUrls`, and\n * persistent context. `setServiceContext` attaches session-lifetime metadata\n * to every event whose `event.service` matches this service's name.\n */\nexport function init(opts: InitRumOptions): RumServiceRegistration {\n const impl = getOrCreateSharedImpl();\n const result = impl.init(opts);\n if (result.initializedSdk) {\n bundleState.isSdkInitializer = true;\n }\n const { name } = opts.service;\n return {\n cleanup: result.cleanup,\n setServiceContext: context => impl.setServiceContext(name, context),\n };\n}\n"],"names":["bundleState","getOrCreateSharedImpl","init","opts","impl","result","initializedSdk","isSdkInitializer","name","service","cleanup","setServiceContext","context"],"mappings":"AAAA,SAASA,WAAW,QAAQ,iBAAiB;AAC7C,SAASC,qBAAqB,QAAQ,eAAe;AAarD;;;;;;;;;;;;CAYC,GACD,OAAO,SAASC,KAAKC,IAAoB;IACrC,MAAMC,OAAOH;IACb,MAAMI,SAASD,KAAKF,IAAI,CAACC;IACzB,IAAIE,OAAOC,cAAc,EAAE;QACvBN,YAAYO,gBAAgB,GAAG;IACnC;IACA,MAAM,EAAEC,IAAI,EAAE,GAAGL,KAAKM,OAAO;IAC7B,OAAO;QACHC,SAASL,OAAOK,OAAO;QACvBC,mBAAmBC,CAAAA,UAAWR,KAAKO,iBAAiB,CAACH,MAAMI;IAC/D;AACJ"}
@@ -0,0 +1,13 @@
1
+ import type { RumUser } from './shared-impl';
2
+ export type { RumUser } from './shared-impl';
3
+ /**
4
+ * Sets the session user on the Datadog RUM SDK.
5
+ *
6
+ * - **SDK initialized via `init`**: only the bundle whose `init` call
7
+ * initialized the SDK may set the user; calls from any other bundle are
8
+ * silent no-ops.
9
+ * - **SDK initialized directly via `datadogRum.init`**: no gating is applied.
10
+ * - **Neither initialized**: no-op.
11
+ */
12
+ export declare function setUser(user: RumUser): void;
13
+ //# sourceMappingURL=set-user.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"set-user.d.ts","sourceRoot":"","sources":["../src/set-user.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAE7C,YAAY,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAE7C;;;;;;;;GAQG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI,CAQ3C"}
@@ -0,0 +1,22 @@
1
+ import { datadogRum } from '@datadog/browser-rum';
2
+ import { bundleState } from './bundle-state';
3
+ import { getSharedImpl } from './get-shared';
4
+ /**
5
+ * Sets the session user on the Datadog RUM SDK.
6
+ *
7
+ * - **SDK initialized via `init`**: only the bundle whose `init` call
8
+ * initialized the SDK may set the user; calls from any other bundle are
9
+ * silent no-ops.
10
+ * - **SDK initialized directly via `datadogRum.init`**: no gating is applied.
11
+ * - **Neither initialized**: no-op.
12
+ */ export function setUser(user) {
13
+ if (bundleState.isSdkInitializer) {
14
+ datadogRum.setUser(user);
15
+ return;
16
+ }
17
+ if (!getSharedImpl() && datadogRum.getInitConfiguration()) {
18
+ datadogRum.setUser(user);
19
+ }
20
+ }
21
+
22
+ //# sourceMappingURL=set-user.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/set-user.ts"],"sourcesContent":["import { datadogRum } from '@datadog/browser-rum';\nimport { bundleState } from './bundle-state';\nimport { getSharedImpl } from './get-shared';\nimport type { RumUser } from './shared-impl';\n\nexport type { RumUser } from './shared-impl';\n\n/**\n * Sets the session user on the Datadog RUM SDK.\n *\n * - **SDK initialized via `init`**: only the bundle whose `init` call\n * initialized the SDK may set the user; calls from any other bundle are\n * silent no-ops.\n * - **SDK initialized directly via `datadogRum.init`**: no gating is applied.\n * - **Neither initialized**: no-op.\n */\nexport function setUser(user: RumUser): void {\n if (bundleState.isSdkInitializer) {\n datadogRum.setUser(user);\n return;\n }\n if (!getSharedImpl() && datadogRum.getInitConfiguration()) {\n datadogRum.setUser(user);\n }\n}\n"],"names":["datadogRum","bundleState","getSharedImpl","setUser","user","isSdkInitializer","getInitConfiguration"],"mappings":"AAAA,SAASA,UAAU,QAAQ,uBAAuB;AAClD,SAASC,WAAW,QAAQ,iBAAiB;AAC7C,SAASC,aAAa,QAAQ,eAAe;AAK7C;;;;;;;;CAQC,GACD,OAAO,SAASC,QAAQC,IAAa;IACjC,IAAIH,YAAYI,gBAAgB,EAAE;QAC9BL,WAAWG,OAAO,CAACC;QACnB;IACJ;IACA,IAAI,CAACF,mBAAmBF,WAAWM,oBAAoB,IAAI;QACvDN,WAAWG,OAAO,CAACC;IACvB;AACJ"}
@@ -0,0 +1,11 @@
1
+ import type { Context, User } from '@datadog/browser-core';
2
+ import type { Cleanup, InitRumOptions } from './types';
3
+ export type RumUser = Omit<User, 'id'> & Required<Pick<User, 'id'>>;
4
+ export interface SharedImpl {
5
+ init(opts: InitRumOptions): {
6
+ cleanup: Cleanup;
7
+ initializedSdk: boolean;
8
+ };
9
+ setServiceContext(key: string, context: Context): void;
10
+ }
11
+ //# sourceMappingURL=shared-impl.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shared-impl.d.ts","sourceRoot":"","sources":["../src/shared-impl.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAEvD,MAAM,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;AAEpE,MAAM,WAAW,UAAU;IACvB,IAAI,CAAC,IAAI,EAAE,cAAc,GAAG;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,cAAc,EAAE,OAAO,CAAA;KAAE,CAAC;IAC1E,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,IAAI,CAAC;CAC1D"}
@@ -0,0 +1,3 @@
1
+ export { };
2
+
3
+ //# sourceMappingURL=shared-impl.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/shared-impl.ts"],"sourcesContent":["import type { Context, User } from '@datadog/browser-core';\nimport type { Cleanup, InitRumOptions } from './types';\n\nexport type RumUser = Omit<User, 'id'> & Required<Pick<User, 'id'>>;\n\nexport interface SharedImpl {\n init(opts: InitRumOptions): { cleanup: Cleanup; initializedSdk: boolean };\n setServiceContext(key: string, context: Context): void;\n}\n"],"names":[],"mappings":"AAKA,WAGC"}
@@ -0,0 +1,50 @@
1
+ import type { Context } from '@datadog/browser-core';
2
+ import type { MatchOption, PropagatorType, RumInitConfiguration } from '@datadog/browser-rum';
3
+ import type { ApplicationBeforeSendHandler, ServiceBeforeSendHandler } from './before-send';
4
+ /**
5
+ * URL matchers grouped by the propagator type whose headers they should
6
+ * activate. A service registers its tracing URLs as e.g.
7
+ * `{ tracecontext: [/api\//, 'https://...'] }`.
8
+ */
9
+ export type ServiceTracingUrls = Partial<Record<PropagatorType, MatchOption[]>>;
10
+ /** Tears down a service registration: handlers, tracing URLs, and persistent context. */
11
+ export type Cleanup = () => void;
12
+ /**
13
+ * Service handle returned by `useRumService`. `setServiceContext` is bound to the
14
+ * service registered by the hook, so callers don't pass the service name again.
15
+ */
16
+ export interface RumService {
17
+ setServiceContext: (context: Context) => void;
18
+ }
19
+ /** Imperative service registration returned by `init`. Adds the cleanup contract. */
20
+ export interface RumServiceRegistration extends RumService {
21
+ cleanup: Cleanup;
22
+ }
23
+ export type ApplicationConfig = Omit<RumInitConfiguration, 'beforeSend' | 'allowedTracingUrls' | 'service'> & {
24
+ beforeSend?: ApplicationBeforeSendHandler;
25
+ };
26
+ interface ServiceShape {
27
+ beforeSend?: ServiceBeforeSendHandler;
28
+ allowedTracingUrls?: ServiceTracingUrls;
29
+ }
30
+ export interface ServiceConfig extends ServiceShape {
31
+ name: string;
32
+ }
33
+ export interface InitRumOptions {
34
+ service: ServiceConfig;
35
+ application?: ApplicationConfig;
36
+ }
37
+ /**
38
+ * Per-service entry — `beforeSend` and `allowedTracingUrls` come from
39
+ * `init`; `context` comes from `setServiceContext`. One service = one entry.
40
+ */
41
+ export interface ServiceEntry extends ServiceShape {
42
+ context?: Context;
43
+ }
44
+ /** Registration state: application-level handler + per-service entries keyed by service name. */
45
+ export interface State {
46
+ applicationBeforeSend?: ApplicationBeforeSendHandler;
47
+ services: Map<string, ServiceEntry>;
48
+ }
49
+ export {};
50
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +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"}
package/dist/types.js ADDED
@@ -0,0 +1,3 @@
1
+ /** Registration state: application-level handler + per-service entries keyed by service name. */ export { };
2
+
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +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"}
@@ -0,0 +1,12 @@
1
+ import { type RumService, type ServiceConfig } from './init';
2
+ /**
3
+ * Registers a service on mount and disposes it on unmount. On unmount the
4
+ * service's `beforeSend`, `allowedTracingUrls`, and any context previously set
5
+ * via `setServiceContext` are removed atomically. The config is captured on
6
+ * first render — changing it across renders does not re-register.
7
+ *
8
+ * Returns a `RumService` — `{ setServiceContext }`, bound to this service.
9
+ * The handle is identity-stable across renders.
10
+ */
11
+ export declare function useRumService(config: ServiceConfig): RumService;
12
+ //# sourceMappingURL=use-rum-service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-rum-service.d.ts","sourceRoot":"","sources":["../src/use-rum-service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAQ,KAAK,UAAU,EAAE,KAAK,aAAa,EAAE,MAAM,QAAQ,CAAC;AAEnE;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,aAAa,GAAG,UAAU,CAc/D"}
@@ -0,0 +1,25 @@
1
+ import { useEffect, useMemo } from 'react';
2
+ import { getOrCreateSharedImpl } from './get-shared';
3
+ import { init } from './init';
4
+ /**
5
+ * Registers a service on mount and disposes it on unmount. On unmount the
6
+ * service's `beforeSend`, `allowedTracingUrls`, and any context previously set
7
+ * via `setServiceContext` are removed atomically. The config is captured on
8
+ * first render — changing it across renders does not re-register.
9
+ *
10
+ * Returns a `RumService` — `{ setServiceContext }`, bound to this service.
11
+ * The handle is identity-stable across renders.
12
+ */ export function useRumService(config) {
13
+ useEffect(()=>{
14
+ return init({
15
+ service: config
16
+ }).cleanup;
17
+ // eslint-disable-next-line react-hooks/exhaustive-deps
18
+ }, []);
19
+ return useMemo(()=>({
20
+ setServiceContext: (context)=>getOrCreateSharedImpl().setServiceContext(config.name, context)
21
+ }), // eslint-disable-next-line react-hooks/exhaustive-deps
22
+ []);
23
+ }
24
+
25
+ //# sourceMappingURL=use-rum-service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/use-rum-service.ts"],"sourcesContent":["import { useEffect, useMemo } from 'react';\nimport { getOrCreateSharedImpl } from './get-shared';\nimport { init, type RumService, type ServiceConfig } from './init';\n\n/**\n * Registers a service on mount and disposes it on unmount. On unmount the\n * service's `beforeSend`, `allowedTracingUrls`, and any context previously set\n * via `setServiceContext` are removed atomically. The config is captured on\n * first render — changing it across renders does not re-register.\n *\n * Returns a `RumService` — `{ setServiceContext }`, bound to this service.\n * The handle is identity-stable across renders.\n */\nexport function useRumService(config: ServiceConfig): RumService {\n useEffect(() => {\n return init({ service: config }).cleanup;\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n\n return useMemo(\n () => ({\n setServiceContext: context =>\n getOrCreateSharedImpl().setServiceContext(config.name, context),\n }),\n // eslint-disable-next-line react-hooks/exhaustive-deps\n []\n );\n}\n"],"names":["useEffect","useMemo","getOrCreateSharedImpl","init","useRumService","config","service","cleanup","setServiceContext","context","name"],"mappings":"AAAA,SAASA,SAAS,EAAEC,OAAO,QAAQ,QAAQ;AAC3C,SAASC,qBAAqB,QAAQ,eAAe;AACrD,SAASC,IAAI,QAA6C,SAAS;AAEnE;;;;;;;;CAQC,GACD,OAAO,SAASC,cAAcC,MAAqB;IAC/CL,UAAU;QACN,OAAOG,KAAK;YAAEG,SAASD;QAAO,GAAGE,OAAO;IACxC,uDAAuD;IAC3D,GAAG,EAAE;IAEL,OAAON,QACH,IAAO,CAAA;YACHO,mBAAmBC,CAAAA,UACfP,wBAAwBM,iBAAiB,CAACH,OAAOK,IAAI,EAAED;QAC/D,CAAA,GACA,uDAAuD;IACvD,EAAE;AAEV"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@servicetitan/datadog-rum",
3
- "version": "36.3.0",
3
+ "version": "36.4.0",
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": "^6.32.0",
25
+ "@datadog/browser-rum": "^7.2.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": "5c664540fe1fa530da3b311b28941e2fb26c6cff"
43
+ "gitHead": "8beab9ae3a70ce381e8cd53f2de8691640e96c6e"
44
44
  }
@@ -0,0 +1,6 @@
1
+ export * from './before-send-data';
2
+ export * from './init-config';
3
+ export * from './process-event';
4
+ export * from './reset-shared-impl';
5
+ export * from './tracing-urls';
6
+ export type * from './types';
@@ -0,0 +1,6 @@
1
+ import type { ApplicationConfig } from '../types';
2
+
3
+ export const minimalApplication: ApplicationConfig = {
4
+ applicationId: 'app-id',
5
+ clientToken: 'client-token',
6
+ };
@@ -0,0 +1,20 @@
1
+ import type { RumEvent, RumEventDomainContext } from '@datadog/browser-rum';
2
+ import { datadogRum } from '@datadog/browser-rum';
3
+
4
+ /**
5
+ * Invokes the `beforeSend` captured on the most recent `datadogRum.init` call
6
+ * with the given event partial. Returns whatever `beforeSend` returns (false
7
+ * drops, true/void proceeds). The helper mutates `event` in place, so callers
8
+ * that need to inspect mutations should hold a reference to the event they
9
+ * pass in.
10
+ */
11
+ export const processEvent = (
12
+ event: Partial<RumEvent>,
13
+ context: RumEventDomainContext = {}
14
+ ): boolean | void => {
15
+ const beforeSend = jest.mocked(datadogRum.init).mock.calls.at(-1)?.[0].beforeSend as (
16
+ e: RumEvent,
17
+ c: RumEventDomainContext
18
+ ) => boolean | void;
19
+ return beforeSend(event as RumEvent, context);
20
+ };
@@ -0,0 +1,8 @@
1
+ import { bundleState } from '../bundle-state';
2
+ import { STATE_KEY, type GlobalSlot } from '../get-shared';
3
+
4
+ export function resetSharedImpl(): void {
5
+ const slot = globalThis as unknown as GlobalSlot;
6
+ delete slot[STATE_KEY];
7
+ bundleState.isSdkInitializer = false;
8
+ }
@@ -0,0 +1,30 @@
1
+ import type { PropagatorType, RumInitConfiguration } from '@datadog/browser-rum';
2
+ import { datadogRum } from '@datadog/browser-rum';
3
+
4
+ export type { PropagatorType };
5
+
6
+ interface Matcher {
7
+ match: (url: string) => boolean;
8
+ propagatorTypes?: PropagatorType[] | null;
9
+ }
10
+
11
+ export function isAllowedTracingUrl(
12
+ matchers: NonNullable<RumInitConfiguration['allowedTracingUrls']>,
13
+ url: string,
14
+ propagator: PropagatorType = 'tracecontext'
15
+ ): boolean {
16
+ const matcher = matchers.find(
17
+ (candidate): candidate is Matcher =>
18
+ typeof candidate === 'object' &&
19
+ 'propagatorTypes' in candidate &&
20
+ !!candidate.propagatorTypes?.includes(propagator)
21
+ );
22
+ return matcher?.match(url) ?? false;
23
+ }
24
+
25
+ export function getInitAllowedTracingUrls(): NonNullable<
26
+ RumInitConfiguration['allowedTracingUrls']
27
+ > {
28
+ const call = jest.mocked(datadogRum.init).mock.calls.at(-1)![0];
29
+ return call.allowedTracingUrls!;
30
+ }
@@ -0,0 +1 @@
1
+ export type Mutable<T> = { -readonly [P in keyof T]: T[P] };
@@ -1,124 +1,76 @@
1
1
  import type { RumEvent, RumEventDomainContext } from '@datadog/browser-rum';
2
- import { rumEvents, RumEventType } from '../__mocks__/before-send-data';
2
+ import { type Mutable, type RumEventType } from '../__mocks__';
3
3
  import { beforeSend } from '../before-send';
4
+ import { enrichEvent } from '../enrich-event';
5
+ import { handleResource } from '../handle-resource';
4
6
 
5
- type Mutable<T> = { -readonly [P in keyof T]: T[P] };
7
+ jest.mock('../enrich-event');
8
+ jest.mock('../handle-resource');
6
9
 
7
10
  describe(`[datadog-rum] ${beforeSend.name}`, () => {
8
- let customHandler: (() => boolean) | undefined;
9
-
10
11
  let event: Mutable<RumEventType>;
11
12
  let context: RumEventDomainContext;
13
+ let customHandler: (() => boolean) | undefined;
12
14
 
13
15
  beforeEach(() => {
16
+ jest.clearAllMocks();
14
17
  context = {};
18
+ event = { type: 'action' };
15
19
  customHandler = undefined;
16
20
  });
17
21
 
18
- const subject = () => {
19
- const beforeSendHandler = beforeSend(customHandler);
20
- return beforeSendHandler(event as RumEvent, context);
21
- };
22
-
23
- describe('with an "error" event', () => {
24
- beforeEach(() => (event = { type: 'error' }));
25
-
26
- function itReturns(value: boolean) {
27
- test(`returns ${value}`, () => expect(subject()).toBe(value));
28
- }
29
-
30
- itReturns(true);
31
-
32
- describe('with custom handler', () => {
33
- beforeEach(() => {
34
- customHandler = jest.fn(() => true);
35
- });
22
+ const subject = () => beforeSend(customHandler)(event as RumEvent, context);
36
23
 
37
- itReturns(true);
24
+ function itReturns(value: boolean) {
25
+ test(`returns ${value}`, () => expect(subject()).toBe(value));
26
+ }
38
27
 
39
- test('custom handler is called', () => {
40
- subject();
28
+ itReturns(true);
41
29
 
42
- expect(customHandler).toHaveBeenCalled();
43
- });
30
+ test('calls enrichEvent with the event and context', () => {
31
+ subject();
44
32
 
45
- describe('when the handler returns false', () => {
46
- beforeEach(() => {
47
- customHandler = jest.fn(() => false);
48
- });
49
-
50
- itReturns(false);
51
- });
52
- });
33
+ expect(enrichEvent).toHaveBeenCalledWith(event, context);
53
34
  });
54
35
 
55
36
  describe('with a "resource" event', () => {
56
- const cfRay = 'foo: bar';
57
- const headers = new Map([['cf-ray', cfRay]]);
58
-
59
- beforeEach(() => (event = { type: 'resource' }));
60
-
61
- function itAddsCfRayToEventContext() {
62
- test('adds "cfRay" header to event context', () => {
63
- subject();
64
-
65
- expect(event.context!.cfRay).toEqual(cfRay);
66
- });
67
-
68
- describe('when location is different', () => {
69
- beforeEach(() => ((event.resource as any).url = 'https://example.com'));
37
+ beforeEach(() => (event.type = 'resource'));
70
38
 
71
- test('does not add "cfRay" header', () => {
72
- subject();
73
-
74
- expect(event.context?.cfRay).toBeUndefined();
75
- });
76
- });
77
- }
78
-
79
- describe('with "fetch" resource type', () => {
80
- beforeEach(() => {
81
- event.resource = { type: 'fetch', url: window.location.toString() };
82
- context = { response: { headers } };
83
- });
84
-
85
- itAddsCfRayToEventContext();
86
- });
87
-
88
- describe('with "xhr" resource type', () => {
89
- beforeEach(() => {
90
- event.resource = { type: 'xhr', url: window.location.toString() };
91
- context = { xhr: { getResponseHeader: (key: string) => headers.get(key) } };
92
- });
39
+ test('calls handleResource with the event and context', () => {
40
+ subject();
93
41
 
94
- itAddsCfRayToEventContext();
42
+ expect(handleResource).toHaveBeenCalledWith(event, context);
95
43
  });
44
+ });
96
45
 
97
- describe('with early resource (no request context)', () => {
98
- beforeEach(() => {
99
- event.resource = { type: 'xhr', url: window.location.toString() };
100
- context = {};
101
- });
46
+ describe('with a non-resource event', () => {
47
+ beforeEach(() => (event.type = 'action'));
102
48
 
103
- test('does not add "cfRay" header', () => {
104
- subject();
49
+ test('does not call handleResource', () => {
50
+ subject();
105
51
 
106
- expect(event.context?.cfRay).toBeUndefined();
107
- });
52
+ expect(handleResource).not.toHaveBeenCalled();
108
53
  });
109
54
  });
110
55
 
111
- describe.each(rumEvents)('when event.type=$event.type and $title', rumEvent => {
56
+ describe('with custom handler', () => {
57
+ let result: boolean;
58
+
112
59
  beforeEach(() => {
113
- event = rumEvent.event;
114
- context = rumEvent.context ?? {};
60
+ result = true;
61
+ customHandler = jest.fn(() => result);
115
62
  });
116
63
 
117
- test('assigns expected service and version', () => {
64
+ test('calls custom handler', () => {
118
65
  subject();
119
66
 
120
- expect(event.service).toEqual(rumEvent.service);
121
- expect(event.version).toEqual(rumEvent.version);
67
+ expect(customHandler).toHaveBeenCalled();
68
+ });
69
+
70
+ describe('when the handler returns false', () => {
71
+ beforeEach(() => (result = false));
72
+
73
+ itReturns(false);
122
74
  });
123
75
  });
124
76
  });
@@ -0,0 +1,120 @@
1
+ import { isAllowedTracingUrl, type PropagatorType } from '../__mocks__';
2
+ import { buildAllowedTracingUrls } from '../build-allowed-tracing-urls';
3
+ import type { ServiceEntry, State } from '../types';
4
+
5
+ describe(`[datadog-rum] ${buildAllowedTracingUrls.name}`, () => {
6
+ let serviceEntry: ServiceEntry;
7
+ let state: State;
8
+
9
+ beforeEach(() => {
10
+ serviceEntry = {};
11
+ state = { services: new Map() };
12
+ state.services.set('foo', serviceEntry);
13
+ });
14
+
15
+ const subject = () => buildAllowedTracingUrls(state);
16
+
17
+ test('returns matchers for propagatorTypes', () => {
18
+ expect(subject()).toEqual(
19
+ expect.arrayContaining(
20
+ ['datadog', 'tracecontext', 'b3', 'b3multi'].map(type => ({
21
+ match: expect.any(Function),
22
+ propagatorTypes: [type],
23
+ }))
24
+ )
25
+ );
26
+ });
27
+
28
+ describe.each(['datadog', 'tracecontext', 'b3', 'b3multi'] as PropagatorType[])(
29
+ 'with %s matcher',
30
+ type => {
31
+ test('traces no URLs', () => {
32
+ expect(isAllowedTracingUrl(subject(), 'https://example.com', type)).toBe(false);
33
+ });
34
+
35
+ describe('when service entry contains string (prefix) matcher', () => {
36
+ const prefix = 'https://prefix.example.com';
37
+
38
+ beforeEach(() => {
39
+ serviceEntry.allowedTracingUrls = { [type]: [prefix] };
40
+ });
41
+
42
+ test('traces URLs starting with the prefix', () => {
43
+ expect(isAllowedTracingUrl(subject(), `${prefix}/foo`, type)).toBe(true);
44
+ });
45
+ });
46
+
47
+ describe('when service entry contains RegExp matcher', () => {
48
+ const regex = /bar/;
49
+
50
+ beforeEach(() => {
51
+ serviceEntry.allowedTracingUrls = { [type]: [regex] };
52
+ });
53
+
54
+ test('traces URLs that match the pattern', () => {
55
+ expect(
56
+ isAllowedTracingUrl(
57
+ subject(),
58
+ `https://example.com/${regex.toString()}`,
59
+ type
60
+ )
61
+ ).toBe(true);
62
+ });
63
+ });
64
+
65
+ describe('when service entry contains function matcher', () => {
66
+ const predicate = 'baz';
67
+
68
+ beforeEach(() => {
69
+ serviceEntry.allowedTracingUrls = {
70
+ [type]: [(url: string) => url.includes(predicate)],
71
+ };
72
+ });
73
+
74
+ test('traces URLs for which the predicate returns true', () => {
75
+ expect(
76
+ isAllowedTracingUrl(subject(), `https://example.com/${predicate}`, type)
77
+ ).toBe(true);
78
+ });
79
+ });
80
+ }
81
+ );
82
+
83
+ describe('with multiple matchers', () => {
84
+ let matcher: (url: string) => boolean;
85
+
86
+ beforeEach(() => {
87
+ matcher = (url: string) => url.includes('foo');
88
+ serviceEntry.allowedTracingUrls = {
89
+ tracecontext: [url => matcher(url)],
90
+ b3: [/\/b3\//],
91
+ };
92
+ });
93
+
94
+ test('traces URLs matched under tracecontext', () => {
95
+ expect(isAllowedTracingUrl(subject(), 'https://foo.com', 'tracecontext')).toBe(true);
96
+ });
97
+
98
+ test('traces URLs matched under b3', () => {
99
+ expect(isAllowedTracingUrl(subject(), 'https://example.com/b3/x', 'b3')).toBe(true);
100
+ });
101
+
102
+ describe('when function matcher throws', () => {
103
+ beforeEach(() => {
104
+ matcher = () => {
105
+ throw new Error('Oops!');
106
+ };
107
+ });
108
+
109
+ test('does not trace URLs from failing matcher', () => {
110
+ expect(isAllowedTracingUrl(subject(), 'https://foo.com', 'tracecontext')).toBe(
111
+ false
112
+ );
113
+ });
114
+
115
+ test('traces URLs from healthy matcher', () => {
116
+ expect(isAllowedTracingUrl(subject(), 'https://example.com/b3/x', 'b3')).toBe(true);
117
+ });
118
+ });
119
+ });
120
+ });