1
1
const fs = require ( 'fs' ) ;
2
2
const http = require ( 'http' ) ;
3
+ const os = require ( 'os' ) ;
3
4
4
5
async function wptServer ( harnessTestURLs , testConfigs , env ) {
5
6
@@ -17,23 +18,40 @@ async function wptServer(harnessTestURLs, testConfigs, env) {
17
18
function handler ( req , res ) {
18
19
let url = req . url
19
20
let fileToServe = env . WPT_DIR + url
21
+ let diagnostics = false ;
20
22
if ( polyfillsSet . has ( url ) ) {
21
23
fileToServe = process . cwd ( ) + url
24
+ if ( diagnostics ) {
25
+ console . log ( "Polyfill file requested. Mapping %s to %s" ,
26
+ url , fileToServe ) ;
27
+ }
22
28
}
23
29
// this is not safe to use as generic server
24
30
// anyone can navigate back to serve any file on your server
25
31
// outside the desired `base` but for our use, it's fine.
26
32
fs . readFile ( fileToServe , "utf8" , ( err , data ) => {
27
33
if ( err ) {
34
+ if ( diagnostics ) console . log ( "Returning 404 for %s" , fileToServe ) ;
28
35
res . writeHead ( 404 , { "Content-Type" : "text/plain" } ) ;
29
36
res . write ( '404 not found' ) ;
30
37
return res . end ( ) ;
31
38
}
39
+ // win32 uses backslashes. Need to re-write the url
40
+ let mangledURL = fileToServe ;
41
+ if ( os . platform ( ) === 'win32' ) {
42
+ mangledURL = "test/wpt" + url ;
43
+ mangledURL = mangledURL . replaceAll ( '/' , '\\' ) ;
44
+
45
+ if ( diagnostics ) {
46
+ console . log ( "Windows hack: remap %s to %s" , fileToServe , mangledURL ) ;
47
+ }
48
+ }
32
49
33
50
res . statusCode = 200 ;
51
+ if ( diagnostics ) console . log ( "Testing to see if %s needs a polyfill." , fileToServe ) ;
34
52
// if the URL being served is for a harness test we need to inject the polyfill
35
- if ( harnessTestURLs . has ( fileToServe ) ) {
36
- console . log ( "Injecting polyfill in " + url ) ;
53
+ if ( harnessTestURLs . has ( mangledURL ) ) {
54
+ if ( diagnostics ) console . log ( "******* Injecting polyfill in " + url ) ;
37
55
data = data . replace ( / ( < \/ .* t i t l e .* > ) / gi, `$1${ polyfillStr } ` ) ;
38
56
}
39
57
res . write ( data ) ;
0 commit comments