Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Encode escape for btoa (request help) #1318

Closed
wants to merge 2 commits into from

Conversation

yaustar
Copy link

@yaustar yaustar commented Dec 15, 2022

Attempting to fix #1018
Due to a bug on our services here: playcanvas/editor#950

By encoding and escaping the string before btoa, we can encode the content correctly and this fixes half the tests.

However, I don't understand enough about the second lot of tests regarding the map names. Can someone help point me how these map names are generated please

            result = await minify(result.code, {
                sourceMap: {
                    content: "inline",
                    includeSources: true,
                }
            });
            if (result.error) throw result.error;
            map = JSON.parse(result.map);
            assert.strictEqual(map.names.length, 2);
            assert.strictEqual(map.names[0], "tëst");
            assert.strictEqual(map.names[1], "alert");

@fabiosantoscode
Copy link
Collaborator

I was confused about this PR at first (was wondering why escape/unescape was being used) but now I looked through this page I think I get it.

atob and btoa fallbacks are defined here, but since node 16, node ships with atob and btoa which surfaced this issue.

To fix this, I'm taking the hack you're describing and making it secondary to using the native node Buffer (I'm assuming Buffer is faster).

// to/from base64 functions
// Prefer built-in Buffer, if available, then use hack
// https://developer.mozilla.org/en-US/docs/Glossary/Base64#The_Unicode_Problem
var to_ascii = typeof Buffer !== "undefined"
    ? (b64) => Buffer.from(b64, "base64").toString()
    : (b64) => decodeURIComponent(escape(atob(b64)));
var to_base64 = typeof Buffer !== "undefined"
    ? (str) => Buffer.from(str).toString("base64")
    : (str) => btoa(unescape(encodeURIComponent(str)));

It encodes/decodes base64 in node, bun and deno

@fabiosantoscode
Copy link
Collaborator

Appreciate you reaching out with the fix!

@yaustar
Copy link
Author

yaustar commented Jan 29, 2023

That's awesome, thanks for looking into this!

Do you know roughly when the next tag release will be so we can look at updating it in our tools?

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.

Source map test skipped
3 participants