Skip to content

Commit

Permalink
fix(dev): ensure socket path is accessable (#1115)
Browse files Browse the repository at this point in the history
* 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 <pooya@pi0.io>
  • Loading branch information
sbittmann and pi0 authored Jun 28, 2023
1 parent 373f34c commit 22dab60
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/dev/server.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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(() => {});
}
}

Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit 22dab60

Please sign in to comment.