adraffy / ens-normalize.js

ENSIP-15 in JS
https://adraffy.github.io/ens-normalize.js/test/resolver.html
MIT License
63 stars 17 forks source link

Please polyfill atob function #4

Closed makoto closed 1 year ago

makoto commented 1 year ago

Hi. I am trying to use the library within Google BIg Query Javacript User Defined function but failing due to the lack of atob function in the environment. I tried to polyfill by myself using method here https://stackoverflow.com/questions/44836246/base64-encoding-in-a-bigquery-user-defined-function but doesn't seem to work. Would it be possible to add something like this?

if(typeof atob === 'undefined'){
    function atob(input) {
        var str = String(input).replace(/[=]+$/, ''); // #31: ExtendScript bad parse of /=                                                                                                                                                                                                    
        if (str.length % 4 == 1) {
          throw new InvalidCharacterError("'atob' failed: The string to be decoded is not correctly encoded.");
        }
        for (
          // initialize result and counters                                                                                                                                                                                                                                                   
          var bc = 0, bs, buffer, idx = 0, output = '';
          // get next character                                                                                                                                                                                                                                                               
          buffer = str.charAt(idx++);
          // character found in table? initialize bit storage and add its ascii value;                                                                                                                                                                                                        
          ~buffer && (bs = bc % 4 ? bs * 64 + buffer : buffer,
            // and if not first of each 4 characters,                                                                                                                                                                                                                                         
            // convert the first 8 bits to one ascii character                                                                                                                                                                                                                                
            bc++ % 4) ? output += String.fromCharCode(255 & bs >> (-2 * bc & 6)) : 0
        ) {
          // try to find character in table (0-63, not found => -1)                                                                                                                                                                                                                           
          buffer = chars.indexOf(buffer);
        }
        return output;
    };
} 
adraffy commented 1 year ago

Sure, would a separate dist/ file be sufficient? I was thinking of making a build that also polyfills String.normalize().

In the meantime, if file size doesn't matter, you could try the reference implementation: https://github.com/adraffy/ens-norm-ref-impl.js

makoto commented 1 year ago

Sure, would a separate dist/ file be sufficient? I was thinking of making a build that also polyfills String.normalize().

It would be fine as long as I can npm install and import (I am currently doing something like https://github.com/thedumbterminal/bigquery-js-udf-example/blob/main/src/index.js to make it possible to be accessible within Google BigQuery execution environment.

In the meantime, if file size doesn't matter, you could try the reference implementation: https://github.com/adraffy/ens-norm-ref-impl.js

Looks like 7MB size exceeds 1MB file limit

adraffy commented 1 year ago

I added an atob implementation. I also now include (by default) a NFC implementation to remove another platform dependency.

I will release this with some other changes tomorrow.

Also, just curious: is there anyway to force that importer to use a different include?
I see dist/index.min.js is 1.02 MB, so that won't help in that regard.

makoto commented 1 year ago

I added an atob implementation. I also now include (by default) a NFC implementation to remove another platform dependency.

Great!

Also, just curious: is there anyway to force that importer to use a different include?

What do you mean by this? I am no expert to Google BigQuery but I am guessing 1M is the size limit their platform impose.

adraffy commented 1 year ago

I deployed this change in 1.6.3. Let me know if there are any issues.

What do you mean by this? I am no expert to Google BigQuery but I am guessing 1M is the size limit their platform impose.

I probably had my npm stuff setup incorrectly anyway (I was including too much stuff) but I know applications will use the "browser" import from the package.json, instead of following "main". But in ref-impl's case, the minified version was still over the 1MB limit.