@abtnode/util 1.6.20 → 1.6.23
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/can-pkg-rw.js +1 -1
- package/lib/locate-npm-global-by-binary.js +16 -6
- package/lib/sort-priority-url.js +109 -0
- package/package.json +5 -5
package/lib/can-pkg-rw.js
CHANGED
|
@@ -3,7 +3,7 @@ const locateNpmGlobalByBinary = require('./locate-npm-global-by-binary');
|
|
|
3
3
|
const { canReadAndWriteDir } = require('./fs');
|
|
4
4
|
|
|
5
5
|
module.exports = (cmdName, packageName) => {
|
|
6
|
-
const installDir = locateNpmGlobalByBinary(cmdName);
|
|
6
|
+
const installDir = locateNpmGlobalByBinary(cmdName, packageName);
|
|
7
7
|
if (!installDir) {
|
|
8
8
|
throw new Error(`${packageName} is not installed as a global package`);
|
|
9
9
|
}
|
|
@@ -1,22 +1,32 @@
|
|
|
1
|
-
/* eslint-disable no-unused-vars, no-console */
|
|
2
|
-
|
|
3
1
|
const fs = require('fs');
|
|
4
2
|
const path = require('path');
|
|
5
|
-
const
|
|
3
|
+
const shell = require('shelljs');
|
|
6
4
|
|
|
7
|
-
module.exports = (binaryName) => {
|
|
5
|
+
module.exports = (binaryName, packageName) => {
|
|
8
6
|
if (!binaryName) {
|
|
9
7
|
throw new Error('binaryName is required');
|
|
10
8
|
}
|
|
11
9
|
|
|
12
|
-
|
|
10
|
+
let result = shell.which(binaryName);
|
|
13
11
|
if (!result || !result.stdout) {
|
|
14
12
|
return '';
|
|
15
13
|
}
|
|
16
14
|
|
|
17
|
-
const binPath = result.stdout;
|
|
15
|
+
const binPath = result.stdout.trim();
|
|
18
16
|
|
|
17
|
+
// Check if the package is installed via pnpm: only if we have packageName set
|
|
19
18
|
let binDir = path.dirname(fs.realpathSync(binPath));
|
|
19
|
+
if (packageName) {
|
|
20
|
+
const { stdout: pnpmPath } = shell.which('pnpm');
|
|
21
|
+
if (pnpmPath) {
|
|
22
|
+
result = shell.exec('pnpm bin -g', { silent: true });
|
|
23
|
+
const pnpmBinDir = result.stdout.trim();
|
|
24
|
+
if (binDir === pnpmBinDir) {
|
|
25
|
+
result = shell.exec(`pnpm root -g ${packageName}`, { silent: true });
|
|
26
|
+
return path.join(result.stdout.trim(), packageName);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
20
30
|
|
|
21
31
|
while (binDir && binDir !== path.sep && path.basename(binDir) !== 'node_modules') {
|
|
22
32
|
const packageJsonPath = path.join(binDir, 'package.json');
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
const http = require('http');
|
|
2
|
+
const https = require('https');
|
|
3
|
+
|
|
4
|
+
// 根据url获取状态码
|
|
5
|
+
// 更换使用XMLHttpRequest可兼容浏览器
|
|
6
|
+
const getStatusCode = (url, timeout = 5000) =>
|
|
7
|
+
new Promise((resolve, reject) => {
|
|
8
|
+
const requestFunc = url.startsWith('https://') ? https : http;
|
|
9
|
+
|
|
10
|
+
let timer;
|
|
11
|
+
try {
|
|
12
|
+
const resp = requestFunc
|
|
13
|
+
.get(url, (res) => {
|
|
14
|
+
res.destroy();
|
|
15
|
+
clearTimeout(timer);
|
|
16
|
+
resolve(res.statusCode);
|
|
17
|
+
})
|
|
18
|
+
.on('error', () => {
|
|
19
|
+
clearTimeout(timer);
|
|
20
|
+
reject();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
// 超时终止响应
|
|
24
|
+
timer = setTimeout(() => resp.destroy(new Error('access link timed out')), timeout);
|
|
25
|
+
} catch (err) {
|
|
26
|
+
// 报错属于完全不能访问的网页,优先级最低
|
|
27
|
+
clearTimeout(timer);
|
|
28
|
+
reject();
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Filter an optimal address from the array
|
|
34
|
+
* 对urls进行排序,按照以下顺序
|
|
35
|
+
* 优先级最高:能正常访问的地址 => 有响应但不正常(404) => 异常地址(error)
|
|
36
|
+
* 次优先级:https => 自定义域名 => 域名级别(一级>二级>...) => ip echo 地址 => http => 自定义...
|
|
37
|
+
*
|
|
38
|
+
* @param {Array} urls array of urls
|
|
39
|
+
* @param {Number} timeout request max time (ms)
|
|
40
|
+
* @returns url object
|
|
41
|
+
*/
|
|
42
|
+
const sortPriorityUrl = async (urls, timeout = 5000) => {
|
|
43
|
+
const urlsData = urls.map((e) => {
|
|
44
|
+
// 先进行计分,https优先级最高
|
|
45
|
+
const { protocol, port, hostname } = new URL(e.url);
|
|
46
|
+
|
|
47
|
+
let score = protocol === 'https:' ? 1000 : 0;
|
|
48
|
+
|
|
49
|
+
if (hostname.endsWith('.ip.abtnet.io')) {
|
|
50
|
+
// ip echo
|
|
51
|
+
score += 20;
|
|
52
|
+
} else {
|
|
53
|
+
// 带端口号
|
|
54
|
+
if (port) {
|
|
55
|
+
// 带端口优先权放后
|
|
56
|
+
score -= 1;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/.test(hostname)) {
|
|
60
|
+
// 纯ip地址
|
|
61
|
+
score += 1;
|
|
62
|
+
} else {
|
|
63
|
+
// 自定义域名
|
|
64
|
+
score += 200;
|
|
65
|
+
|
|
66
|
+
// 子层越多越靠后
|
|
67
|
+
score -= hostname.split('.').length;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
score,
|
|
73
|
+
url: e.url,
|
|
74
|
+
};
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// 不能访问的地址靠后
|
|
78
|
+
await Promise.all(
|
|
79
|
+
urlsData.map(
|
|
80
|
+
(e) =>
|
|
81
|
+
new Promise((resolve) => {
|
|
82
|
+
getStatusCode(e.url, timeout)
|
|
83
|
+
.then((statusCode) => {
|
|
84
|
+
if (/^4\d{2}/.test(statusCode)) {
|
|
85
|
+
e.score -= 10000; // 虽然4xx,起码有响应,扣分相对error少
|
|
86
|
+
}
|
|
87
|
+
e.status = statusCode;
|
|
88
|
+
resolve(e);
|
|
89
|
+
})
|
|
90
|
+
.catch(() => {
|
|
91
|
+
// 报错属于完全不能访问的网页,优先级最低
|
|
92
|
+
e.score -= 20000;
|
|
93
|
+
resolve(e);
|
|
94
|
+
});
|
|
95
|
+
})
|
|
96
|
+
)
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
// 根据分数进行排序
|
|
100
|
+
const sortedUrls = urlsData
|
|
101
|
+
.sort((a, b) => b.score - a.score)
|
|
102
|
+
.map((e) => {
|
|
103
|
+
return { url: e.url, status: e.status };
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
return sortedUrls;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
module.exports = sortPriorityUrl;
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.6.
|
|
6
|
+
"version": "1.6.23",
|
|
7
7
|
"description": "ArcBlock's JavaScript utility",
|
|
8
8
|
"main": "lib/index.js",
|
|
9
9
|
"files": [
|
|
@@ -18,9 +18,9 @@
|
|
|
18
18
|
"author": "polunzh <polunzh@gmail.com> (http://github.com/polunzh)",
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@ocap/mcrypto": "^1.14.
|
|
22
|
-
"@ocap/util": "^1.14.
|
|
23
|
-
"@ocap/wallet": "^1.14.
|
|
21
|
+
"@ocap/mcrypto": "^1.14.19",
|
|
22
|
+
"@ocap/util": "^1.14.19",
|
|
23
|
+
"@ocap/wallet": "^1.14.19",
|
|
24
24
|
"axios": "^0.25.0",
|
|
25
25
|
"axios-mock-adapter": "^1.20.0",
|
|
26
26
|
"axon": "^2.0.3",
|
|
@@ -54,5 +54,5 @@
|
|
|
54
54
|
"fs-extra": "^10.0.0",
|
|
55
55
|
"jest": "^27.4.5"
|
|
56
56
|
},
|
|
57
|
-
"gitHead": "
|
|
57
|
+
"gitHead": "6c478fb7e2a30b302981b5339f349c69134e022e"
|
|
58
58
|
}
|