-
Notifications
You must be signed in to change notification settings - Fork 509
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nitro): improve dev worker stability (#1303)
- Loading branch information
Showing
2 changed files
with
76 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,26 @@ | ||
import '#polyfill' | ||
import { Server } from 'http' | ||
import { parentPort } from 'worker_threads' | ||
import type { AddressInfo } from 'net' | ||
import { tmpdir } from 'os' | ||
import { join } from 'path' | ||
import { mkdirSync } from 'fs' | ||
import { threadId, parentPort } from 'worker_threads' | ||
import { handle } from '../server' | ||
|
||
const server = new Server(handle) | ||
|
||
const netServer = server.listen(0, () => { | ||
parentPort.postMessage({ | ||
event: 'listen', | ||
port: (netServer.address() as AddressInfo).port | ||
}) | ||
function createSocket () { | ||
const isWin = process.platform === 'win32' | ||
const socketName = `worker-${process.pid}-${threadId}.sock` | ||
if (isWin) { | ||
return join('\\\\.\\pipe\\nitro', socketName) | ||
} else { | ||
const socketDir = join(tmpdir(), 'nitro') | ||
mkdirSync(socketDir, { recursive: true }) | ||
return join(socketDir, socketName) | ||
} | ||
} | ||
|
||
const socketPath = createSocket() | ||
server.listen(socketPath, () => { | ||
parentPort.postMessage({ event: 'listen', address: { socketPath } }) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters