From 22c7796c74a0502eb5f95265a14f1c0ab3da92a2 Mon Sep 17 00:00:00 2001 From: Alan Agius <17563226+alan-agius4@users.noreply.github.com> Date: Tue, 22 Apr 2025 09:37:37 +0000 Subject: [PATCH] refactor: minor code changes in karma `AngularAssetsMiddleware` Early exits and reduce optional chaining. --- .../src/builders/karma/application_builder.ts | 38 +++++++++---------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/packages/angular/build/src/builders/karma/application_builder.ts b/packages/angular/build/src/builders/karma/application_builder.ts index 2c9c86067e6c..a32f474fa535 100644 --- a/packages/angular/build/src/builders/karma/application_builder.ts +++ b/packages/angular/build/src/builders/karma/application_builder.ts @@ -67,32 +67,30 @@ class AngularAssetsMiddleware { ) {} handle(req: IncomingMessage, res: ServerResponse, next: (err?: unknown) => unknown) { - let err = null; - try { - const url = new URL(`http://${req.headers['host']}${req.url}`); - // Remove the leading slash from the URL path and convert to platform specific. - // The latest build files will use the platform path separator. - let pathname = url.pathname.slice(1); - if (isWindows) { - pathname = pathname.replaceAll(path.posix.sep, path.win32.sep); - } + const url = new URL(`http://${req.headers['host']}${req.url}`); + // Remove the leading slash from the URL path and convert to platform specific. + // The latest build files will use the platform path separator. + let pathname = url.pathname.slice(1); + if (isWindows) { + pathname = pathname.replaceAll(path.posix.sep, path.win32.sep); + } - const file = this.latestBuildFiles.files[pathname]; + const file = this.latestBuildFiles.files[pathname]; + if (!file) { + next(); - if (file?.origin === 'disk') { - this.serveFile(file.inputPath, undefined, res); + return; + } - return; - } else if (file?.origin === 'memory') { + switch (file.origin) { + case 'disk': + this.serveFile(file.inputPath, undefined, res); + break; + case 'memory': // Include pathname to help with Content-Type headers. this.serveFile(`/unused/${url.pathname}`, undefined, res, undefined, file.contents, true); - - return; - } - } catch (e) { - err = e; + break; } - next(err); } static createPlugin(initialFiles: LatestBuildFiles): InlinePluginDef {