Remove body-parser dependency (#7176)

This PR addresses issue #7158 by removing the body-parser dependency in
favor of express.json() and other built-in middleware functionalities
provided by Express.js.
This commit is contained in:
Atharva Rai
2024-12-06 22:17:06 +05:30
committed by GitHub
parent 3aff85a216
commit a3c64dc4dc
5 changed files with 9 additions and 14 deletions

4
app.ts
View File

@@ -29,7 +29,6 @@ import process from 'process';
import url from 'url';
import * as Sentry from '@sentry/node';
import bodyParser from 'body-parser';
import compression from 'compression';
import express from 'express';
import fs from 'fs-extra';
@@ -525,6 +524,7 @@ async function main() {
// Initialise express and then sentry. Sentry as early as possible to catch errors during startup.
const webServer = express(),
router = express.Router();
SetupSentry(aws.getConfig('sentryDsn'), ceProps, releaseBuildNumber, gitReleaseName, defArgs);
startWineInit();
@@ -848,7 +848,7 @@ async function main() {
),
);
})
.use(bodyParser.json({limit: ceProps('bodyParserLimit', maxUploadSize)}))
.use(express.json({limit: ceProps('bodyParserLimit', maxUploadSize)}))
.use(siteTemplateController.createRouter())
.use(sourceController.createRouter())
.use(assemblyDocumentationController.createRouter())

View File

@@ -22,7 +22,6 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import bodyParser from 'body-parser';
import express from 'express';
import _ from 'underscore';
@@ -95,7 +94,7 @@ export class ApiHandler {
.all(methodNotAllowed);
const maxUploadSize = ceProps('maxUploadSize', '1mb');
const textParser = bodyParser.text({limit: ceProps('bodyParserLimit', maxUploadSize), type: () => true});
const textParser = express.text({limit: ceProps('bodyParserLimit', maxUploadSize), type: () => true});
this.handle
.route('/compiler/:compiler/compile')

View File

@@ -22,7 +22,6 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import bodyParser from 'body-parser';
import express from 'express';
import {isString} from '../../shared/common-utils.js';
@@ -48,7 +47,7 @@ export class NoScriptHandler {
readonly defaultLanguage: string;
readonly compileHandler: CompileHandler;
formDataParser: ReturnType<typeof bodyParser.urlencoded> | undefined;
formDataParser: ReturnType<typeof express.urlencoded> | undefined;
/* the type for config makes the most sense to define in app.ts or api.ts */
constructor(
@@ -64,8 +63,7 @@ export class NoScriptHandler {
}
InitializeRoutes(options: {limit: string}) {
this.formDataParser = bodyParser.urlencoded({
type: 'application/x-www-form-urlencoded',
this.formDataParser = express.urlencoded({
limit: options.limit,
extended: false,
});

View File

@@ -22,7 +22,6 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import bodyParser from 'body-parser';
import express from 'express';
import request from 'supertest';
import {beforeAll, describe, expect, it} from 'vitest';
@@ -97,7 +96,7 @@ describe('API handling', () => {
'default',
{ceProps: (key, def) => def} as CompilationEnvironment,
);
app.use(bodyParser.json());
app.use(express.json());
app.use('/api', apiHandler.handle);
apiHandler.setCompilers(compilers);
apiHandler.setLanguages(languages);

View File

@@ -22,7 +22,6 @@
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import bodyParser from 'body-parser';
import express from 'express';
import request from 'supertest';
import {beforeAll, describe, expect, it} from 'vitest';
@@ -47,11 +46,11 @@ describe('Compiler tests', () => {
const compilationEnvironment = makeCompilationEnvironment({languages});
compileHandler = new CompileHandler(compilationEnvironment, fakeProps({}));
const textParser = bodyParser.text({type: () => true});
const formParser = bodyParser.urlencoded({extended: false});
const textParser = express.text({type: () => true});
const formParser = express.urlencoded({extended: false});
app = express();
app.use(bodyParser.json());
app.use(express.json());
app.post('/noscript/compile', formParser, compileHandler.handle.bind(compileHandler));
app.post('/:compiler/compile', textParser, compileHandler.handle.bind(compileHandler));