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
16 changes: 14 additions & 2 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Module, MiddlewareConsumer, NestModule } from '@nestjs/common';
import { Module, NestModule, MiddlewareConsumer, RequestMethod } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from '@nestjs/config';
Expand All @@ -16,6 +17,11 @@ import { PuzzlesModule } from './puzzles/puzzles.module';
import { QuestsModule } from './quests/quests.module';
import { StreakModule } from './streak/strerak.module';
import { CategoriesModule } from './categories/categories.module';
import { TransactionMiddleware } from './middleware/transaction/transaction.middleware';
import { TransactionLogger } from './middleware/transaction/transaction.logger';
import { CompressionMiddleware } from './middleware/compression/compression.middleware';
import { IdempotencyMiddleware } from './middleware/idempotency/idempotency.middleware';
import { IdempotencyService } from './middleware/idempotency/idempotency.service';
import { JwtAuthModule, JwtAuthMiddleware } from './auth/middleware/jwt-auth.module';
import { REDIS_CLIENT } from './redis/redis.constants';
import jwtConfig from './auth/authConfig/jwt.config';
Expand Down Expand Up @@ -87,7 +93,6 @@ import { HealthModule } from './health/health.module';
CommonModule,
RedisModule,
BlockchainModule,
ProgressModule,
CategoriesModule,
// Register the custom JWT Auth Middleware module
JwtAuthModule.registerAsync({
Expand All @@ -104,9 +109,16 @@ import { HealthModule } from './health/health.module';
HealthModule,
],
controllers: [AppController],
providers: [AppService],
providers: [AppService, TransactionLogger],
})
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
// Apply transaction middleware globally
consumer.apply(TransactionMiddleware).forRoutes('*');

// Apply compression middleware globally
consumer.apply(CompressionMiddleware).forRoutes('*');
consumer.apply(IdempotencyMiddleware).forRoutes('*'); // new
/**
* Apply the JWT Authentication Middleware to all routes except public ones.
*/
Expand Down
20 changes: 20 additions & 0 deletions backend/src/middleware/compression/compression.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const COMPRESSION_CONFIG = {
threshold: 1024, // minimum size in bytes
gzip: { level: 6 }, // balance speed vs size
brotli: { quality: 4 }, // modern browsers
skipTypes: [
/^image\//,
/^video\//,
/^audio\//,
/^application\/zip/,
/^application\/gzip/,
],
compressibleTypes: [
'application/json',
'text/html',
'text/plain',
'application/javascript',
'text/css',
'text/xml',
],
};
63 changes: 63 additions & 0 deletions backend/src/middleware/compression/compression.middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';
import * as zlib from 'zlib';
import { COMPRESSION_CONFIG } from './compression.config';

@Injectable()
export class CompressionMiddleware implements NestMiddleware {
use(req: Request, res: Response, next: NextFunction) {
const acceptEncoding = req.headers['accept-encoding'] || '';
const chunks: Buffer[] = [];
const originalWrite = res.write;
const originalEnd = res.end;

// Intercept response body
res.write = function (chunk: any) {
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
return true;
};

res.end = function (chunk: any) {
if (chunk) {
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
}

const body = Buffer.concat(chunks);

// Skip compression if too small
if (body.length < COMPRESSION_CONFIG.threshold) {
return originalEnd.call(res, body);
}

const contentType = res.getHeader('Content-Type') as string;
if (COMPRESSION_CONFIG.skipTypes.some((regex) => regex.test(contentType))) {
return originalEnd.call(res, body);
}

// Select algorithm
if (/\bbr\b/.test(acceptEncoding)) {
zlib.brotliCompress(body, { params: { [zlib.constants.BROTLI_PARAM_QUALITY]: COMPRESSION_CONFIG.brotli.quality } }, (err, compressed) => {
if (err) return originalEnd.call(res, body);
res.setHeader('Content-Encoding', 'br');
originalEnd.call(res, compressed);
});
} else if (/\bgzip\b/.test(acceptEncoding)) {
zlib.gzip(body, { level: COMPRESSION_CONFIG.gzip.level }, (err, compressed) => {
if (err) return originalEnd.call(res, body);
res.setHeader('Content-Encoding', 'gzip');
originalEnd.call(res, compressed);
});
} else if (/\bdeflate\b/.test(acceptEncoding)) {
zlib.deflate(body, (err, compressed) => {
if (err) return originalEnd.call(res, body);
res.setHeader('Content-Encoding', 'deflate');
originalEnd.call(res, compressed);
});
} else {
return originalEnd.call(res, body);
}
};

next();
}
}
9 changes: 9 additions & 0 deletions backend/src/middleware/idempotency/idempotency.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const IDEMPOTENCY_CONFIG = {
ttl: {
puzzleSubmission: 300, // 5 minutes
pointClaim: 600, // 10 minutes
friendRequest: 3600, // 1 hour
profileUpdate: 60, // 1 minute
},
headerKey: 'x-idempotency-key',
};
56 changes: 56 additions & 0 deletions backend/src/middleware/idempotency/idempotency.middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Injectable, NestMiddleware, BadRequestException } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';
import { IdempotencyService } from './idempotency.service';
import { IDEMPOTENCY_CONFIG } from './idempotency.config';

@Injectable()
export class IdempotencyMiddleware implements NestMiddleware {
constructor(private readonly idempotencyService: IdempotencyService) {}

async use(req: Request, res: Response, next: NextFunction) {
// Skip GET requests
if (req.method === 'GET') return next();

const headerKey = IDEMPOTENCY_CONFIG.headerKey;
let idempotencyKey = req.headers[headerKey] as string;

if (!idempotencyKey) {
// Auto-generate key if not provided
idempotencyKey = await this.idempotencyService.generateKey(req);
}

if (typeof idempotencyKey !== 'string') {
throw new BadRequestException('Invalid idempotency key format');
}

const cachedResponse = await this.idempotencyService.getResponse(idempotencyKey);
if (cachedResponse) {
// Return cached response immediately
res.set(cachedResponse.headers);
return res.status(cachedResponse.statusCode).send(cachedResponse.body);
}

// Intercept response to store it
const originalSend = res.send.bind(res);
res.send = async (body: any) => {
const ttl = this.resolveTTL(req.originalUrl);
const responsePayload = {
statusCode: res.statusCode,
headers: res.getHeaders(),
body,
};
await this.idempotencyService.storeResponse(idempotencyKey, responsePayload, ttl);
return originalSend(body);
};

next();
}

private resolveTTL(url: string): number {
if (url.includes('/puzzles')) return IDEMPOTENCY_CONFIG.ttl.puzzleSubmission;
if (url.includes('/points')) return IDEMPOTENCY_CONFIG.ttl.pointClaim;
if (url.includes('/friends')) return IDEMPOTENCY_CONFIG.ttl.friendRequest;
if (url.includes('/profile')) return IDEMPOTENCY_CONFIG.ttl.profileUpdate;
return 300; // default
}
}
25 changes: 25 additions & 0 deletions backend/src/middleware/idempotency/idempotency.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Injectable } from '@nestjs/common';
import { RedisService } from '../../redis/redis.service';
import * as crypto from 'crypto';

@Injectable()
export class IdempotencyService {
constructor(private readonly redisService: RedisService) {}

async generateKey(req: any): Promise<string> {
const userId = req.user?.id || 'anon';
const bodyHash = crypto.createHash('sha256').update(JSON.stringify(req.body)).digest('hex');
return `${userId}:${req.method}:${req.originalUrl}:${bodyHash}`;
}

async storeResponse(key: string, response: any, ttl: number) {
const client = this.redisService.getClient();
await client.set(key, JSON.stringify(response), 'EX', ttl, 'NX'); // SETNX for atomicity
}

async getResponse(key: string): Promise<any | null> {
const client = this.redisService.getClient();
const data = await client.get(key);
return data ? JSON.parse(data) : null;
}
}
14 changes: 14 additions & 0 deletions backend/src/middleware/transaction/transaction.logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Injectable, Logger } from '@nestjs/common';

@Injectable()
export class TransactionLogger {
private readonly logger = new Logger('Transaction');

log(message: string) {
this.logger.log(message);
}

error(message: string, error: any) {
this.logger.error(`${message}: ${error.message}`, error.stack);
}
}
36 changes: 36 additions & 0 deletions backend/src/middleware/transaction/transaction.manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { DataSource, QueryRunner } from 'typeorm';

export class TransactionManager {
private queryRunner: QueryRunner;

constructor(private readonly dataSource: DataSource) {
this.queryRunner = this.dataSource.createQueryRunner();
}

async startTransaction(isolation: 'READ COMMITTED' | 'REPEATABLE READ' | 'SERIALIZABLE' = 'READ COMMITTED') {
await this.queryRunner.connect();
await this.queryRunner.startTransaction(isolation);
}

async commitTransaction() {
await this.queryRunner.commitTransaction();
await this.queryRunner.release();
}

async rollbackTransaction() {
await this.queryRunner.rollbackTransaction();
await this.queryRunner.release();
}

async createSavepoint(name: string) {
await this.queryRunner.query(`SAVEPOINT ${name}`);
}

async rollbackToSavepoint(name: string) {
await this.queryRunner.query(`ROLLBACK TO SAVEPOINT ${name}`);
}

getManager() {
return this.queryRunner.manager;
}
}
37 changes: 37 additions & 0 deletions backend/src/middleware/transaction/transaction.middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';
import { DataSource } from 'typeorm';
import { TransactionManager } from './transaction.manager';
import { TransactionLogger } from './transaction.logger';

@Injectable()
export class TransactionMiddleware implements NestMiddleware {
constructor(private readonly dataSource: DataSource, private readonly logger: TransactionLogger) {}

async use(req: Request, res: Response, next: NextFunction) {
const manager = new TransactionManager(this.dataSource);

try {
await manager.startTransaction();

// Attach transaction manager to request for manual control if needed
(req as any).transactionManager = manager;

res.on('finish', async () => {
if (res.statusCode >= 200 && res.statusCode < 400) {
await manager.commitTransaction();
this.logger.log('Transaction committed successfully');
} else {
await manager.rollbackTransaction();
this.logger.log('Transaction rolled back due to error');
}
});

next();
} catch (error) {
await manager.rollbackTransaction();
this.logger.error('Transaction failed', error);
next(error);
}
}
}
64 changes: 64 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading