forked from mozilla/addon-sdk
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added "base64" module with docs and unit test, applied review comments.
- Loading branch information
Showing
8 changed files
with
196 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<!-- This Source Code Form is subject to the terms of the Mozilla Public | ||
- License, v. 2.0. If a copy of the MPL was not distributed with this | ||
- file, You can obtain one at http://mozilla.org/MPL/2.0/. --> | ||
|
||
The module provides data encoding and decoding using Base64 algorithms. | ||
|
||
##Example | ||
|
||
var base64 = require("api-utils/base64"); | ||
|
||
var encodedData = base64.encode("Hello, World"); | ||
var decodedData = base64.decode(encodedData); | ||
|
||
##Unicode Strings | ||
|
||
In order to `encode` and `decode` properly Unicode strings, the `charset` | ||
parameter needs to be set to `"utf-8"`: | ||
|
||
var base64 = require("api-utils/base64"); | ||
|
||
var encodedData = base64.encode(unicodeString, "utf-8"); | ||
var decodedData = base64.decode(encodedData, "utf-8"); | ||
|
||
<api name="encode"> | ||
@function | ||
Creates a base-64 encoded ASCII string from a string of binary data. | ||
|
||
@param data {string} | ||
The data to encode | ||
@param [charset] {string} | ||
The charset of the string to encode (optional). | ||
The only accepted value is `"utf-8"`. | ||
|
||
@returns {string} | ||
The encoded string | ||
</api> | ||
|
||
<api name="decode"> | ||
@function | ||
Decodes a string of data which has been encoded using base-64 encoding. | ||
|
||
@param data {string} | ||
The encoded data | ||
@param [charset] {string} | ||
The charset of the string to encode (optional). | ||
The only accepted value is `"utf-8"`. | ||
|
||
@returns {string} | ||
The decoded string | ||
</api> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
"use strict"; | ||
|
||
const { Cu } = require("chrome"); | ||
|
||
// If an object is not given as second argument, the JavaScript Module scope is | ||
// returned, so we can obtain from it the `atob` and `btoa` functions | ||
const { atob, btoa } = Cu.import("resource://gre/modules/Services.jsm"); | ||
|
||
function isUTF8(charset) { | ||
let type = typeof charset; | ||
|
||
if (type === "undefined") | ||
return false; | ||
|
||
if (type === "string" && charset.toLowerCase() === "utf-8") | ||
return true; | ||
|
||
throw new Error("The charset argument can be only 'utf-8'"); | ||
} | ||
|
||
exports.decode = function (data, charset) { | ||
if (isUTF8(charset)) | ||
return decodeURIComponent(escape(atob(data))) | ||
|
||
return atob(data); | ||
} | ||
|
||
exports.encode = function (data, charset) { | ||
if (isUTF8(charset)) | ||
return btoa(unescape(encodeURIComponent(data))) | ||
|
||
return btoa(data); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
"use strict"; | ||
|
||
const base64 = require("api-utils/base64"); | ||
|
||
const text = "Awesome!"; | ||
const b64text = "QXdlc29tZSE="; | ||
|
||
const utf8text = "✓ à la mode"; | ||
const b64utf8text = "4pyTIMOgIGxhIG1vZGU="; | ||
|
||
exports["test base64.encode"] = function (assert) { | ||
assert.equal(base64.encode(text), b64text, "encode correctly") | ||
} | ||
|
||
exports["test base64.decode"] = function (assert) { | ||
assert.equal(base64.decode(b64text), text, "decode correctly") | ||
} | ||
|
||
exports["test base64.encode Unicode"] = function (assert) { | ||
|
||
assert.equal(base64.encode(utf8text, "utf-8"), b64utf8text, | ||
"encode correctly Unicode strings.") | ||
} | ||
|
||
exports["test base64.decode Unicode"] = function (assert) { | ||
|
||
assert.equal(base64.decode(b64utf8text, "utf-8"), utf8text, | ||
"decode correctly Unicode strings.") | ||
} | ||
|
||
exports["test base64.encode with wrong charset"] = function (assert) { | ||
|
||
assert.throws(function() { | ||
base64.encode(utf8text, "utf-16"); | ||
}, "The charset argument can be only 'utf-8'"); | ||
|
||
assert.throws(function() { | ||
base64.encode(utf8text, ""); | ||
}, "The charset argument can be only 'utf-8'"); | ||
|
||
assert.throws(function() { | ||
base64.encode(utf8text, 8); | ||
}, "The charset argument can be only 'utf-8'"); | ||
|
||
} | ||
|
||
exports["test base64.decode with wrong charset"] = function (assert) { | ||
|
||
assert.throws(function() { | ||
base64.decode(utf8text, "utf-16"); | ||
}, "The charset argument can be only 'utf-8'"); | ||
|
||
assert.throws(function() { | ||
base64.decode(utf8text, ""); | ||
}, "The charset argument can be only 'utf-8'"); | ||
|
||
assert.throws(function() { | ||
base64.decode(utf8text, 8); | ||
}, "The charset argument can be only 'utf-8'"); | ||
|
||
} | ||
|
||
exports["test encode/decode Unicode without utf-8 as charset"] = function (assert) { | ||
|
||
assert.notEqual(base64.decode(base64.encode(utf8text)), utf8text, | ||
"Unicode strings needs 'utf-8' charset" | ||
); | ||
|
||
} | ||
|
||
require("test").run(exports); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters