-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
web-server.js
118 lines (91 loc) · 3.53 KB
/
web-server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
'use strict'
const fs = require('graceful-fs')
const http = require('http')
const https = require('https')
const path = require('path')
const connect = require('connect')
const mimeType = require('mime')
const common = require('./middleware/common')
const runnerMiddleware = require('./middleware/runner')
const stopperMiddleware = require('./middleware/stopper')
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) {
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
let files = fileList.files
emitter.on('file_list_modified', (filesParam) => { files = filesParam })
return {
then (...args) {
return Promise.resolve(files).then(...args)
}
}
}
function createServeStaticFile (config) {
return common.createServeFile(fs, path.normalize(path.join(__dirname, '/../static')), config)
}
createServeStaticFile.$inject = ['config']
function createServeFile (config) {
return common.createServeFile(fs, null, config)
}
createServeFile.$inject = ['config']
function createWebServer (injector, config) {
const { mime = {} } = config
mimeType.define({ ...mime }, true)
const proxyMiddlewareInstance = injector.invoke(proxyMiddleware.create)
log.debug('Instantiating middleware')
const handler = connect()
if (config.beforeMiddleware) {
config.beforeMiddleware.forEach((middleware) => handler.use(injector.get('middleware:' + middleware)))
}
handler.use(injector.invoke(runnerMiddleware.create))
handler.use(injector.invoke(stopperMiddleware.create))
handler.use(injector.invoke(karmaMiddleware.create))
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)))
}
handler.use((request, response) => common.serve404(response, request.url))
let serverClass = http
const serverArguments = [handler]
if (config.protocol === 'https:') {
serverClass = https
serverArguments.unshift(config.httpsServerOptions || {})
}
if (config.httpModule) {
serverClass = config.httpModule
}
const server = serverClass.createServer.apply(null, serverArguments)
server.on('upgrade', function (req, socket, head) {
log.debug(`upgrade ${req.url}`)
proxyMiddlewareInstance.upgrade(req, socket, head)
})
return server
}
createWebServer.$inject = ['injector', 'config']
module.exports = {
createWebServer,
createServeFile,
createServeStaticFile,
createFilesPromise
}