ESLint: Auto reformat and fixing some warnings / errors.
This commit is contained in:
parent
5237db38e0
commit
91690509d3
50 changed files with 2087 additions and 1439 deletions
6
server/@types/http-auth-connect/index.d.ts
vendored
6
server/@types/http-auth-connect/index.d.ts
vendored
|
|
@ -1,6 +1,6 @@
|
|||
declare module "http-auth-connect" {
|
||||
import {Auth} from "http-auth";
|
||||
import {RequestHandler} from "express"
|
||||
import { Auth } from "http-auth";
|
||||
import { RequestHandler } from "express";
|
||||
|
||||
export default function (auth: Auth): RequestHandler
|
||||
export default function (auth: Auth): RequestHandler;
|
||||
}
|
||||
|
|
|
|||
17
server/@types/http-auth/index.d.ts
vendored
17
server/@types/http-auth/index.d.ts
vendored
|
|
@ -4,9 +4,18 @@ declare module "http-auth" {
|
|||
class BasicAuth extends Auth {}
|
||||
class BasicAuthOptions {}
|
||||
|
||||
type BasicAuthChecker =
|
||||
(username: string, password: string, callback: BasicAuthCheckerCallback) => void
|
||||
type BasicAuthCheckerCallback = (result: boolean | Error, customUser?: string) => void
|
||||
type BasicAuthChecker = (
|
||||
username: string,
|
||||
password: string,
|
||||
callback: BasicAuthCheckerCallback
|
||||
) => void;
|
||||
type BasicAuthCheckerCallback = (
|
||||
result: boolean | Error,
|
||||
customUser?: string
|
||||
) => void;
|
||||
|
||||
function basic(options: BasicAuthOptions, checker: BasicAuthChecker): BasicAuth
|
||||
function basic(
|
||||
options: BasicAuthOptions,
|
||||
checker: BasicAuthChecker
|
||||
): BasicAuth;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
declare module "nodemailer-html-to-text" {
|
||||
import {PluginFunction} from "nodemailer/lib/mailer";
|
||||
import {HtmlToTextOptions} from "html-to-text";
|
||||
import { PluginFunction } from "nodemailer/lib/mailer";
|
||||
import { HtmlToTextOptions } from "html-to-text";
|
||||
|
||||
export function htmlToText(options: HtmlToTextOptions): PluginFunction;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import {MockLogger} from "./logger";
|
||||
import { MockLogger } from "./logger";
|
||||
|
||||
test("should reset single message", () => {
|
||||
// given
|
||||
|
|
@ -104,7 +104,7 @@ test("should get messages for no tag", () => {
|
|||
|
||||
// when
|
||||
logger.tag().debug("message");
|
||||
|
||||
|
||||
// then
|
||||
expect(logger.getMessages("debug")).toEqual([["message"]]);
|
||||
});
|
||||
|
|
@ -152,7 +152,10 @@ test("should get multiple messages", () => {
|
|||
logger.tag("foo", "bar").debug("message 2");
|
||||
|
||||
// then
|
||||
expect(logger.getMessages("debug", "foo", "bar")).toEqual([["message 1"], ["message 2"]]);
|
||||
expect(logger.getMessages("debug", "foo", "bar")).toEqual([
|
||||
["message 1"],
|
||||
["message 2"],
|
||||
]);
|
||||
});
|
||||
|
||||
test("should get complex message", () => {
|
||||
|
|
@ -163,5 +166,7 @@ test("should get complex message", () => {
|
|||
logger.tag("foo", "bar").debug("message", 1, false, {});
|
||||
|
||||
// then
|
||||
expect(logger.getMessages("debug", "foo", "bar")).toEqual([["message", 1, false, {}]]);
|
||||
expect(logger.getMessages("debug", "foo", "bar")).toEqual([
|
||||
["message", 1, false, {}],
|
||||
]);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,21 +1,23 @@
|
|||
import {Logger, TaggedLogger, LogLevel} from '../types';
|
||||
import {ActivatableLogger} from '../logger';
|
||||
import { LogLevel, TaggedLogger } from "../types";
|
||||
import { ActivatableLogger } from "../logger";
|
||||
|
||||
export type MockLogMessages = any[][];
|
||||
export type MockLogMessages = unknown[][];
|
||||
type TaggedLogMessages = {
|
||||
tags: {[key: string]: TaggedLogMessages},
|
||||
logs: {[key: string]: MockLogMessages}
|
||||
}
|
||||
tags: { [key: string]: TaggedLogMessages };
|
||||
logs: { [key: string]: MockLogMessages };
|
||||
};
|
||||
|
||||
export class MockLogger implements ActivatableLogger {
|
||||
private taggedLogMessages: TaggedLogMessages = MockLogger.emptyTaggedLogMessages();
|
||||
private taggedLogMessages: TaggedLogMessages =
|
||||
MockLogger.emptyTaggedLogMessages();
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||
constructor() {}
|
||||
|
||||
private static emptyTaggedLogMessages(): TaggedLogMessages {
|
||||
return {
|
||||
tags: {},
|
||||
logs: {}
|
||||
logs: {},
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -36,46 +38,54 @@ export class MockLogger implements ActivatableLogger {
|
|||
return taggedLogMessages.logs[level] || [];
|
||||
}
|
||||
|
||||
init(...args: any[]): void {}
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-function,@typescript-eslint/no-unused-vars
|
||||
init(...args: unknown[]): void {}
|
||||
|
||||
private doLog(taggedLogMessages: TaggedLogMessages, level: LogLevel, tags: string[], args: any[]): void {
|
||||
private doLog(
|
||||
taggedLogMessages: TaggedLogMessages,
|
||||
level: LogLevel,
|
||||
tags: string[],
|
||||
args: unknown[]
|
||||
): void {
|
||||
if (tags.length > 0) {
|
||||
const tag = tags[0];
|
||||
const remainingTags = tags.slice(1);
|
||||
const subTaggedLogsMessages: TaggedLogMessages =
|
||||
taggedLogMessages.tags[tag] || MockLogger.emptyTaggedLogMessages();
|
||||
taggedLogMessages.tags[tag] ||
|
||||
MockLogger.emptyTaggedLogMessages();
|
||||
this.doLog(subTaggedLogsMessages, level, remainingTags, args);
|
||||
taggedLogMessages.tags[tag] = subTaggedLogsMessages;
|
||||
|
||||
} else {
|
||||
const logMessages: MockLogMessages = taggedLogMessages.logs[level] || [];
|
||||
const logMessages: MockLogMessages =
|
||||
taggedLogMessages.logs[level] || [];
|
||||
logMessages.push(args);
|
||||
taggedLogMessages.logs[level] = logMessages;
|
||||
}
|
||||
}
|
||||
|
||||
tag(...tags: string[]): TaggedLogger {
|
||||
const logger: MockLogger = this;
|
||||
const doLog = this.doLog.bind(this);
|
||||
const taggedLogMessages = this.taggedLogMessages;
|
||||
return {
|
||||
log(level: LogLevel, ...args: any[]): void {
|
||||
logger.doLog(logger.taggedLogMessages, level, tags, args);
|
||||
log(level: LogLevel, ...args: unknown[]): void {
|
||||
doLog(taggedLogMessages, level, tags, args);
|
||||
},
|
||||
debug(...args: any[]): void {
|
||||
this.log('debug', ...args);
|
||||
debug(...args: unknown[]): void {
|
||||
this.log("debug", ...args);
|
||||
},
|
||||
info(...args: any[]): void {
|
||||
this.log('info', ...args);
|
||||
info(...args: unknown[]): void {
|
||||
this.log("info", ...args);
|
||||
},
|
||||
warn(...args: any[]): void {
|
||||
this.log('warn', ...args);
|
||||
warn(...args: unknown[]): void {
|
||||
this.log("warn", ...args);
|
||||
},
|
||||
error(...args: any[]): void {
|
||||
this.log('error', ...args);
|
||||
error(...args: unknown[]): void {
|
||||
this.log("error", ...args);
|
||||
},
|
||||
profile(...args: any[]): void {
|
||||
this.log('profile', ...args);
|
||||
profile(...args: unknown[]): void {
|
||||
this.log("profile", ...args);
|
||||
},
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,15 +1,15 @@
|
|||
import _ from "lodash";
|
||||
import auth, {BasicAuthCheckerCallback} from "http-auth";
|
||||
import auth, { BasicAuthCheckerCallback } from "http-auth";
|
||||
import authConnect from "http-auth-connect";
|
||||
import bodyParser from "body-parser";
|
||||
import bcrypt from "bcrypt";
|
||||
import compress from "compression";
|
||||
import express, {Express, NextFunction, Request, Response} from "express";
|
||||
import {promises as fs} from "graceful-fs";
|
||||
import express, { Express, NextFunction, Request, Response } from "express";
|
||||
import { promises as fs } from "graceful-fs";
|
||||
|
||||
import {config} from "./config";
|
||||
import type {CleartextPassword, PasswordHash, Username} from "./types";
|
||||
import {isString} from "./types";
|
||||
import { config } from "./config";
|
||||
import type { CleartextPassword, PasswordHash, Username } from "./types";
|
||||
import { isString } from "./types";
|
||||
import Logger from "./logger";
|
||||
|
||||
export const app: Express = express();
|
||||
|
|
@ -17,7 +17,8 @@ export const app: Express = express();
|
|||
/**
|
||||
* Used to have some password comparison in case the user does not exist to avoid timing attacks.
|
||||
*/
|
||||
const INVALID_PASSWORD_HASH: PasswordHash = "$2b$05$JebmV1q/ySuxa89GoJYlc.6SEnj1OZYBOfTf.TYAehcC5HLeJiWPi" as PasswordHash;
|
||||
const INVALID_PASSWORD_HASH: PasswordHash =
|
||||
"$2b$05$JebmV1q/ySuxa89GoJYlc.6SEnj1OZYBOfTf.TYAehcC5HLeJiWPi" as PasswordHash;
|
||||
|
||||
/**
|
||||
* Trying to implement a timing safe string compare.
|
||||
|
|
@ -41,7 +42,10 @@ function timingSafeEqual<T extends string>(a: T, b: T): boolean {
|
|||
return different === 0;
|
||||
}
|
||||
|
||||
async function isValidLogin(username: Username, password: CleartextPassword): Promise<boolean> {
|
||||
async function isValidLogin(
|
||||
username: Username,
|
||||
password: CleartextPassword
|
||||
): Promise<boolean> {
|
||||
if (!config.server.internal.active) {
|
||||
return false;
|
||||
}
|
||||
|
|
@ -71,52 +75,63 @@ export function init(): void {
|
|||
// urls beneath /internal are protected
|
||||
const internalAuth = auth.basic(
|
||||
{
|
||||
realm: 'Knotenformular - Intern'
|
||||
realm: "Knotenformular - Intern",
|
||||
},
|
||||
function (username: string, password: string, callback: BasicAuthCheckerCallback): void {
|
||||
function (
|
||||
username: string,
|
||||
password: string,
|
||||
callback: BasicAuthCheckerCallback
|
||||
): void {
|
||||
isValidLogin(username as Username, password as CleartextPassword)
|
||||
.then(result => callback(result))
|
||||
.catch(err => {
|
||||
Logger.tag('login').error(err);
|
||||
.then((result) => callback(result))
|
||||
.catch((err) => {
|
||||
Logger.tag("login").error(err);
|
||||
});
|
||||
}
|
||||
);
|
||||
router.use('/internal', authConnect(internalAuth));
|
||||
router.use("/internal", authConnect(internalAuth));
|
||||
|
||||
router.use(bodyParser.json());
|
||||
router.use(bodyParser.urlencoded({extended: true}));
|
||||
router.use(bodyParser.urlencoded({ extended: true }));
|
||||
|
||||
const adminDir = __dirname + '/../admin';
|
||||
const clientDir = __dirname + '/../client';
|
||||
const templateDir = __dirname + '/templates';
|
||||
const adminDir = __dirname + "/../admin";
|
||||
const clientDir = __dirname + "/../client";
|
||||
const templateDir = __dirname + "/templates";
|
||||
|
||||
const jsTemplateFiles = [
|
||||
'/config.js'
|
||||
];
|
||||
const jsTemplateFiles = ["/config.js"];
|
||||
|
||||
function usePromise(f: (req: Request, res: Response) => Promise<void>): void {
|
||||
function usePromise(
|
||||
f: (req: Request, res: Response) => Promise<void>
|
||||
): void {
|
||||
router.use((req: Request, res: Response, next: NextFunction): void => {
|
||||
f(req, res).then(next).catch(next)
|
||||
f(req, res).then(next).catch(next);
|
||||
});
|
||||
}
|
||||
|
||||
router.use(compress());
|
||||
|
||||
async function serveTemplate(mimeType: string, req: Request, res: Response): Promise<void> {
|
||||
const body = await fs.readFile(templateDir + '/' + req.path + '.template', 'utf8');
|
||||
async function serveTemplate(
|
||||
mimeType: string,
|
||||
req: Request,
|
||||
res: Response
|
||||
): Promise<void> {
|
||||
const body = await fs.readFile(
|
||||
templateDir + "/" + req.path + ".template",
|
||||
"utf8"
|
||||
);
|
||||
|
||||
res.writeHead(200, {'Content-Type': mimeType});
|
||||
res.end(_.template(body)({config: config.client}));
|
||||
res.writeHead(200, { "Content-Type": mimeType });
|
||||
res.end(_.template(body)({ config: config.client }));
|
||||
}
|
||||
|
||||
usePromise(async (req: Request, res: Response): Promise<void> => {
|
||||
if (jsTemplateFiles.indexOf(req.path) >= 0) {
|
||||
await serveTemplate('application/javascript', req, res);
|
||||
await serveTemplate("application/javascript", req, res);
|
||||
}
|
||||
});
|
||||
|
||||
router.use('/internal/admin', express.static(adminDir + '/'));
|
||||
router.use('/', express.static(clientDir + '/'));
|
||||
router.use("/internal/admin", express.static(adminDir + "/"));
|
||||
router.use("/", express.static(clientDir + "/"));
|
||||
|
||||
app.use(config.server.rootPath, router);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,46 +1,65 @@
|
|||
import commandLineArgs from "command-line-args"
|
||||
import commandLineUsage from "command-line-usage"
|
||||
import fs from "graceful-fs"
|
||||
import url from "url"
|
||||
import {parse} from "sparkson"
|
||||
import {Config, Url, Version} from "./types"
|
||||
import commandLineArgs from "command-line-args";
|
||||
import commandLineUsage from "command-line-usage";
|
||||
import fs from "graceful-fs";
|
||||
import url from "url";
|
||||
import { parse } from "sparkson";
|
||||
import { Config, hasOwnProperty, Url, Version } from "./types";
|
||||
|
||||
// @ts-ignore
|
||||
export let config: Config = {};
|
||||
export let config: Config = {} as Config;
|
||||
export let version: Version = "unknown" as Version;
|
||||
|
||||
export function parseCommandLine(): void {
|
||||
const commandLineDefs = [
|
||||
{name: 'help', alias: 'h', type: Boolean, description: 'Show this help'},
|
||||
{name: 'config', alias: 'c', type: String, description: 'Location of config.json'},
|
||||
{name: 'version', alias: 'v', type: Boolean, description: 'Show ffffng version'}
|
||||
{
|
||||
name: "help",
|
||||
alias: "h",
|
||||
type: Boolean,
|
||||
description: "Show this help",
|
||||
},
|
||||
{
|
||||
name: "config",
|
||||
alias: "c",
|
||||
type: String,
|
||||
description: "Location of config.json",
|
||||
},
|
||||
{
|
||||
name: "version",
|
||||
alias: "v",
|
||||
type: Boolean,
|
||||
description: "Show ffffng version",
|
||||
},
|
||||
];
|
||||
|
||||
let commandLineOptions;
|
||||
try {
|
||||
commandLineOptions = commandLineArgs(commandLineDefs);
|
||||
} catch (e: any) {
|
||||
if (e.message) {
|
||||
console.error(e.message);
|
||||
} catch (error) {
|
||||
if (hasOwnProperty(error, "message")) {
|
||||
console.error(error.message);
|
||||
} else {
|
||||
console.error(e);
|
||||
console.error(error);
|
||||
}
|
||||
console.error('Try \'--help\' for more information.');
|
||||
console.error("Try '--help' for more information.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const packageJsonFile = __dirname + '/../package.json';
|
||||
const packageJsonFile = __dirname + "/../package.json";
|
||||
if (fs.existsSync(packageJsonFile)) {
|
||||
version = JSON.parse(fs.readFileSync(packageJsonFile, 'utf8')).version;
|
||||
version = JSON.parse(fs.readFileSync(packageJsonFile, "utf8")).version;
|
||||
}
|
||||
|
||||
function usage() {
|
||||
console.log(commandLineUsage([
|
||||
{
|
||||
header: 'ffffng - ' + version + ' - Freifunk node management form',
|
||||
optionList: commandLineDefs
|
||||
}
|
||||
]));
|
||||
console.log(
|
||||
commandLineUsage([
|
||||
{
|
||||
header:
|
||||
"ffffng - " +
|
||||
version +
|
||||
" - Freifunk node management form",
|
||||
optionList: commandLineDefs,
|
||||
},
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
if (commandLineOptions.help) {
|
||||
|
|
@ -49,7 +68,7 @@ export function parseCommandLine(): void {
|
|||
}
|
||||
|
||||
if (commandLineOptions.version) {
|
||||
console.log('ffffng - ' + version);
|
||||
console.log("ffffng - " + version);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
|
|
@ -62,9 +81,9 @@ export function parseCommandLine(): void {
|
|||
let configJSON = {};
|
||||
|
||||
if (fs.existsSync(configJSONFile)) {
|
||||
configJSON = JSON.parse(fs.readFileSync(configJSONFile, 'utf8'));
|
||||
configJSON = JSON.parse(fs.readFileSync(configJSONFile, "utf8"));
|
||||
} else {
|
||||
console.error('config.json not found: ' + configJSONFile);
|
||||
console.error("config.json not found: " + configJSONFile);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
|
|
@ -72,7 +91,7 @@ export function parseCommandLine(): void {
|
|||
|
||||
function stripTrailingSlash(url: Url): Url {
|
||||
return url.endsWith("/")
|
||||
? url.substring(0, url.length - 1) as Url
|
||||
? (url.substring(0, url.length - 1) as Url)
|
||||
: url;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,48 +1,83 @@
|
|||
import {RunResult, SqlType, Statement, TypedDatabase} from "../../types";
|
||||
import { RunResult, SqlType, Statement, TypedDatabase } from "../../types";
|
||||
import * as sqlite3 from "sqlite3";
|
||||
|
||||
export async function init(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
export class MockDatabase implements TypedDatabase {
|
||||
constructor() {
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||
constructor() {}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
async on(event: string, listener: unknown): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
async on(event: string, listener: any): Promise<void> {
|
||||
}
|
||||
|
||||
async run(sql: SqlType, ...params: any[]): Promise<RunResult> {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
async run(sql: SqlType, ...params: unknown[]): Promise<RunResult> {
|
||||
return {
|
||||
stmt: new Statement(new sqlite3.Statement()),
|
||||
};
|
||||
}
|
||||
|
||||
async get<T = any>(sql: SqlType, ...params: any[]): Promise<T | undefined> {
|
||||
async get<T = unknown>(
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
sql: SqlType,
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
...params: unknown[]
|
||||
): Promise<T | undefined> {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
async each<T = any>(sql: SqlType, callback: (err: any, row: T) => void): Promise<number>;
|
||||
async each<T = any>(sql: SqlType, param1: any, callback: (err: any, row: T) => void): Promise<number>;
|
||||
async each<T = any>(sql: SqlType, param1: any, param2: any, callback: (err: any, row: T) => void): Promise<number>;
|
||||
async each<T = any>(sql: SqlType, param1: any, param2: any, param3: any, callback: (err: any, row: T) => void): Promise<number>;
|
||||
async each<T = any>(sql: SqlType, ...params: any[]): Promise<number>;
|
||||
async each(sql: SqlType, ...callback: (any)[]): Promise<number> {
|
||||
async each<T = unknown>(
|
||||
sql: SqlType,
|
||||
callback: (err: unknown, row: T) => void
|
||||
): Promise<number>;
|
||||
async each<T = unknown>(
|
||||
sql: SqlType,
|
||||
param1: unknown,
|
||||
callback: (err: unknown, row: T) => void
|
||||
): Promise<number>;
|
||||
async each<T = unknown>(
|
||||
sql: SqlType,
|
||||
param1: unknown,
|
||||
param2: unknown,
|
||||
callback: (err: unknown, row: T) => void
|
||||
): Promise<number>;
|
||||
async each<T = unknown>(
|
||||
sql: SqlType,
|
||||
param1: unknown,
|
||||
param2: unknown,
|
||||
param3: unknown,
|
||||
callback: (err: unknown, row: T) => void
|
||||
): Promise<number>;
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
async each<T = unknown>(
|
||||
sql: SqlType,
|
||||
...params: unknown[]
|
||||
): Promise<number>;
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
async each(sql: SqlType, ...callback: unknown[]): Promise<number> {
|
||||
return 0;
|
||||
}
|
||||
|
||||
async all<T>(sql: SqlType, ...params: any[]): Promise<T[]> {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
async all<T>(sql: SqlType, ...params: unknown[]): Promise<T[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
async exec(sql: SqlType, ...params: any[]): Promise<void> {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
async exec(sql: SqlType, ...params: unknown[]): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
async prepare(sql: SqlType, ...params: any[]): Promise<Statement> {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
async prepare(sql: SqlType, ...params: unknown[]): Promise<Statement> {
|
||||
return new Statement(new sqlite3.Statement());
|
||||
}
|
||||
}
|
||||
|
||||
export const db: MockDatabase = new MockDatabase();
|
||||
|
||||
export {TypedDatabase, Statement}
|
||||
export { TypedDatabase, Statement };
|
||||
|
|
|
|||
|
|
@ -2,11 +2,11 @@ import util from "util";
|
|||
import fs from "graceful-fs";
|
||||
import glob from "glob";
|
||||
import path from "path";
|
||||
import {config} from "../config";
|
||||
import { config } from "../config";
|
||||
import Logger from "../logger";
|
||||
import {Database, open, Statement} from "sqlite";
|
||||
import { Database, open, Statement } from "sqlite";
|
||||
import * as sqlite3 from "sqlite3";
|
||||
import {RunResult, SqlType, TypedDatabase} from "../types";
|
||||
import { RunResult, SqlType, TypedDatabase } from "../types";
|
||||
|
||||
const pglob = util.promisify(glob);
|
||||
const pReadFile = util.promisify(fs.readFile);
|
||||
|
|
@ -28,102 +28,145 @@ class DatabasePromiseWrapper implements TypedDatabase {
|
|||
.then(resolve)
|
||||
.catch(reject);
|
||||
});
|
||||
this.db.catch(err => {
|
||||
Logger.tag('database', 'init').error('Error initializing database: ', err);
|
||||
this.db.catch((err) => {
|
||||
Logger.tag("database", "init").error(
|
||||
"Error initializing database: ",
|
||||
err
|
||||
);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
async on(event: string, listener: any): Promise<void> {
|
||||
async on(event: string, listener: unknown): Promise<void> {
|
||||
const db = await this.db;
|
||||
db.on(event, listener);
|
||||
}
|
||||
|
||||
async run(sql: SqlType, ...params: any[]): Promise<RunResult> {
|
||||
async run(sql: SqlType, ...params: unknown[]): Promise<RunResult> {
|
||||
const db = await this.db;
|
||||
return db.run(sql, ...params);
|
||||
}
|
||||
|
||||
async get<T>(sql: SqlType, ...params: any[]): Promise<T | undefined> {
|
||||
async get<T>(sql: SqlType, ...params: unknown[]): Promise<T | undefined> {
|
||||
const db = await this.db;
|
||||
return await db.get<T>(sql, ...params);
|
||||
}
|
||||
|
||||
async each<T>(sql: SqlType, callback: (err: any, row: T) => void): Promise<number>;
|
||||
async each<T>(sql: SqlType, param1: any, callback: (err: any, row: T) => void): Promise<number>;
|
||||
async each<T>(sql: SqlType, param1: any, param2: any, callback: (err: any, row: T) => void): Promise<number>;
|
||||
async each<T>(sql: SqlType, param1: any, param2: any, param3: any, callback: (err: any, row: T) => void): Promise<number>;
|
||||
async each<T>(sql: SqlType, ...params: any[]): Promise<number> {
|
||||
async each<T>(
|
||||
sql: SqlType,
|
||||
callback: (err: unknown, row: T) => void
|
||||
): Promise<number>;
|
||||
async each<T>(
|
||||
sql: SqlType,
|
||||
param1: unknown,
|
||||
callback: (err: unknown, row: T) => void
|
||||
): Promise<number>;
|
||||
async each<T>(
|
||||
sql: SqlType,
|
||||
param1: unknown,
|
||||
param2: unknown,
|
||||
callback: (err: unknown, row: T) => void
|
||||
): Promise<number>;
|
||||
async each<T>(
|
||||
sql: SqlType,
|
||||
param1: unknown,
|
||||
param2: unknown,
|
||||
param3: unknown,
|
||||
callback: (err: unknown, row: T) => void
|
||||
): Promise<number>;
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
async each<T>(sql: SqlType, ...params: unknown[]): Promise<number> {
|
||||
const db = await this.db;
|
||||
// @ts-ignore
|
||||
return await db.each.apply(db, arguments);
|
||||
return await db.each(sql, ...params);
|
||||
}
|
||||
|
||||
async all<T>(sql: SqlType, ...params: any[]): Promise<T[]> {
|
||||
async all<T>(sql: SqlType, ...params: unknown[]): Promise<T[]> {
|
||||
const db = await this.db;
|
||||
return (await db.all<T[]>(sql, ...params));
|
||||
return await db.all<T[]>(sql, ...params);
|
||||
}
|
||||
|
||||
async exec(sql: SqlType, ...params: any[]): Promise<void> {
|
||||
async exec(sql: SqlType, ...params: unknown[]): Promise<void> {
|
||||
const db = await this.db;
|
||||
return await db.exec(sql, ...params);
|
||||
}
|
||||
|
||||
async prepare(sql: SqlType, ...params: any[]): Promise<Statement> {
|
||||
async prepare(sql: SqlType, ...params: unknown[]): Promise<Statement> {
|
||||
const db = await this.db;
|
||||
return await db.prepare(sql, ...params);
|
||||
}
|
||||
}
|
||||
|
||||
async function applyPatch(db: TypedDatabase, file: string): Promise<void> {
|
||||
Logger.tag('database', 'migration').info('Checking if patch need to be applied: %s', file);
|
||||
Logger.tag("database", "migration").info(
|
||||
"Checking if patch need to be applied: %s",
|
||||
file
|
||||
);
|
||||
|
||||
const contents = await pReadFile(file);
|
||||
const version = path.basename(file, '.sql');
|
||||
const version = path.basename(file, ".sql");
|
||||
|
||||
const row = await db.get('SELECT * FROM schema_version WHERE version = ?', version);
|
||||
const row = await db.get(
|
||||
"SELECT * FROM schema_version WHERE version = ?",
|
||||
version
|
||||
);
|
||||
if (row) {
|
||||
// patch is already applied. skip!
|
||||
Logger.tag('database', 'migration').info('Patch already applied, skipping: %s', file);
|
||||
return
|
||||
Logger.tag("database", "migration").info(
|
||||
"Patch already applied, skipping: %s",
|
||||
file
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const sql = 'BEGIN TRANSACTION;\n' +
|
||||
contents.toString() + '\n' +
|
||||
'INSERT INTO schema_version (version) VALUES (\'' + version + '\');\n' +
|
||||
'END TRANSACTION;';
|
||||
const sql =
|
||||
"BEGIN TRANSACTION;\n" +
|
||||
contents.toString() +
|
||||
"\n" +
|
||||
"INSERT INTO schema_version (version) VALUES ('" +
|
||||
version +
|
||||
"');\n" +
|
||||
"END TRANSACTION;";
|
||||
|
||||
await db.exec(sql);
|
||||
|
||||
Logger.tag('database', 'migration').info('Patch successfully applied: %s', file);
|
||||
Logger.tag("database", "migration").info(
|
||||
"Patch successfully applied: %s",
|
||||
file
|
||||
);
|
||||
}
|
||||
|
||||
async function applyMigrations(db: TypedDatabase): Promise<void> {
|
||||
Logger.tag('database', 'migration').info('Migrating database...');
|
||||
Logger.tag("database", "migration").info("Migrating database...");
|
||||
|
||||
const sql = 'BEGIN TRANSACTION; CREATE TABLE IF NOT EXISTS schema_version (\n' +
|
||||
' version VARCHAR(255) PRIMARY KEY ASC,\n' +
|
||||
' applied_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL\n' +
|
||||
'); END TRANSACTION;';
|
||||
const sql =
|
||||
"BEGIN TRANSACTION; CREATE TABLE IF NOT EXISTS schema_version (\n" +
|
||||
" version VARCHAR(255) PRIMARY KEY ASC,\n" +
|
||||
" applied_at DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL\n" +
|
||||
"); END TRANSACTION;";
|
||||
|
||||
await db.exec(sql);
|
||||
|
||||
const files = await pglob(__dirname + '/patches/*.sql');
|
||||
const files = await pglob(__dirname + "/patches/*.sql");
|
||||
for (const file of files) {
|
||||
await applyPatch(db, file)
|
||||
await applyPatch(db, file);
|
||||
}
|
||||
}
|
||||
|
||||
export const db: TypedDatabase = new DatabasePromiseWrapper();
|
||||
|
||||
export async function init(): Promise<void> {
|
||||
Logger.tag('database').info('Setting up database: %s', config.server.databaseFile);
|
||||
await db.on('profile', (sql: string, time: number) => Logger.tag('database').profile('[%sms]\t%s', time, sql));
|
||||
Logger.tag("database").info(
|
||||
"Setting up database: %s",
|
||||
config.server.databaseFile
|
||||
);
|
||||
await db.on("profile", (sql: string, time: number) =>
|
||||
Logger.tag("database").profile("[%sms]\t%s", time, sql)
|
||||
);
|
||||
|
||||
try {
|
||||
await applyMigrations(db);
|
||||
} catch (error) {
|
||||
Logger.tag('database').error('Error migrating database:', error);
|
||||
Logger.tag("database").error("Error migrating database:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +0,0 @@
|
|||
'use strict';
|
||||
|
||||
(function () {
|
||||
// Use graceful-fs instead of fs also in all libraries to have more robust fs handling.
|
||||
const realFs = require('fs');
|
||||
const gracefulFs = require('graceful-fs');
|
||||
gracefulFs.gracefulify(realFs);
|
||||
|
||||
// Init config by parsing commandline. Afterwards all other imports may happen.
|
||||
require('./config').parseCommandLine();
|
||||
})();
|
||||
10
server/init.ts
Normal file
10
server/init.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import realFs from "fs";
|
||||
import gracefulFs from "graceful-fs";
|
||||
|
||||
import { parseCommandLine } from "./config";
|
||||
|
||||
// Use graceful-fs instead of fs also in all libraries to have more robust fs handling.
|
||||
gracefulFs.gracefulify(realFs);
|
||||
|
||||
// Init config by parsing commandline. Afterwards all other imports may happen.
|
||||
parseCommandLine();
|
||||
|
|
@ -1,12 +1,13 @@
|
|||
import {fixNodeFilenames} from "../services/nodeService";
|
||||
import {jobResultOkay} from "./scheduler";
|
||||
import { fixNodeFilenames } from "../services/nodeService";
|
||||
import { jobResultOkay } from "./scheduler";
|
||||
|
||||
export default {
|
||||
name: 'FixNodeFilenamesJob',
|
||||
description: 'Makes sure node files (holding fastd key, name, etc.) are correctly named.',
|
||||
name: "FixNodeFilenamesJob",
|
||||
description:
|
||||
"Makes sure node files (holding fastd key, name, etc.) are correctly named.",
|
||||
|
||||
async run() {
|
||||
await fixNodeFilenames();
|
||||
return jobResultOkay();
|
||||
},
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
import * as MailService from "../services/mailService"
|
||||
import {jobResultOkay} from "./scheduler";
|
||||
import * as MailService from "../services/mailService";
|
||||
import { jobResultOkay } from "./scheduler";
|
||||
|
||||
export default {
|
||||
name: 'MailQueueJob',
|
||||
description: 'Send pending emails (up to 5 attempts in case of failures).',
|
||||
name: "MailQueueJob",
|
||||
description: "Send pending emails (up to 5 attempts in case of failures).",
|
||||
|
||||
async run() {
|
||||
await MailService.sendPendingMails();
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
import * as MonitoringService from "../services/monitoringService";
|
||||
import {jobResultOkay} from "./scheduler";
|
||||
import { jobResultOkay } from "./scheduler";
|
||||
|
||||
export default {
|
||||
name: 'MonitoringMailsSendingJob',
|
||||
description: 'Sends monitoring emails depending on the monitoring state of nodes retrieved by the NodeInformationRetrievalJob.',
|
||||
name: "MonitoringMailsSendingJob",
|
||||
description:
|
||||
"Sends monitoring emails depending on the monitoring state of nodes retrieved by the NodeInformationRetrievalJob.",
|
||||
|
||||
async run() {
|
||||
await MonitoringService.sendMonitoringMails();
|
||||
|
|
|
|||
|
|
@ -1,11 +1,12 @@
|
|||
import * as MonitoringService from "../services/monitoringService";
|
||||
import {jobResultOkay, jobResultWarning} from "./scheduler";
|
||||
import { jobResultOkay, jobResultWarning } from "./scheduler";
|
||||
|
||||
export default {
|
||||
name: 'NodeInformationRetrievalJob',
|
||||
description: 'Fetches the nodes.json and calculates and stores the monitoring / online status for registered nodes.',
|
||||
name: "NodeInformationRetrievalJob",
|
||||
description:
|
||||
"Fetches the nodes.json and calculates and stores the monitoring / online status for registered nodes.",
|
||||
|
||||
async run () {
|
||||
async run() {
|
||||
const result = await MonitoringService.retrieveNodeInformation();
|
||||
if (result.failedParsingNodesCount > 0) {
|
||||
return jobResultWarning(
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
import * as MonitoringService from "../services/monitoringService";
|
||||
import {jobResultOkay} from "./scheduler";
|
||||
import { jobResultOkay } from "./scheduler";
|
||||
|
||||
export default {
|
||||
name: 'OfflineNodesDeletionJob',
|
||||
description: 'Delete nodes that are offline for more than 100 days.',
|
||||
name: "OfflineNodesDeletionJob",
|
||||
description: "Delete nodes that are offline for more than 100 days.",
|
||||
|
||||
async run() {
|
||||
await MonitoringService.deleteOfflineNodes();
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import cron from "node-cron";
|
||||
import moment from "moment";
|
||||
|
||||
import {config} from "../config";
|
||||
import { config } from "../config";
|
||||
import Logger from "../logger";
|
||||
|
||||
import MailQueueJob from "./MailQueueJob";
|
||||
|
|
@ -16,29 +16,29 @@ export enum JobResultState {
|
|||
}
|
||||
|
||||
export type JobResult = {
|
||||
state: JobResultState,
|
||||
message?: string,
|
||||
state: JobResultState;
|
||||
message?: string;
|
||||
};
|
||||
|
||||
export function jobResultOkay(message?: string): JobResult {
|
||||
return {
|
||||
state: JobResultState.OKAY,
|
||||
message
|
||||
}
|
||||
message,
|
||||
};
|
||||
}
|
||||
|
||||
export function jobResultWarning(message?: string): JobResult {
|
||||
return {
|
||||
state: JobResultState.WARNING,
|
||||
message
|
||||
}
|
||||
message,
|
||||
};
|
||||
}
|
||||
|
||||
export interface Job {
|
||||
name: string,
|
||||
description: string,
|
||||
name: string;
|
||||
description: string;
|
||||
|
||||
run(): Promise<JobResult>,
|
||||
run(): Promise<JobResult>;
|
||||
}
|
||||
|
||||
export enum TaskState {
|
||||
|
|
@ -59,7 +59,7 @@ export class Task {
|
|||
public lastRunDuration: number | null,
|
||||
public state: TaskState,
|
||||
public result: JobResult | null,
|
||||
public enabled: boolean,
|
||||
public enabled: boolean
|
||||
) {}
|
||||
|
||||
run(): void {
|
||||
|
|
@ -75,7 +75,7 @@ export class Task {
|
|||
const done = (state: TaskState, result: JobResult | null): void => {
|
||||
const now = moment();
|
||||
const duration = now.diff(this.runningSince || now);
|
||||
Logger.tag('jobs').profile('[%sms]\t%s', duration, this.name);
|
||||
Logger.tag("jobs").profile("[%sms]\t%s", duration, this.name);
|
||||
|
||||
this.runningSince = null;
|
||||
this.lastRunDuration = duration;
|
||||
|
|
@ -83,16 +83,19 @@ export class Task {
|
|||
this.result = result;
|
||||
};
|
||||
|
||||
this.job.run().then(result => {
|
||||
done(TaskState.IDLE, result);
|
||||
}).catch(err => {
|
||||
Logger.tag('jobs').error("Job %s failed: %s", this.name, err);
|
||||
done(TaskState.FAILED, null);
|
||||
});
|
||||
this.job
|
||||
.run()
|
||||
.then((result) => {
|
||||
done(TaskState.IDLE, result);
|
||||
})
|
||||
.catch((err) => {
|
||||
Logger.tag("jobs").error("Job %s failed: %s", this.name, err);
|
||||
done(TaskState.FAILED, null);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
type Tasks = {[key: string]: Task};
|
||||
type Tasks = { [key: string]: Task };
|
||||
|
||||
const tasks: Tasks = {};
|
||||
|
||||
|
|
@ -104,7 +107,7 @@ function nextTaskId(): number {
|
|||
}
|
||||
|
||||
function schedule(expr: string, job: Job): void {
|
||||
Logger.tag('jobs').info('Scheduling job: %s %s', expr, job.name);
|
||||
Logger.tag("jobs").info("Scheduling job: %s %s", expr, job.name);
|
||||
|
||||
const id = nextTaskId();
|
||||
|
||||
|
|
@ -119,33 +122,35 @@ function schedule(expr: string, job: Job): void {
|
|||
null,
|
||||
TaskState.IDLE,
|
||||
null,
|
||||
true,
|
||||
true
|
||||
);
|
||||
|
||||
cron.schedule(expr, () => task.run());
|
||||
|
||||
tasks['' + id] = task;
|
||||
tasks["" + id] = task;
|
||||
}
|
||||
|
||||
export function init() {
|
||||
Logger.tag('jobs').info('Scheduling background jobs...');
|
||||
Logger.tag("jobs").info("Scheduling background jobs...");
|
||||
|
||||
try {
|
||||
schedule('0 */1 * * * *', MailQueueJob);
|
||||
schedule('15 */1 * * * *', FixNodeFilenamesJob);
|
||||
schedule("0 */1 * * * *", MailQueueJob);
|
||||
schedule("15 */1 * * * *", FixNodeFilenamesJob);
|
||||
|
||||
if (config.client.monitoring.enabled) {
|
||||
schedule('30 */15 * * * *', NodeInformationRetrievalJob);
|
||||
schedule('45 */5 * * * *', MonitoringMailsSendingJob);
|
||||
schedule('0 0 3 * * *', OfflineNodesDeletionJob); // every night at 3:00
|
||||
schedule("30 */15 * * * *", NodeInformationRetrievalJob);
|
||||
schedule("45 */5 * * * *", MonitoringMailsSendingJob);
|
||||
schedule("0 0 3 * * *", OfflineNodesDeletionJob); // every night at 3:00
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
Logger.tag('jobs').error('Error during scheduling of background jobs:', error);
|
||||
} catch (error) {
|
||||
Logger.tag("jobs").error(
|
||||
"Error during scheduling of background jobs:",
|
||||
error
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
|
||||
Logger.tag('jobs').info('Scheduling of background jobs done.');
|
||||
Logger.tag("jobs").info("Scheduling of background jobs done.");
|
||||
}
|
||||
|
||||
export function getTasks(): Tasks {
|
||||
|
|
|
|||
|
|
@ -1,46 +1,48 @@
|
|||
import {isLogLevel, isUndefined, LoggingConfig, LogLevel, LogLevels} from "./types";
|
||||
import {ActivatableLoggerImpl} from "./logger";
|
||||
import {
|
||||
isLogLevel,
|
||||
isUndefined,
|
||||
LoggingConfig,
|
||||
LogLevel,
|
||||
LogLevels,
|
||||
} from "./types";
|
||||
import { ActivatableLoggerImpl } from "./logger";
|
||||
|
||||
function withDefault<T>(value: T | undefined, defaultValue: T): T {
|
||||
return isUndefined(value) ? defaultValue : value;
|
||||
}
|
||||
|
||||
class TestableLogger extends ActivatableLoggerImpl {
|
||||
private logs: any[][] = [];
|
||||
private logs: unknown[][] = [];
|
||||
|
||||
constructor(
|
||||
enabled?: boolean,
|
||||
debug?: boolean,
|
||||
profile?: boolean,
|
||||
) {
|
||||
constructor(enabled?: boolean, debug?: boolean, profile?: boolean) {
|
||||
super();
|
||||
this.init(
|
||||
new LoggingConfig(
|
||||
withDefault(enabled, true),
|
||||
withDefault(debug, true),
|
||||
withDefault(profile, true),
|
||||
withDefault(profile, true)
|
||||
),
|
||||
(...args: any[]): void => this.doLog(...args)
|
||||
(...args: unknown[]): void => this.doLog(...args)
|
||||
);
|
||||
}
|
||||
|
||||
doLog(...args: any[]): void {
|
||||
doLog(...args: unknown[]): void {
|
||||
this.logs.push(args);
|
||||
}
|
||||
|
||||
getLogs(): any[][] {
|
||||
getLogs(): unknown[][] {
|
||||
return this.logs;
|
||||
}
|
||||
}
|
||||
|
||||
type ParsedLogEntry = {
|
||||
level: LogLevel,
|
||||
tags: string[],
|
||||
message: string,
|
||||
args: any[],
|
||||
level: LogLevel;
|
||||
tags: string[];
|
||||
message: string;
|
||||
args: unknown[];
|
||||
};
|
||||
|
||||
function parseLogEntry(logEntry: any[]): ParsedLogEntry {
|
||||
function parseLogEntry(logEntry: unknown[]): ParsedLogEntry {
|
||||
if (!logEntry.length) {
|
||||
throw new Error(
|
||||
`Empty log entry. Should always start with log message: ${logEntry}`
|
||||
|
|
@ -55,7 +57,8 @@ function parseLogEntry(logEntry: any[]): ParsedLogEntry {
|
|||
}
|
||||
|
||||
// noinspection RegExpRedundantEscape
|
||||
const regexp = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} ([A-Z]+) - (\[[^\]]*\])? *(.*)$/;
|
||||
const regexp =
|
||||
/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} ([A-Z]+) - (\[[^\]]*\])? *(.*)$/;
|
||||
const groups = logMessage.match(regexp);
|
||||
if (groups === null || groups.length < 4) {
|
||||
throw new Error(
|
||||
|
|
@ -71,7 +74,7 @@ function parseLogEntry(logEntry: any[]): ParsedLogEntry {
|
|||
}
|
||||
|
||||
const tagsStr = groups[2].substring(1, groups[2].length - 1);
|
||||
const tags = tagsStr ? tagsStr.split(", "): [];
|
||||
const tags = tagsStr ? tagsStr.split(", ") : [];
|
||||
const message = groups[3];
|
||||
const args = logEntry.slice(1);
|
||||
|
||||
|
|
@ -83,7 +86,7 @@ function parseLogEntry(logEntry: any[]): ParsedLogEntry {
|
|||
};
|
||||
}
|
||||
|
||||
function parseLogs(logs: any[][]): ParsedLogEntry[] {
|
||||
function parseLogs(logs: unknown[][]): ParsedLogEntry[] {
|
||||
const parsedLogs: ParsedLogEntry[] = [];
|
||||
for (const logEntry of logs) {
|
||||
parsedLogs.push(parseLogEntry(logEntry));
|
||||
|
|
@ -100,12 +103,14 @@ for (const level of LogLevels) {
|
|||
logger.tag()[level]("message");
|
||||
|
||||
// then
|
||||
expect(parseLogs(logger.getLogs())).toEqual([{
|
||||
level,
|
||||
tags: [],
|
||||
message: "message",
|
||||
args: [],
|
||||
}]);
|
||||
expect(parseLogs(logger.getLogs())).toEqual([
|
||||
{
|
||||
level,
|
||||
tags: [],
|
||||
message: "message",
|
||||
args: [],
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
test(`should log single tagged ${level} message without parameters`, () => {
|
||||
|
|
@ -116,12 +121,14 @@ for (const level of LogLevels) {
|
|||
logger.tag("tag1", "tag2")[level]("message");
|
||||
|
||||
// then
|
||||
expect(parseLogs(logger.getLogs())).toEqual([{
|
||||
level,
|
||||
tags: ["tag1", "tag2"],
|
||||
message: "message",
|
||||
args: [],
|
||||
}]);
|
||||
expect(parseLogs(logger.getLogs())).toEqual([
|
||||
{
|
||||
level,
|
||||
tags: ["tag1", "tag2"],
|
||||
message: "message",
|
||||
args: [],
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
test(`should log single tagged ${level} message with parameters`, () => {
|
||||
|
|
@ -132,12 +139,14 @@ for (const level of LogLevels) {
|
|||
logger.tag("tag1", "tag2")[level]("message", 1, {}, [false]);
|
||||
|
||||
// then
|
||||
expect(parseLogs(logger.getLogs())).toEqual([{
|
||||
level,
|
||||
tags: ["tag1", "tag2"],
|
||||
message: "message",
|
||||
args: [1, {}, [false]],
|
||||
}]);
|
||||
expect(parseLogs(logger.getLogs())).toEqual([
|
||||
{
|
||||
level,
|
||||
tags: ["tag1", "tag2"],
|
||||
message: "message",
|
||||
args: [1, {}, [false]],
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
test(`should escape tags for ${level} message without parameters`, () => {
|
||||
|
|
@ -148,12 +157,14 @@ for (const level of LogLevels) {
|
|||
logger.tag("%s", "%d", "%f", "%o", "%")[level]("message");
|
||||
|
||||
// then
|
||||
expect(parseLogs(logger.getLogs())).toEqual([{
|
||||
level,
|
||||
tags: ["%%s", "%%d", "%%f", "%%o", "%%"],
|
||||
message: "message",
|
||||
args: [],
|
||||
}]);
|
||||
expect(parseLogs(logger.getLogs())).toEqual([
|
||||
{
|
||||
level,
|
||||
tags: ["%%s", "%%d", "%%f", "%%o", "%%"],
|
||||
message: "message",
|
||||
args: [],
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
test(`should not escape ${level} message itself`, () => {
|
||||
|
|
@ -164,12 +175,14 @@ for (const level of LogLevels) {
|
|||
logger.tag("tag")[level]("%s %d %f %o %%");
|
||||
|
||||
// then
|
||||
expect(parseLogs(logger.getLogs())).toEqual([{
|
||||
level,
|
||||
tags: ["tag"],
|
||||
message: "%s %d %f %o %%",
|
||||
args: [],
|
||||
}]);
|
||||
expect(parseLogs(logger.getLogs())).toEqual([
|
||||
{
|
||||
level,
|
||||
tags: ["tag"],
|
||||
message: "%s %d %f %o %%",
|
||||
args: [],
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
test(`should not escape ${level} message arguments`, () => {
|
||||
|
|
@ -180,12 +193,14 @@ for (const level of LogLevels) {
|
|||
logger.tag("tag")[level]("message", 1, "%s", "%d", "%f", "%o", "%");
|
||||
|
||||
// then
|
||||
expect(parseLogs(logger.getLogs())).toEqual([{
|
||||
level,
|
||||
tags: ["tag"],
|
||||
message: "message",
|
||||
args: [1, "%s", "%d", "%f", "%o", "%"],
|
||||
}]);
|
||||
expect(parseLogs(logger.getLogs())).toEqual([
|
||||
{
|
||||
level,
|
||||
tags: ["tag"],
|
||||
message: "message",
|
||||
args: [1, "%s", "%d", "%f", "%o", "%"],
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
test(`should not log ${level} message on disabled logger`, () => {
|
||||
|
|
@ -219,12 +234,14 @@ test(`should log profile message with disabled debugging`, () => {
|
|||
logger.tag("tag").profile("message");
|
||||
|
||||
// then
|
||||
expect(parseLogs(logger.getLogs())).toEqual([{
|
||||
level: "profile",
|
||||
tags: ["tag"],
|
||||
message: "message",
|
||||
args: [],
|
||||
}]);
|
||||
expect(parseLogs(logger.getLogs())).toEqual([
|
||||
{
|
||||
level: "profile",
|
||||
tags: ["tag"],
|
||||
message: "message",
|
||||
args: [],
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
test(`should not log profile message with disabled profiling`, () => {
|
||||
|
|
@ -246,10 +263,12 @@ test(`should log debug message with disabled profiling`, () => {
|
|||
logger.tag("tag").debug("message");
|
||||
|
||||
// then
|
||||
expect(parseLogs(logger.getLogs())).toEqual([{
|
||||
level: "debug",
|
||||
tags: ["tag"],
|
||||
message: "message",
|
||||
args: [],
|
||||
}]);
|
||||
expect(parseLogs(logger.getLogs())).toEqual([
|
||||
{
|
||||
level: "debug",
|
||||
tags: ["tag"],
|
||||
message: "message",
|
||||
args: [],
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,15 +1,28 @@
|
|||
import {isString, Logger, LoggingConfig, LogLevel, TaggedLogger} from './types';
|
||||
import moment from 'moment';
|
||||
import {
|
||||
isString,
|
||||
Logger,
|
||||
LoggingConfig,
|
||||
LogLevel,
|
||||
TaggedLogger,
|
||||
} from "./types";
|
||||
import moment from "moment";
|
||||
|
||||
export type LoggingFunction = (...args: any[]) => void;
|
||||
export type LoggingFunction = (...args: unknown[]) => void;
|
||||
|
||||
// noinspection JSUnusedLocalSymbols
|
||||
const noopTaggedLogger: TaggedLogger = {
|
||||
log(_level: LogLevel, ..._args: any[]): void {},
|
||||
debug(..._args: any[]): void {},
|
||||
info(..._args: any[]): void {},
|
||||
warn(..._args: any[]): void {},
|
||||
error(..._args: any[]): void {},
|
||||
profile(..._args: any[]): void {},
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/no-empty-function
|
||||
log(level: LogLevel, ...args: unknown[]): void {},
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/no-empty-function
|
||||
debug(...args: unknown[]): void {},
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/no-empty-function
|
||||
info(...args: unknown[]): void {},
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/no-empty-function
|
||||
warn(...args: unknown[]): void {},
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/no-empty-function
|
||||
error(...args: unknown[]): void {},
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars,@typescript-eslint/no-empty-function
|
||||
profile(...args: unknown[]): void {},
|
||||
};
|
||||
|
||||
export interface ActivatableLogger extends Logger {
|
||||
|
|
@ -34,17 +47,20 @@ export class ActivatableLoggerImpl implements ActivatableLogger {
|
|||
const profile = this.config.profile;
|
||||
const loggingFunction = this.loggingFunction;
|
||||
return {
|
||||
log(level: LogLevel, ...args: any[]): void {
|
||||
const timeStr = moment().format('YYYY-MM-DD HH:mm:ss');
|
||||
log(level: LogLevel, ...args: unknown[]): void {
|
||||
const timeStr = moment().format("YYYY-MM-DD HH:mm:ss");
|
||||
const levelStr = level.toUpperCase();
|
||||
const tagsStr = tags ? '[' + tags.join(', ') + ']' : '';
|
||||
const tagsStr = tags ? "[" + tags.join(", ") + "]" : "";
|
||||
const messagePrefix = `${timeStr} ${levelStr} - ${tagsStr}`;
|
||||
|
||||
// Make sure to only replace %s, etc. in real log message
|
||||
// but not in tags.
|
||||
const escapedMessagePrefix = messagePrefix.replace(/%/g, '%%');
|
||||
const escapedMessagePrefix = messagePrefix.replace(
|
||||
/%/g,
|
||||
"%%"
|
||||
);
|
||||
|
||||
let message = '';
|
||||
let message = "";
|
||||
if (args && isString(args[0])) {
|
||||
message = args[0];
|
||||
args.shift();
|
||||
|
|
@ -55,26 +71,26 @@ export class ActivatableLoggerImpl implements ActivatableLogger {
|
|||
: escapedMessagePrefix;
|
||||
loggingFunction(logStr, ...args);
|
||||
},
|
||||
debug(...args: any[]): void {
|
||||
debug(...args: unknown[]): void {
|
||||
if (debug) {
|
||||
this.log('debug', ...args);
|
||||
this.log("debug", ...args);
|
||||
}
|
||||
},
|
||||
info(...args: any[]): void {
|
||||
this.log('info', ...args);
|
||||
info(...args: unknown[]): void {
|
||||
this.log("info", ...args);
|
||||
},
|
||||
warn(...args: any[]): void {
|
||||
this.log('warn', ...args);
|
||||
warn(...args: unknown[]): void {
|
||||
this.log("warn", ...args);
|
||||
},
|
||||
error(...args: any[]): void {
|
||||
this.log('error', ...args);
|
||||
error(...args: unknown[]): void {
|
||||
this.log("error", ...args);
|
||||
},
|
||||
profile(...args: any[]): void {
|
||||
profile(...args: unknown[]): void {
|
||||
if (profile) {
|
||||
this.log('profile', ...args);
|
||||
this.log("profile", ...args);
|
||||
}
|
||||
},
|
||||
}
|
||||
};
|
||||
} else {
|
||||
return noopTaggedLogger;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import {createTransport, Transporter} from "nodemailer";
|
||||
import {config} from "../config";
|
||||
import { createTransport, Transporter } from "nodemailer";
|
||||
import { config } from "../config";
|
||||
import * as MailTemplateService from "../services/mailTemplateService";
|
||||
import Mail from "nodemailer/lib/mailer";
|
||||
import SMTPTransport from "nodemailer/lib/smtp-transport";
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue