@nodeart/emdash-plugin-picture 0.0.0-stage → 0.1.0
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 +21 -0
- package/README.md +59 -2
- package/dist/astro.d.mts +26 -0
- package/dist/catalog.d.mts +165 -0
- package/dist/catalog.mjs +173 -0
- package/dist/index.d.mts +5 -0
- package/dist/index.mjs +35 -0
- package/dist/main.d.mts +26 -0
- package/dist/main.mjs +69 -0
- package/dist/manifest.json +27 -0
- package/dist/plugin.d.mts +11 -0
- package/dist/plugin.mjs +1 -0
- package/dist/resolve-media.d.mts +158 -0
- package/dist/resolve-media.mjs +505 -0
- package/emdash-plugin.jsonc +31 -0
- package/package.json +113 -5
- package/src/astro/MediaPicture.astro +91 -0
- package/src/astro/index.ts +1 -0
- package/src/catalog.ts +251 -0
- package/src/constants.ts +7 -0
- package/src/main.ts +75 -0
- package/src/plugin.ts +28 -0
- package/src/resolve-media.ts +906 -0
- package/src/svelte/MediaPicture.svelte +90 -0
- package/src/svelte/index.ts +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 NodeArt
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,60 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @nodeart/emdash-plugin-picture
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Responsive `<picture>` image catalog, AVIF/PNG variant resolver, and Astro/Svelte components for [EmDash CMS](https://emdashcms.com).
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- EmDash Marketplace / Registry compatible (`format: "standard"`, built with `@emdash-cms/plugin-cli`).
|
|
8
|
+
- Generates `<picture>` variants in canonical EmDash `/_image?href=...&w=...&q=...&f=...` format for `avif` (with quality) and lossless `png` (omitting `q`).
|
|
9
|
+
- Never upscales known source dimensions (`computeVariantDimensions`) and omits fabricated `width`/`height` HTML attributes when source bounds are unknown.
|
|
10
|
+
- Leaves SVGs, GIFs, and external URLs untouched as plain fallbacks.
|
|
11
|
+
- Ships typed preset catalog helpers (`defineImageCatalog`), URL/cache helpers (`isImmutableMediaRequest`), and reusable `<MediaPicture>` components via npm (`@nodeart/emdash-plugin-picture/astro` and `@nodeart/emdash-plugin-picture/svelte`). Registry installation provides the backend `/_emdash/api/plugins/emdash-picture/resolve` route; install the npm package when using the Astro/Svelte UI components and catalog utilities.
|
|
12
|
+
|
|
13
|
+
## Installation & Configuration
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
bun add @nodeart/emdash-plugin-picture
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
// astro.config.ts
|
|
21
|
+
import { defineConfig } from 'astro/config';
|
|
22
|
+
import emdash from 'emdash/astro';
|
|
23
|
+
import { emdashPicture } from '@nodeart/emdash-plugin-picture';
|
|
24
|
+
|
|
25
|
+
export default defineConfig({
|
|
26
|
+
integrations: [
|
|
27
|
+
emdash({
|
|
28
|
+
plugins: [emdashPicture()]
|
|
29
|
+
})
|
|
30
|
+
]
|
|
31
|
+
});
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Or run in isolated sandbox mode (requires an EmDash `sandboxRunner` such as `@emdash-cms/cloudflare`):
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import emdashPictureDescriptor from '@nodeart/emdash-plugin-picture';
|
|
38
|
+
|
|
39
|
+
emdash({
|
|
40
|
+
sandboxed: [emdashPictureDescriptor]
|
|
41
|
+
});
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Public Resolve Route
|
|
45
|
+
|
|
46
|
+
`GET /_emdash/api/plugins/emdash-picture/resolve?src=<url>&preset=card`
|
|
47
|
+
|
|
48
|
+
- Supports named presets from `DEFAULT_IMAGE_CATALOG` (`hero`, `wide`, `narrow`, `card`, `thumbnail`, `avatar`, `logo`, `icon`) or custom comma-separated `widths` (1..16 integers, max `4000`).
|
|
49
|
+
- Optional `quality` (1..100 for AVIF), `width`, `height`, `sizes`, `alt`, `fit` (`contain` | `cover`), `fallbackFormat` (`png` | `avif`).
|
|
50
|
+
- Returns HTTP 400 (`PluginRouteError`) on invalid inputs.
|
|
51
|
+
|
|
52
|
+
## Publishing
|
|
53
|
+
|
|
54
|
+
```sh
|
|
55
|
+
bun run validate
|
|
56
|
+
bun run build
|
|
57
|
+
bun run bundle
|
|
58
|
+
emdash-plugin login <handle-or-did>
|
|
59
|
+
emdash-plugin publish
|
|
60
|
+
```
|
package/dist/astro.d.mts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// Auto-generated Astro component declarations for @nodeart/emdash-plugin-picture/astro
|
|
2
|
+
import type { HTMLAttributes } from 'astro/types';
|
|
3
|
+
import type { ImageCatalog } from './catalog.mjs';
|
|
4
|
+
import type { MediaProps, MediaRef, PictureFallbackFormat } from './resolve-media.mjs';
|
|
5
|
+
|
|
6
|
+
export interface MediaPictureProps {
|
|
7
|
+
media: MediaProps | MediaRef | string | null | undefined;
|
|
8
|
+
preset: string | null;
|
|
9
|
+
catalog?: ImageCatalog;
|
|
10
|
+
sizes?: string;
|
|
11
|
+
alt?: string;
|
|
12
|
+
width?: number;
|
|
13
|
+
height?: number;
|
|
14
|
+
fallbackFormat?: PictureFallbackFormat;
|
|
15
|
+
class?: string;
|
|
16
|
+
pictureClass?: string;
|
|
17
|
+
style?: HTMLAttributes<'img'>['style'];
|
|
18
|
+
loading?: 'lazy' | 'eager';
|
|
19
|
+
decoding?: 'async' | 'auto' | 'sync';
|
|
20
|
+
fetchpriority?: 'high' | 'low' | 'auto';
|
|
21
|
+
title?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
declare function MediaPicture__AstroComponent_(_props: MediaPictureProps): any;
|
|
25
|
+
|
|
26
|
+
export { MediaPicture__AstroComponent_ as MediaPicture };
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
//#region src/catalog.d.ts
|
|
2
|
+
declare const IMAGE_OUTPUT_FORMATS: readonly ["avif", "png"];
|
|
3
|
+
declare const TRANSFORMABLE_IMAGE_MIME_TYPES: readonly ["image/jpeg", "image/png", "image/webp", "image/avif"];
|
|
4
|
+
declare const MAX_IMAGE_DIMENSION = 4000;
|
|
5
|
+
type ImageOutputFormat = (typeof IMAGE_OUTPUT_FORMATS)[number];
|
|
6
|
+
type TransformableImageMimeType = (typeof TRANSFORMABLE_IMAGE_MIME_TYPES)[number];
|
|
7
|
+
type ImageFit = 'contain' | 'cover';
|
|
8
|
+
interface ImageFormatPolicy {
|
|
9
|
+
readonly preferred: readonly ImageOutputFormat[];
|
|
10
|
+
readonly fallback: ImageOutputFormat;
|
|
11
|
+
}
|
|
12
|
+
/** Check whether a media item is a raster image suitable for `/_image` AVIF/PNG transforms. */
|
|
13
|
+
declare function isTransformableImage(input: {
|
|
14
|
+
mimeType?: string | null;
|
|
15
|
+
url?: string | null;
|
|
16
|
+
filename?: string | null;
|
|
17
|
+
}): boolean;
|
|
18
|
+
interface ImageCandidate {
|
|
19
|
+
readonly width: number;
|
|
20
|
+
readonly height?: number;
|
|
21
|
+
}
|
|
22
|
+
interface ImageRoleDefinition {
|
|
23
|
+
readonly fit: ImageFit;
|
|
24
|
+
readonly candidates: readonly ImageCandidate[];
|
|
25
|
+
readonly defaultSizes: string;
|
|
26
|
+
readonly avifQuality?: number;
|
|
27
|
+
}
|
|
28
|
+
type ImageRoleSpec = ImageRoleDefinition;
|
|
29
|
+
interface ImageCatalog<TRoles extends Record<string, ImageRoleDefinition> = Record<string, ImageRoleDefinition>> {
|
|
30
|
+
readonly version: string;
|
|
31
|
+
readonly roles: { readonly [TRole in keyof TRoles]: TRoles[TRole] };
|
|
32
|
+
}
|
|
33
|
+
type DefinedImageCatalog<TRoles extends Record<string, ImageRoleDefinition> = Record<string, ImageRoleDefinition>> = ImageCatalog<TRoles>;
|
|
34
|
+
interface ImageCatalogInput<TRoles extends Record<string, ImageRoleDefinition> = Record<string, ImageRoleDefinition>> {
|
|
35
|
+
version: string;
|
|
36
|
+
roles: TRoles;
|
|
37
|
+
}
|
|
38
|
+
type ImageRoleName<TCatalog extends ImageCatalog> = keyof TCatalog['roles'] & string;
|
|
39
|
+
/** Freeze and validate an image preset catalog for EmDash Picture. */
|
|
40
|
+
declare function defineImageCatalog<const TRoles extends Record<string, ImageRoleDefinition>>(input: ImageCatalogInput<TRoles>): ImageCatalog<TRoles>;
|
|
41
|
+
/**
|
|
42
|
+
* Predict output pixel dimensions for a candidate without ever upscaling known source bounds.
|
|
43
|
+
* When source dimensions are omitted (e.g. legacy seed snapshots), emit the candidate width
|
|
44
|
+
* directly so responsive `<picture>` srcsets still work.
|
|
45
|
+
*/
|
|
46
|
+
declare function computeVariantDimensions(sourceWidth: number | null | undefined, sourceHeight: number | null | undefined, definition: {
|
|
47
|
+
width: number;
|
|
48
|
+
height?: number;
|
|
49
|
+
fit: ImageFit;
|
|
50
|
+
}): {
|
|
51
|
+
width: number;
|
|
52
|
+
height?: number;
|
|
53
|
+
};
|
|
54
|
+
/** Default general-purpose image presets for standard EmDash sites. */
|
|
55
|
+
declare const DEFAULT_IMAGE_CATALOG: ImageCatalog<{
|
|
56
|
+
readonly hero: {
|
|
57
|
+
readonly fit: "contain";
|
|
58
|
+
readonly defaultSizes: "(min-width: 1280px) 1200px, 100vw";
|
|
59
|
+
readonly candidates: readonly [{
|
|
60
|
+
readonly width: 640;
|
|
61
|
+
}, {
|
|
62
|
+
readonly width: 960;
|
|
63
|
+
}, {
|
|
64
|
+
readonly width: 1200;
|
|
65
|
+
}, {
|
|
66
|
+
readonly width: 1600;
|
|
67
|
+
}];
|
|
68
|
+
};
|
|
69
|
+
readonly wide: {
|
|
70
|
+
readonly fit: "contain";
|
|
71
|
+
readonly defaultSizes: "(min-width: 870px) 840px, 100vw";
|
|
72
|
+
readonly candidates: readonly [{
|
|
73
|
+
readonly width: 480;
|
|
74
|
+
}, {
|
|
75
|
+
readonly width: 768;
|
|
76
|
+
}, {
|
|
77
|
+
readonly width: 840;
|
|
78
|
+
}, {
|
|
79
|
+
readonly width: 1200;
|
|
80
|
+
}, {
|
|
81
|
+
readonly width: 1680;
|
|
82
|
+
}];
|
|
83
|
+
};
|
|
84
|
+
readonly narrow: {
|
|
85
|
+
readonly fit: "contain";
|
|
86
|
+
readonly defaultSizes: "(min-width: 630px) 600px, calc(100vw - 30px)";
|
|
87
|
+
readonly candidates: readonly [{
|
|
88
|
+
readonly width: 320;
|
|
89
|
+
}, {
|
|
90
|
+
readonly width: 480;
|
|
91
|
+
}, {
|
|
92
|
+
readonly width: 600;
|
|
93
|
+
}, {
|
|
94
|
+
readonly width: 960;
|
|
95
|
+
}, {
|
|
96
|
+
readonly width: 1200;
|
|
97
|
+
}];
|
|
98
|
+
};
|
|
99
|
+
readonly card: {
|
|
100
|
+
readonly fit: "contain";
|
|
101
|
+
readonly defaultSizes: "(min-width: 1200px) 380px, (min-width: 768px) 50vw, 100vw";
|
|
102
|
+
readonly candidates: readonly [{
|
|
103
|
+
readonly width: 320;
|
|
104
|
+
}, {
|
|
105
|
+
readonly width: 480;
|
|
106
|
+
}, {
|
|
107
|
+
readonly width: 640;
|
|
108
|
+
}, {
|
|
109
|
+
readonly width: 768;
|
|
110
|
+
}];
|
|
111
|
+
};
|
|
112
|
+
readonly thumbnail: {
|
|
113
|
+
readonly fit: "contain";
|
|
114
|
+
readonly defaultSizes: "(min-width: 768px) 200px, 120px";
|
|
115
|
+
readonly candidates: readonly [{
|
|
116
|
+
readonly width: 120;
|
|
117
|
+
}, {
|
|
118
|
+
readonly width: 200;
|
|
119
|
+
}, {
|
|
120
|
+
readonly width: 320;
|
|
121
|
+
}, {
|
|
122
|
+
readonly width: 400;
|
|
123
|
+
}];
|
|
124
|
+
};
|
|
125
|
+
readonly avatar: {
|
|
126
|
+
readonly fit: "contain";
|
|
127
|
+
readonly defaultSizes: "96px";
|
|
128
|
+
readonly candidates: readonly [{
|
|
129
|
+
readonly width: 48;
|
|
130
|
+
}, {
|
|
131
|
+
readonly width: 96;
|
|
132
|
+
}, {
|
|
133
|
+
readonly width: 192;
|
|
134
|
+
}];
|
|
135
|
+
};
|
|
136
|
+
readonly logo: {
|
|
137
|
+
readonly fit: "contain";
|
|
138
|
+
readonly defaultSizes: "(min-width: 768px) 192px, 128px";
|
|
139
|
+
readonly candidates: readonly [{
|
|
140
|
+
readonly width: 128;
|
|
141
|
+
}, {
|
|
142
|
+
readonly width: 192;
|
|
143
|
+
}, {
|
|
144
|
+
readonly width: 256;
|
|
145
|
+
}, {
|
|
146
|
+
readonly width: 384;
|
|
147
|
+
}];
|
|
148
|
+
};
|
|
149
|
+
readonly icon: {
|
|
150
|
+
readonly fit: "contain";
|
|
151
|
+
readonly defaultSizes: "32px";
|
|
152
|
+
readonly candidates: readonly [{
|
|
153
|
+
readonly width: 18;
|
|
154
|
+
}, {
|
|
155
|
+
readonly width: 32;
|
|
156
|
+
}, {
|
|
157
|
+
readonly width: 36;
|
|
158
|
+
}, {
|
|
159
|
+
readonly width: 64;
|
|
160
|
+
}];
|
|
161
|
+
};
|
|
162
|
+
}>;
|
|
163
|
+
type DefaultImageRoleName = keyof typeof DEFAULT_IMAGE_CATALOG.roles;
|
|
164
|
+
//#endregion
|
|
165
|
+
export { DEFAULT_IMAGE_CATALOG, DefaultImageRoleName, DefinedImageCatalog, IMAGE_OUTPUT_FORMATS, ImageCandidate, ImageCatalog, ImageCatalogInput, ImageFit, ImageFormatPolicy, ImageOutputFormat, ImageRoleDefinition, ImageRoleName, ImageRoleSpec, MAX_IMAGE_DIMENSION, TRANSFORMABLE_IMAGE_MIME_TYPES, TransformableImageMimeType, computeVariantDimensions, defineImageCatalog, isTransformableImage };
|
package/dist/catalog.mjs
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
//#region src/catalog.ts
|
|
2
|
+
const IMAGE_OUTPUT_FORMATS = ["avif", "png"];
|
|
3
|
+
const TRANSFORMABLE_IMAGE_MIME_TYPES = [
|
|
4
|
+
"image/jpeg",
|
|
5
|
+
"image/png",
|
|
6
|
+
"image/webp",
|
|
7
|
+
"image/avif"
|
|
8
|
+
];
|
|
9
|
+
const MAX_IMAGE_DIMENSION = 4e3;
|
|
10
|
+
const TRANSFORMABLE_IMAGE_EXT_RE = /\.(?:jpe?g|png|webp|avif)(?:$|[?#])/i;
|
|
11
|
+
const NON_TRANSFORMABLE_EXT_RE = /\.(?:svg|gif)(?:$|[?#])/i;
|
|
12
|
+
/** Check whether a media item is a raster image suitable for `/_image` AVIF/PNG transforms. */
|
|
13
|
+
function isTransformableImage(input) {
|
|
14
|
+
const rawMime = typeof input.mimeType === "string" ? input.mimeType.split(";", 1)[0].trim().toLowerCase() : "";
|
|
15
|
+
if (rawMime) {
|
|
16
|
+
if (rawMime.includes("svg") || rawMime === "image/gif") return false;
|
|
17
|
+
if (rawMime === "image/jpg" || TRANSFORMABLE_IMAGE_MIME_TYPES.includes(rawMime)) return true;
|
|
18
|
+
if (rawMime.startsWith("image/")) return false;
|
|
19
|
+
}
|
|
20
|
+
const candidates = [input.filename, input.url].filter((value) => typeof value === "string" && value.trim().length > 0);
|
|
21
|
+
if (candidates.some((value) => NON_TRANSFORMABLE_EXT_RE.test(value.trim()))) return false;
|
|
22
|
+
return candidates.some((value) => TRANSFORMABLE_IMAGE_EXT_RE.test(value.trim()));
|
|
23
|
+
}
|
|
24
|
+
function validatePositiveBoundedInt(value, max, label) {
|
|
25
|
+
if (!Number.isInteger(value) || value < 1 || value > max) throw new Error(`${label} must be an integer between 1 and ${max}`);
|
|
26
|
+
}
|
|
27
|
+
/** Freeze and validate an image preset catalog for EmDash Picture. */
|
|
28
|
+
function defineImageCatalog(input) {
|
|
29
|
+
const frozenRoles = {};
|
|
30
|
+
for (const [role, definition] of Object.entries(input.roles)) {
|
|
31
|
+
if (definition.candidates.length === 0) throw new Error(`Image role must declare at least one candidate: ${role}`);
|
|
32
|
+
if (definition.avifQuality !== void 0) validatePositiveBoundedInt(definition.avifQuality, 100, `Image role avifQuality (${role})`);
|
|
33
|
+
const widths = /* @__PURE__ */ new Set();
|
|
34
|
+
const frozenCandidates = [];
|
|
35
|
+
for (const candidate of definition.candidates) {
|
|
36
|
+
validatePositiveBoundedInt(candidate.width, MAX_IMAGE_DIMENSION, `Image candidate width (${role})`);
|
|
37
|
+
if (candidate.height !== void 0) validatePositiveBoundedInt(candidate.height, MAX_IMAGE_DIMENSION, `Image candidate height (${role})`);
|
|
38
|
+
if (widths.has(candidate.width)) throw new Error(`Image candidate widths must be unique within a role: ${role}/${candidate.width}`);
|
|
39
|
+
widths.add(candidate.width);
|
|
40
|
+
frozenCandidates.push(Object.freeze({ ...candidate }));
|
|
41
|
+
}
|
|
42
|
+
frozenRoles[role] = Object.freeze({
|
|
43
|
+
...definition,
|
|
44
|
+
candidates: Object.freeze(frozenCandidates)
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
return Object.freeze({
|
|
48
|
+
version: input.version,
|
|
49
|
+
roles: Object.freeze(frozenRoles)
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Predict output pixel dimensions for a candidate without ever upscaling known source bounds.
|
|
54
|
+
* When source dimensions are omitted (e.g. legacy seed snapshots), emit the candidate width
|
|
55
|
+
* directly so responsive `<picture>` srcsets still work.
|
|
56
|
+
*/
|
|
57
|
+
function computeVariantDimensions(sourceWidth, sourceHeight, definition) {
|
|
58
|
+
if (!sourceWidth && !sourceHeight) return {
|
|
59
|
+
width: definition.width,
|
|
60
|
+
...definition.height === void 0 ? {} : { height: definition.height }
|
|
61
|
+
};
|
|
62
|
+
if (sourceWidth && !sourceHeight) return { width: Math.max(1, Math.min(sourceWidth, definition.width)) };
|
|
63
|
+
if (!sourceWidth && sourceHeight) return {
|
|
64
|
+
width: definition.width,
|
|
65
|
+
...definition.height === void 0 ? {} : { height: Math.max(1, Math.min(sourceHeight, definition.height)) }
|
|
66
|
+
};
|
|
67
|
+
const knownWidth = sourceWidth;
|
|
68
|
+
const knownHeight = sourceHeight;
|
|
69
|
+
const widthRatio = definition.width / knownWidth;
|
|
70
|
+
const heightRatio = definition.height === void 0 ? widthRatio : definition.height / knownHeight;
|
|
71
|
+
const containScale = Math.min(1, widthRatio, heightRatio);
|
|
72
|
+
const coverScale = Math.max(widthRatio, heightRatio);
|
|
73
|
+
const scale = definition.fit === "cover" ? Math.min(coverScale, 1) : containScale;
|
|
74
|
+
const scaledWidth = Math.max(1, Math.round(knownWidth * scale));
|
|
75
|
+
const scaledHeight = Math.max(1, Math.round(knownHeight * scale));
|
|
76
|
+
return definition.fit === "cover" && definition.height !== void 0 ? {
|
|
77
|
+
width: Math.min(scaledWidth, definition.width),
|
|
78
|
+
height: Math.min(scaledHeight, definition.height)
|
|
79
|
+
} : {
|
|
80
|
+
width: scaledWidth,
|
|
81
|
+
height: scaledHeight
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/** Default general-purpose image presets for standard EmDash sites. */
|
|
85
|
+
const DEFAULT_IMAGE_CATALOG = defineImageCatalog({
|
|
86
|
+
version: "1",
|
|
87
|
+
roles: {
|
|
88
|
+
hero: {
|
|
89
|
+
fit: "contain",
|
|
90
|
+
defaultSizes: "(min-width: 1280px) 1200px, 100vw",
|
|
91
|
+
candidates: [
|
|
92
|
+
{ width: 640 },
|
|
93
|
+
{ width: 960 },
|
|
94
|
+
{ width: 1200 },
|
|
95
|
+
{ width: 1600 }
|
|
96
|
+
]
|
|
97
|
+
},
|
|
98
|
+
wide: {
|
|
99
|
+
fit: "contain",
|
|
100
|
+
defaultSizes: "(min-width: 870px) 840px, 100vw",
|
|
101
|
+
candidates: [
|
|
102
|
+
{ width: 480 },
|
|
103
|
+
{ width: 768 },
|
|
104
|
+
{ width: 840 },
|
|
105
|
+
{ width: 1200 },
|
|
106
|
+
{ width: 1680 }
|
|
107
|
+
]
|
|
108
|
+
},
|
|
109
|
+
narrow: {
|
|
110
|
+
fit: "contain",
|
|
111
|
+
defaultSizes: "(min-width: 630px) 600px, calc(100vw - 30px)",
|
|
112
|
+
candidates: [
|
|
113
|
+
{ width: 320 },
|
|
114
|
+
{ width: 480 },
|
|
115
|
+
{ width: 600 },
|
|
116
|
+
{ width: 960 },
|
|
117
|
+
{ width: 1200 }
|
|
118
|
+
]
|
|
119
|
+
},
|
|
120
|
+
card: {
|
|
121
|
+
fit: "contain",
|
|
122
|
+
defaultSizes: "(min-width: 1200px) 380px, (min-width: 768px) 50vw, 100vw",
|
|
123
|
+
candidates: [
|
|
124
|
+
{ width: 320 },
|
|
125
|
+
{ width: 480 },
|
|
126
|
+
{ width: 640 },
|
|
127
|
+
{ width: 768 }
|
|
128
|
+
]
|
|
129
|
+
},
|
|
130
|
+
thumbnail: {
|
|
131
|
+
fit: "contain",
|
|
132
|
+
defaultSizes: "(min-width: 768px) 200px, 120px",
|
|
133
|
+
candidates: [
|
|
134
|
+
{ width: 120 },
|
|
135
|
+
{ width: 200 },
|
|
136
|
+
{ width: 320 },
|
|
137
|
+
{ width: 400 }
|
|
138
|
+
]
|
|
139
|
+
},
|
|
140
|
+
avatar: {
|
|
141
|
+
fit: "contain",
|
|
142
|
+
defaultSizes: "96px",
|
|
143
|
+
candidates: [
|
|
144
|
+
{ width: 48 },
|
|
145
|
+
{ width: 96 },
|
|
146
|
+
{ width: 192 }
|
|
147
|
+
]
|
|
148
|
+
},
|
|
149
|
+
logo: {
|
|
150
|
+
fit: "contain",
|
|
151
|
+
defaultSizes: "(min-width: 768px) 192px, 128px",
|
|
152
|
+
candidates: [
|
|
153
|
+
{ width: 128 },
|
|
154
|
+
{ width: 192 },
|
|
155
|
+
{ width: 256 },
|
|
156
|
+
{ width: 384 }
|
|
157
|
+
]
|
|
158
|
+
},
|
|
159
|
+
icon: {
|
|
160
|
+
fit: "contain",
|
|
161
|
+
defaultSizes: "32px",
|
|
162
|
+
candidates: [
|
|
163
|
+
{ width: 18 },
|
|
164
|
+
{ width: 32 },
|
|
165
|
+
{ width: 36 },
|
|
166
|
+
{ width: 64 }
|
|
167
|
+
]
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
//#endregion
|
|
173
|
+
export { DEFAULT_IMAGE_CATALOG, IMAGE_OUTPUT_FORMATS, MAX_IMAGE_DIMENSION, TRANSFORMABLE_IMAGE_MIME_TYPES, computeVariantDimensions, defineImageCatalog, isTransformableImage };
|
package/dist/index.d.mts
ADDED
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// Auto-generated by emdash-plugin build. Do not edit.
|
|
2
|
+
// Source: emdash-plugin.jsonc + package.json
|
|
3
|
+
//
|
|
4
|
+
// Default-exports a sandboxed plugin descriptor. Pass it directly into
|
|
5
|
+
// emdash's `plugins:` or `sandboxed:` array — no factory call needed.
|
|
6
|
+
|
|
7
|
+
/** @type {import("emdash").PluginDescriptor} */
|
|
8
|
+
const descriptor = Object.freeze({
|
|
9
|
+
"id": "emdash-picture",
|
|
10
|
+
"displayName": "EmDash Picture",
|
|
11
|
+
"description": "Responsive <picture> image catalog, AVIF/PNG variant resolver, and Astro/Svelte components for EmDash CMS.",
|
|
12
|
+
"version": "0.1.0",
|
|
13
|
+
"format": "standard",
|
|
14
|
+
"entrypoint": "@nodeart/emdash-plugin-picture/sandbox",
|
|
15
|
+
"capabilities": [],
|
|
16
|
+
"allowedHosts": [],
|
|
17
|
+
"storage": {},
|
|
18
|
+
"hooks": [],
|
|
19
|
+
"routes": [
|
|
20
|
+
{
|
|
21
|
+
"name": "resolve",
|
|
22
|
+
"methods": [
|
|
23
|
+
"GET",
|
|
24
|
+
"HEAD"
|
|
25
|
+
],
|
|
26
|
+
"request": {
|
|
27
|
+
"body": "none"
|
|
28
|
+
},
|
|
29
|
+
"public": true,
|
|
30
|
+
"cacheControl": "public, max-age=3600"
|
|
31
|
+
}
|
|
32
|
+
]
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
export default descriptor;
|
package/dist/main.d.mts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { DEFAULT_IMAGE_CATALOG, DefaultImageRoleName, DefinedImageCatalog, IMAGE_OUTPUT_FORMATS, ImageCandidate, ImageCatalog, ImageCatalogInput, ImageFit, ImageFormatPolicy, ImageOutputFormat, ImageRoleDefinition, ImageRoleName, ImageRoleSpec, MAX_IMAGE_DIMENSION, TRANSFORMABLE_IMAGE_MIME_TYPES, TransformableImageMimeType, computeVariantDimensions, defineImageCatalog, isTransformableImage } from "./catalog.mjs";
|
|
2
|
+
import { EMDASH_IMAGE_ENDPOINT, EMDASH_MEDIA_PREFIX, MAX_CUSTOM_WIDTHS, MEDIA_FILE_PREFIX, MediaProps, MediaRef, MediaRoleScope, MediaSource, MediaSourceCandidate, MediaVariant, MediaVariantFormats, PictureFallbackFormat, PictureRenderImg, PictureRenderProps, PictureRenderSource, PictureRouteError, ResolveMediaInput, ResolveMediaOptions, ResolvePictureOptions, ResolvePictureRouteResult, buildEmDashTransformUrl, candidateSrcSet, intrinsicDimensions, isImmutableMediaRequest, isSafeMediaUrl, isTransformableEmDashPath, mediaSrc, resolveMedia, resolvePictureRenderProps, resolvePictureRouteQuery } from "./resolve-media.mjs";
|
|
3
|
+
import { PluginDescriptor, ResolvedPlugin } from "emdash";
|
|
4
|
+
|
|
5
|
+
//#region src/constants.d.ts
|
|
6
|
+
declare const PLUGIN_ID = "emdash-picture";
|
|
7
|
+
declare const PLUGIN_DISPLAY_NAME = "EmDash Picture";
|
|
8
|
+
declare const PLUGIN_DESCRIPTION = "Responsive <picture> image catalog, AVIF/PNG variant resolver, and Astro/Svelte components for EmDash CMS.";
|
|
9
|
+
declare const PLUGIN_VERSION = "0.1.0";
|
|
10
|
+
declare const PACKAGE_NAME = "@nodeart/emdash-plugin-picture";
|
|
11
|
+
declare const SANDBOX_ENTRYPOINT = "@nodeart/emdash-plugin-picture/sandbox";
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/main.d.ts
|
|
14
|
+
/**
|
|
15
|
+
* Standard EmDash plugin descriptor usable in both `emdash({ plugins: [...] })`
|
|
16
|
+
* and `emdash({ sandboxed: [...] })`.
|
|
17
|
+
*/
|
|
18
|
+
declare function emdashPicture(): PluginDescriptor;
|
|
19
|
+
/**
|
|
20
|
+
* Instantiate an in-process `ResolvedPlugin` representation of EmDash Picture
|
|
21
|
+
* with the same capability contract and routes as the sandboxed build.
|
|
22
|
+
*/
|
|
23
|
+
declare function createPlugin(): ResolvedPlugin;
|
|
24
|
+
declare const defaultDescriptor: PluginDescriptor;
|
|
25
|
+
//#endregion
|
|
26
|
+
export { DEFAULT_IMAGE_CATALOG, DefaultImageRoleName, DefinedImageCatalog, EMDASH_IMAGE_ENDPOINT, EMDASH_MEDIA_PREFIX, IMAGE_OUTPUT_FORMATS, ImageCandidate, ImageCatalog, ImageCatalogInput, ImageFit, ImageFormatPolicy, ImageOutputFormat, ImageRoleDefinition, ImageRoleName, ImageRoleSpec, MAX_CUSTOM_WIDTHS, MAX_IMAGE_DIMENSION, MEDIA_FILE_PREFIX, MediaProps, MediaRef, MediaRoleScope, MediaSource, MediaSourceCandidate, MediaVariant, MediaVariantFormats, PACKAGE_NAME, PLUGIN_DESCRIPTION, PLUGIN_DISPLAY_NAME, PLUGIN_ID, PLUGIN_VERSION, PictureFallbackFormat, PictureRenderImg, PictureRenderProps, PictureRenderSource, PictureRouteError, ResolveMediaInput, ResolveMediaOptions, ResolvePictureOptions, ResolvePictureRouteResult, SANDBOX_ENTRYPOINT, TRANSFORMABLE_IMAGE_MIME_TYPES, TransformableImageMimeType, buildEmDashTransformUrl, candidateSrcSet, computeVariantDimensions, createPlugin, defaultDescriptor as default, defineImageCatalog, emdashPicture, intrinsicDimensions, isImmutableMediaRequest, isSafeMediaUrl, isTransformableEmDashPath, isTransformableImage, mediaSrc, resolveMedia, resolvePictureRenderProps, resolvePictureRouteQuery };
|
package/dist/main.mjs
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { DEFAULT_IMAGE_CATALOG, IMAGE_OUTPUT_FORMATS, MAX_IMAGE_DIMENSION, TRANSFORMABLE_IMAGE_MIME_TYPES, computeVariantDimensions, defineImageCatalog, isTransformableImage } from "./catalog.mjs";
|
|
2
|
+
import { EMDASH_IMAGE_ENDPOINT, EMDASH_MEDIA_PREFIX, MAX_CUSTOM_WIDTHS, MEDIA_FILE_PREFIX, PictureRouteError, buildEmDashTransformUrl, candidateSrcSet, intrinsicDimensions, isImmutableMediaRequest, isSafeMediaUrl, isTransformableEmDashPath, mediaSrc, resolveMedia, resolvePictureRenderProps, resolvePictureRouteQuery } from "./resolve-media.mjs";
|
|
3
|
+
import { definePlugin } from "emdash";
|
|
4
|
+
|
|
5
|
+
//#region src/constants.ts
|
|
6
|
+
const PLUGIN_ID = "emdash-picture";
|
|
7
|
+
const PLUGIN_DISPLAY_NAME = "EmDash Picture";
|
|
8
|
+
const PLUGIN_DESCRIPTION = "Responsive <picture> image catalog, AVIF/PNG variant resolver, and Astro/Svelte components for EmDash CMS.";
|
|
9
|
+
const PLUGIN_VERSION = "0.1.0";
|
|
10
|
+
const PACKAGE_NAME = "@nodeart/emdash-plugin-picture";
|
|
11
|
+
const SANDBOX_ENTRYPOINT = `${PACKAGE_NAME}/sandbox`;
|
|
12
|
+
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/main.ts
|
|
15
|
+
/**
|
|
16
|
+
* Standard EmDash plugin descriptor usable in both `emdash({ plugins: [...] })`
|
|
17
|
+
* and `emdash({ sandboxed: [...] })`.
|
|
18
|
+
*/
|
|
19
|
+
function emdashPicture() {
|
|
20
|
+
return {
|
|
21
|
+
id: PLUGIN_ID,
|
|
22
|
+
displayName: PLUGIN_DISPLAY_NAME,
|
|
23
|
+
description: PLUGIN_DESCRIPTION,
|
|
24
|
+
version: PLUGIN_VERSION,
|
|
25
|
+
format: "standard",
|
|
26
|
+
entrypoint: SANDBOX_ENTRYPOINT,
|
|
27
|
+
capabilities: [],
|
|
28
|
+
allowedHosts: [],
|
|
29
|
+
storage: {},
|
|
30
|
+
hooks: [],
|
|
31
|
+
routes: [{
|
|
32
|
+
name: "resolve",
|
|
33
|
+
public: true,
|
|
34
|
+
methods: ["GET", "HEAD"],
|
|
35
|
+
request: { body: "none" },
|
|
36
|
+
cacheControl: "public, max-age=3600"
|
|
37
|
+
}]
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Instantiate an in-process `ResolvedPlugin` representation of EmDash Picture
|
|
42
|
+
* with the same capability contract and routes as the sandboxed build.
|
|
43
|
+
*/
|
|
44
|
+
function createPlugin() {
|
|
45
|
+
return definePlugin({
|
|
46
|
+
id: PLUGIN_ID,
|
|
47
|
+
displayName: PLUGIN_DISPLAY_NAME,
|
|
48
|
+
description: PLUGIN_DESCRIPTION,
|
|
49
|
+
version: PLUGIN_VERSION,
|
|
50
|
+
capabilities: [],
|
|
51
|
+
allowedHosts: [],
|
|
52
|
+
storage: {},
|
|
53
|
+
routes: { resolve: {
|
|
54
|
+
public: true,
|
|
55
|
+
methods: ["GET", "HEAD"],
|
|
56
|
+
request: { body: "none" },
|
|
57
|
+
cacheControl: "public, max-age=3600",
|
|
58
|
+
handler: async (ctx) => {
|
|
59
|
+
const result = resolvePictureRouteQuery(ctx.input);
|
|
60
|
+
if (!result.ok) throw new PictureRouteError(result.error);
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
63
|
+
} }
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
const defaultDescriptor = Object.freeze(emdashPicture());
|
|
67
|
+
|
|
68
|
+
//#endregion
|
|
69
|
+
export { DEFAULT_IMAGE_CATALOG, EMDASH_IMAGE_ENDPOINT, EMDASH_MEDIA_PREFIX, IMAGE_OUTPUT_FORMATS, MAX_CUSTOM_WIDTHS, MAX_IMAGE_DIMENSION, MEDIA_FILE_PREFIX, PACKAGE_NAME, PLUGIN_DESCRIPTION, PLUGIN_DISPLAY_NAME, PLUGIN_ID, PLUGIN_VERSION, PictureRouteError, SANDBOX_ENTRYPOINT, TRANSFORMABLE_IMAGE_MIME_TYPES, buildEmDashTransformUrl, candidateSrcSet, computeVariantDimensions, createPlugin, defaultDescriptor as default, defineImageCatalog, emdashPicture, intrinsicDimensions, isImmutableMediaRequest, isSafeMediaUrl, isTransformableEmDashPath, isTransformableImage, mediaSrc, resolveMedia, resolvePictureRenderProps, resolvePictureRouteQuery };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"id": "emdash-picture",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"declaredAccess": {},
|
|
5
|
+
"capabilities": [],
|
|
6
|
+
"allowedHosts": [],
|
|
7
|
+
"storage": {},
|
|
8
|
+
"hooks": [],
|
|
9
|
+
"routes": [
|
|
10
|
+
{
|
|
11
|
+
"name": "resolve",
|
|
12
|
+
"methods": [
|
|
13
|
+
"GET",
|
|
14
|
+
"HEAD"
|
|
15
|
+
],
|
|
16
|
+
"request": {
|
|
17
|
+
"body": "none"
|
|
18
|
+
},
|
|
19
|
+
"public": true,
|
|
20
|
+
"cacheControl": "public, max-age=3600"
|
|
21
|
+
}
|
|
22
|
+
],
|
|
23
|
+
"admin": {
|
|
24
|
+
"pages": [],
|
|
25
|
+
"widgets": []
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { SandboxedPlugin } from "emdash/plugin";
|
|
2
|
+
|
|
3
|
+
//#region src/plugin.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Sandboxed EmDash runtime entry. Compatible with both isolated sandbox
|
|
6
|
+
* execution (`emdash({ sandboxed: [...] })`) and in-process adaptation
|
|
7
|
+
* (`emdash({ plugins: [...] })`).
|
|
8
|
+
*/
|
|
9
|
+
declare const plugin: SandboxedPlugin;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { plugin as default };
|
package/dist/plugin.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function e(e){return e}const t=[`image/jpeg`,`image/png`,`image/webp`,`image/avif`],n=4e3,r=/\.(?:jpe?g|png|webp|avif)(?:$|[?#])/i,i=/\.(?:svg|gif)(?:$|[?#])/i;function a(e){let n=typeof e.mimeType==`string`?e.mimeType.split(`;`,1)[0].trim().toLowerCase():``;if(n){if(n.includes(`svg`)||n===`image/gif`)return!1;if(n===`image/jpg`||t.includes(n))return!0;if(n.startsWith(`image/`))return!1}let a=[e.filename,e.url].filter(e=>typeof e==`string`&&e.trim().length>0);return a.some(e=>i.test(e.trim()))?!1:a.some(e=>r.test(e.trim()))}function o(e,t,n){if(!Number.isInteger(e)||e<1||e>t)throw Error(`${n} must be an integer between 1 and ${t}`)}function s(e){let t={};for(let[r,i]of Object.entries(e.roles)){if(i.candidates.length===0)throw Error(`Image role must declare at least one candidate: ${r}`);i.avifQuality!==void 0&&o(i.avifQuality,100,`Image role avifQuality (${r})`);let e=new Set,a=[];for(let t of i.candidates){if(o(t.width,n,`Image candidate width (${r})`),t.height!==void 0&&o(t.height,n,`Image candidate height (${r})`),e.has(t.width))throw Error(`Image candidate widths must be unique within a role: ${r}/${t.width}`);e.add(t.width),a.push(Object.freeze({...t}))}t[r]=Object.freeze({...i,candidates:Object.freeze(a)})}return Object.freeze({version:e.version,roles:Object.freeze(t)})}function c(e,t,n){if(!e&&!t)return{width:n.width,...n.height===void 0?{}:{height:n.height}};if(e&&!t)return{width:Math.max(1,Math.min(e,n.width))};if(!e&&t)return{width:n.width,...n.height===void 0?{}:{height:Math.max(1,Math.min(t,n.height))}};let r=e,i=t,a=n.width/r,o=n.height===void 0?a:n.height/i,s=Math.min(1,a,o),c=Math.max(a,o),l=n.fit===`cover`?Math.min(c,1):s,u=Math.max(1,Math.round(r*l)),d=Math.max(1,Math.round(i*l));return n.fit===`cover`&&n.height!==void 0?{width:Math.min(u,n.width),height:Math.min(d,n.height)}:{width:u,height:d}}const l=s({version:`1`,roles:{hero:{fit:`contain`,defaultSizes:`(min-width: 1280px) 1200px, 100vw`,candidates:[{width:640},{width:960},{width:1200},{width:1600}]},wide:{fit:`contain`,defaultSizes:`(min-width: 870px) 840px, 100vw`,candidates:[{width:480},{width:768},{width:840},{width:1200},{width:1680}]},narrow:{fit:`contain`,defaultSizes:`(min-width: 630px) 600px, calc(100vw - 30px)`,candidates:[{width:320},{width:480},{width:600},{width:960},{width:1200}]},card:{fit:`contain`,defaultSizes:`(min-width: 1200px) 380px, (min-width: 768px) 50vw, 100vw`,candidates:[{width:320},{width:480},{width:640},{width:768}]},thumbnail:{fit:`contain`,defaultSizes:`(min-width: 768px) 200px, 120px`,candidates:[{width:120},{width:200},{width:320},{width:400}]},avatar:{fit:`contain`,defaultSizes:`96px`,candidates:[{width:48},{width:96},{width:192}]},logo:{fit:`contain`,defaultSizes:`(min-width: 768px) 192px, 128px`,candidates:[{width:128},{width:192},{width:256},{width:384}]},icon:{fit:`contain`,defaultSizes:`32px`,candidates:[{width:18},{width:32},{width:36},{width:64}]}}}),u=`/_emdash/api/media/file/`,d=/^[A-Za-z0-9._-]+$/,f=/^[1-9]\d*$/;var p=class extends Error{code;status;constructor(e,t=`BAD_REQUEST`,n=400){super(e),this.name=`PluginRouteError`,this.code=t,this.status=n}};function m(e){let t=new URLSearchParams;return t.set(`href`,e.href),t.set(`w`,String(e.width)),e.height!==void 0&&t.set(`h`,String(e.height)),e.format!==`png`&&e.quality!==void 0&&t.set(`q`,String(e.quality)),t.set(`f`,e.format),`/_image?${t.toString()}`}function h(e){if(!e.startsWith(u))return!1;let t=e.slice(24);return t.length>0&&d.test(t)&&!t.includes(`..`)}function g(e,t,n){if(e===null||!f.test(e))return null;let r=Number(e);return Number.isSafeInteger(r)&&r>=t&&r<=n?r:null}function _(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function v(e){if(typeof e!=`string`)return;let t=e.trim();return t.length>0?t:void 0}function y(e){return typeof e==`number`&&Number.isFinite(e)&&e>0?Math.round(e):void 0}function b(e){let t=e.trim();if(!t||t.length>2048||t.startsWith(`//`))return!1;if(t.startsWith(`/`))return!0;try{let e=new URL(t);return e.protocol===`http:`||e.protocol===`https:`}catch{return!1}}function x(e,t){if(!t||t.length===0)return!1;let n=e.toLowerCase();return t.some(e=>{let t=e.trim().toLowerCase();if(!t)return!1;if(t.startsWith(`*.`)){let e=t.slice(1);return n.endsWith(e)&&n.length>e.length}return n===t})}function S(e,t,n){let r=e.trim();if(!b(r))return null;if(r.startsWith(u)){try{let e=new URL(r,`http://localhost`);if(h(e.pathname))return e.pathname}catch{return null}return null}if(r.startsWith(`/`))return null;try{let e=new URL(r);if(e.protocol!==`http:`&&e.protocol!==`https:`)return null;if(h(e.pathname)&&(t||x(e.hostname,n)))return e.pathname}catch{return null}return null}function C(e){let t=v(e);if(t)return d.test(t)&&!t.includes(`..`)?t:void 0}function w(e){let t=v(e.provider);if(t&&t!==`local`)return!1;let n=_(e.asset)?e.asset:void 0,r=v(n?.provider);return r&&r!==`local`?!1:!!(v(e.id)??v(e.mediaId)??v(e.storageKey)??v(n?._ref)??v(n?.storageKey))}function T(e,t){let n=_(e.meta)?e.meta:void 0,r=_(e.asset)?e.asset:void 0,i=r&&_(r.meta)?r.meta:void 0,a=v(e.provider)??v(r?.provider),o=!a||a===`local`,s=C(e.storageKey)??C(n?.storageKey)??C(r?.storageKey)??C(i?.storageKey)??(o?C(r?._ref):void 0),c=v(e.src)??v(e.url)??v(r?.url)??v(r?.src)??(s?`${u}${s}`:void 0);return!c||!b(c)?null:S(c,w(e),t)??c.trim()}function E(e,t,n,r,i){let a=new Map,o=i===`avif`?t.avifQuality??76:void 0;for(let s of[...t.candidates].sort((e,t)=>e.width-t.width)){let l=c(n,r,{width:s.width,fit:t.fit,...s.height===void 0?{}:{height:s.height}});a.set(l.width,{url:m({href:e,width:l.width,...s.height===void 0?{}:{height:l.height},format:i,quality:o}),width:l.width,...l.height===void 0?{}:{height:l.height}})}return[...a.values()].sort((e,t)=>e.width-t.width)}function D(e,t,n,r){let i=E(e,t,n,r,`avif`),a=E(e,t,n,r,`png`),o=i.at(-1);return{avif:i,png:a,...o?.width===void 0?{}:{width:o.width},...o?.height===void 0?{}:{height:o.height},sizes:t.defaultSizes}}function O(e){return _(e)&&typeof e.url==`string`&&e.url.trim().length>0&&b(e.url)&&(`variants`in e||`originalUrl`in e)}function k(e,t=`all`,n=l,r){if(!e)return null;let i=r?.allowedHosts;if(O(e)){let r=S(e.url,!1,i)??e.url.trim(),o=r===e.url?e:{...e,url:r},s=t===`all`?Object.keys(n.roles):t.filter(e=>Object.hasOwn(n.roles,e));if(s.every(e=>!!(o.variants&&Object.hasOwn(o.variants,e)))||!h(r)||!a({mimeType:o.mimeType,filename:o.filename,url:r}))return o;let c={...o.variants??{}};for(let e of s)if(!Object.hasOwn(c,e)){let t=n.roles[e];t&&(c[e]=D(r,t,o.width,o.height))}return Object.keys(c).length>0?{...o,variants:c}:o}let o=typeof e==`string`?{src:e}:_(e)?e:null;if(!o)return null;let s=_(o.meta)?o.meta:void 0,c=_(o.asset)?o.asset:void 0,u=c&&_(c.meta)?c.meta:void 0,d=T(o,i);if(!d)return null;let f=typeof o.alt==`string`?o.alt:typeof c?.alt==`string`?c.alt:void 0,p=y(o.width)??y(c?.width),m=y(o.height)??y(c?.height),g=v(o.mimeType)??v(c?.mimeType)??v(s?.mimeType)??v(u?.mimeType),b=v(o.filename)??v(c?.filename)??v(s?.filename)??v(u?.filename),x={url:d,originalUrl:d,...f===void 0?{}:{alt:f},...p===void 0?{}:{width:p},...m===void 0?{}:{height:m},...g===void 0?{}:{mimeType:g},...b===void 0?{}:{filename:b}};if(h(d)&&a({mimeType:g,filename:b,url:d})){let e=Object.entries(n.roles).filter(([e])=>t===`all`||t.includes(e));e.length>0&&(x.variants=Object.fromEntries(e.map(([e,t])=>[e,D(d,t,p,m)])))}return x}function A(e){return e?.originalUrl||e?.url||``}function j(e){return e.map(({url:e,width:t})=>`${e} ${t}w`).join(`, `)}function M(e,t){if(!e.width&&!e.height)return{};if(e.width&&!e.height)return{width:t?.width===void 0?e.width:Math.min(e.width,t.width)};if(!e.width&&e.height)return{height:t?.height===void 0?e.height:Math.min(e.height,t.height)};let n=e.width,r=e.height;return t?.width===void 0?{width:n,height:r}:t.height===void 0?{width:t.width,height:Math.max(1,Math.round(t.width*r/n))}:{width:t.width,height:t.height}}function N(e,t,n,r){if(n!==void 0&&r!==void 0)return{width:n,height:r};let i=e.width&&e.height?e.width/e.height:t.width&&t.height?t.width/t.height:void 0;return n===void 0?r===void 0?t:{...i?{width:Math.max(1,Math.round(r*i))}:t.width===void 0?{}:{width:t.width},height:r}:{width:n,...i?{height:Math.max(1,Math.round(n/i))}:t.height===void 0?{}:{height:t.height}}}function P(e){let{media:t,preset:n,catalog:r=l,sizes:i,alt:a,width:o,height:s,fallbackFormat:c=`png`,allowedHosts:u}=e,d=r,f=k(t,n?[n]:[],d,{allowedHosts:u});if(!f||!f.url)return null;let p=n&&f.variants&&Object.hasOwn(f.variants,n)?f.variants[n]:void 0,m=n&&Object.hasOwn(d.roles,n)?d.roles[n]:void 0,h=i??m?.defaultSizes??p?.sizes,g=p?.png??[],_=p?.avif??[],v=g.length>0?j(g):void 0,y=_.length>0?j(_):void 0,b=c===`avif`?_:g,x=c===`avif`?y:v,S=b.at(-1)?.url??A(f),C=[];y&&C.push({type:`image/avif`,srcset:y,...h?{sizes:h}:{}}),v&&C.push({type:`image/png`,srcset:v,...h?{sizes:h}:{}});let w=N(f,M(f,p),o,s),T=a??f.alt??``;return{resolved:f,resolvedSizes:h,fallbackSrc:S,pngSrcSet:v,avifSrcSet:y,alt:T,width:w.width,height:w.height,sources:C,img:{src:S,...C.length===0&&x?{srcset:x}:{},...C.length===0&&x&&h?{sizes:h}:{},alt:T,...w.width===void 0?{}:{width:w.width},...w.height===void 0?{}:{height:w.height}}}}function F(e,t){if(!_(e)||!Object.hasOwn(e,t))return;let n=e[t];if(typeof n==`string`)return v(n);if(Array.isArray(n)&&typeof n[0]==`string`)return v(n[0])}function I(e){if(e===void 0)return{ok:!0,widths:void 0};let t=e.split(`,`).map(e=>e.trim());if(t.length===0||t.length>16)return{ok:!1,error:`Invalid "widths" parameter: expected 1..16 comma-separated integers.`};let r=[];for(let e of t){let t=g(e,1,n);if(t===null)return{ok:!1,error:`Invalid width "${e}": expected integer between 1 and ${n}.`};r.push(t)}return{ok:!0,widths:[...new Set(r)].sort((e,t)=>e-t)}}function L(e){let t=F(e,`src`)??F(e,`href`);if(!t||!b(t))return{ok:!1,error:`Invalid or missing "src"/"href" query parameter.`};let r=I(F(e,`widths`));if(!r.ok)return r;let i=F(e,`width`),a;if(i!==void 0){let e=g(i,1,n);if(e===null)return{ok:!1,error:`Invalid "width" parameter: expected integer between 1 and ${n}.`};a=e}let o=F(e,`height`),c;if(o!==void 0){let e=g(o,1,n);if(e===null)return{ok:!1,error:`Invalid "height" parameter: expected integer between 1 and ${n}.`};c=e}let u=F(e,`quality`),d;if(u!==void 0){let e=g(u,1,100);if(e===null)return{ok:!1,error:`Invalid "quality" parameter: expected integer between 1 and 100.`};d=e}let f=F(e,`sizes`);if(f&&f.length>512)return{ok:!1,error:`"sizes" parameter exceeds maximum length.`};let p=F(e,`alt`);if(p&&p.length>512)return{ok:!1,error:`"alt" parameter exceeds maximum length.`};let m=F(e,`fit`);if(m!==void 0&&m!==`contain`&&m!==`cover`)return{ok:!1,error:`Invalid "fit" parameter: expected "contain" or "cover".`};let h=m??`contain`,_=F(e,`fallbackFormat`);if(_!==void 0&&_!==`png`&&_!==`avif`)return{ok:!1,error:`Invalid "fallbackFormat" parameter: expected "png" or "avif".`};let v=_??`png`,y=F(e,`preset`)??`card`,x=l,S=y;if(r.widths)S=`custom`,x=s({version:`1`,roles:{custom:{fit:h,defaultSizes:f??`100vw`,candidates:r.widths.map(e=>({width:e})),...d===void 0?{}:{avifQuality:d}}}});else{let e=l;if(!Object.hasOwn(e.roles,y))return{ok:!1,error:`Unknown preset "${y}". Available presets: ${Object.keys(e.roles).join(`, `)}.`};if(d!==void 0){let t=e.roles[y];x=s({version:e.version,roles:{[y]:{...t,avifQuality:d}}})}}let C=P({media:{src:t,...a===void 0?{}:{width:a},...c===void 0?{}:{height:c},...p===void 0?{}:{alt:p}},preset:S,catalog:x,sizes:f,alt:p,fallbackFormat:v});return C?{ok:!0,preset:S,media:C.resolved,sources:C.sources,img:C.img}:{ok:!1,error:`Unable to resolve media input.`}}const R={routes:{resolve:e({public:!0,methods:[`GET`,`HEAD`],cacheControl:`public, max-age=3600`,request:{body:`none`},handler:async(e,t)=>{let n=L(e.input);if(!n.ok)throw t.log.warn(`Invalid picture resolve request`,{error:n.error}),new p(n.error);return n}})}};export{R as default};
|