Repository navigation
Upload Media: Smallest encoding test plugin replaces a core operation - #83333
Draft
swissspidy wants to merge 4 commits into
Draft
swissspidy wants to merge 4 commits into
swissspidy wants to merge 4 commits into
Conversation
An e2e test plugin that takes the escape hatch of the operation API: it unregisters core/transcode-image, registers its own step under the same name, and plans that step into every still image upload. The step encodes the image with the browser's own encoders in every format that would not lose anything the image has, and uploads whichever came out smallest, the original included. It records what it compared as a field of the upload request, which the plugin's PHP side keeps. The spec checks the invariant rather than a winner, since which format wins depends on the browser's encoders: the file the server holds is the one the step picked, that one is the smallest of the candidates, and the bytes served back are exactly that many. Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com> Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Sr7G5nRsbso744zkruyGHZ
🤖 PR meta 🤖📦 Bundle sizeSize Change: 0 B Total Size: 8.21 MB
⚡ PerformanceShow the resultsClient side metrics exclude the server response time. front-end-block-theme
front-end-classic-theme
media-processing
media-upload
post-editor
site-editor
|
…peration-smallest-encoding
Where core placed core/transcode-image — the original when the site asks for a conversion, and every sub-size — the replacement arrives with core's arguments, and until now ignored them: the site asked for WebP and could get whatever came out smallest, sub-sizes included. Now a requested format is the answer and the comparison only runs when nothing was asked. The step also returns a preview through context.createBlobURL(), which the queue revokes when the item leaves, so the editor shows the chosen file rather than the original until the server's URL replaces it. Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com> Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Sr7G5nRsbso744zkruyGHZ
The REST field was declared as an object while the plugin sent it as a JSON string. Additional fields become arguments of the create endpoint and are validated against their schema before the attachment is made, so the mismatch refused every upload the step touched, and the spec saw an image block with no attachment. The record now travels as nested form fields, which the server reads back as an object, and the schema says what is in it so the sizes arrive as integers. The spec watches the create request and reports its status and body when it is not 201, so a refused upload reads as the refusal rather than as a missing ID. Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com> Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Sr7G5nRsbso744zkruyGHZ
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What?
See #82035. Second of the step-3 proofs of concept for the upload operation API: a plugin that replaces a core step under its own name.
Stacked on #83331 (which is stacked on #82474); sibling of #83332 and #83334. The diff here is only the test plugin and its spec.
Why?
The proposal's escape hatch is "unregister a core step, register your own under the same name". This exercises it end to end from outside the package, and with it the consequence settled in #82474: a replacement of a core operation runs with a plugin's context, not core's, so everything it does has to fit the public contract.
It also surfaced two facts about replacing a core step, both now documented in #83331's README:
core/transcode-imagefor an original image when the site asks for a format conversion (image_editor_output_format). A replacement that expects to run on every upload also has to plan itself in — which the API supports, since the replacement has aplan()of its own and sees the pipeline as planned so far.UploadOperationArgs['core/transcode-image']:outputFormat,outputQuality,interlaced). A replacement has to honour them: the site asked for WebP, so WebP is the answer.How?
packages/e2e-tests/plugins/upload-operation-smallest-encoding/index.js:unregisterUploadOperation( 'core/transcode-image' )thenregisterUploadOperation( 'core/transcode-image', … ).plan()places the step{ before: 'core/upload' }for JPEG, PNG, WebP and AVIF originals, unless the pipeline already carries acore/transcode-imagestep. Animated GIFs are left alone:createImageBitmapdecodes the first frame and re-encoding that would upload a still.OffscreenCanvas. With core's args (outputFormatset), it encodes exactly that format at the requested quality and returns it. Without, it encodes WebP, PNG and — when the image is opaque — JPEG at one quality so they compete on size alone, and returns the smallest, the original included, so the file never gets bigger. Either way it records{ type, size, candidates, requested? }inadditionalData.smallest_encoding.attachment.urlthroughcontext.createBlobURL()(added in Upload Media: Public API for registering upload operations #83331), so the editor shows the chosen file until the server's URL replaces it, and the queue revokes the URL when the item leaves.packages/e2e-tests/plugins/upload-operation-smallest-encoding.phpacceptssmallest_encodingas a REST field on attachments, keeps it as post meta, and exposes it on the attachment.test/e2e/specs/editor/various/upload-operation-smallest-encoding.spec.jsuploads1024x768_e2e_test_image.pngand asserts the invariant rather than a winner (which format wins depends on the browser's encoders): the attachment'smime_typeis the type the step picked, that type's size is the minimum of the candidates, and fetchingsource_urlreturns exactly that many bytes. A second test checksgetUploadOperation( 'core/transcode-image' )now carries the plugin's label.Testing Instructions
npm run test:e2e -- test/e2e/specs/editor/various/upload-operation-smallest-encoding.spec.jsThe spec skips itself where client-side media processing is inactive. I could not run it locally (no Docker in this environment), so CI is the first run; the plugin JS and the spec pass eslint, the PHP passes phpcs.
Testing Instructions for Keyboard
N/A — no UI changes.
Screenshots or screencast
N/A.
Use of AI Tools
Authored with Claude Code: the plugin, the spec and this description.
🤖 Generated with Claude Code
https://claude.ai/code/session_01Sr7G5nRsbso744zkruyGHZ