@hamur/image 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +24 -2
- package/dist/chunks/bun-index-n7xnn0a2.js +502 -0
- package/dist/src/adapters/angular.js +275 -0
- package/dist/src/adapters/html.js +70 -0
- package/dist/src/adapters/preact.js +82 -0
- package/dist/src/adapters/react.js +84 -0
- package/dist/src/adapters/solid.js +93 -0
- package/dist/src/adapters/svelte.js +15 -0
- package/dist/src/adapters/vue.js +147 -0
- package/dist/src/index.js +1442 -0
- package/dist/src/server/index.js +9 -0
- package/dist/types/src/adapters/angular-img.d.ts +40 -0
- package/dist/types/src/adapters/angular-picture.d.ts +36 -0
- package/dist/types/src/adapters/angular.d.ts +3 -0
- package/dist/types/src/adapters/html-img.d.ts +10 -0
- package/dist/types/src/adapters/html-picture.d.ts +6 -0
- package/dist/types/src/adapters/html.d.ts +6 -0
- package/dist/types/src/adapters/preact-img.d.ts +8 -0
- package/dist/types/src/adapters/preact-picture.d.ts +6 -0
- package/dist/types/src/adapters/preact.d.ts +6 -0
- package/dist/types/src/adapters/react-img.d.ts +8 -0
- package/dist/types/src/adapters/react-picture.d.ts +6 -0
- package/dist/types/src/adapters/react.d.ts +6 -0
- package/dist/types/src/adapters/solid-img.d.ts +8 -0
- package/dist/types/src/adapters/solid-picture.d.ts +6 -0
- package/dist/types/src/adapters/solid.d.ts +6 -0
- package/dist/types/src/adapters/svelte.d.ts +3 -0
- package/dist/types/src/adapters/vue-img.d.ts +119 -0
- package/dist/types/src/adapters/vue-picture.d.ts +121 -0
- package/dist/types/src/adapters/vue.d.ts +234 -0
- package/dist/types/src/build/endpoint.d.ts +12 -0
- package/dist/types/src/build/platform.d.ts +24 -0
- package/dist/types/src/build/runtime.d.ts +9 -0
- package/dist/types/src/build/types.d.ts +4 -0
- package/dist/types/src/build/variants.d.ts +8 -0
- package/dist/types/src/domains.d.ts +27 -0
- package/dist/types/src/index.d.ts +9 -0
- package/dist/types/src/module.d.ts +3 -0
- package/dist/types/src/options.d.ts +15 -0
- package/dist/types/src/providers/bunny.d.ts +9 -0
- package/dist/types/src/providers/cloudflare.d.ts +9 -0
- package/dist/types/src/providers/cloudflareImages.d.ts +9 -0
- package/dist/types/src/providers/cloudinary.d.ts +9 -0
- package/dist/types/src/providers/contentful.d.ts +9 -0
- package/dist/types/src/providers/fastly.d.ts +9 -0
- package/dist/types/src/providers/hamur-url.d.ts +3 -0
- package/dist/types/src/providers/hamur.d.ts +3 -0
- package/dist/types/src/providers/imagekit.d.ts +9 -0
- package/dist/types/src/providers/imgix.d.ts +9 -0
- package/dist/types/src/providers/imgproxy.d.ts +9 -0
- package/dist/types/src/providers/ipx.d.ts +9 -0
- package/dist/types/src/providers/netlify.d.ts +9 -0
- package/dist/types/src/providers/none.d.ts +8 -0
- package/dist/types/src/providers/sanity.d.ts +9 -0
- package/dist/types/src/providers/shared.d.ts +7 -0
- package/dist/types/src/providers/storyblok.d.ts +9 -0
- package/dist/types/src/providers/strapi.d.ts +9 -0
- package/dist/types/src/providers/supabase.d.ts +9 -0
- package/dist/types/src/providers/twicpics.d.ts +9 -0
- package/dist/types/src/providers/uploadcare.d.ts +9 -0
- package/dist/types/src/providers/vercel-url.d.ts +3 -0
- package/dist/types/src/providers/vercel.d.ts +3 -0
- package/dist/types/src/providers/weserv.d.ts +9 -0
- package/dist/types/src/runtime/image.d.ts +28 -0
- package/dist/types/src/runtime/img.d.ts +71 -0
- package/dist/types/src/runtime/index.d.ts +9 -0
- package/dist/types/src/runtime/picture.d.ts +24 -0
- package/dist/types/src/runtime/sizes.d.ts +8 -0
- package/dist/types/src/runtime/source.d.ts +3 -0
- package/dist/types/src/server/door.d.ts +1 -0
- package/dist/types/src/server/endpoint.d.ts +46 -0
- package/dist/types/src/server/handler.d.ts +1 -0
- package/dist/types/src/server/index.d.ts +1 -0
- package/dist/types/src/server/miss-handler.d.ts +1 -0
- package/dist/types/src/server/miss.d.ts +1 -0
- package/dist/types/src/server/parse.d.ts +30 -0
- package/dist/types/src/server/remote.d.ts +20 -0
- package/dist/types/src/types.d.ts +178 -0
- package/package.json +114 -4
- package/src/adapters/Img.svelte +33 -0
- package/src/adapters/Picture.svelte +39 -0
- package/src/adapters/angular-img.ts +144 -0
- package/src/adapters/angular-picture.ts +146 -0
- package/src/adapters/angular.ts +3 -0
- package/src/adapters/html-img.ts +42 -0
- package/src/adapters/html-picture.ts +30 -0
- package/src/adapters/html.ts +9 -0
- package/src/adapters/preact-img.ts +51 -0
- package/src/adapters/preact-picture.ts +40 -0
- package/src/adapters/preact.ts +9 -0
- package/src/adapters/react-img.ts +54 -0
- package/src/adapters/react-picture.ts +49 -0
- package/src/adapters/react.ts +9 -0
- package/src/adapters/solid-img.ts +47 -0
- package/src/adapters/solid-picture.ts +43 -0
- package/src/adapters/solid.ts +9 -0
- package/src/adapters/svelte.ts +3 -0
- package/src/adapters/test/ImgCustomPage.svelte +14 -0
- package/src/adapters/test/ImgPage.svelte +5 -0
- package/src/adapters/test/ImgPreloadPage.svelte +6 -0
- package/src/adapters/test/PicturePage.svelte +6 -0
- package/src/adapters/test/PicturePreloadPage.svelte +5 -0
- package/src/adapters/test/support.ts +116 -0
- package/src/adapters/vue-img.ts +72 -0
- package/src/adapters/vue-picture.ts +64 -0
- package/src/adapters/vue.ts +7 -0
- package/src/build/endpoint.ts +78 -0
- package/src/build/platform.ts +99 -0
- package/src/build/runtime.ts +118 -0
- package/src/build/types.ts +23 -0
- package/src/build/variants.ts +174 -0
- package/src/domains.ts +84 -0
- package/src/env.d.ts +24 -0
- package/src/index.ts +23 -0
- package/src/module.ts +29 -0
- package/src/options.ts +329 -0
- package/src/providers/bunny.ts +21 -0
- package/src/providers/cloudflare.ts +22 -0
- package/src/providers/cloudflareImages.ts +25 -0
- package/src/providers/cloudinary.ts +31 -0
- package/src/providers/contentful.ts +26 -0
- package/src/providers/fastly.ts +29 -0
- package/src/providers/hamur-url.ts +81 -0
- package/src/providers/hamur.ts +8 -0
- package/src/providers/imagekit.ts +30 -0
- package/src/providers/imgix.ts +26 -0
- package/src/providers/imgproxy.ts +24 -0
- package/src/providers/ipx.ts +26 -0
- package/src/providers/netlify.ts +23 -0
- package/src/providers/none.ts +9 -0
- package/src/providers/sanity.ts +29 -0
- package/src/providers/shared.ts +17 -0
- package/src/providers/storyblok.ts +24 -0
- package/src/providers/strapi.ts +24 -0
- package/src/providers/supabase.ts +26 -0
- package/src/providers/twicpics.ts +26 -0
- package/src/providers/uploadcare.ts +32 -0
- package/src/providers/vercel-url.ts +18 -0
- package/src/providers/vercel.ts +8 -0
- package/src/providers/weserv.ts +25 -0
- package/src/runtime/image.ts +266 -0
- package/src/runtime/img.ts +241 -0
- package/src/runtime/index.ts +18 -0
- package/src/runtime/picture.ts +62 -0
- package/src/runtime/sizes.ts +52 -0
- package/src/runtime/source.ts +8 -0
- package/src/server/door.ts +1 -0
- package/src/server/endpoint.ts +428 -0
- package/src/server/handler.ts +33 -0
- package/src/server/index.ts +1 -0
- package/src/server/miss-handler.ts +20 -0
- package/src/server/miss.ts +1 -0
- package/src/server/parse.ts +152 -0
- package/src/server/remote.ts +166 -0
- package/src/types.ts +177 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { localPath, REMOTE } from "../runtime/source.ts"
|
|
2
|
+
import type { ImageProvider } from "../types.ts"
|
|
3
|
+
import { JPG, option } from "./shared.ts"
|
|
4
|
+
|
|
5
|
+
/** An IPX server the app runs; `options.origin` is its URL. A local `src` is its path below the source dir, a remote one the URL IPX's `http` storage takes (its `domains` allowlist). */
|
|
6
|
+
export default {
|
|
7
|
+
formats: ["avif", "webp", "jpeg", "png"],
|
|
8
|
+
negotiates: false,
|
|
9
|
+
fits: ["cover", "contain", "fill", "inside", "outside"],
|
|
10
|
+
modifiers: ["rotate", "flip", "flop"],
|
|
11
|
+
getImage(src, { modifiers: m, options, baseURL }) {
|
|
12
|
+
const origin = option("ipx", options, "origin")
|
|
13
|
+
const steps: string[] = []
|
|
14
|
+
if (m.width !== undefined && m.height !== undefined) steps.push(`s_${m.width}x${m.height}`)
|
|
15
|
+
else if (m.width !== undefined) steps.push(`w_${m.width}`)
|
|
16
|
+
else if (m.height !== undefined) steps.push(`h_${m.height}`)
|
|
17
|
+
if (m.fit !== undefined) steps.push(`fit_${m.fit}`)
|
|
18
|
+
if (m.format !== undefined) steps.push(`f_${JPG[m.format]}`)
|
|
19
|
+
if (m.quality !== undefined) steps.push(`q_${m.quality}`)
|
|
20
|
+
if (m.rotate !== undefined) steps.push(`rotate_${m.rotate}`)
|
|
21
|
+
if (m.flip === true) steps.push("flip")
|
|
22
|
+
if (m.flop === true) steps.push("flop")
|
|
23
|
+
const id = REMOTE.test(src) ? src : localPath(src, baseURL)
|
|
24
|
+
return `${origin}/${steps.length ? steps.join(",") : "_"}/${id}`
|
|
25
|
+
},
|
|
26
|
+
} satisfies ImageProvider
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { localPath, REMOTE } from "../runtime/source.ts"
|
|
2
|
+
import type { ImageFit, ImageFormat, ImageProvider } from "../types.ts"
|
|
3
|
+
|
|
4
|
+
const FITS: Partial<Record<ImageFit, string>> = { cover: "cover", inside: "contain", fill: "fill" }
|
|
5
|
+
const FORMATS: Record<ImageFormat, string> = { avif: "avif", webp: "webp", jpeg: "jpg", png: "png" }
|
|
6
|
+
|
|
7
|
+
/** The Netlify Image CDN; `fit=contain` there is `inside`. */
|
|
8
|
+
export default {
|
|
9
|
+
formats: ["avif", "webp", "jpeg", "png"],
|
|
10
|
+
negotiates: true,
|
|
11
|
+
fits: ["cover", "inside", "fill"],
|
|
12
|
+
modifiers: [],
|
|
13
|
+
getImage(src, { modifiers: m, baseURL }) {
|
|
14
|
+
const url = REMOTE.test(src) ? src : `${baseURL}${localPath(src, baseURL)}`
|
|
15
|
+
const query = [`url=${encodeURIComponent(url)}`]
|
|
16
|
+
if (m.width !== undefined) query.push(`w=${m.width}`)
|
|
17
|
+
if (m.height !== undefined) query.push(`h=${m.height}`)
|
|
18
|
+
if (m.fit !== undefined) query.push(`fit=${FITS[m.fit]}`)
|
|
19
|
+
if (m.format !== undefined) query.push(`fm=${FORMATS[m.format]}`)
|
|
20
|
+
if (m.quality !== undefined) query.push(`q=${m.quality}`)
|
|
21
|
+
return `/.netlify/images?${query.join("&")}`
|
|
22
|
+
},
|
|
23
|
+
} satisfies ImageProvider
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { localPath, REMOTE } from "../runtime/source.ts"
|
|
2
|
+
import type { ImageFit, ImageProvider } from "../types.ts"
|
|
3
|
+
import { JPG, option, withQuery } from "./shared.ts"
|
|
4
|
+
|
|
5
|
+
const FITS: Partial<Record<ImageFit, string>> = { cover: "crop", inside: "clip", contain: "fill", fill: "scale" }
|
|
6
|
+
|
|
7
|
+
/** The Sanity image pipeline; a local `src` is a file name below `options.projectId` and `options.dataset` (default `production`), a remote one is the asset URL. No AVIF there. */
|
|
8
|
+
export default {
|
|
9
|
+
formats: ["webp", "jpeg", "png"],
|
|
10
|
+
negotiates: true,
|
|
11
|
+
fits: ["cover", "inside", "contain", "fill"],
|
|
12
|
+
modifiers: ["rotate", "flip", "flop"],
|
|
13
|
+
getImage(src, { modifiers: m, options, baseURL }) {
|
|
14
|
+
const query: string[] = []
|
|
15
|
+
if (m.width !== undefined) query.push(`w=${m.width}`)
|
|
16
|
+
if (m.height !== undefined) query.push(`h=${m.height}`)
|
|
17
|
+
if (m.fit !== undefined) query.push(`fit=${FITS[m.fit]}`)
|
|
18
|
+
query.push(m.format === undefined ? "auto=format" : `fm=${JPG[m.format]}`)
|
|
19
|
+
if (m.quality !== undefined) query.push(`q=${m.quality}`)
|
|
20
|
+
if (m.rotate !== undefined) query.push(`or=${m.rotate}`)
|
|
21
|
+
if (m.flip === true || m.flop === true)
|
|
22
|
+
query.push(`flip=${m.flop === true ? "h" : ""}${m.flip === true ? "v" : ""}`)
|
|
23
|
+
if (REMOTE.test(src)) return withQuery(src, query)
|
|
24
|
+
const origin = option("sanity", options, "origin", "https://cdn.sanity.io")
|
|
25
|
+
const project = option("sanity", options, "projectId")
|
|
26
|
+
const dataset = option("sanity", options, "dataset", "production")
|
|
27
|
+
return withQuery(`${origin}/images/${project}/${dataset}/${localPath(src, baseURL)}`, query)
|
|
28
|
+
},
|
|
29
|
+
} satisfies ImageProvider
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ImageFormat } from "../types.ts"
|
|
2
|
+
|
|
3
|
+
/** The string option `key` of `image.providers.<provider>.options` without a trailing slash; `fallback` stands in when it is unset. */
|
|
4
|
+
export function option(provider: string, options: Record<string, unknown>, key: string, fallback?: string): string {
|
|
5
|
+
const value = options[key] ?? fallback
|
|
6
|
+
if (typeof value !== "string" || value === "")
|
|
7
|
+
throw new Error(`image.providers.${provider}.options.${key} is required`)
|
|
8
|
+
return value.replace(/\/+$/, "")
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** `url` with `pairs` (`key=value`) after its query, if any. */
|
|
12
|
+
export function withQuery(url: string, pairs: string[]): string {
|
|
13
|
+
return pairs.length ? `${url}${url.includes("?") ? "&" : "?"}${pairs.join("&")}` : url
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** The name most services give an output format: `jpg` for `jpeg`. */
|
|
17
|
+
export const JPG: Record<ImageFormat, string> = { avif: "avif", webp: "webp", jpeg: "jpg", png: "png" }
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { localPath, REMOTE } from "../runtime/source.ts"
|
|
2
|
+
import type { ImageProvider } from "../types.ts"
|
|
3
|
+
import { option } from "./shared.ts"
|
|
4
|
+
|
|
5
|
+
/** The Storyblok Image Service, which takes the operations after `/m/` on the asset's own URL; a local `src` is the asset's path below `options.origin` (default `https://a.storyblok.com`). A size has `0` for the side left open, and `fit-in` there is `inside`. */
|
|
6
|
+
export default {
|
|
7
|
+
formats: ["avif", "webp", "jpeg", "png"],
|
|
8
|
+
negotiates: false,
|
|
9
|
+
fits: ["cover", "inside"],
|
|
10
|
+
modifiers: [],
|
|
11
|
+
getImage(src, { modifiers: m, options, baseURL }) {
|
|
12
|
+
const asset = REMOTE.test(src)
|
|
13
|
+
? src
|
|
14
|
+
: `${option("storyblok", options, "origin", "https://a.storyblok.com")}/${localPath(src, baseURL)}`
|
|
15
|
+
const operations: string[] = []
|
|
16
|
+
if (m.width !== undefined || m.height !== undefined)
|
|
17
|
+
operations.push(`${m.fit === "inside" ? "fit-in/" : ""}${m.width ?? 0}x${m.height ?? 0}`)
|
|
18
|
+
const filters: string[] = []
|
|
19
|
+
if (m.format !== undefined) filters.push(`format(${m.format})`)
|
|
20
|
+
if (m.quality !== undefined) filters.push(`quality(${m.quality})`)
|
|
21
|
+
if (filters.length) operations.push(`filters:${filters.join(":")}`)
|
|
22
|
+
return `${asset}/m/${operations.join("/")}`
|
|
23
|
+
},
|
|
24
|
+
} satisfies ImageProvider
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { localPath, REMOTE } from "../runtime/source.ts"
|
|
2
|
+
import type { ImageProvider } from "../types.ts"
|
|
3
|
+
|
|
4
|
+
const BREAKPOINTS: [name: string, width: number][] = [
|
|
5
|
+
["small", 500],
|
|
6
|
+
["medium", 750],
|
|
7
|
+
["large", 1000],
|
|
8
|
+
]
|
|
9
|
+
|
|
10
|
+
/** Strapi's upload plugin has no transform URL; its default responsive breakpoints are `small` (500), `medium` (750) and `large` (1000), and the file of each is the original name with `small_`, `medium_` or `large_` before it, and the smallest one that is at least `width` wide is used. A wider `width`, or none, gives the original. `options.origin` is Strapi's URL for a local `src`. */
|
|
11
|
+
export default {
|
|
12
|
+
formats: [],
|
|
13
|
+
negotiates: false,
|
|
14
|
+
fits: [],
|
|
15
|
+
modifiers: [],
|
|
16
|
+
getImage(src, { modifiers: m, options, baseURL }) {
|
|
17
|
+
const origin = typeof options.origin === "string" ? options.origin.replace(/\/+$/, "") : ""
|
|
18
|
+
const url = REMOTE.test(src) ? src : `${origin}/${localPath(src, baseURL)}`
|
|
19
|
+
const breakpoint = m.width === undefined ? undefined : BREAKPOINTS.find(([, width]) => width >= m.width!)
|
|
20
|
+
if (breakpoint === undefined) return url
|
|
21
|
+
const slash = url.lastIndexOf("/")
|
|
22
|
+
return `${url.slice(0, slash + 1)}${breakpoint[0]}_${url.slice(slash + 1)}`
|
|
23
|
+
},
|
|
24
|
+
} satisfies ImageProvider
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { localPath, REMOTE } from "../runtime/source.ts"
|
|
2
|
+
import type { ImageFit, ImageProvider } from "../types.ts"
|
|
3
|
+
import { option, withQuery } from "./shared.ts"
|
|
4
|
+
|
|
5
|
+
const FITS: Partial<Record<ImageFit, string>> = { cover: "cover", inside: "contain", fill: "fill" }
|
|
6
|
+
const OBJECT = "/storage/v1/object/public/"
|
|
7
|
+
const RENDER = "/storage/v1/render/image/public/"
|
|
8
|
+
|
|
9
|
+
/** Supabase Storage image transformations of a public bucket; `options.origin` is the project's URL (`https://<project>.supabase.co`) and a local `src` is `/<bucket>/<path>`. A remote `src` of the object route is moved to the render route. The service picks WebP from `Accept`, and `resize=contain` there is `inside`. */
|
|
10
|
+
export default {
|
|
11
|
+
formats: [],
|
|
12
|
+
negotiates: true,
|
|
13
|
+
fits: ["cover", "inside", "fill"],
|
|
14
|
+
modifiers: [],
|
|
15
|
+
getImage(src, { modifiers: m, options, baseURL }) {
|
|
16
|
+
const query: string[] = []
|
|
17
|
+
if (m.width !== undefined) query.push(`width=${m.width}`)
|
|
18
|
+
if (m.height !== undefined) query.push(`height=${m.height}`)
|
|
19
|
+
if (m.fit !== undefined) query.push(`resize=${FITS[m.fit]}`)
|
|
20
|
+
if (m.quality !== undefined) query.push(`quality=${m.quality}`)
|
|
21
|
+
const url = REMOTE.test(src)
|
|
22
|
+
? src.replace(OBJECT, RENDER)
|
|
23
|
+
: `${option("supabase", options, "origin")}${RENDER}${localPath(src, baseURL)}`
|
|
24
|
+
return withQuery(url, query)
|
|
25
|
+
},
|
|
26
|
+
} satisfies ImageProvider
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { localPath, REMOTE } from "../runtime/source.ts"
|
|
2
|
+
import type { ImageFit, ImageProvider } from "../types.ts"
|
|
3
|
+
import { option, withQuery } from "./shared.ts"
|
|
4
|
+
|
|
5
|
+
const FITS: Partial<Record<ImageFit, string>> = { cover: "cover", inside: "contain", fill: "resize" }
|
|
6
|
+
|
|
7
|
+
/** TwicPics; `options.origin` is the domain, `https://demo.twic.pics` for example. `contain` there fits inside without padding, so it is `inside`; a fit needs both sizes, else `resize=<width>`. `flip=y` mirrors vertically. A remote `src` gets the `twic` query as it is. */
|
|
8
|
+
export default {
|
|
9
|
+
formats: ["avif", "webp", "jpeg", "png"],
|
|
10
|
+
negotiates: true,
|
|
11
|
+
fits: ["cover", "inside", "fill"],
|
|
12
|
+
modifiers: ["rotate", "flip", "flop"],
|
|
13
|
+
getImage(src, { modifiers: m, options, baseURL }) {
|
|
14
|
+
const steps = ["v1"]
|
|
15
|
+
if (m.rotate !== undefined) steps.push(`turn=${m.rotate}`)
|
|
16
|
+
if (m.flip === true || m.flop === true)
|
|
17
|
+
steps.push(`flip=${m.flip === true && m.flop === true ? "both" : m.flip === true ? "y" : "x"}`)
|
|
18
|
+
if (m.fit !== undefined && m.width !== undefined && m.height !== undefined)
|
|
19
|
+
steps.push(`${FITS[m.fit]}=${m.width}x${m.height}`)
|
|
20
|
+
else if (m.width !== undefined) steps.push(`resize=${m.width}`)
|
|
21
|
+
if (m.format !== undefined) steps.push(`output=${m.format}`)
|
|
22
|
+
if (m.quality !== undefined) steps.push(`quality=${m.quality}`)
|
|
23
|
+
const url = REMOTE.test(src) ? src : `${option("twicpics", options, "origin")}/${localPath(src, baseURL)}`
|
|
24
|
+
return withQuery(url, steps.length > 1 ? [`twic=${steps.join("/")}`] : [])
|
|
25
|
+
},
|
|
26
|
+
} satisfies ImageProvider
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { localPath, REMOTE } from "../runtime/source.ts"
|
|
2
|
+
import type { ImageProvider } from "../types.ts"
|
|
3
|
+
import { option } from "./shared.ts"
|
|
4
|
+
|
|
5
|
+
function level(quality: number): string {
|
|
6
|
+
if (quality <= 25) return "lightest"
|
|
7
|
+
if (quality <= 45) return "lighter"
|
|
8
|
+
if (quality <= 70) return "normal"
|
|
9
|
+
if (quality <= 85) return "better"
|
|
10
|
+
return "best"
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Uploadcare CDN operations; `src` is `/<uuid>/…` or an asset URL; the file name after the uuid is dropped, and the file is served from `options.origin` (default `https://ucarecdn.com`). `-/rotate/` turns counter-clockwise, so `rotate` is sent as 360 minus it; `-/format/` has no AVIF, only `auto` may choose it; the docs do not say which axis `-/flip/` and `-/mirror/` use, so neither is made. The service takes named qualities only: up to 25 is `lightest`, 45 `lighter`, 70 `normal`, 85 `better`, above `best`. */
|
|
14
|
+
export default {
|
|
15
|
+
formats: ["webp", "jpeg", "png"],
|
|
16
|
+
negotiates: true,
|
|
17
|
+
fits: ["cover", "inside"],
|
|
18
|
+
modifiers: ["rotate"],
|
|
19
|
+
getImage(src, { modifiers: m, options, baseURL }) {
|
|
20
|
+
const origin = option("uploadcare", options, "origin", "https://ucarecdn.com")
|
|
21
|
+
const uuid = (REMOTE.test(src) ? new URL(src).pathname.slice(1) : localPath(src, baseURL)).split("/")[0]
|
|
22
|
+
const steps: string[] = []
|
|
23
|
+
if (m.rotate !== undefined) steps.push(`rotate/${360 - m.rotate}`)
|
|
24
|
+
if (m.width !== undefined || m.height !== undefined) {
|
|
25
|
+
const size = `${m.width ?? ""}x${m.height ?? ""}`
|
|
26
|
+
steps.push(m.fit === "cover" ? `scale_crop/${size}/center` : `resize/${size}`)
|
|
27
|
+
}
|
|
28
|
+
steps.push(`format/${m.format ?? "auto"}`)
|
|
29
|
+
if (m.quality !== undefined) steps.push(`quality/${level(m.quality)}`)
|
|
30
|
+
return `${origin}/${uuid}/${steps.map((step) => `-/${step}/`).join("")}`
|
|
31
|
+
},
|
|
32
|
+
} satisfies ImageProvider
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { localPath, REMOTE } from "../runtime/source.ts"
|
|
2
|
+
import type { ImageProvider } from "../types.ts"
|
|
3
|
+
|
|
4
|
+
/** Vercel's image optimisation, which refuses a width outside `images.sizes` and needs a quality; `widths` is `image.widths`, sorted. */
|
|
5
|
+
export function createVercelProvider(widths: readonly number[]): ImageProvider {
|
|
6
|
+
return {
|
|
7
|
+
formats: [],
|
|
8
|
+
negotiates: true,
|
|
9
|
+
fits: [],
|
|
10
|
+
modifiers: [],
|
|
11
|
+
getImage(src, { modifiers: m, baseURL }) {
|
|
12
|
+
if (m.width === undefined || widths.length === 0) return src
|
|
13
|
+
const width = widths.find((allowed) => allowed >= m.width!) ?? widths[widths.length - 1]!
|
|
14
|
+
const url = REMOTE.test(src) ? src : `${baseURL}${localPath(src, baseURL)}`
|
|
15
|
+
return `/_vercel/image?url=${encodeURIComponent(url)}&w=${width}&q=${m.quality ?? 75}`
|
|
16
|
+
},
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/// <reference path="../env.d.ts" />
|
|
2
|
+
import options from "#hamur/image/options"
|
|
3
|
+
import type { ImageProvider } from "../types.ts"
|
|
4
|
+
import { createVercelProvider } from "./vercel-url.ts"
|
|
5
|
+
|
|
6
|
+
const provider: ImageProvider = createVercelProvider(options.widths)
|
|
7
|
+
|
|
8
|
+
export default provider
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { localPath, REMOTE } from "../runtime/source.ts"
|
|
2
|
+
import type { ImageProvider } from "../types.ts"
|
|
3
|
+
import { JPG, option } from "./shared.ts"
|
|
4
|
+
|
|
5
|
+
/** images.weserv.nl; `options.origin` is the service (default `https://wsrv.nl`) and `options.sourceURL` the public URL of the app, which a local `src` is read from. */
|
|
6
|
+
export default {
|
|
7
|
+
formats: ["webp", "jpeg", "png"],
|
|
8
|
+
negotiates: false,
|
|
9
|
+
fits: ["cover", "contain", "fill", "inside", "outside"],
|
|
10
|
+
modifiers: ["rotate", "flip", "flop"],
|
|
11
|
+
getImage(src, { modifiers: m, options, baseURL }) {
|
|
12
|
+
const origin = option("weserv", options, "origin", "https://wsrv.nl")
|
|
13
|
+
const source = REMOTE.test(src) ? src : `${option("weserv", options, "sourceURL")}/${localPath(src, baseURL)}`
|
|
14
|
+
const query = [`url=${encodeURIComponent(source)}`]
|
|
15
|
+
if (m.width !== undefined) query.push(`w=${m.width}`)
|
|
16
|
+
if (m.height !== undefined) query.push(`h=${m.height}`)
|
|
17
|
+
if (m.fit !== undefined) query.push(`fit=${m.fit}`)
|
|
18
|
+
if (m.format !== undefined) query.push(`output=${JPG[m.format]}`)
|
|
19
|
+
if (m.quality !== undefined) query.push(`q=${m.quality}`)
|
|
20
|
+
if (m.rotate !== undefined) query.push(`ro=${m.rotate}`)
|
|
21
|
+
if (m.flip === true) query.push("flip")
|
|
22
|
+
if (m.flop === true) query.push("flop")
|
|
23
|
+
return `${origin}/?${query.join("&")}`
|
|
24
|
+
},
|
|
25
|
+
} satisfies ImageProvider
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import type { ImageMeta } from "hamur"
|
|
2
|
+
import { compileDomain, matchesDomain } from "../domains.ts"
|
|
3
|
+
import type {
|
|
4
|
+
ImageFormat,
|
|
5
|
+
ImageModifiers,
|
|
6
|
+
ImageProvider,
|
|
7
|
+
ImageRuntimeOptions,
|
|
8
|
+
ImageSizes,
|
|
9
|
+
ImageSources,
|
|
10
|
+
HamurImagePreset,
|
|
11
|
+
HamurImageProvider,
|
|
12
|
+
} from "../types.ts"
|
|
13
|
+
import { localPath, REMOTE } from "./source.ts"
|
|
14
|
+
import { planSizes } from "./sizes.ts"
|
|
15
|
+
|
|
16
|
+
export interface ImageCallOptions {
|
|
17
|
+
provider?: HamurImageProvider
|
|
18
|
+
preset?: HamurImagePreset
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface GetSizesOptions extends ImageCallOptions {
|
|
22
|
+
sizes?: string
|
|
23
|
+
width?: number
|
|
24
|
+
height?: number
|
|
25
|
+
densities?: number[]
|
|
26
|
+
modifiers?: ImageModifiers
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface ImageApi {
|
|
30
|
+
getImage(src: string | ImageMeta, modifiers?: ImageModifiers, options?: ImageCallOptions): { url: string }
|
|
31
|
+
getSizes(src: string | ImageMeta, options: GetSizesOptions): ImageSizes
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface ImageEnvironment {
|
|
35
|
+
options: ImageRuntimeOptions
|
|
36
|
+
sources: ImageSources
|
|
37
|
+
providers: Record<string, ImageProvider>
|
|
38
|
+
/** Default: `console.warn` under `hamur dev`. */
|
|
39
|
+
warn?: (message: string) => void
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface Resolved {
|
|
43
|
+
name: string
|
|
44
|
+
provider: ImageProvider
|
|
45
|
+
providerOptions: Record<string, unknown>
|
|
46
|
+
source: string
|
|
47
|
+
width?: number
|
|
48
|
+
height?: number
|
|
49
|
+
/** An animated source, a remote host `image.domains` does not allow, or a blob source where none is read: the URL is the source's own. */
|
|
50
|
+
untransformed: boolean
|
|
51
|
+
base: ImageModifiers
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const PLAIN_KEYS = ["width", "height", "fit", "format", "quality"]
|
|
55
|
+
|
|
56
|
+
const list = (names: string[]): string => (names.length ? names.join(", ") : "none")
|
|
57
|
+
|
|
58
|
+
/** `getImage` and `getSizes` as pure functions of the options and the sources. */
|
|
59
|
+
export function createImage(env: ImageEnvironment): ImageApi {
|
|
60
|
+
const { options, sources, providers } = env
|
|
61
|
+
const matchers = options.domains.map(compileDomain)
|
|
62
|
+
const aliasKeys = Object.keys(options.alias).sort((a, b) => b.length - a.length)
|
|
63
|
+
const warned = new Set<string>()
|
|
64
|
+
const warn =
|
|
65
|
+
env.warn ??
|
|
66
|
+
((message: string) => {
|
|
67
|
+
if ((import.meta as { dev?: boolean }).dev) console.warn(message)
|
|
68
|
+
})
|
|
69
|
+
const defaultProvider = options.provider === "auto" ? "hamur" : options.provider
|
|
70
|
+
|
|
71
|
+
const roundUp = (width: number): number =>
|
|
72
|
+
options.widths.find((allowed) => allowed >= width) ?? options.widths[options.widths.length - 1]!
|
|
73
|
+
const finalWidth = (r: Resolved, width: number): number => Math.min(roundUp(width), r.width ?? Infinity)
|
|
74
|
+
|
|
75
|
+
function expand(src: string): string {
|
|
76
|
+
const key = aliasKeys.find((alias) => src.startsWith(alias))
|
|
77
|
+
if (key === undefined) return src
|
|
78
|
+
const target = options.alias[key]!
|
|
79
|
+
const rest = src.slice(key.length)
|
|
80
|
+
const joined = `${target.endsWith("/") && rest.startsWith("/") ? target.slice(0, -1) : target}${rest}`
|
|
81
|
+
const href = new URL(joined).href
|
|
82
|
+
if (!href.startsWith(new URL(target).href)) throw new Error(`image: ${JSON.stringify(src)} leaves its alias target`)
|
|
83
|
+
return joined
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const blobKept = (name: string, source: string): boolean => {
|
|
87
|
+
const kept = options.blobUntransformed
|
|
88
|
+
return (
|
|
89
|
+
kept !== undefined &&
|
|
90
|
+
kept.providers.includes(name) &&
|
|
91
|
+
!REMOTE.test(source) &&
|
|
92
|
+
`/${localPath(source, options.baseURL)}`.startsWith(kept.prefix)
|
|
93
|
+
)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** A remote `src` that `image.domains` does not allow is its own URL, logged once per host. */
|
|
97
|
+
function unlisted(name: string, source: string): boolean {
|
|
98
|
+
if (name === "none" || matchers.some((matcher) => matchesDomain(matcher, new URL(source)))) return false
|
|
99
|
+
const host = new URL(source).hostname
|
|
100
|
+
if (!warned.has(`host ${host}`)) {
|
|
101
|
+
warned.add(`host ${host}`)
|
|
102
|
+
warn(`[image] remote image host ${host} is not in image.domains; served untransformed`)
|
|
103
|
+
}
|
|
104
|
+
return true
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function resolve(input: string | ImageMeta, modifiers: ImageModifiers, call: ImageCallOptions): Resolved {
|
|
108
|
+
let preset: { provider?: string; modifiers: ImageModifiers } | undefined
|
|
109
|
+
if (call.preset !== undefined) {
|
|
110
|
+
preset = Object.hasOwn(options.presets, call.preset) ? options.presets[call.preset] : undefined
|
|
111
|
+
if (!preset)
|
|
112
|
+
throw new Error(`image: no preset named "${call.preset}" (configured: ${list(Object.keys(options.presets))})`)
|
|
113
|
+
}
|
|
114
|
+
const name = call.provider ?? preset?.provider ?? defaultProvider
|
|
115
|
+
const provider = Object.hasOwn(providers, name) ? providers[name] : undefined
|
|
116
|
+
if (!provider) throw new Error(`image: no provider named "${name}" (configured: ${list(Object.keys(providers))})`)
|
|
117
|
+
const config = options.providers[name]
|
|
118
|
+
if (typeof provider.getImage !== "function")
|
|
119
|
+
throw new Error(
|
|
120
|
+
`image provider "${name}"${config?.file === undefined ? "" : ` (${config.file})`} has no getImage`,
|
|
121
|
+
)
|
|
122
|
+
const base: ImageModifiers = {}
|
|
123
|
+
for (const [key, value] of Object.entries({ ...preset?.modifiers, ...modifiers }))
|
|
124
|
+
if (value !== undefined) base[key] = value
|
|
125
|
+
const resolved: Resolved = {
|
|
126
|
+
name,
|
|
127
|
+
provider,
|
|
128
|
+
providerOptions: config?.options ?? {},
|
|
129
|
+
source: "",
|
|
130
|
+
untransformed: false,
|
|
131
|
+
base,
|
|
132
|
+
}
|
|
133
|
+
if (typeof input !== "string") {
|
|
134
|
+
resolved.source = input.src
|
|
135
|
+
resolved.width = input.width
|
|
136
|
+
resolved.height = input.height
|
|
137
|
+
resolved.untransformed = input.animated === true || (REMOTE.test(input.src) && unlisted(name, input.src))
|
|
138
|
+
} else {
|
|
139
|
+
const source = expand(input)
|
|
140
|
+
resolved.source = source
|
|
141
|
+
if (REMOTE.test(source)) {
|
|
142
|
+
resolved.untransformed = source === input && unlisted(name, source)
|
|
143
|
+
} else if (!blobKept(name, source)) {
|
|
144
|
+
const entry = sources[`/${localPath(source, options.baseURL)}`]
|
|
145
|
+
if (entry) {
|
|
146
|
+
resolved.width = entry[0]
|
|
147
|
+
resolved.height = entry[1]
|
|
148
|
+
resolved.untransformed = entry[4] === true
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (blobKept(name, resolved.source)) resolved.untransformed = true
|
|
153
|
+
return resolved
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function prepare(r: Resolved, modifiers: ImageModifiers): ImageModifiers {
|
|
157
|
+
const m: ImageModifiers = {}
|
|
158
|
+
for (const [key, value] of Object.entries(modifiers)) if (value !== undefined) m[key] = value
|
|
159
|
+
if (m.width !== undefined) {
|
|
160
|
+
const requested = m.width
|
|
161
|
+
m.width = finalWidth(r, requested)
|
|
162
|
+
if (m.height !== undefined) m.height = Math.round((m.height * m.width) / requested)
|
|
163
|
+
}
|
|
164
|
+
if (m.fit === undefined) delete m.height
|
|
165
|
+
else if (!r.provider.fits.includes(m.fit)) {
|
|
166
|
+
if (r.name !== "none" && !warned.has(`${r.name} ${m.fit} ${r.source}`)) {
|
|
167
|
+
warned.add(`${r.name} ${m.fit} ${r.source}`)
|
|
168
|
+
warn(
|
|
169
|
+
`[image] provider "${r.name}" has no fit "${m.fit}"; resized by width only (set CSS object-fit: ${m.fit} on the image)`,
|
|
170
|
+
)
|
|
171
|
+
}
|
|
172
|
+
delete m.fit
|
|
173
|
+
delete m.height
|
|
174
|
+
}
|
|
175
|
+
for (const key of Object.keys(m)) {
|
|
176
|
+
if (PLAIN_KEYS.includes(key)) continue
|
|
177
|
+
const value = m[key]
|
|
178
|
+
if (value === undefined || value === false) delete m[key]
|
|
179
|
+
else if (!r.provider.modifiers.includes(key))
|
|
180
|
+
throw new Error(`image provider "${r.name}" does not support ${key}`)
|
|
181
|
+
}
|
|
182
|
+
const candidates: ImageFormat[] =
|
|
183
|
+
m.format === undefined ? (r.provider.negotiates ? [] : options.format) : [m.format, ...options.format]
|
|
184
|
+
const format = candidates.find((candidate) => r.provider.formats.includes(candidate))
|
|
185
|
+
if (format === undefined) delete m.format
|
|
186
|
+
else m.format = format
|
|
187
|
+
const quality = m.quality ?? options.quality
|
|
188
|
+
if (quality === undefined) delete m.quality
|
|
189
|
+
else m.quality = quality
|
|
190
|
+
return m
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const urlFor = (r: Resolved, modifiers: ImageModifiers): string =>
|
|
194
|
+
r.untransformed
|
|
195
|
+
? r.source
|
|
196
|
+
: r.provider.getImage(r.source, {
|
|
197
|
+
modifiers: prepare(r, modifiers),
|
|
198
|
+
options: r.providerOptions,
|
|
199
|
+
baseURL: options.baseURL,
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
function getImage(
|
|
203
|
+
src: string | ImageMeta,
|
|
204
|
+
modifiers: ImageModifiers = {},
|
|
205
|
+
call: ImageCallOptions = {},
|
|
206
|
+
): { url: string } {
|
|
207
|
+
const r = resolve(src, modifiers, call)
|
|
208
|
+
return { url: urlFor(r, r.base) }
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function getSizes(src: string | ImageMeta, o: GetSizesOptions): ImageSizes {
|
|
212
|
+
const r = resolve(src, o.modifiers ?? {}, o)
|
|
213
|
+
const baseWidth = o.width ?? r.base.width
|
|
214
|
+
const baseHeight = o.height ?? r.base.height
|
|
215
|
+
const ratio = baseWidth !== undefined && baseHeight !== undefined ? baseHeight / baseWidth : undefined
|
|
216
|
+
const make = (width: number): string =>
|
|
217
|
+
urlFor(r, { ...r.base, width, ...(ratio === undefined ? {} : { height: Math.round(width * ratio) }) })
|
|
218
|
+
|
|
219
|
+
let width = o.width
|
|
220
|
+
let height = o.height
|
|
221
|
+
if (r.width !== undefined && r.height !== undefined) {
|
|
222
|
+
if (width === undefined && height === undefined) {
|
|
223
|
+
width = r.width
|
|
224
|
+
height = r.height
|
|
225
|
+
} else if (height === undefined) height = Math.round((width! * r.height) / r.width)
|
|
226
|
+
else if (width === undefined) width = Math.round((height * r.width) / r.height)
|
|
227
|
+
}
|
|
228
|
+
const dims: ImageSizes = {
|
|
229
|
+
src: "",
|
|
230
|
+
...(width === undefined ? {} : { width }),
|
|
231
|
+
...(height === undefined ? {} : { height }),
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const densities = [...new Set(o.densities ?? options.densities)].sort((a, b) => a - b)
|
|
235
|
+
if (o.sizes !== undefined) {
|
|
236
|
+
const plan = planSizes(o.sizes, options.screens)
|
|
237
|
+
const finals = [
|
|
238
|
+
...new Set(plan.slots.flatMap((slot) => densities.map((density) => finalWidth(r, slot * density)))),
|
|
239
|
+
].sort((a, b) => a - b)
|
|
240
|
+
const urls = finals.map(make)
|
|
241
|
+
if (urls[0] === r.source) return { ...dims, src: r.source }
|
|
242
|
+
return {
|
|
243
|
+
...dims,
|
|
244
|
+
src: urls[urls.length - 1]!,
|
|
245
|
+
srcset: finals.map((w, i) => `${urls[i]} ${w}w`).join(", "),
|
|
246
|
+
sizes: plan.attribute,
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
if (o.width !== undefined) {
|
|
250
|
+
const src = make(finalWidth(r, o.width))
|
|
251
|
+
if (src === r.source) return { ...dims, src }
|
|
252
|
+
const seen = new Set<number>()
|
|
253
|
+
const candidates: string[] = []
|
|
254
|
+
for (const density of densities) {
|
|
255
|
+
const final = finalWidth(r, o.width * density)
|
|
256
|
+
if (seen.has(final)) continue
|
|
257
|
+
seen.add(final)
|
|
258
|
+
candidates.push(`${make(final)} ${density}x`)
|
|
259
|
+
}
|
|
260
|
+
return { ...dims, src, srcset: candidates.join(", ") }
|
|
261
|
+
}
|
|
262
|
+
return { ...dims, src: urlFor(r, r.base) }
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return { getImage, getSizes }
|
|
266
|
+
}
|