source: trip-planner-front/node_modules/karma/lib/web-server.js@ 59329aa

Last change on this file since 59329aa was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.5 KB
Line 
1'use strict'
2
3const fs = require('graceful-fs')
4const http = require('http')
5const https = require('https')
6const path = require('path')
7const connect = require('connect')
8const mimeType = require('mime')
9
10const common = require('./middleware/common')
11const runnerMiddleware = require('./middleware/runner')
12const stopperMiddleware = require('./middleware/stopper')
13const karmaMiddleware = require('./middleware/karma')
14const sourceFilesMiddleware = require('./middleware/source_files')
15const proxyMiddleware = require('./middleware/proxy')
16
17const log = require('./logger').create('web-server')
18
19function 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
36createCustomHandler.$inject = ['customFileHandlers', 'config']
37
38function 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
51function createServeStaticFile (config) {
52 return common.createServeFile(fs, path.normalize(path.join(__dirname, '/../static')), config)
53}
54createServeStaticFile.$inject = ['config']
55
56function createServeFile (config) {
57 return common.createServeFile(fs, null, config)
58}
59createServeFile.$inject = ['config']
60
61function 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
111createWebServer.$inject = ['injector', 'config']
112
113module.exports = {
114 createWebServer,
115 createServeFile,
116 createServeStaticFile,
117 createFilesPromise
118}
Note: See TracBrowser for help on using the repository browser.