Skip to content
Open
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
2 changes: 1 addition & 1 deletion src/providers/cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export const generate: URLGenerator<"cloudflare"> = (
) => {
const modifiers = operationsGenerator(operations);
const url = toUrl(options?.domain ? `https://${options.domain}` : "/");

const srcStr = src.toString();
let pathSuffix = stripLeadingSlash(srcStr);

Expand Down
72 changes: 72 additions & 0 deletions src/providers/cloudflare_images.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,42 @@ Deno.test("Cloudflare Images CDN - extract", async (t) => {
const result = extract("https://example.com/image.jpg");
assertEquals(result, null);
});

await t.step(
"should handle URL with named variant (no transformations)",
() => {
const result = extract(
"https://imagedelivery.net/5LGXGUnHU18h6ehN_xjpXQ/abc123/public",
);
assertEquals(result, {
src: "https://imagedelivery.net/5LGXGUnHU18h6ehN_xjpXQ/abc123",
operations: {},
options: {
host: "imagedelivery.net",
accountHash: "5LGXGUnHU18h6ehN_xjpXQ",
imageId: "abc123",
},
});
},
);

await t.step(
"should handle URL without any transformations or variant",
() => {
const result = extract(
"https://imagedelivery.net/5LGXGUnHU18h6ehN_xjpXQ/abc123",
);
assertEquals(result, {
src: "https://imagedelivery.net/5LGXGUnHU18h6ehN_xjpXQ/abc123",
operations: {},
options: {
host: "imagedelivery.net",
accountHash: "5LGXGUnHU18h6ehN_xjpXQ",
imageId: "abc123",
},
});
},
);
});

Deno.test("Cloudflare Images CDN - generate", async (t) => {
Expand Down Expand Up @@ -154,4 +190,40 @@ Deno.test("Cloudflare Images CDN - transform", async (t) => {
);
}
});

await t.step(
"should transform URL with named variant without =undefined issue",
() => {
const result = transform(
"https://imagedelivery.net/5LGXGUnHU18h6ehN_xjpXQ/abc123/public",
{
width: 640,
height: 480,
},
{},
);
assertEqualIgnoringQueryOrder(
result,
"https://imagedelivery.net/5LGXGUnHU18h6ehN_xjpXQ/abc123/w=640,h=480,fit=cover",
);
},
);

await t.step(
"should transform URL without variant",
() => {
const result = transform(
"https://imagedelivery.net/5LGXGUnHU18h6ehN_xjpXQ/abc123",
{
width: 640,
height: 480,
},
{},
);
assertEqualIgnoringQueryOrder(
result,
"https://imagedelivery.net/5LGXGUnHU18h6ehN_xjpXQ/abc123/w=640,h=480,fit=cover",
);
},
);
});
4 changes: 3 additions & 1 deletion src/providers/cloudflare_images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ export const extract: URLExtractor<
}

const { host, accountHash, imageId, transformations } = matches[0].groups;
const operations = operationsParser(transformations || "");
// Named variants (like "public") don't contain "=", only actual transformations do
const hasTransformations = transformations?.includes("=");
const operations = operationsParser(hasTransformations ? transformations : "");

const options = { host, accountHash, imageId };

Expand Down