1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-11-13 03:35:52 +01:00

refactor: get rid off require('../lib/logger'); (#1040)

refactored `require('../lib/logger');` in `src/api/index.js`, `src/api/middleware.js` and `src/api/storage.js` and `require ('./midderlware')` from `src/api/index.js`
This commit is contained in:
Varij Kapil 2018-10-03 21:35:24 +02:00 committed by Juan Picado @jotadeveloper
parent 06706c857c
commit 385a791237
3 changed files with 15 additions and 18 deletions

@ -21,9 +21,8 @@ import web from './web';
import type { $Application } from 'express';
import type { $ResponseExtend, $RequestExtend, $NextFunctionVer, IStorageHandler, IAuth } from '../../types';
import type { Config as IConfig, IPluginMiddleware } from '@verdaccio/types';
const LoggerApp = require('../lib/logger');
const Middleware = require('./middleware');
import { setup, logger } from '../lib/logger';
import { log, final, errorReportingMiddleware } from './middleware';
const defineAPI = function(config: IConfig, storage: IStorageHandler) {
const auth: IAuth = new Auth(config);
@ -34,8 +33,8 @@ const defineAPI = function(config: IConfig, storage: IStorageHandler) {
app.use(cors());
// Router setup
app.use(Middleware.log);
app.use(Middleware.errorReportingMiddleware);
app.use(log);
app.use(errorReportingMiddleware);
app.use(function(req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer) {
res.setHeader('X-Powered-By', config.user_agent);
next();
@ -56,7 +55,7 @@ const defineAPI = function(config: IConfig, storage: IStorageHandler) {
// register middleware plugins
const plugin_params = {
config: config,
logger: LoggerApp.logger,
logger: logger,
};
const plugins = loadPlugin(config, config.middlewares, plugin_params, function(plugin: IPluginMiddleware) {
return plugin.register_middlewares;
@ -91,7 +90,7 @@ const defineAPI = function(config: IConfig, storage: IStorageHandler) {
if (_.isFunction(res.report_error) === false) {
// in case of very early error this middleware may not be loaded before error is generated
// fixing that
Middleware.errorReportingMiddleware(req, res, _.noop);
errorReportingMiddleware(req, res, _.noop);
}
res.report_error(err);
} else {
@ -100,13 +99,13 @@ const defineAPI = function(config: IConfig, storage: IStorageHandler) {
}
});
app.use(Middleware.final);
app.use(final);
return app;
};
export default (async function(configHash: any) {
LoggerApp.setup(configHash.logs);
setup(configHash.logs);
const config: IConfig = new AppConfig(configHash);
const storage: IStorageHandler = new Storage(config);
// waits until init calls have been intialized

@ -10,8 +10,7 @@ import { API_ERROR, HEADER_TYPE, HEADERS, HTTP_STATUS, TOKEN_BASIC, TOKEN_BEARER
import { stringToMD5 } from '../lib/crypto-utils';
import type { $ResponseExtend, $RequestExtend, $NextFunctionVer, IAuth } from '../../types';
import type { Config } from '@verdaccio/types';
const Logger = require('../lib/logger');
import { logger } from '../lib/logger';
export function match(regexp: RegExp) {
return function(req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer, value: string) {
@ -166,7 +165,7 @@ export function final(body: any, req: $RequestExtend, res: $ResponseExtend, next
export function log(req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer) {
// logger
req.log = Logger.logger.child({ sub: 'in' });
req.log = logger.child({ sub: 'in' });
let _auth = req.headers.authorization;
if (_.isNil(_auth) === false) {
@ -263,9 +262,9 @@ export function errorReportingMiddleware(req: $RequestExtend, res: $ResponseExte
next({ error: err.message || API_ERROR.UNKNOWN_ERROR });
}
} else {
Logger.logger.error({ err: err }, 'unexpected error: @{!err.message}\n@{err.stack}');
logger.error({ err: err }, 'unexpected error: @{!err.message}\n@{err.stack}');
if (!res.status || !res.send) {
Logger.logger.error('this is an error in express.js, please report this');
logger.error('this is an error in express.js, please report this');
res.destroy();
} else if (!res.headersSent) {
res.status(HTTP_STATUS.INTERNAL_ERROR);

@ -20,8 +20,7 @@ import type { IStorage, IProxy, IStorageHandler, ProxyList, StringValue } from '
import type { Versions, Package, Config, MergeTags, Version, DistFile, Callback, Logger } from '@verdaccio/types';
import type { IReadTarball, IUploadTarball } from '@verdaccio/streams';
import { hasProxyTo } from './config-utils';
const LoggerApi = require('../lib/logger');
import { logger } from '../lib/logger';
class Storage implements IStorageHandler {
localStorage: IStorage;
@ -32,11 +31,11 @@ class Storage implements IStorageHandler {
constructor(config: Config) {
this.config = config;
this.uplinks = setupUpLinks(config);
this.logger = LoggerApi.logger.child();
this.logger = logger.child();
}
init(config: Config) {
this.localStorage = new LocalStorage(this.config, LoggerApi.logger);
this.localStorage = new LocalStorage(this.config, logger);
return this.localStorage.getSecret(config);
}