-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { assertEquals } from "https://deno.land/std@0.172.0/testing/asserts.ts"; | ||
|
||
import { ParsedUrl } from "../types.ts"; | ||
import { AstroParams, parse, transform } from "./astro.ts"; | ||
|
||
const img = | ||
"https://images.ctfassets.net/aaaa/xxxx/yyyy/how-to-wow-a-customer.jpg"; | ||
|
||
Deno.test("astro parser", () => { | ||
const parsed = parse(img); | ||
const expected: ParsedUrl<AstroParams> = { | ||
base: "/_image?href=https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg", | ||
params: { | ||
"href": "https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg" | ||
}, | ||
cdn: "astro" | ||
} | ||
assertEquals(JSON.stringify(parsed), JSON.stringify(expected)); | ||
}); | ||
|
||
Deno.test("astro parser endpoint", () => { | ||
const parsed = parse("/_image?href=https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg"); | ||
const expected: ParsedUrl<AstroParams> = { | ||
base: "/_image?href=https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg", | ||
params: { | ||
"href": "https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg" | ||
}, | ||
cdn: "astro" | ||
} | ||
assertEquals(JSON.stringify(parsed), JSON.stringify(expected)); | ||
}); | ||
|
||
|
||
Deno.test("astro", async (t) => { | ||
await t.step("should format a URL", () => { | ||
const result = transform({ | ||
url: img, | ||
width: 200, | ||
height: 100, | ||
}); | ||
assertEquals( | ||
result?.toString(), | ||
"/_image?href=https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg&w=200&h=100", | ||
); | ||
}); | ||
await t.step("should not set height if not provided", () => { | ||
const result = transform({ url: img, width: 200 }); | ||
assertEquals( | ||
result?.toString(), | ||
"/_image?href=https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg&w=200", | ||
); | ||
}); | ||
await t.step("should delete height if not set", () => { | ||
const url = new URL(img); | ||
url.searchParams.set("h", "100"); | ||
const result = transform({ url, width: 200 }); | ||
assertEquals( | ||
result?.toString(), | ||
"/_image?href=https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg&w=200", | ||
); | ||
}); | ||
|
||
await t.step("should round non-integer params", () => { | ||
const result = transform({ | ||
url: img, | ||
width: 200.6, | ||
height: 100.2, | ||
}); | ||
assertEquals( | ||
result?.toString(), | ||
"/_image?href=https%3A%2F%2Fimages.ctfassets.net%2Faaaa%2Fxxxx%2Fyyyy%2Fhow-to-wow-a-customer.jpg&w=201&h=100", | ||
); | ||
}); | ||
|
||
await t.step("should transform a local image with a relative base", () => { | ||
const result = transform({ | ||
url: "/static/moose.png", | ||
width: 100, | ||
height: 200, | ||
format: "webp", | ||
}); | ||
assertEquals( | ||
result?.toString(), | ||
"/_image?href=%2Fstatic%2Fmoose.png&w=100&h=200&f=webp", | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { UrlParser, UrlTransformer, ShouldDelegateUrl } from "../types.ts"; | ||
import { | ||
getNumericParam, | ||
setParamIfDefined, | ||
toUrl, | ||
toCanonicalUrlString, | ||
} from "../utils.ts"; | ||
import { getImageCdnForUrlByDomain } from "../detect.ts"; | ||
|
||
export interface AstroParams { | ||
href: string; | ||
quality?: string | number; | ||
} | ||
|
||
export const delegateUrl: ShouldDelegateUrl = (url) => { | ||
const parsedUrl = toUrl(url); | ||
const searchParamHref = parsedUrl.searchParams.get("href") | ||
const decodedHref = typeof searchParamHref === "string" | ||
? decodeURIComponent(searchParamHref) | ||
: new URL(parsedUrl.pathname, parsedUrl.origin).toString() | ||
const source = toCanonicalUrlString(toUrl(decodedHref)); | ||
|
||
if (!source || !source.startsWith("http")) { | ||
return false; | ||
} | ||
const cdn = getImageCdnForUrlByDomain(source); | ||
if (!cdn) { | ||
return false; | ||
} | ||
return { | ||
cdn, | ||
url: source, | ||
}; | ||
}; | ||
|
||
export const parse: UrlParser<AstroParams> = (url) => { | ||
const parsedUrl = toUrl(url); | ||
const searchParamHref = parsedUrl.searchParams.get("href") | ||
const decodedHref = typeof searchParamHref === "string" | ||
? decodeURIComponent(searchParamHref) | ||
: new URL(parsedUrl.pathname, parsedUrl.origin).toString() | ||
const encodedHref = encodeURIComponent(toCanonicalUrlString(toUrl(decodedHref))); | ||
const width = getNumericParam(parsedUrl, "w") || undefined; | ||
const height = getNumericParam(parsedUrl, "h") || undefined; | ||
const format = parsedUrl.searchParams.get("f") || undefined; | ||
const quality = parsedUrl.searchParams.get("q") || undefined; | ||
|
||
return { | ||
width, | ||
height, | ||
format, | ||
base: `/_image?href=${encodedHref}`, | ||
params: { quality, href: encodedHref }, | ||
cdn: "astro", | ||
}; | ||
}; | ||
|
||
export const transform: UrlTransformer = ( | ||
{ url: originalUrl, width, height, format, }, | ||
) => { | ||
const parsedUrl = toUrl(originalUrl); | ||
const href = toCanonicalUrlString(new URL(parsedUrl.pathname, parsedUrl.origin)) | ||
const url = {searchParams: new URLSearchParams()} as URL | ||
|
||
setParamIfDefined(url, "href", href, true, true); | ||
setParamIfDefined(url, "w", width, true, true); | ||
setParamIfDefined(url, "h", height, true, true); | ||
setParamIfDefined(url, "f", format); | ||
|
||
return `/_image?${url.searchParams.toString()}`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters