feat(filters): add binary-safe base64_decode_bytes - #944
Open
talboren wants to merge 1 commit into
Open
Conversation
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.
Contributor
|
Since we're having base64_decode_bytes, can we also have opposite base64_encode_bytes to encode information into bytes array? |
Contributor
Author
@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. |
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.
Summary
Adds a
base64_decode_bytesfilter that decodes Base64 directly to raw bytes without interpreting them as UTF-8:Bufferin the Node build;Uint8Arrayin browser builds;base64_decodetext behavior unchanged for backward compatibility.This complements the Buffer support added to
base64_encodein #881. Today,base64_decodecallsBuffer.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()orevalValueSync()to preserve the binary return value:Rendering the result directly into a text template still converts it to text, so the documentation calls this out explicitly.
Compatibility
Changing
base64_decodeitself 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
Uint8ArraybehaviorMade with Cursor