|
@@ -1,7 +1,6 @@
|
|
|
import { spawn, type ChildProcess } from 'node:child_process'
|
|
import { spawn, type ChildProcess } from 'node:child_process'
|
|
|
-import { existsSync } from 'node:fs'
|
|
|
|
|
|
|
+import { existsSync, readdirSync } from 'node:fs'
|
|
|
import { join } from 'node:path'
|
|
import { join } from 'node:path'
|
|
|
-import { connect } from 'node:net'
|
|
|
|
|
import { getPipeBundleDir } from './pipe-path'
|
|
import { getPipeBundleDir } from './pipe-path'
|
|
|
import { PIPE_PATH } from './client'
|
|
import { PIPE_PATH } from './client'
|
|
|
|
|
|
|
@@ -16,34 +15,22 @@ function getPipeServerExecutablePath(): string {
|
|
|
return join(dir, 'pipe')
|
|
return join(dir, 'pipe')
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-function probeNamedPipe(timeoutMs: number): Promise<boolean> {
|
|
|
|
|
- return new Promise((resolve) => {
|
|
|
|
|
- let finished = false
|
|
|
|
|
- const socket = connect(PIPE_PATH)
|
|
|
|
|
- const timer = setTimeout(() => {
|
|
|
|
|
- if (finished) return
|
|
|
|
|
- finished = true
|
|
|
|
|
- socket.destroy()
|
|
|
|
|
- resolve(false)
|
|
|
|
|
- }, timeoutMs)
|
|
|
|
|
-
|
|
|
|
|
- const done = (ok: boolean) => {
|
|
|
|
|
- if (finished) return
|
|
|
|
|
- finished = true
|
|
|
|
|
- clearTimeout(timer)
|
|
|
|
|
- socket.destroy()
|
|
|
|
|
- resolve(ok)
|
|
|
|
|
- }
|
|
|
|
|
|
|
+function getPipeName(pipePath: string): string {
|
|
|
|
|
+ return pipePath.replace(/^\\\\\.\\pipe\\/, '')
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- socket.once('connect', () => done(true))
|
|
|
|
|
- socket.once('error', () => done(false))
|
|
|
|
|
- })
|
|
|
|
|
|
|
+function hasNamedPipe(pipePath: string): boolean {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return readdirSync('\\\\.\\pipe\\').includes(getPipeName(pipePath))
|
|
|
|
|
+ } catch {
|
|
|
|
|
+ return false
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-async function waitUntilPipeAccepts(totalMs: number, intervalMs: number): Promise<void> {
|
|
|
|
|
|
|
+async function waitUntilPipeAppears(totalMs: number, intervalMs: number): Promise<void> {
|
|
|
const deadline = Date.now() + totalMs
|
|
const deadline = Date.now() + totalMs
|
|
|
while (Date.now() < deadline) {
|
|
while (Date.now() < deadline) {
|
|
|
- if (await probeNamedPipe(500)) {
|
|
|
|
|
|
|
+ if (hasNamedPipe(PIPE_PATH)) {
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
await new Promise((r) => setTimeout(r, intervalMs))
|
|
await new Promise((r) => setTimeout(r, intervalMs))
|
|
@@ -63,7 +50,7 @@ export async function ensurePipeServerRunning(): Promise<void> {
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- if (await probeNamedPipe(300)) {
|
|
|
|
|
|
|
+ if (hasNamedPipe(PIPE_PATH)) {
|
|
|
console.log('[pipe] pipe server already running')
|
|
console.log('[pipe] pipe server already running')
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
@@ -96,7 +83,7 @@ export async function ensurePipeServerRunning(): Promise<void> {
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
- await waitUntilPipeAccepts(25_000, 200)
|
|
|
|
|
|
|
+ await waitUntilPipeAppears(25_000, 200)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
export function stopSpawnedPipeServer(): void {
|
|
export function stopSpawnedPipeServer(): void {
|