Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@extractus/oembed-extractor",
"version": "6.0.0",
"version": "6.0.1",
"description": "Get oEmbed data from given URL.",
"homepage": "https://github.com/extractus/oembed-extractor",
"repository": {
Expand All @@ -19,7 +19,7 @@
"imports": {
"@deno/dnt": "jsr:@deno/dnt@^0.43.2",
"@std/assert": "jsr:@std/assert@1.0.12",
"linkedom": "npm:linkedom@^0.18.13"
"linkedom": "npm:linkedom@^0.18.15"
},
"lint": {
"include": ["src/", "mod.ts", "scripts/"],
Expand Down
2 changes: 1 addition & 1 deletion scripts/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const sync = async (): Promise<void> => {
"",
"/** Simplified provider entry with scheme patterns and endpoint. */",
"export interface SimplifiedProvider {",
" /** Scheme patterns (protocol-relative, regex-escaped) */",
" /** Scheme patterns (protocol-relative globs with * wildcards) */",
" s: string[];",
" /** Endpoint URL (protocol-relative, format=json) */",
" e: string;",
Expand Down
60 changes: 36 additions & 24 deletions src/utils/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,40 @@ export interface FindResult {
url: string;
}

/**
* Escape regex metacharacters so scheme text is matched literally.
* The `*` wildcard is intentionally left untouched.
*
* @param text - Host or path portion of a scheme
* @returns Text safe to embed in a RegExp
*/
const escapeRegex = (text: string): string => {
return text.replace(/[.+?^${}()|[\]\\]/g, "\\$&");
};

/**
* Convert a scheme string into a RegExp for URL matching.
*
* @param scheme - Scheme pattern with wildcards
* The scheme is treated as an oEmbed glob (spec 2.1): `*` is the only
* wildcard and every other character is literal. Provider data may ship
* with backslash-escaped dots, so escapes are normalized away first.
* The pattern is split into host and path so host wildcards cannot
* cross into the path, then anchored so it must match the whole URL.
*
* @param scheme - Protocol-relative scheme pattern, e.g. `//*.x.com/*`
* @returns Compiled regular expression
*/
const toRegExp = (scheme = ""): RegExp => {
return new RegExp(
scheme
.replace(/\\./g, ".")
.replace(/\*/g, "(.*)")
.replace(/\?/g, "\\?")
.replace(/,$/g, ""),
"i",
);
const raw = scheme.replace(/\\(.)/g, "$1");
const body = raw.startsWith("//") ? raw.slice(2) : raw;
const slash = body.indexOf("/");
const host = slash === -1 ? body : body.slice(0, slash);
const path = slash === -1 ? "" : body.slice(slash);
const hostRe = escapeRegex(host).replace(/\*/g, "[^/]*");
const pathRe = path === ""
? "(?:/.*)?"
: escapeRegex(path).replace(/\*/g, ".*");
return new RegExp(`^(?:https?:)?//${hostRe}${pathRe}$`, "i");
};

/**
Expand All @@ -44,16 +63,6 @@ const uniquify = (arr: string[]): string[] => {
return [...new Set(arr)];
};

/**
* Escape dots in a scheme string for regex use.
*
* @param scheme - Scheme string
* @returns Scheme with escaped dots
*/
const undotted = (scheme = ""): string => {
return scheme.replace(/\./g, "\\.");
};

/**
* Strip protocol prefix (https:// or http://) from a URL.
*
Expand All @@ -80,7 +89,7 @@ export const simplify = (
const schemes = (endpoint.schemes as string[]) || [];
const url = endpoint.url as string;
const patterns = schemes.length > 0
? uniquify(schemes.map(removeProtocol).map(undotted))
? uniquify(schemes.map(removeProtocol))
: [];

return {
Expand Down Expand Up @@ -138,9 +147,9 @@ export const set = (providers: Record<string, unknown>[]): number => {
};

/**
* Match a URL against a provider's schemes; fall back to domain comparison when no schemes exist.
* Match a normalized URL against a provider's schemes; fall back to domain comparison when no schemes exist.
*
* @param url - URL to match
* @param url - Normalized URL (from `URL#href`) to match
* @param endpoint - Provider endpoint URL
* @param schemes - RegExp schemes to test against
* @returns True if URL matches the provider
Expand All @@ -156,7 +165,7 @@ const compare = (
return domain === endpointDomain;
}
return schemes.some((scheme) => {
return url.match(scheme);
return scheme.test(url);
});
};

Expand All @@ -171,11 +180,14 @@ export const find = (url = ""): FindResult | null => {
return null;
}

// Match against the normalized form so whitespace, letter case and
// default ports cannot bypass the anchored scheme patterns.
const normalized = new URL(url).href;
const providers = get();

for (let i = 0; i < providers.length; i++) {
const { endpoint, schemes } = providers[i];
const isMatched = compare(url, endpoint, schemes);
const isMatched = compare(normalized, endpoint, schemes);
if (isMatched) {
return {
schemes,
Expand Down
Loading
Loading