From 5272aa3770f22b9ff63a3bf14277471795d1aa8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Usarz?= Date: Tue, 12 Jun 2018 01:47:10 +0200 Subject: [PATCH] refactor(web-server): refactor lib/web-server to be more ES6 (#3034) --- lib/server.js | 4 +-- lib/web-server.js | 92 +++++++++++++++++++++-------------------------- 2 files changed, 43 insertions(+), 53 deletions(-) diff --git a/lib/server.js b/lib/server.js index 1f2054e26..e4b2c2e5a 100644 --- a/lib/server.js +++ b/lib/server.js @@ -17,7 +17,7 @@ const constant = require('./constants') const watcher = require('./watcher') const plugin = require('./plugin') -const ws = require('./web-server') +const createWebServer = require('./web-server').createWebServer const preprocessor = require('./preprocessor') const Launcher = require('./launcher').Launcher const FileList = require('./file-list') @@ -68,7 +68,7 @@ class Server extends KarmaEventEmitter { config: ['value', config], preprocess: ['factory', preprocessor.createPreprocessor], fileList: ['factory', FileList.factory], - webServer: ['factory', ws.create], + webServer: ['factory', createWebServer], socketServer: ['factory', createSocketIoServer], executor: ['factory', Executor.factory], // TODO(vojta): remove diff --git a/lib/web-server.js b/lib/web-server.js index 8df084d5d..9534ff9a9 100644 --- a/lib/web-server.js +++ b/lib/web-server.js @@ -1,49 +1,45 @@ -var fs = require('graceful-fs') -var http = require('http') -var https = require('https') -var path = require('path') -var connect = require('connect') -var Promise = require('bluebird') - -var common = require('./middleware/common') -var runnerMiddleware = require('./middleware/runner') -var stopperMiddleware = require('./middleware/stopper') -var stripHostMiddleware = require('./middleware/strip_host') -var karmaMiddleware = require('./middleware/karma') -var sourceFilesMiddleware = require('./middleware/source_files') -var proxyMiddleware = require('./middleware/proxy') - -var log = require('./logger').create('web-server') - -var createCustomHandler = function (customFileHandlers, /* config.basePath */ basePath) { +'use strict' + +const fs = require('graceful-fs') +const http = require('http') +const https = require('https') +const path = require('path') +const connect = require('connect') +const Promise = require('bluebird') + +const common = require('./middleware/common') +const runnerMiddleware = require('./middleware/runner') +const stopperMiddleware = require('./middleware/stopper') +const stripHostMiddleware = require('./middleware/strip_host') +const karmaMiddleware = require('./middleware/karma') +const sourceFilesMiddleware = require('./middleware/source_files') +const proxyMiddleware = require('./middleware/proxy') + +const log = require('./logger').create('web-server') + +function createCustomHandler (customFileHandlers, config) { return function (request, response, next) { - for (var i = 0; i < customFileHandlers.length; i++) { - if (customFileHandlers[i].urlRegex.test(request.url)) { - return customFileHandlers[i].handler(request, response, 'fake/static', 'fake/adapter', - basePath, 'fake/root') - } - } - - return next() + const handler = customFileHandlers.find((handler) => handler.urlRegex.test(request.url)) + return handler + ? handler.handler(request, response, 'fake/static', 'fake/adapter', config.basePath, 'fake/root') + : next() } } -createCustomHandler.$inject = ['customFileHandlers', 'config.basePath'] +createCustomHandler.$inject = ['customFileHandlers', 'config'] -var createWebServer = function (injector, emitter, fileList) { - var config = injector.get('config') +function createWebServer (injector, emitter, fileList) { + const config = injector.get('config') common.initializeMimeTypes(config) - var serveStaticFile = common.createServeFile(fs, path.normalize(path.join(__dirname, '/../static')), config) - var serveFile = common.createServeFile(fs, null, config) - var filesPromise = new common.PromiseContainer() + const serveStaticFile = common.createServeFile(fs, path.normalize(path.join(__dirname, '/../static')), config) + const serveFile = common.createServeFile(fs, null, config) + const filesPromise = new common.PromiseContainer() // Set an empty list of files to avoid race issues with // file_list_modified not having been emitted yet filesPromise.set(Promise.resolve(fileList.files)) - emitter.on('file_list_modified', function (files) { - filesPromise.set(Promise.resolve(files)) - }) + emitter.on('file_list_modified', (files) => filesPromise.set(Promise.resolve(files))) // locals for webserver module // NOTE(vojta): figure out how to do this with DI @@ -53,15 +49,13 @@ var createWebServer = function (injector, emitter, fileList) { filesPromise: ['value', filesPromise] }]) - var proxyMiddlewareInstance = injector.invoke(proxyMiddleware.create) + const proxyMiddlewareInstance = injector.invoke(proxyMiddleware.create) log.debug('Instantiating middleware') - var handler = connect() + const handler = connect() if (config.beforeMiddleware) { - config.beforeMiddleware.forEach(function (middleware) { - handler.use(injector.get('middleware:' + middleware)) - }) + config.beforeMiddleware.forEach((middleware) => handler.use(injector.get('middleware:' + middleware))) } handler.use(injector.invoke(runnerMiddleware.create)) @@ -76,17 +70,13 @@ var createWebServer = function (injector, emitter, fileList) { handler.use(injector.invoke(createCustomHandler)) if (config.middleware) { - config.middleware.forEach(function (middleware) { - handler.use(injector.get('middleware:' + middleware)) - }) + config.middleware.forEach((middleware) => handler.use(injector.get('middleware:' + middleware))) } - handler.use(function (request, response) { - common.serve404(response, request.url) - }) + handler.use((request, response) => common.serve404(response, request.url)) - var serverClass = http - var serverArguments = [handler] + let serverClass = http + const serverArguments = [handler] if (config.protocol === 'https:') { serverClass = https @@ -97,7 +87,7 @@ var createWebServer = function (injector, emitter, fileList) { serverClass = config.httpModule } - var server = serverClass.createServer.apply(null, serverArguments) + const server = serverClass.createServer.apply(null, serverArguments) server.on('upgrade', function (req, socket, head) { log.debug('upgrade %s', req.url) @@ -107,5 +97,5 @@ var createWebServer = function (injector, emitter, fileList) { return server } -// PUBLIC API -exports.create = createWebServer +createWebServer.$inject = ['injector', 'emitter', 'fileList'] +exports.createWebServer = createWebServer