@naturalcycles/js-lib 14.83.1 → 14.85.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/dist/decorators/asyncMemo.decorator.d.ts +32 -9
- package/dist/decorators/asyncMemo.decorator.js +35 -33
- package/dist/decorators/decorator.util.d.ts +1 -1
- package/dist/decorators/decorator.util.js +2 -2
- package/dist/decorators/logMethod.decorator.d.ts +6 -4
- package/dist/decorators/logMethod.decorator.js +3 -3
- package/dist/decorators/memo.decorator.d.ts +27 -30
- package/dist/decorators/memo.decorator.js +31 -56
- package/dist/decorators/memo.util.d.ts +6 -6
- package/dist/decorators/memoFn.d.ts +5 -0
- package/dist/decorators/memoFn.js +21 -40
- package/dist/decorators/memoFnAsync.d.ts +10 -0
- package/dist/decorators/memoFnAsync.js +66 -0
- package/dist/decorators/memoSimple.decorator.d.ts +1 -1
- package/dist/decorators/memoSimple.decorator.js +3 -3
- package/dist/error/assert.d.ts +4 -4
- package/dist/error/error.model.d.ts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/promise/pRetry.d.ts +5 -1
- package/dist/promise/pRetry.js +6 -1
- package/dist/promise/pTimeout.d.ts +5 -0
- package/dist/promise/pTimeout.js +5 -1
- package/dist-esm/decorators/asyncMemo.decorator.js +36 -46
- package/dist-esm/decorators/decorator.util.js +2 -2
- package/dist-esm/decorators/logMethod.decorator.js +3 -3
- package/dist-esm/decorators/memo.decorator.js +30 -56
- package/dist-esm/decorators/memoFn.js +21 -40
- package/dist-esm/decorators/memoFnAsync.js +62 -0
- package/dist-esm/decorators/memoSimple.decorator.js +3 -3
- package/dist-esm/index.js +1 -0
- package/dist-esm/promise/pRetry.js +3 -1
- package/dist-esm/promise/pTimeout.js +2 -1
- package/package.json +1 -1
- package/src/decorators/asyncMemo.decorator.ts +80 -60
- package/src/decorators/decorator.util.ts +2 -2
- package/src/decorators/logMethod.decorator.ts +16 -7
- package/src/decorators/memo.decorator.ts +51 -103
- package/src/decorators/memo.util.ts +7 -7
- package/src/decorators/memoFn.ts +21 -52
- package/src/decorators/memoFnAsync.ts +87 -0
- package/src/decorators/memoSimple.decorator.ts +4 -4
- package/src/error/assert.ts +4 -4
- package/src/error/error.model.ts +2 -0
- package/src/index.ts +1 -0
- package/src/promise/pRetry.ts +12 -2
- package/src/promise/pTimeout.ts +14 -1
package/src/decorators/memoFn.ts
CHANGED
|
@@ -7,6 +7,11 @@ export interface MemoizedFunction {
|
|
|
7
7
|
cache: MemoCache
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
+
/**
|
|
11
|
+
* Only supports Sync functions.
|
|
12
|
+
* To support Async functions - use _memoFnAsync.
|
|
13
|
+
* Technically, you can use it with Async functions, but it'll return the Promise without awaiting it.
|
|
14
|
+
*/
|
|
10
15
|
export function _memoFn<T extends (...args: any[]) => any>(
|
|
11
16
|
fn: T,
|
|
12
17
|
opt: MemoOptions = {},
|
|
@@ -14,16 +19,14 @@ export function _memoFn<T extends (...args: any[]) => any>(
|
|
|
14
19
|
const {
|
|
15
20
|
logHit = false,
|
|
16
21
|
logMiss = false,
|
|
17
|
-
|
|
22
|
+
logArgs = true,
|
|
18
23
|
logger = console,
|
|
19
|
-
|
|
20
|
-
noCacheResolved = false,
|
|
24
|
+
cacheErrors = false,
|
|
21
25
|
cacheFactory = () => new MapMemoCache(),
|
|
22
26
|
cacheKeyFn = jsonMemoSerializer,
|
|
23
27
|
} = opt
|
|
24
28
|
|
|
25
29
|
const cache = cacheFactory()
|
|
26
|
-
const awaitPromise = Boolean(noCacheRejected || noCacheResolved)
|
|
27
30
|
const fnName = fn.name
|
|
28
31
|
|
|
29
32
|
const memoizedFn = function (this: any, ...args: any[]): T {
|
|
@@ -32,68 +35,34 @@ export function _memoFn<T extends (...args: any[]) => any>(
|
|
|
32
35
|
|
|
33
36
|
if (cache.has(cacheKey)) {
|
|
34
37
|
if (logHit) {
|
|
35
|
-
logger.log(`${fnName}(${_getArgsSignature(args,
|
|
38
|
+
logger.log(`${fnName}(${_getArgsSignature(args, logArgs)}) memoFn hit`)
|
|
36
39
|
}
|
|
37
40
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
if (awaitPromise) {
|
|
41
|
-
return res instanceof Error ? (Promise.reject(res) as any) : Promise.resolve(res)
|
|
42
|
-
} else {
|
|
43
|
-
return res
|
|
44
|
-
}
|
|
41
|
+
return cache.get(cacheKey)
|
|
45
42
|
}
|
|
46
43
|
|
|
47
44
|
const started = Date.now()
|
|
48
45
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
if (awaitPromise) {
|
|
52
|
-
return (res as Promise<any>)
|
|
53
|
-
.then(res => {
|
|
54
|
-
// console.log('RESOLVED', res)
|
|
55
|
-
if (logMiss) {
|
|
56
|
-
logger.log(
|
|
57
|
-
`${fnName}(${_getArgsSignature(args, noLogArgs)}) memoFn miss resolved (${_since(
|
|
58
|
-
started,
|
|
59
|
-
)})`,
|
|
60
|
-
)
|
|
61
|
-
}
|
|
46
|
+
let value: any
|
|
62
47
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
48
|
+
try {
|
|
49
|
+
value = fn.apply(ctx, args)
|
|
66
50
|
|
|
67
|
-
|
|
68
|
-
})
|
|
69
|
-
.catch(err => {
|
|
70
|
-
// console.log('REJECTED', err)
|
|
71
|
-
if (logMiss) {
|
|
72
|
-
logger.log(
|
|
73
|
-
`${fnName}(${_getArgsSignature(args, noLogArgs)}) memoFn miss rejected (${_since(
|
|
74
|
-
started,
|
|
75
|
-
)})`,
|
|
76
|
-
)
|
|
77
|
-
}
|
|
51
|
+
cache.set(cacheKey, value)
|
|
78
52
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
}
|
|
53
|
+
return value
|
|
54
|
+
} catch (err) {
|
|
55
|
+
if (cacheErrors) {
|
|
56
|
+
cache.set(cacheKey, err)
|
|
57
|
+
}
|
|
85
58
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
} else {
|
|
59
|
+
throw err
|
|
60
|
+
} finally {
|
|
89
61
|
if (logMiss) {
|
|
90
62
|
logger.log(
|
|
91
|
-
`${fnName}(${_getArgsSignature(args,
|
|
63
|
+
`${fnName}(${_getArgsSignature(args, logArgs)}) memoFn miss (${_since(started)})`,
|
|
92
64
|
)
|
|
93
65
|
}
|
|
94
|
-
|
|
95
|
-
cache.set(cacheKey, res)
|
|
96
|
-
return res
|
|
97
66
|
}
|
|
98
67
|
}
|
|
99
68
|
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { _since } from '../time/time.util'
|
|
2
|
+
import { AsyncMemoOptions } from './asyncMemo.decorator'
|
|
3
|
+
import { _getArgsSignature } from './decorator.util'
|
|
4
|
+
import { AsyncMemoCache, jsonMemoSerializer, MapMemoCache } from './memo.util'
|
|
5
|
+
|
|
6
|
+
export interface MemoizedAsyncFunction {
|
|
7
|
+
cache: AsyncMemoCache
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Only supports Sync functions.
|
|
12
|
+
* To support Async functions - use _memoFnAsync
|
|
13
|
+
*/
|
|
14
|
+
export function _memoFnAsync<T extends (...args: any[]) => Promise<any>>(
|
|
15
|
+
fn: T,
|
|
16
|
+
opt: AsyncMemoOptions = {},
|
|
17
|
+
): T & MemoizedAsyncFunction {
|
|
18
|
+
const {
|
|
19
|
+
logHit = false,
|
|
20
|
+
logMiss = false,
|
|
21
|
+
logArgs = true,
|
|
22
|
+
logger = console,
|
|
23
|
+
cacheRejections = false,
|
|
24
|
+
cacheFactory = () => new MapMemoCache(),
|
|
25
|
+
cacheKeyFn = jsonMemoSerializer,
|
|
26
|
+
} = opt
|
|
27
|
+
|
|
28
|
+
const cache = cacheFactory()
|
|
29
|
+
const fnName = fn.name
|
|
30
|
+
|
|
31
|
+
const memoizedFn = async function (this: any, ...args: any[]): Promise<any> {
|
|
32
|
+
const ctx = this
|
|
33
|
+
const cacheKey = cacheKeyFn(args)
|
|
34
|
+
let value: any
|
|
35
|
+
|
|
36
|
+
try {
|
|
37
|
+
value = await cache.get(cacheKey)
|
|
38
|
+
} catch (err) {
|
|
39
|
+
logger.error(err)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (value !== undefined) {
|
|
43
|
+
if (logHit) {
|
|
44
|
+
logger.log(`${fnName}(${_getArgsSignature(args, logArgs)}) memoFnAsync hit`)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return value
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const started = Date.now()
|
|
51
|
+
|
|
52
|
+
try {
|
|
53
|
+
value = await fn.apply(ctx, args)
|
|
54
|
+
|
|
55
|
+
void (async () => {
|
|
56
|
+
try {
|
|
57
|
+
await cache.set(cacheKey, value)
|
|
58
|
+
} catch (err) {
|
|
59
|
+
logger.error(err)
|
|
60
|
+
}
|
|
61
|
+
})()
|
|
62
|
+
|
|
63
|
+
return value
|
|
64
|
+
} catch (err) {
|
|
65
|
+
if (cacheRejections) {
|
|
66
|
+
void (async () => {
|
|
67
|
+
try {
|
|
68
|
+
await cache.set(cacheKey, err)
|
|
69
|
+
} catch (err) {
|
|
70
|
+
logger.error(err)
|
|
71
|
+
}
|
|
72
|
+
})()
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
throw err
|
|
76
|
+
} finally {
|
|
77
|
+
if (logMiss) {
|
|
78
|
+
logger.log(
|
|
79
|
+
`${fnName}(${_getArgsSignature(args, logArgs)}) memoFnAsync miss (${_since(started)})`,
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
Object.assign(memoizedFn, { cache })
|
|
86
|
+
return memoizedFn as T & MemoizedAsyncFunction
|
|
87
|
+
}
|
|
@@ -18,7 +18,7 @@ import { jsonMemoSerializer, MapMemoCache, MemoCache } from './memo.util'
|
|
|
18
18
|
export interface MemoOpts {
|
|
19
19
|
logHit?: boolean
|
|
20
20
|
logMiss?: boolean
|
|
21
|
-
|
|
21
|
+
logArgs?: boolean
|
|
22
22
|
logger?: CommonLogger
|
|
23
23
|
}
|
|
24
24
|
|
|
@@ -57,7 +57,7 @@ export const memoSimple =
|
|
|
57
57
|
*/
|
|
58
58
|
const cache: MemoCache = new MapMemoCache()
|
|
59
59
|
|
|
60
|
-
const { logHit, logMiss,
|
|
60
|
+
const { logHit, logMiss, logArgs = true, logger = console } = opt
|
|
61
61
|
const keyStr = String(key)
|
|
62
62
|
const methodSignature = _getTargetMethodSignature(target, keyStr)
|
|
63
63
|
|
|
@@ -67,7 +67,7 @@ export const memoSimple =
|
|
|
67
67
|
|
|
68
68
|
if (cache.has(cacheKey)) {
|
|
69
69
|
if (logHit) {
|
|
70
|
-
logger.log(`${methodSignature}(${_getArgsSignature(args,
|
|
70
|
+
logger.log(`${methodSignature}(${_getArgsSignature(args, logArgs)}) @memo hit`)
|
|
71
71
|
}
|
|
72
72
|
return cache.get(cacheKey)
|
|
73
73
|
}
|
|
@@ -78,7 +78,7 @@ export const memoSimple =
|
|
|
78
78
|
|
|
79
79
|
if (logMiss) {
|
|
80
80
|
logger.log(
|
|
81
|
-
`${methodSignature}(${_getArgsSignature(args,
|
|
81
|
+
`${methodSignature}(${_getArgsSignature(args, logArgs)}) @memo miss (${
|
|
82
82
|
Date.now() - d
|
|
83
83
|
} ms)`,
|
|
84
84
|
)
|
package/src/error/assert.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ErrorData,
|
|
1
|
+
import { ErrorData, _deepEquals, _stringifyAny } from '..'
|
|
2
2
|
import { AppError } from './app.error'
|
|
3
3
|
|
|
4
4
|
/**
|
|
@@ -19,7 +19,7 @@ import { AppError } from './app.error'
|
|
|
19
19
|
export function _assert(
|
|
20
20
|
condition: any, // will be evaluated as Boolean
|
|
21
21
|
message?: string,
|
|
22
|
-
errorData?:
|
|
22
|
+
errorData?: ErrorData,
|
|
23
23
|
): asserts condition {
|
|
24
24
|
if (!condition) {
|
|
25
25
|
throw new AssertionError(message || 'see stacktrace', {
|
|
@@ -39,7 +39,7 @@ export function _assertEquals<T>(
|
|
|
39
39
|
actual: any,
|
|
40
40
|
expected: T,
|
|
41
41
|
message?: string,
|
|
42
|
-
errorData?:
|
|
42
|
+
errorData?: ErrorData,
|
|
43
43
|
): asserts actual is T {
|
|
44
44
|
if (actual !== expected) {
|
|
45
45
|
const msg = [
|
|
@@ -67,7 +67,7 @@ export function _assertDeepEquals<T>(
|
|
|
67
67
|
actual: any,
|
|
68
68
|
expected: T,
|
|
69
69
|
message?: string,
|
|
70
|
-
errorData?:
|
|
70
|
+
errorData?: ErrorData,
|
|
71
71
|
): asserts actual is T {
|
|
72
72
|
if (!_deepEquals(actual, expected)) {
|
|
73
73
|
const msg = [
|
package/src/error/error.model.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -15,6 +15,7 @@ export * from './decorators/memo.decorator'
|
|
|
15
15
|
export * from './decorators/asyncMemo.decorator'
|
|
16
16
|
import { MemoCache, AsyncMemoCache } from './decorators/memo.util'
|
|
17
17
|
export * from './decorators/memoFn'
|
|
18
|
+
export * from './decorators/memoFnAsync'
|
|
18
19
|
export * from './decorators/retry.decorator'
|
|
19
20
|
export * from './decorators/timeout.decorator'
|
|
20
21
|
export * from './error/app.error'
|
package/src/promise/pRetry.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { _since, _stringifyAny, AnyFunction, CommonLogger } from '..'
|
|
1
|
+
import { _since, _stringifyAny, AnyFunction, AppError, CommonLogger, ErrorData } from '..'
|
|
2
2
|
import { TimeoutError } from './pTimeout'
|
|
3
3
|
|
|
4
4
|
export interface PRetryOptions {
|
|
@@ -91,6 +91,11 @@ export interface PRetryOptions {
|
|
|
91
91
|
* @experimental
|
|
92
92
|
*/
|
|
93
93
|
keepStackTrace?: boolean
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Will be merged with `err.data` object.
|
|
97
|
+
*/
|
|
98
|
+
errorData?: ErrorData
|
|
94
99
|
}
|
|
95
100
|
|
|
96
101
|
/**
|
|
@@ -139,7 +144,7 @@ export async function pRetry<T>(
|
|
|
139
144
|
return await new Promise((resolve, reject) => {
|
|
140
145
|
const rejectWithTimeout = () => {
|
|
141
146
|
timedOut = true // to prevent more tries
|
|
142
|
-
const err = new TimeoutError(`"${fname}" timed out after ${timeout} ms
|
|
147
|
+
const err = new TimeoutError(`"${fname}" timed out after ${timeout} ms`, opt.errorData)
|
|
143
148
|
if (fakeError) {
|
|
144
149
|
// keep original stack
|
|
145
150
|
err.stack = fakeError.stack!.replace('Error: RetryError', 'TimeoutError')
|
|
@@ -199,6 +204,11 @@ export async function pRetry<T>(
|
|
|
199
204
|
})
|
|
200
205
|
}
|
|
201
206
|
|
|
207
|
+
;(err as AppError).data = {
|
|
208
|
+
...(err as AppError).data,
|
|
209
|
+
...opt.errorData,
|
|
210
|
+
}
|
|
211
|
+
|
|
202
212
|
reject(err)
|
|
203
213
|
} else {
|
|
204
214
|
// Retry after delay
|
package/src/promise/pTimeout.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AppError } from '../error/app.error'
|
|
2
|
+
import { ErrorData } from '../error/error.model'
|
|
2
3
|
import { AnyFunction } from '../types'
|
|
3
4
|
|
|
4
5
|
export class TimeoutError extends AppError {}
|
|
@@ -29,6 +30,11 @@ export interface PTimeoutOptions {
|
|
|
29
30
|
* @experimental
|
|
30
31
|
*/
|
|
31
32
|
keepStackTrace?: boolean
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Will be merged with `err.data` object.
|
|
36
|
+
*/
|
|
37
|
+
errorData?: ErrorData
|
|
32
38
|
}
|
|
33
39
|
|
|
34
40
|
/**
|
|
@@ -63,12 +69,19 @@ export async function pTimeout<T>(promise: Promise<T>, opt: PTimeoutOptions): Pr
|
|
|
63
69
|
resolve(onTimeout())
|
|
64
70
|
} catch (err: any) {
|
|
65
71
|
if (fakeError) err.stack = fakeError.stack // keep original stack
|
|
72
|
+
err.data = {
|
|
73
|
+
...err.data,
|
|
74
|
+
...opt.errorData,
|
|
75
|
+
}
|
|
66
76
|
reject(err)
|
|
67
77
|
}
|
|
68
78
|
return
|
|
69
79
|
}
|
|
70
80
|
|
|
71
|
-
const err = new TimeoutError(
|
|
81
|
+
const err = new TimeoutError(
|
|
82
|
+
`"${name || 'pTimeout function'}" timed out after ${timeout} ms`,
|
|
83
|
+
opt.errorData,
|
|
84
|
+
)
|
|
72
85
|
if (fakeError) err.stack = fakeError.stack // keep original stack
|
|
73
86
|
reject(err)
|
|
74
87
|
}, timeout)
|