mirror of
https://github.com/denoland/deno.git
synced 2025-09-26 20:29:11 +00:00
Make atob
follow the spec (#2242)
This commit is contained in:
parent
a217e55fec
commit
bbeb30fc5e
2 changed files with 53 additions and 8 deletions
|
@ -176,20 +176,28 @@ class UTF8Encoder implements Encoder {
|
||||||
|
|
||||||
/** Decodes a string of data which has been encoded using base-64. */
|
/** Decodes a string of data which has been encoded using base-64. */
|
||||||
export function atob(s: string): string {
|
export function atob(s: string): string {
|
||||||
const rem = s.length % 4;
|
s = String(s);
|
||||||
// base64-js requires length exactly times of 4
|
s = s.replace(/[\t\n\f\r ]/g, "");
|
||||||
if (rem > 0) {
|
|
||||||
s = s.padEnd(s.length + (4 - rem), "=");
|
if (s.length % 4 === 0) {
|
||||||
|
s = s.replace(/==?$/, "");
|
||||||
}
|
}
|
||||||
let byteArray;
|
|
||||||
try {
|
const rem = s.length % 4;
|
||||||
byteArray = base64.toByteArray(s);
|
if (rem === 1 || /[^+/0-9A-Za-z]/.test(s)) {
|
||||||
} catch (_) {
|
// TODO: throw `DOMException`
|
||||||
throw new DenoError(
|
throw new DenoError(
|
||||||
ErrorKind.InvalidInput,
|
ErrorKind.InvalidInput,
|
||||||
"The string to be decoded is not correctly encoded"
|
"The string to be decoded is not correctly encoded"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// base64-js requires length exactly times of 4
|
||||||
|
if (rem > 0) {
|
||||||
|
s = s.padEnd(s.length + (4 - rem), "=");
|
||||||
|
}
|
||||||
|
|
||||||
|
const byteArray: Uint8Array = base64.toByteArray(s);
|
||||||
let result = "";
|
let result = "";
|
||||||
for (let i = 0; i < byteArray.length; i++) {
|
for (let i = 0; i < byteArray.length; i++) {
|
||||||
result += String.fromCharCode(byteArray[i]);
|
result += String.fromCharCode(byteArray[i]);
|
||||||
|
|
|
@ -13,6 +13,43 @@ test(function atobSuccess(): void {
|
||||||
assertEquals(decoded, "hello world");
|
assertEquals(decoded, "hello world");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test(function atobWithAsciiWhitespace(): void {
|
||||||
|
const encodedList = [
|
||||||
|
" aGVsbG8gd29ybGQ=",
|
||||||
|
" aGVsbG8gd29ybGQ=",
|
||||||
|
"aGVsbG8gd29ybGQ= ",
|
||||||
|
"aGVsbG8gd29ybGQ=\n",
|
||||||
|
"aGVsbG\t8gd29ybGQ=",
|
||||||
|
`aGVsbG\t8g
|
||||||
|
d29ybGQ=`
|
||||||
|
];
|
||||||
|
|
||||||
|
for (let encoded of encodedList) {
|
||||||
|
let decoded = atob(encoded);
|
||||||
|
assertEquals(decoded, "hello world");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
test(function atobThrows(): void {
|
||||||
|
let threw = false;
|
||||||
|
try {
|
||||||
|
atob("aGVsbG8gd29ybGQ==");
|
||||||
|
} catch (e) {
|
||||||
|
threw = true;
|
||||||
|
}
|
||||||
|
assert(threw);
|
||||||
|
});
|
||||||
|
|
||||||
|
test(function atobThrows2(): void {
|
||||||
|
let threw = false;
|
||||||
|
try {
|
||||||
|
atob("aGVsbG8gd29ybGQ===");
|
||||||
|
} catch (e) {
|
||||||
|
threw = true;
|
||||||
|
}
|
||||||
|
assert(threw);
|
||||||
|
});
|
||||||
|
|
||||||
test(function btoaFailed(): void {
|
test(function btoaFailed(): void {
|
||||||
const text = "你好";
|
const text = "你好";
|
||||||
let err;
|
let err;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue