git problem

This commit is contained in:
Cheney 2025-03-11 16:50:44 +08:00
parent 5713e9e663
commit 8149846f16
16 changed files with 1615 additions and 1307 deletions

BIN
kit/bun.lockb Normal file

Binary file not shown.

67
kit/package-lock.json generated
View File

@ -8,11 +8,13 @@
"name": "kit", "name": "kit",
"version": "0.1", "version": "0.1",
"dependencies": { "dependencies": {
"auto-launch": "^5.0.6",
"cross-port-killer": "^1.4.0", "cross-port-killer": "^1.4.0",
"extract-zip": "^2.0.1", "extract-zip": "^2.0.1",
"get-port": "^7.0.0", "get-port": "^7.0.0",
"json5": "^2.2.3", "json5": "^2.2.3",
"log4js": "^6.9.1", "log4js": "^6.9.1",
"physical-cpu-count": "^2.0.0",
"sm-crypto": "^0.3.13", "sm-crypto": "^0.3.13",
"webdav": "^5.3.1" "webdav": "^5.3.1"
}, },
@ -46,6 +48,26 @@
"@types/node": "*" "@types/node": "*"
} }
}, },
"node_modules/applescript": {
"version": "1.0.0",
"resolved": "https://registry.npmmirror.com/applescript/-/applescript-1.0.0.tgz",
"integrity": "sha512-yvtNHdWvtbYEiIazXAdp/NY+BBb65/DAseqlNiJQjOx9DynuzOYDbVLBJvuc0ve0VL9x6B3OHF6eH52y9hCBtQ=="
},
"node_modules/auto-launch": {
"version": "5.0.6",
"resolved": "https://registry.npmmirror.com/auto-launch/-/auto-launch-5.0.6.tgz",
"integrity": "sha512-OgxiAm4q9EBf9EeXdPBiVNENaWE3jUZofwrhAkWjHDYGezu1k3FRZHU8V2FBxGuSJOHzKmTJEd0G7L7/0xDGFA==",
"dependencies": {
"applescript": "^1.0.0",
"mkdirp": "^0.5.1",
"path-is-absolute": "^1.0.0",
"untildify": "^3.0.2",
"winreg": "1.2.4"
},
"engines": {
"node": ">=4.0.0"
}
},
"node_modules/balanced-match": { "node_modules/balanced-match": {
"version": "1.0.2", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz",
@ -359,6 +381,25 @@
"url": "https://github.com/sponsors/isaacs" "url": "https://github.com/sponsors/isaacs"
} }
}, },
"node_modules/minimist": {
"version": "1.2.8",
"resolved": "https://registry.npmmirror.com/minimist/-/minimist-1.2.8.tgz",
"integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==",
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/mkdirp": {
"version": "0.5.6",
"resolved": "https://registry.npmmirror.com/mkdirp/-/mkdirp-0.5.6.tgz",
"integrity": "sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==",
"dependencies": {
"minimist": "^1.2.6"
},
"bin": {
"mkdirp": "bin/cmd.js"
}
},
"node_modules/ms": { "node_modules/ms": {
"version": "2.1.2", "version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
@ -414,6 +455,14 @@
"wrappy": "1" "wrappy": "1"
} }
}, },
"node_modules/path-is-absolute": {
"version": "1.0.1",
"resolved": "https://registry.npmmirror.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz",
"integrity": "sha512-AVbw3UJ2e9bq64vSaS9Am0fje1Pa8pbGqTTsmXfaIiMpnr5DlDhfJOuLj9Sf95ZPVDAUerDfEk88MPmPe7UCQg==",
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/path-posix": { "node_modules/path-posix": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/path-posix/-/path-posix-1.0.0.tgz", "resolved": "https://registry.npmjs.org/path-posix/-/path-posix-1.0.0.tgz",
@ -424,6 +473,11 @@
"resolved": "https://registry.npmjs.org/pend/-/pend-1.2.0.tgz", "resolved": "https://registry.npmjs.org/pend/-/pend-1.2.0.tgz",
"integrity": "sha512-F3asv42UuXchdzt+xXqfW1OGlVBe+mxa2mqI0pg5yAHZPvFmY3Y6drSf/GQ1A86WgWEN9Kzh/WrgKa6iGcHXLg==" "integrity": "sha512-F3asv42UuXchdzt+xXqfW1OGlVBe+mxa2mqI0pg5yAHZPvFmY3Y6drSf/GQ1A86WgWEN9Kzh/WrgKa6iGcHXLg=="
}, },
"node_modules/physical-cpu-count": {
"version": "2.0.0",
"resolved": "https://registry.npmmirror.com/physical-cpu-count/-/physical-cpu-count-2.0.0.tgz",
"integrity": "sha512-rxJOljMuWtYlvREBmd6TZYanfcPhNUKtGDZBjBBS8WG1dpN2iwPsRJZgQqN/OtJuiQckdRFOfzogqJClTrsi7g=="
},
"node_modules/pump": { "node_modules/pump": {
"version": "3.0.0", "version": "3.0.0",
"resolved": "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz", "resolved": "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz",
@ -488,6 +542,14 @@
"node": ">= 4.0.0" "node": ">= 4.0.0"
} }
}, },
"node_modules/untildify": {
"version": "3.0.3",
"resolved": "https://registry.npmmirror.com/untildify/-/untildify-3.0.3.tgz",
"integrity": "sha512-iSk/J8efr8uPT/Z4eSUywnqyrQU7DSdMfdqK4iWEaUVVmcP5JcnpRqmVMwcwcnmI1ATFNgC5V90u09tBynNFKA==",
"engines": {
"node": ">=4"
}
},
"node_modules/url-join": { "node_modules/url-join": {
"version": "5.0.0", "version": "5.0.0",
"resolved": "https://registry.npmjs.org/url-join/-/url-join-5.0.0.tgz", "resolved": "https://registry.npmjs.org/url-join/-/url-join-5.0.0.tgz",
@ -537,6 +599,11 @@
"node": ">=14" "node": ">=14"
} }
}, },
"node_modules/winreg": {
"version": "1.2.4",
"resolved": "https://registry.npmmirror.com/winreg/-/winreg-1.2.4.tgz",
"integrity": "sha512-IHpzORub7kYlb8A43Iig3reOvlcBJGX9gZ0WycHhghHtA65X0LYnMRuJs+aH1abVnMJztQkvQNlltnbPi5aGIA=="
},
"node_modules/wrappy": { "node_modules/wrappy": {
"version": "1.0.2", "version": "1.0.2",
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",

View File

@ -3,17 +3,22 @@
"version": "0.1", "version": "0.1",
"bin": "src/index.js", "bin": "src/index.js",
"scripts": { "scripts": {
"linux": "pkg --compress GZip . -t node18-linux -o ./dist/linux/kit", "linux": "bun build ./src/index.js --compile --bytecode --minify --target=bun-linux-x64-baseline --outfile=./dist/linux/kit",
"windows": "pkg --compress GZip . -t win -o ./dist/windows/kit.exe", "windows": "pkg --compress GZip . -t win -o ./dist/windows/kit.exe",
"alpine" : "pkg --compress GZip . -t node14-alpine-arm64 -o ./dist/linux/kit" "alpine": "pkg --compress GZip . -t node14-alpine-arm64 -o ./dist/linux/kit",
"centos": "pkg --compress GZip . -t node14-linux-arm64 -o ./dist/linux/kit"
}, },
"dependencies": { "dependencies": {
"auto-launch": "^5.0.6",
"cross-port-killer": "^1.4.0", "cross-port-killer": "^1.4.0",
"diskusage-ng": "^1.0.4",
"extract-zip": "^2.0.1", "extract-zip": "^2.0.1",
"get-port": "^7.0.0", "get-port": "^7.0.0",
"json5": "^2.2.3", "json5": "^2.2.3",
"log4js": "^6.9.1", "log4js": "^6.9.1",
"physical-cpu-count": "^2.0.0",
"sm-crypto": "^0.3.13", "sm-crypto": "^0.3.13",
"systeminformation": "^5.25.11",
"webdav": "^5.3.1" "webdav": "^5.3.1"
} }
} }

View File

@ -13,106 +13,124 @@ const fs = require("fs");
const {sm3} = require("sm-crypto"); const {sm3} = require("sm-crypto");
const zlib = require("zlib"); const zlib = require("zlib");
const mem = require("./mem") const mem = require("./mem")
const { getAllDeviceInfo } = require("./platform")
parser.addCmdLine("man", "打开在线帮助;", async function (){ const kitcommand = require("./kitcommand")
const uConfig = await $context.get("uConfig") kitcommand.init()
shell.execSync("start " + uConfig.get("man")) kitcommand.regTo( parser )
});
parser.addCmdLine("manu", "人工维护配置;", function (){
shell.execSync("start " + $sConfig.getUserConfigPath())
});
parser.addCmdLine("run [--file] [func]", "运行项目脚本;", function (cli) {
$logger.info( "kit 当前执行目录 " + process.cwd());
const fs = require("fs");
let step = cli.getParamValue("func");
if ( ! step ) {
step = "main"
}
let file = cli.getParamValue("file");
if ( ! file ) {
file = "kit"
}
const stat = fs.existsSync(`./${file}.js`)
if ( ! stat ) {
if ( ! cli.getParamValue("file") ) {
fs.writeFileSync("./kit.js",
`
async function main(){
console.log("hello kit!")
}
`);
} else {
$logger.info( `文件 ${file} 不存在` );
return
}
}
const projectScript = fs.readFileSync("./kit.js", "utf-8"); // parser.addCmdLine("man", "打开在线帮助;", async function (){
const vm = require('node:vm'); // const uConfig = await $context.get("uConfig")
const script = new vm.Script(projectScript + ";" + step + "()"); // shell.execSync("start " + uConfig.get("man"))
const context = { // });
animal: 'cat', // parser.addCmdLine("manu", "人工维护配置;", function (){
count: 2, // shell.execSync("start " + $sConfig.getUserConfigPath())
$logger // });
}; //
// parser.addCmdLine("run [--file] [func]", "运行项目脚本;", function (cli) {
// $logger.info( "kit 当前执行目录 " + process.cwd());
// const fs = require("fs");
//
// let step = cli.getParamValue("func");
// if ( ! step ) {
// step = "main"
// }
//
// let file = cli.getParamValue("file");
// if ( ! file ) {
// file = "kit"
// }
//
// const stat = fs.existsSync(`./${file}.js`)
// if ( ! stat ) {
// if ( ! cli.getParamValue("file") ) {
// fs.writeFileSync("./kit.js",
// `
// async function main(){
// console.log("hello kit!")
// }
// `);
// } else {
// $logger.info( `文件 ${file} 不存在` );
// return
// }
// }
//
//
// const projectScript = fs.readFileSync("./kit.js", "utf-8");
// const vm = require('node:vm');
// const script = new vm.Script(projectScript + ";" + step + "()");
// const context = {
// animal: 'cat',
// count: 2,
// $logger,
// shell
// };
//
// script.runInNewContext(context, {
// timeout : 10 * 60 * 1000,
// breakOnSigint : true,
// });
// $logger.info( "暂未实现" );
// });
//
// parser.addCmdLine("config <key> [value]", "查询/设置配置;", async function (cli){
// let key = cli.getParamValue("key");
// let value = cli.getParamValue("value");
// const uConfig = await $context.get("uConfig")
// if (null != key && null == value) {
// let v = uConfig.get(key);
// $logger.info("{} : {}", key, v);
// } else if (null != key && null != value) {
// uConfig.set(key, value);
// $logger.info("set {} : {}", key, value);
// } else {
// $logger.error("参数异常");
// }
// });
//
// parser.addCmdLine("remote-check", "验证远程仓库;", async function (){
// let remote = await $context.get("remote")
// if (! ( await remote.check()) ) {
// $logger.error("远程仓库不可用")
// return
// } else {
// $logger.info("远程仓库可用")
// }
// });
//
// parser.addCmdLine("publish", "发布到远程仓库;", async function (){
// let remote = await $context.get("remote")
// if (! ( await remote.check()) ) {
// $logger.error("远程仓库不可用")
// return
// } else {
// $logger.info("远程仓库可用")
// }
// remote.publish()
// });
// parser.addCmdLine("install [module]", "安装/更新模块;", async function (cli){
// let remote = await $context.get("remote")
// if (! ( await remote.check()) ) {
// $logger.error("远程仓库不可用")
// return
// } else {
// $logger.info("远程仓库可用")
// }
// remote.install(cli.getParamValue("module"))
// });
script.runInNewContext(context, {
timeout : 10 * 60 * 1000, parser.addCmdLine("platform", "平台信息", function (cli) {
breakOnSigint : true, // 调用主函数并打印结果
getAllDeviceInfo().then(info => {
console.log('Device Information:');
console.log( JSON.stringify(info, null, 4));
}).catch(error => {
console.error('Error getting device information:', error);
}); });
$logger.info( "暂未实现" );
}); });
parser.addCmdLine("config <key> [value]", "查询/设置配置;", async function (cli){
let key = cli.getParamValue("key");
let value = cli.getParamValue("value");
const uConfig = await $context.get("uConfig")
if (null != key && null == value) {
let v = uConfig.get(key);
$logger.info("{} : {}", key, v);
} else if (null != key && null != value) {
uConfig.set(key, value);
$logger.info("set {} : {}", key, value);
} else {
$logger.error("参数异常");
}
});
parser.addCmdLine("remote-check", "验证远程仓库;", async function (){
let remote = await $context.get("remote")
if (! ( await remote.check()) ) {
$logger.error("远程仓库不可用")
return
} else {
$logger.info("远程仓库可用")
}
});
parser.addCmdLine("publish", "发布到远程仓库;", async function (){
let remote = await $context.get("remote")
if (! ( await remote.check()) ) {
$logger.error("远程仓库不可用")
return
} else {
$logger.info("远程仓库可用")
}
remote.publish()
});
parser.addCmdLine("install [module]", "安装/更新模块;", async function (cli){
let remote = await $context.get("remote")
if (! ( await remote.check()) ) {
$logger.error("远程仓库不可用")
return
} else {
$logger.info("远程仓库可用")
}
remote.install(cli.getParamValue("module"))
});
parser.addCmdLine("freem [level]", "释放内存", function (cli) { parser.addCmdLine("freem [level]", "释放内存", function (cli) {
@ -177,8 +195,10 @@ parser.addCmdLine("telnet [--timeout] <ip-port> [port]", "验证远程 TCP 端
try { try {
await net.telnet(port, ip, cli.getParamValue("timeout")) await net.telnet(port, ip, cli.getParamValue("timeout"))
$logger.info("连接成功") $logger.info("连接成功")
process.exit(0)
}catch (e) { }catch (e) {
$logger.error( e ) $logger.error( e )
process.exit(-1)
} }
}); });
@ -294,6 +314,22 @@ parser.addCmdLine("base64 [-en] [-de] [-hex] <data>", "base64 编解码;", funct
} }
}); });
parser.addCmdLine("touch [-d] <file_name>", "文件时间修改为当前时间;", function (cli) {
const fs = require('fs');
const path = cli.getParamValue("file_name")
// 获取当前日期时间
const now = new Date();
// 修改文件的访问时间和修改时间为当前时间
fs.utimes(path, now, now, (err) => {
if (err) {
$logger.error('修改文件时间出错:', err);
} else {
$logger.info('文件时间修改成功');
}
});
});
parser.addCmdLine("replace [-d] <file_name> <re1> <re2>", "文件内容正则替换;", function (cli) { parser.addCmdLine("replace [-d] <file_name> <re1> <re2>", "文件内容正则替换;", function (cli) {
$logger.info( "暂未实现" ); $logger.info( "暂未实现" );
}); });

View File

@ -1,6 +1,4 @@
const shell = require("./shell"); const shell = require("./shell");
const fs = require("fs");
const vm = require("node:vm");
module.exports = { module.exports = {
@ -50,13 +48,13 @@ async function main(){
const vm = require('node:vm'); const vm = require('node:vm');
const script = new vm.Script(projectScript + ";" + step + "()"); const script = new vm.Script(projectScript + ";" + step + "()");
const context = { const context = {
animal: 'cat', count: 2, $logger animal: 'cat', count: 2, $logger, shell
}; };
script.runInNewContext(context, { script.runInNewContext(context, {
timeout: 10 * 60 * 1000, breakOnSigint: true, timeout: 10 * 60 * 1000, breakOnSigint: true,
}); });
$logger.info("暂未实现"); $logger.info("执行结束");
}); });
parser.addCmdLine("config <key> [value]", "查询/设置配置;", async function (cli) { parser.addCmdLine("config <key> [value]", "查询/设置配置;", async function (cli) {
@ -104,5 +102,6 @@ async function main(){
} }
remote.install(cli.getParamValue("module")) remote.install(cli.getParamValue("module"))
}); });
} }
} }

21
kit/src/launch.js Normal file
View File

@ -0,0 +1,21 @@
/**
* kit 或其他文件注册为开机启动
* kit 启动通过 auto-launch 模块实现其他文件通过 kit 调用系统命令启动
*/
const AutoLaunch = require('auto-launch');
function reg(){
var launch = new AutoLaunch({
name: 'Kit',
path: 'node D:\\fullstack\\TDevOps\\kit\\src\\index.js launch',
});
launch.enable();
}
reg()

87
kit/src/platform.js Normal file
View File

@ -0,0 +1,87 @@
const os = require('os');
const diskusage = require('diskusage-ng');
const { physicalCpuCount } = require('physical-cpu-count');
const { formatBytes } = require("./util/convutil")
// 获取操作系统信息
function getOSInfo() {
return {
type: os.type(),
platform: os.platform(),
release: os.release(),
arch: os.arch()
};
}
// 获取 CPU 信息
function getCPUInfo() {
const cpus = os.cpus();
return {
model: cpus[0].model,
speed: cpus[0].speed,
cores: cpus.length,
physicalCores: physicalCpuCount
};
}
// 获取内存信息
function getMemoryInfo() {
let info = {
totalSize: os.totalmem(),
freeSize: os.freemem()
}
return Object.assign(info, {
total : formatBytes(info.totalSize),
free : formatBytes(info.freeSize),
});
}
// 获取硬盘信息
async function getDiskInfo() {
try {
const path = os.platform() === 'win32' ? 'C:' : '/';
return new Promise(function (resolve, reject) {
diskusage(path, function (err, usage) {
if (err) {
console.error('Error getting disk information:', err);
reject(err);
}
resolve({
totalSize: usage.total,
freeSize: usage.available,
total : formatBytes(usage.total),
free : formatBytes(usage.available),
});
});
})
} catch (error) {
console.error('Error getting disk information:', error);
return null;
}
}
// 主函数,调用上述函数获取所有信息
async function getAllDeviceInfo() {
const osInfo = getOSInfo();
const cpuInfo = getCPUInfo();
const memoryInfo = getMemoryInfo();
const diskInfo = await getDiskInfo();
return {
os: osInfo,
cpu: cpuInfo,
memory: memoryInfo,
disk: diskInfo
};
}
module.exports = {
getOSInfo,
getCPUInfo,
getMemoryInfo,
getDiskInfo,
getAllDeviceInfo
};

View File

@ -23,5 +23,11 @@ module.exports = {
$logger.debug(`execSync ${cmd}`) $logger.debug(`execSync ${cmd}`)
const output = execSync(cmd); const output = execSync(cmd);
return output.toString() return output.toString()
},
callSync(cmd){
$logger.debug(`callSync ${cmd}`)
const output = execSync(cmd);
console.log(output.toString())
} }
} }

View File

@ -1,3 +1,21 @@
function formatBytes(bytes, decimals = 2) {
if (bytes === 0) return "0 B"; // 处理 0 字节的情况
const units = ["B", "K", "M", "G", "T", "P", "E", "Z", "Y"];
let unitIndex = 0;
// 循环除以 1024直到字节数小于 1024 或单位用完
while (bytes >= 1024 && unitIndex < units.length - 1) {
bytes /= 1024;
unitIndex++;
}
// 保留小数位数,并移除末尾多余的零(如 2.0 → 2
const formattedNumber = bytes.toFixed(decimals).replace(/\.0+$/, "");
return `${formattedNumber} ${units[unitIndex]}`;
}
var codePoint2utf8 = function (code) { var codePoint2utf8 = function (code) {
var bin = parseInt(code).toString(2) var bin = parseInt(code).toString(2)
var ln = 0; var ln = 0;
@ -209,135 +227,6 @@ function base64ArrayBuffer(arrayBuffer) {
return base64 return base64
} }
(function() {
var base64EncodeChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
base64DecodeChars = new Array(( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), 62, ( - 1), ( - 1), ( - 1), 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, ( - 1), ( - 1), ( - 1), ( - 1), ( - 1));
this.base64encode = function(e) {
var r, a, c, h, o, t;
for (c = e.length, a = 0, r = ''; a < c;) {
if (h = 255 & e.charCodeAt(a++), a == c) {
r += base64EncodeChars.charAt(h >> 2),
r += base64EncodeChars.charAt((3 & h) << 4),
r += '==';
break
}
if (o = e.charCodeAt(a++), a == c) {
r += base64EncodeChars.charAt(h >> 2),
r += base64EncodeChars.charAt((3 & h) << 4 | (240 & o) >> 4),
r += base64EncodeChars.charAt((15 & o) << 2),
r += '=';
break
}
t = e.charCodeAt(a++),
r += base64EncodeChars.charAt(h >> 2),
r += base64EncodeChars.charAt((3 & h) << 4 | (240 & o) >> 4),
r += base64EncodeChars.charAt((15 & o) << 2 | (192 & t) >> 6),
r += base64EncodeChars.charAt(63 & t)
}
return r
}
this.base64decode = function(e) {
var r, a, c, h, o, t, d;
for (t = e.length, o = 0, d = ''; o < t;) {
do r = base64DecodeChars[255 & e.charCodeAt(o++)];
while (o < t && r == -1);
if (r == -1) break;
do a = base64DecodeChars[255 & e.charCodeAt(o++)];
while (o < t && a == -1);
if (a == -1) break;
d += String.fromCharCode(r << 2 | (48 & a) >> 4);
do {
if (c = 255 & e.charCodeAt(o++), 61 == c) return d;
c = base64DecodeChars[c]
} while ( o < t && c == - 1 );
if (c == -1) break;
d += String.fromCharCode((15 & a) << 4 | (60 & c) >> 2);
do {
if (h = 255 & e.charCodeAt(o++), 61 == h) return d;
h = base64DecodeChars[h]
} while ( o < t && h == - 1 );
if (h == -1) break;
d += String.fromCharCode((3 & c) << 6 | h)
}
return d
}
this.HexToBase64 = function(str) {
return base64encode(String.fromCharCode.apply(null, str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" ")));
}
this.Base64Tohex = function(str) {
for (var i = 0,
bin = base64decode(str.replace(/[ \r\n]+$/, "")), hex = []; i < bin.length; ++i) {
var tmp = bin.charCodeAt(i).toString(16);
if (tmp.length === 1) tmp = "0" + tmp;
hex[hex.length] = tmp;
}
return hex.join("");
},
this.toBytes = function(str) {
for (var bytes = [], c = 0; c < str.length; c += 2)
bytes.push(parseInt(str.substr(c, 2), 16));
return bytes;
}
}) ();
//hexToBase64 Base64Tohex base64decode base64encode
var bytesToString = function(bytes){
return hexToString(bytesToHex(bytes));
}
var bytesToBase64 = function(bytes){
return base64ArrayBuffer(bytes);
}
// Convert a byte array to a hex string
var bytesToHex = function(bytes) {
for (var hex = [], i = 0; i < bytes.length; i++) {
hex.push((bytes[i] >>> 4).toString(16));
hex.push((bytes[i] & 0xF).toString(16));
}
return hex.join("");
}
var stringToBase64 = function(str){
return base64encode(str);
}
var stringToBytes = function(str){
return hexToBytes(stringToHex(str));
}
// Convert a ASCII string to a hex string
var stringToHex = function(str) {
return str.split("").map(function(c) {
return ("0" + c.charCodeAt(0).toString(16)).slice(-2);
}).join("");
}
var hexToBytes= function(hex) {
for (var bytes = [], c = 0; c < hex.length; c += 2)
bytes.push(parseInt(hex.substr(c, 2), 16));
return bytes;
}
// Convert a hex string to a ASCII string
var hexToString = function(hexStr) {
var hex = hexStr.toString();//force conversion
var str = '';
for (var i = 0; i < hex.length; i += 2)
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
return str;
}
var hexToBase64 = function(hexStr){
return HexToBase64(hexStr);
}
var base64ToString = function(base64str){
return base64decode(base64str);
}
const Base64 = { const Base64 = {
@ -434,6 +323,138 @@ const Base64 = {
} }
} }
// (function() {
// var base64EncodeChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
// base64DecodeChars = new Array(( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), 62, ( - 1), ( - 1), ( - 1), 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), ( - 1), 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, ( - 1), ( - 1), ( - 1), ( - 1), ( - 1));
// this.base64encode = function(e) {
// var r, a, c, h, o, t;
// for (c = e.length, a = 0, r = ''; a < c;) {
// if (h = 255 & e.charCodeAt(a++), a == c) {
// r += base64EncodeChars.charAt(h >> 2),
// r += base64EncodeChars.charAt((3 & h) << 4),
// r += '==';
// break
// }
// if (o = e.charCodeAt(a++), a == c) {
// r += base64EncodeChars.charAt(h >> 2),
// r += base64EncodeChars.charAt((3 & h) << 4 | (240 & o) >> 4),
// r += base64EncodeChars.charAt((15 & o) << 2),
// r += '=';
// break
// }
// t = e.charCodeAt(a++),
// r += base64EncodeChars.charAt(h >> 2),
// r += base64EncodeChars.charAt((3 & h) << 4 | (240 & o) >> 4),
// r += base64EncodeChars.charAt((15 & o) << 2 | (192 & t) >> 6),
// r += base64EncodeChars.charAt(63 & t)
// }
// return r
// }
// this.base64decode = function(e) {
// var r, a, c, h, o, t, d;
// for (t = e.length, o = 0, d = ''; o < t;) {
// do r = base64DecodeChars[255 & e.charCodeAt(o++)];
// while (o < t && r == -1);
// if (r == -1) break;
// do a = base64DecodeChars[255 & e.charCodeAt(o++)];
// while (o < t && a == -1);
// if (a == -1) break;
// d += String.fromCharCode(r << 2 | (48 & a) >> 4);
// do {
// if (c = 255 & e.charCodeAt(o++), 61 == c) return d;
// c = base64DecodeChars[c]
// } while ( o < t && c == - 1 );
// if (c == -1) break;
// d += String.fromCharCode((15 & a) << 4 | (60 & c) >> 2);
// do {
// if (h = 255 & e.charCodeAt(o++), 61 == h) return d;
// h = base64DecodeChars[h]
// } while ( o < t && h == - 1 );
// if (h == -1) break;
// d += String.fromCharCode((3 & c) << 6 | h)
// }
// return d
// }
// this.HexToBase64 = function(str) {
// return base64encode(String.fromCharCode.apply(null, str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" ")));
// }
// this.Base64Tohex = function(str) {
// for (var i = 0,
// bin = base64decode(str.replace(/[ \r\n]+$/, "")), hex = []; i < bin.length; ++i) {
// var tmp = bin.charCodeAt(i).toString(16);
// if (tmp.length === 1) tmp = "0" + tmp;
// hex[hex.length] = tmp;
// }
// return hex.join("");
// },
// this.toBytes = function(str) {
// for (var bytes = [], c = 0; c < str.length; c += 2)
// bytes.push(parseInt(str.substr(c, 2), 16));
// return bytes;
// }
//
// }) ();
//hexToBase64 Base64Tohex base64decode base64encode
var bytesToString = function(bytes){
return hexToString(bytesToHex(bytes));
}
var bytesToBase64 = function(bytes){
return base64ArrayBuffer(bytes);
}
// Convert a byte array to a hex string
var bytesToHex = function(bytes) {
for (var hex = [], i = 0; i < bytes.length; i++) {
hex.push((bytes[i] >>> 4).toString(16));
hex.push((bytes[i] & 0xF).toString(16));
}
return hex.join("");
}
var stringToBase64 = function(str){
return base64encode(str);
}
var stringToBytes = function(str){
return hexToBytes(stringToHex(str));
}
// Convert a ASCII string to a hex string
var stringToHex = function(str) {
return str.split("").map(function(c) {
return ("0" + c.charCodeAt(0).toString(16)).slice(-2);
}).join("");
}
var hexToBytes= function(hex) {
for (var bytes = [], c = 0; c < hex.length; c += 2)
bytes.push(parseInt(hex.substr(c, 2), 16));
return bytes;
}
// Convert a hex string to a ASCII string
var hexToString = function(hexStr) {
var hex = hexStr.toString();//force conversion
var str = '';
for (var i = 0; i < hex.length; i += 2)
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
return str;
}
var hexToBase64 = function(hexStr){
return HexToBase64(hexStr);
}
var base64ToString = function(base64str){
return base64decode(base64str);
}
function encodeUtf8(text) { function encodeUtf8(text) {
const code = encodeURIComponent(text); const code = encodeURIComponent(text);
const bytes = []; const bytes = [];
@ -546,6 +567,10 @@ if ( module ) {
utf8ToBase64, utf8ToBase64,
base64ToUtf8, base64ToUtf8,
utf8ToHex, utf8ToHex,
hexToUtf8 hexToUtf8,
formatBytes
} }
} }

BIN
kit/test/kit Normal file

Binary file not shown.

View File

@ -1,7 +1,56 @@
const fs = require("fs"); const fs = require("fs");
const { Transform } = require('stream');
const { pipeline } = require('node:stream/promises'); const { pipeline } = require('node:stream/promises');
const paths = require('path');
function convertBytes(bytes) {
if (bytes >= 1024 * 1024 * 1024) {
const gigabytes = bytes / (1024 * 1024 * 1024);
return `${gigabytes.toFixed(2)}GB`;
} else if (bytes >= 1024 * 1024) {
const megabytes = bytes / (1024 * 1024);
return `${megabytes.toFixed(2)}MB`;
} else if (bytes >= 1024 ) {
const megabytes = bytes / (1024 );
return `${megabytes.toFixed(2)}KB`;
} else {
return `${bytes} B`;
}
}
// 创建一个用于统计字节数的Transform流
class ByteCounter extends Transform {
constructor(totalSize) {
super();
this.bytesCount = 0;
this.totalSize = totalSize;
this.timestamp = (new Date()).getTime()
this.recordBytesCount = 0;
this.speed = 0;
this.intervalId = setInterval(()=>{
let now = (new Date()).getTime()
let timecost = now - this.timestamp;
this.speed = ((this.bytesCount - this.recordBytesCount ) * 1000 / timecost).toFixed(2)
this.timestamp = now;
this.recordBytesCount = this.bytesCount;
}, 2000);
}
_transform(chunk, encoding, callback) {
this.bytesCount += chunk.length;
let process = (this.bytesCount * 100 / this.totalSize).toFixed(2)
console.log(`进度 ${process}%\t速度${ convertBytes(this.speed) }/s\t${convertBytes(this.bytesCount)} / ${convertBytes(this.totalSize)}`);
callback(null, chunk);
}
_flush(callback) {
if (this.intervalId) {
clearInterval(this.intervalId);
}
callback();
}
}
async function wUpload(client , from, to){ async function wUpload(client , from, to){
const totalSize = fs.statSync(from).size; const totalSize = fs.statSync(from).size;
const rs = fs.createReadStream( from ); const rs = fs.createReadStream( from );
@ -22,33 +71,53 @@ async function wUpload(client , from, to){
return pipeline( rs, ws ); return pipeline( rs, ws );
} }
/**
* 文件不存在会抛出异常
* @param client
* @param file
* @param to
* @returns {Promise<void>}
*/
async function wDownload(client, file, to){
// 确认文件存在以及大小
let stat = await client.stat(file);
let totalSize = stat.size;
if ( ! to ) {
to = paths.basename(file);
}
const byteCounter = new ByteCounter(totalSize);
await pipeline(
client.createReadStream(file),
byteCounter,
fs.createWriteStream(to)
);
await pipeline(rs , ws );
}
async function main(){ async function main(){
const webdav = await import("webdav"); const webdav = await import("webdav");
const client = webdav.createClient(
"http://baishe.fullstack.club:5244/dav",
{
username: "cheney",
password: "722V587"
}
);
// const client = webdav.createClient( // const client = webdav.createClient(
// "http://172.16.17.230:5244/dav", // "http://baishe.fullstack.club:5244/dav",
// { // {
// username: "kit", // username: "cheney",
// password: "kitkit" // password: "722V587"
// } // }
// ); // );
const client = webdav.createClient(
"http://172.16.18.45:5244/dav",
{
username: "kit",
password: "kitkit"
}
);
// Get directory contents // Get directory contents
const directoryItems = await client.getDirectoryContents("/"); const directoryItems = await client.getDirectoryContents("/");
console.log(directoryItems) console.log(directoryItems)
// await pipeline( // 下载测试
// client.createReadStream( await wDownload(client, "/kit")
// "/index.json"
// ),
// fs.createWriteStream("./xxx.json")
// );
// let rs = fs.createReadStream("D:\\fullstack\\TDevOps\\kit\\dist\\windows\\kit.exe") // let rs = fs.createReadStream("D:\\fullstack\\TDevOps\\kit\\dist\\windows\\kit.exe")
// rs.on("data", (r)=>{ // rs.on("data", (r)=>{
@ -64,10 +133,10 @@ async function main(){
// ws.on("data", (r)=>{ // ws.on("data", (r)=>{
// console.log("w", r.length) // console.log("w", r.length)
// }) // })
// await pipeline(rs , ws );
// console.log("end") // console.log("end")
await wUpload( client, "./kit", "/kit" ) // await wUpload( client, "./kit", "/kit" )
// await wUpload( client, "../dist/windows/kit.exe", "/modules/kit/0.1.20240227094429/Windows/X86_64/kit.exe" ) // await wUpload( client, "../dist/windows/kit.exe", "/modules/kit/0.1.20240227094429/Windows/X86_64/kit.exe" )
console.log("over") console.log("over")
} }

View File

@ -1,7 +0,0 @@
[2024-09-02T17:10:24.971] [INFO] default - 执行命令 checkip
[2024-09-02T17:10:25.242] [INFO] default - ip : 61.164.40.82
[2024-09-02T17:10:25.243] [INFO] default - location : 中国 浙江 杭州 电信
[2024-09-02T17:10:25.244] [INFO] default -
61.164.40.82
[2024-09-02T17:32:08.694] [INFO] default - 执行命令 freem
[2024-09-02T17:32:22.663] [INFO] default - 执行命令 freem