diff --git a/src/providers/cloudflare.ts b/src/providers/cloudflare.ts index a6ce00a..773a23f 100644 --- a/src/providers/cloudflare.ts +++ b/src/providers/cloudflare.ts @@ -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); diff --git a/src/providers/cloudflare_images.test.ts b/src/providers/cloudflare_images.test.ts index 61abc49..54a3efd 100644 --- a/src/providers/cloudflare_images.test.ts +++ b/src/providers/cloudflare_images.test.ts @@ -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) => { @@ -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", + ); + }, + ); }); diff --git a/src/providers/cloudflare_images.ts b/src/providers/cloudflare_images.ts index 0d72091..97360d3 100644 --- a/src/providers/cloudflare_images.ts +++ b/src/providers/cloudflare_images.ts @@ -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 };