From 424890a1e19ed3a58db56090051ba6ed34c09522 Mon Sep 17 00:00:00 2001 From: xiaojunnuo Date: Wed, 30 Apr 2025 09:38:44 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20safePromise=20=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugins/plugin-lib/src/qiniu/lib/sdk.ts | 4 +- packages/plugins/plugin-lib/src/ssh/ssh.ts | 86 +++++++++---------- .../plugin-lib/src/tencent/lib/cos-client.ts | 10 +-- .../src/modules/auto/https/server.ts | 4 +- .../modules/monitor/service/site-tester.ts | 4 +- 5 files changed, 52 insertions(+), 56 deletions(-) diff --git a/packages/plugins/plugin-lib/src/qiniu/lib/sdk.ts b/packages/plugins/plugin-lib/src/qiniu/lib/sdk.ts index 39a22946..f6cd04b3 100644 --- a/packages/plugins/plugin-lib/src/qiniu/lib/sdk.ts +++ b/packages/plugins/plugin-lib/src/qiniu/lib/sdk.ts @@ -1,4 +1,4 @@ -import { HttpClient, ILogger, utils } from "@certd/basic"; +import { HttpClient, ILogger, safePromise, utils } from "@certd/basic"; import { QiniuAccess } from "../access.js"; import fs from "fs"; @@ -77,7 +77,7 @@ export class QiniuClient { const http = new HttpClient({ timeout: 10000, middlewares: [auth] }); console.log("http", http); - return new Promise((resolve, reject) => { + return safePromise((resolve, reject) => { try { http.get({ url: opts.url, diff --git a/packages/plugins/plugin-lib/src/ssh/ssh.ts b/packages/plugins/plugin-lib/src/ssh/ssh.ts index cce23731..c026022e 100644 --- a/packages/plugins/plugin-lib/src/ssh/ssh.ts +++ b/packages/plugins/plugin-lib/src/ssh/ssh.ts @@ -1,7 +1,7 @@ // @ts-ignore import path from "path"; import { isArray } from "lodash-es"; -import { ILogger } from "@certd/basic"; +import { ILogger, safePromise } from "@certd/basic"; import { SshAccess } from "./ssh-access.js"; import fs from "fs"; @@ -70,7 +70,7 @@ export class AsyncSsh2Client { const ssh2 = await import("ssh2"); const ssh2Constants = await import("ssh2/lib/protocol/constants.js"); const { SUPPORTED_KEX, SUPPORTED_SERVER_HOST_KEY, SUPPORTED_CIPHER, SUPPORTED_MAC } = ssh2Constants.default; - return new Promise((resolve, reject) => { + return safePromise((resolve, reject) => { try { const conn = new ssh2.default.Client(); conn @@ -108,7 +108,7 @@ export class AsyncSsh2Client { }); } async getSftp() { - return new Promise((resolve, reject) => { + return safePromise((resolve, reject) => { this.logger.info("获取sftp"); this.conn.sftp((err: any, sftp: any) => { if (err) { @@ -122,7 +122,7 @@ export class AsyncSsh2Client { async fastPut(options: { sftp: any; localPath: string; remotePath: string; opts?: { mode?: string } }) { const { sftp, localPath, remotePath, opts } = options; - return new Promise((resolve, reject) => { + return safePromise((resolve, reject) => { this.logger.info(`开始上传:${localPath} => ${remotePath}`); sftp.fastPut(localPath, remotePath, { ...(opts ?? {}) }, (err: Error) => { if (err) { @@ -138,7 +138,7 @@ export class AsyncSsh2Client { async listDir(options: { sftp: any; remotePath: string }) { const { sftp, remotePath } = options; - return new Promise((resolve, reject) => { + return safePromise((resolve, reject) => { this.logger.info(`listDir:${remotePath}`); sftp.readdir(remotePath, (err: Error, list: any) => { if (err) { @@ -152,7 +152,7 @@ export class AsyncSsh2Client { async unlink(options: { sftp: any; remotePath: string }) { const { sftp, remotePath } = options; - return new Promise((resolve, reject) => { + return safePromise((resolve, reject) => { this.logger.info(`开始删除远程文件:${remotePath}`); sftp.unlink(remotePath, (err: Error) => { if (err) { @@ -182,7 +182,7 @@ export class AsyncSsh2Client { // script += "\r\nexit\r\n"; // //保证windows下正常退出 // } - return new Promise((resolve, reject) => { + return safePromise((resolve, reject) => { this.logger.info(`执行命令:[${this.connConf.host}][exec]: \n` + script); // pty 伪终端,window下的输出会带上conhost.exe之类的多余的字符串,影响返回结果判断 // linux下 当使用keyboard-interactive 登录时,需要pty @@ -232,7 +232,7 @@ export class AsyncSsh2Client { async shell(script: string | string[]): Promise { const stripAnsiModule = await import("strip-ansi"); const stripAnsi = stripAnsiModule.default; - return new Promise((resolve, reject) => { + return safePromise((resolve, reject) => { this.logger.info(`执行shell脚本:[${this.connConf.host}][shell]: ` + script); this.conn.shell((err: Error, stream: any) => { if (err) { @@ -299,7 +299,7 @@ export class AsyncSsh2Client { } async download(param: { remotePath: string; savePath: string; sftp: any }) { - return new Promise((resolve, reject) => { + return safePromise((resolve, reject) => { const { remotePath, savePath, sftp } = param; sftp.fastGet( remotePath, @@ -385,44 +385,40 @@ export class SshClient { async scpUpload(options: { conn: any; localPath: string; remotePath: string; opts?: { mode?: string } }) { const { conn, localPath, remotePath } = options; - return new Promise((resolve, reject) => { + return safePromise((resolve, reject) => { // 关键步骤:构造 SCP 命令 - try { - this.logger.info(`开始上传:${localPath} => ${remotePath}`); - conn.conn.exec( - `scp -t ${remotePath}`, // -t 表示目标模式 - (err, stream) => { - if (err) { - return reject(err); - } - try { - // 准备 SCP 协议头 - const fileStats = fs.statSync(localPath); - const fileName = path.basename(localPath); - - // SCP 协议格式:C[权限] [文件大小] [文件名]\n - stream.write(`C0644 ${fileStats.size} ${fileName}\n`); - - // 通过管道传输文件 - fs.createReadStream(localPath) - .on("error", e => { - this.logger.info("read stream error", e); - reject(e); - }) - .pipe(stream) - .on("finish", async () => { - this.logger.info(`上传完成:${localPath} => ${remotePath}`); - resolve(true); - }) - .on("error", reject); - } catch (e) { - reject(e); - } + this.logger.info(`开始上传:${localPath} => ${remotePath}`); + conn.conn.exec( + `scp -t ${remotePath}`, // -t 表示目标模式 + (err, stream) => { + if (err) { + return reject(err); } - ); - } catch (e) { - reject(e); - } + try { + // 准备 SCP 协议头 + const fileStats = fs.statSync(localPath); + const fileName = path.basename(localPath); + + // SCP 协议格式:C[权限] [文件大小] [文件名]\n + stream.write(`C0644 ${fileStats.size} ${fileName}\n`); + + // 通过管道传输文件 + fs.createReadStream(localPath) + .on("error", e => { + this.logger.info("read stream error", e); + reject(e); + }) + .pipe(stream) + .on("finish", async () => { + this.logger.info(`上传完成:${localPath} => ${remotePath}`); + resolve(true); + }) + .on("error", reject); + } catch (e) { + reject(e); + } + } + ); }); } diff --git a/packages/plugins/plugin-lib/src/tencent/lib/cos-client.ts b/packages/plugins/plugin-lib/src/tencent/lib/cos-client.ts index cfa72acb..b733f0e6 100644 --- a/packages/plugins/plugin-lib/src/tencent/lib/cos-client.ts +++ b/packages/plugins/plugin-lib/src/tencent/lib/cos-client.ts @@ -1,5 +1,5 @@ import { TencentAccess } from "../access.js"; -import { ILogger } from "@certd/basic"; +import { ILogger, safePromise } from "@certd/basic"; import fs from "fs"; export class TencentCosClient { @@ -26,7 +26,7 @@ export class TencentCosClient { async uploadFile(key: string, file: Buffer | string) { const cos = await this.getCosClient(); - return new Promise((resolve, reject) => { + return safePromise((resolve, reject) => { let readableStream = file as any; if (typeof file === "string") { readableStream = fs.createReadStream(file); @@ -54,7 +54,7 @@ export class TencentCosClient { async removeFile(key: string) { const cos = await this.getCosClient(); - return new Promise((resolve, reject) => { + return safePromise((resolve, reject) => { cos.deleteObject( { Bucket: this.bucket, @@ -75,7 +75,7 @@ export class TencentCosClient { async downloadFile(key: string, savePath: string) { const cos = await this.getCosClient(); const writeStream = fs.createWriteStream(savePath); - return new Promise((resolve, reject) => { + return safePromise((resolve, reject) => { cos.getObject( { Bucket: this.bucket, @@ -96,7 +96,7 @@ export class TencentCosClient { async listDir(dirKey: string) { const cos = await this.getCosClient(); - return new Promise((resolve, reject) => { + return safePromise((resolve, reject) => { cos.getBucket( { Bucket: this.bucket, diff --git a/packages/ui/certd-server/src/modules/auto/https/server.ts b/packages/ui/certd-server/src/modules/auto/https/server.ts index 93910472..8804dd06 100644 --- a/packages/ui/certd-server/src/modules/auto/https/server.ts +++ b/packages/ui/certd-server/src/modules/auto/https/server.ts @@ -2,7 +2,7 @@ import https from 'node:https'; import fs from 'fs'; import { Application } from '@midwayjs/koa'; import { createSelfCertificate } from './self-certificate.js'; -import { logger } from '@certd/basic'; +import {logger, safePromise} from '@certd/basic'; export type HttpsServerOptions = { enabled: boolean; @@ -23,7 +23,7 @@ export class HttpsServer { } async close() { - return new Promise((resolve, reject) => { + return safePromise((resolve, reject) => { this.server.close(() => { resolve(true); }); diff --git a/packages/ui/certd-server/src/modules/monitor/service/site-tester.ts b/packages/ui/certd-server/src/modules/monitor/service/site-tester.ts index ec677bb8..903fbdc5 100644 --- a/packages/ui/certd-server/src/modules/monitor/service/site-tester.ts +++ b/packages/ui/certd-server/src/modules/monitor/service/site-tester.ts @@ -1,4 +1,4 @@ -import { logger, utils } from '@certd/basic'; +import {logger, safePromise, utils} from '@certd/basic'; import { merge } from 'lodash-es'; import https from 'https'; import { PeerCertificate } from 'tls'; @@ -49,7 +49,7 @@ export class SiteTester { ); options.agent = agent; // 创建 HTTPS 请求 - const requestPromise = new Promise((resolve, reject) => { + const requestPromise = safePromise((resolve, reject) => { const req = https.request(options, res => { // 获取证书 // @ts-ignore