@newrelic/browser-agent 0.1.230 → 1.232.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 (365) hide show
  1. package/README.md +27 -3
  2. package/dist/cjs/common/browser-version/ios-version.js +4 -3
  3. package/dist/cjs/common/config/state/configurable.js +27 -21
  4. package/dist/cjs/common/config/state/info.js +1 -1
  5. package/dist/cjs/common/config/state/init.js +9 -1
  6. package/dist/cjs/common/config/state/loader-config.js +1 -1
  7. package/dist/cjs/common/config/state/runtime.js +26 -28
  8. package/dist/cjs/common/constants/env.cdn.js +29 -0
  9. package/dist/cjs/common/constants/env.js +32 -0
  10. package/dist/cjs/common/constants/env.npm.js +30 -0
  11. package/dist/cjs/common/context/shared-context.js +2 -1
  12. package/dist/cjs/common/event-emitter/contextual-ee.test.js +282 -0
  13. package/dist/cjs/common/event-emitter/handle.test.js +58 -0
  14. package/dist/cjs/common/event-emitter/register-handler.test.js +55 -0
  15. package/dist/cjs/common/event-listener/event-listener-opts.js +4 -2
  16. package/dist/cjs/common/harvest/harvest-scheduler.js +14 -11
  17. package/dist/cjs/common/harvest/harvest.js +5 -3
  18. package/dist/cjs/common/ids/id.js +14 -6
  19. package/dist/cjs/common/ids/id.test.js +85 -0
  20. package/dist/cjs/common/ids/unique-id.js +75 -51
  21. package/dist/cjs/common/ids/unique-id.test.js +49 -0
  22. package/dist/cjs/common/session/constants.js +12 -0
  23. package/dist/cjs/common/session/session-entity.js +278 -0
  24. package/dist/cjs/common/session/session-entity.test.js +436 -0
  25. package/dist/cjs/common/storage/first-party-cookies.js +35 -0
  26. package/dist/cjs/common/storage/local-memory.js +35 -0
  27. package/dist/cjs/common/storage/local-memory.test.js +20 -0
  28. package/dist/cjs/common/storage/local-storage.js +33 -0
  29. package/dist/cjs/common/storage/local-storage.test.js +14 -0
  30. package/dist/cjs/common/timer/interaction-timer.js +78 -0
  31. package/dist/cjs/common/timer/interaction-timer.test.js +216 -0
  32. package/dist/cjs/common/timer/timer.js +32 -0
  33. package/dist/cjs/common/timer/timer.test.js +105 -0
  34. package/dist/cjs/common/timing/nav-timing.js +51 -30
  35. package/dist/cjs/common/timing/nav-timing.test.js +192 -0
  36. package/dist/cjs/common/unload/eol.js +2 -2
  37. package/dist/cjs/common/url/clean-url.test.js +9 -0
  38. package/dist/cjs/common/url/encode.test.js +74 -0
  39. package/dist/cjs/common/url/location.js +4 -0
  40. package/dist/cjs/common/url/location.test.js +13 -0
  41. package/dist/cjs/common/url/parse-url.test.js +111 -0
  42. package/dist/cjs/common/url/protocol.js +2 -12
  43. package/dist/cjs/common/url/protocol.test.js +16 -0
  44. package/dist/cjs/common/util/console.js +1 -1
  45. package/dist/cjs/common/util/data-size.js +6 -0
  46. package/dist/cjs/common/util/data-size.test.js +47 -0
  47. package/dist/cjs/common/util/invoke.js +73 -0
  48. package/dist/cjs/common/util/invoke.test.js +49 -0
  49. package/dist/cjs/common/util/map-own.test.js +3 -3
  50. package/dist/cjs/common/util/obfuscate.js +1 -5
  51. package/dist/cjs/common/window/page-visibility.js +5 -2
  52. package/dist/cjs/common/wrap/index.js +0 -7
  53. package/dist/cjs/common/wrap/wrap-events.js +6 -9
  54. package/dist/cjs/common/wrap/wrap-fetch.js +6 -6
  55. package/dist/cjs/common/wrap/wrap-history.js +7 -6
  56. package/dist/cjs/common/wrap/wrap-jsonp.js +7 -6
  57. package/dist/cjs/common/wrap/wrap-mutation.js +7 -6
  58. package/dist/cjs/common/wrap/wrap-promise.js +7 -6
  59. package/dist/cjs/common/wrap/wrap-promise.test.js +119 -0
  60. package/dist/cjs/common/wrap/wrap-raf.js +6 -6
  61. package/dist/cjs/common/wrap/wrap-timer.js +7 -7
  62. package/dist/cjs/common/wrap/wrap-xhr.js +5 -6
  63. package/dist/cjs/features/ajax/aggregate/index.js +3 -3
  64. package/dist/cjs/features/jserrors/aggregate/compute-stack-trace.test.js +5 -5
  65. package/dist/cjs/features/jserrors/aggregate/format-stack-trace.test.js +1 -1
  66. package/dist/cjs/features/jserrors/aggregate/index.js +6 -6
  67. package/dist/cjs/features/jserrors/instrument/index.js +2 -2
  68. package/dist/cjs/features/metrics/aggregate/index.js +19 -9
  69. package/dist/cjs/features/metrics/instrument/index.js +0 -25
  70. package/dist/cjs/features/metrics/instrument/workers-helper.js +5 -5
  71. package/dist/cjs/features/page_action/aggregate/index.js +3 -3
  72. package/dist/cjs/features/page_view_event/aggregate/index.js +23 -9
  73. package/dist/cjs/features/page_view_timing/aggregate/index.js +40 -30
  74. package/dist/cjs/features/session_trace/aggregate/index.js +18 -15
  75. package/dist/cjs/features/spa/aggregate/index.js +6 -5
  76. package/dist/cjs/features/utils/agent-session.js +73 -0
  77. package/dist/cjs/features/utils/feature-base.js +1 -1
  78. package/dist/cjs/features/utils/instrument-base.js +12 -3
  79. package/dist/cjs/features/utils/lazy-loader.js +2 -2
  80. package/dist/cjs/loaders/agent.js +2 -2
  81. package/dist/cjs/loaders/api/api.js +9 -9
  82. package/dist/cjs/loaders/api/apiAsync.js +3 -2
  83. package/dist/cjs/loaders/configure/configure.js +0 -6
  84. package/dist/cjs/loaders/features/enabled-features.js +1 -1
  85. package/dist/cjs/loaders/micro-agent.js +2 -1
  86. package/dist/esm/common/browser-version/ios-version.js +4 -3
  87. package/dist/esm/common/config/state/configurable.js +26 -20
  88. package/dist/esm/common/config/state/info.js +1 -1
  89. package/dist/esm/common/config/state/init.js +9 -1
  90. package/dist/esm/common/config/state/loader-config.js +1 -1
  91. package/dist/esm/common/config/state/runtime.js +26 -28
  92. package/dist/esm/common/constants/env.cdn.js +20 -0
  93. package/dist/esm/common/constants/env.js +23 -0
  94. package/dist/esm/common/constants/env.npm.js +21 -0
  95. package/dist/esm/common/context/shared-context.js +2 -1
  96. package/dist/esm/common/event-emitter/contextual-ee.test.js +278 -0
  97. package/dist/esm/common/event-emitter/handle.test.js +54 -0
  98. package/dist/esm/common/event-emitter/register-handler.test.js +51 -0
  99. package/dist/esm/common/event-listener/event-listener-opts.js +4 -2
  100. package/dist/esm/common/harvest/harvest-scheduler.js +14 -11
  101. package/dist/esm/common/harvest/harvest.js +4 -2
  102. package/dist/esm/common/ids/id.js +16 -6
  103. package/dist/esm/common/ids/id.test.js +81 -0
  104. package/dist/esm/common/ids/unique-id.js +75 -51
  105. package/dist/esm/common/ids/unique-id.test.js +44 -0
  106. package/dist/esm/common/session/constants.js +3 -0
  107. package/dist/esm/common/session/session-entity.js +271 -0
  108. package/dist/esm/common/session/session-entity.test.js +434 -0
  109. package/dist/esm/common/storage/first-party-cookies.js +28 -0
  110. package/dist/esm/common/storage/local-memory.js +28 -0
  111. package/dist/esm/common/storage/local-memory.test.js +18 -0
  112. package/dist/esm/common/storage/local-storage.js +26 -0
  113. package/dist/esm/common/storage/local-storage.test.js +12 -0
  114. package/dist/esm/common/timer/interaction-timer.js +71 -0
  115. package/dist/esm/common/timer/interaction-timer.test.js +214 -0
  116. package/dist/esm/common/timer/timer.js +25 -0
  117. package/dist/esm/common/timer/timer.test.js +103 -0
  118. package/dist/esm/common/timing/nav-timing.js +51 -29
  119. package/dist/esm/common/timing/nav-timing.test.js +190 -0
  120. package/dist/esm/common/unload/eol.js +1 -1
  121. package/dist/esm/common/url/clean-url.test.js +7 -0
  122. package/dist/esm/common/url/encode.test.js +70 -0
  123. package/dist/esm/common/url/location.js +4 -0
  124. package/dist/esm/common/url/location.test.js +11 -0
  125. package/dist/esm/common/url/parse-url.test.js +107 -0
  126. package/dist/esm/common/url/protocol.js +3 -12
  127. package/dist/esm/common/url/protocol.test.js +14 -0
  128. package/dist/esm/common/util/console.js +1 -1
  129. package/dist/esm/common/util/data-size.js +7 -0
  130. package/dist/esm/common/util/data-size.test.js +45 -0
  131. package/dist/esm/common/util/invoke.js +66 -0
  132. package/dist/esm/common/util/invoke.test.js +47 -0
  133. package/dist/esm/common/util/map-own.test.js +3 -3
  134. package/dist/esm/common/util/obfuscate.js +2 -6
  135. package/dist/esm/common/window/page-visibility.js +5 -2
  136. package/dist/esm/common/wrap/index.js +1 -2
  137. package/dist/esm/common/wrap/wrap-events.js +6 -9
  138. package/dist/esm/common/wrap/wrap-fetch.js +6 -6
  139. package/dist/esm/common/wrap/wrap-history.js +7 -6
  140. package/dist/esm/common/wrap/wrap-jsonp.js +7 -6
  141. package/dist/esm/common/wrap/wrap-mutation.js +7 -6
  142. package/dist/esm/common/wrap/wrap-promise.js +7 -6
  143. package/dist/esm/common/wrap/wrap-promise.test.js +115 -0
  144. package/dist/esm/common/wrap/wrap-raf.js +6 -6
  145. package/dist/esm/common/wrap/wrap-timer.js +7 -7
  146. package/dist/esm/common/wrap/wrap-xhr.js +5 -6
  147. package/dist/esm/features/ajax/aggregate/index.js +3 -3
  148. package/dist/esm/features/jserrors/aggregate/compute-stack-trace.test.js +5 -5
  149. package/dist/esm/features/jserrors/aggregate/format-stack-trace.test.js +1 -1
  150. package/dist/esm/features/jserrors/aggregate/index.js +6 -6
  151. package/dist/esm/features/jserrors/instrument/index.js +2 -2
  152. package/dist/esm/features/metrics/aggregate/index.js +21 -11
  153. package/dist/esm/features/metrics/instrument/index.js +0 -25
  154. package/dist/esm/features/metrics/instrument/workers-helper.js +5 -5
  155. package/dist/esm/features/page_action/aggregate/index.js +3 -3
  156. package/dist/esm/features/page_view_event/aggregate/index.js +23 -9
  157. package/dist/esm/features/page_view_timing/aggregate/index.js +40 -30
  158. package/dist/esm/features/session_trace/aggregate/index.js +18 -15
  159. package/dist/esm/features/spa/aggregate/index.js +6 -5
  160. package/dist/esm/features/utils/agent-session.js +67 -0
  161. package/dist/esm/features/utils/feature-base.js +1 -1
  162. package/dist/esm/features/utils/instrument-base.js +7 -2
  163. package/dist/esm/features/utils/lazy-loader.js +2 -2
  164. package/dist/esm/loaders/agent.js +2 -2
  165. package/dist/esm/loaders/api/api.js +6 -9
  166. package/dist/esm/loaders/api/apiAsync.js +2 -1
  167. package/dist/esm/loaders/configure/configure.js +2 -8
  168. package/dist/esm/loaders/features/enabled-features.js +1 -1
  169. package/dist/types/common/config/state/configurable.d.ts.map +1 -1
  170. package/dist/types/common/config/state/init.d.ts.map +1 -1
  171. package/dist/types/common/config/state/runtime.d.ts.map +1 -1
  172. package/dist/types/common/constants/env.cdn.d.ts +18 -0
  173. package/dist/types/common/constants/env.cdn.d.ts.map +1 -0
  174. package/dist/types/common/constants/env.d.ts +13 -0
  175. package/dist/types/common/constants/env.d.ts.map +1 -0
  176. package/dist/types/common/constants/env.npm.d.ts +19 -0
  177. package/dist/types/common/constants/env.npm.d.ts.map +1 -0
  178. package/dist/types/common/context/shared-context.d.ts.map +1 -1
  179. package/dist/types/common/event-listener/event-listener-opts.d.ts +2 -2
  180. package/dist/types/common/event-listener/event-listener-opts.d.ts.map +1 -1
  181. package/dist/types/common/harvest/harvest-scheduler.d.ts +1 -0
  182. package/dist/types/common/harvest/harvest-scheduler.d.ts.map +1 -1
  183. package/dist/types/common/harvest/harvest.d.ts.map +1 -1
  184. package/dist/types/common/ids/id.d.ts +11 -1
  185. package/dist/types/common/ids/id.d.ts.map +1 -1
  186. package/dist/types/common/ids/unique-id.d.ts +24 -1
  187. package/dist/types/common/ids/unique-id.d.ts.map +1 -1
  188. package/dist/types/common/session/constants.d.ts +4 -0
  189. package/dist/types/common/session/constants.d.ts.map +1 -0
  190. package/dist/types/common/session/session-entity.d.ts +72 -0
  191. package/dist/types/common/session/session-entity.d.ts.map +1 -0
  192. package/dist/types/common/storage/first-party-cookies.d.ts +8 -0
  193. package/dist/types/common/storage/first-party-cookies.d.ts.map +1 -0
  194. package/dist/types/common/storage/local-memory.d.ts +8 -0
  195. package/dist/types/common/storage/local-memory.d.ts.map +1 -0
  196. package/dist/types/common/storage/local-storage.d.ts +6 -0
  197. package/dist/types/common/storage/local-storage.d.ts.map +1 -0
  198. package/dist/types/common/timer/interaction-timer.d.ts +11 -0
  199. package/dist/types/common/timer/interaction-timer.d.ts.map +1 -0
  200. package/dist/types/common/timer/timer.d.ts +12 -0
  201. package/dist/types/common/timer/timer.d.ts.map +1 -0
  202. package/dist/types/common/timing/nav-timing.d.ts +1 -2
  203. package/dist/types/common/timing/nav-timing.d.ts.map +1 -1
  204. package/dist/types/common/unload/eol.d.ts.map +1 -1
  205. package/dist/types/common/url/location.d.ts +4 -0
  206. package/dist/types/common/url/location.d.ts.map +1 -1
  207. package/dist/types/common/url/parse-url.d.ts.map +1 -1
  208. package/dist/types/common/url/protocol.d.ts +1 -6
  209. package/dist/types/common/url/protocol.d.ts.map +1 -1
  210. package/dist/types/common/util/data-size.d.ts +7 -1
  211. package/dist/types/common/util/data-size.d.ts.map +1 -1
  212. package/dist/types/common/util/global-scope.d.ts.map +1 -1
  213. package/dist/types/common/util/invoke.d.ts +35 -0
  214. package/dist/types/common/util/invoke.d.ts.map +1 -0
  215. package/dist/types/common/util/obfuscate.d.ts.map +1 -1
  216. package/dist/types/common/window/page-visibility.d.ts +1 -1
  217. package/dist/types/common/window/page-visibility.d.ts.map +1 -1
  218. package/dist/types/common/wrap/index.d.ts +1 -2
  219. package/dist/types/common/wrap/index.d.ts.map +1 -1
  220. package/dist/types/common/wrap/wrap-fetch.d.ts.map +1 -1
  221. package/dist/types/common/wrap/wrap-history.d.ts.map +1 -1
  222. package/dist/types/common/wrap/wrap-jsonp.d.ts.map +1 -1
  223. package/dist/types/common/wrap/wrap-mutation.d.ts.map +1 -1
  224. package/dist/types/common/wrap/wrap-promise.d.ts.map +1 -1
  225. package/dist/types/common/wrap/wrap-raf.d.ts.map +1 -1
  226. package/dist/types/common/wrap/wrap-timer.d.ts.map +1 -1
  227. package/dist/types/common/wrap/wrap-xhr.d.ts.map +1 -1
  228. package/dist/types/features/ajax/aggregate/index.d.ts +2 -2
  229. package/dist/types/features/ajax/aggregate/index.d.ts.map +1 -1
  230. package/dist/types/features/jserrors/aggregate/index.d.ts +2 -2
  231. package/dist/types/features/jserrors/aggregate/index.d.ts.map +1 -1
  232. package/dist/types/features/jserrors/instrument/index.d.ts.map +1 -1
  233. package/dist/types/features/metrics/aggregate/index.d.ts +2 -2
  234. package/dist/types/features/metrics/aggregate/index.d.ts.map +1 -1
  235. package/dist/types/features/metrics/instrument/index.d.ts +0 -1
  236. package/dist/types/features/metrics/instrument/index.d.ts.map +1 -1
  237. package/dist/types/features/metrics/instrument/workers-helper.d.ts.map +1 -1
  238. package/dist/types/features/page_action/aggregate/index.d.ts +2 -2
  239. package/dist/types/features/page_action/aggregate/index.d.ts.map +1 -1
  240. package/dist/types/features/page_view_event/aggregate/index.d.ts +2 -2
  241. package/dist/types/features/page_view_event/aggregate/index.d.ts.map +1 -1
  242. package/dist/types/features/page_view_timing/aggregate/index.d.ts +3 -4
  243. package/dist/types/features/page_view_timing/aggregate/index.d.ts.map +1 -1
  244. package/dist/types/features/session_trace/aggregate/index.d.ts +3 -3
  245. package/dist/types/features/session_trace/aggregate/index.d.ts.map +1 -1
  246. package/dist/types/features/session_trace/instrument/index.d.ts.map +1 -1
  247. package/dist/types/features/spa/aggregate/index.d.ts +2 -2
  248. package/dist/types/features/spa/aggregate/index.d.ts.map +1 -1
  249. package/dist/types/features/spa/instrument/index.d.ts.map +1 -1
  250. package/dist/types/features/utils/agent-session.d.ts +2 -0
  251. package/dist/types/features/utils/agent-session.d.ts.map +1 -0
  252. package/dist/types/features/utils/instrument-base.d.ts.map +1 -1
  253. package/dist/types/features/utils/lazy-loader.d.ts +2 -2
  254. package/dist/types/features/utils/lazy-loader.d.ts.map +1 -1
  255. package/dist/types/loaders/api/api.d.ts.map +1 -1
  256. package/dist/types/loaders/api/apiAsync.d.ts.map +1 -1
  257. package/dist/types/loaders/configure/configure.d.ts.map +1 -1
  258. package/package.json +12 -32
  259. package/src/common/browser-version/ios-version.js +4 -3
  260. package/src/common/config/state/configurable.js +26 -19
  261. package/src/common/config/state/init.js +7 -0
  262. package/src/common/config/state/runtime.js +7 -10
  263. package/src/common/constants/env.cdn.js +20 -0
  264. package/src/common/constants/env.js +23 -0
  265. package/src/common/constants/env.npm.js +21 -0
  266. package/src/common/context/shared-context.js +2 -1
  267. package/src/common/event-emitter/contextual-ee.test.js +310 -0
  268. package/src/common/event-emitter/handle.test.js +56 -0
  269. package/src/common/event-emitter/register-handler.test.js +61 -0
  270. package/src/common/event-listener/event-listener-opts.js +4 -4
  271. package/src/common/harvest/harvest-scheduler.js +12 -8
  272. package/src/common/harvest/harvest.js +5 -3
  273. package/src/common/ids/id.js +15 -6
  274. package/src/common/ids/id.test.js +92 -0
  275. package/src/common/ids/unique-id.js +77 -54
  276. package/src/common/ids/unique-id.test.js +58 -0
  277. package/src/common/session/constants.js +3 -0
  278. package/src/common/session/session-entity.js +271 -0
  279. package/src/common/session/session-entity.test.js +317 -0
  280. package/src/common/storage/first-party-cookies.js +31 -0
  281. package/src/common/storage/local-memory.js +30 -0
  282. package/src/common/storage/local-memory.test.js +19 -0
  283. package/src/common/storage/local-storage.js +28 -0
  284. package/src/common/storage/local-storage.test.js +17 -0
  285. package/src/common/timer/interaction-timer.js +75 -0
  286. package/src/common/timer/interaction-timer.test.js +167 -0
  287. package/src/common/timer/timer.js +31 -0
  288. package/src/common/timer/timer.test.js +100 -0
  289. package/src/common/timing/nav-timing.js +50 -30
  290. package/src/common/timing/nav-timing.test.js +161 -0
  291. package/src/common/unload/eol.js +2 -3
  292. package/src/common/url/clean-url.test.js +25 -0
  293. package/src/common/url/encode.test.js +80 -0
  294. package/src/common/url/location.js +4 -0
  295. package/src/common/url/location.test.js +15 -0
  296. package/src/common/url/parse-url.js +1 -2
  297. package/src/common/url/parse-url.test.js +110 -0
  298. package/src/common/url/protocol.js +3 -13
  299. package/src/common/url/protocol.test.js +18 -0
  300. package/src/common/util/data-size.js +6 -0
  301. package/src/common/util/data-size.test.js +50 -0
  302. package/src/common/util/global-scope.js +1 -2
  303. package/src/common/util/invoke.js +55 -0
  304. package/src/common/util/invoke.test.js +65 -0
  305. package/src/common/util/obfuscate.js +2 -6
  306. package/src/common/window/page-visibility.js +3 -3
  307. package/src/common/wrap/index.js +1 -2
  308. package/src/common/wrap/wrap-events.js +5 -5
  309. package/src/common/wrap/wrap-fetch.js +4 -3
  310. package/src/common/wrap/wrap-history.js +6 -3
  311. package/src/common/wrap/wrap-jsonp.js +5 -3
  312. package/src/common/wrap/wrap-mutation.js +6 -3
  313. package/src/common/wrap/wrap-promise.js +7 -6
  314. package/src/common/wrap/wrap-promise.test.js +140 -0
  315. package/src/common/wrap/wrap-raf.js +5 -3
  316. package/src/common/wrap/wrap-timer.js +6 -4
  317. package/src/common/wrap/wrap-xhr.js +4 -3
  318. package/src/features/ajax/aggregate/index.js +2 -2
  319. package/src/features/ajax/instrument/index.js +1 -1
  320. package/src/features/jserrors/aggregate/index.js +3 -3
  321. package/src/features/jserrors/instrument/index.js +4 -2
  322. package/src/features/metrics/aggregate/index.js +21 -7
  323. package/src/features/metrics/instrument/index.js +0 -30
  324. package/src/features/metrics/instrument/workers-helper.js +9 -6
  325. package/src/features/page_action/aggregate/index.js +2 -2
  326. package/src/features/page_view_event/aggregate/index.js +21 -9
  327. package/src/features/page_view_timing/aggregate/index.js +40 -29
  328. package/src/features/page_view_timing/long-tasks.js +10 -10
  329. package/src/features/session_trace/aggregate/index.js +17 -14
  330. package/src/features/session_trace/instrument/index.js +3 -2
  331. package/src/features/spa/aggregate/index.js +5 -5
  332. package/src/features/spa/instrument/index.js +4 -2
  333. package/src/features/utils/agent-session.js +68 -0
  334. package/src/features/utils/feature-base.js +1 -1
  335. package/src/features/utils/instrument-base.js +5 -2
  336. package/src/features/utils/lazy-loader.js +1 -1
  337. package/src/loaders/agent.js +1 -1
  338. package/src/loaders/api/api.js +3 -6
  339. package/src/loaders/api/apiAsync.js +2 -1
  340. package/src/loaders/configure/configure.js +2 -7
  341. package/dist/cjs/common/constants/environment-variables.js +0 -20
  342. package/dist/cjs/common/util/single.js +0 -23
  343. package/dist/cjs/common/window/session-storage.js +0 -87
  344. package/dist/cjs/common/wrap/wrap-console.js +0 -54
  345. package/dist/cjs/features/utils/aggregate-base.js +0 -13
  346. package/dist/esm/common/constants/environment-variables.js +0 -11
  347. package/dist/esm/common/util/single.js +0 -16
  348. package/dist/esm/common/window/session-storage.js +0 -77
  349. package/dist/esm/common/wrap/wrap-console.js +0 -46
  350. package/dist/esm/features/utils/aggregate-base.js +0 -6
  351. package/dist/types/common/constants/environment-variables.d.ts +0 -4
  352. package/dist/types/common/constants/environment-variables.d.ts.map +0 -1
  353. package/dist/types/common/util/single.d.ts +0 -2
  354. package/dist/types/common/util/single.d.ts.map +0 -1
  355. package/dist/types/common/window/session-storage.d.ts +0 -18
  356. package/dist/types/common/window/session-storage.d.ts.map +0 -1
  357. package/dist/types/common/wrap/wrap-console.d.ts +0 -16
  358. package/dist/types/common/wrap/wrap-console.d.ts.map +0 -1
  359. package/dist/types/features/utils/aggregate-base.d.ts +0 -4
  360. package/dist/types/features/utils/aggregate-base.d.ts.map +0 -1
  361. package/src/common/constants/environment-variables.js +0 -11
  362. package/src/common/util/single.js +0 -18
  363. package/src/common/window/session-storage.js +0 -75
  364. package/src/common/wrap/wrap-console.js +0 -47
  365. package/src/features/utils/aggregate-base.js +0 -7
@@ -5,77 +5,100 @@
5
5
 
6
6
  import { globalScope } from '../util/global-scope'
7
7
 
8
+ const uuidv4Template = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
9
+
10
+ /**
11
+ * Creates a random single hexadecimal value from a provided random value
12
+ * table and corresponding index. If a random value table is not provided,
13
+ * Math.random will be used to generate the value.
14
+ *
15
+ * @param {Uint8Array} valueTable Random value table typically generated using
16
+ * the built-in crypto engine.
17
+ * @param {int} tableIndex The index of the value table to use for generating
18
+ * the hexadecimal value.
19
+ * @returns {int} single hexadecimal value in decimal format
20
+ */
21
+ function getRandomValue (valueTable, tableIndex) {
22
+ if (valueTable) {
23
+ /**
24
+ * The value table could have any number value in the given index. Use
25
+ * bitwise AND to ensure the value we generate is a valid hex value.
26
+ * x & 15 will ensure the value converted to hex using `toString(16)`
27
+ * falls within the range of 0 and 15 inclusively.
28
+ */
29
+ return valueTable[tableIndex] & 15
30
+ } else {
31
+ return Math.random() * 16 | 0
32
+ }
33
+ }
34
+
35
+ /**
36
+ * Generates a RFC compliant UUIDv4 using native browser crypto engine. If the browser
37
+ * does not support the crypto engine, the function will fallback to insecure Math.random()
38
+ * @returns {string} uuid version 4 string
39
+ */
8
40
  export function generateUuid () {
9
- var randomVals = null
10
- var rvIndex = 0
11
- var crypto = globalScope?.crypto || globalScope?.msCrypto
41
+ const crypto = globalScope?.crypto || globalScope?.msCrypto
42
+
43
+ let randomValueTable
44
+ let randomValueIndex = 0
12
45
  if (crypto && crypto.getRandomValues) {
13
46
  // eslint-disable-next-line
14
- randomVals = crypto.getRandomValues(new Uint8Array(31))
15
- }
16
-
17
- function getRandomValue () {
18
- if (randomVals) {
19
- // same as % 16
20
- return randomVals[rvIndex++] & 15
21
- } else {
22
- return Math.random() * 16 | 0
23
- }
47
+ randomValueTable = crypto.getRandomValues(new Uint8Array(31))
24
48
  }
25
49
 
26
- // v4 UUID
27
- var template = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
28
- var id = ''
29
- var c
30
- for (var i = 0; i < template.length; i++) {
31
- c = template[i]
32
- if (c === 'x') {
33
- id += getRandomValue().toString(16)
34
- } else if (c === 'y') {
50
+ return uuidv4Template.split('').map(templateInput => {
51
+ if (templateInput === 'x') {
52
+ return getRandomValue(randomValueTable, ++randomValueIndex).toString(16)
53
+ } else if (templateInput === 'y') {
35
54
  // this is the uuid variant per spec (8, 9, a, b)
36
55
  // % 4, then shift to get values 8-11
37
- c = getRandomValue() & 0x3 | 0x8
38
- id += c.toString(16)
56
+ return (getRandomValue() & 0x3 | 0x8).toString(16)
39
57
  } else {
40
- id += c
58
+ return templateInput
41
59
  }
42
- }
43
-
44
- return id
45
- }
46
-
47
- // 16-character hex string (per DT spec)
48
- export function generateSpanId () {
49
- return generateRandomHexString(16)
50
- }
51
-
52
- // 32-character hex string (per DT spec)
53
- export function generateTraceId () {
54
- return generateRandomHexString(32)
60
+ }).join('')
55
61
  }
56
62
 
63
+ /**
64
+ * Generates a string of the given length containing only hexadecimal
65
+ * value 0-9 and a-f.
66
+ * @param {int} length length of the string to generate
67
+ * @returns {string} generated hex string
68
+ */
57
69
  export function generateRandomHexString (length) {
58
- var randomVals = null
59
- var rvIndex = 0
60
- var crypto = self.crypto || self.msCrypto
61
- // eslint-disable-next-line
62
- if (crypto && crypto.getRandomValues && Uint8Array) {
70
+ const crypto = globalScope?.crypto || globalScope?.msCrypto
71
+
72
+ let randomValueTable
73
+ let randomValueIndex = 0
74
+ if (crypto && crypto.getRandomValues) {
63
75
  // eslint-disable-next-line
64
- randomVals = crypto.getRandomValues(new Uint8Array(31))
76
+ randomValueTable = crypto.getRandomValues(new Uint8Array(31))
65
77
  }
66
78
 
67
- var chars = []
79
+ const chars = []
68
80
  for (var i = 0; i < length; i++) {
69
- chars.push(getRandomValue().toString(16))
81
+ chars.push(getRandomValue(randomValueTable, ++randomValueIndex).toString(16))
70
82
  }
71
83
  return chars.join('')
84
+ }
72
85
 
73
- function getRandomValue () {
74
- if (randomVals) {
75
- // same as % 16
76
- return randomVals[rvIndex++] & 15
77
- } else {
78
- return Math.random() * 16 | 0
79
- }
80
- }
86
+ /**
87
+ * Generates a 16 character length hexadecimal string.
88
+ * per DT-spec.
89
+ * @see generateRandomHexString
90
+ * @returns {string} generated hex string
91
+ */
92
+ export function generateSpanId () {
93
+ return generateRandomHexString(16)
94
+ }
95
+
96
+ /**
97
+ * Generates a 32 character length hexadecimal string.
98
+ * per DT-spec.
99
+ * @see generateRandomHexString
100
+ * @returns {string} generated hex string
101
+ */
102
+ export function generateTraceId () {
103
+ return generateRandomHexString(32)
81
104
  }
@@ -0,0 +1,58 @@
1
+ import crypto from 'crypto'
2
+ import * as uniqueId from './unique-id'
3
+
4
+ const getRandomValues = jest.fn(arr => crypto.randomBytes(arr.length))
5
+
6
+ afterEach(() => {
7
+ delete global.crypto
8
+ })
9
+
10
+ describe('generateUuid', () => {
11
+ const uuidv4Regex = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/
12
+
13
+ test('should generate a uuidv4 that matches the expected format', () => {
14
+ const id = uniqueId.generateUuid()
15
+
16
+ expect(uuidv4Regex.test(id)).toEqual(true)
17
+ })
18
+
19
+ test('should support using native crypto library', () => {
20
+ global.crypto = { getRandomValues }
21
+
22
+ const id = uniqueId.generateUuid()
23
+
24
+ expect(uuidv4Regex.test(id)).toEqual(true)
25
+ expect(getRandomValues).toHaveBeenCalledTimes(1)
26
+ })
27
+ })
28
+
29
+ describe('generateRandomHexString', () => {
30
+ const hexRegex = /^[0-9a-f]{8}$/
31
+
32
+ test('should generate a valid hex string', () => {
33
+ const id = uniqueId.generateRandomHexString(8)
34
+
35
+ expect(hexRegex.test(id)).toEqual(true)
36
+ })
37
+
38
+ test('should support using native crypto library', () => {
39
+ global.crypto = { getRandomValues }
40
+
41
+ const id = uniqueId.generateRandomHexString(8)
42
+
43
+ expect(hexRegex.test(id)).toEqual(true)
44
+ expect(getRandomValues).toHaveBeenCalledTimes(1)
45
+ })
46
+ })
47
+
48
+ test('generateSpanId should generate a 16 character hex string', () => {
49
+ const id = uniqueId.generateSpanId()
50
+
51
+ expect(/^[0-9a-f]{16}$/.test(id)).toEqual(true)
52
+ })
53
+
54
+ test('generateTraceId should generate a 32 character hex string', () => {
55
+ const id = uniqueId.generateTraceId()
56
+
57
+ expect(/^[0-9a-f]{32}$/.test(id)).toEqual(true)
58
+ })
@@ -0,0 +1,3 @@
1
+ export const PREFIX = 'NRBA'
2
+ export const DEFAULT_EXPIRES_MS = 14400000
3
+ export const DEFAULT_INACTIVE_MS = 1800000
@@ -0,0 +1,271 @@
1
+ import { generateRandomHexString } from '../ids/unique-id'
2
+ import { warn } from '../util/console'
3
+ import { stringify } from '../util/stringify'
4
+ import { ee } from '../event-emitter/contextual-ee'
5
+ import { Timer } from '../timer/timer'
6
+ import { isBrowserScope } from '../util/global-scope'
7
+ import { DEFAULT_EXPIRES_MS, DEFAULT_INACTIVE_MS, PREFIX } from './constants'
8
+ import { LocalMemory } from '../storage/local-memory'
9
+ import { InteractionTimer } from '../timer/interaction-timer'
10
+ import { wrapEvents } from '../wrap'
11
+ import { Configurable } from '../config/state/configurable'
12
+ import { handle } from '../event-emitter/handle'
13
+ import { SUPPORTABILITY_METRIC_CHANNEL } from '../../features/metrics/constants'
14
+ import { FEATURE_NAMES } from '../../loaders/features/features'
15
+
16
+ const model = {
17
+ value: '',
18
+ inactiveAt: 0,
19
+ expiresAt: 0,
20
+ updatedAt: Date.now(),
21
+ sessionReplayActive: false,
22
+ sessionTraceActive: false,
23
+ custom: {}
24
+ }
25
+
26
+ export class SessionEntity {
27
+ /**
28
+ * Create a self-managing Session Entity. This entity is scoped to the agent identifier which triggered it, allowing for multiple simultaneous session objects to exist.
29
+ * There is one "namespace" an agent can store data in LS -- NRBA_{key}. If there are two agents on one page, and they both use the same key, they could overwrite each other since they would both use the same namespace in LS by default.
30
+ * The value can be overridden in the constructor, but will default to a unique 16 character hex string
31
+ * expiresMs and inactiveMs are used to "expire" the session, but can be overridden in the constructor. Pass 0 to disable expiration timers.
32
+ */
33
+ constructor (opts) {
34
+ this.setup(opts)
35
+ }
36
+
37
+ setup ({ agentIdentifier, key, value = generateRandomHexString(16), expiresMs = DEFAULT_EXPIRES_MS, inactiveMs = DEFAULT_INACTIVE_MS, storageAPI = new LocalMemory() }) {
38
+ if (!agentIdentifier || !key) throw new Error('Missing Required Fields')
39
+ if (!isBrowserScope) this.storage = new LocalMemory()
40
+ else this.storage = storageAPI
41
+
42
+ this.sync(model)
43
+
44
+ this.agentIdentifier = agentIdentifier
45
+
46
+ // key is intended to act as the k=v pair
47
+ this.key = key
48
+ // value is intended to act as the primary value of the k=v pair
49
+ this.value = value
50
+
51
+ this.expiresMs = expiresMs
52
+ this.inactiveMs = inactiveMs
53
+
54
+ this.ee = ee.get(agentIdentifier)
55
+
56
+ wrapEvents(this.ee)
57
+
58
+ // the first time the session entity class is instantiated, we check the storage API for an existing
59
+ // object. If it exists, the values inside the object are used to inform the timers that run locally.
60
+ // if the initial read is empty, it allows us to set a "fresh" "new" session immediately.
61
+ // the local timers are used after the session is running to "expire" the session, allowing for pausing timers etc.
62
+ // the timestamps stored in the storage API can be checked at initial run, and when the page is restored, otherwise we lean
63
+ // on the local timers to expire the session
64
+ const initialRead = this.read()
65
+
66
+ // the set-up of the timer used to expire the session "naturally" at a certain time
67
+ // this gets ignored if the value is falsy, allowing for session entities that do not expire
68
+ if (expiresMs) {
69
+ this.expiresAt = initialRead?.expiresAt || this.getFutureTimestamp(expiresMs)
70
+ this.expiresTimer = new Timer({
71
+ // When the inactive timer ends, collect a SM and reset the session
72
+ onEnd: () => {
73
+ this.collectSM('expired', this)
74
+ this.collectSM('duration', this)
75
+ this.reset()
76
+ }
77
+ }, this.expiresAt - Date.now())
78
+ } else {
79
+ this.expiresAt = Infinity
80
+ }
81
+
82
+ // the set-up of the timer used to expire the session due to "inactivity" at a certain time
83
+ // this gets ignored if the value is falsy, allowing for session entities that do not expire
84
+ // this gets "refreshed" when "activity" is observed
85
+ if (inactiveMs) {
86
+ this.inactiveAt = initialRead?.inactiveAt || this.getFutureTimestamp(inactiveMs)
87
+ this.inactiveTimer = new InteractionTimer({
88
+ // When the inactive timer ends, collect a SM and reset the session
89
+ onEnd: () => {
90
+ this.collectSM('inactive', this)
91
+ this.collectSM('duration', this)
92
+ this.reset()
93
+ },
94
+ // When the inactive timer refreshes, it will update the storage values with an update timestamp
95
+ onRefresh: this.refresh.bind(this),
96
+ // When the inactive timer pauses, update the storage values with an update timestamp
97
+ onPause: () => this.write(new Configurable(this.read(), model)),
98
+ ee: this.ee,
99
+ refreshEvents: ['click', 'keydown', 'scroll']
100
+ }, this.inactiveAt - Date.now())
101
+ } else {
102
+ this.inactiveAt = Infinity
103
+ }
104
+
105
+ // The fact that the session is "new" or pre-existing is used in some places in the agent. Session Replay and Trace
106
+ // can use this info to inform whether to trust a new sampling decision vs continue a previous tracking effort.
107
+ this.isNew = !Object.keys(initialRead).length
108
+ // if its a "new" session, we write to storage API with the default values. These values may change over the lifespan of the agent run.
109
+ // we can use configurable here to help us know and manage what values are being used. -- see "model" above
110
+ if (this.isNew) this.write(new Configurable(this, model), true)
111
+ else this.sync(initialRead)
112
+
113
+ this.initialized = true
114
+ }
115
+
116
+ // This is the actual key appended to the storage API
117
+ get lookupKey () {
118
+ return `${PREFIX}_${this.key}`
119
+ }
120
+
121
+ sync (data) {
122
+ Object.assign(this, data)
123
+ }
124
+
125
+ /**
126
+ * Fetch the stored values from the storage API tied to this entity
127
+ * @returns {Object}
128
+ */
129
+ read () {
130
+ try {
131
+ const val = this.storage.get(this.lookupKey)
132
+ if (!val) return {}
133
+ // TODO - decompression would need to happen here if we decide to do it
134
+ const obj = typeof val === 'string' ? JSON.parse(val) : val
135
+ if (this.isInvalid(obj)) return {}
136
+ // if the session expires, collect a SM count before resetting
137
+ if (this.isExpired(obj.expiresAt)) {
138
+ this.collectSM('expired', this)
139
+ this.collectSM('duration', obj, true)
140
+ return this.reset()
141
+ }
142
+ // if "inactive" timer is expired at "read" time -- esp. initial read -- reset
143
+ // collect a SM count before resetting
144
+ if (this.isExpired(obj.inactiveAt)) {
145
+ this.collectSM('inactive', this)
146
+ this.collectSM('duration', obj, true)
147
+ return this.reset()
148
+ }
149
+
150
+ return obj
151
+ } catch (e) {
152
+ warn('Failed to read from storage API', e)
153
+ // storage is inaccessible
154
+ return {}
155
+ }
156
+ }
157
+
158
+ /**
159
+ * Store data to the storage API tied to this entity
160
+ * To preseve existing attributes, the output of ...session.read()
161
+ * should be appended to the data argument
162
+ * @param {Object} data
163
+ * @returns {Object}
164
+ */
165
+ write (data) {
166
+ try {
167
+ if (!data || typeof data !== 'object') return
168
+ // everytime we update, we can update a timestamp for sanity
169
+ data.updatedAt = Date.now()
170
+ this.sync(data)
171
+ // TODO - compression would need happen here if we decide to do it
172
+ this.storage.set(this.lookupKey, stringify(data))
173
+ return data
174
+ } catch (e) {
175
+ // storage is inaccessible
176
+ warn('Failed to write to the storage API', e)
177
+ return null
178
+ }
179
+ }
180
+
181
+ reset () {
182
+ // this method should set off a chain of actions across the features by emitting 'new-session'
183
+ // * send off pending payloads
184
+ // * stop recording (stn and sr)...
185
+ // * delete the session and start over
186
+ try {
187
+ if (this.initialized) this.ee.emit('session-reset')
188
+
189
+ this.storage.remove(this.lookupKey)
190
+ this.inactiveTimer?.abort?.()
191
+ this.expiresTimer?.clear?.()
192
+ delete this.custom
193
+ delete this.value
194
+
195
+ this.setup({
196
+ agentIdentifier: this.agentIdentifier,
197
+ key: this.key,
198
+ storageAPI: this.storage,
199
+ expiresMs: this.expiresMs,
200
+ inactiveMs: this.inactiveMs
201
+ })
202
+ return this.read()
203
+ } catch (e) {
204
+ return {}
205
+ }
206
+ }
207
+
208
+ /**
209
+ * Refresh the inactivity timer data
210
+ */
211
+ refresh () {
212
+ // read here & invalidate
213
+ const existingData = this.read()
214
+ this.inactiveAt = this.getFutureTimestamp(this.inactiveMs)
215
+ this.write({ ...existingData, inactiveAt: this.inactiveAt })
216
+ }
217
+
218
+ /**
219
+ * @param {number} timestamp
220
+ * @returns {boolean}
221
+ */
222
+ isExpired (timestamp) {
223
+ return Date.now() > timestamp
224
+ }
225
+
226
+ /**
227
+ * @param {Object} data
228
+ * @returns {boolean}
229
+ */
230
+ isInvalid (data) {
231
+ const requiredKeys = ['value', 'expiresAt', 'inactiveAt']
232
+ return !requiredKeys.every(x => Object.keys(data).includes(x))
233
+ }
234
+
235
+ collectSM (type, data, useUpdatedAt) {
236
+ let value, tag
237
+ if (type === 'duration') {
238
+ const startingTimestamp = data.expiresAt - data.expiresMs
239
+ const endingTimestamp = useUpdatedAt ? data.updatedAt : Date.now()
240
+ value = endingTimestamp - startingTimestamp
241
+ tag = 'Session/Duration/Ms'
242
+ }
243
+ if (type === 'expired') tag = 'Session/Expired/Seen'
244
+ if (type === 'inactive') tag = 'Session/Inactive/Seen'
245
+
246
+ if (tag) handle(SUPPORTABILITY_METRIC_CHANNEL, [tag, value], undefined, FEATURE_NAMES.metrics, this.ee)
247
+ }
248
+
249
+ /**
250
+ * @param {number} futureMs - The number of ms to use to generate a future timestamp
251
+ * @returns {number}
252
+ */
253
+ getFutureTimestamp (futureMs) {
254
+ return Date.now() + futureMs
255
+ }
256
+
257
+ syncCustomAttribute (key, value) {
258
+ if (!isBrowserScope) return
259
+ if (value === null) {
260
+ const curr = this.read()
261
+ if (curr.custom) {
262
+ delete curr.custom[key]
263
+ this.write({ ...curr })
264
+ }
265
+ } else {
266
+ const curr = this.read()
267
+ this.custom = { ...(curr?.custom || {}), [key]: value }
268
+ this.write({ ...curr, custom: this.custom })
269
+ }
270
+ }
271
+ }