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: improve compatibility of URLs in Node #72

Merged
merged 1 commit into from
Jul 3, 2023
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
10 changes: 5 additions & 5 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"deno.enable": true,
"deno.unstable": true,
"deno.enablePaths": [
"src",
"scripts",
"mod.ts",
"deps.ts",
"./src",
"./scripts",
"./mod.ts",
"./deps.ts",
"*/*.json",
"example"
"./example"
],
"editor.formatOnSave": true,
"[typescript]": {
Expand Down
89 changes: 88 additions & 1 deletion deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion scripts/build_npm.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { build, emptyDir } from "https://deno.land/x/dnt@0.22.0/mod.ts";
import { build, emptyDir } from "https://deno.land/x/dnt@0.37.0/mod.ts";

await emptyDir("./npm");

Expand All @@ -25,6 +25,12 @@ await build({
bugs: {
url: "https://github.com/ascorbic/unpic/issues",
},
devDependencies: {
"@unpic/pixels": "latest",
},
},
mappings: {
"https://deno.land/x/get_pixels@1.0.0/mod.ts": "@unpic/pixels",
},
});

Expand Down
5 changes: 3 additions & 2 deletions src/detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import domains from "../data/domains.json" assert { type: "json" };
import subdomains from "../data/subdomains.json" assert { type: "json" };
import paths from "../data/paths.json" assert { type: "json" };
import { ImageCdn } from "./types.ts";
import { toUrl } from "./utils.ts";

const cdnDomains = new Map(Object.entries(domains));
const cdnSubdomains = Object.entries(subdomains);
Expand All @@ -18,7 +19,7 @@ export function getImageCdnForUrlByDomain(
if (typeof url === "string" && !url.startsWith("https://")) {
return false;
}
const { hostname } = new URL(url);
const { hostname } = toUrl(url);
if (cdnDomains.has(hostname)) {
return cdnDomains.get(hostname) as ImageCdn;
}
Expand All @@ -34,7 +35,7 @@ export function getImageCdnForUrlByPath(
url: string | URL,
): ImageCdn | false {
// Allow relative URLs
const { pathname } = new URL(url, "http://n");
const { pathname } = toUrl(url);
for (const [prefix, cdn] of Object.entries(paths)) {
if (pathname.startsWith(prefix)) {
return cdn as ImageCdn;
Expand Down
5 changes: 3 additions & 2 deletions src/transformers/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import {
getNumericParam,
setParamIfDefined,
setParamIfUndefined,
toUrl,
} from "../utils.ts";

export const parse: UrlParser<{ fit?: string; quality?: number }> = (url) => {
const parsedUrl = new URL(url);
const parsedUrl = toUrl(url);

const width = getNumericParam(parsedUrl, "width");
const height = getNumericParam(parsedUrl, "height");
Expand All @@ -28,7 +29,7 @@ export const parse: UrlParser<{ fit?: string; quality?: number }> = (url) => {
export const transform: UrlTransformer = (
{ url: originalUrl, width, height, format },
) => {
const url = new URL(originalUrl);
const url = toUrl(originalUrl);
setParamIfDefined(url, "width", width, true, true);
setParamIfDefined(url, "height", height, true, true);
setParamIfDefined(url, "format", format);
Expand Down
5 changes: 3 additions & 2 deletions src/transformers/bunny.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import {
getNumericParam,
setParamIfDefined,
setParamIfUndefined,
toUrl,
} from "../utils.ts";

export const parse: UrlParser<{ fit?: string }> = (url) => {
const parsedUrl = new URL(url);
const parsedUrl = toUrl(url);

const width = getNumericParam(parsedUrl, "width");
const height = getNumericParam(parsedUrl, "height");
Expand All @@ -27,7 +28,7 @@ export const parse: UrlParser<{ fit?: string }> = (url) => {
export const transform: UrlTransformer = (
{ url: originalUrl, width, height },
) => {
const url = new URL(originalUrl);
const url = toUrl(originalUrl);
setParamIfDefined(url, "width", width, true, true);
if (width && height) {
setParamIfUndefined(url, "aspect_ratio", `${width}:${height}`);
Expand Down
3 changes: 2 additions & 1 deletion src/transformers/cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
UrlParser,
UrlTransformer,
} from "../types.ts";
import { toUrl } from "../utils.ts";

const cloudflareRegex =
/https?:\/\/(?<host>[^\/]+)\/cdn-cgi\/image\/(?<transformations>[^\/]+)\/(?<path>.*)$/g;
Expand Down Expand Up @@ -40,7 +41,7 @@ export interface CloudflareParams {
export const parse: UrlParser<CloudflareParams> = (
imageUrl,
) => {
const url = new URL(imageUrl);
const url = toUrl(imageUrl);
const matches = [...url.toString().matchAll(cloudflareRegex)];
if (!matches.length) {
throw new Error("Invalid Cloudflare URL");
Expand Down
Loading