From 53aae6604f1bf3f67b1cb44d918d97c431760dae Mon Sep 17 00:00:00 2001 From: Juergen Kunz Date: Thu, 20 Nov 2025 15:36:45 +0000 Subject: [PATCH] add smartlog dependency and integrate logging into NpmRegistry class --- package.json | 1 + pnpm-lock.yaml | 3 ++ ts/npm/classes.npmregistry.ts | 58 +++++++++++++++++++++++++++++------ ts/plugins.ts | 3 +- 4 files changed, 54 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 8d9c2a4..1bae1d1 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "dependencies": { "@push.rocks/qenv": "^6.1.3", "@push.rocks/smartbucket": "^4.3.0", + "@push.rocks/smartlog": "^3.1.10", "@push.rocks/smartpath": "^6.0.0" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e36606a..9bcb3b5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,6 +14,9 @@ importers: '@push.rocks/smartbucket': specifier: ^4.3.0 version: 4.3.0 + '@push.rocks/smartlog': + specifier: ^3.1.10 + version: 3.1.10 '@push.rocks/smartpath': specifier: ^6.0.0 version: 6.0.0 diff --git a/ts/npm/classes.npmregistry.ts b/ts/npm/classes.npmregistry.ts index 6331aeb..de439f4 100644 --- a/ts/npm/classes.npmregistry.ts +++ b/ts/npm/classes.npmregistry.ts @@ -1,3 +1,4 @@ +import { Smartlog } from '@push.rocks/smartlog'; import { BaseRegistry } from '../core/classes.baseregistry.js'; import { RegistryStorage } from '../core/classes.registrystorage.js'; import { AuthManager } from '../core/classes.authmanager.js'; @@ -23,6 +24,7 @@ export class NpmRegistry extends BaseRegistry { private authManager: AuthManager; private basePath: string = '/npm'; private registryUrl: string; + private logger: Smartlog; constructor( storage: RegistryStorage, @@ -35,6 +37,19 @@ export class NpmRegistry extends BaseRegistry { this.authManager = authManager; this.basePath = basePath; this.registryUrl = registryUrl; + + // Initialize logger + this.logger = new Smartlog({ + logContext: { + company: 'push.rocks', + companyunit: 'smartregistry', + containerName: 'npm-registry', + environment: (process.env.NODE_ENV as any) || 'development', + runtime: 'node', + zone: 'npm' + } + }); + this.logger.enableConsole(); } public async init(): Promise { @@ -47,14 +62,17 @@ export class NpmRegistry extends BaseRegistry { public async handleRequest(context: IRequestContext): Promise { const path = context.path.replace(this.basePath, ''); - console.log(`[NPM handleRequest] method=${context.method}, path=${path}`); // Extract token from Authorization header const authHeader = context.headers['authorization'] || context.headers['Authorization']; const tokenString = authHeader?.replace(/^Bearer\s+/i, ''); - console.log(`[NPM handleRequest] authHeader=${authHeader}, tokenString=${tokenString}`); const token = tokenString ? await this.authManager.validateToken(tokenString, 'npm') : null; - console.log(`[NPM handleRequest] token validated:`, token); + + this.logger.log('debug', `handleRequest: ${context.method} ${path}`, { + method: context.method, + path, + hasAuth: !!token + }); // Registry root if (path === '/' || path === '') { @@ -192,7 +210,12 @@ export class NpmRegistry extends BaseRegistry { query: Record ): Promise { const packument = await this.storage.getNpmPackument(packageName); - console.log(`[getPackument] packageName=${packageName}, versions=`, packument ? Object.keys(packument.versions) : 'null'); + this.logger.log('debug', `getPackument: ${packageName}`, { + packageName, + found: !!packument, + versions: packument ? Object.keys(packument.versions).length : 0 + }); + if (!packument) { return { status: 404, @@ -279,9 +302,16 @@ export class NpmRegistry extends BaseRegistry { body: IPublishRequest, token: IAuthToken | null ): Promise { - console.log(`[publishPackage] packageName=${packageName}, token=`, token); + this.logger.log('info', `publishPackage: ${packageName}`, { + packageName, + versions: Object.keys(body.versions || {}), + hasAuth: !!token + }); + const hasPermission = await this.checkPermission(token, packageName, 'write'); - console.log(`[publishPackage] hasPermission=${hasPermission}`); + if (!hasPermission) { + this.logger.log('warn', `publishPackage: unauthorized`, { packageName, userId: token?.userId }); + } if (!hasPermission) { return { status: 401, @@ -382,9 +412,12 @@ export class NpmRegistry extends BaseRegistry { } // Save packument - console.log(`[publishPackage] Saving packument with versions:`, Object.keys(packument.versions)); await this.storage.putNpmPackument(packageName, packument); - console.log(`[publishPackage] Packument saved successfully`); + this.logger.log('success', `publishPackage: saved ${packageName}`, { + packageName, + versions: Object.keys(packument.versions), + distTags: packument['dist-tags'] + }); return { status: 201, @@ -531,13 +564,14 @@ export class NpmRegistry extends BaseRegistry { const size = parseInt(query.size || '20', 10); const from = parseInt(query.from || '0', 10); + this.logger.log('debug', `handleSearch: query="${text}"`, { text, size, from }); + // Simple search implementation const results: ISearchResult[] = []; try { // List all package paths const packagePaths = await this.storage.listObjects('npm/packages/'); - console.log(`[handleSearch] packagePaths (${packagePaths.length}):`, packagePaths.slice(0, 10)); // Extract unique package names from paths (format: npm/packages/{packageName}/...) const packageNames = new Set(); @@ -547,7 +581,11 @@ export class NpmRegistry extends BaseRegistry { packageNames.add(match[1]); } } - console.log(`[handleSearch] Found ${packageNames.size} packages:`, Array.from(packageNames)); + + this.logger.log('debug', `handleSearch: found ${packageNames.size} packages`, { + totalPackages: packageNames.size, + pathsScanned: packagePaths.length + }); // Load packuments and filter by search text for (const packageName of packageNames) { diff --git a/ts/plugins.ts b/ts/plugins.ts index 8d02592..1e89589 100644 --- a/ts/plugins.ts +++ b/ts/plugins.ts @@ -5,6 +5,7 @@ export { path }; // @push.rocks scope import * as smartbucket from '@push.rocks/smartbucket'; +import * as smartlog from '@push.rocks/smartlog'; import * as smartpath from '@push.rocks/smartpath'; -export { smartbucket, smartpath }; +export { smartbucket, smartlog, smartpath };