box-node-sdk 1.35.0 → 1.37.2
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/CHANGELOG.md +37 -2
- package/README.md +1 -1
- package/lib/api-request-manager.d.ts +38 -0
- package/lib/api-request-manager.js +48 -55
- package/lib/api-request-manager.js.map +1 -0
- package/lib/api-request.d.ts +141 -0
- package/lib/api-request.js +202 -281
- package/lib/api-request.js.map +1 -0
- package/lib/box-client.d.ts +269 -0
- package/lib/box-client.js +551 -713
- package/lib/box-client.js.map +1 -0
- package/lib/box-node-sdk.d.ts +216 -0
- package/lib/box-node-sdk.js +317 -352
- package/lib/box-node-sdk.js.map +1 -0
- package/lib/chunked-uploader.d.ts +129 -0
- package/lib/chunked-uploader.js +287 -358
- package/lib/chunked-uploader.js.map +1 -0
- package/lib/enterprise-event-stream.d.ts +82 -0
- package/lib/enterprise-event-stream.js +189 -203
- package/lib/enterprise-event-stream.js.map +1 -0
- package/lib/event-stream.d.ts +92 -0
- package/lib/event-stream.js +274 -302
- package/lib/event-stream.js.map +1 -0
- package/lib/managers/collaboration-allowlist.d.ts +137 -0
- package/lib/managers/collaboration-allowlist.js +200 -0
- package/lib/managers/collaboration-allowlist.js.map +1 -0
- package/lib/managers/collaboration-whitelist.d.ts +3 -0
- package/lib/managers/collaboration-whitelist.js +8 -222
- package/lib/managers/collaboration-whitelist.js.map +1 -0
- package/lib/managers/collaborations.d.ts +166 -0
- package/lib/managers/collaborations.js +225 -258
- package/lib/managers/collaborations.js.map +1 -0
- package/lib/managers/collections.d.ts +42 -0
- package/lib/managers/collections.js +45 -50
- package/lib/managers/collections.js.map +1 -0
- package/lib/managers/comments.d.ts +103 -0
- package/lib/managers/comments.js +158 -173
- package/lib/managers/comments.js.map +1 -0
- package/lib/managers/device-pins.d.ts +52 -0
- package/lib/managers/device-pins.js +75 -88
- package/lib/managers/device-pins.js.map +1 -0
- package/lib/managers/enterprise.d.ts +162 -0
- package/lib/managers/enterprise.js +168 -199
- package/lib/managers/enterprise.js.map +1 -0
- package/lib/managers/events.d.ts +177 -0
- package/lib/managers/events.js +230 -254
- package/lib/managers/events.js.map +1 -0
- package/lib/managers/files.d.ts +772 -0
- package/lib/managers/files.js +1400 -1602
- package/lib/managers/files.js.map +1 -0
- package/lib/managers/folders.d.ts +347 -0
- package/lib/managers/folders.js +551 -567
- package/lib/managers/folders.js.map +1 -0
- package/lib/managers/groups.d.ts +202 -0
- package/lib/managers/groups.js +238 -287
- package/lib/managers/groups.js.map +1 -0
- package/lib/managers/legal-hold-policies.d.ts +190 -0
- package/lib/managers/legal-hold-policies.js +228 -272
- package/lib/managers/legal-hold-policies.js.map +1 -0
- package/lib/managers/metadata.d.ts +228 -0
- package/lib/managers/metadata.js +265 -328
- package/lib/managers/metadata.js.map +1 -0
- package/lib/managers/recent-items.d.ts +38 -0
- package/lib/managers/recent-items.js +32 -39
- package/lib/managers/recent-items.js.map +1 -0
- package/lib/managers/retention-policies.d.ts +213 -0
- package/lib/managers/retention-policies.js +235 -281
- package/lib/managers/retention-policies.js.map +1 -0
- package/lib/managers/search.d.ts +82 -0
- package/lib/managers/search.js +68 -88
- package/lib/managers/search.js.map +1 -0
- package/lib/managers/shared-items.d.ts +33 -0
- package/lib/managers/shared-items.js +54 -62
- package/lib/managers/shared-items.js.map +1 -0
- package/lib/managers/storage-policies.d.ts +86 -0
- package/lib/managers/storage-policies.js +108 -142
- package/lib/managers/storage-policies.js.map +1 -0
- package/lib/managers/tasks.d.ts +161 -0
- package/lib/managers/tasks.js +219 -260
- package/lib/managers/tasks.js.map +1 -0
- package/lib/managers/terms-of-service.d.ts +161 -0
- package/lib/managers/terms-of-service.js +250 -273
- package/lib/managers/terms-of-service.js.map +1 -0
- package/lib/managers/trash.d.ts +30 -0
- package/lib/managers/trash.js +30 -41
- package/lib/managers/trash.js.map +1 -0
- package/lib/managers/users.d.ts +131 -0
- package/lib/managers/users.js +160 -203
- package/lib/managers/users.js.map +1 -0
- package/lib/managers/web-links.d.ts +127 -0
- package/lib/managers/web-links.js +183 -209
- package/lib/managers/web-links.js.map +1 -0
- package/lib/managers/webhooks.d.ts +166 -0
- package/lib/managers/webhooks.js +312 -305
- package/lib/managers/webhooks.js.map +1 -0
- package/lib/sessions/anonymous-session.d.ts +69 -0
- package/lib/sessions/anonymous-session.js +88 -102
- package/lib/sessions/anonymous-session.js.map +1 -0
- package/lib/sessions/app-auth-session.d.ts +92 -0
- package/lib/sessions/app-auth-session.js +140 -160
- package/lib/sessions/app-auth-session.js.map +1 -0
- package/lib/sessions/basic-session.d.ts +56 -0
- package/lib/sessions/basic-session.js +40 -50
- package/lib/sessions/basic-session.js.map +1 -0
- package/lib/sessions/persistent-session.d.ts +96 -0
- package/lib/sessions/persistent-session.js +191 -211
- package/lib/sessions/persistent-session.js.map +1 -0
- package/lib/token-manager.d.ts +191 -0
- package/lib/token-manager.js +390 -465
- package/lib/token-manager.js.map +1 -0
- package/lib/util/config.d.ts +86 -0
- package/lib/util/config.js +124 -152
- package/lib/util/config.js.map +1 -0
- package/lib/util/errors.d.ts +50 -0
- package/lib/util/errors.js +134 -145
- package/lib/util/errors.js.map +1 -0
- package/lib/util/exponential-backoff.d.ts +11 -0
- package/lib/util/exponential-backoff.js +10 -22
- package/lib/util/exponential-backoff.js.map +1 -0
- package/lib/util/paging-iterator.d.ts +53 -0
- package/lib/util/paging-iterator.js +202 -218
- package/lib/util/paging-iterator.js.map +1 -0
- package/lib/util/url-path.d.ts +16 -0
- package/lib/util/url-path.js +20 -35
- package/lib/util/url-path.js.map +1 -0
- package/package.json +24 -9
package/lib/event-stream.js
CHANGED
|
@@ -1,32 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
/**
|
|
2
3
|
* @fileoverview Event stream backed by the events API
|
|
3
4
|
*/
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
var __extends = (this && this.__extends) || (function () {
|
|
6
|
+
var extendStatics = function (d, b) {
|
|
7
|
+
extendStatics = Object.setPrototypeOf ||
|
|
8
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
9
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
10
|
+
return extendStatics(d, b);
|
|
11
|
+
};
|
|
12
|
+
return function (d, b) {
|
|
13
|
+
if (typeof b !== "function" && b !== null)
|
|
14
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
15
|
+
extendStatics(d, b);
|
|
16
|
+
function __() { this.constructor = d; }
|
|
17
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
18
|
+
};
|
|
19
|
+
})();
|
|
20
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
21
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
22
|
+
};
|
|
7
23
|
// ------------------------------------------------------------------------------
|
|
8
24
|
// Requirements
|
|
9
25
|
// ------------------------------------------------------------------------------
|
|
10
|
-
|
|
11
|
-
var
|
|
12
|
-
|
|
13
|
-
util = require('util'),
|
|
14
|
-
Promise = require('bluebird');
|
|
15
|
-
|
|
26
|
+
var bluebird_1 = require("bluebird");
|
|
27
|
+
var querystring_1 = __importDefault(require("querystring"));
|
|
28
|
+
var stream_1 = require("stream");
|
|
16
29
|
// ------------------------------------------------------------------------------
|
|
17
30
|
// Private
|
|
18
31
|
// ------------------------------------------------------------------------------
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
fetchInterval: 1000
|
|
32
|
+
var DEFAULT_OPTIONS = Object.freeze({
|
|
33
|
+
deduplicationFilterSize: 5000,
|
|
34
|
+
retryDelay: 1000,
|
|
35
|
+
fetchInterval: 1000,
|
|
24
36
|
});
|
|
25
|
-
|
|
26
37
|
// ------------------------------------------------------------------------------
|
|
27
38
|
// Public
|
|
28
39
|
// ------------------------------------------------------------------------------
|
|
29
|
-
|
|
30
40
|
/**
|
|
31
41
|
* Stream of Box events from a given client and point in time.
|
|
32
42
|
* @param {BoxClient} client The client to use to get events
|
|
@@ -38,293 +48,255 @@ const DEFAULT_OPTIONS = Object.freeze({
|
|
|
38
48
|
* @constructor
|
|
39
49
|
* @extends Readable
|
|
40
50
|
*/
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
*/
|
|
273
|
-
EventStream.prototype.cleanupDedupFilter = function(latestEvents) {
|
|
274
|
-
|
|
275
|
-
var dedupIDs = Object.keys(this._dedupHash);
|
|
276
|
-
|
|
277
|
-
dedupIDs.forEach(eventID => {
|
|
278
|
-
|
|
279
|
-
var isEventCleared = !latestEvents.find(e => e.event_id === eventID);
|
|
280
|
-
if (isEventCleared) {
|
|
281
|
-
delete this._dedupHash[eventID];
|
|
282
|
-
}
|
|
283
|
-
});
|
|
284
|
-
};
|
|
285
|
-
|
|
286
|
-
/**
|
|
287
|
-
* Implementation of the stream-internal read function. This is called
|
|
288
|
-
* by the stream whenever it needs more data, and will not be called again
|
|
289
|
-
* until data is pushed into the stream.
|
|
290
|
-
* @returns {void}
|
|
291
|
-
* @private
|
|
292
|
-
*/
|
|
293
|
-
EventStream.prototype._read = function() {
|
|
294
|
-
|
|
295
|
-
// Start the process of getting new events
|
|
296
|
-
this.getLongPollInfo();
|
|
297
|
-
};
|
|
298
|
-
|
|
299
|
-
/**
|
|
300
|
-
* Implementation of stream-internal `_destroy` function (v8.0.0 and later).
|
|
301
|
-
* Called by stream consumers to effectively stop polling via the public
|
|
302
|
-
* `destroy()`.
|
|
303
|
-
* @returns {void}
|
|
304
|
-
* @private
|
|
305
|
-
*/
|
|
306
|
-
EventStream.prototype._destroy = function() {
|
|
307
|
-
clearTimeout(this._retryTimer);
|
|
308
|
-
delete this._retryTimer;
|
|
309
|
-
};
|
|
310
|
-
|
|
51
|
+
var EventStream = /** @class */ (function (_super) {
|
|
52
|
+
__extends(EventStream, _super);
|
|
53
|
+
function EventStream(client, streamPosition, options) {
|
|
54
|
+
var _this = _super.call(this, {
|
|
55
|
+
objectMode: true,
|
|
56
|
+
}) || this;
|
|
57
|
+
/**
|
|
58
|
+
* @var {BoxClient} The client for making API calls
|
|
59
|
+
* @private
|
|
60
|
+
*/
|
|
61
|
+
_this._client = client;
|
|
62
|
+
/**
|
|
63
|
+
* @var {string} The latest stream position
|
|
64
|
+
* @private
|
|
65
|
+
*/
|
|
66
|
+
_this._streamPosition = streamPosition;
|
|
67
|
+
/**
|
|
68
|
+
* @var {?Object} The information for how to long poll
|
|
69
|
+
* @private
|
|
70
|
+
*/
|
|
71
|
+
_this._longPollInfo = undefined;
|
|
72
|
+
/**
|
|
73
|
+
* @var {int} The number of long poll requests we've made against one URL so far
|
|
74
|
+
* @private
|
|
75
|
+
*/
|
|
76
|
+
_this._longPollRetries = 0;
|
|
77
|
+
/**
|
|
78
|
+
* @var {Object.<string, boolean>} Hash of event IDs we've already pushed
|
|
79
|
+
* @private
|
|
80
|
+
*/
|
|
81
|
+
_this._dedupHash = {};
|
|
82
|
+
/**
|
|
83
|
+
* Rate limiting promise to ensure that events are not fetched too often,
|
|
84
|
+
* initially resolved to allow an immediate API call.
|
|
85
|
+
* @var {Promise}
|
|
86
|
+
* @private
|
|
87
|
+
*/
|
|
88
|
+
_this._rateLimiter = bluebird_1.Promise.resolve();
|
|
89
|
+
_this._options = Object.assign({}, DEFAULT_OPTIONS, options);
|
|
90
|
+
return _this;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Retrieve the url and params for long polling for new updates
|
|
94
|
+
* @returns {Promise} Promise for testing purposes
|
|
95
|
+
* @private
|
|
96
|
+
*/
|
|
97
|
+
EventStream.prototype.getLongPollInfo = function () {
|
|
98
|
+
var _this = this;
|
|
99
|
+
if (this.destroyed) {
|
|
100
|
+
return bluebird_1.Promise.resolve(false);
|
|
101
|
+
}
|
|
102
|
+
return this._client.events
|
|
103
|
+
.getLongPollInfo()
|
|
104
|
+
.then(function (longPollInfo) {
|
|
105
|
+
// On getting new long poll info, reset everything
|
|
106
|
+
_this._longPollInfo = longPollInfo;
|
|
107
|
+
_this._longPollRetries = 0;
|
|
108
|
+
return _this.doLongPoll();
|
|
109
|
+
})
|
|
110
|
+
.catch(function (err /* FIXME */) {
|
|
111
|
+
_this.emit('error', err);
|
|
112
|
+
// Only retry on resolvable errors
|
|
113
|
+
if (!err.authExpired) {
|
|
114
|
+
_this.retryPollInfo();
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
};
|
|
118
|
+
/**
|
|
119
|
+
* Long poll for notification of new events. We do this rather than
|
|
120
|
+
* polling for the events directly in order to minimize the number of API
|
|
121
|
+
* calls necessary.
|
|
122
|
+
* @returns {Promise} Promise for testing pruposes
|
|
123
|
+
* @private
|
|
124
|
+
*/
|
|
125
|
+
EventStream.prototype.doLongPoll = function () {
|
|
126
|
+
var _this = this;
|
|
127
|
+
if (this.destroyed) {
|
|
128
|
+
return bluebird_1.Promise.resolve(false);
|
|
129
|
+
}
|
|
130
|
+
// If we're over the max number of retries, reset
|
|
131
|
+
if (this._longPollRetries > this._longPollInfo.max_retries) {
|
|
132
|
+
return this.getLongPollInfo();
|
|
133
|
+
}
|
|
134
|
+
var url = this._longPollInfo.url, qsDelim = url.indexOf('?'), query = {};
|
|
135
|
+
// Break out the query params, otherwise the request URL gets messed up
|
|
136
|
+
if (qsDelim > 0) {
|
|
137
|
+
query = querystring_1.default.parse(url.substr(qsDelim + 1));
|
|
138
|
+
url = url.substr(0, qsDelim);
|
|
139
|
+
}
|
|
140
|
+
query.stream_position = this._streamPosition;
|
|
141
|
+
var options = {
|
|
142
|
+
qs: query,
|
|
143
|
+
timeout: this._longPollInfo.retry_timeout * 1000,
|
|
144
|
+
};
|
|
145
|
+
this._longPollRetries += 1;
|
|
146
|
+
return this._client
|
|
147
|
+
.wrapWithDefaultHandler(this._client.get)(url, options)
|
|
148
|
+
.then(function (data /* FIXME */) {
|
|
149
|
+
if (_this.destroyed) {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
if (data.message === 'reconnect') {
|
|
153
|
+
return _this.getLongPollInfo();
|
|
154
|
+
}
|
|
155
|
+
// We don't expect any messages other than reconnect and new_change, so if
|
|
156
|
+
// we get one just retry the long poll
|
|
157
|
+
if (data.message !== 'new_change') {
|
|
158
|
+
return _this.doLongPoll();
|
|
159
|
+
}
|
|
160
|
+
return _this.fetchEvents();
|
|
161
|
+
})
|
|
162
|
+
.catch(function () {
|
|
163
|
+
_this.retryPollInfo();
|
|
164
|
+
});
|
|
165
|
+
};
|
|
166
|
+
/**
|
|
167
|
+
* Retries long-polling after a delay.
|
|
168
|
+
* Does not attempt if stream is already destroyed.
|
|
169
|
+
* @returns {void}
|
|
170
|
+
* @private
|
|
171
|
+
*/
|
|
172
|
+
EventStream.prototype.retryPollInfo = function () {
|
|
173
|
+
var _this = this;
|
|
174
|
+
if (!this.destroyed) {
|
|
175
|
+
this._retryTimer = setTimeout(function () { return _this.getLongPollInfo(); }, this._options.retryDelay);
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
/**
|
|
179
|
+
* Fetch the latest group of events and push them into the stream
|
|
180
|
+
* @returns {Promise} Promise for testing purposes
|
|
181
|
+
* @private
|
|
182
|
+
*/
|
|
183
|
+
EventStream.prototype.fetchEvents = function () {
|
|
184
|
+
var _this = this;
|
|
185
|
+
if (this.destroyed) {
|
|
186
|
+
return bluebird_1.Promise.resolve(false);
|
|
187
|
+
}
|
|
188
|
+
var eventParams = {
|
|
189
|
+
stream_position: this._streamPosition,
|
|
190
|
+
limit: 500,
|
|
191
|
+
};
|
|
192
|
+
// Get new events after the rate limiter expires
|
|
193
|
+
return this._rateLimiter.then(function () {
|
|
194
|
+
return _this._client.events
|
|
195
|
+
.get(eventParams)
|
|
196
|
+
.then(function (events /* FIXME */) {
|
|
197
|
+
// Reset the rate limiter
|
|
198
|
+
_this._rateLimiter = bluebird_1.Promise.delay(_this._options.fetchInterval);
|
|
199
|
+
// If the response wasn't what we expected, re-poll
|
|
200
|
+
if (!events.entries || !events.next_stream_position) {
|
|
201
|
+
return _this.doLongPoll();
|
|
202
|
+
}
|
|
203
|
+
_this._streamPosition = events.next_stream_position;
|
|
204
|
+
// De-duplicate the fetched events, since the API often returns
|
|
205
|
+
// the same events at multiple subsequent stream positions
|
|
206
|
+
var newEvents = events.entries.filter(function (event /* FIXME */) { return !_this._dedupHash[event.event_id]; });
|
|
207
|
+
// If there aren't any non-duplicate events, go back to polling
|
|
208
|
+
if (newEvents.length === 0) {
|
|
209
|
+
return _this.doLongPoll();
|
|
210
|
+
}
|
|
211
|
+
// Pause the stream to avoid race conditions while pushing in the new events.
|
|
212
|
+
// Without this, _read() would be called again from inside each push(),
|
|
213
|
+
// resulting in multiple parallel calls to fetchEvents().
|
|
214
|
+
// See https://github.com/nodejs/node/issues/3203
|
|
215
|
+
var wasPaused = _this.isPaused();
|
|
216
|
+
_this.pause();
|
|
217
|
+
// Push new events into the stream
|
|
218
|
+
newEvents.forEach(function (event /* FIXME */) {
|
|
219
|
+
_this._dedupHash[event.event_id] = true;
|
|
220
|
+
_this.push(event);
|
|
221
|
+
});
|
|
222
|
+
if (!wasPaused) {
|
|
223
|
+
// This will deliver the events and trigger the next call to _read() once they have been consumed.
|
|
224
|
+
_this.resume();
|
|
225
|
+
}
|
|
226
|
+
// Once the deduplication filter gets too big, clean it up
|
|
227
|
+
if (Object.keys(_this._dedupHash).length >=
|
|
228
|
+
_this._options.deduplicationFilterSize) {
|
|
229
|
+
_this.cleanupDedupFilter(events.entries);
|
|
230
|
+
}
|
|
231
|
+
return true;
|
|
232
|
+
})
|
|
233
|
+
.catch(function (err /* FIXME */) {
|
|
234
|
+
_this.emit('error', err);
|
|
235
|
+
_this.retryPollInfo();
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
};
|
|
239
|
+
/**
|
|
240
|
+
* Clean up the deduplication filter, to prevent it from growing
|
|
241
|
+
* too big and eating up memory. We look at the latest set of events
|
|
242
|
+
* returned and assume that any IDs not in that set don't need to be
|
|
243
|
+
* tracked for deduplication any more.
|
|
244
|
+
* @param {Object[]} latestEvents The latest events from the API
|
|
245
|
+
* @returns {void}
|
|
246
|
+
* @private
|
|
247
|
+
*/
|
|
248
|
+
EventStream.prototype.cleanupDedupFilter = function (latestEvents /* FIXME */) {
|
|
249
|
+
var _this = this;
|
|
250
|
+
var dedupIDs = Object.keys(this._dedupHash);
|
|
251
|
+
dedupIDs.forEach(function (eventID) {
|
|
252
|
+
var isEventCleared = !latestEvents.find(function (e /* FIXME */) { return e.event_id === eventID; });
|
|
253
|
+
if (isEventCleared) {
|
|
254
|
+
delete _this._dedupHash[eventID];
|
|
255
|
+
}
|
|
256
|
+
});
|
|
257
|
+
};
|
|
258
|
+
/**
|
|
259
|
+
* Implementation of the stream-internal read function. This is called
|
|
260
|
+
* by the stream whenever it needs more data, and will not be called again
|
|
261
|
+
* until data is pushed into the stream.
|
|
262
|
+
* @returns {void}
|
|
263
|
+
* @private
|
|
264
|
+
*/
|
|
265
|
+
EventStream.prototype._read = function () {
|
|
266
|
+
// Start the process of getting new events
|
|
267
|
+
this.getLongPollInfo();
|
|
268
|
+
};
|
|
269
|
+
/**
|
|
270
|
+
* Implementation of stream-internal `_destroy` function (v8.0.0 and later).
|
|
271
|
+
* Called by stream consumers to effectively stop polling via the public
|
|
272
|
+
* `destroy()`.
|
|
273
|
+
* @returns {void}
|
|
274
|
+
* @private
|
|
275
|
+
*/
|
|
276
|
+
EventStream.prototype._destroy = function () {
|
|
277
|
+
clearTimeout(this._retryTimer);
|
|
278
|
+
delete this._retryTimer;
|
|
279
|
+
};
|
|
280
|
+
return EventStream;
|
|
281
|
+
}(stream_1.Readable));
|
|
311
282
|
// backwards-compat for Node.js pre-v8.0.0
|
|
312
283
|
/* istanbul ignore if */
|
|
313
|
-
if (typeof Readable.prototype.destroy !== 'function') {
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
284
|
+
if (typeof stream_1.Readable.prototype.destroy !== 'function') {
|
|
285
|
+
/**
|
|
286
|
+
* Destroys the stream. Rough polyfill for `Readable#destroy`.
|
|
287
|
+
* @returns {void}
|
|
288
|
+
* @public
|
|
289
|
+
*/
|
|
290
|
+
EventStream.prototype.destroy = function () {
|
|
291
|
+
var _this = this;
|
|
292
|
+
if (!this.destroyed) {
|
|
293
|
+
process.nextTick(function () {
|
|
294
|
+
_this.emit('close');
|
|
295
|
+
});
|
|
296
|
+
this.destroyed = true;
|
|
297
|
+
this._destroy();
|
|
298
|
+
}
|
|
299
|
+
};
|
|
328
300
|
}
|
|
329
|
-
|
|
330
301
|
module.exports = EventStream;
|
|
302
|
+
//# sourceMappingURL=event-stream.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"event-stream.js","sourceRoot":"","sources":["../src/event-stream.ts"],"names":[],"mappings":";AAAA;;GAEG;;;;;;;;;;;;;;;;;;;AAEH,iFAAiF;AACjF,eAAe;AACf,iFAAiF;AAEjF,qCAAmC;AACnC,4DAA6B;AAC7B,iCAAkC;AAoBlC,iFAAiF;AACjF,UAAU;AACV,iFAAiF;AAEjF,IAAM,eAAe,GAAY,MAAM,CAAC,MAAM,CAAC;IAC9C,uBAAuB,EAAE,IAAI;IAC7B,UAAU,EAAE,IAAI;IAChB,aAAa,EAAE,IAAI;CACnB,CA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