Skip to content

btoa() and atob() #3462

Description

@silverwind

All major browsers expose the btoa and atob globals for encoding ASCII strings as base64 and vice versa. It'd be beneficial for the "isomorphic" javascript topic if we'd provide these too:

As for the implementation, we can't Buffer('string').toString('base64') because that would encode unicode while atob and btoa must per spec throw on charcodes > 255. https://github.com/mathiasbynens/base64 looks like a solid implemenation we could pretty much drop in.

Activity

  1. MylesBorins commented on Oct 21, 2015

    @MylesBorins
    Contributor

    I'd be up for writing a bunch of tests if people want to see this happen

  2. jasnell commented on Oct 21, 2015

    @jasnell
    Member

    While I understand the desire on this, it could just as easily be implemented as a userland module that registers the globals. I'm -0 on adding it in to core.

  3. mscdex commented on Oct 21, 2015

    @mscdex
    Contributor

    I think I'm -1 on this for the same reason.

  4. trevnorris commented on Oct 21, 2015

    @trevnorris
    Contributor
    function btoa(str) {
      if (Buffer.byteLength(str) !== str.length)
        throw new Error('bad string!');
      return Buffer(str, 'binary').toString('base64');
    }

    Think that will about cover it.

    Update: actually, that's a bad length check and I'm too tired to work the correct one. we'd probably have to expose v8::String::ContainsOnlyOneByte() for the fastest option.

  5. Fishrock123 commented on Oct 21, 2015

    @Fishrock123
    Contributor

    I'd rather not add more globals, ever if possible.

  6. tflanagan commented on Oct 21, 2015

    @tflanagan
    Contributor

    The fact that you are linking to a userland package that already gets over a 1000 downloads a week (via npm) is solid evidence that the need for this, in core at least, is very minimal

  7. silverwind commented on Oct 21, 2015

    @silverwind
    ContributorAuthor

    By the way: Why doesn't v8 provide functions like these?

  8. bnoordhuis commented on Oct 21, 2015

    @bnoordhuis
    Member

    They're browser extensions, they're not part of the ECMAScript spec: https://html.spec.whatwg.org/multipage/webappapis.html#atob

  9. jasnell commented on Oct 21, 2015

    @jasnell
    Member

    Feel free to continue discussing, but given the -1's, I'm closing.

  10. added a commit that references this issue on Dec 15, 2015
  11. 53 remaining items

  12. bmeck commented on Oct 6, 2020

    @bmeck
    Member

    looking at WHAT spec btoa/atob only handle strings with 8bits, so emoji etc. will throw errors, likely those APIs are not sufficient, but I do not see a web API that does universal base64 transformation except FileReader.prototype.readAsDataURL.

  13. MylesBorins commented on Oct 6, 2020

    @MylesBorins
    Contributor

    for base 64 there is https://en.wikipedia.org/wiki/Base64#RFC_4648

    I have helped to maintain a library that has quite a bit of usage in this space

    https://github.com/brianloveswords/base64url with 877k weekly downloads.

    Could be worth looking at that for API inspiration

  14. jasnell commented on Feb 27, 2021

    @jasnell
    Member

    Opened a pr that implemented this and the immediate reaction was meh given that our current mechanisms for this are better and it's something that is easily handled by userland. Closing the issue as it's not something we're likely to land.

  15. devinivy commented on Apr 10, 2021

    @devinivy

    In nodejs/citgm#852 (comment) it was flagged that there are some new globals in node v16 including atob() and btoa() that our test suites should account for. If these are here to stay we'll ensure our test suites allow these globals, but based on what @jasnell mentioned above it seems like they may not land in node v16. Any clarity on this would be much appreciated!

  16. cjihrig commented on Apr 10, 2021

    @cjihrig
    Contributor

    @devinivy these landed already, so I think it's safe for lab to start tracking them as allowed globals.

  17. Tofandel commented on Jun 19, 2021

    @Tofandel
    Contributor

    It's weird, so most people were against it but it landed anyway under the radar..

  18. jasnell commented on Jun 19, 2021

    @jasnell
    Member

    @Tofandel ... there is no "under the radar" here. All changes go through the same review and approval process before landing, and all discussions/reviews are public.

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

    feature requestIssues requesting new Node.js features.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions