fix(image): make Buffer load synchronous and add decode() method#1186
Merged
Brooooooklyn merged 1 commit intomainfrom Dec 25, 2025
Merged
fix(image): make Buffer load synchronous and add decode() method#1186Brooooooklyn merged 1 commit intomainfrom
Brooooooklyn merged 1 commit intomainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR makes Buffer image loading synchronous (matching node-canvas behavior) by firing onload/onerror events synchronously after extracting dimensions from image headers. It also adds a decode() method that returns a Promise for the bitmap decoding operation, per the W3C HTML specification.
Key changes:
- Natural dimensions are extracted from image headers using the imagesize crate before bitmap decoding
- Buffer sources now fire onload/onerror synchronously with
complete=true - Added
decode()method returning Promise for async bitmap decoding - 18 comprehensive tests for decode() behavior per WHATWG HTML spec
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 11 comments.
| File | Description |
|---|---|
| src/image.rs | Adds natural_width/natural_height fields, synchronous Buffer loading with header parsing via imagesize, BufferDecodeTask for async bitmap decoding, and decode() method that returns the decoder task promise |
| test/image.spec.ts | Updates existing tests for new synchronous Buffer behavior and adds 18 new tests covering decode() method per W3C spec |
| Cargo.toml | Adds imagesize dependency for fast image header parsing without full decoding |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
d42f8a1 to
c9b8837
Compare
c9b8837 to
14a5f1b
Compare
14a5f1b to
1bb0f82
Compare
1bb0f82 to
9f04e08
Compare
- Buffer src now fires onload/onerror synchronously (matches node-canvas) - Dimensions extracted from image header before events fire - Add decode() method returning Promise per W3C spec - Add 18 comprehensive decode() tests per WHATWG HTML spec 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
9f04e08 to
eb2bf0d
Compare
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.

🤖 Generated with Claude Code
Co-Authored-By: Claude Opus 4.5 noreply@anthropic.com
Note
Introduces a decoding API and refactors image loading to be spec-aligned and race-safe.
Image.decode()Promise API with typings, and implement internaldecoder_taskusingScopedTaskto expose a resolvable/rejectable decode promisecomplete=trueimmediately; dimensions parsed viaimagesizebefore full decode; SVG decoded synchronously;onloadnow fires after bitmap decode sodrawImage/createPatternwork in handlerscurrentSrcsemantics, clearingsrc=''resets state without events, broken images report 0 dimensions, external memory accounting fixedlibaviffirst (beforeinfer) and decode correctlyload-image.jsnow waits forimage.decode()before resolvingindex.d.tsaddsdecode()toImageimagesize(png/jpeg/gif/webp/heif) for header-based sizingawait image.decode()where needed; add extensive WHATWG/W3C spec compliance and regression tests forcomplete/currentSrc, aborts, decode behavior, and AVIFWritten by Cursor Bugbot for commit eb2bf0d. This will update automatically on new commits. Configure here.