@constructor-io/constructorio-client-javascript 2.25.3 → 2.26.1

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.
@@ -18,6 +18,7 @@ var HumanityCheck = require('../utils/humanity-check');
18
18
  var helpers = require('../utils/helpers');
19
19
 
20
20
  var storageKey = '_constructorio_requests';
21
+ var requestTTL = 1800000; // 30 minutes in milliseconds
21
22
 
22
23
  var RequestQueue = /*#__PURE__*/function () {
23
24
  function RequestQueue(options, eventemitter) {
@@ -78,21 +79,37 @@ var RequestQueue = /*#__PURE__*/function () {
78
79
  var _nextInQueue = nextInQueue,
79
80
  networkParameters = _nextInQueue.networkParameters;
80
81
  var signal;
82
+ var instance = this;
83
+ RequestQueue.set(queue);
81
84
 
82
85
  if (networkParameters) {
83
86
  var controller = new AbortController();
84
87
  signal = controller.signal;
85
88
  helpers.applyNetworkTimeout(this.options, networkParameters, controller);
86
- }
87
-
88
- RequestQueue.set(queue); // Backwards compatibility with versions <= 2.0.0, can be removed in future
89
+ } // Backwards compatibility with versions <= 2.0.0, can be removed in future
89
90
  // - Request queue entries used to be strings with 'GET' method assumed
90
91
 
92
+
91
93
  if (typeof nextInQueue === 'string') {
92
94
  nextInQueue = {
93
95
  url: nextInQueue,
94
96
  method: 'GET'
95
97
  };
98
+ } // If events older than `requestTTL` exist in queue, clear request queue
99
+ // - Prevents issue where stale items are sent in perputity
100
+ // - No request should go unsent for longer than `requestTTL`
101
+
102
+
103
+ if (nextInQueue.url) {
104
+ // Pull `dt` parameter from URL, indicating origin time of request
105
+ var dtMatch = nextInQueue.url.match(/\?.*_dt=([^&]+)/);
106
+ var requestOriginTime = dtMatch && dtMatch[1];
107
+ var now = +new Date();
108
+
109
+ if (requestOriginTime && now - requestOriginTime > requestTTL) {
110
+ RequestQueue.remove();
111
+ return;
112
+ }
96
113
  }
97
114
 
98
115
  if (nextInQueue.method === 'GET') {
@@ -115,37 +132,44 @@ var RequestQueue = /*#__PURE__*/function () {
115
132
 
116
133
  if (request) {
117
134
  this.requestPending = true;
118
- var instance = this;
119
135
  request.then(function (response) {
120
136
  // Request was successful, and returned a 2XX status code
121
137
  if (response.ok) {
122
- instance.eventemitter.emit('success', {
123
- url: nextInQueue.url,
124
- method: nextInQueue.method,
125
- message: 'ok'
126
- });
127
- } // Request was successful, but returned a non-2XX status code
128
- else {
129
- response.json().then(function (json) {
130
- instance.eventemitter.emit('error', {
138
+ if (instance.eventemitter) {
139
+ instance.eventemitter.emit('success', {
131
140
  url: nextInQueue.url,
132
141
  method: nextInQueue.method,
133
- message: json && json.message
142
+ message: 'ok'
134
143
  });
144
+ }
145
+ } // Request was successful, but returned a non-2XX status code
146
+ else {
147
+ response.json().then(function (json) {
148
+ if (instance.eventemitter) {
149
+ instance.eventemitter.emit('error', {
150
+ url: nextInQueue.url,
151
+ method: nextInQueue.method,
152
+ message: json && json.message
153
+ });
154
+ }
135
155
  })["catch"](function (error) {
136
- instance.eventemitter.emit('error', {
137
- url: nextInQueue.url,
138
- method: nextInQueue.method,
139
- message: error.type
140
- });
156
+ if (instance.eventemitter) {
157
+ instance.eventemitter.emit('error', {
158
+ url: nextInQueue.url,
159
+ method: nextInQueue.method,
160
+ message: error.type
161
+ });
162
+ }
141
163
  });
142
164
  }
143
165
  })["catch"](function (error) {
144
- instance.eventemitter.emit('error', {
145
- url: nextInQueue.url,
146
- method: nextInQueue.method,
147
- message: error.toString()
148
- });
166
+ if (instance.eventemitter) {
167
+ instance.eventemitter.emit('error', {
168
+ url: nextInQueue.url,
169
+ method: nextInQueue.method,
170
+ message: error.toString()
171
+ });
172
+ }
149
173
  })["finally"](function () {
150
174
  _this2.requestPending = false;
151
175
 
@@ -177,7 +201,26 @@ var RequestQueue = /*#__PURE__*/function () {
177
201
  }, {
178
202
  key: "set",
179
203
  value: function set(queue) {
180
- store.local.set(storageKey, queue);
204
+ // If queue length is zero, remove entry entirely
205
+ if (!queue || Array.isArray(queue) && queue.length === 0) {
206
+ RequestQueue.remove();
207
+ } else {
208
+ store.local.set(storageKey, queue);
209
+ }
210
+
211
+ var localStorageQueue = RequestQueue.get(); // Ensure storage queue was set correctly in storage by checking length
212
+ // - Otherwise remove all pending requests as preventative measure
213
+ // - Firefox seeing identical events being transmitted multiple times
214
+
215
+ if (Array.isArray(localStorageQueue) && localStorageQueue.length !== queue.length) {
216
+ RequestQueue.remove();
217
+ }
218
+ } // Remove current request queue key
219
+
220
+ }, {
221
+ key: "remove",
222
+ value: function remove() {
223
+ store.local.remove(storageKey);
181
224
  }
182
225
  }]);
183
226
  return RequestQueue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@constructor-io/constructorio-client-javascript",
3
- "version": "2.25.3",
3
+ "version": "2.26.1",
4
4
  "description": "Constructor.io JavaScript client",
5
5
  "main": "lib/constructorio.js",
6
6
  "scripts": {