Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30,565 changes: 20,037 additions & 10,528 deletions package-lock.json

Large diffs are not rendered by default.

27 changes: 19 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"@nestjs/bull": "^10.0.1",
"@nestjs/cache-manager": "^3.1.0",
"@nestjs/common": "^10.3.0",
"@nestjs/config": "^3.1.1",
"@nestjs/config": "^4.0.3",
"@nestjs/core": "^10.3.0",
"@nestjs/jwt": "^11.0.2",
"@nestjs/passport": "^10.0.3",
Expand All @@ -58,7 +58,11 @@
"@nestjs/throttler": "^5.1.1",
"@nestjs/typeorm": "^10.0.2",
"@nomicfoundation/hardhat-toolbox": "^4.0.0",
"@opentelemetry/auto-instrumentations-node": "^0.70.0",
"@opentelemetry/sdk-node": "^0.212.0",
"@prisma/client": "^6.19.2",
"@sentry/node": "^9.20.0",
"@willsoto/nestjs-prometheus": "^6.0.2",
"axios": "^1.6.2",
"bcrypt": "^6.0.0",
"bull": "^4.12.2",
Expand All @@ -75,27 +79,33 @@
"dotenv": "^16.3.1",
"ethers": "^6.9.0",
"express-rate-limit": "^7.1.5",
"hardhat": "^2.19.4",
"hardhat": "^3.1.9",
"helmet": "^7.1.0",
"ioredis": "^5.3.2",
"joi": "^17.11.0",
"jsonwebtoken": "^9.0.2",
"lodash": "^4.17.21",
"keyv": "^5.6.0",
"lodash": "^4.17.23",
"mapped-types": "^0.0.1",
"moment": "^2.29.4",
"multer": "^2.0.2",
"nest-winston": "^1.10.2",
"nestjs-pino": "^4.6.0",
"opossum": "^9.0.0",
"passport": "^0.7.0",
"passport-custom": "^1.1.1",
"passport-jwt": "^4.0.1",
"passport-local": "^1.0.0",
"pg": "^8.11.3",
"pino": "^10.3.1",
"pino-http": "^11.0.0",
"pino-pretty": "^13.1.3",
"prom-client": "^15.1.3",
"redis": "^4.6.12",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1",
"sharp": "^0.33.1",
"typeorm": "^0.3.28",
"typeorm": "^0.0.1",
"uuid": "^9.0.1",
"web3": "^4.3.0",
"winston": "^3.11.0",
Expand All @@ -104,7 +114,7 @@
"devDependencies": {
"@commitlint/cli": "^18.4.3",
"@commitlint/config-conventional": "^18.4.3",
"@nestjs/cli": "^10.3.0",
"@nestjs/cli": "^11.0.16",
"@nestjs/schematics": "^10.0.3",
"@nestjs/testing": "^10.3.0",
"@types/bcrypt": "^5.0.2",
Expand All @@ -119,19 +129,20 @@
"@types/node": "^20.10.4",
"@types/passport-jwt": "^3.0.13",
"@types/passport-local": "^1.0.38",
"@types/shimmer": "^1.2.0",
"@types/supertest": "^2.0.16",
"@types/uuid": "^9.0.7",
"@typescript-eslint/eslint-plugin": "^6.13.1",
"@typescript-eslint/parser": "^6.13.1",
"eslint": "^8.54.0",
"eslint": "^4.1.1",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.1",
"husky": "^8.0.3",
"jest": "^29.7.0",
"jest": "^25.0.0",
"lint-staged": "^15.2.0",
"prettier": "^3.1.0",
"prisma": "^6.19.2",
"rimraf": "^5.0.5",
"rimraf": "^6.1.3",
"source-map-support": "^0.5.21",
"supertest": "^6.3.3",
"testcontainers": "^10.4.0",
Expand Down
6 changes: 6 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ import { AuditController } from './common/controllers/audit.controller';

// Middleware
import { AuthRateLimitMiddleware } from './auth/middleware/auth.middleware';
import { LoggerModule } from './common/logger/logger.module';
import { CorrelationMiddleware } from './common/middleware/correlation.middleware';
import { MetricsModule } from './observability/metrics.module';

@Module({
imports: [
Expand All @@ -63,6 +66,7 @@ import { AuthRateLimitMiddleware } from './auth/middleware/auth.middleware';
PrismaModule,
HealthModule,
RedisModule,
LoggerModule,

// Security & rate limiting
ThrottlerModule.forRootAsync({
Expand Down Expand Up @@ -101,6 +105,7 @@ import { AuthRateLimitMiddleware } from './auth/middleware/auth.middleware';
// Compliance & Security
AuditModule,
RbacModule,
MetricsModule
],
controllers: [
AuditController, // Add the audit controller
Expand All @@ -116,6 +121,7 @@ export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer
// Auth rate limiting
.apply(CorrelationMiddleware).forRoutes('*')
.apply(AuthRateLimitMiddleware)
.forRoutes('/auth*');
}
Expand Down
21 changes: 21 additions & 0 deletions src/common/filters/sentry.filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ExceptionFilter, Catch, ArgumentsHost, HttpException } from '@nestjs/common';
import * as Sentry from '@sentry/node';

@Catch()
export class SentryFilter implements ExceptionFilter {
catch(exception: unknown, host: ArgumentsHost) {
Sentry.captureException(exception);

const ctx = host.switchToHttp();
const response = ctx.getResponse();

if (exception instanceof HttpException) {
response.status(exception.getStatus()).json(exception.getResponse());
} else {
response.status(500).json({
statusCode: 500,
message: 'Internal server error',
});
}
}
}
18 changes: 18 additions & 0 deletions src/common/logger/logger.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { ConfigService } from '@nestjs/config';
import * as winston from 'winston';
import * as DailyRotateFile from 'winston-daily-rotate-file';
import { LoggingInterceptor } from './logging.interceptor';
import { randomUUID } from 'crypto';
import { LoggerModule as PinoLoggerModule } from 'nestjs-pino';

@Global()
@Module({
Expand Down Expand Up @@ -57,6 +59,22 @@ import { LoggingInterceptor } from './logging.interceptor';
],
}),
}),
PinoLoggerModule.forRoot({
pinoHttp: {
level: process.env.LOG_LEVEL || 'info',
transport: {
targets: [
{
target: 'pino/file',
options: { destination: './logs/app.log', mkdir: true },
},
{
target: 'pino-pretty',
},
],
},
},
}),
],
providers: [LoggerService, LoggingInterceptor],
exports: [LoggerService, WinstonModule, LoggingInterceptor],
Expand Down
19 changes: 19 additions & 0 deletions src/common/middleware/correlation.middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Injectable, NestMiddleware } from '@nestjs/common';
import { AsyncLocalStorage } from 'async_hooks';
import { randomUUID } from 'crypto';

export const asyncLocalStorage = new AsyncLocalStorage<Map<string, string>>();

@Injectable()
export class CorrelationMiddleware implements NestMiddleware {
use(req: any, res: any, next: () => void) {
const store = new Map<string, string>();
const correlationId = req.headers['x-request-id'] || randomUUID();

store.set('correlationId', correlationId);

asyncLocalStorage.run(store, () => {
next();
});
}
}
8 changes: 8 additions & 0 deletions src/common/tracing/tracing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { NodeSDK } from '@opentelemetry/sdk-node';
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';

const sdk = new NodeSDK({
instrumentations: [getNodeAutoInstrumentations()],
});

sdk.start();
8 changes: 8 additions & 0 deletions src/config/sentry.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as Sentry from '@sentry/node';

export function initSentry() {
Sentry.init({
dsn: process.env.SENTRY_DSN,
tracesSampleRate: 1.0,
});
}
6 changes: 5 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ import { LoggingInterceptor } from './common/logging/logging.interceptor';
// FIX: Corrected import name from AppExceptionFilter to AllExceptionsFilter
import { AllExceptionsFilter } from './common/errors/error.filter';
import { ResponseInterceptor } from './common/interceptors/response.interceptor';
import { initSentry } from './config/sentry.config';
import { SentryFilter } from './common/filters/sentry.filter';

async function bootstrap() {
initSentry();

const app = await NestFactory.create(AppModule, {
bufferLogs: true,
});
Expand Down Expand Up @@ -52,7 +56,7 @@ async function bootstrap() {

// Global filters and interceptors
// FIX: Removed arguments from AllExceptionsFilter because the constructor expects 0
app.useGlobalFilters(new AllExceptionsFilter());
app.useGlobalFilters(new SentryFilter());

// Using 'as any' to bypass the strict LoggerService interface mismatch
app.useGlobalInterceptors(new ResponseInterceptor(logger as any), new LoggingInterceptor(logger as any));
Expand Down
13 changes: 13 additions & 0 deletions src/observability/metrics.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Module } from '@nestjs/common';
import { PrometheusModule } from '@willsoto/nestjs-prometheus';

@Module({
imports: [
PrometheusModule.register({
defaultMetrics: {
enabled: true,
},
}),
],
})
export class MetricsModule {}