1- // Copyright (c) 2024 , Sudipto Chandra
1+ // Copyright (c) 2025 , Sudipto Chandra
22// All rights reserved. Check LICENSE file for details.
33
44import 'dart:math' show Random;
5- import 'dart:js_interop' ;
65
7- const int _mask32 = 0xFFFFFFFF ;
8-
9- const bool isDart2JS = bool .fromEnvironment ('dart.tool.dart2js' );
10-
11- @JS ()
12- @staticInterop
13- class Process {}
14-
15- @JS ()
16- @staticInterop
17- class Versions {}
18-
19- @JS ('process' )
20- external Process ? get _process;
21-
22- extension on Process {
23- external Versions ? get versions;
24- }
6+ import 'generator_js_legacy.dart'
7+ if (dart.library.js_interop) 'generator_js_interop.dart' ;
258
26- extension on Versions {
27- external JSAny get node;
28- }
29-
30- bool get isNodeDart2JS => _process? .versions? .node != null && isDart2JS;
31-
32- @JS ()
33- @staticInterop
34- class Crypto {}
35-
36- extension on Crypto {
37- external int randomInt (final int max);
38- }
39-
40- @JS ()
41- external Crypto require (final String id);
9+ const int _mask32 = 0xFFFFFFFF ;
4210
4311/// For Node.js environment + dart2js compiler
44- class NodeRandom implements Random {
12+ class NodeRandom extends CryptoRandom implements Random {
4513 @override
4614 int nextInt (final int max) {
4715 if (max < 1 || max > _mask32 + 1 ) {
4816 throw RangeError .range (
4917 max, 1 , _mask32 + 1 , 'max' , 'max must be <= (1 << 32)' );
5018 }
51- return require ( 'crypto' ). randomInt (max);
19+ return cryptoRandomInt (max);
5220 }
5321
5422 @override
@@ -64,7 +32,7 @@ class NodeRandom implements Random {
6432}
6533
6634/// Returns a secure random generator in JS runtime
67- Random secureRandom () => isNodeDart2JS ? NodeRandom () : Random . secure ();
35+ Random secureRandom () => NodeRandom ();
6836
6937/// Generates a random seed
7038int $generateSeed () => secureRandom ().nextInt (_mask32);
0 commit comments