0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-03-11 02:15:57 -05:00

refactor middleware dependencies (#3588)

* refactor middleware dependencies

* improve wrap

* chore: fix local

* chore: fix test

* changeset
This commit is contained in:
Juan Picado 2023-02-04 11:34:33 +01:00 committed by GitHub
parent 7abfb6aa3d
commit 9943e2b189
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 177 additions and 657 deletions

View file

@ -0,0 +1,8 @@
---
'@verdaccio/middleware': patch
'@verdaccio/server': patch
'@verdaccio/test-helper': patch
'@verdaccio/local-publish': patch
---
fix: extract logger from middleware

View file

@ -157,7 +157,8 @@
"crowdin:sync": "pnpm crowdin:upload && pnpm crowdin:download --verbose",
"postinstall": "husky install",
"local:registry": "pnpm start --filter ...@verdaccio/local-publish",
"local:publish": "cross-env npm_config_registry=http://localhost:4873 pnpm ci:publish",
"local:snapshots": "changeset version --snapshot",
"local:publish": "cross-env npm_config_registry=http://localhost:4873 pnpm ci:publish -- --no-git-tag",
"local:publish:release": "concurrently \"pnpm local:registry\" \"pnpm local:publish\""
},
"pnpm": {

View file

@ -4,7 +4,7 @@ module.exports = Object.assign({}, config, {
coverageThreshold: {
global: {
lines: 67,
functions: 80,
functions: 75,
branches: 56,
statements: 67,
},

View file

@ -26,8 +26,7 @@
"verdaccio"
],
"engines": {
"node": ">=14",
"npm": ">=6"
"node": ">=12"
},
"scripts": {
"clean": "rimraf ./build",
@ -40,7 +39,6 @@
},
"dependencies": {
"@verdaccio/core": "workspace:6.0.0-6-next.56",
"@verdaccio/logger": "workspace:6.0.0-6-next.24",
"@verdaccio/utils": "workspace:6.0.0-6-next.24",
"debug": "4.3.4",
"lodash": "4.17.21",
@ -51,6 +49,7 @@
"url": "https://opencollective.com/verdaccio"
},
"devDependencies": {
"@verdaccio/logger": "workspace:6.0.0-6-next.24",
"body-parser": "1.20.1",
"supertest": "6.3.3"
}

View file

@ -3,13 +3,13 @@ import { HttpError } from 'http-errors';
import _ from 'lodash';
import { API_ERROR, HTTP_STATUS, VerdaccioError } from '@verdaccio/core';
import { logger } from '@verdaccio/logger';
import { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types';
const debug = buildDebug('verdaccio:middleware:error');
export function handleError(
export const handleError = (logger) =>
function handleError(
err: HttpError,
req: $RequestExtend,
res: $ResponseExtend,
@ -25,7 +25,7 @@ export function handleError(
debug('is locals error report ref');
// in case of very early error this middleware may not be loaded before error is generated
// fixing that
errorReportingMiddleware(req, res, _.noop);
errorReportingMiddleware(logger)(req, res, _.noop);
}
debug('set locals error report ref');
res.locals.report_error(err);
@ -34,10 +34,11 @@ export function handleError(
debug('no error to report, jump next layer');
return next(err);
}
}
};
// Middleware
export function errorReportingMiddleware(
export const errorReportingMiddleware = (logger) =>
function errorReportingMiddleware(
req: $RequestExtend,
res: $ResponseExtend,
next: $NextFunctionVer
@ -75,4 +76,4 @@ export function errorReportingMiddleware(
debug('error report middleware next()');
next();
}
};

View file

@ -1,7 +1,5 @@
import _ from 'lodash';
import { logger } from '@verdaccio/logger';
import { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types';
// FIXME: deprecated, moved to @verdaccio/dev-commons
@ -10,7 +8,8 @@ export const LOG_STATUS_MESSAGE =
export const LOG_VERDACCIO_ERROR = `${LOG_STATUS_MESSAGE}, error: @{!error}`;
export const LOG_VERDACCIO_BYTES = `${LOG_STATUS_MESSAGE}, bytes: @{bytes.in}/@{bytes.out}`;
export function log(req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer): void {
export const log = (logger) => {
return function log(req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer): void {
// logger
req.log = logger.child({ sub: 'in' });
@ -100,4 +99,5 @@ export function log(req: $RequestExtend, res: $ResponseExtend, next: $NextFuncti
log();
};
next();
}
};
};

View file

@ -2,7 +2,7 @@ import path from 'path';
import request from 'supertest';
import { HTTP_STATUS } from '@verdaccio/core';
import { setup } from '@verdaccio/logger';
import { logger, setup } from '@verdaccio/logger';
import { log } from '../src';
import { getApp } from './helper';
@ -17,7 +17,7 @@ setup({
test('should log request', async () => {
const app = getApp([]);
// @ts-ignore
app.use(log);
app.use(log(logger));
// @ts-ignore
app.get('/:package', (req, res) => {
res.status(HTTP_STATUS.OK).json({});

View file

@ -36,9 +36,11 @@ const defineAPI = async function (config: IConfig, storage: Storage): Promise<an
app.use(cors());
app.use(limiter);
const errorReportingMiddlewareWrap = errorReportingMiddleware(logger);
// Router setup
app.use(log);
app.use(errorReportingMiddleware);
app.use(log(logger));
app.use(errorReportingMiddlewareWrap);
app.use(function (req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer): void {
res.setHeader('x-powered-by', getUserAgent(config.user_agent));
next();
@ -111,7 +113,7 @@ const defineAPI = async function (config: IConfig, storage: Storage): Promise<an
if (_.isFunction(res.locals.report_error) === false) {
// in case of very early error this middleware may not be loaded before error is generated
// fixing that
errorReportingMiddleware(req, res, _.noop);
errorReportingMiddlewareWrap(req, res, _.noop);
}
res.locals.report_error(err);
} else {

View file

@ -14,6 +14,7 @@
"@verdaccio/core": "workspace:6.0.0-6-next.56",
"@verdaccio/config": "workspace:6.0.0-6-next.56",
"@verdaccio/middleware": "workspace:6.0.0-6-next.35",
"@verdaccio/logger": "workspace:6.0.0-6-next.24",
"@verdaccio/utils": "workspace:6.0.0-6-next.24",
"body-parser": "1.20.1",
"express": "4.18.2",

View file

@ -7,6 +7,7 @@ import path from 'path';
import { Auth } from '@verdaccio/auth';
import { Config } from '@verdaccio/config';
import { errorUtils } from '@verdaccio/core';
import { logger } from '@verdaccio/logger';
import { errorReportingMiddleware, final, handleError } from '@verdaccio/middleware';
import { generateRandomHexString } from '@verdaccio/utils';
@ -31,7 +32,7 @@ export async function initializeServer(
// TODO: this might not be need it, used in apiEndpoints
app.use(bodyParser.json({ strict: false, limit: '10mb' }));
// @ts-ignore
app.use(errorReportingMiddleware);
app.use(errorReportingMiddleware(logger));
for (let route of routesMiddleware) {
if (route.async) {
const middleware = await route.routes(config, auth, storage);
@ -47,7 +48,7 @@ export async function initializeServer(
});
// @ts-ignore
app.use(handleError);
app.use(handleError(logger));
// @ts-ignore
app.use(final);

View file

@ -12,7 +12,7 @@ fileUtils
logs: { level: 'info', type: 'stdout', format: 'pretty' },
uplinks: {},
packages: {},
self_path: folderPath,
configPath: folderPath,
})
.addUplink('npmjs', { url: 'https://registry.npmjs.org' })
.addPackageAccess('@verdaccio/*', {

505
pnpm-lock.yaml generated

File diff suppressed because it is too large Load diff