mirror of https://github.com/certd/certd
chore: safePromise 优化
parent
5f85219495
commit
424890a1e1
|
@ -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 { QiniuAccess } from "../access.js";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
|
|
||||||
|
@ -77,7 +77,7 @@ export class QiniuClient {
|
||||||
const http = new HttpClient({ timeout: 10000, middlewares: [auth] });
|
const http = new HttpClient({ timeout: 10000, middlewares: [auth] });
|
||||||
console.log("http", http);
|
console.log("http", http);
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return safePromise((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
http.get({
|
http.get({
|
||||||
url: opts.url,
|
url: opts.url,
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import { isArray } from "lodash-es";
|
import { isArray } from "lodash-es";
|
||||||
import { ILogger } from "@certd/basic";
|
import { ILogger, safePromise } from "@certd/basic";
|
||||||
import { SshAccess } from "./ssh-access.js";
|
import { SshAccess } from "./ssh-access.js";
|
||||||
|
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
|
@ -70,7 +70,7 @@ export class AsyncSsh2Client {
|
||||||
const ssh2 = await import("ssh2");
|
const ssh2 = await import("ssh2");
|
||||||
const ssh2Constants = await import("ssh2/lib/protocol/constants.js");
|
const ssh2Constants = await import("ssh2/lib/protocol/constants.js");
|
||||||
const { SUPPORTED_KEX, SUPPORTED_SERVER_HOST_KEY, SUPPORTED_CIPHER, SUPPORTED_MAC } = ssh2Constants.default;
|
const { SUPPORTED_KEX, SUPPORTED_SERVER_HOST_KEY, SUPPORTED_CIPHER, SUPPORTED_MAC } = ssh2Constants.default;
|
||||||
return new Promise((resolve, reject) => {
|
return safePromise((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
const conn = new ssh2.default.Client();
|
const conn = new ssh2.default.Client();
|
||||||
conn
|
conn
|
||||||
|
@ -108,7 +108,7 @@ export class AsyncSsh2Client {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
async getSftp() {
|
async getSftp() {
|
||||||
return new Promise((resolve, reject) => {
|
return safePromise((resolve, reject) => {
|
||||||
this.logger.info("获取sftp");
|
this.logger.info("获取sftp");
|
||||||
this.conn.sftp((err: any, sftp: any) => {
|
this.conn.sftp((err: any, sftp: any) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
@ -122,7 +122,7 @@ export class AsyncSsh2Client {
|
||||||
|
|
||||||
async fastPut(options: { sftp: any; localPath: string; remotePath: string; opts?: { mode?: string } }) {
|
async fastPut(options: { sftp: any; localPath: string; remotePath: string; opts?: { mode?: string } }) {
|
||||||
const { sftp, localPath, remotePath, opts } = options;
|
const { sftp, localPath, remotePath, opts } = options;
|
||||||
return new Promise((resolve, reject) => {
|
return safePromise((resolve, reject) => {
|
||||||
this.logger.info(`开始上传:${localPath} => ${remotePath}`);
|
this.logger.info(`开始上传:${localPath} => ${remotePath}`);
|
||||||
sftp.fastPut(localPath, remotePath, { ...(opts ?? {}) }, (err: Error) => {
|
sftp.fastPut(localPath, remotePath, { ...(opts ?? {}) }, (err: Error) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
@ -138,7 +138,7 @@ export class AsyncSsh2Client {
|
||||||
|
|
||||||
async listDir(options: { sftp: any; remotePath: string }) {
|
async listDir(options: { sftp: any; remotePath: string }) {
|
||||||
const { sftp, remotePath } = options;
|
const { sftp, remotePath } = options;
|
||||||
return new Promise((resolve, reject) => {
|
return safePromise((resolve, reject) => {
|
||||||
this.logger.info(`listDir:${remotePath}`);
|
this.logger.info(`listDir:${remotePath}`);
|
||||||
sftp.readdir(remotePath, (err: Error, list: any) => {
|
sftp.readdir(remotePath, (err: Error, list: any) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
@ -152,7 +152,7 @@ export class AsyncSsh2Client {
|
||||||
|
|
||||||
async unlink(options: { sftp: any; remotePath: string }) {
|
async unlink(options: { sftp: any; remotePath: string }) {
|
||||||
const { sftp, remotePath } = options;
|
const { sftp, remotePath } = options;
|
||||||
return new Promise((resolve, reject) => {
|
return safePromise((resolve, reject) => {
|
||||||
this.logger.info(`开始删除远程文件:${remotePath}`);
|
this.logger.info(`开始删除远程文件:${remotePath}`);
|
||||||
sftp.unlink(remotePath, (err: Error) => {
|
sftp.unlink(remotePath, (err: Error) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
@ -182,7 +182,7 @@ export class AsyncSsh2Client {
|
||||||
// script += "\r\nexit\r\n";
|
// script += "\r\nexit\r\n";
|
||||||
// //保证windows下正常退出
|
// //保证windows下正常退出
|
||||||
// }
|
// }
|
||||||
return new Promise((resolve, reject) => {
|
return safePromise((resolve, reject) => {
|
||||||
this.logger.info(`执行命令:[${this.connConf.host}][exec]: \n` + script);
|
this.logger.info(`执行命令:[${this.connConf.host}][exec]: \n` + script);
|
||||||
// pty 伪终端,window下的输出会带上conhost.exe之类的多余的字符串,影响返回结果判断
|
// pty 伪终端,window下的输出会带上conhost.exe之类的多余的字符串,影响返回结果判断
|
||||||
// linux下 当使用keyboard-interactive 登录时,需要pty
|
// linux下 当使用keyboard-interactive 登录时,需要pty
|
||||||
|
@ -232,7 +232,7 @@ export class AsyncSsh2Client {
|
||||||
async shell(script: string | string[]): Promise<string> {
|
async shell(script: string | string[]): Promise<string> {
|
||||||
const stripAnsiModule = await import("strip-ansi");
|
const stripAnsiModule = await import("strip-ansi");
|
||||||
const stripAnsi = stripAnsiModule.default;
|
const stripAnsi = stripAnsiModule.default;
|
||||||
return new Promise<any>((resolve, reject) => {
|
return safePromise<any>((resolve, reject) => {
|
||||||
this.logger.info(`执行shell脚本:[${this.connConf.host}][shell]: ` + script);
|
this.logger.info(`执行shell脚本:[${this.connConf.host}][shell]: ` + script);
|
||||||
this.conn.shell((err: Error, stream: any) => {
|
this.conn.shell((err: Error, stream: any) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
@ -299,7 +299,7 @@ export class AsyncSsh2Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
async download(param: { remotePath: string; savePath: string; sftp: any }) {
|
async download(param: { remotePath: string; savePath: string; sftp: any }) {
|
||||||
return new Promise((resolve, reject) => {
|
return safePromise((resolve, reject) => {
|
||||||
const { remotePath, savePath, sftp } = param;
|
const { remotePath, savePath, sftp } = param;
|
||||||
sftp.fastGet(
|
sftp.fastGet(
|
||||||
remotePath,
|
remotePath,
|
||||||
|
@ -385,44 +385,40 @@ export class SshClient {
|
||||||
|
|
||||||
async scpUpload(options: { conn: any; localPath: string; remotePath: string; opts?: { mode?: string } }) {
|
async scpUpload(options: { conn: any; localPath: string; remotePath: string; opts?: { mode?: string } }) {
|
||||||
const { conn, localPath, remotePath } = options;
|
const { conn, localPath, remotePath } = options;
|
||||||
return new Promise((resolve, reject) => {
|
return safePromise((resolve, reject) => {
|
||||||
// 关键步骤:构造 SCP 命令
|
// 关键步骤:构造 SCP 命令
|
||||||
try {
|
this.logger.info(`开始上传:${localPath} => ${remotePath}`);
|
||||||
this.logger.info(`开始上传:${localPath} => ${remotePath}`);
|
conn.conn.exec(
|
||||||
conn.conn.exec(
|
`scp -t ${remotePath}`, // -t 表示目标模式
|
||||||
`scp -t ${remotePath}`, // -t 表示目标模式
|
(err, stream) => {
|
||||||
(err, stream) => {
|
if (err) {
|
||||||
if (err) {
|
return reject(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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
);
|
try {
|
||||||
} catch (e) {
|
// 准备 SCP 协议头
|
||||||
reject(e);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { TencentAccess } from "../access.js";
|
import { TencentAccess } from "../access.js";
|
||||||
import { ILogger } from "@certd/basic";
|
import { ILogger, safePromise } from "@certd/basic";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
|
|
||||||
export class TencentCosClient {
|
export class TencentCosClient {
|
||||||
|
@ -26,7 +26,7 @@ export class TencentCosClient {
|
||||||
|
|
||||||
async uploadFile(key: string, file: Buffer | string) {
|
async uploadFile(key: string, file: Buffer | string) {
|
||||||
const cos = await this.getCosClient();
|
const cos = await this.getCosClient();
|
||||||
return new Promise((resolve, reject) => {
|
return safePromise((resolve, reject) => {
|
||||||
let readableStream = file as any;
|
let readableStream = file as any;
|
||||||
if (typeof file === "string") {
|
if (typeof file === "string") {
|
||||||
readableStream = fs.createReadStream(file);
|
readableStream = fs.createReadStream(file);
|
||||||
|
@ -54,7 +54,7 @@ export class TencentCosClient {
|
||||||
|
|
||||||
async removeFile(key: string) {
|
async removeFile(key: string) {
|
||||||
const cos = await this.getCosClient();
|
const cos = await this.getCosClient();
|
||||||
return new Promise((resolve, reject) => {
|
return safePromise((resolve, reject) => {
|
||||||
cos.deleteObject(
|
cos.deleteObject(
|
||||||
{
|
{
|
||||||
Bucket: this.bucket,
|
Bucket: this.bucket,
|
||||||
|
@ -75,7 +75,7 @@ export class TencentCosClient {
|
||||||
async downloadFile(key: string, savePath: string) {
|
async downloadFile(key: string, savePath: string) {
|
||||||
const cos = await this.getCosClient();
|
const cos = await this.getCosClient();
|
||||||
const writeStream = fs.createWriteStream(savePath);
|
const writeStream = fs.createWriteStream(savePath);
|
||||||
return new Promise((resolve, reject) => {
|
return safePromise((resolve, reject) => {
|
||||||
cos.getObject(
|
cos.getObject(
|
||||||
{
|
{
|
||||||
Bucket: this.bucket,
|
Bucket: this.bucket,
|
||||||
|
@ -96,7 +96,7 @@ export class TencentCosClient {
|
||||||
|
|
||||||
async listDir(dirKey: string) {
|
async listDir(dirKey: string) {
|
||||||
const cos = await this.getCosClient();
|
const cos = await this.getCosClient();
|
||||||
return new Promise((resolve, reject) => {
|
return safePromise((resolve, reject) => {
|
||||||
cos.getBucket(
|
cos.getBucket(
|
||||||
{
|
{
|
||||||
Bucket: this.bucket,
|
Bucket: this.bucket,
|
||||||
|
|
|
@ -2,7 +2,7 @@ import https from 'node:https';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { Application } from '@midwayjs/koa';
|
import { Application } from '@midwayjs/koa';
|
||||||
import { createSelfCertificate } from './self-certificate.js';
|
import { createSelfCertificate } from './self-certificate.js';
|
||||||
import { logger } from '@certd/basic';
|
import {logger, safePromise} from '@certd/basic';
|
||||||
|
|
||||||
export type HttpsServerOptions = {
|
export type HttpsServerOptions = {
|
||||||
enabled: boolean;
|
enabled: boolean;
|
||||||
|
@ -23,7 +23,7 @@ export class HttpsServer {
|
||||||
}
|
}
|
||||||
|
|
||||||
async close() {
|
async close() {
|
||||||
return new Promise((resolve, reject) => {
|
return safePromise((resolve, reject) => {
|
||||||
this.server.close(() => {
|
this.server.close(() => {
|
||||||
resolve(true);
|
resolve(true);
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { logger, utils } from '@certd/basic';
|
import {logger, safePromise, utils} from '@certd/basic';
|
||||||
import { merge } from 'lodash-es';
|
import { merge } from 'lodash-es';
|
||||||
import https from 'https';
|
import https from 'https';
|
||||||
import { PeerCertificate } from 'tls';
|
import { PeerCertificate } from 'tls';
|
||||||
|
@ -49,7 +49,7 @@ export class SiteTester {
|
||||||
);
|
);
|
||||||
options.agent = agent;
|
options.agent = agent;
|
||||||
// 创建 HTTPS 请求
|
// 创建 HTTPS 请求
|
||||||
const requestPromise = new Promise((resolve, reject) => {
|
const requestPromise = safePromise((resolve, reject) => {
|
||||||
const req = https.request(options, res => {
|
const req = https.request(options, res => {
|
||||||
// 获取证书
|
// 获取证书
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
|
|
Loading…
Reference in New Issue