@dongnanyang/deploy-cli 0.5.1 → 0.5.3

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/dist/bin.js CHANGED
@@ -2,7 +2,7 @@
2
2
  const argv = process.argv.slice(2);
3
3
  if (argv.length === 0 || argv[0] === "tui") await import("./main.js");
4
4
  else {
5
- const cli = await import("./cli-DC-8dd-4.js");
5
+ const cli = await import("./cli-T37L9fUN.js");
6
6
  process.exitCode = await cli.main(argv);
7
7
  }
8
8
  //#endregion
@@ -459,6 +459,10 @@ function removeProject(cfg, name) {
459
459
  const CONF_PREFIX = "deploy-cli-";
460
460
  const CONF_SUFFIX = ".conf";
461
461
  const SLUG_RE = /^[a-z0-9-]+$/;
462
+ /** 渲染进 conf 的字段形状校验(防 conf 注入):域名 / 绝对路径 / host:port,统一排除 conf 元字符与空白 */
463
+ const DOMAIN_RE = /^(\*\.)?[A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])?)*$/;
464
+ const ABS_PATH_RE = /^\/[A-Za-z0-9._/-]*$/;
465
+ const UPSTREAM_RE = /^(\[[0-9a-fA-F:.]+\]|[A-Za-z0-9.-]+):([0-9]{1,5})$/;
462
466
  const DEFAULT_SITES_AVAILABLE = "/etc/nginx/sites-available";
463
467
  const DEFAULT_SITES_ENABLED = "/etc/nginx/sites-enabled";
464
468
  const DEFAULT_CONF_D = "/etc/nginx/conf.d";
@@ -482,8 +486,19 @@ function validateSlug(slug) {
482
486
  function validateSpec(spec) {
483
487
  validateSlug(spec.slug);
484
488
  if (!spec.serverName?.trim()) throw new Error("NginxSiteSpec 缺少 serverName");
489
+ if (!DOMAIN_RE.test(spec.serverName)) throw new Error(`NginxSiteSpec.serverName 非法: ${JSON.stringify(spec.serverName)},仅允许域名字符(字母数字.-,可选 *. 前缀;禁止 conf 元字符与空白)`);
485
490
  if ((spec.root !== void 0 && spec.root !== "") === (spec.upstream !== void 0 && spec.upstream !== "")) throw new Error("NginxSiteSpec 需要且仅需要 root(static 型)或 upstream(代理型)之一");
491
+ if (spec.root !== void 0 && spec.root !== "") validateAbsPath("root", spec.root);
492
+ if (spec.upstream !== void 0 && spec.upstream !== "") {
493
+ const match = spec.upstream.match(UPSTREAM_RE);
494
+ if (!match || Number(match[2]) < 1 || Number(match[2]) > 65535) throw new Error(`NginxSiteSpec.upstream 非法: ${JSON.stringify(spec.upstream)},须为 host:port(端口 1-65535;禁止 conf 元字符与空白)`);
495
+ }
486
496
  if (spec.ssl === "custom" && (!spec.certPath || !spec.keyPath)) throw new Error("ssl:custom 需要 certPath 与 keyPath");
497
+ if (spec.certPath) validateAbsPath("certPath", spec.certPath);
498
+ if (spec.keyPath) validateAbsPath("keyPath", spec.keyPath);
499
+ }
500
+ function validateAbsPath(field, value) {
501
+ if (!ABS_PATH_RE.test(value)) throw new Error(`NginxSiteSpec.${field} 非法: ${JSON.stringify(value)},须为绝对路径且不含 conf 元字符与空白`);
487
502
  }
488
503
  /** 失败信息:命令原文 + exit 码 + stderr 摘要(包裹为 ExecError,附完整结果) */
489
504
  function execFailure(cmd, args, result, note) {
@@ -541,14 +556,16 @@ function createNginx(runner = run, paths) {
541
556
  const sitesAvailable = resolved.sitesAvailable;
542
557
  const sitesEnabled = resolved.sitesEnabled;
543
558
  const confName = (slug) => `${CONF_PREFIX}${slug}${CONF_SUFFIX}`;
559
+ const tmpName = (slug) => `.${confName(slug)}.new`;
560
+ const bakName = (slug) => `.${confName(slug)}.bak`;
544
561
  /**
545
- * 写盘/回滚流程:新内容写 .conf.new → 旧 conf 备份 .bak → .new 原子改名 → 建 sites-enabled 软链 → nginx -t;
562
+ * 写盘/回滚流程:新内容写隐藏 .conf.new → 旧 conf 备份隐藏 .bak → .new 原子改名 → 建 sites-enabled 软链 → nginx -t;
546
563
  * 失败:恢复 .bak(或删新文件)、移除本次新建的软链、抛 ExecError;成功:nginx -s reload、清 .bak。
547
564
  */
548
565
  async function writeSite(slug, content) {
549
566
  const conf = join$1(sitesAvailable, confName(slug));
550
- const tmp = `${conf}.new`;
551
- const bak = `${conf}.bak`;
567
+ const tmp = join$1(sitesAvailable, tmpName(slug));
568
+ const bak = join$1(sitesAvailable, bakName(slug));
552
569
  writeFileSync$1(tmp, content, "utf-8");
553
570
  const existed = pathExists(conf);
554
571
  if (existed) renameSync(conf, bak);
@@ -556,14 +573,28 @@ function createNginx(runner = run, paths) {
556
573
  const link = sitesEnabled ? join$1(sitesEnabled, confName(slug)) : void 0;
557
574
  const linkCreated = link ? !pathExists(link) : false;
558
575
  if (link && linkCreated) symlinkSync(conf, link);
559
- const test = await runner("nginx", ["-t"]);
560
- if (test.code !== 0) {
576
+ const rollback = () => {
561
577
  if (existed) renameSync(bak, conf);
562
578
  else unlinkSync(conf);
563
579
  if (link && linkCreated) unlinkSync(link);
580
+ };
581
+ let test;
582
+ try {
583
+ test = await runner("nginx", ["-t"]);
584
+ } catch (error) {
585
+ rollback();
586
+ throw error;
587
+ }
588
+ if (test.code !== 0) {
589
+ rollback();
564
590
  throw execFailure("nginx", ["-t"], test, `nginx -t 校验失败,已回滚站点配置 ${conf}`);
565
591
  }
566
- const reload = await runner("nginx", ["-s", "reload"]);
592
+ let reload;
593
+ try {
594
+ reload = await runner("nginx", ["-s", "reload"]);
595
+ } catch (error) {
596
+ throw error;
597
+ }
567
598
  if (reload.code !== 0) throw execFailure("nginx", ["-s", "reload"], reload, `nginx reload 失败,保留备份 ${bak} 供人工恢复`);
568
599
  if (existed) unlinkSync(bak);
569
600
  }
@@ -622,18 +653,32 @@ function createNginx(runner = run, paths) {
622
653
  validateSlug(slug);
623
654
  const conf = join$1(sitesAvailable, confName(slug));
624
655
  if (!pathExists(conf)) throw new Error(`未找到站点配置: ${conf}`);
625
- const bak = `${conf}.bak`;
656
+ const bak = join$1(sitesAvailable, bakName(slug));
626
657
  const link = sitesEnabled ? join$1(sitesEnabled, confName(slug)) : void 0;
627
658
  const hadLink = link ? pathExists(link) : false;
628
659
  renameSync(conf, bak);
629
660
  if (link && hadLink) unlinkSync(link);
630
- const test = await runner("nginx", ["-t"]);
631
- if (test.code !== 0) {
661
+ const rollback = () => {
632
662
  renameSync(bak, conf);
633
663
  if (link && hadLink) symlinkSync(conf, link);
664
+ };
665
+ let test;
666
+ try {
667
+ test = await runner("nginx", ["-t"]);
668
+ } catch (error) {
669
+ rollback();
670
+ throw error;
671
+ }
672
+ if (test.code !== 0) {
673
+ rollback();
634
674
  throw execFailure("nginx", ["-t"], test, `nginx -t 校验失败,已回滚删除操作 ${conf}`);
635
675
  }
636
- const reload = await runner("nginx", ["-s", "reload"]);
676
+ let reload;
677
+ try {
678
+ reload = await runner("nginx", ["-s", "reload"]);
679
+ } catch (error) {
680
+ throw error;
681
+ }
637
682
  if (reload.code !== 0) throw execFailure("nginx", ["-s", "reload"], reload, `nginx reload 失败,保留备份 ${bak} 供人工恢复`);
638
683
  unlinkSync(bak);
639
684
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dongnanyang/deploy-cli",
3
- "version": "0.5.1",
3
+ "version": "0.5.3",
4
4
  "description": "Local CLI TUI for SSH tunnels & deploy scripts",
5
5
  "type": "module",
6
6
  "bin": {