#95 ✓resolved
Thatcher

implement btoa and atob

Reported by Thatcher | February 10th, 2010 @ 03:30 AM | in 1.2.x

these are standard browser functions

Comments and changes to this ticket

  • nickg

    nickg March 1st, 2010 @ 07:31 PM

    I never these existed! I might have some code for this which I can donate.

    For anyone else, the reference is
    https://developer.mozilla.org/en/DOM/window.atob

  • nickg

    nickg March 2nd, 2010 @ 07:57 AM

    Hi there,

    I'm writing unit tests and tracking down the exceptions that are thrown (as they are undocumented), the basics are
    http://code.google.com/p/stringencoders/source/browse/#svn/trunk/ja...

    Chrome actually uses the C code from the this project.

    Assuming you want to use, what License should i put in. I would prefer MIT, but open to options here.

    --nickg

  • Thatcher

    Thatcher March 2nd, 2010 @ 11:51 AM

    awesome work nick. MIT is what we prefer too.

  • nickg

    nickg March 3rd, 2010 @ 03:05 PM

    thanks!

    Unit tests for speed and correctness added.
    MIT License added.
    Probably one more day for completely done.

  • Thatcher

    Thatcher March 4th, 2010 @ 01:11 AM

    I'll get window.btoa and atob in envjs today!

  • sephr

    sephr March 4th, 2010 @ 04:55 PM

    Hey nickg. I was checking out your implementation and it seems you throw a string instead of a SyntaxError with a message for your syntax error handling. You should use the following code instead:

    throw new SyntaxError("Not enough arguments");
    
  • nickg

    nickg March 5th, 2010 @ 09:04 AM

    @sephr Yeah, the last step is getting the exceptions fixed. thanks for the tip!

    Investigating ways to make the code browser neutral and ok for server-based js as well.

    I'll get this sorted out asap.

  • nickg

    nickg March 6th, 2010 @ 04:58 AM

    And it's sorted out.

    • SyntaxError is used (and unit tested)
    • A DOMException is thrown in other cases. I believe you allow (new DOMException(5);), but other environments don't allow that, so if that fails it passed back a duck-typed equiv:
    base64.makeDOMException = function() {
        // sadly in FF,Safari,Chrome you can't make a DOMException                                                                                         
        var e, tmp;
        try {
            e = new DOMException(DOMException.INVALID_CHARACTER_ERR);
        } catch (tmp) {
            // not available, just passback a duck-typed equiv                                                                                             
            //  Firefox 3.6 has a much more complicated Error object                                                                                       
            //   but this should suffice                                                                                                                   
            e = {
                code: 5,
                toString: function() { return "Error: INVALID_CHARACTER_ERR: DOM Exception 5"; }
            };
        }
        return e;
    }
    

    Thoughts welcome on alternatives here.

  • Thatcher

    Thatcher March 6th, 2010 @ 05:03 AM

    This would work with Envjs. nice work. I'm going to add it now as a static
    source file, but moving forward I might add a build step to get the latest
    from your svn.

  • nickg

    nickg March 6th, 2010 @ 05:20 AM

    thanks!

    Minus some possible hacks to make this a jquery plugin, I don't see this changing much. I more-or-less wrote this just for env-js. So I'm happy to accept changes for style or whatnot so it blends in with your code (e.g. get rid of big MIT license block etc).

    I have another big treat(?) for you coming up.

  • Thatcher

    Thatcher March 6th, 2010 @ 05:36 AM

    looking forward to it, the code was perfect as is, though i did add a var
    base64 = {} to keep the base64 name hidden inside the window closure. I
    didnt add tests yet but your coverage was really nice and so other than
    checking for the existing of atob and btoa I think I happy delegating to the
    tests in your project.

    curious what your working on ;) just to make sure I'm not stepping on your
    toes, I was going to build on this work to try to start supporting the html
    data: protocol... is this where you where going?

    Thatcher

    PS its in and committed and also tagged in 1.2.0.3

  • sephr

    sephr March 6th, 2010 @ 05:42 AM

    nickg: For making your own error classes, please piggyback on Error or the appropriate error type, so magic metadata can propagate. new Error magically includes the line number and filename of where an error was thrown. Here is my revised version of your code:

    function() {
        // sadly in FF,Safari,Chrome you can't make a DOMException                                                                                  
        if (typeof DOMException !== "undefined") {
            return new DOMException(DOMException.INVALID_CHARACTER_ERR);
        } else {
            var ex = new Error("INVALID_CHARACTER_ERR: DOM Exception 5");
            ex.code = 5;
            return ex;
        }
    }
    
  • sephr

    sephr March 6th, 2010 @ 05:47 AM

    Also, I should mention that all errors (it's practically a standard) must have a message and name property. So if you find yourself ever absolutely needing to use a fake error, store the message in message and have toString return this.name + ": " + this.message as it's what Error.prototype.toString does. I also just noticed that I forgot to set the name in my code. This is what it should be:

    function() {
        // sadly in FF,Safari,Chrome you can't make a DOMException                                                                                  
        if (typeof DOMException !== "undefined") {
            return new DOMException(DOMException.INVALID_CHARACTER_ERR);
        } else {
            var ex = new Error("DOM Exception 5");
            ex.code = 5;
            ex.name = "INVALID_CHARACTER_ERR";
            return ex;
        }
    }
    
  • nickg

    nickg March 6th, 2010 @ 05:53 AM

    Thanks for the tips (again).

    @thatcher: go for it. I'm not working any tickets.

    @all don't be scared that I just forked envjs. Its just so I have aplace to dump all sorts of code you may or may not find useful (independent self-contained code, not patches).

    More on google groups.

    --nickg

  • Thatcher

    Thatcher March 6th, 2010 @ 07:50 AM

    nick I was going to add you as a collaborator on the env-js project. do you
    want me to add client9 or nickg ?

  • nickg

    nickg March 6th, 2010 @ 08:01 AM

    @thatcher : not sure how github works. nickg is fine, but client9 is ok too.

    @sephr: still need to use try/catch since DOMException exists, but one can't make a new one via JS.

    @all: exceptions/error is bizarro in "real browsers". Check this out:

    • DOMException is on all browsers is not an instanceof Error ! But on FF, it has all the metadata as if it were an Error.
    • On FF, when btoa/atob throws a "SyntaxError" it's not an instance of SyntaxError. It's some good-awful remnants of C code that acts like a SyntaxError (this is likely to be very very old code in Mozilla)
    • Looks like DOMException in SS/Chrome has a custom "toString"

    But I agree with sephr that making an SyntaxError/Error object is correct since it adds in metadata.
    I also added some extra fields to make it more compatible with IE-style Errors (prob not needed for envjs).

    I updated in SVN. I think this is the best we can do until a "real user" complains.

    --nickg

  • nickg

    nickg March 21st, 2010 @ 11:03 AM

    • Assigned user changed from “Thatcher” to “nickg”

    nickg to add smoke tests, then this can be closed.

  • nickg

    nickg March 21st, 2010 @ 05:50 PM

    • State changed from “open” to “resolved”

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

a javascript browser environment

People watching this ticket

Tags

Pages