ObsidiaHQ / cuid2

A dart implementation of cuid2, the secure, collision-resistant ids optimized for horizontal scaling and performance (v2).
MIT License
6 stars 1 forks source link

Web support #2

Closed heralight closed 1 year ago

heralight commented 1 year ago

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.

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

geommr commented 1 year ago

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.

geommr commented 1 year ago

should be fixed now

srmncnk commented 1 year ago

@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]
geommr commented 1 year ago

@srmncnk weird, it works on scaffolded flutter web app and Platform shouldnt even be invoked. can you give me a minimum reproducible example? or more details about the host?

srmncnk commented 1 year ago

@geommr Sorry, false positive. Works, you can close this.