deepwork-node-watchdog 1.0.0 → 1.0.1
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/helpers/blacklist.helper.js +1 -3
- package/dist/helpers/command.helper.d.ts +1 -1
- package/dist/helpers/command.helper.js +3 -3
- package/dist/index.js +3 -6
- package/dist/libs/args.lib.d.ts +14 -0
- package/dist/libs/args.lib.js +31 -0
- package/dist/main.js +62 -12
- package/dist/utils/log.util.d.ts +1 -1
- package/dist/utils/log.util.js +1 -43
- package/dist/watchdog.d.ts +1 -2
- package/dist/watchdog.js +4 -4
- package/dist/watchdog.types.d.ts +0 -1
- package/package.json +5 -1
|
@@ -35,7 +35,6 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
exports.loadBlacklist = loadBlacklist;
|
|
37
37
|
const fs = __importStar(require("fs"));
|
|
38
|
-
const normalize_util_1 = require("../utils/normalize.util");
|
|
39
38
|
/**
|
|
40
39
|
* Read the content file at the given path.
|
|
41
40
|
*
|
|
@@ -55,8 +54,7 @@ function loadBlacklist(filePath) {
|
|
|
55
54
|
.readFileSync(filePath, "utf8")
|
|
56
55
|
.split("\n")
|
|
57
56
|
.map((l) => l.trim())
|
|
58
|
-
.filter((l) => l && !l.startsWith("#"))
|
|
59
|
-
.map(normalize_util_1.normalizeProcessName);
|
|
57
|
+
.filter((l) => l && !l.startsWith("#"));
|
|
60
58
|
}
|
|
61
59
|
catch {
|
|
62
60
|
return [];
|
|
@@ -41,7 +41,7 @@ const os = __importStar(require("os"));
|
|
|
41
41
|
const log_util_1 = require("../utils/log.util");
|
|
42
42
|
const normalize_util_1 = require("../utils/normalize.util");
|
|
43
43
|
const execAsync = (0, util_1.promisify)(child_process_1.exec);
|
|
44
|
-
async function killProcess(pid, name
|
|
44
|
+
async function killProcess(pid, name) {
|
|
45
45
|
try {
|
|
46
46
|
if (os.platform() === "win32") {
|
|
47
47
|
await execAsync(`taskkill /F /PID ${pid}`, { timeout: 5000 });
|
|
@@ -49,10 +49,10 @@ async function killProcess(pid, name, logFile) {
|
|
|
49
49
|
else {
|
|
50
50
|
await execAsync(`kill -9 ${pid}`, { timeout: 5000 });
|
|
51
51
|
}
|
|
52
|
-
(0, log_util_1.log)(`Killed process: ${name} (PID: ${pid})
|
|
52
|
+
(0, log_util_1.log)(`Killed process: ${name} (PID: ${pid})`);
|
|
53
53
|
}
|
|
54
54
|
catch (err) {
|
|
55
|
-
(0, log_util_1.log)(`Failed to kill ${name} (PID: ${pid}): ${err}
|
|
55
|
+
(0, log_util_1.log)(`Failed to kill ${name} (PID: ${pid}): ${err}`);
|
|
56
56
|
}
|
|
57
57
|
}
|
|
58
58
|
async function listProcesses() {
|
package/dist/index.js
CHANGED
|
@@ -15,11 +15,8 @@ let activeOptions = null;
|
|
|
15
15
|
async function loop() {
|
|
16
16
|
if (!running || !activeOptions)
|
|
17
17
|
return;
|
|
18
|
-
const { blacklistPath, blacklist, intervalMs = 3000
|
|
19
|
-
await watchdog_1.default.scan(blacklistPath ?? blacklist
|
|
20
|
-
INTERVAL_MS: intervalMs,
|
|
21
|
-
LOG_FILE: logFile,
|
|
22
|
-
});
|
|
18
|
+
const { blacklistPath, blacklist, intervalMs = 3000 } = activeOptions;
|
|
19
|
+
await watchdog_1.default.scan(blacklistPath ?? blacklist);
|
|
23
20
|
if (running) {
|
|
24
21
|
timer = setTimeout(loop, intervalMs);
|
|
25
22
|
}
|
|
@@ -31,7 +28,7 @@ function start(options) {
|
|
|
31
28
|
activeOptions = options;
|
|
32
29
|
if (!options.blacklistPath &&
|
|
33
30
|
(!options.blacklist || options.blacklist.length === 0)) {
|
|
34
|
-
(0, log_util_1.log)("No blacklist specified. Use options.blacklistPath or options.blacklist to set it."
|
|
31
|
+
(0, log_util_1.log)("No blacklist specified. Use options.blacklistPath or options.blacklist to set it.");
|
|
35
32
|
return;
|
|
36
33
|
}
|
|
37
34
|
loop();
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
type ArgType = "string" | "boolean" | "number";
|
|
2
|
+
interface ArgDef {
|
|
3
|
+
flag: string;
|
|
4
|
+
type: ArgType;
|
|
5
|
+
required?: boolean;
|
|
6
|
+
description?: string;
|
|
7
|
+
default?: string | boolean | number;
|
|
8
|
+
}
|
|
9
|
+
type InferType<T extends ArgType> = T extends "string" ? string : T extends "boolean" ? boolean : number;
|
|
10
|
+
type ParsedArgs<T extends ArgDef[]> = {
|
|
11
|
+
[K in T[number] as K["flag"] extends `--${infer Name}` ? Name : never]: K["required"] extends true ? InferType<K["type"]> : InferType<K["type"]> | null;
|
|
12
|
+
};
|
|
13
|
+
export declare function parseArgs<const T extends ArgDef[]>(defs: T): ParsedArgs<T>;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseArgs = parseArgs;
|
|
4
|
+
function parseArgs(defs) {
|
|
5
|
+
const raw = process.argv.slice(2);
|
|
6
|
+
const result = {};
|
|
7
|
+
for (const def of defs) {
|
|
8
|
+
const name = def.flag.replace(/^--/, "");
|
|
9
|
+
const idx = raw.indexOf(def.flag);
|
|
10
|
+
if (def.type === "boolean") {
|
|
11
|
+
result[name] = idx !== -1;
|
|
12
|
+
}
|
|
13
|
+
else if (idx !== -1) {
|
|
14
|
+
const val = raw[idx + 1];
|
|
15
|
+
if (val === undefined || val.startsWith("--")) {
|
|
16
|
+
throw new Error(`Missing value for ${def.flag}`);
|
|
17
|
+
}
|
|
18
|
+
result[name] = def.type === "number" ? Number(val) : val;
|
|
19
|
+
}
|
|
20
|
+
else if (def.default !== undefined) {
|
|
21
|
+
result[name] = def.default;
|
|
22
|
+
}
|
|
23
|
+
else if (def.required) {
|
|
24
|
+
throw new Error(`Required argument ${def.flag} is missing`);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
result[name] = null;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return result;
|
|
31
|
+
}
|
package/dist/main.js
CHANGED
|
@@ -5,31 +5,74 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
};
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
const log_util_1 = require("./utils/log.util");
|
|
8
|
-
const
|
|
8
|
+
const args_lib_1 = require("./libs/args.lib");
|
|
9
9
|
const watchdog_1 = __importDefault(require("./watchdog"));
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
|
|
10
|
+
const path_1 = __importDefault(require("path"));
|
|
11
|
+
// Default configuration values
|
|
12
|
+
const DEFAULT_INTERVAL_MS = 3000;
|
|
13
|
+
// Define and parse command-line arguments
|
|
14
|
+
const args = (0, args_lib_1.parseArgs)([
|
|
15
|
+
{
|
|
16
|
+
flag: "--blacklist",
|
|
17
|
+
type: "string",
|
|
18
|
+
description: "Absolute path to blacklist file",
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
flag: "--list",
|
|
22
|
+
type: "boolean",
|
|
23
|
+
description: "List current running processes",
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
flag: "--version",
|
|
27
|
+
type: "boolean",
|
|
28
|
+
description: "Show the version of the watchdog",
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
flag: "--interval",
|
|
32
|
+
type: "number",
|
|
33
|
+
description: "Interval in milliseconds for scanning processes",
|
|
34
|
+
default: DEFAULT_INTERVAL_MS,
|
|
35
|
+
},
|
|
36
|
+
]);
|
|
14
37
|
(0, log_util_1.log)("Process Watchdog started" +
|
|
15
|
-
(
|
|
38
|
+
(args.blacklist ? ` with blacklist file: ${args.blacklist}` : ""));
|
|
16
39
|
let running = true;
|
|
17
40
|
let timer;
|
|
41
|
+
function validateArgsRequirementsToScan() {
|
|
42
|
+
if (!args.blacklist) {
|
|
43
|
+
(0, log_util_1.log)("No blacklist file specified. Use --blacklist <absolutePath> to set it.");
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
if (args.interval <= 0) {
|
|
47
|
+
(0, log_util_1.log)("Interval must be a positive number.");
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
18
52
|
async function loop() {
|
|
19
53
|
if (!running)
|
|
20
54
|
return;
|
|
21
|
-
|
|
22
|
-
if (!BLACKLIST_FILE) {
|
|
23
|
-
(0, log_util_1.log)("No blacklist file specified. Use --blacklist <absolutePath> to set it.", config.LOG_FILE);
|
|
55
|
+
if (!validateArgsRequirementsToScan()) {
|
|
24
56
|
return;
|
|
25
57
|
}
|
|
26
58
|
console.log("Scanning processes...");
|
|
27
|
-
await watchdog_1.default.scan(
|
|
59
|
+
await watchdog_1.default.scan(args.blacklist);
|
|
28
60
|
if (running) {
|
|
29
|
-
timer = setTimeout(loop,
|
|
61
|
+
timer = setTimeout(loop, args.interval);
|
|
30
62
|
}
|
|
31
63
|
}
|
|
32
|
-
if (
|
|
64
|
+
if (args.version) {
|
|
65
|
+
let pkg;
|
|
66
|
+
try {
|
|
67
|
+
pkg = require(path_1.default.resolve(__dirname, "../package.json"));
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
pkg = require(path_1.default.resolve(__dirname, "package.json"));
|
|
71
|
+
}
|
|
72
|
+
console.log(`Process Watchdog version: ${pkg.version}`);
|
|
73
|
+
process.exit(0);
|
|
74
|
+
}
|
|
75
|
+
else if (args.list) {
|
|
33
76
|
(async () => {
|
|
34
77
|
const processes = await watchdog_1.default.listProcesses();
|
|
35
78
|
console.log("Current running processes:");
|
|
@@ -38,6 +81,13 @@ if (listIndex !== -1) {
|
|
|
38
81
|
})();
|
|
39
82
|
}
|
|
40
83
|
else {
|
|
84
|
+
const blacklistAbsPath = path_1.default.isAbsolute(args.blacklist || "")
|
|
85
|
+
? args.blacklist
|
|
86
|
+
: path_1.default.resolve(process.cwd(), args.blacklist || "");
|
|
87
|
+
args.blacklist = blacklistAbsPath;
|
|
88
|
+
console.log(`Using blacklist file: ${args.blacklist}`);
|
|
89
|
+
console.log(`Starting process scan loop with interval: ${args.interval}ms`);
|
|
90
|
+
console.log("Press Ctrl+C to stop the watchdog.");
|
|
41
91
|
loop();
|
|
42
92
|
}
|
|
43
93
|
process.on("SIGINT", () => {
|
package/dist/utils/log.util.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function log(message: string
|
|
1
|
+
export declare function log(message: string): void;
|
package/dist/utils/log.util.js
CHANGED
|
@@ -1,50 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
3
|
exports.log = log;
|
|
37
|
-
|
|
38
|
-
function log(message, logFile) {
|
|
4
|
+
function log(message) {
|
|
39
5
|
const timestamp = new Date().toISOString();
|
|
40
6
|
const msg = `[${timestamp}] ${message}`;
|
|
41
7
|
console.log(msg);
|
|
42
|
-
if (logFile) {
|
|
43
|
-
try {
|
|
44
|
-
fs.appendFileSync(logFile, msg + "\n");
|
|
45
|
-
}
|
|
46
|
-
catch {
|
|
47
|
-
/* ignore */
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
8
|
}
|
package/dist/watchdog.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { Config } from "./watchdog.types";
|
|
2
1
|
import { listProcesses } from "./helpers/command.helper";
|
|
3
|
-
declare function scan(blacklistPathOrList: string | string[]
|
|
2
|
+
declare function scan(blacklistPathOrList: string | string[]): Promise<void>;
|
|
4
3
|
declare const _default: {
|
|
5
4
|
scan: typeof scan;
|
|
6
5
|
listProcesses: typeof listProcesses;
|
package/dist/watchdog.js
CHANGED
|
@@ -3,8 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const blacklist_helper_1 = require("./helpers/blacklist.helper");
|
|
4
4
|
const command_helper_1 = require("./helpers/command.helper");
|
|
5
5
|
const normalize_util_1 = require("./utils/normalize.util");
|
|
6
|
-
async function scan(blacklistPathOrList
|
|
7
|
-
const { LOG_FILE } = config;
|
|
6
|
+
async function scan(blacklistPathOrList) {
|
|
8
7
|
// Load blacklist
|
|
9
8
|
const blacklist = typeof blacklistPathOrList === "string"
|
|
10
9
|
? (0, blacklist_helper_1.loadBlacklist)(blacklistPathOrList)
|
|
@@ -17,8 +16,9 @@ async function scan(blacklistPathOrList, config) {
|
|
|
17
16
|
for (const proc of processes) {
|
|
18
17
|
const normalizedName = (0, normalize_util_1.normalizeProcessName)(proc.name);
|
|
19
18
|
for (const black of blacklist) {
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
const normalizedBlack = (0, normalize_util_1.normalizeProcessName)(black);
|
|
20
|
+
if (normalizedName.indexOf(normalizedBlack) !== -1) {
|
|
21
|
+
await (0, command_helper_1.killProcess)(proc.pid, proc.name);
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
}
|
package/dist/watchdog.types.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "deepwork-node-watchdog",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "A Node.js watchdog for DeepWork.",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"author": "Kevin Javier Reyes",
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
"start": "ts-node main.ts --blacklist blacklist.txt",
|
|
17
17
|
"dev": "ts-node-dev --respawn --transpile-only --ignore-watch node_modules main.ts --blacklist blacklist.txt",
|
|
18
18
|
"list": "ts-node main.ts --list",
|
|
19
|
+
"version": "ts-node main.ts --version",
|
|
19
20
|
"build": "tsc"
|
|
20
21
|
},
|
|
21
22
|
"devDependencies": {
|
|
@@ -23,5 +24,8 @@
|
|
|
23
24
|
"ts-node": "^10.9.2",
|
|
24
25
|
"ts-node-dev": "^2.0.0",
|
|
25
26
|
"typescript": "^5.4.0"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"deepwork-node-watchdog": "1.0.0"
|
|
26
30
|
}
|
|
27
31
|
}
|