0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-16 21:56:25 -05:00

refactor logger module (#3592)

* refactor logger module

* Update index.ts
This commit is contained in:
Juan Picado 2023-02-05 21:00:32 +01:00 committed by GitHub
parent 4122520a19
commit 631abe1ac1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
81 changed files with 643 additions and 635 deletions

View file

@ -0,0 +1,25 @@
---
'@verdaccio/api': minor
'@verdaccio/auth': minor
'@verdaccio/cli': minor
'@verdaccio/types': minor
'@verdaccio/hooks': minor
'@verdaccio/loaders': minor
'@verdaccio/middleware': minor
'@verdaccio/node-api': minor
'verdaccio-audit': minor
'verdaccio-auth-memory': minor
'verdaccio-htpasswd': minor
'@verdaccio/local-storage': minor
'@verdaccio/proxy': minor
'@verdaccio/server': minor
'@verdaccio/server-fastify': minor
'@verdaccio/store': minor
'verdaccio': minor
'@verdaccio/web': minor
'@verdaccio/logger': minor
'@verdaccio/logger-7': minor
'@verdaccio/logger-commons': minor
---
feat: refacor logger

View file

@ -4,13 +4,17 @@ import mime from 'mime';
import { Auth } from '@verdaccio/auth'; import { Auth } from '@verdaccio/auth';
import { constants, errorUtils } from '@verdaccio/core'; import { constants, errorUtils } from '@verdaccio/core';
import { logger } from '@verdaccio/logger';
import { allow, media } from '@verdaccio/middleware'; import { allow, media } from '@verdaccio/middleware';
import { Storage } from '@verdaccio/store'; import { Storage } from '@verdaccio/store';
import { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types/custom'; import { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types/custom';
export default function (route: Router, auth: Auth, storage: Storage): void { export default function (route: Router, auth: Auth, storage: Storage): void {
const can = allow(auth); const can = allow(auth, {
beforeAll: (a, b) => logger.trace(a, b),
afterAll: (a, b) => logger.trace(a, b),
});
const addTagPackageVersionMiddleware = async function ( const addTagPackageVersionMiddleware = async function (
req: $RequestExtend, req: $RequestExtend,
res: $ResponseExtend, res: $ResponseExtend,

View file

@ -3,6 +3,7 @@ import { Router } from 'express';
import { Auth } from '@verdaccio/auth'; import { Auth } from '@verdaccio/auth';
import { HEADERS, HEADER_TYPE, stringUtils } from '@verdaccio/core'; import { HEADERS, HEADER_TYPE, stringUtils } from '@verdaccio/core';
import { logger } from '@verdaccio/logger';
import { allow } from '@verdaccio/middleware'; import { allow } from '@verdaccio/middleware';
import { Storage } from '@verdaccio/store'; import { Storage } from '@verdaccio/store';
@ -11,8 +12,10 @@ import { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types/cust
const debug = buildDebug('verdaccio:api:package'); const debug = buildDebug('verdaccio:api:package');
export default function (route: Router, auth: Auth, storage: Storage): void { export default function (route: Router, auth: Auth, storage: Storage): void {
const can = allow(auth); const can = allow(auth, {
beforeAll: (a, b) => logger.trace(a, b),
afterAll: (a, b) => logger.trace(a, b),
});
route.get( route.get(
'/:package/:version?', '/:package/:version?',
can('access'), can('access'),

View file

@ -93,7 +93,10 @@ const debug = buildDebug('verdaccio:api:publish');
* *
*/ */
export default function publish(router: Router, auth: Auth, storage: Storage): void { export default function publish(router: Router, auth: Auth, storage: Storage): void {
const can = allow(auth); const can = allow(auth, {
beforeAll: (a, b) => logger.trace(a, b),
afterAll: (a, b) => logger.trace(a, b),
});
router.put( router.put(
'/:package', '/:package',
can('publish'), can('publish'),

View file

@ -23,7 +23,7 @@
"path": "../hooks" "path": "../hooks"
}, },
{ {
"path": "../logger" "path": "../logger/logger"
}, },
{ {
"path": "../middleware" "path": "../middleware"

View file

@ -20,7 +20,7 @@
"path": "../loaders" "path": "../loaders"
}, },
{ {
"path": "../logger" "path": "../logger/logger"
}, },
{ {
"path": "../utils" "path": "../utils"

View file

@ -20,7 +20,7 @@
"path": "../node-api" "path": "../node-api"
}, },
{ {
"path": "../logger" "path": "../logger/logger"
} }
] ]
} }

View file

@ -1,4 +1,4 @@
import { PackageAccess, PackageList } from './manifest'; import { PackageAccess, PackageList } from '@verdaccio/types/src/manifest';
export type TypeToken = 'Bearer' | 'Basic'; export type TypeToken = 'Bearer' | 'Basic';

View file

@ -1 +1 @@
export * from './storage'; export * from '@verdaccio/types/src/plugins/storage';

View file

@ -1,5 +1,5 @@
import { Callback, CallbackAction } from '../commons'; import { Callback, CallbackAction } from '@verdaccio/types/src/commons';
import { Manifest, Token } from '../manifest'; import { Manifest, Token } from '@verdaccio/types/src/manifest';
export type StorageList = string[]; export type StorageList = string[];

View file

@ -1,5 +1,5 @@
export * from './plugins'; export * from '@verdaccio/types/src/plugins';
export * from './manifest'; export * from '@verdaccio/types/src/manifest';
export * from './search'; export * from '@verdaccio/types/src/search';
export * from './commons'; export * from '@verdaccio/types/src/commons';
export * from './configuration'; export * from '@verdaccio/types/src/configuration';

View file

@ -14,7 +14,7 @@
"path": "../config" "path": "../config"
}, },
{ {
"path": "../logger" "path": "../logger/logger"
} }
] ]
} }

View file

@ -80,7 +80,9 @@ export async function asyncLoadPlugin<T extends pluginUtils.Plugin<T>>(
await isDirectory(pluginsPath); await isDirectory(pluginsPath);
const pluginDir = pluginsPath; const pluginDir = pluginsPath;
const externalFilePlugin = resolve(pluginDir, `${prefix}-${pluginId}`); const externalFilePlugin = resolve(pluginDir, `${prefix}-${pluginId}`);
let plugin = tryLoad<T>(externalFilePlugin); let plugin = tryLoad<T>(externalFilePlugin, (a: any, b: any) => {
logger.error(a, b);
});
if (plugin && isValid(plugin)) { if (plugin && isValid(plugin)) {
plugin = executePlugin(plugin, pluginConfigs[pluginId], params); plugin = executePlugin(plugin, pluginConfigs[pluginId], params);
if (!sanityCheck(plugin)) { if (!sanityCheck(plugin)) {
@ -106,7 +108,9 @@ export async function asyncLoadPlugin<T extends pluginUtils.Plugin<T>>(
debug('is scoped plugin %s', isScoped); debug('is scoped plugin %s', isScoped);
const pluginName = isScoped ? pluginId : `${prefix}-${pluginId}`; const pluginName = isScoped ? pluginId : `${prefix}-${pluginId}`;
debug('plugin pkg name %s', pluginName); debug('plugin pkg name %s', pluginName);
let plugin = tryLoad<T>(pluginName); let plugin = tryLoad<T>(pluginName, (a: any, b: any) => {
logger.error(a, b);
});
if (plugin && isValid(plugin)) { if (plugin && isValid(plugin)) {
plugin = executePlugin(plugin, pluginConfigs[pluginId], params); plugin = executePlugin(plugin, pluginConfigs[pluginId], params);
if (!sanityCheck(plugin)) { if (!sanityCheck(plugin)) {

View file

@ -2,7 +2,6 @@ import buildDebug from 'debug';
import _ from 'lodash'; import _ from 'lodash';
import { pluginUtils } from '@verdaccio/core'; import { pluginUtils } from '@verdaccio/core';
import { logger } from '@verdaccio/logger';
const debug = buildDebug('verdaccio:plugin:loader:utils'); const debug = buildDebug('verdaccio:plugin:loader:utils');
const MODULE_NOT_FOUND = 'MODULE_NOT_FOUND'; const MODULE_NOT_FOUND = 'MODULE_NOT_FOUND';
@ -23,7 +22,7 @@ export function isES6<T>(plugin: PluginType<T>): boolean {
* @param {*} path the module's path * @param {*} path the module's path
* @return {Object} * @return {Object}
*/ */
export function tryLoad<T>(path: string): PluginType<T> | null { export function tryLoad<T>(path: string, onError: any): PluginType<T> | null {
try { try {
debug('loading plugin %s', path); debug('loading plugin %s', path);
return require(path) as PluginType<T>; return require(path) as PluginType<T>;
@ -32,7 +31,7 @@ export function tryLoad<T>(path: string): PluginType<T> | null {
debug('plugin %s not found', path); debug('plugin %s not found', path);
return null; return null;
} }
logger.error({ err: err.msg }, 'error loading plugin @{err}'); onError({ err: err.msg }, 'error loading plugin @{err}');
throw err; throw err;
} }
} }

View file

@ -15,7 +15,7 @@
"path": "../core/core" "path": "../core/core"
}, },
{ {
"path": "../logger" "path": "../logger/logger"
} }
] ]
} }

View file

@ -1,3 +0,0 @@
{
"extends": "../../.babelrc"
}

View file

@ -1,3 +0,0 @@
{
"extends": "../../.babelrc"
}

View file

@ -0,0 +1,14 @@
{
"extends": "../../../.babelrc",
"presets": [
[
"@babel/env",
{
"targets": {
"node": 12
}
}
],
"@babel/typescript"
]
}

View file

@ -0,0 +1,12 @@
# @verdaccio/logger-7
[![backers](https://opencollective.com/verdaccio/tiers/backer/badge.svg?label=Backer&color=brightgreen)](https://opencollective.com/verdaccio)
[![stackshare](https://img.shields.io/badge/Follow%20on-StackShare-blue.svg?logo=stackshare&style=flat)](https://stackshare.io/verdaccio)
[![MIT](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/verdaccio/verdaccio/blob/master/LICENSE)
[![Crowdin](https://d322cqt584bo4o.cloudfront.net/verdaccio/localized.svg)](https://crowdin.com/project/verdaccio)
[![TODOs](https://badgen.net/https/api.tickgit.com/badgen/github.com/verdaccio/verdaccio)](https://www.tickgit.com/browse?repo=github.com/verdaccio/verdaccio)
[![Twitter followers](https://img.shields.io/twitter/follow/verdaccio_npm.svg?style=social&label=Follow)](https://twitter.com/verdaccio_npm)
[![Github](https://img.shields.io/github/stars/verdaccio/verdaccio.svg?style=social&label=Stars)](https://github.com/verdaccio/verdaccio/stargazers)
Special version of `verdaccio` logger for verdaccio 5.x versions.

View file

@ -1,4 +1,4 @@
const config = require('../../jest/config'); const config = require('../../../jest/config');
module.exports = Object.assign({}, config, { module.exports = Object.assign({}, config, {
coverageThreshold: { coverageThreshold: {

View file

@ -0,0 +1,51 @@
{
"name": "@verdaccio/logger-7",
"version": "6.0.0-6-next.5-1",
"description": "logger for verdaccio 5.x version",
"main": "./build/index.js",
"types": "./build/index.d.ts",
"author": {
"name": "Juan Picado",
"email": "juanpicado19@gmail.com"
},
"repository": {
"type": "https",
"url": "https://github.com/verdaccio/verdaccio"
},
"license": "MIT",
"homepage": "https://verdaccio.org",
"keywords": [
"private",
"package",
"repository",
"registry",
"enterprise",
"modules",
"proxy",
"server",
"verdaccio"
],
"engines": {
"node": ">=12"
},
"scripts": {
"clean": "rimraf ./build",
"test": "echo 1",
"type-check": "tsc --noEmit -p tsconfig.build.json",
"build:types": "tsc --emitDeclarationOnly -p tsconfig.build.json",
"build:js": "babel src/ --out-dir build/ --copy-files --extensions \".ts,.tsx\" --source-maps",
"watch": "pnpm build:js -- --watch",
"build": "pnpm run build:js && pnpm run build:types"
},
"dependencies": {
"@verdaccio/logger-commons": "workspace:6.0.0-6-next.25",
"pino": "7.11.0"
},
"devDependencies": {
"@verdaccio/types": "workspace:11.0.0-6-next.19"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/verdaccio"
}
}

View file

@ -0,0 +1,24 @@
import pino from 'pino';
import { prepareSetup } from '@verdaccio/logger-commons';
import { Logger, LoggerFormat, LoggerType } from '@verdaccio/types';
let logger: Logger;
export type LoggerConfigItem = {
type?: LoggerType;
format?: LoggerFormat;
path?: string;
level?: string;
colors?: boolean;
async?: boolean;
};
export function setup(options: LoggerConfigItem) {
if (typeof logger !== 'undefined') {
return logger;
}
logger = prepareSetup(options, pino);
return logger;
}

View file

@ -1,5 +1,5 @@
{ {
"extends": "../../tsconfig.base", "extends": "../../../tsconfig.base",
"compilerOptions": { "compilerOptions": {
"rootDir": "./src", "rootDir": "./src",
"outDir": "./build" "outDir": "./build"

View file

@ -0,0 +1,16 @@
{
"extends": "../../../tsconfig.reference.json",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./build"
},
"include": ["src/**/*.ts"],
"references": [
{
"path": "../logger-prettify"
},
{
"path": "../logger-commons"
}
]
}

View file

@ -0,0 +1,14 @@
{
"extends": "../../../.babelrc",
"presets": [
[
"@babel/env",
{
"targets": {
"node": 12
}
}
],
"@babel/typescript"
]
}

View file

@ -0,0 +1,10 @@
const config = require('../../../jest/config');
module.exports = Object.assign({}, config, {
coverageThreshold: {
global: {
// FIXME: increase to 90
lines: 39,
},
},
});

View file

@ -1,5 +1,5 @@
{ {
"name": "@verdaccio/logger", "name": "@verdaccio/logger-commons",
"version": "6.0.0-6-next.25", "version": "6.0.0-6-next.25",
"description": "logger", "description": "logger",
"main": "./build/index.js", "main": "./build/index.js",
@ -41,10 +41,10 @@
"@verdaccio/core": "workspace:6.0.0-6-next.57", "@verdaccio/core": "workspace:6.0.0-6-next.57",
"@verdaccio/logger-prettify": "workspace:6.0.0-6-next.8", "@verdaccio/logger-prettify": "workspace:6.0.0-6-next.8",
"debug": "4.3.4", "debug": "4.3.4",
"colorette": "2.0.19", "colorette": "2.0.19"
"pino": "8.8.0"
}, },
"devDependencies": { "devDependencies": {
"pino": "8.8.0",
"@verdaccio/types": "workspace:11.0.0-6-next.19" "@verdaccio/types": "workspace:11.0.0-6-next.19"
}, },
"funding": { "funding": {

View file

@ -0,0 +1 @@
export { setup, createLogger, prepareSetup } from './logger';

View file

@ -1,7 +1,6 @@
// <reference types="node" /> // <reference types="node" />
import { isColorSupported } from 'colorette'; import { isColorSupported } from 'colorette';
import buildDebug from 'debug'; import buildDebug from 'debug';
import pino from 'pino';
import { fillInMsgTemplate } from '@verdaccio/logger-prettify'; import { fillInMsgTemplate } from '@verdaccio/logger-prettify';
import { Logger, LoggerConfigItem, LoggerFormat } from '@verdaccio/types'; import { Logger, LoggerConfigItem, LoggerFormat } from '@verdaccio/types';
@ -33,7 +32,8 @@ export function createLogger(
// eslint-disable-next-line no-undef // eslint-disable-next-line no-undef
// @ts-ignore // @ts-ignore
destination: NodeJS.WritableStream = pino.destination(1), destination: NodeJS.WritableStream = pino.destination(1),
format: LoggerFormat = DEFAULT_LOG_FORMAT format: LoggerFormat = DEFAULT_LOG_FORMAT,
pino
): any { ): any {
debug('setup logger'); debug('setup logger');
let pinoConfig = { let pinoConfig = {
@ -109,7 +109,7 @@ const DEFAULT_LOGGER_CONF: LoggerConfigItem = {
export type LoggerConfig = LoggerConfigItem; export type LoggerConfig = LoggerConfigItem;
export function prepareSetup(options: LoggerConfigItem = DEFAULT_LOGGER_CONF) { export function prepareSetup(options: LoggerConfigItem = DEFAULT_LOGGER_CONF, pino) {
let logger: Logger; let logger: Logger;
let loggerConfig = options; let loggerConfig = options;
if (!loggerConfig?.level) { if (!loggerConfig?.level) {
@ -133,7 +133,8 @@ export function prepareSetup(options: LoggerConfigItem = DEFAULT_LOGGER_CONF) {
{ level: loggerConfig.level, path: loggerConfig.path, colors: loggerConfig.colors }, { level: loggerConfig.level, path: loggerConfig.path, colors: loggerConfig.colors },
// @ts-ignore // @ts-ignore
destination, destination,
loggerConfig.format loggerConfig.format,
pino
); );
return logger; return logger;
} else { } else {
@ -143,7 +144,8 @@ export function prepareSetup(options: LoggerConfigItem = DEFAULT_LOGGER_CONF) {
{ level: loggerConfig.level, colors: loggerConfig.colors }, { level: loggerConfig.level, colors: loggerConfig.colors },
// @ts-ignore // @ts-ignore
pino.destination(1), pino.destination(1),
loggerConfig.format loggerConfig.format,
pino
); );
return logger; return logger;
} }
@ -151,11 +153,11 @@ export function prepareSetup(options: LoggerConfigItem = DEFAULT_LOGGER_CONF) {
export let logger: Logger; export let logger: Logger;
export function setup(options: LoggerConfigItem) { export function setup(options: LoggerConfigItem, pino) {
if (typeof logger !== 'undefined') { if (typeof logger !== 'undefined') {
return logger; return logger;
} }
logger = prepareSetup(options); logger = prepareSetup(options, pino);
return logger; return logger;
} }

View file

@ -1,3 +1,4 @@
import pino from 'pino';
import { Writable } from 'stream'; import { Writable } from 'stream';
import { createLogger } from '../src'; import { createLogger } from '../src';
@ -13,7 +14,7 @@ describe('logger test', () => {
callback(); callback();
}, },
}); });
const logger = createLogger({ level: 'http' }, stream, 'json'); const logger = createLogger({ level: 'http' }, stream, 'json', pino);
logger.info('test'); logger.info('test');
}); });
}); });

View file

@ -1,5 +1,6 @@
import { readFile } from 'fs/promises'; import { readFile } from 'fs/promises';
import { join } from 'path'; import { join } from 'path';
import pino from 'pino';
import { setTimeout } from 'timers/promises'; import { setTimeout } from 'timers/promises';
import { fileUtils } from '@verdaccio/core'; import { fileUtils } from '@verdaccio/core';
@ -27,7 +28,7 @@ describe('logger test', () => {
describe('basic', () => { describe('basic', () => {
test('should include default level', async () => { test('should include default level', async () => {
const file = await createLogFile(); const file = await createLogFile();
const logger = prepareSetup({ type: 'file', path: file, colors: false }); const logger = prepareSetup({ type: 'file', path: file, colors: false }, pino);
logger.info({ packageName: 'test' }, `testing @{packageName}`); logger.info({ packageName: 'test' }, `testing @{packageName}`);
// Note: this should not be logged // Note: this should not be logged
logger.debug(`this should not be logged`); logger.debug(`this should not be logged`);
@ -40,7 +41,10 @@ describe('logger test', () => {
test('should include all logging level', async () => { test('should include all logging level', async () => {
const file = await createLogFile(); const file = await createLogFile();
const logger = prepareSetup({ type: 'file', level: 'trace', path: file, colors: false }); const logger = prepareSetup(
{ type: 'file', level: 'trace', path: file, colors: false },
pino
);
logger.info({ packageName: 'test' }, `testing @{packageName}`); logger.info({ packageName: 'test' }, `testing @{packageName}`);
logger.debug(`this should not be logged`); logger.debug(`this should not be logged`);
logger.trace(`this should not be logged`); logger.trace(`this should not be logged`);
@ -55,13 +59,16 @@ describe('logger test', () => {
describe('json format', () => { describe('json format', () => {
test('should log into a file with json format', async () => { test('should log into a file with json format', async () => {
const file = await createLogFile(); const file = await createLogFile();
const logger = prepareSetup({ const logger = prepareSetup(
...defaultOptions, {
format: 'json', ...defaultOptions,
type: 'file', format: 'json',
path: file, type: 'file',
level: 'info', path: file,
}); level: 'info',
},
pino
);
logger.info( logger.info(
{ packageName: 'test' }, { packageName: 'test' },
`publishing or updating a new version for @{packageName}` `publishing or updating a new version for @{packageName}`
@ -79,13 +86,16 @@ describe('logger test', () => {
describe('pretty format', () => { describe('pretty format', () => {
test('should log into a file with pretty', async () => { test('should log into a file with pretty', async () => {
const file = await createLogFile(); const file = await createLogFile();
const logger = prepareSetup({ const logger = prepareSetup(
format: 'pretty', {
type: 'file', format: 'pretty',
path: file, type: 'file',
level: 'trace', path: file,
colors: false, level: 'trace',
}); colors: false,
},
pino
);
logger.info( logger.info(
{ packageName: 'test' }, { packageName: 'test' },
`publishing or updating a new version for @{packageName}` `publishing or updating a new version for @{packageName}`
@ -96,13 +106,16 @@ describe('logger test', () => {
test('should log into a file with pretty-timestamped', async () => { test('should log into a file with pretty-timestamped', async () => {
const file = await createLogFile(); const file = await createLogFile();
const logger = prepareSetup({ const logger = prepareSetup(
format: 'pretty-timestamped', {
type: 'file', format: 'pretty-timestamped',
path: file, type: 'file',
level: 'trace', path: file,
colors: false, level: 'trace',
}); colors: false,
},
pino
);
logger.info( logger.info(
{ packageName: 'test' }, { packageName: 'test' },
`publishing or updating a new version for @{packageName}` `publishing or updating a new version for @{packageName}`

View file

@ -1,5 +1,5 @@
{ {
"extends": "../../tsconfig.base.json", "extends": "../../../tsconfig.base",
"compilerOptions": { "compilerOptions": {
"rootDir": "./src", "rootDir": "./src",
"outDir": "./build" "outDir": "./build"

View file

@ -1,5 +1,5 @@
{ {
"extends": "../../tsconfig.reference.json", "extends": "../../../tsconfig.reference.json",
"compilerOptions": { "compilerOptions": {
"rootDir": "./src", "rootDir": "./src",
"outDir": "./build" "outDir": "./build"

View file

@ -0,0 +1,14 @@
{
"extends": "../../../.babelrc",
"presets": [
[
"@babel/env",
{
"targets": {
"node": 12
}
}
],
"@babel/typescript"
]
}

View file

@ -1,4 +1,4 @@
const config = require('../../jest/config'); const config = require('../../../jest/config');
module.exports = Object.assign({}, config, { module.exports = Object.assign({}, config, {
coverageThreshold: { coverageThreshold: {

View file

@ -0,0 +1,9 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./build"
},
"include": ["src/**/*.ts"],
"exclude": ["src/**/*.test.ts"]
}

View file

@ -0,0 +1,9 @@
{
"extends": "../../../tsconfig.reference",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./build"
},
"include": ["src/**/*.ts"],
"exclude": ["src/**/*.test.ts"]
}

View file

@ -0,0 +1,3 @@
{
"extends": "../../../.babelrc"
}

View file

@ -0,0 +1,75 @@
# @verdaccio/logger
[![backers](https://opencollective.com/verdaccio/tiers/backer/badge.svg?label=Backer&color=brightgreen)](https://opencollective.com/verdaccio)
[![stackshare](https://img.shields.io/badge/Follow%20on-StackShare-blue.svg?logo=stackshare&style=flat)](https://stackshare.io/verdaccio)
[![MIT](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/verdaccio/verdaccio/blob/master/LICENSE)
[![Crowdin](https://d322cqt584bo4o.cloudfront.net/verdaccio/localized.svg)](https://crowdin.com/project/verdaccio)
[![TODOs](https://badgen.net/https/api.tickgit.com/badgen/github.com/verdaccio/verdaccio)](https://www.tickgit.com/browse?repo=github.com/verdaccio/verdaccio)
[![Twitter followers](https://img.shields.io/twitter/follow/verdaccio_npm.svg?style=social&label=Follow)](https://twitter.com/verdaccio_npm)
[![Github](https://img.shields.io/github/stars/verdaccio/verdaccio.svg?style=social&label=Stars)](https://github.com/verdaccio/verdaccio/stargazers)
## Donations
Verdaccio is run by **volunteers**; nobody is working full-time on it. If you find this project to be useful and would like to support its development, consider making a donation - **your logo might end up in this readme.** 😉
**[Donate](https://opencollective.com/verdaccio)** 💵👍🏻 starting from _\$1/month_ or just one single contribution.
## Report a vulnerability
If you want to report a security vulnerability, please follow the steps which we have defined for you in our [security policy](https://github.com/verdaccio/verdaccio/security/policy).
## Open Collective Sponsors
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/verdaccio#sponsor)]
[![sponsor](https://opencollective.com/verdaccio/sponsor/0/avatar.svg)](https://opencollective.com/verdaccio/sponsor/0/website)
[![sponsor](https://opencollective.com/verdaccio/sponsor/1/avatar.svg)](https://opencollective.com/verdaccio/sponsor/1/website)
[![sponsor](https://opencollective.com/verdaccio/sponsor/2/avatar.svg)](https://opencollective.com/verdaccio/sponsor/2/website)
[![sponsor](https://opencollective.com/verdaccio/sponsor/3/avatar.svg)](https://opencollective.com/verdaccio/sponsor/3/website)
[![sponsor](https://opencollective.com/verdaccio/sponsor/4/avatar.svg)](https://opencollective.com/verdaccio/sponsor/4/website)
[![sponsor](https://opencollective.com/verdaccio/sponsor/5/avatar.svg)](https://opencollective.com/verdaccio/sponsor/5/website)
[![sponsor](https://opencollective.com/verdaccio/sponsor/6/avatar.svg)](https://opencollective.com/verdaccio/sponsor/6/website)
[![sponsor](https://opencollective.com/verdaccio/sponsor/7/avatar.svg)](https://opencollective.com/verdaccio/sponsor/7/website)
[![sponsor](https://opencollective.com/verdaccio/sponsor/8/avatar.svg)](https://opencollective.com/verdaccio/sponsor/8/website)
[![sponsor](https://opencollective.com/verdaccio/sponsor/9/avatar.svg)](https://opencollective.com/verdaccio/sponsor/9/website)
## Open Collective Backers
Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/verdaccio#backer)]
[![backers](https://opencollective.com/verdaccio/backers.svg?width=890)](https://opencollective.com/verdaccio#backers)
## Special Thanks
Thanks to the following companies to help us to achieve our goals providing free open source licenses.
[![jetbrain](assets/thanks/jetbrains/logo.png)](https://www.jetbrains.com/)
[![crowdin](assets/thanks/crowdin/logo.png)](https://crowdin.com/)
[![balsamiq](assets/thanks/balsamiq/logo.jpg)](https://balsamiq.com/)
## Contributors
This project exists thanks to all the people who contribute. [[Contribute](CONTRIBUTING.md)].
[![contributors](https://opencollective.com/verdaccio/contributors.svg?width=890&button=true)](../../graphs/contributors)
### FAQ / Contact / Troubleshoot
If you have any issue you can try the following options, do no desist to ask or check our issues database, perhaps someone has asked already what you are looking for.
- [Blog](https://verdaccio.org/blog/)
- [Donations](https://opencollective.com/verdaccio)
- [Reporting an issue](https://github.com/verdaccio/verdaccio/blob/master/CONTRIBUTING.md#reporting-a-bug)
- [Running discussions](https://github.com/verdaccio/verdaccio/issues?q=is%3Aissue+is%3Aopen+label%3Adiscuss)
- [Chat](http://chat.verdaccio.org/)
- [Logos](https://verdaccio.org/docs/en/logo)
- [Docker Examples](https://github.com/verdaccio/docker-examples)
- [FAQ](https://github.com/verdaccio/verdaccio/issues?utf8=%E2%9C%93&q=is%3Aissue%20label%3Aquestion%20)
### License
Verdaccio is [MIT licensed](https://github.com/verdaccio/verdaccio/blob/master/LICENSE)
The Verdaccio documentation and logos (excluding /thanks, e.g., .md, .png, .sketch) files within the /assets folder) is
[Creative Commons licensed](https://github.com/verdaccio/verdaccio/blob/master/LICENSE-docs).

View file

@ -0,0 +1,10 @@
const config = require('../../../jest/config');
module.exports = Object.assign({}, config, {
coverageThreshold: {
global: {
// FIXME: increase to 90
lines: 39,
},
},
});

View file

@ -0,0 +1,51 @@
{
"name": "@verdaccio/logger",
"version": "6.0.0-6-next.25",
"description": "logger",
"main": "./build/index.js",
"types": "./build/index.d.ts",
"author": {
"name": "Juan Picado",
"email": "juanpicado19@gmail.com"
},
"repository": {
"type": "https",
"url": "https://github.com/verdaccio/verdaccio"
},
"license": "MIT",
"homepage": "https://verdaccio.org",
"keywords": [
"private",
"package",
"repository",
"registry",
"enterprise",
"modules",
"proxy",
"server",
"verdaccio"
],
"engines": {
"node": ">=12"
},
"scripts": {
"clean": "rimraf ./build",
"test": "echo 1",
"type-check": "tsc --noEmit -p tsconfig.build.json",
"build:types": "tsc --emitDeclarationOnly -p tsconfig.build.json",
"build:js": "babel src/ --out-dir build/ --copy-files --extensions \".ts,.tsx\" --source-maps",
"watch": "pnpm build:js -- --watch",
"build": "pnpm run build:js && pnpm run build:types"
},
"dependencies": {
"@verdaccio/logger-commons": "workspace:6.0.0-6-next.25",
"pino": "8.8.0"
},
"devDependencies": {
"@verdaccio/types": "workspace:11.0.0-6-next.19"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/verdaccio"
}
}

View file

@ -0,0 +1,15 @@
import pino from 'pino';
import { prepareSetup } from '@verdaccio/logger-commons';
import { Logger, LoggerConfigItem } from '@verdaccio/types';
export function setup(options: LoggerConfigItem) {
if (typeof logger !== 'undefined') {
return logger;
}
logger = prepareSetup(options, pino);
return logger;
}
export let logger: Logger;

View file

@ -1,5 +1,5 @@
{ {
"extends": "../../tsconfig.reference", "extends": "../../../tsconfig.base",
"compilerOptions": { "compilerOptions": {
"rootDir": "./src", "rootDir": "./src",
"outDir": "./build" "outDir": "./build"

View file

@ -0,0 +1,16 @@
{
"extends": "../../../tsconfig.reference.json",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./build"
},
"include": ["src/**/*.ts"],
"references": [
{
"path": "../logger-prettify"
},
{
"path": "../logger-commons"
}
]
}

View file

@ -1 +0,0 @@
export { setup, createLogger, logger, prepareSetup } from './logger';

View file

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

View file

@ -1,10 +1,18 @@
import { API_ERROR, errorUtils } from '@verdaccio/core'; import { API_ERROR, errorUtils } from '@verdaccio/core';
import { logger } from '@verdaccio/logger';
import { getVersionFromTarball } from '@verdaccio/utils'; import { getVersionFromTarball } from '@verdaccio/utils';
import { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types'; import { $NextFunctionVer, $RequestExtend, $ResponseExtend } from '../types';
export function allow<T>(auth: T): Function { export function allow<T>(
auth: T,
options = {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
beforeAll: (_a: any, _b: any) => {},
// eslint-disable-next-line @typescript-eslint/no-unused-vars
afterAll: (_a: any, _b: any) => {},
}
): Function {
const { beforeAll, afterAll } = options;
return function (action: string): Function { return function (action: string): Function {
return function (req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer): void { return function (req: $RequestExtend, res: $ResponseExtend, next: $NextFunctionVer): void {
req.pause(); req.pause();
@ -15,7 +23,7 @@ export function allow<T>(auth: T): Function {
? getVersionFromTarball(req.params.filename) ? getVersionFromTarball(req.params.filename)
: undefined; : undefined;
const remote = req.remote_user; const remote = req.remote_user;
logger.trace( beforeAll?.(
{ action, user: remote?.name }, { action, user: remote?.name },
`[middleware/allow][@{action}] allow for @{user}` `[middleware/allow][@{action}] allow for @{user}`
); );
@ -27,6 +35,10 @@ export function allow<T>(auth: T): Function {
if (error) { if (error) {
next(error); next(error);
} else if (allowed) { } else if (allowed) {
afterAll?.(
{ action, user: remote?.name },
`[middleware/allow][@{action}] allowed for @{user}`
);
next(); next();
} else { } else {
// last plugin (that's our built-in one) returns either // last plugin (that's our built-in one) returns either

View file

@ -1,7 +1,7 @@
import request from 'supertest'; import request from 'supertest';
import { HTTP_STATUS } from '@verdaccio/core'; import { HTTP_STATUS } from '@verdaccio/core';
import { setup } from '@verdaccio/logger'; import { logger, setup } from '@verdaccio/logger';
import { allow } from '../src'; import { allow } from '../src';
import { getApp } from './helper'; import { getApp } from './helper';
@ -9,11 +9,14 @@ import { getApp } from './helper';
setup({}); setup({});
test('should allow request', async () => { test('should allow request', async () => {
const can = allow({ const can = allow(
allow_publish: (params, remove, cb) => { {
return cb(null, true); allow_publish: (params, remove, cb) => {
return cb(null, true);
},
}, },
}); logger
);
const app = getApp([]); const app = getApp([]);
// @ts-ignore // @ts-ignore
app.get('/:package', can('publish'), (req, res) => { app.get('/:package', can('publish'), (req, res) => {
@ -24,11 +27,14 @@ test('should allow request', async () => {
}); });
test('should allow scope request', async () => { test('should allow scope request', async () => {
const can = allow({ const can = allow(
allow_publish: (params, remove, cb) => { {
return cb(null, true); allow_publish: (params, remove, cb) => {
return cb(null, true);
},
}, },
}); logger
);
const app = getApp([]); const app = getApp([]);
// @ts-ignore // @ts-ignore
app.get('/:package/:scope', can('publish'), (req, res) => { app.get('/:package/:scope', can('publish'), (req, res) => {
@ -39,11 +45,14 @@ test('should allow scope request', async () => {
}); });
test('should allow filename request', async () => { test('should allow filename request', async () => {
const can = allow({ const can = allow(
allow_publish: (params, remove, cb) => { {
return cb(null, true); allow_publish: (params, remove, cb) => {
return cb(null, true);
},
}, },
}); logger
);
const app = getApp([]); const app = getApp([]);
// @ts-ignore // @ts-ignore
app.get('/:filename', can('publish'), (req, res) => { app.get('/:filename', can('publish'), (req, res) => {
@ -54,11 +63,14 @@ test('should allow filename request', async () => {
}); });
test('should not allow request', async () => { test('should not allow request', async () => {
const can = allow({ const can = allow(
allow_publish: (params, remove, cb) => { {
return cb(null, false); allow_publish: (params, remove, cb) => {
return cb(null, false);
},
}, },
}); logger
);
const app = getApp([]); const app = getApp([]);
// @ts-ignore // @ts-ignore
app.get('/sec', can('publish'), (req, res) => { app.get('/sec', can('publish'), (req, res) => {
@ -69,11 +81,14 @@ test('should not allow request', async () => {
}); });
test('should handle error request', async () => { test('should handle error request', async () => {
const can = allow({ const can = allow(
allow_publish: (params, remove, cb) => { {
return cb(Error('foo error')); allow_publish: (params, remove, cb) => {
return cb(Error('foo error'));
},
}, },
}); logger
);
const app = getApp([]); const app = getApp([]);
// @ts-ignore // @ts-ignore
app.get('/err', can('publish')); app.get('/err', can('publish'));

View file

@ -11,7 +11,7 @@
"path": "../auth" "path": "../auth"
}, },
{ {
"path": "../logger" "path": "../logger/logger"
}, },
{ {
"path": "../utils" "path": "../utils"

View file

@ -11,7 +11,7 @@
"path": "../config" "path": "../config"
}, },
{ {
"path": "../logger" "path": "../logger/logger"
}, },
{ {
"path": "../server/express" "path": "../server/express"

View file

@ -11,7 +11,7 @@
"path": "../../config" "path": "../../config"
}, },
{ {
"path": "../../logger" "path": "../../logger/logger"
}, },
{ {
"path": "../../core/core" "path": "../../core/core"

View file

@ -14,7 +14,7 @@
"path": "../../auth" "path": "../../auth"
}, },
{ {
"path": "../../logger" "path": "../../logger/logger"
}, },
{ {
"path": "../../core/core" "path": "../../core/core"

View file

@ -12,7 +12,7 @@
"path": "../../config" "path": "../../config"
}, },
{ {
"path": "../../logger" "path": "../../logger/logger"
}, },
{ {
"path": "../../core/core" "path": "../../core/core"

View file

@ -14,7 +14,7 @@
"path": "../../config" "path": "../../config"
}, },
{ {
"path": "../../logger" "path": "../../logger/logger"
}, },
{ {
"path": "../../core/core" "path": "../../core/core"

View file

@ -13,7 +13,7 @@
"path": "../../config" "path": "../../config"
}, },
{ {
"path": "../../logger" "path": "../../logger/logger"
}, },
{ {
"path": "../../core/core" "path": "../../core/core"

View file

@ -14,7 +14,7 @@
"path": "../core/core" "path": "../core/core"
}, },
{ {
"path": "../logger" "path": "../logger/logger"
}, },
{ {
"path": "../utils" "path": "../utils"

View file

@ -20,7 +20,7 @@
"path": "../../loaders" "path": "../../loaders"
}, },
{ {
"path": "../../logger" "path": "../../logger/logger"
}, },
{ {
"path": "../../middleware" "path": "../../middleware"

View file

@ -17,7 +17,7 @@
"path": "../../auth" "path": "../../auth"
}, },
{ {
"path": "../../logger" "path": "../../logger/logger"
}, },
{ {
"path": "../../utils" "path": "../../utils"

View file

@ -26,7 +26,7 @@
"path": "../loaders" "path": "../loaders"
}, },
{ {
"path": "../logger" "path": "../logger/logger"
}, },
{ {
"path": "../proxy" "path": "../proxy"

View file

@ -26,7 +26,7 @@
"path": "../hooks" "path": "../hooks"
}, },
{ {
"path": "../logger" "path": "../logger/logger"
}, },
{ {
"path": "../node-api" "path": "../node-api"

View file

@ -3,6 +3,7 @@ import { Router } from 'express';
import { Auth } from '@verdaccio/auth'; import { Auth } from '@verdaccio/auth';
import { HEADERS, HEADER_TYPE } from '@verdaccio/core'; import { HEADERS, HEADER_TYPE } from '@verdaccio/core';
import { logger } from '@verdaccio/logger';
import { $NextFunctionVer, $RequestExtend, $ResponseExtend, allow } from '@verdaccio/middleware'; import { $NextFunctionVer, $RequestExtend, $ResponseExtend, allow } from '@verdaccio/middleware';
import { Storage } from '@verdaccio/store'; import { Storage } from '@verdaccio/store';
import { Manifest } from '@verdaccio/types'; import { Manifest } from '@verdaccio/types';
@ -28,7 +29,10 @@ const getReadme = (readme) => {
function addReadmeWebApi(storage: Storage, auth: Auth): Router { function addReadmeWebApi(storage: Storage, auth: Auth): Router {
debug('initialized readme web api'); debug('initialized readme web api');
const can = allow(auth); const can = allow(auth, {
beforeAll: (a, b) => logger.trace(a, b),
afterAll: (a, b) => logger.trace(a, b),
});
const pkgRouter = Router(); /* eslint new-cap: 0 */ const pkgRouter = Router(); /* eslint new-cap: 0 */
pkgRouter.get( pkgRouter.get(

View file

@ -3,6 +3,7 @@ import { Router } from 'express';
import { Auth } from '@verdaccio/auth'; import { Auth } from '@verdaccio/auth';
import { DIST_TAGS, HTTP_STATUS } from '@verdaccio/core'; import { DIST_TAGS, HTTP_STATUS } from '@verdaccio/core';
import { logger } from '@verdaccio/logger';
import { $NextFunctionVer, $RequestExtend, $ResponseExtend, allow } from '@verdaccio/middleware'; import { $NextFunctionVer, $RequestExtend, $ResponseExtend, allow } from '@verdaccio/middleware';
import { Storage } from '@verdaccio/store'; import { Storage } from '@verdaccio/store';
import { convertDistRemoteToLocalTarballUrls } from '@verdaccio/tarball'; import { convertDistRemoteToLocalTarballUrls } from '@verdaccio/tarball';
@ -21,7 +22,10 @@ const debug = buildDebug('verdaccio:web:api:sidebar');
function addSidebarWebApi(config: Config, storage: Storage, auth: Auth): Router { function addSidebarWebApi(config: Config, storage: Storage, auth: Auth): Router {
debug('initialized sidebar web api'); debug('initialized sidebar web api');
const router = Router(); /* eslint new-cap: 0 */ const router = Router(); /* eslint new-cap: 0 */
const can = allow(auth); const can = allow(auth, {
beforeAll: (a, b) => logger.trace(a, b),
afterAll: (a, b) => logger.trace(a, b),
});
// Get package readme // Get package readme
router.get( router.get(
'/sidebar/(@:scope/)?:package', '/sidebar/(@:scope/)?:package',

View file

@ -13,9 +13,6 @@
{ {
"path": "../config" "path": "../config"
}, },
{
"path": "../core/readme"
},
{ {
"path": "../core/tarball" "path": "../core/tarball"
}, },
@ -29,7 +26,7 @@
"path": "../api" "path": "../api"
}, },
{ {
"path": "../logger" "path": "../logger/logger"
}, },
{ {
"path": "../middleware" "path": "../middleware"

File diff suppressed because it is too large Load diff

View file

@ -4,6 +4,7 @@ packages:
- packages/tools/* - packages/tools/*
- packages/ui/* - packages/ui/*
- packages/server/* - packages/server/*
- packages/logger/*
- packages/plugins/audit - packages/plugins/audit
- packages/plugins/auth-memory - packages/plugins/auth-memory
- packages/plugins/htpasswd - packages/plugins/htpasswd