Skip to content
Merged
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
11 changes: 10 additions & 1 deletion llm-server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,16 @@ export class App {

constructor(llmProvider: LLMProvider) {
this.app = express();
this.app.use(express.json());
this.app.use(
express.json({
limit: '50mb',
verify: (req, res, buf) => {
if (buf?.length > 50 * 1024 * 1024) {
throw new Error('Request body exceeds 50MB limit');
}
},
}),
);
Comment on lines +35 to +44
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Simplify the middleware configuration and improve error handling.

The current implementation has redundant size checks and suboptimal error handling. Consider these improvements:

+const MAX_REQUEST_BODY_SIZE = '50mb';
+
 export class App {
   private readonly logger = new Logger(App.name);
   private app: Express;
   // ...
   constructor(llmProvider: LLMProvider) {
     this.app = express();
-    this.app.use(
-      express.json({
-        limit: '50mb',
-        verify: (req, res, buf) => {
-          if (buf?.length > 50 * 1024 * 1024) {
-            throw new Error('Request body exceeds 50MB limit');
-          }
-        },
-      }),
-    );
+    this.app.use(express.json({ limit: MAX_REQUEST_BODY_SIZE }));

Rationale:

  1. The limit option in express.json() already handles size limits and automatically sends a 413 (Payload Too Large) status code.
  2. The verification function adds unnecessary overhead with a redundant check.
  3. Defining the limit as a constant improves maintainability.

this.PORT = parseInt(process.env.PORT || '8001', 10);
this.llmProvider = llmProvider;
this.logger.log(`App initialized with PORT: ${this.PORT}`);
Expand Down
Loading