Commit 7fde69af authored by ChanHaeng Lee's avatar ChanHaeng Lee
Browse files

Replace handle regex already exists

parent 94f38bf4
Loading
Loading
Loading
Loading
+2 −9
Original line number Diff line number Diff line
import { Command } from "@cliffy/command";
import { handleRegexp } from "@fedify/fedify/vocab";
import { lookupWebFinger } from "@fedify/fedify/webfinger";
import ora from "ora";
import { printJson } from "./utils.ts";
@@ -56,14 +57,6 @@ function convertUrlIfHandle(handleOrUrl: string): URL {
  }
}

/**
 * Regular expression to match a handle in the format `@username@domain`.
 * The username can contain any characters except `@`.
 * The domain must be match with `[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}`.
 */
const HANDLE_REGEX =
  /^@?([^@]+)@([-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6})$/;

/**
 * Custom error class for invalid handle formats.
 * @param handle The invalid handle that caused the error.
@@ -89,7 +82,7 @@ class InvalidHandleError extends Error {
 * ```
 */
function convertHandleToUrl(handle: string): URL {
  const match = handle.match(HANDLE_REGEX);
  const match = handle.match(handleRegexp);
  if (!match) {
    throw new InvalidHandleError(handle);
  }
+6 −1
Original line number Diff line number Diff line
@@ -47,7 +47,12 @@ export interface LookupObjectOptions {
  tracerProvider?: TracerProvider;
}

const handleRegexp =
/**
 * Regular expression to match a fediverse handle in the format `@user@server` or `user@server`.
 * The `user` part can contain alphanumeric characters and some special characters except `@`.
 * The `server` part is all characters after the `@` symbol in the middle.
 */
export const handleRegexp =
  /^@?((?:[-A-Za-z0-9._~!$&'()*+,;=]|%[A-Fa-f0-9]{2})+)@([^@]+)$/;

/**