@abtnode/util 1.16.43-beta-20250424-125523-08a65a5c → 1.16.43-beta-20250425-130658-8da18f4d
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/get-origin.js +3 -3
- package/lib/single-flight-lru-cache.js +80 -0
- package/package.json +7 -5
package/lib/get-origin.js
CHANGED
|
@@ -2,11 +2,11 @@ const defaultLogger = {
|
|
|
2
2
|
error: console.error,
|
|
3
3
|
};
|
|
4
4
|
|
|
5
|
-
async function getOrigin({ req }, { printError = defaultLogger.error } = {}) {
|
|
5
|
+
async function getOrigin({ req, blockletInfo }, { printError = defaultLogger.error } = {}) {
|
|
6
6
|
try {
|
|
7
7
|
if (req.getBlockletInfo) {
|
|
8
|
-
const
|
|
9
|
-
return
|
|
8
|
+
const _blockletInfo = blockletInfo || (await req.getBlockletInfo());
|
|
9
|
+
return _blockletInfo.appUrl;
|
|
10
10
|
}
|
|
11
11
|
const host = req.get('host');
|
|
12
12
|
return `https://${host}`;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
const { LRUCache } = require('lru-cache');
|
|
2
|
+
const pWaitFor = require('p-wait-for');
|
|
3
|
+
|
|
4
|
+
const PADDING_VALUE = '__padding__';
|
|
5
|
+
|
|
6
|
+
// Single flight lru cache
|
|
7
|
+
// ref: https://www.codingexplorations.com/blog/understanding-singleflight-in-golang-a-solution-for-eliminating-redundant-work
|
|
8
|
+
class SingleFlightLRUCache extends LRUCache {
|
|
9
|
+
paddingInterval = 100;
|
|
10
|
+
|
|
11
|
+
paddingTimeout = 10 * 1000;
|
|
12
|
+
|
|
13
|
+
setPadding(key) {
|
|
14
|
+
this.set(key, PADDING_VALUE);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
isPadding(key) {
|
|
18
|
+
return this.get(key) === PADDING_VALUE;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async waitPending(key) {
|
|
22
|
+
if (this.get(key) === PADDING_VALUE) {
|
|
23
|
+
try {
|
|
24
|
+
await pWaitFor(() => this.get(key) !== PADDING_VALUE, {
|
|
25
|
+
interval: this.paddingInterval,
|
|
26
|
+
timeout: this.paddingTimeout,
|
|
27
|
+
});
|
|
28
|
+
} catch (_) {
|
|
29
|
+
if (this.get(key) === PADDING_VALUE) {
|
|
30
|
+
this.delete(key);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return this.get(key);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async wrapPending(key, fn) {
|
|
38
|
+
if (!key) {
|
|
39
|
+
return Promise.resolve(fn());
|
|
40
|
+
}
|
|
41
|
+
const old = this.get(key);
|
|
42
|
+
if (old) {
|
|
43
|
+
if (old === PADDING_VALUE) {
|
|
44
|
+
const result = await this.waitPending(key);
|
|
45
|
+
if (result && result !== PADDING_VALUE) {
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
} else {
|
|
49
|
+
return old;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
this.setPadding(key);
|
|
53
|
+
try {
|
|
54
|
+
const result = await Promise.resolve(fn());
|
|
55
|
+
return result;
|
|
56
|
+
} finally {
|
|
57
|
+
if (this.get(key) === PADDING_VALUE) {
|
|
58
|
+
this.delete(key);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* autoCache will set the result to the cache if the key is not null
|
|
65
|
+
* if the key is null, it will return the result of the function
|
|
66
|
+
* if the key is not null, it will return the cached result
|
|
67
|
+
* if padding, it will wait for the padding to finish
|
|
68
|
+
* if the key is not in the cache, it will set the result to the cache
|
|
69
|
+
* if the key is in the cache, it will return the cached result
|
|
70
|
+
*/
|
|
71
|
+
async autoCache(key, fn) {
|
|
72
|
+
const result = await this.wrapPending(key, fn);
|
|
73
|
+
if (key) {
|
|
74
|
+
this.set(key, result);
|
|
75
|
+
}
|
|
76
|
+
return result;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
module.exports = SingleFlightLRUCache;
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.16.43-beta-
|
|
6
|
+
"version": "1.16.43-beta-20250425-130658-8da18f4d",
|
|
7
7
|
"description": "ArcBlock's JavaScript utility",
|
|
8
8
|
"main": "lib/index.js",
|
|
9
9
|
"files": [
|
|
@@ -18,11 +18,11 @@
|
|
|
18
18
|
"author": "polunzh <polunzh@gmail.com> (http://github.com/polunzh)",
|
|
19
19
|
"license": "Apache-2.0",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@abtnode/constant": "1.16.43-beta-
|
|
21
|
+
"@abtnode/constant": "1.16.43-beta-20250425-130658-8da18f4d",
|
|
22
22
|
"@arcblock/did": "^1.20.2",
|
|
23
23
|
"@arcblock/pm2": "^5.4.0",
|
|
24
|
-
"@blocklet/constant": "1.16.43-beta-
|
|
25
|
-
"@blocklet/meta": "1.16.43-beta-
|
|
24
|
+
"@blocklet/constant": "1.16.43-beta-20250425-130658-8da18f4d",
|
|
25
|
+
"@blocklet/meta": "1.16.43-beta-20250425-130658-8da18f4d",
|
|
26
26
|
"@ocap/client": "1.20.2",
|
|
27
27
|
"@ocap/mcrypto": "1.20.2",
|
|
28
28
|
"@ocap/util": "1.20.2",
|
|
@@ -53,10 +53,12 @@
|
|
|
53
53
|
"is-url": "^1.2.4",
|
|
54
54
|
"json-stable-stringify": "^1.0.1",
|
|
55
55
|
"lodash": "^4.17.21",
|
|
56
|
+
"lru-cache": "^11.0.2",
|
|
56
57
|
"minimatch": "^10.0.1",
|
|
57
58
|
"multiformats": "9.9.0",
|
|
58
59
|
"npm-packlist": "^7.0.4",
|
|
59
60
|
"p-retry": "^4.6.2",
|
|
61
|
+
"p-wait-for": "^3.2.0",
|
|
60
62
|
"parallel-transform": "^1.2.0",
|
|
61
63
|
"public-ip": "^4.0.4",
|
|
62
64
|
"pump": "^3.0.0",
|
|
@@ -84,5 +86,5 @@
|
|
|
84
86
|
"fs-extra": "^11.2.0",
|
|
85
87
|
"jest": "^29.7.0"
|
|
86
88
|
},
|
|
87
|
-
"gitHead": "
|
|
89
|
+
"gitHead": "b5195a0290d5ced00bb716a709548b1a56356134"
|
|
88
90
|
}
|