node-karin 1.10.2 → 1.10.4

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 CHANGED
@@ -1,5 +1,19 @@
1
1
  # 更新日志
2
2
 
3
+ ## [1.10.4](https://github.com/KarinJS/Karin/compare/core-v1.10.3...core-v1.10.4) (2025-06-08)
4
+
5
+
6
+ ### 🐛 Bug Fixes
7
+
8
+ * 提升`create-karin`的兼容性 ([#468](https://github.com/KarinJS/Karin/issues/468)) ([23153fd](https://github.com/KarinJS/Karin/commit/23153fdbd8a022af567c095d00f37602d50dc085))
9
+
10
+ ## [1.10.3](https://github.com/KarinJS/Karin/compare/core-v1.10.2...core-v1.10.3) (2025-06-08)
11
+
12
+
13
+ ### 🐛 Bug Fixes
14
+
15
+ * kill Process ([#465](https://github.com/KarinJS/Karin/issues/465)) ([77c4fb6](https://github.com/KarinJS/Karin/commit/77c4fb62405607b585fff5be75357f09e676dd11))
16
+
3
17
  ## [1.10.2](https://github.com/KarinJS/Karin/compare/core-v1.10.1...core-v1.10.2) (2025-06-06)
4
18
 
5
19
 
package/dist/index.mjs CHANGED
@@ -19362,7 +19362,7 @@ var init_exit = __esm({
19362
19362
  init_exec();
19363
19363
  init_uptime();
19364
19364
  exitStatus = false;
19365
- processExit = async (code) => {
19365
+ processExit = async (code, isKillPm2 = false) => {
19366
19366
  logger.debug("[child] \u5B50\u8FDB\u7A0B\u6536\u5230\u9000\u51FA\u7533\u8BF7:", code);
19367
19367
  try {
19368
19368
  if (exitStatus) return;
@@ -19372,7 +19372,7 @@ var init_exit = __esm({
19372
19372
  if (redis3.id === "mock") await redis3.save();
19373
19373
  logger.debug("[child] redis\u6570\u636E\u4FDD\u5B58\u5B8C\u6210");
19374
19374
  logger.mark(tips(`\u8FD0\u884C\u7ED3\u675F \u8FD0\u884C\u65F6\u95F4\uFF1A${uptime2()} \u9000\u51FA\u7801\uFF1A${code ?? "\u672A\u77E5"}`));
19375
- if (process.env.pm_id) {
19375
+ if (process.env.pm_id && isKillPm2) {
19376
19376
  logger.mark(tips("[child] pm2\u73AF\u5883 \u5220\u9664pm2\u8FDB\u7A0B"));
19377
19377
  await exec(`pm2 delete ${process.env.pm_id}`);
19378
19378
  }
@@ -20768,7 +20768,7 @@ var init_manage = __esm({
20768
20768
  logger.mark("\u6536\u5230\u9000\u51FA\u8BF7\u6C42\uFF0C\u6B63\u5728\u9000\u51FA...");
20769
20769
  createSuccessResponse(res, null, "\u9000\u51FA\u6307\u4EE4\u53D1\u9001\u6210\u529F");
20770
20770
  const { processExit: processExit2 } = await Promise.resolve().then(() => (init_exit(), exit_exports));
20771
- await processExit2(0);
20771
+ await processExit2(0, true);
20772
20772
  };
20773
20773
  }
20774
20774
  });
@@ -1,10 +1,331 @@
1
1
  import os from 'node:os';
2
2
  import fs from 'node:fs';
3
+ import require$$0 from 'fs';
4
+ import require$$1 from 'path';
5
+ import require$$2 from 'os';
6
+ import require$$3 from 'crypto';
3
7
  import path from 'node:path';
8
+ import { createServer } from 'node:net';
4
9
  import { fileURLToPath } from 'node:url';
5
10
  import { fork } from 'node:child_process';
6
11
 
7
12
  // src/start/index.ts
13
+ function _mergeNamespaces(n, m) {
14
+ for (var i = 0; i < m.length; i++) {
15
+ const e = m[i];
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;
8
329
  var ProcessManager = class {
9
330
  /** 是否已经启动 */
10
331
  isStarted = false;
@@ -25,6 +346,8 @@ var ProcessManager = class {
25
346
  /** 最后重启时间 */
26
347
  lastRestartTime: 0
27
348
  };
349
+ /** 默认HTTP端口 */
350
+ DEFAULT_HTTP_PORT = 7777;
28
351
  // ANSI 颜色代码
29
352
  COLORS = {
30
353
  green: "\x1B[32m",
@@ -97,7 +420,7 @@ var ProcessManager = class {
97
420
  const pid = this.childProcess.pid;
98
421
  const uptime = this.getProcessUptime();
99
422
  this.log(`\u6B63\u5728\u5173\u95ED\u5B50\u8FDB\u7A0B | PID: ${pid} | \u8FD0\u884C\u65F6\u95F4: ${uptime}`);
100
- this.terminateChildProcess(false);
423
+ this.terminateChildProcess();
101
424
  this.childProcess = null;
102
425
  this.isStarted = false;
103
426
  this.isRestarting = false;
@@ -158,21 +481,98 @@ var ProcessManager = class {
158
481
  this.stop();
159
482
  }
160
483
  }
484
+ /**
485
+ * 等待端口释放
486
+ * @param port - 需要检查的端口号
487
+ * @param maxAttempts - 最大尝试次数
488
+ * @param interval - 检查间隔(ms)
489
+ * @returns 端口是否可用
490
+ */
491
+ async waitForPortRelease(port, maxAttempts = 30, interval = 500) {
492
+ const checkPort = (port2) => {
493
+ return new Promise((resolve) => {
494
+ const server = createServer();
495
+ server.once("error", () => {
496
+ server.close();
497
+ resolve(false);
498
+ });
499
+ server.once("listening", () => {
500
+ server.close();
501
+ resolve(true);
502
+ });
503
+ server.listen(port2, "127.0.0.1");
504
+ });
505
+ };
506
+ const isProcessAlive = (pid) => {
507
+ try {
508
+ process.kill(pid, 0);
509
+ return true;
510
+ } catch {
511
+ return false;
512
+ }
513
+ };
514
+ for (let attempt = 0; attempt < maxAttempts; attempt++) {
515
+ if (this.childProcess?.pid && !isProcessAlive(this.childProcess.pid)) {
516
+ this.log(`\u5B50\u8FDB\u7A0B\u5DF2\u9000\u51FA | PID: ${this.childProcess.pid}`);
517
+ const portAvailable2 = await checkPort(port);
518
+ if (portAvailable2) {
519
+ this.log(`\u7AEF\u53E3 ${port} \u5DF2\u91CA\u653E\uFF0C\u53EF\u4EE5\u4F7F\u7528`);
520
+ return true;
521
+ } else {
522
+ this.logWarn(`\u5B50\u8FDB\u7A0B\u5DF2\u9000\u51FA\uFF0C\u4F46\u7AEF\u53E3 ${port} \u4ECD\u88AB\u5360\u7528 (\u53EF\u80FD\u88AB\u5176\u4ED6\u8FDB\u7A0B\u5360\u7528)`);
523
+ }
524
+ }
525
+ const portAvailable = await checkPort(port);
526
+ if (portAvailable) {
527
+ this.log(`\u7AEF\u53E3 ${port} \u5DF2\u91CA\u653E\uFF0C\u53EF\u4EE5\u4F7F\u7528`);
528
+ return true;
529
+ }
530
+ this.logWarn(`\u7AEF\u53E3 ${port} \u4ECD\u88AB\u5360\u7528\uFF0C\u7B49\u5F85\u91CA\u653E... (${attempt + 1}/${maxAttempts})`);
531
+ try {
532
+ if (this?.childProcess?.pid && isProcessAlive(this.childProcess.pid)) {
533
+ this.log(`\u5C1D\u8BD5\u7EC8\u6B62\u5B50\u8FDB\u7A0B | PID: ${this.childProcess.pid}`);
534
+ process.kill(this.childProcess.pid);
535
+ }
536
+ } catch {
537
+ }
538
+ await new Promise((resolve) => setTimeout(resolve, interval));
539
+ }
540
+ this.logError(`\u7AEF\u53E3 ${port} \u5728 ${maxAttempts} \u6B21\u5C1D\u8BD5\u540E\u4ECD\u88AB\u5360\u7528`);
541
+ return false;
542
+ }
543
+ /**
544
+ * 获取HTTP端口号
545
+ * @returns HTTP端口号
546
+ */
547
+ getHttpPort() {
548
+ try {
549
+ const envPath = path.resolve(process.cwd(), ".env");
550
+ if (fs.existsSync(envPath)) {
551
+ const envConfig = dotenv_default.parse(fs.readFileSync(envPath));
552
+ if (envConfig.HTTP_PORT) {
553
+ return Number(envConfig.HTTP_PORT);
554
+ }
555
+ }
556
+ return this.DEFAULT_HTTP_PORT;
557
+ } catch (error) {
558
+ this.logWarn(`\u8BFB\u53D6HTTP\u7AEF\u53E3\u5931\u8D25: ${error.message}\uFF0C\u4F7F\u7528\u9ED8\u8BA4\u7AEF\u53E3${this.DEFAULT_HTTP_PORT}`);
559
+ return this.DEFAULT_HTTP_PORT;
560
+ }
561
+ }
161
562
  /**
162
563
  * 终止子进程
163
564
  */
164
- async terminateChildProcess(waitForTermination = true) {
565
+ async terminateChildProcess() {
165
566
  if (!this.childProcess) return;
166
567
  try {
167
568
  const pid = this.childProcess.pid;
168
569
  this.log(`\u53D1\u9001\u7EC8\u6B62\u4FE1\u53F7 | PID: ${pid} | \u4FE1\u53F7: SIGTERM`);
169
570
  this.childProcess.kill("SIGTERM");
170
- if (waitForTermination) {
171
- this.log(`\u7B49\u5F85\u8FDB\u7A0B\u7EC8\u6B62 | PID: ${pid} | \u8D85\u65F6: ${this.RESTART_DELAY_MS}ms`);
172
- await new Promise((resolve) => setTimeout(resolve, this.RESTART_DELAY_MS));
173
- }
571
+ this.log(`\u7B49\u5F85\u8FDB\u7A0B\u7EC8\u6B62 | PID: ${pid} | \u8D85\u65F6: ${this.RESTART_DELAY_MS}ms`);
572
+ const port = this.getHttpPort();
573
+ await this.waitForPortRelease(port);
174
574
  try {
175
- if (this.childProcess.pid) {
575
+ if (this?.childProcess?.pid) {
176
576
  this.log(`\u786E\u4FDD\u8FDB\u7A0B\u7EC8\u6B62 | PID: ${pid} | \u53D1\u9001\u5F3A\u5236\u7EC8\u6B62\u4FE1\u53F7`);
177
577
  process.kill(this.childProcess.pid);
178
578
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-karin",
3
- "version": "1.10.2",
3
+ "version": "1.10.4",
4
4
  "description": "Lightweight, efficient, concise, and stable robot framework.",
5
5
  "keywords": [
6
6
  "node",