cafe-utility 32.2.0 → 33.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/index.d.ts +12 -1
- package/index.js +33 -8
- package/module.mjs +1 -0
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -57,6 +57,7 @@ declare function getFirstDeep(object: CafeObject, paths: string[], fallbackToAny
|
|
|
57
57
|
declare function forever(callable: (() => Promise<void>) | (() => void), millis: number, log?: (message: string, metadata: object) => void): Promise<never>
|
|
58
58
|
declare function runAndSetInterval(callable: () => void, millis: number): () => void
|
|
59
59
|
declare function whereAmI(): 'browser' | 'node'
|
|
60
|
+
declare function withRetries<T>(callable: () => Promise<T>, allowedFailures: number, delayMillisFirst: number, delayMillisLast: number, log?: (message: string, metadata: object) => void, onFailure?: () => void): Promise<T>
|
|
60
61
|
declare function asMegabytes(number: number): number
|
|
61
62
|
declare function convertBytes(bytes: number, divisor?: number): string
|
|
62
63
|
declare function hexToRgb(hex: string): [number, number, number]
|
|
@@ -109,8 +110,10 @@ declare function asHexString(
|
|
|
109
110
|
options?: {
|
|
110
111
|
name?: string
|
|
111
112
|
byteLength?: number
|
|
113
|
+
uneven?: boolean
|
|
114
|
+
strictPrefix?: boolean
|
|
112
115
|
}
|
|
113
|
-
): string
|
|
116
|
+
): `0x${string}`
|
|
114
117
|
declare function asSafeString(
|
|
115
118
|
string: any,
|
|
116
119
|
options?: {
|
|
@@ -744,6 +747,13 @@ export declare class TrieRouter<Q, S> {
|
|
|
744
747
|
insert(pathSegments: string[], handler: RouterFn<Q, S>): void
|
|
745
748
|
handle(pathSegments: string[], request: Q, response: S, context: Map<string, string>): Promise<boolean>
|
|
746
749
|
}
|
|
750
|
+
export declare class RollingValueProvider<T> {
|
|
751
|
+
private values
|
|
752
|
+
private index
|
|
753
|
+
constructor(values: T[])
|
|
754
|
+
current(): T
|
|
755
|
+
next(): T
|
|
756
|
+
}
|
|
747
757
|
export declare const Binary: {
|
|
748
758
|
hexToUint8Array: typeof hexToUint8Array
|
|
749
759
|
uint8ArrayToHex: typeof uint8ArrayToHex
|
|
@@ -865,6 +875,7 @@ export declare const System: {
|
|
|
865
875
|
expandError: typeof expandError
|
|
866
876
|
runAndSetInterval: typeof runAndSetInterval
|
|
867
877
|
whereAmI: typeof whereAmI
|
|
878
|
+
withRetries: typeof withRetries
|
|
868
879
|
}
|
|
869
880
|
export declare const Numbers: {
|
|
870
881
|
make: typeof makeNumber
|
package/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
var _a
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: !0 }), (exports.Vector = exports.Cache = exports.Assertions = exports.Strings = exports.Types = exports.Objects = exports.Dates = exports.Promises = exports.Numbers = exports.System = exports.Arrays = exports.Random = exports.Elliptic = exports.Binary = exports.TrieRouter = exports.AsyncQueue = exports.PubSubChannel = exports.FixedPointNumber = exports.MerkleTree = exports.Chunk = exports.Uint8ArrayWriter = exports.Uint8ArrayReader = exports.AsyncLazy = exports.Lazy = exports.Optional = void 0)
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: !0 }), (exports.Vector = exports.Cache = exports.Assertions = exports.Strings = exports.Types = exports.Objects = exports.Dates = exports.Promises = exports.Numbers = exports.System = exports.Arrays = exports.Random = exports.Elliptic = exports.Binary = exports.RollingValueProvider = exports.TrieRouter = exports.AsyncQueue = exports.PubSubChannel = exports.FixedPointNumber = exports.MerkleTree = exports.Chunk = exports.Uint8ArrayWriter = exports.Uint8ArrayReader = exports.AsyncLazy = exports.Lazy = exports.Optional = void 0)
|
|
4
4
|
async function invertPromise(n) {
|
|
5
5
|
return new Promise((e, t) => n.then(t, e))
|
|
6
6
|
}
|
|
@@ -231,6 +231,18 @@ function whereAmI() {
|
|
|
231
231
|
const n = globalThis.process
|
|
232
232
|
return n ? (n.browser === !0 ? 'browser' : 'node') : 'browser'
|
|
233
233
|
}
|
|
234
|
+
async function withRetries(n, e, t, r, o, i) {
|
|
235
|
+
let u = null
|
|
236
|
+
for (let s = 0; s <= e; s++)
|
|
237
|
+
try {
|
|
238
|
+
return await n()
|
|
239
|
+
} catch (c) {
|
|
240
|
+
if (((u = c), s === e)) break
|
|
241
|
+
const f = t + (r - t) * (s / (e - 1))
|
|
242
|
+
o && o('Error in withRetries, retrying', { attempt: s + 1, allowedFailures: e, delayMillis: f, error: c }), i && i(), await sleepMillis(f)
|
|
243
|
+
}
|
|
244
|
+
throw u
|
|
245
|
+
}
|
|
234
246
|
function asMegabytes(n) {
|
|
235
247
|
return n / 1024 / 1024
|
|
236
248
|
}
|
|
@@ -373,10 +385,11 @@ function asString(n, e) {
|
|
|
373
385
|
}
|
|
374
386
|
function asHexString(n, e) {
|
|
375
387
|
if (!isHexString(n)) throw new TypeError(`Expected hex string${e?.name ? ` for ${e.name}` : ''}, got: ` + n)
|
|
388
|
+
if (e?.strictPrefix && !n.startsWith('0x')) throw new TypeError(`Expected hex string with 0x prefix${e?.name ? ` for ${e.name}` : ''}, got: ` + n)
|
|
376
389
|
const t = n.replace(/^0x/, '')
|
|
377
|
-
if (t.length % 2 !== 0) throw RangeError(`Expected even number of hex digits${e?.name ? ` for ${e.name}` : ''}; got: ` + n)
|
|
390
|
+
if (t.length % 2 !== 0 && !e?.uneven) throw RangeError(`Expected even number of hex digits${e?.name ? ` for ${e.name}` : ''}; got: ` + n)
|
|
378
391
|
if (e && e.byteLength && t.length !== e.byteLength * 2) throw RangeError(`Expected hex string${e?.name ? ` for ${e.name}` : ''} of byte length ${e.byteLength}; got: ` + t)
|
|
379
|
-
return t
|
|
392
|
+
return `0x${t}`
|
|
380
393
|
}
|
|
381
394
|
function asSafeString(n, e) {
|
|
382
395
|
if (
|
|
@@ -2083,8 +2096,8 @@ function keccakPermutate(n) {
|
|
|
2083
2096
|
I = (n[7] << 28) | (n[6] >>> 4),
|
|
2084
2097
|
C = (n[8] << 27) | (n[9] >>> 5),
|
|
2085
2098
|
D = (n[9] << 27) | (n[8] >>> 5),
|
|
2086
|
-
|
|
2087
|
-
|
|
2099
|
+
P = (n[11] << 4) | (n[10] >>> 28),
|
|
2100
|
+
B = (n[10] << 4) | (n[11] >>> 28),
|
|
2088
2101
|
v = (n[13] << 12) | (n[12] >>> 20),
|
|
2089
2102
|
U = (n[12] << 12) | (n[13] >>> 20),
|
|
2090
2103
|
L = (n[14] << 6) | (n[15] >>> 26),
|
|
@@ -2123,7 +2136,7 @@ function keccakPermutate(n) {
|
|
|
2123
2136
|
wn = (n[46] << 24) | (n[47] >>> 8),
|
|
2124
2137
|
xn = (n[48] << 14) | (n[49] >>> 18),
|
|
2125
2138
|
yn = (n[49] << 14) | (n[48] >>> 18)
|
|
2126
|
-
;(n[0] = E ^ (~v & K)), (n[1] = M ^ (~U & Z)), (n[2] = v ^ (~K & un)), (n[3] = U ^ (~Z & cn)), (n[4] = K ^ (~un & xn)), (n[5] = Z ^ (~cn & yn)), (n[6] = un ^ (~xn & E)), (n[7] = cn ^ (~yn & M)), (n[8] = xn ^ (~E & v)), (n[9] = yn ^ (~M & U)), (n[10] = R ^ (~F & q)), (n[11] = I ^ (~W & H)), (n[12] = F ^ (~q & en)), (n[13] = W ^ (~H & tn)), (n[14] = q ^ (~en & pn)), (n[15] = H ^ (~tn & mn)), (n[16] = en ^ (~pn & R)), (n[17] = tn ^ (~mn & I)), (n[18] = pn ^ (~R & F)), (n[19] = mn ^ (~I & W)), (n[20] = O ^ (~L & Q)), (n[21] = k ^ (~N & G)), (n[22] = L ^ (~Q & sn)), (n[23] = N ^ (~G & fn)), (n[24] = Q ^ (~sn & ln)), (n[25] = G ^ (~fn & an)), (n[26] = sn ^ (~ln & O)), (n[27] = fn ^ (~an & k)), (n[28] = ln ^ (~O & L)), (n[29] = an ^ (~k & N)), (n[30] = C ^ (~
|
|
2139
|
+
;(n[0] = E ^ (~v & K)), (n[1] = M ^ (~U & Z)), (n[2] = v ^ (~K & un)), (n[3] = U ^ (~Z & cn)), (n[4] = K ^ (~un & xn)), (n[5] = Z ^ (~cn & yn)), (n[6] = un ^ (~xn & E)), (n[7] = cn ^ (~yn & M)), (n[8] = xn ^ (~E & v)), (n[9] = yn ^ (~M & U)), (n[10] = R ^ (~F & q)), (n[11] = I ^ (~W & H)), (n[12] = F ^ (~q & en)), (n[13] = W ^ (~H & tn)), (n[14] = q ^ (~en & pn)), (n[15] = H ^ (~tn & mn)), (n[16] = en ^ (~pn & R)), (n[17] = tn ^ (~mn & I)), (n[18] = pn ^ (~R & F)), (n[19] = mn ^ (~I & W)), (n[20] = O ^ (~L & Q)), (n[21] = k ^ (~N & G)), (n[22] = L ^ (~Q & sn)), (n[23] = N ^ (~G & fn)), (n[24] = Q ^ (~sn & ln)), (n[25] = G ^ (~fn & an)), (n[26] = sn ^ (~ln & O)), (n[27] = fn ^ (~an & k)), (n[28] = ln ^ (~O & L)), (n[29] = an ^ (~k & N)), (n[30] = C ^ (~P & V)), (n[31] = D ^ (~B & J)), (n[32] = P ^ (~V & rn)), (n[33] = B ^ (~J & on)), (n[34] = V ^ (~rn & gn)), (n[35] = J ^ (~on & wn)), (n[36] = rn ^ (~gn & C)), (n[37] = on ^ (~wn & D)), (n[38] = gn ^ (~C & P)), (n[39] = wn ^ (~D & B)), (n[40] = T ^ (~j & Y)), (n[41] = S ^ (~z & _)), (n[42] = j ^ (~Y & X)), (n[43] = z ^ (~_ & nn)), (n[44] = Y ^ (~X & hn)), (n[45] = _ ^ (~nn & dn)), (n[46] = X ^ (~hn & T)), (n[47] = nn ^ (~dn & S)), (n[48] = hn ^ (~T & j)), (n[49] = dn ^ (~S & z)), (n[0] ^= IOTA_CONSTANTS[e * 2]), (n[1] ^= IOTA_CONSTANTS[e * 2 + 1])
|
|
2127
2140
|
}
|
|
2128
2141
|
}
|
|
2129
2142
|
function bytesToNumbers(n) {
|
|
@@ -2717,12 +2730,24 @@ class TrieRouter {
|
|
|
2717
2730
|
return c ? (o.set('wildcard', e.join('/')), c.handler ? (await c.handler(t, r, o), !0) : !1) : !1
|
|
2718
2731
|
}
|
|
2719
2732
|
}
|
|
2720
|
-
|
|
2733
|
+
exports.TrieRouter = TrieRouter
|
|
2734
|
+
class RollingValueProvider {
|
|
2735
|
+
constructor(e) {
|
|
2736
|
+
;(this.index = 0), (this.values = e)
|
|
2737
|
+
}
|
|
2738
|
+
current() {
|
|
2739
|
+
return this.values[this.index]
|
|
2740
|
+
}
|
|
2741
|
+
next() {
|
|
2742
|
+
return (this.index = (this.index + 1) % this.values.length), this.values[this.index]
|
|
2743
|
+
}
|
|
2744
|
+
}
|
|
2745
|
+
;(exports.RollingValueProvider = RollingValueProvider),
|
|
2721
2746
|
(exports.Binary = { hexToUint8Array, uint8ArrayToHex, binaryToUint8Array, uint8ArrayToBinary, base64ToUint8Array, uint8ArrayToBase64, base32ToUint8Array, uint8ArrayToBase32, log2Reduce, partition, concatBytes, numberToUint8, uint8ToNumber, numberToUint16, uint16ToNumber, numberToUint32, uint32ToNumber, numberToUint64, uint64ToNumber, numberToUint256, uint256ToNumber, sliceBytes, keccak256, sha3_256, proximity, commonPrefix, setBit, getBit, indexOf: binaryIndexOf, equals: binaryEquals, padStart: binaryPadStart, padStartToMultiple: binaryPadStartToMultiple, padEnd: binaryPadEnd, padEndToMultiple: binaryPadEndToMultiple, xorCypher, isUtf8 }),
|
|
2722
2747
|
(exports.Elliptic = { privateKeyToPublicKey, compressPublicKey, publicKeyFromCompressed, publicKeyToAddress, signMessage, signHash, verifySignature, recoverPublicKey, checksumEncode }),
|
|
2723
2748
|
(exports.Random = { intBetween, floatBetween, chance, signed: signedRandom, makeSeededRng, point: randomPoint, procs }),
|
|
2724
2749
|
(exports.Arrays = { countUnique, makeUnique, splitBySize, splitByCount, index: indexArray, indexCollection: indexArrayToCollection, onlyOrThrow, onlyOrNull, firstOrThrow, firstOrNull, shuffle, initialize: initializeArray, initialize2D: initialize2DArray, rotate2D: rotate2DArray, containsShape, glue, pluck, pick, pickMany, pickManyUnique, pickWeighted, pickRandomIndices, pickGuaranteed, last, pipe, makePipe, sortWeighted, pushAll, unshiftAll, filterAndRemove, merge: mergeArrays, empty, pushToBucket, unshiftAndLimit, atRolling, group, createOscillator, organiseWithLimits, tickPlaybook, getArgument, getBooleanArgument, getNumberArgument, requireStringArgument, requireNumberArgument, bringToFront, bringToFrontInPlace, findInstance, filterInstances, interleave, toggle, createHierarchy, multicall, maxBy }),
|
|
2725
|
-
(exports.System = { sleepMillis, forever, scheduleMany, waitFor, expandError, runAndSetInterval, whereAmI }),
|
|
2750
|
+
(exports.System = { sleepMillis, forever, scheduleMany, waitFor, expandError, runAndSetInterval, whereAmI, withRetries }),
|
|
2726
2751
|
(exports.Numbers = { make: makeNumber, sum, average, median, getDistanceFromMidpoint, clamp, range, interpolate, createSequence, increment, decrement, format: formatNumber, fromDecimals, makeStorage, asMegabytes, convertBytes, hexToRgb, rgbToHex, haversineDistanceToMeters, roundToNearest, formatDistance, triangularNumber, searchFloat, binomialSample }),
|
|
2727
2752
|
(exports.Promises = { raceFulfilled, invert: invertPromise, runInParallelBatches }),
|
|
2728
2753
|
(exports.Dates = { getTimestamp, getTimeDelta, secondsToHumanTime, countCycles, isoDate, throttle, timeSince, dateTimeSlug, unixTimestamp, fromUtcString, fromMillis, getProgress, humanizeTime, humanizeProgress, createTimeDigits, mapDayNumber, getDayInfoFromDate, getDayInfoFromDateTimeString, seconds, minutes, hours, days, make: makeDate, normalizeTime, absoluteDays }),
|
package/module.mjs
CHANGED
|
@@ -26,3 +26,4 @@ export const AsyncQueue = utility.AsyncQueue
|
|
|
26
26
|
export const Uint8ArrayReader = utility.Uint8ArrayReader
|
|
27
27
|
export const Uint8ArrayWriter = utility.Uint8ArrayWriter
|
|
28
28
|
export const TrieRouter = utility.TrieRouter
|
|
29
|
+
export const RollingValueProvider = utility.RollingValueProvider
|