Add support for Base64 in NodeJS
This commit is contained in:
parent
b1908915d5
commit
090daa68b5
1 changed files with 17 additions and 8 deletions
|
@ -41,19 +41,28 @@ class common extends utils {
|
||||||
static b64encode (str) {
|
static b64encode (str) {
|
||||||
// Unicode safe b64 encoding
|
// Unicode safe b64 encoding
|
||||||
// https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#The_Unicode_Problem
|
// https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#The_Unicode_Problem
|
||||||
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
|
if (process.browser) {
|
||||||
function toSolidBytes (match, p1) {
|
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
|
||||||
// noinspection JSCheckFunctionSignatures
|
function toSolidBytes (match, p1) {
|
||||||
return String.fromCharCode('0x' + p1)
|
// noinspection JSCheckFunctionSignatures
|
||||||
}))
|
return String.fromCharCode('0x' + p1)
|
||||||
|
})
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
return Buffer.from(str).toString('base64')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static b64decode (str) {
|
static b64decode (str) {
|
||||||
// Unicode safe b64 decoding
|
// Unicode safe b64 decoding
|
||||||
// https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#The_Unicode_Problem
|
// https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#The_Unicode_Problem
|
||||||
return decodeURIComponent(atob(str).split('').map(function (c) {
|
if (process.browser) {
|
||||||
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
|
return decodeURIComponent(atob(str).split('').map(function (c) {
|
||||||
}).join(''))
|
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)
|
||||||
|
}).join(''))
|
||||||
|
} else {
|
||||||
|
return Buffer.from(str, 'base64').toString('ascii')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static uniqueList (list) {
|
static uniqueList (list) {
|
||||||
|
|
Loading…
Reference in a new issue