2024-01-01 14:58:21 -05:00
|
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-11-13 13:35:48 -05:00
|
|
|
|
import {
|
|
|
|
|
assert,
|
|
|
|
|
assertEquals,
|
|
|
|
|
assertStrictEquals,
|
|
|
|
|
assertThrows,
|
|
|
|
|
} from "./test_util.ts";
|
2018-09-20 18:53:29 -04:00
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function btoaSuccess() {
|
2018-09-20 18:53:29 -04:00
|
|
|
|
const text = "hello world";
|
|
|
|
|
const encoded = btoa(text);
|
2019-03-06 20:48:46 -05:00
|
|
|
|
assertEquals(encoded, "aGVsbG8gd29ybGQ=");
|
2018-09-20 18:53:29 -04:00
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function atobSuccess() {
|
2018-09-20 18:53:29 -04:00
|
|
|
|
const encoded = "aGVsbG8gd29ybGQ=";
|
|
|
|
|
const decoded = atob(encoded);
|
2019-03-06 20:48:46 -05:00
|
|
|
|
assertEquals(decoded, "hello world");
|
2018-09-20 18:53:29 -04:00
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function atobWithAsciiWhitespace() {
|
2019-04-30 14:25:37 -04:00
|
|
|
|
const encodedList = [
|
|
|
|
|
" aGVsbG8gd29ybGQ=",
|
|
|
|
|
" aGVsbG8gd29ybGQ=",
|
|
|
|
|
"aGVsbG8gd29ybGQ= ",
|
|
|
|
|
"aGVsbG8gd29ybGQ=\n",
|
|
|
|
|
"aGVsbG\t8gd29ybGQ=",
|
|
|
|
|
`aGVsbG\t8g
|
2020-03-28 13:03:49 -04:00
|
|
|
|
d29ybGQ=`,
|
2019-04-30 14:25:37 -04:00
|
|
|
|
];
|
|
|
|
|
|
2019-09-07 12:27:18 -04:00
|
|
|
|
for (const encoded of encodedList) {
|
|
|
|
|
const decoded = atob(encoded);
|
2019-04-30 14:25:37 -04:00
|
|
|
|
assertEquals(decoded, "hello world");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function atobThrows() {
|
2019-04-30 14:25:37 -04:00
|
|
|
|
let threw = false;
|
|
|
|
|
try {
|
|
|
|
|
atob("aGVsbG8gd29ybGQ==");
|
2021-04-09 17:35:29 -04:00
|
|
|
|
} catch (_e) {
|
2019-04-30 14:25:37 -04:00
|
|
|
|
threw = true;
|
|
|
|
|
}
|
|
|
|
|
assert(threw);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function atobThrows2() {
|
2019-04-30 14:25:37 -04:00
|
|
|
|
let threw = false;
|
|
|
|
|
try {
|
|
|
|
|
atob("aGVsbG8gd29ybGQ===");
|
2021-04-09 17:35:29 -04:00
|
|
|
|
} catch (_e) {
|
2019-04-30 14:25:37 -04:00
|
|
|
|
threw = true;
|
|
|
|
|
}
|
|
|
|
|
assert(threw);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function atobThrows3() {
|
2021-01-10 17:15:32 -05:00
|
|
|
|
let threw = false;
|
|
|
|
|
try {
|
|
|
|
|
atob("foobar!!");
|
|
|
|
|
} catch (e) {
|
|
|
|
|
if (
|
|
|
|
|
e instanceof DOMException &&
|
|
|
|
|
e.toString().startsWith("InvalidCharacterError:")
|
|
|
|
|
) {
|
|
|
|
|
threw = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
assert(threw);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function btoaFailed() {
|
2018-09-20 18:53:29 -04:00
|
|
|
|
const text = "你好";
|
2020-06-24 18:57:08 -04:00
|
|
|
|
assertThrows(() => {
|
2018-09-20 18:53:29 -04:00
|
|
|
|
btoa(text);
|
2021-02-15 20:10:59 -05:00
|
|
|
|
}, DOMException);
|
2018-09-20 18:53:29 -04:00
|
|
|
|
});
|
2018-12-06 13:01:15 -05:00
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function textDecoder2() {
|
2020-07-14 15:24:17 -04:00
|
|
|
|
// deno-fmt-ignore
|
2018-12-06 13:01:15 -05:00
|
|
|
|
const fixture = new Uint8Array([
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xbd,
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xae,
|
|
|
|
|
0xf0, 0x9d, 0x94, 0x81,
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xbd
|
|
|
|
|
]);
|
|
|
|
|
const decoder = new TextDecoder();
|
2019-03-06 20:48:46 -05:00
|
|
|
|
assertEquals(decoder.decode(fixture), "𝓽𝓮𝔁𝓽");
|
2018-12-06 13:01:15 -05:00
|
|
|
|
});
|
|
|
|
|
|
2021-01-05 13:50:40 -05:00
|
|
|
|
// ignoreBOM is tested through WPT
|
2019-10-01 20:08:51 -04:00
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function textDecoderASCII() {
|
2018-12-06 18:23:29 -05:00
|
|
|
|
const fixture = new Uint8Array([0x89, 0x95, 0x9f, 0xbf]);
|
|
|
|
|
const decoder = new TextDecoder("ascii");
|
2019-03-06 20:48:46 -05:00
|
|
|
|
assertEquals(decoder.decode(fixture), "‰•Ÿ¿");
|
2018-12-06 18:23:29 -05:00
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function textDecoderErrorEncoding() {
|
2018-12-06 18:23:29 -05:00
|
|
|
|
let didThrow = false;
|
|
|
|
|
try {
|
2020-10-20 07:47:38 -04:00
|
|
|
|
new TextDecoder("Foo");
|
2018-12-06 18:23:29 -05:00
|
|
|
|
} catch (e) {
|
|
|
|
|
didThrow = true;
|
2021-10-27 17:43:40 -04:00
|
|
|
|
assert(e instanceof Error);
|
2020-10-20 07:47:38 -04:00
|
|
|
|
assertEquals(e.message, "The encoding label provided ('Foo') is invalid.");
|
2018-12-06 18:23:29 -05:00
|
|
|
|
}
|
|
|
|
|
assert(didThrow);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function textEncoder() {
|
2018-12-06 13:01:15 -05:00
|
|
|
|
const fixture = "𝓽𝓮𝔁𝓽";
|
|
|
|
|
const encoder = new TextEncoder();
|
2020-07-14 15:24:17 -04:00
|
|
|
|
// deno-fmt-ignore
|
2019-03-06 20:48:46 -05:00
|
|
|
|
assertEquals(Array.from(encoder.encode(fixture)), [
|
2018-12-06 13:01:15 -05:00
|
|
|
|
0xf0, 0x9d, 0x93, 0xbd,
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xae,
|
|
|
|
|
0xf0, 0x9d, 0x94, 0x81,
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xbd
|
|
|
|
|
]);
|
|
|
|
|
});
|
2019-03-15 16:29:54 -04:00
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function textEncodeInto() {
|
2019-06-21 18:32:14 -04:00
|
|
|
|
const fixture = "text";
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
const bytes = new Uint8Array(5);
|
|
|
|
|
const result = encoder.encodeInto(fixture, bytes);
|
|
|
|
|
assertEquals(result.read, 4);
|
|
|
|
|
assertEquals(result.written, 4);
|
2020-07-14 15:24:17 -04:00
|
|
|
|
// deno-fmt-ignore
|
2019-06-21 18:32:14 -04:00
|
|
|
|
assertEquals(Array.from(bytes), [
|
|
|
|
|
0x74, 0x65, 0x78, 0x74, 0x00,
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function textEncodeInto2() {
|
2019-06-21 18:32:14 -04:00
|
|
|
|
const fixture = "𝓽𝓮𝔁𝓽";
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
const bytes = new Uint8Array(17);
|
|
|
|
|
const result = encoder.encodeInto(fixture, bytes);
|
|
|
|
|
assertEquals(result.read, 8);
|
|
|
|
|
assertEquals(result.written, 16);
|
2020-07-14 15:24:17 -04:00
|
|
|
|
// deno-fmt-ignore
|
2019-06-21 18:32:14 -04:00
|
|
|
|
assertEquals(Array.from(bytes), [
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xbd,
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xae,
|
|
|
|
|
0xf0, 0x9d, 0x94, 0x81,
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xbd, 0x00,
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function textEncodeInto3() {
|
2020-05-06 13:10:15 -04:00
|
|
|
|
const fixture = "𝓽𝓮𝔁𝓽";
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
const bytes = new Uint8Array(5);
|
|
|
|
|
const result = encoder.encodeInto(fixture, bytes);
|
|
|
|
|
assertEquals(result.read, 2);
|
|
|
|
|
assertEquals(result.written, 4);
|
2020-07-14 15:24:17 -04:00
|
|
|
|
// deno-fmt-ignore
|
2020-05-06 13:10:15 -04:00
|
|
|
|
assertEquals(Array.from(bytes), [
|
|
|
|
|
0xf0, 0x9d, 0x93, 0xbd, 0x00,
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function loneSurrogateEncodeInto() {
|
2021-05-08 17:31:40 -04:00
|
|
|
|
const fixture = "lone𝄞\ud888surrogate";
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
const bytes = new Uint8Array(20);
|
|
|
|
|
const result = encoder.encodeInto(fixture, bytes);
|
|
|
|
|
assertEquals(result.read, 16);
|
|
|
|
|
assertEquals(result.written, 20);
|
|
|
|
|
// deno-fmt-ignore
|
|
|
|
|
assertEquals(Array.from(bytes), [
|
|
|
|
|
0x6c, 0x6f, 0x6e, 0x65,
|
|
|
|
|
0xf0, 0x9d, 0x84, 0x9e,
|
|
|
|
|
0xef, 0xbf, 0xbd, 0x73,
|
|
|
|
|
0x75, 0x72, 0x72, 0x6f,
|
|
|
|
|
0x67, 0x61, 0x74, 0x65
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function loneSurrogateEncodeInto2() {
|
2021-05-08 17:31:40 -04:00
|
|
|
|
const fixture = "\ud800";
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
const bytes = new Uint8Array(3);
|
|
|
|
|
const result = encoder.encodeInto(fixture, bytes);
|
|
|
|
|
assertEquals(result.read, 1);
|
|
|
|
|
assertEquals(result.written, 3);
|
|
|
|
|
// deno-fmt-ignore
|
|
|
|
|
assertEquals(Array.from(bytes), [
|
|
|
|
|
0xef, 0xbf, 0xbd
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function loneSurrogateEncodeInto3() {
|
2021-05-08 17:31:40 -04:00
|
|
|
|
const fixture = "\udc00";
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
const bytes = new Uint8Array(3);
|
|
|
|
|
const result = encoder.encodeInto(fixture, bytes);
|
|
|
|
|
assertEquals(result.read, 1);
|
|
|
|
|
assertEquals(result.written, 3);
|
|
|
|
|
// deno-fmt-ignore
|
|
|
|
|
assertEquals(Array.from(bytes), [
|
|
|
|
|
0xef, 0xbf, 0xbd
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function swappedSurrogatePairEncodeInto4() {
|
2021-05-08 17:31:40 -04:00
|
|
|
|
const fixture = "\udc00\ud800";
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
const bytes = new Uint8Array(8);
|
|
|
|
|
const result = encoder.encodeInto(fixture, bytes);
|
|
|
|
|
assertEquals(result.read, 2);
|
|
|
|
|
assertEquals(result.written, 6);
|
|
|
|
|
// deno-fmt-ignore
|
|
|
|
|
assertEquals(Array.from(bytes), [
|
|
|
|
|
0xef, 0xbf, 0xbd, 0xef, 0xbf, 0xbd, 0x00, 0x00
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function textDecoderSharedUint8Array() {
|
2019-03-15 16:29:54 -04:00
|
|
|
|
const ab = new SharedArrayBuffer(6);
|
|
|
|
|
const dataView = new DataView(ab);
|
|
|
|
|
const charCodeA = "A".charCodeAt(0);
|
|
|
|
|
for (let i = 0; i < ab.byteLength; i++) {
|
|
|
|
|
dataView.setUint8(i, charCodeA + i);
|
|
|
|
|
}
|
|
|
|
|
const ui8 = new Uint8Array(ab);
|
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
|
const actual = decoder.decode(ui8);
|
|
|
|
|
assertEquals(actual, "ABCDEF");
|
|
|
|
|
});
|
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function textDecoderSharedInt32Array() {
|
2019-03-15 16:29:54 -04:00
|
|
|
|
const ab = new SharedArrayBuffer(8);
|
|
|
|
|
const dataView = new DataView(ab);
|
|
|
|
|
const charCodeA = "A".charCodeAt(0);
|
|
|
|
|
for (let i = 0; i < ab.byteLength; i++) {
|
|
|
|
|
dataView.setUint8(i, charCodeA + i);
|
|
|
|
|
}
|
|
|
|
|
const i32 = new Int32Array(ab);
|
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
|
const actual = decoder.decode(i32);
|
|
|
|
|
assertEquals(actual, "ABCDEFGH");
|
|
|
|
|
});
|
2019-04-01 13:05:19 -04:00
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function toStringShouldBeWebCompatibility() {
|
2019-04-01 13:05:19 -04:00
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
assertEquals(encoder.toString(), "[object TextEncoder]");
|
|
|
|
|
|
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
|
assertEquals(decoder.toString(), "[object TextDecoder]");
|
|
|
|
|
});
|
2022-10-24 14:27:22 -04:00
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
|
Deno.test(function textEncoderShouldCoerceToString() {
|
2020-11-01 18:57:18 -05:00
|
|
|
|
const encoder = new TextEncoder();
|
2023-06-26 09:10:27 -04:00
|
|
|
|
const fixtureText = "text";
|
2020-11-01 18:57:18 -05:00
|
|
|
|
const fixture = {
|
|
|
|
|
toString() {
|
2023-06-26 09:10:27 -04:00
|
|
|
|
return fixtureText;
|
2020-11-01 18:57:18 -05:00
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const bytes = encoder.encode(fixture as unknown as string);
|
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
|
const decoded = decoder.decode(bytes);
|
2023-06-26 09:10:27 -04:00
|
|
|
|
assertEquals(decoded, fixtureText);
|
2020-11-01 18:57:18 -05:00
|
|
|
|
});
|
2022-10-24 14:27:22 -04:00
|
|
|
|
|
|
|
|
|
Deno.test(function binaryEncode() {
|
2023-01-24 12:54:10 -05:00
|
|
|
|
// @ts-ignore: Deno[Deno.internal].core allowed
|
2024-02-06 20:16:08 -05:00
|
|
|
|
const core = Deno[Deno.internal].core;
|
2022-10-24 14:27:22 -04:00
|
|
|
|
function asBinaryString(bytes: Uint8Array): string {
|
|
|
|
|
return Array.from(bytes).map(
|
|
|
|
|
(v: number) => String.fromCodePoint(v),
|
|
|
|
|
).join("");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function decodeBinary(binaryString: string) {
|
|
|
|
|
const chars: string[] = Array.from(binaryString);
|
|
|
|
|
return chars.map((v: string): number | undefined => v.codePointAt(0));
|
|
|
|
|
}
|
|
|
|
|
const inputs = [
|
|
|
|
|
"σ😀",
|
|
|
|
|
"Кириллица is Cyrillic",
|
|
|
|
|
"𝓽𝓮𝔁𝓽",
|
|
|
|
|
"lone𝄞\ud888surrogate",
|
|
|
|
|
"\udc00\ud800",
|
|
|
|
|
"\ud800",
|
|
|
|
|
];
|
|
|
|
|
for (const input of inputs) {
|
|
|
|
|
const bytes = new TextEncoder().encode(input);
|
2024-02-06 20:16:08 -05:00
|
|
|
|
const binaryString = core.encodeBinaryString(bytes);
|
2022-10-24 14:27:22 -04:00
|
|
|
|
assertEquals(
|
|
|
|
|
binaryString,
|
|
|
|
|
asBinaryString(bytes),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
assertEquals(Array.from(bytes), decodeBinary(binaryString));
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-11-12 14:47:03 -05:00
|
|
|
|
|
|
|
|
|
Deno.test(
|
|
|
|
|
{ permissions: { read: true } },
|
|
|
|
|
async function textDecoderStreamCleansUpOnCancel() {
|
2023-11-13 13:35:48 -05:00
|
|
|
|
let cancelled = false;
|
|
|
|
|
const readable = new ReadableStream({
|
|
|
|
|
start: (controller) => {
|
|
|
|
|
controller.enqueue(new Uint8Array(12));
|
|
|
|
|
},
|
|
|
|
|
cancel: () => {
|
|
|
|
|
cancelled = true;
|
|
|
|
|
},
|
|
|
|
|
}).pipeThrough(new TextDecoderStream());
|
2023-11-12 14:47:03 -05:00
|
|
|
|
const chunks = [];
|
|
|
|
|
for await (const chunk of readable) {
|
|
|
|
|
chunks.push(chunk);
|
|
|
|
|
// breaking out of the loop prevents normal shutdown at end of async iterator values and triggers the cancel method of the stream instead
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
assertEquals(chunks.length, 1);
|
|
|
|
|
assertEquals(chunks[0].length, 12);
|
2023-11-13 13:35:48 -05:00
|
|
|
|
assertStrictEquals(cancelled, true);
|
2023-11-12 14:47:03 -05:00
|
|
|
|
},
|
|
|
|
|
);
|