dankogai / js-base64

Base64 implementation for JavaScript
BSD 3-Clause "New" or "Revised" License
4.27k stars 1.33k forks source link

Caching the result of atob existence is causing web worker to crash #174

Closed Itaypa closed 8 months ago

Itaypa commented 8 months ago

Hi,

Awesome library, I use it in all my projects.

recently I used this library in a web app that launches a web worker, the issue occurred when _hasatob is calculated when app is loaded on the main thread (while atob) exists and when running the web worker it mistakenly fails because it does not exist.

by not caching the result it solves the issue

dankogai commented 8 months ago

Thank you!

I was a little puzzled with:

--- a/base64.js
+++ b/base64.js
@@ -42,8 +42,8 @@
      * @deprecated use lowercase `version`.
      */
     var VERSION = version;
-    var _hasatob = typeof atob === 'function';
-    var _hasbtoa = typeof btoa === 'function';
+    var typeof atob === 'function' = typeof atob === 'function';
+    var typeof btoa === 'function' = typeof btoa === 'function';
     var _hasBuffer = typeof Buffer === 'function';
     var _TD = typeof TextDecoder === 'function' ? new TextDecoder() : undefined

But It was gone after npm test which rebuilds .js and .mjs from *.ts.