@abtnode/util 1.16.13-beta-0656f92f → 1.16.13-beta-2eb54cbc
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/dayjs.js +18 -0
- package/lib/log.js +2 -1
- package/lib/port.js +126 -0
- package/package.json +6 -6
- package/lib/kill-process-occupied-ports.js +0 -34
package/lib/dayjs.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const dayjs = require('dayjs');
|
|
2
|
+
|
|
3
|
+
require('dayjs/locale/en');
|
|
4
|
+
require('dayjs/locale/zh');
|
|
5
|
+
|
|
6
|
+
const relativeTime = require('dayjs/plugin/relativeTime');
|
|
7
|
+
const localizedFormat = require('dayjs/plugin/localizedFormat');
|
|
8
|
+
const duration = require('dayjs/plugin/duration');
|
|
9
|
+
const utc = require('dayjs/plugin/utc');
|
|
10
|
+
const timezone = require('dayjs/plugin/timezone'); // dependent on utc plugin
|
|
11
|
+
|
|
12
|
+
dayjs.extend(relativeTime);
|
|
13
|
+
dayjs.extend(localizedFormat);
|
|
14
|
+
dayjs.extend(duration);
|
|
15
|
+
dayjs.extend(utc);
|
|
16
|
+
dayjs.extend(timezone);
|
|
17
|
+
|
|
18
|
+
module.exports = dayjs;
|
package/lib/log.js
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
const path = require('path');
|
|
3
3
|
const createArchive = require('archiver');
|
|
4
4
|
const fs = require('fs-extra');
|
|
5
|
-
const dayjs = require('dayjs');
|
|
6
5
|
const glob = require('fast-glob');
|
|
7
6
|
const isEqual = require('lodash/isEqual');
|
|
8
7
|
const { Tail } = require('tail');
|
|
@@ -10,6 +9,8 @@ const readLastLines = require('read-last-lines');
|
|
|
10
9
|
const { BLOCKLET_MODES } = require('@blocklet/constant');
|
|
11
10
|
const logger = require('@abtnode/logger')(require('../package.json').name);
|
|
12
11
|
|
|
12
|
+
const dayjs = require('./dayjs');
|
|
13
|
+
|
|
13
14
|
class StreamLog {
|
|
14
15
|
constructor() {
|
|
15
16
|
this._files = null; // Object { <level>: <filePath> }
|
package/lib/port.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
const net = require('net');
|
|
2
|
+
const killProcess = require('fkill');
|
|
3
|
+
const getPm2ProcessInfo = require('./get-pm2-process-info');
|
|
4
|
+
|
|
5
|
+
const noop = () => {};
|
|
6
|
+
|
|
7
|
+
const killProcessOccupiedPorts = async ({ pm2ProcessId, ports = {}, printError = noop } = {}) => {
|
|
8
|
+
if (!pm2ProcessId) {
|
|
9
|
+
throw new Error('pm2ProcessId is required');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const info = await getPm2ProcessInfo(pm2ProcessId, { throwOnNotExist: false });
|
|
13
|
+
await Promise.all(
|
|
14
|
+
Object.entries(ports).map(async ([envName, configPort]) => {
|
|
15
|
+
if (!configPort) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const pm2ProcessPort = (info?.pm2_env?.env || {})[envName];
|
|
20
|
+
const pm2ProcessExist = pm2ProcessPort === configPort;
|
|
21
|
+
|
|
22
|
+
if (pm2ProcessExist) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
await killProcess(`:${configPort}`, { silent: true, force: true });
|
|
28
|
+
} catch (error) {
|
|
29
|
+
printError('Failed to kill process', { error });
|
|
30
|
+
}
|
|
31
|
+
})
|
|
32
|
+
);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const isPortTaken = (port) =>
|
|
36
|
+
new Promise((resolve, reject) => {
|
|
37
|
+
const tester = net
|
|
38
|
+
.createServer()
|
|
39
|
+
.once('error', (err) => {
|
|
40
|
+
if (err.code !== 'EADDRINUSE') {
|
|
41
|
+
reject(err);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
resolve(true);
|
|
45
|
+
})
|
|
46
|
+
.once('listening', () => {
|
|
47
|
+
tester.once('close', () => resolve(false)).close();
|
|
48
|
+
})
|
|
49
|
+
.listen(port);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @param {{
|
|
54
|
+
* [key: string]: number;
|
|
55
|
+
* }} ports
|
|
56
|
+
* @param {{
|
|
57
|
+
* blackList?: number[];
|
|
58
|
+
* range?: [number, number];
|
|
59
|
+
* }} options
|
|
60
|
+
* @returns {
|
|
61
|
+
* Promise<{
|
|
62
|
+
* [key: string]: number;
|
|
63
|
+
* }>
|
|
64
|
+
* }
|
|
65
|
+
*/
|
|
66
|
+
const refreshPorts = async (ports, { blackList = [], range = [10000, 65535] } = {}) => {
|
|
67
|
+
const res = {};
|
|
68
|
+
const blackListPorts = [...Object.values(ports), ...blackList];
|
|
69
|
+
|
|
70
|
+
for (const key of Object.keys(ports)) {
|
|
71
|
+
let found = false;
|
|
72
|
+
while (!found) {
|
|
73
|
+
const port = Math.floor(Math.random() * [range[1] - range[0]]) + range[0];
|
|
74
|
+
|
|
75
|
+
if (!blackListPorts.includes(port)) {
|
|
76
|
+
// eslint-disable-next-line no-await-in-loop
|
|
77
|
+
const isTaken = await isPortTaken(port);
|
|
78
|
+
if (!isTaken) {
|
|
79
|
+
res[key] = port;
|
|
80
|
+
blackListPorts.push(port);
|
|
81
|
+
found = true;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return res;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const isPortsOccupiedByOtherProcess = async ({ pm2ProcessId, ports = {} } = {}) => {
|
|
91
|
+
if (!pm2ProcessId) {
|
|
92
|
+
throw new Error('pm2ProcessId is required');
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const info = await getPm2ProcessInfo(pm2ProcessId, { throwOnNotExist: false });
|
|
96
|
+
|
|
97
|
+
let occupied = false;
|
|
98
|
+
await Promise.all(
|
|
99
|
+
Object.entries(ports).map(async ([envName, configPort]) => {
|
|
100
|
+
if (!configPort) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const pm2ProcessPort = (info?.pm2_env?.env || {})[envName];
|
|
105
|
+
const pm2ProcessExist = pm2ProcessPort === configPort;
|
|
106
|
+
|
|
107
|
+
if (pm2ProcessExist) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const isYes = await isPortTaken(configPort);
|
|
112
|
+
if (isYes) {
|
|
113
|
+
occupied = isYes;
|
|
114
|
+
}
|
|
115
|
+
})
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
return occupied;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
module.exports = {
|
|
122
|
+
isPortsOccupiedByOtherProcess,
|
|
123
|
+
killProcessOccupiedPorts,
|
|
124
|
+
isPortTaken,
|
|
125
|
+
refreshPorts,
|
|
126
|
+
};
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.16.13-beta-
|
|
6
|
+
"version": "1.16.13-beta-2eb54cbc",
|
|
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": "Apache-2.0",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@abtnode/constant": "1.16.13-beta-
|
|
22
|
-
"@abtnode/logger": "1.16.13-beta-
|
|
23
|
-
"@blocklet/constant": "1.16.13-beta-
|
|
21
|
+
"@abtnode/constant": "1.16.13-beta-2eb54cbc",
|
|
22
|
+
"@abtnode/logger": "1.16.13-beta-2eb54cbc",
|
|
23
|
+
"@blocklet/constant": "1.16.13-beta-2eb54cbc",
|
|
24
24
|
"@ocap/client": "1.18.84",
|
|
25
25
|
"@ocap/mcrypto": "1.18.84",
|
|
26
26
|
"@ocap/util": "1.18.84",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"cookie": "^0.5.0",
|
|
33
33
|
"cookie-parser": "^1.4.6",
|
|
34
34
|
"cross-spawn": "^7.0.3",
|
|
35
|
-
"dayjs": "^1.11.
|
|
35
|
+
"dayjs": "^1.11.8",
|
|
36
36
|
"debug": "^4.3.4",
|
|
37
37
|
"fast-glob": "^3.2.12",
|
|
38
38
|
"find-up": "^5.0.0",
|
|
@@ -74,5 +74,5 @@
|
|
|
74
74
|
"jest": "^27.5.1",
|
|
75
75
|
"unzipper": "^0.10.11"
|
|
76
76
|
},
|
|
77
|
-
"gitHead": "
|
|
77
|
+
"gitHead": "7adf96c2eb31762d8eb14b8121fce9865d6a458c"
|
|
78
78
|
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
const killProcess = require('fkill');
|
|
2
|
-
const getPm2ProcessInfo = require('./get-pm2-process-info');
|
|
3
|
-
|
|
4
|
-
const noop = () => {};
|
|
5
|
-
|
|
6
|
-
const killProcessOccupiedPorts = async ({ pm2ProcessId, ports = {}, printError = noop } = {}) => {
|
|
7
|
-
if (!pm2ProcessId) {
|
|
8
|
-
throw new Error('pm2ProcessId is required');
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
const info = await getPm2ProcessInfo(pm2ProcessId, { throwOnNotExist: false });
|
|
12
|
-
await Promise.all(
|
|
13
|
-
Object.entries(ports).map(async ([envName, configPort]) => {
|
|
14
|
-
if (!configPort) {
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const pm2ProcessPort = (info?.pm2_env?.env || {})[envName];
|
|
19
|
-
const pm2ProcessExist = pm2ProcessPort === configPort;
|
|
20
|
-
|
|
21
|
-
if (pm2ProcessExist) {
|
|
22
|
-
return;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
try {
|
|
26
|
-
await killProcess(`:${configPort}`, { silent: true, force: true });
|
|
27
|
-
} catch (error) {
|
|
28
|
-
printError('Failed to kill process', { error });
|
|
29
|
-
}
|
|
30
|
-
})
|
|
31
|
-
);
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
module.exports = killProcessOccupiedPorts;
|