Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: encode added routes #973

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

feat: encode added routes #973

wants to merge 3 commits into from

Conversation

huseeiin
Copy link

now you can add h3 routes with arabic letters.

encode pathname before adding route
add tests for arabic routes
@huseeiin huseeiin requested a review from pi0 as a code owner February 18, 2025 19:02
@huseeiin
Copy link
Author

@pi0 you might need to rerun prettier because i didn't have time to do that

src/h3.ts Outdated
@@ -277,7 +277,7 @@ class _H3 implements H3 {
}
const _method = (method || "").toUpperCase();
const _handler = (handler as H3)?.handler || handler;
addRoute(this._router, _method, route, <H3Route>{
addRoute(this._router, _method, new URL(route, "http://localhost").pathname, <H3Route>{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably do manual encoding (or do it behind a flag) since URL constructor adds runtime overhead

@pi0 pi0 changed the title fix arabic-based routes feat: encode added routes Feb 19, 2025
@pi0
Copy link
Member

pi0 commented Feb 19, 2025

(from chat)

const preservedChars = [
  "%",
  "[",
  "]",
  "!",
  "*",
  "'",
  "(",
  ")",
  ";",
  ":",
  "@",
  "&",
  "=",
  "+",
  "$",
  ",",
  "/",
  "?",
  "#",
  "-",
];

/**
 * Transform the `pathname` segment of a URL like the browser does.
 */
export function sanitizePathname(url: string) {
  url = String(url);

  const segments = url.replace(/\\/g, "/").split("/");

  const normalizedSegments: string[] = [];

  for (const segment of segments) {
    if (segment === "") {
      continue;
    }

    if (segment === ".") {
      continue;
    }

    if (segment === "..") {
      if (normalizedSegments.length > 0) {
        normalizedSegments.pop();
      }
    } else {
      normalizedSegments.push(segment);
    }
  }

  const result = normalizedSegments
    .join("/")
    .split("")
    .map((char) =>
      preservedChars.includes(char) ? char : encodeURIComponent(char)
    )
    .join("");

  return result.startsWith("/") ? result : `/${result}`;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants