From 22dab60c0b33b5ed7500d4985a0a2d49c75ce017 Mon Sep 17 00:00:00 2001 From: Stefan Bittmann Date: Wed, 28 Jun 2023 13:01:42 +0200 Subject: [PATCH] fix(dev): ensure socket path is accessable (#1115) * fix: windows dev-Server returns 503 * refactor: use accessSync and util * set access error if no other errors * suppress error for removing socket --------- Co-authored-by: Pooya Parsa --- src/dev/server.ts | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/dev/server.ts b/src/dev/server.ts index 8411af318a..667d762097 100644 --- a/src/dev/server.ts +++ b/src/dev/server.ts @@ -1,5 +1,5 @@ import { Worker } from "node:worker_threads"; -import { existsSync, promises as fsp } from "node:fs"; +import { existsSync, accessSync, promises as fsp } from "node:fs"; import { debounce } from "perfect-debounce"; import { App, @@ -95,7 +95,7 @@ async function killWorker(worker: NitroWorker, nitro: Nitro) { worker.worker = null; } if (worker.address.socketPath && existsSync(worker.address.socketPath)) { - await fsp.rm(worker.address.socketPath); + await fsp.rm(worker.address.socketPath).catch(() => {}); } } @@ -186,11 +186,30 @@ export function createDevServer(nitro: Nitro): NitroDevServer { proxyReq.setHeader("X-Forwarded-Proto", req.socket.remoteFamily); } }); + + const getWorkerAddress = () => { + const address = currentWorker?.address; + if (!address) { + return; + } + if (address.socketPath) { + try { + accessSync(address.socketPath); + } catch (err) { + if (!lastError) { + lastError = err; + } + return; + } + } + return address; + }; + app.use( eventHandler(async (event) => { await reloadPromise; - const address = currentWorker && currentWorker.address; - if (!address || (address.socketPath && !existsSync(address.socketPath))) { + const address = getWorkerAddress(); + if (!address) { return errorHandler(lastError, event); } await proxy.handle(event, { target: address }).catch((err) => {