node-karin 1.15.5 → 1.16.1
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/CHANGELOG.md +31 -0
- package/dist/adapter-BqlH3u3X.mjs +218 -0
- package/dist/app-DdMQbBEY.mjs +4109 -0
- package/dist/cache-CPcPeo6N.mjs +163 -0
- package/dist/chunk-NzVPYdc1.mjs +21 -0
- package/dist/cli/index.cjs +10900 -1
- package/dist/cli/index.d.ts +1 -1
- package/dist/cli/index.mjs +10770 -10224
- package/dist/file-ZGuqNDd-.mjs +15987 -0
- package/dist/file-dGy9of8-.mjs +268 -0
- package/dist/fsSync-Cf5MWILk.mjs +65 -0
- package/dist/index.d.ts +12235 -12738
- package/dist/index.mjs +2054 -25247
- package/dist/internal-DupfycKE.mjs +597 -0
- package/dist/kv-DZp4UIxg.mjs +192 -0
- package/dist/module/art-template.d.ts +2 -13
- package/dist/module/art-template.mjs +3 -1
- package/dist/module/axios.d.ts +3 -2
- package/dist/module/axios.mjs +5 -2
- package/dist/module/chalk.d.ts +3 -2
- package/dist/module/chalk.mjs +5 -2
- package/dist/module/chokidar.d.ts +3 -2
- package/dist/module/chokidar.mjs +5 -2
- package/dist/module/express.d.ts +2 -1
- package/dist/module/express.mjs +3 -1
- package/dist/module/lodash.d.ts +2 -1
- package/dist/module/lodash.mjs +3 -1
- package/dist/module/log4js.d.ts +3 -2
- package/dist/module/log4js.mjs +5 -2
- package/dist/module/moment.d.ts +2 -1
- package/dist/module/moment.mjs +3 -1
- package/dist/module/node-schedule.d.ts +3 -2
- package/dist/module/node-schedule.mjs +5 -2
- package/dist/module/redis.d.ts +3 -2
- package/dist/module/redis.mjs +5 -2
- package/dist/module/sqlite3.d.ts +3 -2
- package/dist/module/sqlite3.mjs +5 -2
- package/dist/module/ws.d.ts +3 -2
- package/dist/module/ws.mjs +5 -2
- package/dist/module/yaml.d.ts +3 -2
- package/dist/module/yaml.mjs +5 -2
- package/dist/queue-CnKedaZA.mjs +70 -0
- package/dist/redis-aLJ7wbJH.mjs +1556 -0
- package/dist/render-DPqueDZr.mjs +170 -0
- package/dist/root.d.ts +46 -46
- package/dist/root.mjs +136 -93
- package/dist/router-zPSN9-tY.mjs +124 -0
- package/dist/server-DT64D-m-.mjs +38 -0
- package/dist/snapka-BTlnZOyI.mjs +450 -0
- package/dist/sqlite-Dcj9jlW9.mjs +307 -0
- package/dist/start/app.d.ts +1 -1
- package/dist/start/app.mjs +14 -7
- package/dist/start/index.d.ts +1 -1
- package/dist/start/index.mjs +325 -656
- package/dist/template-Djk6y0uC.mjs +133 -0
- package/dist/terminalManager-Lxa8Sm06.mjs +783 -0
- package/dist/uptime-C121X_rq.mjs +210 -0
- package/dist/web/{CompressaPRO-GX.woff2.br → CompressaPRO-GX.woff2} +0 -0
- package/dist/web/assets/css/style-CBB8wM_W.css +14880 -0
- package/dist/web/assets/js/entry-Blf4Trpx.js +258540 -0
- package/dist/web/{googleapis.woff2.br → googleapis.woff2} +0 -0
- package/dist/web/index.html +2 -15
- package/dist/web/karin.png +0 -0
- package/dist/web/sha256.min.js +9 -0
- package/dist/ws-BLDoC2gV.mjs +80 -0
- package/dist/ws-CcoWd3Ar.mjs +106 -0
- package/package.json +7 -7
- package/dist/global.d.d.ts +0 -68
- package/dist/types-hAhbXJDZ.d.ts +0 -109
- package/dist/web/assets/css/components-ep7vm38G.css +0 -1
- package/dist/web/assets/css/index-Dadvd9mn.css.br +0 -0
- package/dist/web/assets/css/vendor-editor-CFbL2ovg.css.br +0 -0
- package/dist/web/assets/css/vendor-others-ZgkIHsf0.css +0 -1
- package/dist/web/assets/js/components-CU2xw4lY.js.br +0 -0
- package/dist/web/assets/js/entry-Dvb7eYLE.js.br +0 -0
- package/dist/web/assets/js/hooks-CRfhs4ON.js.br +0 -0
- package/dist/web/assets/js/page-404.tsx-DYMd_RI_.js +0 -1
- package/dist/web/assets/js/page-dashboard-CG60V_Z-.js.br +0 -0
- package/dist/web/assets/js/page-loading.tsx-wY8a9me3.js.br +0 -0
- package/dist/web/assets/js/page-login.tsx-B54ZOEZB.js.br +0 -0
- package/dist/web/assets/js/utils-C9nWTSuo.js +0 -2
- package/dist/web/assets/js/vendor-editor-BmqYP7lh.js.br +0 -0
- package/dist/web/assets/js/vendor-heroui-ClBCy2zk.js.br +0 -0
- package/dist/web/assets/js/vendor-others-6GiMrjd4.js.br +0 -0
- package/dist/web/assets/js/vendor-react-Dc9jdQiK.js.br +0 -0
- package/dist/web/assets/js/vendor-ui-utils-D0xkboLL.js.br +0 -0
- package/dist/web/assets/js/vendor-visual-saF8KLH_.js.br +0 -0
- package/dist/web/karin.png.br +0 -0
- package/dist/web/sha256.min.js.br +0 -0
package/dist/start/index.mjs
CHANGED
|
@@ -1,660 +1,329 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
import { createServer } from 'node:net';
|
|
9
|
-
import { fileURLToPath } from 'node:url';
|
|
10
|
-
import { fork } from 'node:child_process';
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import dotenv from "dotenv";
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import { fork } from "node:child_process";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import os from "node:os";
|
|
7
|
+
import { createServer } from "node:net";
|
|
11
8
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
if (typeof e !== "string" && !Array.isArray(e)) {
|
|
17
|
-
for (const k in e) {
|
|
18
|
-
if (k !== "default" && !(k in n)) {
|
|
19
|
-
const d = Object.getOwnPropertyDescriptor(e, k);
|
|
20
|
-
if (d) {
|
|
21
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
22
|
-
enumerable: true,
|
|
23
|
-
get: () => e[k]
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
return Object.freeze(Object.defineProperty(n, Symbol.toStringTag, { value: "Module" }));
|
|
31
|
-
}
|
|
32
|
-
function getDefaultExportFromCjs(x) {
|
|
33
|
-
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, "default") ? x["default"] : x;
|
|
34
|
-
}
|
|
35
|
-
var main$1 = { exports: {} };
|
|
36
|
-
var version = "16.5.0";
|
|
37
|
-
var require$$4 = {
|
|
38
|
-
version
|
|
39
|
-
};
|
|
40
|
-
var hasRequiredMain;
|
|
41
|
-
function requireMain() {
|
|
42
|
-
if (hasRequiredMain) return main$1.exports;
|
|
43
|
-
hasRequiredMain = 1;
|
|
44
|
-
const fs2 = require$$0;
|
|
45
|
-
const path2 = require$$1;
|
|
46
|
-
const os2 = require$$2;
|
|
47
|
-
const crypto = require$$3;
|
|
48
|
-
const packageJson = require$$4;
|
|
49
|
-
const version2 = packageJson.version;
|
|
50
|
-
const LINE = /(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/mg;
|
|
51
|
-
function parse2(src) {
|
|
52
|
-
const obj = {};
|
|
53
|
-
let lines = src.toString();
|
|
54
|
-
lines = lines.replace(/\r\n?/mg, "\n");
|
|
55
|
-
let match;
|
|
56
|
-
while ((match = LINE.exec(lines)) != null) {
|
|
57
|
-
const key = match[1];
|
|
58
|
-
let value = match[2] || "";
|
|
59
|
-
value = value.trim();
|
|
60
|
-
const maybeQuote = value[0];
|
|
61
|
-
value = value.replace(/^(['"`])([\s\S]*)\1$/mg, "$2");
|
|
62
|
-
if (maybeQuote === '"') {
|
|
63
|
-
value = value.replace(/\\n/g, "\n");
|
|
64
|
-
value = value.replace(/\\r/g, "\r");
|
|
65
|
-
}
|
|
66
|
-
obj[key] = value;
|
|
67
|
-
}
|
|
68
|
-
return obj;
|
|
69
|
-
}
|
|
70
|
-
function _parseVault2(options) {
|
|
71
|
-
const vaultPath = _vaultPath(options);
|
|
72
|
-
const result = DotenvModule.configDotenv({ path: vaultPath });
|
|
73
|
-
if (!result.parsed) {
|
|
74
|
-
const err = new Error(`MISSING_DATA: Cannot parse ${vaultPath} for an unknown reason`);
|
|
75
|
-
err.code = "MISSING_DATA";
|
|
76
|
-
throw err;
|
|
77
|
-
}
|
|
78
|
-
const keys = _dotenvKey(options).split(",");
|
|
79
|
-
const length = keys.length;
|
|
80
|
-
let decrypted;
|
|
81
|
-
for (let i = 0; i < length; i++) {
|
|
82
|
-
try {
|
|
83
|
-
const key = keys[i].trim();
|
|
84
|
-
const attrs = _instructions(result, key);
|
|
85
|
-
decrypted = DotenvModule.decrypt(attrs.ciphertext, attrs.key);
|
|
86
|
-
break;
|
|
87
|
-
} catch (error) {
|
|
88
|
-
if (i + 1 >= length) {
|
|
89
|
-
throw error;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
return DotenvModule.parse(decrypted);
|
|
94
|
-
}
|
|
95
|
-
function _warn(message) {
|
|
96
|
-
console.log(`[dotenv@${version2}][WARN] ${message}`);
|
|
97
|
-
}
|
|
98
|
-
function _debug(message) {
|
|
99
|
-
console.log(`[dotenv@${version2}][DEBUG] ${message}`);
|
|
100
|
-
}
|
|
101
|
-
function _dotenvKey(options) {
|
|
102
|
-
if (options && options.DOTENV_KEY && options.DOTENV_KEY.length > 0) {
|
|
103
|
-
return options.DOTENV_KEY;
|
|
104
|
-
}
|
|
105
|
-
if (process.env.DOTENV_KEY && process.env.DOTENV_KEY.length > 0) {
|
|
106
|
-
return process.env.DOTENV_KEY;
|
|
107
|
-
}
|
|
108
|
-
return "";
|
|
109
|
-
}
|
|
110
|
-
function _instructions(result, dotenvKey) {
|
|
111
|
-
let uri;
|
|
112
|
-
try {
|
|
113
|
-
uri = new URL(dotenvKey);
|
|
114
|
-
} catch (error) {
|
|
115
|
-
if (error.code === "ERR_INVALID_URL") {
|
|
116
|
-
const err = new Error("INVALID_DOTENV_KEY: Wrong format. Must be in valid uri format like dotenv://:key_1234@dotenvx.com/vault/.env.vault?environment=development");
|
|
117
|
-
err.code = "INVALID_DOTENV_KEY";
|
|
118
|
-
throw err;
|
|
119
|
-
}
|
|
120
|
-
throw error;
|
|
121
|
-
}
|
|
122
|
-
const key = uri.password;
|
|
123
|
-
if (!key) {
|
|
124
|
-
const err = new Error("INVALID_DOTENV_KEY: Missing key part");
|
|
125
|
-
err.code = "INVALID_DOTENV_KEY";
|
|
126
|
-
throw err;
|
|
127
|
-
}
|
|
128
|
-
const environment = uri.searchParams.get("environment");
|
|
129
|
-
if (!environment) {
|
|
130
|
-
const err = new Error("INVALID_DOTENV_KEY: Missing environment part");
|
|
131
|
-
err.code = "INVALID_DOTENV_KEY";
|
|
132
|
-
throw err;
|
|
133
|
-
}
|
|
134
|
-
const environmentKey = `DOTENV_VAULT_${environment.toUpperCase()}`;
|
|
135
|
-
const ciphertext = result.parsed[environmentKey];
|
|
136
|
-
if (!ciphertext) {
|
|
137
|
-
const err = new Error(`NOT_FOUND_DOTENV_ENVIRONMENT: Cannot locate environment ${environmentKey} in your .env.vault file.`);
|
|
138
|
-
err.code = "NOT_FOUND_DOTENV_ENVIRONMENT";
|
|
139
|
-
throw err;
|
|
140
|
-
}
|
|
141
|
-
return { ciphertext, key };
|
|
142
|
-
}
|
|
143
|
-
function _vaultPath(options) {
|
|
144
|
-
let possibleVaultPath = null;
|
|
145
|
-
if (options && options.path && options.path.length > 0) {
|
|
146
|
-
if (Array.isArray(options.path)) {
|
|
147
|
-
for (const filepath of options.path) {
|
|
148
|
-
if (fs2.existsSync(filepath)) {
|
|
149
|
-
possibleVaultPath = filepath.endsWith(".vault") ? filepath : `${filepath}.vault`;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
} else {
|
|
153
|
-
possibleVaultPath = options.path.endsWith(".vault") ? options.path : `${options.path}.vault`;
|
|
154
|
-
}
|
|
155
|
-
} else {
|
|
156
|
-
possibleVaultPath = path2.resolve(process.cwd(), ".env.vault");
|
|
157
|
-
}
|
|
158
|
-
if (fs2.existsSync(possibleVaultPath)) {
|
|
159
|
-
return possibleVaultPath;
|
|
160
|
-
}
|
|
161
|
-
return null;
|
|
162
|
-
}
|
|
163
|
-
function _resolveHome(envPath) {
|
|
164
|
-
return envPath[0] === "~" ? path2.join(os2.homedir(), envPath.slice(1)) : envPath;
|
|
165
|
-
}
|
|
166
|
-
function _configVault2(options) {
|
|
167
|
-
const debug = Boolean(options && options.debug);
|
|
168
|
-
if (debug) {
|
|
169
|
-
_debug("Loading env from encrypted .env.vault");
|
|
170
|
-
}
|
|
171
|
-
const parsed = DotenvModule._parseVault(options);
|
|
172
|
-
let processEnv = process.env;
|
|
173
|
-
if (options && options.processEnv != null) {
|
|
174
|
-
processEnv = options.processEnv;
|
|
175
|
-
}
|
|
176
|
-
DotenvModule.populate(processEnv, parsed, options);
|
|
177
|
-
return { parsed };
|
|
178
|
-
}
|
|
179
|
-
function configDotenv2(options) {
|
|
180
|
-
const dotenvPath = path2.resolve(process.cwd(), ".env");
|
|
181
|
-
let encoding = "utf8";
|
|
182
|
-
const debug = Boolean(options && options.debug);
|
|
183
|
-
if (options && options.encoding) {
|
|
184
|
-
encoding = options.encoding;
|
|
185
|
-
} else {
|
|
186
|
-
if (debug) {
|
|
187
|
-
_debug("No encoding is specified. UTF-8 is used by default");
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
let optionPaths = [dotenvPath];
|
|
191
|
-
if (options && options.path) {
|
|
192
|
-
if (!Array.isArray(options.path)) {
|
|
193
|
-
optionPaths = [_resolveHome(options.path)];
|
|
194
|
-
} else {
|
|
195
|
-
optionPaths = [];
|
|
196
|
-
for (const filepath of options.path) {
|
|
197
|
-
optionPaths.push(_resolveHome(filepath));
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
let lastError;
|
|
202
|
-
const parsedAll = {};
|
|
203
|
-
for (const path22 of optionPaths) {
|
|
204
|
-
try {
|
|
205
|
-
const parsed = DotenvModule.parse(fs2.readFileSync(path22, { encoding }));
|
|
206
|
-
DotenvModule.populate(parsedAll, parsed, options);
|
|
207
|
-
} catch (e) {
|
|
208
|
-
if (debug) {
|
|
209
|
-
_debug(`Failed to load ${path22} ${e.message}`);
|
|
210
|
-
}
|
|
211
|
-
lastError = e;
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
let processEnv = process.env;
|
|
215
|
-
if (options && options.processEnv != null) {
|
|
216
|
-
processEnv = options.processEnv;
|
|
217
|
-
}
|
|
218
|
-
DotenvModule.populate(processEnv, parsedAll, options);
|
|
219
|
-
if (lastError) {
|
|
220
|
-
return { parsed: parsedAll, error: lastError };
|
|
221
|
-
} else {
|
|
222
|
-
return { parsed: parsedAll };
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
function config2(options) {
|
|
226
|
-
if (_dotenvKey(options).length === 0) {
|
|
227
|
-
return DotenvModule.configDotenv(options);
|
|
228
|
-
}
|
|
229
|
-
const vaultPath = _vaultPath(options);
|
|
230
|
-
if (!vaultPath) {
|
|
231
|
-
_warn(`You set DOTENV_KEY but you are missing a .env.vault file at ${vaultPath}. Did you forget to build it?`);
|
|
232
|
-
return DotenvModule.configDotenv(options);
|
|
233
|
-
}
|
|
234
|
-
return DotenvModule._configVault(options);
|
|
235
|
-
}
|
|
236
|
-
function decrypt2(encrypted, keyStr) {
|
|
237
|
-
const key = Buffer.from(keyStr.slice(-64), "hex");
|
|
238
|
-
let ciphertext = Buffer.from(encrypted, "base64");
|
|
239
|
-
const nonce = ciphertext.subarray(0, 12);
|
|
240
|
-
const authTag = ciphertext.subarray(-16);
|
|
241
|
-
ciphertext = ciphertext.subarray(12, -16);
|
|
242
|
-
try {
|
|
243
|
-
const aesgcm = crypto.createDecipheriv("aes-256-gcm", key, nonce);
|
|
244
|
-
aesgcm.setAuthTag(authTag);
|
|
245
|
-
return `${aesgcm.update(ciphertext)}${aesgcm.final()}`;
|
|
246
|
-
} catch (error) {
|
|
247
|
-
const isRange = error instanceof RangeError;
|
|
248
|
-
const invalidKeyLength = error.message === "Invalid key length";
|
|
249
|
-
const decryptionFailed = error.message === "Unsupported state or unable to authenticate data";
|
|
250
|
-
if (isRange || invalidKeyLength) {
|
|
251
|
-
const err = new Error("INVALID_DOTENV_KEY: It must be 64 characters long (or more)");
|
|
252
|
-
err.code = "INVALID_DOTENV_KEY";
|
|
253
|
-
throw err;
|
|
254
|
-
} else if (decryptionFailed) {
|
|
255
|
-
const err = new Error("DECRYPTION_FAILED: Please check your DOTENV_KEY");
|
|
256
|
-
err.code = "DECRYPTION_FAILED";
|
|
257
|
-
throw err;
|
|
258
|
-
} else {
|
|
259
|
-
throw error;
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
function populate2(processEnv, parsed, options = {}) {
|
|
264
|
-
const debug = Boolean(options && options.debug);
|
|
265
|
-
const override = Boolean(options && options.override);
|
|
266
|
-
if (typeof parsed !== "object") {
|
|
267
|
-
const err = new Error("OBJECT_REQUIRED: Please check the processEnv argument being passed to populate");
|
|
268
|
-
err.code = "OBJECT_REQUIRED";
|
|
269
|
-
throw err;
|
|
270
|
-
}
|
|
271
|
-
for (const key of Object.keys(parsed)) {
|
|
272
|
-
if (Object.prototype.hasOwnProperty.call(processEnv, key)) {
|
|
273
|
-
if (override === true) {
|
|
274
|
-
processEnv[key] = parsed[key];
|
|
275
|
-
}
|
|
276
|
-
if (debug) {
|
|
277
|
-
if (override === true) {
|
|
278
|
-
_debug(`"${key}" is already defined and WAS overwritten`);
|
|
279
|
-
} else {
|
|
280
|
-
_debug(`"${key}" is already defined and was NOT overwritten`);
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
} else {
|
|
284
|
-
processEnv[key] = parsed[key];
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
const DotenvModule = {
|
|
289
|
-
configDotenv: configDotenv2,
|
|
290
|
-
_configVault: _configVault2,
|
|
291
|
-
_parseVault: _parseVault2,
|
|
292
|
-
config: config2,
|
|
293
|
-
decrypt: decrypt2,
|
|
294
|
-
parse: parse2,
|
|
295
|
-
populate: populate2
|
|
296
|
-
};
|
|
297
|
-
main$1.exports.configDotenv = DotenvModule.configDotenv;
|
|
298
|
-
main$1.exports._configVault = DotenvModule._configVault;
|
|
299
|
-
main$1.exports._parseVault = DotenvModule._parseVault;
|
|
300
|
-
main$1.exports.config = DotenvModule.config;
|
|
301
|
-
main$1.exports.decrypt = DotenvModule.decrypt;
|
|
302
|
-
main$1.exports.parse = DotenvModule.parse;
|
|
303
|
-
main$1.exports.populate = DotenvModule.populate;
|
|
304
|
-
main$1.exports = DotenvModule;
|
|
305
|
-
return main$1.exports;
|
|
306
|
-
}
|
|
307
|
-
var mainExports = requireMain();
|
|
308
|
-
var main = /* @__PURE__ */ getDefaultExportFromCjs(mainExports);
|
|
309
|
-
var dotenv = /* @__PURE__ */ _mergeNamespaces({
|
|
310
|
-
__proto__: null,
|
|
311
|
-
default: main
|
|
312
|
-
}, [mainExports]);
|
|
313
|
-
|
|
314
|
-
// ../../node_modules/.pnpm/@karinjs+dotenv@1.1.2/node_modules/@karinjs/dotenv/dist/index.js
|
|
315
|
-
var {
|
|
316
|
-
// @ts-ignore
|
|
317
|
-
_configVault,
|
|
318
|
-
_parseVault
|
|
319
|
-
} = dotenv;
|
|
320
|
-
mainExports.config;
|
|
321
|
-
mainExports.configDotenv;
|
|
322
|
-
mainExports.decrypt;
|
|
323
|
-
mainExports.parse;
|
|
324
|
-
mainExports.populate;
|
|
325
|
-
|
|
326
|
-
// ../../node_modules/.pnpm/@karinjs+dotenv@1.1.2/node_modules/@karinjs/dotenv/index.js
|
|
327
|
-
var app = dotenv.default;
|
|
328
|
-
var dotenv_default = app;
|
|
9
|
+
//#region src/start/index.ts
|
|
10
|
+
/**
|
|
11
|
+
* 进程管理器 - 负责子进程的生命周期管理
|
|
12
|
+
*/
|
|
329
13
|
var ProcessManager = class {
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
const memory = `${Math.round(os.totalmem() / 1024 / 1024 / 1024)}GB`;
|
|
642
|
-
this.log(`\u7CFB\u7EDF\u4FE1\u606F | Node: ${nodeVersion} | \u5E73\u53F0: ${platform} | CPU\u6838\u5FC3: ${cpus} | \u5185\u5B58: ${memory}`);
|
|
643
|
-
}
|
|
644
|
-
/**
|
|
645
|
-
* 日志输出方法
|
|
646
|
-
*/
|
|
647
|
-
log(message) {
|
|
648
|
-
console.log(`${this.createLogPrefix("INFO" /* INFO */)} [${this.MODULE_NAME}] ${message}`);
|
|
649
|
-
}
|
|
650
|
-
logWarn(message) {
|
|
651
|
-
console.warn(`${this.createLogPrefix("WARN" /* WARN */)} [${this.MODULE_NAME}] ${message}`);
|
|
652
|
-
}
|
|
653
|
-
logError(message, error) {
|
|
654
|
-
console.error(`${this.createLogPrefix("ERROR" /* ERROR */)} [${this.MODULE_NAME}] ${message}`, error || "");
|
|
655
|
-
}
|
|
656
|
-
// private logDebug(message: string): void {
|
|
657
|
-
// console.debug(`${this.createLogPrefix(LogLevel.DEBUG)} [${this.MODULE_NAME}] ${message}`)
|
|
658
|
-
// }
|
|
14
|
+
/** 是否已经启动 */
|
|
15
|
+
isStarted = false;
|
|
16
|
+
/** 子进程实例 */
|
|
17
|
+
childProcess = null;
|
|
18
|
+
/** 重启延迟时间 */
|
|
19
|
+
RESTART_DELAY_MS = 200;
|
|
20
|
+
/** 模块名称 */
|
|
21
|
+
MODULE_NAME = "ProcessManager";
|
|
22
|
+
/** 启动时间 */
|
|
23
|
+
startTime = Date.now();
|
|
24
|
+
/** 进程统计信息 */
|
|
25
|
+
processStats = {
|
|
26
|
+
/** 重启次数 */
|
|
27
|
+
restartCount: 0,
|
|
28
|
+
/** 最后重启时间 */
|
|
29
|
+
lastRestartTime: 0
|
|
30
|
+
};
|
|
31
|
+
/** 默认HTTP端口 */
|
|
32
|
+
DEFAULT_HTTP_PORT = 7777;
|
|
33
|
+
COLORS = {
|
|
34
|
+
green: "\x1B[32m",
|
|
35
|
+
yellow: "\x1B[33m",
|
|
36
|
+
red: "\x1B[31m",
|
|
37
|
+
blue: "\x1B[34m",
|
|
38
|
+
magenta: "\x1B[35m",
|
|
39
|
+
cyan: "\x1B[36m",
|
|
40
|
+
gray: "\x1B[90m",
|
|
41
|
+
reset: "\x1B[0m"
|
|
42
|
+
};
|
|
43
|
+
/** exit 事件监听器引用 */
|
|
44
|
+
exitListenerRef = null;
|
|
45
|
+
constructor() {
|
|
46
|
+
process.on("exit", this.stop.bind(this));
|
|
47
|
+
this.logSystemInfo();
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* 启动子进程
|
|
51
|
+
*/
|
|
52
|
+
start(options = {}) {
|
|
53
|
+
if (this.isAlreadyRunning()) {
|
|
54
|
+
this.log(`子进程已在运行 | PID: ${this.childProcess.pid}`);
|
|
55
|
+
return this.childProcess;
|
|
56
|
+
}
|
|
57
|
+
this.isStarted = true;
|
|
58
|
+
const entryPath = this.resolveEntryPath();
|
|
59
|
+
const args = options.reloadDeps ? [`--reload-timestamp=${Date.now()}`] : [];
|
|
60
|
+
this.childProcess = fork(entryPath, args);
|
|
61
|
+
const pid = this.childProcess.pid;
|
|
62
|
+
this.setupEventListeners();
|
|
63
|
+
this.log(`启动子进程 | PID: ${pid} | 入口: ${path.basename(entryPath)} | 参数: ${args.join(" ") || "无"}`);
|
|
64
|
+
return this.childProcess;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* 重启子进程
|
|
68
|
+
*/
|
|
69
|
+
async restart(reloadDeps = false) {
|
|
70
|
+
if (!this.childProcess) {
|
|
71
|
+
this.logWarn("重启失败 | 原因: 子进程不存在 | 操作: 直接启动新进程");
|
|
72
|
+
return this.start({ reloadDeps });
|
|
73
|
+
}
|
|
74
|
+
const oldPid = this.childProcess.pid;
|
|
75
|
+
this.processStats.restartCount++;
|
|
76
|
+
this.processStats.lastRestartTime = Date.now();
|
|
77
|
+
this.log(`准备重启子进程 | 当前PID: ${oldPid} | 重载依赖: ${reloadDeps ? "是" : "否"} | 重启次数: ${this.processStats.restartCount}`);
|
|
78
|
+
try {
|
|
79
|
+
await this.terminateChildProcess();
|
|
80
|
+
this.isStarted = false;
|
|
81
|
+
const newChild = this.start({ reloadDeps });
|
|
82
|
+
const duration = Date.now() - this.processStats.lastRestartTime;
|
|
83
|
+
this.log(`重启完成 | 旧PID: ${oldPid} | 新PID: ${newChild.pid} | 耗时: ${duration}ms`);
|
|
84
|
+
return newChild;
|
|
85
|
+
} catch (error) {
|
|
86
|
+
if (error.code === "ESRCH") {
|
|
87
|
+
this.logWarn(`重启过程中发现进程已不存在 | 错误码: ${error.code} | 操作: 启动新进程`);
|
|
88
|
+
this.isStarted = false;
|
|
89
|
+
return this.start({ reloadDeps: true });
|
|
90
|
+
}
|
|
91
|
+
this.logError(`重启失败 | 错误: ${error.message} | 代码: ${error.code || "未知"}`);
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* 停止子进程并退出父进程
|
|
97
|
+
*/
|
|
98
|
+
stop() {
|
|
99
|
+
if (this.childProcess) {
|
|
100
|
+
const pid = this.childProcess.pid;
|
|
101
|
+
const uptime = this.getProcessUptime();
|
|
102
|
+
this.log(`正在关闭子进程 | PID: ${pid} | 运行时间: ${uptime}`);
|
|
103
|
+
this.terminateChildProcess();
|
|
104
|
+
this.childProcess = null;
|
|
105
|
+
this.isStarted = false;
|
|
106
|
+
}
|
|
107
|
+
const totalUptime = this.getTotalUptime();
|
|
108
|
+
this.log(`父进程退出 | 总运行时间: ${totalUptime} | 重启次数: ${this.processStats.restartCount}`);
|
|
109
|
+
process.exit(0);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* 检查子进程是否已经在运行
|
|
113
|
+
*/
|
|
114
|
+
isAlreadyRunning() {
|
|
115
|
+
return this.isStarted && this.childProcess !== null;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* 设置子进程事件监听器
|
|
119
|
+
*/
|
|
120
|
+
setupEventListeners() {
|
|
121
|
+
if (!this.childProcess) return;
|
|
122
|
+
this.childProcess.on("message", this.handleChildMessage.bind(this));
|
|
123
|
+
if (this.exitListenerRef) this.childProcess.off("exit", this.exitListenerRef);
|
|
124
|
+
this.exitListenerRef = this.handleChildExit.bind(this);
|
|
125
|
+
this.childProcess.once("exit", this.exitListenerRef);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* 处理子进程发送的消息
|
|
129
|
+
*/
|
|
130
|
+
async handleChildMessage(message) {
|
|
131
|
+
if (typeof message !== "string") return;
|
|
132
|
+
try {
|
|
133
|
+
const { type, reloadDeps } = JSON.parse(message);
|
|
134
|
+
if (type === "restart" || type === "rs") {
|
|
135
|
+
this.log(`收到消息 | 类型: restart | 重载依赖: ${reloadDeps ? "是" : "否"}`);
|
|
136
|
+
const child = await this.restart(reloadDeps);
|
|
137
|
+
if (!child) this.logWarn("重启操作失败 | 无法获取新的子进程");
|
|
138
|
+
else this.childProcess = child;
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
if (type === "stop") {
|
|
142
|
+
this.log("收到消息 | 类型: stop | 操作: 停止进程");
|
|
143
|
+
this.stop();
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
this.logWarn(`收到未知消息 | 类型: ${type || "未知"} | 内容: ${JSON.stringify(message).substring(0, 100)}`);
|
|
147
|
+
} catch (error) {
|
|
148
|
+
this.logError(`消息处理错误 | 原始消息: ${String(message).substring(0, 50)}`, error);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* 处理子进程退出事件
|
|
153
|
+
*/
|
|
154
|
+
handleChildExit(code, signal) {
|
|
155
|
+
const exitType = signal ? `信号: ${signal}` : `退出码: ${code ?? "未知"}`;
|
|
156
|
+
const uptime = this.getProcessUptime();
|
|
157
|
+
this.log(`子进程退出 | ${exitType} | 运行时间: ${uptime}`);
|
|
158
|
+
this.stop();
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* 等待端口释放
|
|
162
|
+
* @param port - 需要检查的端口号
|
|
163
|
+
* @param maxAttempts - 最大尝试次数
|
|
164
|
+
* @param interval - 检查间隔(ms)
|
|
165
|
+
* @returns 端口是否可用
|
|
166
|
+
*/
|
|
167
|
+
async waitForPortRelease(port, maxAttempts = 30, interval = 500) {
|
|
168
|
+
const checkPort = (port) => {
|
|
169
|
+
return new Promise((resolve) => {
|
|
170
|
+
const server = createServer();
|
|
171
|
+
server.once("error", () => {
|
|
172
|
+
server.close();
|
|
173
|
+
resolve(false);
|
|
174
|
+
});
|
|
175
|
+
server.once("listening", () => {
|
|
176
|
+
server.close();
|
|
177
|
+
resolve(true);
|
|
178
|
+
});
|
|
179
|
+
server.listen(port, "127.0.0.1");
|
|
180
|
+
});
|
|
181
|
+
};
|
|
182
|
+
/**
|
|
183
|
+
* 检查指定进程是否存在
|
|
184
|
+
* @param pid - 进程ID
|
|
185
|
+
* @returns 进程是否存在
|
|
186
|
+
*/
|
|
187
|
+
const isProcessAlive = (pid) => {
|
|
188
|
+
try {
|
|
189
|
+
process.kill(pid, 0);
|
|
190
|
+
return true;
|
|
191
|
+
} catch {
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
196
|
+
if (this.childProcess?.pid && !isProcessAlive(this.childProcess.pid)) {
|
|
197
|
+
this.log(`子进程已退出 | PID: ${this.childProcess.pid}`);
|
|
198
|
+
if (await checkPort(port)) {
|
|
199
|
+
this.log(`端口 ${port} 已释放,可以使用`);
|
|
200
|
+
return true;
|
|
201
|
+
} else this.logWarn(`子进程已退出,但端口 ${port} 仍被占用 (可能被其他进程占用)`);
|
|
202
|
+
}
|
|
203
|
+
if (await checkPort(port)) {
|
|
204
|
+
this.log(`端口 ${port} 已释放,可以使用`);
|
|
205
|
+
return true;
|
|
206
|
+
}
|
|
207
|
+
this.logWarn(`端口 ${port} 仍被占用,等待释放... (${attempt + 1}/${maxAttempts})`);
|
|
208
|
+
try {
|
|
209
|
+
if (this?.childProcess?.pid && isProcessAlive(this.childProcess.pid)) {
|
|
210
|
+
this.log(`尝试终止子进程 | PID: ${this.childProcess.pid}`);
|
|
211
|
+
process.kill(this.childProcess.pid);
|
|
212
|
+
}
|
|
213
|
+
} catch {}
|
|
214
|
+
await new Promise((resolve) => setTimeout(resolve, interval));
|
|
215
|
+
}
|
|
216
|
+
this.logError(`端口 ${port} 在 ${maxAttempts} 次尝试后仍被占用`);
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* 获取HTTP端口号
|
|
221
|
+
* @returns HTTP端口号
|
|
222
|
+
*/
|
|
223
|
+
getHttpPort() {
|
|
224
|
+
try {
|
|
225
|
+
const envPath = path.resolve(process.cwd(), ".env");
|
|
226
|
+
if (fs.existsSync(envPath)) {
|
|
227
|
+
const envConfig = dotenv.parse(fs.readFileSync(envPath));
|
|
228
|
+
if (envConfig.HTTP_PORT) return Number(envConfig.HTTP_PORT);
|
|
229
|
+
}
|
|
230
|
+
return this.DEFAULT_HTTP_PORT;
|
|
231
|
+
} catch (error) {
|
|
232
|
+
this.logWarn(`读取HTTP端口失败: ${error.message},使用默认端口${this.DEFAULT_HTTP_PORT}`);
|
|
233
|
+
return this.DEFAULT_HTTP_PORT;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* 终止子进程
|
|
238
|
+
*/
|
|
239
|
+
async terminateChildProcess() {
|
|
240
|
+
if (!this.childProcess) return;
|
|
241
|
+
if (this.exitListenerRef) {
|
|
242
|
+
this.childProcess.off("exit", this.exitListenerRef);
|
|
243
|
+
this.exitListenerRef = null;
|
|
244
|
+
}
|
|
245
|
+
try {
|
|
246
|
+
const pid = this.childProcess.pid;
|
|
247
|
+
this.log(`发送终止信号 | PID: ${pid} | 信号: SIGTERM`);
|
|
248
|
+
this.childProcess.kill("SIGTERM");
|
|
249
|
+
this.log(`等待进程终止 | PID: ${pid} | 超时: ${this.RESTART_DELAY_MS}ms`);
|
|
250
|
+
const port = this.getHttpPort();
|
|
251
|
+
await this.waitForPortRelease(port);
|
|
252
|
+
try {
|
|
253
|
+
if (this?.childProcess?.pid) {
|
|
254
|
+
this.log(`确保进程终止 | PID: ${pid} | 发送强制终止信号`);
|
|
255
|
+
process.kill(this.childProcess.pid);
|
|
256
|
+
}
|
|
257
|
+
} catch {}
|
|
258
|
+
} catch {}
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* 解析子进程入口文件路径
|
|
262
|
+
*/
|
|
263
|
+
resolveEntryPath() {
|
|
264
|
+
const currentFilePath = fileURLToPath(import.meta.url);
|
|
265
|
+
const currentDirPath = path.dirname(currentFilePath);
|
|
266
|
+
const isESM = import.meta.url.includes(".mjs");
|
|
267
|
+
const localEntryPath = path.join(currentDirPath, isESM ? "app.mjs" : "app.ts");
|
|
268
|
+
return fs.existsSync(localEntryPath) ? localEntryPath : path.join(process.cwd(), "node_modules", "node-karin", "dist", "start", "app.mjs");
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* 获取当前时间戳,格式为 HH:MM:SS.mmm
|
|
272
|
+
*/
|
|
273
|
+
getTimestamp() {
|
|
274
|
+
const now = /* @__PURE__ */ new Date();
|
|
275
|
+
return `${now.getHours().toString().padStart(2, "0")}:${now.getMinutes().toString().padStart(2, "0")}:${now.getSeconds().toString().padStart(2, "0")}.${now.getMilliseconds().toString().padStart(3, "0")}`;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* 创建日志前缀
|
|
279
|
+
*/
|
|
280
|
+
createLogPrefix(level) {
|
|
281
|
+
const prefix = `[Karin][${this.getTimestamp()}][${level}]`;
|
|
282
|
+
return `${this.COLORS.green}${prefix}${this.COLORS.reset}`;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* 获取进程运行时间的友好字符串
|
|
286
|
+
*/
|
|
287
|
+
getProcessUptime() {
|
|
288
|
+
if (!this.childProcess?.pid) return "0s";
|
|
289
|
+
const uptime = (Date.now() - this.processStats.lastRestartTime) / 1e3;
|
|
290
|
+
if (uptime < 60) return `${uptime.toFixed(1)}s`;
|
|
291
|
+
if (uptime < 3600) return `${(uptime / 60).toFixed(1)}m`;
|
|
292
|
+
return `${(uptime / 3600).toFixed(1)}h`;
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* 获取总运行时间的友好字符串
|
|
296
|
+
*/
|
|
297
|
+
getTotalUptime() {
|
|
298
|
+
const uptime = (Date.now() - this.startTime) / 1e3;
|
|
299
|
+
if (uptime < 60) return `${uptime.toFixed(1)}s`;
|
|
300
|
+
if (uptime < 3600) return `${(uptime / 60).toFixed(1)}m`;
|
|
301
|
+
return `${(uptime / 3600).toFixed(1)}h`;
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* 输出系统信息
|
|
305
|
+
*/
|
|
306
|
+
logSystemInfo() {
|
|
307
|
+
const nodeVersion = process.version;
|
|
308
|
+
const platform = `${os.platform()} ${os.release()}`;
|
|
309
|
+
const cpus = os.cpus().length;
|
|
310
|
+
const memory = `${Math.round(os.totalmem() / 1024 / 1024 / 1024)}GB`;
|
|
311
|
+
this.log(`系统信息 | Node: ${nodeVersion} | 平台: ${platform} | CPU核心: ${cpus} | 内存: ${memory}`);
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* 日志输出方法
|
|
315
|
+
*/
|
|
316
|
+
log(message) {
|
|
317
|
+
console.log(`${this.createLogPrefix("INFO")} [${this.MODULE_NAME}] ${message}`);
|
|
318
|
+
}
|
|
319
|
+
logWarn(message) {
|
|
320
|
+
console.warn(`${this.createLogPrefix("WARN")} [${this.MODULE_NAME}] ${message}`);
|
|
321
|
+
}
|
|
322
|
+
logError(message, error) {
|
|
323
|
+
console.error(`${this.createLogPrefix("ERROR")} [${this.MODULE_NAME}] ${message}`, error || "");
|
|
324
|
+
}
|
|
659
325
|
};
|
|
660
326
|
new ProcessManager().start();
|
|
327
|
+
|
|
328
|
+
//#endregion
|
|
329
|
+
export { };
|