addon-sdk/source/lib/sdk/base64.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 "use strict";
     7 module.metadata = {
     8   "stability": "unstable"
     9 };
    11 const { Cu } = require("chrome");
    13 // Passing an empty object as second argument to avoid scope's pollution
    14 const { atob, btoa } = Cu.import("resource://gre/modules/Services.jsm", {});
    16 function isUTF8(charset) {
    17   let type = typeof charset;
    19   if (type === "undefined")
    20     return false;
    22   if (type === "string" && charset.toLowerCase() === "utf-8")
    23     return true;
    25   throw new Error("The charset argument can be only 'utf-8'");
    26 }
    28 exports.decode = function (data, charset) {
    29   if (isUTF8(charset))
    30 		return decodeURIComponent(escape(atob(data)))
    32 	return atob(data);
    33 }
    35 exports.encode = function (data, charset) {
    36   if (isUTF8(charset))
    37     return btoa(unescape(encodeURIComponent(data)))
    39 	return btoa(data);
    40 }

mercurial