add smartlog dependency and integrate logging into NpmRegistry class

This commit is contained in:
2025-11-20 15:36:45 +00:00
parent 057383fb7c
commit 53aae6604f
4 changed files with 54 additions and 11 deletions

View File

@@ -46,6 +46,7 @@
"dependencies": { "dependencies": {
"@push.rocks/qenv": "^6.1.3", "@push.rocks/qenv": "^6.1.3",
"@push.rocks/smartbucket": "^4.3.0", "@push.rocks/smartbucket": "^4.3.0",
"@push.rocks/smartlog": "^3.1.10",
"@push.rocks/smartpath": "^6.0.0" "@push.rocks/smartpath": "^6.0.0"
} }
} }

3
pnpm-lock.yaml generated
View File

@@ -14,6 +14,9 @@ importers:
'@push.rocks/smartbucket': '@push.rocks/smartbucket':
specifier: ^4.3.0 specifier: ^4.3.0
version: 4.3.0 version: 4.3.0
'@push.rocks/smartlog':
specifier: ^3.1.10
version: 3.1.10
'@push.rocks/smartpath': '@push.rocks/smartpath':
specifier: ^6.0.0 specifier: ^6.0.0
version: 6.0.0 version: 6.0.0

View File

@@ -1,3 +1,4 @@
import { Smartlog } from '@push.rocks/smartlog';
import { BaseRegistry } from '../core/classes.baseregistry.js'; import { BaseRegistry } from '../core/classes.baseregistry.js';
import { RegistryStorage } from '../core/classes.registrystorage.js'; import { RegistryStorage } from '../core/classes.registrystorage.js';
import { AuthManager } from '../core/classes.authmanager.js'; import { AuthManager } from '../core/classes.authmanager.js';
@@ -23,6 +24,7 @@ export class NpmRegistry extends BaseRegistry {
private authManager: AuthManager; private authManager: AuthManager;
private basePath: string = '/npm'; private basePath: string = '/npm';
private registryUrl: string; private registryUrl: string;
private logger: Smartlog;
constructor( constructor(
storage: RegistryStorage, storage: RegistryStorage,
@@ -35,6 +37,19 @@ export class NpmRegistry extends BaseRegistry {
this.authManager = authManager; this.authManager = authManager;
this.basePath = basePath; this.basePath = basePath;
this.registryUrl = registryUrl; 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<void> { public async init(): Promise<void> {
@@ -47,14 +62,17 @@ export class NpmRegistry extends BaseRegistry {
public async handleRequest(context: IRequestContext): Promise<IResponse> { public async handleRequest(context: IRequestContext): Promise<IResponse> {
const path = context.path.replace(this.basePath, ''); const path = context.path.replace(this.basePath, '');
console.log(`[NPM handleRequest] method=${context.method}, path=${path}`);
// Extract token from Authorization header // Extract token from Authorization header
const authHeader = context.headers['authorization'] || context.headers['Authorization']; const authHeader = context.headers['authorization'] || context.headers['Authorization'];
const tokenString = authHeader?.replace(/^Bearer\s+/i, ''); 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; 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 // Registry root
if (path === '/' || path === '') { if (path === '/' || path === '') {
@@ -192,7 +210,12 @@ export class NpmRegistry extends BaseRegistry {
query: Record<string, string> query: Record<string, string>
): Promise<IResponse> { ): Promise<IResponse> {
const packument = await this.storage.getNpmPackument(packageName); 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) { if (!packument) {
return { return {
status: 404, status: 404,
@@ -279,9 +302,16 @@ export class NpmRegistry extends BaseRegistry {
body: IPublishRequest, body: IPublishRequest,
token: IAuthToken | null token: IAuthToken | null
): Promise<IResponse> { ): Promise<IResponse> {
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'); 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) { if (!hasPermission) {
return { return {
status: 401, status: 401,
@@ -382,9 +412,12 @@ export class NpmRegistry extends BaseRegistry {
} }
// Save packument // Save packument
console.log(`[publishPackage] Saving packument with versions:`, Object.keys(packument.versions));
await this.storage.putNpmPackument(packageName, packument); 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 { return {
status: 201, status: 201,
@@ -531,13 +564,14 @@ export class NpmRegistry extends BaseRegistry {
const size = parseInt(query.size || '20', 10); const size = parseInt(query.size || '20', 10);
const from = parseInt(query.from || '0', 10); const from = parseInt(query.from || '0', 10);
this.logger.log('debug', `handleSearch: query="${text}"`, { text, size, from });
// Simple search implementation // Simple search implementation
const results: ISearchResult[] = []; const results: ISearchResult[] = [];
try { try {
// List all package paths // List all package paths
const packagePaths = await this.storage.listObjects('npm/packages/'); 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}/...) // Extract unique package names from paths (format: npm/packages/{packageName}/...)
const packageNames = new Set<string>(); const packageNames = new Set<string>();
@@ -547,7 +581,11 @@ export class NpmRegistry extends BaseRegistry {
packageNames.add(match[1]); 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 // Load packuments and filter by search text
for (const packageName of packageNames) { for (const packageName of packageNames) {

View File

@@ -5,6 +5,7 @@ export { path };
// @push.rocks scope // @push.rocks scope
import * as smartbucket from '@push.rocks/smartbucket'; import * as smartbucket from '@push.rocks/smartbucket';
import * as smartlog from '@push.rocks/smartlog';
import * as smartpath from '@push.rocks/smartpath'; import * as smartpath from '@push.rocks/smartpath';
export { smartbucket, smartpath }; export { smartbucket, smartlog, smartpath };