@microsoft/1ds-post-js 3.1.9 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/README.md +124 -5
  2. package/bundle/{ms.post-3.1.9.gbl.js → ms.post-3.2.0.gbl.js} +1706 -825
  3. package/bundle/ms.post-3.2.0.gbl.js.map +1 -0
  4. package/bundle/ms.post-3.2.0.gbl.min.js +7 -0
  5. package/bundle/ms.post-3.2.0.gbl.min.js.map +1 -0
  6. package/bundle/ms.post-3.2.0.integrity.json +46 -0
  7. package/bundle/{ms.post-3.1.9.js → ms.post-3.2.0.js} +1706 -825
  8. package/bundle/ms.post-3.2.0.js.map +1 -0
  9. package/bundle/ms.post-3.2.0.min.js +7 -0
  10. package/bundle/ms.post-3.2.0.min.js.map +1 -0
  11. package/bundle/ms.post.gbl.js +1705 -824
  12. package/bundle/ms.post.gbl.js.map +1 -1
  13. package/bundle/ms.post.gbl.min.js +2 -2
  14. package/bundle/ms.post.gbl.min.js.map +1 -1
  15. package/bundle/ms.post.integrity.json +17 -17
  16. package/bundle/ms.post.js +1705 -824
  17. package/bundle/ms.post.js.map +1 -1
  18. package/bundle/ms.post.min.js +2 -2
  19. package/bundle/ms.post.min.js.map +1 -1
  20. package/dist/ms.post.js +395 -212
  21. package/dist/ms.post.js.map +1 -1
  22. package/dist/ms.post.min.js +2 -2
  23. package/dist/ms.post.min.js.map +1 -1
  24. package/dist-esm/src/BatchNotificationActions.js +1 -1
  25. package/dist-esm/src/ClockSkewManager.js +1 -1
  26. package/dist-esm/src/Constants.d.ts +25 -0
  27. package/dist-esm/src/Constants.js +31 -0
  28. package/dist-esm/src/Constants.js.map +1 -0
  29. package/dist-esm/src/DataModels.d.ts +55 -0
  30. package/dist-esm/src/DataModels.js +1 -1
  31. package/dist-esm/src/EventBatch.d.ts +5 -2
  32. package/dist-esm/src/EventBatch.js +35 -15
  33. package/dist-esm/src/EventBatch.js.map +1 -1
  34. package/dist-esm/src/HttpManager.d.ts +2 -2
  35. package/dist-esm/src/HttpManager.js +186 -94
  36. package/dist-esm/src/HttpManager.js.map +1 -1
  37. package/dist-esm/src/Index.js +1 -1
  38. package/dist-esm/src/KillSwitch.js +1 -1
  39. package/dist-esm/src/PostChannel.d.ts +0 -4
  40. package/dist-esm/src/PostChannel.js +175 -107
  41. package/dist-esm/src/PostChannel.js.map +1 -1
  42. package/dist-esm/src/RetryPolicy.d.ts +20 -25
  43. package/dist-esm/src/RetryPolicy.js +35 -44
  44. package/dist-esm/src/RetryPolicy.js.map +1 -1
  45. package/dist-esm/src/Serializer.js +1 -1
  46. package/dist-esm/src/typings/XDomainRequest.js +1 -1
  47. package/package.json +3 -3
  48. package/src/Constants.ts +28 -0
  49. package/src/DataModels.ts +68 -0
  50. package/src/EventBatch.ts +47 -14
  51. package/src/HttpManager.ts +216 -98
  52. package/src/PostChannel.ts +207 -130
  53. package/src/RetryPolicy.ts +33 -38
  54. package/bundle/ms.post-3.1.9.gbl.js.map +0 -1
  55. package/bundle/ms.post-3.1.9.gbl.min.js +0 -7
  56. package/bundle/ms.post-3.1.9.gbl.min.js.map +0 -1
  57. package/bundle/ms.post-3.1.9.integrity.json +0 -46
  58. package/bundle/ms.post-3.1.9.js.map +0 -1
  59. package/bundle/ms.post-3.1.9.min.js +0 -7
  60. package/bundle/ms.post-3.1.9.min.js.map +0 -1
package/src/DataModels.ts CHANGED
@@ -14,6 +14,8 @@ export interface IPayloadData {
14
14
  urlString: string;
15
15
  data: Uint8Array | string;
16
16
  headers?: { [name: string]: string };
17
+ timeout?: number;
18
+ disableXhrSync?: boolean;
17
19
  }
18
20
 
19
21
  /**
@@ -69,32 +71,43 @@ export interface IChannelConfiguration {
69
71
 
70
72
  /**
71
73
  * [Optional] The HTTP override that should be used to send requests, as an IXHROverride object.
74
+ * By default during the unload of a page or if the event specifies that it wants to use sendBeacon() or sync fetch (with keep-alive),
75
+ * this override will NOT be called. You can now change this behavior by enabling the 'alwaysUseXhrOverride' configuration value.
76
+ * The payload data (first argument) now also includes any configured 'timeout' (defaults to undefined) and whether you should avoid
77
+ * creating any synchronous XHR requests 'disableXhrSync' (defaults to false/undefined)
72
78
  */
73
79
  httpXHROverride?: IXHROverride;
80
+
74
81
  /**
75
82
  * Override for Instrumentation key
76
83
  */
77
84
  overrideInstrumentationKey?: string;
85
+
78
86
  /**
79
87
  * Override for Endpoint where telemetry data is sent
80
88
  */
81
89
  overrideEndpointUrl?: string;
90
+
82
91
  /**
83
92
  * The master off switch. Do not send any data if set to TRUE
84
93
  */
85
94
  disableTelemetry?: boolean;
95
+
86
96
  /**
87
97
  * MC1 and MS0 cookies will not be returned from Collector endpoint.
88
98
  */
89
99
  ignoreMc1Ms0CookieProcessing?: boolean;
100
+
90
101
  /**
91
102
  * Override for setTimeout
92
103
  */
93
104
  setTimeoutOverride?: typeof setTimeout;
105
+
94
106
  /**
95
107
  * Override for clearTimeout
96
108
  */
97
109
  clearTimeoutOverride?: typeof clearTimeout;
110
+
98
111
  /**
99
112
  * [Optional] POST channel preprocessing function. Can be used to gzip the payload before transmission and to set the appropriate
100
113
  * Content-Encoding header. The preprocessor is explicitly not called during teardown when using the sendBeacon() API.
@@ -151,6 +164,61 @@ export interface IChannelConfiguration {
151
164
  * This is used during initialization to identify the requested send transport, it will be ignored if a httpXHROverride is provided.
152
165
  */
153
166
  transports?: number | number[];
167
+
168
+ /**
169
+ * [Optional] Avoid adding request headers to the outgoing request that would cause a pre-flight (OPTIONS) request to be sent for each request.
170
+ * This currently defaults to true but will be changed once the collector handles additional settings to allow the browser to better cache any
171
+ * previous OPTIONS response, at which point some of the current dynamic values sent on the query string will be moved to a header.
172
+ */
173
+ avoidOptions?: boolean;
174
+
175
+ /**
176
+ * [Optional] Specify a timeout (in ms) to apply to requests when sending requests using XHR, XDR or fetch requests. Defaults to undefined
177
+ * and therefore the runtime defaults (normally zero for browser environments)
178
+ */
179
+ xhrTimeout?: number;
180
+
181
+ /**
182
+ * [Optional] When using Xhr for sending requests disable sending as synchronous during unload or synchronous flush.
183
+ * You should enable this feature for IE (when there is no sendBeacon() or fetch (with keep-alive)) and you have clients
184
+ * that end up blocking the UI during page unloading. This will cause ALL XHR requests to be sent asynchronously which
185
+ * during page unload may result in the lose of telemetry.
186
+ */
187
+ disableXhrSync?: boolean;
188
+
189
+ /**
190
+ * [Optional] By default during unload (or when you specify to use sendBeacon() or sync fetch (with keep-alive) for an event) the SDK
191
+ * ignores any provided httpXhrOverride and attempts to use sendBeacon() or fetch(with keep-alive) when they are available.
192
+ * When this configuration option is true any provided httpXhrOverride will always be used, so any provided httpXhrOverride will
193
+ * also need to "handle" the synchronous unload scenario.
194
+ */
195
+ alwaysUseXhrOverride?: boolean;
196
+
197
+ /**
198
+ * [Optional] Identifies the number of times any single event will be retried if it receives a failed (retirable) response, this
199
+ * causes the event to be internally "requeued" and resent in the next batch. As each normal batched send request is retried at
200
+ * least once before starting to increase the internal backoff send interval, normally batched events will generally be attempted
201
+ * the next nearest even number of times. This means that the total number of actual send attempts will almost always be even
202
+ * (setting to 5 will cause 6 requests), unless using manual synchronous flushing (calling flush(false)) which is not subject to
203
+ * request level retry attempts.
204
+ * Defaults to 6 times.
205
+ */
206
+ maxEventRetryAttempts?: number;
207
+
208
+ /**
209
+ * [Optional] Identifies the number of times any single event will be retried if it receives a failed (retriable) response as part
210
+ * of processing / flushing events once a page unload state has been detected, this causes the event to be internally "requeued"
211
+ * and resent in the next batch, which during page unload. Unlike the normal batching process, send requests are never retried,
212
+ * so the value listed here is always the maximum number of attempts for any single event.
213
+ * Defaults to 2 times.
214
+ * Notes:
215
+ * The SDK by default will use the sendBeacon() API if it exists which is treated as a fire and forget successful response, so for
216
+ * environments that support or supply this API the events won't be retried (because they will be deeded to be successfully sent).
217
+ * When an environment (IE) doesn't support sendBeacon(), this will cause multiple synchronous (by default) XMLHttpRequests to be sent,
218
+ * which will block the UI until a response is received. You can disable ALL synchronous XHR requests by setting the 'disableXhrSync'
219
+ * configuration setting and/or changing this value to 0 or 1.
220
+ */
221
+ maxUnloadEventRetryAttempts?: number;
154
222
  }
155
223
 
156
224
  /**
package/src/EventBatch.ts CHANGED
@@ -4,7 +4,27 @@
4
4
  * @copyright Microsoft 2020
5
5
  */
6
6
  import { IPostTransmissionTelemetryItem } from "./DataModels";
7
- import { isNullOrUndefined } from "@microsoft/1ds-core-js";
7
+ import { isNullOrUndefined, isUndefined, isValueAssigned } from "@microsoft/1ds-core-js";
8
+ import { strMsfpc } from "./Constants";
9
+
10
+ function _getEventMsfpc(theEvent: IPostTransmissionTelemetryItem): string {
11
+ let intWeb = ((theEvent.ext || {})["intweb"]);
12
+ if (intWeb && isValueAssigned(intWeb[strMsfpc])) {
13
+ return intWeb[strMsfpc];
14
+ }
15
+
16
+ return null;
17
+ }
18
+
19
+ function _getMsfpc(theEvents: IPostTransmissionTelemetryItem[]): string {
20
+ let msfpc: string = null;
21
+
22
+ for (let lp = 0; msfpc === null && lp < theEvents.length; lp++) {
23
+ msfpc = _getEventMsfpc(theEvents[lp]);
24
+ }
25
+
26
+ return msfpc;
27
+ }
8
28
 
9
29
  /**
10
30
  * This class defines a "batch" events related to a specific iKey, it is used by the PostChannel and HttpManager
@@ -27,6 +47,11 @@ export class EventBatch {
27
47
  */
28
48
  public iKey: () => string;
29
49
 
50
+ /**
51
+ * Returns the first msfpc value from the batch
52
+ */
53
+ public Msfpc:() => string;
54
+
30
55
  /**
31
56
  * Returns the number of events contained in the batch
32
57
  */
@@ -38,10 +63,9 @@ export class EventBatch {
38
63
  * Add all of the events to the current batch, if the max number of events would be exceeded then no
39
64
  * events are added.
40
65
  * @param theEvents - The events that needs to be batched.
41
- * @param append - True (default) add the event to the end of the queue otherwise prepend (used for re-queuing)
42
66
  * @returns The number of events added.
43
67
  */
44
- public addEvents: (theEvents: IPostTransmissionTelemetryItem[], append: boolean) => number;
68
+ public addEvent: (theEvents: IPostTransmissionTelemetryItem) => boolean;
45
69
 
46
70
  /**
47
71
  * Split this batch into 2 with any events > fromEvent returned in the new batch and all other
@@ -59,11 +83,17 @@ export class EventBatch {
59
83
  private constructor(iKey: string, addEvents?: IPostTransmissionTelemetryItem[]) {
60
84
  let events: IPostTransmissionTelemetryItem[] = addEvents ? [].concat(addEvents) : [];
61
85
  let _self = this;
86
+ let _msfpc = _getMsfpc(events);
62
87
 
63
88
  _self.iKey = (): string => {
64
89
  return iKey;
65
90
  };
66
91
 
92
+ _self.Msfpc = (): string => {
93
+ // return the cached value unless it's undefined -- used to avoid cpu
94
+ return _msfpc || "";
95
+ };
96
+
67
97
  _self.count = (): number => {
68
98
  return events.length;
69
99
  };
@@ -72,33 +102,36 @@ export class EventBatch {
72
102
  return events;
73
103
  };
74
104
 
75
- _self.addEvents = (theEvents: IPostTransmissionTelemetryItem[], append = true): number => {
76
- if (theEvents && theEvents.length > 0) {
77
- if (append) {
78
- events = events.concat(theEvents);
79
- } else {
80
- events = theEvents.concat(events);
105
+ _self.addEvent = (theEvent: IPostTransmissionTelemetryItem): boolean => {
106
+ if (theEvent) {
107
+ events.push(theEvent);
108
+ if (!_msfpc) {
109
+ // Not found so try and find one
110
+ _msfpc = _getEventMsfpc(theEvent);
81
111
  }
82
112
 
83
- return theEvents.length;
113
+ return true;
84
114
  }
85
115
 
86
- return 0;
116
+ return false;
87
117
  };
88
118
 
89
119
  _self.split = (fromEvent: number, numEvents?: number) => {
90
120
  // Create a new batch with the same iKey
91
- let newBatch = new EventBatch(iKey);
121
+ let theEvents: IPostTransmissionTelemetryItem[];
92
122
  if (fromEvent < events.length) {
93
123
  let cnt = events.length - fromEvent;
94
124
  if (!isNullOrUndefined(numEvents)) {
95
125
  cnt = numEvents < cnt ? numEvents : cnt;
96
126
  }
97
127
 
98
- newBatch.addEvents(events.splice(fromEvent, cnt), true);
128
+ theEvents = events.splice(fromEvent, cnt);
129
+
130
+ // reset the fetched msfpc value
131
+ _msfpc = _getMsfpc(events);
99
132
  }
100
133
 
101
- return newBatch;
134
+ return new EventBatch(iKey, theEvents);
102
135
  };
103
136
  }
104
137
  }