-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
web-server.js
84 lines (69 loc) · 2.75 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
var fs = require('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 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) {
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()
}
}
createCustomHandler.$inject = ['customFileHandlers', 'config.basePath']
var createWebServer = function (injector, emitter) {
var config = injector.get('config')
var serveStaticFile = common.createServeFile(fs, path.normalize(__dirname + '/../static'))
var serveFile = common.createServeFile(fs)
var filesPromise = new common.PromiseContainer()
emitter.on('file_list_modified', function (files) {
filesPromise.set(Promise.resolve(files))
})
// locals for webserver module
// NOTE(vojta): figure out how to do this with DI
injector = injector.createChild([{
serveFile: ['value', serveFile],
serveStaticFile: ['value', serveStaticFile],
filesPromise: ['value', filesPromise]
}])
var proxyMiddlewareInstance = injector.invoke(proxyMiddleware.create)
var handler = connect()
.use(injector.invoke(runnerMiddleware.create))
.use(injector.invoke(stripHostMiddleware.create))
.use(injector.invoke(karmaMiddleware.create))
.use(injector.invoke(sourceFilesMiddleware.create))
// TODO(vojta): extract the proxy into a plugin
.use(proxyMiddlewareInstance)
// TODO(vojta): remove, this is only here because of karma-dart
// we need a better way of custom handlers
.use(injector.invoke(createCustomHandler))
.use(function (request, response) {
common.serve404(response, request.url)
})
var serverClass = http
var serverArguments = [handler]
if (config.protocol === 'https:') {
serverClass = https
serverArguments.unshift(config.httpsServerOptions || {})
}
var server = serverClass.createServer.apply(null, serverArguments)
server.on('upgrade', function (req, socket, head) {
log.debug('upgrade %s', req.url)
proxyMiddlewareInstance.upgrade(req, socket, head)
})
return server
}
// PUBLIC API
exports.create = createWebServer