-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use process and net to improve restart and lock
- Loading branch information
Showing
7 changed files
with
86 additions
and
27 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
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
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
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 +1,3 @@ | ||
export const log = console.log.bind(console, '\x1B[36m[node-network-debugger]:', '\x1B[32m') | ||
|
||
export const warn = console.warn.bind(console, '\x1B[36m[node-network-debugger](warn):', '\x1B[33m') |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import fs from 'fs' | ||
|
||
export const unlinkSafe = (path: string) => { | ||
try { | ||
fs.unlinkSync(path) | ||
} catch {} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import net from 'net' | ||
|
||
export const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)) | ||
|
||
export const checkMainProcessAlive = (pid: number | string, port: number) => { | ||
try { | ||
if (Number(pid) === process.pid) { | ||
return Promise.resolve(true) | ||
} | ||
process.kill(Number(pid), 0) | ||
|
||
// 检查 port 是否被占用 | ||
return new Promise<boolean>((resolve) => { | ||
const server = net.createServer() | ||
|
||
server.once('error', (err: any) => { | ||
if (err.code === 'EADDRINUSE') { | ||
// 端口被占用 | ||
resolve(false) | ||
} | ||
// fallback 其他错误 | ||
resolve(false) | ||
}) | ||
|
||
server.once('listening', () => { | ||
// 端口未被占用 | ||
server.close(() => resolve(true)) | ||
}) | ||
|
||
server.listen(port) | ||
}) | ||
} catch { | ||
return Promise.resolve(false) | ||
} | ||
} |
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