browser atob() in node?

4,598 views
Skip to first unread message

Felipe Gasper

unread,
Oct 13, 2012, 5:13:01 PM10/13/12
to nod...@googlegroups.com
Hi all,

What’s the best way to emulate atob() as modern browsers implement it?
(At least, I am fairly sure the Mozilla implementation is a de facto
“standard”?)

-Felipe Gasper
Houston, TX

Rick Waldron

unread,
Oct 13, 2012, 5:28:06 PM10/13/12
to nod...@googlegroups.com
On Sat, Oct 13, 2012 at 5:13 PM, Felipe Gasper <fel...@felipegasper.com> wrote:
Hi all,

        What’s the best way to emulate atob() as modern browsers implement it? (At least, I am fairly sure the Mozilla implementation is a de facto “standard”?)

 


-Felipe Gasper
Houston, TX

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Isaac Schlueter

unread,
Oct 13, 2012, 6:20:43 PM10/13/12
to nod...@googlegroups.com
Or:

new Buffer(b64string, 'base64').toString('binary')


On Sat, Oct 13, 2012 at 2:28 PM, Rick Waldron <waldro...@gmail.com> wrote:
>
>
> On Sat, Oct 13, 2012 at 5:13 PM, Felipe Gasper <fel...@felipegasper.com>
> wrote:
>>
>> Hi all,
>>
>> What’s the best way to emulate atob() as modern browsers implement
>> it? (At least, I am fairly sure the Mozilla implementation is a de facto
>> “standard”?)
>
>
> https://npmjs.org/package/atob
>
>>
>>
>>
>> -Felipe Gasper
>> Houston, TX
>>
>> --
>> Job Board: http://jobs.nodejs.org/
>> Posting guidelines:
>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>> You received this message because you are subscribed to the Google
>> Groups "nodejs" group.
>> To post to this group, send email to nod...@googlegroups.com
>> To unsubscribe from this group, send email to
>> nodejs+un...@googlegroups.com
>> For more options, visit this group at
>> http://groups.google.com/group/nodejs?hl=en?hl=en
>
>
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com

Rick Waldron

unread,
Oct 13, 2012, 8:31:34 PM10/13/12
to nod...@googlegroups.com


On Saturday, October 13, 2012 at 6:20 PM, Isaac Schlueter wrote:

Or:

new Buffer(b64string, 'base64').toString('binary')

Quality of Life Index +1

Isaac Schlueter

unread,
Oct 13, 2012, 8:33:07 PM10/13/12
to nod...@googlegroups.com
Ha, it looks like the atob module just does exactly that :)

Rick Waldron

unread,
Oct 13, 2012, 8:37:57 PM10/13/12
to nod...@googlegroups.com
Even more amusing is that I didn't look at the code at all. I typed the URL into mobile safari, confirmed it existed and just copy/pasted :D

-Rick

Felipe Gasper

unread,
Oct 13, 2012, 8:40:17 PM10/13/12
to nod...@googlegroups.com
The problem with this is that “binary” appears to be deprecated and
slated for removal:

http://nodejs.org/api/buffer.html
'binary' - A way of encoding raw binary data into strings by using only
the first 8 bits of each character. This encoding method is deprecated
and should be avoided in favor of Buffer objects where possible. This
encoding will be removed in future versions of Node.

This is what I’ve ended up doing, but it seems like there ought to be an
easier way?
+ global.atob = function(text) {
+ var buf = new Buffer(text, "base64");
+ var bytes = [];
+ for ( var i = buf.length; i >= 0; i-- ) {
+ bytes[i] = String.fromCharCode(buf[i]);
+ }
+
+ return bytes.join("");
+ };

-FG

Quoth Isaac Schlueter on 10/13/2012 7:33 PM...
--
Felipe M. L. Gasper
http://felipegasper.com

“Wisdom can never learn enough. Ignorance is sufficient unto itself.”
-Mechtild of Magdeburg

“Dad always thought laughter was the best medicine,
which I guess is why several of us died of tuberculosis.”
-Jack Handey

Felipe Gasper

unread,
Oct 13, 2012, 8:41:48 PM10/13/12
to nod...@googlegroups.com
Quoth Felipe Gasper on 10/13/2012 7:40 PM...
> + global.atob = function(text) {
> + var buf = new Buffer(text, "base64");
> + var bytes = [];
> + for ( var i = buf.length; i >= 0; i-- ) {
> + bytes[i] = String.fromCharCode(buf[i]);
> + }
> +
> + return bytes.join("");
> + };

(Ech, I just saw the off-by-one error up there.)

-F

Isaac Schlueter

unread,
Oct 13, 2012, 8:42:59 PM10/13/12
to nod...@googlegroups.com
Meh. We should probably remove that warning. People do have a lot of
pretty novel uses for binary encoded strings, and removing Buffer
encodings is very costly.
Reply all
Reply to author
Forward
0 new messages