@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.
Files changed (156) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +24 -2
  3. package/dist/chunks/bun-index-n7xnn0a2.js +502 -0
  4. package/dist/src/adapters/angular.js +275 -0
  5. package/dist/src/adapters/html.js +70 -0
  6. package/dist/src/adapters/preact.js +82 -0
  7. package/dist/src/adapters/react.js +84 -0
  8. package/dist/src/adapters/solid.js +93 -0
  9. package/dist/src/adapters/svelte.js +15 -0
  10. package/dist/src/adapters/vue.js +147 -0
  11. package/dist/src/index.js +1442 -0
  12. package/dist/src/server/index.js +9 -0
  13. package/dist/types/src/adapters/angular-img.d.ts +43 -0
  14. package/dist/types/src/adapters/angular-picture.d.ts +39 -0
  15. package/dist/types/src/adapters/angular.d.ts +3 -0
  16. package/dist/types/src/adapters/html-img.d.ts +10 -0
  17. package/dist/types/src/adapters/html-picture.d.ts +6 -0
  18. package/dist/types/src/adapters/html.d.ts +6 -0
  19. package/dist/types/src/adapters/preact-img.d.ts +8 -0
  20. package/dist/types/src/adapters/preact-picture.d.ts +6 -0
  21. package/dist/types/src/adapters/preact.d.ts +6 -0
  22. package/dist/types/src/adapters/react-img.d.ts +8 -0
  23. package/dist/types/src/adapters/react-picture.d.ts +6 -0
  24. package/dist/types/src/adapters/react.d.ts +6 -0
  25. package/dist/types/src/adapters/solid-img.d.ts +8 -0
  26. package/dist/types/src/adapters/solid-picture.d.ts +6 -0
  27. package/dist/types/src/adapters/solid.d.ts +6 -0
  28. package/dist/types/src/adapters/svelte.d.ts +3 -0
  29. package/dist/types/src/adapters/vue-img.d.ts +119 -0
  30. package/dist/types/src/adapters/vue-picture.d.ts +121 -0
  31. package/dist/types/src/adapters/vue.d.ts +234 -0
  32. package/dist/types/src/build/endpoint.d.ts +12 -0
  33. package/dist/types/src/build/platform.d.ts +24 -0
  34. package/dist/types/src/build/runtime.d.ts +9 -0
  35. package/dist/types/src/build/types.d.ts +4 -0
  36. package/dist/types/src/build/variants.d.ts +8 -0
  37. package/dist/types/src/domains.d.ts +27 -0
  38. package/dist/types/src/index.d.ts +9 -0
  39. package/dist/types/src/module.d.ts +3 -0
  40. package/dist/types/src/options.d.ts +15 -0
  41. package/dist/types/src/providers/bunny.d.ts +9 -0
  42. package/dist/types/src/providers/cloudflare.d.ts +9 -0
  43. package/dist/types/src/providers/cloudflareImages.d.ts +9 -0
  44. package/dist/types/src/providers/cloudinary.d.ts +9 -0
  45. package/dist/types/src/providers/contentful.d.ts +9 -0
  46. package/dist/types/src/providers/fastly.d.ts +9 -0
  47. package/dist/types/src/providers/hamur-url.d.ts +3 -0
  48. package/dist/types/src/providers/hamur.d.ts +3 -0
  49. package/dist/types/src/providers/imagekit.d.ts +9 -0
  50. package/dist/types/src/providers/imgix.d.ts +9 -0
  51. package/dist/types/src/providers/imgproxy.d.ts +9 -0
  52. package/dist/types/src/providers/ipx.d.ts +9 -0
  53. package/dist/types/src/providers/netlify.d.ts +9 -0
  54. package/dist/types/src/providers/none.d.ts +8 -0
  55. package/dist/types/src/providers/sanity.d.ts +9 -0
  56. package/dist/types/src/providers/shared.d.ts +7 -0
  57. package/dist/types/src/providers/storyblok.d.ts +9 -0
  58. package/dist/types/src/providers/strapi.d.ts +9 -0
  59. package/dist/types/src/providers/supabase.d.ts +9 -0
  60. package/dist/types/src/providers/twicpics.d.ts +9 -0
  61. package/dist/types/src/providers/uploadcare.d.ts +9 -0
  62. package/dist/types/src/providers/vercel-url.d.ts +3 -0
  63. package/dist/types/src/providers/vercel.d.ts +3 -0
  64. package/dist/types/src/providers/weserv.d.ts +9 -0
  65. package/dist/types/src/runtime/image.d.ts +28 -0
  66. package/dist/types/src/runtime/img.d.ts +71 -0
  67. package/dist/types/src/runtime/index.d.ts +9 -0
  68. package/dist/types/src/runtime/picture.d.ts +24 -0
  69. package/dist/types/src/runtime/sizes.d.ts +8 -0
  70. package/dist/types/src/runtime/source.d.ts +3 -0
  71. package/dist/types/src/server/door.d.ts +1 -0
  72. package/dist/types/src/server/endpoint.d.ts +46 -0
  73. package/dist/types/src/server/handler.d.ts +1 -0
  74. package/dist/types/src/server/index.d.ts +1 -0
  75. package/dist/types/src/server/miss-handler.d.ts +1 -0
  76. package/dist/types/src/server/miss.d.ts +1 -0
  77. package/dist/types/src/server/parse.d.ts +30 -0
  78. package/dist/types/src/server/remote.d.ts +20 -0
  79. package/dist/types/src/types.d.ts +178 -0
  80. package/package.json +141 -4
  81. package/src/adapters/Img.svelte +33 -0
  82. package/src/adapters/Picture.svelte +39 -0
  83. package/src/adapters/angular-img.ts +143 -0
  84. package/src/adapters/angular-picture.ts +172 -0
  85. package/src/adapters/angular.ts +3 -0
  86. package/src/adapters/html-img.ts +42 -0
  87. package/src/adapters/html-picture.ts +30 -0
  88. package/src/adapters/html.ts +9 -0
  89. package/src/adapters/preact-img.ts +51 -0
  90. package/src/adapters/preact-picture.ts +40 -0
  91. package/src/adapters/preact.ts +9 -0
  92. package/src/adapters/react-img.ts +54 -0
  93. package/src/adapters/react-picture.ts +49 -0
  94. package/src/adapters/react.ts +9 -0
  95. package/src/adapters/solid-img.ts +47 -0
  96. package/src/adapters/solid-picture.ts +43 -0
  97. package/src/adapters/solid.ts +9 -0
  98. package/src/adapters/svelte.ts +3 -0
  99. package/src/adapters/test/ImgCustomPage.svelte +14 -0
  100. package/src/adapters/test/ImgPage.svelte +5 -0
  101. package/src/adapters/test/ImgPreloadPage.svelte +6 -0
  102. package/src/adapters/test/PicturePage.svelte +6 -0
  103. package/src/adapters/test/PicturePreloadPage.svelte +5 -0
  104. package/src/adapters/test/support.ts +116 -0
  105. package/src/adapters/vue-img.ts +72 -0
  106. package/src/adapters/vue-picture.ts +64 -0
  107. package/src/adapters/vue.ts +7 -0
  108. package/src/build/endpoint.ts +78 -0
  109. package/src/build/platform.ts +99 -0
  110. package/src/build/runtime.ts +118 -0
  111. package/src/build/types.ts +23 -0
  112. package/src/build/variants.ts +174 -0
  113. package/src/domains.ts +84 -0
  114. package/src/env.d.ts +24 -0
  115. package/src/index.ts +23 -0
  116. package/src/module.ts +29 -0
  117. package/src/options.ts +329 -0
  118. package/src/providers/bunny.ts +21 -0
  119. package/src/providers/cloudflare.ts +22 -0
  120. package/src/providers/cloudflareImages.ts +25 -0
  121. package/src/providers/cloudinary.ts +31 -0
  122. package/src/providers/contentful.ts +26 -0
  123. package/src/providers/fastly.ts +29 -0
  124. package/src/providers/hamur-url.ts +81 -0
  125. package/src/providers/hamur.ts +8 -0
  126. package/src/providers/imagekit.ts +30 -0
  127. package/src/providers/imgix.ts +26 -0
  128. package/src/providers/imgproxy.ts +24 -0
  129. package/src/providers/ipx.ts +26 -0
  130. package/src/providers/netlify.ts +23 -0
  131. package/src/providers/none.ts +9 -0
  132. package/src/providers/sanity.ts +29 -0
  133. package/src/providers/shared.ts +17 -0
  134. package/src/providers/storyblok.ts +24 -0
  135. package/src/providers/strapi.ts +24 -0
  136. package/src/providers/supabase.ts +26 -0
  137. package/src/providers/twicpics.ts +26 -0
  138. package/src/providers/uploadcare.ts +32 -0
  139. package/src/providers/vercel-url.ts +18 -0
  140. package/src/providers/vercel.ts +8 -0
  141. package/src/providers/weserv.ts +25 -0
  142. package/src/runtime/image.ts +266 -0
  143. package/src/runtime/img.ts +241 -0
  144. package/src/runtime/index.ts +18 -0
  145. package/src/runtime/picture.ts +62 -0
  146. package/src/runtime/sizes.ts +52 -0
  147. package/src/runtime/source.ts +8 -0
  148. package/src/server/door.ts +1 -0
  149. package/src/server/endpoint.ts +428 -0
  150. package/src/server/handler.ts +33 -0
  151. package/src/server/index.ts +1 -0
  152. package/src/server/miss-handler.ts +20 -0
  153. package/src/server/miss.ts +1 -0
  154. package/src/server/parse.ts +152 -0
  155. package/src/server/remote.ts +166 -0
  156. package/src/types.ts +177 -0
@@ -0,0 +1,71 @@
1
+ import type { ImageMeta } from "hamur";
2
+ import type { HeadInput } from "hamur/app";
3
+ import type { ImageFit, ImageFormat, ImageModifiers, HamurImagePreset, HamurImageProvider } from "../types.ts";
4
+ import type { ImageApi } from "./image.ts";
5
+ /** The props every adapter's `Img` takes besides the `<img>` attributes it passes through. */
6
+ export interface ImgOptions {
7
+ src: string | ImageMeta;
8
+ /** `""` for a decorative image. */
9
+ alt: string;
10
+ width?: number | `${number}`;
11
+ height?: number | `${number}`;
12
+ sizes?: string;
13
+ densities?: number[];
14
+ format?: ImageFormat;
15
+ quality?: number;
16
+ fit?: ImageFit;
17
+ modifiers?: ImageModifiers;
18
+ provider?: HamurImageProvider;
19
+ preset?: HamurImagePreset;
20
+ loading?: "lazy" | "eager";
21
+ /** The LCP image; `loading="eager"`, `fetchpriority="high"` unless set, and a `<link rel="preload" as="image">` in the head. */
22
+ preload?: boolean;
23
+ /** A URL or data URL painted as the `<img>`'s background until it loads (`import blur from "./hero.jpg?placeholder"`). */
24
+ placeholder?: string;
25
+ /** A class on the `<img>` until it loads. */
26
+ placeholderClass?: string;
27
+ }
28
+ export type ImgAttributes = (props: object) => Record<string, unknown>;
29
+ /** The attributes of an `<img>` in the order `src srcset sizes width height alt loading decoding fetchpriority`, then the props that are not `Img`'s own. `fetchpriority` is lower-case here; React's adapter renames it. */
30
+ export declare function createImgAttributes(api: Pick<ImageApi, "getSizes">, warn?: (message: string) => void): ImgAttributes;
31
+ /** The style the placeholder paints, in CSS property names. */
32
+ export declare function placeholderStyle(url: string): Record<string, string>;
33
+ export interface PlaceholderTarget {
34
+ /** React: `className` and an object `style` with camel-case names. */
35
+ react?: boolean;
36
+ }
37
+ /**
38
+ * `attributes` with the placeholder's `style` and class added until the image has loaded. The app's own style wins over the placeholder's;
39
+ * a string style stays a string and an object stays an object (React always takes an object).
40
+ */
41
+ export declare function applyPlaceholder(attributes: Record<string, unknown>, props: {
42
+ placeholder?: unknown;
43
+ placeholderClass?: unknown;
44
+ }, loaded: boolean, target?: PlaceholderTarget): Record<string, unknown>;
45
+ export type ImagePreloadLink = NonNullable<HeadInput["link"]>[number];
46
+ /** The `<link rel="preload" as="image">` of an image's attributes, keyed `image:<href>` so two `Img` of one source give one link. `source` is a `Picture`'s first `<source>`: its `srcset`, `sizes` and `type` replace the `<img>`'s. */
47
+ export declare function imagePreloadLink(attributes: Record<string, unknown>, source?: {
48
+ type: string;
49
+ srcset: string;
50
+ sizes?: string;
51
+ }): ImagePreloadLink;
52
+ /** What a `custom` render receives. `imgAttrs` is spread on the app's own `<img>`; in a client library it also holds that library's `load` and `error` listeners and a ref. */
53
+ export interface ImgState<Attributes extends object = Record<string, unknown>> {
54
+ imgAttrs: Attributes;
55
+ src: string;
56
+ isLoaded: boolean;
57
+ }
58
+ export interface ImgEvents {
59
+ load(event: Event): void;
60
+ error(event: Event): void;
61
+ /** Fires `load` or `error` on an image the browser finished before the listeners existed. */
62
+ replay(image: HTMLImageElement): void;
63
+ }
64
+ /**
65
+ * The listeners of one `<img>`: each calls the app's own handler once per `src`, so an image that the browser finished while the page was
66
+ * still hydrating (no event) and one whose event is still on its way (a fresh mount) both fire exactly once.
67
+ */
68
+ export declare function createImgEvents(handlers: () => {
69
+ load?: (event: Event) => void;
70
+ error?: (event: Event) => void;
71
+ }, loaded: (value: boolean) => void): ImgEvents;
@@ -0,0 +1,9 @@
1
+ import { type ImageApi } from "./image.ts";
2
+ export declare const getImage: ImageApi["getImage"];
3
+ export declare const getSizes: ImageApi["getSizes"];
4
+ export declare const imgAttributes: import("./img.ts").ImgAttributes;
5
+ export declare const pictureAttributes: import("./picture.ts").PictureAttributes;
6
+ export type { PictureAttributes, PictureOptions, PictureParts } from "./picture.ts";
7
+ export type { ImgAttributes, ImgOptions } from "./img.ts";
8
+ export { imagePreloadLink, type ImagePreloadLink } from "./img.ts";
9
+ export { picturePreloadLink } from "./picture.ts";
@@ -0,0 +1,24 @@
1
+ import type { ImageFormat, ImageProvider, ImageRuntimeOptions } from "../types.ts";
2
+ import { type ImagePreloadLink, type ImgAttributes, type ImgOptions } from "./img.ts";
3
+ /** `Img`'s props, with a list of formats for the `<source>` elements and the format of the `<img>`. */
4
+ export type PictureOptions = Omit<ImgOptions, "format"> & {
5
+ format?: ImageFormat | ImageFormat[];
6
+ legacyFormat?: ImageFormat;
7
+ };
8
+ export interface PictureParts {
9
+ sources: {
10
+ type: string;
11
+ srcset: string;
12
+ sizes?: string;
13
+ }[];
14
+ img: Record<string, unknown>;
15
+ }
16
+ export type PictureAttributes = (props: object) => PictureParts;
17
+ export interface PictureEnvironment {
18
+ options: Pick<ImageRuntimeOptions, "provider" | "presets" | "format">;
19
+ providers: Record<string, ImageProvider>;
20
+ }
21
+ /** The preload link of a `Picture`: its first `<source>` with `type`, or the `<img>` when there is none. */
22
+ export declare function picturePreloadLink(parts: PictureParts): ImagePreloadLink;
23
+ /** The `<source>` attributes and the `<img>` attributes of a `<picture>`; every adapter only renders them. */
24
+ export declare function createPictureAttributes(img: ImgAttributes, env: PictureEnvironment): PictureAttributes;
@@ -0,0 +1,8 @@
1
+ export interface SizesPlan {
2
+ /** The `sizes` attribute. */
3
+ attribute: string;
4
+ /** The slot width in CSS pixels of each range, ascending by range. */
5
+ slots: number[];
6
+ }
7
+ /** `[<screen>:]<size>` entries; the attribute lists the conditions largest first and each slot is the widest viewport of its range times its size. */
8
+ export declare function planSizes(sizes: string, screens: Record<string, number>): SizesPlan;
@@ -0,0 +1,3 @@
1
+ export declare const REMOTE: RegExp;
2
+ /** A local `src` as the path below the source dir: no `app.baseURL`, query, hash or leading slash. */
3
+ export declare function localPath(src: string, baseURL: string): string;
@@ -0,0 +1 @@
1
+ export { imageEndpoint } from "./handler.ts";
@@ -0,0 +1,46 @@
1
+ import type { ImageBlobStore, ImageEndpointOptions, ImageRuntimeOptions, ImageSources } from "../types.ts";
2
+ import { type ImageRefusal, type ImageRequest } from "./parse.ts";
3
+ import { type RemoteEnvironment } from "./remote.ts";
4
+ export interface SourceSize {
5
+ width: number;
6
+ height: number;
7
+ }
8
+ /** Decodes `bytes`, applies the request and encodes it in the request's output format. */
9
+ export type ImageTransform = (bytes: Uint8Array<ArrayBuffer>, request: ImageRequest, source: SourceSize, maxPixels: number) => Promise<Uint8Array>;
10
+ export interface ImageCache {
11
+ getItemRaw(key: string): Promise<Uint8Array | undefined>;
12
+ setItemRaw(key: string, value: Uint8Array): Promise<void>;
13
+ }
14
+ export interface EndpointEnvironment {
15
+ options: Pick<ImageRuntimeOptions, "baseURL" | "widths" | "domains" | "alias">;
16
+ /** `#hamur/image/sources`; a built server answers only the files it lists. */
17
+ sources: ImageSources;
18
+ endpoint: ImageEndpointOptions;
19
+ /** The bytes of a file below the source dir (`path` has no leading `/`), `undefined` when there is none. */
20
+ readSource(path: string): Promise<Uint8Array | undefined>;
21
+ /** The bytes of an asset URL below `app.baseURL` (`_hamur/<name>`), `undefined` when there is none. */
22
+ readAsset(path: string): Promise<Uint8Array | undefined>;
23
+ blob?: ImageBlobStore;
24
+ cache(): ImageCache;
25
+ transform?: ImageTransform;
26
+ /** Told once when a result cannot be written to the cache; the answer is sent anyway. */
27
+ warn?: (message: string) => void;
28
+ /** Every address of a remote host; default `Bun.dns.lookup`. */
29
+ lookup?: RemoteEnvironment["lookup"];
30
+ /** Default `fetch`; called with the checked address in the URL. */
31
+ fetch?: RemoteEnvironment["fetch"];
32
+ /** Milliseconds a remote fetch may take; default 10 000. */
33
+ remoteTimeout?: number;
34
+ /** Milliseconds one address of several may take to answer; default 2 500. */
35
+ remoteConnectTimeout?: number;
36
+ }
37
+ /** The container of `bytes` by its magic number; `undefined` for anything else. */
38
+ export declare function sniffImage(bytes: Uint8Array): string | undefined;
39
+ /** Under `hamur dev`: the first of `dirs` that holds `path`, opened as a `BunFile` from segments the parser checked, never a request string. */
40
+ export declare function readFromDirs(dirs: readonly string[]): (path: string) => Promise<Uint8Array | undefined>;
41
+ /** The header of a source the endpoint transforms, or why it does not. */
42
+ export declare function checkSource(bytes: Uint8Array, maxPixels: number): SourceSize | ImageRefusal;
43
+ /** With `Bun.Image`: `rot`, `flip`, `flop`, then the resize, then the encoder of the extension. */
44
+ export declare const transformImage: ImageTransform;
45
+ /** The `/_hamur/image/**` answer. Every 200 is bytes this endpoint encoded, never a source's own. */
46
+ export declare function createImageEndpoint(env: EndpointEnvironment): (request: Request) => Promise<Response>;
@@ -0,0 +1 @@
1
+ export declare const imageEndpoint: import("hamur/server").Handler<unknown, Response, unknown, Response, {}>;
@@ -0,0 +1 @@
1
+ export { getImage, getSizes } from "../runtime/index.ts";
@@ -0,0 +1 @@
1
+ export declare const imageMiss: import("hamur/server").Handler<unknown, Response, unknown, Response, {}>;
@@ -0,0 +1 @@
1
+ export { imageMiss } from "./miss-handler.ts";
@@ -0,0 +1,30 @@
1
+ export type OutputExtension = "jpg" | "png" | "webp";
2
+ export interface ImageRequest {
3
+ /** The modifier segment as written, which the parser checked is the one spelling. */
4
+ modifiers: string;
5
+ fit?: "fill" | "inside" | "outside";
6
+ flip: boolean;
7
+ flop: boolean;
8
+ h?: number;
9
+ q?: number;
10
+ rot?: 90 | 180 | 270;
11
+ v?: string;
12
+ w?: number;
13
+ /** A local source: the decoded path below the source dir, without the leading `/`. */
14
+ path?: string;
15
+ /** A remote source: `https:` or `http:`, its host and its path as written. */
16
+ remote?: {
17
+ protocol: "https:" | "http:";
18
+ host: string;
19
+ path: string;
20
+ };
21
+ extension: OutputExtension;
22
+ }
23
+ export interface ImageRefusal {
24
+ status: number;
25
+ message: string;
26
+ }
27
+ export declare const refuse: (status: number, message: string) => ImageRefusal;
28
+ export declare const isRefusal: (value: unknown) => value is ImageRefusal;
29
+ /** `<modifiers>/<source>.<extension>`, the part of the path after `_hamur/image/`, still percent-encoded. */
30
+ export declare function parseImagePath(rest: string): ImageRequest | ImageRefusal;
@@ -0,0 +1,20 @@
1
+ import { type ImageRefusal } from "./parse.ts";
2
+ export interface RemoteEnvironment {
3
+ /** True when `image.domains` or an alias target allows `url`. */
4
+ allowed(url: URL): boolean;
5
+ /** Every address of `hostname`. */
6
+ lookup(hostname: string): Promise<string[]>;
7
+ fetch(url: string, init: BunFetchRequestInit): Promise<Response>;
8
+ maxBytes: number;
9
+ /** Milliseconds for the whole fetch: lookups, redirects and body. */
10
+ timeout: number;
11
+ /** Milliseconds one address may take to answer while another is left to try. */
12
+ connectTimeout: number;
13
+ }
14
+ export interface RemoteImage {
15
+ bytes: Uint8Array;
16
+ /** The final answer's strong `etag`; `undefined` for a weak one or none. */
17
+ etag?: string;
18
+ }
19
+ /** `start` fetched at an address it checked, following at most 3 redirects, each target checked again, and read up to `maxBytes`. */
20
+ export declare function fetchRemote(start: URL, env: RemoteEnvironment): Promise<RemoteImage | ImageRefusal>;
@@ -0,0 +1,178 @@
1
+ export type ImageFormat = "avif" | "webp" | "jpeg" | "png";
2
+ export type ImageFit = "cover" | "contain" | "fill" | "inside" | "outside";
3
+ /** The provider-neutral transform request. */
4
+ export interface ImageModifiers {
5
+ /** CSS pixels times density, rounded up to `image.widths`. */
6
+ width?: number;
7
+ /** Sent only with `fit`. */
8
+ height?: number;
9
+ fit?: ImageFit;
10
+ format?: ImageFormat;
11
+ /** 1 to 100. */
12
+ quality?: number;
13
+ rotate?: 90 | 180 | 270;
14
+ /** Mirror about the x-axis. */
15
+ flip?: boolean;
16
+ /** Mirror about the y-axis. */
17
+ flop?: boolean;
18
+ /** A provider's own keys. */
19
+ [provider: string]: string | number | boolean | undefined;
20
+ }
21
+ export interface ImageProviderContext {
22
+ /** After the modifier rules are applied. */
23
+ modifiers: ImageModifiers;
24
+ /** `image.providers.<name>.options`. */
25
+ options: Record<string, unknown>;
26
+ /** `app.baseURL` with one trailing slash. */
27
+ baseURL: string;
28
+ }
29
+ /** Turns a source and image modifiers into a URL. */
30
+ export interface ImageProvider {
31
+ /** The output formats a URL can ask for; `[]` when the service only negotiates. */
32
+ formats: readonly ImageFormat[];
33
+ /** True when omitting the format makes the service pick one from `Accept`. */
34
+ negotiates: boolean;
35
+ /** The fits it makes; others degrade. */
36
+ fits: readonly ImageFit[];
37
+ /** The transforms beyond width, height, fit, format and quality it takes: `rotate`, `flip`, `flop` and its own keys. */
38
+ modifiers: readonly string[];
39
+ getImage(src: string, ctx: ImageProviderContext): string;
40
+ }
41
+ /** Provider name → `true`, filled by typegen. */
42
+ export interface HamurImageProviders {
43
+ }
44
+ /** Preset name → `true`, filled by typegen. */
45
+ export interface HamurImagePresets {
46
+ }
47
+ /** A provider the build bundles; `string` while typegen has not run. */
48
+ export type HamurImageProvider = keyof HamurImageProviders extends never ? string : keyof HamurImageProviders & string;
49
+ /** A preset of `image.presets`; `string` while typegen has not run. */
50
+ export type HamurImagePreset = keyof HamurImagePresets extends never ? string : keyof HamurImagePresets & string;
51
+ export interface ImageSizes {
52
+ src: string;
53
+ srcset?: string;
54
+ sizes?: string;
55
+ width?: number;
56
+ height?: number;
57
+ }
58
+ export interface ImageProviderConfig {
59
+ /** A built-in provider name (default: the key) or the path of a custom provider file. */
60
+ provider?: string;
61
+ options?: Record<string, unknown>;
62
+ }
63
+ export interface ImagePreset {
64
+ provider?: string;
65
+ modifiers: ImageModifiers;
66
+ }
67
+ export interface ImageOptions {
68
+ /** `"auto"` (default), `"hamur"`, `"none"`, a built-in provider or a key of `providers`. */
69
+ provider?: string;
70
+ providers?: Record<string, ImageProviderConfig>;
71
+ presets?: Record<string, ImagePreset>;
72
+ /** Breakpoint names of `sizes`, in CSS pixels; default `{ sm: 640, md: 768, lg: 1024, xl: 1280, "2xl": 1536 }`. */
73
+ screens?: Record<string, number>;
74
+ /** Pixel densities, each from 1 to 4; default `[1, 2]`. */
75
+ densities?: number[];
76
+ /** The allowed widths; default every screen times every density, plus 64, 128, 256, 384. */
77
+ widths?: number[];
78
+ /** Output formats in order of preference; default `["webp"]`. */
79
+ format?: ImageFormat[];
80
+ /** Default `quality` modifier, 1 to 100; unset sends none. */
81
+ quality?: number;
82
+ /** Remote sources allowed: host patterns (`https` only) or URL patterns. */
83
+ domains?: string[];
84
+ /** A `src` starting with a key is that URL plus the rest. */
85
+ alias?: Record<string, string>;
86
+ /** Where local sources are read from; default the public dirs. */
87
+ dir?: string;
88
+ maxPixels?: number;
89
+ maxBytes?: number;
90
+ prerender?: string[];
91
+ sources?: {
92
+ blob?: {
93
+ prefix: string;
94
+ };
95
+ };
96
+ }
97
+ export interface ResolvedProvider {
98
+ /** The built-in provider's name; absent for a custom file. */
99
+ builtIn?: string;
100
+ /** The custom provider's file as written in the config. */
101
+ file?: string;
102
+ options: Record<string, unknown>;
103
+ }
104
+ export interface ResolvedImageOptions {
105
+ provider: string;
106
+ providers: Record<string, ResolvedProvider>;
107
+ presets: Record<string, ImagePreset>;
108
+ screens: Record<string, number>;
109
+ densities: number[];
110
+ widths: number[];
111
+ format: ImageFormat[];
112
+ quality?: number;
113
+ domains: string[];
114
+ alias: Record<string, string>;
115
+ /** Absolute directories local sources are read from, highest priority first. */
116
+ dirs: string[];
117
+ maxPixels: number;
118
+ maxBytes: number;
119
+ prerender: string[];
120
+ sources: {
121
+ blob?: {
122
+ prefix: string;
123
+ };
124
+ };
125
+ }
126
+ declare const resolvedName: unique symbol;
127
+ /** A provider name `auto` has been resolved to; `resolveProvider` makes it. */
128
+ export type ImageProviderName = string & {
129
+ readonly [resolvedName]: true;
130
+ };
131
+ /** The options the build's setup steps take: `provider` is never `auto`. */
132
+ export interface ImageBuildOptions extends Omit<ResolvedImageOptions, "provider"> {
133
+ provider: ImageProviderName;
134
+ }
135
+ /** What `#hamur/image/options` holds: the part of the resolved options a URL needs. */
136
+ export interface ImageRuntimeOptions {
137
+ baseURL: string;
138
+ /** Never `auto`: the build wrote the name it resolved to. */
139
+ provider: string;
140
+ providers: Record<string, {
141
+ file?: string;
142
+ options: Record<string, unknown>;
143
+ }>;
144
+ presets: Record<string, ImagePreset>;
145
+ screens: Record<string, number>;
146
+ densities: number[];
147
+ widths: number[];
148
+ format: ImageFormat[];
149
+ quality?: number;
150
+ domains: string[];
151
+ alias: Record<string, string>;
152
+ /** Where the `hamur` provider transforms at build, which reads no blob: `providers` are the keys naming it. */
153
+ blobUntransformed?: {
154
+ prefix: string;
155
+ providers: string[];
156
+ };
157
+ }
158
+ /** `#hamur/image/sources`: `"/<path>"` to `[width, height, format, v, animated?]`. */
159
+ export type ImageSources = Record<string, [width: number, height: number, format: string, v: string, animated?: true]>;
160
+ /** What `#hamur/image/endpoint` holds: the endpoint's own settings, written where transforms run at run time. */
161
+ export interface ImageEndpointOptions {
162
+ /** `hamur dev`: sources are read live from `dirs` and answered `no-cache`. */
163
+ dev: boolean;
164
+ /** The absolute source dirs, under `hamur dev` only. */
165
+ dirs: string[];
166
+ /** `image.dir` outside the public dirs: a built server reads sources from the `assets:image` mount. */
167
+ assets: boolean;
168
+ maxPixels: number;
169
+ /** The most bytes read from a remote source. */
170
+ maxBytes: number;
171
+ /** `image.sources.blob.prefix`. */
172
+ blobPrefix?: string;
173
+ }
174
+ /** Declared here, since `@hamur/image` does not depend on `@hamur/blob`. */
175
+ export interface ImageBlobStore {
176
+ get(pathname: string): Promise<Blob | undefined>;
177
+ }
178
+ export {};
package/package.json CHANGED
@@ -1,6 +1,143 @@
1
1
  {
2
2
  "name": "@hamur/image",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.0.2",
4
+ "type": "module",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/productdevbook/hamur.git",
9
+ "directory": "packages/image"
10
+ },
11
+ "exports": {
12
+ ".": {
13
+ "types": "./dist/types/src/index.d.ts",
14
+ "bun": "./src/index.ts",
15
+ "hamur-source": "./src/index.ts",
16
+ "node": "./dist/src/index.js",
17
+ "default": "./src/index.ts"
18
+ },
19
+ "./server": {
20
+ "types": "./dist/types/src/server/index.d.ts",
21
+ "bun": "./src/server/index.ts",
22
+ "hamur-source": "./src/server/index.ts",
23
+ "node": "./dist/src/server/index.js",
24
+ "default": "./src/server/index.ts"
25
+ },
26
+ "./react": {
27
+ "types": "./dist/types/src/adapters/react.d.ts",
28
+ "bun": "./src/adapters/react.ts",
29
+ "hamur-source": "./src/adapters/react.ts",
30
+ "node": "./dist/src/adapters/react.js",
31
+ "default": "./src/adapters/react.ts"
32
+ },
33
+ "./vue": {
34
+ "types": "./dist/types/src/adapters/vue.d.ts",
35
+ "bun": "./src/adapters/vue.ts",
36
+ "hamur-source": "./src/adapters/vue.ts",
37
+ "node": "./dist/src/adapters/vue.js",
38
+ "default": "./src/adapters/vue.ts"
39
+ },
40
+ "./preact": {
41
+ "types": "./dist/types/src/adapters/preact.d.ts",
42
+ "bun": "./src/adapters/preact.ts",
43
+ "hamur-source": "./src/adapters/preact.ts",
44
+ "node": "./dist/src/adapters/preact.js",
45
+ "default": "./src/adapters/preact.ts"
46
+ },
47
+ "./svelte": {
48
+ "types": "./dist/types/src/adapters/svelte.d.ts",
49
+ "bun": "./src/adapters/svelte.ts",
50
+ "hamur-source": "./src/adapters/svelte.ts",
51
+ "node": "./dist/src/adapters/svelte.js",
52
+ "default": "./src/adapters/svelte.ts"
53
+ },
54
+ "./solid": {
55
+ "types": "./dist/types/src/adapters/solid.d.ts",
56
+ "bun": "./src/adapters/solid.ts",
57
+ "hamur-source": "./src/adapters/solid.ts",
58
+ "node": "./dist/src/adapters/solid.js",
59
+ "default": "./src/adapters/solid.ts"
60
+ },
61
+ "./html": {
62
+ "types": "./dist/types/src/adapters/html.d.ts",
63
+ "bun": "./src/adapters/html.ts",
64
+ "hamur-source": "./src/adapters/html.ts",
65
+ "node": "./dist/src/adapters/html.js",
66
+ "default": "./src/adapters/html.ts"
67
+ },
68
+ "./angular": {
69
+ "types": "./dist/types/src/adapters/angular.d.ts",
70
+ "bun": "./src/adapters/angular.ts",
71
+ "hamur-source": "./src/adapters/angular.ts",
72
+ "node": "./dist/src/adapters/angular.js",
73
+ "default": "./src/adapters/angular.ts"
74
+ },
75
+ "./package.json": "./package.json"
76
+ },
77
+ "files": [
78
+ "dist",
79
+ "src"
80
+ ],
81
+ "dependencies": {},
82
+ "peerDependencies": {
83
+ "hamur": "~0.0.2",
84
+ "@hamur/adapter-react": "^0.0.2",
85
+ "@hamur/adapter-vue": "^0.0.2",
86
+ "@hamur/adapter-preact": "^0.0.2",
87
+ "@hamur/adapter-svelte": "^0.0.2",
88
+ "@hamur/adapter-solid": "^0.0.2",
89
+ "@hamur/adapter-html": "^0.0.2",
90
+ "@hamur/adapter-angular": "^0.0.2",
91
+ "react": "^19.3.1",
92
+ "preact": "^11.0.2",
93
+ "vue": "^3.5.44",
94
+ "svelte": "^5.57.2",
95
+ "solid-js": "^2.0.4",
96
+ "@solidjs/web": "^2.0.4",
97
+ "@angular/core": "^22.3.0"
98
+ },
99
+ "peerDependenciesMeta": {
100
+ "@hamur/adapter-react": {
101
+ "optional": true
102
+ },
103
+ "@hamur/adapter-vue": {
104
+ "optional": true
105
+ },
106
+ "@hamur/adapter-preact": {
107
+ "optional": true
108
+ },
109
+ "@hamur/adapter-svelte": {
110
+ "optional": true
111
+ },
112
+ "@hamur/adapter-solid": {
113
+ "optional": true
114
+ },
115
+ "@hamur/adapter-html": {
116
+ "optional": true
117
+ },
118
+ "@hamur/adapter-angular": {
119
+ "optional": true
120
+ },
121
+ "react": {
122
+ "optional": true
123
+ },
124
+ "preact": {
125
+ "optional": true
126
+ },
127
+ "vue": {
128
+ "optional": true
129
+ },
130
+ "svelte": {
131
+ "optional": true
132
+ },
133
+ "solid-js": {
134
+ "optional": true
135
+ },
136
+ "@solidjs/web": {
137
+ "optional": true
138
+ },
139
+ "@angular/core": {
140
+ "optional": true
141
+ }
142
+ }
143
+ }
@@ -0,0 +1,33 @@
1
+ <script lang="ts">
2
+ import type { Snippet } from "svelte"
3
+ import type { HTMLImgAttributes } from "svelte/elements"
4
+ import { useHead } from "@hamur/adapter-svelte"
5
+ import { imgAttributes } from "../runtime/index.ts"
6
+ import { applyPlaceholder, imagePreloadLink, type ImgOptions, type ImgState } from "../runtime/img.ts"
7
+
8
+ type Props = ImgOptions & {
9
+ fetchpriority?: "high" | "low" | "auto"
10
+ children?: Snippet<[ImgState]>
11
+ } & Omit<HTMLImgAttributes, keyof ImgOptions | "srcset" | "fetchpriority" | "children">
12
+
13
+ let { children, onload, onerror, ...props }: Props = $props()
14
+ let isLoaded = $state(false)
15
+ if (props.preload) useHead(() => ({ link: [imagePreloadLink(imgAttributes(props))] }))
16
+ const attrs = $derived({
17
+ ...applyPlaceholder(imgAttributes(props), props, isLoaded),
18
+ onload: (event: Event) => {
19
+ isLoaded = true
20
+ ;(onload as ((event: Event) => void) | null | undefined)?.(event)
21
+ },
22
+ onerror: (event: Event) => {
23
+ isLoaded = false
24
+ ;(onerror as ((event: Event) => void) | null | undefined)?.(event)
25
+ },
26
+ })
27
+ </script>
28
+
29
+ {#if children}
30
+ {@render children({ imgAttrs: attrs, src: String(attrs.src), isLoaded })}
31
+ {:else}
32
+ <img {...attrs} />
33
+ {/if}
@@ -0,0 +1,39 @@
1
+ <script lang="ts">
2
+ import type { HTMLImgAttributes } from "svelte/elements"
3
+ import { useHead } from "@hamur/adapter-svelte"
4
+ import { pictureAttributes } from "../runtime/index.ts"
5
+ import { applyPlaceholder } from "../runtime/img.ts"
6
+ import { picturePreloadLink, type PictureOptions } from "../runtime/picture.ts"
7
+
8
+ type Props = PictureOptions & { fetchpriority?: "high" | "low" | "auto" } & Omit<
9
+ HTMLImgAttributes,
10
+ keyof PictureOptions | "srcset" | "fetchpriority"
11
+ >
12
+
13
+ let { onload, onerror, ...props }: Props = $props()
14
+ let isLoaded = $state(false)
15
+ const parts = $derived(pictureAttributes(props))
16
+ const img = $derived({
17
+ ...applyPlaceholder(parts.img, props, isLoaded),
18
+ onload: (event: Event) => {
19
+ isLoaded = true
20
+ ;(onload as ((event: Event) => void) | null | undefined)?.(event)
21
+ },
22
+ onerror: (event: Event) => {
23
+ isLoaded = false
24
+ ;(onerror as ((event: Event) => void) | null | undefined)?.(event)
25
+ },
26
+ })
27
+ if (props.preload) useHead(() => ({ link: [picturePreloadLink(parts)] }))
28
+ </script>
29
+
30
+ {#if parts.sources.length === 0}
31
+ <img {...img} />
32
+ {:else}
33
+ <picture>
34
+ {#each parts.sources as source (source.type)}
35
+ <source {...source} />
36
+ {/each}
37
+ <img {...img} />
38
+ </picture>
39
+ {/if}