@futdevpro/nts-dynamo 1.15.19 → 1.15.21

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.
Files changed (50) hide show
  1. package/_specifications/BACKLOG.md +4 -4
  2. package/build/_modules/admin-auth/_models/admin-api-key-config.interface.d.ts +32 -0
  3. package/build/_modules/admin-auth/_models/admin-api-key-config.interface.d.ts.map +1 -0
  4. package/build/_modules/admin-auth/_models/admin-api-key-config.interface.js +3 -0
  5. package/build/_modules/admin-auth/_models/admin-api-key-config.interface.js.map +1 -0
  6. package/build/_modules/admin-auth/admin-api-key.auth-service.d.ts +90 -0
  7. package/build/_modules/admin-auth/admin-api-key.auth-service.d.ts.map +1 -0
  8. package/build/_modules/admin-auth/admin-api-key.auth-service.js +195 -0
  9. package/build/_modules/admin-auth/admin-api-key.auth-service.js.map +1 -0
  10. package/build/_modules/admin-auth/index.d.ts +3 -0
  11. package/build/_modules/admin-auth/index.d.ts.map +1 -0
  12. package/build/_modules/admin-auth/index.js +6 -0
  13. package/build/_modules/admin-auth/index.js.map +1 -0
  14. package/build/_modules/logs/_models/file-log-entry.interface.d.ts +14 -0
  15. package/build/_modules/logs/_models/file-log-entry.interface.d.ts.map +1 -0
  16. package/build/_modules/logs/_models/file-log-entry.interface.js +3 -0
  17. package/build/_modules/logs/_models/file-log-entry.interface.js.map +1 -0
  18. package/build/_modules/logs/_models/file-log-read-result.interface.d.ts +36 -0
  19. package/build/_modules/logs/_models/file-log-read-result.interface.d.ts.map +1 -0
  20. package/build/_modules/logs/_models/file-log-read-result.interface.js +3 -0
  21. package/build/_modules/logs/_models/file-log-read-result.interface.js.map +1 -0
  22. package/build/_modules/logs/file-log.service.d.ts +46 -0
  23. package/build/_modules/logs/file-log.service.d.ts.map +1 -1
  24. package/build/_modules/logs/file-log.service.js +178 -0
  25. package/build/_modules/logs/file-log.service.js.map +1 -1
  26. package/build/_modules/logs/file-logs.controller.d.ts +41 -0
  27. package/build/_modules/logs/file-logs.controller.d.ts.map +1 -0
  28. package/build/_modules/logs/file-logs.controller.js +139 -0
  29. package/build/_modules/logs/file-logs.controller.js.map +1 -0
  30. package/build/_modules/logs/get-file-logs-routing-module.util.d.ts +32 -0
  31. package/build/_modules/logs/get-file-logs-routing-module.util.d.ts.map +1 -0
  32. package/build/_modules/logs/get-file-logs-routing-module.util.js +38 -0
  33. package/build/_modules/logs/get-file-logs-routing-module.util.js.map +1 -0
  34. package/build/_modules/logs/index.d.ts +4 -0
  35. package/build/_modules/logs/index.d.ts.map +1 -1
  36. package/build/_modules/logs/index.js +5 -1
  37. package/build/_modules/logs/index.js.map +1 -1
  38. package/package.json +1 -1
  39. package/src/_modules/admin-auth/_models/admin-api-key-config.interface.ts +33 -0
  40. package/src/_modules/admin-auth/admin-api-key.auth-service.spec.ts +200 -0
  41. package/src/_modules/admin-auth/admin-api-key.auth-service.ts +220 -0
  42. package/src/_modules/admin-auth/index.ts +2 -0
  43. package/src/_modules/logs/_models/file-log-entry.interface.ts +13 -0
  44. package/src/_modules/logs/_models/file-log-read-result.interface.ts +37 -0
  45. package/src/_modules/logs/file-log.service.spec.ts +139 -0
  46. package/src/_modules/logs/file-log.service.ts +183 -0
  47. package/src/_modules/logs/file-logs.controller.spec.ts +245 -0
  48. package/src/_modules/logs/file-logs.controller.ts +165 -0
  49. package/src/_modules/logs/get-file-logs-routing-module.util.ts +51 -0
  50. package/src/_modules/logs/index.ts +7 -0
@@ -3,6 +3,8 @@ import * as path from 'path';
3
3
 
4
4
  import { DyNTS_SingletonServiceBase } from '../../_services/base/singleton.service-base';
5
5
  import { DyNTS_global_settings } from '../../_collections/global-settings.const';
6
+ import { DyNTS_FileLog_Entry } from './_models/file-log-entry.interface';
7
+ import { DyNTS_FileLog_ReadOptions, DyNTS_FileLog_ReadResult } from './_models/file-log-read-result.interface';
6
8
 
7
9
 
8
10
  /**
@@ -22,6 +24,12 @@ const DEFAULT_INCLUDE_STDERR: boolean = true;
22
24
  /** ANSI escape code regex (szin/formazas kodok). */
23
25
  const ANSI_ESCAPE_REGEX: RegExp = /\x1b\[[0-9;]*[a-zA-Z]/g;
24
26
 
27
+ /** Max sor szam egy `readLogFile()` hivasra — vedi a memory-t nagy fajloknal. */
28
+ const MAX_READ_LINES: number = 10000;
29
+
30
+ /** Default tail sor szam (ha semmi mas mod nincs megadva). */
31
+ const DEFAULT_READ_TAIL: number = 200;
32
+
25
33
 
26
34
  /**
27
35
  * File-based szerver log service — duplikalja a stdout/stderr-t per-session
@@ -149,6 +157,181 @@ export class DyNTS_FileLog_Service extends DyNTS_SingletonServiceBase {
149
157
  return this.currentLogPath;
150
158
  }
151
159
 
160
+ /**
161
+ * Az aktualis aktiv log fajl basename-je (NEM teljes path). Csak akkor
162
+ * nem-ures, ha az install() sikeresen lefutott.
163
+ */
164
+ getCurrentLogFilename(): string {
165
+ if (!this.currentLogPath) { return ''; }
166
+ return path.basename(this.currentLogPath);
167
+ }
168
+
169
+ /**
170
+ * Aktiv log dir abszolut path-ja. Csak akkor nem-ures, ha az install()
171
+ * sikeresen lefutott.
172
+ */
173
+ getActiveLogDir(): string {
174
+ return this.activeLogDir;
175
+ }
176
+
177
+ /**
178
+ * Aktiv filename prefix (a service config-jabol). Hasznalja a controller
179
+ * a request-fajlnev whitelist regex epitesehez.
180
+ */
181
+ getFilenamePrefix(): string {
182
+ return this.filenamePrefix;
183
+ }
184
+
185
+ /**
186
+ * Osszes log fajl listazasa az aktiv log dir-bol. Csak a service prefix-evel
187
+ * matchelo fajlokat veszi (`{prefix}*.log`).
188
+ *
189
+ * Rendezes: mtime DESC (legujabb elol). Hibakat csendben elnyel — ha a dir
190
+ * nem letezik vagy nem olvashato, ures array-t ad vissza.
191
+ */
192
+ listLogFiles(): DyNTS_FileLog_Entry[] {
193
+ if (!this.activeLogDir || !this.installed) { return []; }
194
+ const currentBasename: string = this.getCurrentLogFilename();
195
+ const entries: DyNTS_FileLog_Entry[] = [];
196
+
197
+ try {
198
+ const dirContents: string[] = fs.readdirSync(this.activeLogDir);
199
+ for (const name of dirContents) {
200
+ if (!name.startsWith(this.filenamePrefix) || !name.endsWith(DEFAULT_FILENAME_SUFFIX)) { continue; }
201
+ const fullPath: string = path.join(this.activeLogDir, name);
202
+ try {
203
+ const stats: fs.Stats = fs.statSync(fullPath);
204
+ if (!stats.isFile()) { continue; }
205
+ entries.push({
206
+ name: name,
207
+ sizeBytes: stats.size,
208
+ mtimeMs: stats.mtimeMs,
209
+ isCurrent: name === currentBasename,
210
+ });
211
+ } catch {
212
+ // egy fajlonkenti hiba ne szakitsa meg a listat
213
+ }
214
+ }
215
+ } catch {
216
+ // dir read hiba → ures listat adunk vissza
217
+ return [];
218
+ }
219
+
220
+ // Legujabb elol
221
+ entries.sort((a: DyNTS_FileLog_Entry, b: DyNTS_FileLog_Entry): number => b.mtimeMs - a.mtimeMs);
222
+ return entries;
223
+ }
224
+
225
+ /**
226
+ * Egy konkret log fajl olvasasa, line-based mode-ban (tail / head / range).
227
+ *
228
+ * **Biztonsagi safeguards:**
229
+ * - Filename whitelist: `{prefix}*.log` mintara matchelo nev kotelezo
230
+ * - Path resolution `path.resolve(activeLogDir, name)` + `startsWith(activeLogDir)` (path traversal vedelem)
231
+ * - Sor szam cap: `MAX_READ_LINES` (10000)
232
+ *
233
+ * **Hibak:**
234
+ * - Throw `Error('not installed')` ha a service nincs install-olva
235
+ * - Throw `Error('invalid filename')` ha a name nem matchel a whitelist-re VAGY kilep az activeLogDir-bol
236
+ * - Throw `Error('file not found')` ha a fajl nem letezik
237
+ *
238
+ * A hivot kell vigyaznia hogy ezeket HTTP status code-okra forditja.
239
+ */
240
+ readLogFile(name: string, options: DyNTS_FileLog_ReadOptions = {}): DyNTS_FileLog_ReadResult {
241
+ if (!this.installed || !this.activeLogDir) {
242
+ throw new Error('FileLog service not installed');
243
+ }
244
+
245
+ // Whitelist regex: csak a service prefix-evel matchelo fajlnev
246
+ if (!this.isValidFilename(name)) {
247
+ throw new Error('invalid filename');
248
+ }
249
+
250
+ // Path resolve + traversal check
251
+ const resolvedPath: string = path.resolve(this.activeLogDir, name);
252
+ const resolvedDir: string = path.resolve(this.activeLogDir);
253
+ // path.sep + sufix ellenőrzéssel megelőzzük a hasonló prefixű dir átverést
254
+ // (pl. '/logs' vs '/logs-other'); a resolvedPath az activeLogDir-en BELUL
255
+ // kell legyen.
256
+ if (!resolvedPath.startsWith(resolvedDir + path.sep) && resolvedPath !== resolvedDir) {
257
+ throw new Error('invalid filename');
258
+ }
259
+
260
+ if (!fs.existsSync(resolvedPath)) {
261
+ throw new Error('file not found');
262
+ }
263
+
264
+ const stats: fs.Stats = fs.statSync(resolvedPath);
265
+ if (!stats.isFile()) {
266
+ throw new Error('file not found');
267
+ }
268
+
269
+ // Olvass + split soroka. UTF-8 encoding alapertelmezett. Nagy fajloknal a
270
+ // teljes betoltes nem ideal, de a 10000-soros cap es a Stream-based
271
+ // optimization egy kesobbi iteracio lehet ha kell.
272
+ const fullContent: string = fs.readFileSync(resolvedPath, 'utf-8');
273
+ // Split a sorvegekre — utolso ures stringet kivagjuk ha a fajl
274
+ // sorvégzodessel zarult (regenerált sor szám hibakerulest celozzuk meg)
275
+ const allLines: string[] = fullContent.split(/\r?\n/);
276
+ if (allLines.length > 0 && allLines[allLines.length - 1] === '') {
277
+ allLines.pop();
278
+ }
279
+ const totalLines: number = allLines.length;
280
+
281
+ // Mod meghatarozasa: range > head > tail
282
+ let mode: 'tail' | 'head' | 'range';
283
+ let startIdx: number;
284
+ let endIdx: number; // exclusive
285
+
286
+ if (options.rangeStart !== undefined && options.rangeEnd !== undefined) {
287
+ mode = 'range';
288
+ const start1: number = Math.max(1, Math.floor(options.rangeStart));
289
+ const end1: number = Math.max(start1, Math.floor(options.rangeEnd));
290
+ startIdx = Math.max(0, Math.min(totalLines, start1 - 1));
291
+ endIdx = Math.max(startIdx, Math.min(totalLines, end1));
292
+ // Cap
293
+ if (endIdx - startIdx > MAX_READ_LINES) {
294
+ endIdx = startIdx + MAX_READ_LINES;
295
+ }
296
+ } else if (options.head !== undefined) {
297
+ mode = 'head';
298
+ const headN: number = Math.min(Math.max(1, Math.floor(options.head)), MAX_READ_LINES);
299
+ startIdx = 0;
300
+ endIdx = Math.min(totalLines, headN);
301
+ } else {
302
+ mode = 'tail';
303
+ const tailN: number = Math.min(Math.max(1, Math.floor(options.tail ?? DEFAULT_READ_TAIL)), MAX_READ_LINES);
304
+ startIdx = Math.max(0, totalLines - tailN);
305
+ endIdx = totalLines;
306
+ }
307
+
308
+ const lines: string[] = allLines.slice(startIdx, endIdx);
309
+
310
+ return {
311
+ name: path.basename(resolvedPath),
312
+ sizeBytes: stats.size,
313
+ totalLines: totalLines,
314
+ lines: lines,
315
+ mode: mode,
316
+ rangeStart: startIdx + 1, // 1-based, inclusive
317
+ rangeEnd: Math.max(startIdx, endIdx), // 1-based, inclusive ha lines van; egyebkent 0-szeru
318
+ };
319
+ }
320
+
321
+ /**
322
+ * Filename whitelist ellenorzes. Csak a service prefix-evel matchelo,
323
+ * biztonsagos karakterkeszletu fajlnev megengedett.
324
+ */
325
+ private isValidFilename(name: string): boolean {
326
+ if (typeof name !== 'string' || name.length === 0 || name.length > 256) { return false; }
327
+ // Path-szeparatorokat kifejezetten tiltjuk (mar a regex is, de gyors check)
328
+ if (name.includes('/') || name.includes('\\') || name.includes('..')) { return false; }
329
+ // `{prefix}[\w\-.]+\.log` — szigoru ASCII set
330
+ const escapedPrefix: string = this.filenamePrefix.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
331
+ const regex: RegExp = new RegExp(`^${escapedPrefix}[\\w\\-.]+\\.log$`);
332
+ return regex.test(name);
333
+ }
334
+
152
335
  /**
153
336
  * Telepitve van-e (csak akkor true, ha az enabled === true es a setup nem bukott).
154
337
  */
@@ -0,0 +1,245 @@
1
+ import * as fs from 'fs';
2
+ import * as os from 'os';
3
+ import * as path from 'path';
4
+
5
+ import { DyFM_HttpCallType } from '@futdevpro/fsm-dynamo';
6
+
7
+ import { DyNTS_global_settings } from '../../_collections/global-settings.const';
8
+
9
+ import { DyNTS_FileLog_Service } from './file-log.service';
10
+ import { DyNTS_FileLogs_Controller } from './file-logs.controller';
11
+
12
+
13
+ /** Minimal Request mock builder. */
14
+ const mockReq = (args: { params?: Record<string, string>; query?: Record<string, any> } = {}): any => ({
15
+ params: args.params ?? {},
16
+ query: args.query ?? {},
17
+ headers: {},
18
+ });
19
+
20
+ /** Minimal Response mock that records status + send payload. */
21
+ interface MockRes {
22
+ statusCode: number;
23
+ payload: any;
24
+ status: (code: number) => MockRes;
25
+ send: (data: any) => MockRes;
26
+ }
27
+ const mockRes = (): MockRes => {
28
+ const res: MockRes = {
29
+ statusCode: 200,
30
+ payload: undefined,
31
+ status(code: number): MockRes { res.statusCode = code; return res; },
32
+ send(data: any): MockRes { res.payload = data; return res; },
33
+ };
34
+ return res;
35
+ };
36
+
37
+
38
+ /** Extract a task function by endpoint name. */
39
+ const taskByName = (controller: DyNTS_FileLogs_Controller, name: string): ((req: any, res: any) => Promise<void>) => {
40
+ const ep: any = controller.endpoints.find((e: any) => e.name === name);
41
+ if (!ep) { throw new Error(`endpoint ${name} not found`); }
42
+ return ep.tasks[0];
43
+ };
44
+
45
+
46
+ describe('| DyNTS_FileLogs_Controller', (): void => {
47
+ let tempDir: string;
48
+ let originalFileLogConfig: typeof DyNTS_global_settings.log_settings.file_log;
49
+
50
+ beforeEach((): void => {
51
+ tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'nts-filelogs-ctrl-'));
52
+ originalFileLogConfig = DyNTS_global_settings.log_settings.file_log;
53
+ });
54
+
55
+ afterEach((): void => {
56
+ DyNTS_FileLog_Service.getInstance()._teardownForTesting();
57
+ DyNTS_FileLogs_Controller.configure({});
58
+ DyNTS_global_settings.log_settings.file_log = originalFileLogConfig;
59
+ try { fs.rmSync(tempDir, { recursive: true, force: true }); } catch { /* swallow */ }
60
+ });
61
+
62
+
63
+ describe('| singleton + endpoint shape', (): void => {
64
+ it('| getInstance() singleton', (): void => {
65
+ const a = DyNTS_FileLogs_Controller.getInstance();
66
+ const b = DyNTS_FileLogs_Controller.getInstance();
67
+ expect(a).toBe(b);
68
+ });
69
+
70
+ it('| setupEndpoints letrehozza /list + /file/:filename endpointokat', (): void => {
71
+ const ctrl = DyNTS_FileLogs_Controller.getInstance();
72
+ ctrl.setupEndpoints();
73
+
74
+ const list = ctrl.endpoints.find((e: any) => e.name === 'listFileLogs');
75
+ const read = ctrl.endpoints.find((e: any) => e.name === 'readFileLog');
76
+ expect(list).toBeDefined();
77
+ expect(list?.type).toBe(DyFM_HttpCallType.get);
78
+ expect(read).toBeDefined();
79
+ expect(read?.type).toBe(DyFM_HttpCallType.get);
80
+ });
81
+
82
+ it('| authPreProcess hozzaadodik preProcesses-be ha config-olva', (): void => {
83
+ const myAuth = async (): Promise<void> => { /* noop */ };
84
+ DyNTS_FileLogs_Controller.configure({ authPreProcess: myAuth });
85
+ const ctrl = DyNTS_FileLogs_Controller.getInstance();
86
+ ctrl.setupEndpoints();
87
+ const list: any = ctrl.endpoints.find((e: any) => e.name === 'listFileLogs');
88
+ expect(list).toBeDefined();
89
+ // A preProcesses property private, de cast-tal ellenorizheto
90
+ const pre: any = (list as any).preProcesses;
91
+ expect(pre).toBeDefined();
92
+ expect(pre.length).toBe(1);
93
+ expect(pre[0]).toBe(myAuth);
94
+ });
95
+ });
96
+
97
+
98
+ describe('| GET /list', (): void => {
99
+ it('| 503 ha a service nincs installalva', async (): Promise<void> => {
100
+ const ctrl = DyNTS_FileLogs_Controller.getInstance();
101
+ ctrl.setupEndpoints();
102
+ const task = taskByName(ctrl, 'listFileLogs');
103
+
104
+ const res = mockRes();
105
+ await task(mockReq(), res);
106
+ expect(res.statusCode).toBe(503);
107
+ expect(res.payload.error).toContain('not installed');
108
+ });
109
+
110
+ it('| 200 + files lista ha installalva van', async (): Promise<void> => {
111
+ DyNTS_global_settings.log_settings.file_log = { enabled: true, logDir: tempDir, filenamePrefix: 'spec-' };
112
+ DyNTS_FileLog_Service.getInstance().install();
113
+ // Egy extra spec- fajlt kezzel
114
+ fs.writeFileSync(path.join(tempDir, 'spec-extra-2026.log'), 'extra-content');
115
+
116
+ const ctrl = DyNTS_FileLogs_Controller.getInstance();
117
+ ctrl.setupEndpoints();
118
+ const task = taskByName(ctrl, 'listFileLogs');
119
+
120
+ const res = mockRes();
121
+ await task(mockReq(), res);
122
+ expect(res.statusCode).toBe(200);
123
+ expect(res.payload.count).toBeGreaterThanOrEqual(2); // session + extra
124
+ expect(res.payload.totalSizeBytes).toBeGreaterThan(0);
125
+ expect(Array.isArray(res.payload.files)).toBe(true);
126
+ });
127
+ });
128
+
129
+
130
+ describe('| GET /file/:filename', (): void => {
131
+ let testFile: string;
132
+
133
+ beforeEach((): void => {
134
+ DyNTS_global_settings.log_settings.file_log = { enabled: true, logDir: tempDir, filenamePrefix: 'spec-' };
135
+ DyNTS_FileLog_Service.getInstance().install();
136
+ testFile = `spec-read-${Date.now()}.log`;
137
+ const lines: string[] = [];
138
+ for (let i: number = 1; i <= 30; i++) {
139
+ lines.push(`row-${i}`);
140
+ }
141
+ fs.writeFileSync(path.join(tempDir, testFile), lines.join('\n') + '\n');
142
+ });
143
+
144
+ it('| 503 ha a service nincs installalva', async (): Promise<void> => {
145
+ DyNTS_FileLog_Service.getInstance()._teardownForTesting();
146
+ const ctrl = DyNTS_FileLogs_Controller.getInstance();
147
+ ctrl.setupEndpoints();
148
+ const task = taskByName(ctrl, 'readFileLog');
149
+
150
+ const res = mockRes();
151
+ await task(mockReq({ params: { filename: testFile } }), res);
152
+ expect(res.statusCode).toBe(503);
153
+ });
154
+
155
+ it('| 200 + tail mod default', async (): Promise<void> => {
156
+ const ctrl = DyNTS_FileLogs_Controller.getInstance();
157
+ ctrl.setupEndpoints();
158
+ const task = taskByName(ctrl, 'readFileLog');
159
+
160
+ const res = mockRes();
161
+ await task(mockReq({ params: { filename: testFile }, query: { tail: '5' } }), res);
162
+ expect(res.statusCode).toBe(200);
163
+ expect(res.payload.mode).toBe('tail');
164
+ expect(res.payload.lines).toEqual(['row-26', 'row-27', 'row-28', 'row-29', 'row-30']);
165
+ expect(res.payload.totalLines).toBe(30);
166
+ });
167
+
168
+ it('| 200 + head mod', async (): Promise<void> => {
169
+ const ctrl = DyNTS_FileLogs_Controller.getInstance();
170
+ ctrl.setupEndpoints();
171
+ const task = taskByName(ctrl, 'readFileLog');
172
+
173
+ const res = mockRes();
174
+ await task(mockReq({ params: { filename: testFile }, query: { head: '3' } }), res);
175
+ expect(res.statusCode).toBe(200);
176
+ expect(res.payload.mode).toBe('head');
177
+ expect(res.payload.lines).toEqual(['row-1', 'row-2', 'row-3']);
178
+ });
179
+
180
+ it('| 200 + range mod', async (): Promise<void> => {
181
+ const ctrl = DyNTS_FileLogs_Controller.getInstance();
182
+ ctrl.setupEndpoints();
183
+ const task = taskByName(ctrl, 'readFileLog');
184
+
185
+ const res = mockRes();
186
+ await task(mockReq({ params: { filename: testFile }, query: { rangeStart: '5', rangeEnd: '8' } }), res);
187
+ expect(res.statusCode).toBe(200);
188
+ expect(res.payload.mode).toBe('range');
189
+ expect(res.payload.lines).toEqual(['row-5', 'row-6', 'row-7', 'row-8']);
190
+ });
191
+
192
+ it('| 400 invalid tail (negativ)', async (): Promise<void> => {
193
+ const ctrl = DyNTS_FileLogs_Controller.getInstance();
194
+ ctrl.setupEndpoints();
195
+ const task = taskByName(ctrl, 'readFileLog');
196
+
197
+ const res = mockRes();
198
+ await task(mockReq({ params: { filename: testFile }, query: { tail: '-5' } }), res);
199
+ expect(res.statusCode).toBe(400);
200
+ });
201
+
202
+ it('| 400 invalid range (end < start)', async (): Promise<void> => {
203
+ const ctrl = DyNTS_FileLogs_Controller.getInstance();
204
+ ctrl.setupEndpoints();
205
+ const task = taskByName(ctrl, 'readFileLog');
206
+
207
+ const res = mockRes();
208
+ await task(mockReq({ params: { filename: testFile }, query: { rangeStart: '10', rangeEnd: '5' } }), res);
209
+ expect(res.statusCode).toBe(400);
210
+ });
211
+
212
+ it('| 404 invalid filename (prefix nem matchel)', async (): Promise<void> => {
213
+ const ctrl = DyNTS_FileLogs_Controller.getInstance();
214
+ ctrl.setupEndpoints();
215
+ const task = taskByName(ctrl, 'readFileLog');
216
+
217
+ const res = mockRes();
218
+ await task(mockReq({ params: { filename: 'evil-2026.log' } }), res);
219
+ expect(res.statusCode).toBe(404);
220
+ expect(res.payload.error).toBe('invalid filename');
221
+ });
222
+
223
+ it('| 404 path traversal (`../`)', async (): Promise<void> => {
224
+ const ctrl = DyNTS_FileLogs_Controller.getInstance();
225
+ ctrl.setupEndpoints();
226
+ const task = taskByName(ctrl, 'readFileLog');
227
+
228
+ const res = mockRes();
229
+ await task(mockReq({ params: { filename: '../etc/passwd' } }), res);
230
+ expect(res.statusCode).toBe(404);
231
+ expect(res.payload.error).toBe('invalid filename');
232
+ });
233
+
234
+ it('| 404 file nem letezik (de valid prefix)', async (): Promise<void> => {
235
+ const ctrl = DyNTS_FileLogs_Controller.getInstance();
236
+ ctrl.setupEndpoints();
237
+ const task = taskByName(ctrl, 'readFileLog');
238
+
239
+ const res = mockRes();
240
+ await task(mockReq({ params: { filename: 'spec-nonexistent.log' } }), res);
241
+ expect(res.statusCode).toBe(404);
242
+ expect(res.payload.error).toBe('file not found');
243
+ });
244
+ });
245
+ });
@@ -0,0 +1,165 @@
1
+ import { Request, Response } from 'express';
2
+
3
+ import { DyFM_HttpCallType } from '@futdevpro/fsm-dynamo';
4
+
5
+ import { DyNTS_Controller } from '../../_services/route/controller.service';
6
+ import { DyNTS_Endpoint_Params } from '../../_models/control-models/endpoint-params.control-model';
7
+
8
+ import { DyNTS_FileLog_Entry } from './_models/file-log-entry.interface';
9
+ import { DyNTS_FileLog_ReadResult, DyNTS_FileLog_ReadOptions } from './_models/file-log-read-result.interface';
10
+ import { DyNTS_FileLog_Service } from './file-log.service';
11
+
12
+
13
+ /**
14
+ * Config a `DyNTS_FileLogs_Controller`-hez (opcionalis auth pre-process).
15
+ */
16
+ export interface DyNTS_FileLogsController_Config {
17
+ /**
18
+ * Opcionalis auth pre-process — ha megadod, a controller-en levo endpointok
19
+ * vedettek. Tipikusan: `DyNTS_AdminApiKey_AuthService.getInstance().verify`.
20
+ */
21
+ authPreProcess?: (req: Request, res: Response) => Promise<void>;
22
+ }
23
+
24
+
25
+ /**
26
+ * NTS File Logs Controller — endpointok a `DyNTS_FileLog_Service` altal irt
27
+ * log fajl-ok lekeresehez.
28
+ *
29
+ * **Endpoint-ok:**
30
+ * GET /list — log fajlok listazasa (basename, sizeBytes, mtimeMs, isCurrent)
31
+ * GET /file/:filename — egy fajl olvasasa, line-based: ?tail=N | ?head=N | ?rangeStart=A&rangeEnd=B
32
+ *
33
+ * **Mount-olas (host app):**
34
+ * app.use(DyNTS_getFileLogsRoutingModule({
35
+ * authPreProcess: adminAuth.verify, // opcionalis
36
+ * route: '/admin/file-logs', // default '/file-logs'
37
+ * }));
38
+ *
39
+ * **Status code-ok:**
40
+ * 200 — sikeres response (lehet ures lista is)
41
+ * 400 — invalid query parameter (tail/head/range)
42
+ * 401 — auth pre-process throw-ol DyFM_Error(401)
43
+ * 404 — fajl nem letezik vagy nem megfelelo nev (whitelist failure / path traversal kiserlet)
44
+ * 503 — a DyNTS_FileLog_Service nincs install-olva (vagy file_log.enabled=false)
45
+ */
46
+ export class DyNTS_FileLogs_Controller extends DyNTS_Controller {
47
+
48
+ private static config: DyNTS_FileLogsController_Config = {};
49
+
50
+ static configure(config: DyNTS_FileLogsController_Config): void {
51
+ DyNTS_FileLogs_Controller.config = config;
52
+ }
53
+
54
+ static getInstance(): DyNTS_FileLogs_Controller {
55
+ return DyNTS_FileLogs_Controller.getSingletonInstance();
56
+ }
57
+
58
+ private readonly fileLogService: DyNTS_FileLog_Service = DyNTS_FileLog_Service.getInstance();
59
+
60
+ setupEndpoints(): void {
61
+ const preProcesses: ((req: Request, res: Response) => Promise<void>)[] = [];
62
+ if (DyNTS_FileLogs_Controller.config.authPreProcess) {
63
+ preProcesses.push(DyNTS_FileLogs_Controller.config.authPreProcess);
64
+ }
65
+
66
+ this.endpoints = [
67
+ // GET /list — fajllista
68
+ new DyNTS_Endpoint_Params({
69
+ name: 'listFileLogs',
70
+ type: DyFM_HttpCallType.get,
71
+ endpoint: '/list',
72
+ preProcesses: preProcesses,
73
+ logRequest: false,
74
+ tasks: [
75
+ async (req: Request, res: Response): Promise<void> => {
76
+ if (!this.fileLogService.isInstalled()) {
77
+ res.status(503).send({
78
+ error: 'FileLog service is not installed.',
79
+ hint: 'Set DyNTS_global_settings.log_settings.file_log.enabled = true and call DyNTS_FileLog_Service.getInstance().install() at server startup.',
80
+ });
81
+ return;
82
+ }
83
+
84
+ const files: DyNTS_FileLog_Entry[] = this.fileLogService.listLogFiles();
85
+ const totalSizeBytes: number = files.reduce(
86
+ (acc: number, f: DyNTS_FileLog_Entry): number => acc + f.sizeBytes, 0,
87
+ );
88
+
89
+ res.send({
90
+ count: files.length,
91
+ totalSizeBytes: totalSizeBytes,
92
+ files: files,
93
+ });
94
+ },
95
+ ],
96
+ }),
97
+
98
+ // GET /file/:filename — egy fajl olvasasa tail / head / range modban
99
+ new DyNTS_Endpoint_Params({
100
+ name: 'readFileLog',
101
+ type: DyFM_HttpCallType.get,
102
+ endpoint: '/file/:filename',
103
+ preProcesses: preProcesses,
104
+ logRequest: false,
105
+ tasks: [
106
+ async (req: Request, res: Response): Promise<void> => {
107
+ if (!this.fileLogService.isInstalled()) {
108
+ res.status(503).send({
109
+ error: 'FileLog service is not installed.',
110
+ });
111
+ return;
112
+ }
113
+
114
+ const filename: string = req.params.filename;
115
+ const options: DyNTS_FileLog_ReadOptions = {};
116
+
117
+ // Mod prioritas: range > head > tail. Csak a megadott mezo aktivalja az adott modot.
118
+ if (req.query.rangeStart !== undefined && req.query.rangeEnd !== undefined) {
119
+ const start: number = parseInt(req.query.rangeStart as string, 10);
120
+ const end: number = parseInt(req.query.rangeEnd as string, 10);
121
+ if (!Number.isFinite(start) || !Number.isFinite(end) || start < 1 || end < start) {
122
+ res.status(400).send({ error: 'Invalid rangeStart/rangeEnd query parameters' });
123
+ return;
124
+ }
125
+ options.rangeStart = start;
126
+ options.rangeEnd = end;
127
+ } else if (req.query.head !== undefined) {
128
+ const head: number = parseInt(req.query.head as string, 10);
129
+ if (!Number.isFinite(head) || head < 1) {
130
+ res.status(400).send({ error: 'Invalid head query parameter' });
131
+ return;
132
+ }
133
+ options.head = head;
134
+ } else if (req.query.tail !== undefined) {
135
+ const tail: number = parseInt(req.query.tail as string, 10);
136
+ if (!Number.isFinite(tail) || tail < 1) {
137
+ res.status(400).send({ error: 'Invalid tail query parameter' });
138
+ return;
139
+ }
140
+ options.tail = tail;
141
+ }
142
+ // egyik query mezo sincs → service alapertelmezett tail (200)
143
+
144
+ try {
145
+ const result: DyNTS_FileLog_ReadResult = this.fileLogService.readLogFile(filename, options);
146
+ res.send(result);
147
+ } catch (err: unknown) {
148
+ const msg: string = err instanceof Error ? err.message : String(err);
149
+ if (msg === 'FileLog service not installed') {
150
+ res.status(503).send({ error: msg });
151
+ return;
152
+ }
153
+ if (msg === 'invalid filename' || msg === 'file not found') {
154
+ res.status(404).send({ error: msg, filename: filename });
155
+ return;
156
+ }
157
+ // Egyeb hibak (pl. FS read failure) — 500
158
+ res.status(500).send({ error: msg });
159
+ }
160
+ },
161
+ ],
162
+ }),
163
+ ];
164
+ }
165
+ }
@@ -0,0 +1,51 @@
1
+ import { DyNTS_RoutingModule_Settings } from '../../_models/interfaces/routing-module-settings.interface';
2
+ import { DyNTS_RoutingModule } from '../../_services/route/routing-module.service';
3
+
4
+ import { DyNTS_FileLogs_Controller, DyNTS_FileLogsController_Config } from './file-logs.controller';
5
+
6
+
7
+ /**
8
+ * Config a `DyNTS_getFileLogsRoutingModule()`-hoz.
9
+ */
10
+ export interface DyNTS_FileLogsRoutingModule_Config extends DyNTS_FileLogsController_Config {
11
+ /** Route prefix. Default: `/file-logs`. */
12
+ route?: string;
13
+ }
14
+
15
+
16
+ /**
17
+ * Letrehoz egy routing module-t a `DyNTS_FileLogs_Controller`-rel.
18
+ *
19
+ * **Hasznalat (auth nelkul, fejlesztoi env):**
20
+ * ```ts
21
+ * app.use(DyNTS_getFileLogsRoutingModule());
22
+ * ```
23
+ *
24
+ * **Hasznalat (admin API key auth-tal — javasolt produkcios env-re):**
25
+ * ```ts
26
+ * const adminAuth = DyNTS_AdminApiKey_AuthService.getInstance();
27
+ * app.use(DyNTS_getFileLogsRoutingModule({
28
+ * authPreProcess: adminAuth.verify,
29
+ * route: '/admin/file-logs', // opcionalis override
30
+ * }));
31
+ * ```
32
+ *
33
+ * **Elofeltetel:** `DyNTS_FileLog_Service.getInstance().install()` lefutott
34
+ * a szerver startup-on (es a `log_settings.file_log.enabled = true`).
35
+ * Egyebkent a controller endpointjai 503-at adnak.
36
+ */
37
+ export function DyNTS_getFileLogsRoutingModule(
38
+ config?: DyNTS_FileLogsRoutingModule_Config,
39
+ ): DyNTS_RoutingModule {
40
+ // Controller config atadasa (static — getInstance() elott)
41
+ if (config) {
42
+ DyNTS_FileLogs_Controller.configure({ authPreProcess: config.authPreProcess });
43
+ }
44
+
45
+ const settings: DyNTS_RoutingModule_Settings = {
46
+ route: config?.route ?? '/file-logs',
47
+ controllers: [DyNTS_FileLogs_Controller.getInstance()],
48
+ };
49
+
50
+ return new DyNTS_RoutingModule(settings);
51
+ }
@@ -2,3 +2,10 @@ export { DyNTS_Logs_Service } from './logs.service';
2
2
  export { DyNTS_Logs_Controller, DyNTS_LogsController_Config } from './logs.controller';
3
3
  export { DyNTS_getLogsRoutingModule } from './get-logs-routing-module.util';
4
4
  export { DyNTS_FileLog_Service } from './file-log.service';
5
+ export { DyNTS_FileLogs_Controller, DyNTS_FileLogsController_Config } from './file-logs.controller';
6
+ export {
7
+ DyNTS_getFileLogsRoutingModule,
8
+ DyNTS_FileLogsRoutingModule_Config,
9
+ } from './get-file-logs-routing-module.util';
10
+ export { DyNTS_FileLog_Entry } from './_models/file-log-entry.interface';
11
+ export { DyNTS_FileLog_ReadResult, DyNTS_FileLog_ReadOptions } from './_models/file-log-read-result.interface';