@futdevpro/nts-dynamo 1.15.20 → 1.15.23
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/__documentations/2026-05-17-static-client-serving-howto.md +144 -0
- package/_specifications/BACKLOG.md +6 -6
- package/build/_modules/logs/_models/file-log-entry.interface.d.ts +14 -0
- package/build/_modules/logs/_models/file-log-entry.interface.d.ts.map +1 -0
- package/build/_modules/logs/_models/file-log-entry.interface.js +3 -0
- package/build/_modules/logs/_models/file-log-entry.interface.js.map +1 -0
- package/build/_modules/logs/_models/file-log-read-result.interface.d.ts +36 -0
- package/build/_modules/logs/_models/file-log-read-result.interface.d.ts.map +1 -0
- package/build/_modules/logs/_models/file-log-read-result.interface.js +3 -0
- package/build/_modules/logs/_models/file-log-read-result.interface.js.map +1 -0
- package/build/_modules/logs/file-log.service.d.ts +46 -0
- package/build/_modules/logs/file-log.service.d.ts.map +1 -1
- package/build/_modules/logs/file-log.service.js +178 -0
- package/build/_modules/logs/file-log.service.js.map +1 -1
- package/build/_modules/logs/file-logs.controller.d.ts +41 -0
- package/build/_modules/logs/file-logs.controller.d.ts.map +1 -0
- package/build/_modules/logs/file-logs.controller.js +139 -0
- package/build/_modules/logs/file-logs.controller.js.map +1 -0
- package/build/_modules/logs/get-file-logs-routing-module.util.d.ts +32 -0
- package/build/_modules/logs/get-file-logs-routing-module.util.d.ts.map +1 -0
- package/build/_modules/logs/get-file-logs-routing-module.util.js +38 -0
- package/build/_modules/logs/get-file-logs-routing-module.util.js.map +1 -0
- package/build/_modules/logs/index.d.ts +4 -0
- package/build/_modules/logs/index.d.ts.map +1 -1
- package/build/_modules/logs/index.js +5 -1
- package/build/_modules/logs/index.js.map +1 -1
- package/build/_modules/server/errors/errors.controller.d.ts +64 -0
- package/build/_modules/server/errors/errors.controller.d.ts.map +1 -1
- package/build/_modules/server/errors/errors.controller.js +66 -0
- package/build/_modules/server/errors/errors.controller.js.map +1 -1
- package/package.json +1 -1
- package/src/_modules/logs/_models/file-log-entry.interface.ts +13 -0
- package/src/_modules/logs/_models/file-log-read-result.interface.ts +37 -0
- package/src/_modules/logs/file-log.service.spec.ts +139 -0
- package/src/_modules/logs/file-log.service.ts +183 -0
- package/src/_modules/logs/file-logs.controller.spec.ts +245 -0
- package/src/_modules/logs/file-logs.controller.ts +165 -0
- package/src/_modules/logs/get-file-logs-routing-module.util.ts +51 -0
- package/src/_modules/logs/index.ts +7 -0
- package/src/_modules/server/errors/errors.controller.spec.ts +70 -0
- package/src/_modules/server/errors/errors.controller.ts +102 -0
|
@@ -199,4 +199,143 @@ describe('| DyNTS_FileLog_Service', (): void => {
|
|
|
199
199
|
expect(DyNTS_FileLog_Service.getInstance().isInstalled()).toBe(false);
|
|
200
200
|
});
|
|
201
201
|
});
|
|
202
|
+
|
|
203
|
+
describe('| listLogFiles()', (): void => {
|
|
204
|
+
it('| ures lista ha nincs installalva', (): void => {
|
|
205
|
+
const svc = DyNTS_FileLog_Service.getInstance();
|
|
206
|
+
// NEM hivunk install-t
|
|
207
|
+
expect(svc.listLogFiles()).toEqual([]);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it('| listazza az osszes prefix-elt log fajlt + jelzi az isCurrent flag-et', (): void => {
|
|
211
|
+
// 3 fajl letrehozasa kezzel kulonbozo prefix-szel
|
|
212
|
+
fs.writeFileSync(path.join(tempDir, 'spec-2026-01-01_aaa.log'), 'a');
|
|
213
|
+
fs.writeFileSync(path.join(tempDir, 'spec-2026-01-02_bbb.log'), 'bb');
|
|
214
|
+
fs.writeFileSync(path.join(tempDir, 'spec-2026-01-03_ccc.log'), 'ccc');
|
|
215
|
+
fs.writeFileSync(path.join(tempDir, 'other-2026-01-04_zzz.log'), 'should-be-ignored');
|
|
216
|
+
|
|
217
|
+
DyNTS_global_settings.log_settings.file_log = {
|
|
218
|
+
enabled: true,
|
|
219
|
+
logDir: tempDir,
|
|
220
|
+
filenamePrefix: 'spec-',
|
|
221
|
+
};
|
|
222
|
+
const svc = DyNTS_FileLog_Service.getInstance();
|
|
223
|
+
svc.install();
|
|
224
|
+
|
|
225
|
+
const list = svc.listLogFiles();
|
|
226
|
+
// 3 spec- fajl + 1 aktualis session fajl = 4
|
|
227
|
+
expect(list.length).toBe(4);
|
|
228
|
+
const names: string[] = list.map((e) => e.name);
|
|
229
|
+
expect(names.find((n) => n.startsWith('other-'))).toBeUndefined();
|
|
230
|
+
expect(list.filter((e) => e.isCurrent).length).toBe(1);
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
describe('| readLogFile() — happy paths', (): void => {
|
|
235
|
+
let svc: DyNTS_FileLog_Service;
|
|
236
|
+
let testFile: string;
|
|
237
|
+
|
|
238
|
+
beforeEach((): void => {
|
|
239
|
+
DyNTS_global_settings.log_settings.file_log = {
|
|
240
|
+
enabled: true,
|
|
241
|
+
logDir: tempDir,
|
|
242
|
+
filenamePrefix: 'spec-',
|
|
243
|
+
};
|
|
244
|
+
svc = DyNTS_FileLog_Service.getInstance();
|
|
245
|
+
svc.install();
|
|
246
|
+
// Kulon teszt-fajl
|
|
247
|
+
testFile = `spec-test-${Date.now()}.log`;
|
|
248
|
+
const lines: string[] = [];
|
|
249
|
+
for (let i: number = 1; i <= 50; i++) {
|
|
250
|
+
lines.push(`line-${i}`);
|
|
251
|
+
}
|
|
252
|
+
fs.writeFileSync(path.join(tempDir, testFile), lines.join('\n') + '\n');
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it('| tail mod: utolso N sor', (): void => {
|
|
256
|
+
const result = svc.readLogFile(testFile, { tail: 5 });
|
|
257
|
+
expect(result.mode).toBe('tail');
|
|
258
|
+
expect(result.lines).toEqual(['line-46', 'line-47', 'line-48', 'line-49', 'line-50']);
|
|
259
|
+
expect(result.totalLines).toBe(50);
|
|
260
|
+
expect(result.rangeStart).toBe(46);
|
|
261
|
+
expect(result.rangeEnd).toBe(50);
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it('| head mod: elso N sor', (): void => {
|
|
265
|
+
const result = svc.readLogFile(testFile, { head: 3 });
|
|
266
|
+
expect(result.mode).toBe('head');
|
|
267
|
+
expect(result.lines).toEqual(['line-1', 'line-2', 'line-3']);
|
|
268
|
+
expect(result.rangeStart).toBe(1);
|
|
269
|
+
expect(result.rangeEnd).toBe(3);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it('| range mod: explicit 10-15 tartomany', (): void => {
|
|
273
|
+
const result = svc.readLogFile(testFile, { rangeStart: 10, rangeEnd: 15 });
|
|
274
|
+
expect(result.mode).toBe('range');
|
|
275
|
+
expect(result.lines).toEqual(['line-10', 'line-11', 'line-12', 'line-13', 'line-14', 'line-15']);
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
it('| default tail 200 ha semmi mod nincs megadva', (): void => {
|
|
279
|
+
const result = svc.readLogFile(testFile, {});
|
|
280
|
+
expect(result.mode).toBe('tail');
|
|
281
|
+
// A teszt fajl 50 sor → mind visszajon
|
|
282
|
+
expect(result.lines.length).toBe(50);
|
|
283
|
+
});
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
describe('| readLogFile() — error paths', (): void => {
|
|
287
|
+
let svc: DyNTS_FileLog_Service;
|
|
288
|
+
|
|
289
|
+
beforeEach((): void => {
|
|
290
|
+
DyNTS_global_settings.log_settings.file_log = {
|
|
291
|
+
enabled: true,
|
|
292
|
+
logDir: tempDir,
|
|
293
|
+
filenamePrefix: 'spec-',
|
|
294
|
+
};
|
|
295
|
+
svc = DyNTS_FileLog_Service.getInstance();
|
|
296
|
+
svc.install();
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
it('| throw ha invalid filename (prefix nem matchel)', (): void => {
|
|
300
|
+
expect(() => svc.readLogFile('other-2026.log')).toThrowError('invalid filename');
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
it('| throw ha path traversal kiserlet (`../`)', (): void => {
|
|
304
|
+
expect(() => svc.readLogFile('../etc/passwd')).toThrowError('invalid filename');
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
it('| throw ha path separator (`/`) van a nevben', (): void => {
|
|
308
|
+
expect(() => svc.readLogFile('spec-foo/bar.log')).toThrowError('invalid filename');
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it('| throw ha file nem letezik', (): void => {
|
|
312
|
+
expect(() => svc.readLogFile('spec-nonexistent.log')).toThrowError('file not found');
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
it('| NEM installalt service → throw `not installed`', (): void => {
|
|
316
|
+
svc._teardownForTesting();
|
|
317
|
+
expect(() => svc.readLogFile('spec-foo.log')).toThrowError(/not installed/);
|
|
318
|
+
});
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
describe('| getCurrentLogFilename()', (): void => {
|
|
322
|
+
it('| ures string ha nincs installalva', (): void => {
|
|
323
|
+
expect(DyNTS_FileLog_Service.getInstance().getCurrentLogFilename()).toBe('');
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it('| basename-t ad vissza ha installalva van', (): void => {
|
|
327
|
+
DyNTS_global_settings.log_settings.file_log = {
|
|
328
|
+
enabled: true,
|
|
329
|
+
logDir: tempDir,
|
|
330
|
+
filenamePrefix: 'spec-',
|
|
331
|
+
};
|
|
332
|
+
const svc = DyNTS_FileLog_Service.getInstance();
|
|
333
|
+
svc.install();
|
|
334
|
+
const basename: string = svc.getCurrentLogFilename();
|
|
335
|
+
expect(basename.startsWith('spec-')).toBe(true);
|
|
336
|
+
expect(basename.endsWith('.log')).toBe(true);
|
|
337
|
+
expect(basename.includes('/')).toBe(false);
|
|
338
|
+
expect(basename.includes('\\')).toBe(false);
|
|
339
|
+
});
|
|
340
|
+
});
|
|
202
341
|
});
|
|
@@ -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
|
+
});
|