♻️(frontend) simplify Express middleware

Simplify the use of middleware in Express

Signed-off-by: Stephan Meijer <me@stephanmeijer.com>
This commit is contained in:
Stephan Meijer
2025-07-04 11:32:11 +02:00
parent 186ae952f5
commit a9db392a61
2 changed files with 21 additions and 24 deletions

View File

@@ -8,9 +8,15 @@ vi.mock('../src/env', async (importOriginal) => {
return {
...(await importOriginal()),
COLLABORATION_SERVER_ORIGIN: 'http://localhost:3000',
Y_PROVIDER_API_KEY: 'yprovider-api-key',
};
});
import {
Y_PROVIDER_API_KEY as apiKey,
COLLABORATION_SERVER_ORIGIN as origin,
} from '../src/env';
console.error = vi.fn();
describe('Server Tests', () => {
@@ -40,8 +46,10 @@ describe('Server Tests', () => {
const largePayload = 'a'.repeat(400 * 1024); // 400kb payload
const response = await request(app)
.post(routes.CONVERT)
.send({ data: largePayload })
.set('Content-Type', 'application/json');
.set('Origin', origin)
.set('Authorization', apiKey)
.set('Content-Type', 'application/json')
.send({ data: largePayload });
expect(response.status).not.toBe(413);
});
@@ -52,20 +60,10 @@ describe('Server Tests', () => {
const oversizedPayload = 'a'.repeat(501 * 1024); // 501kb payload
const response = await request(app)
.post(routes.CONVERT)
.send({ data: oversizedPayload })
.set('Content-Type', 'application/json');
expect(response.status).toBe(413);
});
it('uses the default JSON limit for other routes', async () => {
const app = initApp();
const largePayload = 'a'.repeat(150 * 1024);
const response = await request(app)
.post('/some-other-route')
.send({ data: largePayload })
.set('Content-Type', 'application/json');
.set('Origin', origin)
.set('Authorization', apiKey)
.set('Content-Type', 'application/json')
.send({ data: oversizedPayload });
expect(response.status).toBe(413);
});

View File

@@ -22,13 +22,6 @@ import { logger } from '@/utils';
export const initApp = () => {
const { app } = expressWebsockets(express());
app.use((req, res, next) => {
if (req.path === routes.CONVERT) {
// Large transcript files are bigger than the default '100kb' limit
return express.json({ limit: '500kb' })(req, res, next);
}
express.json()(req, res, next);
});
app.use(corsMiddleware);
/**
@@ -44,13 +37,19 @@ export const initApp = () => {
app.post(
routes.COLLABORATION_RESET_CONNECTIONS,
httpSecurity,
express.json(),
collaborationResetConnectionsHandler,
);
/**
* Route to convert Markdown or BlockNote blocks
*/
app.post(routes.CONVERT, httpSecurity, convertHandler);
app.post(
routes.CONVERT,
httpSecurity,
express.json({ limit: '500kb' }),
convertHandler,
);
Sentry.setupExpressErrorHandler(app);