@ledgerhq/coin-module-framework 7.0.0 → 7.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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @ledgerhq/coin-module-framework
2
2
 
3
+ ## 7.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#784](https://github.com/LedgerHQ/coin-modules/pull/784) [`a985acb`](https://github.com/LedgerHQ/coin-modules/commit/a985acb786f8edb407e383cac98d38f652cfa235) Thanks [@francois-guerin-ledger](https://github.com/francois-guerin-ledger)! - feat(framework): fork `@ledgerhq/live-promise`
8
+
3
9
  ## 7.0.0
4
10
 
5
11
  ### Major Changes
@@ -0,0 +1,2 @@
1
+ export { delay, retry, promiseAllBatched } from './promises';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/promises/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA"}
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ // SPDX-FileCopyrightText: © 2024 LEDGER SAS
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.promiseAllBatched = exports.retry = exports.delay = void 0;
6
+ var promises_1 = require("./promises");
7
+ Object.defineProperty(exports, "delay", { enumerable: true, get: function () { return promises_1.delay; } });
8
+ Object.defineProperty(exports, "retry", { enumerable: true, get: function () { return promises_1.retry; } });
9
+ Object.defineProperty(exports, "promiseAllBatched", { enumerable: true, get: function () { return promises_1.promiseAllBatched; } });
10
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/promises/index.ts"],"names":[],"mappings":";AAAA,4CAA4C;AAC5C,sCAAsC;;;AAEtC,uCAA4D;AAAnD,iGAAA,KAAK,OAAA;AAAE,iGAAA,KAAK,OAAA;AAAE,6GAAA,iBAAiB,OAAA"}
@@ -0,0 +1,12 @@
1
+ export declare const delay: (ms: number) => Promise<void>;
2
+ declare const defaults: {
3
+ maxRetry: number;
4
+ interval: number;
5
+ intervalMultiplicator: number;
6
+ context: string;
7
+ retryCondition: (_: unknown) => boolean;
8
+ };
9
+ export declare function retry<A>(f: () => Promise<A>, options?: Partial<typeof defaults>): Promise<A>;
10
+ export declare function promiseAllBatched<A, B>(batch: number, items: Array<A>, fn: (arg0: A, arg1: number) => Promise<B>): Promise<B[]>;
11
+ export {};
12
+ //# sourceMappingURL=promises.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"promises.d.ts","sourceRoot":"","sources":["../../src/promises/promises.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,KAAK,GAAI,IAAI,MAAM,KAAG,OAAO,CAAC,IAAI,CAA0C,CAAA;AAEzF,QAAA,MAAM,QAAQ;;;;;wBAKQ,OAAO;CAC5B,CAAA;AAED,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,OAAO,QAAQ,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAsB5F;AAED,wBAAsB,iBAAiB,CAAC,CAAC,EAAE,CAAC,EAC1C,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,EACf,EAAE,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,CAAC,CAAC,GACxC,OAAO,CAAC,CAAC,EAAE,CAAC,CAuBd"}
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ // SPDX-FileCopyrightText: © 2024 LEDGER SAS
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ // Forked from @ledgerhq/promises/src/promise.ts
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.delay = void 0;
7
+ exports.retry = retry;
8
+ exports.promiseAllBatched = promiseAllBatched;
9
+ const delay = (ms) => new Promise((f) => setTimeout(f, ms));
10
+ exports.delay = delay;
11
+ const defaults = {
12
+ maxRetry: 4,
13
+ interval: 300,
14
+ intervalMultiplicator: 1.5,
15
+ context: '',
16
+ retryCondition: (_) => true,
17
+ };
18
+ function retry(f, options) {
19
+ const { maxRetry, interval, intervalMultiplicator, retryCondition } = {
20
+ ...defaults,
21
+ ...options,
22
+ };
23
+ function rec(remainingTry, i) {
24
+ const result = f();
25
+ if (remainingTry <= 0) {
26
+ return result;
27
+ }
28
+ return result.catch((e) => {
29
+ if (!retryCondition(e)) {
30
+ throw e;
31
+ }
32
+ return (0, exports.delay)(i).then(() => rec(remainingTry - 1, i * intervalMultiplicator));
33
+ });
34
+ }
35
+ return rec(maxRetry, interval);
36
+ }
37
+ async function promiseAllBatched(batch, items, fn) {
38
+ const data = Array(items.length);
39
+ const queue = items.map((item, index) => ({
40
+ item,
41
+ index,
42
+ }));
43
+ async function step() {
44
+ if (queue.length === 0)
45
+ return;
46
+ const first = queue.shift();
47
+ if (first) {
48
+ const { item, index } = first;
49
+ data[index] = await fn(item, index);
50
+ }
51
+ await step();
52
+ }
53
+ await Promise.all(Array(Math.min(batch, items.length))
54
+ .fill(() => undefined)
55
+ .map(step));
56
+ return data;
57
+ }
58
+ //# sourceMappingURL=promises.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"promises.js","sourceRoot":"","sources":["../../src/promises/promises.ts"],"names":[],"mappings":";AAAA,4CAA4C;AAC5C,sCAAsC;AACtC,gDAAgD;;;AAYhD,sBAsBC;AAED,8CA2BC;AA7DM,MAAM,KAAK,GAAG,CAAC,EAAU,EAAiB,EAAE,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAA;AAA5E,QAAA,KAAK,SAAuE;AA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,KAAK,CAAC,MAAM,KAAK,CAAC;YAAE,OAAM;QAC9B,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,EAAE,CAAA;QAC3B,IAAI,KAAK,EAAE,CAAC;YACV,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,KAAK,CAAA;YAC7B,IAAI,CAAC,KAAK,CAAC,GAAG,MAAM,EAAE,CAAC,IAAI,EAAE,KAAK,CAAC,CAAA;QACrC,CAAC;QACD,MAAM,IAAI,EAAE,CAAA;IACd,CAAC;IAED,MAAM,OAAO,CAAC,GAAG,CACf,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;SACjC,IAAI,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC;SACrB,GAAG,CAAC,IAAI,CAAC,CACb,CAAA;IACD,OAAO,IAAI,CAAA;AACb,CAAC"}
@@ -0,0 +1,2 @@
1
+ export { delay, retry, promiseAllBatched } from './promises';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/promises/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA"}
@@ -0,0 +1,4 @@
1
+ // SPDX-FileCopyrightText: © 2024 LEDGER SAS
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ export { delay, retry, promiseAllBatched } from './promises';
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/promises/index.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,sCAAsC;AAEtC,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA"}
@@ -0,0 +1,12 @@
1
+ export declare const delay: (ms: number) => Promise<void>;
2
+ declare const defaults: {
3
+ maxRetry: number;
4
+ interval: number;
5
+ intervalMultiplicator: number;
6
+ context: string;
7
+ retryCondition: (_: unknown) => boolean;
8
+ };
9
+ export declare function retry<A>(f: () => Promise<A>, options?: Partial<typeof defaults>): Promise<A>;
10
+ export declare function promiseAllBatched<A, B>(batch: number, items: Array<A>, fn: (arg0: A, arg1: number) => Promise<B>): Promise<B[]>;
11
+ export {};
12
+ //# sourceMappingURL=promises.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"promises.d.ts","sourceRoot":"","sources":["../../src/promises/promises.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,KAAK,GAAI,IAAI,MAAM,KAAG,OAAO,CAAC,IAAI,CAA0C,CAAA;AAEzF,QAAA,MAAM,QAAQ;;;;;wBAKQ,OAAO;CAC5B,CAAA;AAED,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,OAAO,QAAQ,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAsB5F;AAED,wBAAsB,iBAAiB,CAAC,CAAC,EAAE,CAAC,EAC1C,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,EACf,EAAE,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,KAAK,OAAO,CAAC,CAAC,CAAC,GACxC,OAAO,CAAC,CAAC,EAAE,CAAC,CAuBd"}
@@ -0,0 +1,52 @@
1
+ // SPDX-FileCopyrightText: © 2024 LEDGER SAS
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ // Forked from @ledgerhq/promises/src/promise.ts
4
+ export const delay = (ms) => new Promise((f) => setTimeout(f, ms));
5
+ const defaults = {
6
+ maxRetry: 4,
7
+ interval: 300,
8
+ intervalMultiplicator: 1.5,
9
+ context: '',
10
+ retryCondition: (_) => true,
11
+ };
12
+ export function retry(f, options) {
13
+ const { maxRetry, interval, intervalMultiplicator, retryCondition } = {
14
+ ...defaults,
15
+ ...options,
16
+ };
17
+ function rec(remainingTry, i) {
18
+ const result = f();
19
+ if (remainingTry <= 0) {
20
+ return result;
21
+ }
22
+ return result.catch((e) => {
23
+ if (!retryCondition(e)) {
24
+ throw e;
25
+ }
26
+ return delay(i).then(() => rec(remainingTry - 1, i * intervalMultiplicator));
27
+ });
28
+ }
29
+ return rec(maxRetry, interval);
30
+ }
31
+ export async function promiseAllBatched(batch, items, fn) {
32
+ const data = Array(items.length);
33
+ const queue = items.map((item, index) => ({
34
+ item,
35
+ index,
36
+ }));
37
+ async function step() {
38
+ if (queue.length === 0)
39
+ return;
40
+ const first = queue.shift();
41
+ if (first) {
42
+ const { item, index } = first;
43
+ data[index] = await fn(item, index);
44
+ }
45
+ await step();
46
+ }
47
+ await Promise.all(Array(Math.min(batch, items.length))
48
+ .fill(() => undefined)
49
+ .map(step));
50
+ return data;
51
+ }
52
+ //# sourceMappingURL=promises.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"promises.js","sourceRoot":"","sources":["../../src/promises/promises.ts"],"names":[],"mappings":"AAAA,4CAA4C;AAC5C,sCAAsC;AACtC,gDAAgD;AAEhD,MAAM,CAAC,MAAM,KAAK,GAAG,CAAC,EAAU,EAAiB,EAAE,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAA;AAEzF,MAAM,QAAQ,GAAG;IACf,QAAQ,EAAE,CAAC;IACX,QAAQ,EAAE,GAAG;IACb,qBAAqB,EAAE,GAAG;IAC1B,OAAO,EAAE,EAAE;IACX,cAAc,EAAE,CAAC,CAAU,EAAE,EAAE,CAAC,IAAI;CACrC,CAAA;AAED,MAAM,UAAU,KAAK,CAAI,CAAmB,EAAE,OAAkC;IAC9E,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,qBAAqB,EAAE,cAAc,EAAE,GAAG;QACpE,GAAG,QAAQ;QACX,GAAG,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,OAAO,CAAC,GAAG,CACf,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;SACjC,IAAI,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC;SACrB,GAAG,CAAC,IAAI,CAAC,CACb,CAAA;IACD,OAAO,IAAI,CAAA;AACb,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ledgerhq/coin-module-framework",
3
- "version": "7.0.0",
3
+ "version": "7.1.0",
4
4
  "description": "Ledger framework for Coin integration",
5
5
  "keywords": [
6
6
  "Ledger",
@@ -29,6 +29,9 @@
29
29
  "currencies": [
30
30
  "lib/currencies/index"
31
31
  ],
32
+ "promises": [
33
+ "lib/promises/index"
34
+ ],
32
35
  "test-utils": [
33
36
  "lib/test-utils/helpers"
34
37
  ],
@@ -45,6 +48,11 @@
45
48
  "require": "./lib/currencies/index.js",
46
49
  "default": "./lib-es/currencies/index.js"
47
50
  },
51
+ "./promises": {
52
+ "@ledgerhq/source": "./src/promises/index.ts",
53
+ "require": "./lib/promises/index.js",
54
+ "default": "./lib-es/promises/index.js"
55
+ },
48
56
  "./errors": {
49
57
  "@ledgerhq/source": "./src/errors.ts",
50
58
  "require": "./lib/errors.js",
@@ -0,0 +1,4 @@
1
+ // SPDX-FileCopyrightText: © 2024 LEDGER SAS
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export { delay, retry, promiseAllBatched } from './promises'
@@ -0,0 +1,73 @@
1
+ // SPDX-FileCopyrightText: © 2024 LEDGER SAS
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ // Forked from @ledgerhq/promises/src/promise.test.ts
4
+
5
+ import { promiseAllBatched, retry } from './promises'
6
+
7
+ describe('promise', () => {
8
+ test('promiseAllBatched', async () => {
9
+ const promisifyIdPlusOne = (a: number) => Promise.resolve(a + 1)
10
+
11
+ const p = promiseAllBatched(5, [], promisifyIdPlusOne)
12
+ expect(typeof p.then).toBe('function')
13
+ expect(await p).toEqual([])
14
+ expect(await promiseAllBatched(5, [1, 2, 3, 4, 5, 6, 7, 8], promisifyIdPlusOne)).toEqual([
15
+ 2, 3, 4, 5, 6, 7, 8, 9,
16
+ ])
17
+ expect(await promiseAllBatched(1, [1, 2, 3, 4, 5, 6, 7, 8], promisifyIdPlusOne)).toEqual([
18
+ 2, 3, 4, 5, 6, 7, 8, 9,
19
+ ])
20
+ expect(await promiseAllBatched(10, [1, 2, 3, 4, 5, 6, 7, 8], promisifyIdPlusOne)).toEqual([
21
+ 2, 3, 4, 5, 6, 7, 8, 9,
22
+ ])
23
+ expect(await promiseAllBatched(2, Array(6).fill(0), (_, i) => Promise.resolve(i))).toEqual([
24
+ 0, 1, 2, 3, 4, 5,
25
+ ])
26
+ })
27
+
28
+ describe('promise retries', () => {
29
+ test('should retry by default', async () => {
30
+ let numberOfExecutions = 0
31
+ const testFunction = () => Promise.reject(numberOfExecutions++)
32
+ try {
33
+ await retry(() => testFunction(), { maxRetry: 5, interval: 0 })
34
+ } catch (e) {
35
+ expect(e).toBe(5)
36
+ }
37
+ expect(numberOfExecutions).toBe(6)
38
+ })
39
+
40
+ test('should retry when retryCondition is passed', async () => {
41
+ let numberOfExecutions = 0
42
+ const testFunction = () => Promise.reject(numberOfExecutions++)
43
+ try {
44
+ await retry(() => testFunction(), {
45
+ maxRetry: 5,
46
+ interval: 0,
47
+ retryCondition: (_) => true,
48
+ })
49
+ } catch (e) {
50
+ expect(e).toBe(5)
51
+ }
52
+ expect(numberOfExecutions).toBe(6)
53
+ })
54
+
55
+ test('should not retry when retryCondition returns false', async () => {
56
+ let numberOfExecutions = 0
57
+ const testFunction = () => Promise.reject(numberOfExecutions++)
58
+ try {
59
+ await retry(() => testFunction(), {
60
+ maxRetry: 5,
61
+ interval: 0,
62
+ retryCondition: (e) => {
63
+ expect(e).toBe(0)
64
+ return false
65
+ },
66
+ })
67
+ } catch (e) {
68
+ expect(e).toBe(0)
69
+ }
70
+ expect(numberOfExecutions).toBe(1)
71
+ })
72
+ })
73
+ })
@@ -0,0 +1,66 @@
1
+ // SPDX-FileCopyrightText: © 2024 LEDGER SAS
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ // Forked from @ledgerhq/promises/src/promise.ts
4
+
5
+ export const delay = (ms: number): Promise<void> => new Promise((f) => setTimeout(f, ms))
6
+
7
+ const defaults = {
8
+ maxRetry: 4,
9
+ interval: 300,
10
+ intervalMultiplicator: 1.5,
11
+ context: '',
12
+ retryCondition: (_: unknown) => true,
13
+ }
14
+
15
+ export function retry<A>(f: () => Promise<A>, options?: Partial<typeof defaults>): Promise<A> {
16
+ const { maxRetry, interval, intervalMultiplicator, retryCondition } = {
17
+ ...defaults,
18
+ ...options,
19
+ }
20
+
21
+ function rec(remainingTry: number, i: number): Promise<A> {
22
+ const result = f()
23
+
24
+ if (remainingTry <= 0) {
25
+ return result
26
+ }
27
+
28
+ return result.catch((e) => {
29
+ if (!retryCondition(e)) {
30
+ throw e
31
+ }
32
+ return delay(i).then(() => rec(remainingTry - 1, i * intervalMultiplicator))
33
+ })
34
+ }
35
+
36
+ return rec(maxRetry, interval)
37
+ }
38
+
39
+ export async function promiseAllBatched<A, B>(
40
+ batch: number,
41
+ items: Array<A>,
42
+ fn: (arg0: A, arg1: number) => Promise<B>
43
+ ): Promise<B[]> {
44
+ const data = Array(items.length)
45
+ const queue = items.map((item, index) => ({
46
+ item,
47
+ index,
48
+ }))
49
+
50
+ async function step() {
51
+ if (queue.length === 0) return
52
+ const first = queue.shift()
53
+ if (first) {
54
+ const { item, index } = first
55
+ data[index] = await fn(item, index)
56
+ }
57
+ await step()
58
+ }
59
+
60
+ await Promise.all(
61
+ Array(Math.min(batch, items.length))
62
+ .fill(() => undefined)
63
+ .map(step)
64
+ )
65
+ return data
66
+ }