import { TextDecoder as BaseTextDecoder } from "util" ; import { DOMException } from "@miniflare/core" ; export class TextDecoder extends BaseTextDecoder { constructor ( encoding ? , options ? ) { const validEncoding = encoding === undefined || encoding === "utf-8" || encoding === "utf8" || encoding === "unicode-1-1-utf-8" ; if ( ! validEncoding ) { throw new RangeError ( "TextDecoder only supports utf-8 encoding" ) ; } super ( encoding , options ) ; } } export function btoa ( input ) { input = `${ input }` ; for ( let n = 0 ; n < input . length ; n ++ ) { if ( input [ n ] . charCodeAt ( 0 ) > 0xff ) { throw new DOMException ( "Invalid character" , "InvalidCharacterError" ) ; } } return Buffer . from ( input , "latin1" ) . toString ( "base64" ) ; } const BASE_64_DIGITS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=" ; export function atob ( input ) { input = `${ input }` . replace ( /[\t\n\f\r ]+/g , "" ) ; for ( let n = 0 ; n < input . length ; n ++ ) { if ( ! BASE_64_DIGITS . includes ( input [ n ] ) ) { throw new DOMException ( "Invalid character" , "InvalidCharacterError" ) ; } } return Buffer . from ( input , "base64" ) . toString ( "latin1" ) ; }