@constructor-io/constructorio-client-javascript 2.25.5 → 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.
@@ -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,6 +79,7 @@ var RequestQueue = /*#__PURE__*/function () {
78
79
  var _nextInQueue = nextInQueue,
79
80
  networkParameters = _nextInQueue.networkParameters;
80
81
  var signal;
82
+ var instance = this;
81
83
  RequestQueue.set(queue);
82
84
 
83
85
  if (networkParameters) {
@@ -93,6 +95,26 @@ var RequestQueue = /*#__PURE__*/function () {
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.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', {
143
+ if (instance.eventemitter) {
144
+ instance.eventemitter.emit('success', {
131
145
  url: nextInQueue.url,
132
146
  method: nextInQueue.method,
133
- message: json && json.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.emit('error', {
137
- url: nextInQueue.url,
138
- method: nextInQueue.method,
139
- message: error.type
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.emit('error', {
145
- url: nextInQueue.url,
146
- method: nextInQueue.method,
147
- message: error.toString()
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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@constructor-io/constructorio-client-javascript",
3
- "version": "2.25.5",
3
+ "version": "2.26.0",
4
4
  "description": "Constructor.io JavaScript client",
5
5
  "main": "lib/constructorio.js",
6
6
  "scripts": {