CH-J Server Managerspráva serverů přes SSH
Menu
Vydané zdroje

CH-J Server Manager

Procházejte adresáře a soubory konkrétního vydání aplikace.

Stáhnout zdroje ZIP
CH-J Proprietary Software License 1.14

Zdroje jsou zveřejněny pod CH-J Proprietary Software License 1.14. Jejich dostupnost nemění licenční podmínky ani neposkytuje další oprávnění.

1,6 KB · 53 řádkůStáhnout soubor
1"use strict";
3const fs = require("node:fs");
5const DEFAULT_LIMIT = 256 * 1024;
6const MAX_LIMIT = 1024 * 1024;
8function normalizeLimit(value) {
9 const parsed = Number(value);
10 if (!Number.isSafeInteger(parsed) || parsed <= 0) return DEFAULT_LIMIT;
11 return Math.min(parsed, MAX_LIMIT);
14class LogService {
15 constructor(logPath) {
16 this.logPath = String(logPath || "");
17 if (!this.logPath) throw new Error("Log path is required.");
18 }
20 async readTail(payload = {}) {
21 const maxBytes = normalizeLimit(payload.maxBytes);
22 let handle;
23 try {
24 const stat = await fs.promises.stat(this.logPath);
25 if (!stat.isFile()) throw new Error("Application log is not a file.");
26 const length = Math.min(stat.size, maxBytes);
27 const offset = Math.max(0, stat.size - length);
28 const buffer = Buffer.alloc(length);
29 handle = await fs.promises.open(this.logPath, "r");
30 const { bytesRead } = await handle.read(buffer, 0, length, offset);
31 let text = buffer.subarray(0, bytesRead).toString("utf8");
32 if (offset > 0) {
33 const firstNewline = text.indexOf("\n");
34 text = firstNewline >= 0 ? text.slice(firstNewline + 1) : "";
35 }
36 return {
37 text,
38 truncated: offset > 0,
39 size: stat.size,
40 readAt: new Date().toISOString()
41 };
42 } catch (error) {
43 if (error?.code === "ENOENT") {
44 return { text: "", truncated: false, size: 0, readAt: new Date().toISOString() };
45 }
46 throw error;
47 } finally {
48 await handle?.close().catch(() => {});
49 }
50 }
53module.exports = { DEFAULT_LIMIT, LogService, MAX_LIMIT, normalizeLimit };

SHA-256: 36887b34740b6272535cb482c08ce23086fc1e30716612d11ddb1be66bb1dc02

SHA-256 archivu: 5ac91caf4fa32a6fdb114f2430deed486fbe7489d5eea343d1f034169fafb5e0