@newrelic/browser-agent 0.0.9 → 0.1.229

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 (339) hide show
  1. package/README.md +84 -207
  2. package/dist/cjs/cdn/lite.js +14 -0
  3. package/dist/cjs/cdn/polyfills/lite.js +4 -0
  4. package/dist/cjs/cdn/polyfills/pro.js +4 -0
  5. package/dist/cjs/cdn/polyfills/spa.js +4 -0
  6. package/dist/cjs/cdn/polyfills.js +14 -0
  7. package/dist/cjs/cdn/pro.js +19 -0
  8. package/dist/cjs/cdn/spa.js +19 -0
  9. package/dist/cjs/cdn/worker.js +16 -0
  10. package/dist/cjs/common/aggregate/aggregator.js +168 -0
  11. package/dist/cjs/common/browser-version/firefox-version.js +17 -0
  12. package/dist/cjs/common/browser-version/ios-version.js +18 -0
  13. package/dist/cjs/common/config/config.js +76 -0
  14. package/dist/cjs/common/config/state/configurable.js +32 -0
  15. package/dist/cjs/common/config/state/info.js +50 -0
  16. package/dist/cjs/common/config/state/init.js +86 -0
  17. package/dist/cjs/common/config/state/loader-config.js +28 -0
  18. package/dist/cjs/common/config/state/originals.js +9 -0
  19. package/dist/cjs/common/config/state/runtime.js +50 -0
  20. package/dist/cjs/common/constants/environment-variables.js +20 -0
  21. package/dist/cjs/common/context/shared-context.js +25 -0
  22. package/dist/cjs/common/deny-list/deny-list.js +108 -0
  23. package/dist/cjs/common/drain/drain.js +126 -0
  24. package/dist/cjs/common/event-emitter/contextual-ee.js +149 -0
  25. package/dist/cjs/common/event-emitter/handle.js +24 -0
  26. package/dist/cjs/common/event-emitter/register-handler.js +24 -0
  27. package/dist/cjs/common/event-listener/event-listener-opts.js +46 -0
  28. package/dist/cjs/common/harvest/harvest-scheduler.js +111 -0
  29. package/dist/cjs/common/harvest/harvest.js +236 -0
  30. package/dist/cjs/common/ids/id.js +30 -0
  31. package/dist/cjs/common/ids/unique-id.js +84 -0
  32. package/dist/cjs/common/metrics/framework-detection.js +72 -0
  33. package/dist/cjs/common/metrics/paint-metrics.js +13 -0
  34. package/dist/cjs/common/serialize/bel-serializer.js +89 -0
  35. package/dist/cjs/common/timing/nav-timing.js +77 -0
  36. package/dist/cjs/common/timing/now.js +15 -0
  37. package/dist/cjs/common/unload/eol.js +69 -0
  38. package/dist/cjs/common/url/clean-url.js +16 -0
  39. package/dist/cjs/common/url/encode.js +79 -0
  40. package/dist/cjs/common/url/location.js +14 -0
  41. package/dist/cjs/common/url/parse-url.js +66 -0
  42. package/dist/cjs/common/url/protocol.js +25 -0
  43. package/dist/cjs/common/util/console.js +17 -0
  44. package/dist/cjs/common/util/data-size.js +25 -0
  45. package/dist/cjs/common/util/feature-flags.js +42 -0
  46. package/dist/cjs/common/util/get-or-set.js +39 -0
  47. package/dist/cjs/common/util/global-scope.js +56 -0
  48. package/dist/cjs/common/util/map-own.js +24 -0
  49. package/dist/cjs/common/util/obfuscate.js +76 -0
  50. package/dist/cjs/common/util/reduce.js +22 -0
  51. package/dist/cjs/common/util/s-hash.js +19 -0
  52. package/dist/cjs/common/util/single.js +23 -0
  53. package/dist/cjs/common/util/stringify.js +47 -0
  54. package/dist/cjs/common/util/submit-data.js +99 -0
  55. package/dist/cjs/common/util/traverse.js +41 -0
  56. package/dist/cjs/common/util/user-agent.js +57 -0
  57. package/dist/cjs/common/window/load.js +19 -0
  58. package/dist/cjs/common/window/nreum.js +107 -0
  59. package/dist/cjs/common/window/page-visibility.js +28 -0
  60. package/dist/cjs/common/window/session-storage.js +42 -0
  61. package/dist/cjs/common/window/supports-performance-observer.js +15 -0
  62. package/dist/cjs/common/window/top-level-callers.js +23 -0
  63. package/dist/cjs/common/wrap/index.js +68 -0
  64. package/dist/cjs/common/wrap/wrap-events.js +105 -0
  65. package/dist/cjs/common/wrap/wrap-fetch.js +114 -0
  66. package/dist/cjs/common/wrap/wrap-function.js +269 -0
  67. package/dist/cjs/common/wrap/wrap-history.js +56 -0
  68. package/dist/cjs/common/wrap/wrap-jsonp.js +129 -0
  69. package/dist/cjs/common/wrap/wrap-mutation.js +61 -0
  70. package/dist/cjs/common/wrap/wrap-promise.js +160 -0
  71. package/dist/cjs/common/wrap/wrap-raf.js +55 -0
  72. package/dist/cjs/common/wrap/wrap-timer.js +70 -0
  73. package/dist/cjs/common/wrap/wrap-xhr.js +206 -0
  74. package/dist/cjs/features/ajax/aggregate/index.js +226 -0
  75. package/dist/cjs/features/ajax/constants.js +9 -0
  76. package/dist/cjs/features/ajax/index.js +12 -0
  77. package/dist/cjs/features/ajax/instrument/distributed-tracing.js +145 -0
  78. package/dist/cjs/features/ajax/instrument/index.js +338 -0
  79. package/dist/cjs/features/ajax/instrument/response-size.js +26 -0
  80. package/dist/cjs/features/jserrors/aggregate/canonical-function-name.js +18 -0
  81. package/dist/cjs/features/jserrors/aggregate/canonical-function-name.test.js +30 -0
  82. package/dist/cjs/features/jserrors/aggregate/compute-stack-trace.js +216 -0
  83. package/dist/cjs/features/jserrors/aggregate/compute-stack-trace.test.js +257 -0
  84. package/dist/cjs/features/jserrors/aggregate/format-stack-trace.js +36 -0
  85. package/dist/cjs/features/jserrors/aggregate/format-stack-trace.test.js +39 -0
  86. package/dist/cjs/features/jserrors/aggregate/index.js +267 -0
  87. package/dist/cjs/features/jserrors/aggregate/string-hash-code.js +23 -0
  88. package/dist/cjs/features/jserrors/aggregate/string-hash-code.test.js +26 -0
  89. package/dist/cjs/features/jserrors/constants.js +11 -0
  90. package/dist/cjs/features/jserrors/index.js +12 -0
  91. package/dist/cjs/features/jserrors/instrument/debug.js +40 -0
  92. package/dist/cjs/features/jserrors/instrument/index.js +158 -0
  93. package/dist/cjs/features/metrics/aggregate/index.js +136 -0
  94. package/dist/cjs/features/metrics/constants.js +17 -0
  95. package/dist/cjs/features/metrics/index.js +12 -0
  96. package/dist/cjs/features/metrics/instrument/index.js +20 -0
  97. package/dist/cjs/features/metrics/instrument/workers-helper.js +121 -0
  98. package/dist/cjs/features/page_action/aggregate/index.js +112 -0
  99. package/dist/cjs/features/page_action/constants.js +9 -0
  100. package/dist/cjs/features/page_action/index.js +12 -0
  101. package/dist/cjs/features/page_action/instrument/index.js +21 -0
  102. package/dist/cjs/features/page_view_event/aggregate/index.js +133 -0
  103. package/dist/cjs/features/page_view_event/aggregate/initialized-features.js +39 -0
  104. package/dist/cjs/features/page_view_event/constants.js +15 -0
  105. package/dist/cjs/features/page_view_event/index.js +12 -0
  106. package/dist/cjs/features/page_view_event/instrument/index.js +38 -0
  107. package/dist/cjs/features/page_view_timing/aggregate/index.js +265 -0
  108. package/dist/cjs/features/page_view_timing/constants.js +9 -0
  109. package/dist/cjs/features/page_view_timing/first-paint.js +50 -0
  110. package/dist/cjs/features/page_view_timing/index.js +12 -0
  111. package/dist/cjs/features/page_view_timing/instrument/index.js +36 -0
  112. package/dist/cjs/features/page_view_timing/long-tasks.js +75 -0
  113. package/dist/cjs/features/session_trace/aggregate/index.js +375 -0
  114. package/dist/cjs/features/session_trace/constants.js +32 -0
  115. package/dist/cjs/features/session_trace/index.js +12 -0
  116. package/dist/cjs/features/session_trace/instrument/index.js +133 -0
  117. package/dist/cjs/features/spa/aggregate/index.js +684 -0
  118. package/dist/cjs/features/spa/aggregate/interaction-node.js +84 -0
  119. package/dist/cjs/features/spa/aggregate/interaction-node.test.js +16 -0
  120. package/dist/cjs/features/spa/aggregate/interaction.js +98 -0
  121. package/dist/cjs/features/spa/aggregate/serializer.js +147 -0
  122. package/dist/cjs/features/spa/constants.js +53 -0
  123. package/dist/cjs/features/spa/index.js +12 -0
  124. package/dist/cjs/features/spa/instrument/index.js +114 -0
  125. package/dist/cjs/features/utils/aggregate-base.js +13 -0
  126. package/dist/cjs/features/utils/feature-base.js +58 -0
  127. package/dist/cjs/features/utils/handler-cache.js +64 -0
  128. package/dist/cjs/features/utils/instrument-base.js +71 -0
  129. package/dist/cjs/features/utils/lazy-loader.js +44 -0
  130. package/dist/cjs/index.js +81 -58
  131. package/dist/cjs/loaders/agent.js +86 -0
  132. package/dist/cjs/loaders/api/api.js +109 -0
  133. package/dist/cjs/loaders/api/apiAsync.js +94 -0
  134. package/dist/cjs/loaders/browser-agent.js +29 -0
  135. package/dist/cjs/loaders/configure/configure.js +47 -0
  136. package/dist/cjs/loaders/features/enabled-features.js +19 -0
  137. package/dist/cjs/loaders/features/featureDependencies.js +32 -0
  138. package/dist/cjs/loaders/features/features.js +33 -0
  139. package/dist/cjs/loaders/micro-agent.js +93 -0
  140. package/dist/cjs/loaders/worker-agent.js +24 -0
  141. package/dist/esm/cdn/lite.js +12 -0
  142. package/dist/esm/cdn/polyfills/lite.js +7 -0
  143. package/dist/esm/cdn/polyfills/pro.js +7 -0
  144. package/dist/esm/cdn/polyfills/spa.js +7 -0
  145. package/dist/esm/cdn/polyfills.js +17 -0
  146. package/dist/esm/cdn/pro.js +17 -0
  147. package/dist/esm/cdn/spa.js +17 -0
  148. package/dist/esm/cdn/worker.js +14 -0
  149. package/dist/esm/common/aggregate/aggregator.js +161 -0
  150. package/dist/esm/common/browser-version/firefox-version.js +10 -0
  151. package/dist/esm/common/browser-version/ios-version.js +10 -0
  152. package/dist/esm/common/config/config.js +11 -0
  153. package/dist/esm/common/config/state/configurable.js +25 -0
  154. package/dist/esm/common/config/state/info.js +42 -0
  155. package/dist/esm/common/config/state/init.js +78 -0
  156. package/dist/esm/common/config/state/loader-config.js +21 -0
  157. package/dist/esm/common/config/state/originals.js +2 -0
  158. package/dist/esm/common/config/state/runtime.js +41 -0
  159. package/dist/esm/common/constants/environment-variables.js +11 -0
  160. package/dist/esm/common/context/shared-context.js +18 -0
  161. package/dist/esm/common/deny-list/deny-list.js +101 -0
  162. package/dist/esm/common/drain/drain.js +119 -0
  163. package/dist/esm/common/event-emitter/contextual-ee.js +142 -0
  164. package/dist/esm/common/event-emitter/handle.js +16 -0
  165. package/dist/esm/common/event-emitter/register-handler.js +19 -0
  166. package/dist/esm/common/event-listener/event-listener-opts.js +39 -0
  167. package/dist/esm/common/harvest/harvest-scheduler.js +105 -0
  168. package/dist/esm/common/harvest/harvest.js +228 -0
  169. package/dist/esm/common/ids/id.js +23 -0
  170. package/dist/esm/common/ids/unique-id.js +75 -0
  171. package/dist/esm/common/metrics/framework-detection.js +66 -0
  172. package/dist/esm/common/metrics/paint-metrics.js +6 -0
  173. package/dist/esm/common/serialize/bel-serializer.js +80 -0
  174. package/dist/esm/common/timing/nav-timing.js +67 -0
  175. package/dist/esm/common/timing/now.js +9 -0
  176. package/dist/esm/common/unload/eol.js +62 -0
  177. package/dist/esm/common/url/clean-url.js +10 -0
  178. package/dist/esm/common/url/encode.js +71 -0
  179. package/dist/esm/common/url/location.js +8 -0
  180. package/dist/esm/common/url/parse-url.js +60 -0
  181. package/dist/esm/common/url/protocol.js +17 -0
  182. package/dist/esm/common/util/console.js +11 -0
  183. package/dist/esm/common/util/data-size.js +19 -0
  184. package/dist/esm/common/util/feature-flags.js +33 -0
  185. package/dist/esm/common/util/get-or-set.js +33 -0
  186. package/dist/esm/common/util/global-scope.js +44 -0
  187. package/dist/esm/common/util/map-own.js +18 -0
  188. package/dist/esm/common/util/obfuscate.js +67 -0
  189. package/dist/esm/common/util/reduce.js +16 -0
  190. package/dist/esm/common/util/s-hash.js +13 -0
  191. package/dist/esm/common/util/single.js +16 -0
  192. package/dist/esm/common/util/stringify.js +42 -0
  193. package/dist/esm/common/util/submit-data.js +91 -0
  194. package/dist/esm/common/util/traverse.js +35 -0
  195. package/dist/esm/common/util/user-agent.js +48 -0
  196. package/dist/esm/common/window/load.js +12 -0
  197. package/dist/esm/common/window/nreum.js +91 -0
  198. package/dist/esm/common/window/page-visibility.js +23 -0
  199. package/dist/esm/common/window/session-storage.js +36 -0
  200. package/dist/esm/common/window/supports-performance-observer.js +9 -0
  201. package/dist/esm/common/window/top-level-callers.js +17 -0
  202. package/dist/esm/common/wrap/index.js +14 -0
  203. package/dist/esm/common/wrap/wrap-events.js +97 -0
  204. package/dist/esm/common/wrap/wrap-fetch.js +105 -0
  205. package/dist/esm/common/wrap/wrap-function.js +257 -0
  206. package/dist/esm/common/wrap/wrap-history.js +48 -0
  207. package/dist/esm/common/wrap/wrap-jsonp.js +122 -0
  208. package/dist/esm/common/wrap/wrap-mutation.js +54 -0
  209. package/dist/esm/common/wrap/wrap-promise.js +153 -0
  210. package/dist/esm/common/wrap/wrap-raf.js +48 -0
  211. package/dist/esm/common/wrap/wrap-timer.js +63 -0
  212. package/dist/esm/common/wrap/wrap-xhr.js +199 -0
  213. package/dist/esm/features/ajax/aggregate/index.js +218 -0
  214. package/dist/esm/features/ajax/constants.js +2 -0
  215. package/dist/esm/features/ajax/index.js +1 -0
  216. package/dist/esm/features/ajax/instrument/distributed-tracing.js +137 -0
  217. package/dist/esm/features/ajax/instrument/index.js +330 -0
  218. package/dist/esm/features/ajax/instrument/response-size.js +19 -0
  219. package/dist/esm/features/jserrors/aggregate/canonical-function-name.js +12 -0
  220. package/dist/esm/features/jserrors/aggregate/canonical-function-name.test.js +28 -0
  221. package/dist/esm/features/jserrors/aggregate/compute-stack-trace.js +209 -0
  222. package/dist/esm/features/jserrors/aggregate/compute-stack-trace.test.js +255 -0
  223. package/dist/esm/features/jserrors/aggregate/format-stack-trace.js +29 -0
  224. package/dist/esm/features/jserrors/aggregate/format-stack-trace.test.js +37 -0
  225. package/dist/esm/features/jserrors/aggregate/index.js +260 -0
  226. package/dist/esm/features/jserrors/aggregate/string-hash-code.js +17 -0
  227. package/dist/esm/features/jserrors/aggregate/string-hash-code.test.js +24 -0
  228. package/dist/esm/features/jserrors/constants.js +3 -0
  229. package/dist/esm/features/jserrors/index.js +1 -0
  230. package/dist/esm/features/jserrors/instrument/debug.js +38 -0
  231. package/dist/esm/features/jserrors/instrument/index.js +150 -0
  232. package/dist/esm/features/metrics/aggregate/index.js +129 -0
  233. package/dist/esm/features/metrics/constants.js +6 -0
  234. package/dist/esm/features/metrics/index.js +1 -0
  235. package/dist/esm/features/metrics/instrument/index.js +13 -0
  236. package/dist/esm/features/metrics/instrument/workers-helper.js +116 -0
  237. package/dist/esm/features/page_action/aggregate/index.js +105 -0
  238. package/dist/esm/features/page_action/constants.js +2 -0
  239. package/dist/esm/features/page_action/index.js +1 -0
  240. package/dist/esm/features/page_action/instrument/index.js +14 -0
  241. package/dist/esm/features/page_view_event/aggregate/index.js +124 -0
  242. package/dist/esm/features/page_view_event/aggregate/initialized-features.js +34 -0
  243. package/dist/esm/features/page_view_event/constants.js +5 -0
  244. package/dist/esm/features/page_view_event/index.js +1 -0
  245. package/dist/esm/features/page_view_event/instrument/index.js +29 -0
  246. package/dist/esm/features/page_view_timing/aggregate/index.js +258 -0
  247. package/dist/esm/features/page_view_timing/constants.js +2 -0
  248. package/dist/esm/features/page_view_timing/first-paint.js +43 -0
  249. package/dist/esm/features/page_view_timing/index.js +1 -0
  250. package/dist/esm/features/page_view_timing/instrument/index.js +28 -0
  251. package/dist/esm/features/page_view_timing/long-tasks.js +69 -0
  252. package/dist/esm/features/session_trace/aggregate/index.js +366 -0
  253. package/dist/esm/features/session_trace/constants.js +14 -0
  254. package/dist/esm/features/session_trace/index.js +1 -0
  255. package/dist/esm/features/session_trace/instrument/index.js +123 -0
  256. package/dist/esm/features/spa/aggregate/index.js +674 -0
  257. package/dist/esm/features/spa/aggregate/interaction-node.js +78 -0
  258. package/dist/esm/features/spa/aggregate/interaction-node.test.js +14 -0
  259. package/dist/esm/features/spa/aggregate/interaction.js +92 -0
  260. package/dist/esm/features/spa/aggregate/serializer.js +139 -0
  261. package/dist/esm/features/spa/constants.js +25 -0
  262. package/dist/esm/features/spa/index.js +1 -0
  263. package/dist/esm/features/spa/instrument/index.js +104 -0
  264. package/dist/esm/features/utils/aggregate-base.js +6 -0
  265. package/dist/esm/features/utils/feature-base.js +51 -0
  266. package/dist/esm/features/utils/handler-cache.js +57 -0
  267. package/dist/esm/features/utils/instrument-base.js +69 -0
  268. package/dist/esm/features/utils/lazy-loader.js +37 -0
  269. package/dist/esm/index.js +15 -0
  270. package/dist/esm/loaders/agent.js +77 -0
  271. package/dist/esm/loaders/api/api.js +104 -0
  272. package/dist/esm/loaders/api/apiAsync.js +88 -0
  273. package/dist/esm/loaders/browser-agent.js +23 -0
  274. package/dist/esm/loaders/configure/configure.js +41 -0
  275. package/dist/esm/loaders/features/enabled-features.js +13 -0
  276. package/dist/esm/loaders/features/featureDependencies.js +25 -0
  277. package/dist/esm/loaders/features/features.js +25 -0
  278. package/dist/esm/loaders/micro-agent.js +86 -0
  279. package/dist/esm/loaders/worker-agent.js +18 -0
  280. package/package.json +204 -71
  281. package/types.ts +221 -0
  282. package/dist/bundled/es5/index.js +0 -2
  283. package/dist/bundled/es5/index.js.map +0 -1
  284. package/dist/bundled/es6/index.js +0 -2
  285. package/dist/bundled/es6/index.js.map +0 -1
  286. package/dist/cjs/index.d.ts +0 -19
  287. package/dist/cjs/index.js.map +0 -1
  288. package/dist/cjs/types.d.ts +0 -94
  289. package/dist/cjs/types.js +0 -28
  290. package/dist/cjs/types.js.map +0 -1
  291. package/dist/cjs/utils/api/api.d.ts +0 -10
  292. package/dist/cjs/utils/api/api.js +0 -40
  293. package/dist/cjs/utils/api/api.js.map +0 -1
  294. package/dist/cjs/utils/config/build-configs.d.ts +0 -6
  295. package/dist/cjs/utils/config/build-configs.js +0 -68
  296. package/dist/cjs/utils/config/build-configs.js.map +0 -1
  297. package/dist/cjs/utils/features/features.d.ts +0 -5
  298. package/dist/cjs/utils/features/features.js +0 -14
  299. package/dist/cjs/utils/features/features.js.map +0 -1
  300. package/dist/cjs/utils/features/initialize.d.ts +0 -5
  301. package/dist/cjs/utils/features/initialize.js +0 -51
  302. package/dist/cjs/utils/features/initialize.js.map +0 -1
  303. package/dist/es/index.d.ts +0 -19
  304. package/dist/es/index.js +0 -55
  305. package/dist/es/index.js.map +0 -1
  306. package/dist/es/types.d.ts +0 -94
  307. package/dist/es/types.js +0 -24
  308. package/dist/es/types.js.map +0 -1
  309. package/dist/es/utils/api/api.d.ts +0 -10
  310. package/dist/es/utils/api/api.js +0 -36
  311. package/dist/es/utils/api/api.js.map +0 -1
  312. package/dist/es/utils/config/build-configs.d.ts +0 -6
  313. package/dist/es/utils/config/build-configs.js +0 -64
  314. package/dist/es/utils/config/build-configs.js.map +0 -1
  315. package/dist/es/utils/features/features.d.ts +0 -5
  316. package/dist/es/utils/features/features.js +0 -10
  317. package/dist/es/utils/features/features.js.map +0 -1
  318. package/dist/es/utils/features/initialize.d.ts +0 -5
  319. package/dist/es/utils/features/initialize.js +0 -24
  320. package/dist/es/utils/features/initialize.js.map +0 -1
  321. package/dist/umd/index.d.ts +0 -19
  322. package/dist/umd/index.js +0 -69
  323. package/dist/umd/index.js.map +0 -1
  324. package/dist/umd/types.d.ts +0 -94
  325. package/dist/umd/types.js +0 -38
  326. package/dist/umd/types.js.map +0 -1
  327. package/dist/umd/utils/api/api.d.ts +0 -10
  328. package/dist/umd/utils/api/api.js +0 -50
  329. package/dist/umd/utils/api/api.js.map +0 -1
  330. package/dist/umd/utils/config/build-configs.d.ts +0 -6
  331. package/dist/umd/utils/config/build-configs.js +0 -78
  332. package/dist/umd/utils/config/build-configs.js.map +0 -1
  333. package/dist/umd/utils/features/features.d.ts +0 -5
  334. package/dist/umd/utils/features/features.js +0 -24
  335. package/dist/umd/utils/features/features.js.map +0 -1
  336. package/dist/umd/utils/features/initialize.d.ts +0 -5
  337. package/dist/umd/utils/features/initialize.js +0 -63
  338. package/dist/umd/utils/features/initialize.js.map +0 -1
  339. package/dist/webpack-analysis.html +0 -38
@@ -0,0 +1,228 @@
1
+ /*
2
+ * Copyright 2020 New Relic Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ import { mapOwn } from '../util/map-own';
7
+ import { obj as encodeObj, param as encodeParam } from '../url/encode';
8
+ import { stringify } from '../util/stringify';
9
+ import { submitData } from '../util/submit-data';
10
+ import { getLocation } from '../url/location';
11
+ import { getInfo, getConfigurationValue, getRuntime, getConfiguration } from '../config/config';
12
+ import { cleanURL } from '../url/clean-url';
13
+ import { now } from '../timing/now';
14
+ import { eventListenerOpts } from '../event-listener/event-listener-opts';
15
+ import { Obfuscator } from '../util/obfuscate';
16
+ import { applyFnToProps } from '../util/traverse';
17
+ import { SharedContext } from '../context/shared-context';
18
+ import { VERSION } from '../constants/environment-variables';
19
+ import { isBrowserScope, isWorkerScope } from '../util/global-scope';
20
+ const haveSendBeacon = !!navigator.sendBeacon; // only the web window obj has sendBeacon at this time, so 'false' for other envs
21
+
22
+ export class Harvest extends SharedContext {
23
+ constructor(parent) {
24
+ super(parent); // gets any allowed properties from the parent and stores them in `sharedContext`
25
+
26
+ this.tooManyRequestsDelay = getConfigurationValue(this.sharedContext.agentIdentifier, 'harvest.tooManyRequestsDelay') || 60;
27
+ this.obfuscator = new Obfuscator(this.sharedContext);
28
+ this.getScheme = () => getConfigurationValue(this.sharedContext.agentIdentifier, 'ssl') === false ? 'http' : 'https';
29
+ this._events = {};
30
+ }
31
+
32
+ /**
33
+ * Initiate a harvest from multiple sources. An event that corresponds to the endpoint
34
+ * name is emitted, which gives any listeners the opportunity to provide payload data.
35
+ *
36
+ * @param {string} endpoint - The endpoint of the harvest (jserrors, events, resources etc.)
37
+ *
38
+ * @param {object} opts
39
+ * @param {bool} opts.needResponse - Specify whether the caller expects a response data.
40
+ * @param {bool} opts.unload - Specify whether the call is a final harvest during page unload.
41
+ */
42
+ sendX(endpoint, opts, cbFinished) {
43
+ var submitMethod = getSubmitMethod(endpoint, opts);
44
+ if (!submitMethod) return false;
45
+ var options = {
46
+ retry: submitMethod.method === submitData.xhr
47
+ };
48
+ return this.obfuscator.shouldObfuscate() ? this.obfuscateAndSend(endpoint, this.createPayload(endpoint, options), opts, submitMethod, cbFinished) : this._send(endpoint, this.createPayload(endpoint, options), opts, submitMethod, cbFinished);
49
+ }
50
+
51
+ /**
52
+ * Initiate a harvest call.
53
+ *
54
+ * @param {string} endpoint - The endpoint of the harvest (jserrors, events, resources etc.)
55
+ * @param {object} nr - The loader singleton.
56
+ *
57
+ * @param {object} singlePayload - Object representing payload.
58
+ * @param {object} singlePayload.qs - Map of values that should be sent as part of the request query string.
59
+ * @param {string} singlePayload.body - String that should be sent as the body of the request.
60
+ * @param {string} singlePayload.body.e - Special case of body used for browser interactions.
61
+ *
62
+ * @param {object} opts
63
+ * @param {bool} opts.needResponse - Specify whether the caller expects a response data.
64
+ * @param {bool} opts.unload - Specify whether the call is a final harvest during page unload.
65
+ */
66
+ send(endpoint, singlePayload, opts, submitMethod, cbFinished) {
67
+ var makeBody = createAccumulator();
68
+ var makeQueryString = createAccumulator();
69
+ if (singlePayload.body) mapOwn(singlePayload.body, makeBody);
70
+ if (singlePayload.qs) mapOwn(singlePayload.qs, makeQueryString);
71
+ var payload = {
72
+ body: makeBody(),
73
+ qs: makeQueryString()
74
+ };
75
+ var caller = this.obfuscator.shouldObfuscate() ? (...args) => this.obfuscateAndSend(...args) : (...args) => this._send(...args);
76
+ return caller(endpoint, payload, opts, submitMethod, cbFinished);
77
+ }
78
+ obfuscateAndSend(endpoint, payload, opts, submitMethod, cbFinished) {
79
+ applyFnToProps(payload, (...args) => this.obfuscator.obfuscateString(...args), 'string', ['e']);
80
+ return this._send(endpoint, payload, opts, submitMethod, cbFinished);
81
+ }
82
+ _send(endpoint, payload, opts, submitMethod, cbFinished) {
83
+ var info = getInfo(this.sharedContext.agentIdentifier);
84
+ if (!info.errorBeacon) return false;
85
+ var agentRuntime = getRuntime(this.sharedContext.agentIdentifier);
86
+ if (!payload.body) {
87
+ // no payload body? nothing to send, just run onfinish stuff and return
88
+ if (cbFinished) {
89
+ cbFinished({
90
+ sent: false
91
+ });
92
+ }
93
+ return false;
94
+ }
95
+ if (!opts) opts = {};
96
+ var url = this.getScheme() + '://' + info.errorBeacon + '/' + endpoint + '/1/' + info.licenseKey + this.baseQueryString();
97
+ if (payload.qs) url += encodeObj(payload.qs, agentRuntime.maxBytes);
98
+ if (!submitMethod) {
99
+ submitMethod = getSubmitMethod(endpoint, opts);
100
+ }
101
+ var method = submitMethod.method;
102
+ var useBody = submitMethod.useBody;
103
+ var body;
104
+ var fullUrl = url;
105
+ if (useBody && endpoint === 'events') {
106
+ body = payload.body.e;
107
+ } else if (useBody) {
108
+ body = stringify(payload.body);
109
+ } else {
110
+ fullUrl = url + encodeObj(payload.body, agentRuntime.maxBytes);
111
+ }
112
+
113
+ /* Since workers don't support sendBeacon right now, or Image(), they can only use XHR method.
114
+ Because they still do permit synch XHR, the idea is that at final harvest time (worker is closing),
115
+ we just make a BLOCKING request--trivial impact--with the remaining data as a temp fill-in for sendBeacon. */
116
+ var result = method(fullUrl, body, opts.unload && isWorkerScope);
117
+ if (cbFinished && method === submitData.xhr) {
118
+ var xhr = result;
119
+ xhr.addEventListener('load', function () {
120
+ var result = {
121
+ sent: true
122
+ };
123
+ if (this.status === 429) {
124
+ result.retry = true;
125
+ result.delay = this.tooManyRequestsDelay;
126
+ } else if (this.status === 408 || this.status === 500 || this.status === 503) {
127
+ result.retry = true;
128
+ }
129
+ if (opts.needResponse) {
130
+ result.responseText = this.responseText;
131
+ }
132
+ cbFinished(result);
133
+ }, eventListenerOpts(false));
134
+ }
135
+
136
+ // if beacon request failed, retry with an alternative method -- will not happen for workers
137
+ if (!result && method === submitData.beacon) {
138
+ method = submitData.img;
139
+ result = method(url + encodeObj(payload.body, agentRuntime.maxBytes));
140
+ }
141
+ return result;
142
+ }
143
+
144
+ // The stuff that gets sent every time.
145
+ baseQueryString() {
146
+ var runtime = getRuntime(this.sharedContext.agentIdentifier);
147
+ var info = getInfo(this.sharedContext.agentIdentifier);
148
+ var location = cleanURL(getLocation());
149
+ var ref = this.obfuscator.shouldObfuscate() ? this.obfuscator.obfuscateString(location) : location;
150
+ return ['?a=' + info.applicationID, encodeParam('sa', info.sa ? '' + info.sa : ''), encodeParam('v', VERSION), transactionNameParam(info), encodeParam('ct', runtime.customTransaction), '&rst=' + now(), '&ck=0',
151
+ // ck param DEPRECATED - still expected by backend
152
+ '&s=' + (runtime.sessionId || '0'),
153
+ // the 0 id encaps all untrackable and default traffic
154
+ encodeParam('ref', ref), encodeParam('ptid', runtime.ptid ? '' + runtime.ptid : '')].join('');
155
+ }
156
+ createPayload(type, options) {
157
+ var makeBody = createAccumulator();
158
+ var makeQueryString = createAccumulator();
159
+ var listeners = this._events[type] && this._events[type] || [];
160
+ for (var i = 0; i < listeners.length; i++) {
161
+ var singlePayload = listeners[i](options);
162
+ if (!singlePayload) continue;
163
+ if (singlePayload.body) mapOwn(singlePayload.body, makeBody);
164
+ if (singlePayload.qs) mapOwn(singlePayload.qs, makeQueryString);
165
+ }
166
+ return {
167
+ body: makeBody(),
168
+ qs: makeQueryString()
169
+ };
170
+ }
171
+ on(type, listener) {
172
+ var listeners = this._events[type] || (this._events[type] = []);
173
+ listeners.push(listener);
174
+ }
175
+ resetListeners() {
176
+ mapOwn(this._events, key => {
177
+ this._events[key] = [];
178
+ });
179
+ }
180
+ }
181
+ function or(a, b) {
182
+ return a || b;
183
+ }
184
+ export function getSubmitMethod(endpoint, opts) {
185
+ opts = opts || {};
186
+ var method;
187
+ var useBody;
188
+ if (opts.needResponse) {
189
+ // currently: only STN needs a response
190
+ useBody = true;
191
+ method = submitData.xhr;
192
+ } else if (opts.unload && isBrowserScope) {
193
+ // all the features' final harvest; neither methods work outside window context
194
+ useBody = haveSendBeacon;
195
+ method = haveSendBeacon ? submitData.beacon : submitData.img; // really only IE doesn't have Beacon API for web browsers
196
+ } else {
197
+ // `submitData.beacon` was removed, there is an upper limit to the
198
+ // number of data allowed before it starts failing, so we save it only for page unloading
199
+ useBody = true;
200
+ method = submitData.xhr;
201
+ }
202
+ return {
203
+ method: method,
204
+ useBody: useBody
205
+ };
206
+ }
207
+
208
+ // Constructs the transaction name param for the beacon URL.
209
+ // Prefers the obfuscated transaction name over the plain text.
210
+ // Falls back to making up a name.
211
+ function transactionNameParam(info) {
212
+ if (info.transactionName) return encodeParam('to', info.transactionName);
213
+ return encodeParam('t', info.tNamePlain || 'Unnamed Transaction');
214
+ }
215
+
216
+ // returns a function that can be called to accumulate values to a single object
217
+ // when the function is called without parameters, then the accumulator is returned
218
+ function createAccumulator() {
219
+ var accumulator = {};
220
+ var hasData = false;
221
+ return function (key, val) {
222
+ if (val && val.length) {
223
+ accumulator[key] = val;
224
+ hasData = true;
225
+ }
226
+ if (hasData) return accumulator;
227
+ };
228
+ }
@@ -0,0 +1,23 @@
1
+ /*
2
+ * Copyright 2020 New Relic Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ // Start assigning ids at 1 so 0 can always be used for WindowOrWorkerGlobalScope, without
7
+ // actually setting it (which would create a global variable).
8
+ import { getOrSet } from '../util/get-or-set';
9
+ import { globalScope } from '../util/global-scope';
10
+ var index = 1;
11
+ var prop = 'nr@id';
12
+
13
+ // Always returns id of obj, may tag obj with an id in the process.
14
+ export function id(obj) {
15
+ var type = typeof obj;
16
+ // We can only tag objects, functions, and arrays with ids.
17
+ // For all primitive values we instead return -1.
18
+ if (!obj || !(type === 'object' || type === 'function')) return -1;
19
+ if (obj === globalScope) return 0;
20
+ return getOrSet(obj, prop, function () {
21
+ return index++;
22
+ });
23
+ }
@@ -0,0 +1,75 @@
1
+ /*
2
+ * Copyright 2020 New Relic Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ import { globalScope } from '../util/global-scope';
7
+ export function generateUuid() {
8
+ var randomVals = null;
9
+ var rvIndex = 0;
10
+ var crypto = globalScope?.crypto || globalScope?.msCrypto;
11
+ if (crypto && crypto.getRandomValues) {
12
+ // eslint-disable-next-line
13
+ randomVals = crypto.getRandomValues(new Uint8Array(31));
14
+ }
15
+ function getRandomValue() {
16
+ if (randomVals) {
17
+ // same as % 16
18
+ return randomVals[rvIndex++] & 15;
19
+ } else {
20
+ return Math.random() * 16 | 0;
21
+ }
22
+ }
23
+
24
+ // v4 UUID
25
+ var template = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
26
+ var id = '';
27
+ var c;
28
+ for (var i = 0; i < template.length; i++) {
29
+ c = template[i];
30
+ if (c === 'x') {
31
+ id += getRandomValue().toString(16);
32
+ } else if (c === 'y') {
33
+ // this is the uuid variant per spec (8, 9, a, b)
34
+ // % 4, then shift to get values 8-11
35
+ c = getRandomValue() & 0x3 | 0x8;
36
+ id += c.toString(16);
37
+ } else {
38
+ id += c;
39
+ }
40
+ }
41
+ return id;
42
+ }
43
+
44
+ // 16-character hex string (per DT spec)
45
+ export function generateSpanId() {
46
+ return generateRandomHexString(16);
47
+ }
48
+
49
+ // 32-character hex string (per DT spec)
50
+ export function generateTraceId() {
51
+ return generateRandomHexString(32);
52
+ }
53
+ export function generateRandomHexString(length) {
54
+ var randomVals = null;
55
+ var rvIndex = 0;
56
+ var crypto = self.crypto || self.msCrypto;
57
+ // eslint-disable-next-line
58
+ if (crypto && crypto.getRandomValues && Uint8Array) {
59
+ // eslint-disable-next-line
60
+ randomVals = crypto.getRandomValues(new Uint8Array(31));
61
+ }
62
+ var chars = [];
63
+ for (var i = 0; i < length; i++) {
64
+ chars.push(getRandomValue().toString(16));
65
+ }
66
+ return chars.join('');
67
+ function getRandomValue() {
68
+ if (randomVals) {
69
+ // same as % 16
70
+ return randomVals[rvIndex++] & 15;
71
+ } else {
72
+ return Math.random() * 16 | 0;
73
+ }
74
+ }
75
+ }
@@ -0,0 +1,66 @@
1
+ import { isBrowserScope } from '../util/global-scope';
2
+ var FRAMEWORKS = {
3
+ REACT: 'React',
4
+ ANGULAR: 'Angular',
5
+ ANGULARJS: 'AngularJS',
6
+ BACKBONE: 'Backbone',
7
+ EMBER: 'Ember',
8
+ VUE: 'Vue',
9
+ METEOR: 'Meteor',
10
+ ZEPTO: 'Zepto',
11
+ JQUERY: 'Jquery'
12
+ };
13
+ export function getFrameworks() {
14
+ if (!isBrowserScope) return []; // don't bother detecting frameworks if not in the main window context
15
+
16
+ var frameworks = [];
17
+ try {
18
+ if (detectReact()) frameworks.push(FRAMEWORKS.REACT);
19
+ if (detectAngularJs()) frameworks.push(FRAMEWORKS.ANGULARJS);
20
+ if (detectAngular()) frameworks.push(FRAMEWORKS.ANGULAR);
21
+ if (window.Backbone) frameworks.push(FRAMEWORKS.BACKBONE);
22
+ if (window.Ember) frameworks.push(FRAMEWORKS.EMBER);
23
+ if (window.Vue) frameworks.push(FRAMEWORKS.VUE);
24
+ if (window.Meteor) frameworks.push(FRAMEWORKS.METEOR);
25
+ if (window.Zepto) frameworks.push(FRAMEWORKS.ZEPTO);
26
+ if (window.jQuery) frameworks.push(FRAMEWORKS.JQUERY);
27
+ } catch (err) {
28
+ // not supported?
29
+ }
30
+ return frameworks;
31
+ }
32
+ function detectReact() {
33
+ try {
34
+ if (!!window.React || !!window.ReactDOM || !!window.ReactRedux) return true;
35
+ if (document.querySelector('[data-reactroot], [data-reactid]')) return true;
36
+ var divs = document.querySelectorAll('body > div');
37
+ for (var i = 0; i < divs.length; i++) {
38
+ if (Object.keys(divs[i]).indexOf('_reactRootContainer') >= 0) return true;
39
+ }
40
+ return false;
41
+ } catch (err) {
42
+ // not supported?
43
+ return false;
44
+ }
45
+ }
46
+ function detectAngularJs() {
47
+ try {
48
+ if (window.angular) return true;
49
+ if (document.querySelector('.ng-binding, [ng-app], [data-ng-app], [ng-controller], [data-ng-controller], [ng-repeat], [data-ng-repeat]')) return true;
50
+ if (document.querySelector('script[src*="angular.js"], script[src*="angular.min.js"]')) return true;
51
+ return false;
52
+ } catch (err) {
53
+ // not supported?
54
+ return false;
55
+ }
56
+ }
57
+ function detectAngular() {
58
+ try {
59
+ // eslint-disable-next-line
60
+ if (window.hasOwnProperty('ng') && window.ng.hasOwnProperty('coreTokens') && window.ng.coreTokens.hasOwnProperty('NgZone')) return true;
61
+ return !!document.querySelectorAll('[ng-version]').length;
62
+ } catch (err) {
63
+ // not supported?
64
+ return false;
65
+ }
66
+ }
@@ -0,0 +1,6 @@
1
+ /*
2
+ * Copyright 2020 New Relic Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ export const paintMetrics = {};
@@ -0,0 +1,80 @@
1
+ /*
2
+ * Copyright 2020 New Relic Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ import { mapOwn } from '../util/map-own';
7
+ import { stringify } from '../util/stringify';
8
+ import { Obfuscator } from '../util/obfuscate';
9
+ var hasOwnProp = Object.prototype.hasOwnProperty;
10
+ var MAX_ATTRIBUTES = 64;
11
+ export function nullable(val, fn, comma) {
12
+ return val || val === 0 || val === '' ? fn(val) + (comma ? ',' : '') : '!';
13
+ }
14
+ export function numeric(n, noDefault) {
15
+ if (noDefault) {
16
+ return Math.floor(n).toString(36);
17
+ }
18
+ return n === undefined || n === 0 ? '' : Math.floor(n).toString(36);
19
+ }
20
+ export function getAddStringContext(agentIdentifier) {
21
+ // eslint-disable-next-line
22
+ var stringTable = Object.hasOwnProperty('create') ? Object.create(null) : {};
23
+ var stringTableIdx = 0;
24
+ return addString;
25
+ function addString(str) {
26
+ if (typeof str === 'undefined' || str === '') return '';
27
+ var obfuscator = new Obfuscator({
28
+ agentIdentifier: agentIdentifier
29
+ });
30
+ str = String(str);
31
+ if (obfuscator.shouldObfuscate()) str = obfuscator.obfuscateString(str);
32
+ if (hasOwnProp.call(stringTable, str)) {
33
+ return numeric(stringTable[str], true);
34
+ } else {
35
+ stringTable[str] = stringTableIdx++;
36
+ return quoteString(str);
37
+ }
38
+ }
39
+ }
40
+ export function addCustomAttributes(attrs, addString) {
41
+ var attrParts = [];
42
+ mapOwn(attrs, function (key, val) {
43
+ if (attrParts.length >= MAX_ATTRIBUTES) return;
44
+ var type = 5;
45
+ var serializedValue;
46
+ // add key to string table first
47
+ key = addString(key);
48
+ switch (typeof val) {
49
+ case 'object':
50
+ if (val) {
51
+ // serialize objects to strings
52
+ serializedValue = addString(stringify(val));
53
+ } else {
54
+ // null attribute type
55
+ type = 9;
56
+ }
57
+ break;
58
+ case 'number':
59
+ type = 6;
60
+ // make sure numbers contain a `.` so they are parsed as doubles
61
+ serializedValue = val % 1 ? val : val + '.';
62
+ break;
63
+ case 'boolean':
64
+ type = val ? 7 : 8;
65
+ break;
66
+ case 'undefined':
67
+ // we treat undefined as a null attribute (since dirac does not have a concept of undefined)
68
+ type = 9;
69
+ break;
70
+ default:
71
+ serializedValue = addString(val);
72
+ }
73
+ attrParts.push([type, key + (serializedValue ? ',' + serializedValue : '')]);
74
+ });
75
+ return attrParts;
76
+ }
77
+ var escapable = /([,\\;])/g;
78
+ function quoteString(str) {
79
+ return "'" + str.replace(escapable, '\\$1');
80
+ }
@@ -0,0 +1,67 @@
1
+ /*
2
+ * Copyright 2020 New Relic Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ // We don't use JSON.stringify directly on the performance timing data for these reasons:
7
+ // * Chrome has extra data in the performance object that we don't want to send all the time (wasteful)
8
+ // * Firefox fails to stringify the native object due to - http://code.google.com/p/v8/issues/detail?id=1223
9
+ // * The variable names are long and wasteful to transmit
10
+
11
+ // Add Performance Timing values to the given object.
12
+ // * Values are written relative to an offset to reduce their length (i.e. number of characters).
13
+ // * The offset is sent with the data
14
+ // * 0's are not included unless the value is a 'relative zero'
15
+ //
16
+
17
+ var START = 'Start';
18
+ var END = 'End';
19
+ var UNLOAD_EVENT = 'unloadEvent';
20
+ var REDIRECT = 'redirect';
21
+ var DOMAIN_LOOKUP = 'domainLookup';
22
+ var ONNECT = 'onnect';
23
+ var REQUEST = 'request';
24
+ var RESPONSE = 'response';
25
+ var LOAD_EVENT = 'loadEvent';
26
+ var DOM_CONTENT_LOAD_EVENT = 'domContentLoadedEvent';
27
+ export var navTimingValues = [];
28
+ export function addPT(offset, pt, v) {
29
+ v.of = offset;
30
+ addRel(offset, offset, v, 'n');
31
+ addRel(pt[UNLOAD_EVENT + START], offset, v, 'u');
32
+ addRel(pt[REDIRECT + START], offset, v, 'r');
33
+ addRel(pt[UNLOAD_EVENT + END], offset, v, 'ue');
34
+ addRel(pt[REDIRECT + END], offset, v, 're');
35
+ addRel(pt['fetch' + START], offset, v, 'f');
36
+ addRel(pt[DOMAIN_LOOKUP + START], offset, v, 'dn');
37
+ addRel(pt[DOMAIN_LOOKUP + END], offset, v, 'dne');
38
+ addRel(pt['c' + ONNECT + START], offset, v, 'c');
39
+ addRel(pt['secureC' + ONNECT + 'ion' + START], offset, v, 's');
40
+ addRel(pt['c' + ONNECT + END], offset, v, 'ce');
41
+ addRel(pt[REQUEST + START], offset, v, 'rq');
42
+ addRel(pt[RESPONSE + START], offset, v, 'rp');
43
+ addRel(pt[RESPONSE + END], offset, v, 'rpe');
44
+ addRel(pt.domLoading, offset, v, 'dl');
45
+ addRel(pt.domInteractive, offset, v, 'di');
46
+ addRel(pt[DOM_CONTENT_LOAD_EVENT + START], offset, v, 'ds');
47
+ addRel(pt[DOM_CONTENT_LOAD_EVENT + END], offset, v, 'de');
48
+ addRel(pt.domComplete, offset, v, 'dc');
49
+ addRel(pt[LOAD_EVENT + START], offset, v, 'l');
50
+ addRel(pt[LOAD_EVENT + END], offset, v, 'le');
51
+ return v;
52
+ }
53
+
54
+ // Add Performance Navigation values to the given object
55
+ export function addPN(pn, v) {
56
+ addRel(pn.type, 0, v, 'ty');
57
+ addRel(pn.redirectCount, 0, v, 'rc');
58
+ return v;
59
+ }
60
+ export function addRel(value, offset, obj, prop) {
61
+ var relativeValue;
62
+ if (typeof value === 'number' && value > 0) {
63
+ relativeValue = Math.max(Math.round(value - offset), 0);
64
+ obj[prop] = relativeValue;
65
+ }
66
+ navTimingValues.push(relativeValue);
67
+ }
@@ -0,0 +1,9 @@
1
+ /*
2
+ * Copyright 2020 New Relic Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ // This is our own layer around performance.now. It's not strictly necessary, but we keep it in case of future mod-ing of the value for refactor purpose.
7
+ export function now() {
8
+ return Math.round(performance.now());
9
+ }
@@ -0,0 +1,62 @@
1
+ /*
2
+ * Copyright 2020 New Relic Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+ import { ffVersion } from '../browser-version/firefox-version';
6
+ import { windowAddEventListener } from '../event-listener/event-listener-opts';
7
+ import { single } from '../util/single';
8
+ import { globalScope, isWorkerScope, isBrowserScope } from '../util/global-scope';
9
+ import { subscribeToVisibilityChange } from '../window/page-visibility';
10
+ if (isWorkerScope) {
11
+ globalScope.cleanupTasks = []; // create new list on WorkerGlobalScope to track funcs to run before exiting thread
12
+
13
+ const origClose = globalScope.close;
14
+ globalScope.close = () => {
15
+ // on worker's EoL signal, execute all "listeners", e.g. final harvests
16
+ for (let task of globalScope.cleanupTasks) {
17
+ task();
18
+ }
19
+ origClose();
20
+ };
21
+ }
22
+
23
+ /**
24
+ * Subscribes a provided callback to the time/event when the agent should treat it as end-of-life.
25
+ * This is used, for example, to submit a final harvest and send all remaining data on best-effort.
26
+ * @param {function} cb - func to run before or during the last reliable event or time of an env's life span
27
+ * @param {boolean} allowBFCache - (temp) feature flag to gate new v1222 BFC support
28
+ */
29
+ export function subscribeToEOL(cb, allowBFCache) {
30
+ if (isBrowserScope) {
31
+ if (allowBFCache) {
32
+ subscribeToVisibilityChange(cb, true); // when user switches tab or hides window, esp. mobile scenario
33
+ windowAddEventListener('pagehide', cb); // when user navigates away, and because safari iOS v14.4- doesn't fully support vis change
34
+ // --this ought to be removed once support for version below 14.5 phases out
35
+ } else {
36
+ var oneCall = single(cb);
37
+
38
+ // Firefox has a bug wherein a slow-loading resource loaded from the 'pagehide'
39
+ // or 'unload' event will delay the 'load' event firing on the next page load.
40
+ // In Firefox versions that support sendBeacon, this doesn't matter, because
41
+ // we'll use it instead of an image load for our final harvest.
42
+ //
43
+ // Some Safari versions never fire the 'unload' event for pages that are being
44
+ // put into the WebKit page cache, so we *need* to use the pagehide event for
45
+ // the final submission from Safari.
46
+ //
47
+ // Generally speaking, we will try to submit our final harvest from either
48
+ // pagehide or unload, whichever comes first, but in Firefox, we need to avoid
49
+ // attempting to submit from pagehide to ensure that we don't slow down loading
50
+ // of the next page.
51
+ if (!ffVersion || navigator.sendBeacon) {
52
+ windowAddEventListener('pagehide', oneCall);
53
+ } else {
54
+ windowAddEventListener('beforeunload', oneCall);
55
+ }
56
+ windowAddEventListener('unload', oneCall);
57
+ }
58
+ } else if (isWorkerScope) {
59
+ globalScope.cleanupTasks.push(cb); // close() should run these tasks before quitting thread
60
+ }
61
+ // By default (for other env), this fn has no effect.
62
+ }
@@ -0,0 +1,10 @@
1
+ /*
2
+ * Copyright 2020 New Relic Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ var withHash = /([^?#]*)[^#]*(#[^?]*|$).*/;
7
+ var withoutHash = /([^?#]*)().*/;
8
+ export function cleanURL(url, keepHash) {
9
+ return url.replace(keepHash ? withHash : withoutHash, '$1$2');
10
+ }