Skip to content

Commit

Permalink
add support for cloudflare images without custom domain
Browse files Browse the repository at this point in the history
  • Loading branch information
dawaltconley committed May 23, 2024
1 parent c8cadc5 commit 925244b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
3 changes: 2 additions & 1 deletion data/domains.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
"ip.keycdn.com": "keycdn",
"assets.caisy.io": "bunny",
"images.contentstack.io": "contentstack",
"ucarecdn.com": "uploadcare"
"ucarecdn.com": "uploadcare",
"imagedelivery.net": "cloudflare_images"
}
37 changes: 37 additions & 0 deletions src/transformers/cloudflare_images.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,40 @@ Deno.test("cloudflare images transformer", async (t) => {
);
});
});

const img2 =
"https://imagedelivery.net/1aS6NlIe-Sc1o3NhVvy8Qw/2ba36ba9-69f6-41b6-8ff0-2779b41df200/w=128,h=128,rotate=90,f=auto";

Deno.test("imagedelivery.net images parser", () => {
const parsed = parse(img2);
const expected: ParsedUrl<CloudflareImagesParams> = {
base: img2,
cdn: "cloudflare_images",
format: "auto",
width: 128,
height: 128,
params: {
host: "imagedelivery.net",
accountHash: "1aS6NlIe-Sc1o3NhVvy8Qw",
imageId: "2ba36ba9-69f6-41b6-8ff0-2779b41df200",
transformations: {
rotate: "90",
},
},
};
assertEquals(parsed, expected);
});

Deno.test("imagedelivery.net images transformer", async (t) => {
await t.step("transforms a URL", () => {
const result = transform({
url: img2,
width: 100,
height: 200,
});
assertEquals(
result?.toString(),
"https://imagedelivery.net/1aS6NlIe-Sc1o3NhVvy8Qw/2ba36ba9-69f6-41b6-8ff0-2779b41df200/rotate=90,w=100,h=200,f=auto,fit=cover",
);
});
});
21 changes: 14 additions & 7 deletions src/transformers/cloudflare_images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { toUrl } from "../utils.ts";

const cloudflareImagesRegex =
/https?:\/\/(?<host>[^\/]+)\/cdn-cgi\/imagedelivery\/(?<accountHash>[^\/]+)\/(?<imageId>[^\/]+)\/*(?<transformations>[^\/]+)*$/g;
const imagedeliveryRegex =
/https?:\/\/(?<host>imagedelivery.net)\/(?<accountHash>[^\/]+)\/(?<imageId>[^\/]+)\/*(?<transformations>[^\/]+)*$/g

const parseTransforms = (transformations: string) =>
Object.fromEntries(
Expand All @@ -22,14 +24,16 @@ const formatUrl = (
imageId,
}: CloudflareImagesParams,
): string => {
const transformString = Object.entries(transformations).map(
([key, value]) => `${key}=${value}`,
).join(",");
const transformString = Object.entries(transformations)
.filter(([key, value]) => Boolean(key && value))
.map(([key, value]) => `${key}=${value}`)
.join(",");

const pathSegments = [
host,
"cdn-cgi",
"imagedelivery",
...(host === 'imagedelivery.net'
? [host]
: [host, "cdn-cgi", "imagedelivery"]
),
accountHash,
imageId,
transformString,
Expand All @@ -47,7 +51,10 @@ export const parse: UrlParser<CloudflareImagesParams> = (
imageUrl,
) => {
const url = toUrl(imageUrl);
const matches = [...url.toString().matchAll(cloudflareImagesRegex)];
const matches = [
...url.toString().matchAll(cloudflareImagesRegex),
...url.toString().matchAll(imagedeliveryRegex),
];
if (!matches.length) {
throw new Error("Invalid Cloudflare Images URL");
}
Expand Down

0 comments on commit 925244b

Please sign in to comment.