@idoa/utils 0.1.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/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # @idoa/utils
2
+
3
+ Chain-agnostic helpers for retry/backoff, timeout boundaries, and small utility guards.
4
+
5
+ ## Usage
6
+
7
+ ```ts
8
+ import { retryWithBackoff, withTimeout } from '@idoa/utils';
9
+ ```
@@ -0,0 +1,11 @@
1
+ export interface RetryOptions {
2
+ retries: number;
3
+ retryDelayMs: number;
4
+ maxRetryDelayMs: number;
5
+ shouldRetry?: (error: unknown, attempt: number) => boolean;
6
+ }
7
+ export declare function retryWithBackoff<T>(operation: () => Promise<T>, options: RetryOptions): Promise<T>;
8
+ export declare function withTimeout<T>(operation: Promise<T>, timeoutMs: number, label?: string): Promise<T>;
9
+ export declare function toError(error: unknown): Error;
10
+ export declare function sleep(ms: number): Promise<void>;
11
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,YAAY;IAC3B,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,EAAE,MAAM,CAAC;IACrB,eAAe,EAAE,MAAM,CAAC;IACxB,WAAW,CAAC,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,KAAK,OAAO,CAAC;CAC5D;AAED,wBAAsB,gBAAgB,CAAC,CAAC,EACtC,SAAS,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EAC3B,OAAO,EAAE,YAAY,GACpB,OAAO,CAAC,CAAC,CAAC,CAwBZ;AAED,wBAAsB,WAAW,CAAC,CAAC,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,MAAM,EAAE,KAAK,SAAc,cAoBjG;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,CAM7C;AAED,wBAAgB,KAAK,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAE/C"}
package/dist/index.js ADDED
@@ -0,0 +1,53 @@
1
+ // src/index.ts
2
+ async function retryWithBackoff(operation, options) {
3
+ let attempt = 0;
4
+ let lastError;
5
+ while (attempt <= options.retries) {
6
+ try {
7
+ return await operation();
8
+ } catch (error) {
9
+ lastError = error;
10
+ const canRetry = attempt < options.retries && (options.shouldRetry ? options.shouldRetry(error, attempt + 1) : true);
11
+ if (!canRetry) {
12
+ break;
13
+ }
14
+ const waitMs = Math.min(options.retryDelayMs * 2 ** attempt, options.maxRetryDelayMs);
15
+ await sleep(waitMs);
16
+ attempt += 1;
17
+ }
18
+ }
19
+ throw lastError instanceof Error ? lastError : new Error("Retry operation failed");
20
+ }
21
+ async function withTimeout(operation, timeoutMs, label = "operation") {
22
+ if (timeoutMs <= 0) {
23
+ throw new Error("timeoutMs must be greater than 0");
24
+ }
25
+ let timer;
26
+ const timeoutPromise = new Promise((_, reject) => {
27
+ timer = setTimeout(() => {
28
+ reject(new Error(`${label} timed out after ${timeoutMs}ms`));
29
+ }, timeoutMs);
30
+ });
31
+ try {
32
+ return await Promise.race([operation, timeoutPromise]);
33
+ } finally {
34
+ if (timer) {
35
+ clearTimeout(timer);
36
+ }
37
+ }
38
+ }
39
+ function toError(error) {
40
+ if (error instanceof Error) {
41
+ return error;
42
+ }
43
+ return new Error(typeof error === "string" ? error : "Unknown error");
44
+ }
45
+ function sleep(ms) {
46
+ return new Promise((resolve) => setTimeout(resolve, ms));
47
+ }
48
+ export {
49
+ retryWithBackoff,
50
+ sleep,
51
+ toError,
52
+ withTimeout
53
+ };
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@idoa/utils",
3
+ "version": "0.1.0",
4
+ "description": "Utility helpers for retry, timeout, and defensive runtime handling.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/Mateja3m/chain-dev-doctor-core.git",
9
+ "directory": "packages/utils"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/Mateja3m/chain-dev-doctor-core/issues"
13
+ },
14
+ "homepage": "https://github.com/Mateja3m/chain-dev-doctor-core/tree/main/packages/utils#readme",
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "sideEffects": false,
19
+ "keywords": [
20
+ "idoa",
21
+ "utils",
22
+ "retry",
23
+ "timeout",
24
+ "web3"
25
+ ],
26
+ "type": "module",
27
+ "main": "./dist/index.js",
28
+ "module": "./dist/index.js",
29
+ "types": "./dist/index.d.ts",
30
+ "exports": {
31
+ ".": {
32
+ "types": "./dist/index.d.ts",
33
+ "import": "./dist/index.js"
34
+ }
35
+ },
36
+ "files": ["dist"],
37
+ "scripts": {
38
+ "build": "tsup src/index.ts --format esm --clean && tsc -p tsconfig.build.json",
39
+ "clean": "rm -rf dist",
40
+ "lint": "eslint src test --ext .ts",
41
+ "test": "vitest run",
42
+ "typecheck": "tsc -p tsconfig.json --noEmit"
43
+ }
44
+ }