-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(media): binding-based image transforms for same-origin media (Access-safe) #1494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| --- | ||
| "emdash": minor | ||
| "@emdash-cms/cloudflare": minor | ||
| --- | ||
|
|
||
| Adds a binding-based image transform service so responsive images work behind Cloudflare Access. | ||
|
|
||
| Previously, resizing R2/local media routed through Astro's `/_image` endpoint, which made the server fetch the media's own URL to load the source bytes — a self-referential request that fails when the site is behind Cloudflare Access or has loopback fetches disabled, surfacing as 404s on transformed images. EmDash now serves transforms from `/_emdash/api/media/transform/{key}`, reading bytes straight from the storage adapter and resizing them with a configured transformer, so no server-side fetch of the media URL is made. | ||
|
|
||
| To enable it on Cloudflare, add an `IMAGES` binding to your wrangler config and wire it up: | ||
|
|
||
| ```ts | ||
| import { imageBinding } from "@emdash-cms/cloudflare"; | ||
|
|
||
| emdash({ | ||
| storage: r2({ binding: "MEDIA" }), | ||
| images: imageBinding({ binding: "IMAGES" }), | ||
| }); | ||
| ``` | ||
|
|
||
| When `images` is not configured, behavior is unchanged. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /** | ||
| * Cloudflare Images binding — image transformer RUNTIME ENTRY. | ||
| * | ||
| * Resizes media source bytes with the Cloudflare `IMAGES` binding. Imported at | ||
| * runtime via the `images` descriptor (see `imageBinding()`); the EmDash | ||
| * transform route reads bytes from storage and passes them here, so no public | ||
| * fetch of the media URL is required. | ||
| * | ||
| * This module imports from `cloudflare:workers` to access the binding. Do NOT | ||
| * import it at config time — use `imageBinding()` from `@emdash-cms/cloudflare`. | ||
| */ | ||
|
|
||
| import { env } from "cloudflare:workers"; | ||
| import type { CreateImageTransformerFn, ImageTransformFormat, TransformedImage } from "emdash"; | ||
|
|
||
| /** Map EmDash's short format names to the MIME types the binding expects. */ | ||
| const FORMAT_MIME: Record<ImageTransformFormat, ImageOutputOptions["format"]> = { | ||
| webp: "image/webp", | ||
| avif: "image/avif", | ||
| jpeg: "image/jpeg", | ||
| png: "image/png", | ||
| }; | ||
|
|
||
| /** | ||
| * Create the Cloudflare Images binding transformer. | ||
| * | ||
| * Resolves the binding by name from the Worker env at request time. | ||
| */ | ||
| export const createImageTransformer: CreateImageTransformerFn = (config) => { | ||
| const bindingName = | ||
| typeof config.binding === "string" && config.binding ? config.binding : "IMAGES"; | ||
|
|
||
| // env from cloudflare:workers has no index signature, so a cast is needed. | ||
| // eslint-disable-next-line typescript/no-unsafe-type-assertion -- Images binding accessed from untyped env object | ||
| const images = (env as Record<string, unknown>)[bindingName] as ImagesBinding | undefined; | ||
|
|
||
| if (!images) { | ||
| throw new Error( | ||
| `Cloudflare Images binding "${bindingName}" not found. ` + | ||
| `Add it to wrangler.jsonc:\n` + | ||
| `{\n "images": {\n "binding": "${bindingName}"\n }\n}`, | ||
| ); | ||
| } | ||
|
|
||
| return { | ||
| async transform(input, options): Promise<TransformedImage> { | ||
| const mime = FORMAT_MIME[options.format ?? "webp"] ?? "image/webp"; | ||
|
|
||
| const transform: ImageTransform = {}; | ||
| if (options.width) transform.width = options.width; | ||
| if (options.height) transform.height = options.height; | ||
|
|
||
| const output: ImageOutputOptions = { format: mime }; | ||
| if (options.quality) output.quality = options.quality; | ||
|
|
||
| const result = await images.input(input).transform(transform).output(output); | ||
| const response = result.response(); | ||
| if (!response.body) { | ||
| throw new Error("Cloudflare Images transform produced an empty body"); | ||
| } | ||
|
|
||
| return { | ||
| body: response.body, | ||
| contentType: response.headers.get("Content-Type") ?? mime, | ||
| }; | ||
| }, | ||
| }; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[needs fixing] The
_transformImagevariable is a module-scope singleton cache. AGENTS.md explicitly requires these to live onglobalThisbecause Vite can duplicate modules across SSR chunks, turning a plainletinto multiple independent variables:Move the cache to
globalThisso it is stable across chunk boundaries. For example:...and store the result back onto
globalThisat each exit point instead of the module-scoped_transformImage.