2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 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" />
|
|
|
|
|
2024-01-26 17:46:46 -05:00
|
|
|
import { primordials } from "ext:core/mod.js";
|
|
|
|
import { op_base64_atob, op_base64_btoa } from "ext:core/ops";
|
2023-02-07 14:22:46 -05:00
|
|
|
const {
|
|
|
|
ObjectPrototypeIsPrototypeOf,
|
|
|
|
TypeErrorPrototype,
|
|
|
|
} = primordials;
|
2021-06-05 17:10:07 -04:00
|
|
|
|
2024-01-10 17:37:25 -05:00
|
|
|
import * as webidl from "ext:deno_webidl/00_webidl.js";
|
2024-03-22 12:21:05 -04:00
|
|
|
import { DOMException } from "./01_dom_exception.js";
|
2024-01-10 17:37:25 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/**
|
|
|
|
* @param {string} data
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function atob(data) {
|
|
|
|
const prefix = "Failed to execute 'atob'";
|
2023-04-12 15:58:57 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 1, prefix);
|
2023-05-01 06:47:13 -04:00
|
|
|
data = webidl.converters.DOMString(data, prefix, "Argument 1");
|
2023-02-07 14:22:46 -05:00
|
|
|
try {
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_base64_atob(data);
|
2023-02-07 14:22:46 -05:00
|
|
|
} catch (e) {
|
|
|
|
if (ObjectPrototypeIsPrototypeOf(TypeErrorPrototype, e)) {
|
|
|
|
throw new DOMException(
|
|
|
|
"Failed to decode base64: invalid character",
|
|
|
|
"InvalidCharacterError",
|
|
|
|
);
|
2022-03-05 14:12:30 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
throw e;
|
2021-06-05 17:10:07 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-06-05 17:10:07 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
/**
|
|
|
|
* @param {string} data
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
function btoa(data) {
|
|
|
|
const prefix = "Failed to execute 'btoa'";
|
2023-04-12 15:58:57 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 1, prefix);
|
2023-05-01 06:47:13 -04:00
|
|
|
data = webidl.converters.DOMString(data, prefix, "Argument 1");
|
2023-02-07 14:22:46 -05:00
|
|
|
try {
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_base64_btoa(data);
|
2023-02-07 14:22:46 -05:00
|
|
|
} catch (e) {
|
|
|
|
if (ObjectPrototypeIsPrototypeOf(TypeErrorPrototype, e)) {
|
|
|
|
throw new DOMException(
|
2024-12-02 22:30:39 -05:00
|
|
|
"Cannot encode string: string contains characters outside of the Latin1 range",
|
2023-02-07 14:22:46 -05:00
|
|
|
"InvalidCharacterError",
|
|
|
|
);
|
2022-03-05 14:12:30 -05:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
throw e;
|
2021-06-05 17:10:07 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
2021-06-05 17:10:07 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
export { atob, btoa };
|