@maiyunnet/kebab 9.26.4 → 9.27.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.
package/sys/master.js CHANGED
@@ -11,12 +11,11 @@ import * as path from 'path';
11
11
  import * as stream from 'stream/promises';
12
12
  // --- 库和定义 ---
13
13
  import * as kebab from '#kebab/index.js';
14
- import * as sRoute from '#kebab/sys/route.js';
15
14
  import * as sMonitorFiles from '#kebab/sys/monitor/files.js';
16
15
  import * as lCore from '#kebab/lib/core.js';
17
16
  import * as lFs from '#kebab/lib/fs.js';
18
17
  import * as lText from '#kebab/lib/text.js';
19
- import * as lCrypto from '#kebab/lib/crypto.js';
18
+ import * as rpc from '#kebab/lib/core/rpc.js';
20
19
  import * as lTime from '#kebab/lib/time.js';
21
20
  import * as lZip from '#kebab/lib/zip.js';
22
21
  import * as masterLog from '#kebab/abox/master-log.js';
@@ -258,25 +257,30 @@ async function run() {
258
257
  function createRpcListener() {
259
258
  http.createServer(function (req, res) {
260
259
  (async function () {
261
- /** --- 当前时间戳 --- */
262
- const time = lTime.stamp();
263
- /** --- cmd 数据对象 --- */
264
- const cmd = lCrypto.aesDecrypt((req.url ?? '').slice(1), lCore.globalConfig.rpcSecret);
265
- if (!cmd) {
266
- res.end('Error');
267
- return;
268
- }
269
- const msg = lText.parseJson(cmd);
260
+ const msg = rpc.authenticate(req, lCore.globalConfig.rpcSecret);
270
261
  if (!msg) {
271
- res.end('Failed');
262
+ if (rpc.reject(req)) {
263
+ lCore.display('[master] [rpc] authentication rejected', req.socket.remoteAddress ?? 'unknown');
264
+ }
265
+ res.statusCode = 401;
266
+ res.end('Unauthorized');
272
267
  return;
273
268
  }
274
- if ((typeof msg.time !== 'number') || (Math.abs(msg.time - time) > 5)) {
275
- res.end('Timeout');
276
- return;
269
+ const upload = msg.action === 'code' && req.method === 'POST';
270
+ let bodyLimit = 0;
271
+ if (upload) {
272
+ bodyLimit = 512 * 1024 * 1024;
277
273
  }
278
- if (lCore.globalConfig.rpcSecret === 'MUSTCHANGE') {
279
- res.end(`The rpcSecret is not set. It's recommended to set it to: ${lCore.random(32, lCore.RANDOM_LUN)}`);
274
+ else if (msg.action === 'vhost' && req.method === 'POST') {
275
+ bodyLimit = 50 * 1024 * 1024;
276
+ }
277
+ const body = await rpc.readBody(req, msg.bodyHash, kebab.FTMP_CWD, upload, bodyLimit);
278
+ if (!body) {
279
+ if (rpc.reject(req)) {
280
+ lCore.display('[master] [rpc] authentication rejected', req.socket.remoteAddress ?? 'unknown');
281
+ }
282
+ res.statusCode = 401;
283
+ res.end('Unauthorized');
280
284
  return;
281
285
  }
282
286
  switch (msg.action) {
@@ -446,289 +450,277 @@ function createRpcListener() {
446
450
  res.end('Invalid method');
447
451
  return;
448
452
  }
449
- const body = await sRoute.getPost(req);
450
- if (body === false || !isVhostFiles(body.raw)) {
453
+ const files = lText.parseJson(body.body?.toString() ?? '');
454
+ if (!isVhostFiles(files)) {
451
455
  res.end('Invalid vhost files');
452
456
  return;
453
457
  }
454
458
  // --- 仅更新磁盘配置,是否 reload 由调用方决定 ---
455
- const result = await replaceVhost(kebab.VHOST_CWD, body.raw, (message, error) => {
459
+ const result = await replaceVhost(kebab.VHOST_CWD, files, (message, error) => {
456
460
  lCore.display(`[master] [vhost] ${message}`, error);
457
461
  });
458
462
  res.end(result);
459
463
  return;
460
464
  }
461
465
  case 'code': {
462
- // --- 更新 code 代码包 ---
463
- const rtn = await sRoute.getFormData(req);
464
- if (!rtn) {
465
- res.end('Abnormal');
466
- return;
467
- }
468
- const file = rtn.files['file'];
469
- if (!file || Array.isArray(file)) {
470
- res.end('Abnormal');
471
- await sRoute.unlinkUploadFiles(rtn.files);
472
- return;
473
- }
474
- const inputPath = rtn.post['path'];
475
- if (typeof inputPath !== 'string') {
476
- res.end('Invalid path');
477
- await sRoute.unlinkUploadFiles(rtn.files);
478
- return;
479
- }
480
- /** --- 最终更新的根目录 --- */
481
- const targetPath = await resolvePathInside(kebab.ROOT_CWD, inputPath);
482
- if (!targetPath) {
483
- res.end('Invalid path');
484
- await sRoute.unlinkUploadFiles(rtn.files);
485
- return;
486
- }
487
- const to = targetPath + path.sep;
488
- if (!await lFs.isDir(to)) {
489
- if (rtn.post['strict'] === '1') {
490
- res.end(`[code][0] [${rtn.post['strict']}] Path not found: ${to}`);
491
- await sRoute.unlinkUploadFiles(rtn.files);
466
+ try {
467
+ // --- 更新 code 代码包 ---
468
+ if (!body.file || req.method !== 'POST') {
469
+ res.end('Abnormal');
492
470
  return;
493
471
  }
494
- await lFs.mkdir(to);
495
- }
496
- const buf = await lFs.getContent(file.path);
497
- if (!buf) {
498
- res.end('System error');
499
- await sRoute.unlinkUploadFiles(rtn.files);
500
- return;
501
- }
502
- const zip = await lZip.get(buf);
503
- if (!zip) {
504
- res.end('Zip error');
505
- await sRoute.unlinkUploadFiles(rtn.files);
506
- return;
507
- }
508
- const archiveEntries = zip.readDir('/', {
509
- 'hasChildren': true,
510
- 'hasDir': false,
511
- });
512
- const archiveSize = archiveEntries.reduce((total, item) => total + item.uncompressedSize, 0);
513
- if ((archiveEntries.length > 10_000) || (archiveSize > 512 * 1024 * 1024)) {
514
- res.end('Archive limit exceeded');
515
- await sRoute.unlinkUploadFiles(rtn.files);
516
- return;
517
- }
518
- const rawList = await zip.getList();
519
- /** --- 通过路径检查的 ZIP 文件,路径都以 / 开头 --- */
520
- const ls = {};
521
- for (const archivePath in rawList) {
522
- const normalizedPath = normalizeArchivePath(archivePath);
523
- if (!normalizedPath || (ls[normalizedPath] !== undefined)) {
524
- res.end('Invalid archive path');
525
- await sRoute.unlinkUploadFiles(rtn.files);
472
+ const inputPath = msg.path;
473
+ if (typeof inputPath !== 'string') {
474
+ res.end('Invalid path');
526
475
  return;
527
476
  }
528
- ls[normalizedPath] = rawList[archivePath];
529
- }
530
- // --- 预扫描:收集 .cga 锁定目录和 kebab 子项目目录 ---
531
- /** --- .cga 锁定的目录集合,key 格式为"父路径/目录名/"(不含开头/,含尾部/),例如 "www/pika/" --- */
532
- const cgaLockedDirs = new Set();
533
- /** --- 含有 kebab.json 的目录路径集合(不含开头/,含尾部/,根目录为空字符串) --- */
534
- const kebabProjectDirs = new Set();
535
- /** --- 本次部署涉及到的目录路径集合(不含开头/,含尾部/,根目录为空字符串) --- */
536
- const touchedDirs = new Set();
537
- for (const scanPath in ls) {
538
- /** --- 纯路径中最后一个 / 的位置索引 --- */
539
- const scanLio = scanPath.lastIndexOf('/');
540
- /** --- 纯路径,不以 / 开头,以 / 结尾,若是根路径就是空字符串,例如 "www/pika/" --- */
541
- const scanPat = scanPath.slice(1, scanLio + 1);
542
- /** --- 纯文件名,例如 "index.ts" 或 "pika.cga" --- */
543
- const scanFname = scanPath.slice(scanLio + 1);
544
- touchedDirs.add(scanPat);
545
- if (scanFname.endsWith('.cga')) {
546
- // --- 记录 .cga 锁定的同级目录(去掉 .cga 后缀的名字即为被锁定的目录名)---
547
- cgaLockedDirs.add(scanPat + scanFname.slice(0, -4) + '/');
548
- }
549
- if (scanFname === 'kebab.json') {
550
- // --- 记录含有 kebab.json 的目录(即 kebab 子项目根目录)---
551
- kebabProjectDirs.add(scanPat);
477
+ /** --- 最终更新的根目录 --- */
478
+ const targetPath = await resolvePathInside(kebab.ROOT_CWD, inputPath);
479
+ if (!targetPath) {
480
+ res.end('Invalid path');
481
+ return;
552
482
  }
553
- }
554
- // --- 补充扫描目标目录中现存的 kebab.json,兼容压缩包未携带 kebab.json 的情况 ---
555
- for (const dir of touchedDirs) {
556
- /** --- 把目录拆成各级目录名,先检查当前目录,再一层层往上找 --- */
557
- const dirs = dir ? dir.split('/').filter(Boolean) : [];
558
- for (let i = dirs.length; i >= 0; --i) {
559
- /** --- 当前要检查的目录路径,不含开头/,含尾部/,根目录为空字符串 --- */
560
- const currentDir = i ? dirs.slice(0, i).join('/') + '/' : '';
561
- if (kebabProjectDirs.has(currentDir)) {
562
- break;
563
- }
564
- if (await lFs.isFile(`${to}${currentDir}kebab.json`)) {
565
- kebabProjectDirs.add(currentDir);
566
- break;
483
+ const to = targetPath + path.sep;
484
+ if (!await lFs.isDir(to)) {
485
+ if (msg.strict === '1') {
486
+ res.end(`[code][0] [${msg.strict}] Path not found: ${to}`);
487
+ return;
567
488
  }
489
+ await lFs.mkdir(to);
568
490
  }
569
- }
570
- /** --- kebab 子项目中仅允许部署的子文件夹名集合 --- */
571
- const KEBAB_ALLOWED_DIRS = new Set(['ctr', 'data', 'stc', 'view', 'lib', 'abox']);
572
- /** --- 常见开发、依赖、缓存、构建产物目录不参与部署 --- */
573
- const DEPLOY_EXCLUDE_DIRS = new Set([
574
- '.git',
575
- '.svn',
576
- '.hg',
577
- '.vscode',
578
- '.idea',
579
- '.history',
580
- 'node_modules',
581
- 'bower_components',
582
- '.cache',
583
- '.parcel-cache',
584
- '.turbo',
585
- '.next',
586
- '.nuxt',
587
- '.vite',
588
- 'coverage',
589
- '.nyc_output',
590
- 'tmp',
591
- 'temp',
592
- ]);
593
- for (const archivePath in ls) {
594
- /** --- 纯路径中最后一个 / 的位置索引 --- */
595
- const lio = archivePath.lastIndexOf('/');
596
- /** --- 纯路径,不以 / 开头,以 / 结尾,若是根路径就是空字符串,例如 "www/pika/ctr/" --- */
597
- const pat = archivePath.slice(1, lio + 1);
598
- /** --- 纯文件名,例如 "api.js" --- */
599
- const fname = archivePath.slice(lio + 1);
600
- const dirs = archivePath.slice(1).split('/').slice(0, -1);
601
- if (dirs.some(seg => DEPLOY_EXCLUDE_DIRS.has(seg))) {
602
- // --- 开发、依赖、缓存、构建产物目录不覆盖 ---
603
- continue;
604
- }
605
- if (dirs.some((seg, index) => seg === 'abox' && ['test', 'tests'].includes(dirs[index + 1]))) {
606
- // --- abox 运行代码参与部署,测试目录不进入线上目录 ---
607
- continue;
491
+ const buf = await lFs.getContent(body.file);
492
+ if (!buf) {
493
+ res.end('System error');
494
+ return;
608
495
  }
609
- if (fname === 'kebab.json' && hasSensitiveConfig(ls[archivePath])) {
610
- // --- 包含密码或密钥的 kebab.json 不覆盖,避免部署包覆盖现有敏感配置 ---
611
- continue;
496
+ const zip = await lZip.get(buf);
497
+ if (!zip) {
498
+ res.end('Zip error');
499
+ return;
612
500
  }
613
- if (pat === 'conf/' && fname === 'config.json') {
614
- // --- 全局配置文件不能覆盖 ---
615
- continue;
501
+ const archiveEntries = zip.readDir('/', {
502
+ 'hasChildren': true,
503
+ 'hasDir': false,
504
+ });
505
+ const archiveSize = archiveEntries.reduce((total, item) => total + item.uncompressedSize, 0);
506
+ if ((archiveEntries.length > 10_000) || (archiveSize > 512 * 1024 * 1024)) {
507
+ res.end('Archive limit exceeded');
508
+ return;
616
509
  }
617
- if (fname.endsWith('.js.map') || fname.endsWith('.ts') || fname.endsWith('.tsx') || fname.endsWith('.scss') || fname.endsWith('.gitignore') || fname.endsWith('.DS_Store')) {
618
- // --- 测试或开发文件不覆盖 ---
619
- continue;
510
+ const rawList = await zip.getList();
511
+ /** --- 通过路径检查的 ZIP 文件,路径都以 / 开头 --- */
512
+ const ls = {};
513
+ for (const archivePath in rawList) {
514
+ const normalizedPath = normalizeArchivePath(archivePath);
515
+ if (!normalizedPath || (ls[normalizedPath] !== undefined)) {
516
+ res.end('Invalid archive path');
517
+ return;
518
+ }
519
+ ls[normalizedPath] = rawList[archivePath];
620
520
  }
621
- if (fname === 'AGENTS.md') {
622
- // --- 排除文件不部署 ---
623
- continue;
521
+ // --- 预扫描:收集 .cga 锁定目录和 kebab 子项目目录 ---
522
+ /** --- .cga 锁定的目录集合,key 格式为"父路径/目录名/"(不含开头/,含尾部/),例如 "www/pika/" --- */
523
+ const cgaLockedDirs = new Set();
524
+ /** --- 含有 kebab.json 的目录路径集合(不含开头/,含尾部/,根目录为空字符串) --- */
525
+ const kebabProjectDirs = new Set();
526
+ /** --- 本次部署涉及到的目录路径集合(不含开头/,含尾部/,根目录为空字符串) --- */
527
+ const touchedDirs = new Set();
528
+ for (const scanPath in ls) {
529
+ /** --- 纯路径中最后一个 / 的位置索引 --- */
530
+ const scanLio = scanPath.lastIndexOf('/');
531
+ /** --- 纯路径,不以 / 开头,以 / 结尾,若是根路径就是空字符串,例如 "www/pika/" --- */
532
+ const scanPat = scanPath.slice(1, scanLio + 1);
533
+ /** --- 纯文件名,例如 "index.ts" 或 "pika.cga" --- */
534
+ const scanFname = scanPath.slice(scanLio + 1);
535
+ touchedDirs.add(scanPat);
536
+ if (scanFname.endsWith('.cga')) {
537
+ // --- 记录 .cga 锁定的同级目录(去掉 .cga 后缀的名字即为被锁定的目录名)---
538
+ cgaLockedDirs.add(scanPat + scanFname.slice(0, -4) + '/');
539
+ }
540
+ if (scanFname === 'kebab.json') {
541
+ // --- 记录含有 kebab.json 的目录(即 kebab 子项目根目录)---
542
+ kebabProjectDirs.add(scanPat);
543
+ }
624
544
  }
625
- // --- 规则 1:若同级目录存在 xxx.cga,则 xxx 目录下所有内容不部署 ---
626
- if (pat) {
627
- /** --- 当前目录或上级目录是否有同名 .cga 文件 --- */
628
- let cgaBlocked = false;
629
- /** --- 逐层拼出的目录路径,如 a/、a/b/,检查是否有同名 .cga 文件 --- */
630
- let accumulated = '';
631
- for (const seg of pat.split('/').filter(Boolean)) {
632
- accumulated += seg + '/';
633
- if (cgaLockedDirs.has(accumulated)) {
634
- cgaBlocked = true;
545
+ // --- 补充扫描目标目录中现存的 kebab.json,兼容压缩包未携带 kebab.json 的情况 ---
546
+ for (const dir of touchedDirs) {
547
+ /** --- 把目录拆成各级目录名,先检查当前目录,再一层层往上找 --- */
548
+ const dirs = dir ? dir.split('/').filter(Boolean) : [];
549
+ for (let i = dirs.length; i >= 0; --i) {
550
+ /** --- 当前要检查的目录路径,不含开头/,含尾部/,根目录为空字符串 --- */
551
+ const currentDir = i ? dirs.slice(0, i).join('/') + '/' : '';
552
+ if (kebabProjectDirs.has(currentDir)) {
553
+ break;
554
+ }
555
+ if (await lFs.isFile(`${to}${currentDir}kebab.json`)) {
556
+ kebabProjectDirs.add(currentDir);
635
557
  break;
636
558
  }
637
559
  }
638
- if (cgaBlocked) {
560
+ }
561
+ /** --- kebab 子项目中仅允许部署的子文件夹名集合 --- */
562
+ const KEBAB_ALLOWED_DIRS = new Set(['ctr', 'data', 'stc', 'view', 'lib', 'abox']);
563
+ /** --- 常见开发、依赖、缓存、构建产物目录不参与部署 --- */
564
+ const DEPLOY_EXCLUDE_DIRS = new Set([
565
+ '.git',
566
+ '.svn',
567
+ '.hg',
568
+ '.vscode',
569
+ '.idea',
570
+ '.history',
571
+ 'node_modules',
572
+ 'bower_components',
573
+ '.cache',
574
+ '.parcel-cache',
575
+ '.turbo',
576
+ '.next',
577
+ '.nuxt',
578
+ '.vite',
579
+ 'coverage',
580
+ '.nyc_output',
581
+ 'tmp',
582
+ 'temp',
583
+ ]);
584
+ for (const archivePath in ls) {
585
+ /** --- 纯路径中最后一个 / 的位置索引 --- */
586
+ const lio = archivePath.lastIndexOf('/');
587
+ /** --- 纯路径,不以 / 开头,以 / 结尾,若是根路径就是空字符串,例如 "www/pika/ctr/" --- */
588
+ const pat = archivePath.slice(1, lio + 1);
589
+ /** --- 纯文件名,例如 "api.js" --- */
590
+ const fname = archivePath.slice(lio + 1);
591
+ const dirs = archivePath.slice(1).split('/').slice(0, -1);
592
+ if (dirs.some(seg => DEPLOY_EXCLUDE_DIRS.has(seg))) {
593
+ // --- 开发、依赖、缓存、构建产物目录不覆盖 ---
639
594
  continue;
640
595
  }
641
- }
642
- // --- 规则 2:kebab 子项目目录中,仅允许部署 ctr/data/stc/view/lib/abox 子文件夹内的内容 ---
643
- if (kebabProjectDirs.size > 0) {
644
- // --- 同时属于多个 Kebab 项目时,按最外层项目的部署规则检查 ---
645
- // --- 例如根项目不允许 backup/,backup/ 自带 kebab.json 也不能绕过 ---
646
- /** --- 当前文件所属的最外层 Kebab 项目目录,找不到时为 null --- */
647
- let shortestMatch = null;
648
- for (const kdir of kebabProjectDirs) {
649
- /** --- kdir 是否比已选的项目目录更靠外(路径更短)--- */
650
- const isShallower = shortestMatch === null || kdir.length < shortestMatch.length;
651
- if (pat.startsWith(kdir) && isShallower) {
652
- shortestMatch = kdir;
596
+ if (dirs.some((seg, index) => seg === 'abox' && ['test', 'tests'].includes(dirs[index + 1]))) {
597
+ // --- abox 运行代码参与部署,测试目录不进入线上目录 ---
598
+ continue;
599
+ }
600
+ if (fname === 'kebab.json' && hasSensitiveConfig(ls[archivePath])) {
601
+ // --- 包含密码或密钥的 kebab.json 不覆盖,避免部署包覆盖现有敏感配置 ---
602
+ continue;
603
+ }
604
+ if (pat === 'conf/' && fname === 'config.json') {
605
+ // --- 全局配置文件不能覆盖 ---
606
+ continue;
607
+ }
608
+ if (fname.endsWith('.js.map') || fname.endsWith('.ts') || fname.endsWith('.tsx') || fname.endsWith('.scss') || fname.endsWith('.gitignore') || fname.endsWith('.DS_Store')) {
609
+ // --- 测试或开发文件不覆盖 ---
610
+ continue;
611
+ }
612
+ if (fname === 'AGENTS.md') {
613
+ // --- 排除文件不部署 ---
614
+ continue;
615
+ }
616
+ // --- 规则 1:若同级目录存在 xxx.cga,则 xxx 目录下所有内容不部署 ---
617
+ if (pat) {
618
+ /** --- 当前目录或上级目录是否有同名 .cga 文件 --- */
619
+ let cgaBlocked = false;
620
+ /** --- 逐层拼出的目录路径,如 a/、a/b/,检查是否有同名 .cga 文件 --- */
621
+ let accumulated = '';
622
+ for (const seg of pat.split('/').filter(Boolean)) {
623
+ accumulated += seg + '/';
624
+ if (cgaLockedDirs.has(accumulated)) {
625
+ cgaBlocked = true;
626
+ break;
627
+ }
628
+ }
629
+ if (cgaBlocked) {
630
+ continue;
653
631
  }
654
632
  }
655
- if (shortestMatch !== null) {
656
- // --- 取相对于 kebab 项目目录的路径,检查第一级子目录 ---
657
- /** --- 相对路径,例如若 pat 为 "www/pika/ctr/" 且 shortestMatch 为 "www/pika/",则 relPath 为 "ctr/" --- */
658
- const relPath = pat.slice(shortestMatch.length);
659
- if (relPath) {
660
- /** --- 路径的第一级子目录名,例如 "ctr" 或 "data",用于判断是否在允许列表中 --- */
661
- const firstSeg = relPath.split('/')[0];
662
- if (!KEBAB_ALLOWED_DIRS.has(firstSeg)) {
663
- continue;
633
+ // --- 规则 2:kebab 子项目目录中,仅允许部署 ctr/data/stc/view/lib/abox 子文件夹内的内容 ---
634
+ if (kebabProjectDirs.size > 0) {
635
+ // --- 同时属于多个 Kebab 项目时,按最外层项目的部署规则检查 ---
636
+ // --- 例如根项目不允许 backup/,backup/ 自带 kebab.json 也不能绕过 ---
637
+ /** --- 当前文件所属的最外层 Kebab 项目目录,找不到时为 null --- */
638
+ let shortestMatch = null;
639
+ for (const kdir of kebabProjectDirs) {
640
+ /** --- kdir 是否比已选的项目目录更靠外(路径更短)--- */
641
+ const isShallower = shortestMatch === null || kdir.length < shortestMatch.length;
642
+ if (pat.startsWith(kdir) && isShallower) {
643
+ shortestMatch = kdir;
644
+ }
645
+ }
646
+ if (shortestMatch !== null) {
647
+ // --- 取相对于 kebab 项目目录的路径,检查第一级子目录 ---
648
+ /** --- 相对路径,例如若 pat 为 "www/pika/ctr/" 且 shortestMatch 为 "www/pika/",则 relPath 为 "ctr/" --- */
649
+ const relPath = pat.slice(shortestMatch.length);
650
+ if (relPath) {
651
+ /** --- 路径的第一级子目录名,例如 "ctr" 或 "data",用于判断是否在允许列表中 --- */
652
+ const firstSeg = relPath.split('/')[0];
653
+ if (!KEBAB_ALLOWED_DIRS.has(firstSeg)) {
654
+ continue;
655
+ }
664
656
  }
665
657
  }
666
658
  }
667
- }
668
- // --- 看文件夹是否存在 ---
669
- const targetDir = await resolvePathInside(to, pat);
670
- if (!targetDir) {
671
- res.end('Invalid archive directory');
672
- await sRoute.unlinkUploadFiles(rtn.files);
673
- return;
674
- }
675
- if (pat && !await lFs.isDir(targetDir)) {
676
- if (rtn.post['strict'] === '1') {
677
- res.end(`[code][1] [${rtn.post['strict']}] Path not found: ${targetDir}`);
678
- await sRoute.unlinkUploadFiles(rtn.files);
659
+ // --- 看文件夹是否存在 ---
660
+ const targetDir = await resolvePathInside(to, pat);
661
+ if (!targetDir) {
662
+ res.end('Invalid archive directory');
679
663
  return;
680
664
  }
681
- await lFs.mkdir(targetDir);
682
- }
683
- const targetFile = await resolvePathInside(to, pat + fname);
684
- if (!targetFile) {
685
- res.end('Invalid archive file');
686
- await sRoute.unlinkUploadFiles(rtn.files);
687
- return;
688
- }
689
- // --- 覆盖或创建文件 ---
690
- if ((rtn.post['strict'] === '1') && !await lFs.isFile(targetFile)) {
691
- res.end(`[code][2] [${rtn.post['strict']}] Path not found: ${targetFile}`);
692
- await sRoute.unlinkUploadFiles(rtn.files);
693
- return;
694
- }
695
- await lFs.putContent(targetFile, ls[archivePath]);
696
- }
697
- await sRoute.unlinkUploadFiles(rtn.files);
698
- // --- 检查是否更新 config ---
699
- const configType = rtn.post['config'];
700
- if (configType === '1') {
701
- /** --- 本次部署要更新的静态版本号 --- */
702
- const staticVer = lTime.format(null, 'YmdHis');
703
- for (const kdir of kebabProjectDirs) {
704
- const projectFile = `${to}${kdir}kebab.json`;
705
- if (!await lFs.isFile(projectFile)) {
706
- continue;
665
+ if (pat && !await lFs.isDir(targetDir)) {
666
+ if (msg.strict === '1') {
667
+ res.end(`[code][1] [${msg.strict}] Path not found: ${targetDir}`);
668
+ return;
669
+ }
670
+ await lFs.mkdir(targetDir);
707
671
  }
708
- const projectContent = await lFs.getContent(projectFile, 'utf8');
709
- if (!projectContent) {
710
- continue;
672
+ const targetFile = await resolvePathInside(to, pat + fname);
673
+ if (!targetFile) {
674
+ res.end('Invalid archive file');
675
+ return;
711
676
  }
712
- const projectJson = lText.parseJson(projectContent);
713
- if (!projectJson) {
714
- continue;
677
+ // --- 覆盖或创建文件 ---
678
+ if ((msg.strict === '1') && !await lFs.isFile(targetFile)) {
679
+ res.end(`[code][2] [${msg.strict}] Path not found: ${targetFile}`);
680
+ return;
681
+ }
682
+ await lFs.putContent(targetFile, ls[archivePath]);
683
+ }
684
+ // --- 检查是否更新 config ---
685
+ const configType = msg.config;
686
+ if (configType === '1') {
687
+ /** --- 本次部署要更新的静态版本号 --- */
688
+ const staticVer = lTime.format(null, 'YmdHis');
689
+ for (const kdir of kebabProjectDirs) {
690
+ const projectFile = `${to}${kdir}kebab.json`;
691
+ if (!await lFs.isFile(projectFile)) {
692
+ continue;
693
+ }
694
+ const projectContent = await lFs.getContent(projectFile, 'utf8');
695
+ if (!projectContent) {
696
+ continue;
697
+ }
698
+ const projectJson = lText.parseJson(projectContent);
699
+ if (!projectJson) {
700
+ continue;
701
+ }
702
+ projectJson.set ??= {};
703
+ projectJson.set.staticVer = staticVer;
704
+ await lFs.putContent(projectFile, lText.stringifyJson(projectJson, 4), {
705
+ 'encoding': 'utf8'
706
+ });
707
+ }
708
+ }
709
+ else if (configType === '2') {
710
+ const configContent = await lFs.getContent(kebab.CONF_CWD + 'config.json', 'utf8');
711
+ if (configContent) {
712
+ await lFs.putContent(kebab.CONF_CWD + 'config.json', configContent.replace(/"staticVer": ".+?"/, `"staticVer": "${lTime.format(null, 'YmdHis')}"`), {
713
+ 'encoding': 'utf8'
714
+ });
715
715
  }
716
- projectJson.set ??= {};
717
- projectJson.set.staticVer = staticVer;
718
- await lFs.putContent(projectFile, lText.stringifyJson(projectJson, 4), {
719
- 'encoding': 'utf8'
720
- });
721
716
  }
717
+ break;
722
718
  }
723
- else if (configType === '2') {
724
- const configContent = await lFs.getContent(kebab.CONF_CWD + 'config.json', 'utf8');
725
- if (configContent) {
726
- await lFs.putContent(kebab.CONF_CWD + 'config.json', configContent.replace(/"staticVer": ".+?"/, `"staticVer": "${lTime.format(null, 'YmdHis')}"`), {
727
- 'encoding': 'utf8'
728
- });
719
+ finally {
720
+ if (body.file) {
721
+ await fs.promises.unlink(body.file).catch(() => { });
729
722
  }
730
723
  }
731
- break;
732
724
  }
733
725
  case 'log': {
734
726
  // --- 获取日志信息 ---
@@ -886,7 +878,11 @@ function createRpcListener() {
886
878
  res.end('Invalid path');
887
879
  return;
888
880
  }
889
- const targetPath = lText.urlResolve(kebab.ROOT_CWD, msg.path, true);
881
+ const targetPath = await resolvePathInside(kebab.ROOT_CWD, msg.path);
882
+ if (targetPath === null) {
883
+ res.end('Invalid path');
884
+ return;
885
+ }
890
886
  res.end(lText.stringifyJson({
891
887
  'result': 1,
892
888
  'data': (await lFs.readDir(targetPath, msg.encoding)).map(item => ({