@constructor-io/constructorio-client-javascript 2.25.2 → 2.26.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.
- package/lib/modules/tracker.js +1 -1
- package/lib/utils/request-queue.js +73 -25
- package/package.json +1 -1
package/lib/modules/tracker.js
CHANGED
|
@@ -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,42 @@ 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
|
+
instance.eventemitter.emit('error', {
|
|
112
|
+
url: nextInQueue.url,
|
|
113
|
+
method: nextInQueue.method,
|
|
114
|
+
message: "Request queue cleared - an item in the queue existed for longer than TTL value of ".concat(requestTTL, "ms")
|
|
115
|
+
});
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
96
118
|
}
|
|
97
119
|
|
|
98
120
|
if (nextInQueue.method === 'GET') {
|
|
@@ -115,37 +137,44 @@ var RequestQueue = /*#__PURE__*/function () {
|
|
|
115
137
|
|
|
116
138
|
if (request) {
|
|
117
139
|
this.requestPending = true;
|
|
118
|
-
var instance = this;
|
|
119
140
|
request.then(function (response) {
|
|
120
141
|
// Request was successful, and returned a 2XX status code
|
|
121
142
|
if (response.ok) {
|
|
122
|
-
instance.eventemitter
|
|
123
|
-
|
|
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', {
|
|
143
|
+
if (instance.eventemitter) {
|
|
144
|
+
instance.eventemitter.emit('success', {
|
|
131
145
|
url: nextInQueue.url,
|
|
132
146
|
method: nextInQueue.method,
|
|
133
|
-
message:
|
|
147
|
+
message: 'ok'
|
|
134
148
|
});
|
|
149
|
+
}
|
|
150
|
+
} // Request was successful, but returned a non-2XX status code
|
|
151
|
+
else {
|
|
152
|
+
response.json().then(function (json) {
|
|
153
|
+
if (instance.eventemitter) {
|
|
154
|
+
instance.eventemitter.emit('error', {
|
|
155
|
+
url: nextInQueue.url,
|
|
156
|
+
method: nextInQueue.method,
|
|
157
|
+
message: json && json.message
|
|
158
|
+
});
|
|
159
|
+
}
|
|
135
160
|
})["catch"](function (error) {
|
|
136
|
-
instance.eventemitter
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
161
|
+
if (instance.eventemitter) {
|
|
162
|
+
instance.eventemitter.emit('error', {
|
|
163
|
+
url: nextInQueue.url,
|
|
164
|
+
method: nextInQueue.method,
|
|
165
|
+
message: error.type
|
|
166
|
+
});
|
|
167
|
+
}
|
|
141
168
|
});
|
|
142
169
|
}
|
|
143
170
|
})["catch"](function (error) {
|
|
144
|
-
instance.eventemitter
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
171
|
+
if (instance.eventemitter) {
|
|
172
|
+
instance.eventemitter.emit('error', {
|
|
173
|
+
url: nextInQueue.url,
|
|
174
|
+
method: nextInQueue.method,
|
|
175
|
+
message: error.toString()
|
|
176
|
+
});
|
|
177
|
+
}
|
|
149
178
|
})["finally"](function () {
|
|
150
179
|
_this2.requestPending = false;
|
|
151
180
|
|
|
@@ -177,7 +206,26 @@ var RequestQueue = /*#__PURE__*/function () {
|
|
|
177
206
|
}, {
|
|
178
207
|
key: "set",
|
|
179
208
|
value: function set(queue) {
|
|
180
|
-
|
|
209
|
+
// If queue length is zero, remove entry entirely
|
|
210
|
+
if (!queue || Array.isArray(queue) && queue.length === 0) {
|
|
211
|
+
RequestQueue.remove();
|
|
212
|
+
} else {
|
|
213
|
+
store.local.set(storageKey, queue);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
var localStorageQueue = RequestQueue.get(); // Ensure storage queue was set correctly in storage by checking length
|
|
217
|
+
// - Otherwise remove all pending requests as preventative measure
|
|
218
|
+
// - Firefox seeing identical events being transmitted multiple times
|
|
219
|
+
|
|
220
|
+
if (Array.isArray(localStorageQueue) && localStorageQueue.length !== queue.length) {
|
|
221
|
+
RequestQueue.remove();
|
|
222
|
+
}
|
|
223
|
+
} // Remove current request queue key
|
|
224
|
+
|
|
225
|
+
}, {
|
|
226
|
+
key: "remove",
|
|
227
|
+
value: function remove() {
|
|
228
|
+
store.local.remove(storageKey);
|
|
181
229
|
}
|
|
182
230
|
}]);
|
|
183
231
|
return RequestQueue;
|