filepath.js 1016 B

1234567891011121314151617181920212223242526272829303132333435
  1. // src/utils/filepath.ts
  2. var getFilePath = (options) => {
  3. let filename = options.filename;
  4. const defaultDocument = options.defaultDocument || "index.html";
  5. if (filename.endsWith("/")) {
  6. filename = filename.concat(defaultDocument);
  7. } else if (!filename.match(/\.[a-zA-Z0-9_-]+$/)) {
  8. filename = filename.concat("/" + defaultDocument);
  9. }
  10. const path = getFilePathWithoutDefaultDocument({
  11. root: options.root,
  12. filename
  13. });
  14. return path;
  15. };
  16. var getFilePathWithoutDefaultDocument = (options) => {
  17. let root = options.root || "";
  18. let filename = options.filename;
  19. if (/(?:^|[\/\\])\.\.(?:$|[\/\\])/.test(filename)) {
  20. return;
  21. }
  22. filename = filename.replace(/^\.?[\/\\]/, "");
  23. filename = filename.replace(/\\/, "/");
  24. root = root.replace(/\/$/, "");
  25. let path = root ? root + "/" + filename : filename;
  26. path = path.replace(/^\.?\//, "");
  27. if (root[0] !== "/" && path[0] === "/") {
  28. return;
  29. }
  30. return path;
  31. };
  32. export {
  33. getFilePath,
  34. getFilePathWithoutDefaultDocument
  35. };