@maiyunnet/kebab 9.23.0 → 9.24.0

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.
@@ -0,0 +1,2 @@
1
+ export declare function isFiles(files: unknown): files is Record<string, string>;
2
+ export declare function replace(directory: string, files: Record<string, string>, report: (message: string, error: unknown) => void): Promise<string>;
@@ -0,0 +1,97 @@
1
+ import * as fs from 'fs/promises';
2
+ import * as path from 'path';
3
+ import * as lText from '#kebab/lib/text.js';
4
+ let pending = Promise.resolve('Done');
5
+ export function isFiles(files) {
6
+ if (!files || typeof files !== 'object' || Array.isArray(files)) {
7
+ return false;
8
+ }
9
+ const entries = Object.entries(files);
10
+ if (!entries.length) {
11
+ return false;
12
+ }
13
+ const names = new Set();
14
+ for (const [name, content] of entries) {
15
+ if (!name.endsWith('.json') || /[/\\\0:]/.test(name) || names.has(name.toLowerCase()) || typeof content !== 'string') {
16
+ return false;
17
+ }
18
+ names.add(name.toLowerCase());
19
+ const config = lText.parseJson(content);
20
+ const list = Array.isArray(config) ? config : [config];
21
+ for (const item of list) {
22
+ if (!item || typeof item !== 'object' || Array.isArray(item)) {
23
+ return false;
24
+ }
25
+ const vhost = item;
26
+ if (!Array.isArray(vhost['domains']) || vhost['domains'].some(domain => typeof domain !== 'string') ||
27
+ typeof vhost['root'] !== 'string' ||
28
+ vhost['name'] !== undefined && typeof vhost['name'] !== 'string' ||
29
+ vhost['remark'] !== undefined && typeof vhost['remark'] !== 'string') {
30
+ return false;
31
+ }
32
+ }
33
+ }
34
+ return true;
35
+ }
36
+ export function replace(directory, files, report) {
37
+ pending = pending.catch(() => '').then(() => replaceOnce(path.resolve(directory), files, report));
38
+ return pending;
39
+ }
40
+ async function replaceOnce(directory, files, report) {
41
+ let temporary = '';
42
+ let moved = false;
43
+ let committed = false;
44
+ let preserveBackup = false;
45
+ try {
46
+ let current;
47
+ try {
48
+ current = await fs.lstat(directory);
49
+ }
50
+ catch (error) {
51
+ if (error.code !== 'ENOENT') {
52
+ throw error;
53
+ }
54
+ }
55
+ if (current && !current.isDirectory()) {
56
+ return 'Invalid vhost directory';
57
+ }
58
+ temporary = await fs.mkdtemp(path.join(path.dirname(directory), '.vhost-'));
59
+ const staged = path.join(temporary, 'new');
60
+ const backup = path.join(temporary, 'old');
61
+ await fs.mkdir(staged, { mode: current ? current.mode & 0o777 : 0o755 });
62
+ for (const [name, content] of Object.entries(files)) {
63
+ await fs.writeFile(path.join(staged, name), content, { encoding: 'utf8', flag: 'wx' });
64
+ }
65
+ if (current) {
66
+ await fs.rename(directory, backup);
67
+ moved = true;
68
+ }
69
+ await fs.rename(staged, directory);
70
+ committed = true;
71
+ return 'Done';
72
+ }
73
+ catch (error) {
74
+ report('Directory replacement failed.', error);
75
+ if (moved && !committed) {
76
+ try {
77
+ await fs.rename(path.join(temporary, 'old'), directory);
78
+ }
79
+ catch (restoreError) {
80
+ preserveBackup = true;
81
+ report('Directory restore failed.', restoreError);
82
+ return `Failed to restore vhost directory; backup: ${path.join(temporary, 'old')}`;
83
+ }
84
+ }
85
+ return 'Failed to replace vhost directory';
86
+ }
87
+ finally {
88
+ if (temporary && !preserveBackup) {
89
+ try {
90
+ await fs.rm(temporary, { recursive: true, force: true });
91
+ }
92
+ catch (error) {
93
+ report(`Temporary directory cleanup failed: ${temporary}`, error);
94
+ }
95
+ }
96
+ }
97
+ }
package/doc/kebab-rag.md CHANGED
@@ -33,6 +33,25 @@ quick-start.md
33
33
  - `view/`: 视图目录,存放 ejs 文件
34
34
  - `ws/`: WebSocket 目录
35
35
 
36
+ ## 全量更新虚拟主机配置
37
+
38
+ `lCore.sendVhost(files, hosts?)` 全量替换目标节点的 `conf/vhost/`,未提交的旧文件及子目录会被移除。`files` 是文件名到 JSON 原文的非空集合,仅接受直接 `.json` 文件名,不允许路径及大小写重名;每份 JSON 为 `IVhost` 或 `IVhost[]`。文件原文会被保留。
39
+
40
+ ```ts
41
+ import * as lCore from '@maiyunnet/kebab/lib/core.js';
42
+
43
+ const results = await lCore.sendVhost({
44
+ 'default.json': '{"domains":["*"],"root":"default/"}',
45
+ 'sites.json': '[{"domains":["example.com"],"root":"example.com/"}]',
46
+ }, 'config');
47
+ ```
48
+
49
+ 省略 `hosts` 或传入空列表时更新本机;传入 `'config'` 使用全局 `hosts`,也可指定节点列表。各节点独立返回 `{ result, return }`,失败节点可单独重试。接收端复用 RPC 鉴权,通过 POST body 接收配置(上限为现有 POST 解析的 50 MiB),不传输证书或全局配置。
50
+
51
+ 同一节点串行执行替换。全部文件校验、写入临时目录成功后,才备份旧目录并切换;切换失败会恢复旧目录。若恢复也失败,返回信息会给出保留的备份目录,需人工恢复;目标 `vhost` 本身是软链接或普通文件时拒绝更新。替换包含两次目录重命名,过程中不要同时执行 reload 或手动修改该目录。
52
+
53
+ 此方法不自动 reload,成功仅表示磁盘目录替换完成。确认各节点结果后,自行调用 `sendReload()`;部分节点失败时只向成功节点发送 reload。现有 reload 返回成功表示通知已发送,不代表所有 worker 已加载完成。
54
+
36
55
  ## 请求主机与完整 URL
37
56
 
38
57
  控制器通过 `this._config.const.hostCanonical` 获取用于 URL 的主机地址:HTTPS 的 443 和 HTTP 的 80 端口会省略,其余端口及 IPv6 方括号保留。`host`、`hostname` 和 `hostport` 保留原有含义。`urlOrigin`、`urlFull` 和 `urlStcFull` 使用 `hostCanonical` 生成,SSR 注入的 `_urlFull`、默认 `_staticPathFull` 同步省略默认端口。
@@ -1737,7 +1756,7 @@ index/variables/VER.md
1737
1756
 
1738
1757
  # Variable: VER
1739
1758
 
1740
- > `const` **VER**: `"9.23.0"` = `'9.23.0'`
1759
+ > `const` **VER**: `"9.24.0"` = `'9.24.0'`
1741
1760
 
1742
1761
  Defined in: [index.ts:10](https://github.com/maiyunnet/kebab/blob/master/index.ts#L10)
1743
1762
 
@@ -3727,7 +3746,7 @@ lib/core/functions/clone.md
3727
3746
 
3728
3747
  > **clone**\<`T`\>(`obj`): `T`
3729
3748
 
3730
- Defined in: [lib/core.ts:1468](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1468)
3749
+ Defined in: [lib/core.ts:1509](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1509)
3731
3750
 
3732
3751
  完整的克隆一份数组/对象
3733
3752
 
@@ -3791,7 +3810,7 @@ lib/core/functions/debug.md
3791
3810
 
3792
3811
  > **debug**(`message?`, ...`optionalParams`): `void`
3793
3812
 
3794
- Defined in: [lib/core.ts:1514](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1514)
3813
+ Defined in: [lib/core.ts:1555](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1555)
3795
3814
 
3796
3815
  打印调试信息,线上环境不会打印
3797
3816
 
@@ -3826,7 +3845,7 @@ lib/core/functions/display.md
3826
3845
 
3827
3846
  > **display**(`message?`, ...`optionalParams`): `void`
3828
3847
 
3829
- Defined in: [lib/core.ts:1527](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1527)
3848
+ Defined in: [lib/core.ts:1568](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1568)
3830
3849
 
3831
3850
  向控制台直接显示内容,一般情况下禁止使用
3832
3851
 
@@ -3938,7 +3957,7 @@ lib/core/functions/getLog.md
3938
3957
 
3939
3958
  > **getLog**(`opt`): `Promise`\<`false` \| \{ `list`: `any`[] \| `string`[][]; `total`: `number`; \}\>
3940
3959
 
3941
- Defined in: [lib/core.ts:1248](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1248)
3960
+ Defined in: [lib/core.ts:1289](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1289)
3942
3961
 
3943
3962
  获取日志内容为一个数组
3944
3963
 
@@ -4007,7 +4026,7 @@ lib/core/functions/getMonitorEvent.md
4007
4026
 
4008
4027
  > **getMonitorEvent**(`opt`): `Promise`\<`false` \| [`IMonitorEvent`](../interfaces/IMonitorEvent.md) \| `null`\>
4009
4028
 
4010
- Defined in: [lib/core.ts:1384](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1384)
4029
+ Defined in: [lib/core.ts:1425](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1425)
4011
4030
 
4012
4031
  读取事件详情和文件清单;只解析事件记录,不解析 Profile 或堆快照
4013
4032
 
@@ -4044,7 +4063,7 @@ lib/core/functions/getMonitorEvents.md
4044
4063
 
4045
4064
  > **getMonitorEvents**(`opt?`): `Promise`\<`false` \| \{ `list`: [`IMonitorEventSummary`](../interfaces/IMonitorEventSummary.md)[]; `total`: `number`; \}\>
4046
4065
 
4047
- Defined in: [lib/core.ts:1357](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1357)
4066
+ Defined in: [lib/core.ts:1398](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1398)
4048
4067
 
4049
4068
  获取某天已保存的监控事件,按目录时间倒序分页
4050
4069
 
@@ -4091,7 +4110,7 @@ lib/core/functions/getMonitorFile.md
4091
4110
 
4092
4111
  > **getMonitorFile**(`opt`): `Promise`\<`false` \| [`Response`](../../undici/response/classes/Response.md) \| `null`\>
4093
4112
 
4094
- Defined in: [lib/core.ts:1408](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1408)
4113
+ Defined in: [lib/core.ts:1449](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1449)
4095
4114
 
4096
4115
  获取监控原始文件的响应,可使用 getStream() 下载;小文件可用 getText()/getJson() 预览
4097
4116
 
@@ -4236,7 +4255,7 @@ lib/core/functions/loadEnv.md
4236
4255
 
4237
4256
  > **loadEnv**(`dir`): `Promise`\<`void`\>
4238
4257
 
4239
- Defined in: [lib/core.ts:1598](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1598)
4258
+ Defined in: [lib/core.ts:1639](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1639)
4240
4259
 
4241
4260
  加载 .env 文件到 process.env,若文件不存在则跳过
4242
4261
 
@@ -4265,7 +4284,7 @@ lib/core/functions/log.md
4265
4284
 
4266
4285
  > **log**(`opt`, `msg`, `fend?`): `void`
4267
4286
 
4268
- Defined in: [lib/core.ts:1135](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1135)
4287
+ Defined in: [lib/core.ts:1176](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1176)
4269
4288
 
4270
4289
  写入文件日志
4271
4290
 
@@ -4306,7 +4325,7 @@ lib/core/functions/ls.md
4306
4325
 
4307
4326
  > **ls**(`opt`): `Promise`\<`object`[]\>
4308
4327
 
4309
- Defined in: [lib/core.ts:1429](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1429)
4328
+ Defined in: [lib/core.ts:1470](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1470)
4310
4329
 
4311
4330
  获取目录内文件/文件夹列表
4312
4331
 
@@ -4711,7 +4730,7 @@ lib/core/functions/resolveEnvVars.md
4711
4730
 
4712
4731
  > **resolveEnvVars**(`obj`): `void`
4713
4732
 
4714
- Defined in: [lib/core.ts:1628](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1628)
4733
+ Defined in: [lib/core.ts:1669](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1669)
4715
4734
 
4716
4735
  将配置对象中的 ${ENV_VAR} 占位符替换为 process.env 的值
4717
4736
 
@@ -4974,6 +4993,43 @@ Defined in: [lib/core.ts:799](https://github.com/maiyunnet/kebab/blob/master/lib
4974
4993
 
4975
4994
  各主机发送结果
4976
4995
 
4996
+ lib/core/functions/sendVhost.md
4997
+ ---
4998
+
4999
+ [**Documents for @maiyunnet/kebab**](../../../index.md)
5000
+
5001
+ ***
5002
+
5003
+ [Documents for @maiyunnet/kebab](../../../index.md) / [lib/core](../index.md) / sendVhost
5004
+
5005
+ # Function: sendVhost()
5006
+
5007
+ > **sendVhost**(`files`, `hosts?`): `Promise`\<`Record`\<`string`, \{ `result`: `boolean`; `return`: `string`; \}\>\>
5008
+
5009
+ Defined in: [lib/core.ts:1123](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1123)
5010
+
5011
+ 全量替换各节点 conf/vhost/ 中的文件,不自动 reload
5012
+
5013
+ ## Parameters
5014
+
5015
+ ### files
5016
+
5017
+ `Record`\<`string`, `string`\>
5018
+
5019
+ 文件名与 JSON 原文的非空集合;仅允许直接 .json 文件名,内容为 IVhost 或 IVhost[]
5020
+
5021
+ ### hosts?
5022
+
5023
+ `string`[] \| `"config"`
5024
+
5025
+ 节点列表,config 使用全局 hosts;省略或列表为空时更新本机
5026
+
5027
+ ## Returns
5028
+
5029
+ `Promise`\<`Record`\<`string`, \{ `result`: `boolean`; `return`: `string`; \}\>\>
5030
+
5031
+ 各节点的写入结果;成功表示目录替换完成,需自行调用 sendReload 生效
5032
+
4977
5033
  lib/core/functions/setCookie.md
4978
5034
  ---
4979
5035
 
@@ -5186,7 +5242,7 @@ lib/core/functions/writeEventStreamHead.md
5186
5242
 
5187
5243
  > **writeEventStreamHead**(`res`): `void`
5188
5244
 
5189
- Defined in: [lib/core.ts:1568](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1568)
5245
+ Defined in: [lib/core.ts:1609](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1609)
5190
5246
 
5191
5247
  提交服务器发送事件(SSE)响应头
5192
5248
 
@@ -5221,7 +5277,7 @@ lib/core/functions/writeHead.md
5221
5277
 
5222
5278
  > **writeHead**(`res`, `statusCode`, `headers?`): `void`
5223
5279
 
5224
- Defined in: [lib/core.ts:1547](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1547)
5280
+ Defined in: [lib/core.ts:1588](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1588)
5225
5281
 
5226
5282
  提交 HTTP 响应状态和头部,兼容 HTTP/1.1 与 HTTP/2
5227
5283
 
@@ -5271,7 +5327,7 @@ lib/core/functions/write.md
5271
5327
 
5272
5328
  > **write**(`res`, `data`): `void`
5273
5329
 
5274
- Defined in: [lib/core.ts:1580](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1580)
5330
+ Defined in: [lib/core.ts:1621](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1621)
5275
5331
 
5276
5332
  向 res 发送数据
5277
5333
 
@@ -5374,6 +5430,7 @@ lib/core/index.md
5374
5430
  - [sendReload](functions/sendReload.md)
5375
5431
  - [sendRestart](functions/sendRestart.md)
5376
5432
  - [sendStop](functions/sendStop.md)
5433
+ - [sendVhost](functions/sendVhost.md)
5377
5434
  - [setCookie](functions/setCookie.md)
5378
5435
  - [setGlobal](functions/setGlobal.md)
5379
5436
  - [sleep](functions/sleep.md)
@@ -5457,7 +5514,7 @@ lib/core/interfaces/ILogOptions.md
5457
5514
 
5458
5515
  # Interface: ILogOptions
5459
5516
 
5460
- Defined in: [lib/core.ts:1118](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1118)
5517
+ Defined in: [lib/core.ts:1159](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1159)
5461
5518
 
5462
5519
  log 设置的选项
5463
5520
 
@@ -5467,7 +5524,7 @@ log 设置的选项
5467
5524
 
5468
5525
  > `optional` **cookie?**: `Record`\<`string`, `string`\>
5469
5526
 
5470
- Defined in: [lib/core.ts:1124](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1124)
5527
+ Defined in: [lib/core.ts:1165](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1165)
5471
5528
 
5472
5529
  ***
5473
5530
 
@@ -5475,7 +5532,7 @@ Defined in: [lib/core.ts:1124](https://github.com/maiyunnet/kebab/blob/master/li
5475
5532
 
5476
5533
  > `optional` **get?**: `Record`\<`string`, `any`\>
5477
5534
 
5478
- Defined in: [lib/core.ts:1123](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1123)
5535
+ Defined in: [lib/core.ts:1164](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1164)
5479
5536
 
5480
5537
  ***
5481
5538
 
@@ -5483,7 +5540,7 @@ Defined in: [lib/core.ts:1123](https://github.com/maiyunnet/kebab/blob/master/li
5483
5540
 
5484
5541
  > `optional` **headers?**: `IncomingHttpHeaders`
5485
5542
 
5486
- Defined in: [lib/core.ts:1126](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1126)
5543
+ Defined in: [lib/core.ts:1167](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1167)
5487
5544
 
5488
5545
  ***
5489
5546
 
@@ -5491,7 +5548,7 @@ Defined in: [lib/core.ts:1126](https://github.com/maiyunnet/kebab/blob/master/li
5491
5548
 
5492
5549
  > `optional` **hostname?**: `string`
5493
5550
 
5494
- Defined in: [lib/core.ts:1121](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1121)
5551
+ Defined in: [lib/core.ts:1162](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1162)
5495
5552
 
5496
5553
  ***
5497
5554
 
@@ -5499,7 +5556,7 @@ Defined in: [lib/core.ts:1121](https://github.com/maiyunnet/kebab/blob/master/li
5499
5556
 
5500
5557
  > `optional` **path?**: `string`
5501
5558
 
5502
- Defined in: [lib/core.ts:1119](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1119)
5559
+ Defined in: [lib/core.ts:1160](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1160)
5503
5560
 
5504
5561
  ***
5505
5562
 
@@ -5507,7 +5564,7 @@ Defined in: [lib/core.ts:1119](https://github.com/maiyunnet/kebab/blob/master/li
5507
5564
 
5508
5565
  > `optional` **req?**: `IncomingMessage` \| `Http2ServerRequest` \| `null`
5509
5566
 
5510
- Defined in: [lib/core.ts:1122](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1122)
5567
+ Defined in: [lib/core.ts:1163](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1163)
5511
5568
 
5512
5569
  ***
5513
5570
 
@@ -5515,7 +5572,7 @@ Defined in: [lib/core.ts:1122](https://github.com/maiyunnet/kebab/blob/master/li
5515
5572
 
5516
5573
  > `optional` **session?**: `Record`\<`string`, `any`\>
5517
5574
 
5518
- Defined in: [lib/core.ts:1125](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1125)
5575
+ Defined in: [lib/core.ts:1166](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1166)
5519
5576
 
5520
5577
  ***
5521
5578
 
@@ -5523,7 +5580,7 @@ Defined in: [lib/core.ts:1125](https://github.com/maiyunnet/kebab/blob/master/li
5523
5580
 
5524
5581
  > `optional` **urlFull?**: `string`
5525
5582
 
5526
- Defined in: [lib/core.ts:1120](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1120)
5583
+ Defined in: [lib/core.ts:1161](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1161)
5527
5584
 
5528
5585
  lib/core/interfaces/IMonitorEvent.md
5529
5586
  ---
@@ -5536,7 +5593,7 @@ lib/core/interfaces/IMonitorEvent.md
5536
5593
 
5537
5594
  # Interface: IMonitorEvent
5538
5595
 
5539
- Defined in: [lib/core.ts:1314](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1314)
5596
+ Defined in: [lib/core.ts:1355](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1355)
5540
5597
 
5541
5598
  事件原始数据与可读取的文件;data 为 null 时可下载原始记录复盘
5542
5599
 
@@ -5546,7 +5603,7 @@ Defined in: [lib/core.ts:1314](https://github.com/maiyunnet/kebab/blob/master/li
5546
5603
 
5547
5604
  > **data**: `Record`\<`string`, `unknown`\> \| `null`
5548
5605
 
5549
- Defined in: [lib/core.ts:1316](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1316)
5606
+ Defined in: [lib/core.ts:1357](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1357)
5550
5607
 
5551
5608
  ***
5552
5609
 
@@ -5554,7 +5611,7 @@ Defined in: [lib/core.ts:1316](https://github.com/maiyunnet/kebab/blob/master/li
5554
5611
 
5555
5612
  > **files**: [`IMonitorFile`](IMonitorFile.md)[]
5556
5613
 
5557
- Defined in: [lib/core.ts:1318](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1318)
5614
+ Defined in: [lib/core.ts:1359](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1359)
5558
5615
 
5559
5616
  ***
5560
5617
 
@@ -5562,7 +5619,7 @@ Defined in: [lib/core.ts:1318](https://github.com/maiyunnet/kebab/blob/master/li
5562
5619
 
5563
5620
  > **recordStatus**: `"loaded"` \| `"invalid"` \| `"missing"` \| `"too-large"` \| `"unreadable"`
5564
5621
 
5565
- Defined in: [lib/core.ts:1317](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1317)
5622
+ Defined in: [lib/core.ts:1358](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1358)
5566
5623
 
5567
5624
  ***
5568
5625
 
@@ -5570,7 +5627,7 @@ Defined in: [lib/core.ts:1317](https://github.com/maiyunnet/kebab/blob/master/li
5570
5627
 
5571
5628
  > **summary**: [`IMonitorEventSummary`](IMonitorEventSummary.md)
5572
5629
 
5573
- Defined in: [lib/core.ts:1315](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1315)
5630
+ Defined in: [lib/core.ts:1356](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1356)
5574
5631
 
5575
5632
  lib/core/interfaces/IMonitorEventSummary.md
5576
5633
  ---
@@ -5583,7 +5640,7 @@ lib/core/interfaces/IMonitorEventSummary.md
5583
5640
 
5584
5641
  # Interface: IMonitorEventSummary
5585
5642
 
5586
- Defined in: [lib/core.ts:1294](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1294)
5643
+ Defined in: [lib/core.ts:1335](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1335)
5587
5644
 
5588
5645
  已保存监控事件的简短摘要,path 相对于 log/monitor/
5589
5646
 
@@ -5593,7 +5650,7 @@ Defined in: [lib/core.ts:1294](https://github.com/maiyunnet/kebab/blob/master/li
5593
5650
 
5594
5651
  > **path**: `string`
5595
5652
 
5596
- Defined in: [lib/core.ts:1295](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1295)
5653
+ Defined in: [lib/core.ts:1336](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1336)
5597
5654
 
5598
5655
  ***
5599
5656
 
@@ -5601,7 +5658,7 @@ Defined in: [lib/core.ts:1295](https://github.com/maiyunnet/kebab/blob/master/li
5601
5658
 
5602
5659
  > **pid**: `number`
5603
5660
 
5604
- Defined in: [lib/core.ts:1296](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1296)
5661
+ Defined in: [lib/core.ts:1337](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1337)
5605
5662
 
5606
5663
  ***
5607
5664
 
@@ -5609,7 +5666,7 @@ Defined in: [lib/core.ts:1296](https://github.com/maiyunnet/kebab/blob/master/li
5609
5666
 
5610
5667
  > **reasons**: `string`[]
5611
5668
 
5612
- Defined in: [lib/core.ts:1300](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1300)
5669
+ Defined in: [lib/core.ts:1341](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1341)
5613
5670
 
5614
5671
  ***
5615
5672
 
@@ -5617,7 +5674,7 @@ Defined in: [lib/core.ts:1300](https://github.com/maiyunnet/kebab/blob/master/li
5617
5674
 
5618
5675
  > **source**: `"main"` \| `"watchdog"`
5619
5676
 
5620
- Defined in: [lib/core.ts:1298](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1298)
5677
+ Defined in: [lib/core.ts:1339](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1339)
5621
5678
 
5622
5679
  ***
5623
5680
 
@@ -5625,7 +5682,7 @@ Defined in: [lib/core.ts:1298](https://github.com/maiyunnet/kebab/blob/master/li
5625
5682
 
5626
5683
  > **status**: `string`
5627
5684
 
5628
- Defined in: [lib/core.ts:1299](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1299)
5685
+ Defined in: [lib/core.ts:1340](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1340)
5629
5686
 
5630
5687
  ***
5631
5688
 
@@ -5633,7 +5690,7 @@ Defined in: [lib/core.ts:1299](https://github.com/maiyunnet/kebab/blob/master/li
5633
5690
 
5634
5691
  > **time**: `number`
5635
5692
 
5636
- Defined in: [lib/core.ts:1297](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1297)
5693
+ Defined in: [lib/core.ts:1338](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1338)
5637
5694
 
5638
5695
  lib/core/interfaces/IMonitorFile.md
5639
5696
  ---
@@ -5646,7 +5703,7 @@ lib/core/interfaces/IMonitorFile.md
5646
5703
 
5647
5704
  # Interface: IMonitorFile
5648
5705
 
5649
- Defined in: [lib/core.ts:1304](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1304)
5706
+ Defined in: [lib/core.ts:1345](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1345)
5650
5707
 
5651
5708
  监控文件元数据;复杂格式或超过 1 MiB 的文件不提供预览
5652
5709
 
@@ -5656,7 +5713,7 @@ Defined in: [lib/core.ts:1304](https://github.com/maiyunnet/kebab/blob/master/li
5656
5713
 
5657
5714
  > **mtime**: `number`
5658
5715
 
5659
- Defined in: [lib/core.ts:1309](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1309)
5716
+ Defined in: [lib/core.ts:1350](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1350)
5660
5717
 
5661
5718
  最后修改时间,单位 ms
5662
5719
 
@@ -5666,7 +5723,7 @@ Defined in: [lib/core.ts:1309](https://github.com/maiyunnet/kebab/blob/master/li
5666
5723
 
5667
5724
  > **name**: `string`
5668
5725
 
5669
- Defined in: [lib/core.ts:1306](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1306)
5726
+ Defined in: [lib/core.ts:1347](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1347)
5670
5727
 
5671
5728
  ***
5672
5729
 
@@ -5674,7 +5731,7 @@ Defined in: [lib/core.ts:1306](https://github.com/maiyunnet/kebab/blob/master/li
5674
5731
 
5675
5732
  > **path**: `string`
5676
5733
 
5677
- Defined in: [lib/core.ts:1305](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1305)
5734
+ Defined in: [lib/core.ts:1346](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1346)
5678
5735
 
5679
5736
  ***
5680
5737
 
@@ -5682,7 +5739,7 @@ Defined in: [lib/core.ts:1305](https://github.com/maiyunnet/kebab/blob/master/li
5682
5739
 
5683
5740
  > **preview**: `"text"` \| `"json"` \| `null`
5684
5741
 
5685
- Defined in: [lib/core.ts:1310](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1310)
5742
+ Defined in: [lib/core.ts:1351](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1351)
5686
5743
 
5687
5744
  ***
5688
5745
 
@@ -5690,7 +5747,7 @@ Defined in: [lib/core.ts:1310](https://github.com/maiyunnet/kebab/blob/master/li
5690
5747
 
5691
5748
  > **size**: `number`
5692
5749
 
5693
- Defined in: [lib/core.ts:1307](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1307)
5750
+ Defined in: [lib/core.ts:1348](https://github.com/maiyunnet/kebab/blob/master/lib/core.ts#L1348)
5694
5751
 
5695
5752
  lib/core/namespaces/v/classes/ValiError.md
5696
5753
  ---
package/index.d.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  * --- 本文件用来定义每个目录实体地址的常量 ---
6
6
  */
7
7
  /** --- 当前系统版本号 --- */
8
- export declare const VER = "9.23.0";
8
+ export declare const VER = "9.24.0";
9
9
  /** --- 框架根目录,以 / 结尾 --- */
10
10
  export declare const ROOT_PATH: string;
11
11
  /** --- 框架的 LIB,以 / 结尾 --- */
package/index.js CHANGED
@@ -6,7 +6,7 @@
6
6
  * --- 本文件用来定义每个目录实体地址的常量 ---
7
7
  */
8
8
  /** --- 当前系统版本号 --- */
9
- export const VER = '9.23.0';
9
+ export const VER = '9.24.0';
10
10
  // --- 服务端用的路径 ---
11
11
  const imu = decodeURIComponent(import.meta.url).replace('file://', '').replace(/^\/(\w:)/, '$1');
12
12
  /** --- /xxx/xxx --- */
package/lib/core.d.ts CHANGED
@@ -290,6 +290,16 @@ export declare function sendPackage(content: string, hosts?: string[] | 'config'
290
290
  'result': boolean;
291
291
  'return': string;
292
292
  }>>;
293
+ /**
294
+ * --- 全量替换各节点 conf/vhost/ 中的文件,不自动 reload ---
295
+ * @param files 文件名与 JSON 原文的非空集合;仅允许直接 .json 文件名,内容为 IVhost 或 IVhost[]
296
+ * @param hosts 节点列表,config 使用全局 hosts;省略或列表为空时更新本机
297
+ * @returns 各节点的写入结果;成功表示目录替换完成,需自行调用 sendReload 生效
298
+ */
299
+ export declare function sendVhost(files: Record<string, string>, hosts?: string[] | 'config'): Promise<Record<string, {
300
+ 'result': boolean;
301
+ 'return': string;
302
+ }>>;
293
303
  /** --- log 设置的选项 --- */
294
304
  export interface ILogOptions {
295
305
  'path'?: string;
package/lib/core.js CHANGED
@@ -971,6 +971,41 @@ export async function sendPackage(content, hosts) {
971
971
  }));
972
972
  return Object.fromEntries(results);
973
973
  }
974
+ /**
975
+ * --- 全量替换各节点 conf/vhost/ 中的文件,不自动 reload ---
976
+ * @param files 文件名与 JSON 原文的非空集合;仅允许直接 .json 文件名,内容为 IVhost 或 IVhost[]
977
+ * @param hosts 节点列表,config 使用全局 hosts;省略或列表为空时更新本机
978
+ * @returns 各节点的写入结果;成功表示目录替换完成,需自行调用 sendReload 生效
979
+ */
980
+ export async function sendVhost(files, hosts) {
981
+ if (hosts === 'config') {
982
+ hosts = globalConfig.hosts;
983
+ }
984
+ if (!hosts?.length) {
985
+ hosts = ['127.0.0.1'];
986
+ }
987
+ // --- 每个节点独立发送和返回,失败节点可单独重试 ---
988
+ const results = await Promise.all(hosts.map(async (host) => {
989
+ try {
990
+ const res = await lUndici.postJson(`http://${host}:${globalConfig.rpcPort}/${lCrypto.aesEncrypt(lText.stringifyJson({
991
+ 'action': 'vhost',
992
+ 'time': lTime.stamp(),
993
+ }), globalConfig.rpcSecret)}`, files, {
994
+ 'timeout': 30,
995
+ });
996
+ const content = await res.getContent();
997
+ if (!content) {
998
+ return [host, { 'result': false, 'return': 'Network error' }];
999
+ }
1000
+ const str = content.toString();
1001
+ return [host, { 'result': str === 'Done', 'return': str }];
1002
+ }
1003
+ catch {
1004
+ return [host, { 'result': false, 'return': 'Network error' }];
1005
+ }
1006
+ }));
1007
+ return Object.fromEntries(results);
1008
+ }
974
1009
  /**
975
1010
  * --- 写入文件日志 ---
976
1011
  * @param opt 选项
@@ -31,7 +31,7 @@ export declare class Response {
31
31
  /**
32
32
  * --- 获取响应读取流对象 ---
33
33
  */
34
- getStream(): zlib.BrotliDecompress | zlib.Gunzip | zlib.Inflate | (import("undici/types/readable").default & undici.Dispatcher.BodyMixin) | null;
34
+ getStream(): (import("undici/types/readable").default & undici.Dispatcher.BodyMixin) | zlib.Gunzip | zlib.Inflate | zlib.BrotliDecompress | null;
35
35
  /**
36
36
  * --- 获取原生响应读取流对象 ---
37
37
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maiyunnet/kebab",
3
- "version": "9.23.0",
3
+ "version": "9.24.0",
4
4
  "description": "Simple, easy-to-use, and fully-featured Node.js framework that is ready-to-use out of the box.",
5
5
  "type": "module",
6
6
  "keywords": [
package/sys/master.js CHANGED
@@ -20,12 +20,28 @@ import * as lCrypto from '#kebab/lib/crypto.js';
20
20
  import * as lTime from '#kebab/lib/time.js';
21
21
  import * as lZip from '#kebab/lib/zip.js';
22
22
  import * as masterLog from '#kebab/abox/master-log.js';
23
+ import * as masterVhost from '#kebab/abox/master-vhost.js';
23
24
  /**
24
25
  * --- 解析一行 CSV,保留空字段,并处理字段里的逗号和引号 ---
25
26
  * @param line 不含换行符的 CSV 行
26
27
  * @returns 按原顺序排列的字段
27
28
  */
28
29
  const parseCsvLine = masterLog.parseCsvLine;
30
+ /**
31
+ * --- 串行替换 vhost 目录;先写临时目录,切换失败时恢复旧目录 ---
32
+ * @param directory 固定的 vhost 目录
33
+ * @param files 已通过校验的文件名与 JSON 原文
34
+ * @param report 记录替换、恢复或临时目录清理失败
35
+ * @returns 成功返回 Done;失败返回错误信息,恢复失败时包含备份目录
36
+ */
37
+ const replaceVhost = masterVhost.replace;
38
+ /**
39
+ * --- 校验非空 vhost 文件集合:仅直接 .json 文件名,不允许路径或大小写重名 ---
40
+ * --- 每份 JSON 为 IVhost 或其数组:domains 为字符串数组,root 为字符串,name/remark 可选字符串 ---
41
+ * @param files POST 中的文件集合
42
+ * @returns true 表示所有文件可写入;false 表示整批拒绝,不修改旧配置
43
+ */
44
+ const isVhostFiles = masterVhost.isFiles;
29
45
  /** --- 当前运行中的子进程列表 --- */
30
46
  const workerList = {};
31
47
  /** --- 是否已开始停止整个服务 --- */
@@ -425,6 +441,23 @@ function createRpcListener() {
425
441
  }
426
442
  break;
427
443
  }
444
+ case 'vhost': {
445
+ if (req.method !== 'POST') {
446
+ res.end('Invalid method');
447
+ return;
448
+ }
449
+ const body = await sRoute.getPost(req);
450
+ if (body === false || !isVhostFiles(body.raw)) {
451
+ res.end('Invalid vhost files');
452
+ return;
453
+ }
454
+ // --- 仅更新磁盘配置,是否 reload 由调用方决定 ---
455
+ const result = await replaceVhost(kebab.VHOST_CWD, body.raw, (message, error) => {
456
+ lCore.display(`[master] [vhost] ${message}`, error);
457
+ });
458
+ res.end(result);
459
+ return;
460
+ }
428
461
  case 'code': {
429
462
  // --- 更新 code 代码包 ---
430
463
  const rtn = await sRoute.getFormData(req);