@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,28 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.subscribeToVisibilityChange = subscribeToVisibilityChange;
7
+ var _eventListenerOpts = require("../event-listener/event-listener-opts");
8
+ /*
9
+ * Copyright 2020 New Relic Corporation. All rights reserved.
10
+ * SPDX-License-Identifier: Apache-2.0
11
+ */
12
+
13
+ /**
14
+ * @param {function} cb - called when a visibility change occurs with the vis state at that time
15
+ * @param {boolean} [toHiddenOnly=false] - only execute the 'cb' when the vis is changing to the hidden state; no arg is passed to 'cb' if used
16
+ * @returns void
17
+ */
18
+ function subscribeToVisibilityChange(cb, toHiddenOnly = false) {
19
+ (0, _eventListenerOpts.documentAddEventListener)('visibilitychange', handleVisibilityChange);
20
+ return;
21
+ function handleVisibilityChange() {
22
+ if (toHiddenOnly) {
23
+ // trigger cb on change to hidden state only
24
+ if (document.visibilityState == 'hidden') cb();else return;
25
+ }
26
+ cb(document.visibilityState);
27
+ }
28
+ }
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getCurrentSessionIdOrMakeNew = getCurrentSessionIdOrMakeNew;
7
+ var _uniqueId = require("../ids/unique-id");
8
+ var _globalScope = require("../util/global-scope");
9
+ /**
10
+ * Interface for Agents' usage of the Window.sessionStorage as a replacement to third-party cookies.
11
+ * (All agents on a tab|window will share the same sessionStorage.)
12
+ *
13
+ * @design https://newrelic.atlassian.net/wiki/spaces/INST/pages/2522513791/JSESSIONID+Cookie+Change+Design+Document
14
+ * @environment Browser script
15
+ */
16
+
17
+ const SESS_ID = 'NRBA_SESSION_ID'; // prevents potential key collisions in session storage
18
+
19
+ /**
20
+ * @returns {string} This tab|window's session identifier, or a new ID if not found in storage
21
+ */
22
+ function getCurrentSessionIdOrMakeNew() {
23
+ if (!_globalScope.isBrowserScope) {
24
+ return null;
25
+ }
26
+ try {
27
+ let sessionId;
28
+ if ((sessionId = window.sessionStorage.getItem(SESS_ID)) === null) {
29
+ // Generate a new one if storage is empty (no previous ID was created or currently exists)
30
+ sessionId = (0, _uniqueId.generateRandomHexString)(16);
31
+ window.sessionStorage.setItem(SESS_ID, sessionId);
32
+ }
33
+ return sessionId;
34
+ } catch (ex) {
35
+ // Session storage quota most likely 0mb and cannot be written to
36
+ // OR the agent is running in a security context that does not allow
37
+ // access to the browser sessionStorage
38
+ return null;
39
+ }
40
+ }
41
+
42
+ // In the future, we may expand sessionStorage to, say, auto-save some other agent data between page refreshes.
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.supportsPerformanceObserver = supportsPerformanceObserver;
7
+ var _globalScope = require("../util/global-scope");
8
+ /*
9
+ * Copyright 2020 New Relic Corporation. All rights reserved.
10
+ * SPDX-License-Identifier: Apache-2.0
11
+ */
12
+
13
+ function supportsPerformanceObserver() {
14
+ return typeof _globalScope.globalScope?.PerformanceObserver === 'function';
15
+ }
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.setTopLevelCallers = setTopLevelCallers;
7
+ var _nreum = require("./nreum");
8
+ function setTopLevelCallers() {
9
+ const nr = (0, _nreum.gosCDN)();
10
+ const funcs = ['setErrorHandler', 'finished', 'addToTrace', 'inlineHit', 'addRelease', 'addPageAction', 'setCurrentRouteName', 'setPageViewName', 'setCustomAttribute', 'interaction', 'noticeError'];
11
+ funcs.forEach(f => {
12
+ nr[f] = (...args) => caller(f, ...args);
13
+ });
14
+ function caller(fnName, ...args) {
15
+ let returnVals = [];
16
+ Object.values(nr.initializedAgents).forEach(val => {
17
+ if (val.exposed && val.api[fnName]) {
18
+ returnVals.push(val.api[fnName](...args));
19
+ }
20
+ });
21
+ return returnVals.length > 1 ? returnsVals : returnVals[0];
22
+ }
23
+ }
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "wrapEvents", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _wrapEvents.wrapEvents;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "wrapFetch", {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _wrapFetch.wrapFetch;
16
+ }
17
+ });
18
+ Object.defineProperty(exports, "wrapHistory", {
19
+ enumerable: true,
20
+ get: function () {
21
+ return _wrapHistory.wrapHistory;
22
+ }
23
+ });
24
+ Object.defineProperty(exports, "wrapJsonP", {
25
+ enumerable: true,
26
+ get: function () {
27
+ return _wrapJsonp.wrapJsonP;
28
+ }
29
+ });
30
+ Object.defineProperty(exports, "wrapMutation", {
31
+ enumerable: true,
32
+ get: function () {
33
+ return _wrapMutation.wrapMutation;
34
+ }
35
+ });
36
+ Object.defineProperty(exports, "wrapPromise", {
37
+ enumerable: true,
38
+ get: function () {
39
+ return _wrapPromise.wrapPromise;
40
+ }
41
+ });
42
+ Object.defineProperty(exports, "wrapRaf", {
43
+ enumerable: true,
44
+ get: function () {
45
+ return _wrapRaf.wrapRaf;
46
+ }
47
+ });
48
+ Object.defineProperty(exports, "wrapTimer", {
49
+ enumerable: true,
50
+ get: function () {
51
+ return _wrapTimer.wrapTimer;
52
+ }
53
+ });
54
+ Object.defineProperty(exports, "wrapXhr", {
55
+ enumerable: true,
56
+ get: function () {
57
+ return _wrapXhr.wrapXhr;
58
+ }
59
+ });
60
+ var _wrapFetch = require("./wrap-fetch");
61
+ var _wrapTimer = require("./wrap-timer");
62
+ var _wrapRaf = require("./wrap-raf");
63
+ var _wrapHistory = require("./wrap-history");
64
+ var _wrapJsonp = require("./wrap-jsonp");
65
+ var _wrapMutation = require("./wrap-mutation");
66
+ var _wrapPromise = require("./wrap-promise");
67
+ var _wrapXhr = require("./wrap-xhr");
68
+ var _wrapEvents = require("./wrap-events");
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.scopedEE = scopedEE;
7
+ exports.wrapEvents = wrapEvents;
8
+ var _contextualEe = require("../event-emitter/contextual-ee");
9
+ var _wrapFunction = require("./wrap-function");
10
+ var _getOrSet = require("../util/get-or-set");
11
+ var _globalScope = require("../util/global-scope");
12
+ /*
13
+ * Copyright 2020 New Relic Corporation. All rights reserved.
14
+ * SPDX-License-Identifier: Apache-2.0
15
+ */
16
+ /**
17
+ * @file Wraps `addEventListener` and `removeEventListener` for instrumentation.
18
+ * This module is used directly by: session_trace.
19
+ * It is also called by -> wrapXhr <-, so see "wrap-xhr.js" for features that use this indirectly.
20
+ */
21
+
22
+ const wrapped = {};
23
+ const XHR = XMLHttpRequest;
24
+ const ADD_EVENT_LISTENER = 'addEventListener';
25
+ const REMOVE_EVENT_LISTENER = 'removeEventListener';
26
+
27
+ /**
28
+ * Wraps `addEventListener` and `removeEventListener` on: global scope; the prototype of `XMLHttpRequest`, and
29
+ * `document` (if in a browser scope). Adds custom events in context of a new emitter scoped only to these methods.
30
+ * @param {Object} sharedEE - The shared event emitter on which a new scoped
31
+ * event emitter will be based.
32
+ * @returns {Object} Scoped event emitter with a debug ID of `events`.
33
+ */
34
+ function wrapEvents(sharedEE) {
35
+ var ee = scopedEE(sharedEE);
36
+ if (wrapped[ee.debugId]++)
37
+ // Notice if our wrapping never ran yet, the falsey NaN will not early return; but if it has,
38
+ {
39
+ return ee;
40
+ } // then we increment the count to track # of feats using this at runtime.
41
+ wrapped[ee.debugId] = 1;
42
+ var wrapFn = (0, _wrapFunction.createWrapperWithEmitter)(ee, true);
43
+
44
+ // Guard against instrumenting environments w/o necessary features
45
+ if ('getPrototypeOf' in Object) {
46
+ if (_globalScope.isBrowserScope) {
47
+ findEventListenerProtoAndCb(document, wrapNode);
48
+ }
49
+ findEventListenerProtoAndCb(_globalScope.globalScope, wrapNode);
50
+ findEventListenerProtoAndCb(XHR.prototype, wrapNode);
51
+ }
52
+ ee.on(ADD_EVENT_LISTENER + '-start', function (args, target) {
53
+ var originalListener = args[1];
54
+ if (originalListener === null || typeof originalListener !== 'function' && typeof originalListener !== 'object') {
55
+ return;
56
+ }
57
+ var wrapped = (0, _getOrSet.getOrSet)(originalListener, 'nr@wrapped', function () {
58
+ var listener = {
59
+ object: wrapHandleEvent,
60
+ function: originalListener
61
+ }[typeof originalListener];
62
+ return listener ? wrapFn(listener, 'fn-', null, listener.name || 'anonymous') : originalListener;
63
+ function wrapHandleEvent() {
64
+ if (typeof originalListener.handleEvent !== 'function') return;
65
+ return originalListener.handleEvent.apply(originalListener, arguments);
66
+ }
67
+ });
68
+ this.wrapped = args[1] = wrapped;
69
+ });
70
+ ee.on(REMOVE_EVENT_LISTENER + '-start', function (args) {
71
+ args[1] = this.wrapped || args[1];
72
+ });
73
+ function wrapNode(node) {
74
+ wrapFn.inPlace(node, [ADD_EVENT_LISTENER, REMOVE_EVENT_LISTENER], '-', uniqueListener);
75
+ }
76
+ function uniqueListener(args, obj) {
77
+ // Context for the listener is stored on itself.
78
+ return args[1];
79
+ }
80
+ return ee;
81
+ }
82
+ /**
83
+ * Find the base prototype of 'object' that has its own "addEventListener" property, and run some function on it.
84
+ * @param {Object} object - the initial object to traverse prototype chain on
85
+ * @param {Function} cb - the function to run on the ancestral object once found, accepts an object as a arg
86
+ * @param {Array} rest - [optional] any additional arguments to pass to the cb
87
+ */
88
+ function findEventListenerProtoAndCb(object, cb, ...rest) {
89
+ let step = object;
90
+ while (typeof step === 'object' && !Object.prototype.hasOwnProperty.call(step, ADD_EVENT_LISTENER)) {
91
+ step = Object.getPrototypeOf(step);
92
+ }
93
+ if (step) cb(step, ...rest);
94
+ }
95
+
96
+ /**
97
+ * Returns an event emitter scoped specifically for the `events` context. This scoping is a remnant from when all the
98
+ * features shared the same group in the event, to isolate events between features. It will likely be revisited.
99
+ * @param {Object} sharedEE - Optional event emitter on which to base the scoped emitter.
100
+ * Uses `ee` on the global scope if undefined).
101
+ * @returns {Object} Scoped event emitter with a debug ID of 'events'.
102
+ */
103
+ function scopedEE(sharedEE) {
104
+ return (sharedEE || _contextualEe.ee).get('events');
105
+ }
@@ -0,0 +1,114 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.scopedEE = scopedEE;
7
+ exports.wrapFetch = wrapFetch;
8
+ var _contextualEe = require("../event-emitter/contextual-ee");
9
+ var _lodash = _interopRequireDefault(require("lodash._slice"));
10
+ var _globalScope = require("../util/global-scope");
11
+ var _wrapFunction = require("./wrap-function");
12
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
+ /*
14
+ * Copyright 2020 New Relic Corporation. All rights reserved.
15
+ * SPDX-License-Identifier: Apache-2.0
16
+ */
17
+ /**
18
+ * @file Wraps `fetch` and related methods for instrumentation.
19
+ * This module is used by: ajax, spa.
20
+ */
21
+
22
+ var prefix = 'fetch-';
23
+ var bodyPrefix = prefix + 'body-';
24
+ var bodyMethods = ['arrayBuffer', 'blob', 'json', 'text', 'formData'];
25
+ var Req = _globalScope.globalScope.Request;
26
+ var Res = _globalScope.globalScope.Response;
27
+ var proto = 'prototype';
28
+ var ctxId = 'nr@context';
29
+ const wrapped = {};
30
+
31
+ /**
32
+ * Wraps the `fetch` method of the global scope for instrumentation. Also wraps the prototypes of the async methods
33
+ * that parse Request and Response bodies to generate start and end events for each, in context of a new event
34
+ * emitter scoped only to fetch and related methods.
35
+ * @param {Object} sharedEE - The shared event emitter on which a new scoped
36
+ * event emitter will be based.
37
+ * @returns {Object} Scoped event emitter with a debug ID of `fetch`.
38
+ */
39
+ function wrapFetch(sharedEE) {
40
+ const ee = scopedEE(sharedEE);
41
+ if (!(Req && Res && _globalScope.globalScope.fetch)) {
42
+ return ee;
43
+ }
44
+ if (wrapped[ee.debugId]++)
45
+ // Notice if our wrapping never ran yet, the falsey NaN will not early return; but if it has,
46
+ {
47
+ return ee;
48
+ } // then we increment the count to track # of feats using this at runtime.
49
+ wrapped[ee.debugId] = 1;
50
+ bodyMethods.forEach(method => {
51
+ wrapPromiseMethod(Req[proto], method, bodyPrefix);
52
+ wrapPromiseMethod(Res[proto], method, bodyPrefix);
53
+ });
54
+ wrapPromiseMethod(_globalScope.globalScope, 'fetch', prefix);
55
+ ee.on(prefix + 'end', function (err, res) {
56
+ var ctx = this;
57
+ if (res) {
58
+ var size = res.headers.get('content-length');
59
+ if (size !== null) {
60
+ ctx.rxSize = size;
61
+ }
62
+ ee.emit(prefix + 'done', [null, res], ctx);
63
+ } else {
64
+ ee.emit(prefix + 'done', [err], ctx);
65
+ }
66
+ });
67
+
68
+ /**
69
+ * Wraps a Promise-returning function (referenced by `target[name]`) to emit custom events before and after
70
+ * execution, each decorated with metadata (arguments, payloads, errors). Used to wrap the async body
71
+ * parsing methods of Request and Response (e.g. `json`, `text`, `formData`).
72
+ * @param {Object} target - The object having the method to be wrapped.
73
+ * @param {string} name - The name of the method to wrap.
74
+ * @param {string} prefix - Used to decorate event names with context.
75
+ */
76
+ function wrapPromiseMethod(target, name, prefix) {
77
+ var fn = target[name];
78
+ if (typeof fn === 'function') {
79
+ target[name] = function () {
80
+ var args = (0, _lodash.default)(arguments);
81
+ var ctx = {};
82
+ // we are wrapping args in an array so we can preserve the reference
83
+ ee.emit(prefix + 'before-start', [args], ctx);
84
+ var dtPayload;
85
+ if (ctx[ctxId] && ctx[ctxId].dt) dtPayload = ctx[ctxId].dt;
86
+ var origPromiseFromFetch = fn.apply(this, args);
87
+ ee.emit(prefix + 'start', [args, dtPayload], origPromiseFromFetch);
88
+
89
+ // Note we need to cast the returned (orig) Promise from native APIs into the current global Promise, which may or may not be our WrappedPromise.
90
+ return origPromiseFromFetch.then(function (val) {
91
+ ee.emit(prefix + 'end', [null, val], origPromiseFromFetch);
92
+ return val;
93
+ }, function (err) {
94
+ ee.emit(prefix + 'end', [err], origPromiseFromFetch);
95
+ throw err;
96
+ });
97
+ };
98
+ target[name][_wrapFunction.flag] = fn; // track original similar to in wrap-function.js, so that they can be unwrapped with ease
99
+ }
100
+ }
101
+
102
+ return ee;
103
+ }
104
+
105
+ /**
106
+ * Returns an event emitter scoped specifically for the `fetch` context. This scoping is a remnant from when all the
107
+ * features shared the same group in the event, to isolate events between features. It will likely be revisited.
108
+ * @param {Object} sharedEE - Optional event emitter on which to base the scoped emitter.
109
+ * Uses `ee` on the global scope if undefined).
110
+ * @returns {Object} Scoped event emitter with a debug ID of 'fetch'.
111
+ */
112
+ function scopedEE(sharedEE) {
113
+ return (sharedEE || _contextualEe.ee).get('fetch');
114
+ }
@@ -0,0 +1,269 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createWrapperWithEmitter = createWrapperWithEmitter;
7
+ exports.flag = exports.default = void 0;
8
+ exports.unwrapFunction = unwrapFunction;
9
+ exports.wrapFunction = wrapFunction;
10
+ exports.wrapInPlace = wrapInPlace;
11
+ var _contextualEe = require("../event-emitter/contextual-ee");
12
+ var _lodash = _interopRequireDefault(require("lodash._slice"));
13
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
14
+ /*
15
+ * Copyright 2020 New Relic Corporation. All rights reserved.
16
+ * SPDX-License-Identifier: Apache-2.0
17
+ */
18
+ /**
19
+ * @file Provides helper functions for wrapping functions in various scenarios.
20
+ */
21
+
22
+ const flag = 'nr@original';
23
+
24
+ /**
25
+ * A convenience alias of `hasOwnProperty`.
26
+ * @type {function}
27
+ */
28
+ exports.flag = flag;
29
+ var has = Object.prototype.hasOwnProperty;
30
+
31
+ /**
32
+ * For tracking whether an event is already being emitted inside the wrapper.
33
+ * @type {boolean}
34
+ */
35
+ var inWrapper = false;
36
+
37
+ // eslint-disable-next-line
38
+ var _default = createWrapperWithEmitter;
39
+ /**
40
+ * Wraps a function in order to emit events on start, end, and error.
41
+ * @param {Object} [emitter] - The desired emitter for events emitted by the wrapper. Defaults to the global emitter.
42
+ * @param {boolean} always - If `true`, emit events even if already emitting an event.
43
+ * @returns {function} The wrapped function.
44
+ */
45
+ exports.default = _default;
46
+ function createWrapperWithEmitter(emitter, always) {
47
+ emitter || (emitter = _contextualEe.ee);
48
+ wrapFn.inPlace = inPlace;
49
+
50
+ /**
51
+ * A flag used to determine if a native function has already been wrapped.
52
+ * As a property on a wrapped function, contains the original function.
53
+ * @type {string}
54
+ */
55
+ wrapFn.flag = flag;
56
+ return wrapFn;
57
+
58
+ /**
59
+ * Wraps a function with event emitting functionality.
60
+ * @param {function} fn - The function to wrap.
61
+ * @param {string} prefix - A prefix for the names of emitted events.
62
+ * @param {function|object} getContext - The function or object that will serve as the 'this' context for handlers of events emitted by this wrapper.
63
+ * @param {string} methodName - The name of the method being wrapped.
64
+ * @param {boolean} bubble - If true, emitted events should also bubble up to the old emitter upon which the `emitter` in the current scope was based (if it defines one).
65
+ * @returns {function} The wrapped function.
66
+ */
67
+ function wrapFn(fn, prefix, getContext, methodName, bubble) {
68
+ // Unless fn is both wrappable and unwrapped, return it unchanged.
69
+ if (notWrappable(fn)) return fn;
70
+ if (!prefix) prefix = '';
71
+ nrWrapper[flag] = fn;
72
+ copy(fn, nrWrapper, emitter);
73
+ return nrWrapper;
74
+
75
+ /**
76
+ * A wrapper function that emits events before and after calling the wrapped function.
77
+ * Any arguments will be passed along to the original function.
78
+ * @returns {any} The return value of the wrapped function.
79
+ */
80
+ function nrWrapper() {
81
+ var args;
82
+ var originalThis;
83
+ var ctx;
84
+ var result;
85
+ try {
86
+ originalThis = this;
87
+ args = (0, _lodash.default)(arguments);
88
+ if (typeof getContext === 'function') {
89
+ ctx = getContext(args, originalThis);
90
+ } else {
91
+ ctx = getContext || {};
92
+ }
93
+ } catch (e) {
94
+ report([e, '', [args, originalThis, methodName], ctx], emitter);
95
+ }
96
+
97
+ // Warning: start events may mutate args!
98
+ safeEmit(prefix + 'start', [args, originalThis, methodName], ctx, bubble);
99
+ try {
100
+ result = fn.apply(originalThis, args);
101
+ return result;
102
+ } catch (err) {
103
+ safeEmit(prefix + 'err', [args, originalThis, err], ctx, bubble);
104
+
105
+ // rethrow error so we don't effect execution by observing.
106
+ throw err;
107
+ } finally {
108
+ // happens no matter what.
109
+ safeEmit(prefix + 'end', [args, originalThis, result], ctx, bubble);
110
+ }
111
+ }
112
+ }
113
+
114
+ /**
115
+ * Creates wrapper functions around each of an array of methods on a specified object.
116
+ * @param {Object} obj - The object to which the specified methods belong.
117
+ * @param {string[]} methods - An array of method names to be wrapped.
118
+ * @param {string} [prefix=''] - A prefix to add to the names of emitted events.
119
+ * @param {function|object} [getContext] - The function or object that will serve as the 'this' context for handlers
120
+ * of events emitted by this wrapper.
121
+ * @param {boolean} [bubble=false] - If `true`, emitted events should also bubble up to the old emitter upon which
122
+ * the `emitter` in the current scope was based (if it defines one).
123
+ */
124
+ function inPlace(obj, methods, prefix, getContext, bubble) {
125
+ if (!prefix) prefix = '';
126
+ // If prefix starts with '-' set this boolean to add the method name to
127
+ // the prefix before passing each one to wrap.
128
+ var prependMethodPrefix = prefix.charAt(0) === '-';
129
+ var fn;
130
+ var method;
131
+ var i;
132
+ for (i = 0; i < methods.length; i++) {
133
+ method = methods[i];
134
+ fn = obj[method];
135
+
136
+ // Unless fn is both wrappable and unwrapped bail,
137
+ // so we don't add extra properties with undefined values.
138
+ if (notWrappable(fn)) continue;
139
+ obj[method] = wrapFn(fn, prependMethodPrefix ? method + prefix : prefix, getContext, method, bubble);
140
+ }
141
+ }
142
+
143
+ /**
144
+ * Emits an event using the `emit` method of the `emitter` object in the executing scope, but only if an event is not
145
+ * already being emitted (except when the executing scope defines `always` as `true`).
146
+ * @param {string} evt - The name of the event to be emitted.
147
+ * @param {array} arr - An array of arguments to pass with the event.
148
+ * @param {Object} store - The function or object that will serve as the 'this'
149
+ * context when applying handler functions for this event.
150
+ * @param {boolean} bubble - If `true`, emitted events should also
151
+ * bubble up to the old emitter upon which the `emitter` in the
152
+ * executing scope was based (if it defines one).
153
+ */
154
+ function safeEmit(evt, arr, store, bubble) {
155
+ if (inWrapper && !always) return;
156
+ var prev = inWrapper;
157
+ inWrapper = true;
158
+ try {
159
+ emitter.emit(evt, arr, store, always, bubble);
160
+ } catch (e) {
161
+ report([e, evt, arr, store], emitter);
162
+ }
163
+ inWrapper = prev;
164
+ }
165
+ }
166
+
167
+ /**
168
+ * Emits an "internal-error" event. Used to report errors encountered when emitting events using `safeEmit`.
169
+ * @param {array} args - Arguments to be passed to the "internal-error" event.
170
+ * @param {Object} [emitter] - The (optional) desired event emitter. Defaults to the global event emitter.
171
+ */
172
+ function report(args, emitter) {
173
+ emitter || (emitter = _contextualEe.ee);
174
+ try {
175
+ emitter.emit('internal-error', args);
176
+ } catch (err) {
177
+ // do nothing
178
+ }
179
+ }
180
+
181
+ /**
182
+ * Copies properties from one object to another. Used for creating a wrapper function from an original function and for
183
+ * copying an original function to a property of a wrapper function named by `flag` in the executing context.
184
+ * @param {Object} from - The source function or object.
185
+ * @param {Object} to - The destination function or object.
186
+ * @param {Object} [emitter] - The (optional) desired event emitter if errors are encountered while copying.
187
+ * Defaults to the global event emitter.
188
+ * @returns {object} - The destination founction or object with copied properties.
189
+ */
190
+ function copy(from, to, emitter) {
191
+ if (Object.defineProperty && Object.keys) {
192
+ // Create accessors that proxy to actual function
193
+ try {
194
+ var keys = Object.keys(from);
195
+ // eslint-disable-next-line
196
+ keys.forEach(function (key) {
197
+ Object.defineProperty(to, key, {
198
+ get: function () {
199
+ return from[key];
200
+ },
201
+ // eslint-disable-next-line
202
+ set: function (val) {
203
+ from[key] = val;
204
+ return val;
205
+ }
206
+ });
207
+ });
208
+ return to;
209
+ } catch (e) {
210
+ report([e], emitter);
211
+ }
212
+ }
213
+ // fall back to copying properties
214
+ for (var i in from) {
215
+ if (has.call(from, i)) {
216
+ to[i] = from[i];
217
+ }
218
+ }
219
+ return to;
220
+ }
221
+
222
+ /**
223
+ * Determines whether a function is eligible to be wrapped in part based on whether it has already been wrapped.
224
+ * @param {function} fn - The function in question.
225
+ * @returns {boolean} Whether the passed function is ineligible to be wrapped.
226
+ */
227
+ function notWrappable(fn) {
228
+ return !(fn && fn instanceof Function && fn.apply && !fn[flag]);
229
+ }
230
+
231
+ /**
232
+ * Creates a wrapped version of a function using a specified wrapper function. The new wrapped function references the
233
+ * original function. Also copies the properties of the original function to the wrapped function.
234
+ * @param {function} fn - A function to be wrapped.
235
+ * @param {function} wrapper - A higher order function that returns a new function, which executes the function passed
236
+ * to the higher order function as an argument.
237
+ * @returns {function} A wrapped function with an internal reference to the original function.
238
+ */
239
+ function wrapFunction(fn, wrapper) {
240
+ var wrapped = wrapper(fn);
241
+ wrapped[flag] = fn;
242
+ copy(fn, wrapped, _contextualEe.ee);
243
+ return wrapped;
244
+ }
245
+
246
+ /**
247
+ * Replaces a function with a wrapped version of itself. To preserve object references, rather than taking the function
248
+ * itself as an argument, takes the object on which the particular named function is a property.
249
+ * @param {Object} obj - The object on which the named function is a property.
250
+ * @param {string} fnName - The name of the function to be wrapped.
251
+ * @param {function} wrapper - A higher order function that returns a new function, which executes the function passed
252
+ * to the higher order function as an argument.
253
+ */
254
+ function wrapInPlace(obj, fnName, wrapper) {
255
+ var fn = obj[fnName];
256
+ obj[fnName] = wrapFunction(fn, wrapper);
257
+ }
258
+
259
+ /**
260
+ * If a function property on an object (e.g. window) was previously wrapped (by this module), removes the wrapper.
261
+ * @param {Object} obj - The object on which the named function is a property.
262
+ * @param {string} fnName - The name of the function to be unwrapped.
263
+ */
264
+ function unwrapFunction(obj, fnName) {
265
+ if (obj?.[fnName]?.[flag]) {
266
+ // previous state of the function property is stored under our wrapper's "flag"; we don't wrap properties that *were* undefined to begin with
267
+ obj[fnName] = obj[fnName][flag];
268
+ }
269
+ }