@naturalcycles/js-lib 15.107.0 → 15.108.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/array/array.util.js +3 -1
- package/dist/browser/adminService.js +1 -1
- package/dist/datetime/localDate.js +3 -1
- package/dist/decorators/asyncMemo.decorator.d.ts +1 -1
- package/dist/decorators/asyncMemo.decorator.js +88 -86
- package/dist/decorators/memo.decorator.d.ts +1 -1
- package/dist/decorators/memo.decorator.js +62 -60
- package/dist/decorators/memoSimple.decorator.d.ts +1 -1
- package/dist/decorators/memoSimple.decorator.js +39 -37
- package/dist/decorators/memoized.js +2 -2
- package/dist/decorators/memoizedAsync.js +2 -2
- package/dist/decorators/swarmSafe.decorator.d.ts +1 -1
- package/dist/decorators/swarmSafe.decorator.js +28 -26
- package/dist/define.js +2 -2
- package/dist/error/tryCatch.d.ts +1 -1
- package/dist/error/tryCatch.js +7 -5
- package/dist/is.util.d.ts +6 -6
- package/dist/is.util.js +18 -6
- package/dist/log/commonLogger.js +6 -2
- package/dist/object/keySortedMap.js +1 -0
- package/dist/object/lazyKeySortedMap.js +1 -0
- package/dist/promise/pMap.js +2 -2
- package/dist/qr/qr.js +3 -1
- package/dist/semver.js +3 -1
- package/dist/string/slugify.js +4 -6
- package/dist/types.d.ts +1 -1
- package/dist/types.js +3 -1
- package/package.json +2 -2
- package/src/array/array.util.ts +3 -1
- package/src/browser/adminService.ts +2 -1
- package/src/datetime/localDate.ts +5 -1
- package/src/decorators/asyncMemo.decorator.ts +3 -3
- package/src/decorators/memo.decorator.ts +3 -3
- package/src/decorators/memoSimple.decorator.ts +14 -14
- package/src/decorators/memoized.ts +1 -1
- package/src/decorators/memoizedAsync.ts +1 -1
- package/src/decorators/swarmSafe.decorator.ts +29 -27
- package/src/define.ts +1 -1
- package/src/error/tryCatch.ts +3 -3
- package/src/is.util.ts +22 -6
- package/src/log/commonLogger.ts +5 -2
- package/src/object/keySortedMap.ts +1 -0
- package/src/object/lazyKeySortedMap.ts +1 -0
- package/src/promise/pMap.ts +1 -1
- package/src/qr/qr.ts +5 -1
- package/src/semver.ts +5 -1
- package/src/string/slugify.ts +7 -9
- package/src/types.ts +3 -1
package/dist/array/array.util.js
CHANGED
|
@@ -362,7 +362,9 @@ export function _shuffle(array, opt = {}) {
|
|
|
362
362
|
const a = opt.mutate ? array : array.slice();
|
|
363
363
|
for (let i = a.length - 1; i > 0; i--) {
|
|
364
364
|
const j = Math.floor(Math.random() * (i + 1));
|
|
365
|
-
|
|
365
|
+
const tmp = a[i];
|
|
366
|
+
a[i] = a[j];
|
|
367
|
+
a[j] = tmp;
|
|
366
368
|
}
|
|
367
369
|
return a;
|
|
368
370
|
}
|
|
@@ -619,7 +619,9 @@ class LocalDateFactory {
|
|
|
619
619
|
*/
|
|
620
620
|
sort(items, opt = {}) {
|
|
621
621
|
const mod = opt.dir === 'desc' ? -1 : 1;
|
|
622
|
-
|
|
622
|
+
function cmp(a, b) {
|
|
623
|
+
return a.compare(b) * mod;
|
|
624
|
+
}
|
|
623
625
|
return opt.mutate ? items.sort(cmp) : items.toSorted(cmp);
|
|
624
626
|
}
|
|
625
627
|
/**
|
|
@@ -41,7 +41,7 @@ export interface AsyncMemoInstance {
|
|
|
41
41
|
* Implementation is more complex than `@_Memo`, because it needs to handle "in-flight" Promises
|
|
42
42
|
* while waiting for cache to resolve, to prevent "async swarm" issue.
|
|
43
43
|
*/
|
|
44
|
-
export declare
|
|
44
|
+
export declare function _AsyncMemo<FN>(opt?: AsyncMemoOptions<FN>): MethodDecorator<FN>;
|
|
45
45
|
/**
|
|
46
46
|
Call it on a method that is decorated with `@_AsyncMemo` to get access to additional functions,
|
|
47
47
|
e.g `clear` to clear the cache, or get its underlying data.
|
|
@@ -16,96 +16,98 @@ import { jsonMemoSerializer, MapAsyncMemoCache } from './memo.util.js';
|
|
|
16
16
|
* Implementation is more complex than `@_Memo`, because it needs to handle "in-flight" Promises
|
|
17
17
|
* while waiting for cache to resolve, to prevent "async swarm" issue.
|
|
18
18
|
*/
|
|
19
|
-
export
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
if (!promiseCache) {
|
|
45
|
-
promiseCache = new Map();
|
|
46
|
-
instancePromiseCache.set(ctx, promiseCache);
|
|
47
|
-
}
|
|
48
|
-
let promise = promiseCache.get(cacheKey);
|
|
49
|
-
// If there's already "in-flight" cache request - return that, to avoid "async swarm"
|
|
50
|
-
if (promise) {
|
|
51
|
-
// console.log('return promise', promiseCache.size)
|
|
52
|
-
return promise;
|
|
53
|
-
}
|
|
54
|
-
promise = cache.get(cacheKey).then(async (value) => {
|
|
55
|
-
if (value !== MISS) {
|
|
56
|
-
// console.log('hit', promiseCache.size)
|
|
57
|
-
promiseCache.delete(cacheKey);
|
|
58
|
-
return value;
|
|
19
|
+
export function _AsyncMemo(opt = {}) {
|
|
20
|
+
return (target, key, descriptor) => {
|
|
21
|
+
_assertTypeOf(descriptor.value, 'function', 'Memoization can be applied only to methods');
|
|
22
|
+
const originalFn = descriptor.value;
|
|
23
|
+
// Map from "instance" of the Class where @_AsyncMemo is applied to AsyncMemoCache instance.
|
|
24
|
+
const instanceCache = new Map();
|
|
25
|
+
// Cache from Instance to Map<key, Promise>
|
|
26
|
+
// This cache is temporary, with only one purpose - to prevent "async swarm"
|
|
27
|
+
// It only holds values that are "in-flight", until Promise is resolved
|
|
28
|
+
// After it's resolved - it's evicted from the cache and moved to the "proper" `instanceCache`
|
|
29
|
+
const instancePromiseCache = new Map();
|
|
30
|
+
const { logger = consoleLogger, cacheFactory = () => new MapAsyncMemoCache(), cacheKeyFn = jsonMemoSerializer, } = opt;
|
|
31
|
+
const keyStr = String(key);
|
|
32
|
+
const methodSignature = _getTargetMethodSignature(target, keyStr);
|
|
33
|
+
// oxlint-disable-next-line @typescript-eslint/promise-function-async
|
|
34
|
+
descriptor.value = function (...args) {
|
|
35
|
+
const ctx = this;
|
|
36
|
+
const cacheKey = cacheKeyFn(args);
|
|
37
|
+
let cache = instanceCache.get(ctx);
|
|
38
|
+
let promiseCache = instancePromiseCache.get(ctx);
|
|
39
|
+
if (!cache) {
|
|
40
|
+
cache = cacheFactory();
|
|
41
|
+
instanceCache.set(ctx, cache);
|
|
42
|
+
// here, no need to check the cache. It's definitely a miss, because the cacheLayers is just created
|
|
43
|
+
// UPD: no! AsyncMemo supports "persistent caches" (e.g Database-backed cache)
|
|
59
44
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
return promise;
|
|
70
|
-
//
|
|
71
|
-
async function onMiss() {
|
|
72
|
-
try {
|
|
73
|
-
const value = await originalFn.apply(ctx, args);
|
|
74
|
-
// Save the value in the Cache, in parallel,
|
|
75
|
-
// not to slow down the main function execution
|
|
76
|
-
// and not to fail on possible cache issues
|
|
77
|
-
void (async () => {
|
|
78
|
-
try {
|
|
79
|
-
await cache.set(cacheKey, value);
|
|
80
|
-
}
|
|
81
|
-
catch (err) {
|
|
82
|
-
logger.error({ err }); // log and ignore the error
|
|
83
|
-
}
|
|
84
|
-
finally {
|
|
85
|
-
// Clear the "in-flight" promise cache entry, as we now have a "permanent" cache entry
|
|
86
|
-
promiseCache.delete(cacheKey);
|
|
87
|
-
// console.log('cache set and cleared', promiseCache!.size)
|
|
88
|
-
}
|
|
89
|
-
})();
|
|
90
|
-
return value;
|
|
45
|
+
if (!promiseCache) {
|
|
46
|
+
promiseCache = new Map();
|
|
47
|
+
instancePromiseCache.set(ctx, promiseCache);
|
|
48
|
+
}
|
|
49
|
+
let promise = promiseCache.get(cacheKey);
|
|
50
|
+
// If there's already "in-flight" cache request - return that, to avoid "async swarm"
|
|
51
|
+
if (promise) {
|
|
52
|
+
// console.log('return promise', promiseCache.size)
|
|
53
|
+
return promise;
|
|
91
54
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
55
|
+
promise = cache.get(cacheKey).then(async (value) => {
|
|
56
|
+
if (value !== MISS) {
|
|
57
|
+
// console.log('hit', promiseCache.size)
|
|
58
|
+
promiseCache.delete(cacheKey);
|
|
59
|
+
return value;
|
|
60
|
+
}
|
|
61
|
+
// Miss
|
|
62
|
+
// console.log('miss', promiseCache.size)
|
|
63
|
+
return await onMiss();
|
|
64
|
+
}, async (err) => {
|
|
65
|
+
// Log the cache error and proceed "as cache Miss"
|
|
66
|
+
logger.error({ err });
|
|
67
|
+
return await onMiss();
|
|
68
|
+
});
|
|
69
|
+
promiseCache.set(cacheKey, promise);
|
|
70
|
+
return promise;
|
|
71
|
+
//
|
|
72
|
+
async function onMiss() {
|
|
73
|
+
try {
|
|
74
|
+
const value = await originalFn.apply(ctx, args);
|
|
75
|
+
// Save the value in the Cache, in parallel,
|
|
76
|
+
// not to slow down the main function execution
|
|
77
|
+
// and not to fail on possible cache issues
|
|
78
|
+
void (async () => {
|
|
79
|
+
try {
|
|
80
|
+
await cache.set(cacheKey, value);
|
|
81
|
+
}
|
|
82
|
+
catch (err) {
|
|
83
|
+
logger.error({ err }); // log and ignore the error
|
|
84
|
+
}
|
|
85
|
+
finally {
|
|
86
|
+
// Clear the "in-flight" promise cache entry, as we now have a "permanent" cache entry
|
|
87
|
+
promiseCache.delete(cacheKey);
|
|
88
|
+
// console.log('cache set and cleared', promiseCache!.size)
|
|
89
|
+
}
|
|
90
|
+
})();
|
|
91
|
+
return value;
|
|
92
|
+
}
|
|
93
|
+
catch (err) {
|
|
94
|
+
promiseCache.delete(cacheKey);
|
|
95
|
+
throw err;
|
|
96
|
+
}
|
|
95
97
|
}
|
|
96
|
-
}
|
|
98
|
+
};
|
|
99
|
+
_objectAssign(descriptor.value, {
|
|
100
|
+
clear: async () => {
|
|
101
|
+
logger.log(`${methodSignature} @_AsyncMemo.clear()`);
|
|
102
|
+
await Promise.all(Array.from(instanceCache.values(), c => c.clear()));
|
|
103
|
+
instanceCache.clear();
|
|
104
|
+
},
|
|
105
|
+
getInstanceCache: () => instanceCache,
|
|
106
|
+
getCache: instance => instanceCache.get(instance),
|
|
107
|
+
});
|
|
108
|
+
return descriptor;
|
|
97
109
|
};
|
|
98
|
-
|
|
99
|
-
clear: async () => {
|
|
100
|
-
logger.log(`${methodSignature} @_AsyncMemo.clear()`);
|
|
101
|
-
await Promise.all(Array.from(instanceCache.values(), c => c.clear()));
|
|
102
|
-
instanceCache.clear();
|
|
103
|
-
},
|
|
104
|
-
getInstanceCache: () => instanceCache,
|
|
105
|
-
getCache: instance => instanceCache.get(instance),
|
|
106
|
-
});
|
|
107
|
-
return descriptor;
|
|
108
|
-
};
|
|
110
|
+
}
|
|
109
111
|
/**
|
|
110
112
|
Call it on a method that is decorated with `@_AsyncMemo` to get access to additional functions,
|
|
111
113
|
e.g `clear` to clear the cache, or get its underlying data.
|
|
@@ -51,7 +51,7 @@ export interface MemoInstance {
|
|
|
51
51
|
* http://inlehmansterms.net/2015/03/01/javascript-memoization/
|
|
52
52
|
* https://community.risingstack.com/the-worlds-fastest-javascript-memoization-library/
|
|
53
53
|
*/
|
|
54
|
-
export declare
|
|
54
|
+
export declare function _Memo<FN>(opt?: MemoOptions<FN>): MethodDecorator<FN>;
|
|
55
55
|
/**
|
|
56
56
|
Call it on a method that is decorated with `@_Memo` to get access to additional functions,
|
|
57
57
|
e.g `clear` to clear the cache, or get its underlying data.
|
|
@@ -28,67 +28,69 @@ import { jsonMemoSerializer, MapMemoCache } from './memo.util.js';
|
|
|
28
28
|
* http://inlehmansterms.net/2015/03/01/javascript-memoization/
|
|
29
29
|
* https://community.risingstack.com/the-worlds-fastest-javascript-memoization-library/
|
|
30
30
|
*/
|
|
31
|
-
export
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
cache.
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
31
|
+
export function _Memo(opt = {}) {
|
|
32
|
+
return (target, key, descriptor) => {
|
|
33
|
+
_assertTypeOf(descriptor.value, 'function', 'Memoization can be applied only to methods');
|
|
34
|
+
const originalFn = descriptor.value;
|
|
35
|
+
// Map<ctx => MemoCache<cacheKey, result>>
|
|
36
|
+
//
|
|
37
|
+
// Internal map is from cacheKey to result
|
|
38
|
+
// External map (instanceCache) is from ctx (instance of class) to Internal map
|
|
39
|
+
// External map is Weak to not cause memory leaks, to allow ctx objects to be garbage collected
|
|
40
|
+
// UPD: tests show that normal Map also doesn't leak (to be tested further)
|
|
41
|
+
// Normal Map is needed to allow .clear()
|
|
42
|
+
const instanceCache = new Map();
|
|
43
|
+
const { logger = consoleLogger, cacheFactory = () => new MapMemoCache(), cacheKeyFn = jsonMemoSerializer, } = opt;
|
|
44
|
+
const keyStr = String(key);
|
|
45
|
+
const methodSignature = _getTargetMethodSignature(target, keyStr);
|
|
46
|
+
descriptor.value = function (...args) {
|
|
47
|
+
const ctx = this;
|
|
48
|
+
const cacheKey = cacheKeyFn(args);
|
|
49
|
+
let cache = instanceCache.get(ctx);
|
|
50
|
+
if (!cache) {
|
|
51
|
+
cache = cacheFactory();
|
|
52
|
+
instanceCache.set(ctx, cache);
|
|
53
|
+
}
|
|
54
|
+
if (cache.has(cacheKey)) {
|
|
55
|
+
// Hit
|
|
56
|
+
return cache.get(cacheKey);
|
|
57
|
+
}
|
|
58
|
+
// Miss
|
|
59
|
+
const value = originalFn.apply(ctx, args);
|
|
60
|
+
try {
|
|
61
|
+
cache.set(cacheKey, value);
|
|
62
|
+
}
|
|
63
|
+
catch (err) {
|
|
64
|
+
logger.error({ err });
|
|
65
|
+
}
|
|
66
|
+
if (value instanceof Promise) {
|
|
67
|
+
// Don't cache a rejection: evict it, so the next call retries the method.
|
|
68
|
+
// The derived Promise is handled, so it adds no unhandled rejection.
|
|
69
|
+
value.catch(() => {
|
|
70
|
+
try {
|
|
71
|
+
// Only if the key still holds this very Promise (and not a newer one, cached after an eviction)
|
|
72
|
+
if (cache.get(cacheKey) === value)
|
|
73
|
+
cache.delete(cacheKey);
|
|
74
|
+
}
|
|
75
|
+
catch (err) {
|
|
76
|
+
logger.error({ err });
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
return value;
|
|
81
|
+
};
|
|
82
|
+
_objectAssign(descriptor.value, {
|
|
83
|
+
clear: () => {
|
|
84
|
+
logger.log(`${methodSignature} @_Memo.clear()`);
|
|
85
|
+
instanceCache.forEach(memoCache => memoCache.clear());
|
|
86
|
+
instanceCache.clear();
|
|
87
|
+
},
|
|
88
|
+
getInstanceCache: () => instanceCache,
|
|
89
|
+
getCache: instance => instanceCache.get(instance),
|
|
90
|
+
});
|
|
91
|
+
return descriptor;
|
|
80
92
|
};
|
|
81
|
-
|
|
82
|
-
clear: () => {
|
|
83
|
-
logger.log(`${methodSignature} @_Memo.clear()`);
|
|
84
|
-
instanceCache.forEach(memoCache => memoCache.clear());
|
|
85
|
-
instanceCache.clear();
|
|
86
|
-
},
|
|
87
|
-
getInstanceCache: () => instanceCache,
|
|
88
|
-
getCache: instance => instanceCache.get(instance),
|
|
89
|
-
});
|
|
90
|
-
return descriptor;
|
|
91
|
-
};
|
|
93
|
+
}
|
|
92
94
|
/**
|
|
93
95
|
Call it on a method that is decorated with `@_Memo` to get access to additional functions,
|
|
94
96
|
e.g `clear` to clear the cache, or get its underlying data.
|
|
@@ -10,4 +10,4 @@ export interface MemoOpts {
|
|
|
10
10
|
*
|
|
11
11
|
* Supports dropping it's cache by calling .dropCache() method of decorated function (useful in unit testing).
|
|
12
12
|
*/
|
|
13
|
-
export declare
|
|
13
|
+
export declare function memoSimple(opt?: MemoOpts): MethodDecorator;
|
|
@@ -21,42 +21,44 @@ import { jsonMemoSerializer, MapMemoCache } from './memo.util.js';
|
|
|
21
21
|
*
|
|
22
22
|
* Supports dropping it's cache by calling .dropCache() method of decorated function (useful in unit testing).
|
|
23
23
|
*/
|
|
24
|
-
export
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const originalFn = descriptor.value;
|
|
29
|
-
// console.log(`${key} len: ${originalFn.length}`)
|
|
30
|
-
// todo: optimization disabled until "default arg value" use case is solved (see memo.decorator.test.ts)
|
|
31
|
-
/*
|
|
32
|
-
let cache: MemoCache
|
|
33
|
-
|
|
34
|
-
// Function with 0 arguments
|
|
35
|
-
if (!originalFn.length) {
|
|
36
|
-
cache = new SingleValueMemoCache()
|
|
37
|
-
} else {
|
|
38
|
-
// Function with > 0 arguments
|
|
39
|
-
cache = new MapMemoCache()
|
|
40
|
-
// cache = new ObjectMemoCache()
|
|
41
|
-
}
|
|
42
|
-
*/
|
|
43
|
-
const cache = new MapMemoCache();
|
|
44
|
-
const { logger = consoleLogger } = opt;
|
|
45
|
-
const keyStr = String(key);
|
|
46
|
-
const methodSignature = _getTargetMethodSignature(target, keyStr);
|
|
47
|
-
descriptor.value = function (...args) {
|
|
48
|
-
const ctx = this;
|
|
49
|
-
const cacheKey = jsonMemoSerializer(args);
|
|
50
|
-
if (cache.has(cacheKey)) {
|
|
51
|
-
return cache.get(cacheKey);
|
|
24
|
+
export function memoSimple(opt = {}) {
|
|
25
|
+
return (target, key, descriptor) => {
|
|
26
|
+
if (typeof descriptor.value !== 'function') {
|
|
27
|
+
throw new TypeError('Memoization can be applied only to methods');
|
|
52
28
|
}
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
29
|
+
const originalFn = descriptor.value;
|
|
30
|
+
// console.log(`${key} len: ${originalFn.length}`)
|
|
31
|
+
// todo: optimization disabled until "default arg value" use case is solved (see memo.decorator.test.ts)
|
|
32
|
+
/*
|
|
33
|
+
let cache: MemoCache
|
|
34
|
+
|
|
35
|
+
// Function with 0 arguments
|
|
36
|
+
if (!originalFn.length) {
|
|
37
|
+
cache = new SingleValueMemoCache()
|
|
38
|
+
} else {
|
|
39
|
+
// Function with > 0 arguments
|
|
40
|
+
cache = new MapMemoCache()
|
|
41
|
+
// cache = new ObjectMemoCache()
|
|
42
|
+
}
|
|
43
|
+
*/
|
|
44
|
+
const cache = new MapMemoCache();
|
|
45
|
+
const { logger = consoleLogger } = opt;
|
|
46
|
+
const keyStr = String(key);
|
|
47
|
+
const methodSignature = _getTargetMethodSignature(target, keyStr);
|
|
48
|
+
descriptor.value = function (...args) {
|
|
49
|
+
const ctx = this;
|
|
50
|
+
const cacheKey = jsonMemoSerializer(args);
|
|
51
|
+
if (cache.has(cacheKey)) {
|
|
52
|
+
return cache.get(cacheKey);
|
|
53
|
+
}
|
|
54
|
+
const res = originalFn.apply(ctx, args);
|
|
55
|
+
cache.set(cacheKey, res);
|
|
56
|
+
return res;
|
|
57
|
+
};
|
|
58
|
+
descriptor.value.dropCache = () => {
|
|
59
|
+
logger.log(`${methodSignature} @memo.dropCache()`);
|
|
60
|
+
cache.clear();
|
|
61
|
+
};
|
|
62
|
+
return descriptor;
|
|
60
63
|
};
|
|
61
|
-
|
|
62
|
-
};
|
|
64
|
+
}
|
|
@@ -8,7 +8,7 @@ import { jsonMemoSerializer, MapMemoCache } from './memo.util.js';
|
|
|
8
8
|
export function _memoized(fn, opt = {}) {
|
|
9
9
|
const { logger = consoleLogger, cacheFactory = () => new MapMemoCache(), cacheKeyFn = jsonMemoSerializer, } = opt;
|
|
10
10
|
const cache = cacheFactory();
|
|
11
|
-
|
|
11
|
+
function memoizedFn(...args) {
|
|
12
12
|
const ctx = this;
|
|
13
13
|
const cacheKey = cacheKeyFn(args);
|
|
14
14
|
if (cache.has(cacheKey)) {
|
|
@@ -36,7 +36,7 @@ export function _memoized(fn, opt = {}) {
|
|
|
36
36
|
});
|
|
37
37
|
}
|
|
38
38
|
return value;
|
|
39
|
-
}
|
|
39
|
+
}
|
|
40
40
|
Object.assign(memoizedFn, { cache });
|
|
41
41
|
return memoizedFn;
|
|
42
42
|
}
|
|
@@ -10,7 +10,7 @@ import { jsonMemoSerializer, MapAsyncMemoCache } from './memo.util.js';
|
|
|
10
10
|
export function _memoizedAsync(fn, opt = {}) {
|
|
11
11
|
const { logger = consoleLogger, cacheFactory = () => new MapAsyncMemoCache(), cacheKeyFn = jsonMemoSerializer, } = opt;
|
|
12
12
|
const cache = cacheFactory();
|
|
13
|
-
|
|
13
|
+
async function memoizedFn(...args) {
|
|
14
14
|
const ctx = this;
|
|
15
15
|
const cacheKey = cacheKeyFn(args);
|
|
16
16
|
// MISS, so that a failed cache `get` is a miss too (and not a cached `undefined`)
|
|
@@ -34,7 +34,7 @@ export function _memoizedAsync(fn, opt = {}) {
|
|
|
34
34
|
}
|
|
35
35
|
})();
|
|
36
36
|
return value;
|
|
37
|
-
}
|
|
37
|
+
}
|
|
38
38
|
Object.assign(memoizedFn, { cache });
|
|
39
39
|
return memoizedFn;
|
|
40
40
|
}
|
|
@@ -7,31 +7,33 @@ import { _getTargetMethodSignature } from './decorator.util.js';
|
|
|
7
7
|
* Does not support `cacheKey`.
|
|
8
8
|
* So, the same Promise is returned, regardless of the arguments.
|
|
9
9
|
*/
|
|
10
|
-
export
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const originalFn = descriptor.value;
|
|
15
|
-
const keyStr = String(key);
|
|
16
|
-
const methodSignature = _getTargetMethodSignature(target, keyStr);
|
|
17
|
-
const instanceCache = new Map();
|
|
18
|
-
console.log('SwarmSafe constructor called', { key, methodSignature });
|
|
19
|
-
// oxlint-disable-next-line @typescript-eslint/promise-function-async
|
|
20
|
-
descriptor.value = function (...args) {
|
|
21
|
-
console.log('SwarmSafe method called', { key, methodSignature, args });
|
|
22
|
-
const ctx = this;
|
|
23
|
-
let inFlightPromise = instanceCache.get(ctx);
|
|
24
|
-
if (inFlightPromise) {
|
|
25
|
-
console.log(`SwarmSafe: returning in-flight promise`);
|
|
26
|
-
return inFlightPromise;
|
|
10
|
+
export function _SwarmSafe() {
|
|
11
|
+
return (target, key, descriptor) => {
|
|
12
|
+
if (typeof descriptor.value !== 'function') {
|
|
13
|
+
throw new TypeError('@_SwarmSafe can be applied only to methods');
|
|
27
14
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
15
|
+
const originalFn = descriptor.value;
|
|
16
|
+
const keyStr = String(key);
|
|
17
|
+
const methodSignature = _getTargetMethodSignature(target, keyStr);
|
|
18
|
+
const instanceCache = new Map();
|
|
19
|
+
console.log('SwarmSafe constructor called', { key, methodSignature });
|
|
20
|
+
// oxlint-disable-next-line @typescript-eslint/promise-function-async
|
|
21
|
+
descriptor.value = function (...args) {
|
|
22
|
+
console.log('SwarmSafe method called', { key, methodSignature, args });
|
|
23
|
+
const ctx = this;
|
|
24
|
+
let inFlightPromise = instanceCache.get(ctx);
|
|
25
|
+
if (inFlightPromise) {
|
|
26
|
+
console.log(`SwarmSafe: returning in-flight promise`);
|
|
27
|
+
return inFlightPromise;
|
|
28
|
+
}
|
|
29
|
+
console.log(`SwarmSafe: first-time call, creating in-flight promise`);
|
|
30
|
+
inFlightPromise = originalFn.apply(ctx, args);
|
|
31
|
+
instanceCache.set(ctx, inFlightPromise);
|
|
32
|
+
void inFlightPromise.finally(() => {
|
|
33
|
+
console.log(`SwarmSafe: in-flight promise resolved`);
|
|
34
|
+
instanceCache.delete(ctx);
|
|
35
|
+
});
|
|
36
|
+
return inFlightPromise;
|
|
37
|
+
};
|
|
36
38
|
};
|
|
37
|
-
}
|
|
39
|
+
}
|
package/dist/define.js
CHANGED
|
@@ -35,9 +35,9 @@ export function _lazyValue(fn) {
|
|
|
35
35
|
* Based on: https://github.com/sindresorhus/define-lazy-prop
|
|
36
36
|
*/
|
|
37
37
|
export function _defineLazyProperty(obj, propertyName, fn) {
|
|
38
|
-
|
|
38
|
+
function define(value) {
|
|
39
39
|
Object.defineProperty(obj, propertyName, { value, enumerable: true, writable: true });
|
|
40
|
-
}
|
|
40
|
+
}
|
|
41
41
|
Object.defineProperty(obj, propertyName, {
|
|
42
42
|
configurable: true,
|
|
43
43
|
enumerable: true,
|
package/dist/error/tryCatch.d.ts
CHANGED
|
@@ -28,4 +28,4 @@ export interface TryCatchOptions {
|
|
|
28
28
|
* @experimental
|
|
29
29
|
*/
|
|
30
30
|
export declare function _tryCatch<T extends AnyFunction>(fn: T, opt?: TryCatchOptions): T;
|
|
31
|
-
export declare
|
|
31
|
+
export declare function _TryCatch(opt?: TryCatchOptions): MethodDecorator;
|
package/dist/error/tryCatch.js
CHANGED
|
@@ -35,8 +35,10 @@ export function _tryCatch(fn, opt = {}) {
|
|
|
35
35
|
}
|
|
36
36
|
};
|
|
37
37
|
}
|
|
38
|
-
export
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
38
|
+
export function _TryCatch(opt = {}) {
|
|
39
|
+
return (_target, _key, descriptor) => {
|
|
40
|
+
const originalFn = descriptor.value;
|
|
41
|
+
descriptor.value = _tryCatch(originalFn, opt);
|
|
42
|
+
return descriptor;
|
|
43
|
+
};
|
|
44
|
+
}
|
package/dist/is.util.d.ts
CHANGED
|
@@ -2,10 +2,10 @@ import type { AnyObject, FalsyValue, NullishValue, PlainObject, Primitive } from
|
|
|
2
2
|
type Nullish<T> = T extends NullishValue ? T : never;
|
|
3
3
|
type Truthy<T> = T extends FalsyValue ? never : T;
|
|
4
4
|
type Falsy<T> = T extends FalsyValue ? T : never;
|
|
5
|
-
export declare
|
|
6
|
-
export declare
|
|
7
|
-
export declare
|
|
8
|
-
export declare
|
|
5
|
+
export declare function _isNull<T>(v: T): v is T extends null ? T : never;
|
|
6
|
+
export declare function _isUndefined<T>(v: T): v is T extends undefined ? T : never;
|
|
7
|
+
export declare function _isNullish<T>(v: T): v is Nullish<T>;
|
|
8
|
+
export declare function _isNotNullish<T>(v: T): v is NonNullable<T>;
|
|
9
9
|
/**
|
|
10
10
|
* Same as Boolean, but with correct type output.
|
|
11
11
|
* Related:
|
|
@@ -17,8 +17,8 @@ export declare const _isNotNullish: <T>(v: T) => v is NonNullable<T>;
|
|
|
17
17
|
* [1, 2, undefined].filter(_isTruthy)
|
|
18
18
|
* // => [1, 2]
|
|
19
19
|
*/
|
|
20
|
-
export declare
|
|
21
|
-
export declare
|
|
20
|
+
export declare function _isTruthy<T>(v: T): v is Truthy<T>;
|
|
21
|
+
export declare function _isFalsy<T>(v: T): v is Falsy<T>;
|
|
22
22
|
/**
|
|
23
23
|
* Returns true if item is Object, not null and not Array.
|
|
24
24
|
*
|
package/dist/is.util.js
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export
|
|
1
|
+
export function _isNull(v) {
|
|
2
|
+
return v === null;
|
|
3
|
+
}
|
|
4
|
+
export function _isUndefined(v) {
|
|
5
|
+
return v === undefined;
|
|
6
|
+
}
|
|
7
|
+
export function _isNullish(v) {
|
|
8
|
+
return v === undefined || v === null;
|
|
9
|
+
}
|
|
10
|
+
export function _isNotNullish(v) {
|
|
11
|
+
return v !== undefined && v !== null;
|
|
12
|
+
}
|
|
5
13
|
/**
|
|
6
14
|
* Same as Boolean, but with correct type output.
|
|
7
15
|
* Related:
|
|
@@ -13,8 +21,12 @@ export const _isNotNullish = (v) => v !== undefined && v !== null;
|
|
|
13
21
|
* [1, 2, undefined].filter(_isTruthy)
|
|
14
22
|
* // => [1, 2]
|
|
15
23
|
*/
|
|
16
|
-
export
|
|
17
|
-
|
|
24
|
+
export function _isTruthy(v) {
|
|
25
|
+
return !!v;
|
|
26
|
+
}
|
|
27
|
+
export function _isFalsy(v) {
|
|
28
|
+
return !v;
|
|
29
|
+
}
|
|
18
30
|
/**
|
|
19
31
|
* Returns true if item is Object, not null and not Array.
|
|
20
32
|
*
|
package/dist/log/commonLogger.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// copy-pasted to avoid weird circular dependency
|
|
2
|
-
|
|
2
|
+
function _noop(..._args) {
|
|
3
|
+
return undefined;
|
|
4
|
+
}
|
|
3
5
|
export const commonLogLevelNumber = {
|
|
4
6
|
debug: 10,
|
|
5
7
|
log: 20,
|
|
@@ -12,7 +14,9 @@ export const commonLogLevelNumber = {
|
|
|
12
14
|
* after the logger is created (e.g a request context whose user is resolved later).
|
|
13
15
|
*/
|
|
14
16
|
export function commonLoggerCreate(sink, context) {
|
|
15
|
-
|
|
17
|
+
function emit(level, [input, data]) {
|
|
18
|
+
sink({ ...context?.(), ...toLogData(input, data), level });
|
|
19
|
+
}
|
|
16
20
|
return {
|
|
17
21
|
debug: (...args) => emit('debug', args),
|
|
18
22
|
log: (...args) => emit('log', args),
|
package/dist/promise/pMap.js
CHANGED
|
@@ -57,7 +57,7 @@ export async function pMap(iterable, mapper, opt = {}) {
|
|
|
57
57
|
let resolvingCount = 0;
|
|
58
58
|
let currentIndex = 0;
|
|
59
59
|
return await new Promise((resolve, reject) => {
|
|
60
|
-
|
|
60
|
+
function next() {
|
|
61
61
|
if (isSettled) {
|
|
62
62
|
return;
|
|
63
63
|
}
|
|
@@ -104,7 +104,7 @@ export async function pMap(iterable, mapper, opt = {}) {
|
|
|
104
104
|
next(); // oxlint-disable-line no-callback-in-promise
|
|
105
105
|
}
|
|
106
106
|
});
|
|
107
|
-
}
|
|
107
|
+
}
|
|
108
108
|
for (let i = 0; i < concurrency; i++) {
|
|
109
109
|
next();
|
|
110
110
|
if (isSettled) {
|
package/dist/qr/qr.js
CHANGED
|
@@ -426,7 +426,9 @@ function lostPointBlocks(modules) {
|
|
|
426
426
|
function lostPointFinderLike(modules) {
|
|
427
427
|
const size = modules.length;
|
|
428
428
|
let lostPoint = 0;
|
|
429
|
-
|
|
429
|
+
function isDark(r, c) {
|
|
430
|
+
return modules[r][c] === true;
|
|
431
|
+
}
|
|
430
432
|
for (let row = 0; row < size; row++) {
|
|
431
433
|
for (let col = 0; col < size - 6; col++) {
|
|
432
434
|
if (isDark(row, col) &&
|
package/dist/semver.js
CHANGED
|
@@ -122,7 +122,9 @@ class SemverFactory {
|
|
|
122
122
|
*/
|
|
123
123
|
sort(items, opt = {}) {
|
|
124
124
|
const mod = opt.dir === 'desc' ? -1 : 1;
|
|
125
|
-
|
|
125
|
+
function cmp(a, b) {
|
|
126
|
+
return a.compare(b) * mod;
|
|
127
|
+
}
|
|
126
128
|
return opt.mutate ? items.sort(cmp) : items.toSorted(cmp);
|
|
127
129
|
}
|
|
128
130
|
}
|
package/dist/string/slugify.js
CHANGED
|
@@ -31,13 +31,11 @@ export function _slugify(s, opt = {}) {
|
|
|
31
31
|
function buildPatternSlug(options) {
|
|
32
32
|
let negationSetPattern = String.raw `a-z\d`;
|
|
33
33
|
negationSetPattern += options.lowercase ? '' : 'A-Z';
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
throw new Error(`The separator character \`${options.separator}\` cannot be included in preserved characters: ${options.preserveCharacters}`);
|
|
38
|
-
}
|
|
39
|
-
negationSetPattern += escapeStringRegexp(character);
|
|
34
|
+
for (const character of options.preserveCharacters) {
|
|
35
|
+
if (character === options.separator) {
|
|
36
|
+
throw new Error(`The separator character \`${options.separator}\` cannot be included in preserved characters: ${options.preserveCharacters}`);
|
|
40
37
|
}
|
|
38
|
+
negationSetPattern += escapeStringRegexp(character);
|
|
41
39
|
}
|
|
42
40
|
return new RegExp(`[^${negationSetPattern}]+`, 'g');
|
|
43
41
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -151,7 +151,7 @@ export declare const _passUndefinedMapper: IndexedMapper<any, void>;
|
|
|
151
151
|
/**
|
|
152
152
|
* Function that does nothings and returns `undefined`.
|
|
153
153
|
*/
|
|
154
|
-
export declare
|
|
154
|
+
export declare function _noop(..._args: any[]): undefined;
|
|
155
155
|
export type Predicate<T> = (item: T, index: number) => boolean;
|
|
156
156
|
export type AsyncPredicate<T> = (item: T, index: number) => PromiseLike<boolean>;
|
|
157
157
|
export type AbortablePredicate<T> = (item: T, i: number) => boolean | typeof END;
|
package/dist/types.js
CHANGED
|
@@ -16,7 +16,9 @@ export const _passUndefinedMapper = () => undefined;
|
|
|
16
16
|
/**
|
|
17
17
|
* Function that does nothings and returns `undefined`.
|
|
18
18
|
*/
|
|
19
|
-
export
|
|
19
|
+
export function _noop(..._args) {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
20
22
|
export const _passthroughPredicate = () => true;
|
|
21
23
|
export const _passNothingPredicate = () => false;
|
|
22
24
|
export const JWT_REGEX = /^[\w-]+\.[\w-]+\.[\w-]+$/;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/js-lib",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "15.
|
|
4
|
+
"version": "15.108.0",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"tslib": "^2"
|
|
7
7
|
},
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
17
|
-
"@naturalcycles/dev-lib": "20.
|
|
17
|
+
"@naturalcycles/dev-lib": "20.80.0",
|
|
18
18
|
"@types/node": "^26",
|
|
19
19
|
"@types/semver": "^7",
|
|
20
20
|
"rolldown": "^1",
|
package/src/array/array.util.ts
CHANGED
|
@@ -426,7 +426,9 @@ export function _shuffle<T>(array: T[], opt: MutateOptions = {}): T[] {
|
|
|
426
426
|
|
|
427
427
|
for (let i = a.length - 1; i > 0; i--) {
|
|
428
428
|
const j = Math.floor(Math.random() * (i + 1))
|
|
429
|
-
|
|
429
|
+
const tmp = a[i]!
|
|
430
|
+
a[i] = a[j]!
|
|
431
|
+
a[j] = tmp
|
|
430
432
|
}
|
|
431
433
|
|
|
432
434
|
return a
|
|
@@ -725,7 +725,11 @@ class LocalDateFactory {
|
|
|
725
725
|
*/
|
|
726
726
|
sort(items: LocalDate[], opt: SortOptions = {}): LocalDate[] {
|
|
727
727
|
const mod = opt.dir === 'desc' ? -1 : 1
|
|
728
|
-
|
|
728
|
+
|
|
729
|
+
function cmp(a: LocalDate, b: LocalDate): number {
|
|
730
|
+
return a.compare(b) * mod
|
|
731
|
+
}
|
|
732
|
+
|
|
729
733
|
return opt.mutate ? items.sort(cmp) : items.toSorted(cmp)
|
|
730
734
|
}
|
|
731
735
|
|
|
@@ -53,9 +53,8 @@ export interface AsyncMemoInstance {
|
|
|
53
53
|
* Implementation is more complex than `@_Memo`, because it needs to handle "in-flight" Promises
|
|
54
54
|
* while waiting for cache to resolve, to prevent "async swarm" issue.
|
|
55
55
|
*/
|
|
56
|
-
export
|
|
57
|
-
|
|
58
|
-
(target, key, descriptor) => {
|
|
56
|
+
export function _AsyncMemo<FN>(opt: AsyncMemoOptions<FN> = {}): MethodDecorator<FN> {
|
|
57
|
+
return (target, key, descriptor) => {
|
|
59
58
|
_assertTypeOf<AnyFunction>(
|
|
60
59
|
descriptor.value,
|
|
61
60
|
'function',
|
|
@@ -171,6 +170,7 @@ export const _AsyncMemo =
|
|
|
171
170
|
|
|
172
171
|
return descriptor
|
|
173
172
|
}
|
|
173
|
+
}
|
|
174
174
|
|
|
175
175
|
/**
|
|
176
176
|
Call it on a method that is decorated with `@_AsyncMemo` to get access to additional functions,
|
|
@@ -63,9 +63,8 @@ export interface MemoInstance {
|
|
|
63
63
|
* http://inlehmansterms.net/2015/03/01/javascript-memoization/
|
|
64
64
|
* https://community.risingstack.com/the-worlds-fastest-javascript-memoization-library/
|
|
65
65
|
*/
|
|
66
|
-
export
|
|
67
|
-
|
|
68
|
-
(target, key, descriptor) => {
|
|
66
|
+
export function _Memo<FN>(opt: MemoOptions<FN> = {}): MethodDecorator<FN> {
|
|
67
|
+
return (target, key, descriptor) => {
|
|
69
68
|
_assertTypeOf<AnyFunction>(
|
|
70
69
|
descriptor.value,
|
|
71
70
|
'function',
|
|
@@ -144,6 +143,7 @@ export const _Memo =
|
|
|
144
143
|
|
|
145
144
|
return descriptor
|
|
146
145
|
}
|
|
146
|
+
}
|
|
147
147
|
|
|
148
148
|
/**
|
|
149
149
|
Call it on a method that is decorated with `@_Memo` to get access to additional functions,
|
|
@@ -31,9 +31,8 @@ export interface MemoOpts {
|
|
|
31
31
|
*
|
|
32
32
|
* Supports dropping it's cache by calling .dropCache() method of decorated function (useful in unit testing).
|
|
33
33
|
*/
|
|
34
|
-
export
|
|
35
|
-
(
|
|
36
|
-
(target, key, descriptor) => {
|
|
34
|
+
export function memoSimple(opt: MemoOpts = {}): MethodDecorator {
|
|
35
|
+
return (target, key, descriptor) => {
|
|
37
36
|
if (typeof descriptor.value !== 'function') {
|
|
38
37
|
throw new TypeError('Memoization can be applied only to methods')
|
|
39
38
|
}
|
|
@@ -43,17 +42,17 @@ export const memoSimple =
|
|
|
43
42
|
|
|
44
43
|
// todo: optimization disabled until "default arg value" use case is solved (see memo.decorator.test.ts)
|
|
45
44
|
/*
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
45
|
+
let cache: MemoCache
|
|
46
|
+
|
|
47
|
+
// Function with 0 arguments
|
|
48
|
+
if (!originalFn.length) {
|
|
49
|
+
cache = new SingleValueMemoCache()
|
|
50
|
+
} else {
|
|
51
|
+
// Function with > 0 arguments
|
|
52
|
+
cache = new MapMemoCache()
|
|
53
|
+
// cache = new ObjectMemoCache()
|
|
54
|
+
}
|
|
55
|
+
*/
|
|
57
56
|
const cache: MemoCache = new MapMemoCache()
|
|
58
57
|
|
|
59
58
|
const { logger = consoleLogger } = opt
|
|
@@ -79,3 +78,4 @@ export const memoSimple =
|
|
|
79
78
|
|
|
80
79
|
return descriptor
|
|
81
80
|
}
|
|
81
|
+
}
|
|
@@ -25,7 +25,7 @@ export function _memoized<FN extends AnyFunction>(
|
|
|
25
25
|
|
|
26
26
|
const cache = cacheFactory()
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
function memoizedFn(this: any, ...args: MaybeParameters<FN>): FN {
|
|
29
29
|
const ctx = this
|
|
30
30
|
const cacheKey = cacheKeyFn(args)
|
|
31
31
|
|
|
@@ -27,7 +27,7 @@ export function _memoizedAsync<FN extends AnyAsyncFunction>(
|
|
|
27
27
|
|
|
28
28
|
const cache = cacheFactory()
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
async function memoizedFn(this: any, ...args: MaybeParameters<FN>): Promise<any> {
|
|
31
31
|
const ctx = this
|
|
32
32
|
const cacheKey = cacheKeyFn(args)
|
|
33
33
|
// MISS, so that a failed cache `get` is a miss too (and not a cached `undefined`)
|
|
@@ -9,38 +9,40 @@ import { _getTargetMethodSignature } from './decorator.util.js'
|
|
|
9
9
|
* Does not support `cacheKey`.
|
|
10
10
|
* So, the same Promise is returned, regardless of the arguments.
|
|
11
11
|
*/
|
|
12
|
-
export
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
export function _SwarmSafe(): MethodDecorator {
|
|
13
|
+
return (target, key, descriptor) => {
|
|
14
|
+
if (typeof descriptor.value !== 'function') {
|
|
15
|
+
throw new TypeError('@_SwarmSafe can be applied only to methods')
|
|
16
|
+
}
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
const originalFn = descriptor.value
|
|
19
|
+
const keyStr = String(key)
|
|
20
|
+
const methodSignature = _getTargetMethodSignature(target, keyStr)
|
|
21
|
+
const instanceCache = new Map<AnyObject, Promise<any>>()
|
|
21
22
|
|
|
22
|
-
|
|
23
|
+
console.log('SwarmSafe constructor called', { key, methodSignature })
|
|
23
24
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
// oxlint-disable-next-line @typescript-eslint/promise-function-async
|
|
26
|
+
descriptor.value = function (this: typeof target, ...args: any[]): Promise<any> {
|
|
27
|
+
console.log('SwarmSafe method called', { key, methodSignature, args })
|
|
28
|
+
const ctx = this
|
|
28
29
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
let inFlightPromise = instanceCache.get(ctx)
|
|
31
|
+
if (inFlightPromise) {
|
|
32
|
+
console.log(`SwarmSafe: returning in-flight promise`)
|
|
33
|
+
return inFlightPromise
|
|
34
|
+
}
|
|
34
35
|
|
|
35
|
-
|
|
36
|
+
console.log(`SwarmSafe: first-time call, creating in-flight promise`)
|
|
36
37
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
38
|
+
inFlightPromise = originalFn.apply(ctx, args) as Promise<any>
|
|
39
|
+
instanceCache.set(ctx, inFlightPromise)
|
|
40
|
+
void inFlightPromise.finally(() => {
|
|
41
|
+
console.log(`SwarmSafe: in-flight promise resolved`)
|
|
42
|
+
instanceCache.delete(ctx)
|
|
43
|
+
})
|
|
43
44
|
|
|
44
|
-
|
|
45
|
-
|
|
45
|
+
return inFlightPromise
|
|
46
|
+
} as any
|
|
47
|
+
}
|
|
46
48
|
}
|
package/src/define.ts
CHANGED
|
@@ -44,7 +44,7 @@ export function _defineLazyProperty<OBJ extends AnyObject>(
|
|
|
44
44
|
propertyName: keyof OBJ,
|
|
45
45
|
fn: AnyFunction,
|
|
46
46
|
): OBJ {
|
|
47
|
-
|
|
47
|
+
function define(value: any): void {
|
|
48
48
|
Object.defineProperty(obj, propertyName, { value, enumerable: true, writable: true })
|
|
49
49
|
}
|
|
50
50
|
|
package/src/error/tryCatch.ts
CHANGED
|
@@ -66,10 +66,10 @@ export function _tryCatch<T extends AnyFunction>(fn: T, opt: TryCatchOptions = {
|
|
|
66
66
|
} as any
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
export
|
|
70
|
-
(
|
|
71
|
-
(_target, _key, descriptor) => {
|
|
69
|
+
export function _TryCatch(opt: TryCatchOptions = {}): MethodDecorator {
|
|
70
|
+
return (_target, _key, descriptor) => {
|
|
72
71
|
const originalFn = descriptor.value
|
|
73
72
|
descriptor.value = _tryCatch(originalFn as any, opt)
|
|
74
73
|
return descriptor
|
|
75
74
|
}
|
|
75
|
+
}
|
package/src/is.util.ts
CHANGED
|
@@ -4,10 +4,21 @@ type Nullish<T> = T extends NullishValue ? T : never
|
|
|
4
4
|
type Truthy<T> = T extends FalsyValue ? never : T
|
|
5
5
|
type Falsy<T> = T extends FalsyValue ? T : never
|
|
6
6
|
|
|
7
|
-
export
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
export function _isNull<T>(v: T): v is T extends null ? T : never {
|
|
8
|
+
return v === null
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function _isUndefined<T>(v: T): v is T extends undefined ? T : never {
|
|
12
|
+
return v === undefined
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function _isNullish<T>(v: T): v is Nullish<T> {
|
|
16
|
+
return v === undefined || v === null
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function _isNotNullish<T>(v: T): v is NonNullable<T> {
|
|
20
|
+
return v !== undefined && v !== null
|
|
21
|
+
}
|
|
11
22
|
|
|
12
23
|
/**
|
|
13
24
|
* Same as Boolean, but with correct type output.
|
|
@@ -20,8 +31,13 @@ export const _isNotNullish = <T>(v: T): v is NonNullable<T> => v !== undefined &
|
|
|
20
31
|
* [1, 2, undefined].filter(_isTruthy)
|
|
21
32
|
* // => [1, 2]
|
|
22
33
|
*/
|
|
23
|
-
export
|
|
24
|
-
|
|
34
|
+
export function _isTruthy<T>(v: T): v is Truthy<T> {
|
|
35
|
+
return !!v
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function _isFalsy<T>(v: T): v is Falsy<T> {
|
|
39
|
+
return !v
|
|
40
|
+
}
|
|
25
41
|
|
|
26
42
|
/**
|
|
27
43
|
* Returns true if item is Object, not null and not Array.
|
package/src/log/commonLogger.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// copy-pasted to avoid weird circular dependency
|
|
2
|
-
|
|
2
|
+
function _noop(..._args: any[]): undefined {
|
|
3
|
+
return undefined
|
|
4
|
+
}
|
|
3
5
|
|
|
4
6
|
/**
|
|
5
7
|
* These levels follow console.* naming,
|
|
@@ -76,8 +78,9 @@ export interface CommonLogger {
|
|
|
76
78
|
* after the logger is created (e.g a request context whose user is resolved later).
|
|
77
79
|
*/
|
|
78
80
|
export function commonLoggerCreate(sink: LogSink, context?: () => LogData): CommonLogger {
|
|
79
|
-
|
|
81
|
+
function emit(level: CommonLogLevel, [input, data]: CommonLogArgs): void {
|
|
80
82
|
sink({ ...context?.(), ...toLogData(input, data), level })
|
|
83
|
+
}
|
|
81
84
|
|
|
82
85
|
return {
|
|
83
86
|
debug: (...args) => emit('debug', args),
|
|
@@ -25,6 +25,7 @@ export interface KeySortedMapOptions<K> {
|
|
|
25
25
|
// oxlint-disable-next-line no-unsafe-declaration-merging -- Map<K,V> workaround for oxlint TS2420 false positive
|
|
26
26
|
export interface KeySortedMap<K, V> extends Map<K, V> {}
|
|
27
27
|
|
|
28
|
+
// oxlint-disable-next-line no-unsafe-declaration-merging -- see the interface above
|
|
28
29
|
export class KeySortedMap<K, V> {
|
|
29
30
|
private readonly map: Map<K, V>
|
|
30
31
|
readonly #sortedKeys: K[]
|
|
@@ -20,6 +20,7 @@ export interface LazyKeySortedMapOptions<K> {
|
|
|
20
20
|
// oxlint-disable-next-line no-unsafe-declaration-merging -- Map<K,V> workaround for oxlint TS2420 false positive
|
|
21
21
|
export interface LazyKeySortedMap<K, V> extends Map<K, V> {}
|
|
22
22
|
|
|
23
|
+
// oxlint-disable-next-line no-unsafe-declaration-merging -- see the interface above
|
|
23
24
|
export class LazyKeySortedMap<K, V> {
|
|
24
25
|
private readonly map: Map<K, V>
|
|
25
26
|
private readonly maybeSortedKeys: K[]
|
package/src/promise/pMap.ts
CHANGED
package/src/qr/qr.ts
CHANGED
|
@@ -543,7 +543,11 @@ function lostPointBlocks(modules: boolean[][]): number {
|
|
|
543
543
|
function lostPointFinderLike(modules: boolean[][]): number {
|
|
544
544
|
const size = modules.length
|
|
545
545
|
let lostPoint = 0
|
|
546
|
-
|
|
546
|
+
|
|
547
|
+
function isDark(r: number, c: number): boolean {
|
|
548
|
+
return modules[r]![c] === true
|
|
549
|
+
}
|
|
550
|
+
|
|
547
551
|
for (let row = 0; row < size; row++) {
|
|
548
552
|
for (let col = 0; col < size - 6; col++) {
|
|
549
553
|
if (
|
package/src/semver.ts
CHANGED
|
@@ -138,7 +138,11 @@ class SemverFactory {
|
|
|
138
138
|
*/
|
|
139
139
|
sort(items: Semver[], opt: SortOptions = {}): Semver[] {
|
|
140
140
|
const mod = opt.dir === 'desc' ? -1 : 1
|
|
141
|
-
|
|
141
|
+
|
|
142
|
+
function cmp(a: Semver, b: Semver): number {
|
|
143
|
+
return a.compare(b) * mod
|
|
144
|
+
}
|
|
145
|
+
|
|
142
146
|
return opt.mutate ? items.sort(cmp) : items.toSorted(cmp)
|
|
143
147
|
}
|
|
144
148
|
}
|
package/src/string/slugify.ts
CHANGED
|
@@ -61,16 +61,14 @@ function buildPatternSlug(options: any): RegExp {
|
|
|
61
61
|
let negationSetPattern = String.raw`a-z\d`
|
|
62
62
|
negationSetPattern += options.lowercase ? '' : 'A-Z'
|
|
63
63
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
negationSetPattern += escapeStringRegexp(character)
|
|
64
|
+
for (const character of options.preserveCharacters) {
|
|
65
|
+
if (character === options.separator) {
|
|
66
|
+
throw new Error(
|
|
67
|
+
`The separator character \`${options.separator}\` cannot be included in preserved characters: ${options.preserveCharacters}`,
|
|
68
|
+
)
|
|
73
69
|
}
|
|
70
|
+
|
|
71
|
+
negationSetPattern += escapeStringRegexp(character)
|
|
74
72
|
}
|
|
75
73
|
|
|
76
74
|
return new RegExp(`[^${negationSetPattern}]+`, 'g')
|
package/src/types.ts
CHANGED
|
@@ -182,7 +182,9 @@ export const _passUndefinedMapper: IndexedMapper<any, void> = () => undefined
|
|
|
182
182
|
/**
|
|
183
183
|
* Function that does nothings and returns `undefined`.
|
|
184
184
|
*/
|
|
185
|
-
export
|
|
185
|
+
export function _noop(..._args: any[]): undefined {
|
|
186
|
+
return undefined
|
|
187
|
+
}
|
|
186
188
|
|
|
187
189
|
export type Predicate<T> = (item: T, index: number) => boolean
|
|
188
190
|
export type AsyncPredicate<T> = (item: T, index: number) => PromiseLike<boolean>
|