♻️(frontend) simplify Express middleware
Simplify the use of middleware in Express Signed-off-by: Stephan Meijer <me@stephanmeijer.com>
This commit is contained in:
@@ -8,9 +8,15 @@ vi.mock('../src/env', async (importOriginal) => {
|
|||||||
return {
|
return {
|
||||||
...(await importOriginal()),
|
...(await importOriginal()),
|
||||||
COLLABORATION_SERVER_ORIGIN: 'http://localhost:3000',
|
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();
|
console.error = vi.fn();
|
||||||
|
|
||||||
describe('Server Tests', () => {
|
describe('Server Tests', () => {
|
||||||
@@ -40,8 +46,10 @@ describe('Server Tests', () => {
|
|||||||
const largePayload = 'a'.repeat(400 * 1024); // 400kb payload
|
const largePayload = 'a'.repeat(400 * 1024); // 400kb payload
|
||||||
const response = await request(app)
|
const response = await request(app)
|
||||||
.post(routes.CONVERT)
|
.post(routes.CONVERT)
|
||||||
.send({ data: largePayload })
|
.set('Origin', origin)
|
||||||
.set('Content-Type', 'application/json');
|
.set('Authorization', apiKey)
|
||||||
|
.set('Content-Type', 'application/json')
|
||||||
|
.send({ data: largePayload });
|
||||||
|
|
||||||
expect(response.status).not.toBe(413);
|
expect(response.status).not.toBe(413);
|
||||||
});
|
});
|
||||||
@@ -52,20 +60,10 @@ describe('Server Tests', () => {
|
|||||||
const oversizedPayload = 'a'.repeat(501 * 1024); // 501kb payload
|
const oversizedPayload = 'a'.repeat(501 * 1024); // 501kb payload
|
||||||
const response = await request(app)
|
const response = await request(app)
|
||||||
.post(routes.CONVERT)
|
.post(routes.CONVERT)
|
||||||
.send({ data: oversizedPayload })
|
.set('Origin', origin)
|
||||||
.set('Content-Type', 'application/json');
|
.set('Authorization', apiKey)
|
||||||
|
.set('Content-Type', 'application/json')
|
||||||
expect(response.status).toBe(413);
|
.send({ data: oversizedPayload });
|
||||||
});
|
|
||||||
|
|
||||||
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');
|
|
||||||
|
|
||||||
expect(response.status).toBe(413);
|
expect(response.status).toBe(413);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -22,13 +22,6 @@ import { logger } from '@/utils';
|
|||||||
export const initApp = () => {
|
export const initApp = () => {
|
||||||
const { app } = expressWebsockets(express());
|
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);
|
app.use(corsMiddleware);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -44,13 +37,19 @@ export const initApp = () => {
|
|||||||
app.post(
|
app.post(
|
||||||
routes.COLLABORATION_RESET_CONNECTIONS,
|
routes.COLLABORATION_RESET_CONNECTIONS,
|
||||||
httpSecurity,
|
httpSecurity,
|
||||||
|
express.json(),
|
||||||
collaborationResetConnectionsHandler,
|
collaborationResetConnectionsHandler,
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Route to convert Markdown or BlockNote blocks
|
* 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);
|
Sentry.setupExpressErrorHandler(app);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user