Skip to content

Commit

Permalink
refactor(web-server): refactor lib/web-server to be more ES6 (#3034)
Browse files Browse the repository at this point in the history
  • Loading branch information
lusarz authored and johnjbarton committed Jun 11, 2018
1 parent f47d901 commit 5272aa3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 53 deletions.
4 changes: 2 additions & 2 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down
92 changes: 41 additions & 51 deletions lib/web-server.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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))
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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

0 comments on commit 5272aa3

Please sign in to comment.