@juspay/neurolink 9.1.0 → 9.2.0
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/CHANGELOG.md +12 -0
- package/README.md +54 -7
- package/dist/agent/directTools.d.ts +3 -3
- package/dist/cli/commands/config.d.ts +6 -6
- package/dist/image-gen/ImageGenService.d.ts +143 -0
- package/dist/image-gen/ImageGenService.js +345 -0
- package/dist/image-gen/imageGenTools.d.ts +126 -0
- package/dist/image-gen/imageGenTools.js +304 -0
- package/dist/image-gen/index.d.ts +46 -0
- package/dist/image-gen/index.js +48 -0
- package/dist/image-gen/types.d.ts +237 -0
- package/dist/image-gen/types.js +24 -0
- package/dist/lib/agent/directTools.d.ts +3 -3
- package/dist/lib/image-gen/ImageGenService.d.ts +143 -0
- package/dist/lib/image-gen/ImageGenService.js +346 -0
- package/dist/lib/image-gen/imageGenTools.d.ts +126 -0
- package/dist/lib/image-gen/imageGenTools.js +305 -0
- package/dist/lib/image-gen/index.d.ts +46 -0
- package/dist/lib/image-gen/index.js +49 -0
- package/dist/lib/image-gen/types.d.ts +237 -0
- package/dist/lib/image-gen/types.js +25 -0
- package/dist/lib/processors/base/BaseFileProcessor.d.ts +273 -0
- package/dist/lib/processors/base/BaseFileProcessor.js +614 -0
- package/dist/lib/processors/base/index.d.ts +14 -0
- package/dist/lib/processors/base/index.js +20 -0
- package/dist/lib/processors/base/types.d.ts +593 -0
- package/dist/lib/processors/base/types.js +77 -0
- package/dist/lib/processors/cli/fileProcessorCli.d.ts +163 -0
- package/dist/lib/processors/cli/fileProcessorCli.js +389 -0
- package/dist/lib/processors/cli/index.d.ts +37 -0
- package/dist/lib/processors/cli/index.js +50 -0
- package/dist/lib/processors/code/ConfigProcessor.d.ts +171 -0
- package/dist/lib/processors/code/ConfigProcessor.js +401 -0
- package/dist/lib/processors/code/SourceCodeProcessor.d.ts +174 -0
- package/dist/lib/processors/code/SourceCodeProcessor.js +305 -0
- package/dist/lib/processors/code/index.d.ts +44 -0
- package/dist/lib/processors/code/index.js +61 -0
- package/dist/lib/processors/config/fileTypes.d.ts +283 -0
- package/dist/lib/processors/config/fileTypes.js +521 -0
- package/dist/lib/processors/config/index.d.ts +32 -0
- package/dist/lib/processors/config/index.js +93 -0
- package/dist/lib/processors/config/languageMap.d.ts +66 -0
- package/dist/lib/processors/config/languageMap.js +411 -0
- package/dist/lib/processors/config/mimeTypes.d.ts +376 -0
- package/dist/lib/processors/config/mimeTypes.js +339 -0
- package/dist/lib/processors/config/sizeLimits.d.ts +194 -0
- package/dist/lib/processors/config/sizeLimits.js +247 -0
- package/dist/lib/processors/data/JsonProcessor.d.ts +122 -0
- package/dist/lib/processors/data/JsonProcessor.js +204 -0
- package/dist/lib/processors/data/XmlProcessor.d.ts +160 -0
- package/dist/lib/processors/data/XmlProcessor.js +284 -0
- package/dist/lib/processors/data/YamlProcessor.d.ts +163 -0
- package/dist/lib/processors/data/YamlProcessor.js +295 -0
- package/dist/lib/processors/data/index.d.ts +49 -0
- package/dist/lib/processors/data/index.js +77 -0
- package/dist/lib/processors/document/ExcelProcessor.d.ts +238 -0
- package/dist/lib/processors/document/ExcelProcessor.js +520 -0
- package/dist/lib/processors/document/OpenDocumentProcessor.d.ts +69 -0
- package/dist/lib/processors/document/OpenDocumentProcessor.js +211 -0
- package/dist/lib/processors/document/RtfProcessor.d.ts +152 -0
- package/dist/lib/processors/document/RtfProcessor.js +362 -0
- package/dist/lib/processors/document/WordProcessor.d.ts +168 -0
- package/dist/lib/processors/document/WordProcessor.js +354 -0
- package/dist/lib/processors/document/index.d.ts +54 -0
- package/dist/lib/processors/document/index.js +91 -0
- package/dist/lib/processors/errors/FileErrorCode.d.ts +98 -0
- package/dist/lib/processors/errors/FileErrorCode.js +256 -0
- package/dist/lib/processors/errors/errorHelpers.d.ts +151 -0
- package/dist/lib/processors/errors/errorHelpers.js +379 -0
- package/dist/lib/processors/errors/errorSerializer.d.ts +139 -0
- package/dist/lib/processors/errors/errorSerializer.js +508 -0
- package/dist/lib/processors/errors/index.d.ts +46 -0
- package/dist/lib/processors/errors/index.js +50 -0
- package/dist/lib/processors/index.d.ts +76 -0
- package/dist/lib/processors/index.js +113 -0
- package/dist/lib/processors/integration/FileProcessorIntegration.d.ts +244 -0
- package/dist/lib/processors/integration/FileProcessorIntegration.js +273 -0
- package/dist/lib/processors/integration/index.d.ts +42 -0
- package/dist/lib/processors/integration/index.js +45 -0
- package/dist/lib/processors/markup/HtmlProcessor.d.ts +169 -0
- package/dist/lib/processors/markup/HtmlProcessor.js +250 -0
- package/dist/lib/processors/markup/MarkdownProcessor.d.ts +165 -0
- package/dist/lib/processors/markup/MarkdownProcessor.js +245 -0
- package/dist/lib/processors/markup/SvgProcessor.d.ts +156 -0
- package/dist/lib/processors/markup/SvgProcessor.js +241 -0
- package/dist/lib/processors/markup/TextProcessor.d.ts +135 -0
- package/dist/lib/processors/markup/TextProcessor.js +189 -0
- package/dist/lib/processors/markup/index.d.ts +66 -0
- package/dist/lib/processors/markup/index.js +103 -0
- package/dist/lib/processors/registry/ProcessorRegistry.d.ts +334 -0
- package/dist/lib/processors/registry/ProcessorRegistry.js +609 -0
- package/dist/lib/processors/registry/index.d.ts +12 -0
- package/dist/lib/processors/registry/index.js +17 -0
- package/dist/lib/processors/registry/types.d.ts +53 -0
- package/dist/lib/processors/registry/types.js +11 -0
- package/dist/lib/providers/sagemaker/language-model.d.ts +2 -2
- package/dist/lib/server/utils/validation.d.ts +6 -6
- package/dist/lib/types/fileTypes.d.ts +51 -1
- package/dist/lib/types/index.d.ts +25 -24
- package/dist/lib/types/index.js +21 -20
- package/dist/lib/types/modelTypes.d.ts +18 -18
- package/dist/lib/types/pptTypes.d.ts +14 -2
- package/dist/lib/types/pptTypes.js +16 -0
- package/dist/lib/utils/async/delay.d.ts +40 -0
- package/dist/lib/utils/async/delay.js +43 -0
- package/dist/lib/utils/async/index.d.ts +23 -0
- package/dist/lib/utils/async/index.js +24 -0
- package/dist/lib/utils/async/retry.d.ts +141 -0
- package/dist/lib/utils/async/retry.js +172 -0
- package/dist/lib/utils/async/withTimeout.d.ts +73 -0
- package/dist/lib/utils/async/withTimeout.js +97 -0
- package/dist/lib/utils/csvProcessor.js +442 -0
- package/dist/lib/utils/fileDetector.d.ts +7 -1
- package/dist/lib/utils/fileDetector.js +91 -18
- package/dist/lib/utils/json/extract.d.ts +103 -0
- package/dist/lib/utils/json/extract.js +249 -0
- package/dist/lib/utils/json/index.d.ts +36 -0
- package/dist/lib/utils/json/index.js +37 -0
- package/dist/lib/utils/json/safeParse.d.ts +137 -0
- package/dist/lib/utils/json/safeParse.js +191 -0
- package/dist/lib/utils/messageBuilder.d.ts +2 -2
- package/dist/lib/utils/messageBuilder.js +15 -7
- package/dist/lib/utils/sanitizers/filename.d.ts +137 -0
- package/dist/lib/utils/sanitizers/filename.js +366 -0
- package/dist/lib/utils/sanitizers/html.d.ts +170 -0
- package/dist/lib/utils/sanitizers/html.js +326 -0
- package/dist/lib/utils/sanitizers/index.d.ts +26 -0
- package/dist/lib/utils/sanitizers/index.js +30 -0
- package/dist/lib/utils/sanitizers/svg.d.ts +81 -0
- package/dist/lib/utils/sanitizers/svg.js +483 -0
- package/dist/processors/base/BaseFileProcessor.d.ts +273 -0
- package/dist/processors/base/BaseFileProcessor.js +613 -0
- package/dist/processors/base/index.d.ts +14 -0
- package/dist/processors/base/index.js +19 -0
- package/dist/processors/base/types.d.ts +593 -0
- package/dist/processors/base/types.js +76 -0
- package/dist/processors/cli/fileProcessorCli.d.ts +163 -0
- package/dist/processors/cli/fileProcessorCli.js +388 -0
- package/dist/processors/cli/index.d.ts +37 -0
- package/dist/processors/cli/index.js +49 -0
- package/dist/processors/code/ConfigProcessor.d.ts +171 -0
- package/dist/processors/code/ConfigProcessor.js +400 -0
- package/dist/processors/code/SourceCodeProcessor.d.ts +174 -0
- package/dist/processors/code/SourceCodeProcessor.js +304 -0
- package/dist/processors/code/index.d.ts +44 -0
- package/dist/processors/code/index.js +60 -0
- package/dist/processors/config/fileTypes.d.ts +283 -0
- package/dist/processors/config/fileTypes.js +520 -0
- package/dist/processors/config/index.d.ts +32 -0
- package/dist/processors/config/index.js +92 -0
- package/dist/processors/config/languageMap.d.ts +66 -0
- package/dist/processors/config/languageMap.js +410 -0
- package/dist/processors/config/mimeTypes.d.ts +376 -0
- package/dist/processors/config/mimeTypes.js +338 -0
- package/dist/processors/config/sizeLimits.d.ts +194 -0
- package/dist/processors/config/sizeLimits.js +246 -0
- package/dist/processors/data/JsonProcessor.d.ts +122 -0
- package/dist/processors/data/JsonProcessor.js +203 -0
- package/dist/processors/data/XmlProcessor.d.ts +160 -0
- package/dist/processors/data/XmlProcessor.js +283 -0
- package/dist/processors/data/YamlProcessor.d.ts +163 -0
- package/dist/processors/data/YamlProcessor.js +294 -0
- package/dist/processors/data/index.d.ts +49 -0
- package/dist/processors/data/index.js +76 -0
- package/dist/processors/document/ExcelProcessor.d.ts +238 -0
- package/dist/processors/document/ExcelProcessor.js +519 -0
- package/dist/processors/document/OpenDocumentProcessor.d.ts +69 -0
- package/dist/processors/document/OpenDocumentProcessor.js +210 -0
- package/dist/processors/document/RtfProcessor.d.ts +152 -0
- package/dist/processors/document/RtfProcessor.js +361 -0
- package/dist/processors/document/WordProcessor.d.ts +168 -0
- package/dist/processors/document/WordProcessor.js +353 -0
- package/dist/processors/document/index.d.ts +54 -0
- package/dist/processors/document/index.js +90 -0
- package/dist/processors/errors/FileErrorCode.d.ts +98 -0
- package/dist/processors/errors/FileErrorCode.js +255 -0
- package/dist/processors/errors/errorHelpers.d.ts +151 -0
- package/dist/processors/errors/errorHelpers.js +378 -0
- package/dist/processors/errors/errorSerializer.d.ts +139 -0
- package/dist/processors/errors/errorSerializer.js +507 -0
- package/dist/processors/errors/index.d.ts +46 -0
- package/dist/processors/errors/index.js +49 -0
- package/dist/processors/index.d.ts +76 -0
- package/dist/processors/index.js +112 -0
- package/dist/processors/integration/FileProcessorIntegration.d.ts +244 -0
- package/dist/processors/integration/FileProcessorIntegration.js +272 -0
- package/dist/processors/integration/index.d.ts +42 -0
- package/dist/processors/integration/index.js +44 -0
- package/dist/processors/markup/HtmlProcessor.d.ts +169 -0
- package/dist/processors/markup/HtmlProcessor.js +249 -0
- package/dist/processors/markup/MarkdownProcessor.d.ts +165 -0
- package/dist/processors/markup/MarkdownProcessor.js +244 -0
- package/dist/processors/markup/SvgProcessor.d.ts +156 -0
- package/dist/processors/markup/SvgProcessor.js +240 -0
- package/dist/processors/markup/TextProcessor.d.ts +135 -0
- package/dist/processors/markup/TextProcessor.js +188 -0
- package/dist/processors/markup/index.d.ts +66 -0
- package/dist/processors/markup/index.js +102 -0
- package/dist/processors/registry/ProcessorRegistry.d.ts +334 -0
- package/dist/processors/registry/ProcessorRegistry.js +608 -0
- package/dist/processors/registry/index.d.ts +12 -0
- package/dist/processors/registry/index.js +16 -0
- package/dist/processors/registry/types.d.ts +53 -0
- package/dist/processors/registry/types.js +10 -0
- package/dist/server/utils/validation.d.ts +6 -6
- package/dist/types/fileTypes.d.ts +51 -1
- package/dist/types/index.d.ts +25 -24
- package/dist/types/index.js +21 -20
- package/dist/types/modelTypes.d.ts +10 -10
- package/dist/types/pptTypes.d.ts +14 -2
- package/dist/types/pptTypes.js +16 -0
- package/dist/utils/async/delay.d.ts +40 -0
- package/dist/utils/async/delay.js +42 -0
- package/dist/utils/async/index.d.ts +23 -0
- package/dist/utils/async/index.js +23 -0
- package/dist/utils/async/retry.d.ts +141 -0
- package/dist/utils/async/retry.js +171 -0
- package/dist/utils/async/withTimeout.d.ts +73 -0
- package/dist/utils/async/withTimeout.js +96 -0
- package/dist/utils/csvProcessor.js +442 -0
- package/dist/utils/fileDetector.d.ts +7 -1
- package/dist/utils/fileDetector.js +91 -18
- package/dist/utils/json/extract.d.ts +103 -0
- package/dist/utils/json/extract.js +248 -0
- package/dist/utils/json/index.d.ts +36 -0
- package/dist/utils/json/index.js +36 -0
- package/dist/utils/json/safeParse.d.ts +137 -0
- package/dist/utils/json/safeParse.js +190 -0
- package/dist/utils/messageBuilder.d.ts +2 -2
- package/dist/utils/messageBuilder.js +15 -7
- package/dist/utils/sanitizers/filename.d.ts +137 -0
- package/dist/utils/sanitizers/filename.js +365 -0
- package/dist/utils/sanitizers/html.d.ts +170 -0
- package/dist/utils/sanitizers/html.js +325 -0
- package/dist/utils/sanitizers/index.d.ts +26 -0
- package/dist/utils/sanitizers/index.js +29 -0
- package/dist/utils/sanitizers/svg.d.ts +81 -0
- package/dist/utils/sanitizers/svg.js +482 -0
- package/package.json +2 -2
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retry Utilities
|
|
3
|
+
*
|
|
4
|
+
* Provides retry logic with exponential backoff for resilient async operations.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Configuration options for retry operations.
|
|
8
|
+
*/
|
|
9
|
+
export interface RetryOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Maximum number of retry attempts (not including the initial attempt).
|
|
12
|
+
* @default 3
|
|
13
|
+
*/
|
|
14
|
+
maxRetries: number;
|
|
15
|
+
/**
|
|
16
|
+
* Initial delay between retries in milliseconds.
|
|
17
|
+
* @default 1000
|
|
18
|
+
*/
|
|
19
|
+
baseDelayMs: number;
|
|
20
|
+
/**
|
|
21
|
+
* Maximum delay cap in milliseconds.
|
|
22
|
+
* @default 30000
|
|
23
|
+
*/
|
|
24
|
+
maxDelayMs: number;
|
|
25
|
+
/**
|
|
26
|
+
* Multiplier for exponential backoff.
|
|
27
|
+
* @default 2
|
|
28
|
+
*/
|
|
29
|
+
backoffMultiplier?: number;
|
|
30
|
+
/**
|
|
31
|
+
* Function to determine if a retry should be attempted.
|
|
32
|
+
* Return false to stop retrying immediately.
|
|
33
|
+
*/
|
|
34
|
+
shouldRetry?: (error: Error, attempt: number) => boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Callback invoked before each retry attempt.
|
|
37
|
+
* Useful for logging or metrics.
|
|
38
|
+
*/
|
|
39
|
+
onRetry?: (error: Error, attempt: number, delayMs: number) => void;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Default retry configuration.
|
|
43
|
+
*/
|
|
44
|
+
export declare const DEFAULT_RETRY_OPTIONS: RetryOptions;
|
|
45
|
+
/**
|
|
46
|
+
* Calculate exponential backoff delay with optional jitter.
|
|
47
|
+
*
|
|
48
|
+
* Uses the formula: min(baseDelay * 2^(attempt-1) + jitter, maxDelay)
|
|
49
|
+
*
|
|
50
|
+
* @param attempt - Current attempt number (1-based)
|
|
51
|
+
* @param baseDelayMs - Base delay in milliseconds
|
|
52
|
+
* @param maxDelayMs - Maximum delay cap in milliseconds
|
|
53
|
+
* @param addJitter - Whether to add random jitter (default: true)
|
|
54
|
+
* @returns Calculated delay in milliseconds
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```typescript
|
|
58
|
+
* // Without jitter
|
|
59
|
+
* calculateBackoff(1, 1000, 30000, false); // 1000ms
|
|
60
|
+
* calculateBackoff(2, 1000, 30000, false); // 2000ms
|
|
61
|
+
* calculateBackoff(3, 1000, 30000, false); // 4000ms
|
|
62
|
+
*
|
|
63
|
+
* // With jitter (adds up to 10% random delay)
|
|
64
|
+
* calculateBackoff(3, 1000, 30000, true); // ~4000-4400ms
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export declare function calculateBackoff(attempt: number, baseDelayMs: number, maxDelayMs: number, addJitter?: boolean): number;
|
|
68
|
+
/**
|
|
69
|
+
* Error thrown when all retry attempts are exhausted.
|
|
70
|
+
*/
|
|
71
|
+
export declare class RetryExhaustedError extends Error {
|
|
72
|
+
readonly attempts: number;
|
|
73
|
+
readonly lastError: Error;
|
|
74
|
+
/**
|
|
75
|
+
* Creates a new RetryExhaustedError.
|
|
76
|
+
*
|
|
77
|
+
* @param message - Error message
|
|
78
|
+
* @param attempts - Total number of attempts made
|
|
79
|
+
* @param lastError - The last error that caused the final failure
|
|
80
|
+
*/
|
|
81
|
+
constructor(message: string, attempts: number, lastError: Error);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Retry an async function with exponential backoff.
|
|
85
|
+
*
|
|
86
|
+
* Executes the provided function and retries on failure according to
|
|
87
|
+
* the specified options. Uses exponential backoff between retries.
|
|
88
|
+
*
|
|
89
|
+
* @param fn - Async function to execute
|
|
90
|
+
* @param options - Retry configuration (merged with defaults)
|
|
91
|
+
* @returns Promise that resolves with the function result
|
|
92
|
+
* @throws The last error if all retries are exhausted
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```typescript
|
|
96
|
+
* // Basic usage with defaults
|
|
97
|
+
* const data = await retry(() => fetchFromAPI());
|
|
98
|
+
* ```
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```typescript
|
|
102
|
+
* // With custom options
|
|
103
|
+
* const data = await retry(
|
|
104
|
+
* () => fetchFromAPI(),
|
|
105
|
+
* {
|
|
106
|
+
* maxRetries: 5,
|
|
107
|
+
* baseDelayMs: 500,
|
|
108
|
+
* maxDelayMs: 10000,
|
|
109
|
+
* onRetry: (err, attempt, delay) => {
|
|
110
|
+
* console.log(`Retry ${attempt} after ${delay}ms: ${err.message}`);
|
|
111
|
+
* },
|
|
112
|
+
* shouldRetry: (err) => {
|
|
113
|
+
* // Only retry on network errors
|
|
114
|
+
* return err.name === 'NetworkError';
|
|
115
|
+
* }
|
|
116
|
+
* }
|
|
117
|
+
* );
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
export declare function retry<T>(fn: () => Promise<T>, options?: Partial<RetryOptions>): Promise<T>;
|
|
121
|
+
/**
|
|
122
|
+
* Create a retry wrapper with pre-configured options.
|
|
123
|
+
*
|
|
124
|
+
* Useful for creating reusable retry strategies.
|
|
125
|
+
*
|
|
126
|
+
* @param defaultOptions - Default retry options for the wrapper
|
|
127
|
+
* @returns A retry function with the specified defaults
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```typescript
|
|
131
|
+
* const apiRetry = createRetry({
|
|
132
|
+
* maxRetries: 5,
|
|
133
|
+
* baseDelayMs: 100,
|
|
134
|
+
* onRetry: (err, attempt) => logger.warn(`API retry ${attempt}`)
|
|
135
|
+
* });
|
|
136
|
+
*
|
|
137
|
+
* // Use the configured retry
|
|
138
|
+
* const data = await apiRetry(() => fetchFromAPI());
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
export declare function createRetry(defaultOptions: Partial<RetryOptions>): <T>(fn: () => Promise<T>, overrideOptions?: Partial<RetryOptions>) => Promise<T>;
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retry Utilities
|
|
3
|
+
*
|
|
4
|
+
* Provides retry logic with exponential backoff for resilient async operations.
|
|
5
|
+
*/
|
|
6
|
+
import { delay } from "./delay.js";
|
|
7
|
+
/**
|
|
8
|
+
* Default retry configuration.
|
|
9
|
+
*/
|
|
10
|
+
export const DEFAULT_RETRY_OPTIONS = {
|
|
11
|
+
maxRetries: 3,
|
|
12
|
+
baseDelayMs: 1000,
|
|
13
|
+
maxDelayMs: 30000,
|
|
14
|
+
backoffMultiplier: 2,
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Calculate exponential backoff delay with optional jitter.
|
|
18
|
+
*
|
|
19
|
+
* Uses the formula: min(baseDelay * 2^(attempt-1) + jitter, maxDelay)
|
|
20
|
+
*
|
|
21
|
+
* @param attempt - Current attempt number (1-based)
|
|
22
|
+
* @param baseDelayMs - Base delay in milliseconds
|
|
23
|
+
* @param maxDelayMs - Maximum delay cap in milliseconds
|
|
24
|
+
* @param addJitter - Whether to add random jitter (default: true)
|
|
25
|
+
* @returns Calculated delay in milliseconds
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* // Without jitter
|
|
30
|
+
* calculateBackoff(1, 1000, 30000, false); // 1000ms
|
|
31
|
+
* calculateBackoff(2, 1000, 30000, false); // 2000ms
|
|
32
|
+
* calculateBackoff(3, 1000, 30000, false); // 4000ms
|
|
33
|
+
*
|
|
34
|
+
* // With jitter (adds up to 10% random delay)
|
|
35
|
+
* calculateBackoff(3, 1000, 30000, true); // ~4000-4400ms
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export function calculateBackoff(attempt, baseDelayMs, maxDelayMs, addJitter = true) {
|
|
39
|
+
const exponentialDelay = baseDelayMs * 2 ** (attempt - 1);
|
|
40
|
+
if (addJitter) {
|
|
41
|
+
// Add up to 10% jitter to prevent thundering herd
|
|
42
|
+
const jitter = Math.random() * 0.1 * exponentialDelay;
|
|
43
|
+
return Math.min(exponentialDelay + jitter, maxDelayMs);
|
|
44
|
+
}
|
|
45
|
+
return Math.min(exponentialDelay, maxDelayMs);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Error thrown when all retry attempts are exhausted.
|
|
49
|
+
*/
|
|
50
|
+
export class RetryExhaustedError extends Error {
|
|
51
|
+
attempts;
|
|
52
|
+
lastError;
|
|
53
|
+
/**
|
|
54
|
+
* Creates a new RetryExhaustedError.
|
|
55
|
+
*
|
|
56
|
+
* @param message - Error message
|
|
57
|
+
* @param attempts - Total number of attempts made
|
|
58
|
+
* @param lastError - The last error that caused the final failure
|
|
59
|
+
*/
|
|
60
|
+
constructor(message, attempts, lastError) {
|
|
61
|
+
super(message);
|
|
62
|
+
this.attempts = attempts;
|
|
63
|
+
this.lastError = lastError;
|
|
64
|
+
this.name = "RetryExhaustedError";
|
|
65
|
+
if (Error.captureStackTrace) {
|
|
66
|
+
Error.captureStackTrace(this, RetryExhaustedError);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Retry an async function with exponential backoff.
|
|
72
|
+
*
|
|
73
|
+
* Executes the provided function and retries on failure according to
|
|
74
|
+
* the specified options. Uses exponential backoff between retries.
|
|
75
|
+
*
|
|
76
|
+
* @param fn - Async function to execute
|
|
77
|
+
* @param options - Retry configuration (merged with defaults)
|
|
78
|
+
* @returns Promise that resolves with the function result
|
|
79
|
+
* @throws The last error if all retries are exhausted
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* // Basic usage with defaults
|
|
84
|
+
* const data = await retry(() => fetchFromAPI());
|
|
85
|
+
* ```
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* // With custom options
|
|
90
|
+
* const data = await retry(
|
|
91
|
+
* () => fetchFromAPI(),
|
|
92
|
+
* {
|
|
93
|
+
* maxRetries: 5,
|
|
94
|
+
* baseDelayMs: 500,
|
|
95
|
+
* maxDelayMs: 10000,
|
|
96
|
+
* onRetry: (err, attempt, delay) => {
|
|
97
|
+
* console.log(`Retry ${attempt} after ${delay}ms: ${err.message}`);
|
|
98
|
+
* },
|
|
99
|
+
* shouldRetry: (err) => {
|
|
100
|
+
* // Only retry on network errors
|
|
101
|
+
* return err.name === 'NetworkError';
|
|
102
|
+
* }
|
|
103
|
+
* }
|
|
104
|
+
* );
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
export async function retry(fn, options = {}) {
|
|
108
|
+
const config = {
|
|
109
|
+
...DEFAULT_RETRY_OPTIONS,
|
|
110
|
+
...options,
|
|
111
|
+
};
|
|
112
|
+
const { maxRetries, baseDelayMs, maxDelayMs, backoffMultiplier = 2, shouldRetry = () => true, onRetry, } = config;
|
|
113
|
+
let lastError = new Error("Retry failed");
|
|
114
|
+
let currentDelay = baseDelayMs;
|
|
115
|
+
// Total attempts = initial attempt + retries
|
|
116
|
+
const totalAttempts = maxRetries + 1;
|
|
117
|
+
for (let attempt = 1; attempt <= totalAttempts; attempt++) {
|
|
118
|
+
try {
|
|
119
|
+
return await fn();
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
123
|
+
lastError = err;
|
|
124
|
+
// Check if we've exhausted all retries
|
|
125
|
+
if (attempt >= totalAttempts) {
|
|
126
|
+
throw new RetryExhaustedError(`All ${totalAttempts} retry attempts exhausted`, totalAttempts, err);
|
|
127
|
+
}
|
|
128
|
+
// Check if we should retry this error
|
|
129
|
+
if (!shouldRetry(err, attempt)) {
|
|
130
|
+
throw err;
|
|
131
|
+
}
|
|
132
|
+
// Calculate delay with exponential backoff (capped at maxDelay)
|
|
133
|
+
const delayMs = Math.min(currentDelay, maxDelayMs);
|
|
134
|
+
// Notify about retry
|
|
135
|
+
if (onRetry) {
|
|
136
|
+
onRetry(err, attempt, delayMs);
|
|
137
|
+
}
|
|
138
|
+
// Wait before next attempt
|
|
139
|
+
await delay(delayMs);
|
|
140
|
+
// Increase delay for next iteration
|
|
141
|
+
currentDelay = currentDelay * backoffMultiplier;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
// This should never be reached, but TypeScript needs it
|
|
145
|
+
throw new RetryExhaustedError(`All ${totalAttempts} retry attempts exhausted`, totalAttempts, lastError || new Error("Unknown retry failure"));
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Create a retry wrapper with pre-configured options.
|
|
149
|
+
*
|
|
150
|
+
* Useful for creating reusable retry strategies.
|
|
151
|
+
*
|
|
152
|
+
* @param defaultOptions - Default retry options for the wrapper
|
|
153
|
+
* @returns A retry function with the specified defaults
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```typescript
|
|
157
|
+
* const apiRetry = createRetry({
|
|
158
|
+
* maxRetries: 5,
|
|
159
|
+
* baseDelayMs: 100,
|
|
160
|
+
* onRetry: (err, attempt) => logger.warn(`API retry ${attempt}`)
|
|
161
|
+
* });
|
|
162
|
+
*
|
|
163
|
+
* // Use the configured retry
|
|
164
|
+
* const data = await apiRetry(() => fetchFromAPI());
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
export function createRetry(defaultOptions) {
|
|
168
|
+
return (fn, overrideOptions) => {
|
|
169
|
+
return retry(fn, { ...defaultOptions, ...overrideOptions });
|
|
170
|
+
};
|
|
171
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Timeout Utilities
|
|
3
|
+
*
|
|
4
|
+
* Wrapper functions for adding timeout protection to async operations.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Error thrown when an operation times out.
|
|
8
|
+
*/
|
|
9
|
+
export declare class TimeoutError extends Error {
|
|
10
|
+
readonly timeoutMs: number;
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new TimeoutError.
|
|
13
|
+
*
|
|
14
|
+
* @param message - Error message describing the timeout
|
|
15
|
+
* @param timeoutMs - The timeout duration that was exceeded
|
|
16
|
+
*/
|
|
17
|
+
constructor(message: string, timeoutMs: number);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Execute a promise with timeout protection.
|
|
21
|
+
*
|
|
22
|
+
* Wraps a promise and rejects with a TimeoutError if the operation
|
|
23
|
+
* takes longer than the specified duration.
|
|
24
|
+
*
|
|
25
|
+
* @param promise - The promise to wrap with timeout
|
|
26
|
+
* @param ms - Maximum time to wait in milliseconds
|
|
27
|
+
* @param message - Optional custom error message for timeout
|
|
28
|
+
* @returns Promise that resolves with the result or rejects on timeout
|
|
29
|
+
* @throws {TimeoutError} If the operation exceeds the timeout duration
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* const result = await withTimeout(
|
|
34
|
+
* fetchData(),
|
|
35
|
+
* 5000,
|
|
36
|
+
* 'Data fetch timed out'
|
|
37
|
+
* );
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* try {
|
|
43
|
+
* const data = await withTimeout(slowOperation(), 3000);
|
|
44
|
+
* } catch (error) {
|
|
45
|
+
* if (error instanceof TimeoutError) {
|
|
46
|
+
* console.log(`Timed out after ${error.timeoutMs}ms`);
|
|
47
|
+
* }
|
|
48
|
+
* }
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export declare function withTimeout<T>(promise: Promise<T>, ms: number, message?: string): Promise<T>;
|
|
52
|
+
/**
|
|
53
|
+
* Execute a function with timeout protection.
|
|
54
|
+
*
|
|
55
|
+
* Alternative signature that accepts a function instead of a promise,
|
|
56
|
+
* useful when you want to delay starting the operation.
|
|
57
|
+
*
|
|
58
|
+
* @param fn - Async function to execute
|
|
59
|
+
* @param ms - Maximum time to wait in milliseconds
|
|
60
|
+
* @param message - Optional custom error message for timeout
|
|
61
|
+
* @returns Promise that resolves with the function result or rejects on timeout
|
|
62
|
+
* @throws {TimeoutError} If the operation exceeds the timeout duration
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```typescript
|
|
66
|
+
* const result = await withTimeoutFn(
|
|
67
|
+
* () => fetchData(),
|
|
68
|
+
* 5000,
|
|
69
|
+
* 'Data fetch timed out'
|
|
70
|
+
* );
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
export declare function withTimeoutFn<T>(fn: () => Promise<T>, ms: number, message?: string): Promise<T>;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Timeout Utilities
|
|
3
|
+
*
|
|
4
|
+
* Wrapper functions for adding timeout protection to async operations.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Error thrown when an operation times out.
|
|
8
|
+
*/
|
|
9
|
+
export class TimeoutError extends Error {
|
|
10
|
+
timeoutMs;
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new TimeoutError.
|
|
13
|
+
*
|
|
14
|
+
* @param message - Error message describing the timeout
|
|
15
|
+
* @param timeoutMs - The timeout duration that was exceeded
|
|
16
|
+
*/
|
|
17
|
+
constructor(message, timeoutMs) {
|
|
18
|
+
super(message);
|
|
19
|
+
this.timeoutMs = timeoutMs;
|
|
20
|
+
this.name = "TimeoutError";
|
|
21
|
+
// Maintains proper stack trace for where error was thrown (V8 engines)
|
|
22
|
+
if (Error.captureStackTrace) {
|
|
23
|
+
Error.captureStackTrace(this, TimeoutError);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Execute a promise with timeout protection.
|
|
29
|
+
*
|
|
30
|
+
* Wraps a promise and rejects with a TimeoutError if the operation
|
|
31
|
+
* takes longer than the specified duration.
|
|
32
|
+
*
|
|
33
|
+
* @param promise - The promise to wrap with timeout
|
|
34
|
+
* @param ms - Maximum time to wait in milliseconds
|
|
35
|
+
* @param message - Optional custom error message for timeout
|
|
36
|
+
* @returns Promise that resolves with the result or rejects on timeout
|
|
37
|
+
* @throws {TimeoutError} If the operation exceeds the timeout duration
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* const result = await withTimeout(
|
|
42
|
+
* fetchData(),
|
|
43
|
+
* 5000,
|
|
44
|
+
* 'Data fetch timed out'
|
|
45
|
+
* );
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* try {
|
|
51
|
+
* const data = await withTimeout(slowOperation(), 3000);
|
|
52
|
+
* } catch (error) {
|
|
53
|
+
* if (error instanceof TimeoutError) {
|
|
54
|
+
* console.log(`Timed out after ${error.timeoutMs}ms`);
|
|
55
|
+
* }
|
|
56
|
+
* }
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export async function withTimeout(promise, ms, message) {
|
|
60
|
+
let timeoutId;
|
|
61
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
62
|
+
timeoutId = setTimeout(() => {
|
|
63
|
+
reject(new TimeoutError(message || `Operation timed out after ${ms}ms`, ms));
|
|
64
|
+
}, ms);
|
|
65
|
+
});
|
|
66
|
+
try {
|
|
67
|
+
return await Promise.race([promise, timeoutPromise]);
|
|
68
|
+
}
|
|
69
|
+
finally {
|
|
70
|
+
clearTimeout(timeoutId);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Execute a function with timeout protection.
|
|
75
|
+
*
|
|
76
|
+
* Alternative signature that accepts a function instead of a promise,
|
|
77
|
+
* useful when you want to delay starting the operation.
|
|
78
|
+
*
|
|
79
|
+
* @param fn - Async function to execute
|
|
80
|
+
* @param ms - Maximum time to wait in milliseconds
|
|
81
|
+
* @param message - Optional custom error message for timeout
|
|
82
|
+
* @returns Promise that resolves with the function result or rejects on timeout
|
|
83
|
+
* @throws {TimeoutError} If the operation exceeds the timeout duration
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* const result = await withTimeoutFn(
|
|
88
|
+
* () => fetchData(),
|
|
89
|
+
* 5000,
|
|
90
|
+
* 'Data fetch timed out'
|
|
91
|
+
* );
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
export async function withTimeoutFn(fn, ms, message) {
|
|
95
|
+
return withTimeout(fn(), ms, message);
|
|
96
|
+
}
|