@wireapp/api-client 26.10.14 → 26.10.16
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/http/HttpClient.d.ts
CHANGED
|
@@ -19,6 +19,7 @@ export declare class HttpClient extends EventEmitter {
|
|
|
19
19
|
private readonly logger;
|
|
20
20
|
private connectionState;
|
|
21
21
|
private readonly requestQueue;
|
|
22
|
+
private readonly backOffQueue;
|
|
22
23
|
static readonly TOPIC: typeof TOPIC;
|
|
23
24
|
private versionPrefix;
|
|
24
25
|
constructor(config: Config, accessTokenStore: AccessTokenStore);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"HttpClient.d.ts","sourceRoot":"","sources":["../../src/http/HttpClient.ts"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"HttpClient.d.ts","sourceRoot":"","sources":["../../src/http/HttpClient.ts"],"names":[],"mappings":";AAmBA,OAAc,EAAC,UAAU,EAA+B,kBAAkB,EAAE,aAAa,EAAC,MAAM,OAAO,CAAC;AAKxG,OAAO,EAAC,YAAY,EAAC,MAAM,QAAQ,CAAC;AAKpC,OAAO,EACL,eAAe,EACf,gBAAgB,EAEhB,iBAAiB,EACjB,kBAAkB,EAEnB,MAAM,UAAU,CAAC;AAClB,OAAO,EAAC,MAAM,EAAC,MAAM,WAAW,CAAC;AACjC,OAAO,EAAC,YAAY,EAAsB,eAAe,EAA0B,MAAM,UAAU,CAAC;AAIpG,aAAK,KAAK;IACR,0BAA0B,gDAAgD;IAC1E,gBAAgB,sCAAsC;CACvD;AAED,MAAM,WAAW,UAAU;IACzB,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,0BAA0B,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,eAAe,KAAK,IAAI,GAAG,IAAI,CAAC;IAE9F,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,gBAAgB,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,iBAAiB,GAAG,k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,EACxB,MAAM,EAAE,kBAAkB,EAC1B,YAAY,GAAE,OAAe,EAC7B,oBAAoB,GAAE,OAAe,GACpC,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;IAoBrB,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,kBAAkB,EAAE,oBAAoB,GAAE,OAAe,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;IAmBzG,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,kBAAkB,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;IAQjE,kBAAkB,CAAC,CAAC,EACzB,MAAM,EAAE,kBAAkB,EAC1B,oBAAoB,GAAE,OAAe,GACpC,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;IAQrB,eAAe,CAAC,CAAC,EACtB,MAAM,EAAE,kBAAkB,EAC1B,oBAAoB,GAAE,OAAe,GACpC,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;CAO7B"}
|
package/lib/http/HttpClient.js
CHANGED
|
@@ -45,8 +45,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
45
45
|
};
|
|
46
46
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
47
47
|
exports.HttpClient = void 0;
|
|
48
|
-
const ExponentialBackoff_1 = require("@wireapp/commons/lib/util/ExponentialBackoff");
|
|
49
|
-
const TimeUtil_1 = require("@wireapp/commons/lib/util/TimeUtil");
|
|
50
48
|
const axios_1 = __importStar(require("axios"));
|
|
51
49
|
const axios_retry_1 = __importStar(require("axios-retry"));
|
|
52
50
|
const logdown_1 = __importDefault(require("logdown"));
|
|
@@ -101,6 +99,16 @@ class HttpClient extends events_1.EventEmitter {
|
|
|
101
99
|
maxRetries: 0,
|
|
102
100
|
retryDelay: commons_1.TimeUtil.TimeInMillis.SECOND,
|
|
103
101
|
});
|
|
102
|
+
this.backOffQueue = new priority_queue_1.PriorityQueue({
|
|
103
|
+
maxRetries: 10,
|
|
104
|
+
retryDelay: 200,
|
|
105
|
+
maxRetryDelay: 2000,
|
|
106
|
+
shouldRetry: error => {
|
|
107
|
+
var _a;
|
|
108
|
+
const isTooManyRequestsError = axios_1.default.isAxiosError(error) && ((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 420;
|
|
109
|
+
return isTooManyRequestsError;
|
|
110
|
+
},
|
|
111
|
+
});
|
|
104
112
|
}
|
|
105
113
|
getBaseUrl() {
|
|
106
114
|
return `${this.config.urls.rest}${this.versionPrefix}`;
|
|
@@ -115,12 +123,7 @@ class HttpClient extends events_1.EventEmitter {
|
|
|
115
123
|
}
|
|
116
124
|
}
|
|
117
125
|
async _sendRequest(config, tokenAsParam = false, isFirstTry = true) {
|
|
118
|
-
var _a, _b
|
|
119
|
-
const backoffKey = 'too-many-request-backoff';
|
|
120
|
-
const { backOff, resetBackOff } = (0, ExponentialBackoff_1.exponentialBackoff)(backoffKey, {
|
|
121
|
-
maxDelay: TimeUtil_1.TimeInMillis.SECOND * 2,
|
|
122
|
-
minDelay: 100,
|
|
123
|
-
});
|
|
126
|
+
var _a, _b;
|
|
124
127
|
if (this.accessTokenStore.accessToken) {
|
|
125
128
|
const { token_type, access_token } = this.accessTokenStore.accessToken;
|
|
126
129
|
if (tokenAsParam) {
|
|
@@ -135,17 +138,9 @@ class HttpClient extends events_1.EventEmitter {
|
|
|
135
138
|
// We want to prefix all urls, except the ones with cookies which are attached to unprefixed urls
|
|
136
139
|
url: config.withCredentials ? config.url : `${this.versionPrefix}${config.url}`, maxBodyLength: FILE_SIZE_100_MB, maxContentLength: FILE_SIZE_100_MB }));
|
|
137
140
|
this.updateConnectionState(http_1.ConnectionState.CONNECTED);
|
|
138
|
-
resetBackOff();
|
|
139
141
|
return response;
|
|
140
142
|
}
|
|
141
143
|
catch (error) {
|
|
142
|
-
const isTooManyRequestsError = axios_1.default.isAxiosError(error) && ((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 420;
|
|
143
|
-
if (isTooManyRequestsError) {
|
|
144
|
-
return backOff(() => this._sendRequest(config, tokenAsParam, false), () => {
|
|
145
|
-
this.logger.error('Too many requests error retry limit reached', error);
|
|
146
|
-
throw error;
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
144
|
const retryWithTokenRefresh = async () => {
|
|
150
145
|
this.logger.warn(`Access token refresh triggered for "${config.method}" request to "${config.url}".`);
|
|
151
146
|
await this.refreshAccessToken();
|
|
@@ -154,8 +149,8 @@ class HttpClient extends events_1.EventEmitter {
|
|
|
154
149
|
};
|
|
155
150
|
return this._sendRequest(config, tokenAsParam, false);
|
|
156
151
|
};
|
|
157
|
-
const hasAccessToken = !!((
|
|
158
|
-
if (axios_1.default.isAxiosError(error) && ((
|
|
152
|
+
const hasAccessToken = !!((_a = this.accessTokenStore) === null || _a === void 0 ? void 0 : _a.accessToken);
|
|
153
|
+
if (axios_1.default.isAxiosError(error) && ((_b = error.response) === null || _b === void 0 ? void 0 : _b.status) === http_1.StatusCode.UNAUTHORIZED) {
|
|
159
154
|
return retryWithTokenRefresh();
|
|
160
155
|
}
|
|
161
156
|
if (HttpClient.isBackendError(error)) {
|
|
@@ -234,9 +229,22 @@ class HttpClient extends events_1.EventEmitter {
|
|
|
234
229
|
return this.accessTokenStore.updateToken(newToken);
|
|
235
230
|
}
|
|
236
231
|
async sendRequest(config, tokenAsParam = false, isSynchronousRequest = false) {
|
|
237
|
-
|
|
232
|
+
var _a;
|
|
233
|
+
const promise = isSynchronousRequest
|
|
238
234
|
? this.requestQueue.add(() => this._sendRequest(config, tokenAsParam))
|
|
239
235
|
: this._sendRequest(config, tokenAsParam);
|
|
236
|
+
try {
|
|
237
|
+
return await promise;
|
|
238
|
+
}
|
|
239
|
+
catch (error) {
|
|
240
|
+
// If the request failed due to too many requests, we want put it into the backoff queue
|
|
241
|
+
// It will be retried after a (growing) delay
|
|
242
|
+
const isTooManyRequestsError = axios_1.default.isAxiosError(error) && ((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 420;
|
|
243
|
+
if (isTooManyRequestsError) {
|
|
244
|
+
return this.backOffQueue.add(() => this._sendRequest(config, tokenAsParam));
|
|
245
|
+
}
|
|
246
|
+
throw error;
|
|
247
|
+
}
|
|
240
248
|
}
|
|
241
249
|
sendJSON(config, isSynchronousRequest = false) {
|
|
242
250
|
var _a, _b, _c;
|
package/package.json
CHANGED
|
@@ -13,8 +13,8 @@
|
|
|
13
13
|
"./lib/shims/node/websocket": "./lib/shims/browser/websocket.js"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@wireapp/commons": "^5.2.
|
|
17
|
-
"@wireapp/priority-queue": "^2.1.
|
|
16
|
+
"@wireapp/commons": "^5.2.7",
|
|
17
|
+
"@wireapp/priority-queue": "^2.1.5",
|
|
18
18
|
"@wireapp/protocol-messaging": "1.44.0",
|
|
19
19
|
"axios": "1.6.7",
|
|
20
20
|
"axios-retry": "4.0.0",
|
|
@@ -66,6 +66,6 @@
|
|
|
66
66
|
"watch": "webpack serve --config webpack.browser.js",
|
|
67
67
|
"prepare": "yarn dist"
|
|
68
68
|
},
|
|
69
|
-
"version": "26.10.
|
|
70
|
-
"gitHead": "
|
|
69
|
+
"version": "26.10.16",
|
|
70
|
+
"gitHead": "8f1f305c6a2d6993b40a498933ba7258466cd572"
|
|
71
71
|
}
|