Skip to content

feat(filters): add binary-safe base64_decode_bytes - #944

Open
talboren wants to merge 1 commit into
harttle:masterfrom
talboren:fix/base64-decode-bytes
Open

feat(filters): add binary-safe base64_decode_bytes#944
talboren wants to merge 1 commit into
harttle:masterfrom
talboren:fix/base64-decode-bytes

Conversation

@talboren

@talboren talboren commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds a base64_decode_bytes filter that decodes Base64 directly to raw bytes without interpreting them as UTF-8:

  • returns a Node.js Buffer in the Node build;
  • returns a Uint8Array in browser builds;
  • leaves the existing base64_decode text behavior unchanged for backward compatibility.

This complements the Buffer support added to base64_encode in #881. Today, base64_decode calls Buffer.from(value, 'base64').toString('utf8'), which irreversibly replaces invalid UTF-8 byte sequences. Binary files such as PNGs and PDFs therefore cannot be decoded without corruption.

Usage

Use evalValue() or evalValueSync() to preserve the binary return value:

const bytes = engine.evalValueSync(
  'encoded | base64_decode_bytes',
  { encoded: 'iVBORw0KGgr//g==' }
)

Rendering the result directly into a text template still converts it to text, so the documentation calls this out explicitly.

Compatibility

Changing base64_decode itself to return bytes would break existing text templates and produce different rendered output between Node (Buffer) and browsers (Uint8Array). A separate filter keeps the existing Shopify-compatible behavior stable while providing an explicit binary-safe path.

Tests

  • Raw PNG bytes, including invalid UTF-8 sequences
  • Empty input
  • Arbitrary-byte decode → encode round trip
  • Browser Uint8Array behavior
  • Full test suite: 1,636 tests passing
  • Full lint and all Node/browser bundles passing

Made with Cursor

Add a cross-platform filter that decodes Base64 into raw bytes without
forcing the result through UTF-8. It returns a Buffer in Node.js and a
Uint8Array in browsers, while preserving the existing text behavior of
base64_decode.
@skynetigor

Copy link
Copy Markdown
Contributor

Since we're having base64_decode_bytes, can we also have opposite base64_encode_bytes to encode information into bytes array?

@talboren

talboren commented Aug 4, 2026

Copy link
Copy Markdown
Contributor Author

Since we're having base64_decode_bytes, can we also have opposite base64_encode_bytes to encode information into bytes array?

@skynetigor Encoding always returns Base64 text by definition. Existing base64_encode already accepts raw Buffer bytes without UTF-8 conversion, so it is binary-safe.

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