@@ -10,9 +10,9 @@ import {
1010 getWindowsSystem32ExePath ,
1111 getWindowsWmicExePath ,
1212} from "./windows-install-roots.js" ;
13+ import { decodeWindowsProcessOutput } from "./windows-process-start.js" ;
1314
1415const DEFAULT_TIMEOUT_MS = 5_000 ;
15- const DEFAULT_PROCESS_START_TIMEOUT_MS = 10_000 ;
1616
1717export type WindowsListeningPidsResult =
1818 | { ok : true ; pids : number [ ] }
@@ -85,15 +85,6 @@ export function readWindowsListeningPidsResultSync(
8585// Windows process identity reading (PowerShell → WMIC fallback)
8686// ---------------------------------------------------------------------------
8787
88- function decodeWindowsProcessOutput ( output : Buffer | string ) : string {
89- if ( ! Buffer . isBuffer ( output ) ) {
90- return output ;
91- }
92- return output . length >= 2 && output [ 0 ] === 0xff && output [ 1 ] === 0xfe
93- ? output . toString ( "utf16le" )
94- : output . toString ( "utf8" ) ;
95- }
96-
9788function extractWindowsCommandLine ( raw : Buffer | string ) : string | null {
9889 const lines = normalizeStringEntries ( decodeWindowsProcessOutput ( raw ) . split ( / \r ? \n / ) ) ;
9990 for ( const line of lines ) {
@@ -106,85 +97,6 @@ function extractWindowsCommandLine(raw: Buffer | string): string | null {
10697 return lines . find ( ( line ) => normalizeLowercaseStringOrEmpty ( line ) !== "commandline" ) ?? null ;
10798}
10899
109- function parseWindowsProcessStartTime ( raw : Buffer | string ) : number | null {
110- const lines = normalizeStringEntries ( decodeWindowsProcessOutput ( raw ) . split ( / \r ? \n / ) ) ;
111- const value =
112- lines
113- . find ( ( line ) => normalizeLowercaseStringOrEmpty ( line ) . startsWith ( "creationdate=" ) )
114- ?. slice ( "creationdate=" . length )
115- . trim ( ) ??
116- lines . find ( ( line ) => normalizeLowercaseStringOrEmpty ( line ) !== "creationdate" ) ??
117- "" ;
118- const parsedIso = Date . parse ( value ) ;
119- if ( Number . isFinite ( parsedIso ) ) {
120- return parsedIso ;
121- }
122- const dmtf = value . match ( / ^ ( \d { 4 } ) ( \d { 2 } ) ( \d { 2 } ) ( \d { 2 } ) ( \d { 2 } ) ( \d { 2 } ) \. ( \d { 6 } ) ( [ + - ] ) ( \d { 3 } ) $ / ) ;
123- if ( ! dmtf ) {
124- return null ;
125- }
126- const [ , year , month , day , hour , minute , second , microseconds , offsetSign , offset ] = dmtf ;
127- const localTimeMs = Date . UTC (
128- Number ( year ) ,
129- Number ( month ) - 1 ,
130- Number ( day ) ,
131- Number ( hour ) ,
132- Number ( minute ) ,
133- Number ( second ) ,
134- Math . floor ( Number ( microseconds ) / 1000 ) ,
135- ) ;
136- const offsetMs = Number ( offset ) * 60_000 * ( offsetSign === "+" ? 1 : - 1 ) ;
137- return localTimeMs - offsetMs ;
138- }
139-
140- /** Read a stable Windows process creation time for lock-owner identity checks. */
141- export function readWindowsProcessStartTimeSync (
142- pid : number ,
143- timeoutMs = DEFAULT_PROCESS_START_TIMEOUT_MS ,
144- ) : number | null {
145- if ( ! Number . isInteger ( pid ) || pid <= 0 ) {
146- return null ;
147- }
148- // Keep the former default availability: PowerShell and WMIC each had 5s.
149- // Explicit callers still get one end-to-end budget, while the default 10s
150- // deadline preserves enough time for WMIC after a stalled primary probe.
151- const deadline = Date . now ( ) + timeoutMs ;
152- const powershell = spawnSync (
153- getWindowsPowerShellExePath ( ) ,
154- [
155- "-NoProfile" ,
156- "-NonInteractive" ,
157- "-Command" ,
158- `$process = Get-CimInstance Win32_Process -Filter "ProcessId = ${ pid } " -ErrorAction Stop; [Console]::Out.Write($process.CreationDate.ToUniversalTime().ToString("o"))` ,
159- ] ,
160- {
161- encoding : "utf8" ,
162- timeout : Math . min ( timeoutMs , DEFAULT_TIMEOUT_MS ) ,
163- windowsHide : true ,
164- } ,
165- ) ;
166- if ( ! powershell . error && powershell . status === 0 ) {
167- const startTime = parseWindowsProcessStartTime ( powershell . stdout ) ;
168- if ( startTime !== null ) {
169- return startTime ;
170- }
171- }
172- const remainingMs = deadline - Date . now ( ) ;
173- if ( remainingMs <= 0 ) {
174- return null ;
175- }
176- const wmic = spawnSync (
177- getWindowsWmicExePath ( ) ,
178- [ "process" , "where" , `ProcessId=${ pid } ` , "get" , "CreationDate" , "/value" ] ,
179- {
180- timeout : remainingMs ,
181- windowsHide : true ,
182- stdio : [ "ignore" , "pipe" , "ignore" ] ,
183- } ,
184- ) ;
185- return ! wmic . error && wmic . status === 0 ? parseWindowsProcessStartTime ( wmic . stdout ) : null ;
186- }
187-
188100export function readWindowsProcessArgsSync (
189101 pid : number ,
190102 timeoutMs = DEFAULT_TIMEOUT_MS ,
0 commit comments