index.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var trailing_slash_exports = {};
  19. __export(trailing_slash_exports, {
  20. appendTrailingSlash: () => appendTrailingSlash,
  21. trimTrailingSlash: () => trimTrailingSlash
  22. });
  23. module.exports = __toCommonJS(trailing_slash_exports);
  24. const trimTrailingSlash = (options) => {
  25. return async function trimTrailingSlash2(c, next) {
  26. if (options?.alwaysRedirect) {
  27. if ((c.req.method === "GET" || c.req.method === "HEAD") && c.req.path !== "/" && c.req.path.at(-1) === "/") {
  28. const url = new URL(c.req.url);
  29. url.pathname = url.pathname.substring(0, url.pathname.length - 1);
  30. return c.redirect(url.toString(), 301);
  31. }
  32. }
  33. await next();
  34. if (!options?.alwaysRedirect && c.res.status === 404 && (c.req.method === "GET" || c.req.method === "HEAD") && c.req.path !== "/" && c.req.path.at(-1) === "/") {
  35. const url = new URL(c.req.url);
  36. url.pathname = url.pathname.substring(0, url.pathname.length - 1);
  37. c.res = c.redirect(url.toString(), 301);
  38. }
  39. };
  40. };
  41. const appendTrailingSlash = (options) => {
  42. return async function appendTrailingSlash2(c, next) {
  43. if (options?.alwaysRedirect) {
  44. if ((c.req.method === "GET" || c.req.method === "HEAD") && c.req.path.at(-1) !== "/") {
  45. const url = new URL(c.req.url);
  46. url.pathname += "/";
  47. return c.redirect(url.toString(), 301);
  48. }
  49. }
  50. await next();
  51. if (!options?.alwaysRedirect && c.res.status === 404 && (c.req.method === "GET" || c.req.method === "HEAD") && c.req.path.at(-1) !== "/") {
  52. const url = new URL(c.req.url);
  53. url.pathname += "/";
  54. c.res = c.redirect(url.toString(), 301);
  55. }
  56. };
  57. };
  58. // Annotate the CommonJS export names for ESM import in node:
  59. 0 && (module.exports = {
  60. appendTrailingSlash,
  61. trimTrailingSlash
  62. });