kempo-server 1.7.11 → 1.7.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/findFile.js +1 -1
- package/package.json +1 -1
package/dist/findFile.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import path from"path";export default(files,rootPath,requestPath,method,log)=>{log(`Finding file for: ${method} ${requestPath}`,3);
|
|
1
|
+
import path from"path";export default(files,rootPath,requestPath,method,log)=>{log(`Finding file for: ${method} ${requestPath}`,3);let normalizeRequestPath=requestPath.startsWith("/")?requestPath.slice(1):requestPath;normalizeRequestPath.endsWith("/")&&normalizeRequestPath.length>0&&(normalizeRequestPath=normalizeRequestPath.slice(0,-1));const requestSegments=normalizeRequestPath?normalizeRequestPath.split("/"):[];log(`Normalized request path: ${normalizeRequestPath}`,4),log(`Request segments: [${requestSegments.join(", ")}]`,4);const isDynamicSegment=segment=>segment.startsWith("[")&&segment.endsWith("]"),getParamName=segment=>segment.slice(1,-1),getRelativePath=filePath=>path.relative(rootPath,filePath).replace(/\\/g,"/"),exactMatch=files.find(file=>getRelativePath(file)===normalizeRequestPath);if(exactMatch)return log(`Found exact match: ${exactMatch}`,2),[exactMatch,{}];const isDirectoryRequest="/"===requestPath||requestPath.endsWith("/")||!path.extname(requestPath);if(isDirectoryRequest){log("Processing directory request",3);const dirPath=normalizeRequestPath||"",methodUpper=method.toUpperCase(),indexFiles=[`${methodUpper}.js`,`${methodUpper}.html`,"index.js","index.html"];log(`Looking for index files: [${indexFiles.join(", ")}]`,3);for(const indexFile of indexFiles){const indexPath=dirPath?`${dirPath}/${indexFile}`:indexFile,exactIndexMatch=files.find(file=>getRelativePath(file)===indexPath);if(exactIndexMatch)return log(`Found index file: ${exactIndexMatch}`,2),[exactIndexMatch,{}]}}log("Searching for dynamic routes...",3);const dynamicMatches=[];for(const file of files){const fileSegments=getRelativePath(file).split("/"),fileName=fileSegments[fileSegments.length-1],fileDirSegments=fileSegments.slice(0,-1);if(fileDirSegments.length!==requestSegments.length)continue;const pathParams={};let isMatch=!0;for(let i=0;i<fileDirSegments.length;i++){const fileSegment=fileDirSegments[i],requestSegment=requestSegments[i];if(isDynamicSegment(fileSegment)){const paramName=getParamName(fileSegment);pathParams[paramName]=requestSegment,log(`Dynamic match: [${paramName}] = ${requestSegment}`,4)}else if(fileSegment!==requestSegment){isMatch=!1;break}}if(isMatch)if(isDirectoryRequest){const methodUpper=method.toUpperCase(),priority=[`${methodUpper}.js`,`${methodUpper}.html`,"index.js","index.html"].indexOf(fileName);-1!==priority&&(log(`Found dynamic directory match: ${file} (priority: ${priority})`,3),dynamicMatches.push({file:file,pathParams:pathParams,priority:priority}))}else log(`Found dynamic file match: ${file}`,3),dynamicMatches.push({file:file,pathParams:pathParams,priority:0})}if(dynamicMatches.length>0){dynamicMatches.sort((a,b)=>a.priority-b.priority);const bestMatch=dynamicMatches[0];return log(`Best dynamic match: ${bestMatch.file} with params: ${JSON.stringify(bestMatch.pathParams)}`,2),[bestMatch.file,bestMatch.pathParams]}return log(`No file found for: ${requestPath}`,3),[!1,{}]};
|