@ohos-ports/genesys-cloud-streaming-client 20.1.0-beta.1

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.
Files changed (176) hide show
  1. package/README.md +47 -0
  2. package/dist/cjs/alerting-leader.d.ts +29 -0
  3. package/dist/cjs/alerting-leader.js +163 -0
  4. package/dist/cjs/client.d.ts +84 -0
  5. package/dist/cjs/client.js +680 -0
  6. package/dist/cjs/connection-manager.d.ts +18 -0
  7. package/dist/cjs/connection-manager.js +120 -0
  8. package/dist/cjs/connection-transfer.d.ts +7 -0
  9. package/dist/cjs/connection-transfer.js +20 -0
  10. package/dist/cjs/http-client.d.ts +18 -0
  11. package/dist/cjs/http-client.js +230 -0
  12. package/dist/cjs/index.d.ts +10 -0
  13. package/dist/cjs/index.js +31 -0
  14. package/dist/cjs/messenger.d.ts +37 -0
  15. package/dist/cjs/messenger.js +54 -0
  16. package/dist/cjs/notifications.d.ts +94 -0
  17. package/dist/cjs/notifications.js +495 -0
  18. package/dist/cjs/ping.d.ts +21 -0
  19. package/dist/cjs/ping.js +60 -0
  20. package/dist/cjs/polyfills.d.ts +0 -0
  21. package/dist/cjs/polyfills.js +6 -0
  22. package/dist/cjs/server-monitor.d.ts +16 -0
  23. package/dist/cjs/server-monitor.js +44 -0
  24. package/dist/cjs/stanza-definitions/webrtc-signaling.d.ts +29 -0
  25. package/dist/cjs/stanza-definitions/webrtc-signaling.js +113 -0
  26. package/dist/cjs/stanza-definitions/xep0051.d.ts +19 -0
  27. package/dist/cjs/stanza-definitions/xep0051.js +28 -0
  28. package/dist/cjs/stats-formatter.d.ts +6 -0
  29. package/dist/cjs/stats-formatter.js +63 -0
  30. package/dist/cjs/types/genesys-cloud-media-session.d.ts +64 -0
  31. package/dist/cjs/types/genesys-cloud-media-session.js +353 -0
  32. package/dist/cjs/types/interfaces.d.ts +310 -0
  33. package/dist/cjs/types/interfaces.js +22 -0
  34. package/dist/cjs/types/libs.d.ts +32 -0
  35. package/dist/cjs/types/libs.js +1 -0
  36. package/dist/cjs/types/media-session.d.ts +76 -0
  37. package/dist/cjs/types/media-session.js +2 -0
  38. package/dist/cjs/types/named-agent.d.ts +13 -0
  39. package/dist/cjs/types/named-agent.js +2 -0
  40. package/dist/cjs/types/offline-error.d.ts +2 -0
  41. package/dist/cjs/types/offline-error.js +5 -0
  42. package/dist/cjs/types/sasl-error.d.ts +8 -0
  43. package/dist/cjs/types/sasl-error.js +12 -0
  44. package/dist/cjs/types/stanza-media-session.d.ts +56 -0
  45. package/dist/cjs/types/stanza-media-session.js +255 -0
  46. package/dist/cjs/types/streaming-client-extension.d.ts +14 -0
  47. package/dist/cjs/types/streaming-client-extension.js +2 -0
  48. package/dist/cjs/types/timeout-error.d.ts +3 -0
  49. package/dist/cjs/types/timeout-error.js +9 -0
  50. package/dist/cjs/types/user-cancelled-error.d.ts +2 -0
  51. package/dist/cjs/types/user-cancelled-error.js +5 -0
  52. package/dist/cjs/utils.d.ts +34 -0
  53. package/dist/cjs/utils.js +202 -0
  54. package/dist/cjs/webrtc.d.ts +126 -0
  55. package/dist/cjs/webrtc.js +891 -0
  56. package/dist/deploy-info.json +21 -0
  57. package/dist/es/alerting-leader.d.ts +29 -0
  58. package/dist/es/alerting-leader.js +177 -0
  59. package/dist/es/client.d.ts +84 -0
  60. package/dist/es/client.js +699 -0
  61. package/dist/es/connection-manager.d.ts +18 -0
  62. package/dist/es/connection-manager.js +124 -0
  63. package/dist/es/connection-transfer.d.ts +7 -0
  64. package/dist/es/connection-transfer.js +16 -0
  65. package/dist/es/http-client.d.ts +18 -0
  66. package/dist/es/http-client.js +194 -0
  67. package/dist/es/index.bundle.js +39629 -0
  68. package/dist/es/index.d.ts +10 -0
  69. package/dist/es/index.js +10 -0
  70. package/dist/es/messenger.d.ts +37 -0
  71. package/dist/es/messenger.js +62 -0
  72. package/dist/es/notifications.d.ts +94 -0
  73. package/dist/es/notifications.js +507 -0
  74. package/dist/es/ping.d.ts +21 -0
  75. package/dist/es/ping.js +68 -0
  76. package/dist/es/polyfills.d.ts +0 -0
  77. package/dist/es/polyfills.js +6 -0
  78. package/dist/es/server-monitor.d.ts +16 -0
  79. package/dist/es/server-monitor.js +41 -0
  80. package/dist/es/stanza-definitions/webrtc-signaling.d.ts +29 -0
  81. package/dist/es/stanza-definitions/webrtc-signaling.js +110 -0
  82. package/dist/es/stanza-definitions/xep0051.d.ts +19 -0
  83. package/dist/es/stanza-definitions/xep0051.js +25 -0
  84. package/dist/es/stats-formatter.d.ts +6 -0
  85. package/dist/es/stats-formatter.js +54 -0
  86. package/dist/es/types/genesys-cloud-media-session.d.ts +64 -0
  87. package/dist/es/types/genesys-cloud-media-session.js +379 -0
  88. package/dist/es/types/interfaces.d.ts +310 -0
  89. package/dist/es/types/interfaces.js +19 -0
  90. package/dist/es/types/libs.d.ts +32 -0
  91. package/dist/es/types/libs.js +1 -0
  92. package/dist/es/types/media-session.d.ts +76 -0
  93. package/dist/es/types/media-session.js +1 -0
  94. package/dist/es/types/named-agent.d.ts +13 -0
  95. package/dist/es/types/named-agent.js +1 -0
  96. package/dist/es/types/offline-error.d.ts +2 -0
  97. package/dist/es/types/offline-error.js +2 -0
  98. package/dist/es/types/sasl-error.d.ts +8 -0
  99. package/dist/es/types/sasl-error.js +9 -0
  100. package/dist/es/types/stanza-media-session.d.ts +56 -0
  101. package/dist/es/types/stanza-media-session.js +264 -0
  102. package/dist/es/types/streaming-client-extension.d.ts +14 -0
  103. package/dist/es/types/streaming-client-extension.js +1 -0
  104. package/dist/es/types/timeout-error.d.ts +3 -0
  105. package/dist/es/types/timeout-error.js +5 -0
  106. package/dist/es/types/user-cancelled-error.d.ts +2 -0
  107. package/dist/es/types/user-cancelled-error.js +2 -0
  108. package/dist/es/utils.d.ts +34 -0
  109. package/dist/es/utils.js +192 -0
  110. package/dist/es/webrtc.d.ts +126 -0
  111. package/dist/es/webrtc.js +917 -0
  112. package/dist/manifest.json +19 -0
  113. package/dist/npm/CHANGELOG.md +637 -0
  114. package/dist/npm/README.md +47 -0
  115. package/dist/npm/alerting-leader.d.ts +29 -0
  116. package/dist/npm/alerting-leader.js +163 -0
  117. package/dist/npm/client.d.ts +84 -0
  118. package/dist/npm/client.js +680 -0
  119. package/dist/npm/connection-manager.d.ts +18 -0
  120. package/dist/npm/connection-manager.js +120 -0
  121. package/dist/npm/connection-transfer.d.ts +7 -0
  122. package/dist/npm/connection-transfer.js +20 -0
  123. package/dist/npm/http-client.d.ts +18 -0
  124. package/dist/npm/http-client.js +230 -0
  125. package/dist/npm/index.d.ts +10 -0
  126. package/dist/npm/index.js +31 -0
  127. package/dist/npm/messenger.d.ts +37 -0
  128. package/dist/npm/messenger.js +54 -0
  129. package/dist/npm/module.js +10 -0
  130. package/dist/npm/notifications.d.ts +94 -0
  131. package/dist/npm/notifications.js +495 -0
  132. package/dist/npm/ping.d.ts +21 -0
  133. package/dist/npm/ping.js +60 -0
  134. package/dist/npm/polyfills.d.ts +0 -0
  135. package/dist/npm/polyfills.js +6 -0
  136. package/dist/npm/server-monitor.d.ts +16 -0
  137. package/dist/npm/server-monitor.js +44 -0
  138. package/dist/npm/stanza-definitions/webrtc-signaling.d.ts +29 -0
  139. package/dist/npm/stanza-definitions/webrtc-signaling.js +113 -0
  140. package/dist/npm/stanza-definitions/xep0051.d.ts +19 -0
  141. package/dist/npm/stanza-definitions/xep0051.js +28 -0
  142. package/dist/npm/stats-formatter.d.ts +6 -0
  143. package/dist/npm/stats-formatter.js +63 -0
  144. package/dist/npm/types/genesys-cloud-media-session.d.ts +64 -0
  145. package/dist/npm/types/genesys-cloud-media-session.js +353 -0
  146. package/dist/npm/types/interfaces.d.ts +310 -0
  147. package/dist/npm/types/interfaces.js +22 -0
  148. package/dist/npm/types/libs.d.ts +32 -0
  149. package/dist/npm/types/libs.js +1 -0
  150. package/dist/npm/types/media-session.d.ts +76 -0
  151. package/dist/npm/types/media-session.js +2 -0
  152. package/dist/npm/types/named-agent.d.ts +13 -0
  153. package/dist/npm/types/named-agent.js +2 -0
  154. package/dist/npm/types/offline-error.d.ts +2 -0
  155. package/dist/npm/types/offline-error.js +5 -0
  156. package/dist/npm/types/sasl-error.d.ts +8 -0
  157. package/dist/npm/types/sasl-error.js +12 -0
  158. package/dist/npm/types/stanza-media-session.d.ts +56 -0
  159. package/dist/npm/types/stanza-media-session.js +255 -0
  160. package/dist/npm/types/streaming-client-extension.d.ts +14 -0
  161. package/dist/npm/types/streaming-client-extension.js +2 -0
  162. package/dist/npm/types/timeout-error.d.ts +3 -0
  163. package/dist/npm/types/timeout-error.js +9 -0
  164. package/dist/npm/types/user-cancelled-error.d.ts +2 -0
  165. package/dist/npm/types/user-cancelled-error.js +5 -0
  166. package/dist/npm/utils.d.ts +34 -0
  167. package/dist/npm/utils.js +207 -0
  168. package/dist/npm/webrtc.d.ts +126 -0
  169. package/dist/npm/webrtc.js +893 -0
  170. package/dist/streaming-client.browser.js +2 -0
  171. package/dist/streaming-client.browser.js.LICENSE.txt +12 -0
  172. package/dist/v20/streaming-client.browser.js +2 -0
  173. package/dist/v20/streaming-client.browser.js.LICENSE.txt +12 -0
  174. package/dist/v20.1.0/streaming-client.browser.js +2 -0
  175. package/dist/v20.1.0/streaming-client.browser.js.LICENSE.txt +12 -0
  176. package/package.json +132 -0
@@ -0,0 +1,18 @@
1
+ import Logger from 'genesys-cloud-client-logger';
2
+ import { IClientConfig } from './types/interfaces';
3
+ import { NamedAgent } from './types/named-agent';
4
+ export declare class ConnectionManager {
5
+ private logger;
6
+ private config;
7
+ currentStanzaInstance?: NamedAgent;
8
+ constructor(logger: Logger, config: IClientConfig);
9
+ setConfig(config: IClientConfig): void;
10
+ getNewStanzaConnection(): Promise<NamedAgent>;
11
+ private handleSessionStarted;
12
+ private handleSessionSasl;
13
+ private handleSessionDisconnected;
14
+ private checkForErrorStanza;
15
+ private getStanzaOptions;
16
+ private getJwtOptions;
17
+ private getStandardOptions;
18
+ }
@@ -0,0 +1,124 @@
1
+ // so the idea here is any time stanza gets disconnected, we are effectively going to kill that instance of
2
+ // stanza and never reuse it. We may reuse the config for a given stanza client, but every connection attempt
3
+ // will merit its own brand new client. The purpose of this is because the stanza.connect() is not transactional
4
+ // and failed connection attempts trigger `disconnect` events either by timeout or some actual error. This is
5
+ // a problem because it effectively means we can get multiple disconnect events for a single connection attempt
6
+ // which makes trying to reconnect a stanza instance an tricky endeavor because we don't know if a disconnect
7
+ // event applies to a past connection attempt or if it applies to the most recent connection attempt. For this
8
+ // reason, we want a vanilla instance of stanza every time we call `connect` so wires don't get crossed.
9
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
10
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
11
+ return new (P || (P = Promise))(function (resolve, reject) {
12
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
13
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
14
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
15
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
16
+ });
17
+ };
18
+ import { createClient } from 'stanza';
19
+ import SaslError from './types/sasl-error';
20
+ import { parseJwt, timeoutPromise } from './utils';
21
+ export class ConnectionManager {
22
+ constructor(logger, config) {
23
+ this.logger = logger;
24
+ this.config = config;
25
+ }
26
+ setConfig(config) {
27
+ this.config = config;
28
+ }
29
+ getNewStanzaConnection() {
30
+ return __awaiter(this, void 0, void 0, function* () {
31
+ const options = this.getStanzaOptions();
32
+ const stanza = createClient({});
33
+ this.currentStanzaInstance = stanza;
34
+ // this is a hack because stanza messes up the auth mechanism priority.
35
+ stanza.sasl.mechanisms.find(mech => mech.name === 'ANONYMOUS').priority = 0;
36
+ stanza.sasl.mechanisms = stanza.sasl.mechanisms.sort((a, b) => b.priority - a.priority);
37
+ // we are going to give the stanza instance an id for tracking and logging purposes
38
+ stanza.id = globalThis.crypto.randomUUID();
39
+ const channelId = stanza.channelId = this.config.channelId;
40
+ let boundCheckForErrorStanza;
41
+ let boundSessionStarted;
42
+ let boundSessionSasl;
43
+ let boundSessionDisconnected;
44
+ const connectionAttemptPromise = timeoutPromise((resolve, reject) => {
45
+ boundCheckForErrorStanza = this.checkForErrorStanza.bind(this, stanza);
46
+ stanza.on('raw:incoming', boundCheckForErrorStanza);
47
+ boundSessionStarted = this.handleSessionStarted.bind(this, stanza, resolve);
48
+ stanza.on('session:started', boundSessionStarted);
49
+ boundSessionSasl = this.handleSessionSasl.bind(this, stanza, reject);
50
+ stanza.on('sasl', boundSessionSasl);
51
+ boundSessionDisconnected = this.handleSessionDisconnected.bind(this, stanza, reject);
52
+ stanza.on('disconnected', boundSessionDisconnected);
53
+ stanza.updateConfig(options);
54
+ stanza.connect();
55
+ }, 15 * 1000, 'connecting to streaming service', { channelId, stanzaInstanceId: stanza.id });
56
+ connectionAttemptPromise.catch(() => stanza.disconnect());
57
+ return connectionAttemptPromise.finally(() => {
58
+ stanza.off('raw:incoming', boundCheckForErrorStanza);
59
+ stanza.off('session:started', boundSessionStarted);
60
+ stanza.off('sasl', boundSessionSasl);
61
+ stanza.off('disconnected', boundSessionDisconnected);
62
+ });
63
+ });
64
+ }
65
+ handleSessionStarted(stanza, resolve) {
66
+ this.logger.info('new stanza instance connected', { stanzaInstanceId: stanza.id, channelId: stanza.channelId });
67
+ resolve(stanza);
68
+ }
69
+ handleSessionSasl(stanza, reject, sasl) {
70
+ if (sasl.type === 'failure') {
71
+ reject(new SaslError(sasl.condition, stanza.channelId, stanza.id));
72
+ }
73
+ }
74
+ handleSessionDisconnected(stanza, reject) {
75
+ this.logger.error('stanza disconnected', { stanzaInstanceId: stanza.id, channelId: stanza.channelId });
76
+ reject();
77
+ }
78
+ checkForErrorStanza(stanza, rawStanza) {
79
+ if (rawStanza.includes('error')) {
80
+ this.logger.error('Received a stanza during setup that tripped the error filter', { rawStanza, stanzaInstanceId: stanza.id, channelId: stanza.channelId });
81
+ }
82
+ }
83
+ getStanzaOptions() {
84
+ if (this.config.jwt) {
85
+ return this.getJwtOptions();
86
+ }
87
+ return this.getStandardOptions();
88
+ }
89
+ getJwtOptions() {
90
+ const config = this.config;
91
+ const jwt = parseJwt(config.jwt);
92
+ let jidDomain;
93
+ const jwtJid = jwt.data.jid;
94
+ try {
95
+ jidDomain = jwtJid.split('@')[1].replace('conference.', '');
96
+ }
97
+ catch (e) {
98
+ throw new Error('failed to parse jid');
99
+ }
100
+ const wsHost = config.host.replace(/\/$/, '');
101
+ return {
102
+ resource: config.jidResource,
103
+ transports: {
104
+ websocket: `${wsHost}/stream/jwt/${config.jwt}`
105
+ },
106
+ server: jidDomain
107
+ };
108
+ }
109
+ getStandardOptions() {
110
+ const config = this.config;
111
+ const wsHost = config.host.replace(/\/$/, '');
112
+ return {
113
+ jid: config.jid,
114
+ resource: config.jidResource,
115
+ credentials: {
116
+ username: config.jid,
117
+ password: `authKey:${config.authToken}`
118
+ },
119
+ transports: {
120
+ websocket: `${wsHost}/stream/channels/${config.channelId}`
121
+ }
122
+ };
123
+ }
124
+ }
@@ -0,0 +1,7 @@
1
+ import { Client } from './client';
2
+ import { NamedAgent } from './types/named-agent';
3
+ export declare class ConnectionTransfer {
4
+ private client;
5
+ private stanzaInstance;
6
+ constructor(client: Client, stanzaInstance: NamedAgent);
7
+ }
@@ -0,0 +1,16 @@
1
+ import { connectionTransfer } from './stanza-definitions/xep0051';
2
+ export class ConnectionTransfer {
3
+ constructor(client, stanzaInstance) {
4
+ this.client = client;
5
+ this.stanzaInstance = stanzaInstance;
6
+ stanzaInstance.stanzas.define(connectionTransfer);
7
+ // Hawk maps `v2.system.socket_closing` to XEP-0051 Connection Transfer
8
+ // The docs says we have up to one minute to disconnect and connect a new WebSocket, so we should be proactive in reconnecting.
9
+ stanzaInstance.on('iq:set:connectionTransfer', (iq) => {
10
+ client.logger.warn('connection transfer (socket_closing) event received', { stanzaInstanceId: stanzaInstance.id, channelId: stanzaInstance.channelId });
11
+ void client.disconnect();
12
+ client.emit('disconnected', { reconnecting: true });
13
+ void client.connect({ keepTryingOnFailure: true });
14
+ });
15
+ }
16
+ }
@@ -0,0 +1,18 @@
1
+ import { RetryPromise } from './utils';
2
+ import { RequestApiOptions, ISuperagentNetworkError, ISuperagentResponseError, INetworkError, IResponseError, ICustomHeader, IHttpClientOptions } from './types/interfaces';
3
+ export declare class HttpClient {
4
+ customHeaders?: ICustomHeader;
5
+ private _httpRetryingRequests;
6
+ static retryStatusCodes: Set<number>;
7
+ static retryNetworkErrorCodes: Set<string>;
8
+ constructor(httpClientOptions?: IHttpClientOptions);
9
+ requestApiWithRetry<T = any>(path: string, opts: RequestApiOptions, retryInterval?: number): RetryPromise<T>;
10
+ requestApi(path: string, opts: RequestApiOptions): Promise<any>;
11
+ private handleResponse;
12
+ stopAllRetries(): void;
13
+ cancelRetryRequest(retryId: string): true;
14
+ formatRequestError(error: Error | ISuperagentNetworkError | ISuperagentResponseError): Error | INetworkError | IResponseError;
15
+ private isSuperagentNetworkError;
16
+ private isSuperagentResponseError;
17
+ private _buildUri;
18
+ }
@@ -0,0 +1,194 @@
1
+ import axios, { AxiosError } from 'axios';
2
+ import { retryPromise } from './utils';
3
+ const correlationIdHeaderName = 'inin-correlation-id';
4
+ export class HttpClient {
5
+ constructor(httpClientOptions) {
6
+ this._httpRetryingRequests = new Map();
7
+ this.customHeaders = httpClientOptions === null || httpClientOptions === void 0 ? void 0 : httpClientOptions.customHeaders;
8
+ }
9
+ requestApiWithRetry(path, opts, retryInterval) {
10
+ const maxRetries = opts.maxAttempts || 10;
11
+ let retryCount = 0;
12
+ const retry = retryPromise(this.requestApi.bind(this, path, opts), (error) => {
13
+ var _a, _b, _c;
14
+ retryCount++;
15
+ if (retryCount >= maxRetries) {
16
+ (opts.logger || console).info('Max retries reached, will not retry.', { maxRetries, retryCount, path });
17
+ return false;
18
+ }
19
+ let retryValue = false;
20
+ if (error === null || error === void 0 ? void 0 : error.response) {
21
+ retryValue = HttpClient.retryStatusCodes.has(error.response.status || 0);
22
+ // This *should* be an axios error according to typings, but it appears this could be an AxiosError *or* and XmlHttpRequest
23
+ // we'll check both to be safe
24
+ const retryAfter = ((_a = error.response.headers) === null || _a === void 0 ? void 0 : _a['retry-after']) || ((_c = (_b = error.response).getResponseHeader) === null || _c === void 0 ? void 0 : _c.call(_b, 'retry-after'));
25
+ if (retryAfter) {
26
+ (opts.logger || console).debug('retry-after header found on response. setting retry delay', { retryAfter });
27
+ // retry after comes in seconds, we need to return milliseconds
28
+ retryValue = parseInt(retryAfter, 10) * 1000;
29
+ }
30
+ }
31
+ // Check network error codes independently - error may have both response (with status 0) and a network error code.
32
+ if ((error === null || error === void 0 ? void 0 : error.code) && retryValue === false) {
33
+ // Retry on network error codes - request didn't make it to the server or no response was received
34
+ if (HttpClient.retryNetworkErrorCodes.has(error.code)) {
35
+ (opts.logger || console).debug('Retry network error code found, will retry.', { code: error.code });
36
+ retryValue = true;
37
+ }
38
+ }
39
+ return retryValue;
40
+ }, retryInterval, opts.logger);
41
+ this._httpRetryingRequests.set(retry._id, retry);
42
+ retry.promise.then(() => this.cancelRetryRequest(retry._id), () => this.cancelRetryRequest(retry._id));
43
+ return retry;
44
+ }
45
+ requestApi(path, opts) {
46
+ const logger = opts.logger || console;
47
+ const start = new Date().getTime();
48
+ opts.customHeaders = Object.assign(Object.assign({}, (this.customHeaders || {})), (opts.customHeaders || {}));
49
+ const url = this._buildUri(opts.host, path, opts.version);
50
+ const headers = Object.assign({ 'content-type': opts.contentType || 'application/json' }, opts.customHeaders);
51
+ const params = {
52
+ method: opts.method,
53
+ url,
54
+ data: opts.data,
55
+ responseType: opts.responseType,
56
+ timeout: opts.requestTimeout || 30000,
57
+ headers,
58
+ signal: opts.signal
59
+ };
60
+ // default to include auth header
61
+ if (!opts.noAuthHeader) {
62
+ params.headers.Authorization = `Bearer ${opts.authToken}`;
63
+ }
64
+ const boundHandler = this.handleResponse.bind(this, logger, start, params);
65
+ return axios(params)
66
+ .then(boundHandler, boundHandler);
67
+ }
68
+ handleResponse(logger, start, params, res) {
69
+ var _a, _b, _c;
70
+ const now = new Date().getTime();
71
+ const elapsed = (now - start) + 'ms';
72
+ if (res instanceof AxiosError) {
73
+ // sanitize the auth token
74
+ if ((_b = (_a = res.config) === null || _a === void 0 ? void 0 : _a.headers) === null || _b === void 0 ? void 0 : _b.Authorization) {
75
+ res.config.headers.Authorization = 'redacted';
76
+ }
77
+ // handles request timeout
78
+ if (res.code === 'ECONNABORTED') {
79
+ logger.debug(`request error: ${params.url}`, {
80
+ message: res.message,
81
+ now,
82
+ elapsed
83
+ }, { skipServer: true });
84
+ return Promise.reject(res);
85
+ }
86
+ /* istanbul ignore next */
87
+ const response = res.response || {};
88
+ const status = response.status;
89
+ const correlationId = response.headers && response.headers[correlationIdHeaderName];
90
+ const body = response.data;
91
+ const error = Object.assign(Object.assign({}, res), { text: (_c = response.request) === null || _c === void 0 ? void 0 : _c.response });
92
+ logger.debug(`request error: ${params.url}`, {
93
+ message: res.message,
94
+ now,
95
+ elapsed,
96
+ status,
97
+ correlationId,
98
+ body
99
+ }, { skipServer: true });
100
+ return Promise.reject(error);
101
+ }
102
+ const status = res.status;
103
+ const correlationId = res.headers[correlationIdHeaderName];
104
+ const body = JSON.stringify(res.data);
105
+ logger.debug(`response: ${params.method.toUpperCase()} ${params.url}`, {
106
+ now,
107
+ status,
108
+ elapsed,
109
+ correlationId,
110
+ body
111
+ }, { skipServer: true });
112
+ return Promise.resolve(res);
113
+ }
114
+ stopAllRetries() {
115
+ Array.from(this._httpRetryingRequests.keys())
116
+ .forEach(key => this.cancelRetryRequest(key));
117
+ }
118
+ cancelRetryRequest(retryId) {
119
+ const value = this._httpRetryingRequests.get(retryId);
120
+ if (value) {
121
+ /* if the promise has already completed, this will do nothing. Still need to remove it from the map */
122
+ value.cancel(new Error('Retry request cancelled'));
123
+ this._httpRetryingRequests.delete(retryId);
124
+ }
125
+ return true;
126
+ }
127
+ formatRequestError(error) {
128
+ /* if network error */
129
+ if (this.isSuperagentNetworkError(error)) {
130
+ return {
131
+ status: error.status,
132
+ method: error.method,
133
+ url: error.url,
134
+ crossDomain: error.crossDomain,
135
+ message: error.message,
136
+ name: error.name,
137
+ stack: error.stack
138
+ };
139
+ }
140
+ /* if superagent response error */
141
+ if (this.isSuperagentResponseError(error)) {
142
+ const res = error.response;
143
+ return {
144
+ status: error.status,
145
+ correlationId: res.headers['inin-correlation-id'],
146
+ // Potentially could contain PII
147
+ // responseBody: res.text,
148
+ // requestBody: res.req._data,
149
+ // url: res.error.url,
150
+ message: 'Error making HTTP request',
151
+ method: res.req.method,
152
+ name: res.error.name,
153
+ stack: res.error.stack
154
+ };
155
+ }
156
+ /* if we don't have a superagent error */
157
+ return error;
158
+ }
159
+ isSuperagentNetworkError(error) {
160
+ return (error &&
161
+ // these properties may have the value of `undefined` but they will still be set
162
+ error.hasOwnProperty('status') &&
163
+ error.hasOwnProperty('method') &&
164
+ error.hasOwnProperty('url'));
165
+ }
166
+ isSuperagentResponseError(error) {
167
+ return !!(error &&
168
+ error.response &&
169
+ error.response.body &&
170
+ error.response.req);
171
+ }
172
+ _buildUri(host, path, version = 'v2') {
173
+ path = path.replace(/^\/+|\/+$/g, ''); // trim leading/trailing /
174
+ if (host.indexOf('http') === 0) {
175
+ return `${host}/api/${version}/${path}`;
176
+ }
177
+ return `https://api.${host}/api/${version}/${path}`;
178
+ }
179
+ }
180
+ HttpClient.retryStatusCodes = new Set([
181
+ 408,
182
+ 413,
183
+ 429,
184
+ 500,
185
+ 502,
186
+ 503,
187
+ 504
188
+ ]);
189
+ HttpClient.retryNetworkErrorCodes = new Set([
190
+ 'ECONNABORTED',
191
+ 'ECONNRESET',
192
+ 'ERR_NETWORK',
193
+ 'ETIMEDOUT'
194
+ ]);