22
33import { spawn } from 'node:child_process' ;
44import { existsSync , readFileSync , unlinkSync , writeFileSync } from 'node:fs' ;
5+ import { createServer } from 'node:http' ;
56import { tmpdir } from 'node:os' ;
67import { join } from 'node:path' ;
78import { parseArgs } from 'node:util' ;
@@ -28,12 +29,33 @@ Commands:
2829 run Run the CORS proxy server in the foreground
2930 start Start the CORS proxy server daemon
3031 stop Stop the CORS proxy server daemon
31-
32+ status Show the status of the CORS proxy server daemon
3233Options:
3334 -h, --help Show this help message
3435 -p, --port <port> Port to listen on (default: 9999)
35- --pid <path> Path to PID file (default: ${ join ( temp , 'cors-proxy.pid' ) }
36- ` ) ;
36+ --pid <path> Path to PID file (default: ${ join ( temp , 'cors-proxy.pid' ) } ` ) ;
37+ }
38+
39+ function getPID ( strict ) {
40+ let pid ;
41+ try {
42+ pid = parseInt ( readFileSync ( pidFile , 'utf8' ) ) ;
43+ } catch ( e ) {
44+ if ( e . code === 'ENOENT' ) {
45+ console . error ( 'No PID file' ) ;
46+ process . exit ( strict ? 2 : 0 ) ;
47+ }
48+
49+ console . error ( 'Found existing PID file but could not read it' ) ;
50+ process . exit ( strict ? 13 : 0 ) ;
51+ }
52+
53+ try {
54+ process . kill ( pid , 0 ) ;
55+ return [ pid , true ] ;
56+ } catch ( e ) {
57+ return [ pid , false ] ;
58+ }
3759}
3860
3961switch ( cmd ) {
@@ -43,49 +65,47 @@ switch (cmd) {
4365 process . on ( 'exit' , ( ) => {
4466 console . log ( 'Shutting down server' ) ;
4567 server . close ( ) ;
68+ unlinkSync ( pidFile ) ;
4669 } ) ;
4770 break ;
4871 }
49-
5072 case 'start' : {
5173 if ( existsSync ( pidFile ) ) {
52- let pid ;
53- try {
54- pid = parseInt ( readFileSync ( pidFile , 'utf8' ) ) ;
55- } catch ( e ) {
56- console . error ( 'Found existing PID file but could not read it' ) ;
57- process . exit ( 13 ) ;
58- }
74+ const [ pid , processExists ] = getPID ( true ) ;
5975
60- try {
61- process . kill ( pid , 0 ) ;
62- } catch {
63- console . error ( `Server is already running (pid is ${ pid } )` ) ;
76+ if ( processExists ) {
77+ console . error ( `Daemon is already running (pid is ${ pid } )` ) ;
6478 process . exit ( 16 ) ;
79+ } else {
80+ unlinkSync ( pidFile ) ;
81+ console . error ( 'Removed stale PID file' ) ;
6582 }
66-
67- console . error ( 'Removing stale PID file' ) ;
68- unlinkSync ( pidFile ) ;
6983 }
7084
7185 const daemon = spawn (
72- process . execPath ,
86+ import . meta . filename ,
7387 [ 'run' , port && `--port=${ port } ` ] . filter ( ( a ) => a ) ,
74- {
75- stdio : 'ignore' ,
76- detached : true ,
77- } ,
88+ { stdio : 'ignore' , detached : true } ,
7889 ) ;
7990 daemon . unref ( ) ;
8091 console . log ( 'Started CORS proxy server with PID' , daemon . pid ) ;
8192 writeFileSync ( pidFile , daemon . pid . toString ( ) ) ;
82- process . exit ( 0 ) ;
93+ break ;
8394 }
84- case 'stop' :
85- try {
86- const pid = parseInt ( readFileSync ( pidFile , 'utf8' ) ) ;
87- process . kill ( pid ) ;
88- } catch ( e ) {
89- console . error ( 'Invalid or missing PID file' ) ;
95+ case 'stop' : {
96+ const [ pid , processExists ] = getPID ( true ) ;
97+
98+ if ( processExists ) process . kill ( pid ) ;
99+ else {
100+ unlinkSync ( pidFile ) ;
101+ console . error ( 'Removed stale PID file' ) ;
90102 }
103+ break ;
104+ }
105+ case 'status' : {
106+ const [ pid , processExists ] = getPID ( false ) ;
107+ if ( processExists ) console . error ( 'Daemon is running as pid' , pid ) ;
108+ else console . error ( 'Not running, stale PID file' ) ;
109+ break ;
110+ }
91111}
0 commit comments