@@ -7,7 +7,9 @@ const finalhandler = require("finalhandler");
77
88const DEFAULT_PORT = 8080 ;
99
10- function serve ( { acceptRanges = true , debug = false , root, port } = { acceptRanges : true } ) {
10+ const TRUES = [ "TRUE" , "True" , "true" , true ] ;
11+
12+ function serve ( { acceptRanges = true , debug = false , wait = 60 , root, port } = { acceptRanges : true , wait : 60 } ) {
1113 // console.log(arguments);
1214 if ( ! root ) {
1315 root = process . cwd ( ) ;
@@ -17,38 +19,63 @@ function serve ({ acceptRanges = true, debug = false, root, port } = { acceptRan
1719 if ( ! port ) port = process . env . SRVD_DEFAULT_PORT ? process . env . SRVD_DEFAULT_PORT : DEFAULT_PORT ;
1820 port = parseInt ( port ) ;
1921
20- const serve = serveStatic ( root , {
21- acceptRanges
22- } ) ;
22+ if ( ! wait ) wait = 5 ;
23+ wait = parseInt ( wait ) ;
24+ if ( debug ) console . log ( `[srvd] waiting ${ wait } seconds for requests` ) ;
25+ const wait_ms = wait * 1000 ;
2326
24- const server = http . createServer ( function onRequest ( req , res ) {
25- serve ( req , res , finalhandler ( req , res ) ) ;
26- } ) ;
27+ const serve = serveStatic ( root , { acceptRanges } ) ;
2728
28- server . listen ( port ) ;
29- if ( debug ) console . log ( "[srvd] serving on port " + port ) ;
29+ let last = Date . now ( ) ;
30+ let server ;
31+
32+ let checkWaitTimeout , checkForCloseTimeout ;
33+
34+ function clearTimeouts ( ) {
35+ if ( checkWaitTimeout ) clearTimeout ( checkWaitTimeout ) ;
36+ if ( checkForCloseTimeout ) clearTimeout ( checkForCloseTimeout ) ;
37+ }
38+
39+ function checkWait ( ) {
40+ if ( Date . now ( ) - last > wait_ms ) {
41+ if ( debug ) console . log ( `[srvd] we haven't received a request in ${ wait } seconds, so closing the server` ) ;
42+ clearTimeouts ( ) ;
43+ server . close ( ) ;
44+ }
45+ }
3046
3147 function checkForCloseRequest ( ) {
32- if ( [ "TRUE" , "True" , "true" ] . includes ( process . env . SRVD_PLZ_CLOSE ) ) {
48+ if ( TRUES . includes ( process . env . SRVD_PLZ_CLOSE ) ) {
49+ clearTimeouts ( ) ;
3350 server . close ( ) ;
34- } else {
35- setTimeout ( ( ) => checkForCloseRequest ( ) , 500 ) ;
3651 }
3752 }
38- setTimeout ( ( ) => checkForCloseRequest ( ) , 500 ) ;
3953
40- return { acceptRanges, debug, server, port, root } ;
41- } ;
54+ server = http . createServer ( function onRequest ( req , res ) {
55+ last = Date . now ( ) ;
56+ serve ( req , res , finalhandler ( req , res ) ) ;
57+ } ) ;
58+
59+ server . listen ( port ) ;
60+ if ( debug ) console . log ( "[srvd] serving on port " + port ) ;
61+
62+ checkWaitTimeout = setInterval ( checkWait , 500 ) ;
63+ checkForCloseTimeout = setInterval ( checkForCloseRequest , 500 ) ;
64+ server . on ( "close" , ( ) => clearTimeouts ( ) ) ;
65+
66+ return { acceptRanges, debug, server, port, root, wait } ;
67+ }
4268
4369module . exports = { serve } ;
4470
4571if ( require . main === module ) {
4672 const args = Array . from ( process . argv ) ;
47- const str = args . join ( " " ) ;
73+ const str = args . join ( " " ) ;
4874
4975 serve ( {
5076 debug : ! ! str . match ( / - ? - d e b u g ( ( = | = = ) ( t r u e | T r u e | T R U E ) ) ? / ) ,
5177 port : Array . prototype . slice . call ( str . match ( / - ? - p o r t (?: = | = = ) ( \d + ) / ) || [ ] , 1 ) [ 0 ] ,
52- root : Array . prototype . slice . call ( str . match ( / - ? - r o o t (?: = | = = ) ( [ ^ ] + ) / ) || [ ] , 1 ) [ 0 ]
78+ root : Array . prototype . slice . call ( str . match ( / - ? - r o o t (?: = | = = ) ( [ ^ ] + ) / ) || [ ] , 1 ) [ 0 ] ,
79+ wait : Array . prototype . slice . call ( str . match ( / - ? - w a i t (?: = | = = ) ( \d + ) / ) || [ ] , 1 ) [ 0 ]
5380 } ) ;
5481}
0 commit comments