Skip to content

Commit

Permalink
fix: restore customFileHandlers provider
Browse files Browse the repository at this point in the history
The removal of `customFileHandlers` turned out to be more disruptive change than anticipated as this provider is still used by several popular plugins: #3619, codymikol/karma-webpack#462, angular/angular-cli#19815. Hence we restore the provider and print a deprecation warning to make upgrading easier. This should give more time for the plugin authors to release new versions and users to adopt these versions.
  • Loading branch information
devoto13 committed Jan 19, 2021
1 parent 3653caf commit 2a64e7c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ class Server extends KarmaEventEmitter {
filesPromise: ['factory', createFilesPromise],
socketServer: ['factory', createSocketIoServer],
executor: ['factory', Executor.factory],
// TODO: Deprecated. Remove in the next major
customFileHandlers: ['value', []],
reporter: ['factory', reporter.createReporters],
capturedBrowsers: ['factory', BrowserCollection.factory],
args: ['value', {}],
Expand Down
21 changes: 21 additions & 0 deletions lib/web-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@ const proxyMiddleware = require('./middleware/proxy')

const log = require('./logger').create('web-server')

function createCustomHandler (customFileHandlers, config) {
let warningDone = false

return function (request, response, next) {
const handler = customFileHandlers.find((handler) => handler.urlRegex.test(request.url))

if (customFileHandlers.length > 0 && !warningDone) {
warningDone = true
log.warn('The `customFileHandlers` is deprecated and will be removed in Karma 7. Please upgrade plugins relying on this provider.')
}

return handler
? handler.handler(request, response, 'fake/static', 'fake/adapter', config.basePath, 'fake/root')
: next()
}
}

createCustomHandler.$inject = ['customFileHandlers', 'config']

function createFilesPromise (emitter, fileList) {
// Set an empty list of files to avoid race issues with
// file_list_modified not having been emitted yet
Expand Down Expand Up @@ -58,6 +77,8 @@ function createWebServer (injector, config) {
handler.use(injector.invoke(sourceFilesMiddleware.create))
// TODO(vojta): extract the proxy into a plugin
handler.use(proxyMiddlewareInstance)
// TODO: Deprecated. Remove in the next major
handler.use(injector.invoke(createCustomHandler))

if (config.middleware) {
config.middleware.forEach((middleware) => handler.use(injector.get('middleware:' + middleware)))
Expand Down

0 comments on commit 2a64e7c

Please sign in to comment.