fix(nitro): promisify proxy handler (#398)

This commit is contained in:
Daniel Roe 2021-07-28 18:24:19 +01:00 committed by GitHub
parent 4a989e9b7a
commit 858e31c7fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,10 +1,11 @@
import { Worker } from 'worker_threads' import { Worker } from 'worker_threads'
import { IncomingMessage, ServerResponse } from 'http'
import { loading as loadingTemplate } from '@nuxt/design' import { loading as loadingTemplate } from '@nuxt/design'
import chokidar, { FSWatcher } from 'chokidar' import chokidar, { FSWatcher } from 'chokidar'
import debounce from 'debounce' import debounce from 'debounce'
import { stat } from 'fs-extra' import { stat } from 'fs-extra'
import { createApp, Middleware, useBase } from 'h3' import { promisifyHandle, createApp, Middleware, useBase } from 'h3'
import { createProxy } from 'http-proxy' import { createProxy } from 'http-proxy'
import { listen, Listener, ListenOptions } from 'listhen' import { listen, Listener, ListenOptions } from 'listhen'
import servePlaceholder from 'serve-placeholder' import servePlaceholder from 'serve-placeholder'
@ -72,6 +73,9 @@ export function createDevServer (nitroContext: NitroContext) {
// SSR Proxy // SSR Proxy
const proxy = createProxy() const proxy = createProxy()
const proxyHandle = promisifyHandle((req: IncomingMessage, res: ServerResponse) => proxy.web(req, res, { target: workerAddress }, (_err: unknown) => {
// console.error('[proxy]', err)
}))
app.use((req, res) => { app.use((req, res) => {
if (workerAddress) { if (workerAddress) {
// Workaround to pass legacy req.spa to proxy // Workaround to pass legacy req.spa to proxy
@ -79,9 +83,7 @@ export function createDevServer (nitroContext: NitroContext) {
if (req.spa) { if (req.spa) {
req.headers['x-nuxt-no-ssr'] = 'true' req.headers['x-nuxt-no-ssr'] = 'true'
} }
proxy.web(req, res, { target: workerAddress }, (_err: unknown) => { return proxyHandle(req, res)
// console.error('[proxy]', err)
})
} else { } else {
res.setHeader('Content-Type', 'text/html; charset=UTF-8') res.setHeader('Content-Type', 'text/html; charset=UTF-8')
res.end(loadingTemplate({})) res.end(loadingTemplate({}))