1212 * npm run test:endpoints
1313 */
1414
15- import { readFileSync } from 'fs' ;
1615import { fileURLToPath } from 'url' ;
17- import { dirname , join } from 'path' ;
16+ import { dirname } from 'path' ;
1817
1918// Get __dirname equivalent for ES modules
2019const __filename = fileURLToPath ( import . meta. url ) ;
2120const __dirname = dirname ( __filename ) ;
2221
23- // Read and parse locations.js as text since it's CommonJS
24- const locationsPath = join ( __dirname , '../lib/locations.js' ) ;
25- const locationsContent = readFileSync ( locationsPath , 'utf8' ) ;
26-
27- // Use eval to execute the module.exports function in a safe context
28- // Extract just the function part
29- const funcMatch = locationsContent . match ( / m o d u l e \. e x p o r t s = ( \( \) = > \[ [ \s \S ] * ?\] ) ; / ) ;
30- if ( ! funcMatch ) {
31- throw new Error ( 'Could not parse locations.js' ) ;
32- }
33-
34- // Evaluate the function and call it to get the array
35- const getLocationsFunc = eval ( funcMatch [ 1 ] ) ;
36- const getLocations = getLocationsFunc ;
22+ // Import locations.js as an ES6 module
23+ import getLocations from '../lib/locations.js' ;
3724
3825// Test configuration
3926const TEST_TIMEOUT = 10000 ; // 10 seconds
@@ -82,10 +69,11 @@ async function testEndpoint(location) {
8269 } ;
8370
8471 const startTime = Date . now ( ) ;
72+ let timeoutId ;
8573
8674 try {
8775 const controller = new AbortController ( ) ;
88- const timeoutId = setTimeout ( ( ) => controller . abort ( ) , TEST_TIMEOUT ) ;
76+ timeoutId = setTimeout ( ( ) => controller . abort ( ) , TEST_TIMEOUT ) ;
8977
9078 const response = await fetch ( url , {
9179 method : 'GET' ,
@@ -100,7 +88,7 @@ async function testEndpoint(location) {
10088 result . success = true ; // Any response (including 404, 500, etc.) is considered successful for latency testing
10189
10290 } catch ( error ) {
103- clearTimeout ( timeoutId ) ;
91+ if ( timeoutId ) clearTimeout ( timeoutId ) ;
10492
10593 if ( error . name === 'AbortError' ) {
10694 result . responseTime = Date . now ( ) - startTime ;
0 commit comments