зеркало из https://github.com/Azure/ms-rest-js.git
Fix base64 in browser
This commit is contained in:
Родитель
3ea7186da9
Коммит
1d25498686
|
@ -19,7 +19,11 @@ export function encodeString(value: string): string {
|
|||
*/
|
||||
export function encodeByteArray(value: Uint8Array): string {
|
||||
if (typeof Buffer === "undefined") {
|
||||
return btoa(encodeURIComponent(new TextDecoder().decode(value)));
|
||||
let str = "";
|
||||
for (let i = 0; i < value.length; i++) {
|
||||
str += String.fromCharCode(value[i]);
|
||||
}
|
||||
return btoa(str);
|
||||
} else {
|
||||
// Buffer.from accepts <ArrayBuffer> | <SharedArrayBuffer>-- the TypeScript definition is off here
|
||||
// https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_arraybuffer_byteoffset_length
|
||||
|
@ -34,7 +38,12 @@ export function encodeByteArray(value: Uint8Array): string {
|
|||
*/
|
||||
export function decodeString(value: string): Uint8Array {
|
||||
if (typeof Buffer === "undefined") {
|
||||
return new TextEncoder().encode(atob(value));
|
||||
const byteString = atob(value);
|
||||
const arr = new Uint8Array(byteString.length);
|
||||
for (let i = 0; i < byteString.length; i++) {
|
||||
arr[i] = byteString.charCodeAt(i);
|
||||
}
|
||||
return arr;
|
||||
} else {
|
||||
return Buffer.from(value, "base64");
|
||||
}
|
||||
|
|
|
@ -772,7 +772,7 @@ describe("msrest", function () {
|
|||
"birthday": new Date("2012-01-05T01:00:00Z").toISOString(),
|
||||
"species": "king",
|
||||
"length": 1.0,
|
||||
"picture": Uint8Array.from([255, 255, 255, 255, 254]).toString(),
|
||||
"picture": "/////g==",
|
||||
"siblings": [
|
||||
{
|
||||
"fish.type": "shark",
|
||||
|
@ -786,7 +786,7 @@ describe("msrest", function () {
|
|||
"age": 105,
|
||||
"birthday": new Date("1900-01-05T01:00:00Z").toISOString(),
|
||||
"length": 10.0,
|
||||
"picture": Uint8Array.from([255, 255, 255, 255, 254]).toString(),
|
||||
"picture": "/////g==",
|
||||
"species": "dangerous"
|
||||
}
|
||||
]
|
||||
|
@ -873,14 +873,14 @@ describe("msrest", function () {
|
|||
'birthday': new Date('2012-01-05T01:00:00Z').toISOString(),
|
||||
'species': 'king',
|
||||
'length': 1.0,
|
||||
'picture': Uint8Array.from([255, 255, 255, 255, 254]).toString(),
|
||||
'picture': "/////g==",
|
||||
'siblings': [
|
||||
{
|
||||
'fish.type': 'mutatedshark',
|
||||
'age': 105,
|
||||
'birthday': new Date('1900-01-05T01:00:00Z').toISOString(),
|
||||
'length': 10.0,
|
||||
'picture': Uint8Array.from([255, 255, 255, 255, 254]).toString(),
|
||||
'picture': "/////g==",
|
||||
'species': 'dangerous',
|
||||
'siblings': [
|
||||
{
|
||||
|
|
Загрузка…
Ссылка в новой задаче