UNPKG

973 BJavaScriptView Raw
1"use strict";
2
3const DOMException = require("domexception");
4
5module.exports = {
6 btoa,
7 atob,
8};
9
10function 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]; // eslint-disable-line prefer-template
14
15 if (typeof encodedData !== "string") throw new DOMException("The string to be decoded is not correctly encoded.");
16 return Buffer.from(encodedData, "base64").toString("latin1");
17}
18
19function 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]; // eslint-disable-line prefer-template
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}