From 4275120cba7b15780e19359db31d96359602aaeb Mon Sep 17 00:00:00 2001 From: Cheney Date: Mon, 2 Sep 2024 17:37:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20freem=20=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kit/package-lock.json | 4 ++-- kit/package.json | 2 +- kit/src/index.js | 8 ++++++++ kit/src/mem.js | 22 ++++++++++++++++++++++ kit/src/util/osutil.js | 8 ++++---- kit/test/net_test.js | 2 -- kit/~/.kit/logs/kit.2024-09-02.log | 7 +++++++ 7 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 kit/src/mem.js create mode 100644 kit/~/.kit/logs/kit.2024-09-02.log diff --git a/kit/package-lock.json b/kit/package-lock.json index 8bebcde..dfded14 100644 --- a/kit/package-lock.json +++ b/kit/package-lock.json @@ -1,12 +1,12 @@ { "name": "kit", - "version": "1.0.0", + "version": "0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "kit", - "version": "1.0.0", + "version": "0.1", "dependencies": { "cross-port-killer": "^1.4.0", "extract-zip": "^2.0.1", diff --git a/kit/package.json b/kit/package.json index 8472ae6..683e782 100644 --- a/kit/package.json +++ b/kit/package.json @@ -3,7 +3,7 @@ "version": "0.1", "bin": "src/index.js", "scripts": { - "linux": "pkg --compress GZip . -t linux -o ./dist/linux/kit", + "linux": "pkg --compress GZip . -t node18-linux -o ./dist/linux/kit", "windows": "pkg --compress GZip . -t win -o ./dist/windows/kit.exe", "alpine" : "pkg --compress GZip . -t node14-alpine-arm64 -o ./dist/linux/kit" }, diff --git a/kit/src/index.js b/kit/src/index.js index 60ebc52..c7663f0 100644 --- a/kit/src/index.js +++ b/kit/src/index.js @@ -12,6 +12,7 @@ const path = require("path"); const fs = require("fs"); const {sm3} = require("sm-crypto"); const zlib = require("zlib"); +const mem = require("./mem") parser.addCmdLine("man", "打开在线帮助;", async function (){ const uConfig = await $context.get("uConfig") @@ -113,6 +114,13 @@ parser.addCmdLine("install [module]", "安装/更新模块;", async function (cl remote.install(cli.getParamValue("module")) }); + +parser.addCmdLine("freem [level]", "释放内存", function (cli) { + let level = cli.getParamValue("level") + mem.free( level ) +}); + + parser.addCmdLine("killport ", "释放端口", function (cli) { let port = cli.getParamValue("port") net.killport( parseInt(port, 10) ); diff --git a/kit/src/mem.js b/kit/src/mem.js new file mode 100644 index 0000000..707b7f2 --- /dev/null +++ b/kit/src/mem.js @@ -0,0 +1,22 @@ +const osutil = require("./util/osutil") +const shell = require("./shell") + +function free(level) { + osutil.choise({ + [osutil.Windows] : ( level ) => { + throw Error("暂不支持") + }, + [osutil.Linux] : ( level ) => { + if ( [1,2,3].indexOf( parseInt(level,10)) === -1 ) { + level = 1 + } + shell.execSync("sync") + shell.execSync(`echo ${level} > /proc/sys/vm/drop_caches`) + }, + }) +} + + +module.exports = { + free +} \ No newline at end of file diff --git a/kit/src/util/osutil.js b/kit/src/util/osutil.js index c6fe006..eea5a88 100644 --- a/kit/src/util/osutil.js +++ b/kit/src/util/osutil.js @@ -1,7 +1,7 @@ const os = require('os'); const fs = require("fs"); -const Windows = 0; -const Linux = 1; +const Windows = "Windows"; +const Linux = "Linux"; module.exports = { @@ -11,11 +11,11 @@ module.exports = { * 根据 os 不同,载入不同的方法 */ choise : function ( cbs ) { - if (os.type() == 'Windows_NT') { + if (os.type() === 'Windows_NT') { // windows平台 return cbs && cbs[ Windows ] } - else if (os.type() == 'Linux') { + else if (os.type() === 'Linux') { // Linux平台 return cbs && cbs[ Linux ] } diff --git a/kit/test/net_test.js b/kit/test/net_test.js index 40705a3..45ce4f1 100644 --- a/kit/test/net_test.js +++ b/kit/test/net_test.js @@ -1,6 +1,4 @@ const net = require(__dirname + "/../src/net") - - net.killport(6667); diff --git a/kit/~/.kit/logs/kit.2024-09-02.log b/kit/~/.kit/logs/kit.2024-09-02.log new file mode 100644 index 0000000..f05665e --- /dev/null +++ b/kit/~/.kit/logs/kit.2024-09-02.log @@ -0,0 +1,7 @@ +[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