-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
341 lines (285 loc) · 8.57 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
const request = require('request-promise');
class PeachFactory {
constructor({
origin,
getAuthorisation,
repeatedTriggerDelay = 10,
checkedOutMax = 3,
postAuthFetchFeeze = 2000,
maxAttemptsPerRequest = 3,
verifySsl = true,
logRequests = true,
retryAfterHeader = 'Retry-After'
}) {
this.queue = [];
this.origin = origin;
this.maxAttemptsPerRequest = maxAttemptsPerRequest;
this.checkedOut = 0;
this.checkedOutMax = checkedOutMax;
this.postAuthFetchFeeze = postAuthFetchFeeze;
this.freezeUntil = null;
this.verifySsl = verifySsl;
this.logRequests = logRequests;
this.retryAfterHeader = retryAfterHeader.toLowerCase();
this.repeatedTriggerDelay = repeatedTriggerDelay;
this.authBeingUpdatedPromises = [];
this.auth = null;
this.latestRequestFactoryAuthVersion = 0;
if (typeof getAuthorisation === 'object' && getAuthorisation != null) {
this.getAuthorisation = this.getStandardAuthorisationFunction(getAuthorisation);
} else if (typeof getAuthorisation === 'function') {
this.getAuthorisation = getAuthorisation;
} else {
this.getAuthorisation = this.getStandardAuthorisationFunction({auth_method: 'none'});
}
this.repeatedTrigger();
}
getStandardAuthorisationFunction(getAuthorisation) {
switch (getAuthorisation.auth_method) {
case 'oauth2':
return async () => {
const data = await request({
method: 'POST',
uri: getAuthorisation.uri,
headers: {
Authorization: 'Basic ' + Buffer.from(getAuthorisation.client_id + ':' + getAuthorisation.client_secret).toString('base64'),
'Content-Type': 'application/x-www-form-urlencoded'
},
form: {
grant_type: 'client_credentials'
},
json: true
});
console.log(`RequestFactory fetched new Oauth2 authorisation from ${getAuthorisation.uri}:`, data);
return data;
};
case 'basic':
return () => {
return {
token_type: 'Basic',
access_token: Buffer.from(getAuthorisation.client_id + ':' + getAuthorisation.client_secret).toString('base64'),
expires_in: 999999
};
};
case 'none':
return () => {
return {
token_type: 'None',
access_token: '',
expires_in: 999999
};
};
}
throw new Error(`${getAuthorisation.auth_method} is an unknown/unsupported authorisation method`);
}
async getNewAuthObject() {
if (typeof this.getAuthorisation !== 'function') {
throw new Error('Factory attempted to update authorisation but no authorisation function was provided');
}
const {token_type, access_token, expires_at, expires_in} = await this.getAuthorisation();
if (typeof token_type !== 'string') {
throw new Error(`Call to getAuthorisation function returned a non-string token_type: "${token_type}"`);
}
if (typeof access_token !== 'string') {
throw new Error(`Call to getAuthorisation function returned a non-string access_token: "${access_token}"`);
}
let altered_expires_at;
if (expires_at instanceof Date) {
altered_expires_at = expires_at;
} else if (Number.isInteger(expires_in)) {
altered_expires_at = new Date(Date.now() + (expires_in * 1000))
} else {
throw new Error(`Call to getAuthorisation function returned a non-Date expires_at: "${expires_at}"`);
}
return {token_type, access_token, expires_at: altered_expires_at, version: ++this.latestRequestFactoryAuthVersion};
}
getAuthProperty(forceRefresh) {
return new Promise(async (resolve, reject) => {
if (!forceRefresh && this.auth != null && this.auth.expires_at > new Date()) {
resolve(this.auth);
return;
}
this.authBeingUpdatedPromises.push({resolve, reject});
if (this.authBeingUpdatedPromises.length === 1) {
let error = null;
let authObject = null
try {
authObject = await this.getNewAuthObject();
this.auth = authObject;
} catch (err) {
error = err;
} finally {
while (this.authBeingUpdatedPromises.length > 0) {
const poppedItem = this.authBeingUpdatedPromises.pop();
if (error) {
poppedItem.reject(error);
} else {
poppedItem.resolve(authObject);
}
}
this.freezeFor(this.postAuthFetchFeeze);
}
}
});
}
async getAuth(forceRefresh) {
const auth = await this.getAuthProperty(forceRefresh);
switch(auth.token_type) {
case 'Basic':
return {header: 'Basic ' + auth.access_token, version: auth.version};
case 'Bearer':
return {header: 'Bearer ' + auth.access_token, version: auth.version};
case 'None':
return {header: '', version: auth.version};
}
throw new Error(`Received an invalid token_type from getAuthorisation function "${auth.token_type}"`);
}
freezeFor(freezeFor) {
if (freezeFor > 0) {
const freezeUntil = new Date(Date.now() + freezeFor);
if (!(this.freezeUntil instanceof Date) || this.freezeUntil < freezeUntil) {
this.freezeUntil = freezeUntil;
}
}
}
getQueue() {
return this.queue;
}
pauseIfFrozen() {
return new Promise((resolve, reject) => {
const freezeUntil = this.freezeUntil;
if (!(freezeUntil instanceof Date)) {
resolve();
return;
}
const delay = freezeUntil.valueOf() - (new Date()).valueOf();
if (delay <= 0) {
resolve();
return;
}
setTimeout(async () => {
await this.pauseIfFrozen();
if (this.freezeUntil != null && freezeUntil.valueOf() === this.freezeUntil.valueOf()) {
this.freezeUntil = null;
}
resolve();
}, delay);
});
}
async doQueueItem(queueItem) {
const settings = {
method: queueItem.options.method,
uri: this.origin + queueItem.options.path,
body: queueItem.options.data,
headers: queueItem.options.headers ?? {},
json: !queueItem.options.notJson,
resolveWithFullResponse: true,
rejectUnauthorized: this.verifySsl
};
// Setup auth
let authType;
let requestFactoryAuthVersion;
if (queueItem.options.auth != null) {
if (typeof queueItem.options.auth === 'function') {
authType = 'CustomFunction';
settings.headers.Authorization = await queueItem.options.auth(queueItem.forceAuthRefresh);
} else {
authType = 'CustomStatic';
settings.headers.Authorization = queueItem.options.auth
}
} else if (this.getAuthorisation) {
authType = 'RequestFactory';
const {version, header} = await this.getAuth(queueItem.forceAuthRefresh);
requestFactoryAuthVersion = version;
settings.headers.Authorization = header;
}
queueItem.forceAuthRefresh = false;
// Pause if frozen
await this.pauseIfFrozen();
// log request
if (this.logRequests) {
console.log(settings);
}
// Do request
let resp;
try {
resp = await request(settings);
} catch (err) {
switch(err.statusCode) {
case 401:
if (authType === 'RequestFactory') {
if (this.auth != null && requestFactoryAuthVersion === this.latestRequestFactoryAuthVersion) {
this.auth = null;
}
} else if (authType === 'CustomFunction') {
queueItem.forceAuthRefresh = true;
}
break;
case 429:
const retryAfter = Number(err.response.headers['retry-after'] ?? 10) * 1000 + 150;
this.freezeFor(retryAfter);
break;
}
throw err;
}
return resp;
}
repeatedTrigger() {
this.triggerNextQueueItem();
setTimeout(() => {
this.repeatedTrigger();
}, this.repeatedTriggerDelay);
}
// This function should NEVER throw exceptions
async triggerNextQueueItem() {
if (this.queue.length === 0 || this.checkedOut >= this.checkedOutMax) {
return;
}
const queueItem = this.queue.shift();
if (queueItem == null) {
return;
}
this.checkedOut++;
try {
const response = await this.doQueueItem(queueItem);
queueItem.resolve(response);
} catch (err) {
queueItem.attempts++;
const maxAttempts = queueItem.options.maxAttempts || this.maxAttemptsPerRequest;
console.warn(`Failed RequestFactory Request (Attempt ${queueItem.attempts}/${maxAttempts}):`, queueItem, err);
if (queueItem.attempts >= maxAttempts) {
queueItem.reject(err);
} else {
this.queue.unshift(queueItem);
this.triggerNextQueueItem();
}
} finally {
this.checkedOut--;
}
}
do(options) { // options should contain .method, .path and (optionally) .data and .token attributes
return new Promise((resolve, reject) => {
this.queue.push({
options: options,
attempts: 0,
forceAuthRefresh: false,
resolve: resolve,
reject: reject
});
this.triggerNextQueueItem();
});
}
doPriority(options) {
return new Promise((resolve, reject) => {
this.queue.unshift({
options: options,
attempts: 0,
forceAuthRefresh: false,
resolve: resolve,
reject: reject
});
this.triggerNextQueueItem();
});
}
}
module.exports.PeachFactory = PeachFactory;