Skip to content

Commit 4de0f81

Browse files
clydinalan-agius4
authored andcommitted
fix(@angular-devkit/build-angular): use URL rewrite in karma fallback middleware
This changes the internal Angular Karma plugin's asset fallback middleware to rewrite the request URL directly instead of trying to copy the request properties. With changes in newer Node.js versions, not all request properties may be enumerable. Fixes: #19644 (cherry picked from commit 0e5dab4)
1 parent 9fc3459 commit 4de0f81

File tree

1 file changed

+16
-15
lines changed
  • packages/angular_devkit/build_angular/src/webpack/plugins

1 file changed

+16
-15
lines changed

packages/angular_devkit/build_angular/src/webpack/plugins/karma.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
// tslint:disable
99
// TODO: cleanup this file, it's copied as is from Angular CLI.
10-
10+
import * as http from 'http';
1111
import * as path from 'path';
1212
import * as glob from 'glob';
1313
import * as webpack from 'webpack';
@@ -29,6 +29,8 @@ import { normalizeSourceMaps } from '../../utils/index';
2929
* require('karma-source-map-support')
3030
*/
3131

32+
const KARMA_APPLICATION_PATH = '_karma_webpack_';
33+
3234

3335
let blocked: any[] = [];
3436
let isBlocked = false;
@@ -121,7 +123,7 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {
121123
// Hide webpack output because its noisy.
122124
logLevel: 'error',
123125
stats: false,
124-
publicPath: '/_karma_webpack_/',
126+
publicPath: `/${KARMA_APPLICATION_PATH}/`,
125127
};
126128

127129
const compilationErrorCb = (error: string | undefined, errors: string[]) => {
@@ -165,8 +167,8 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {
165167
});
166168
}
167169
// Files need to be served from a custom path for Karma.
168-
webpackConfig.output.path = '/_karma_webpack_/';
169-
webpackConfig.output.publicPath = '/_karma_webpack_/';
170+
webpackConfig.output.path = `/${KARMA_APPLICATION_PATH}/`;
171+
webpackConfig.output.publicPath = `/${KARMA_APPLICATION_PATH}/`;
170172

171173
let compiler: any;
172174
try {
@@ -221,18 +223,18 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {
221223

222224
// Forward requests to webpack server.
223225
customFileHandlers.push({
224-
urlRegex: /^\/_karma_webpack_\/.*/,
226+
urlRegex: new RegExp(`\\/${KARMA_APPLICATION_PATH}\\/.*`),
225227
handler: function handler(req: any, res: any) {
226228
webpackMiddleware(req, res, function () {
227229
// Ensure script and style bundles are served.
228230
// They are mentioned in the custom karma context page and we don't want them to 404.
229231
const alwaysServe = [
230-
'/_karma_webpack_/runtime.js',
231-
'/_karma_webpack_/polyfills.js',
232-
'/_karma_webpack_/polyfills-es5.js',
233-
'/_karma_webpack_/scripts.js',
234-
'/_karma_webpack_/styles.js',
235-
'/_karma_webpack_/vendor.js',
232+
`/${KARMA_APPLICATION_PATH}/runtime.js`,
233+
`/${KARMA_APPLICATION_PATH}/polyfills.js`,
234+
`/${KARMA_APPLICATION_PATH}/polyfills-es5.js`,
235+
`/${KARMA_APPLICATION_PATH}/scripts.js`,
236+
`/${KARMA_APPLICATION_PATH}/styles.js`,
237+
`/${KARMA_APPLICATION_PATH}/vendor.js`,
236238
];
237239
if (alwaysServe.indexOf(req.url) != -1) {
238240
res.statusCode = 200;
@@ -325,11 +327,10 @@ sourceMapReporter.$inject = ['baseReporterDecorator', 'config'];
325327

326328
// When a request is not found in the karma server, try looking for it from the webpack server root.
327329
function fallbackMiddleware() {
328-
return function (req: any, res: any, next: () => void) {
330+
return function (request: http.IncomingMessage, response: http.ServerResponse, next: () => void) {
329331
if (webpackMiddleware) {
330-
const webpackUrl = '/_karma_webpack_' + req.url;
331-
const webpackReq = { ...req, url: webpackUrl }
332-
webpackMiddleware(webpackReq, res, next);
332+
request.url = '/' + KARMA_APPLICATION_PATH + request.url;
333+
webpackMiddleware(request, response, next);
333334
} else {
334335
next();
335336
}

0 commit comments

Comments
 (0)