8000 feat(core): apply() runs a list of edits stored as data by hipstersmoothie · Pull Request #1558 · jimp-dev/jimp · GitHub
[go: up one dir, main page]

Skip to content

feat(core): apply() runs a list of edits stored as data - #1558

Merged
hipstersmoothie merged 1 commit into
mainfrom
feat/apply-edits
Nov 25, 2026
Merged

hipstersmoothie merged 1 commit into
mainfrom
feat/apply-edits

Conversation

@hipstersmoothie
Copy link
Copy Markdown
Collaborator

Adds image.apply(edits), which runs a list of [methodName, ...args] edits in order:

image.apply([
  ["cover", { w: 400, h: 300 }],
  ["greyscale"],
  ["blur", 2],
]);

Because the list is plain data, it can be stored as JSON, kept as a preset, or sent to a server or worker and applied there. It's the foundation for the planned @jimp/server (image transforms from URL params) and @jimp/worker (running edits off the main thread).

Design

  • It lives in core. It's a method on every Jimp class, not a new package: it's ~50 lines and has no dependencies.
  • Edits are tuples: [name, ...args]. Every plugin method takes (image, options?), so most edits are [name] or [name, options]. Tuples also cover positional arguments such as ["blur", 2].
  • Only plugin methods that change the image are allowed. Core methods (write, getBuffer, clone, composite, scan, …) and inherited properties (constructor, __proto__) are rejected by name, so a recipe can't touch the filesystem. Methods that return a value (hash, pHash, distanceFromHash, autocropRect) are type errors, and throw at runtime when reached.
  • Names are checked first. Every name is validated before the first edit runs, so a bad name leaves the image untouched. Arguments are validated by each method's own zod schema. A failure is rethrown as Edit 1 (resize) failed: …, with the original error as cause.
  • Typing: JimpEdit<Image> in @jimp/core derives edits from the instance type, and jimp exports JimpEdit for the default class. Deriving it from the plugin method map instead doubled jimp's index.d.ts: tsc inlined the whole map in each of the 12 places the class type repeats. This way the emitted declaration is one line: apply<S extends JimpClass>(this: S, edits: CoreJimpEdit<S>[]): S.

Docs

  • New method page: /methods/apply/
  • A short "Edits as data" section in Chaining and async

Tests

  • packages/jimp/src/apply.test.ts: results match chaining, JSON round-trips, validation and error messages, custom classes, and every default plugin name being accepted.
  • packages/jimp/src/apply.test-d.ts: argument checking, exclusion of core methods and value-returning methods, custom-class plugins, and that JimpEdit names only plugin methods.

🤖 Generated with Claude Code

image.apply([["resize", { w: 100 }], ["greyscale"]]) runs plugin methods
from a list of [name, ...args] tuples, so edits can be stored as JSON, sent
to a server or worker, and applied later. Names are checked before any edit
runs; only plugin methods that return the image are allowed. JimpEdit types
each edit from the image type, so the declaration stays one line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hipstersmoothie hipstersmoothie added the minor Increment the minor version when merged label Nov 24, 2026
@hipstersmoothie
hipstersmoothie merged commit 8ca8a91 into main Nov 25, 2026
13 of 14 checks passed
@hipstersmoothie
hipstersmoothie deleted the feat/apply-edits branch November 25, 2026 01:02
@hipstersmoothie
Copy link
Copy Markdown
Collaborator Author

🚀 PR was released in v2.2.0 🚀

@hipstersmoothie hipstersmoothie added the released This issue/pull request has been released. label Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

minor Increment the minor version when merged released This issue/pull request has been released.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0