paralleldrive / cuid

Collision-resistant ids optimized for horizontal scaling and performance.
Other
3.44k stars 123 forks source link

support web workers #65

Closed jampy closed 6 years ago

jampy commented 8 years ago

Please add support for web workers.

cuid() currently throws an exception in web workers since navigator.mimeTypes is not available in that context.

Generally, for the fingerprint I suggest to collect various strings as a whole, gracefully handling errors, and then create a fingerprint out of that (arbitrary length) string using some hash function - like MD5, but perhaps something more lightweight.

Something in this direction:

api.fingerprint = function browserPrint() {

  var things = [];

  try {
    for (var i = 0; i < navigator.mimeTypes.length; i++) {
      things.push(navigator.mimeTypes[i]);
    }
  } catch (e) {
    // ignore errors
  }

  try {
    things.push(navigator.userAgent);
  } catch (e) {
    // ignore errors
  }

  // maybe add some more...

  things.push(api.globalCount());

  return someHashFunction(things.join(""));
};

I think you get the idea...

ericelliott commented 8 years ago

I get the idea. Willing to look at a PR. I don't like the idea of using anything but the most rudimentary fast hashing algorithm (like, something that could be implemented in less than 10 LOC). cuid should remain a tiny dependency.

Also, I'd prefer not to use try/catch. We can check for feature availability without slowing things down with error catching.

While we're at it, we could use feature detection to eliminate the separate build for Node. Game on!

jampy commented 8 years ago

Okay, will think about a PR once I get some time perhaps (at the moment I'm using shortid)

As for the try/catch: AFAIK the fingerprint() function is only executed once, so I don't think speed is relevant.

ericelliott commented 8 years ago

Good point.

ericelliott commented 6 years ago

This should be fixed now. Reopen if you still have trouble.