Skip to content

Commit

Permalink
Move noscript compile handler into own controller file (compiler-expl…
Browse files Browse the repository at this point in the history
…orer#7371)

Continuation of my previous work on the http endpoints. This leaves the
old noscriptHandler to only serve the HTML pages.
  • Loading branch information
junlarsen authored Feb 10, 2025
1 parent be1e197 commit 943d9fb
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 12 deletions.
8 changes: 6 additions & 2 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ import {FormattingService} from './lib/formatting-service.js';
import {AssemblyDocumentationController} from './lib/handlers/api/assembly-documentation-controller.js';
import {FormattingController} from './lib/handlers/api/formatting-controller.js';
import {HealthcheckController} from './lib/handlers/api/healthcheck-controller.js';
import {NoScriptController} from './lib/handlers/api/noscript-controller.js';
import {SiteTemplateController} from './lib/handlers/api/site-template-controller.js';
import {SourceController} from './lib/handlers/api/source-controller.js';
import {CompileHandler} from './lib/handlers/compile.js';
import {cached, csp} from './lib/handlers/middleware.js';
import {cached, createFormDataHandler, csp} from './lib/handlers/middleware.js';
import {NoScriptHandler} from './lib/handlers/noscript.js';
import {RouteAPI, ShortLinkMetaData} from './lib/handlers/route-api.js';
import {languages as allLanguages} from './lib/languages.js';
Expand Down Expand Up @@ -549,6 +550,7 @@ async function main() {

const isExecutionWorker = ceProps<boolean>('execqueue.is_worker', false);
const healthCheckFilePath = ceProps('healthCheckFilePath', null) as string | null;
const formDataHandler = createFormDataHandler();

const siteTemplateController = new SiteTemplateController();
const sourceController = new SourceController(sources);
Expand All @@ -560,6 +562,7 @@ async function main() {
isExecutionWorker,
);
const formattingController = new FormattingController(formattingService);
const noScriptController = new NoScriptController(compileHandler, formDataHandler);

logger.info('=======================================');
if (gitReleaseName) logger.info(` git release ${gitReleaseName}`);
Expand Down Expand Up @@ -850,9 +853,10 @@ async function main() {
.use(sourceController.createRouter())
.use(assemblyDocumentationController.createRouter())
.use(formattingController.createRouter())
.use(noScriptController.createRouter())
.get('/g/:id', oldGoogleUrlHandler);

noscriptHandler.InitializeRoutes({limit: ceProps('bodyParserLimit', maxUploadSize)});
noscriptHandler.InitializeRoutes();
routeApi.InitializeRoutes();

if (!defArgs.doCache) {
Expand Down
45 changes: 45 additions & 0 deletions lib/handlers/api/noscript-controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2025, Compiler Explorer Authors
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

import express from 'express';

import {CompileHandler} from '../compile.js';
import {HttpController} from './controller.interfaces.js';

export class NoScriptController implements HttpController {
public constructor(
private compileHandler: CompileHandler,
private formDataHandler: express.Handler,
) {}

createRouter(): express.Router {
const router = express.Router();
router.post(
'/api/noscript/compile',
this.formDataHandler,
this.compileHandler.handle.bind(this.compileHandler),
);
return router;
}
}
14 changes: 14 additions & 0 deletions lib/handlers/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,17 @@ export const csp: express.Handler = (_, res, next) => {
// res.set('Content-Security-Policy', `default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'; frame-src 'self';`);
return next();
};

/**
* Parse the incoming request as urlencoded FormData.
*/
export function createFormDataHandler(): express.Handler {
const defaultMaxBodySize = ceProps('maxBodySize', '1mb');
const parser = express.urlencoded({
limit: ceProps('bodyParserLimit', defaultMaxBodySize),
extended: false,
});
return (req, res, next) => {
parser(req, res, next);
};
}
12 changes: 2 additions & 10 deletions lib/handlers/noscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ export class NoScriptHandler {
readonly defaultLanguage: string;
readonly compileHandler: CompileHandler;

formDataParser: ReturnType<typeof express.urlencoded> | undefined;

/* the type for config makes the most sense to define in app.ts or api.ts */
constructor(
private readonly router: express.Router,
Expand All @@ -62,12 +60,7 @@ export class NoScriptHandler {
this.defaultLanguage = config.opts.wantedLanguage;
}

InitializeRoutes(options: {limit: string}) {
this.formDataParser = express.urlencoded({
limit: options.limit,
extended: false,
});

InitializeRoutes() {
this.router
.get('/noscript', cached, csp, (req, res) => {
this.renderNoScriptLayout(undefined, req, res);
Expand All @@ -87,8 +80,7 @@ export class NoScriptHandler {
})
.get('/noscript/:language', cached, csp, (req, res) => {
this.renderNoScriptLayout(undefined, req, res);
})
.post('/api/noscript/compile', this.formDataParser, this.compileHandler.handle.bind(this.compileHandler));
});
}

storedStateHandlerNoScript(req: express.Request, res: express.Response, next: express.NextFunction) {
Expand Down

0 comments on commit 943d9fb

Please sign in to comment.