[6a3a178] | 1 | 'use strict'
|
---|
| 2 |
|
---|
| 3 | const fs = require('graceful-fs')
|
---|
| 4 | const http = require('http')
|
---|
| 5 | const https = require('https')
|
---|
| 6 | const path = require('path')
|
---|
| 7 | const connect = require('connect')
|
---|
| 8 | const mimeType = require('mime')
|
---|
| 9 |
|
---|
| 10 | const common = require('./middleware/common')
|
---|
| 11 | const runnerMiddleware = require('./middleware/runner')
|
---|
| 12 | const stopperMiddleware = require('./middleware/stopper')
|
---|
| 13 | const karmaMiddleware = require('./middleware/karma')
|
---|
| 14 | const sourceFilesMiddleware = require('./middleware/source_files')
|
---|
| 15 | const proxyMiddleware = require('./middleware/proxy')
|
---|
| 16 |
|
---|
| 17 | const log = require('./logger').create('web-server')
|
---|
| 18 |
|
---|
| 19 | function createCustomHandler (customFileHandlers, config) {
|
---|
| 20 | let warningDone = false
|
---|
| 21 |
|
---|
| 22 | return function (request, response, next) {
|
---|
| 23 | const handler = customFileHandlers.find((handler) => handler.urlRegex.test(request.url))
|
---|
| 24 |
|
---|
| 25 | if (customFileHandlers.length > 0 && !warningDone) {
|
---|
| 26 | warningDone = true
|
---|
| 27 | log.warn('The `customFileHandlers` is deprecated and will be removed in Karma 7. Please upgrade plugins relying on this provider.')
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | return handler
|
---|
| 31 | ? handler.handler(request, response, 'fake/static', 'fake/adapter', config.basePath, 'fake/root')
|
---|
| 32 | : next()
|
---|
| 33 | }
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | createCustomHandler.$inject = ['customFileHandlers', 'config']
|
---|
| 37 |
|
---|
| 38 | function createFilesPromise (emitter, fileList) {
|
---|
| 39 | // Set an empty list of files to avoid race issues with
|
---|
| 40 | // file_list_modified not having been emitted yet
|
---|
| 41 | let files = fileList.files
|
---|
| 42 | emitter.on('file_list_modified', (filesParam) => { files = filesParam })
|
---|
| 43 |
|
---|
| 44 | return {
|
---|
| 45 | then (...args) {
|
---|
| 46 | return Promise.resolve(files).then(...args)
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | function createServeStaticFile (config) {
|
---|
| 52 | return common.createServeFile(fs, path.normalize(path.join(__dirname, '/../static')), config)
|
---|
| 53 | }
|
---|
| 54 | createServeStaticFile.$inject = ['config']
|
---|
| 55 |
|
---|
| 56 | function createServeFile (config) {
|
---|
| 57 | return common.createServeFile(fs, null, config)
|
---|
| 58 | }
|
---|
| 59 | createServeFile.$inject = ['config']
|
---|
| 60 |
|
---|
| 61 | function createWebServer (injector, config) {
|
---|
| 62 | const { mime = {} } = config
|
---|
| 63 | mimeType.define({ ...mime }, true)
|
---|
| 64 |
|
---|
| 65 | const proxyMiddlewareInstance = injector.invoke(proxyMiddleware.create)
|
---|
| 66 |
|
---|
| 67 | log.debug('Instantiating middleware')
|
---|
| 68 | const handler = connect()
|
---|
| 69 |
|
---|
| 70 | if (config.beforeMiddleware) {
|
---|
| 71 | config.beforeMiddleware.forEach((middleware) => handler.use(injector.get('middleware:' + middleware)))
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | handler.use(injector.invoke(runnerMiddleware.create))
|
---|
| 75 | handler.use(injector.invoke(stopperMiddleware.create))
|
---|
| 76 | handler.use(injector.invoke(karmaMiddleware.create))
|
---|
| 77 | handler.use(injector.invoke(sourceFilesMiddleware.create))
|
---|
| 78 | // TODO(vojta): extract the proxy into a plugin
|
---|
| 79 | handler.use(proxyMiddlewareInstance)
|
---|
| 80 | // TODO: Deprecated. Remove in the next major
|
---|
| 81 | handler.use(injector.invoke(createCustomHandler))
|
---|
| 82 |
|
---|
| 83 | if (config.middleware) {
|
---|
| 84 | config.middleware.forEach((middleware) => handler.use(injector.get('middleware:' + middleware)))
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | handler.use((request, response) => common.serve404(response, request.url))
|
---|
| 88 |
|
---|
| 89 | let serverClass = http
|
---|
| 90 | const serverArguments = [handler]
|
---|
| 91 |
|
---|
| 92 | if (config.protocol === 'https:') {
|
---|
| 93 | serverClass = https
|
---|
| 94 | serverArguments.unshift(config.httpsServerOptions || {})
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | if (config.httpModule) {
|
---|
| 98 | serverClass = config.httpModule
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | const server = serverClass.createServer.apply(null, serverArguments)
|
---|
| 102 |
|
---|
| 103 | server.on('upgrade', function (req, socket, head) {
|
---|
| 104 | log.debug(`upgrade ${req.url}`)
|
---|
| 105 | proxyMiddlewareInstance.upgrade(req, socket, head)
|
---|
| 106 | })
|
---|
| 107 |
|
---|
| 108 | return server
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | createWebServer.$inject = ['injector', 'config']
|
---|
| 112 |
|
---|
| 113 | module.exports = {
|
---|
| 114 | createWebServer,
|
---|
| 115 | createServeFile,
|
---|
| 116 | createServeStaticFile,
|
---|
| 117 | createFilesPromise
|
---|
| 118 | }
|
---|