Skip to content

fix(image): make Buffer load synchronous and add decode() method#1186

Merged
Brooooooklyn merged 1 commit intomainfrom
12-25-fix_image_load_buffer_should_be_sync
Dec 25, 2025
Merged

fix(image): make Buffer load synchronous and add decode() method#1186
Brooooooklyn merged 1 commit intomainfrom
12-25-fix_image_load_buffer_should_be_sync

Conversation

@Brooooooklyn
Copy link
Copy Markdown
Owner

@Brooooooklyn Brooooooklyn commented Dec 25, 2025

  • 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

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.

  • Add Image.decode() Promise API with typings, and implement internal decoder_task using ScopedTask to expose a resolvable/rejectable decode promise
  • Buffer src behavior: complete=true immediately; dimensions parsed via imagesize before full decode; SVG decoded synchronously; onload now fires after bitmap decode so drawImage/createPattern work in handlers
  • Robust state/race handling: generation counter to abort stale loads, proper currentSrc semantics, clearing src='' resets state without events, broken images report 0 dimensions, external memory accounting fixed
  • AVIF fix: detect AVIF from Buffer by checking libavif first (before infer) and decode correctly
  • Loader update: load-image.js now waits for image.decode() before resolving
  • Types: index.d.ts adds decode() to Image
  • Dependencies: add imagesize (png/jpeg/gif/webp/heif) for header-based sizing
  • Tests: replace manual onload waits with await image.decode() where needed; add extensive WHATWG/W3C spec compliance and regression tests for complete/currentSrc, aborts, decode behavior, and AVIF

Written by Cursor Bugbot for commit eb2bf0d. This will update automatically on new commits. Configure here.

Copilot AI review requested due to automatic review settings December 25, 2025 08:57
Copy link
Copy Markdown
Owner Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/image.rs Outdated
Comment thread __test__/image.spec.ts Outdated
Comment thread src/image.rs Outdated
Comment thread src/image.rs Outdated
Comment thread src/image.rs
Comment thread src/image.rs
Comment thread src/image.rs
Comment thread src/image.rs Outdated
Comment thread src/image.rs Outdated
Comment thread src/image.rs
Comment thread src/image.rs Outdated
@Brooooooklyn Brooooooklyn force-pushed the 12-25-fix_image_load_buffer_should_be_sync branch from d42f8a1 to c9b8837 Compare December 25, 2025 09:26
Comment thread src/image.rs
@Brooooooklyn Brooooooklyn force-pushed the 12-25-fix_image_load_buffer_should_be_sync branch from c9b8837 to 14a5f1b Compare December 25, 2025 12:42
Comment thread src/image.rs
@Brooooooklyn Brooooooklyn force-pushed the 12-25-fix_image_load_buffer_should_be_sync branch from 14a5f1b to 1bb0f82 Compare December 25, 2025 13:15
Comment thread src/image.rs
@Brooooooklyn Brooooooklyn force-pushed the 12-25-fix_image_load_buffer_should_be_sync branch from 1bb0f82 to 9f04e08 Compare December 25, 2025 15:00
Comment thread src/image.rs
- 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>
@Brooooooklyn Brooooooklyn force-pushed the 12-25-fix_image_load_buffer_should_be_sync branch from 9f04e08 to eb2bf0d Compare December 25, 2025 15:27
@Brooooooklyn Brooooooklyn merged commit d418593 into main Dec 25, 2025
40 checks passed
@Brooooooklyn Brooooooklyn deleted the 12-25-fix_image_load_buffer_should_be_sync branch December 25, 2025 15:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants