From 7c8b37facf64a26292e0a20c82d6331adf12c962 Mon Sep 17 00:00:00 2001 From: Nogringo Date: Sun, 11 Oct 2026 17:23:27 +0200 Subject: [PATCH] fix: read browser globals from globalThis so web workers work --- cryptography/lib/src/browser/_javascript_bindings.dart | 6 +++--- cryptography/lib/src/cryptography/secret_key.dart | 2 +- cryptography/lib/src/helpers/random_bytes_impl_browser.dart | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cryptography/lib/src/browser/_javascript_bindings.dart b/cryptography/lib/src/browser/_javascript_bindings.dart index 95caede7..c244c48b 100644 --- a/cryptography/lib/src/browser/_javascript_bindings.dart +++ b/cryptography/lib/src/browser/_javascript_bindings.dart @@ -24,10 +24,10 @@ import 'package:meta/meta.dart'; /// Note that browsers support Web Cryptography only in secure contexts. @internal final bool isWebCryptoAvailable = - _subtle.isDefinedAndNotNull && _isSecureContext.toDart; + _subtle.isDefinedAndNotNull && (_isSecureContext?.toDart ?? false); -@JS('window.isSecureContext') -external JSBoolean get _isSecureContext; +@JS('isSecureContext') +external JSBoolean? get _isSecureContext; @JS('crypto.subtle') external JSAny get _subtle; diff --git a/cryptography/lib/src/cryptography/secret_key.dart b/cryptography/lib/src/cryptography/secret_key.dart index 88f5264e..ab085e06 100644 --- a/cryptography/lib/src/cryptography/secret_key.dart +++ b/cryptography/lib/src/cryptography/secret_key.dart @@ -106,7 +106,7 @@ class SecretKeyData extends SecretKey { /// Generates _N_ random bytes. /// /// A description of the random number generator: - /// * In browsers, `window.crypto.getRandomValues() is used directly. + /// * In browsers, `crypto.getRandomValues()` is used directly. /// * In other platforms, _dart:math_ [Random.secure()] is used. /// /// You can give a custom random number generator. This can be useful for diff --git a/cryptography/lib/src/helpers/random_bytes_impl_browser.dart b/cryptography/lib/src/helpers/random_bytes_impl_browser.dart index d1bde10b..9bd850ff 100644 --- a/cryptography/lib/src/helpers/random_bytes_impl_browser.dart +++ b/cryptography/lib/src/helpers/random_bytes_impl_browser.dart @@ -51,5 +51,5 @@ void fillBytesWithSecureRandom(Uint8List bytes, {Random? random}) { } } -@JS('window.crypto.getRandomValues') +@JS('crypto.getRandomValues') external void _webCryptoRandom(JSUint8Array buffer);