Skip to content

fix(element): encode the view window, not the backing buffer - #439

Open
PerryLink wants to merge 2 commits into
satorijs:v4from
PerryLink:fix/v4-element-view-window
Open

PerryLink wants to merge 2 commits into
satorijs:v4from
PerryLink:fix/v4-element-view-window

Conversation

@PerryLink

@PerryLink PerryLink commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Problem

createAssetFactory declares its input as Binary.Source, which is an ArrayBuffer or any ArrayBufferView. The view branch dereferenced .buffer before encoding:

} else if (ArrayBuffer.isView(src)) {
  src = prefix + Binary.toBase64(src.buffer)
}

An ArrayBufferView is a window onto a buffer: its bytes are byteOffset .. byteOffset + byteLength, not the whole backing store. Passing .buffer therefore encodes every byte the view was carved out of. A three-byte view into a six-byte buffer yields base64://AAABAgMA instead of base64://AQID; the same happens for DataView and for the mime type form of file().

Fix

Binary.toBase64 already handles a view correctly, so pass the view itself:

-        src = prefix + Binary.toBase64(src.buffer)
+        src = prefix + Binary.toBase64(src)

Relation to #431

This is the same fix as #431, which targets main. main is no longer where development happens: its install has been broken since 2026-10-07 on an unpublished dependency, and active work moved to v4. This PR is the v4 version of the change; #431 is superseded by it.

Tests

Adds packages/element/tests/index.spec.ts, covering string, Buffer, ArrayBuffer, a Uint8Array view with a non-zero byteOffset, a DataView, and the mime type variant — the same cases as #431.

Red before the fix (reproduced twice, identical):

✖ Uint8Array view
  expected 'base64://AAABAgMA' to equal 'base64://AQID'
✖ DataView
  expected 'base64://AAABAgMA' to equal 'base64://AQID'
✖ mime type
  expected 'data:image/png;base64,AAABAgMA' to equal 'data:image/png;base64,AQID'
ℹ tests 6
ℹ pass 3
ℹ fail 3

Green after the fix:

✔ string source
✔ Buffer source
✔ ArrayBuffer source
✔ Uint8Array view
✔ DataView
✔ mime type
ℹ tests 6
ℹ pass 6
ℹ fail 0

Both element test files together: ℹ tests 19 / pass 19 / fail 0.

  • pnpm exec yakumo build element — exit 0
  • pnpm lint — exit 0
  • pnpm exec eslint packages/element/tests/index.spec.ts --no-ignore — exit 0 (the repo's .eslintignore excludes tests, so this is informational)

Because the v4 test script still does not forward the tsx loader to the node --test child, the tests above were run directly with node --import tsx --test packages/element/tests/*.spec.ts.

CI

lint and build pass. test (20) / test (22) currently fail on this PR, but they fail at load time, before any assertion runs, because the base branch's test script still drops the loader: the new file hits ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX (namespace in src), segment.spec.ts hits ERR_UNSUPPORTED_DIR_IMPORT, and protocol.spec.ts hits ERR_MODULE_NOT_FOUND for @satorijs/element/lib. That is the pre-existing v4 test failure fixed separately by #438 (which makes both test jobs green); once #438 is on v4, this PR's test jobs will exercise the new cases. publish is skipped on pull requests; the red publish job on v4's head is unrelated to this change.

@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (v4@fa5ec61). Learn more about missing BASE report.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@          Coverage Diff          @@
##             v4     #439   +/-   ##
=====================================
  Coverage      ?   94.02%           
=====================================
  Files         ?        2           
  Lines         ?     1072           
  Branches      ?      139           
=====================================
  Hits          ?     1008           
  Misses        ?       64           
  Partials      ?        0           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Same one-line script change as satorijs#438. It is only here so this branch can run its own tests before satorijs#438 lands; drop it when rebasing on v4 after satorijs#438 is merged.
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