diff options
Diffstat (limited to 'ext/web/05_base64.js')
-rw-r--r-- | ext/web/05_base64.js | 58 |
1 files changed, 24 insertions, 34 deletions
diff --git a/ext/web/05_base64.js b/ext/web/05_base64.js index 1244ecfd5..8238831f8 100644 --- a/ext/web/05_base64.js +++ b/ext/web/05_base64.js @@ -9,21 +9,10 @@ "use strict"; ((window) => { + const core = Deno.core; const webidl = window.__bootstrap.webidl; - const { - forgivingBase64Encode, - forgivingBase64Decode, - } = window.__bootstrap.infra; const { DOMException } = window.__bootstrap.domException; - const { - ArrayPrototypeMap, - StringPrototypeCharCodeAt, - ArrayPrototypeJoin, - SafeArrayIterator, - StringFromCharCode, - TypedArrayFrom, - Uint8Array, - } = window.__bootstrap.primordials; + const { TypeError } = window.__bootstrap.primordials; /** * @param {string} data @@ -36,13 +25,17 @@ prefix, context: "Argument 1", }); - - const uint8Array = forgivingBase64Decode(data); - const result = ArrayPrototypeMap( - [...new SafeArrayIterator(uint8Array)], - (byte) => StringFromCharCode(byte), - ); - return ArrayPrototypeJoin(result, ""); + try { + return core.opSync("op_base64_atob", data); + } catch (e) { + if (e instanceof TypeError) { + throw new DOMException( + "Failed to decode base64: invalid character", + "InvalidCharacterError", + ); + } + throw e; + } } /** @@ -56,20 +49,17 @@ prefix, context: "Argument 1", }); - const byteArray = ArrayPrototypeMap( - [...new SafeArrayIterator(data)], - (char) => { - const charCode = StringPrototypeCharCodeAt(char, 0); - if (charCode > 0xff) { - throw new DOMException( - "The string to be encoded contains characters outside of the Latin1 range.", - "InvalidCharacterError", - ); - } - return charCode; - }, - ); - return forgivingBase64Encode(TypedArrayFrom(Uint8Array, byteArray)); + try { + return core.opSync("op_base64_btoa", data); + } catch (e) { + if (e instanceof TypeError) { + throw new DOMException( + "The string to be encoded contains characters outside of the Latin1 range.", + "InvalidCharacterError", + ); + } + throw e; + } } window.__bootstrap.base64 = { |