|
5 | 5 | */
|
6 | 6 | "use strict";
|
7 | 7 |
|
| 8 | +var impl = require("base64url"); |
| 9 | + |
8 | 10 | /**
|
9 | 11 | * @namespace base64url
|
10 | 12 | * @description
|
11 | 13 | * Provides methods to encode and decode data according to the
|
12 | 14 | * base64url alphabet.
|
13 | 15 | */
|
14 |
| -var base64url = exports; |
15 |
| -/** |
16 |
| - * Encodes the input to base64url. |
17 |
| - * |
18 |
| - * If {input} is a Buffer, then {encoding} is ignored. Otherwise, |
19 |
| - * {encoding} can be one of "binary", "base64", "hex", "utf8". |
20 |
| - * |
21 |
| - * @param {Buffer|String} input The data to encode. |
22 |
| - * @param {String} [encoding = binary] The input encoding format. |
23 |
| - * @returns {String} the base64url encoding of {input}. |
24 |
| - */ |
25 |
| -base64url.encode = function(input, encoding) { |
26 |
| - var fn = function(match) { |
27 |
| - switch(match) { |
28 |
| - case "+": return "-"; |
29 |
| - case "/": return "_"; |
30 |
| - case "=": return ""; |
31 |
| - } |
32 |
| - // should never happen |
33 |
| - }; |
34 |
| - |
35 |
| - encoding = encoding || "binary"; |
36 |
| - if (Buffer.isBuffer(input)) { |
37 |
| - input = input.toString("base64"); |
38 |
| - } else { |
39 |
| - if ("undefined" !== typeof ArrayBuffer && input instanceof ArrayBuffer) { |
40 |
| - input = new Uint8Array(input); |
41 |
| - } |
42 |
| - input = new Buffer(input, encoding).toString("base64"); |
43 |
| - } |
44 |
| - |
45 |
| - return input.replace(/\+|\/|\=/g, fn); |
| 16 | +var base64url = { |
| 17 | + /** |
| 18 | + * @function |
| 19 | + * Encodes the input to base64url. |
| 20 | + * |
| 21 | + * If {input} is a Buffer, then {encoding} is ignored. Otherwise, |
| 22 | + * {encoding} can be one of "binary", "base64", "hex", "utf8". |
| 23 | + * |
| 24 | + * @param {Buffer|String} input The data to encode. |
| 25 | + * @param {String} [encoding = binary] The input encoding format. |
| 26 | + * @returns {String} the base64url encoding of {input}. |
| 27 | + */ |
| 28 | + encode: impl.encode, |
| 29 | + /** |
| 30 | + * @function |
| 31 | + * Decodes the input from base64url. |
| 32 | + * |
| 33 | + * @param {String} input The data to decode. |
| 34 | + * @returns {Buffer|String} the base64url decoding of {input}. |
| 35 | + */ |
| 36 | + decode: impl.toBuffer |
46 | 37 | };
|
47 |
| -/** |
48 |
| - * Decodes the input from base64url. |
49 |
| - * |
50 |
| - * If {encoding} is not specified, then this method returns a Buffer. |
51 |
| - * Othewise, {encoding} can be one of "binary", "base64", "hex", "utf8"; |
52 |
| - * this method then returns a string matching the given encoding. |
53 |
| - * |
54 |
| - * @param {String} input The data to decode. |
55 |
| - * @param {String} [encoding] The output encoding format. |
56 |
| - * @returns {Buffer|String} the base64url decoding of {input}. |
57 |
| - */ |
58 |
| -base64url.decode = function(input, encoding) { |
59 |
| - var fn = function(match) { |
60 |
| - switch(match) { |
61 |
| - case "-": return "+"; |
62 |
| - case "_": return "/"; |
63 |
| - } |
64 |
| - // should never happen |
65 |
| - }; |
66 | 38 |
|
67 |
| - input = input.replace(/\-|\_/g, fn); |
68 |
| - var output = new Buffer(input, "base64"); |
69 |
| - if (encoding) { |
70 |
| - output = output.toString(encoding); |
71 |
| - } |
72 |
| - return output; |
73 |
| -}; |
| 39 | +module.exports = base64url; |
0 commit comments