|
| 1 | +"use strict"; |
| 2 | +var __importDefault = (this && this.__importDefault) || function (mod) { |
| 3 | + return (mod && mod.__esModule) ? mod : { "default": mod }; |
| 4 | +}; |
| 5 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 6 | +exports.RetryPolicy = exports.Endpoint = void 0; |
| 7 | +const categories_1 = __importDefault(require("../constants/categories")); |
| 8 | +// -------------------------------------------------------- |
| 9 | +// ------------------------ Types ------------------------- |
| 10 | +// -------------------------------------------------------- |
| 11 | +// region Types |
| 12 | +/** |
| 13 | + * List of known endpoint groups (by context). |
| 14 | + */ |
| 15 | +var Endpoint; |
| 16 | +(function (Endpoint) { |
| 17 | + /** |
| 18 | + * Unknown endpoint. |
| 19 | + * |
| 20 | + * @internal |
| 21 | + */ |
| 22 | + Endpoint["Unknown"] = "UnknownEndpoint"; |
| 23 | + /** |
| 24 | + * The endpoints to send messages. |
| 25 | + */ |
| 26 | + Endpoint["MessageSend"] = "MessageSendEndpoint"; |
| 27 | + /** |
| 28 | + * The endpoint for real-time update retrieval. |
| 29 | + */ |
| 30 | + Endpoint["Subscribe"] = "SubscribeEndpoint"; |
| 31 | + /** |
| 32 | + * The endpoint to access and manage `user_id` presence and fetch channel presence information. |
| 33 | + */ |
| 34 | + Endpoint["Presence"] = "PresenceEndpoint"; |
| 35 | + /** |
| 36 | + * The endpoint to access and manage files in channel-specific storage. |
| 37 | + */ |
| 38 | + Endpoint["Files"] = "FilesEndpoint"; |
| 39 | + /** |
| 40 | + * The endpoint to access and manage messages for a specific channel(s) in the persistent storage. |
| 41 | + */ |
| 42 | + Endpoint["MessageStorage"] = "MessageStorageEndpoint"; |
| 43 | + /** |
| 44 | + * The endpoint to access and manage channel groups. |
| 45 | + */ |
| 46 | + Endpoint["ChannelGroups"] = "ChannelGroupsEndpoint"; |
| 47 | + /** |
| 48 | + * The endpoint to access and manage device registration for channel push notifications. |
| 49 | + */ |
| 50 | + Endpoint["DevicePushNotifications"] = "DevicePushNotificationsEndpoint"; |
| 51 | + /** |
| 52 | + * The endpoint to access and manage App Context objects. |
| 53 | + */ |
| 54 | + Endpoint["AppContext"] = "AppContextEndpoint"; |
| 55 | + /** |
| 56 | + * The endpoint to access and manage reactions for a specific message. |
| 57 | + */ |
| 58 | + Endpoint["MessageReactions"] = "MessageReactionsEndpoint"; |
| 59 | +})(Endpoint || (exports.Endpoint = Endpoint = {})); |
| 60 | +// endregion |
| 61 | +/** |
| 62 | + * Failed request retry policy. |
| 63 | + */ |
| 64 | +class RetryPolicy { |
| 65 | + static LinearRetryPolicy(configuration) { |
| 66 | + var _a; |
| 67 | + return { |
| 68 | + delay: configuration.delay, |
| 69 | + maximumRetry: configuration.maximumRetry, |
| 70 | + excluded: (_a = configuration.excluded) !== null && _a !== void 0 ? _a : [], |
| 71 | + shouldRetry(request, response, error, attempt) { |
| 72 | + return isRetriableRequest(request, response, error, attempt !== null && attempt !== void 0 ? attempt : 0, this.maximumRetry, this.excluded); |
| 73 | + }, |
| 74 | + getDelay(_, response) { |
| 75 | + let delay = -1; |
| 76 | + if (response && response.headers['retry-after'] !== undefined) |
| 77 | + delay = parseInt(response.headers['retry-after'], 10); |
| 78 | + if (delay === -1) |
| 79 | + delay = this.delay; |
| 80 | + return (delay + Math.random()) * 1000; |
| 81 | + }, |
| 82 | + validate() { |
| 83 | + if (this.delay < 2) |
| 84 | + throw new Error('Delay can not be set less than 2 seconds for retry'); |
| 85 | + if (this.maximumRetry > 10) |
| 86 | + throw new Error('Maximum retry for linear retry policy can not be more than 10'); |
| 87 | + }, |
| 88 | + }; |
| 89 | + } |
| 90 | + static ExponentialRetryPolicy(configuration) { |
| 91 | + var _a; |
| 92 | + return { |
| 93 | + minimumDelay: configuration.minimumDelay, |
| 94 | + maximumDelay: configuration.maximumDelay, |
| 95 | + maximumRetry: configuration.maximumRetry, |
| 96 | + excluded: (_a = configuration.excluded) !== null && _a !== void 0 ? _a : [], |
| 97 | + shouldRetry(request, response, error, attempt) { |
| 98 | + return isRetriableRequest(request, response, error, attempt !== null && attempt !== void 0 ? attempt : 0, this.maximumRetry, this.excluded); |
| 99 | + }, |
| 100 | + getDelay(attempt, response) { |
| 101 | + let delay = -1; |
| 102 | + if (response && response.headers['retry-after'] !== undefined) |
| 103 | + delay = parseInt(response.headers['retry-after'], 10); |
| 104 | + if (delay === -1) |
| 105 | + delay = Math.min(Math.pow(2, attempt), this.maximumDelay); |
| 106 | + return (delay + Math.random()) * 1000; |
| 107 | + }, |
| 108 | + validate() { |
| 109 | + if (this.minimumDelay < 2) |
| 110 | + throw new Error('Minimum delay can not be set less than 2 seconds for retry'); |
| 111 | + else if (this.maximumDelay > 150) |
| 112 | + throw new Error('Maximum delay can not be set more than 150 seconds for' + ' retry'); |
| 113 | + else if (this.maximumRetry > 6) |
| 114 | + throw new Error('Maximum retry for exponential retry policy can not be more than 6'); |
| 115 | + }, |
| 116 | + }; |
| 117 | + } |
| 118 | +} |
| 119 | +exports.RetryPolicy = RetryPolicy; |
| 120 | +/** |
| 121 | + * Check whether request can be retried or not. |
| 122 | + * |
| 123 | + * @param req - Request for which retry ability is checked. |
| 124 | + * @param res - Service response which should be taken into consideration. |
| 125 | + * @param errorCategory - Request processing error category. |
| 126 | + * @param retryAttempt - Current retry attempt. |
| 127 | + * @param maximumRetry - Maximum retry attempts count according to the retry policy. |
| 128 | + * @param excluded - List of endpoints for which retry policy won't be applied. |
| 129 | + * |
| 130 | + * @return `true` if request can be retried. |
| 131 | + * |
| 132 | + * @internal |
| 133 | + */ |
| 134 | +const isRetriableRequest = (req, res, errorCategory, retryAttempt, maximumRetry, excluded) => { |
| 135 | + if (errorCategory && errorCategory === categories_1.default.PNCancelledCategory) |
| 136 | + return false; |
| 137 | + else if (isExcludedRequest(req, excluded)) |
| 138 | + return false; |
| 139 | + else if (retryAttempt > maximumRetry) |
| 140 | + return false; |
| 141 | + return res ? res.status === 429 || res.status >= 500 : true; |
| 142 | +}; |
| 143 | +/** |
| 144 | + * Check whether the provided request is in the list of endpoints for which retry is not allowed or not. |
| 145 | + * |
| 146 | + * @param req - Request which will be tested. |
| 147 | + * @param excluded - List of excluded endpoints configured for retry policy. |
| 148 | + * |
| 149 | + * @returns `true` if request has been excluded and shouldn't be retried. |
| 150 | + * |
| 151 | + * @internal |
| 152 | + */ |
| 153 | +const isExcludedRequest = (req, excluded) => excluded && excluded.length > 0 ? excluded.includes(endpointFromRequest(req)) : false; |
| 154 | +/** |
| 155 | + * Identify API group from transport request. |
| 156 | + * |
| 157 | + * @param req - Request for which `path` will be analyzed to identify REST API group. |
| 158 | + * |
| 159 | + * @returns Endpoint group to which request belongs. |
| 160 | + * |
| 161 | + * @internal |
| 162 | + */ |
| 163 | +const endpointFromRequest = (req) => { |
| 164 | + let endpoint = Endpoint.Unknown; |
| 165 | + if (req.path.startsWith('/v2/subscribe')) |
| 166 | + endpoint = Endpoint.Subscribe; |
| 167 | + else if (req.path.startsWith('/publish/') || req.path.startsWith('/signal/')) |
| 168 | + endpoint = Endpoint.MessageSend; |
| 169 | + else if (req.path.startsWith('/v2/presence')) |
| 170 | + endpoint = Endpoint.Presence; |
| 171 | + else if (req.path.startsWith('/v2/history') || req.path.startsWith('/v3/history')) |
| 172 | + endpoint = Endpoint.MessageStorage; |
| 173 | + else if (req.path.startsWith('/v1/message-actions/')) |
| 174 | + endpoint = Endpoint.MessageReactions; |
| 175 | + else if (req.path.startsWith('/v1/channel-registration/')) |
| 176 | + endpoint = Endpoint.ChannelGroups; |
| 177 | + else if (req.path.startsWith('/v2/objects/')) |
| 178 | + endpoint = Endpoint.ChannelGroups; |
| 179 | + else if (req.path.startsWith('/v1/push/') || req.path.startsWith('/v2/push/')) |
| 180 | + endpoint = Endpoint.DevicePushNotifications; |
| 181 | + else if (req.path.startsWith('/v1/files/')) |
| 182 | + endpoint = Endpoint.Files; |
| 183 | + return endpoint; |
| 184 | +}; |
0 commit comments