From 3cb12e27358d024e8058441a97fa4ba9be48c619 Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Thu, 1 May 2025 12:53:38 -0400 Subject: [PATCH] fix(@angular/build): avoid internal karma request cache for assets The internal karma common middleware that handles requests converts the to be cached data into a string when stored. This can lead to invalid data when the cached string is then sent in a followup request if the original content was not intended to be a string. To avoid this problem, asset files are now explicitly not cached by karma's middleware. Ref: https://github.com/karma-runner/karma/blob/84f85e7016efc2266fa6b3465f494a3fa151c85c/lib/middleware/common.js#L72 (cherry picked from commit 90d1db3df9a915a78c287ecc008837e5f8301279) --- .../src/builders/karma/application_builder.ts | 2 +- .../karma/tests/behavior/rebuilds_spec.ts | 55 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/packages/angular/build/src/builders/karma/application_builder.ts b/packages/angular/build/src/builders/karma/application_builder.ts index 40f66a9c6773..b2f4e4bba95c 100644 --- a/packages/angular/build/src/builders/karma/application_builder.ts +++ b/packages/angular/build/src/builders/karma/application_builder.ts @@ -84,7 +84,7 @@ class AngularAssetsMiddleware { const file = this.latestBuildFiles.files[pathname]; if (file?.origin === 'disk') { - this.serveFile(file.inputPath, undefined, res); + this.serveFile(file.inputPath, undefined, res, undefined, undefined, /* doNotCache */ true); return; } else if (file?.origin === 'memory') { diff --git a/packages/angular/build/src/builders/karma/tests/behavior/rebuilds_spec.ts b/packages/angular/build/src/builders/karma/tests/behavior/rebuilds_spec.ts index e740b7adfcd6..6ec02c2c28f1 100644 --- a/packages/angular/build/src/builders/karma/tests/behavior/rebuilds_spec.ts +++ b/packages/angular/build/src/builders/karma/tests/behavior/rebuilds_spec.ts @@ -10,6 +10,7 @@ import { concatMap, count, debounceTime, distinctUntilChanged, take, timeout } f import { execute } from '../../index'; import { BASE_OPTIONS, KARMA_BUILDER_INFO, describeKarmaBuilder } from '../setup'; import { BuilderOutput } from '@angular-devkit/architect'; +import { randomBytes } from 'node:crypto'; describeKarmaBuilder(execute, KARMA_BUILDER_INFO, (harness, setupTarget) => { describe('Behavior: "Rebuilds"', () => { @@ -68,5 +69,59 @@ describeKarmaBuilder(execute, KARMA_BUILDER_INFO, (harness, setupTarget) => { expect(buildCount).toBe(expectedSequence.length); }); + + it('correctly serves binary assets on rebuilds', async () => { + await harness.writeFiles({ + './src/random.bin': randomBytes(1024), + './src/app/app.component.spec.ts': ` + describe('AppComponent', () => { + it('should fetch binary file with correct size', async () => { + const resp = await fetch('/random.bin'); + const data = await resp.arrayBuffer(); + expect(data.byteLength).toBe(1024); + }); + });`, + }); + + harness.useTarget('test', { + ...BASE_OPTIONS, + watch: true, + assets: ['src/random.bin'], + }); + + interface OutputCheck { + (result: BuilderOutput | undefined): Promise; + } + + const expectedSequence: OutputCheck[] = [ + async (result) => { + // Karma run should succeed. + expect(result?.success).withContext('Initial test run should succeed').toBeTrue(); + // Modify test file to trigger a rebuild + await harness.appendToFile( + 'src/app/app.component.spec.ts', + `\n;console.log('modified');`, + ); + }, + async (result) => { + expect(result?.success).withContext('Test should succeed again').toBeTrue(); + }, + ]; + + const buildCount = await harness + .execute({ outputLogsOnFailure: true }) + .pipe( + timeout(60000), + debounceTime(500), + concatMap(async ({ result }, index) => { + await expectedSequence[index](result); + }), + take(expectedSequence.length), + count(), + ) + .toPromise(); + + expect(buildCount).toBe(expectedSequence.length); + }); }); });