Skip to content

base64_encode throws on large input in browser builds #967

Closed

Description

@josefguenther

In the browser builds (liquid.browser.mjs, liquid.browser.umd.js), base64_encode throws once the input goes past roughly 100–125 KB, and that fails the whole render. The node build encodes input of any size.

Version: 10.29.0

Repro (browser build):

import { Liquid } from 'liquidjs/dist/liquid.browser.mjs'
const engine = new Liquid()

await engine.parseAndRender('{{ v | base64_encode }}', { v: 'x'.repeat(200 * 1024) })
// RenderError: Maximum call stack size exceeded
input size node build browser build (V8)
100 KB encodes encodes
125 KB encodes RenderError: Maximum call stack size exceeded
1 MB encodes RenderError: Maximum call stack size exceeded
5 MB encodes RenderError: Maximum call stack size exceeded

The threshold depends on the engine's limit on function arguments. On QuickJS it's lower: anything over 64 KB fails with "too many arguments in function call (only 65535 allowed)".

Size is the only way the two builds differ. On nil, numbers, objects, multi-byte UTF-8, emoji and lone surrogates, their output is byte-for-byte identical.

Cause: the browser build spreads the encoded bytes into String.fromCharCode, which passes one argument per byte:

function base64Encode(str) {
    return btoa(String.fromCharCode(...new TextEncoder().encode(str)));
}

The UMD build does the same through String.fromCharCode.apply.

Suggested fix: build the binary string in chunks.

function base64Encode(str) {
    const bytes = new TextEncoder().encode(str);
    let binary = '';
    for (let i = 0; i < bytes.length; i += 0x8000) {
        binary += String.fromCharCode.apply(null, bytes.subarray(i, i + 0x8000));
    }
    return btoa(binary);
}

I checked this against the node build's Buffer.from(str, 'utf8').toString('base64') on inputs up to 5 MB, including multi-byte characters and lengths just either side of the chunk size. The output was identical in every case.

Related: #965, where the two builds' implementations of base64_decode also disagree. That issue raises a question about intended behaviour; this one doesn't, so I've filed them separately.

Happy to open a PR if that's useful.

Activity

  1. github-actions commented on Oct 1, 2026

    @github-actions

    🎉 This issue has been resolved in version 10.30.0 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

  2. added a commit that references this issue on Oct 9, 2026
    8aace78
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions