Skip to content

Commit a41026c

Browse files
committed
Support Dart v2.19 for NodeRandom
1 parent a6a6b22 commit a41026c

File tree

7 files changed

+83
-47
lines changed

7 files changed

+83
-47
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
.packages
55

66
# Conventional directory for outputs.
7+
dart-*/
78
build/
89
doc/
910
coverage/

.pubignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
/build
66
/scripts
77
/.github
8+
/dart-*

CHANGELOG.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
# 2.3.0
22

33
- generator_vm.dart
4-
- Avoid mixing predictable datetime output with CSPRNG output.
4+
- Avoids mixing predictable datetime output with CSPRNG output.
55
- generator_js.dart
6-
- Avoid mixing predictable datetime output with CSPRNG output.
7-
- Added `NodeRandom` as an implementation of `Random` for NodeJS.
8-
- `nextInt(max)` draws 32-bit values from Node crypto, supports max ∈ [1, 2^32].
9-
- `secureRandom()` uses `Random.secure()` for web browsers, and `NodeRandom` for Node.js.
6+
- Avoids mixing predictable datetime output with CSPRNG output.
7+
- Adds `NodeRandom` as an implementation of `Random` for NodeJS.
8+
- `nextInt(max)` draws 32-bit values from NodeJS's internal `crypto` package.
9+
- `secureRandom()` uses `NodeRandom` for secure random numbers.
1010
- `$generateSeed()` delegates to `secureRandom()`.
1111
- generators.dart
12-
- Fixed spelling of `_hashGenerator`
12+
- Fixes spelling of `_hashGenerator`
1313

1414
# 2.2.0
1515

lib/src/random/generator_js.dart

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,22 @@
1-
// Copyright (c) 2024, Sudipto Chandra
1+
// Copyright (c) 2025, Sudipto Chandra
22
// All rights reserved. Check LICENSE file for details.
33

44
import '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
7038
int $generateSeed() => secureRandom().nextInt(_mask32);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2025, Sudipto Chandra
2+
// All rights reserved. Check LICENSE file for details.
3+
4+
import 'dart:js_interop';
5+
6+
@JS()
7+
@staticInterop
8+
class Process {}
9+
10+
@JS()
11+
@staticInterop
12+
class Versions {}
13+
14+
@JS()
15+
@staticInterop
16+
class Crypto {}
17+
18+
extension on Crypto {
19+
external int randomInt(final int max);
20+
}
21+
22+
@JS('require')
23+
external Crypto require(final String id);
24+
25+
/// For Node.js environment + dart2js compiler
26+
abstract class CryptoRandom {
27+
Crypto? _crypto;
28+
29+
int cryptoRandomInt(final int max) {
30+
_crypto ??= require('crypto');
31+
return _crypto!.randomInt(max);
32+
}
33+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) 2025, Sudipto Chandra
2+
// All rights reserved. Check LICENSE file for details.
3+
4+
// Dart v2.19 compatible version
5+
// Works with dart2js in a Node.js runtime (where `require` exists).
6+
7+
// ignore_for_file: deprecated_member_use, depend_on_referenced_packages
8+
9+
import 'dart:js' as js;
10+
11+
js.JsObject _require(String id) {
12+
final r = js.context['require'];
13+
if (r == null) {
14+
throw StateError("`require` is not available.");
15+
}
16+
return (r as js.JsFunction).apply([id]) as js.JsObject;
17+
}
18+
19+
/// For Node.js environment + dart2js compiler
20+
abstract class CryptoRandom {
21+
js.JsObject? _crypto;
22+
js.JsFunction? _randomInt;
23+
24+
int cryptoRandomInt(final int max) {
25+
_crypto ??= _require('crypto');
26+
_randomInt ??= _crypto!['randomInt'] as js.JsFunction;
27+
return (_randomInt!.apply([max]) as num).toInt();
28+
}
29+
}

lib/src/random/generators.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ import 'package:hashlib/src/algorithms/sm3.dart';
1111
import 'package:hashlib/src/algorithms/xxh64/xxh64.dart';
1212
import 'package:hashlib/src/core/hash_base.dart';
1313

14-
import 'generator_vm.dart' if (dart.library.js) 'generator_js.dart';
15-
export 'generator_vm.dart' if (dart.library.js) 'generator_js.dart';
14+
import 'generator_vm.dart'
15+
if (dart.library.html) 'generator_vm.dart'
16+
if (dart.library.js) 'generator_js.dart';
17+
export 'generator_vm.dart'
18+
if (dart.library.html) 'generator_vm.dart'
19+
if (dart.library.js) 'generator_js.dart';
1620

1721
const int _mask32 = 0xFFFFFFFF;
1822

0 commit comments

Comments
 (0)