From 921bb15130e37fb81a7fc8f1c1f9f45eeddecd7c Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Mon, 7 Dec 2020 14:09:09 +0100 Subject: [PATCH] feat: swtich to h2 stack for dev server --- packages/nitro/src/server.ts | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/packages/nitro/src/server.ts b/packages/nitro/src/server.ts index 476c441465..7393a1a3ed 100644 --- a/packages/nitro/src/server.ts +++ b/packages/nitro/src/server.ts @@ -1,10 +1,9 @@ import { Worker } from 'worker_threads' -import { Server } from 'http' import { resolve } from 'upath' import debounce from 'debounce' -import connect from 'connect' -import getPort from 'get-port-please' import chokidar from 'chokidar' +import { createApp } from '@nuxt/h2' +import { listen, Listener } from 'listhen' import serveStatic from 'serve-static' import { createProxy } from 'http-proxy' import { stat } from 'fs-extra' @@ -48,7 +47,7 @@ export function createDevServer (sigmaContext: SigmaContext) { } // App - const app = connect() + const app = createApp() // _nuxt and static app.use(sigmaContext._nuxt.publicPath, serveStatic(resolve(sigmaContext._nuxt.buildDir, 'dist/client'))) @@ -86,16 +85,11 @@ export function createDevServer (sigmaContext: SigmaContext) { }) // Listen - let listeners: Server[] = [] - async function listen (port) { - port = await getPort({ name: 'nuxt' }) - const listener = await new Promise((resolve, reject) => { - const l = app.listen(port, err => err ? reject(err) : resolve(l)) - }) + let listeners: Listener[] = [] + const _listen = async (port) => { + const listener = await listen(app, { port }) listeners.push(listener) - return { - url: 'http://localhost:' + port - } + return listener } // Watch for dist and reload worker @@ -122,16 +116,14 @@ export function createDevServer (sigmaContext: SigmaContext) { if (pendingWorker) { await pendingWorker.terminate() } - await Promise.all(listeners.map(l => new Promise((resolve) => { - l.close(() => resolve(undefined)) - }))) + await Promise.all(listeners.map(l => l.close())) listeners = [] } sigmaContext._internal.hooks.hook('close', close) return { reload, - listen, + listen: _listen, close, watch, setLoadingMiddleware,