resources.ts (3433B)
1 /** 2 * Base path for resources. The default value is correct for non-worker WPT, but standalone and 3 * workers must access resources using a different base path, so this is overridden in 4 * `test_worker-worker.ts` and `standalone.ts`. 5 */ 6 let baseResourcePath = '/_mozilla/webgpu/resources'; 7 let crossOriginHost = ''; 8 9 function getAbsoluteBaseResourcePath(path: string) { 10 // Path is already an absolute one. 11 if (path[0] === '/') { 12 return path; 13 } 14 15 // Path is relative 16 const relparts = window.location.pathname.split('/'); 17 relparts.pop(); 18 const pathparts = path.split('/'); 19 20 let i; 21 for (i = 0; i < pathparts.length; ++i) { 22 switch (pathparts[i]) { 23 case '': 24 break; 25 case '.': 26 break; 27 case '..': 28 relparts.pop(); 29 break; 30 default: 31 relparts.push(pathparts[i]); 32 break; 33 } 34 } 35 36 return relparts.join('/'); 37 } 38 39 function runningOnLocalHost(): boolean { 40 const hostname = window.location.hostname; 41 return hostname === 'localhost' || hostname === '127.0.0.1' || hostname === '::1'; 42 } 43 44 /** 45 * Get a path to a resource in the `resources` directory relative to the current execution context 46 * (html file or worker .js file), for `fetch()`, `<img>`, `<video>`, etc but from cross origin host. 47 * Provide onlineUrl if the case running online. 48 * @internal MAINTENANCE_TODO: Cases may run in the LAN environment (not localhost but no internet 49 * access). We temporarily use `crossOriginHost` to configure the cross origin host name in that situation. 50 * But opening to auto-detect mechanism or other solutions. 51 */ 52 export function getCrossOriginResourcePath(pathRelativeToResourcesDir: string, onlineUrl = '') { 53 // A cross origin host has been configured. Use this to load resource. 54 if (crossOriginHost !== '') { 55 return ( 56 crossOriginHost + 57 getAbsoluteBaseResourcePath(baseResourcePath) + 58 '/' + 59 pathRelativeToResourcesDir 60 ); 61 } 62 63 // Using 'localhost' and '127.0.0.1' trick to load cross origin resource. Set cross origin host name 64 // to 'localhost' if case is not running in 'localhost' domain. Otherwise, use '127.0.0.1'. 65 // host name to locahost unless the server running in 66 if (runningOnLocalHost()) { 67 let crossOriginHostName = ''; 68 if (location.hostname === 'localhost') { 69 crossOriginHostName = 'http://127.0.0.1'; 70 } else { 71 crossOriginHostName = 'http://localhost'; 72 } 73 74 return ( 75 crossOriginHostName + 76 ':' + 77 location.port + 78 getAbsoluteBaseResourcePath(baseResourcePath) + 79 '/' + 80 pathRelativeToResourcesDir 81 ); 82 } 83 84 return onlineUrl; 85 } 86 87 /** 88 * Get a path to a resource in the `resources` directory, relative to the current execution context 89 * (html file or worker .js file), for `fetch()`, `<img>`, `<video>`, etc. Pass the cross origin host 90 * name if wants to load resource from cross origin host. 91 */ 92 export function getResourcePath(pathRelativeToResourcesDir: string) { 93 return baseResourcePath + '/' + pathRelativeToResourcesDir; 94 } 95 96 /** 97 * Set the base resource path (path to the `resources` directory relative to the current 98 * execution context). 99 */ 100 export function setBaseResourcePath(path: string) { 101 baseResourcePath = path; 102 } 103 104 /** 105 * Set the cross origin host and cases related to cross origin 106 * will load resource from the given host. 107 */ 108 export function setCrossOriginHost(host: string) { 109 crossOriginHost = host; 110 }