From 7924ab5deeaca905192c11a82a937d7615328b48 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Mon, 27 May 2024 16:27:45 -0300 Subject: write: live-dev-server: factor out startTiming --- src/write/build-modes/live-dev-server.js | 37 +++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 13 deletions(-) (limited to 'src/write') diff --git a/src/write/build-modes/live-dev-server.js b/src/write/build-modes/live-dev-server.js index 0f8f7607..fe627f6f 100644 --- a/src/write/build-modes/live-dev-server.js +++ b/src/write/build-modes/live-dev-server.js @@ -332,6 +332,25 @@ export async function go({ // Other routes determined by page and URL specs + const startTiming = () => { + if (!showTimings) { + return () => ''; + } + + const timeStart = Date.now(); + + return () => { + const timeEnd = Date.now(); + const timeDelta = timeEnd - timeStart; + + if (timeDelta > 100) { + return `${(timeDelta / 1000).toFixed(2)}s`; + } else { + return `${timeDelta}ms`; + } + }; + }; + // URL to page map expects trailing slash but no leading slash. const pathnameKey = pathname.replace(/^\//, '') + (pathname.endsWith('/') ? '' : '/'); @@ -395,7 +414,7 @@ export async function go({ return; } - const timeStart = Date.now(); + const timing = startTiming(); const bound = bindUtilities({ absoluteTo, @@ -424,18 +443,10 @@ export async function go({ const {pageHTML} = html.resolve(topLevelResult); - const timeEnd = Date.now(); - const timeDelta = timeEnd - timeStart; - - if (showTimings) { - const timeString = - (timeDelta > 100 - ? `${(timeDelta / 1000).toFixed(2)}s` - : `${timeDelta}ms`); - - console.log(`${requestHead} [200, ${timeString}] ${pathname} (${colors.blue(`page`)})`); - } else if (loudResponses) { - console.log(`${requestHead} [200] ${pathname} (${colors.blue(`page`)})`); + const timeString = timing(); + const status = (timeString ? `200 ${timeString}` : `200`); + if (showTimings || loudResponses) { + console.log(`${requestHead} [${status}] ${pathname} (${colors.blue(`page`)})`); } response.writeHead(200, contentTypeHTML); -- cgit 1.3.0-6-gf8a5