-
Notifications
You must be signed in to change notification settings - Fork 3
/
base64.js
45 lines (39 loc) · 1.17 KB
/
base64.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
module.exports = {
/**
* Base64 URL encode a given text.
*
* @param {Buffer|string} text - The text to encode.
* @returns {Promise<string>} The encoded text.
*/
urlEncode: (text) => {
return new Promise((resolve, reject) => {
let encoded
if (Buffer.isBuffer(text)) {
encoded = text.toString('base64')
} else {
encoded = Buffer.from(text, 'utf8').toString('base64')
}
encoded = encoded.replace('+', '-').replace('/', '_').replace(/=+$/, '')
return resolve(encoded)
})
},
/**
* Base64 URL decode a given text.
*
* @param {string} encodedText - The Base64 encoded text.
* @returns {Promise<Buffer>} The decoded text.
*/
urlDecode: (encodedText) => {
return new Promise((resolve, reject) => {
let encoded
if (typeof encodedText === 'string') {
encodedText += Array(5 - encodedText.length % 4).join('=')
encoded = encodedText.replace('-', '+').replace('_', '/')
return resolve(Buffer.from(encoded, 'base64'))
} else {
return reject(new TypeError(
`Cannot decode non-string value. Found '${typeof encodedText}'.`))
}
})
}
}