From 07dee914a10e53d5ab7ecfebc64ced9dd58c5aba Mon Sep 17 00:00:00 2001 From: Stephen Demjanenko Date: Sat, 28 Mar 2020 11:08:00 -0700 Subject: [PATCH] Local server: split apart the constructor This method was getting pretty hard to follow. --- lib/client/index.js | 1 + lib/local-server/index.js | 71 +++++++++++++++++++++++++++------------ 2 files changed, 50 insertions(+), 22 deletions(-) diff --git a/lib/client/index.js b/lib/client/index.js index 0bea238..01922f3 100644 --- a/lib/client/index.js +++ b/lib/client/index.js @@ -37,6 +37,7 @@ if (config.webpack) { // boot up webpack (if configured) Zen.webpack = new WebpackAdapter() } +// worker and head are the only supported pageTypes right now Zen.indexHtml = function indexHtml (pageType, forS3) { let deps = ['lib/client/latte.js'] if (pageType == 'head') { diff --git a/lib/local-server/index.js b/lib/local-server/index.js index 1410323..5a479a8 100644 --- a/lib/local-server/index.js +++ b/lib/local-server/index.js @@ -16,46 +16,73 @@ module.exports = class Server { this.results = [] // results of all tests run this.passedFocus = [] // all tests that passed after running + const {app, server} = this.createWebApp(Zen.config) + this.startWebpackServer(Zen.webpack, app) + + Zen.s3Sync.on('status', this.sendStatus.bind(this)) + new WebSocket.Server({server}).on('connection', this.onWebsocket.bind(this)) + + this.chrome = new ChromeWrapper() // headless chrome instance + this.chrome.launchLocal({port: 9222}) + + this.startWorkers(Zen.config, app) + } + + createWebApp(config) { // start up the local webserver for `head` to connect to - let app = connect() - let server = http.createServer(app).listen(Zen.config.port) + const app = connect() + const server = http.createServer(app).listen(config.port) app.use('/lib', Util.serveWith404(path.join(__dirname, '..'))) // serve up stuff out of lib - app.use('/node_modules', Util.serveWith404(path.resolve(Zen.config.appRoot, './node_modules'))) // TODO: this doesn't work when yarn link'd - app.use('/base', Util.serveWith404(Zen.config.appRoot)) // base serves things out of the application's root + app.use('/node_modules', Util.serveWith404(path.resolve(config.appRoot, './node_modules'))) // TODO: this doesn't work when yarn link'd + app.use('/base', Util.serveWith404(config.appRoot)) // base serves things out of the application's root app.use('/svelte', Util.serveSvelte) app.use('/icons', Util.serveIcons) - if (Zen.webpack) { - Zen.webpack.startDevServer(app) - Zen.webpack.on('status', stats => { - if (Zen.webpack.status == 'done') { - this.workers.forEach(w => w.tab && w.tab.setCodeHash(Zen.webpack.compile.hash)) + // host worker and head + app.use(async (req, resp, next) => { + let pageType + if (req.url.match(/^\/worker/)) { + pageType = 'worker' + } else { + const pathname = req._parsedUrl.pathname + if (pathname.match(/^\/(index(\.htm(l)?)?)?$/)) { + pageType = 'head' + } + } + + if (pageType) { + resp.end(Zen.indexHtml(pageType)) + } else { + next() + } + }) + + return {app, server} + } + + startWebpackServer(webpack, app) { + if (webpack) { + webpack.startDevServer(app) + webpack.on('status', stats => { + if (webpack.status == 'done') { + this.workers.forEach(w => w.tab && w.tab.setCodeHash(webpack.compile.hash)) } this.sendStatus() // notify head of the compile status }) } + } - Zen.s3Sync.on('status', this.sendStatus.bind(this)) - - this.chrome = new ChromeWrapper() // headless chrome instance - this.chrome.launchLocal({port: 9222}) - new WebSocket.Server({server}).on('connection', this.onWebsocket.bind(this)) - + startWorkers(config, app) { // create a server for each worker. This gives us different origins and isolates things like localStorage this.workers = Array.construct(8, id => { id = id + 1 - let port = Zen.config.port + id + let port = config.port + id http.createServer(app).listen(port) let worker = {id, port} - this.chrome.openTab(`http://localhost:${port}/worker?id=${id}`, `w${id}`, Zen.config) + this.chrome.openTab(`http://localhost:${port}/worker?id=${id}`, `w${id}`, config) .then(t => worker.tab = t) return worker }) - - // host worker and head. NB this should go last after all other `app.use()` calls - app.use(async (req, resp) => { - resp.end(Zen.indexHtml(req.url.match(/^\/worker/) ? 'worker' : 'head')) - }) } filterTests (msg) {