added doga

This commit is contained in:
szabomarton
2025-02-25 09:55:29 +01:00
parent 5174ab4cc4
commit 13254e5623
1149 changed files with 80161 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
export declare class FileLogger {
#private;
private writableStream;
constructor(filename: string);
toFile(stringToLog: string): void;
}

View File

@@ -0,0 +1,59 @@
import { accessSync, writeFileSync, createWriteStream, mkdirSync } from 'node:fs';
import { dirname as directoryname } from 'node:path';
export class FileLogger {
#filename;
#dirname;
writableStream;
constructor(filename) {
this.#dirname = directoryname(filename);
this.#filename = filename;
this.#_stat();
this.#_createWritableStream();
this.#_endStream();
}
#fsAccess(filename, mode) {
try {
accessSync(filename, mode);
return true;
}
catch (error) {
return false;
}
}
#_stat() {
//check if file exists
if (!this.#fsAccess(this.#filename)) {
// check if directory exists
if (!this.#fsAccess(this.#dirname)) {
// create the directory
mkdirSync(this.#dirname, { recursive: true });
}
// create the file and write an empty string to it
writeFileSync(this.#filename, '');
return;
}
}
#_createWritableStream() {
this.writableStream = createWriteStream(this.#filename, { flags: 'a' });
}
toFile(stringToLog) {
this.writableStream.write(stringToLog + '\n');
}
#_endStream() {
process.on('exit', () => {
this.writableStream.close();
});
process.on('SIGTERM', () => {
this.writableStream.close();
process.exit(0);
});
process.on('SIGINT', () => {
this.writableStream.close();
process.exit(0);
});
process.on('uncaughtException', () => {
this.writableStream.close();
process.exit(1);
});
}
}

23
25_02_24/node_modules/@tinyhttp/logger/dist/index.d.ts generated vendored Normal file
View File

@@ -0,0 +1,23 @@
import { ServerResponse as Response, IncomingMessage as Request } from 'node:http';
export declare enum LogLevel {
error = "error",
warn = "warn",
trace = "trace",
info = "info",
log = "log"
}
export interface LoggerOptions {
methods?: string[];
output?: {
color: boolean;
filename?: string;
callback: (string: string) => void;
level?: LogLevel;
};
timestamp?: boolean | {
format?: string;
};
emoji?: boolean;
ip?: boolean;
}
export declare const logger: (options?: LoggerOptions) => (req: Request, res: Response, next?: () => void) => void;

81
25_02_24/node_modules/@tinyhttp/logger/dist/index.js generated vendored Normal file
View File

@@ -0,0 +1,81 @@
import { cyan, red, magenta, bold } from 'colorette';
import statusEmoji from 'http-status-emojis';
import dayjs from 'dayjs';
import { METHODS } from 'node:http';
import { FileLogger } from './filelogger.js';
export var LogLevel;
(function (LogLevel) {
LogLevel["error"] = "error";
LogLevel["warn"] = "warn";
LogLevel["trace"] = "trace";
LogLevel["info"] = "info";
LogLevel["log"] = "log";
})(LogLevel || (LogLevel = {}));
const compileArgs = (args, req, res, options = {}, status, msg) => {
const { method } = req;
const { statusCode } = res;
const url = req.originalUrl || req.url;
const methods = options.methods ?? METHODS;
const timestamp = options.timestamp ?? false;
const emojiEnabled = options.emoji;
const level = options.output && options.output.level ? options.output.level : null;
if (level)
args.push('[' + level.toUpperCase() + ']');
if (methods.includes(method) && timestamp) {
args.push(`${dayjs()
.format(typeof timestamp !== 'boolean' && timestamp.format ? timestamp.format : 'HH:mm:ss')
.toString()} - `);
}
if (options.ip)
args.push(req.ip);
if (emojiEnabled)
args.push(statusEmoji[statusCode]);
args.push(method);
args.push(status || res.statusCode);
args.push(msg || res.statusMessage);
args.push(url);
};
export const logger = (options = {}) => {
const methods = options.methods ?? METHODS;
const output = options.output ?? { callback: console.log, color: true, level: null };
let filelogger = null;
if (options.output && options.output.filename) {
filelogger = new FileLogger(options.output.filename);
}
return (req, res, next) => {
res.on('finish', () => {
const args = [];
// every time
if (methods.includes(req.method)) {
const s = res.statusCode.toString();
let stringToLog = '';
if (!output.color) {
compileArgs(args, req, res, options);
const m = args.join(' ');
stringToLog = m;
}
else {
switch (s[0]) {
case '2':
compileArgs(args, req, res, options, cyan(bold(s)), cyan(res.statusMessage));
stringToLog = args.join(' ');
break;
case '4':
compileArgs(args, req, res, options, red(bold(s)), red(res.statusMessage));
stringToLog = args.join(' ');
break;
case '5':
compileArgs(args, req, res, options, magenta(bold(s)), magenta(res.statusMessage));
stringToLog = args.join(' ');
break;
}
}
output.callback(stringToLog);
if (filelogger) {
filelogger.toFile(stringToLog);
}
}
});
next?.();
};
};