index.ts (1874B)
1 export type RoutePathParts = { 2 path: string; 3 query: string; 4 hash: string; 5 }; 6 7 export const parse_route_path = (route_path: string): RoutePathParts => { 8 const query_idx = route_path.indexOf("?"); 9 const hash_idx = route_path.indexOf("#"); 10 let path_end = route_path.length; 11 if (query_idx >= 0 && hash_idx >= 0) path_end = Math.min(query_idx, hash_idx); 12 else if (query_idx >= 0) path_end = query_idx; 13 else if (hash_idx >= 0) path_end = hash_idx; 14 const path = route_path.slice(0, path_end); 15 const query = query_idx >= 0 16 ? route_path.slice(query_idx, hash_idx >= 0 ? hash_idx : undefined) 17 : ""; 18 const hash = hash_idx >= 0 ? route_path.slice(hash_idx) : ""; 19 return { path, query, hash }; 20 }; 21 22 const has_file_extension = (route_path: string, file_exts: readonly string[]): boolean => { 23 const lower_path = route_path.toLowerCase(); 24 return file_exts.some((ext) => lower_path.endsWith(ext)); 25 }; 26 27 export const resolve_route_path = ( 28 route_path: string | undefined, 29 file_name: string, 30 default_route_path: string, 31 file_exts: readonly string[] 32 ): string => { 33 const resolved_route_path = route_path ?? default_route_path; 34 const { path, query, hash } = parse_route_path(resolved_route_path); 35 const normalized_path = path.endsWith("/") ? path.slice(0, -1) : path; 36 if (!normalized_path) return resolved_route_path; 37 if ( 38 normalized_path === file_name 39 || normalized_path.endsWith(`/${file_name}`) 40 || has_file_extension(normalized_path, file_exts) 41 ) return `${normalized_path}${query}${hash}`; 42 return `${normalized_path}/${file_name}${query}${hash}`; 43 }; 44 45 export const resolve_wasm_path = (wasm_path: string | undefined, wasm_file: string, default_wasm_path: string): string => 46 resolve_route_path(wasm_path, wasm_file, default_wasm_path, [".wasm"]);