plugin-file-preview-auth 1.3.5 → 1.3.8

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 (105) hide show
  1. package/client-v2.d.ts +2 -0
  2. package/client-v2.js +1 -0
  3. package/dist/client/713.79a55458f5b67f39.js +30 -0
  4. package/dist/client/823.8b0ab22c181d4523.js +10 -0
  5. package/dist/client/828.ae8e47a2e7a3bc9e.js +49 -0
  6. package/dist/client/892.a568eb42fd6f0047.js +10 -0
  7. package/dist/client/index.js +1 -1
  8. package/dist/client-v2/index.js +10 -0
  9. package/dist/externalVersion.js +8 -7
  10. package/dist/node_modules/@aws-sdk/client-s3/dist-cjs/index.js +3086 -3725
  11. package/dist/node_modules/@aws-sdk/client-s3/node_modules/.bin/fxparser +16 -0
  12. package/dist/node_modules/@aws-sdk/client-s3/node_modules/.bin/fxparser.cmd +17 -0
  13. package/dist/node_modules/@aws-sdk/client-s3/node_modules/.bin/fxparser.ps1 +28 -0
  14. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@aws-sdk/signature-v4-multi-region/dist-cjs/index.js +110 -0
  15. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@aws-sdk/signature-v4-multi-region/dist-es/SignatureV4MultiRegion.js +66 -0
  16. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@aws-sdk/signature-v4-multi-region/dist-es/index.js +2 -0
  17. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@aws-sdk/signature-v4-multi-region/dist-es/signature-v4-crt-container.js +3 -0
  18. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@aws-sdk/signature-v4-multi-region/dist-types/SignatureV4MultiRegion.d.ts +30 -0
  19. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@aws-sdk/signature-v4-multi-region/dist-types/index.d.ts +5 -0
  20. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@aws-sdk/signature-v4-multi-region/dist-types/signature-v4-crt-container.d.ts +28 -0
  21. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@aws-sdk/signature-v4-multi-region/dist-types/ts3.4/SignatureV4MultiRegion.d.ts +40 -0
  22. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@aws-sdk/signature-v4-multi-region/dist-types/ts3.4/index.d.ts +2 -0
  23. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@aws-sdk/signature-v4-multi-region/dist-types/ts3.4/signature-v4-crt-container.d.ts +20 -0
  24. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@aws-sdk/signature-v4-multi-region/package.json +57 -0
  25. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-cjs/AdaptiveRetryStrategy.js +1 -0
  26. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-cjs/ConfiguredRetryStrategy.js +1 -0
  27. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-cjs/DefaultRateLimiter.js +1 -0
  28. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-cjs/StandardRetryStrategy.js +1 -0
  29. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-cjs/config.js +1 -0
  30. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-cjs/constants.js +1 -0
  31. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-cjs/defaultRetryBackoffStrategy.js +1 -0
  32. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-cjs/defaultRetryToken.js +1 -0
  33. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-cjs/index.js +358 -0
  34. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-cjs/types.js +1 -0
  35. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-es/AdaptiveRetryStrategy.js +24 -0
  36. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-es/ConfiguredRetryStrategy.js +18 -0
  37. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-es/DefaultRateLimiter.js +100 -0
  38. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-es/StandardRetryStrategy.js +65 -0
  39. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-es/config.js +7 -0
  40. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-es/constants.js +9 -0
  41. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-es/defaultRetryBackoffStrategy.js +14 -0
  42. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-es/defaultRetryToken.js +11 -0
  43. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-es/index.js +7 -0
  44. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-es/types.js +1 -0
  45. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-types/AdaptiveRetryStrategy.d.ts +33 -0
  46. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-types/ConfiguredRetryStrategy.d.ts +32 -0
  47. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-types/DefaultRateLimiter.d.ts +49 -0
  48. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-types/StandardRetryStrategy.d.ts +26 -0
  49. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-types/config.d.ts +20 -0
  50. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-types/constants.d.ts +59 -0
  51. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-types/defaultRetryBackoffStrategy.d.ts +5 -0
  52. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-types/defaultRetryToken.d.ts +9 -0
  53. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-types/index.d.ts +7 -0
  54. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-types/ts3.4/AdaptiveRetryStrategy.d.ts +33 -0
  55. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-types/ts3.4/ConfiguredRetryStrategy.d.ts +32 -0
  56. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-types/ts3.4/DefaultRateLimiter.d.ts +49 -0
  57. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-types/ts3.4/StandardRetryStrategy.d.ts +26 -0
  58. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-types/ts3.4/config.d.ts +20 -0
  59. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-types/ts3.4/constants.d.ts +59 -0
  60. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-types/ts3.4/defaultRetryBackoffStrategy.d.ts +5 -0
  61. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-types/ts3.4/defaultRetryToken.d.ts +9 -0
  62. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-types/ts3.4/index.d.ts +7 -0
  63. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-types/ts3.4/types.d.ts +19 -0
  64. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/dist-types/types.d.ts +19 -0
  65. package/dist/node_modules/@aws-sdk/client-s3/node_modules/@smithy/util-retry/package.json +68 -0
  66. package/dist/node_modules/@aws-sdk/client-s3/package.json +1 -1
  67. package/dist/node_modules/xlsx/package.json +1 -1
  68. package/dist/server/ocr/tesseract-runner.js +3 -1
  69. package/dist/server/plugin.js +22 -4
  70. package/package.json +57 -45
  71. package/src/client/AIFilePreviewAction.tsx +282 -0
  72. package/src/client/__tests__/ocr-utils.test.ts +85 -0
  73. package/src/client/client.d.ts +258 -0
  74. package/src/client/index.tsx +1807 -0
  75. package/src/client/locale.ts +21 -0
  76. package/src/client-v2/index.tsx +1 -0
  77. package/src/client-v2/plugin.tsx +7 -0
  78. package/{dist/index.d.ts → src/index.ts} +11 -10
  79. package/src/locale/en-US.json +14 -0
  80. package/src/locale/vi-VN.json +14 -0
  81. package/src/locale/zh-CN.json +14 -0
  82. package/src/server/__tests__/smoke.test.ts +17 -0
  83. package/src/server/collections/attachment-ocr-results.ts +40 -0
  84. package/{dist/server/collections/file-preview-auth.d.ts → src/server/collections/file-preview-auth.ts} +15 -14
  85. package/src/server/excel-parser-handler.ts +128 -0
  86. package/{dist/server/index.d.ts → src/server/index.ts} +10 -9
  87. package/src/server/migrations/20260528000000-move-ocr-fields-out-of-attachments.ts +39 -0
  88. package/src/server/ocr/tesseract-runner.ts +389 -0
  89. package/src/server/ocr/tesseract-worker.ts +235 -0
  90. package/src/server/plugin.ts +1470 -0
  91. package/dist/client/166.17caa11c2ba40313.js +0 -10
  92. package/dist/client/351.0f0ce45c92425c8f.js +0 -10
  93. package/dist/client/374.96762d13b15e7467.js +0 -30
  94. package/dist/client/514.2a8b6aa0d2fcd4b2.js +0 -49
  95. package/dist/client/AIFilePreviewAction.d.ts +0 -42
  96. package/dist/client/index.d.ts +0 -14
  97. package/dist/client/locale.d.ts +0 -10
  98. package/dist/node_modules/xlsx/node_modules/.bin/crc32 +0 -15
  99. package/dist/node_modules/xlsx/node_modules/.bin/crc32.cmd +0 -7
  100. package/dist/server/collections/attachment-ocr-results.d.ts +0 -2
  101. package/dist/server/excel-parser-handler.d.ts +0 -60
  102. package/dist/server/migrations/20260528000000-move-ocr-fields-out-of-attachments.d.ts +0 -5
  103. package/dist/server/ocr/tesseract-runner.d.ts +0 -34
  104. package/dist/server/ocr/tesseract-worker.d.ts +0 -27
  105. package/dist/server/plugin.d.ts +0 -54
@@ -0,0 +1,358 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ AdaptiveRetryStrategy: () => AdaptiveRetryStrategy,
24
+ ConfiguredRetryStrategy: () => ConfiguredRetryStrategy,
25
+ DEFAULT_MAX_ATTEMPTS: () => DEFAULT_MAX_ATTEMPTS,
26
+ DEFAULT_RETRY_DELAY_BASE: () => DEFAULT_RETRY_DELAY_BASE,
27
+ DEFAULT_RETRY_MODE: () => DEFAULT_RETRY_MODE,
28
+ DefaultRateLimiter: () => DefaultRateLimiter,
29
+ INITIAL_RETRY_TOKENS: () => INITIAL_RETRY_TOKENS,
30
+ INVOCATION_ID_HEADER: () => INVOCATION_ID_HEADER,
31
+ MAXIMUM_RETRY_DELAY: () => MAXIMUM_RETRY_DELAY,
32
+ NO_RETRY_INCREMENT: () => NO_RETRY_INCREMENT,
33
+ REQUEST_HEADER: () => REQUEST_HEADER,
34
+ RETRY_COST: () => RETRY_COST,
35
+ RETRY_MODES: () => RETRY_MODES,
36
+ StandardRetryStrategy: () => StandardRetryStrategy,
37
+ THROTTLING_RETRY_DELAY_BASE: () => THROTTLING_RETRY_DELAY_BASE,
38
+ TIMEOUT_RETRY_COST: () => TIMEOUT_RETRY_COST
39
+ });
40
+ module.exports = __toCommonJS(src_exports);
41
+
42
+ // src/config.ts
43
+ var RETRY_MODES = /* @__PURE__ */ ((RETRY_MODES2) => {
44
+ RETRY_MODES2["STANDARD"] = "standard";
45
+ RETRY_MODES2["ADAPTIVE"] = "adaptive";
46
+ return RETRY_MODES2;
47
+ })(RETRY_MODES || {});
48
+ var DEFAULT_MAX_ATTEMPTS = 3;
49
+ var DEFAULT_RETRY_MODE = "standard" /* STANDARD */;
50
+
51
+ // src/DefaultRateLimiter.ts
52
+ var import_service_error_classification = require("@smithy/service-error-classification");
53
+ var DefaultRateLimiter = class _DefaultRateLimiter {
54
+ constructor(options) {
55
+ // Pre-set state variables
56
+ this.currentCapacity = 0;
57
+ this.enabled = false;
58
+ this.lastMaxRate = 0;
59
+ this.measuredTxRate = 0;
60
+ this.requestCount = 0;
61
+ this.lastTimestamp = 0;
62
+ this.timeWindow = 0;
63
+ this.beta = options?.beta ?? 0.7;
64
+ this.minCapacity = options?.minCapacity ?? 1;
65
+ this.minFillRate = options?.minFillRate ?? 0.5;
66
+ this.scaleConstant = options?.scaleConstant ?? 0.4;
67
+ this.smooth = options?.smooth ?? 0.8;
68
+ const currentTimeInSeconds = this.getCurrentTimeInSeconds();
69
+ this.lastThrottleTime = currentTimeInSeconds;
70
+ this.lastTxRateBucket = Math.floor(this.getCurrentTimeInSeconds());
71
+ this.fillRate = this.minFillRate;
72
+ this.maxCapacity = this.minCapacity;
73
+ }
74
+ static {
75
+ __name(this, "DefaultRateLimiter");
76
+ }
77
+ static {
78
+ /**
79
+ * Only used in testing.
80
+ */
81
+ this.setTimeoutFn = setTimeout;
82
+ }
83
+ getCurrentTimeInSeconds() {
84
+ return Date.now() / 1e3;
85
+ }
86
+ async getSendToken() {
87
+ return this.acquireTokenBucket(1);
88
+ }
89
+ async acquireTokenBucket(amount) {
90
+ if (!this.enabled) {
91
+ return;
92
+ }
93
+ this.refillTokenBucket();
94
+ if (amount > this.currentCapacity) {
95
+ const delay = (amount - this.currentCapacity) / this.fillRate * 1e3;
96
+ await new Promise((resolve) => _DefaultRateLimiter.setTimeoutFn(resolve, delay));
97
+ }
98
+ this.currentCapacity = this.currentCapacity - amount;
99
+ }
100
+ refillTokenBucket() {
101
+ const timestamp = this.getCurrentTimeInSeconds();
102
+ if (!this.lastTimestamp) {
103
+ this.lastTimestamp = timestamp;
104
+ return;
105
+ }
106
+ const fillAmount = (timestamp - this.lastTimestamp) * this.fillRate;
107
+ this.currentCapacity = Math.min(this.maxCapacity, this.currentCapacity + fillAmount);
108
+ this.lastTimestamp = timestamp;
109
+ }
110
+ updateClientSendingRate(response) {
111
+ let calculatedRate;
112
+ this.updateMeasuredRate();
113
+ if ((0, import_service_error_classification.isThrottlingError)(response)) {
114
+ const rateToUse = !this.enabled ? this.measuredTxRate : Math.min(this.measuredTxRate, this.fillRate);
115
+ this.lastMaxRate = rateToUse;
116
+ this.calculateTimeWindow();
117
+ this.lastThrottleTime = this.getCurrentTimeInSeconds();
118
+ calculatedRate = this.cubicThrottle(rateToUse);
119
+ this.enableTokenBucket();
120
+ } else {
121
+ this.calculateTimeWindow();
122
+ calculatedRate = this.cubicSuccess(this.getCurrentTimeInSeconds());
123
+ }
124
+ const newRate = Math.min(calculatedRate, 2 * this.measuredTxRate);
125
+ this.updateTokenBucketRate(newRate);
126
+ }
127
+ calculateTimeWindow() {
128
+ this.timeWindow = this.getPrecise(Math.pow(this.lastMaxRate * (1 - this.beta) / this.scaleConstant, 1 / 3));
129
+ }
130
+ cubicThrottle(rateToUse) {
131
+ return this.getPrecise(rateToUse * this.beta);
132
+ }
133
+ cubicSuccess(timestamp) {
134
+ return this.getPrecise(
135
+ this.scaleConstant * Math.pow(timestamp - this.lastThrottleTime - this.timeWindow, 3) + this.lastMaxRate
136
+ );
137
+ }
138
+ enableTokenBucket() {
139
+ this.enabled = true;
140
+ }
141
+ updateTokenBucketRate(newRate) {
142
+ this.refillTokenBucket();
143
+ this.fillRate = Math.max(newRate, this.minFillRate);
144
+ this.maxCapacity = Math.max(newRate, this.minCapacity);
145
+ this.currentCapacity = Math.min(this.currentCapacity, this.maxCapacity);
146
+ }
147
+ updateMeasuredRate() {
148
+ const t = this.getCurrentTimeInSeconds();
149
+ const timeBucket = Math.floor(t * 2) / 2;
150
+ this.requestCount++;
151
+ if (timeBucket > this.lastTxRateBucket) {
152
+ const currentRate = this.requestCount / (timeBucket - this.lastTxRateBucket);
153
+ this.measuredTxRate = this.getPrecise(currentRate * this.smooth + this.measuredTxRate * (1 - this.smooth));
154
+ this.requestCount = 0;
155
+ this.lastTxRateBucket = timeBucket;
156
+ }
157
+ }
158
+ getPrecise(num) {
159
+ return parseFloat(num.toFixed(8));
160
+ }
161
+ };
162
+
163
+ // src/constants.ts
164
+ var DEFAULT_RETRY_DELAY_BASE = 100;
165
+ var MAXIMUM_RETRY_DELAY = 20 * 1e3;
166
+ var THROTTLING_RETRY_DELAY_BASE = 500;
167
+ var INITIAL_RETRY_TOKENS = 500;
168
+ var RETRY_COST = 5;
169
+ var TIMEOUT_RETRY_COST = 10;
170
+ var NO_RETRY_INCREMENT = 1;
171
+ var INVOCATION_ID_HEADER = "amz-sdk-invocation-id";
172
+ var REQUEST_HEADER = "amz-sdk-request";
173
+
174
+ // src/defaultRetryBackoffStrategy.ts
175
+ var getDefaultRetryBackoffStrategy = /* @__PURE__ */ __name(() => {
176
+ let delayBase = DEFAULT_RETRY_DELAY_BASE;
177
+ const computeNextBackoffDelay = /* @__PURE__ */ __name((attempts) => {
178
+ return Math.floor(Math.min(MAXIMUM_RETRY_DELAY, Math.random() * 2 ** attempts * delayBase));
179
+ }, "computeNextBackoffDelay");
180
+ const setDelayBase = /* @__PURE__ */ __name((delay) => {
181
+ delayBase = delay;
182
+ }, "setDelayBase");
183
+ return {
184
+ computeNextBackoffDelay,
185
+ setDelayBase
186
+ };
187
+ }, "getDefaultRetryBackoffStrategy");
188
+
189
+ // src/defaultRetryToken.ts
190
+ var createDefaultRetryToken = /* @__PURE__ */ __name(({
191
+ retryDelay,
192
+ retryCount,
193
+ retryCost
194
+ }) => {
195
+ const getRetryCount = /* @__PURE__ */ __name(() => retryCount, "getRetryCount");
196
+ const getRetryDelay = /* @__PURE__ */ __name(() => Math.min(MAXIMUM_RETRY_DELAY, retryDelay), "getRetryDelay");
197
+ const getRetryCost = /* @__PURE__ */ __name(() => retryCost, "getRetryCost");
198
+ return {
199
+ getRetryCount,
200
+ getRetryDelay,
201
+ getRetryCost
202
+ };
203
+ }, "createDefaultRetryToken");
204
+
205
+ // src/StandardRetryStrategy.ts
206
+ var StandardRetryStrategy = class {
207
+ constructor(maxAttempts) {
208
+ this.maxAttempts = maxAttempts;
209
+ this.mode = "standard" /* STANDARD */;
210
+ this.capacity = INITIAL_RETRY_TOKENS;
211
+ this.retryBackoffStrategy = getDefaultRetryBackoffStrategy();
212
+ this.maxAttemptsProvider = typeof maxAttempts === "function" ? maxAttempts : async () => maxAttempts;
213
+ }
214
+ static {
215
+ __name(this, "StandardRetryStrategy");
216
+ }
217
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
218
+ async acquireInitialRetryToken(retryTokenScope) {
219
+ return createDefaultRetryToken({
220
+ retryDelay: DEFAULT_RETRY_DELAY_BASE,
221
+ retryCount: 0
222
+ });
223
+ }
224
+ async refreshRetryTokenForRetry(token, errorInfo) {
225
+ const maxAttempts = await this.getMaxAttempts();
226
+ if (this.shouldRetry(token, errorInfo, maxAttempts)) {
227
+ const errorType = errorInfo.errorType;
228
+ this.retryBackoffStrategy.setDelayBase(
229
+ errorType === "THROTTLING" ? THROTTLING_RETRY_DELAY_BASE : DEFAULT_RETRY_DELAY_BASE
230
+ );
231
+ const delayFromErrorType = this.retryBackoffStrategy.computeNextBackoffDelay(token.getRetryCount());
232
+ const retryDelay = errorInfo.retryAfterHint ? Math.max(errorInfo.retryAfterHint.getTime() - Date.now() || 0, delayFromErrorType) : delayFromErrorType;
233
+ const capacityCost = this.getCapacityCost(errorType);
234
+ this.capacity -= capacityCost;
235
+ return createDefaultRetryToken({
236
+ retryDelay,
237
+ retryCount: token.getRetryCount() + 1,
238
+ retryCost: capacityCost
239
+ });
240
+ }
241
+ throw new Error("No retry token available");
242
+ }
243
+ recordSuccess(token) {
244
+ this.capacity = Math.max(INITIAL_RETRY_TOKENS, this.capacity + (token.getRetryCost() ?? NO_RETRY_INCREMENT));
245
+ }
246
+ /**
247
+ * @returns the current available retry capacity.
248
+ *
249
+ * This number decreases when retries are executed and refills when requests or retries succeed.
250
+ */
251
+ getCapacity() {
252
+ return this.capacity;
253
+ }
254
+ async getMaxAttempts() {
255
+ try {
256
+ return await this.maxAttemptsProvider();
257
+ } catch (error) {
258
+ console.warn(`Max attempts provider could not resolve. Using default of ${DEFAULT_MAX_ATTEMPTS}`);
259
+ return DEFAULT_MAX_ATTEMPTS;
260
+ }
261
+ }
262
+ shouldRetry(tokenToRenew, errorInfo, maxAttempts) {
263
+ const attempts = tokenToRenew.getRetryCount() + 1;
264
+ return attempts < maxAttempts && this.capacity >= this.getCapacityCost(errorInfo.errorType) && this.isRetryableError(errorInfo.errorType);
265
+ }
266
+ getCapacityCost(errorType) {
267
+ return errorType === "TRANSIENT" ? TIMEOUT_RETRY_COST : RETRY_COST;
268
+ }
269
+ isRetryableError(errorType) {
270
+ return errorType === "THROTTLING" || errorType === "TRANSIENT";
271
+ }
272
+ };
273
+
274
+ // src/AdaptiveRetryStrategy.ts
275
+ var AdaptiveRetryStrategy = class {
276
+ constructor(maxAttemptsProvider, options) {
277
+ this.maxAttemptsProvider = maxAttemptsProvider;
278
+ this.mode = "adaptive" /* ADAPTIVE */;
279
+ const { rateLimiter } = options ?? {};
280
+ this.rateLimiter = rateLimiter ?? new DefaultRateLimiter();
281
+ this.standardRetryStrategy = new StandardRetryStrategy(maxAttemptsProvider);
282
+ }
283
+ static {
284
+ __name(this, "AdaptiveRetryStrategy");
285
+ }
286
+ async acquireInitialRetryToken(retryTokenScope) {
287
+ await this.rateLimiter.getSendToken();
288
+ return this.standardRetryStrategy.acquireInitialRetryToken(retryTokenScope);
289
+ }
290
+ async refreshRetryTokenForRetry(tokenToRenew, errorInfo) {
291
+ this.rateLimiter.updateClientSendingRate(errorInfo);
292
+ return this.standardRetryStrategy.refreshRetryTokenForRetry(tokenToRenew, errorInfo);
293
+ }
294
+ recordSuccess(token) {
295
+ this.rateLimiter.updateClientSendingRate({});
296
+ this.standardRetryStrategy.recordSuccess(token);
297
+ }
298
+ };
299
+
300
+ // src/ConfiguredRetryStrategy.ts
301
+ var ConfiguredRetryStrategy = class extends StandardRetryStrategy {
302
+ static {
303
+ __name(this, "ConfiguredRetryStrategy");
304
+ }
305
+ /**
306
+ * @param maxAttempts - the maximum number of retry attempts allowed.
307
+ * e.g., if set to 3, then 4 total requests are possible.
308
+ * @param computeNextBackoffDelay - a millisecond delay for each retry or a function that takes the retry attempt
309
+ * and returns the delay.
310
+ *
311
+ * @example exponential backoff.
312
+ * ```js
313
+ * new Client({
314
+ * retryStrategy: new ConfiguredRetryStrategy(3, (attempt) => attempt ** 2)
315
+ * });
316
+ * ```
317
+ * @example constant delay.
318
+ * ```js
319
+ * new Client({
320
+ * retryStrategy: new ConfiguredRetryStrategy(3, 2000)
321
+ * });
322
+ * ```
323
+ */
324
+ constructor(maxAttempts, computeNextBackoffDelay = DEFAULT_RETRY_DELAY_BASE) {
325
+ super(typeof maxAttempts === "function" ? maxAttempts : async () => maxAttempts);
326
+ if (typeof computeNextBackoffDelay === "number") {
327
+ this.computeNextBackoffDelay = () => computeNextBackoffDelay;
328
+ } else {
329
+ this.computeNextBackoffDelay = computeNextBackoffDelay;
330
+ }
331
+ }
332
+ async refreshRetryTokenForRetry(tokenToRenew, errorInfo) {
333
+ const token = await super.refreshRetryTokenForRetry(tokenToRenew, errorInfo);
334
+ token.getRetryDelay = () => this.computeNextBackoffDelay(token.getRetryCount());
335
+ return token;
336
+ }
337
+ };
338
+ // Annotate the CommonJS export names for ESM import in node:
339
+
340
+ 0 && (module.exports = {
341
+ AdaptiveRetryStrategy,
342
+ ConfiguredRetryStrategy,
343
+ DefaultRateLimiter,
344
+ StandardRetryStrategy,
345
+ RETRY_MODES,
346
+ DEFAULT_MAX_ATTEMPTS,
347
+ DEFAULT_RETRY_MODE,
348
+ DEFAULT_RETRY_DELAY_BASE,
349
+ MAXIMUM_RETRY_DELAY,
350
+ THROTTLING_RETRY_DELAY_BASE,
351
+ INITIAL_RETRY_TOKENS,
352
+ RETRY_COST,
353
+ TIMEOUT_RETRY_COST,
354
+ NO_RETRY_INCREMENT,
355
+ INVOCATION_ID_HEADER,
356
+ REQUEST_HEADER
357
+ });
358
+
@@ -0,0 +1 @@
1
+ module.exports = require("./index.js");
@@ -0,0 +1,24 @@
1
+ import { RETRY_MODES } from "./config";
2
+ import { DefaultRateLimiter } from "./DefaultRateLimiter";
3
+ import { StandardRetryStrategy } from "./StandardRetryStrategy";
4
+ export class AdaptiveRetryStrategy {
5
+ constructor(maxAttemptsProvider, options) {
6
+ this.maxAttemptsProvider = maxAttemptsProvider;
7
+ this.mode = RETRY_MODES.ADAPTIVE;
8
+ const { rateLimiter } = options ?? {};
9
+ this.rateLimiter = rateLimiter ?? new DefaultRateLimiter();
10
+ this.standardRetryStrategy = new StandardRetryStrategy(maxAttemptsProvider);
11
+ }
12
+ async acquireInitialRetryToken(retryTokenScope) {
13
+ await this.rateLimiter.getSendToken();
14
+ return this.standardRetryStrategy.acquireInitialRetryToken(retryTokenScope);
15
+ }
16
+ async refreshRetryTokenForRetry(tokenToRenew, errorInfo) {
17
+ this.rateLimiter.updateClientSendingRate(errorInfo);
18
+ return this.standardRetryStrategy.refreshRetryTokenForRetry(tokenToRenew, errorInfo);
19
+ }
20
+ recordSuccess(token) {
21
+ this.rateLimiter.updateClientSendingRate({});
22
+ this.standardRetryStrategy.recordSuccess(token);
23
+ }
24
+ }
@@ -0,0 +1,18 @@
1
+ import { DEFAULT_RETRY_DELAY_BASE } from "./constants";
2
+ import { StandardRetryStrategy } from "./StandardRetryStrategy";
3
+ export class ConfiguredRetryStrategy extends StandardRetryStrategy {
4
+ constructor(maxAttempts, computeNextBackoffDelay = DEFAULT_RETRY_DELAY_BASE) {
5
+ super(typeof maxAttempts === "function" ? maxAttempts : async () => maxAttempts);
6
+ if (typeof computeNextBackoffDelay === "number") {
7
+ this.computeNextBackoffDelay = () => computeNextBackoffDelay;
8
+ }
9
+ else {
10
+ this.computeNextBackoffDelay = computeNextBackoffDelay;
11
+ }
12
+ }
13
+ async refreshRetryTokenForRetry(tokenToRenew, errorInfo) {
14
+ const token = await super.refreshRetryTokenForRetry(tokenToRenew, errorInfo);
15
+ token.getRetryDelay = () => this.computeNextBackoffDelay(token.getRetryCount());
16
+ return token;
17
+ }
18
+ }
@@ -0,0 +1,100 @@
1
+ import { isThrottlingError } from "@smithy/service-error-classification";
2
+ export class DefaultRateLimiter {
3
+ constructor(options) {
4
+ this.currentCapacity = 0;
5
+ this.enabled = false;
6
+ this.lastMaxRate = 0;
7
+ this.measuredTxRate = 0;
8
+ this.requestCount = 0;
9
+ this.lastTimestamp = 0;
10
+ this.timeWindow = 0;
11
+ this.beta = options?.beta ?? 0.7;
12
+ this.minCapacity = options?.minCapacity ?? 1;
13
+ this.minFillRate = options?.minFillRate ?? 0.5;
14
+ this.scaleConstant = options?.scaleConstant ?? 0.4;
15
+ this.smooth = options?.smooth ?? 0.8;
16
+ const currentTimeInSeconds = this.getCurrentTimeInSeconds();
17
+ this.lastThrottleTime = currentTimeInSeconds;
18
+ this.lastTxRateBucket = Math.floor(this.getCurrentTimeInSeconds());
19
+ this.fillRate = this.minFillRate;
20
+ this.maxCapacity = this.minCapacity;
21
+ }
22
+ getCurrentTimeInSeconds() {
23
+ return Date.now() / 1000;
24
+ }
25
+ async getSendToken() {
26
+ return this.acquireTokenBucket(1);
27
+ }
28
+ async acquireTokenBucket(amount) {
29
+ if (!this.enabled) {
30
+ return;
31
+ }
32
+ this.refillTokenBucket();
33
+ if (amount > this.currentCapacity) {
34
+ const delay = ((amount - this.currentCapacity) / this.fillRate) * 1000;
35
+ await new Promise((resolve) => DefaultRateLimiter.setTimeoutFn(resolve, delay));
36
+ }
37
+ this.currentCapacity = this.currentCapacity - amount;
38
+ }
39
+ refillTokenBucket() {
40
+ const timestamp = this.getCurrentTimeInSeconds();
41
+ if (!this.lastTimestamp) {
42
+ this.lastTimestamp = timestamp;
43
+ return;
44
+ }
45
+ const fillAmount = (timestamp - this.lastTimestamp) * this.fillRate;
46
+ this.currentCapacity = Math.min(this.maxCapacity, this.currentCapacity + fillAmount);
47
+ this.lastTimestamp = timestamp;
48
+ }
49
+ updateClientSendingRate(response) {
50
+ let calculatedRate;
51
+ this.updateMeasuredRate();
52
+ if (isThrottlingError(response)) {
53
+ const rateToUse = !this.enabled ? this.measuredTxRate : Math.min(this.measuredTxRate, this.fillRate);
54
+ this.lastMaxRate = rateToUse;
55
+ this.calculateTimeWindow();
56
+ this.lastThrottleTime = this.getCurrentTimeInSeconds();
57
+ calculatedRate = this.cubicThrottle(rateToUse);
58
+ this.enableTokenBucket();
59
+ }
60
+ else {
61
+ this.calculateTimeWindow();
62
+ calculatedRate = this.cubicSuccess(this.getCurrentTimeInSeconds());
63
+ }
64
+ const newRate = Math.min(calculatedRate, 2 * this.measuredTxRate);
65
+ this.updateTokenBucketRate(newRate);
66
+ }
67
+ calculateTimeWindow() {
68
+ this.timeWindow = this.getPrecise(Math.pow((this.lastMaxRate * (1 - this.beta)) / this.scaleConstant, 1 / 3));
69
+ }
70
+ cubicThrottle(rateToUse) {
71
+ return this.getPrecise(rateToUse * this.beta);
72
+ }
73
+ cubicSuccess(timestamp) {
74
+ return this.getPrecise(this.scaleConstant * Math.pow(timestamp - this.lastThrottleTime - this.timeWindow, 3) + this.lastMaxRate);
75
+ }
76
+ enableTokenBucket() {
77
+ this.enabled = true;
78
+ }
79
+ updateTokenBucketRate(newRate) {
80
+ this.refillTokenBucket();
81
+ this.fillRate = Math.max(newRate, this.minFillRate);
82
+ this.maxCapacity = Math.max(newRate, this.minCapacity);
83
+ this.currentCapacity = Math.min(this.currentCapacity, this.maxCapacity);
84
+ }
85
+ updateMeasuredRate() {
86
+ const t = this.getCurrentTimeInSeconds();
87
+ const timeBucket = Math.floor(t * 2) / 2;
88
+ this.requestCount++;
89
+ if (timeBucket > this.lastTxRateBucket) {
90
+ const currentRate = this.requestCount / (timeBucket - this.lastTxRateBucket);
91
+ this.measuredTxRate = this.getPrecise(currentRate * this.smooth + this.measuredTxRate * (1 - this.smooth));
92
+ this.requestCount = 0;
93
+ this.lastTxRateBucket = timeBucket;
94
+ }
95
+ }
96
+ getPrecise(num) {
97
+ return parseFloat(num.toFixed(8));
98
+ }
99
+ }
100
+ DefaultRateLimiter.setTimeoutFn = setTimeout;
@@ -0,0 +1,65 @@
1
+ import { DEFAULT_MAX_ATTEMPTS, RETRY_MODES } from "./config";
2
+ import { DEFAULT_RETRY_DELAY_BASE, INITIAL_RETRY_TOKENS, NO_RETRY_INCREMENT, RETRY_COST, THROTTLING_RETRY_DELAY_BASE, TIMEOUT_RETRY_COST, } from "./constants";
3
+ import { getDefaultRetryBackoffStrategy } from "./defaultRetryBackoffStrategy";
4
+ import { createDefaultRetryToken } from "./defaultRetryToken";
5
+ export class StandardRetryStrategy {
6
+ constructor(maxAttempts) {
7
+ this.maxAttempts = maxAttempts;
8
+ this.mode = RETRY_MODES.STANDARD;
9
+ this.capacity = INITIAL_RETRY_TOKENS;
10
+ this.retryBackoffStrategy = getDefaultRetryBackoffStrategy();
11
+ this.maxAttemptsProvider = typeof maxAttempts === "function" ? maxAttempts : async () => maxAttempts;
12
+ }
13
+ async acquireInitialRetryToken(retryTokenScope) {
14
+ return createDefaultRetryToken({
15
+ retryDelay: DEFAULT_RETRY_DELAY_BASE,
16
+ retryCount: 0,
17
+ });
18
+ }
19
+ async refreshRetryTokenForRetry(token, errorInfo) {
20
+ const maxAttempts = await this.getMaxAttempts();
21
+ if (this.shouldRetry(token, errorInfo, maxAttempts)) {
22
+ const errorType = errorInfo.errorType;
23
+ this.retryBackoffStrategy.setDelayBase(errorType === "THROTTLING" ? THROTTLING_RETRY_DELAY_BASE : DEFAULT_RETRY_DELAY_BASE);
24
+ const delayFromErrorType = this.retryBackoffStrategy.computeNextBackoffDelay(token.getRetryCount());
25
+ const retryDelay = errorInfo.retryAfterHint
26
+ ? Math.max(errorInfo.retryAfterHint.getTime() - Date.now() || 0, delayFromErrorType)
27
+ : delayFromErrorType;
28
+ const capacityCost = this.getCapacityCost(errorType);
29
+ this.capacity -= capacityCost;
30
+ return createDefaultRetryToken({
31
+ retryDelay,
32
+ retryCount: token.getRetryCount() + 1,
33
+ retryCost: capacityCost,
34
+ });
35
+ }
36
+ throw new Error("No retry token available");
37
+ }
38
+ recordSuccess(token) {
39
+ this.capacity = Math.max(INITIAL_RETRY_TOKENS, this.capacity + (token.getRetryCost() ?? NO_RETRY_INCREMENT));
40
+ }
41
+ getCapacity() {
42
+ return this.capacity;
43
+ }
44
+ async getMaxAttempts() {
45
+ try {
46
+ return await this.maxAttemptsProvider();
47
+ }
48
+ catch (error) {
49
+ console.warn(`Max attempts provider could not resolve. Using default of ${DEFAULT_MAX_ATTEMPTS}`);
50
+ return DEFAULT_MAX_ATTEMPTS;
51
+ }
52
+ }
53
+ shouldRetry(tokenToRenew, errorInfo, maxAttempts) {
54
+ const attempts = tokenToRenew.getRetryCount() + 1;
55
+ return (attempts < maxAttempts &&
56
+ this.capacity >= this.getCapacityCost(errorInfo.errorType) &&
57
+ this.isRetryableError(errorInfo.errorType));
58
+ }
59
+ getCapacityCost(errorType) {
60
+ return errorType === "TRANSIENT" ? TIMEOUT_RETRY_COST : RETRY_COST;
61
+ }
62
+ isRetryableError(errorType) {
63
+ return errorType === "THROTTLING" || errorType === "TRANSIENT";
64
+ }
65
+ }
@@ -0,0 +1,7 @@
1
+ export var RETRY_MODES;
2
+ (function (RETRY_MODES) {
3
+ RETRY_MODES["STANDARD"] = "standard";
4
+ RETRY_MODES["ADAPTIVE"] = "adaptive";
5
+ })(RETRY_MODES || (RETRY_MODES = {}));
6
+ export const DEFAULT_MAX_ATTEMPTS = 3;
7
+ export const DEFAULT_RETRY_MODE = RETRY_MODES.STANDARD;
@@ -0,0 +1,9 @@
1
+ export const DEFAULT_RETRY_DELAY_BASE = 100;
2
+ export const MAXIMUM_RETRY_DELAY = 20 * 1000;
3
+ export const THROTTLING_RETRY_DELAY_BASE = 500;
4
+ export const INITIAL_RETRY_TOKENS = 500;
5
+ export const RETRY_COST = 5;
6
+ export const TIMEOUT_RETRY_COST = 10;
7
+ export const NO_RETRY_INCREMENT = 1;
8
+ export const INVOCATION_ID_HEADER = "amz-sdk-invocation-id";
9
+ export const REQUEST_HEADER = "amz-sdk-request";
@@ -0,0 +1,14 @@
1
+ import { DEFAULT_RETRY_DELAY_BASE, MAXIMUM_RETRY_DELAY } from "./constants";
2
+ export const getDefaultRetryBackoffStrategy = () => {
3
+ let delayBase = DEFAULT_RETRY_DELAY_BASE;
4
+ const computeNextBackoffDelay = (attempts) => {
5
+ return Math.floor(Math.min(MAXIMUM_RETRY_DELAY, Math.random() * 2 ** attempts * delayBase));
6
+ };
7
+ const setDelayBase = (delay) => {
8
+ delayBase = delay;
9
+ };
10
+ return {
11
+ computeNextBackoffDelay,
12
+ setDelayBase,
13
+ };
14
+ };
@@ -0,0 +1,11 @@
1
+ import { MAXIMUM_RETRY_DELAY } from "./constants";
2
+ export const createDefaultRetryToken = ({ retryDelay, retryCount, retryCost, }) => {
3
+ const getRetryCount = () => retryCount;
4
+ const getRetryDelay = () => Math.min(MAXIMUM_RETRY_DELAY, retryDelay);
5
+ const getRetryCost = () => retryCost;
6
+ return {
7
+ getRetryCount,
8
+ getRetryDelay,
9
+ getRetryCost,
10
+ };
11
+ };
@@ -0,0 +1,7 @@
1
+ export * from "./AdaptiveRetryStrategy";
2
+ export * from "./ConfiguredRetryStrategy";
3
+ export * from "./DefaultRateLimiter";
4
+ export * from "./StandardRetryStrategy";
5
+ export * from "./config";
6
+ export * from "./constants";
7
+ export * from "./types";
@@ -0,0 +1,33 @@
1
+ import { Provider, RetryErrorInfo, RetryStrategyV2, RetryToken, StandardRetryToken } from "@smithy/types";
2
+ import { RateLimiter } from "./types";
3
+ /**
4
+ * @public
5
+ *
6
+ * Strategy options to be passed to AdaptiveRetryStrategy
7
+ */
8
+ export interface AdaptiveRetryStrategyOptions {
9
+ rateLimiter?: RateLimiter;
10
+ }
11
+ /**
12
+ * @public
13
+ *
14
+ * The AdaptiveRetryStrategy is a retry strategy for executing against a very
15
+ * resource constrained set of resources. Care should be taken when using this
16
+ * retry strategy. By default, it uses a dynamic backoff delay based on load
17
+ * currently perceived against the downstream resource and performs circuit
18
+ * breaking to disable retries in the event of high downstream failures using
19
+ * the DefaultRateLimiter.
20
+ *
21
+ * @see {@link StandardRetryStrategy}
22
+ * @see {@link DefaultRateLimiter }
23
+ */
24
+ export declare class AdaptiveRetryStrategy implements RetryStrategyV2 {
25
+ private readonly maxAttemptsProvider;
26
+ private rateLimiter;
27
+ private standardRetryStrategy;
28
+ readonly mode: string;
29
+ constructor(maxAttemptsProvider: Provider<number>, options?: AdaptiveRetryStrategyOptions);
30
+ acquireInitialRetryToken(retryTokenScope: string): Promise<RetryToken>;
31
+ refreshRetryTokenForRetry(tokenToRenew: StandardRetryToken, errorInfo: RetryErrorInfo): Promise<RetryToken>;
32
+ recordSuccess(token: StandardRetryToken): void;
33
+ }