@microsoft/applicationinsights-core-js 2.7.5-nightly.2204-03 → 2.7.6

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 (160) hide show
  1. package/browser/applicationinsights-core-js.integrity.json +9 -9
  2. package/browser/applicationinsights-core-js.js +774 -1973
  3. package/browser/applicationinsights-core-js.js.map +1 -1
  4. package/browser/applicationinsights-core-js.min.js +2 -2
  5. package/browser/applicationinsights-core-js.min.js.map +1 -1
  6. package/dist/applicationinsights-core-js.api.json +9840 -16439
  7. package/dist/applicationinsights-core-js.api.md +48 -425
  8. package/dist/applicationinsights-core-js.d.ts +123 -784
  9. package/dist/applicationinsights-core-js.js +774 -1973
  10. package/dist/applicationinsights-core-js.js.map +1 -1
  11. package/dist/applicationinsights-core-js.min.js +2 -2
  12. package/dist/applicationinsights-core-js.min.js.map +1 -1
  13. package/dist/applicationinsights-core-js.rollup.d.ts +123 -784
  14. package/dist-esm/JavaScriptSDK/AppInsightsCore.js +69 -3
  15. package/dist-esm/JavaScriptSDK/AppInsightsCore.js.map +1 -1
  16. package/dist-esm/JavaScriptSDK/BaseCore.js +99 -496
  17. package/dist-esm/JavaScriptSDK/BaseCore.js.map +1 -1
  18. package/dist-esm/JavaScriptSDK/BaseTelemetryPlugin.js +21 -120
  19. package/dist-esm/JavaScriptSDK/BaseTelemetryPlugin.js.map +1 -1
  20. package/dist-esm/JavaScriptSDK/ChannelController.js +87 -202
  21. package/dist-esm/JavaScriptSDK/ChannelController.js.map +1 -1
  22. package/dist-esm/JavaScriptSDK/Constants.js +1 -1
  23. package/dist-esm/JavaScriptSDK/CookieMgr.js +4 -4
  24. package/dist-esm/JavaScriptSDK/CookieMgr.js.map +1 -1
  25. package/dist-esm/JavaScriptSDK/CoreUtils.js +148 -7
  26. package/dist-esm/JavaScriptSDK/CoreUtils.js.map +1 -1
  27. package/dist-esm/JavaScriptSDK/DbgExtensionUtils.js +1 -1
  28. package/dist-esm/JavaScriptSDK/DiagnosticLogger.js +14 -27
  29. package/dist-esm/JavaScriptSDK/DiagnosticLogger.js.map +1 -1
  30. package/dist-esm/JavaScriptSDK/EnvUtils.js +12 -13
  31. package/dist-esm/JavaScriptSDK/EnvUtils.js.map +1 -1
  32. package/dist-esm/JavaScriptSDK/HelperFuncs.js +86 -89
  33. package/dist-esm/JavaScriptSDK/HelperFuncs.js.map +1 -1
  34. package/dist-esm/JavaScriptSDK/InstrumentHooks.js +1 -3
  35. package/dist-esm/JavaScriptSDK/InstrumentHooks.js.map +1 -1
  36. package/dist-esm/JavaScriptSDK/NotificationManager.js +42 -37
  37. package/dist-esm/JavaScriptSDK/NotificationManager.js.map +1 -1
  38. package/dist-esm/JavaScriptSDK/PerfManager.js +1 -1
  39. package/dist-esm/JavaScriptSDK/ProcessTelemetryContext.js +119 -430
  40. package/dist-esm/JavaScriptSDK/ProcessTelemetryContext.js.map +1 -1
  41. package/dist-esm/JavaScriptSDK/RandomHelper.js +4 -30
  42. package/dist-esm/JavaScriptSDK/RandomHelper.js.map +1 -1
  43. package/dist-esm/JavaScriptSDK/TelemetryHelpers.js +14 -60
  44. package/dist-esm/JavaScriptSDK/TelemetryHelpers.js.map +1 -1
  45. package/dist-esm/JavaScriptSDK/TelemetryPluginChain.js +76 -0
  46. package/dist-esm/JavaScriptSDK/TelemetryPluginChain.js.map +1 -0
  47. package/dist-esm/JavaScriptSDK.Enums/EventsDiscardedReason.js +1 -1
  48. package/dist-esm/JavaScriptSDK.Enums/LoggingEnums.js +96 -90
  49. package/dist-esm/JavaScriptSDK.Enums/LoggingEnums.js.map +1 -1
  50. package/dist-esm/JavaScriptSDK.Enums/SendRequestReason.js +1 -1
  51. package/dist-esm/JavaScriptSDK.Interfaces/IAppInsightsCore.js +2 -1
  52. package/dist-esm/JavaScriptSDK.Interfaces/IAppInsightsCore.js.map +1 -1
  53. package/dist-esm/JavaScriptSDK.Interfaces/IChannelControls.js +1 -1
  54. package/dist-esm/JavaScriptSDK.Interfaces/IConfiguration.js +1 -1
  55. package/dist-esm/JavaScriptSDK.Interfaces/ICookieMgr.js +1 -1
  56. package/dist-esm/JavaScriptSDK.Interfaces/IDbgExtension.js +1 -1
  57. package/dist-esm/JavaScriptSDK.Interfaces/IDiagnosticLogger.js +1 -1
  58. package/dist-esm/JavaScriptSDK.Interfaces/IInstrumentHooks.js +1 -1
  59. package/dist-esm/JavaScriptSDK.Interfaces/INotificationListener.js +1 -1
  60. package/dist-esm/JavaScriptSDK.Interfaces/INotificationManager.js +1 -1
  61. package/dist-esm/JavaScriptSDK.Interfaces/IPerfEvent.js +1 -1
  62. package/dist-esm/JavaScriptSDK.Interfaces/IPerfManager.js +1 -1
  63. package/dist-esm/JavaScriptSDK.Interfaces/IProcessTelemetryContext.js +1 -1
  64. package/dist-esm/JavaScriptSDK.Interfaces/ITelemetryItem.js +1 -1
  65. package/dist-esm/JavaScriptSDK.Interfaces/ITelemetryPlugin.js +1 -1
  66. package/dist-esm/JavaScriptSDK.Interfaces/ITelemetryPluginChain.js +2 -1
  67. package/dist-esm/JavaScriptSDK.Interfaces/ITelemetryPluginChain.js.map +1 -1
  68. package/dist-esm/applicationinsights-core-js.js +7 -12
  69. package/dist-esm/applicationinsights-core-js.js.map +1 -1
  70. package/package.json +67 -70
  71. package/src/JavaScriptSDK/AppInsightsCore.ts +101 -2
  72. package/src/JavaScriptSDK/BaseCore.ts +120 -696
  73. package/src/JavaScriptSDK/BaseTelemetryPlugin.ts +31 -205
  74. package/src/JavaScriptSDK/ChannelController.ts +103 -242
  75. package/src/JavaScriptSDK/CookieMgr.ts +5 -7
  76. package/src/JavaScriptSDK/CoreUtils.ts +172 -8
  77. package/src/JavaScriptSDK/DiagnosticLogger.ts +14 -29
  78. package/src/JavaScriptSDK/EnvUtils.ts +12 -13
  79. package/src/JavaScriptSDK/HelperFuncs.ts +97 -123
  80. package/src/JavaScriptSDK/InstrumentHooks.ts +2 -4
  81. package/src/JavaScriptSDK/NotificationManager.ts +37 -34
  82. package/src/JavaScriptSDK/PerfManager.ts +7 -4
  83. package/src/JavaScriptSDK/ProcessTelemetryContext.ts +134 -543
  84. package/src/JavaScriptSDK/RandomHelper.ts +3 -35
  85. package/src/JavaScriptSDK/TelemetryHelpers.ts +21 -83
  86. package/src/JavaScriptSDK/TelemetryPluginChain.ts +120 -0
  87. package/src/JavaScriptSDK.Enums/LoggingEnums.ts +87 -184
  88. package/src/JavaScriptSDK.Enums/SendRequestReason.ts +0 -5
  89. package/src/JavaScriptSDK.Interfaces/IAppInsightsCore.ts +2 -82
  90. package/src/JavaScriptSDK.Interfaces/IChannelControls.ts +5 -16
  91. package/src/JavaScriptSDK.Interfaces/IInstrumentHooks.ts +6 -16
  92. package/src/JavaScriptSDK.Interfaces/IProcessTelemetryContext.ts +14 -94
  93. package/src/JavaScriptSDK.Interfaces/ITelemetryPlugin.ts +10 -28
  94. package/src/JavaScriptSDK.Interfaces/ITelemetryPluginChain.ts +11 -9
  95. package/types/JavaScriptSDK/AppInsightsCore.d.ts +21 -0
  96. package/types/JavaScriptSDK/BaseCore.d.ts +3 -81
  97. package/types/JavaScriptSDK/BaseTelemetryPlugin.d.ts +10 -53
  98. package/types/JavaScriptSDK/ChannelController.d.ts +17 -16
  99. package/types/JavaScriptSDK/CoreUtils.d.ts +48 -1
  100. package/types/JavaScriptSDK/DiagnosticLogger.d.ts +0 -10
  101. package/types/JavaScriptSDK/HelperFuncs.d.ts +32 -33
  102. package/types/JavaScriptSDK/PerfManager.d.ts +1 -1
  103. package/types/JavaScriptSDK/ProcessTelemetryContext.d.ts +6 -69
  104. package/types/JavaScriptSDK/RandomHelper.d.ts +0 -6
  105. package/types/JavaScriptSDK/TelemetryHelpers.d.ts +3 -23
  106. package/types/JavaScriptSDK/TelemetryPluginChain.d.ts +32 -0
  107. package/types/JavaScriptSDK.Enums/LoggingEnums.d.ts +2 -91
  108. package/types/JavaScriptSDK.Enums/SendRequestReason.d.ts +0 -4
  109. package/types/JavaScriptSDK.Interfaces/IAppInsightsCore.d.ts +1 -72
  110. package/types/JavaScriptSDK.Interfaces/IChannelControls.d.ts +5 -16
  111. package/types/JavaScriptSDK.Interfaces/IInstrumentHooks.d.ts +0 -9
  112. package/types/JavaScriptSDK.Interfaces/IProcessTelemetryContext.d.ts +11 -82
  113. package/types/JavaScriptSDK.Interfaces/ITelemetryPlugin.d.ts +8 -25
  114. package/types/JavaScriptSDK.Interfaces/ITelemetryPluginChain.d.ts +10 -9
  115. package/types/applicationinsights-core-js.d.ts +9 -18
  116. package/types/tsdoc-metadata.json +1 -1
  117. package/dist-esm/JavaScriptSDK/DataCacheHelper.js +0 -93
  118. package/dist-esm/JavaScriptSDK/DataCacheHelper.js.map +0 -1
  119. package/dist-esm/JavaScriptSDK/EventHelpers.js +0 -477
  120. package/dist-esm/JavaScriptSDK/EventHelpers.js.map +0 -1
  121. package/dist-esm/JavaScriptSDK/InternalConstants.js +0 -28
  122. package/dist-esm/JavaScriptSDK/InternalConstants.js.map +0 -1
  123. package/dist-esm/JavaScriptSDK/TelemetryInitializerPlugin.js +0 -81
  124. package/dist-esm/JavaScriptSDK/TelemetryInitializerPlugin.js.map +0 -1
  125. package/dist-esm/JavaScriptSDK/UnloadHandlerContainer.js +0 -33
  126. package/dist-esm/JavaScriptSDK/UnloadHandlerContainer.js.map +0 -1
  127. package/dist-esm/JavaScriptSDK.Enums/TelemetryUnloadReason.js +0 -8
  128. package/dist-esm/JavaScriptSDK.Enums/TelemetryUnloadReason.js.map +0 -1
  129. package/dist-esm/JavaScriptSDK.Enums/TelemetryUpdateReason.js +0 -8
  130. package/dist-esm/JavaScriptSDK.Enums/TelemetryUpdateReason.js.map +0 -1
  131. package/dist-esm/JavaScriptSDK.Interfaces/ITelemetryInitializers.js +0 -6
  132. package/dist-esm/JavaScriptSDK.Interfaces/ITelemetryInitializers.js.map +0 -1
  133. package/dist-esm/JavaScriptSDK.Interfaces/ITelemetryUnloadState.js +0 -8
  134. package/dist-esm/JavaScriptSDK.Interfaces/ITelemetryUnloadState.js.map +0 -1
  135. package/dist-esm/JavaScriptSDK.Interfaces/ITelemetryUpdateState.js +0 -8
  136. package/dist-esm/JavaScriptSDK.Interfaces/ITelemetryUpdateState.js.map +0 -1
  137. package/dist-esm/JavaScriptSDK.Interfaces/IUnloadableComponent.js +0 -6
  138. package/dist-esm/JavaScriptSDK.Interfaces/IUnloadableComponent.js.map +0 -1
  139. package/src/JavaScriptSDK/DataCacheHelper.ts +0 -106
  140. package/src/JavaScriptSDK/EventHelpers.ts +0 -550
  141. package/src/JavaScriptSDK/InternalConstants.ts +0 -26
  142. package/src/JavaScriptSDK/TelemetryInitializerPlugin.ts +0 -111
  143. package/src/JavaScriptSDK/UnloadHandlerContainer.ts +0 -46
  144. package/src/JavaScriptSDK.Enums/TelemetryUnloadReason.ts +0 -27
  145. package/src/JavaScriptSDK.Enums/TelemetryUpdateReason.ts +0 -27
  146. package/src/JavaScriptSDK.Interfaces/ITelemetryInitializers.ts +0 -16
  147. package/src/JavaScriptSDK.Interfaces/ITelemetryUnloadState.ts +0 -10
  148. package/src/JavaScriptSDK.Interfaces/ITelemetryUpdateState.ts +0 -34
  149. package/src/JavaScriptSDK.Interfaces/IUnloadableComponent.ts +0 -17
  150. package/types/JavaScriptSDK/DataCacheHelper.d.ts +0 -13
  151. package/types/JavaScriptSDK/EventHelpers.d.ts +0 -154
  152. package/types/JavaScriptSDK/InternalConstants.d.ts +0 -20
  153. package/types/JavaScriptSDK/TelemetryInitializerPlugin.d.ts +0 -16
  154. package/types/JavaScriptSDK/UnloadHandlerContainer.d.ts +0 -11
  155. package/types/JavaScriptSDK.Enums/TelemetryUnloadReason.d.ts +0 -21
  156. package/types/JavaScriptSDK.Enums/TelemetryUpdateReason.d.ts +0 -20
  157. package/types/JavaScriptSDK.Interfaces/ITelemetryInitializers.d.ts +0 -13
  158. package/types/JavaScriptSDK.Interfaces/ITelemetryUnloadState.d.ts +0 -6
  159. package/types/JavaScriptSDK.Interfaces/ITelemetryUpdateState.d.ts +0 -22
  160. package/types/JavaScriptSDK.Interfaces/IUnloadableComponent.d.ts +0 -13
@@ -1,274 +1,135 @@
1
1
  // Copyright (c) Microsoft Corporation. All rights reserved.
2
- // // Licensed under the MIT License.
2
+ // Licensed under the MIT License.
3
+ "use strict";
3
4
 
4
- import { SendRequestReason } from "../JavaScriptSDK.Enums/SendRequestReason";
5
- import { TelemetryUnloadReason } from "../JavaScriptSDK.Enums/TelemetryUnloadReason";
6
- import { TelemetryUpdateReason } from "../JavaScriptSDK.Enums/TelemetryUpdateReason";
7
- import { IAppInsightsCore } from "../JavaScriptSDK.Interfaces/IAppInsightsCore";
8
- import { IChannelControls } from "../JavaScriptSDK.Interfaces/IChannelControls";
5
+ import dynamicProto from "@microsoft/dynamicproto-js";
6
+ import { IAppInsightsCore } from "../JavaScriptSDK.Interfaces/IAppInsightsCore"
9
7
  import { IConfiguration } from "../JavaScriptSDK.Interfaces/IConfiguration";
10
- import { IBaseProcessingContext, IProcessTelemetryContext, IProcessTelemetryUnloadContext, IProcessTelemetryUpdateContext } from "../JavaScriptSDK.Interfaces/IProcessTelemetryContext";
11
- import { ITelemetryItem } from "../JavaScriptSDK.Interfaces/ITelemetryItem";
12
- import { IPlugin } from "../JavaScriptSDK.Interfaces/ITelemetryPlugin";
8
+ import { IChannelControls } from "../JavaScriptSDK.Interfaces/IChannelControls";
9
+ import { IPlugin, ITelemetryPlugin } from "../JavaScriptSDK.Interfaces/ITelemetryPlugin";
13
10
  import { ITelemetryPluginChain } from "../JavaScriptSDK.Interfaces/ITelemetryPluginChain";
14
- import { ITelemetryUnloadState } from "../JavaScriptSDK.Interfaces/ITelemetryUnloadState";
15
- import { ITelemetryUpdateState } from "../JavaScriptSDK.Interfaces/ITelemetryUpdateState";
16
- import { arrForEach, isArray, objFreeze, throwError } from "./HelperFuncs";
17
- import { strPause, strProcessNext, strResume, strTeardown } from "./InternalConstants";
18
- import { createProcessTelemetryContext, createTelemetryProxyChain } from "./ProcessTelemetryContext";
11
+ import { ITelemetryItem } from "../JavaScriptSDK.Interfaces/ITelemetryItem";
12
+ import { IProcessTelemetryContext } from "../JavaScriptSDK.Interfaces/IProcessTelemetryContext";
13
+ import { _InternalLogMessage } from "./DiagnosticLogger";
14
+ import { BaseTelemetryPlugin } from "./BaseTelemetryPlugin";
15
+ import { ProcessTelemetryContext } from "./ProcessTelemetryContext";
19
16
  import { initializePlugins } from "./TelemetryHelpers";
17
+ import { arrForEach, objDefineAccessors, throwError } from "./HelperFuncs";
20
18
 
21
- export const ChannelControllerPriority = 500;
22
- const ChannelValidationMessage = "Channel has invalid priority - ";
19
+ const ChannelControllerPriority = 500;
20
+ const ChannelValidationMessage = "Channel has invalid priority";
23
21
 
24
- export interface IChannelController extends IChannelControls {
25
- flush(isAsync: boolean, callBack: (flushComplete?: boolean) => void, sendReason: SendRequestReason, cbTimeout?: number): void;
22
+ export class ChannelController extends BaseTelemetryPlugin {
26
23
 
27
- getChannel<T extends IPlugin = IPlugin>(pluginIdentifier: string): T;
28
- }
24
+ identifier: string = "ChannelControllerPlugin";
25
+ priority: number = ChannelControllerPriority; // in reserved range 100 to 200
29
26
 
30
- export interface IInternalChannelController extends IChannelController {
31
- _setQueue: (channels: _IInternalChannels[]) => void;
32
- }
27
+ public setNextPlugin: (next: ITelemetryPlugin | ITelemetryPluginChain) => void;
33
28
 
34
- export interface _IInternalChannels {
35
- queue: IChannelControls[];
36
- chain: ITelemetryPluginChain;
37
- }
29
+ constructor() {
30
+ super();
31
+ let _channelQueue: IChannelControls[][];
38
32
 
39
- function _addChannelQueue(channelQueue: _IInternalChannels[], queue: IChannelControls[], config: IConfiguration, core: IAppInsightsCore) {
40
- if (queue && isArray(queue) && queue.length > 0) {
41
- queue = queue.sort((a, b) => { // sort based on priority within each queue
42
- return a.priority - b.priority;
43
- });
33
+ dynamicProto(ChannelController, this, (_self, _base) => {
34
+ _self.setNextPlugin = (next: ITelemetryPlugin | ITelemetryPluginChain) => {
35
+ // The Channel controller is last in pipeline
36
+ };
44
37
 
45
- arrForEach(queue, queueItem => {
46
- if (queueItem.priority < ChannelControllerPriority) {
47
- throwError(ChannelValidationMessage + queueItem.identifier);
48
- }
49
- });
50
-
51
- channelQueue.push({
52
- queue: objFreeze(queue),
53
- chain: createTelemetryProxyChain(queue, config, core)
54
- });
55
- }
56
- }
57
-
58
- export function createChannelControllerPlugin(channelQueue: _IInternalChannels[], core: IAppInsightsCore): IChannelController {
59
-
60
- function _getTelCtx() {
61
- return createProcessTelemetryContext(null, core.config, core, null)
62
- }
63
-
64
- function _processChannelQueue<T extends IBaseProcessingContext>(theChannels: _IInternalChannels[], itemCtx: T, processFn: (chainCtx: T) => void, onComplete: (() => void) | null) {
65
- let waiting = theChannels ? (theChannels.length + 1) : 1;
66
-
67
- function _runChainOnComplete() {
68
- waiting --;
69
-
70
- if (waiting === 0) {
71
- onComplete && onComplete();
72
- onComplete = null;
73
- }
74
- }
75
-
76
- if (waiting > 0) {
77
- arrForEach(theChannels, (channels) => {
78
- // pass on to first item in queue
79
- if (channels && channels.queue.length > 0) {
80
- let channelChain = channels.chain;
81
- let chainCtx = itemCtx.createNew(channelChain) as T;
82
- chainCtx.onComplete(_runChainOnComplete);
83
-
84
- // Cause this chain to start processing
85
- processFn(chainCtx);
86
- } else {
87
- waiting --;
38
+ _self.processTelemetry = (item: ITelemetryItem, itemCtx: IProcessTelemetryContext) => {
39
+ if (_channelQueue) {
40
+ arrForEach(_channelQueue, queues => {
41
+ // pass on to first item in queue
42
+ if (queues.length > 0) {
43
+ // Copying the item context as we could have mutiple chains that are executing asynchronously
44
+ // and calling _getDefTelCtx as it's possible that the caller doesn't pass any context
45
+ let chainCtx = this._getTelCtx(itemCtx).createNew(queues);
46
+ chainCtx.processNext(item);
47
+ }
48
+ });
49
+ }
50
+ };
51
+
52
+ _self.getChannelControls = (): IChannelControls[][] => {
53
+ return _channelQueue;
54
+ };
55
+
56
+ _self.initialize = (config: IConfiguration, core: IAppInsightsCore, extensions: IPlugin[]) => {
57
+ if (_self.isInitialized()) {
58
+ // already initialized
59
+ return;
88
60
  }
89
- });
90
- }
91
-
92
- _runChainOnComplete();
93
- }
94
-
95
- function _doUpdate(updateCtx: IProcessTelemetryUpdateContext, updateState: ITelemetryUpdateState) {
96
- let theUpdateState: ITelemetryUpdateState = updateState || {
97
- reason: TelemetryUpdateReason.Unknown
98
- };
99
-
100
- _processChannelQueue(channelQueue, updateCtx, (chainCtx: IProcessTelemetryUpdateContext) => {
101
- chainCtx[strProcessNext](theUpdateState);
102
- }, () => {
103
- updateCtx[strProcessNext](theUpdateState);
104
- });
105
-
106
- return true;
107
- }
108
61
 
109
- function _doTeardown(unloadCtx: IProcessTelemetryUnloadContext, unloadState: ITelemetryUnloadState) {
110
- let theUnloadState: ITelemetryUnloadState = unloadState || {
111
- reason: TelemetryUnloadReason.ManualTeardown,
112
- isAsync: false
113
- };
62
+ _base.initialize(config, core, extensions);
114
63
 
115
- _processChannelQueue(channelQueue, unloadCtx, (chainCtx: IProcessTelemetryUnloadContext) => {
116
- chainCtx[strProcessNext](theUnloadState);
117
- }, () => {
118
- unloadCtx[strProcessNext](theUnloadState);
119
- isInitialized = false;
64
+ _createChannelQueues((config||{}).channels, extensions);
65
+
66
+ // Initialize the Queues
67
+ arrForEach(_channelQueue, queue => initializePlugins(new ProcessTelemetryContext(queue, config, core), extensions));
68
+ }
120
69
  });
121
70
 
122
- return true;
123
- }
124
-
125
- function _getChannel<T extends IPlugin = IPlugin>(pluginIdentifier: string): T {
126
- let thePlugin: T = null;
127
-
128
- if (channelQueue && channelQueue.length > 0) {
129
- arrForEach(channelQueue, (channels) => {
130
- // pass on to first item in queue
131
- if (channels && channels.queue.length > 0) {
132
- arrForEach(channels.queue, (ext: any) => {
133
- if (ext.identifier === pluginIdentifier) {
134
- thePlugin = ext;
135
- // Cause arrForEach to stop iterating
136
- return -1;
137
- }
138
- });
139
-
140
- if (thePlugin) {
141
- // Cause arrForEach to stop iterating
142
- return -1;
143
- }
71
+ function _checkQueuePriority(queue:IChannelControls[]) {
72
+ arrForEach(queue, queueItem => {
73
+ if (queueItem.priority < ChannelControllerPriority) {
74
+ throwError(ChannelValidationMessage + queueItem.identifier);
144
75
  }
145
76
  });
146
77
  }
147
-
148
- return thePlugin;
149
- }
150
-
151
- let isInitialized = false;
152
- let channelController: IInternalChannelController = {
153
- identifier: "ChannelControllerPlugin",
154
- priority: ChannelControllerPriority,
155
- initialize: (config: IConfiguration, core: IAppInsightsCore, extensions: IPlugin[], pluginChain?: ITelemetryPluginChain) => {
156
- isInitialized = true;
157
- arrForEach(channelQueue, (channels) => {
158
- if (channels && channels.queue.length > 0) {
159
- initializePlugins(createProcessTelemetryContext(channels.chain, config, core), extensions);
160
- }
161
- });
162
- },
163
- isInitialized: () => {
164
- return isInitialized
165
- },
166
- processTelemetry: (item: ITelemetryItem, itemCtx: IProcessTelemetryContext) => {
167
- _processChannelQueue(channelQueue, itemCtx || _getTelCtx(), (chainCtx: IProcessTelemetryContext) => {
168
- chainCtx[strProcessNext](item);
169
- }, () => {
170
- itemCtx[strProcessNext](item);
171
- });
172
- },
173
- update: _doUpdate,
174
- [strPause]: () => {
175
- _processChannelQueue(channelQueue, _getTelCtx(), (chainCtx: IProcessTelemetryContext) => {
176
- chainCtx.iterate<IChannelControls>((plugin) => {
177
- plugin[strPause] && plugin[strPause]();
178
- });
179
- }, null);
180
- },
181
- [strResume]: () => {
182
- _processChannelQueue(channelQueue, _getTelCtx(), (chainCtx: IProcessTelemetryContext) => {
183
- chainCtx.iterate<IChannelControls>((plugin) => {
184
- plugin[strResume] && plugin[strResume]();
78
+
79
+ function _addChannelQueue(queue:IChannelControls[]) {
80
+ if (queue && queue.length > 0) {
81
+ queue = queue.sort((a, b) => { // sort based on priority within each queue
82
+ return a.priority - b.priority;
185
83
  });
186
- }, null);
187
- },
188
- [strTeardown]: _doTeardown,
189
- getChannel: _getChannel,
190
- flush: (isAsync: boolean, callBack: (flushComplete?: boolean) => void, sendReason: SendRequestReason, cbTimeout?: number) => {
191
- // Setting waiting to one so that we don't call the callBack until we finish iterating
192
- let waiting = 1;
193
- let doneIterating = false;
194
- let cbTimer: any = null;
195
-
196
- cbTimeout = cbTimeout || 5000;
197
-
198
- function doCallback() {
199
- waiting--;
200
- if (doneIterating && waiting === 0) {
201
- if (cbTimer) {
202
- clearTimeout(cbTimer);
203
- cbTimer = null;
204
- }
205
-
206
- callBack && callBack(doneIterating);
207
- callBack = null;
208
- }
84
+
85
+ _checkQueuePriority(queue);
86
+ _channelQueue.push(queue);
209
87
  }
210
-
211
- _processChannelQueue(channelQueue, _getTelCtx(), (chainCtx: IProcessTelemetryContext) => {
212
- chainCtx.iterate<IChannelControls>((plugin) => {
213
- if (plugin.flush) {
214
- waiting ++;
215
-
216
- let handled = false;
217
- // Not all channels will call this callback for every scenario
218
- if (!plugin.flush(isAsync, () => {
219
- handled = true;
220
- doCallback();
221
- }, sendReason)) {
222
- if (!handled) {
223
- // If any channel doesn't return true and it didn't call the callback, then we should assume that the callback
224
- // will never be called, so use a timeout to allow the channel(s) some time to "finish" before triggering any
225
- // followup function (such as unloading)
226
- if (isAsync && cbTimer == null) {
227
- cbTimer = setTimeout(() => {
228
- cbTimer = null;
229
- doCallback();
230
- }, cbTimeout);
231
- } else {
232
- doCallback();
233
- }
234
- }
235
- }
88
+ }
89
+
90
+ function _createChannelQueues(channels:IChannelControls[][], extensions: IPlugin[]) {
91
+ _channelQueue = [];
92
+
93
+ if (channels) {
94
+ // Add and sort the configuration channel queues
95
+ arrForEach(channels, queue => _addChannelQueue(queue));
96
+ }
97
+
98
+ if (extensions) {
99
+ // Create a new channel queue for any extensions with a priority > the ChannelControllerPriority
100
+ let extensionQueue:IChannelControls[] = [];
101
+ arrForEach(extensions as IChannelControls[], plugin => {
102
+ if (plugin.priority > ChannelControllerPriority) {
103
+ extensionQueue.push(plugin);
236
104
  }
237
105
  });
238
- }, () => {
239
- doneIterating = true;
240
- doCallback();
241
- });
242
-
243
- return true;
244
- },
245
- _setQueue: (queue: _IInternalChannels[]) => {
246
- channelQueue = queue;
106
+
107
+ _addChannelQueue(extensionQueue);
108
+ }
247
109
  }
248
- };
249
-
250
- return channelController;
251
- }
110
+ }
252
111
 
253
- export function createChannelQueues(channels: IChannelControls[][], extensions: IPlugin[], config: IConfiguration, core: IAppInsightsCore) {
254
- let channelQueue: _IInternalChannels[] = [];
112
+ public processTelemetry(item: ITelemetryItem, itemCtx: IProcessTelemetryContext) {
113
+ // @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
114
+ }
255
115
 
256
- if (channels) {
257
- // Add and sort the configuration channel queues
258
- arrForEach(channels, queue => _addChannelQueue(channelQueue, queue, config, core));
116
+ public getChannelControls(): IChannelControls[][] {
117
+ // @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
118
+ return null;
259
119
  }
260
120
 
261
- if (extensions) {
262
- // Create a new channel queue for any extensions with a priority > the ChannelControllerPriority
263
- let extensionQueue: IChannelControls[] = [];
264
- arrForEach(extensions as IChannelControls[], plugin => {
265
- if (plugin.priority > ChannelControllerPriority) {
266
- extensionQueue.push(plugin);
267
- }
268
- });
269
-
270
- _addChannelQueue(channelQueue, extensionQueue, config, core);
121
+ public initialize(config: IConfiguration, core: IAppInsightsCore, extensions: IPlugin[]) {
122
+ // @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
271
123
  }
272
124
 
273
- return channelQueue;
125
+ /**
126
+ * Static constructor, attempt to create accessors
127
+ */
128
+ // tslint:disable-next-line
129
+ private static _staticInit = (() => {
130
+ let proto = ChannelController.prototype;
131
+ // Dynamically create get/set property accessors
132
+ objDefineAccessors(proto, "ChannelControls", proto.getChannelControls);
133
+ objDefineAccessors(proto, "channelQueue", proto.getChannelControls);
134
+ })();
274
135
  }
@@ -2,7 +2,7 @@
2
2
  // Licensed under the MIT License.
3
3
  import { IDiagnosticLogger } from "../JavaScriptSDK.Interfaces/IDiagnosticLogger";
4
4
  import { ICookieMgr, ICookieMgrConfig } from "../JavaScriptSDK.Interfaces/ICookieMgr";
5
- import { _eInternalMessageId, eLoggingSeverity } from "../JavaScriptSDK.Enums/LoggingEnums";
5
+ import { _InternalMessageId, LoggingSeverity } from "../JavaScriptSDK.Enums/LoggingEnums";
6
6
  import { dumpObj, getDocument, getLocation, getNavigator, isIE } from "./EnvUtils";
7
7
  import {
8
8
  arrForEach, dateNow, getExceptionName, isFunction, isNotNullOrUndefined, isNullOrUndefined, isString, isTruthy, isUndefined,
@@ -10,8 +10,6 @@ import {
10
10
  } from "./HelperFuncs";
11
11
  import { IConfiguration } from "../JavaScriptSDK.Interfaces/IConfiguration";
12
12
  import { IAppInsightsCore } from "../JavaScriptSDK.Interfaces/IAppInsightsCore";
13
- import { strEmpty } from "./InternalConstants";
14
- import { _throwInternal } from "./DiagnosticLogger";
15
13
 
16
14
  const strToGMTString = "toGMTString";
17
15
  const strToUTCString = "toUTCString";
@@ -21,6 +19,7 @@ const strEnabled = "enabled";
21
19
  const strIsCookieUseDisabled = "isCookieUseDisabled";
22
20
  const strDisableCookiesUsage = "disableCookiesUsage";
23
21
  const strConfigCookieMgr = "_ckMgr";
22
+ const strEmpty = "";
24
23
 
25
24
  let _supportsCookies: boolean = null;
26
25
  let _allowUaSameSite: boolean = null;
@@ -253,10 +252,9 @@ export function areCookiesSupported(logger?: IDiagnosticLogger): any {
253
252
  let doc = _doc || {} as Document;
254
253
  _supportsCookies = doc[strCookie] !== undefined;
255
254
  } catch (e) {
256
- _throwInternal(
257
- logger,
258
- eLoggingSeverity.WARNING,
259
- _eInternalMessageId.CannotAccessCookie,
255
+ logger && logger.throwInternal(
256
+ LoggingSeverity.WARNING,
257
+ _InternalMessageId.CannotAccessCookie,
260
258
  "Cannot access document.cookie - " + getExceptionName(e),
261
259
  { exception: dumpObj(e) });
262
260
  }
@@ -6,21 +6,158 @@ import { IConfiguration } from "../JavaScriptSDK.Interfaces/IConfiguration";
6
6
  import { ICookieMgr } from "../JavaScriptSDK.Interfaces/ICookieMgr";
7
7
  import { IDiagnosticLogger } from "../JavaScriptSDK.Interfaces/IDiagnosticLogger";
8
8
  import { _gblCookieMgr } from "./CookieMgr";
9
- import { getPerformance, isIE } from "./EnvUtils";
9
+ import { getWindow, getDocument, getPerformance, isIE } from "./EnvUtils";
10
10
  import {
11
- arrForEach, arrIndexOf, arrMap, arrReduce, dateNow, hasOwnProperty,
11
+ arrForEach, arrIndexOf, arrMap, arrReduce, attachEvent, dateNow, detachEvent, hasOwnProperty,
12
12
  isArray, isBoolean, isDate, isError, isFunction, isNullOrUndefined, isNumber, isObject, isString, isTypeof,
13
13
  isUndefined, objDefineAccessors, objKeys, strTrim, toISOString
14
14
  } from "./HelperFuncs";
15
- import { addEventHandler, attachEvent, detachEvent } from "./EventHelpers";
16
- import { randomValue, random32, mwcRandomSeed, mwcRandom32, newId } from "./RandomHelper";
17
- import { strEmpty } from "./InternalConstants";
15
+ import { randomValue, random32, mwcRandomSeed, mwcRandom32 } from "./RandomHelper";
16
+
17
+ const strVisibilityChangeEvt: string = "visibilitychange";
18
+ const strPageHide: string = "pagehide";
19
+ const strPageShow: string = "pageshow";
18
20
 
19
21
  let _cookieMgrs: ICookieMgr[] = null;
20
22
  let _canUseCookies: boolean; // legacy supported config
21
23
 
22
24
  // Added to help with minfication
23
25
  export const Undefined = strShimUndefined;
26
+
27
+ /**
28
+ * Trys to add an event handler for the specified event to the window, body and document
29
+ * @param eventName {string} - The name of the event
30
+ * @param callback {any} - The callback function that needs to be executed for the given event
31
+ * @return {boolean} - true if the handler was successfully added
32
+ */
33
+ export function addEventHandler(eventName: string, callback: any): boolean {
34
+ let result = false;
35
+ let w = getWindow();
36
+ if (w) {
37
+ result = attachEvent(w, eventName, callback);
38
+ result = attachEvent(w["body"], eventName, callback) || result;
39
+ }
40
+
41
+ let doc = getDocument();
42
+ if (doc) {
43
+ result = attachEvent(doc, eventName, callback) || result;
44
+ }
45
+
46
+ return result;
47
+ }
48
+
49
+ /**
50
+ * Bind the listener to the array of events
51
+ * @param events An string array of event names to bind the listener to
52
+ * @param listener The event callback to call when the event is triggered
53
+ * @param excludeEvents - [Optional] An array of events that should not be hooked (if possible), unless no other events can be.
54
+ * @returns true - when at least one of the events was registered otherwise false
55
+ */
56
+ export function addEventListeners(events: string[], listener: any, excludeEvents?: string[]): boolean {
57
+ let added = false;
58
+
59
+ if (listener && events && isArray(events)) {
60
+ let excluded: string[] = [];
61
+ arrForEach(events, (name) => {
62
+ if (isString(name)) {
63
+ if (!excludeEvents || arrIndexOf(excludeEvents, name) === -1) {
64
+ added = addEventHandler(name, listener) || added;
65
+ } else {
66
+ excluded.push(name);
67
+ }
68
+ }
69
+ });
70
+
71
+ if (!added && excluded.length > 0) {
72
+ // Failed to add any listeners and we excluded some, so just attempt to add the excluded events
73
+ added = addEventListeners(excluded, listener);
74
+ }
75
+ }
76
+
77
+ return added;
78
+ }
79
+
80
+ /**
81
+ * Listen to the 'beforeunload', 'unload' and 'pagehide' events which indicates a page unload is occurring,
82
+ * this does NOT listen to the 'visibilitychange' event as while it does indicate that the page is being hidden
83
+ * it does not *necessarily* mean that the page is being completely unloaded, it can mean that the user is
84
+ * just navigating to a different Tab and may come back (without unloading the page). As such you may also
85
+ * need to listen to the 'addPageHideEventListener' and 'addPageShowEventListener' events.
86
+ * @param listener - The event callback to call when a page unload event is triggered
87
+ * @param excludeEvents - [Optional] An array of events that should not be hooked, unless no other events can be.
88
+ * @returns true - when at least one of the events was registered otherwise false
89
+ */
90
+ export function addPageUnloadEventListener(listener: any, excludeEvents?: string[]): boolean {
91
+ // Hook the unload event for the document, window and body to ensure that the client events are flushed to the server
92
+ // As just hooking the window does not always fire (on chrome) for page navigation's.
93
+ return addEventListeners(["beforeunload", "unload", "pagehide"], listener, excludeEvents);
94
+ }
95
+
96
+ /**
97
+ * Listen to the pagehide and visibility changing to 'hidden' events
98
+ * @param listener - The event callback to call when a page hide event is triggered
99
+ * @param excludeEvents - [Optional] An array of events that should not be hooked (if possible), unless no other events can be.
100
+ * Suggestion: pass as true if you are also calling addPageUnloadEventListener as that also hooks pagehide
101
+ * @returns true - when at least one of the events was registered otherwise false
102
+ */
103
+ export function addPageHideEventListener(listener: any, excludeEvents?: string[]): boolean {
104
+
105
+ function _handlePageVisibility(evt: any) {
106
+ let doc = getDocument();
107
+ if (listener && doc && doc.visibilityState === "hidden") {
108
+ listener(evt);
109
+ }
110
+ }
111
+
112
+ let pageUnloadAdded = false;
113
+ if (!excludeEvents || arrIndexOf(excludeEvents, strPageHide) === -1) {
114
+ pageUnloadAdded = addEventHandler(strPageHide, listener);
115
+ }
116
+
117
+ if (!excludeEvents || arrIndexOf(excludeEvents, strVisibilityChangeEvt) === -1) {
118
+ pageUnloadAdded = addEventHandler(strVisibilityChangeEvt, _handlePageVisibility) || pageUnloadAdded;
119
+ }
120
+
121
+ if (!pageUnloadAdded && excludeEvents) {
122
+ // Failed to add any listeners and we where requested to exclude some, so just call again without excluding anything
123
+ pageUnloadAdded = addPageHideEventListener(listener);
124
+ }
125
+
126
+ return pageUnloadAdded;
127
+ }
128
+
129
+ /**
130
+ * Listen to the pageshow and visibility changing to 'visible' events
131
+ * @param listener - The event callback to call when a page is show event is triggered
132
+ * @param excludeEvents - [Optional] An array of events that should not be hooked (if possible), unless no other events can be.
133
+ * @returns true - when at least one of the events was registered otherwise false
134
+ */
135
+ export function addPageShowEventListener(listener: any, excludeEvents?: string[]): boolean {
136
+
137
+ function _handlePageVisibility(evt: any) {
138
+ let doc = getDocument();
139
+ if (listener && doc && doc.visibilityState === "visible") {
140
+ listener(evt);
141
+ }
142
+ }
143
+
144
+ let pageShowAdded = false;
145
+ if (!excludeEvents || arrIndexOf(excludeEvents, strPageShow) === -1) {
146
+ pageShowAdded = addEventHandler(strPageShow, listener);
147
+ }
148
+
149
+ if (!excludeEvents || arrIndexOf(excludeEvents, strVisibilityChangeEvt) === -1) {
150
+ pageShowAdded = addEventHandler(strVisibilityChangeEvt, _handlePageVisibility) || pageShowAdded;
151
+ }
152
+
153
+ if (!pageShowAdded && excludeEvents) {
154
+ // Failed to add any listeners and we where requested to exclude some, so just call again without excluding anything
155
+ pageShowAdded = addPageShowEventListener(listener);
156
+ }
157
+
158
+ return pageShowAdded;
159
+ }
160
+
24
161
  export function newGuid(): string {
25
162
  function randomHexDigit() {
26
163
  return randomValue(15); // Get a random value from 0..15
@@ -45,6 +182,33 @@ export function perfNow(): number {
45
182
  return dateNow();
46
183
  }
47
184
 
185
+ /**
186
+ * Generate random base64 id string.
187
+ * The default length is 22 which is 132-bits so almost the same as a GUID but as base64 (the previous default was 5)
188
+ * @param maxLength - Optional value to specify the length of the id to be generated, defaults to 22
189
+ */
190
+ export function newId(maxLength = 22): string {
191
+ const base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
192
+
193
+ // Start with an initial random number, consuming the value in reverse byte order
194
+ let number = random32() >>> 0; // Make sure it's a +ve number
195
+ let chars = 0;
196
+ let result = "";
197
+ while (result.length < maxLength) {
198
+ chars ++;
199
+ result += base64chars.charAt(number & 0x3F);
200
+ number >>>= 6; // Zero fill with right shift
201
+ if (chars === 5) {
202
+ // 5 base64 characters === 30 bits so we don't have enough bits for another base64 char
203
+ // So add on another 30 bits and make sure it's +ve
204
+ number = (((random32() << 2) & 0xFFFFFFFF) | (number & 0x03)) >>> 0;
205
+ chars = 0; // We need to reset the number every 5 chars (30 bits)
206
+ }
207
+ }
208
+
209
+ return result;
210
+ }
211
+
48
212
  /**
49
213
  * The strEndsWith() method determines whether a string ends with the characters of a specified string, returning true or false as appropriate.
50
214
  * @param value - The value to check whether it ends with the search value.
@@ -68,7 +232,7 @@ export function generateW3CId(): string {
68
232
  const hexValues = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"];
69
233
 
70
234
  // rfc4122 version 4 UUID without dashes and with lowercase letters
71
- let oct = strEmpty, tmp;
235
+ let oct = "", tmp;
72
236
  for (let a = 0; a < 4; a++) {
73
237
  tmp = random32();
74
238
  oct +=
@@ -243,7 +407,7 @@ export interface ICoreUtils {
243
407
  * @param callback {any} - The callback function that needs to be executed for the given event
244
408
  * @return {boolean} - true if the handler was successfully added
245
409
  */
246
- addEventHandler: (eventName: string, callback: any, evtNamespace?: string | string[]) => boolean;
410
+ addEventHandler: (eventName: string, callback: any) => boolean;
247
411
 
248
412
  /**
249
413
  * Return the current time via the Date now() function (if available) and falls back to (new Date()).getTime() if now() is unavailable (IE8 or less)
@@ -410,7 +574,7 @@ export const EventHelper: IEventHelper = {
410
574
  * @param logger
411
575
  * @returns
412
576
  */
413
- export function _legacyCookieMgr(config?: IConfiguration, logger?: IDiagnosticLogger): ICookieMgr {
577
+ export function _legacyCookieMgr(config?: IConfiguration, logger?: IDiagnosticLogger): ICookieMgr {
414
578
  let cookieMgr = _gblCookieMgr(config, logger);
415
579
  let legacyCanUseCookies = (CoreUtils as any)._canUseCookies;
416
580