@lodestar/utils 1.35.0-dev.f80d2d52da → 1.35.0-dev.fcf8d024ea
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/lib/assert.d.ts.map +1 -0
- package/lib/assert.js +1 -1
- package/lib/assert.js.map +1 -1
- package/lib/base64.d.ts.map +1 -0
- package/lib/bytes/browser.d.ts.map +1 -0
- package/lib/bytes/index.d.ts.map +1 -0
- package/lib/bytes/nodejs.d.ts.map +1 -0
- package/lib/bytes.d.ts.map +1 -0
- package/lib/command.d.ts.map +1 -0
- package/lib/diff.d.ts.map +1 -0
- package/lib/err.d.ts.map +1 -0
- package/lib/errors.d.ts.map +1 -0
- package/lib/errors.js +1 -0
- package/lib/errors.js.map +1 -1
- package/lib/ethConversion.d.ts.map +1 -0
- package/lib/fetch.d.ts.map +1 -0
- package/lib/fetch.js +3 -0
- package/lib/fetch.js.map +1 -1
- package/lib/format.d.ts.map +1 -0
- package/lib/index.d.ts +8 -8
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +6 -6
- package/lib/index.js.map +1 -1
- package/lib/iterator.d.ts.map +1 -0
- package/lib/logger.d.ts.map +1 -0
- package/lib/map.d.ts.map +1 -0
- package/lib/map.js +6 -7
- package/lib/map.js.map +1 -1
- package/lib/math.d.ts.map +1 -0
- package/lib/metrics.d.ts.map +1 -0
- package/lib/notNullish.d.ts.map +1 -0
- package/lib/objects.d.ts.map +1 -0
- package/lib/promise.d.ts.map +1 -0
- package/lib/retry.d.ts.map +1 -0
- package/lib/sleep.d.ts.map +1 -0
- package/lib/sort.d.ts.map +1 -0
- package/lib/timeout.d.ts.map +1 -0
- package/lib/types.d.ts +6 -4
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js.map +1 -1
- package/lib/url.d.ts.map +1 -0
- package/lib/verifyMerkleBranch.d.ts.map +1 -0
- package/lib/waitFor.d.ts.map +1 -0
- package/lib/yaml/index.d.ts.map +1 -0
- package/lib/yaml/int.d.ts.map +1 -0
- package/lib/yaml/schema.d.ts.map +1 -0
- package/lib/yaml/schema.js.map +1 -1
- package/package.json +11 -8
- package/src/assert.ts +86 -0
- package/src/base64.ts +9 -0
- package/src/bytes/browser.ts +123 -0
- package/src/bytes/index.ts +29 -0
- package/src/bytes/nodejs.ts +63 -0
- package/src/bytes.ts +84 -0
- package/src/command.ts +74 -0
- package/src/diff.ts +234 -0
- package/src/err.ts +105 -0
- package/src/errors.ts +73 -0
- package/src/ethConversion.ts +12 -0
- package/src/fetch.ts +188 -0
- package/src/format.ts +119 -0
- package/src/index.ts +28 -0
- package/src/iterator.ts +10 -0
- package/src/logger.ts +20 -0
- package/src/map.ts +108 -0
- package/src/math.ts +55 -0
- package/src/metrics.ts +73 -0
- package/src/notNullish.ts +11 -0
- package/src/objects.ts +102 -0
- package/src/promise.ts +163 -0
- package/src/retry.ts +75 -0
- package/src/sleep.ts +32 -0
- package/src/sort.ts +9 -0
- package/src/timeout.ts +27 -0
- package/src/types.ts +48 -0
- package/src/url.ts +29 -0
- package/src/verifyMerkleBranch.ts +27 -0
- package/src/waitFor.ts +87 -0
- package/src/yaml/index.ts +12 -0
- package/src/yaml/int.ts +190 -0
- package/src/yaml/schema.ts +8 -0
package/src/waitFor.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import {ErrorAborted, TimeoutError} from "./errors.js";
|
|
2
|
+
|
|
3
|
+
export type WaitForOpts = {
|
|
4
|
+
/** Time in milliseconds between checking condition */
|
|
5
|
+
interval?: number;
|
|
6
|
+
/** Time in milliseconds to wait before throwing TimeoutError */
|
|
7
|
+
timeout?: number;
|
|
8
|
+
/** Abort signal to stop waiting for condition by throwing ErrorAborted */
|
|
9
|
+
signal?: AbortSignal;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Wait for a condition to be true
|
|
14
|
+
*/
|
|
15
|
+
export function waitFor(condition: () => boolean, opts: WaitForOpts = {}): Promise<void> {
|
|
16
|
+
return new Promise((resolve, reject) => {
|
|
17
|
+
const {interval = 10, timeout = Infinity, signal} = opts;
|
|
18
|
+
|
|
19
|
+
if (signal?.aborted) {
|
|
20
|
+
return reject(new ErrorAborted());
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
if (condition()) {
|
|
24
|
+
return resolve();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
let onDone: () => void = () => {};
|
|
28
|
+
|
|
29
|
+
const timeoutId = setTimeout(() => {
|
|
30
|
+
onDone();
|
|
31
|
+
reject(new TimeoutError());
|
|
32
|
+
}, timeout);
|
|
33
|
+
|
|
34
|
+
const intervalId = setInterval(() => {
|
|
35
|
+
if (condition()) {
|
|
36
|
+
onDone();
|
|
37
|
+
resolve();
|
|
38
|
+
}
|
|
39
|
+
}, interval);
|
|
40
|
+
|
|
41
|
+
const onAbort = (): void => {
|
|
42
|
+
onDone();
|
|
43
|
+
reject(new ErrorAborted());
|
|
44
|
+
};
|
|
45
|
+
if (signal) signal.addEventListener("abort", onAbort);
|
|
46
|
+
|
|
47
|
+
onDone = () => {
|
|
48
|
+
clearTimeout(timeoutId);
|
|
49
|
+
clearInterval(intervalId);
|
|
50
|
+
if (signal) signal.removeEventListener("abort", onAbort);
|
|
51
|
+
};
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface ElapsedTimeTracker {
|
|
56
|
+
(): boolean;
|
|
57
|
+
msSinceLastCall: number;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Create a tracker which keeps track of the last time a function was called
|
|
62
|
+
*
|
|
63
|
+
* @param durationMs
|
|
64
|
+
* @returns
|
|
65
|
+
*/
|
|
66
|
+
export function createElapsedTimeTracker({minElapsedTime}: {minElapsedTime: number}): ElapsedTimeTracker {
|
|
67
|
+
// Initialized with undefined as the function has not been called yet
|
|
68
|
+
let lastTimeCalled: number | undefined = undefined;
|
|
69
|
+
|
|
70
|
+
function elapsedTimeTracker(): boolean {
|
|
71
|
+
const now = Date.now();
|
|
72
|
+
const msSinceLastCall = now - (lastTimeCalled ?? 0);
|
|
73
|
+
|
|
74
|
+
if (msSinceLastCall > minElapsedTime) {
|
|
75
|
+
// Do not reset timer if called before timer elapsed
|
|
76
|
+
lastTimeCalled = now;
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return Object.assign(elapsedTimeTracker, {
|
|
83
|
+
get msSinceLastCall() {
|
|
84
|
+
return Date.now() - (lastTimeCalled ?? 0);
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import yaml from "js-yaml";
|
|
2
|
+
import {schema} from "./schema.js";
|
|
3
|
+
|
|
4
|
+
const {load, dump} = yaml;
|
|
5
|
+
|
|
6
|
+
export function loadYaml<T = Record<string, unknown>>(yaml: string): T {
|
|
7
|
+
return load(yaml, {schema}) as T;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function dumpYaml(yaml: unknown): string {
|
|
11
|
+
return dump(yaml, {schema});
|
|
12
|
+
}
|
package/src/yaml/int.ts
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
// Forked from https://github.com/nodeca/js-yaml/blob/master/lib/type/int.js
|
|
2
|
+
// Currently only supports loading ints
|
|
3
|
+
import yaml from "js-yaml";
|
|
4
|
+
|
|
5
|
+
const {Type} = yaml;
|
|
6
|
+
|
|
7
|
+
function isHexCode(c: number): boolean {
|
|
8
|
+
return (
|
|
9
|
+
// 0, 9
|
|
10
|
+
(0x30 <= c && c <= 0x39) ||
|
|
11
|
+
// A, F
|
|
12
|
+
(0x41 <= c && c <= 0x46) ||
|
|
13
|
+
// a, f
|
|
14
|
+
(0x61 <= c && c <= 0x66)
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function isOctCode(c: number): boolean {
|
|
19
|
+
return 0x30 /* 0 */ <= c && c <= 0x37 /* 7 */;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function isDecCode(c: number): boolean {
|
|
23
|
+
return 0x30 /* 0 */ <= c && c <= 0x39 /* 9 */;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function resolveYamlInteger(data: string): boolean {
|
|
27
|
+
if (data === null) return false;
|
|
28
|
+
|
|
29
|
+
const max = data.length;
|
|
30
|
+
let ch: string,
|
|
31
|
+
index = 0,
|
|
32
|
+
hasDigits = false;
|
|
33
|
+
|
|
34
|
+
if (!max) return false;
|
|
35
|
+
|
|
36
|
+
ch = data[index];
|
|
37
|
+
|
|
38
|
+
// sign
|
|
39
|
+
if (ch === "-" || ch === "+") {
|
|
40
|
+
ch = data[++index];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (ch === "0") {
|
|
44
|
+
// 0
|
|
45
|
+
if (index + 1 === max) return true;
|
|
46
|
+
ch = data[++index];
|
|
47
|
+
|
|
48
|
+
// base 2, base 8, base 16
|
|
49
|
+
|
|
50
|
+
if (ch === "b") {
|
|
51
|
+
// base 2
|
|
52
|
+
index++;
|
|
53
|
+
|
|
54
|
+
for (; index < max; index++) {
|
|
55
|
+
ch = data[index];
|
|
56
|
+
if (ch === "_") continue;
|
|
57
|
+
if (ch !== "0" && ch !== "1") return false;
|
|
58
|
+
hasDigits = true;
|
|
59
|
+
}
|
|
60
|
+
return hasDigits && ch !== "_";
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (ch === "x") {
|
|
64
|
+
// base 16
|
|
65
|
+
index++;
|
|
66
|
+
|
|
67
|
+
for (; index < max; index++) {
|
|
68
|
+
ch = data[index];
|
|
69
|
+
if (ch === "_") continue;
|
|
70
|
+
if (!isHexCode(data.charCodeAt(index))) return false;
|
|
71
|
+
hasDigits = true;
|
|
72
|
+
}
|
|
73
|
+
return hasDigits && ch !== "_";
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// base 8
|
|
77
|
+
for (; index < max; index++) {
|
|
78
|
+
ch = data[index];
|
|
79
|
+
if (ch === "_") continue;
|
|
80
|
+
if (!isOctCode(data.charCodeAt(index))) return false;
|
|
81
|
+
hasDigits = true;
|
|
82
|
+
}
|
|
83
|
+
return hasDigits && ch !== "_";
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// base 10 (except 0) or base 60
|
|
87
|
+
|
|
88
|
+
// value should not start with `_`;
|
|
89
|
+
if (ch === "_") return false;
|
|
90
|
+
|
|
91
|
+
for (; index < max; index++) {
|
|
92
|
+
ch = data[index];
|
|
93
|
+
if (ch === "_") continue;
|
|
94
|
+
if (ch === ":") break;
|
|
95
|
+
if (!isDecCode(data.charCodeAt(index))) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
hasDigits = true;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Should have digits and should not end with `_`
|
|
102
|
+
if (!hasDigits || ch === "_") return false;
|
|
103
|
+
|
|
104
|
+
// if !base60 - done;
|
|
105
|
+
if (ch !== ":") return true;
|
|
106
|
+
|
|
107
|
+
// base60 almost not used, no needs to optimize
|
|
108
|
+
return /^(:[0-5]?[0-9])+$/.test(data.slice(index));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function constructYamlInteger(data: string): bigint {
|
|
112
|
+
let value: string | bigint = data,
|
|
113
|
+
sign = 1,
|
|
114
|
+
ch: string,
|
|
115
|
+
base: number | bigint;
|
|
116
|
+
const digits: number[] = [];
|
|
117
|
+
|
|
118
|
+
if (value.indexOf("_") !== -1) {
|
|
119
|
+
value = value.replace(/_/g, "");
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
ch = value[0];
|
|
123
|
+
|
|
124
|
+
if (ch === "-" || ch === "+") {
|
|
125
|
+
if (ch === "-") sign = -1;
|
|
126
|
+
value = value.slice(1);
|
|
127
|
+
ch = value[0];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (value === "0") return BigInt(0);
|
|
131
|
+
|
|
132
|
+
if (ch === "0") {
|
|
133
|
+
if (value[1] === "b" || value[1] === "x") return BigInt(value) * BigInt(sign);
|
|
134
|
+
return BigInt("0o" + value) * BigInt(sign);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (value.indexOf(":") !== -1) {
|
|
138
|
+
for (const v of value.split(":")) {
|
|
139
|
+
digits.unshift(parseInt(v, 10));
|
|
140
|
+
}
|
|
141
|
+
value = BigInt(0);
|
|
142
|
+
base = BigInt(1);
|
|
143
|
+
|
|
144
|
+
for (const d of digits) {
|
|
145
|
+
value = (BigInt(value) + BigInt(base)) * BigInt(d);
|
|
146
|
+
base = BigInt(base) * BigInt(60);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return value * BigInt(sign);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return BigInt(value) * BigInt(sign);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function isInteger(object: unknown): boolean {
|
|
156
|
+
return typeof object === "bigint" || typeof object === "number";
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export const intType = new Type("tag:yaml.org,2002:int", {
|
|
160
|
+
kind: "scalar",
|
|
161
|
+
resolve: resolveYamlInteger,
|
|
162
|
+
construct: constructYamlInteger,
|
|
163
|
+
predicate: isInteger,
|
|
164
|
+
instanceOf: BigInt,
|
|
165
|
+
represent: {
|
|
166
|
+
// @ts-expect-error
|
|
167
|
+
binary: function binary(obj: number) {
|
|
168
|
+
return obj >= 0 ? "0b" + obj.toString(2) : "-0b" + obj.toString(2).slice(1);
|
|
169
|
+
},
|
|
170
|
+
// @ts-expect-error
|
|
171
|
+
octal: function octal(obj: number) {
|
|
172
|
+
return obj >= 0 ? "0" + obj.toString(8) : "-0" + obj.toString(8).slice(1);
|
|
173
|
+
},
|
|
174
|
+
// @ts-expect-error
|
|
175
|
+
decimal: function decimal(obj: number) {
|
|
176
|
+
return obj.toString(10);
|
|
177
|
+
},
|
|
178
|
+
// @ts-expect-error
|
|
179
|
+
hexadecimal: function hexadecimal(obj: number) {
|
|
180
|
+
return obj >= 0 ? "0x" + obj.toString(16).toUpperCase() : "-0x" + obj.toString(16).toUpperCase().slice(1);
|
|
181
|
+
},
|
|
182
|
+
},
|
|
183
|
+
defaultStyle: "decimal",
|
|
184
|
+
styleAliases: {
|
|
185
|
+
binary: [2, "bin"],
|
|
186
|
+
octal: [8, "oct"],
|
|
187
|
+
decimal: [10, "dec"],
|
|
188
|
+
hexadecimal: [16, "hex"],
|
|
189
|
+
},
|
|
190
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import yml, {FAILSAFE_SCHEMA, Type} from "js-yaml";
|
|
2
|
+
import {intType} from "./int.js";
|
|
3
|
+
|
|
4
|
+
export const schema = FAILSAFE_SCHEMA.extend({
|
|
5
|
+
// @ts-expect-error
|
|
6
|
+
implicit: [yml.types.null as Type, yml.types.bool as Type, intType, yml.types.float as Type],
|
|
7
|
+
explicit: [],
|
|
8
|
+
});
|