From 7f421f01c58570b20860caea7ae502dbdef4373a Mon Sep 17 00:00:00 2001 From: Alexande B Date: Thu, 29 Feb 2024 22:29:50 +0100 Subject: [PATCH] feat: migrate to ES5 compatible loader --- sdk/build.gradle | 18 ++++++---- sdk/src/main/html/hcaptcha.html | 33 +++++++++---------- .../hcaptcha/sdk/HCaptchaWebViewHelper.java | 17 ++++++---- .../sdk/HCaptchaWebViewHelperTest.java | 19 +++++++++-- 4 files changed, 56 insertions(+), 31 deletions(-) diff --git a/sdk/build.gradle b/sdk/build.gradle index 9dc1750..2fab4a8 100644 --- a/sdk/build.gradle +++ b/sdk/build.gradle @@ -11,7 +11,7 @@ plugins { } ext { - hcaptchaLoaderVersion = "1.1.3" + hcaptchaLoaderVersion = "1.2.3" } android { @@ -117,14 +117,20 @@ project.afterEvaluate { } } -long MAX_AAR_SIZE_KB = 200 +long MAX_AAR_SIZE_KB = 250 -tasks.register('downloadHCaptchaLoaderJs', Download) { - src "https://www.unpkg.com/@hcaptcha/loader@${hcaptchaLoaderVersion}/dist/index.mjs" - dest layout.buildDirectory.file("generated/assets/hcaptcha/loader.mjs") +tasks.register('downloadPolyfillsJs', Download) { + src "https://www.unpkg.com/@hcaptcha/loader@${hcaptchaLoaderVersion}/dist/polyfills.js" + dest layout.buildDirectory.file("generated/assets/hcaptcha/polyfills.js") onlyIfModified true } +tasks.register('downloadHCaptchaLoaderJs', Download) { + src "https://www.unpkg.com/@hcaptcha/loader@${hcaptchaLoaderVersion}/dist/index.es5.js" + dest layout.buildDirectory.file("generated/assets/hcaptcha/loader.js") + onlyIfModified true +}.get().dependsOn('downloadPolyfillsJs') + android.sourceSets.main.assets.srcDirs += [layout.buildDirectory.file("generated/assets")] android.libraryVariants.all { variant -> @@ -150,7 +156,7 @@ android.libraryVariants.all { variant -> var aarFile = variant.packageLibraryProvider.get().archiveFile.get().getAsFile() long aarSizeKb = aarFile.length() / 1024 if (aarSizeKb > MAX_AAR_SIZE_KB) { - throw new GradleException("${aarPath} size exceeded! ${aarSizeKb}Kbyte > ${MAX_AAR_SIZE_KB}Kbyte") + throw new GradleException("${aarFile} size exceeded! ${aarSizeKb}Kbyte > ${MAX_AAR_SIZE_KB}Kbyte") } } }) diff --git a/sdk/src/main/html/hcaptcha.html b/sdk/src/main/html/hcaptcha.html index 99d15c8..a200fc2 100644 --- a/sdk/src/main/html/hcaptcha.html +++ b/sdk/src/main/html/hcaptcha.html @@ -36,9 +36,9 @@ window.sysDebug = JSON.parse(window.JSDI.getSysDebug()); } - + + diff --git a/sdk/src/main/java/com/hcaptcha/sdk/HCaptchaWebViewHelper.java b/sdk/src/main/java/com/hcaptcha/sdk/HCaptchaWebViewHelper.java index 3dcfbd7..c2819aa 100644 --- a/sdk/src/main/java/com/hcaptcha/sdk/HCaptchaWebViewHelper.java +++ b/sdk/src/main/java/com/hcaptcha/sdk/HCaptchaWebViewHelper.java @@ -24,6 +24,8 @@ import java.io.IOException; import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import java.util.Objects; final class HCaptchaWebViewHelper { @@ -124,15 +126,17 @@ public boolean shouldRetry(HCaptchaException exception) { @RequiresApi(Build.VERSION_CODES.LOLLIPOP) private class HCaptchaWebClient extends WebViewClient { - private final Uri loaderUri = Uri.parse( - "https://www.unpkg.com/@hcaptcha/loader@" + BuildConfig.LOADER_VERSION + "/dist/index.mjs" - ); + private final Map assetsCache = new HashMap<>(); @NonNull private final Handler handler; HCaptchaWebClient(@NonNull Handler handler) { this.handler = handler; + + final String baseUrl = "https://unpkg.com/@hcaptcha/loader@" + BuildConfig.LOADER_VERSION + "/dist"; + assetsCache.put(Uri.parse(baseUrl + "/index.es5.js"), "hcaptcha/loader.js"); + assetsCache.put(Uri.parse(baseUrl + "/polyfills.js"), "hcaptcha/polyfills.js"); } private String stripUrl(String url) { @@ -142,7 +146,8 @@ private String stripUrl(String url) { @Override public WebResourceResponse shouldInterceptRequest (final WebView view, final WebResourceRequest request) { final Uri requestUri = request.getUrl(); - if (loaderUri.equals(requestUri)) { + final String assetPath = assetsCache.get(requestUri); + if (assetPath != null) { try { return new WebResourceResponse( "application/javascript", @@ -151,10 +156,10 @@ public WebResourceResponse shouldInterceptRequest (final WebView view, final Web "OK", Collections.singletonMap("Access-Control-Allow-Origin", Objects.toString(config.getHost(), "null")), - view.getContext().getAssets().open("hcaptcha/loader.mjs") + view.getContext().getAssets().open(assetPath) ); } catch (IOException e) { - HCaptchaLog.w("WebViewHelper wasn't able to load loader.mjs from assets"); + HCaptchaLog.w("WebViewHelper wasn't able to load " + assetPath + " from assets"); } } else if (requestUri != null && requestUri.getScheme() != null && requestUri.getScheme().equals("http")) { handler.post(() -> { diff --git a/test/src/androidTest/java/com/hcaptcha/sdk/HCaptchaWebViewHelperTest.java b/test/src/androidTest/java/com/hcaptcha/sdk/HCaptchaWebViewHelperTest.java index db21482..259ff4b 100644 --- a/test/src/androidTest/java/com/hcaptcha/sdk/HCaptchaWebViewHelperTest.java +++ b/test/src/androidTest/java/com/hcaptcha/sdk/HCaptchaWebViewHelperTest.java @@ -7,11 +7,13 @@ import static org.mockito.Mockito.mock; import android.content.Context; +import android.content.res.AssetManager; import android.app.Activity; import android.os.Handler; import android.os.Looper; import androidx.test.core.app.ActivityScenario; +import androidx.test.core.app.ApplicationProvider; import androidx.test.ext.junit.rules.ActivityScenarioRule; import androidx.test.platform.app.InstrumentationRegistry; @@ -22,6 +24,7 @@ import org.junit.Test; import java.io.IOException; +import java.io.InputStream; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -95,7 +98,19 @@ public void onFailure(HCaptchaException e) { @Test public void testLoaderJsAssetPresence() throws IOException { - Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); - assertNotNull(appContext.getAssets().open("hcaptcha/loader.mjs")); + AssetManager assets = InstrumentationRegistry + .getInstrumentation() + .getTargetContext() + .getAssets(); + assertNotNull(assets.open("hcaptcha/loader.js")); + } + + @Test + public void testPolyfillsJsAssetPresence() throws IOException { + AssetManager assets = InstrumentationRegistry + .getInstrumentation() + .getTargetContext() + .getAssets(); + assertNotNull(assets.open("hcaptcha/polyfills.js")); } }