Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: use accessSync and util
  • Loading branch information
pi0 committed Jun 28, 2023
commit 47cc4a9899af8dc0d1613c6e29112a7371baacc2
29 changes: 19 additions & 10 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 @@ -186,18 +186,27 @@ 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 {
return;
}
}
return address;
};

app.use(
eventHandler(async (event) => {
await reloadPromise;
const address = currentWorker && currentWorker.address;
if (
!address ||
(address.socketPath &&
!(await fsp.access(address.socketPath).then(
() => true,
() => false
)))
) {
const address = getWorkerAddress();
if (!address) {
return errorHandler(lastError, event);
}
await proxy.handle(event, { target: address }).catch((err) => {
Expand Down