@hamur/image 0.0.0-stage → 0.0.2
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 +43 -0
- package/dist/types/src/adapters/angular-picture.d.ts +39 -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 +141 -4
- package/src/adapters/Img.svelte +33 -0
- package/src/adapters/Picture.svelte +39 -0
- package/src/adapters/angular-img.ts +143 -0
- package/src/adapters/angular-picture.ts +172 -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
package/src/options.ts
ADDED
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import { resolve } from "node:path"
|
|
2
|
+
import { checkDomain } from "./domains.ts"
|
|
3
|
+
import type {
|
|
4
|
+
ImageFit,
|
|
5
|
+
ImageFormat,
|
|
6
|
+
ImageModifiers,
|
|
7
|
+
ImagePreset,
|
|
8
|
+
ResolvedImageOptions,
|
|
9
|
+
ResolvedProvider,
|
|
10
|
+
} from "./types.ts"
|
|
11
|
+
import { DiagnosticError } from "hamur/kit"
|
|
12
|
+
|
|
13
|
+
export const BUILT_IN_PROVIDERS: readonly string[] = [
|
|
14
|
+
"hamur",
|
|
15
|
+
"none",
|
|
16
|
+
"vercel",
|
|
17
|
+
"netlify",
|
|
18
|
+
"cloudflare",
|
|
19
|
+
"cloudinary",
|
|
20
|
+
"imgix",
|
|
21
|
+
"imagekit",
|
|
22
|
+
"cloudflareImages",
|
|
23
|
+
"bunny",
|
|
24
|
+
"fastly",
|
|
25
|
+
"sanity",
|
|
26
|
+
"storyblok",
|
|
27
|
+
"contentful",
|
|
28
|
+
"strapi",
|
|
29
|
+
"supabase",
|
|
30
|
+
"uploadcare",
|
|
31
|
+
"twicpics",
|
|
32
|
+
"imgproxy",
|
|
33
|
+
"weserv",
|
|
34
|
+
"ipx",
|
|
35
|
+
]
|
|
36
|
+
export const DEFAULT_SCREENS: Record<string, number> = { sm: 640, md: 768, lg: 1024, xl: 1280, "2xl": 1536 }
|
|
37
|
+
export const DEFAULT_DENSITIES: number[] = [1, 2]
|
|
38
|
+
export const SMALL_WIDTHS: number[] = [64, 128, 256, 384]
|
|
39
|
+
export const DEFAULT_MAX_PIXELS = 50_000_000
|
|
40
|
+
export const DEFAULT_MAX_BYTES = 26_214_400
|
|
41
|
+
|
|
42
|
+
const FORMATS: readonly ImageFormat[] = ["avif", "webp", "jpeg", "png"]
|
|
43
|
+
const FITS: readonly ImageFit[] = ["cover", "contain", "fill", "inside", "outside"]
|
|
44
|
+
const TOP_KEYS = [
|
|
45
|
+
"provider",
|
|
46
|
+
"providers",
|
|
47
|
+
"presets",
|
|
48
|
+
"screens",
|
|
49
|
+
"densities",
|
|
50
|
+
"widths",
|
|
51
|
+
"format",
|
|
52
|
+
"quality",
|
|
53
|
+
"domains",
|
|
54
|
+
"alias",
|
|
55
|
+
"dir",
|
|
56
|
+
"maxPixels",
|
|
57
|
+
"maxBytes",
|
|
58
|
+
"prerender",
|
|
59
|
+
"sources",
|
|
60
|
+
]
|
|
61
|
+
const SECRET_OPTIONS = ["key", "secret", "salt"]
|
|
62
|
+
const MAX_WIDTH = 16384
|
|
63
|
+
const SCREEN_NAME = /^[a-z0-9]+$/i
|
|
64
|
+
|
|
65
|
+
export interface ResolveEnvironment {
|
|
66
|
+
rootDir: string
|
|
67
|
+
/** The app's `public/` and each layer's, highest priority first. */
|
|
68
|
+
publicDirs: string[]
|
|
69
|
+
exists(path: string): boolean
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function fail(message: string): never {
|
|
73
|
+
throw new DiagnosticError("HAMUR_E6001", message)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function shown(value: unknown): string {
|
|
77
|
+
if (Array.isArray(value)) return "an array"
|
|
78
|
+
if (typeof value === "function") return "a function"
|
|
79
|
+
if (value === undefined) return "undefined"
|
|
80
|
+
return JSON.stringify(value)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const isObject = (value: unknown): value is Record<string, unknown> =>
|
|
84
|
+
typeof value === "object" && value !== null && !Array.isArray(value)
|
|
85
|
+
const isInteger = (value: unknown, max = Number.MAX_SAFE_INTEGER): value is number =>
|
|
86
|
+
typeof value === "number" && Number.isInteger(value) && value >= 1 && value <= max
|
|
87
|
+
|
|
88
|
+
function onlyKeys(at: string, value: Record<string, unknown>, keys: string[]): void {
|
|
89
|
+
for (const key of Object.keys(value))
|
|
90
|
+
if (!keys.includes(key)) fail(`${at} has no ${JSON.stringify(key)} (it takes: ${keys.join(", ")})`)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function positive(at: string, value: unknown, fallback: number): number {
|
|
94
|
+
if (value === undefined) return fallback
|
|
95
|
+
if (!isInteger(value)) fail(`${at} must be a positive integer (got ${shown(value)})`)
|
|
96
|
+
return value
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function checkModifiers(at: string, value: unknown): ImageModifiers {
|
|
100
|
+
if (!isObject(value)) return fail(`${at} must be an object (got ${shown(value)})`)
|
|
101
|
+
for (const key of ["width", "height"]) {
|
|
102
|
+
if (value[key] !== undefined && !isInteger(value[key], MAX_WIDTH))
|
|
103
|
+
fail(`${at}.${key} must be a positive integer up to ${MAX_WIDTH} (got ${shown(value[key])})`)
|
|
104
|
+
}
|
|
105
|
+
if (value.quality !== undefined && !isInteger(value.quality, 100))
|
|
106
|
+
fail(`${at}.quality must be an integer from 1 to 100 (got ${shown(value.quality)})`)
|
|
107
|
+
if (value.fit !== undefined && !FITS.includes(value.fit as ImageFit))
|
|
108
|
+
fail(`${at}.fit must be one of ${FITS.join(", ")} (got ${shown(value.fit)})`)
|
|
109
|
+
if (value.format !== undefined && !FORMATS.includes(value.format as ImageFormat))
|
|
110
|
+
fail(`${at}.format must be one of ${FORMATS.join(", ")} (got ${shown(value.format)})`)
|
|
111
|
+
if (value.rotate !== undefined && ![90, 180, 270].includes(value.rotate as number))
|
|
112
|
+
fail(`${at}.rotate must be 90, 180 or 270 (got ${shown(value.rotate)})`)
|
|
113
|
+
for (const key of ["flip", "flop"])
|
|
114
|
+
if (value[key] !== undefined && typeof value[key] !== "boolean")
|
|
115
|
+
fail(`${at}.${key} must be true or false (got ${shown(value[key])})`)
|
|
116
|
+
return value as ImageModifiers
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function resolveScreens(input: unknown): Record<string, number> {
|
|
120
|
+
if (input === undefined) return { ...DEFAULT_SCREENS }
|
|
121
|
+
if (!isObject(input)) return fail(`image.screens must be an object (got ${shown(input)})`)
|
|
122
|
+
const screens: Record<string, number> = { ...DEFAULT_SCREENS }
|
|
123
|
+
for (const [name, value] of Object.entries(input)) {
|
|
124
|
+
if (!SCREEN_NAME.test(name)) fail(`image.screens key ${JSON.stringify(name)} must match /^[a-z0-9]+$/i`)
|
|
125
|
+
if (!isInteger(value)) fail(`image.screens.${name} must be a positive integer (got ${shown(value)})`)
|
|
126
|
+
screens[name] = value
|
|
127
|
+
}
|
|
128
|
+
return screens
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function resolveDensities(input: unknown): number[] {
|
|
132
|
+
if (input === undefined) return [...DEFAULT_DENSITIES]
|
|
133
|
+
if (!Array.isArray(input))
|
|
134
|
+
return fail(`image.densities must be an array of numbers from 1 to 4 (got ${shown(input)})`)
|
|
135
|
+
if (!input.length) fail("image.densities must list at least one density")
|
|
136
|
+
for (const density of input)
|
|
137
|
+
if (typeof density !== "number" || !(density >= 1 && density <= 4))
|
|
138
|
+
fail(`image.densities must be numbers from 1 to 4 (got ${shown(density)})`)
|
|
139
|
+
return [...new Set(input as number[])].sort((a, b) => a - b)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function resolveWidths(input: unknown, screens: Record<string, number>, densities: number[]): number[] {
|
|
143
|
+
if (input === undefined) {
|
|
144
|
+
const widths = new Set<number>(SMALL_WIDTHS)
|
|
145
|
+
for (const screen of Object.values(screens))
|
|
146
|
+
for (const density of densities) widths.add(Math.round(screen * density))
|
|
147
|
+
return [...widths].sort((a, b) => a - b)
|
|
148
|
+
}
|
|
149
|
+
if (!Array.isArray(input) || !input.length)
|
|
150
|
+
return fail(`image.widths must be positive integers up to ${MAX_WIDTH} (got ${shown(input)})`)
|
|
151
|
+
for (const width of input)
|
|
152
|
+
if (!isInteger(width, MAX_WIDTH))
|
|
153
|
+
fail(`image.widths must be positive integers up to ${MAX_WIDTH} (got ${shown(width)})`)
|
|
154
|
+
return [...new Set(input as number[])].sort((a, b) => a - b)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function resolveFormat(input: unknown): ImageFormat[] {
|
|
158
|
+
if (input === undefined) return ["webp"]
|
|
159
|
+
if (!Array.isArray(input)) return fail(`image.format must be an array of output formats (got ${shown(input)})`)
|
|
160
|
+
if (!input.length) fail("image.format must list at least one format")
|
|
161
|
+
input.forEach((format, i) => {
|
|
162
|
+
if (!FORMATS.includes(format))
|
|
163
|
+
fail(`image.format[${i}] ${shown(format)} is not an output format (${FORMATS.join(", ")})`)
|
|
164
|
+
})
|
|
165
|
+
return [...input] as ImageFormat[]
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function resolveDomains(input: unknown): string[] {
|
|
169
|
+
if (input === undefined) return []
|
|
170
|
+
if (!Array.isArray(input)) return fail(`image.domains must be an array of host or URL patterns (got ${shown(input)})`)
|
|
171
|
+
input.forEach((pattern, i) => {
|
|
172
|
+
const kind = typeof pattern === "string" ? checkDomain(pattern) : "invalid"
|
|
173
|
+
if (kind === "invalid") fail(`image.domains[${i}] ${shown(pattern)} is not a host or URL pattern`)
|
|
174
|
+
if (kind === "every") fail(`image.domains[${i}] ${shown(pattern)} allows every host; name the hosts`)
|
|
175
|
+
})
|
|
176
|
+
return [...input] as string[]
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function resolveAlias(input: unknown): Record<string, string> {
|
|
180
|
+
if (input === undefined) return {}
|
|
181
|
+
if (!isObject(input)) return fail(`image.alias must be an object (got ${shown(input)})`)
|
|
182
|
+
for (const [key, target] of Object.entries(input)) {
|
|
183
|
+
if (!key.startsWith("/")) fail(`image.alias key ${JSON.stringify(key)} must start with "/"`)
|
|
184
|
+
let url: URL | undefined
|
|
185
|
+
try {
|
|
186
|
+
url = typeof target === "string" ? new URL(target) : undefined
|
|
187
|
+
} catch {}
|
|
188
|
+
if (url === undefined || (url.protocol !== "http:" && url.protocol !== "https:"))
|
|
189
|
+
fail(`image.alias[${JSON.stringify(key)}] ${shown(target)} must be an http or https URL`)
|
|
190
|
+
if (url.search !== "" || url.hash !== "" || url.username !== "" || url.password !== "") {
|
|
191
|
+
fail(
|
|
192
|
+
`image.alias[${JSON.stringify(key)}] ${shown(target)} must not hold a query string, a fragment or credentials`,
|
|
193
|
+
)
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return { ...input } as Record<string, string>
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function resolveProviders(input: unknown, env: ResolveEnvironment): Record<string, ResolvedProvider> {
|
|
200
|
+
if (input === undefined) return {}
|
|
201
|
+
if (!isObject(input)) return fail(`image.providers must be an object (got ${shown(input)})`)
|
|
202
|
+
const providers: Record<string, ResolvedProvider> = {}
|
|
203
|
+
for (const [key, entry] of Object.entries(input)) {
|
|
204
|
+
const at = `image.providers.${key}`
|
|
205
|
+
if (!isObject(entry)) return fail(`${at} must be an object (got ${shown(entry)})`)
|
|
206
|
+
onlyKeys(at, entry, ["provider", "options"])
|
|
207
|
+
const options = entry.options ?? {}
|
|
208
|
+
if (!isObject(options)) return fail(`${at}.options must be an object (got ${shown(options)})`)
|
|
209
|
+
for (const name of SECRET_OPTIONS) {
|
|
210
|
+
if (Object.hasOwn(options, name))
|
|
211
|
+
fail(`${at}.options.${name} would be sent to every browser; signed URLs are not supported`)
|
|
212
|
+
}
|
|
213
|
+
const named = entry.provider
|
|
214
|
+
if (named !== undefined && typeof named !== "string") fail(`${at}.provider must be a string (got ${shown(named)})`)
|
|
215
|
+
const provider = (named ?? key) as string
|
|
216
|
+
if (named !== undefined && /^\.{0,2}\/|\.[cm]?[jt]s$/.test(named)) {
|
|
217
|
+
if (!env.exists(resolve(env.rootDir, named))) fail(`${at}.provider ${shown(named)} does not exist`)
|
|
218
|
+
providers[key] = { file: named, options }
|
|
219
|
+
} else if (BUILT_IN_PROVIDERS.includes(provider)) {
|
|
220
|
+
providers[key] = { builtIn: provider, options }
|
|
221
|
+
} else {
|
|
222
|
+
fail(
|
|
223
|
+
named === undefined
|
|
224
|
+
? `${at}.provider is required: ${JSON.stringify(key)} is not a built-in provider`
|
|
225
|
+
: `${at}.provider ${shown(named)} is not a built-in provider or a file path`,
|
|
226
|
+
)
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return providers
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function resolvePresets(input: unknown, providers: Record<string, ResolvedProvider>): Record<string, ImagePreset> {
|
|
233
|
+
if (input === undefined) return {}
|
|
234
|
+
if (!isObject(input)) return fail(`image.presets must be an object (got ${shown(input)})`)
|
|
235
|
+
const presets: Record<string, ImagePreset> = {}
|
|
236
|
+
for (const [name, entry] of Object.entries(input)) {
|
|
237
|
+
const at = `image.presets.${name}`
|
|
238
|
+
if (!isObject(entry)) return fail(`${at} must be an object (got ${shown(entry)})`)
|
|
239
|
+
onlyKeys(at, entry, ["provider", "modifiers"])
|
|
240
|
+
const provider = entry.provider
|
|
241
|
+
if (
|
|
242
|
+
provider !== undefined &&
|
|
243
|
+
!(typeof provider === "string" && (BUILT_IN_PROVIDERS.includes(provider) || Object.hasOwn(providers, provider)))
|
|
244
|
+
) {
|
|
245
|
+
fail(`${at}.provider ${shown(provider)} is not "hamur", "none", a built-in provider or a key of image.providers`)
|
|
246
|
+
}
|
|
247
|
+
const modifiers = checkModifiers(`${at}.modifiers`, entry.modifiers ?? {})
|
|
248
|
+
presets[name] = { ...(provider === undefined ? {} : { provider: provider as string }), modifiers }
|
|
249
|
+
}
|
|
250
|
+
return presets
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function resolveSources(input: unknown): ResolvedImageOptions["sources"] {
|
|
254
|
+
if (input === undefined) return {}
|
|
255
|
+
if (!isObject(input)) return fail(`image.sources must be an object (got ${shown(input)})`)
|
|
256
|
+
onlyKeys("image.sources", input, ["blob"])
|
|
257
|
+
if (input.blob === undefined) return {}
|
|
258
|
+
const blob = input.blob
|
|
259
|
+
if (!isObject(blob)) return fail(`image.sources.blob must be an object (got ${shown(blob)})`)
|
|
260
|
+
onlyKeys("image.sources.blob", blob, ["prefix"])
|
|
261
|
+
const prefix = blob.prefix
|
|
262
|
+
if (!(typeof prefix === "string" && prefix.startsWith("/") && prefix.endsWith("/"))) {
|
|
263
|
+
fail(`image.sources.blob.prefix ${shown(prefix)} must start and end with "/"`)
|
|
264
|
+
}
|
|
265
|
+
if (prefix === "/")
|
|
266
|
+
fail('image.sources.blob.prefix "/" would make every source a blob; name a directory such as "/blob/"')
|
|
267
|
+
if (prefix.startsWith("/_hamur/"))
|
|
268
|
+
fail(`image.sources.blob.prefix ${shown(prefix)} is below /_hamur/, where Hamur serves its own files`)
|
|
269
|
+
return { blob: { prefix } }
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** The `image` key checked, with the defaults; every load error names the key. */
|
|
273
|
+
export function resolveOptions(input: unknown, env: ResolveEnvironment): ResolvedImageOptions {
|
|
274
|
+
if (input !== undefined && !isObject(input)) fail(`image must be an object (got ${shown(input)})`)
|
|
275
|
+
const image = input ?? {}
|
|
276
|
+
onlyKeys("image", image, TOP_KEYS)
|
|
277
|
+
const providers = resolveProviders(image.providers, env)
|
|
278
|
+
const provider = image.provider ?? "auto"
|
|
279
|
+
if (!(
|
|
280
|
+
typeof provider === "string" &&
|
|
281
|
+
(provider === "auto" || BUILT_IN_PROVIDERS.includes(provider) || Object.hasOwn(providers, provider))
|
|
282
|
+
)) {
|
|
283
|
+
fail(
|
|
284
|
+
`image.provider ${shown(provider)} is not "auto", "hamur", "none", a built-in provider or a key of image.providers`,
|
|
285
|
+
)
|
|
286
|
+
}
|
|
287
|
+
const presets = resolvePresets(image.presets, providers)
|
|
288
|
+
const screens = resolveScreens(image.screens)
|
|
289
|
+
const densities = resolveDensities(image.densities)
|
|
290
|
+
const widths = resolveWidths(image.widths, screens, densities)
|
|
291
|
+
const format = resolveFormat(image.format)
|
|
292
|
+
if (image.quality !== undefined && !isInteger(image.quality, 100))
|
|
293
|
+
fail(`image.quality must be an integer from 1 to 100 (got ${shown(image.quality)})`)
|
|
294
|
+
const domains = resolveDomains(image.domains)
|
|
295
|
+
const alias = resolveAlias(image.alias)
|
|
296
|
+
let dirs = env.publicDirs
|
|
297
|
+
const dir = image.dir
|
|
298
|
+
if (dir !== undefined) {
|
|
299
|
+
if (typeof dir !== "string" || !env.exists(resolve(env.rootDir, dir)))
|
|
300
|
+
fail(`image.dir ${shown(dir)} does not exist`)
|
|
301
|
+
dirs = [resolve(env.rootDir, dir)]
|
|
302
|
+
}
|
|
303
|
+
const maxPixels = positive("image.maxPixels", image.maxPixels, DEFAULT_MAX_PIXELS)
|
|
304
|
+
const maxBytes = positive("image.maxBytes", image.maxBytes, DEFAULT_MAX_BYTES)
|
|
305
|
+
const prerender = image.prerender ?? []
|
|
306
|
+
if (!Array.isArray(prerender)) fail(`image.prerender must be an array of paths (got ${shown(prerender)})`)
|
|
307
|
+
;(prerender as unknown[]).forEach((path, i) => {
|
|
308
|
+
if (!(typeof path === "string" && path.startsWith("/")))
|
|
309
|
+
fail(`image.prerender[${i}] ${shown(path)} must start with "/"`)
|
|
310
|
+
})
|
|
311
|
+
const sources = resolveSources(image.sources)
|
|
312
|
+
return {
|
|
313
|
+
provider,
|
|
314
|
+
providers,
|
|
315
|
+
presets,
|
|
316
|
+
screens,
|
|
317
|
+
densities,
|
|
318
|
+
widths,
|
|
319
|
+
format,
|
|
320
|
+
...(image.quality === undefined ? {} : { quality: image.quality as number }),
|
|
321
|
+
domains,
|
|
322
|
+
alias,
|
|
323
|
+
dirs,
|
|
324
|
+
maxPixels,
|
|
325
|
+
maxBytes,
|
|
326
|
+
prerender: [...(prerender as string[])],
|
|
327
|
+
sources,
|
|
328
|
+
}
|
|
329
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { localPath, REMOTE } from "../runtime/source.ts"
|
|
2
|
+
import type { ImageProvider } from "../types.ts"
|
|
3
|
+
import { option, withQuery } from "./shared.ts"
|
|
4
|
+
|
|
5
|
+
/** Bunny Optimizer on a pull zone; `options.origin` is the zone's URL, `https://example.b-cdn.net` for example. It converts to `webp` and `png` by `format` and serves WebP to browsers that accept it; the docs give no behaviour for both `width` and `height`, so no fit is made; a remote `src` is returned as it is. */
|
|
6
|
+
export default {
|
|
7
|
+
formats: ["webp", "png"],
|
|
8
|
+
negotiates: true,
|
|
9
|
+
fits: [],
|
|
10
|
+
modifiers: [],
|
|
11
|
+
getImage(src, { modifiers: m, options, baseURL }) {
|
|
12
|
+
if (REMOTE.test(src)) return src
|
|
13
|
+
const origin = option("bunny", options, "origin")
|
|
14
|
+
const query: string[] = []
|
|
15
|
+
if (m.width !== undefined) query.push(`width=${m.width}`)
|
|
16
|
+
if (m.height !== undefined) query.push(`height=${m.height}`)
|
|
17
|
+
if (m.format !== undefined) query.push(`format=${m.format}`)
|
|
18
|
+
if (m.quality !== undefined) query.push(`quality=${m.quality}`)
|
|
19
|
+
return withQuery(`${origin}/${localPath(src, baseURL)}`, query)
|
|
20
|
+
},
|
|
21
|
+
} satisfies ImageProvider
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { localPath, REMOTE } from "../runtime/source.ts"
|
|
2
|
+
import type { ImageFit, ImageProvider } from "../types.ts"
|
|
3
|
+
|
|
4
|
+
const FITS: Partial<Record<ImageFit, string>> = { cover: "cover", inside: "contain", contain: "pad" }
|
|
5
|
+
|
|
6
|
+
/** Cloudflare Image Transformations, which the zone must have turned on; `fit=contain` there is `inside` and `fit=pad` is `contain`. */
|
|
7
|
+
export default {
|
|
8
|
+
formats: ["avif", "webp", "jpeg", "png"],
|
|
9
|
+
negotiates: true,
|
|
10
|
+
fits: ["cover", "inside", "contain"],
|
|
11
|
+
modifiers: [],
|
|
12
|
+
getImage(src, { modifiers: m, baseURL }) {
|
|
13
|
+
const options: string[] = []
|
|
14
|
+
if (m.width !== undefined) options.push(`width=${m.width}`)
|
|
15
|
+
if (m.height !== undefined) options.push(`height=${m.height}`)
|
|
16
|
+
if (m.fit !== undefined) options.push(`fit=${FITS[m.fit]}`)
|
|
17
|
+
if (m.quality !== undefined) options.push(`quality=${m.quality}`)
|
|
18
|
+
options.push(`format=${m.format ?? "auto"}`)
|
|
19
|
+
const source = REMOTE.test(src) ? src : `${baseURL.slice(1)}${localPath(src, baseURL)}`
|
|
20
|
+
return `/cdn-cgi/image/${options.join(",")}/${source}`
|
|
21
|
+
},
|
|
22
|
+
} satisfies ImageProvider
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { localPath, REMOTE } from "../runtime/source.ts"
|
|
2
|
+
import type { ImageFit, ImageProvider } from "../types.ts"
|
|
3
|
+
import { option } from "./shared.ts"
|
|
4
|
+
|
|
5
|
+
const FITS: Partial<Record<ImageFit, string>> = { cover: "cover", inside: "contain", contain: "pad" }
|
|
6
|
+
|
|
7
|
+
/** Cloudflare Images delivery with flexible variants, which the account must have turned on; `options.accountHash` is required. `src` is the image id; a remote `src` is returned as it is. `fit=contain` there is `inside` and `fit=pad` is `contain`. */
|
|
8
|
+
export default {
|
|
9
|
+
formats: ["avif", "webp", "jpeg", "png"],
|
|
10
|
+
negotiates: true,
|
|
11
|
+
fits: ["cover", "inside", "contain"],
|
|
12
|
+
modifiers: [],
|
|
13
|
+
getImage(src, { modifiers: m, options, baseURL }) {
|
|
14
|
+
if (REMOTE.test(src)) return src
|
|
15
|
+
const hash = option("cloudflareImages", options, "accountHash")
|
|
16
|
+
const origin = option("cloudflareImages", options, "origin", "https://imagedelivery.net")
|
|
17
|
+
const variant: string[] = []
|
|
18
|
+
if (m.width !== undefined) variant.push(`width=${m.width}`)
|
|
19
|
+
if (m.height !== undefined) variant.push(`height=${m.height}`)
|
|
20
|
+
if (m.fit !== undefined) variant.push(`fit=${FITS[m.fit]}`)
|
|
21
|
+
if (m.quality !== undefined) variant.push(`quality=${m.quality}`)
|
|
22
|
+
variant.push(`format=${m.format ?? "auto"}`)
|
|
23
|
+
return `${origin}/${hash}/${localPath(src, baseURL)}/${variant.join(",")}`
|
|
24
|
+
},
|
|
25
|
+
} satisfies ImageProvider
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { localPath, REMOTE } from "../runtime/source.ts"
|
|
2
|
+
import type { ImageFit, ImageProvider } from "../types.ts"
|
|
3
|
+
import { JPG, option } from "./shared.ts"
|
|
4
|
+
|
|
5
|
+
const FITS: Partial<Record<ImageFit, string>> = { cover: "c_fill", inside: "c_fit", contain: "c_pad", fill: "c_scale" }
|
|
6
|
+
|
|
7
|
+
/** Cloudinary; `options.cloudName` is required. A local `src` is a public id of the `upload` type, a remote one is delivered through `fetch`. */
|
|
8
|
+
export default {
|
|
9
|
+
formats: ["avif", "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 cloud = option("cloudinary", options, "cloudName")
|
|
15
|
+
const origin = option("cloudinary", options, "origin", "https://res.cloudinary.com")
|
|
16
|
+
const steps = [`f_${m.format === undefined ? "auto" : JPG[m.format]}`]
|
|
17
|
+
if (m.fit !== undefined) steps.push(FITS[m.fit]!)
|
|
18
|
+
if (m.width !== undefined) steps.push(`w_${m.width}`)
|
|
19
|
+
if (m.height !== undefined) steps.push(`h_${m.height}`)
|
|
20
|
+
if (m.quality !== undefined) steps.push(`q_${m.quality}`)
|
|
21
|
+
const angle = [
|
|
22
|
+
m.flip === true ? "vflip" : "",
|
|
23
|
+
m.flop === true ? "hflip" : "",
|
|
24
|
+
m.rotate === undefined ? "" : String(m.rotate),
|
|
25
|
+
].filter(Boolean)
|
|
26
|
+
if (angle.length) steps.push(`a_${angle.join(".")}`)
|
|
27
|
+
const transformation = steps.join(",")
|
|
28
|
+
if (!REMOTE.test(src)) return `${origin}/${cloud}/image/upload/${transformation}/${localPath(src, baseURL)}`
|
|
29
|
+
return `${origin}/${cloud}/image/fetch/${transformation}/${src.includes("?") ? encodeURIComponent(src) : src}`
|
|
30
|
+
},
|
|
31
|
+
} satisfies ImageProvider
|
|
@@ -0,0 +1,26 @@
|
|
|
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: "fill", contain: "pad", fill: "scale" }
|
|
6
|
+
|
|
7
|
+
/** The Contentful Images API; a local `src` is the asset's path below `options.origin` (default `https://images.ctfassets.net`). `fit=fill` there is `cover`, `fit=pad` is `contain` and `fit=scale` is `fill`. */
|
|
8
|
+
export default {
|
|
9
|
+
formats: ["avif", "webp", "jpeg", "png"],
|
|
10
|
+
negotiates: false,
|
|
11
|
+
fits: ["cover", "contain", "fill"],
|
|
12
|
+
modifiers: [],
|
|
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
|
+
if (m.format !== undefined) query.push(`fm=${JPG[m.format]}`)
|
|
19
|
+
if (m.quality !== undefined) query.push(`q=${m.quality}`)
|
|
20
|
+
if (REMOTE.test(src)) return withQuery(src, query)
|
|
21
|
+
return withQuery(
|
|
22
|
+
`${option("contentful", options, "origin", "https://images.ctfassets.net")}/${localPath(src, baseURL)}`,
|
|
23
|
+
query,
|
|
24
|
+
)
|
|
25
|
+
},
|
|
26
|
+
} 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: "bounds" }
|
|
6
|
+
const ORIENT = { 90: "r", 180: "hv", 270: "l" } as const
|
|
7
|
+
|
|
8
|
+
/** Fastly Image Optimizer; `options.origin` is the service's domain and is unset when it serves the app's own domain. A remote `src` gets the query as it is. `fit=bounds` there is `inside` and `fit=crop` is `cover`; `fit` takes effect only with both sizes. */
|
|
9
|
+
export default {
|
|
10
|
+
formats: ["avif", "webp", "jpeg", "png"],
|
|
11
|
+
negotiates: true,
|
|
12
|
+
fits: ["cover", "inside"],
|
|
13
|
+
modifiers: ["rotate"],
|
|
14
|
+
getImage(src, { modifiers: m, options, baseURL }) {
|
|
15
|
+
const query: string[] = []
|
|
16
|
+
if (m.width !== undefined) query.push(`width=${m.width}`)
|
|
17
|
+
if (m.height !== undefined) query.push(`height=${m.height}`)
|
|
18
|
+
if (m.fit !== undefined) query.push(`fit=${FITS[m.fit]}`)
|
|
19
|
+
query.push(m.format === undefined ? "auto=webp" : `format=${JPG[m.format]}`)
|
|
20
|
+
if (m.quality !== undefined) query.push(`quality=${m.quality}`)
|
|
21
|
+
if (m.rotate !== undefined) query.push(`orient=${ORIENT[m.rotate]}`)
|
|
22
|
+
if (REMOTE.test(src)) return withQuery(src, query)
|
|
23
|
+
const origin = options.origin === undefined ? "" : option("fastly", options, "origin")
|
|
24
|
+
return withQuery(
|
|
25
|
+
origin === "" ? `${baseURL}${localPath(src, baseURL)}` : `${origin}/${localPath(src, baseURL)}`,
|
|
26
|
+
query,
|
|
27
|
+
)
|
|
28
|
+
},
|
|
29
|
+
} satisfies ImageProvider
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { localPath, REMOTE } from "../runtime/source.ts"
|
|
2
|
+
import type { ImageFormat, ImageProvider, ImageSources } from "../types.ts"
|
|
3
|
+
|
|
4
|
+
const SOURCE_FORMATS = new Map<string, ImageFormat>([
|
|
5
|
+
["jpg", "jpeg"],
|
|
6
|
+
["jpeg", "jpeg"],
|
|
7
|
+
["png", "png"],
|
|
8
|
+
["webp", "webp"],
|
|
9
|
+
])
|
|
10
|
+
const EXTENSIONS = new Map<ImageFormat, string>([
|
|
11
|
+
["jpeg", "jpg"],
|
|
12
|
+
["png", "png"],
|
|
13
|
+
["webp", "webp"],
|
|
14
|
+
])
|
|
15
|
+
const ROTATIONS = [90, 180, 270]
|
|
16
|
+
const MAX_SIZE = 16384
|
|
17
|
+
|
|
18
|
+
function extensionOf(path: string): string {
|
|
19
|
+
const name = path.slice(path.lastIndexOf("/") + 1)
|
|
20
|
+
const dot = name.lastIndexOf(".")
|
|
21
|
+
return dot < 0 ? "" : name.slice(dot + 1).toLowerCase()
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function integer(key: string, value: number, max: number): string {
|
|
25
|
+
if (!Number.isInteger(value) || value < 1 || value > max)
|
|
26
|
+
throw new Error(`image: ${key} must be an integer from 1 to ${max} (got ${value})`)
|
|
27
|
+
return `${key}_${value}`
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The built-in `hamur` provider; `sources` is `#hamur/image/sources`. */
|
|
31
|
+
export function createHamurProvider(sources: ImageSources): ImageProvider {
|
|
32
|
+
return {
|
|
33
|
+
formats: ["jpeg", "png", "webp"],
|
|
34
|
+
negotiates: false,
|
|
35
|
+
fits: ["fill", "inside", "outside"],
|
|
36
|
+
modifiers: ["rotate", "flip", "flop"],
|
|
37
|
+
getImage(src, { modifiers: m, baseURL }) {
|
|
38
|
+
let source: string
|
|
39
|
+
let own: string
|
|
40
|
+
let query = ""
|
|
41
|
+
let v: string | undefined
|
|
42
|
+
if (REMOTE.test(src)) {
|
|
43
|
+
const url = new URL(src)
|
|
44
|
+
const path = url.pathname.slice(1)
|
|
45
|
+
own = extensionOf(path)
|
|
46
|
+
source = `${url.protocol === "https:" ? "_https" : "_http"}/${url.host}/${path}`
|
|
47
|
+
query = url.search
|
|
48
|
+
} else {
|
|
49
|
+
const path = localPath(src, baseURL)
|
|
50
|
+
if (path.startsWith("_https/") || path.startsWith("_http/"))
|
|
51
|
+
throw new Error(`image: the local source "/${path}" starts with a reserved segment`)
|
|
52
|
+
own = extensionOf(path)
|
|
53
|
+
source = path
|
|
54
|
+
if (!path.startsWith("_hamur/")) v = sources[`/${path}`]?.[3]
|
|
55
|
+
}
|
|
56
|
+
const sourceFormat = SOURCE_FORMATS.get(own)
|
|
57
|
+
if (sourceFormat === undefined) return src
|
|
58
|
+
const extension = EXTENSIONS.get(m.format ?? sourceFormat)
|
|
59
|
+
if (extension === undefined) throw new Error(`image: the hamur provider does not make ${m.format}`)
|
|
60
|
+
const parts: string[] = []
|
|
61
|
+
if (m.width === undefined && (m.fit !== undefined || m.height !== undefined)) {
|
|
62
|
+
throw new Error(
|
|
63
|
+
`image: the hamur provider resizes by width; ${m.fit === undefined ? "height" : `fit "${m.fit}"`} needs width`,
|
|
64
|
+
)
|
|
65
|
+
}
|
|
66
|
+
const boxed = m.fit !== undefined && m.height !== undefined
|
|
67
|
+
if (boxed) parts.push(`fit_${m.fit}`)
|
|
68
|
+
if (m.flip === true) parts.push("flip")
|
|
69
|
+
if (m.flop === true) parts.push("flop")
|
|
70
|
+
if (boxed) parts.push(integer("h", m.height!, MAX_SIZE))
|
|
71
|
+
if (m.quality !== undefined) parts.push(integer("q", m.quality, 100))
|
|
72
|
+
if (m.rotate !== undefined) {
|
|
73
|
+
if (!ROTATIONS.includes(m.rotate)) throw new Error(`image: rotate must be 90, 180 or 270 (got ${m.rotate})`)
|
|
74
|
+
parts.push(`rot_${m.rotate}`)
|
|
75
|
+
}
|
|
76
|
+
if (v !== undefined) parts.push(`v_${v}`)
|
|
77
|
+
if (m.width !== undefined) parts.push(integer("w", m.width, MAX_SIZE))
|
|
78
|
+
return `${baseURL}_hamur/image/${parts.length ? parts.join(",") : "_"}/${source}.${extension}${query}`
|
|
79
|
+
},
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/// <reference path="../env.d.ts" />
|
|
2
|
+
import sources from "#hamur/image/sources"
|
|
3
|
+
import type { ImageProvider } from "../types.ts"
|
|
4
|
+
import { createHamurProvider } from "./hamur-url.ts"
|
|
5
|
+
|
|
6
|
+
const provider: ImageProvider = createHamurProvider(sources)
|
|
7
|
+
|
|
8
|
+
export default provider
|
|
@@ -0,0 +1,30 @@
|
|
|
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>> = {
|
|
6
|
+
cover: "c-maintain_ratio",
|
|
7
|
+
inside: "c-at_max",
|
|
8
|
+
outside: "c-at_least",
|
|
9
|
+
contain: "cm-pad_resize",
|
|
10
|
+
fill: "c-force",
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** ImageKit; `options.origin` is the URL endpoint, `https://ik.imagekit.io/your_id` for example. A remote `src` follows the endpoint as a web proxy source. */
|
|
14
|
+
export default {
|
|
15
|
+
formats: ["avif", "webp", "jpeg", "png"],
|
|
16
|
+
negotiates: true,
|
|
17
|
+
fits: ["cover", "inside", "outside", "contain", "fill"],
|
|
18
|
+
modifiers: ["rotate"],
|
|
19
|
+
getImage(src, { modifiers: m, options, baseURL }) {
|
|
20
|
+
const origin = option("imagekit", options, "origin")
|
|
21
|
+
const steps: string[] = []
|
|
22
|
+
if (m.width !== undefined) steps.push(`w-${m.width}`)
|
|
23
|
+
if (m.height !== undefined) steps.push(`h-${m.height}`)
|
|
24
|
+
if (m.fit !== undefined) steps.push(FITS[m.fit]!)
|
|
25
|
+
steps.push(`f-${m.format === undefined ? "auto" : JPG[m.format]}`)
|
|
26
|
+
if (m.quality !== undefined) steps.push(`q-${m.quality}`)
|
|
27
|
+
if (m.rotate !== undefined) steps.push(`rt-${m.rotate}`)
|
|
28
|
+
return withQuery(`${origin}/${REMOTE.test(src) ? src : localPath(src, baseURL)}`, [`tr=${steps.join(",")}`])
|
|
29
|
+
},
|
|
30
|
+
} satisfies ImageProvider
|
|
@@ -0,0 +1,26 @@
|
|
|
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
|
+
/** Imgix; `rot` turns counter-clockwise, so `rotate` is sent as 360 minus it; `options.origin` is the source's domain, `https://assets.imgix.net` for example. A remote `src` is a web proxy source: the whole URL, encoded, is the path. */
|
|
8
|
+
export default {
|
|
9
|
+
formats: ["avif", "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 origin = option("imgix", options, "origin")
|
|
15
|
+
const query: string[] = []
|
|
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
|
+
query.push(m.format === undefined ? "auto=format" : `fm=${JPG[m.format]}`)
|
|
20
|
+
if (m.quality !== undefined) query.push(`q=${m.quality}`)
|
|
21
|
+
if (m.rotate !== undefined) query.push(`rot=${360 - m.rotate}`)
|
|
22
|
+
if (m.flip === true || m.flop === true)
|
|
23
|
+
query.push(`flip=${m.flop === true ? "h" : ""}${m.flip === true ? "v" : ""}`)
|
|
24
|
+
return withQuery(`${origin}/${REMOTE.test(src) ? encodeURIComponent(src) : localPath(src, baseURL)}`, query)
|
|
25
|
+
},
|
|
26
|
+
} satisfies ImageProvider
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { localPath, REMOTE } from "../runtime/source.ts"
|
|
2
|
+
import type { ImageFit, ImageProvider } from "../types.ts"
|
|
3
|
+
import { JPG, option } from "./shared.ts"
|
|
4
|
+
|
|
5
|
+
const FITS: Partial<Record<ImageFit, string>> = { cover: "fill", inside: "fit", fill: "force" }
|
|
6
|
+
|
|
7
|
+
/** imgproxy's unsigned `/insecure/` URLs: a signing key would reach every browser, so the service must restrict its sources (`IMGPROXY_ALLOWED_SOURCES`). `options.origin` is the service's URL; a local `src` is read as `local:///<path>`. Without a format the service decides from `Accept` when its WebP and AVIF detection is on. */
|
|
8
|
+
export default {
|
|
9
|
+
formats: ["avif", "webp", "jpeg", "png"],
|
|
10
|
+
negotiates: true,
|
|
11
|
+
fits: ["cover", "inside", "fill"],
|
|
12
|
+
modifiers: ["rotate"],
|
|
13
|
+
getImage(src, { modifiers: m, options, baseURL }) {
|
|
14
|
+
const origin = option("imgproxy", options, "origin")
|
|
15
|
+
const steps: string[] = []
|
|
16
|
+
if (m.fit !== undefined) steps.push(`rs:${FITS[m.fit]}:${m.width ?? 0}:${m.height ?? 0}`)
|
|
17
|
+
else if (m.width !== undefined) steps.push(`w:${m.width}`)
|
|
18
|
+
if (m.quality !== undefined) steps.push(`q:${m.quality}`)
|
|
19
|
+
if (m.format !== undefined) steps.push(`f:${JPG[m.format]}`)
|
|
20
|
+
if (m.rotate !== undefined) steps.push(`rot:${m.rotate}`)
|
|
21
|
+
const source = REMOTE.test(src) ? src.replace(/[%?@]/g, encodeURIComponent) : `local:///${localPath(src, baseURL)}`
|
|
22
|
+
return `${origin}/insecure/${steps.length ? `${steps.join("/")}/` : ""}plain/${source}`
|
|
23
|
+
},
|
|
24
|
+
} satisfies ImageProvider
|