1 | "use strict";
|
2 |
|
3 | const DOMException = require("domexception");
|
4 |
|
5 | module.exports = {
|
6 | btoa,
|
7 | atob,
|
8 | };
|
9 |
|
10 | function atob(...args) {
|
11 | if (args.length === 0) throw new TypeError("Failed to execute 'atob' on 'Window': 1 argument required, but only 0 present.");
|
12 |
|
13 | const encodedData = "" + args[0];
|
14 |
|
15 | if (typeof encodedData !== "string" || encodedData.length % 4 !== 0) throw new DOMException("The string to be decoded is not correctly encoded.");
|
16 | return Buffer.from(encodedData, "base64").toString("latin1");
|
17 | }
|
18 |
|
19 | function btoa(...args) {
|
20 | if (args.length === 0) throw new TypeError("Failed to execute 'btoa' on 'Window': 1 argument required, but only 0 present.");
|
21 |
|
22 | const stringToEncode = "" + args[0];
|
23 |
|
24 | if (/[^\u0000-\u00FF]+/.test(stringToEncode.toString())) throw new DOMException("The string to be encoded contains characters outside of the Latin1 range.");
|
25 |
|
26 | return Buffer.from(stringToEncode, "latin1").toString("base64");
|
27 | }
|