Web support
Closed this issue · 5 comments
Hi,
Thank you for your package, very helpful.
I tried to include it inside a mobile/web project but I had an incompatibility with the code for the web platform.
The problem
_createFingerprint() inside cuid2_base.dart use Platform.localHostname; and pid.
- dart:io and Platform.localHostname, I successfully replace it with universal_io
- but I'm concern about the pid that I cannot simulate in a distributed system for a browser.
do you have any recommendation or sample for web integration please ?
Do you think that replace it for web by a Math.random is enough like said that inside: https://github.com/paralleldrive/cuid2/blob/6c64c5d118885eaef9e7594de4416dc4945e3460/src/index.js#L50
a simple fix will in cuid2_base.dart:
import 'package:universal_io/io.dart';
...
String _createFingerprint() {
final hostname = Platform.localHostname;
final version = Platform.operatingSystemVersion;
final os = Platform.operatingSystem;
seed = kIsWeb ? (_random.nextDouble() * 36).floor() : pid;
final fingerprint = '${_pad(seed.toRadixString(36), 3)}$hostname$version$os';
final entropy = _createEntropy(length: _entropyLength);
final sourceString = '$fingerprint$entropy';
return _hash(sourceString).substring(0, _entropyLength);
}
and with that the web platform will supported.
Best regards,
Alexandre
Hey, thanks for reporting this. Yes your workaround should work fine, its just one source of entropy from many.
I got a working solution for web without adding 3rd party dependencies using dart:html but I got into (unrelated) issues that are preventing tests from running. I'll try to publish the new version by the end of the week.
should be fixed now
@geommr This is still not working. Actually the problem is in touching anything relating to Platform in web. See the stack trace below:
Error: Unsupported operation: Platform._localHostname
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 266:49 throw_
dart-sdk/lib/_internal/js_dev_runtime/patch/io_patch.dart 254:5 _localHostname
dart-sdk/lib/io/platform_impl.dart 72:18 get localHostname
dart-sdk/lib/io/platform.dart 66:43 get _localHostname
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 872:8 get
dart-sdk/lib/io/platform.dart 129:38 get localHostname
packages/cuid2/src/cuid2_base.dart 93:31 [_createFingerprint]