Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const startHRTime = process.hrtime();
// tslint:disable-next-line
export type RequestLoggerEntry = {
height: number
now: number
fromStart: [number, number]
req: {
body: any
headers: any
query: any
url: string
}
};
@Middleware({ type: 'before' })
@injectable()
@IoCSymbol(Symbols.api.utils.requestLogger)
export class RequestLogger implements ExpressMiddlewareInterface {
@inject(Symbols.generic.appConfig)
private appConfig: AppConfig;
@inject(Symbols.helpers.logger)
private logger: ILogger;
@inject(Symbols.modules.blocks)
private blocksModule: IBlocksModule;
private isEnabled: boolean;
private logStream: WriteStream;
private minHeight: number;
private lastBlockHeight = 0;
import * as express from 'express';
import { ExpressErrorMiddlewareInterface, HttpError, Middleware } from 'routing-controllers';
import { Logger, LoggerInterface } from '../../decorators/Logger';
import { env } from '../../env';
@Middleware({ type: 'after' })
export class ErrorHandlerMiddleware implements ExpressErrorMiddlewareInterface {
public isProduction = env.isProduction;
constructor(
@Logger(__filename) private log: LoggerInterface
) { }
public error(error: HttpError, req: express.Request, res: express.Response, next: express.NextFunction): void {
res.status(error.httpCode || 500);
res.json({
name: error.name,
message: error.message,
errors: error[`errors`] || [],
});
import compression from 'compression';
import * as express from 'express';
import { ExpressMiddlewareInterface, Middleware } from 'routing-controllers';
@Middleware({ type: 'before' })
export class CompressionMiddleware implements ExpressMiddlewareInterface {
public use(req: express.Request, res: express.Response, next: express.NextFunction): any {
return compression()(req, res, next);
}
}
import * as express from 'express';
import morgan from 'morgan';
import { ExpressMiddlewareInterface, Middleware } from 'routing-controllers';
import { env } from '../../env';
import { Logger } from '../../lib/logger';
@Middleware({ type: 'before' })
export class LogMiddleware implements ExpressMiddlewareInterface {
private log = new Logger(__dirname);
public use(req: express.Request, res: express.Response, next: express.NextFunction): any {
return morgan(env.log.output, {
stream: {
write: this.log.info.bind(this.log),
},
})(req, res, next);
}
}
import { Request, Response, NextFunction } from 'express';
import { Middleware, ExpressMiddlewareInterface, NotFoundError } from 'routing-controllers';
@Middleware({ type: 'after' })
export default class NotFoundHandler implements ExpressMiddlewareInterface {
public use(req: Request, res: Response, next: NextFunction) {
const { name, message, httpCode } = new NotFoundError(
`Resource '${req.originalUrl}' not found.`,
);
res.status(httpCode).send({
httpCode,
name,
message,
});
next();
}
}
import { KoaMiddlewareInterface, Middleware } from 'routing-controllers'
@Middleware({ type: 'before' })
export class HeaderMiddleware implements KoaMiddlewareInterface {
async use(context: any, next: (err?: any) => any): Promise {
context.set('Access-Control-Allow-Methods', 'GET,HEAD,PUT,POST,DELETE,PATCH')
context.set('Access-Control-Allow-Origin', context.request.header.origin || context.request.origin)
context.set('Access-Control-Allow-Headers', ['content-type'])
context.set('Access-Control-Allow-Credentials', 'true')
context.set('Content-Type', 'application/json; charset=utf-8')
return next()
}
}
import { Middleware, ExpressErrorMiddlewareInterface, HttpError } from 'routing-controllers'
import ApiError from './ApiError'
import { createErrorResponse } from './ApiResponse'
@Middleware({ type: 'after' })
export default class ErrorMiddleware implements ExpressErrorMiddlewareInterface {
error(error: any, request: any, response: any, next: (err: any) => any) {
if (error instanceof HttpError) {
if (response.headersSent) {
return next(error)
}
response.set('Content-Type', 'application/json')
response.status(error.httpCode)
response.json(createErrorResponse(error))
} else {
next(error)
}
}
}
import { env } from 'process';
import { Request, Response, NextFunction } from 'express';
import { Middleware, ExpressMiddlewareInterface } from 'routing-controllers';
import ua, { PageviewParams, Visitor } from 'universal-analytics';
import logger from '../utils/logger';
@Middleware({ type: 'after' })
export default class Analytics implements ExpressMiddlewareInterface {
public use(req: Request, res: Response, next: NextFunction) {
const { headers, path } = req;
logger.info(`Analytics pageview on ${path}`);
if (env.ANALYTICS) {
const visitor: Visitor = ua(env.ANALYTICS);
const options: PageviewParams = {
dp: path,
dh: headers.host,
uip: headers['x-forwarded-for'] || headers['x-real-ip'],
ua: headers['user-agent'],
};
logger.info(`Analytics pageview on ${path}`);
visitor.pageview(options).send(() => next());
}
import * as express from 'express';
import * as helmet from 'helmet';
import { ExpressMiddlewareInterface, Middleware } from 'routing-controllers';
@Middleware({ type: 'before' })
export class SecurityHstsMiddleware implements ExpressMiddlewareInterface {
public use(req: express.Request, res: express.Response, next: express.NextFunction): any {
return helmet.hsts({
maxAge: 31536000,
includeSubdomains: true,
})(req, res, next);
}
}
import * as express from 'express';
import * as helmet from 'helmet';
import { ExpressMiddlewareInterface, Middleware } from 'routing-controllers';
@Middleware({ type: 'before' })
export class SecurityNoCacheMiddleware implements ExpressMiddlewareInterface {
public use(req: express.Request, res: express.Response, next: express.NextFunction): any {
return helmet.noCache()(req, res, next);
}
}