2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2021-06-05 17:10:07 -04:00
|
|
|
|
|
|
|
// @ts-check
|
2021-07-06 08:38:12 -04:00
|
|
|
/// <reference path="../../core/internal.d.ts" />
|
2021-06-05 17:10:07 -04:00
|
|
|
/// <reference path="../webidl/internal.d.ts" />
|
|
|
|
/// <reference path="../web/internal.d.ts" />
|
|
|
|
/// <reference lib="esnext" />
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
((window) => {
|
2022-03-05 14:12:30 -05:00
|
|
|
const core = Deno.core;
|
2022-08-11 09:56:56 -04:00
|
|
|
const ops = core.ops;
|
2021-06-05 17:10:07 -04:00
|
|
|
const webidl = window.__bootstrap.webidl;
|
2021-07-03 15:32:28 -04:00
|
|
|
const { DOMException } = window.__bootstrap.domException;
|
2022-12-19 21:37:50 -05:00
|
|
|
const {
|
|
|
|
ObjectPrototypeIsPrototypeOf,
|
|
|
|
TypeErrorPrototype,
|
|
|
|
} = window.__bootstrap.primordials;
|
2021-06-05 17:10:07 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} data
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function atob(data) {
|
2022-01-11 11:31:13 -05:00
|
|
|
const prefix = "Failed to execute 'atob'";
|
|
|
|
webidl.requiredArguments(arguments.length, 1, { prefix });
|
2021-06-05 17:10:07 -04:00
|
|
|
data = webidl.converters.DOMString(data, {
|
2022-01-11 11:31:13 -05:00
|
|
|
prefix,
|
2021-06-05 17:10:07 -04:00
|
|
|
context: "Argument 1",
|
|
|
|
});
|
2022-03-05 14:12:30 -05:00
|
|
|
try {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_base64_atob(data);
|
2022-03-05 14:12:30 -05:00
|
|
|
} catch (e) {
|
2022-12-19 21:37:50 -05:00
|
|
|
if (ObjectPrototypeIsPrototypeOf(TypeErrorPrototype, e)) {
|
2022-03-05 14:12:30 -05:00
|
|
|
throw new DOMException(
|
|
|
|
"Failed to decode base64: invalid character",
|
|
|
|
"InvalidCharacterError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
}
|
2021-06-05 17:10:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} data
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function btoa(data) {
|
|
|
|
const prefix = "Failed to execute 'btoa'";
|
|
|
|
webidl.requiredArguments(arguments.length, 1, { prefix });
|
|
|
|
data = webidl.converters.DOMString(data, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 1",
|
|
|
|
});
|
2022-03-05 14:12:30 -05:00
|
|
|
try {
|
2022-08-11 09:56:56 -04:00
|
|
|
return ops.op_base64_btoa(data);
|
2022-03-05 14:12:30 -05:00
|
|
|
} catch (e) {
|
2022-12-19 21:37:50 -05:00
|
|
|
if (ObjectPrototypeIsPrototypeOf(TypeErrorPrototype, e)) {
|
2022-03-05 14:12:30 -05:00
|
|
|
throw new DOMException(
|
|
|
|
"The string to be encoded contains characters outside of the Latin1 range.",
|
|
|
|
"InvalidCharacterError",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
}
|
2021-06-05 17:10:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
window.__bootstrap.base64 = {
|
|
|
|
atob,
|
|
|
|
btoa,
|
|
|
|
};
|
|
|
|
})(globalThis);
|