@aphexcms/cms-core 9.9.0 → 10.0.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/dist/api/assets.d.ts +62 -7
- package/dist/api/assets.d.ts.map +1 -1
- package/dist/api/assets.js +116 -9
- package/dist/api/client.d.ts +1 -1
- package/dist/api/client.d.ts.map +1 -1
- package/dist/api/client.js +39 -9
- package/dist/api/limits.d.ts +55 -0
- package/dist/api/limits.d.ts.map +1 -0
- package/dist/api/limits.js +58 -0
- package/dist/api/schemas/assets.d.ts +94 -4
- package/dist/api/schemas/assets.d.ts.map +1 -1
- package/dist/api/schemas/assets.js +82 -5
- package/dist/api/schemas/documents.d.ts +1 -0
- package/dist/api/schemas/documents.d.ts.map +1 -1
- package/dist/api/schemas/documents.js +1 -0
- package/dist/api/types.d.ts +25 -0
- package/dist/api/types.d.ts.map +1 -1
- package/dist/api/upload-timeout.d.ts +17 -0
- package/dist/api/upload-timeout.d.ts.map +1 -0
- package/dist/api/upload-timeout.js +35 -0
- package/dist/api/upload.d.ts +42 -0
- package/dist/api/upload.d.ts.map +1 -0
- package/dist/api/upload.js +144 -0
- package/dist/components/AdminApp.svelte +213 -32
- package/dist/components/AdminApp.svelte.d.ts.map +1 -1
- package/dist/components/admin/AssetBrowserModal.svelte +4 -3
- package/dist/components/admin/AssetBrowserModal.svelte.d.ts +2 -1
- package/dist/components/admin/AssetBrowserModal.svelte.d.ts.map +1 -1
- package/dist/components/admin/MediaBrowser.svelte +1859 -281
- package/dist/components/admin/MediaBrowser.svelte.d.ts +21 -1
- package/dist/components/admin/MediaBrowser.svelte.d.ts.map +1 -1
- package/dist/components/admin/fields/ArrayField.svelte +21 -10
- package/dist/components/admin/fields/ArrayField.svelte.d.ts.map +1 -1
- package/dist/components/admin/fields/FileField.svelte +9 -2
- package/dist/components/admin/fields/FileField.svelte.d.ts.map +1 -1
- package/dist/components/admin/fields/ImageField.svelte +47 -12
- package/dist/components/admin/fields/ImageField.svelte.d.ts.map +1 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +14 -0
- package/dist/db/interfaces/asset.d.ts +114 -5
- package/dist/db/interfaces/asset.d.ts.map +1 -1
- package/dist/db/interfaces/document.d.ts +107 -3
- package/dist/db/interfaces/document.d.ts.map +1 -1
- package/dist/hooks.d.ts.map +1 -1
- package/dist/hooks.js +2 -1
- package/dist/image/Image.svelte +72 -0
- package/dist/image/Image.svelte.d.ts +40 -0
- package/dist/image/Image.svelte.d.ts.map +1 -0
- package/dist/image/index.d.ts +3 -0
- package/dist/image/index.d.ts.map +1 -0
- package/dist/image/index.js +11 -0
- package/dist/image/types.d.ts +28 -0
- package/dist/image/types.d.ts.map +1 -0
- package/dist/image/types.js +1 -0
- package/dist/images/generate.d.ts +55 -0
- package/dist/images/generate.d.ts.map +1 -0
- package/dist/images/generate.js +243 -0
- package/dist/images/index.d.ts +2 -0
- package/dist/images/index.d.ts.map +1 -0
- package/dist/images/index.js +7 -0
- package/dist/images/variants.d.ts +82 -0
- package/dist/images/variants.d.ts.map +1 -0
- package/dist/images/variants.js +124 -0
- package/dist/jobs/asset-reference-jobs.d.ts +49 -0
- package/dist/jobs/asset-reference-jobs.d.ts.map +1 -0
- package/dist/jobs/asset-reference-jobs.js +74 -0
- package/dist/jobs/run-batch.d.ts.map +1 -1
- package/dist/jobs/run-batch.js +5 -0
- package/dist/lib/api/assets.d.ts +62 -7
- package/dist/lib/api/assets.d.ts.map +1 -1
- package/dist/lib/api/assets.js +116 -9
- package/dist/lib/api/assets.js.map +1 -1
- package/dist/lib/api/client.d.ts +1 -1
- package/dist/lib/api/client.d.ts.map +1 -1
- package/dist/lib/api/client.js +39 -9
- package/dist/lib/api/client.js.map +1 -1
- package/dist/lib/api/limits.d.ts +55 -0
- package/dist/lib/api/limits.d.ts.map +1 -0
- package/dist/lib/api/limits.js +59 -0
- package/dist/lib/api/limits.js.map +1 -0
- package/dist/lib/api/schemas/assets.d.ts +94 -4
- package/dist/lib/api/schemas/assets.d.ts.map +1 -1
- package/dist/lib/api/schemas/assets.js +82 -5
- package/dist/lib/api/schemas/assets.js.map +1 -1
- package/dist/lib/api/schemas/documents.d.ts +1 -0
- package/dist/lib/api/schemas/documents.d.ts.map +1 -1
- package/dist/lib/api/schemas/documents.js +1 -0
- package/dist/lib/api/schemas/documents.js.map +1 -1
- package/dist/lib/api/types.d.ts +25 -0
- package/dist/lib/api/types.d.ts.map +1 -1
- package/dist/lib/api/upload-timeout.d.ts +17 -0
- package/dist/lib/api/upload-timeout.d.ts.map +1 -0
- package/dist/lib/api/upload-timeout.js +36 -0
- package/dist/lib/api/upload-timeout.js.map +1 -0
- package/dist/lib/api/upload.d.ts +42 -0
- package/dist/lib/api/upload.d.ts.map +1 -0
- package/dist/lib/api/upload.js +145 -0
- package/dist/lib/api/upload.js.map +1 -0
- package/dist/lib/config.d.ts.map +1 -1
- package/dist/lib/config.js +14 -0
- package/dist/lib/config.js.map +1 -1
- package/dist/lib/db/interfaces/asset.d.ts +114 -5
- package/dist/lib/db/interfaces/asset.d.ts.map +1 -1
- package/dist/lib/db/interfaces/document.d.ts +107 -3
- package/dist/lib/db/interfaces/document.d.ts.map +1 -1
- package/dist/lib/hooks.d.ts.map +1 -1
- package/dist/lib/hooks.js +2 -1
- package/dist/lib/hooks.js.map +1 -1
- package/dist/lib/image/index.d.ts +3 -0
- package/dist/lib/image/index.d.ts.map +1 -0
- package/dist/lib/image/index.js +12 -0
- package/dist/lib/image/index.js.map +1 -0
- package/dist/lib/image/types.d.ts +28 -0
- package/dist/lib/image/types.d.ts.map +1 -0
- package/dist/lib/image/types.js +2 -0
- package/dist/lib/image/types.js.map +1 -0
- package/dist/lib/images/generate.d.ts +55 -0
- package/dist/lib/images/generate.d.ts.map +1 -0
- package/dist/lib/images/generate.js +244 -0
- package/dist/lib/images/generate.js.map +1 -0
- package/dist/lib/images/index.d.ts +2 -0
- package/dist/lib/images/index.d.ts.map +1 -0
- package/dist/lib/images/index.js +8 -0
- package/dist/lib/images/index.js.map +1 -0
- package/dist/lib/images/variants.d.ts +82 -0
- package/dist/lib/images/variants.d.ts.map +1 -0
- package/dist/lib/images/variants.js +125 -0
- package/dist/lib/images/variants.js.map +1 -0
- package/dist/lib/jobs/asset-reference-jobs.d.ts +49 -0
- package/dist/lib/jobs/asset-reference-jobs.d.ts.map +1 -0
- package/dist/lib/jobs/asset-reference-jobs.js +75 -0
- package/dist/lib/jobs/asset-reference-jobs.js.map +1 -0
- package/dist/lib/jobs/run-batch.d.ts.map +1 -1
- package/dist/lib/jobs/run-batch.js +5 -0
- package/dist/lib/jobs/run-batch.js.map +1 -1
- package/dist/lib/local-api/collection-api.d.ts +25 -2
- package/dist/lib/local-api/collection-api.d.ts.map +1 -1
- package/dist/lib/local-api/collection-api.js +107 -20
- package/dist/lib/local-api/collection-api.js.map +1 -1
- package/dist/lib/preview/assets.d.ts +19 -0
- package/dist/lib/preview/assets.d.ts.map +1 -1
- package/dist/lib/preview/assets.js +6 -0
- package/dist/lib/preview/assets.js.map +1 -1
- package/dist/lib/routes/assets-cdn.d.ts +19 -0
- package/dist/lib/routes/assets-cdn.d.ts.map +1 -1
- package/dist/lib/routes/assets-cdn.js +295 -17
- package/dist/lib/routes/assets-cdn.js.map +1 -1
- package/dist/lib/schema-utils/utils.d.ts +31 -1
- package/dist/lib/schema-utils/utils.d.ts.map +1 -1
- package/dist/lib/schema-utils/utils.js +68 -0
- package/dist/lib/schema-utils/utils.js.map +1 -1
- package/dist/lib/server/api/index.d.ts.map +1 -1
- package/dist/lib/server/api/index.js +87 -8
- package/dist/lib/server/api/index.js.map +1 -1
- package/dist/lib/server/api/routes/assets-bulk.d.ts.map +1 -1
- package/dist/lib/server/api/routes/assets-bulk.js +36 -7
- package/dist/lib/server/api/routes/assets-bulk.js.map +1 -1
- package/dist/lib/server/api/routes/assets-by-id.d.ts.map +1 -1
- package/dist/lib/server/api/routes/assets-by-id.js +32 -15
- package/dist/lib/server/api/routes/assets-by-id.js.map +1 -1
- package/dist/lib/server/api/routes/assets-direct-upload.d.ts +4 -0
- package/dist/lib/server/api/routes/assets-direct-upload.d.ts.map +1 -0
- package/dist/lib/server/api/routes/assets-direct-upload.js +164 -0
- package/dist/lib/server/api/routes/assets-direct-upload.js.map +1 -0
- package/dist/lib/server/api/routes/assets-poster.d.ts +4 -0
- package/dist/lib/server/api/routes/assets-poster.d.ts.map +1 -0
- package/dist/lib/server/api/routes/assets-poster.js +120 -0
- package/dist/lib/server/api/routes/assets-poster.js.map +1 -0
- package/dist/lib/server/api/routes/assets-references.d.ts.map +1 -1
- package/dist/lib/server/api/routes/assets-references.js +64 -5
- package/dist/lib/server/api/routes/assets-references.js.map +1 -1
- package/dist/lib/server/api/routes/assets.d.ts.map +1 -1
- package/dist/lib/server/api/routes/assets.js +116 -12
- package/dist/lib/server/api/routes/assets.js.map +1 -1
- package/dist/lib/server/api/routes/clear-asset-references.d.ts +18 -0
- package/dist/lib/server/api/routes/clear-asset-references.d.ts.map +1 -0
- package/dist/lib/server/api/routes/clear-asset-references.js +32 -0
- package/dist/lib/server/api/routes/clear-asset-references.js.map +1 -0
- package/dist/lib/server/api/routes/documents.d.ts.map +1 -1
- package/dist/lib/server/api/routes/documents.js +28 -1
- package/dist/lib/server/api/routes/documents.js.map +1 -1
- package/dist/lib/server/index.d.ts +2 -0
- package/dist/lib/server/index.d.ts.map +1 -1
- package/dist/lib/server/index.js +4 -0
- package/dist/lib/server/index.js.map +1 -1
- package/dist/lib/services/asset-references-service.d.ts +93 -0
- package/dist/lib/services/asset-references-service.d.ts.map +1 -0
- package/dist/lib/services/asset-references-service.js +198 -0
- package/dist/lib/services/asset-references-service.js.map +1 -0
- package/dist/lib/services/asset-service.d.ts +115 -14
- package/dist/lib/services/asset-service.d.ts.map +1 -1
- package/dist/lib/services/asset-service.js +312 -31
- package/dist/lib/services/asset-service.js.map +1 -1
- package/dist/lib/services/references-service.d.ts +19 -15
- package/dist/lib/services/references-service.d.ts.map +1 -1
- package/dist/lib/services/references-service.js +59 -31
- package/dist/lib/services/references-service.js.map +1 -1
- package/dist/lib/services/version-service.d.ts +10 -1
- package/dist/lib/services/version-service.d.ts.map +1 -1
- package/dist/lib/services/version-service.js +26 -4
- package/dist/lib/services/version-service.js.map +1 -1
- package/dist/lib/storage/adapters/local-storage-adapter.d.ts +38 -0
- package/dist/lib/storage/adapters/local-storage-adapter.d.ts.map +1 -1
- package/dist/lib/storage/adapters/local-storage-adapter.js +86 -19
- package/dist/lib/storage/adapters/local-storage-adapter.js.map +1 -1
- package/dist/lib/storage/index.d.ts +1 -0
- package/dist/lib/storage/index.d.ts.map +1 -1
- package/dist/lib/storage/index.js +2 -0
- package/dist/lib/storage/index.js.map +1 -1
- package/dist/lib/storage/interfaces/storage.d.ts +135 -5
- package/dist/lib/storage/interfaces/storage.d.ts.map +1 -1
- package/dist/lib/storage/keys.d.ts +96 -0
- package/dist/lib/storage/keys.d.ts.map +1 -0
- package/dist/lib/storage/keys.js +160 -0
- package/dist/lib/storage/keys.js.map +1 -0
- package/dist/lib/type-gen.d.ts.map +1 -1
- package/dist/lib/type-gen.js +11 -5
- package/dist/lib/type-gen.js.map +1 -1
- package/dist/lib/types/asset.d.ts +92 -12
- package/dist/lib/types/asset.d.ts.map +1 -1
- package/dist/lib/types/capabilities.d.ts +11 -0
- package/dist/lib/types/capabilities.d.ts.map +1 -1
- package/dist/lib/types/capabilities.js +20 -6
- package/dist/lib/types/capabilities.js.map +1 -1
- package/dist/lib/types/config.d.ts +127 -0
- package/dist/lib/types/config.d.ts.map +1 -1
- package/dist/lib/types/filters.d.ts +31 -0
- package/dist/lib/types/filters.d.ts.map +1 -1
- package/dist/lib/types/schemas.d.ts +14 -0
- package/dist/lib/types/schemas.d.ts.map +1 -1
- package/dist/lib/utils/asset-reference-walk.d.ts +44 -0
- package/dist/lib/utils/asset-reference-walk.d.ts.map +1 -0
- package/dist/lib/utils/asset-reference-walk.js +144 -0
- package/dist/lib/utils/asset-reference-walk.js.map +1 -0
- package/dist/lib/utils/image-url.d.ts +61 -26
- package/dist/lib/utils/image-url.d.ts.map +1 -1
- package/dist/lib/utils/image-url.js +143 -26
- package/dist/lib/utils/image-url.js.map +1 -1
- package/dist/lib/utils/video-metadata.d.ts +39 -0
- package/dist/lib/utils/video-metadata.d.ts.map +1 -0
- package/dist/lib/utils/video-metadata.js +161 -0
- package/dist/lib/utils/video-metadata.js.map +1 -0
- package/dist/local-api/collection-api.d.ts +25 -2
- package/dist/local-api/collection-api.d.ts.map +1 -1
- package/dist/local-api/collection-api.js +107 -20
- package/dist/preview/assets.d.ts +19 -0
- package/dist/preview/assets.d.ts.map +1 -1
- package/dist/preview/assets.js +6 -0
- package/dist/routes/assets-cdn.d.ts +19 -0
- package/dist/routes/assets-cdn.d.ts.map +1 -1
- package/dist/routes/assets-cdn.js +295 -17
- package/dist/schema-utils/utils.d.ts +31 -1
- package/dist/schema-utils/utils.d.ts.map +1 -1
- package/dist/schema-utils/utils.js +68 -0
- package/dist/server/api/index.d.ts.map +1 -1
- package/dist/server/api/index.js +87 -8
- package/dist/server/api/routes/assets-bulk.d.ts.map +1 -1
- package/dist/server/api/routes/assets-bulk.js +36 -7
- package/dist/server/api/routes/assets-by-id.d.ts.map +1 -1
- package/dist/server/api/routes/assets-by-id.js +32 -15
- package/dist/server/api/routes/assets-direct-upload.d.ts +4 -0
- package/dist/server/api/routes/assets-direct-upload.d.ts.map +1 -0
- package/dist/server/api/routes/assets-direct-upload.js +163 -0
- package/dist/server/api/routes/assets-poster.d.ts +4 -0
- package/dist/server/api/routes/assets-poster.d.ts.map +1 -0
- package/dist/server/api/routes/assets-poster.js +119 -0
- package/dist/server/api/routes/assets-references.d.ts.map +1 -1
- package/dist/server/api/routes/assets-references.js +64 -5
- package/dist/server/api/routes/assets.d.ts.map +1 -1
- package/dist/server/api/routes/assets.js +116 -12
- package/dist/server/api/routes/clear-asset-references.d.ts +18 -0
- package/dist/server/api/routes/clear-asset-references.d.ts.map +1 -0
- package/dist/server/api/routes/clear-asset-references.js +31 -0
- package/dist/server/api/routes/documents.d.ts.map +1 -1
- package/dist/server/api/routes/documents.js +28 -1
- package/dist/server/index.d.ts +2 -0
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +4 -0
- package/dist/services/asset-references-service.d.ts +93 -0
- package/dist/services/asset-references-service.d.ts.map +1 -0
- package/dist/services/asset-references-service.js +197 -0
- package/dist/services/asset-service.d.ts +115 -14
- package/dist/services/asset-service.d.ts.map +1 -1
- package/dist/services/asset-service.js +312 -31
- package/dist/services/references-service.d.ts +19 -15
- package/dist/services/references-service.d.ts.map +1 -1
- package/dist/services/references-service.js +59 -31
- package/dist/services/version-service.d.ts +10 -1
- package/dist/services/version-service.d.ts.map +1 -1
- package/dist/services/version-service.js +26 -4
- package/dist/storage/adapters/local-storage-adapter.d.ts +38 -0
- package/dist/storage/adapters/local-storage-adapter.d.ts.map +1 -1
- package/dist/storage/adapters/local-storage-adapter.js +86 -19
- package/dist/storage/index.d.ts +1 -0
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/storage/index.js +2 -0
- package/dist/storage/interfaces/storage.d.ts +135 -5
- package/dist/storage/interfaces/storage.d.ts.map +1 -1
- package/dist/storage/keys.d.ts +96 -0
- package/dist/storage/keys.d.ts.map +1 -0
- package/dist/storage/keys.js +159 -0
- package/dist/type-gen.d.ts.map +1 -1
- package/dist/type-gen.js +11 -5
- package/dist/types/asset.d.ts +92 -12
- package/dist/types/asset.d.ts.map +1 -1
- package/dist/types/capabilities.d.ts +11 -0
- package/dist/types/capabilities.d.ts.map +1 -1
- package/dist/types/capabilities.js +20 -6
- package/dist/types/config.d.ts +127 -0
- package/dist/types/config.d.ts.map +1 -1
- package/dist/types/filters.d.ts +31 -0
- package/dist/types/filters.d.ts.map +1 -1
- package/dist/types/schemas.d.ts +14 -0
- package/dist/types/schemas.d.ts.map +1 -1
- package/dist/utils/asset-reference-walk.d.ts +44 -0
- package/dist/utils/asset-reference-walk.d.ts.map +1 -0
- package/dist/utils/asset-reference-walk.js +143 -0
- package/dist/utils/image-url.d.ts +61 -26
- package/dist/utils/image-url.d.ts.map +1 -1
- package/dist/utils/image-url.js +143 -26
- package/dist/utils/video-metadata.d.ts +39 -0
- package/dist/utils/video-metadata.d.ts.map +1 -0
- package/dist/utils/video-metadata.js +160 -0
- package/package.json +6 -1
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { ImageValue } from './types';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Renders an Aphex image field as a single responsive `<img>`.
|
|
6
|
+
*
|
|
7
|
+
* Everything it needs — url, dimensions, srcset, default alt — is injected
|
|
8
|
+
* server-side by `assetService.injectAssetUrls`, so this component holds no
|
|
9
|
+
* knowledge of the width ladder, the config hash, or how a variant URL is
|
|
10
|
+
* built. That keeps one place responsible for addressing derivatives and
|
|
11
|
+
* ships nothing extra to the browser.
|
|
12
|
+
*
|
|
13
|
+
* Renders nothing at all when the value has no resolved url, rather than an
|
|
14
|
+
* `<img>` with an empty `src` — which browsers treat as a request for the
|
|
15
|
+
* current page and is a real, if quiet, waste of a round trip.
|
|
16
|
+
*/
|
|
17
|
+
interface Props {
|
|
18
|
+
/** An image field value, after asset injection. */
|
|
19
|
+
value: ImageValue | null | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* Overrides the alt text. Precedence is placement → asset → empty, so a
|
|
22
|
+
* decorative placement can pass `alt=""` and mean it.
|
|
23
|
+
*/
|
|
24
|
+
alt?: string;
|
|
25
|
+
/**
|
|
26
|
+
* The `sizes` attribute. Without it a browser assumes the image occupies
|
|
27
|
+
* the full viewport width and will happily pick the largest candidate on
|
|
28
|
+
* a phone, which defeats the point of a srcset.
|
|
29
|
+
*/
|
|
30
|
+
sizes?: string;
|
|
31
|
+
/**
|
|
32
|
+
* Above-the-fold hint. Switches off lazy loading and raises fetch
|
|
33
|
+
* priority — correct for an LCP image, wasteful for anything else.
|
|
34
|
+
*/
|
|
35
|
+
priority?: boolean;
|
|
36
|
+
class?: string;
|
|
37
|
+
[key: string]: unknown;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
let {
|
|
41
|
+
value,
|
|
42
|
+
alt,
|
|
43
|
+
sizes = '100vw',
|
|
44
|
+
priority = false,
|
|
45
|
+
class: className,
|
|
46
|
+
...rest
|
|
47
|
+
}: Props = $props();
|
|
48
|
+
|
|
49
|
+
const asset = $derived(value?.asset);
|
|
50
|
+
const src = $derived(asset?.url);
|
|
51
|
+
|
|
52
|
+
// Placement alt wins over the asset's default; an explicitly empty string is
|
|
53
|
+
// preserved, since `alt=""` is the correct markup for a decorative image and
|
|
54
|
+
// is meaningfully different from a missing alt.
|
|
55
|
+
const resolvedAlt = $derived(alt ?? value?.alt ?? asset?.alt ?? '');
|
|
56
|
+
</script>
|
|
57
|
+
|
|
58
|
+
{#if src}
|
|
59
|
+
<img
|
|
60
|
+
{src}
|
|
61
|
+
alt={resolvedAlt}
|
|
62
|
+
srcset={asset?.srcset || undefined}
|
|
63
|
+
sizes={asset?.srcset ? sizes : undefined}
|
|
64
|
+
width={asset?.width || undefined}
|
|
65
|
+
height={asset?.height || undefined}
|
|
66
|
+
loading={priority ? 'eager' : 'lazy'}
|
|
67
|
+
fetchpriority={priority ? 'high' : undefined}
|
|
68
|
+
decoding="async"
|
|
69
|
+
class={className || undefined}
|
|
70
|
+
{...rest}
|
|
71
|
+
/>
|
|
72
|
+
{/if}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { ImageValue } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Renders an Aphex image field as a single responsive `<img>`.
|
|
4
|
+
*
|
|
5
|
+
* Everything it needs — url, dimensions, srcset, default alt — is injected
|
|
6
|
+
* server-side by `assetService.injectAssetUrls`, so this component holds no
|
|
7
|
+
* knowledge of the width ladder, the config hash, or how a variant URL is
|
|
8
|
+
* built. That keeps one place responsible for addressing derivatives and
|
|
9
|
+
* ships nothing extra to the browser.
|
|
10
|
+
*
|
|
11
|
+
* Renders nothing at all when the value has no resolved url, rather than an
|
|
12
|
+
* `<img>` with an empty `src` — which browsers treat as a request for the
|
|
13
|
+
* current page and is a real, if quiet, waste of a round trip.
|
|
14
|
+
*/
|
|
15
|
+
interface Props {
|
|
16
|
+
/** An image field value, after asset injection. */
|
|
17
|
+
value: ImageValue | null | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* Overrides the alt text. Precedence is placement → asset → empty, so a
|
|
20
|
+
* decorative placement can pass `alt=""` and mean it.
|
|
21
|
+
*/
|
|
22
|
+
alt?: string;
|
|
23
|
+
/**
|
|
24
|
+
* The `sizes` attribute. Without it a browser assumes the image occupies
|
|
25
|
+
* the full viewport width and will happily pick the largest candidate on
|
|
26
|
+
* a phone, which defeats the point of a srcset.
|
|
27
|
+
*/
|
|
28
|
+
sizes?: string;
|
|
29
|
+
/**
|
|
30
|
+
* Above-the-fold hint. Switches off lazy loading and raises fetch
|
|
31
|
+
* priority — correct for an LCP image, wasteful for anything else.
|
|
32
|
+
*/
|
|
33
|
+
priority?: boolean;
|
|
34
|
+
class?: string;
|
|
35
|
+
[key: string]: unknown;
|
|
36
|
+
}
|
|
37
|
+
declare const Image: import("svelte").Component<Props, {}, "">;
|
|
38
|
+
type Image = ReturnType<typeof Image>;
|
|
39
|
+
export default Image;
|
|
40
|
+
//# sourceMappingURL=Image.svelte.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Image.svelte.d.ts","sourceRoot":"","sources":["../../src/lib/image/Image.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAGzC;;;;;;;;;;;;GAYG;AACH,UAAU,KAAK;IACd,mDAAmD;IACnD,KAAK,EAAE,UAAU,GAAG,IAAI,GAAG,SAAS,CAAC;IACrC;;;OAGG;IACH,GAAG,CAAC,EAAE,MAAM,CAAC;IACb;;;;OAIG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;OAGG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACvB;AA6BF,QAAA,MAAM,KAAK,2CAAwC,CAAC;AACpD,KAAK,KAAK,GAAG,UAAU,CAAC,OAAO,KAAK,CAAC,CAAC;AACtC,eAAe,KAAK,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/lib/image/index.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,OAAO,IAAI,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAClD,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// `@aphexcms/cms-core/image` — the public rendering surface.
|
|
2
|
+
//
|
|
3
|
+
// Its own narrow entrypoint on purpose. A marketing page importing `<Image>`
|
|
4
|
+
// must not pull in the admin barrel and its TipTap/field-editor chunk, which is
|
|
5
|
+
// exactly what a shared barrel would cause: Rollup's unit is the chunk, not the
|
|
6
|
+
// symbol.
|
|
7
|
+
//
|
|
8
|
+
// It lives here rather than in `@aphexcms/ui` because that package is the
|
|
9
|
+
// shadcn design system and knows nothing about assets — putting an
|
|
10
|
+
// Asset-shaped component there would invert the dependency.
|
|
11
|
+
export { default as Image } from './Image.svelte';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shape `<Image>` consumes: an image field value after asset injection.
|
|
3
|
+
*
|
|
4
|
+
* Declared here rather than imported from the admin types so this entrypoint
|
|
5
|
+
* stays free of anything server- or admin-shaped — it is imported by public
|
|
6
|
+
* marketing pages, and a stray import is how a barrel drags the admin bundle
|
|
7
|
+
* onto them. The dependency runs the other way instead: `types/asset.ts` builds
|
|
8
|
+
* its `ImageAsset` on top of {@link InjectedAsset}, so the set of fields
|
|
9
|
+
* injection produces is declared exactly once and a document's own type can't
|
|
10
|
+
* disagree with what `<Image>` reads off it.
|
|
11
|
+
*/
|
|
12
|
+
export interface InjectedAsset {
|
|
13
|
+
_ref?: string;
|
|
14
|
+
/** Populated by `injectAssetUrls`; absent means the ref didn't resolve. */
|
|
15
|
+
url?: string;
|
|
16
|
+
/** The asset's own default alt, shared across placements. */
|
|
17
|
+
alt?: string;
|
|
18
|
+
width?: number;
|
|
19
|
+
height?: number;
|
|
20
|
+
/** Pre-built responsive candidates. Absent when the pipeline is off. */
|
|
21
|
+
srcset?: string;
|
|
22
|
+
}
|
|
23
|
+
export interface ImageValue {
|
|
24
|
+
asset?: InjectedAsset;
|
|
25
|
+
/** Per-placement alt override, which takes precedence over the asset's. */
|
|
26
|
+
alt?: string;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/lib/image/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,MAAM,WAAW,aAAa;IAC7B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,2EAA2E;IAC3E,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,6DAA6D;IAC7D,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,wEAAwE;IACxE,MAAM,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,UAAU;IAC1B,KAAK,CAAC,EAAE,aAAa,CAAC;IACtB,2EAA2E;IAC3E,GAAG,CAAC,EAAE,MAAM,CAAC;CACb"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { Asset, AssetVariant } from '../types/asset.js';
|
|
2
|
+
import type { StorageAdapter } from '../storage/interfaces/storage.js';
|
|
3
|
+
import type { DatabaseAdapter } from '../db/interfaces/index.js';
|
|
4
|
+
import { type ImageConfig } from './variants.js';
|
|
5
|
+
/**
|
|
6
|
+
* Set how many derivatives may be produced at once.
|
|
7
|
+
*
|
|
8
|
+
* Unlike the upload timeout, this earns being configurable: it encodes a real
|
|
9
|
+
* decision — how much memory this deployment is willing to spend on image
|
|
10
|
+
* processing — and the right answer genuinely differs between a 512MB VPS and a
|
|
11
|
+
* 4GB function. Applied process-wide because the constraint is the process's
|
|
12
|
+
* memory, not any one request's.
|
|
13
|
+
*/
|
|
14
|
+
export declare function setGenerationConcurrency(limit: number): void;
|
|
15
|
+
/** Thrown when the queue is saturated. The caller serves the original instead. */
|
|
16
|
+
export declare class GenerationBusyError extends Error {
|
|
17
|
+
constructor();
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Thrown for a source whose animation would be destroyed by resizing.
|
|
21
|
+
*
|
|
22
|
+
* Sharp reads only the first frame unless told otherwise, so an animated GIF
|
|
23
|
+
* run through this pipeline comes out as a single still — the image still
|
|
24
|
+
* "works", which is what makes it dangerous: nothing errors, the animation is
|
|
25
|
+
* just silently gone.
|
|
26
|
+
*
|
|
27
|
+
* Preserving it is possible (`animated: true` out to an animated WebP) and
|
|
28
|
+
* deliberately not done here, because the memory cost is unbounded in the one
|
|
29
|
+
* dimension nothing else caps: a decoded animation is frames × width × height ×
|
|
30
|
+
* 4, so a couple of hundred frames at 800×600 is well over a gigabyte. That is
|
|
31
|
+
* exactly the out-of-memory case the concurrency gate exists to prevent, and no
|
|
32
|
+
* per-image pixel limit catches it. Animated sources are served as-is instead.
|
|
33
|
+
*/
|
|
34
|
+
export declare class AnimatedSourceError extends Error {
|
|
35
|
+
constructor(pages: number);
|
|
36
|
+
}
|
|
37
|
+
export interface GeneratedVariant {
|
|
38
|
+
variant: AssetVariant;
|
|
39
|
+
buffer: Buffer;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Produce (or await) the derivative of `asset` at `width`.
|
|
43
|
+
*
|
|
44
|
+
* Writes the variant to storage and records it on `asset.metadata.variants`
|
|
45
|
+
* before resolving, so the next request is a cache hit.
|
|
46
|
+
*/
|
|
47
|
+
export declare function generateVariant(opts: {
|
|
48
|
+
asset: Asset;
|
|
49
|
+
width: number;
|
|
50
|
+
config: ImageConfig;
|
|
51
|
+
configHash: string;
|
|
52
|
+
storage: StorageAdapter;
|
|
53
|
+
database: DatabaseAdapter;
|
|
54
|
+
}): Promise<GeneratedVariant>;
|
|
55
|
+
//# sourceMappingURL=generate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generate.d.ts","sourceRoot":"","sources":["../../src/lib/images/generate.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC1D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AACpE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAE9D,OAAO,EAAe,KAAK,WAAW,EAAE,MAAM,YAAY,CAAC;AAkD3D;;;;;;;;GAQG;AACH,wBAAgB,wBAAwB,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAI5D;AAaD,kFAAkF;AAClF,qBAAa,mBAAoB,SAAQ,KAAK;;CAK7C;AAED;;;;;;;;;;;;;;GAcG;AACH,qBAAa,mBAAoB,SAAQ,KAAK;gBACjC,KAAK,EAAE,MAAM;CAIzB;AAqBD,MAAM,WAAW,gBAAgB;IAChC,OAAO,EAAE,YAAY,CAAC;IACtB,MAAM,EAAE,MAAM,CAAC;CACf;AAED;;;;;GAKG;AACH,wBAAsB,eAAe,CAAC,IAAI,EAAE;IAC3C,KAAK,EAAE,KAAK,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,WAAW,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,cAAc,CAAC;IACxB,QAAQ,EAAE,eAAe,CAAC;CAC1B,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAU5B"}
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import sharp from 'sharp';
|
|
2
|
+
import { buildVariantKey, buildVariantUrl, VARIANT_FORMAT } from '../storage/keys.js';
|
|
3
|
+
import { getVariants } from './variants.js';
|
|
4
|
+
import { cmsLogger } from '../utils/logger.js';
|
|
5
|
+
/**
|
|
6
|
+
* Derivative generation. Server-only — imports Sharp.
|
|
7
|
+
*
|
|
8
|
+
* Generation happens on the first request for a width, not at upload. That
|
|
9
|
+
* makes backfill free (an old asset is upgraded simply by being viewed) and
|
|
10
|
+
* means a width nobody asks for is never produced. The cost is that the first
|
|
11
|
+
* request for each (asset, width) pays the resize; every later one is served
|
|
12
|
+
* from storage, and from the CDN after that, because a variant URL embeds the
|
|
13
|
+
* config hash and is therefore immutable.
|
|
14
|
+
*/
|
|
15
|
+
/** Matches the upload path's guard — a decompression bomb must not reach libvips. */
|
|
16
|
+
const MAX_INPUT_PIXELS = 100_000_000;
|
|
17
|
+
/**
|
|
18
|
+
* In-flight generations, keyed by storage key.
|
|
19
|
+
*
|
|
20
|
+
* Ten simultaneous requests for the same cold variant would otherwise run ten
|
|
21
|
+
* identical resizes. This collapses them to one within a process.
|
|
22
|
+
*
|
|
23
|
+
* It is explicitly *not* a distributed lock: on serverless, separate instances
|
|
24
|
+
* will still duplicate work. That is acceptable because generation is
|
|
25
|
+
* idempotent — same source, same config, same key, same bytes — so the worst
|
|
26
|
+
* case is wasted CPU, never a corrupt or half-written variant.
|
|
27
|
+
*/
|
|
28
|
+
const inFlight = new Map();
|
|
29
|
+
/**
|
|
30
|
+
* Cap on *simultaneous* resizes, which is the thing that actually bounds memory.
|
|
31
|
+
*
|
|
32
|
+
* The single-flight map above only dedupes requests for the same variant. It
|
|
33
|
+
* does nothing for twenty requests across twenty different assets, which is the
|
|
34
|
+
* realistic shape of traffic hitting a cold gallery — and each of those holds a
|
|
35
|
+
* fully decoded bitmap while it works. At `limitInputPixels` a single decode can
|
|
36
|
+
* approach 400MB, so unbounded concurrency is an out-of-memory kill on any
|
|
37
|
+
* modest container, not merely slow.
|
|
38
|
+
*
|
|
39
|
+
* Excess work waits rather than failing: an image arriving a little late is
|
|
40
|
+
* strictly better than a 500, and the wait is bounded by how long a resize
|
|
41
|
+
* takes.
|
|
42
|
+
*/
|
|
43
|
+
const DEFAULT_GENERATION_CONCURRENCY = 2;
|
|
44
|
+
let concurrencyLimit = DEFAULT_GENERATION_CONCURRENCY;
|
|
45
|
+
let active = 0;
|
|
46
|
+
const waiting = [];
|
|
47
|
+
/**
|
|
48
|
+
* Set how many derivatives may be produced at once.
|
|
49
|
+
*
|
|
50
|
+
* Unlike the upload timeout, this earns being configurable: it encodes a real
|
|
51
|
+
* decision — how much memory this deployment is willing to spend on image
|
|
52
|
+
* processing — and the right answer genuinely differs between a 512MB VPS and a
|
|
53
|
+
* 4GB function. Applied process-wide because the constraint is the process's
|
|
54
|
+
* memory, not any one request's.
|
|
55
|
+
*/
|
|
56
|
+
export function setGenerationConcurrency(limit) {
|
|
57
|
+
if (Number.isFinite(limit) && limit >= 1) {
|
|
58
|
+
concurrencyLimit = Math.floor(limit);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Cap on how many requests may *queue* for a slot.
|
|
63
|
+
*
|
|
64
|
+
* An unbounded wait queue converts a memory problem into a worse one: under a
|
|
65
|
+
* burst, thousands of requests each hold a connection and a decoded-image
|
|
66
|
+
* ambition, and the ones at the back time out having achieved nothing. Beyond
|
|
67
|
+
* this depth generation is refused, and the route falls back to serving the
|
|
68
|
+
* original — heavier bytes, but immediately, and the CDN absorbs the retry.
|
|
69
|
+
*/
|
|
70
|
+
const MAX_GENERATION_QUEUE = 32;
|
|
71
|
+
/** Thrown when the queue is saturated. The caller serves the original instead. */
|
|
72
|
+
export class GenerationBusyError extends Error {
|
|
73
|
+
constructor() {
|
|
74
|
+
super('Image generation is at capacity');
|
|
75
|
+
this.name = 'GenerationBusyError';
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Thrown for a source whose animation would be destroyed by resizing.
|
|
80
|
+
*
|
|
81
|
+
* Sharp reads only the first frame unless told otherwise, so an animated GIF
|
|
82
|
+
* run through this pipeline comes out as a single still — the image still
|
|
83
|
+
* "works", which is what makes it dangerous: nothing errors, the animation is
|
|
84
|
+
* just silently gone.
|
|
85
|
+
*
|
|
86
|
+
* Preserving it is possible (`animated: true` out to an animated WebP) and
|
|
87
|
+
* deliberately not done here, because the memory cost is unbounded in the one
|
|
88
|
+
* dimension nothing else caps: a decoded animation is frames × width × height ×
|
|
89
|
+
* 4, so a couple of hundred frames at 800×600 is well over a gigabyte. That is
|
|
90
|
+
* exactly the out-of-memory case the concurrency gate exists to prevent, and no
|
|
91
|
+
* per-image pixel limit catches it. Animated sources are served as-is instead.
|
|
92
|
+
*/
|
|
93
|
+
export class AnimatedSourceError extends Error {
|
|
94
|
+
constructor(pages) {
|
|
95
|
+
super(`Refusing to flatten an animated source (${pages} frames)`);
|
|
96
|
+
this.name = 'AnimatedSourceError';
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
async function acquire() {
|
|
100
|
+
if (active < concurrencyLimit) {
|
|
101
|
+
active++;
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (waiting.length >= MAX_GENERATION_QUEUE) {
|
|
105
|
+
throw new GenerationBusyError();
|
|
106
|
+
}
|
|
107
|
+
await new Promise((resolve) => waiting.push(resolve));
|
|
108
|
+
active++;
|
|
109
|
+
}
|
|
110
|
+
function release() {
|
|
111
|
+
active--;
|
|
112
|
+
// Wake exactly one waiter. Waking all would let them race past the limit,
|
|
113
|
+
// which is the bug this whole mechanism exists to prevent.
|
|
114
|
+
waiting.shift()?.();
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Produce (or await) the derivative of `asset` at `width`.
|
|
118
|
+
*
|
|
119
|
+
* Writes the variant to storage and records it on `asset.metadata.variants`
|
|
120
|
+
* before resolving, so the next request is a cache hit.
|
|
121
|
+
*/
|
|
122
|
+
export async function generateVariant(opts) {
|
|
123
|
+
const { asset, width, configHash } = opts;
|
|
124
|
+
const key = buildVariantKey(asset.id, width, configHash);
|
|
125
|
+
const existing = inFlight.get(key);
|
|
126
|
+
if (existing)
|
|
127
|
+
return existing;
|
|
128
|
+
const work = produce(opts, key).finally(() => inFlight.delete(key));
|
|
129
|
+
inFlight.set(key, work);
|
|
130
|
+
return work;
|
|
131
|
+
}
|
|
132
|
+
async function produce(opts, key) {
|
|
133
|
+
const { asset, width, config, configHash, storage, database } = opts;
|
|
134
|
+
// Acquired around the decode/encode only, and released in `finally` — a
|
|
135
|
+
// thrown resize must not leak the slot, or the pool bleeds capacity until
|
|
136
|
+
// nothing can generate at all. Everything after this point is inside the
|
|
137
|
+
// gate, including the storage read, since holding the source buffer is part
|
|
138
|
+
// of the memory being bounded.
|
|
139
|
+
await acquire();
|
|
140
|
+
try {
|
|
141
|
+
return await resize({ asset, width, config, configHash, storage, database }, key);
|
|
142
|
+
}
|
|
143
|
+
finally {
|
|
144
|
+
release();
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
async function resize(opts, key) {
|
|
148
|
+
const { asset, width, config, configHash, storage, database } = opts;
|
|
149
|
+
const source = await storage.getObject(asset.path);
|
|
150
|
+
// Header-only read, so this costs nothing next to the resize. Checked here
|
|
151
|
+
// rather than trusting the row, because assets uploaded before `pages` was
|
|
152
|
+
// recorded have no such field and would otherwise be flattened.
|
|
153
|
+
const sourceMeta = await sharp(source).metadata();
|
|
154
|
+
const pages = sourceMeta.pages ?? 1;
|
|
155
|
+
if (pages > 1)
|
|
156
|
+
throw new AnimatedSourceError(pages);
|
|
157
|
+
// `.rotate()` with no argument applies the EXIF orientation tag. Without it
|
|
158
|
+
// a photo taken in portrait on a phone comes out sideways, because the
|
|
159
|
+
// pixels are landscape and only the tag says otherwise — and Sharp strips
|
|
160
|
+
// metadata by default, so the tag doesn't survive to correct it later.
|
|
161
|
+
//
|
|
162
|
+
// Stripping metadata is also why derivatives carry no EXIF GPS: the privacy
|
|
163
|
+
// win is a side effect of the default, not extra work.
|
|
164
|
+
const pipeline = sharp(source, {
|
|
165
|
+
limitInputPixels: MAX_INPUT_PIXELS,
|
|
166
|
+
// Decode in row order instead of random access. For a downscale — which
|
|
167
|
+
// is all this ever does — libvips can then stream the source rather than
|
|
168
|
+
// holding the whole decoded image, which is the single biggest reduction
|
|
169
|
+
// in peak memory available here.
|
|
170
|
+
sequentialRead: true
|
|
171
|
+
})
|
|
172
|
+
.rotate()
|
|
173
|
+
.resize({
|
|
174
|
+
width,
|
|
175
|
+
// Never scale up. A variant wider than the source is a bigger file
|
|
176
|
+
// that looks no better than the original.
|
|
177
|
+
withoutEnlargement: true,
|
|
178
|
+
fit: 'inside'
|
|
179
|
+
})[VARIANT_FORMAT]({ quality: config.quality });
|
|
180
|
+
const { data, info } = await pipeline.toBuffer({ resolveWithObject: true });
|
|
181
|
+
const stored = await storage.store({
|
|
182
|
+
buffer: data,
|
|
183
|
+
filename: key.split('/').pop() || key,
|
|
184
|
+
mimeType: `image/${VARIANT_FORMAT}`,
|
|
185
|
+
size: data.length,
|
|
186
|
+
key
|
|
187
|
+
});
|
|
188
|
+
const variant = {
|
|
189
|
+
w: info.width,
|
|
190
|
+
h: info.height,
|
|
191
|
+
key: stored.key,
|
|
192
|
+
path: stored.path,
|
|
193
|
+
url: buildVariantUrl(asset.id, width, configHash),
|
|
194
|
+
bytes: data.length
|
|
195
|
+
};
|
|
196
|
+
await recordVariant({ asset, variant, configHash, database });
|
|
197
|
+
return { variant, buffer: data };
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Merge one variant into the asset's record.
|
|
201
|
+
*
|
|
202
|
+
* Re-reads the row first because several widths of the same asset can be
|
|
203
|
+
* generated concurrently — writing a record built from the copy this request
|
|
204
|
+
* happened to load would drop whichever sibling finished in between. This is a
|
|
205
|
+
* read-modify-write and still races under true concurrency; the consequence is
|
|
206
|
+
* a lost *record*, not a lost file, and the next request for that width simply
|
|
207
|
+
* regenerates it. A durable fix belongs with the reference index.
|
|
208
|
+
*
|
|
209
|
+
* A failure here is logged, not thrown: the variant exists in storage and can
|
|
210
|
+
* be served, and failing the request because bookkeeping failed would be worse
|
|
211
|
+
* than serving the image and regenerating the record next time.
|
|
212
|
+
*/
|
|
213
|
+
async function recordVariant(opts) {
|
|
214
|
+
const { asset, variant, configHash, database } = opts;
|
|
215
|
+
try {
|
|
216
|
+
const fresh = (await database.findAssetById(asset.organizationId, asset.id)) ?? asset;
|
|
217
|
+
const current = getVariants(fresh);
|
|
218
|
+
// A record from a different config is replaced wholesale rather than
|
|
219
|
+
// merged — its entries address keys nothing will ever request again.
|
|
220
|
+
const kept = current && current.config === configHash
|
|
221
|
+
? current.widths.filter((v) => v.w !== variant.w)
|
|
222
|
+
: [];
|
|
223
|
+
const updated = await database.updateAsset(asset.organizationId, asset.id, {
|
|
224
|
+
metadata: {
|
|
225
|
+
...fresh.metadata,
|
|
226
|
+
variants: {
|
|
227
|
+
config: configHash,
|
|
228
|
+
generatedAt: new Date().toISOString(),
|
|
229
|
+
widths: [...kept, variant].sort((a, b) => a.w - b.w)
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
// Both relational adapters swallow write errors and return null, so a
|
|
234
|
+
// silent failure here would otherwise look like success and the variant
|
|
235
|
+
// would be regenerated on every single request.
|
|
236
|
+
if (!updated) {
|
|
237
|
+
cmsLogger.warn('[Images]', `Variant record not saved for asset ${asset.id} at w${variant.w}; it will be regenerated on the next request`);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
catch (error) {
|
|
241
|
+
cmsLogger.warn('[Images]', `Could not record variant for asset ${asset.id}:`, error);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/lib/images/index.ts"],"names":[],"mappings":"AAMA,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// Variant reading/selection only — pure, browser-safe, no Sharp.
|
|
2
|
+
//
|
|
3
|
+
// `./generate` is deliberately NOT re-exported here: it imports Sharp, and a
|
|
4
|
+
// barrel that pulled it in would drag a native image library into any chunk
|
|
5
|
+
// that touched this module, including the one `<Image>` ships to the browser.
|
|
6
|
+
// Server callers import it by path.
|
|
7
|
+
export * from './variants.js';
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { Asset, AssetVariant, AssetVariantRecord } from '../types/asset.js';
|
|
2
|
+
/**
|
|
3
|
+
* Reading and selecting image derivatives.
|
|
4
|
+
*
|
|
5
|
+
* Pure functions over an `Asset`, with no Sharp and no storage — so the
|
|
6
|
+
* `<Image>` component can import them in the browser. Generation lives in
|
|
7
|
+
* `./generate.ts`, which is server-only.
|
|
8
|
+
*/
|
|
9
|
+
/** The resolved image configuration a request is served under. */
|
|
10
|
+
export interface ImageConfig {
|
|
11
|
+
widths: number[];
|
|
12
|
+
quality: number;
|
|
13
|
+
}
|
|
14
|
+
export declare const DEFAULT_IMAGE_WIDTHS: number[];
|
|
15
|
+
export declare const DEFAULT_IMAGE_QUALITY = 80;
|
|
16
|
+
/**
|
|
17
|
+
* Normalise the user's `images` config, or return null when disabled.
|
|
18
|
+
*
|
|
19
|
+
* Widths are de-duplicated and sorted so that two configs listing the same
|
|
20
|
+
* widths in a different order hash identically — otherwise reordering the array
|
|
21
|
+
* in `aphex.config.ts` would silently orphan every previously generated
|
|
22
|
+
* derivative and regenerate the lot.
|
|
23
|
+
*/
|
|
24
|
+
export declare function resolveImageConfig(images: {
|
|
25
|
+
widths: number[];
|
|
26
|
+
quality?: number;
|
|
27
|
+
} | null | undefined): ImageConfig | null;
|
|
28
|
+
/** Hash identifying the config a derivative was generated under. */
|
|
29
|
+
export declare function configHashFor(config: ImageConfig): string;
|
|
30
|
+
/**
|
|
31
|
+
* The variant record for an asset, or null when there isn't a usable one.
|
|
32
|
+
*
|
|
33
|
+
* Validates rather than asserts: `metadata` is JSON that has been through a
|
|
34
|
+
* database and may predate this feature entirely, so a malformed or partial
|
|
35
|
+
* record must read as "no variants" and be regenerated, never crash a page
|
|
36
|
+
* render.
|
|
37
|
+
*/
|
|
38
|
+
export declare function getVariants(asset: Pick<Asset, 'metadata'>): AssetVariantRecord | null;
|
|
39
|
+
/**
|
|
40
|
+
* The already-generated derivative for an exact width, under the current config.
|
|
41
|
+
*
|
|
42
|
+
* A record generated under a different config is ignored: every variant URL
|
|
43
|
+
* embeds the config hash, so a stale record's URLs address files that are no
|
|
44
|
+
* longer referenced by anything.
|
|
45
|
+
*/
|
|
46
|
+
export declare function pickVariant(asset: Pick<Asset, 'metadata'>, width: number, configHash: string): AssetVariant | null;
|
|
47
|
+
/**
|
|
48
|
+
* The ladder widths worth offering for this asset.
|
|
49
|
+
*
|
|
50
|
+
* Widths at or above the original's own width are dropped — upscaling produces
|
|
51
|
+
* a larger file that looks no better. The original's width is always included
|
|
52
|
+
* as the top rung so a `srcset` still has something to offer above the last
|
|
53
|
+
* useful ladder step.
|
|
54
|
+
*/
|
|
55
|
+
/**
|
|
56
|
+
* Build a `srcset` for an asset, listing every ladder width worth offering.
|
|
57
|
+
*
|
|
58
|
+
* The URLs it names may not exist yet — that's the point of generate-on-miss.
|
|
59
|
+
* The browser requests one, `/media` produces it on the spot, and every later
|
|
60
|
+
* request for that width is a cache hit. So a `srcset` is correct the moment an
|
|
61
|
+
* asset is uploaded, with no generation having happened at all.
|
|
62
|
+
*/
|
|
63
|
+
export declare function buildSrcset(assetId: string, config: ImageConfig, configHash: string, originalWidth: number | null): string;
|
|
64
|
+
/**
|
|
65
|
+
* Whether derivatives can be produced for this asset at all.
|
|
66
|
+
*
|
|
67
|
+
* Lives here, next to the ladder, because two callers need the same answer: the
|
|
68
|
+
* server building a `srcset` and the admin client picking a thumbnail. A second
|
|
69
|
+
* copy of this rule would drift, and the failure is quiet — a thumbnail URL that
|
|
70
|
+
* falls back to a 14MB original renders perfectly and costs a hundred times what
|
|
71
|
+
* it should.
|
|
72
|
+
*/
|
|
73
|
+
export declare function canGenerateVariants(asset: Pick<Asset, 'assetType' | 'mimeType' | 'metadata'>): boolean;
|
|
74
|
+
/**
|
|
75
|
+
* The smallest derivative worth requesting for a thumbnail.
|
|
76
|
+
*
|
|
77
|
+
* A grid tile is a couple of hundred pixels; the bottom rung of the ladder is
|
|
78
|
+
* the right answer for it, and nothing about a tile justifies more.
|
|
79
|
+
*/
|
|
80
|
+
export declare function thumbnailWidth(config: ImageConfig, originalWidth: number | null): number;
|
|
81
|
+
export declare function usableWidths(config: ImageConfig, originalWidth: number | null): number[];
|
|
82
|
+
//# sourceMappingURL=variants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"variants.d.ts","sourceRoot":"","sources":["../../src/lib/images/variants.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAG9E;;;;;;GAMG;AAEH,kEAAkE;AAClE,MAAM,WAAW,WAAW;IAC3B,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;CAChB;AAED,eAAO,MAAM,oBAAoB,UAA8B,CAAC;AAChE,eAAO,MAAM,qBAAqB,KAAK,CAAC;AAExC;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CACjC,MAAM,EAAE;IAAE,MAAM,EAAE,MAAM,EAAE,CAAC;IAAC,OAAO,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,GAAG,SAAS,GAC/D,WAAW,GAAG,IAAI,CAiBpB;AAED,oEAAoE;AACpE,wBAAgB,aAAa,CAAC,MAAM,EAAE,WAAW,GAAG,MAAM,CAEzD;AAED;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,UAAU,CAAC,GAAG,kBAAkB,GAAG,IAAI,CAkBrF;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAC1B,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,UAAU,CAAC,EAC9B,KAAK,EAAE,MAAM,EACb,UAAU,EAAE,MAAM,GAChB,YAAY,GAAG,IAAI,CAIrB;AAED;;;;;;;GAOG;AACH;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAC1B,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,WAAW,EACnB,UAAU,EAAE,MAAM,EAClB,aAAa,EAAE,MAAM,GAAG,IAAI,GAC1B,MAAM,CAIR;AAED;;;;;;;;GAQG;AACH,wBAAgB,mBAAmB,CAClC,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,WAAW,GAAG,UAAU,GAAG,UAAU,CAAC,GACvD,OAAO,CAOT;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,GAAG,IAAI,GAAG,MAAM,CAExF;AAED,wBAAgB,YAAY,CAAC,MAAM,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,GAAG,IAAI,GAAG,MAAM,EAAE,CAIxF"}
|